Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/ipp_codecs.c

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