blob: 0702f521ec2df02ef6af9ddcc28ff68b196b0aae [file] [log] [blame]
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjmedia-codec/ipp_codecs.h>
21#include <pjmedia/codec.h>
22#include <pjmedia/errno.h>
23#include <pjmedia/endpoint.h>
24#include <pjmedia/plc.h>
25#include <pjmedia/port.h>
26#include <pjmedia/silencedet.h>
27#include <pj/assert.h>
28#include <pj/log.h>
Nanang Izzuddin0b9da642009-06-02 16:28:24 +000029#include <pj/math.h>
Nanang Izzuddin493a8db2008-08-15 13:17:39 +000030#include <pj/pool.h>
31#include <pj/string.h>
32#include <pj/os.h>
33
Nanang Izzuddin23a00b72008-08-25 13:58:25 +000034
Nanang Izzuddin493a8db2008-08-15 13:17:39 +000035/*
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +000036 * Only build this file if PJMEDIA_HAS_INTEL_IPP != 0
Nanang Izzuddin493a8db2008-08-15 13:17:39 +000037 */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +000038#if defined(PJMEDIA_HAS_INTEL_IPP) && PJMEDIA_HAS_INTEL_IPP != 0
Nanang Izzuddin493a8db2008-08-15 13:17:39 +000039
40#include <usc.h>
Benny Prijonoe1e6d512009-01-02 15:17:47 +000041#include <ippversion.h>
Nanang Izzuddin493a8db2008-08-15 13:17:39 +000042
43#define THIS_FILE "ipp_codecs.c"
44
45/* Prototypes for IPP codecs factory */
46static pj_status_t ipp_test_alloc( pjmedia_codec_factory *factory,
47 const pjmedia_codec_info *id );
48static pj_status_t ipp_default_attr( pjmedia_codec_factory *factory,
49 const pjmedia_codec_info *id,
50 pjmedia_codec_param *attr );
51static pj_status_t ipp_enum_codecs( pjmedia_codec_factory *factory,
52 unsigned *count,
53 pjmedia_codec_info codecs[]);
54static pj_status_t ipp_alloc_codec( pjmedia_codec_factory *factory,
55 const pjmedia_codec_info *id,
56 pjmedia_codec **p_codec);
57static pj_status_t ipp_dealloc_codec( pjmedia_codec_factory *factory,
58 pjmedia_codec *codec );
59
60/* Prototypes for IPP codecs implementation. */
61static pj_status_t ipp_codec_init( pjmedia_codec *codec,
62 pj_pool_t *pool );
63static pj_status_t ipp_codec_open( pjmedia_codec *codec,
64 pjmedia_codec_param *attr );
65static pj_status_t ipp_codec_close( pjmedia_codec *codec );
66static pj_status_t ipp_codec_modify(pjmedia_codec *codec,
67 const pjmedia_codec_param *attr );
68static pj_status_t ipp_codec_parse( pjmedia_codec *codec,
69 void *pkt,
70 pj_size_t pkt_size,
71 const pj_timestamp *ts,
72 unsigned *frame_cnt,
73 pjmedia_frame frames[]);
74static pj_status_t ipp_codec_encode( pjmedia_codec *codec,
75 const struct pjmedia_frame *input,
76 unsigned output_buf_len,
77 struct pjmedia_frame *output);
78static pj_status_t ipp_codec_decode( pjmedia_codec *codec,
79 const struct pjmedia_frame *input,
80 unsigned output_buf_len,
81 struct pjmedia_frame *output);
82static pj_status_t ipp_codec_recover(pjmedia_codec *codec,
83 unsigned output_buf_len,
84 struct pjmedia_frame *output);
85
86/* Definition for IPP codecs operations. */
87static pjmedia_codec_op ipp_op =
88{
89 &ipp_codec_init,
90 &ipp_codec_open,
91 &ipp_codec_close,
92 &ipp_codec_modify,
93 &ipp_codec_parse,
94 &ipp_codec_encode,
95 &ipp_codec_decode,
96 &ipp_codec_recover
97};
98
99/* Definition for IPP codecs factory operations. */
100static pjmedia_codec_factory_op ipp_factory_op =
101{
102 &ipp_test_alloc,
103 &ipp_default_attr,
104 &ipp_enum_codecs,
105 &ipp_alloc_codec,
106 &ipp_dealloc_codec
107};
108
109/* IPP codecs factory */
110static struct ipp_factory {
111 pjmedia_codec_factory base;
112 pjmedia_endpt *endpt;
113 pj_pool_t *pool;
114 pj_mutex_t *mutex;
115} ipp_factory;
116
117/* IPP codecs private data. */
118typedef struct ipp_private {
119 int codec_idx; /**< Codec index. */
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000120 void *codec_setting; /**< Specific codec setting. */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000121 pj_pool_t *pool; /**< Pool for each instance. */
122
123 USC_Handle enc; /**< Encoder state. */
124 USC_Handle dec; /**< Decoder state. */
125 USC_CodecInfo *info; /**< Native codec info. */
126 pj_uint16_t frame_size; /**< Bitstream frame size. */
127
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000128 pj_bool_t plc_enabled; /**< PLC enabled flag. */
129 pjmedia_plc *plc; /**< PJMEDIA PLC engine, NULL if
130 codec has internal PLC. */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000131
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000132 pj_bool_t vad_enabled; /**< VAD enabled flag. */
133 pjmedia_silence_det *vad; /**< PJMEDIA VAD engine, NULL if
134 codec has internal VAD. */
135 pj_timestamp last_tx; /**< Timestamp of last transmit.*/
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000136} ipp_private_t;
137
138
139/* USC codec implementations. */
140extern USC_Fxns USC_G729AFP_Fxns;
Nanang Izzuddin762a5bd2008-09-16 14:11:09 +0000141extern USC_Fxns USC_G729I_Fxns;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000142extern USC_Fxns USC_G723_Fxns;
143extern USC_Fxns USC_G726_Fxns;
144extern USC_Fxns USC_G728_Fxns;
145extern USC_Fxns USC_G722_Fxns;
146extern USC_Fxns USC_GSMAMR_Fxns;
147extern USC_Fxns USC_AMRWB_Fxns;
148extern USC_Fxns USC_AMRWBE_Fxns;
149
Nanang Izzuddin762a5bd2008-09-16 14:11:09 +0000150
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000151/* CUSTOM CALLBACKS */
152
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000153/* This callback is useful for translating RTP frame into USC frame, e.g:
154 * reassigning frame attributes, reorder bitstream. Default behaviour of
155 * the translation is just setting the USC frame buffer & its size as
156 * specified in RTP frame, setting USC frame frametype to 0, setting bitrate
157 * of USC frame to bitrate info of codec_data. Implement this callback when
158 * the default behaviour is unapplicable.
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000159 */
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000160typedef void (*predecode_cb)(ipp_private_t *codec_data,
161 const pjmedia_frame *rtp_frame,
162 USC_Bitstream *usc_frame);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000163
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000164/* Parse frames from a packet. Default behaviour of frame parsing is
165 * just separating frames based on calculating frame length derived
166 * from bitrate. Implement this callback when the default behaviour is
167 * unapplicable.
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000168 */
169typedef pj_status_t (*parse_cb)(ipp_private_t *codec_data, void *pkt,
170 pj_size_t pkt_size, const pj_timestamp *ts,
171 unsigned *frame_cnt, pjmedia_frame frames[]);
172
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000173/* Pack frames into a packet. Default behaviour of packing frames is
174 * just stacking the frames with octet aligned without adding any
175 * payload header. Implement this callback when the default behaviour is
176 * unapplicable.
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000177 */
178typedef pj_status_t (*pack_cb)(ipp_private_t *codec_data, void *pkt,
179 pj_size_t *pkt_size, pj_size_t max_pkt_size);
180
181
182
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000183/* Custom callback implementations. */
184static void predecode_g723( ipp_private_t *codec_data,
185 const pjmedia_frame *rtp_frame,
186 USC_Bitstream *usc_frame);
187static pj_status_t parse_g723( ipp_private_t *codec_data, void *pkt,
188 pj_size_t pkt_size, const pj_timestamp *ts,
189 unsigned *frame_cnt, pjmedia_frame frames[]);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000190
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000191static void predecode_g729( ipp_private_t *codec_data,
192 const pjmedia_frame *rtp_frame,
193 USC_Bitstream *usc_frame);
194
195static void predecode_amr( ipp_private_t *codec_data,
196 const pjmedia_frame *rtp_frame,
197 USC_Bitstream *usc_frame);
198static pj_status_t parse_amr( ipp_private_t *codec_data, void *pkt,
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000199 pj_size_t pkt_size, const pj_timestamp *ts,
200 unsigned *frame_cnt, pjmedia_frame frames[]);
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000201static pj_status_t pack_amr( ipp_private_t *codec_data, void *pkt,
202 pj_size_t *pkt_size, pj_size_t max_pkt_size);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000203
Nanang Izzuddin1b9f46b2009-04-06 13:52:01 +0000204static void predecode_g7221( ipp_private_t *codec_data,
205 const pjmedia_frame *rtp_frame,
206 USC_Bitstream *usc_frame);
207static pj_status_t pack_g7221( ipp_private_t *codec_data, void *pkt,
208 pj_size_t *pkt_size, pj_size_t max_pkt_size);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000209
210/* IPP codec implementation descriptions. */
211static struct ipp_codec {
212 int enabled; /* Is this codec enabled? */
213 const char *name; /* Codec name. */
214 pj_uint8_t pt; /* Payload type. */
215 USC_Fxns *fxns; /* USC callback functions. */
216 unsigned clock_rate; /* Codec's clock rate. */
217 unsigned channel_count; /* Codec's channel count. */
218 unsigned samples_per_frame; /* Codec's samples count. */
219
220 unsigned def_bitrate; /* Default bitrate of this codec. */
221 unsigned max_bitrate; /* Maximum bitrate of this codec. */
222 pj_uint8_t frm_per_pkt; /* Default num of frames per packet.*/
223 int has_native_vad; /* Codec has internal VAD? */
224 int has_native_plc; /* Codec has internal PLC? */
225
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000226 predecode_cb predecode; /* Callback to translate RTP frame
Nanang Izzuddin23a00b72008-08-25 13:58:25 +0000227 into USC frame. */
228 parse_cb parse; /* Callback to parse bitstream. */
229 pack_cb pack; /* Callback to pack bitstream. */
230
231 pjmedia_codec_fmtp dec_fmtp; /* Decoder's fmtp params. */
232}
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000233
234ipp_codec[] =
235{
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000236# if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
Benny Prijono329d6382009-05-29 13:04:03 +0000237 /* AMR-NB SID seems to produce noise, so let's just disable its VAD. */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000238 {1, "AMR", PJMEDIA_RTP_PT_AMR, &USC_GSMAMR_Fxns, 8000, 1, 160,
Benny Prijono329d6382009-05-29 13:04:03 +0000239 7400, 12200, 2, 0, 1,
240 &predecode_amr, &parse_amr, &pack_amr,
241 {1, {{{"octet-align", 11}, {"1", 1}}} }
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000242 },
243# endif
244
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000245# if PJMEDIA_HAS_INTEL_IPP_CODEC_AMRWB
246 {1, "AMR-WB", PJMEDIA_RTP_PT_AMRWB, &USC_AMRWB_Fxns, 16000, 1, 320,
247 15850, 23850, 1, 1, 1,
Benny Prijono329d6382009-05-29 13:04:03 +0000248 &predecode_amr, &parse_amr, &pack_amr,
249 {1, {{{"octet-align", 11}, {"1", 1}}} }
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000250 },
251# endif
252
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000253# if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
Nanang Izzuddin762a5bd2008-09-16 14:11:09 +0000254# if defined(PJ_HAS_FLOATING_POINT) && (PJ_HAS_FLOATING_POINT != 0)
255 {1, "G729", PJMEDIA_RTP_PT_G729, &USC_G729AFP_Fxns, 8000, 1, 80,
Nanang Izzuddin23a00b72008-08-25 13:58:25 +0000256 8000, 11800, 2, 1, 1,
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000257 &predecode_g729, NULL, NULL
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000258 },
Nanang Izzuddin762a5bd2008-09-16 14:11:09 +0000259# else
260 {1, "G729", PJMEDIA_RTP_PT_G729, &USC_G729I_Fxns, 8000, 1, 80,
261 8000, 11800, 2, 1, 1,
262 &predecode_g729, NULL, NULL
263 },
264# endif
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000265# endif
266
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000267# if PJMEDIA_HAS_INTEL_IPP_CODEC_G723_1
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000268 /* This is actually G.723.1 */
269 {1, "G723", PJMEDIA_RTP_PT_G723, &USC_G723_Fxns, 8000, 1, 240,
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000270 6300, 6300, 1, 1, 1,
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000271 &predecode_g723, &parse_g723, NULL
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000272 },
273# endif
274
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000275# if PJMEDIA_HAS_INTEL_IPP_CODEC_G726
Nanang Izzuddinf216f822008-08-15 18:35:50 +0000276 {0, "G726-16", PJMEDIA_RTP_PT_G726_16, &USC_G726_Fxns, 8000, 1, 80,
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000277 16000, 16000, 2, 0, 0,
278 NULL, NULL, NULL
279 },
Nanang Izzuddinf216f822008-08-15 18:35:50 +0000280 {0, "G726-24", PJMEDIA_RTP_PT_G726_24, &USC_G726_Fxns, 8000, 1, 80,
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000281 24000, 24000, 2, 0, 0,
282 NULL, NULL, NULL
283 },
284 {1, "G726-32", PJMEDIA_RTP_PT_G726_32, &USC_G726_Fxns, 8000, 1, 80,
285 32000, 32000, 2, 0, 0,
286 NULL, NULL, NULL
287 },
Nanang Izzuddinf216f822008-08-15 18:35:50 +0000288 {0, "G726-40", PJMEDIA_RTP_PT_G726_40, &USC_G726_Fxns, 8000, 1, 80,
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000289 40000, 40000, 2, 0, 0,
290 NULL, NULL, NULL
291 },
292# endif
293
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000294# if PJMEDIA_HAS_INTEL_IPP_CODEC_G728
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000295 {1, "G728", PJMEDIA_RTP_PT_G728, &USC_G728_Fxns, 8000, 1, 80,
296 16000, 16000, 2, 0, 1,
297 NULL, NULL, NULL
298 },
299# endif
300
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000301# if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1
Nanang Izzuddin23a00b72008-08-25 13:58:25 +0000302 {0, "G7221", PJMEDIA_RTP_PT_G722_1_16, &USC_G722_Fxns, 16000, 1, 320,
303 16000, 16000, 1, 0, 1,
Nanang Izzuddin1b9f46b2009-04-06 13:52:01 +0000304 predecode_g7221, NULL, pack_g7221,
Nanang Izzuddin23a00b72008-08-25 13:58:25 +0000305 {1, {{{"bitrate", 7}, {"16000", 5}}} }
306 },
307 {1, "G7221", PJMEDIA_RTP_PT_G722_1_24, &USC_G722_Fxns, 16000, 1, 320,
308 24000, 24000, 1, 0, 1,
Nanang Izzuddin1b9f46b2009-04-06 13:52:01 +0000309 predecode_g7221, NULL, pack_g7221,
Nanang Izzuddin23a00b72008-08-25 13:58:25 +0000310 {1, {{{"bitrate", 7}, {"24000", 5}}} }
311 },
312 {1, "G7221", PJMEDIA_RTP_PT_G722_1_32, &USC_G722_Fxns, 16000, 1, 320,
313 32000, 32000, 1, 0, 1,
Nanang Izzuddin1b9f46b2009-04-06 13:52:01 +0000314 predecode_g7221, NULL, pack_g7221,
Nanang Izzuddin23a00b72008-08-25 13:58:25 +0000315 {1, {{{"bitrate", 7}, {"32000", 5}}} }
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000316 },
317# endif
318};
319
320
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000321#if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
322
323static void predecode_g729( ipp_private_t *codec_data,
324 const pjmedia_frame *rtp_frame,
325 USC_Bitstream *usc_frame)
326{
327 switch (rtp_frame->size) {
328 case 2:
329 /* SID */
330 usc_frame->frametype = 1;
331 usc_frame->bitrate = codec_data->info->params.modes.bitrate;
332 break;
333 case 8:
334 /* G729D */
335 usc_frame->frametype = 2;
336 usc_frame->bitrate = 6400;
337 break;
338 case 10:
339 /* G729 */
340 usc_frame->frametype = 3;
341 usc_frame->bitrate = 8000;
342 break;
343 case 15:
344 /* G729E */
345 usc_frame->frametype = 4;
346 usc_frame->bitrate = 11800;
347 break;
348 default:
349 usc_frame->frametype = 0;
350 usc_frame->bitrate = 0;
351 break;
352 }
353
354 usc_frame->pBuffer = rtp_frame->buf;
355 usc_frame->nbytes = rtp_frame->size;
356}
357
358#endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G729 */
359
360
361#if PJMEDIA_HAS_INTEL_IPP_CODEC_G723_1
362
363static void predecode_g723( ipp_private_t *codec_data,
364 const pjmedia_frame *rtp_frame,
365 USC_Bitstream *usc_frame)
366{
367 int i, HDR = 0;
368 pj_uint8_t *f = (pj_uint8_t*)rtp_frame->buf;
369
370 PJ_UNUSED_ARG(codec_data);
371
372 for (i = 0; i < 2; ++i){
373 int tmp;
374 tmp = (f[0] >> (i & 0x7)) & 1;
375 HDR += tmp << i ;
376 }
377
378 usc_frame->pBuffer = rtp_frame->buf;
379 usc_frame->nbytes = rtp_frame->size;
380 usc_frame->bitrate = HDR == 0? 6300 : 5300;
381 usc_frame->frametype = 0;
382}
383
384static pj_status_t parse_g723(ipp_private_t *codec_data, void *pkt,
385 pj_size_t pkt_size, const pj_timestamp *ts,
386 unsigned *frame_cnt, pjmedia_frame frames[])
387{
388 unsigned count = 0;
389 pj_uint8_t *f = (pj_uint8_t*)pkt;
390
391 while (pkt_size && count < *frame_cnt) {
392 int framesize, i, j;
393 int HDR = 0;
394
395 for (i = 0; i < 2; ++i){
396 j = (f[0] >> (i & 0x7)) & 1;
397 HDR += j << i ;
398 }
399
400 if (HDR == 0)
401 framesize = 24;
402 else if (HDR == 1)
403 framesize = 20;
404 else if (HDR == 2)
405 framesize = 4;
406 else if (HDR == 3)
407 framesize = 1;
408 else {
409 pj_assert(!"Unknown G723.1 frametype, packet may be corrupted!");
410 return PJMEDIA_CODEC_EINMODE;
411 }
412
413 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
414 frames[count].buf = f;
415 frames[count].size = framesize;
416 frames[count].timestamp.u64 = ts->u64 + count *
417 ipp_codec[codec_data->codec_idx].samples_per_frame;
418
419 f += framesize;
420 pkt_size -= framesize;
421
422 ++count;
423 }
424
425 *frame_cnt = count;
426 return PJ_SUCCESS;
427}
428
429#endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G723_1 */
430
431
432#if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
433
434#include <pjmedia-codec/amr_helper.h>
435
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000436typedef struct amr_settings_t {
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +0000437 pjmedia_codec_amr_pack_setting enc_setting;
438 pjmedia_codec_amr_pack_setting dec_setting;
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000439 pj_int8_t enc_mode;
440} amr_settings_t;
441
442
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000443/* Rearrange AMR bitstream and convert RTP frame into USC frame:
444 * - make the start_bit to be 0
445 * - if it is speech frame, reorder bitstream from sensitivity bits order
446 * to encoder bits order.
447 * - set the appropriate value of usc_frame.
448 */
449static void predecode_amr( ipp_private_t *codec_data,
450 const pjmedia_frame *rtp_frame,
451 USC_Bitstream *usc_frame)
452{
453 pjmedia_frame frame;
454 pjmedia_codec_amr_bit_info *info;
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +0000455 pjmedia_codec_amr_pack_setting *setting;
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000456
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000457 setting = &((amr_settings_t*)codec_data->codec_setting)->dec_setting;
458
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000459 frame = *rtp_frame;
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000460 pjmedia_codec_amr_predecode(rtp_frame, setting, &frame);
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000461 info = (pjmedia_codec_amr_bit_info*) &frame.bit_info;
462
463 usc_frame->pBuffer = frame.buf;
464 usc_frame->nbytes = frame.size;
465 if (info->mode != -1) {
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000466 usc_frame->bitrate = setting->amr_nb?
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000467 pjmedia_codec_amrnb_bitrates[info->mode]:
468 pjmedia_codec_amrwb_bitrates[info->mode];
469 } else {
470 usc_frame->bitrate = 0;
471 }
472
473 if (frame.size > 5) {
474 /* Speech */
475 if (info->good_quality)
476 usc_frame->frametype = 0;
477 else
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000478 usc_frame->frametype = setting->amr_nb ? 5 : 6;
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000479 } else if (frame.size == 5) {
480 /* SID */
481 if (info->good_quality) {
482 pj_bool_t STI;
483 STI = (((pj_uint8_t*)frame.buf)[35 >> 3] & 0x10) != 0;
484 usc_frame->frametype = STI? 2 : 1;
485 } else {
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000486 usc_frame->frametype = setting->amr_nb ? 6 : 7;
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000487 }
488 } else {
489 /* no data */
490 usc_frame->frametype = 3;
491 }
492}
493
494/* Pack AMR payload */
495static pj_status_t pack_amr(ipp_private_t *codec_data, void *pkt,
496 pj_size_t *pkt_size, pj_size_t max_pkt_size)
497{
Nanang Izzuddin0290a572010-05-11 06:33:55 +0000498 enum {MAX_FRAMES_PER_PACKET = PJMEDIA_MAX_FRAME_DURATION_MS / 20};
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000499
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000500 pjmedia_frame frames[MAX_FRAMES_PER_PACKET];
501 unsigned nframes = 0;
502 pjmedia_codec_amr_bit_info *info;
503 pj_uint8_t *r; /* Read cursor */
504 pj_uint8_t SID_FT;
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +0000505 pjmedia_codec_amr_pack_setting *setting;
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000506
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000507 setting = &((amr_settings_t*)codec_data->codec_setting)->enc_setting;
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000508
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000509 SID_FT = (pj_uint8_t)(setting->amr_nb? 8 : 9);
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000510
511 /* Align pkt buf right */
512 r = (pj_uint8_t*)pkt + max_pkt_size - *pkt_size;
513 pj_memmove(r, pkt, *pkt_size);
514
515 /* Get frames */
516 for (;;) {
517 pj_bool_t eof;
518 pj_uint16_t info_;
519
520 info_ = *((pj_uint16_t*)r);
521 eof = ((info_ & 0x40) != 0);
522
523 info = (pjmedia_codec_amr_bit_info*) &frames[nframes].bit_info;
524 pj_bzero(info, sizeof(*info));
525 info->frame_type = (pj_uint8_t)(info_ & 0x0F);
526 info->good_quality = (pj_uint8_t)((info_ & 0x80) == 0);
527 info->mode = (pj_int8_t) ((info_ >> 8) & 0x0F);
528
529 frames[nframes].buf = r + 2;
530 frames[nframes].size = info->frame_type <= SID_FT ?
531 pjmedia_codec_amrnb_framelen[info->frame_type] :
532 0;
533
534 r += frames[nframes].size + 2;
535
536 /* Last frame */
537 if (++nframes >= MAX_FRAMES_PER_PACKET || eof)
538 break;
539 }
540
541 /* Pack */
542 *pkt_size = max_pkt_size;
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000543 return pjmedia_codec_amr_pack(frames, nframes, setting, pkt, pkt_size);
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000544}
545
546
547/* Parse AMR payload into frames. */
548static pj_status_t parse_amr(ipp_private_t *codec_data, void *pkt,
549 pj_size_t pkt_size, const pj_timestamp *ts,
550 unsigned *frame_cnt, pjmedia_frame frames[])
551{
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000552 amr_settings_t* s = (amr_settings_t*)codec_data->codec_setting;
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +0000553 pjmedia_codec_amr_pack_setting *setting;
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000554 pj_status_t status;
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +0000555 pj_uint8_t cmr;
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000556
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000557 setting = &s->dec_setting;
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000558
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000559 status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, setting, frames,
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +0000560 frame_cnt, &cmr);
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000561 if (status != PJ_SUCCESS)
562 return status;
563
564 /* Check Change Mode Request. */
Nanang Izzuddin42bb38b2010-05-26 13:50:42 +0000565 if (((setting->amr_nb && cmr <= 7) || (!setting->amr_nb && cmr <= 8)) &&
566 s->enc_mode != cmr)
567 {
Benny Prijono329d6382009-05-29 13:04:03 +0000568 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
569
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +0000570 s->enc_mode = cmr;
Benny Prijono329d6382009-05-29 13:04:03 +0000571 codec_data->info->params.modes.bitrate = s->enc_setting.amr_nb?
572 pjmedia_codec_amrnb_bitrates[s->enc_mode] :
573 pjmedia_codec_amrwb_bitrates[s->enc_mode];
574 ippc->fxns->std.Control(&codec_data->info->params.modes,
575 codec_data->enc);
Nanang Izzuddin18a9ef12009-06-02 12:38:15 +0000576
577 PJ_LOG(4,(THIS_FILE, "AMR%s switched encoding mode to: %d (%dbps)",
578 (s->enc_setting.amr_nb?"":"-WB"),
579 s->enc_mode,
580 codec_data->info->params.modes.bitrate));
Nanang Izzuddin35e01de2008-10-29 10:17:02 +0000581 }
582
583 return PJ_SUCCESS;
Nanang Izzuddind55553a2008-10-28 02:24:46 +0000584}
585
586#endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_AMR */
587
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000588
Nanang Izzuddin1b9f46b2009-04-06 13:52:01 +0000589#if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1
590
591static void predecode_g7221( ipp_private_t *codec_data,
592 const pjmedia_frame *rtp_frame,
593 USC_Bitstream *usc_frame)
594{
595 usc_frame->pBuffer = (char*)rtp_frame->buf;
596 usc_frame->nbytes = rtp_frame->size;
597 usc_frame->frametype = 0;
598 usc_frame->bitrate = codec_data->info->params.modes.bitrate;
599
600#if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0
601 {
602 pj_uint16_t *p, *p_end;
603
604 p = (pj_uint16_t*)rtp_frame->buf;
605 p_end = p + rtp_frame->size/2;
606 while (p < p_end) {
607 *p = pj_ntohs(*p);
608 ++p;
609 }
610 }
611#endif
612}
613
614static pj_status_t pack_g7221( ipp_private_t *codec_data, void *pkt,
615 pj_size_t *pkt_size, pj_size_t max_pkt_size)
616{
617 PJ_UNUSED_ARG(codec_data);
618 PJ_UNUSED_ARG(max_pkt_size);
619
620#if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0
621 {
622 pj_uint16_t *p, *p_end;
623
624 p = (pj_uint16_t*)pkt;
625 p_end = p + *pkt_size/2;
626 while (p < p_end) {
627 *p = pj_htons(*p);
628 ++p;
629 }
630 }
631#else
632 PJ_UNUSED_ARG(pkt);
633 PJ_UNUSED_ARG(pkt_size);
634#endif
635
636 return PJ_SUCCESS;
637}
638
639#endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1 */
640
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000641/*
642 * Initialize and register IPP codec factory to pjmedia endpoint.
643 */
644PJ_DEF(pj_status_t) pjmedia_codec_ipp_init( pjmedia_endpt *endpt )
645{
646 pjmedia_codec_mgr *codec_mgr;
647 pj_status_t status;
648
649 if (ipp_factory.pool != NULL) {
650 /* Already initialized. */
651 return PJ_SUCCESS;
652 }
653
654 /* Create IPP codec factory. */
655 ipp_factory.base.op = &ipp_factory_op;
656 ipp_factory.base.factory_data = NULL;
657 ipp_factory.endpt = endpt;
658
659 ipp_factory.pool = pjmedia_endpt_create_pool(endpt, "IPP codecs", 4000, 4000);
660 if (!ipp_factory.pool)
661 return PJ_ENOMEM;
662
663 /* Create mutex. */
664 status = pj_mutex_create_simple(ipp_factory.pool, "IPP codecs",
665 &ipp_factory.mutex);
666 if (status != PJ_SUCCESS)
667 goto on_error;
668
669 /* Get the codec manager. */
670 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
671 if (!codec_mgr) {
672 status = PJ_EINVALIDOP;
673 goto on_error;
674 }
675
676 /* Register codec factory to endpoint. */
677 status = pjmedia_codec_mgr_register_factory(codec_mgr,
678 &ipp_factory.base);
679 if (status != PJ_SUCCESS)
680 goto on_error;
681
682 /* Done. */
683 return PJ_SUCCESS;
684
685on_error:
686 pj_pool_release(ipp_factory.pool);
687 ipp_factory.pool = NULL;
688 return status;
689}
690
691/*
692 * Unregister IPP codecs factory from pjmedia endpoint.
693 */
694PJ_DEF(pj_status_t) pjmedia_codec_ipp_deinit(void)
695{
696 pjmedia_codec_mgr *codec_mgr;
697 pj_status_t status;
698
699 if (ipp_factory.pool == NULL) {
700 /* Already deinitialized */
701 return PJ_SUCCESS;
702 }
703
704 pj_mutex_lock(ipp_factory.mutex);
705
706 /* Get the codec manager. */
707 codec_mgr = pjmedia_endpt_get_codec_mgr(ipp_factory.endpt);
708 if (!codec_mgr) {
709 pj_pool_release(ipp_factory.pool);
710 ipp_factory.pool = NULL;
711 return PJ_EINVALIDOP;
712 }
713
714 /* Unregister IPP codecs factory. */
715 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
716 &ipp_factory.base);
717
718 /* Destroy mutex. */
719 pj_mutex_destroy(ipp_factory.mutex);
720
721 /* Destroy pool. */
722 pj_pool_release(ipp_factory.pool);
723 ipp_factory.pool = NULL;
724
725 return status;
726}
727
728/*
729 * Check if factory can allocate the specified codec.
730 */
731static pj_status_t ipp_test_alloc( pjmedia_codec_factory *factory,
732 const pjmedia_codec_info *info )
733{
734 unsigned i;
735
736 PJ_UNUSED_ARG(factory);
737
738 /* Type MUST be audio. */
739 if (info->type != PJMEDIA_TYPE_AUDIO)
740 return PJMEDIA_CODEC_EUNSUP;
741
742 for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) {
743 pj_str_t name = pj_str((char*)ipp_codec[i].name);
744 if ((pj_stricmp(&info->encoding_name, &name) == 0) &&
745 (info->clock_rate == (unsigned)ipp_codec[i].clock_rate) &&
746 (info->channel_cnt == (unsigned)ipp_codec[i].channel_count) &&
747 (ipp_codec[i].enabled))
748 {
749 return PJ_SUCCESS;
750 }
751 }
752
753 /* Unsupported, or mode is disabled. */
754 return PJMEDIA_CODEC_EUNSUP;
755}
756
757/*
758 * Generate default attribute.
759 */
760static pj_status_t ipp_default_attr (pjmedia_codec_factory *factory,
761 const pjmedia_codec_info *id,
762 pjmedia_codec_param *attr )
763{
764 unsigned i;
765
766 PJ_ASSERT_RETURN(factory==&ipp_factory.base, PJ_EINVAL);
767
768 pj_bzero(attr, sizeof(pjmedia_codec_param));
769
770 for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) {
771 pj_str_t name = pj_str((char*)ipp_codec[i].name);
772 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
773 (id->clock_rate == (unsigned)ipp_codec[i].clock_rate) &&
Nanang Izzuddin23a00b72008-08-25 13:58:25 +0000774 (id->channel_cnt == (unsigned)ipp_codec[i].channel_count) &&
775 (id->pt == (unsigned)ipp_codec[i].pt))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000776 {
777 attr->info.pt = (pj_uint8_t)id->pt;
778 attr->info.channel_cnt = ipp_codec[i].channel_count;
779 attr->info.clock_rate = ipp_codec[i].clock_rate;
780 attr->info.avg_bps = ipp_codec[i].def_bitrate;
781 attr->info.max_bps = ipp_codec[i].max_bitrate;
782 attr->info.pcm_bits_per_sample = 16;
783 attr->info.frm_ptime = (pj_uint16_t)
784 (ipp_codec[i].samples_per_frame * 1000 /
785 ipp_codec[i].channel_count /
786 ipp_codec[i].clock_rate);
787 attr->setting.frm_per_pkt = ipp_codec[i].frm_per_pkt;
788
789 /* Default flags. */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000790 attr->setting.plc = 1;
791 attr->setting.penh= 0;
Nanang Izzuddin23a00b72008-08-25 13:58:25 +0000792 attr->setting.vad = 1;
793 attr->setting.cng = attr->setting.vad;
794 attr->setting.dec_fmtp = ipp_codec[i].dec_fmtp;
795
796 if (attr->setting.vad == 0) {
797#if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
798 if (id->pt == PJMEDIA_RTP_PT_G729) {
799 /* Signal G729 Annex B is being disabled */
800 attr->setting.dec_fmtp.cnt = 1;
801 pj_strset2(&attr->setting.dec_fmtp.param[0].name, "annexb");
802 pj_strset2(&attr->setting.dec_fmtp.param[0].val, "no");
803 }
804#endif
805 }
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000806
807 return PJ_SUCCESS;
808 }
809 }
810
811 return PJMEDIA_CODEC_EUNSUP;
812}
813
814/*
815 * Enum codecs supported by this factory.
816 */
817static pj_status_t ipp_enum_codecs(pjmedia_codec_factory *factory,
818 unsigned *count,
819 pjmedia_codec_info codecs[])
820{
821 unsigned max;
822 unsigned i;
823
824 PJ_UNUSED_ARG(factory);
825 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
826
827 max = *count;
828
829 for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(ipp_codec) && *count < max; ++i)
830 {
831 if (!ipp_codec[i].enabled)
832 continue;
833
834 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
835 codecs[*count].encoding_name = pj_str((char*)ipp_codec[i].name);
836 codecs[*count].pt = ipp_codec[i].pt;
837 codecs[*count].type = PJMEDIA_TYPE_AUDIO;
838 codecs[*count].clock_rate = ipp_codec[i].clock_rate;
839 codecs[*count].channel_cnt = ipp_codec[i].channel_count;
840
841 ++*count;
842 }
843
844 return PJ_SUCCESS;
845}
846
847/*
848 * Allocate a new codec instance.
849 */
850static pj_status_t ipp_alloc_codec( pjmedia_codec_factory *factory,
851 const pjmedia_codec_info *id,
852 pjmedia_codec **p_codec)
853{
854 ipp_private_t *codec_data;
855 pjmedia_codec *codec;
856 int idx;
857 pj_pool_t *pool;
858 unsigned i;
859
860 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
861 PJ_ASSERT_RETURN(factory == &ipp_factory.base, PJ_EINVAL);
862
863 pj_mutex_lock(ipp_factory.mutex);
864
865 /* Find codec's index */
866 idx = -1;
867 for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) {
868 pj_str_t name = pj_str((char*)ipp_codec[i].name);
869 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
870 (id->clock_rate == (unsigned)ipp_codec[i].clock_rate) &&
871 (id->channel_cnt == (unsigned)ipp_codec[i].channel_count) &&
872 (ipp_codec[i].enabled))
873 {
874 idx = i;
875 break;
876 }
877 }
878 if (idx == -1) {
879 *p_codec = NULL;
880 return PJMEDIA_CODEC_EFAILED;
881 }
882
883 /* Create pool for codec instance */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000884 pool = pjmedia_endpt_create_pool(ipp_factory.endpt, "IPPcodec", 512, 512);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000885 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
886 PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM);
887 codec->op = &ipp_op;
888 codec->factory = factory;
889 codec->codec_data = PJ_POOL_ZALLOC_T(pool, ipp_private_t);
890 codec_data = (ipp_private_t*) codec->codec_data;
891
892 /* Create PLC if codec has no internal PLC */
893 if (!ipp_codec[idx].has_native_plc) {
894 pj_status_t status;
895 status = pjmedia_plc_create(pool, ipp_codec[idx].clock_rate,
896 ipp_codec[idx].samples_per_frame, 0,
897 &codec_data->plc);
898 if (status != PJ_SUCCESS) {
899 pj_pool_release(pool);
900 pj_mutex_unlock(ipp_factory.mutex);
901 return status;
902 }
903 }
904
905 /* Create silence detector if codec has no internal VAD */
906 if (!ipp_codec[idx].has_native_vad) {
907 pj_status_t status;
908 status = pjmedia_silence_det_create(pool,
909 ipp_codec[idx].clock_rate,
910 ipp_codec[idx].samples_per_frame,
911 &codec_data->vad);
912 if (status != PJ_SUCCESS) {
913 pj_pool_release(pool);
914 pj_mutex_unlock(ipp_factory.mutex);
915 return status;
916 }
917 }
918
919 codec_data->pool = pool;
920 codec_data->codec_idx = idx;
921
922 pj_mutex_unlock(ipp_factory.mutex);
923
924 *p_codec = codec;
925 return PJ_SUCCESS;
926}
927
928/*
929 * Free codec.
930 */
931static pj_status_t ipp_dealloc_codec( pjmedia_codec_factory *factory,
932 pjmedia_codec *codec )
933{
934 ipp_private_t *codec_data;
935
936 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
937 PJ_ASSERT_RETURN(factory == &ipp_factory.base, PJ_EINVAL);
938
939 /* Close codec, if it's not closed. */
940 codec_data = (ipp_private_t*) codec->codec_data;
941 if (codec_data->enc != NULL || codec_data->dec != NULL) {
942 ipp_codec_close(codec);
943 }
944
945 pj_pool_release(codec_data->pool);
946
947 return PJ_SUCCESS;
948}
949
950/*
951 * Init codec.
952 */
953static pj_status_t ipp_codec_init( pjmedia_codec *codec,
954 pj_pool_t *pool )
955{
956 PJ_UNUSED_ARG(codec);
957 PJ_UNUSED_ARG(pool);
958 return PJ_SUCCESS;
959}
960
961/*
962 * Open codec.
963 */
964static pj_status_t ipp_codec_open( pjmedia_codec *codec,
965 pjmedia_codec_param *attr )
966{
967 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000968 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000969 int info_size;
970 pj_pool_t *pool;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000971 int i, j;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000972 USC_MemBank *membanks;
973 int nb_membanks;
974
975 pool = codec_data->pool;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000976
977 /* Get the codec info size */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000978 if (USC_NoError != ippc->fxns->std.GetInfoSize(&info_size)) {
979 PJ_LOG(1,(THIS_FILE, "Error getting codec info size"));
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000980 goto on_error;
981 }
982 /* Get the codec info */
983 codec_data->info = pj_pool_zalloc(pool, info_size);
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000984 if (USC_NoError != ippc->fxns->std.GetInfo((USC_Handle)NULL,
985 codec_data->info))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000986 {
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000987 PJ_LOG(1,(THIS_FILE, "Error getting codec info"));
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000988 goto on_error;
989 }
990
991 /* PREPARING THE ENCODER */
992
993 /* Setting the encoder params */
994 codec_data->info->params.direction = USC_ENCODE;
Nanang Izzuddinf216f822008-08-15 18:35:50 +0000995 codec_data->info->params.modes.vad = attr->setting.vad &&
Nanang Izzuddin23a00b72008-08-25 13:58:25 +0000996 ippc->has_native_vad;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000997 codec_data->info->params.modes.bitrate = attr->info.avg_bps;
998 codec_data->info->params.law = 0; /* Linear PCM input */
999
Nanang Izzuddin23a00b72008-08-25 13:58:25 +00001000#if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
1001 if (ippc->pt == PJMEDIA_RTP_PT_G729) {
1002 /* Check if G729 Annex B is signaled to be disabled */
1003 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
1004 if (pj_stricmp2(&attr->setting.enc_fmtp.param[i].name, "annexb")==0)
1005 {
1006 if (pj_stricmp2(&attr->setting.enc_fmtp.param[i].val, "no")==0)
1007 codec_data->info->params.modes.vad = 0;
1008 break;
1009 }
1010 }
1011 }
1012#endif
1013
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001014 /* Get number of memory blocks needed by the encoder */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001015 if (USC_NoError != ippc->fxns->std.NumAlloc(&codec_data->info->params,
1016 &nb_membanks))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001017 {
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001018 PJ_LOG(1,(THIS_FILE, "Error getting no of memory blocks of encoder"));
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001019 goto on_error;
1020 }
1021
1022 /* Allocate memory blocks table */
1023 membanks = (USC_MemBank*) pj_pool_zalloc(pool,
1024 sizeof(USC_MemBank) * nb_membanks);
1025 /* Get size of each memory block */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001026 if (USC_NoError != ippc->fxns->std.MemAlloc(&codec_data->info->params,
1027 membanks))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001028 {
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001029 PJ_LOG(1,(THIS_FILE, "Error getting memory blocks size of encoder"));
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001030 goto on_error;
1031 }
1032
1033 /* Allocate memory for each block */
1034 for (i = 0; i < nb_membanks; i++) {
1035 membanks[i].pMem = (char*) pj_pool_zalloc(pool, membanks[i].nbytes);
1036 }
1037
1038 /* Create encoder instance */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001039 if (USC_NoError != ippc->fxns->std.Init(&codec_data->info->params,
1040 membanks,
1041 &codec_data->enc))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001042 {
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001043 PJ_LOG(1,(THIS_FILE, "Error initializing encoder"));
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001044 goto on_error;
1045 }
1046
1047 /* PREPARING THE DECODER */
1048
1049 /* Setting the decoder params */
1050 codec_data->info->params.direction = USC_DECODE;
1051
Nanang Izzuddin23a00b72008-08-25 13:58:25 +00001052 /* Not sure if VAD affects decoder, just try to be safe */
Benny Prijono329d6382009-05-29 13:04:03 +00001053 //codec_data->info->params.modes.vad = ippc->has_native_vad;
Nanang Izzuddin23a00b72008-08-25 13:58:25 +00001054
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001055 /* Get number of memory blocks needed by the decoder */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001056 if (USC_NoError != ippc->fxns->std.NumAlloc(&codec_data->info->params,
1057 &nb_membanks))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001058 {
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001059 PJ_LOG(1,(THIS_FILE, "Error getting no of memory blocks of decoder"));
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001060 goto on_error;
1061 }
1062
1063 /* Allocate memory blocks table */
1064 membanks = (USC_MemBank*) pj_pool_zalloc(pool,
1065 sizeof(USC_MemBank) * nb_membanks);
1066 /* Get size of each memory block */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001067 if (USC_NoError != ippc->fxns->std.MemAlloc(&codec_data->info->params,
1068 membanks))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001069 {
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001070 PJ_LOG(1,(THIS_FILE, "Error getting memory blocks size of decoder"));
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001071 goto on_error;
1072 }
1073
1074 /* Allocate memory for each block */
1075 for (i = 0; i < nb_membanks; i++) {
1076 membanks[i].pMem = (char*) pj_pool_zalloc(pool, membanks[i].nbytes);
1077 }
1078
1079 /* Create decoder instance */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001080 if (USC_NoError != ippc->fxns->std.Init(&codec_data->info->params,
1081 membanks, &codec_data->dec))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001082 {
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001083 PJ_LOG(1,(THIS_FILE, "Error initializing decoder"));
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001084 goto on_error;
1085 }
1086
1087 /* Update codec info */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001088 ippc->fxns->std.GetInfo((USC_Handle)codec_data->enc, codec_data->info);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001089
1090 /* Get bitstream size */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001091 i = codec_data->info->params.modes.bitrate * ippc->samples_per_frame;
1092 j = ippc->clock_rate << 3;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001093 codec_data->frame_size = (pj_uint16_t)(i / j);
1094 if (i % j) ++codec_data->frame_size;
1095
1096 codec_data->vad_enabled = (attr->setting.vad != 0);
1097 codec_data->plc_enabled = (attr->setting.plc != 0);
1098
Nanang Izzuddin35e01de2008-10-29 10:17:02 +00001099#if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
1100 /* Init AMR settings */
1101 if (ippc->pt == PJMEDIA_RTP_PT_AMR || ippc->pt == PJMEDIA_RTP_PT_AMRWB) {
1102 amr_settings_t *s;
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +00001103 pj_uint8_t octet_align = 0;
Nanang Izzuddin0b9da642009-06-02 16:28:24 +00001104 pj_int8_t enc_mode;
1105
1106 enc_mode = pjmedia_codec_amr_get_mode(
1107 codec_data->info->params.modes.bitrate);
1108 pj_assert(enc_mode >= 0 && enc_mode <= 8);
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +00001109
Benny Prijono329d6382009-05-29 13:04:03 +00001110 /* Check AMR specific attributes */
1111
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +00001112 for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) {
Benny Prijono329d6382009-05-29 13:04:03 +00001113 /* octet-align, one of the parameters that must have same value
1114 * in offer & answer (RFC 4867 Section 8.3.1). Just check fmtp
1115 * in the decoder side, since it's value is guaranteed to fulfil
1116 * above requirement (by SDP negotiator).
1117 */
1118 const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11};
1119
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +00001120 if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name,
1121 &STR_FMTP_OCTET_ALIGN) == 0)
1122 {
1123 octet_align=(pj_uint8_t)
Benny Prijono329d6382009-05-29 13:04:03 +00001124 pj_strtoul(&attr->setting.dec_fmtp.param[i].val);
1125 break;
1126 }
1127 }
Nanang Izzuddin0b9da642009-06-02 16:28:24 +00001128
Benny Prijono329d6382009-05-29 13:04:03 +00001129 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
Nanang Izzuddin0b9da642009-06-02 16:28:24 +00001130 /* mode-set, encoding mode is chosen based on local default mode
1131 * setting:
1132 * - if local default mode is included in the mode-set, use it
1133 * - otherwise, find the closest mode to local default mode;
1134 * if there are two closest modes, prefer to use the higher
1135 * one, e.g: local default mode is 4, the mode-set param
1136 * contains '2,3,5,6', then 5 will be chosen.
1137 */
Benny Prijono329d6382009-05-29 13:04:03 +00001138 const pj_str_t STR_FMTP_MODE_SET = {"mode-set", 8};
1139
1140 if (pj_stricmp(&attr->setting.enc_fmtp.param[i].name,
1141 &STR_FMTP_MODE_SET) == 0)
1142 {
Nanang Izzuddin18a9ef12009-06-02 12:38:15 +00001143 const char *p;
1144 pj_size_t l;
Nanang Izzuddin0b9da642009-06-02 16:28:24 +00001145 pj_int8_t diff = 99;
1146
Nanang Izzuddin18a9ef12009-06-02 12:38:15 +00001147 p = pj_strbuf(&attr->setting.enc_fmtp.param[i].val);
1148 l = pj_strlen(&attr->setting.enc_fmtp.param[i].val);
Benny Prijono329d6382009-05-29 13:04:03 +00001149
Nanang Izzuddin18a9ef12009-06-02 12:38:15 +00001150 while (l--) {
Nanang Izzuddin0b9da642009-06-02 16:28:24 +00001151 if ((ippc->pt==PJMEDIA_RTP_PT_AMR && *p>='0' && *p<='7') ||
1152 (ippc->pt==PJMEDIA_RTP_PT_AMRWB && *p>='0' && *p<='8'))
Nanang Izzuddin18a9ef12009-06-02 12:38:15 +00001153 {
Benny Prijono1383e472009-08-01 09:23:15 +00001154 pj_int8_t tmp = (pj_int8_t)(*p - '0' - enc_mode);
Nanang Izzuddin0b9da642009-06-02 16:28:24 +00001155
1156 if (PJ_ABS(diff) > PJ_ABS(tmp) ||
1157 (PJ_ABS(diff) == PJ_ABS(tmp) && tmp > diff))
1158 {
1159 diff = tmp;
1160 if (diff == 0) break;
1161 }
Nanang Izzuddin18a9ef12009-06-02 12:38:15 +00001162 }
1163 ++p;
Benny Prijono329d6382009-05-29 13:04:03 +00001164 }
Nanang Izzuddin0b9da642009-06-02 16:28:24 +00001165
1166 if (diff == 99)
1167 goto on_error;
1168
Benny Prijono1383e472009-08-01 09:23:15 +00001169 enc_mode = (pj_int8_t)(enc_mode + diff);
Nanang Izzuddin0b9da642009-06-02 16:28:24 +00001170
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +00001171 break;
1172 }
1173 }
Nanang Izzuddin35e01de2008-10-29 10:17:02 +00001174
Benny Prijono329d6382009-05-29 13:04:03 +00001175 /* Initialize AMR specific settings */
Nanang Izzuddin35e01de2008-10-29 10:17:02 +00001176 s = PJ_POOL_ZALLOC_T(pool, amr_settings_t);
1177 codec_data->codec_setting = s;
1178
Benny Prijono6b6fce12009-03-17 10:13:30 +00001179 s->enc_setting.amr_nb = (pj_uint8_t)(ippc->pt == PJMEDIA_RTP_PT_AMR);
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +00001180 s->enc_setting.octet_aligned = octet_align;
Nanang Izzuddin35e01de2008-10-29 10:17:02 +00001181 s->enc_setting.reorder = PJ_TRUE;
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +00001182 s->enc_setting.cmr = 15;
Benny Prijono329d6382009-05-29 13:04:03 +00001183
Benny Prijono6b6fce12009-03-17 10:13:30 +00001184 s->dec_setting.amr_nb = (pj_uint8_t)(ippc->pt == PJMEDIA_RTP_PT_AMR);
Nanang Izzuddin0b8f4ca2008-11-11 11:25:13 +00001185 s->dec_setting.octet_aligned = octet_align;
Nanang Izzuddin35e01de2008-10-29 10:17:02 +00001186 s->dec_setting.reorder = PJ_TRUE;
Benny Prijono329d6382009-05-29 13:04:03 +00001187
Nanang Izzuddin0b9da642009-06-02 16:28:24 +00001188 /* Apply encoder mode/bitrate */
1189 s->enc_mode = enc_mode;
Nanang Izzuddin18a9ef12009-06-02 12:38:15 +00001190 codec_data->info->params.modes.bitrate = s->enc_setting.amr_nb?
1191 pjmedia_codec_amrnb_bitrates[s->enc_mode]:
1192 pjmedia_codec_amrwb_bitrates[s->enc_mode];
1193 ippc->fxns->std.Control(&codec_data->info->params.modes,
1194 codec_data->enc);
Benny Prijono329d6382009-05-29 13:04:03 +00001195
Nanang Izzuddin18a9ef12009-06-02 12:38:15 +00001196 PJ_LOG(4,(THIS_FILE, "AMR%s encoding mode: %d (%dbps)",
1197 (s->enc_setting.amr_nb?"":"-WB"),
1198 s->enc_mode,
1199 codec_data->info->params.modes.bitrate));
Nanang Izzuddin06839e72010-01-27 11:48:31 +00001200
1201 /* Return back bitrate info to application */
1202 attr->info.avg_bps = codec_data->info->params.modes.bitrate;
Nanang Izzuddin35e01de2008-10-29 10:17:02 +00001203 }
1204#endif
1205
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001206 return PJ_SUCCESS;
1207
1208on_error:
1209 return PJMEDIA_CODEC_EFAILED;
1210}
1211
1212/*
1213 * Close codec.
1214 */
1215static pj_status_t ipp_codec_close( pjmedia_codec *codec )
1216{
1217 PJ_UNUSED_ARG(codec);
1218
1219 return PJ_SUCCESS;
1220}
1221
1222
1223/*
1224 * Modify codec settings.
1225 */
1226static pj_status_t ipp_codec_modify(pjmedia_codec *codec,
1227 const pjmedia_codec_param *attr )
1228{
1229 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001230 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001231
1232 codec_data->vad_enabled = (attr->setting.vad != 0);
1233 codec_data->plc_enabled = (attr->setting.plc != 0);
1234
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001235 if (ippc->has_native_vad) {
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001236 USC_Modes modes;
1237
1238 modes = codec_data->info->params.modes;
1239 modes.vad = codec_data->vad_enabled;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001240 ippc->fxns->std.Control(&modes, codec_data->enc);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001241 }
1242
1243 return PJ_SUCCESS;
1244}
1245
1246/*
1247 * Get frames in the packet.
1248 */
1249static pj_status_t ipp_codec_parse( pjmedia_codec *codec,
1250 void *pkt,
1251 pj_size_t pkt_size,
1252 const pj_timestamp *ts,
1253 unsigned *frame_cnt,
1254 pjmedia_frame frames[])
1255{
1256 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001257 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001258 unsigned count = 0;
1259
1260 PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
1261
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001262 if (ippc->parse != NULL) {
1263 return ippc->parse(codec_data, pkt, pkt_size, ts, frame_cnt, frames);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001264 }
1265
1266 while (pkt_size >= codec_data->frame_size && count < *frame_cnt) {
1267 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
1268 frames[count].buf = pkt;
1269 frames[count].size = codec_data->frame_size;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001270 frames[count].timestamp.u64 = ts->u64 + count*ippc->samples_per_frame;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001271
1272 pkt = ((char*)pkt) + codec_data->frame_size;
1273 pkt_size -= codec_data->frame_size;
1274
1275 ++count;
1276 }
1277
1278 if (pkt_size && count < *frame_cnt) {
1279 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
1280 frames[count].buf = pkt;
1281 frames[count].size = pkt_size;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001282 frames[count].timestamp.u64 = ts->u64 + count*ippc->samples_per_frame;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001283 ++count;
1284 }
1285
1286 *frame_cnt = count;
1287 return PJ_SUCCESS;
1288}
1289
1290/*
1291 * Encode frames.
1292 */
1293static pj_status_t ipp_codec_encode( pjmedia_codec *codec,
1294 const struct pjmedia_frame *input,
1295 unsigned output_buf_len,
1296 struct pjmedia_frame *output)
1297{
1298 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001299 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001300 unsigned samples_per_frame;
1301 unsigned nsamples;
1302 pj_size_t tx = 0;
1303 pj_int16_t *pcm_in = (pj_int16_t*)input->buf;
Benny Prijonob1339242008-08-21 20:58:55 +00001304 pj_uint8_t *bits_out = (pj_uint8_t*) output->buf;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001305 pj_uint8_t pt;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001306
1307 /* Invoke external VAD if codec has no internal VAD */
1308 if (codec_data->vad && codec_data->vad_enabled) {
1309 pj_bool_t is_silence;
1310 pj_int32_t silence_duration;
1311
1312 silence_duration = pj_timestamp_diff32(&codec_data->last_tx,
1313 &input->timestamp);
1314
1315 is_silence = pjmedia_silence_det_detect(codec_data->vad,
1316 (const pj_int16_t*) input->buf,
1317 (input->size >> 1),
1318 NULL);
1319 if (is_silence &&
Nanang Izzuddin4ff93f42009-06-13 15:28:37 +00001320 (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
1321 silence_duration < (PJMEDIA_CODEC_MAX_SILENCE_PERIOD *
1322 (int)ippc->clock_rate / 1000)))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001323 {
1324 output->type = PJMEDIA_FRAME_TYPE_NONE;
1325 output->buf = NULL;
1326 output->size = 0;
1327 output->timestamp = input->timestamp;
1328 return PJ_SUCCESS;
1329 } else {
1330 codec_data->last_tx = input->timestamp;
1331 }
1332 }
1333
1334 nsamples = input->size >> 1;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001335 samples_per_frame = ippc->samples_per_frame;
1336 pt = ippc->pt;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001337
1338 PJ_ASSERT_RETURN(nsamples % samples_per_frame == 0,
1339 PJMEDIA_CODEC_EPCMFRMINLEN);
1340
1341 /* Encode the frames */
1342 while (nsamples >= samples_per_frame) {
1343 USC_PCMStream in;
1344 USC_Bitstream out;
1345
1346 in.bitrate = codec_data->info->params.modes.bitrate;
1347 in.nbytes = samples_per_frame << 1;
1348 in.pBuffer = (char*)pcm_in;
1349 in.pcmType.bitPerSample = codec_data->info->params.pcmType.bitPerSample;
1350 in.pcmType.nChannels = codec_data->info->params.pcmType.nChannels;
1351 in.pcmType.sample_frequency = codec_data->info->params.pcmType.sample_frequency;
1352
Benny Prijonob1339242008-08-21 20:58:55 +00001353 out.pBuffer = (char*)bits_out;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001354
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001355#if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001356 /* For AMR: reserve two octets for AMR frame info */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001357 if (pt == PJMEDIA_RTP_PT_AMR || pt == PJMEDIA_RTP_PT_AMRWB) {
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001358 out.pBuffer += 2;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001359 }
1360#endif
1361
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001362 if (USC_NoError != ippc->fxns->Encode(codec_data->enc, &in, &out)) {
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001363 break;
1364 }
1365
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001366#if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
Nanang Izzuddin35e01de2008-10-29 10:17:02 +00001367 /* For AMR: put info (frametype, degraded, last frame, mode) in the
1368 * first two octets for payload packing.
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001369 */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001370 if (pt == PJMEDIA_RTP_PT_AMR || pt == PJMEDIA_RTP_PT_AMRWB) {
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001371 pj_uint16_t *info = (pj_uint16_t*)bits_out;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001372
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001373 /* Two octets for AMR frame info, 0=LSB:
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001374 * bit 0-3 : frame type
1375 * bit 6 : last frame flag
1376 * bit 7 : quality flag
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001377 * bit 8-11 : mode
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001378 */
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001379 out.nbytes += 2;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001380 if (out.frametype == 0 || out.frametype == 4 ||
1381 (pt == PJMEDIA_RTP_PT_AMR && out.frametype == 5) ||
1382 (pt == PJMEDIA_RTP_PT_AMRWB && out.frametype == 6))
1383 {
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001384 /* Speech frame type */
1385 *info = (char)pjmedia_codec_amr_get_mode(out.bitrate);
1386 /* Quality */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001387 if (out.frametype == 5 || out.frametype == 6)
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001388 *info |= 0x80;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001389 } else if (out.frametype == 1 || out.frametype == 2 ||
1390 (pt == PJMEDIA_RTP_PT_AMR && out.frametype == 6) ||
1391 (pt == PJMEDIA_RTP_PT_AMRWB && out.frametype == 7))
1392 {
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001393 /* SID frame type */
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001394 *info = (pj_uint8_t)(pt == PJMEDIA_RTP_PT_AMRWB? 9 : 8);
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001395 /* Quality */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001396 if (out.frametype == 6 || out.frametype == 7)
1397 *info |= 0x80;
1398 } else {
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001399 /* Untransmited */
1400 *info = 15;
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001401 out.nbytes = 2;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001402 }
1403
Nanang Izzuddind55553a2008-10-28 02:24:46 +00001404 /* Mode */
1405 *info |= (char)pjmedia_codec_amr_get_mode(out.bitrate) << 8;
1406
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001407 /* Last frame flag */
1408 if (nsamples == samples_per_frame)
1409 *info |= 0x40;
1410 }
Nanang Izzuddin7dd32682008-08-19 11:23:33 +00001411#endif
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001412
1413 pcm_in += samples_per_frame;
1414 nsamples -= samples_per_frame;
1415 tx += out.nbytes;
1416 bits_out += out.nbytes;
Nanang Izzuddin23a00b72008-08-25 13:58:25 +00001417
1418#if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
Nanang Izzuddine3a6fca2008-08-27 13:15:25 +00001419 if (pt == PJMEDIA_RTP_PT_G729) {
1420 if (out.frametype == 1) {
1421 /* SID */
1422 break;
1423 } else if (out.frametype == 0) {
1424 /* Untransmitted */
1425 tx -= out.nbytes;
1426 break;
1427 }
Nanang Izzuddin23a00b72008-08-25 13:58:25 +00001428 }
1429#endif
1430
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001431 }
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001432
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001433 if (ippc->pack != NULL) {
1434 ippc->pack(codec_data, output->buf, &tx, output_buf_len);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001435 }
1436
1437 /* Check if we don't need to transmit the frame (DTX) */
1438 if (tx == 0) {
1439 output->buf = NULL;
1440 output->size = 0;
1441 output->timestamp.u64 = input->timestamp.u64;
1442 output->type = PJMEDIA_FRAME_TYPE_NONE;
1443 return PJ_SUCCESS;
1444 }
1445
1446 output->size = tx;
1447 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1448 output->timestamp = input->timestamp;
1449
1450 return PJ_SUCCESS;
1451}
1452
1453/*
1454 * Decode frame.
1455 */
1456static pj_status_t ipp_codec_decode( pjmedia_codec *codec,
1457 const struct pjmedia_frame *input,
1458 unsigned output_buf_len,
1459 struct pjmedia_frame *output)
1460{
1461 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001462 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001463 unsigned samples_per_frame;
1464 USC_PCMStream out;
1465 USC_Bitstream in;
1466 pj_uint8_t pt;
1467
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001468 pt = ippc->pt;
1469 samples_per_frame = ippc->samples_per_frame;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001470
1471 PJ_ASSERT_RETURN(output_buf_len >= samples_per_frame << 1,
1472 PJMEDIA_CODEC_EPCMTOOSHORT);
1473
1474 if (input->type == PJMEDIA_FRAME_TYPE_AUDIO) {
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001475 if (ippc->predecode) {
1476 ippc->predecode(codec_data, input, &in);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001477 } else {
1478 /* Most IPP codecs have frametype==0 for speech frame */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001479 in.pBuffer = (char*)input->buf;
1480 in.nbytes = input->size;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001481 in.frametype = 0;
1482 in.bitrate = codec_data->info->params.modes.bitrate;
1483 }
1484
1485 out.pBuffer = output->buf;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001486 }
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001487
1488 if (input->type != PJMEDIA_FRAME_TYPE_AUDIO ||
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001489 USC_NoError != ippc->fxns->Decode(codec_data->dec, &in, &out))
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001490 {
1491 pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame);
1492 output->size = samples_per_frame << 1;
1493 output->timestamp.u64 = input->timestamp.u64;
1494 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1495 return PJ_SUCCESS;
1496 }
1497
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001498#if PJMEDIA_HAS_INTEL_IPP_CODEC_G726
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001499 /* For G.726: amplify decoding result (USC G.726 encoder deamplified it) */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001500 if (pt == PJMEDIA_RTP_PT_G726_16 || pt == PJMEDIA_RTP_PT_G726_24 ||
1501 pt == PJMEDIA_RTP_PT_G726_32 || pt == PJMEDIA_RTP_PT_G726_40)
1502 {
1503 unsigned i;
1504 pj_int16_t *s = (pj_int16_t*)output->buf;
1505
1506 for (i = 0; i < samples_per_frame; ++i)
1507 s[i] <<= 2;
1508 }
1509#endif
1510
1511 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1512 output->size = samples_per_frame << 1;
1513 output->timestamp.u64 = input->timestamp.u64;
1514
1515 /* Invoke external PLC if codec has no internal PLC */
1516 if (codec_data->plc && codec_data->plc_enabled)
1517 pjmedia_plc_save(codec_data->plc, (pj_int16_t*)output->buf);
1518
1519 return PJ_SUCCESS;
1520}
1521
1522/*
1523 * Recover lost frame.
1524 */
1525static pj_status_t ipp_codec_recover(pjmedia_codec *codec,
1526 unsigned output_buf_len,
1527 struct pjmedia_frame *output)
1528{
1529 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001530 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001531 unsigned samples_per_frame;
1532
1533 PJ_UNUSED_ARG(output_buf_len);
1534
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001535 samples_per_frame = ippc->samples_per_frame;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001536
1537 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1538 output->size = samples_per_frame << 1;
1539
1540 if (codec_data->plc_enabled) {
1541 if (codec_data->plc) {
1542 pjmedia_plc_generate(codec_data->plc, (pj_int16_t*)output->buf);
1543 } else {
1544 USC_PCMStream out;
1545 out.pBuffer = output->buf;
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001546 ippc->fxns->Decode(codec_data->dec, NULL, &out);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001547 }
1548 } else {
1549 pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame);
1550 }
1551
1552 return PJ_SUCCESS;
1553}
1554
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001555
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001556#if defined(_MSC_VER) && PJMEDIA_AUTO_LINK_IPP_LIBS
1557# pragma comment( lib, "ippcore.lib")
1558# pragma comment( lib, "ipps.lib")
1559# pragma comment( lib, "ippsc.lib")
1560# pragma comment( lib, "ippsr.lib")
Benny Prijonoc543e9e2009-01-03 12:19:53 +00001561//# pragma comment( lib, "ippcorel.lib")
1562//# pragma comment( lib, "ippsemerged.lib")
1563//# pragma comment( lib, "ippsmerged.lib")
1564//# pragma comment( lib, "ippscemerged.lib")
1565//# pragma comment( lib, "ippscmerged.lib")
1566//# pragma comment( lib, "ippsremerged.lib")
1567//# pragma comment( lib, "ippsrmerged.lib")
Benny Prijonoe1e6d512009-01-02 15:17:47 +00001568# if defined(IPP_VERSION_MAJOR) && IPP_VERSION_MAJOR>=6
1569# pragma comment( lib, "speech.lib")
1570# else
1571# pragma comment( lib, "usc.lib")
1572# endif
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001573#endif
1574
1575
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +00001576#endif /* PJMEDIA_HAS_INTEL_IPP */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001577