1 /* $Id$ */
2 /*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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/amr_sdp_match.h>
22 #include <pjmedia-codec/g7221_sdp_match.h>
23 #include <pjmedia/codec.h>
24 #include <pjmedia/errno.h>
25 #include <pjmedia/endpoint.h>
26 #include <pjmedia/plc.h>
27 #include <pjmedia/port.h>
28 #include <pjmedia/silencedet.h>
29 #include <pj/assert.h>
30 #include <pj/log.h>
31 #include <pj/math.h>
32 #include <pj/pool.h>
33 #include <pj/string.h>
34 #include <pj/os.h>
35
36
37 /*
38 * Only build this file if PJMEDIA_HAS_INTEL_IPP != 0
39 */
40 #if defined(PJMEDIA_HAS_INTEL_IPP) && PJMEDIA_HAS_INTEL_IPP != 0
41
42 #include <usc.h>
43 #include <ippversion.h>
44
45 #define THIS_FILE "ipp_codecs.c"
46
47
48 /* Prototypes for IPP codecs factory */
49 static pj_status_t ipp_test_alloc( pjmedia_codec_factory *factory,
50 const pjmedia_codec_info *id );
51 static pj_status_t ipp_default_attr( pjmedia_codec_factory *factory,
52 const pjmedia_codec_info *id,
53 pjmedia_codec_param *attr );
54 static pj_status_t ipp_enum_codecs( pjmedia_codec_factory *factory,
55 unsigned *count,
56 pjmedia_codec_info codecs[]);
57 static pj_status_t ipp_alloc_codec( pjmedia_codec_factory *factory,
58 const pjmedia_codec_info *id,
59 pjmedia_codec **p_codec);
60 static pj_status_t ipp_dealloc_codec( pjmedia_codec_factory *factory,
61 pjmedia_codec *codec );
62
63 /* Prototypes for IPP codecs implementation. */
64 static pj_status_t ipp_codec_init( pjmedia_codec *codec,
65 pj_pool_t *pool );
66 static pj_status_t ipp_codec_open( pjmedia_codec *codec,
67 pjmedia_codec_param *attr );
68 static pj_status_t ipp_codec_close( pjmedia_codec *codec );
69 static pj_status_t ipp_codec_modify(pjmedia_codec *codec,
70 const pjmedia_codec_param *attr );
71 static pj_status_t ipp_codec_parse( pjmedia_codec *codec,
72 void *pkt,
73 pj_size_t pkt_size,
74 const pj_timestamp *ts,
75 unsigned *frame_cnt,
76 pjmedia_frame frames[]);
77 static pj_status_t ipp_codec_encode( pjmedia_codec *codec,
78 const struct pjmedia_frame *input,
79 unsigned output_buf_len,
80 struct pjmedia_frame *output);
81 static pj_status_t ipp_codec_decode( pjmedia_codec *codec,
82 const struct pjmedia_frame *input,
83 unsigned output_buf_len,
84 struct pjmedia_frame *output);
85 static pj_status_t ipp_codec_recover(pjmedia_codec *codec,
86 unsigned output_buf_len,
87 struct pjmedia_frame *output);
88
89 /* Definition for IPP codecs operations. */
90 static pjmedia_codec_op ipp_op =
91 {
92 &ipp_codec_init,
93 &ipp_codec_open,
94 &ipp_codec_close,
95 &ipp_codec_modify,
96 &ipp_codec_parse,
97 &ipp_codec_encode,
98 &ipp_codec_decode,
99 &ipp_codec_recover
100 };
101
102 /* Definition for IPP codecs factory operations. */
103 static pjmedia_codec_factory_op ipp_factory_op =
104 {
105 &ipp_test_alloc,
106 &ipp_default_attr,
107 &ipp_enum_codecs,
108 &ipp_alloc_codec,
109 &ipp_dealloc_codec,
110 &pjmedia_codec_ipp_deinit
111 };
112
113 /* IPP codecs factory */
114 static struct ipp_factory {
115 pjmedia_codec_factory base;
116 pjmedia_endpt *endpt;
117 pj_pool_t *pool;
118 pj_mutex_t *mutex;
119 unsigned g7221_pcm_shift;
120 } ipp_factory;
121
122 /* IPP codecs private data. */
123 typedef struct ipp_private {
124 int codec_idx; /**< Codec index. */
125 void *codec_setting; /**< Specific codec setting. */
126 pj_pool_t *pool; /**< Pool for each instance. */
127
128 USC_Handle enc; /**< Encoder state. */
129 USC_Handle dec; /**< Decoder state. */
130 USC_CodecInfo *info; /**< Native codec info. */
131 pj_uint16_t frame_size; /**< Bitstream frame size. */
132
133 pj_bool_t plc_enabled; /**< PLC enabled flag. */
134 pjmedia_plc *plc; /**< PJMEDIA PLC engine, NULL if
135 codec has internal PLC. */
136
137 pj_bool_t vad_enabled; /**< VAD enabled flag. */
138 pjmedia_silence_det *vad; /**< PJMEDIA VAD engine, NULL if
139 codec has internal VAD. */
140 pj_timestamp last_tx; /**< Timestamp of last transmit.*/
141
142 unsigned g7221_pcm_shift; /**< G722.1 PCM level adjustment*/
143 } ipp_private_t;
144
145
146 /* USC codec implementations. */
147 extern USC_Fxns USC_G729AFP_Fxns;
148 extern USC_Fxns USC_G729I_Fxns;
149 extern USC_Fxns USC_G723_Fxns;
150 extern USC_Fxns USC_G726_Fxns;
151 extern USC_Fxns USC_G728_Fxns;
152 extern USC_Fxns USC_G722_Fxns;
153 extern USC_Fxns USC_GSMAMR_Fxns;
154 extern USC_Fxns USC_AMRWB_Fxns;
155 extern USC_Fxns USC_AMRWBE_Fxns;
156
157
158 /* CUSTOM CALLBACKS */
159
160 /* This callback is useful for translating RTP frame into USC frame, e.g:
161 * reassigning frame attributes, reorder bitstream. Default behaviour of
162 * the translation is just setting the USC frame buffer & its size as
163 * specified in RTP frame, setting USC frame frametype to 0, setting bitrate
164 * of USC frame to bitrate info of codec_data. Implement this callback when
165 * the default behaviour is unapplicable.
166 */
167 typedef void (*predecode_cb)(ipp_private_t *codec_data,
168 const pjmedia_frame *rtp_frame,
169 USC_Bitstream *usc_frame);
170
171 /* Parse frames from a packet. Default behaviour of frame parsing is
172 * just separating frames based on calculating frame length derived
173 * from bitrate. Implement this callback when the default behaviour is
174 * unapplicable.
175 */
176 typedef pj_status_t (*parse_cb)(ipp_private_t *codec_data, void *pkt,
177 pj_size_t pkt_size, const pj_timestamp *ts,
178 unsigned *frame_cnt, pjmedia_frame frames[]);
179
180 /* Pack frames into a packet. Default behaviour of packing frames is
181 * just stacking the frames with octet aligned without adding any
182 * payload header. Implement this callback when the default behaviour is
183 * unapplicable.
184 */
185 typedef pj_status_t (*pack_cb)(ipp_private_t *codec_data, void *pkt,
186 pj_size_t *pkt_size, pj_size_t max_pkt_size);
187
188
189
190 /* Custom callback implementations. */
191 static void predecode_g723( ipp_private_t *codec_data,
192 const pjmedia_frame *rtp_frame,
193 USC_Bitstream *usc_frame);
194 static pj_status_t parse_g723( ipp_private_t *codec_data, void *pkt,
195 pj_size_t pkt_size, const pj_timestamp *ts,
196 unsigned *frame_cnt, pjmedia_frame frames[]);
197
198 static void predecode_g729( ipp_private_t *codec_data,
199 const pjmedia_frame *rtp_frame,
200 USC_Bitstream *usc_frame);
201
202 static void predecode_amr( ipp_private_t *codec_data,
203 const pjmedia_frame *rtp_frame,
204 USC_Bitstream *usc_frame);
205 static pj_status_t parse_amr( ipp_private_t *codec_data, void *pkt,
206 pj_size_t pkt_size, const pj_timestamp *ts,
207 unsigned *frame_cnt, pjmedia_frame frames[]);
208 static pj_status_t pack_amr( ipp_private_t *codec_data, void *pkt,
209 pj_size_t *pkt_size, pj_size_t max_pkt_size);
210
211 static void predecode_g7221( ipp_private_t *codec_data,
212 const pjmedia_frame *rtp_frame,
213 USC_Bitstream *usc_frame);
214 static pj_status_t pack_g7221( ipp_private_t *codec_data, void *pkt,
215 pj_size_t *pkt_size, pj_size_t max_pkt_size);
216
217 /* IPP codec implementation descriptions. */
218 static struct ipp_codec {
219 int enabled; /* Is this codec enabled? */
220 const char *name; /* Codec name. */
221 pj_uint8_t pt; /* Payload type. */
222 USC_Fxns *fxns; /* USC callback functions. */
223 unsigned clock_rate; /* Codec's clock rate. */
224 unsigned channel_count; /* Codec's channel count. */
225 unsigned samples_per_frame; /* Codec's samples count. */
226
227 unsigned def_bitrate; /* Default bitrate of this codec. */
228 unsigned max_bitrate; /* Maximum bitrate of this codec. */
229 pj_uint8_t frm_per_pkt; /* Default num of frames per packet.*/
230 int has_native_vad; /* Codec has internal VAD? */
231 int has_native_plc; /* Codec has internal PLC? */
232
233 predecode_cb predecode; /* Callback to translate RTP frame
234 into USC frame. */
235 parse_cb parse; /* Callback to parse bitstream. */
236 pack_cb pack; /* Callback to pack bitstream. */
237
238 pjmedia_codec_fmtp dec_fmtp; /* Decoder's fmtp params. */
239 }
240
241 ipp_codec[] =
242 {
243 # if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
244 {1, "AMR", PJMEDIA_RTP_PT_AMR, &USC_GSMAMR_Fxns, 8000, 1, 160,
245 7400, 12200, 2, 1, 1,
246 &predecode_amr, &parse_amr, &pack_amr,
247 {1, {{{"octet-align", 11}, {"1", 1}}} }
248 },
249 # endif
250
251 # if PJMEDIA_HAS_INTEL_IPP_CODEC_AMRWB
252 {1, "AMR-WB", PJMEDIA_RTP_PT_AMRWB, &USC_AMRWB_Fxns, 16000, 1, 320,
253 15850, 23850, 2, 1, 1,
254 &predecode_amr, &parse_amr, &pack_amr,
255 {1, {{{"octet-align", 11}, {"1", 1}}} }
256 },
257 # endif
258
259 # if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
260 # if defined(PJ_HAS_FLOATING_POINT) && (PJ_HAS_FLOATING_POINT != 0)
261 {1, "G729", PJMEDIA_RTP_PT_G729, &USC_G729AFP_Fxns, 8000, 1, 80,
262 8000, 11800, 2, 1, 1,
263 &predecode_g729, NULL, NULL
264 },
265 # else
266 {1, "G729", PJMEDIA_RTP_PT_G729, &USC_G729I_Fxns, 8000, 1, 80,
267 8000, 11800, 2, 1, 1,
268 &predecode_g729, NULL, NULL
269 },
270 # endif
271 # endif
272
273 # if PJMEDIA_HAS_INTEL_IPP_CODEC_G723_1
274 /* This is actually G.723.1 */
275 {1, "G723", PJMEDIA_RTP_PT_G723, &USC_G723_Fxns, 8000, 1, 240,
276 6300, 6300, 1, 1, 1,
277 &predecode_g723, &parse_g723, NULL
278 },
279 # endif
280
281 # if PJMEDIA_HAS_INTEL_IPP_CODEC_G726
282 {0, "G726-16", PJMEDIA_RTP_PT_G726_16, &USC_G726_Fxns, 8000, 1, 80,
283 16000, 16000, 2, 0, 0,
284 NULL, NULL, NULL
285 },
286 {0, "G726-24", PJMEDIA_RTP_PT_G726_24, &USC_G726_Fxns, 8000, 1, 80,
287 24000, 24000, 2, 0, 0,
288 NULL, NULL, NULL
289 },
290 {1, "G726-32", PJMEDIA_RTP_PT_G726_32, &USC_G726_Fxns, 8000, 1, 80,
291 32000, 32000, 2, 0, 0,
292 NULL, NULL, NULL
293 },
294 {0, "G726-40", PJMEDIA_RTP_PT_G726_40, &USC_G726_Fxns, 8000, 1, 80,
295 40000, 40000, 2, 0, 0,
296 NULL, NULL, NULL
297 },
298 /* Old definition of G726-32 */
299 {1, "G721", PJMEDIA_RTP_PT_G721, &USC_G726_Fxns, 8000, 1, 80,
300 32000, 32000, 2, 0, 0,
301 NULL, NULL, NULL
302 },
303 # endif
304
305 # if PJMEDIA_HAS_INTEL_IPP_CODEC_G728
306 {1, "G728", PJMEDIA_RTP_PT_G728, &USC_G728_Fxns, 8000, 1, 80,
307 16000, 16000, 2, 0, 1,
308 NULL, NULL, NULL
309 },
310 # endif
311
312 # if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1
313 {0, "G7221", PJMEDIA_RTP_PT_G722_1_16, &USC_G722_Fxns, 16000, 1, 320,
314 16000, 16000, 1, 0, 1,
315 predecode_g7221, NULL, pack_g7221,
316 {1, {{{"bitrate", 7}, {"16000", 5}}} }
317 },
318 {1, "G7221", PJMEDIA_RTP_PT_G722_1_24, &USC_G722_Fxns, 16000, 1, 320,
319 24000, 24000, 1, 0, 1,
320 predecode_g7221, NULL, pack_g7221,
321 {1, {{{"bitrate", 7}, {"24000", 5}}} }
322 },
323 {1, "G7221", PJMEDIA_RTP_PT_G722_1_32, &USC_G722_Fxns, 16000, 1, 320,
324 32000, 32000, 1, 0, 1,
325 predecode_g7221, NULL, pack_g7221,
326 {1, {{{"bitrate", 7}, {"32000", 5}}} }
327 },
328 # endif
329 };
330
331
332 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
333
predecode_g729(ipp_private_t * codec_data,const pjmedia_frame * rtp_frame,USC_Bitstream * usc_frame)334 static void predecode_g729( ipp_private_t *codec_data,
335 const pjmedia_frame *rtp_frame,
336 USC_Bitstream *usc_frame)
337 {
338 switch (rtp_frame->size) {
339 case 2:
340 /* SID */
341 usc_frame->frametype = 1;
342 usc_frame->bitrate = codec_data->info->params.modes.bitrate;
343 break;
344 case 8:
345 /* G729D */
346 usc_frame->frametype = 2;
347 usc_frame->bitrate = 6400;
348 break;
349 case 10:
350 /* G729 */
351 usc_frame->frametype = 3;
352 usc_frame->bitrate = 8000;
353 break;
354 case 15:
355 /* G729E */
356 usc_frame->frametype = 4;
357 usc_frame->bitrate = 11800;
358 break;
359 default:
360 usc_frame->frametype = 0;
361 usc_frame->bitrate = 0;
362 break;
363 }
364
365 usc_frame->pBuffer = rtp_frame->buf;
366 usc_frame->nbytes = rtp_frame->size;
367 }
368
369 #endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G729 */
370
371
372 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G723_1
373
predecode_g723(ipp_private_t * codec_data,const pjmedia_frame * rtp_frame,USC_Bitstream * usc_frame)374 static void predecode_g723( ipp_private_t *codec_data,
375 const pjmedia_frame *rtp_frame,
376 USC_Bitstream *usc_frame)
377 {
378 int i, HDR = 0;
379 pj_uint8_t *f = (pj_uint8_t*)rtp_frame->buf;
380
381 PJ_UNUSED_ARG(codec_data);
382
383 for (i = 0; i < 2; ++i){
384 int tmp;
385 tmp = (f[0] >> (i & 0x7)) & 1;
386 HDR += tmp << i ;
387 }
388
389 usc_frame->pBuffer = rtp_frame->buf;
390 usc_frame->nbytes = rtp_frame->size;
391 usc_frame->bitrate = HDR == 0? 6300 : 5300;
392 usc_frame->frametype = 0;
393 }
394
parse_g723(ipp_private_t * codec_data,void * pkt,pj_size_t pkt_size,const pj_timestamp * ts,unsigned * frame_cnt,pjmedia_frame frames[])395 static pj_status_t parse_g723(ipp_private_t *codec_data, void *pkt,
396 pj_size_t pkt_size, const pj_timestamp *ts,
397 unsigned *frame_cnt, pjmedia_frame frames[])
398 {
399 unsigned count = 0;
400 pj_uint8_t *f = (pj_uint8_t*)pkt;
401
402 while (pkt_size && count < *frame_cnt) {
403 int framesize, i, j;
404 int HDR = 0;
405
406 for (i = 0; i < 2; ++i){
407 j = (f[0] >> (i & 0x7)) & 1;
408 HDR += j << i ;
409 }
410
411 if (HDR == 0)
412 framesize = 24;
413 else if (HDR == 1)
414 framesize = 20;
415 else if (HDR == 2)
416 framesize = 4;
417 else if (HDR == 3)
418 framesize = 1;
419 else {
420 pj_assert(!"Unknown G723.1 frametype, packet may be corrupted!");
421 return PJMEDIA_CODEC_EINMODE;
422 }
423
424 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
425 frames[count].buf = f;
426 frames[count].size = framesize;
427 frames[count].timestamp.u64 = ts->u64 + count *
428 ipp_codec[codec_data->codec_idx].samples_per_frame;
429
430 f += framesize;
431 pkt_size -= framesize;
432
433 ++count;
434 }
435
436 *frame_cnt = count;
437 return PJ_SUCCESS;
438 }
439
440 #endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G723_1 */
441
442
443 #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR || PJMEDIA_HAS_INTEL_IPP_CODEC_AMRWB
444
445 #include <pjmedia-codec/amr_helper.h>
446
447 typedef struct amr_settings_t {
448 pjmedia_codec_amr_pack_setting enc_setting;
449 pjmedia_codec_amr_pack_setting dec_setting;
450 pj_int8_t enc_mode;
451 } amr_settings_t;
452
453
454 /* Rearrange AMR bitstream and convert RTP frame into USC frame:
455 * - make the start_bit to be 0
456 * - if it is speech frame, reorder bitstream from sensitivity bits order
457 * to encoder bits order.
458 * - set the appropriate value of usc_frame.
459 */
predecode_amr(ipp_private_t * codec_data,const pjmedia_frame * rtp_frame,USC_Bitstream * usc_frame)460 static void predecode_amr( ipp_private_t *codec_data,
461 const pjmedia_frame *rtp_frame,
462 USC_Bitstream *usc_frame)
463 {
464 pjmedia_frame frame;
465 pjmedia_codec_amr_bit_info *info;
466 pjmedia_codec_amr_pack_setting *setting;
467
468 setting = &((amr_settings_t*)codec_data->codec_setting)->dec_setting;
469
470 frame = *rtp_frame;
471 pjmedia_codec_amr_predecode(rtp_frame, setting, &frame);
472 info = (pjmedia_codec_amr_bit_info*) &frame.bit_info;
473
474 usc_frame->pBuffer = frame.buf;
475 usc_frame->nbytes = frame.size;
476 if (info->mode != -1) {
477 usc_frame->bitrate = setting->amr_nb?
478 pjmedia_codec_amrnb_bitrates[info->mode]:
479 pjmedia_codec_amrwb_bitrates[info->mode];
480 } else {
481 usc_frame->bitrate = 0;
482 }
483
484 if (frame.size > 5) {
485 /* Speech */
486 if (info->good_quality)
487 usc_frame->frametype = 0;
488 else
489 usc_frame->frametype = setting->amr_nb ? 5 : 6;
490 } else if (frame.size == 5) {
491 /* SID */
492 if (info->good_quality) {
493 usc_frame->frametype = info->STI? 2 : 1;
494 } else {
495 usc_frame->frametype = setting->amr_nb ? 6 : 7;
496 }
497 } else {
498 /* no data */
499 usc_frame->frametype = 3;
500 }
501 }
502
503 /* Pack AMR payload */
pack_amr(ipp_private_t * codec_data,void * pkt,pj_size_t * pkt_size,pj_size_t max_pkt_size)504 static pj_status_t pack_amr(ipp_private_t *codec_data, void *pkt,
505 pj_size_t *pkt_size, pj_size_t max_pkt_size)
506 {
507 enum {MAX_FRAMES_PER_PACKET = PJMEDIA_MAX_FRAME_DURATION_MS / 20};
508
509 pjmedia_frame frames[MAX_FRAMES_PER_PACKET];
510 unsigned nframes = 0;
511 pjmedia_codec_amr_bit_info *info;
512 pj_uint8_t *r; /* Read cursor */
513 pj_uint8_t SID_FT;
514 pjmedia_codec_amr_pack_setting *setting;
515 const pj_uint8_t *framelen_tbl;
516
517 setting = &((amr_settings_t*)codec_data->codec_setting)->enc_setting;
518 framelen_tbl = setting->amr_nb? pjmedia_codec_amrnb_framelen:
519 pjmedia_codec_amrwb_framelen;
520
521 SID_FT = (pj_uint8_t)(setting->amr_nb? 8 : 9);
522
523 /* Align pkt buf right */
524 r = (pj_uint8_t*)pkt + max_pkt_size - *pkt_size;
525 pj_memmove(r, pkt, *pkt_size);
526
527 /* Get frames */
528 for (;;) {
529 pj_bool_t eof;
530 pj_uint16_t info_;
531
532 info_ = *((pj_uint16_t*)r);
533 eof = ((info_ & 0x40) != 0);
534
535 info = (pjmedia_codec_amr_bit_info*) &frames[nframes].bit_info;
536 pj_bzero(info, sizeof(*info));
537 info->frame_type = (pj_uint8_t)(info_ & 0x0F);
538 info->good_quality = (pj_uint8_t)((info_ & 0x80) == 0);
539 info->mode = (pj_int8_t) ((info_ >> 8) & 0x0F);
540 info->STI = (pj_uint8_t)((info_ >> 5) & 1);
541
542 frames[nframes].buf = r + 2;
543 frames[nframes].size = info->frame_type <= SID_FT ?
544 framelen_tbl[info->frame_type] : 0;
545
546 r += frames[nframes].size + 2;
547
548 /* Last frame */
549 if (++nframes >= MAX_FRAMES_PER_PACKET || eof)
550 break;
551 }
552
553 /* Pack */
554 *pkt_size = max_pkt_size;
555 return pjmedia_codec_amr_pack(frames, nframes, setting, pkt, pkt_size);
556 }
557
558
559 /* Parse AMR payload into frames. */
parse_amr(ipp_private_t * codec_data,void * pkt,pj_size_t pkt_size,const pj_timestamp * ts,unsigned * frame_cnt,pjmedia_frame frames[])560 static pj_status_t parse_amr(ipp_private_t *codec_data, void *pkt,
561 pj_size_t pkt_size, const pj_timestamp *ts,
562 unsigned *frame_cnt, pjmedia_frame frames[])
563 {
564 amr_settings_t* s = (amr_settings_t*)codec_data->codec_setting;
565 pjmedia_codec_amr_pack_setting *setting;
566 pj_status_t status;
567 pj_uint8_t cmr;
568
569 setting = &s->dec_setting;
570
571 status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, setting, frames,
572 frame_cnt, &cmr);
573 if (status != PJ_SUCCESS)
574 return status;
575
576 /* Check Change Mode Request. */
577 if (((setting->amr_nb && cmr <= 7) || (!setting->amr_nb && cmr <= 8)) &&
578 s->enc_mode != cmr)
579 {
580 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
581
582 s->enc_mode = cmr;
583 codec_data->info->params.modes.bitrate = s->enc_setting.amr_nb?
584 pjmedia_codec_amrnb_bitrates[s->enc_mode] :
585 pjmedia_codec_amrwb_bitrates[s->enc_mode];
586 ippc->fxns->std.Control(&codec_data->info->params.modes,
587 codec_data->enc);
588
589 PJ_LOG(4,(THIS_FILE, "AMR%s switched encoding mode to: %d (%dbps)",
590 (s->enc_setting.amr_nb?"":"-WB"),
591 s->enc_mode,
592 codec_data->info->params.modes.bitrate));
593 }
594
595 return PJ_SUCCESS;
596 }
597
598 #endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_AMR */
599
600
601 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1
602
predecode_g7221(ipp_private_t * codec_data,const pjmedia_frame * rtp_frame,USC_Bitstream * usc_frame)603 static void predecode_g7221( ipp_private_t *codec_data,
604 const pjmedia_frame *rtp_frame,
605 USC_Bitstream *usc_frame)
606 {
607 usc_frame->pBuffer = (char*)rtp_frame->buf;
608 usc_frame->nbytes = rtp_frame->size;
609 usc_frame->frametype = 0;
610 usc_frame->bitrate = codec_data->info->params.modes.bitrate;
611
612 #if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0
613 {
614 pj_uint16_t *p, *p_end;
615
616 p = (pj_uint16_t*)rtp_frame->buf;
617 p_end = p + rtp_frame->size/2;
618 while (p < p_end) {
619 *p = pj_ntohs(*p);
620 ++p;
621 }
622 }
623 #endif
624 }
625
pack_g7221(ipp_private_t * codec_data,void * pkt,pj_size_t * pkt_size,pj_size_t max_pkt_size)626 static pj_status_t pack_g7221( ipp_private_t *codec_data, void *pkt,
627 pj_size_t *pkt_size, pj_size_t max_pkt_size)
628 {
629 PJ_UNUSED_ARG(codec_data);
630 PJ_UNUSED_ARG(max_pkt_size);
631
632 #if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0
633 {
634 pj_uint16_t *p, *p_end;
635
636 p = (pj_uint16_t*)pkt;
637 p_end = p + *pkt_size/2;
638 while (p < p_end) {
639 *p = pj_htons(*p);
640 ++p;
641 }
642 }
643 #else
644 PJ_UNUSED_ARG(pkt);
645 PJ_UNUSED_ARG(pkt_size);
646 #endif
647
648 return PJ_SUCCESS;
649 }
650
651
652 #include <pjmedia-codec/g7221.h>
653
654
pjmedia_codec_g7221_set_pcm_shift(int val)655 PJ_DEF(pj_status_t) pjmedia_codec_g7221_set_pcm_shift(int val)
656 {
657 PJ_ASSERT_RETURN(val >= 0, PJ_EINVAL);
658
659 ipp_factory.g7221_pcm_shift = val;
660 return PJ_SUCCESS;
661 }
662
663
664 #endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1 */
665
666 /*
667 * Initialize and register IPP codec factory to pjmedia endpoint.
668 */
pjmedia_codec_ipp_init(pjmedia_endpt * endpt)669 PJ_DEF(pj_status_t) pjmedia_codec_ipp_init( pjmedia_endpt *endpt )
670 {
671 pjmedia_codec_mgr *codec_mgr;
672 pj_str_t codec_name;
673 pj_status_t status;
674
675 if (ipp_factory.pool != NULL) {
676 /* Already initialized. */
677 return PJ_SUCCESS;
678 }
679
680 /* Create IPP codec factory. */
681 ipp_factory.base.op = &ipp_factory_op;
682 ipp_factory.base.factory_data = NULL;
683 ipp_factory.endpt = endpt;
684 ipp_factory.g7221_pcm_shift = PJMEDIA_G7221_DEFAULT_PCM_SHIFT;
685
686 ipp_factory.pool = pjmedia_endpt_create_pool(endpt, "IPP codecs", 4000, 4000);
687 if (!ipp_factory.pool)
688 return PJ_ENOMEM;
689
690 /* Create mutex. */
691 status = pj_mutex_create_simple(ipp_factory.pool, "IPP codecs",
692 &ipp_factory.mutex);
693 if (status != PJ_SUCCESS)
694 goto on_error;
695
696 /* Get the codec manager. */
697 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
698 if (!codec_mgr) {
699 status = PJ_EINVALIDOP;
700 goto on_error;
701 }
702
703 /* Register format match callback. */
704 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1
705 pj_cstr(&codec_name, "G7221");
706 status = pjmedia_sdp_neg_register_fmt_match_cb(
707 &codec_name,
708 &pjmedia_codec_g7221_match_sdp);
709 if (status != PJ_SUCCESS)
710 goto on_error;
711 #endif
712
713 #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
714 pj_cstr(&codec_name, "AMR");
715 status = pjmedia_sdp_neg_register_fmt_match_cb(
716 &codec_name,
717 &pjmedia_codec_amr_match_sdp);
718 if (status != PJ_SUCCESS)
719 goto on_error;
720 #endif
721
722 #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMRWB
723 pj_cstr(&codec_name, "AMR-WB");
724 status = pjmedia_sdp_neg_register_fmt_match_cb(
725 &codec_name,
726 &pjmedia_codec_amr_match_sdp);
727 if (status != PJ_SUCCESS)
728 goto on_error;
729 #endif
730
731 /* Suppress compile warning */
732 PJ_UNUSED_ARG(codec_name);
733
734 /* Register codec factory to endpoint. */
735 status = pjmedia_codec_mgr_register_factory(codec_mgr,
736 &ipp_factory.base);
737 if (status != PJ_SUCCESS)
738 goto on_error;
739
740 /* Done. */
741 return PJ_SUCCESS;
742
743 on_error:
744 pj_pool_release(ipp_factory.pool);
745 ipp_factory.pool = NULL;
746 return status;
747 }
748
749 /*
750 * Unregister IPP codecs factory from pjmedia endpoint.
751 */
pjmedia_codec_ipp_deinit(void)752 PJ_DEF(pj_status_t) pjmedia_codec_ipp_deinit(void)
753 {
754 pjmedia_codec_mgr *codec_mgr;
755 pj_status_t status;
756
757 if (ipp_factory.pool == NULL) {
758 /* Already deinitialized */
759 return PJ_SUCCESS;
760 }
761
762 pj_mutex_lock(ipp_factory.mutex);
763
764 /* Get the codec manager. */
765 codec_mgr = pjmedia_endpt_get_codec_mgr(ipp_factory.endpt);
766 if (!codec_mgr) {
767 pj_pool_release(ipp_factory.pool);
768 ipp_factory.pool = NULL;
769 pj_mutex_unlock(ipp_factory.mutex);
770 return PJ_EINVALIDOP;
771 }
772
773 /* Unregister IPP codecs factory. */
774 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
775 &ipp_factory.base);
776
777 /* Destroy mutex. */
778 pj_mutex_unlock(ipp_factory.mutex);
779 pj_mutex_destroy(ipp_factory.mutex);
780 ipp_factory.mutex = NULL;
781
782 /* Destroy pool. */
783 pj_pool_release(ipp_factory.pool);
784 ipp_factory.pool = NULL;
785
786 return status;
787 }
788
789
790 /*
791 * Check if factory can allocate the specified codec.
792 */
ipp_test_alloc(pjmedia_codec_factory * factory,const pjmedia_codec_info * info)793 static pj_status_t ipp_test_alloc( pjmedia_codec_factory *factory,
794 const pjmedia_codec_info *info )
795 {
796 unsigned i;
797
798 PJ_UNUSED_ARG(factory);
799
800 /* Type MUST be audio. */
801 if (info->type != PJMEDIA_TYPE_AUDIO)
802 return PJMEDIA_CODEC_EUNSUP;
803
804 for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) {
805 pj_str_t name = pj_str((char*)ipp_codec[i].name);
806 if ((pj_stricmp(&info->encoding_name, &name) == 0) &&
807 (info->clock_rate == (unsigned)ipp_codec[i].clock_rate) &&
808 (info->channel_cnt == (unsigned)ipp_codec[i].channel_count) &&
809 (ipp_codec[i].enabled))
810 {
811 return PJ_SUCCESS;
812 }
813 }
814
815 /* Unsupported, or mode is disabled. */
816 return PJMEDIA_CODEC_EUNSUP;
817 }
818
819 /*
820 * Generate default attribute.
821 */
ipp_default_attr(pjmedia_codec_factory * factory,const pjmedia_codec_info * id,pjmedia_codec_param * attr)822 static pj_status_t ipp_default_attr (pjmedia_codec_factory *factory,
823 const pjmedia_codec_info *id,
824 pjmedia_codec_param *attr )
825 {
826 unsigned i;
827
828 PJ_ASSERT_RETURN(factory==&ipp_factory.base, PJ_EINVAL);
829
830 pj_bzero(attr, sizeof(pjmedia_codec_param));
831
832 for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) {
833 pj_str_t name = pj_str((char*)ipp_codec[i].name);
834 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
835 (id->clock_rate == (unsigned)ipp_codec[i].clock_rate) &&
836 (id->channel_cnt == (unsigned)ipp_codec[i].channel_count) &&
837 (id->pt == (unsigned)ipp_codec[i].pt))
838 {
839 attr->info.pt = (pj_uint8_t)id->pt;
840 attr->info.channel_cnt = ipp_codec[i].channel_count;
841 attr->info.clock_rate = ipp_codec[i].clock_rate;
842 attr->info.avg_bps = ipp_codec[i].def_bitrate;
843 attr->info.max_bps = ipp_codec[i].max_bitrate;
844 attr->info.pcm_bits_per_sample = 16;
845 attr->info.frm_ptime = (pj_uint16_t)
846 (ipp_codec[i].samples_per_frame * 1000 /
847 ipp_codec[i].channel_count /
848 ipp_codec[i].clock_rate);
849 attr->setting.frm_per_pkt = ipp_codec[i].frm_per_pkt;
850
851 /* Default flags. */
852 attr->setting.plc = 1;
853 attr->setting.penh= 0;
854 attr->setting.vad = 1;
855 attr->setting.cng = attr->setting.vad;
856 attr->setting.dec_fmtp = ipp_codec[i].dec_fmtp;
857
858 if (attr->setting.vad == 0) {
859 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
860 if (id->pt == PJMEDIA_RTP_PT_G729) {
861 /* Signal G729 Annex B is being disabled */
862 attr->setting.dec_fmtp.cnt = 1;
863 pj_strset2(&attr->setting.dec_fmtp.param[0].name, "annexb");
864 pj_strset2(&attr->setting.dec_fmtp.param[0].val, "no");
865 }
866 #endif
867 }
868
869 return PJ_SUCCESS;
870 }
871 }
872
873 return PJMEDIA_CODEC_EUNSUP;
874 }
875
876 /*
877 * Enum codecs supported by this factory.
878 */
ipp_enum_codecs(pjmedia_codec_factory * factory,unsigned * count,pjmedia_codec_info codecs[])879 static pj_status_t ipp_enum_codecs(pjmedia_codec_factory *factory,
880 unsigned *count,
881 pjmedia_codec_info codecs[])
882 {
883 unsigned max;
884 unsigned i;
885
886 PJ_UNUSED_ARG(factory);
887 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
888
889 max = *count;
890
891 for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(ipp_codec) && *count < max; ++i)
892 {
893 if (!ipp_codec[i].enabled)
894 continue;
895
896 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
897 codecs[*count].encoding_name = pj_str((char*)ipp_codec[i].name);
898 codecs[*count].pt = ipp_codec[i].pt;
899 codecs[*count].type = PJMEDIA_TYPE_AUDIO;
900 codecs[*count].clock_rate = ipp_codec[i].clock_rate;
901 codecs[*count].channel_cnt = ipp_codec[i].channel_count;
902
903 ++*count;
904 }
905
906 return PJ_SUCCESS;
907 }
908
909 /*
910 * Allocate a new codec instance.
911 */
ipp_alloc_codec(pjmedia_codec_factory * factory,const pjmedia_codec_info * id,pjmedia_codec ** p_codec)912 static pj_status_t ipp_alloc_codec( pjmedia_codec_factory *factory,
913 const pjmedia_codec_info *id,
914 pjmedia_codec **p_codec)
915 {
916 ipp_private_t *codec_data;
917 pjmedia_codec *codec;
918 int idx;
919 pj_pool_t *pool;
920 unsigned i;
921
922 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
923 PJ_ASSERT_RETURN(factory == &ipp_factory.base, PJ_EINVAL);
924
925 pj_mutex_lock(ipp_factory.mutex);
926
927 /* Find codec's index */
928 idx = -1;
929 for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) {
930 pj_str_t name = pj_str((char*)ipp_codec[i].name);
931 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
932 (id->clock_rate == (unsigned)ipp_codec[i].clock_rate) &&
933 (id->channel_cnt == (unsigned)ipp_codec[i].channel_count) &&
934 (ipp_codec[i].enabled))
935 {
936 idx = i;
937 break;
938 }
939 }
940 if (idx == -1) {
941 *p_codec = NULL;
942 return PJMEDIA_CODEC_EFAILED;
943 }
944
945 /* Create pool for codec instance */
946 pool = pjmedia_endpt_create_pool(ipp_factory.endpt, "IPPcodec", 512, 512);
947 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
948 PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM);
949 codec->op = &ipp_op;
950 codec->factory = factory;
951 codec->codec_data = PJ_POOL_ZALLOC_T(pool, ipp_private_t);
952 codec_data = (ipp_private_t*) codec->codec_data;
953
954 /* Create PLC if codec has no internal PLC */
955 if (!ipp_codec[idx].has_native_plc) {
956 pj_status_t status;
957 status = pjmedia_plc_create(pool, ipp_codec[idx].clock_rate,
958 ipp_codec[idx].samples_per_frame, 0,
959 &codec_data->plc);
960 if (status != PJ_SUCCESS) {
961 pj_pool_release(pool);
962 pj_mutex_unlock(ipp_factory.mutex);
963 return status;
964 }
965 }
966
967 /* Create silence detector if codec has no internal VAD */
968 if (!ipp_codec[idx].has_native_vad) {
969 pj_status_t status;
970 status = pjmedia_silence_det_create(pool,
971 ipp_codec[idx].clock_rate,
972 ipp_codec[idx].samples_per_frame,
973 &codec_data->vad);
974 if (status != PJ_SUCCESS) {
975 pj_pool_release(pool);
976 pj_mutex_unlock(ipp_factory.mutex);
977 return status;
978 }
979 }
980
981 codec_data->pool = pool;
982 codec_data->codec_idx = idx;
983
984 pj_mutex_unlock(ipp_factory.mutex);
985
986 *p_codec = codec;
987 return PJ_SUCCESS;
988 }
989
990 /*
991 * Free codec.
992 */
ipp_dealloc_codec(pjmedia_codec_factory * factory,pjmedia_codec * codec)993 static pj_status_t ipp_dealloc_codec( pjmedia_codec_factory *factory,
994 pjmedia_codec *codec )
995 {
996 ipp_private_t *codec_data;
997
998 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
999 PJ_ASSERT_RETURN(factory == &ipp_factory.base, PJ_EINVAL);
1000
1001 /* Close codec, if it's not closed. */
1002 codec_data = (ipp_private_t*) codec->codec_data;
1003 if (codec_data->enc != NULL || codec_data->dec != NULL) {
1004 ipp_codec_close(codec);
1005 }
1006
1007 pj_pool_release(codec_data->pool);
1008
1009 return PJ_SUCCESS;
1010 }
1011
1012 /*
1013 * Init codec.
1014 */
ipp_codec_init(pjmedia_codec * codec,pj_pool_t * pool)1015 static pj_status_t ipp_codec_init( pjmedia_codec *codec,
1016 pj_pool_t *pool )
1017 {
1018 PJ_UNUSED_ARG(codec);
1019 PJ_UNUSED_ARG(pool);
1020 return PJ_SUCCESS;
1021 }
1022
1023 /*
1024 * Open codec.
1025 */
ipp_codec_open(pjmedia_codec * codec,pjmedia_codec_param * attr)1026 static pj_status_t ipp_codec_open( pjmedia_codec *codec,
1027 pjmedia_codec_param *attr )
1028 {
1029 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
1030 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
1031 int info_size;
1032 pj_pool_t *pool;
1033 int i, j;
1034 USC_MemBank *membanks;
1035 int nb_membanks;
1036
1037 pool = codec_data->pool;
1038
1039 /* Get the codec info size */
1040 if (USC_NoError != ippc->fxns->std.GetInfoSize(&info_size)) {
1041 PJ_LOG(1,(THIS_FILE, "Error getting codec info size"));
1042 goto on_error;
1043 }
1044 /* Get the codec info */
1045 codec_data->info = pj_pool_zalloc(pool, info_size);
1046 if (USC_NoError != ippc->fxns->std.GetInfo((USC_Handle)NULL,
1047 codec_data->info))
1048 {
1049 PJ_LOG(1,(THIS_FILE, "Error getting codec info"));
1050 goto on_error;
1051 }
1052
1053 /* PREPARING THE ENCODER */
1054
1055 /* Setting the encoder params */
1056 codec_data->info->params.direction = USC_ENCODE;
1057 codec_data->info->params.modes.vad = attr->setting.vad &&
1058 ippc->has_native_vad;
1059 codec_data->info->params.modes.bitrate = attr->info.avg_bps;
1060 codec_data->info->params.law = 0; /* Linear PCM input */
1061
1062 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
1063 if (ippc->pt == PJMEDIA_RTP_PT_G729) {
1064 /* Check if G729 Annex B is signaled to be disabled */
1065 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
1066 if (pj_stricmp2(&attr->setting.enc_fmtp.param[i].name, "annexb")==0)
1067 {
1068 if (pj_stricmp2(&attr->setting.enc_fmtp.param[i].val, "no")==0)
1069 {
1070 attr->setting.vad = 0;
1071 codec_data->info->params.modes.vad = 0;
1072 }
1073 break;
1074 }
1075 }
1076 }
1077 #endif
1078
1079 /* Get number of memory blocks needed by the encoder */
1080 if (USC_NoError != ippc->fxns->std.NumAlloc(&codec_data->info->params,
1081 &nb_membanks))
1082 {
1083 PJ_LOG(1,(THIS_FILE, "Error getting no of memory blocks of encoder"));
1084 goto on_error;
1085 }
1086
1087 /* Allocate memory blocks table */
1088 membanks = (USC_MemBank*) pj_pool_zalloc(pool,
1089 sizeof(USC_MemBank) * nb_membanks);
1090 /* Get size of each memory block */
1091 if (USC_NoError != ippc->fxns->std.MemAlloc(&codec_data->info->params,
1092 membanks))
1093 {
1094 PJ_LOG(1,(THIS_FILE, "Error getting memory blocks size of encoder"));
1095 goto on_error;
1096 }
1097
1098 /* Allocate memory for each block */
1099 for (i = 0; i < nb_membanks; i++) {
1100 membanks[i].pMem = (char*) pj_pool_zalloc(pool, membanks[i].nbytes);
1101 }
1102
1103 /* Create encoder instance */
1104 if (USC_NoError != ippc->fxns->std.Init(&codec_data->info->params,
1105 membanks,
1106 &codec_data->enc))
1107 {
1108 PJ_LOG(1,(THIS_FILE, "Error initializing encoder"));
1109 goto on_error;
1110 }
1111
1112 /* PREPARING THE DECODER */
1113
1114 /* Setting the decoder params */
1115 codec_data->info->params.direction = USC_DECODE;
1116
1117 /* Not sure if VAD affects decoder, just try to be safe */
1118 //codec_data->info->params.modes.vad = ippc->has_native_vad;
1119
1120 /* Get number of memory blocks needed by the decoder */
1121 if (USC_NoError != ippc->fxns->std.NumAlloc(&codec_data->info->params,
1122 &nb_membanks))
1123 {
1124 PJ_LOG(1,(THIS_FILE, "Error getting no of memory blocks of decoder"));
1125 goto on_error;
1126 }
1127
1128 /* Allocate memory blocks table */
1129 membanks = (USC_MemBank*) pj_pool_zalloc(pool,
1130 sizeof(USC_MemBank) * nb_membanks);
1131 /* Get size of each memory block */
1132 if (USC_NoError != ippc->fxns->std.MemAlloc(&codec_data->info->params,
1133 membanks))
1134 {
1135 PJ_LOG(1,(THIS_FILE, "Error getting memory blocks size of decoder"));
1136 goto on_error;
1137 }
1138
1139 /* Allocate memory for each block */
1140 for (i = 0; i < nb_membanks; i++) {
1141 membanks[i].pMem = (char*) pj_pool_zalloc(pool, membanks[i].nbytes);
1142 }
1143
1144 /* Create decoder instance */
1145 if (USC_NoError != ippc->fxns->std.Init(&codec_data->info->params,
1146 membanks, &codec_data->dec))
1147 {
1148 PJ_LOG(1,(THIS_FILE, "Error initializing decoder"));
1149 goto on_error;
1150 }
1151
1152 /* Update codec info */
1153 ippc->fxns->std.GetInfo((USC_Handle)codec_data->enc, codec_data->info);
1154
1155 /* Get bitstream size */
1156 i = codec_data->info->params.modes.bitrate * ippc->samples_per_frame;
1157 j = ippc->clock_rate << 3;
1158 codec_data->frame_size = (pj_uint16_t)(i / j);
1159 if (i % j) ++codec_data->frame_size;
1160
1161 codec_data->vad_enabled = (attr->setting.vad != 0);
1162 codec_data->plc_enabled = (attr->setting.plc != 0);
1163
1164 #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
1165 /* Init AMR settings */
1166 if (ippc->pt == PJMEDIA_RTP_PT_AMR || ippc->pt == PJMEDIA_RTP_PT_AMRWB) {
1167 amr_settings_t *s;
1168 pj_uint8_t octet_align = 0;
1169 pj_int8_t enc_mode;
1170
1171 enc_mode = pjmedia_codec_amr_get_mode(
1172 codec_data->info->params.modes.bitrate);
1173 pj_assert(enc_mode >= 0 && enc_mode <= 8);
1174
1175 /* Check AMR specific attributes */
1176
1177 for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) {
1178 /* octet-align, one of the parameters that must have same value
1179 * in offer & answer (RFC 4867 Section 8.3.1). Just check fmtp
1180 * in the decoder side, since it's value is guaranteed to fulfil
1181 * above requirement (by SDP negotiator).
1182 */
1183 const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11};
1184
1185 if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name,
1186 &STR_FMTP_OCTET_ALIGN) == 0)
1187 {
1188 octet_align=(pj_uint8_t)
1189 pj_strtoul(&attr->setting.dec_fmtp.param[i].val);
1190 break;
1191 }
1192 }
1193
1194 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
1195 /* mode-set, encoding mode is chosen based on local default mode
1196 * setting:
1197 * - if local default mode is included in the mode-set, use it
1198 * - otherwise, find the closest mode to local default mode;
1199 * if there are two closest modes, prefer to use the higher
1200 * one, e.g: local default mode is 4, the mode-set param
1201 * contains '2,3,5,6', then 5 will be chosen.
1202 */
1203 const pj_str_t STR_FMTP_MODE_SET = {"mode-set", 8};
1204
1205 if (pj_stricmp(&attr->setting.enc_fmtp.param[i].name,
1206 &STR_FMTP_MODE_SET) == 0)
1207 {
1208 const char *p;
1209 pj_size_t l;
1210 pj_int8_t diff = 99;
1211
1212 p = pj_strbuf(&attr->setting.enc_fmtp.param[i].val);
1213 l = pj_strlen(&attr->setting.enc_fmtp.param[i].val);
1214
1215 while (l--) {
1216 if ((ippc->pt==PJMEDIA_RTP_PT_AMR && *p>='0' && *p<='7') ||
1217 (ippc->pt==PJMEDIA_RTP_PT_AMRWB && *p>='0' && *p<='8'))
1218 {
1219 pj_int8_t tmp = (pj_int8_t)(*p - '0' - enc_mode);
1220
1221 if (PJ_ABS(diff) > PJ_ABS(tmp) ||
1222 (PJ_ABS(diff) == PJ_ABS(tmp) && tmp > diff))
1223 {
1224 diff = tmp;
1225 if (diff == 0) break;
1226 }
1227 }
1228 ++p;
1229 }
1230
1231 if (diff == 99)
1232 goto on_error;
1233
1234 enc_mode = (pj_int8_t)(enc_mode + diff);
1235
1236 break;
1237 }
1238 }
1239
1240 /* Initialize AMR specific settings */
1241 s = PJ_POOL_ZALLOC_T(pool, amr_settings_t);
1242 codec_data->codec_setting = s;
1243
1244 s->enc_setting.amr_nb = (pj_uint8_t)(ippc->pt == PJMEDIA_RTP_PT_AMR);
1245 s->enc_setting.octet_aligned = octet_align;
1246 s->enc_setting.reorder = PJ_TRUE;
1247 s->enc_setting.cmr = 15;
1248
1249 s->dec_setting.amr_nb = (pj_uint8_t)(ippc->pt == PJMEDIA_RTP_PT_AMR);
1250 s->dec_setting.octet_aligned = octet_align;
1251 s->dec_setting.reorder = PJ_TRUE;
1252
1253 /* Apply encoder mode/bitrate */
1254 s->enc_mode = enc_mode;
1255 codec_data->info->params.modes.bitrate = s->enc_setting.amr_nb?
1256 pjmedia_codec_amrnb_bitrates[s->enc_mode]:
1257 pjmedia_codec_amrwb_bitrates[s->enc_mode];
1258 ippc->fxns->std.Control(&codec_data->info->params.modes,
1259 codec_data->enc);
1260
1261 PJ_LOG(4,(THIS_FILE, "AMR%s encoding mode: %d (%dbps)",
1262 (s->enc_setting.amr_nb?"":"-WB"),
1263 s->enc_mode,
1264 codec_data->info->params.modes.bitrate));
1265
1266 /* Return back bitrate info to application */
1267 attr->info.avg_bps = codec_data->info->params.modes.bitrate;
1268 }
1269 #endif
1270
1271 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1
1272 if (ippc->pt >= PJMEDIA_RTP_PT_G722_1_16 &&
1273 ippc->pt <= PJMEDIA_RTP_PT_G7221_RSV2)
1274 {
1275 codec_data->g7221_pcm_shift = ipp_factory.g7221_pcm_shift;
1276 }
1277 #endif
1278
1279 return PJ_SUCCESS;
1280
1281 on_error:
1282 return PJMEDIA_CODEC_EFAILED;
1283 }
1284
1285 /*
1286 * Close codec.
1287 */
ipp_codec_close(pjmedia_codec * codec)1288 static pj_status_t ipp_codec_close( pjmedia_codec *codec )
1289 {
1290 PJ_UNUSED_ARG(codec);
1291
1292 return PJ_SUCCESS;
1293 }
1294
1295
1296 /*
1297 * Modify codec settings.
1298 */
ipp_codec_modify(pjmedia_codec * codec,const pjmedia_codec_param * attr)1299 static pj_status_t ipp_codec_modify(pjmedia_codec *codec,
1300 const pjmedia_codec_param *attr )
1301 {
1302 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
1303 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
1304
1305 codec_data->vad_enabled = (attr->setting.vad != 0);
1306 codec_data->plc_enabled = (attr->setting.plc != 0);
1307
1308 if (ippc->has_native_vad) {
1309 USC_Modes modes;
1310
1311 modes = codec_data->info->params.modes;
1312 modes.vad = codec_data->vad_enabled;
1313 ippc->fxns->std.Control(&modes, codec_data->enc);
1314 }
1315
1316 return PJ_SUCCESS;
1317 }
1318
1319 /*
1320 * Get frames in the packet.
1321 */
ipp_codec_parse(pjmedia_codec * codec,void * pkt,pj_size_t pkt_size,const pj_timestamp * ts,unsigned * frame_cnt,pjmedia_frame frames[])1322 static pj_status_t ipp_codec_parse( pjmedia_codec *codec,
1323 void *pkt,
1324 pj_size_t pkt_size,
1325 const pj_timestamp *ts,
1326 unsigned *frame_cnt,
1327 pjmedia_frame frames[])
1328 {
1329 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
1330 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
1331 unsigned count = 0;
1332
1333 PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
1334
1335 if (ippc->parse != NULL) {
1336 return ippc->parse(codec_data, pkt, pkt_size, ts, frame_cnt, frames);
1337 }
1338
1339 while (pkt_size >= codec_data->frame_size && count < *frame_cnt) {
1340 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
1341 frames[count].buf = pkt;
1342 frames[count].size = codec_data->frame_size;
1343 frames[count].timestamp.u64 = ts->u64 + count*ippc->samples_per_frame;
1344
1345 pkt = ((char*)pkt) + codec_data->frame_size;
1346 pkt_size -= codec_data->frame_size;
1347
1348 ++count;
1349 }
1350
1351 if (pkt_size && count < *frame_cnt) {
1352 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
1353 frames[count].buf = pkt;
1354 frames[count].size = pkt_size;
1355 frames[count].timestamp.u64 = ts->u64 + count*ippc->samples_per_frame;
1356 ++count;
1357 }
1358
1359 *frame_cnt = count;
1360 return PJ_SUCCESS;
1361 }
1362
1363 /*
1364 * Encode frames.
1365 */
ipp_codec_encode(pjmedia_codec * codec,const struct pjmedia_frame * input,unsigned output_buf_len,struct pjmedia_frame * output)1366 static pj_status_t ipp_codec_encode( pjmedia_codec *codec,
1367 const struct pjmedia_frame *input,
1368 unsigned output_buf_len,
1369 struct pjmedia_frame *output)
1370 {
1371 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
1372 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
1373 unsigned samples_per_frame;
1374 unsigned nsamples;
1375 pj_size_t tx = 0;
1376 pj_int16_t *pcm_in = (pj_int16_t*)input->buf;
1377 pj_uint8_t *bits_out = (pj_uint8_t*) output->buf;
1378 pj_uint8_t pt;
1379
1380 /* Invoke external VAD if codec has no internal VAD */
1381 if (codec_data->vad && codec_data->vad_enabled) {
1382 pj_bool_t is_silence;
1383 pj_int32_t silence_duration;
1384
1385 silence_duration = pj_timestamp_diff32(&codec_data->last_tx,
1386 &input->timestamp);
1387
1388 is_silence = pjmedia_silence_det_detect(codec_data->vad,
1389 (const pj_int16_t*) input->buf,
1390 (input->size >> 1),
1391 NULL);
1392 if (is_silence &&
1393 (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
1394 silence_duration < (PJMEDIA_CODEC_MAX_SILENCE_PERIOD *
1395 (int)ippc->clock_rate / 1000)))
1396 {
1397 output->type = PJMEDIA_FRAME_TYPE_NONE;
1398 output->buf = NULL;
1399 output->size = 0;
1400 output->timestamp = input->timestamp;
1401 return PJ_SUCCESS;
1402 } else {
1403 codec_data->last_tx = input->timestamp;
1404 }
1405 }
1406
1407 nsamples = input->size >> 1;
1408 samples_per_frame = ippc->samples_per_frame;
1409 pt = ippc->pt;
1410
1411 PJ_ASSERT_RETURN(nsamples % samples_per_frame == 0,
1412 PJMEDIA_CODEC_EPCMFRMINLEN);
1413
1414 /* Encode the frames */
1415 while (nsamples >= samples_per_frame) {
1416 USC_PCMStream in;
1417 USC_Bitstream out;
1418
1419 in.bitrate = codec_data->info->params.modes.bitrate;
1420 in.nbytes = samples_per_frame << 1;
1421 in.pBuffer = (char*)pcm_in;
1422 in.pcmType.bitPerSample = codec_data->info->params.pcmType.bitPerSample;
1423 in.pcmType.nChannels = codec_data->info->params.pcmType.nChannels;
1424 in.pcmType.sample_frequency = codec_data->info->params.pcmType.sample_frequency;
1425
1426 out.pBuffer = (char*)bits_out;
1427
1428 #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
1429 /* For AMR: reserve two octets for AMR frame info */
1430 if (pt == PJMEDIA_RTP_PT_AMR || pt == PJMEDIA_RTP_PT_AMRWB) {
1431 out.pBuffer += 2;
1432 }
1433 #endif
1434
1435 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1
1436 /* For G722.1: adjust the encoder input signal level */
1437 if (pt >= PJMEDIA_RTP_PT_G722_1_16 &&
1438 pt <= PJMEDIA_RTP_PT_G7221_RSV2 &&
1439 codec_data->g7221_pcm_shift)
1440 {
1441 unsigned i;
1442 for (i = 0; i < samples_per_frame; ++i)
1443 pcm_in[i] >>= codec_data->g7221_pcm_shift;
1444 }
1445 #endif
1446
1447 if (USC_NoError != ippc->fxns->Encode(codec_data->enc, &in, &out)) {
1448 break;
1449 }
1450
1451 #if PJMEDIA_HAS_INTEL_IPP_CODEC_AMR
1452 /* For AMR: put info (frametype, degraded, last frame, mode) in the
1453 * first two octets for payload packing.
1454 */
1455 if (pt == PJMEDIA_RTP_PT_AMR || pt == PJMEDIA_RTP_PT_AMRWB) {
1456 pj_uint16_t *info = (pj_uint16_t*)bits_out;
1457
1458 /* Two octets for AMR frame info, 0=LSB:
1459 * bit 0-3 : frame type
1460 * bit 5 : STI flag
1461 * bit 6 : last frame flag
1462 * bit 7 : quality flag
1463 * bit 8-11 : mode
1464 */
1465 out.nbytes += 2;
1466 if (out.frametype == 0 || out.frametype == 4 ||
1467 (pt == PJMEDIA_RTP_PT_AMR && out.frametype == 5) ||
1468 (pt == PJMEDIA_RTP_PT_AMRWB && out.frametype == 6))
1469 {
1470 /* Speech frame type */
1471 *info = (char)pjmedia_codec_amr_get_mode(out.bitrate);
1472 /* Quality */
1473 if (out.frametype == 5 || out.frametype == 6)
1474 *info |= 0x80;
1475 } else if (out.frametype == 1 || out.frametype == 2 ||
1476 (pt == PJMEDIA_RTP_PT_AMR && out.frametype == 6) ||
1477 (pt == PJMEDIA_RTP_PT_AMRWB && out.frametype == 7))
1478 {
1479 /* SID frame type */
1480 *info = (pj_uint8_t)(pt == PJMEDIA_RTP_PT_AMRWB? 9 : 8);
1481 /* Quality */
1482 if (out.frametype == 6 || out.frametype == 7)
1483 *info |= 0x80;
1484 /* STI */
1485 if (out.frametype != 1)
1486 *info |= 0x20;
1487 } else {
1488 /* Untransmited */
1489 *info = 15;
1490 out.nbytes = 2;
1491 }
1492
1493 /* Mode */
1494 *info |= (char)pjmedia_codec_amr_get_mode(out.bitrate) << 8;
1495
1496 /* Last frame flag */
1497 if (nsamples == samples_per_frame)
1498 *info |= 0x40;
1499 }
1500 #endif
1501
1502 pcm_in += samples_per_frame;
1503 nsamples -= samples_per_frame;
1504 tx += out.nbytes;
1505 bits_out += out.nbytes;
1506
1507 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G729
1508 if (pt == PJMEDIA_RTP_PT_G729) {
1509 if (out.frametype == 1) {
1510 /* SID */
1511 break;
1512 } else if (out.frametype == 0) {
1513 /* Untransmitted */
1514 tx -= out.nbytes;
1515 break;
1516 }
1517 }
1518 #endif
1519
1520 }
1521
1522 if (ippc->pack != NULL) {
1523 ippc->pack(codec_data, output->buf, &tx, output_buf_len);
1524 }
1525
1526 /* Check if we don't need to transmit the frame (DTX) */
1527 if (tx == 0) {
1528 output->buf = NULL;
1529 output->size = 0;
1530 output->timestamp.u64 = input->timestamp.u64;
1531 output->type = PJMEDIA_FRAME_TYPE_NONE;
1532 return PJ_SUCCESS;
1533 }
1534
1535 output->size = tx;
1536 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1537 output->timestamp = input->timestamp;
1538
1539 return PJ_SUCCESS;
1540 }
1541
1542 /*
1543 * Decode frame.
1544 */
ipp_codec_decode(pjmedia_codec * codec,const struct pjmedia_frame * input,unsigned output_buf_len,struct pjmedia_frame * output)1545 static pj_status_t ipp_codec_decode( pjmedia_codec *codec,
1546 const struct pjmedia_frame *input,
1547 unsigned output_buf_len,
1548 struct pjmedia_frame *output)
1549 {
1550 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
1551 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
1552 unsigned samples_per_frame;
1553 USC_PCMStream out;
1554 USC_Bitstream in;
1555 pj_uint8_t pt;
1556
1557 pt = ippc->pt;
1558 samples_per_frame = ippc->samples_per_frame;
1559
1560 PJ_ASSERT_RETURN(output_buf_len >= samples_per_frame << 1,
1561 PJMEDIA_CODEC_EPCMTOOSHORT);
1562
1563 if (input->type == PJMEDIA_FRAME_TYPE_AUDIO) {
1564 if (ippc->predecode) {
1565 ippc->predecode(codec_data, input, &in);
1566 } else {
1567 /* Most IPP codecs have frametype==0 for speech frame */
1568 in.pBuffer = (char*)input->buf;
1569 in.nbytes = input->size;
1570 in.frametype = 0;
1571 in.bitrate = codec_data->info->params.modes.bitrate;
1572 }
1573
1574 out.pBuffer = output->buf;
1575 }
1576
1577 if (input->type != PJMEDIA_FRAME_TYPE_AUDIO ||
1578 USC_NoError != ippc->fxns->Decode(codec_data->dec, &in, &out))
1579 {
1580 pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame);
1581 output->size = samples_per_frame << 1;
1582 output->timestamp.u64 = input->timestamp.u64;
1583 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1584 return PJ_SUCCESS;
1585 }
1586
1587 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G726
1588 /* For G.726: amplify decoding result (USC G.726 encoder deamplified it) */
1589 if (pt == PJMEDIA_RTP_PT_G726_16 || pt == PJMEDIA_RTP_PT_G726_24 ||
1590 pt == PJMEDIA_RTP_PT_G726_32 || pt == PJMEDIA_RTP_PT_G726_40 ||
1591 pt == PJMEDIA_RTP_PT_G721)
1592 {
1593 unsigned i;
1594 pj_int16_t *s = (pj_int16_t*)output->buf;
1595
1596 for (i = 0; i < samples_per_frame; ++i)
1597 s[i] <<= 2;
1598 }
1599 #endif
1600
1601 #if PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1
1602 /* For G722.1: adjust the decoder output signal level */
1603 if (pt >= PJMEDIA_RTP_PT_G722_1_16 &&
1604 pt <= PJMEDIA_RTP_PT_G7221_RSV2 &&
1605 codec_data->g7221_pcm_shift)
1606 {
1607 unsigned i;
1608 pj_int16_t *s = (pj_int16_t*)output->buf;
1609
1610 for (i = 0; i < samples_per_frame; ++i)
1611 s[i] <<= codec_data->g7221_pcm_shift;
1612 }
1613 #endif
1614
1615 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1616 output->size = samples_per_frame << 1;
1617 output->timestamp.u64 = input->timestamp.u64;
1618
1619 /* Invoke external PLC if codec has no internal PLC */
1620 if (codec_data->plc && codec_data->plc_enabled)
1621 pjmedia_plc_save(codec_data->plc, (pj_int16_t*)output->buf);
1622
1623 return PJ_SUCCESS;
1624 }
1625
1626 /*
1627 * Recover lost frame.
1628 */
ipp_codec_recover(pjmedia_codec * codec,unsigned output_buf_len,struct pjmedia_frame * output)1629 static pj_status_t ipp_codec_recover(pjmedia_codec *codec,
1630 unsigned output_buf_len,
1631 struct pjmedia_frame *output)
1632 {
1633 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
1634 struct ipp_codec *ippc = &ipp_codec[codec_data->codec_idx];
1635 unsigned samples_per_frame;
1636
1637 PJ_UNUSED_ARG(output_buf_len);
1638
1639 samples_per_frame = ippc->samples_per_frame;
1640
1641 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1642 output->size = samples_per_frame << 1;
1643
1644 if (codec_data->plc_enabled) {
1645 if (codec_data->plc) {
1646 pjmedia_plc_generate(codec_data->plc, (pj_int16_t*)output->buf);
1647 } else {
1648 USC_PCMStream out;
1649 out.pBuffer = output->buf;
1650 ippc->fxns->Decode(codec_data->dec, NULL, &out);
1651 }
1652 } else {
1653 pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame);
1654 }
1655
1656 return PJ_SUCCESS;
1657 }
1658
1659
1660 #if defined(_MSC_VER) && PJMEDIA_AUTO_LINK_IPP_LIBS
1661 # pragma comment( lib, "ippcore.lib")
1662 # pragma comment( lib, "ipps.lib")
1663 # pragma comment( lib, "ippsc.lib")
1664 # if defined(IPP_VERSION_MAJOR) && IPP_VERSION_MAJOR<=6
1665 # pragma comment( lib, "ippsr.lib")
1666 # endif
1667 //# pragma comment( lib, "ippcorel.lib")
1668 //# pragma comment( lib, "ippsemerged.lib")
1669 //# pragma comment( lib, "ippsmerged.lib")
1670 //# pragma comment( lib, "ippscemerged.lib")
1671 //# pragma comment( lib, "ippscmerged.lib")
1672 //# pragma comment( lib, "ippsremerged.lib")
1673 //# pragma comment( lib, "ippsrmerged.lib")
1674 # if defined(IPP_VERSION_MAJOR) && IPP_VERSION_MAJOR>=6
1675 # pragma comment( lib, "speech.lib")
1676 # else
1677 # pragma comment( lib, "usc.lib")
1678 # endif
1679 #endif
1680
1681
1682 #endif /* PJMEDIA_HAS_INTEL_IPP */
1683
1684