blob: 476e54e3b8e92b6dfe93cf928b9f437072a2ccf0 [file] [log] [blame]
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
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.h>
21#include <pjmedia/errno.h>
22#include <pjmedia/endpoint.h>
23#include <pjmedia/plc.h>
24#include <pjmedia/port.h>
25#include <pjmedia/silencedet.h>
26#include <pj/assert.h>
27#include <pj/log.h>
28#include <pj/pool.h>
29#include <pj/string.h>
30#include <pj/os.h>
31
32/*
33 * Only build this file if PJMEDIA_HAS_INTEL_IPP_CODECS != 0
34 */
35#if defined(PJMEDIA_HAS_INTEL_IPP_CODECS) && PJMEDIA_HAS_INTEL_IPP_CODECS != 0
36
37#include <usc.h>
38
39#define THIS_FILE "ipp_codecs.c"
40
41/* Prototypes for IPP codecs factory */
42static pj_status_t ipp_test_alloc( pjmedia_codec_factory *factory,
43 const pjmedia_codec_info *id );
44static pj_status_t ipp_default_attr( pjmedia_codec_factory *factory,
45 const pjmedia_codec_info *id,
46 pjmedia_codec_param *attr );
47static pj_status_t ipp_enum_codecs( pjmedia_codec_factory *factory,
48 unsigned *count,
49 pjmedia_codec_info codecs[]);
50static pj_status_t ipp_alloc_codec( pjmedia_codec_factory *factory,
51 const pjmedia_codec_info *id,
52 pjmedia_codec **p_codec);
53static pj_status_t ipp_dealloc_codec( pjmedia_codec_factory *factory,
54 pjmedia_codec *codec );
55
56/* Prototypes for IPP codecs implementation. */
57static pj_status_t ipp_codec_init( pjmedia_codec *codec,
58 pj_pool_t *pool );
59static pj_status_t ipp_codec_open( pjmedia_codec *codec,
60 pjmedia_codec_param *attr );
61static pj_status_t ipp_codec_close( pjmedia_codec *codec );
62static pj_status_t ipp_codec_modify(pjmedia_codec *codec,
63 const pjmedia_codec_param *attr );
64static pj_status_t ipp_codec_parse( pjmedia_codec *codec,
65 void *pkt,
66 pj_size_t pkt_size,
67 const pj_timestamp *ts,
68 unsigned *frame_cnt,
69 pjmedia_frame frames[]);
70static pj_status_t ipp_codec_encode( pjmedia_codec *codec,
71 const struct pjmedia_frame *input,
72 unsigned output_buf_len,
73 struct pjmedia_frame *output);
74static pj_status_t ipp_codec_decode( pjmedia_codec *codec,
75 const struct pjmedia_frame *input,
76 unsigned output_buf_len,
77 struct pjmedia_frame *output);
78static pj_status_t ipp_codec_recover(pjmedia_codec *codec,
79 unsigned output_buf_len,
80 struct pjmedia_frame *output);
81
82/* Definition for IPP codecs operations. */
83static pjmedia_codec_op ipp_op =
84{
85 &ipp_codec_init,
86 &ipp_codec_open,
87 &ipp_codec_close,
88 &ipp_codec_modify,
89 &ipp_codec_parse,
90 &ipp_codec_encode,
91 &ipp_codec_decode,
92 &ipp_codec_recover
93};
94
95/* Definition for IPP codecs factory operations. */
96static pjmedia_codec_factory_op ipp_factory_op =
97{
98 &ipp_test_alloc,
99 &ipp_default_attr,
100 &ipp_enum_codecs,
101 &ipp_alloc_codec,
102 &ipp_dealloc_codec
103};
104
105/* IPP codecs factory */
106static struct ipp_factory {
107 pjmedia_codec_factory base;
108 pjmedia_endpt *endpt;
109 pj_pool_t *pool;
110 pj_mutex_t *mutex;
111} ipp_factory;
112
113/* IPP codecs private data. */
114typedef struct ipp_private {
115 int codec_idx; /**< Codec index. */
116 pj_pool_t *pool; /**< Pool for each instance. */
117
118 USC_Handle enc; /**< Encoder state. */
119 USC_Handle dec; /**< Decoder state. */
120 USC_CodecInfo *info; /**< Native codec info. */
121 pj_uint16_t frame_size; /**< Bitstream frame size. */
122
123 pj_bool_t plc_enabled;
124 pjmedia_plc *plc;
125
126 pj_bool_t vad_enabled;
127 pjmedia_silence_det *vad;
128 pj_timestamp last_tx;
129} ipp_private_t;
130
131
132/* USC codec implementations. */
133extern USC_Fxns USC_G729AFP_Fxns;
134extern USC_Fxns USC_G723_Fxns;
135extern USC_Fxns USC_G726_Fxns;
136extern USC_Fxns USC_G728_Fxns;
137extern USC_Fxns USC_G722_Fxns;
138extern USC_Fxns USC_GSMAMR_Fxns;
139extern USC_Fxns USC_AMRWB_Fxns;
140extern USC_Fxns USC_AMRWBE_Fxns;
141
142/* CUSTOM CALLBACKS */
143
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000144/* This callback is useful for translating RTP frame into USC frame, e.g:
145 * reassigning frame attributes, reorder bitstream. Default behaviour of
146 * the translation is just setting the USC frame buffer & its size as
147 * specified in RTP frame, setting USC frame frametype to 0, setting bitrate
148 * of USC frame to bitrate info of codec_data. Implement this callback when
149 * the default behaviour is unapplicable.
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000150 */
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000151typedef void (*predecode_cb)(ipp_private_t *codec_data,
152 const pjmedia_frame *rtp_frame,
153 USC_Bitstream *usc_frame);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000154
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000155/* Parse frames from a packet. Default behaviour of frame parsing is
156 * just separating frames based on calculating frame length derived
157 * from bitrate. Implement this callback when the default behaviour is
158 * unapplicable.
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000159 */
160typedef pj_status_t (*parse_cb)(ipp_private_t *codec_data, void *pkt,
161 pj_size_t pkt_size, const pj_timestamp *ts,
162 unsigned *frame_cnt, pjmedia_frame frames[]);
163
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000164/* Pack frames into a packet. Default behaviour of packing frames is
165 * just stacking the frames with octet aligned without adding any
166 * payload header. Implement this callback when the default behaviour is
167 * unapplicable.
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000168 */
169typedef pj_status_t (*pack_cb)(ipp_private_t *codec_data, void *pkt,
170 pj_size_t *pkt_size, pj_size_t max_pkt_size);
171
172
173
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000174/* Custom callback implementations. */
175static void predecode_g723( ipp_private_t *codec_data,
176 const pjmedia_frame *rtp_frame,
177 USC_Bitstream *usc_frame);
178static pj_status_t parse_g723( ipp_private_t *codec_data, void *pkt,
179 pj_size_t pkt_size, const pj_timestamp *ts,
180 unsigned *frame_cnt, pjmedia_frame frames[]);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000181
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000182static void predecode_g729( ipp_private_t *codec_data,
183 const pjmedia_frame *rtp_frame,
184 USC_Bitstream *usc_frame);
185
186static void predecode_amr( ipp_private_t *codec_data,
187 const pjmedia_frame *rtp_frame,
188 USC_Bitstream *usc_frame);
189static pj_status_t parse_amr( ipp_private_t *codec_data, void *pkt,
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000190 pj_size_t pkt_size, const pj_timestamp *ts,
191 unsigned *frame_cnt, pjmedia_frame frames[]);
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000192static pj_status_t pack_amr( ipp_private_t *codec_data, void *pkt,
193 pj_size_t *pkt_size, pj_size_t max_pkt_size);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000194
195
196/* IPP codec implementation descriptions. */
197static struct ipp_codec {
198 int enabled; /* Is this codec enabled? */
199 const char *name; /* Codec name. */
200 pj_uint8_t pt; /* Payload type. */
201 USC_Fxns *fxns; /* USC callback functions. */
202 unsigned clock_rate; /* Codec's clock rate. */
203 unsigned channel_count; /* Codec's channel count. */
204 unsigned samples_per_frame; /* Codec's samples count. */
205
206 unsigned def_bitrate; /* Default bitrate of this codec. */
207 unsigned max_bitrate; /* Maximum bitrate of this codec. */
208 pj_uint8_t frm_per_pkt; /* Default num of frames per packet.*/
209 int has_native_vad; /* Codec has internal VAD? */
210 int has_native_plc; /* Codec has internal PLC? */
211
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000212 predecode_cb predecode; /* Callback to translate RTP frame
213 into USC frame */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000214 parse_cb parse; /* Callback to parse bitstream */
215 pack_cb pack; /* Callback to pack bitstream */
216}
217
218ipp_codec[] =
219{
220# if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_AMR) && PJMEDIA_HAS_INTEL_IPP_CODEC_AMR != 0
221 {1, "AMR", PJMEDIA_RTP_PT_AMR, &USC_GSMAMR_Fxns, 8000, 1, 160,
222 5900, 12200, 4, 1, 1,
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000223 &predecode_amr, &parse_amr, &pack_amr
224 },
225# endif
226
227# if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_AMRWB) && PJMEDIA_HAS_INTEL_IPP_CODEC_AMRWB != 0
228 {1, "AMR-WB", PJMEDIA_RTP_PT_AMRWB, &USC_AMRWB_Fxns, 16000, 1, 320,
229 15850, 23850, 4, 1, 1,
230 &predecode_amr, &parse_amr, &pack_amr
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000231 },
232# endif
233
234# if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_G729) && PJMEDIA_HAS_INTEL_IPP_CODEC_G729 != 0
235 /* G.729 actually has internal VAD, but for now we need to disable it,
236 * since its RTP packaging (multiple frames per packet) requires
237 * SID frame to only be occured in the last frame, while controling
238 * encoder on each loop (to enable/disable VAD) is considered inefficient.
239 * This should still be interoperable with other implementations.
240 */
241 {1, "G729", PJMEDIA_RTP_PT_G729, &USC_G729AFP_Fxns, 8000, 1, 80,
242 8000, 11800, 2, 0, 1,
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000243 &predecode_g729, NULL, NULL
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000244 },
245# endif
246
247# if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_G723) && PJMEDIA_HAS_INTEL_IPP_CODEC_G723 != 0
248 /* This is actually G.723.1 */
249 {1, "G723", PJMEDIA_RTP_PT_G723, &USC_G723_Fxns, 8000, 1, 240,
250 5300, 6300, 1, 1, 1,
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000251 &predecode_g723, &parse_g723, NULL
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000252 },
253# endif
254
255# if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_G726) && PJMEDIA_HAS_INTEL_IPP_CODEC_G726 != 0
Nanang Izzuddinf216f822008-08-15 18:35:50 +0000256 {0, "G726-16", PJMEDIA_RTP_PT_G726_16, &USC_G726_Fxns, 8000, 1, 80,
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000257 16000, 16000, 2, 0, 0,
258 NULL, NULL, NULL
259 },
Nanang Izzuddinf216f822008-08-15 18:35:50 +0000260 {0, "G726-24", PJMEDIA_RTP_PT_G726_24, &USC_G726_Fxns, 8000, 1, 80,
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000261 24000, 24000, 2, 0, 0,
262 NULL, NULL, NULL
263 },
264 {1, "G726-32", PJMEDIA_RTP_PT_G726_32, &USC_G726_Fxns, 8000, 1, 80,
265 32000, 32000, 2, 0, 0,
266 NULL, NULL, NULL
267 },
Nanang Izzuddinf216f822008-08-15 18:35:50 +0000268 {0, "G726-40", PJMEDIA_RTP_PT_G726_40, &USC_G726_Fxns, 8000, 1, 80,
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000269 40000, 40000, 2, 0, 0,
270 NULL, NULL, NULL
271 },
272# endif
273
274# if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_G728) && PJMEDIA_HAS_INTEL_IPP_CODEC_G728 != 0
275 {1, "G728", PJMEDIA_RTP_PT_G728, &USC_G728_Fxns, 8000, 1, 80,
276 16000, 16000, 2, 0, 1,
277 NULL, NULL, NULL
278 },
279# endif
280
281# if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1) && PJMEDIA_HAS_INTEL_IPP_CODEC_G722_1 != 0
Nanang Izzuddinf216f822008-08-15 18:35:50 +0000282 {0, "G7221", PJMEDIA_RTP_PT_G722_1, &USC_G722_Fxns, 16000, 1, 320,
283 24000, 32000, 1, 0, 1,
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000284 NULL, NULL, NULL
285 },
286# endif
287};
288
289
290static int amr_get_mode(unsigned bitrate);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000291
292/*
293 * Initialize and register IPP codec factory to pjmedia endpoint.
294 */
295PJ_DEF(pj_status_t) pjmedia_codec_ipp_init( pjmedia_endpt *endpt )
296{
297 pjmedia_codec_mgr *codec_mgr;
298 pj_status_t status;
299
300 if (ipp_factory.pool != NULL) {
301 /* Already initialized. */
302 return PJ_SUCCESS;
303 }
304
305 /* Create IPP codec factory. */
306 ipp_factory.base.op = &ipp_factory_op;
307 ipp_factory.base.factory_data = NULL;
308 ipp_factory.endpt = endpt;
309
310 ipp_factory.pool = pjmedia_endpt_create_pool(endpt, "IPP codecs", 4000, 4000);
311 if (!ipp_factory.pool)
312 return PJ_ENOMEM;
313
314 /* Create mutex. */
315 status = pj_mutex_create_simple(ipp_factory.pool, "IPP codecs",
316 &ipp_factory.mutex);
317 if (status != PJ_SUCCESS)
318 goto on_error;
319
320 /* Get the codec manager. */
321 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
322 if (!codec_mgr) {
323 status = PJ_EINVALIDOP;
324 goto on_error;
325 }
326
327 /* Register codec factory to endpoint. */
328 status = pjmedia_codec_mgr_register_factory(codec_mgr,
329 &ipp_factory.base);
330 if (status != PJ_SUCCESS)
331 goto on_error;
332
333 /* Done. */
334 return PJ_SUCCESS;
335
336on_error:
337 pj_pool_release(ipp_factory.pool);
338 ipp_factory.pool = NULL;
339 return status;
340}
341
342/*
343 * Unregister IPP codecs factory from pjmedia endpoint.
344 */
345PJ_DEF(pj_status_t) pjmedia_codec_ipp_deinit(void)
346{
347 pjmedia_codec_mgr *codec_mgr;
348 pj_status_t status;
349
350 if (ipp_factory.pool == NULL) {
351 /* Already deinitialized */
352 return PJ_SUCCESS;
353 }
354
355 pj_mutex_lock(ipp_factory.mutex);
356
357 /* Get the codec manager. */
358 codec_mgr = pjmedia_endpt_get_codec_mgr(ipp_factory.endpt);
359 if (!codec_mgr) {
360 pj_pool_release(ipp_factory.pool);
361 ipp_factory.pool = NULL;
362 return PJ_EINVALIDOP;
363 }
364
365 /* Unregister IPP codecs factory. */
366 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
367 &ipp_factory.base);
368
369 /* Destroy mutex. */
370 pj_mutex_destroy(ipp_factory.mutex);
371
372 /* Destroy pool. */
373 pj_pool_release(ipp_factory.pool);
374 ipp_factory.pool = NULL;
375
376 return status;
377}
378
379/*
380 * Check if factory can allocate the specified codec.
381 */
382static pj_status_t ipp_test_alloc( pjmedia_codec_factory *factory,
383 const pjmedia_codec_info *info )
384{
385 unsigned i;
386
387 PJ_UNUSED_ARG(factory);
388
389 /* Type MUST be audio. */
390 if (info->type != PJMEDIA_TYPE_AUDIO)
391 return PJMEDIA_CODEC_EUNSUP;
392
393 for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) {
394 pj_str_t name = pj_str((char*)ipp_codec[i].name);
395 if ((pj_stricmp(&info->encoding_name, &name) == 0) &&
396 (info->clock_rate == (unsigned)ipp_codec[i].clock_rate) &&
397 (info->channel_cnt == (unsigned)ipp_codec[i].channel_count) &&
398 (ipp_codec[i].enabled))
399 {
400 return PJ_SUCCESS;
401 }
402 }
403
404 /* Unsupported, or mode is disabled. */
405 return PJMEDIA_CODEC_EUNSUP;
406}
407
408/*
409 * Generate default attribute.
410 */
411static pj_status_t ipp_default_attr (pjmedia_codec_factory *factory,
412 const pjmedia_codec_info *id,
413 pjmedia_codec_param *attr )
414{
415 unsigned i;
416
417 PJ_ASSERT_RETURN(factory==&ipp_factory.base, PJ_EINVAL);
418
419 pj_bzero(attr, sizeof(pjmedia_codec_param));
420
421 for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) {
422 pj_str_t name = pj_str((char*)ipp_codec[i].name);
423 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
424 (id->clock_rate == (unsigned)ipp_codec[i].clock_rate) &&
425 (id->channel_cnt == (unsigned)ipp_codec[i].channel_count))
426 {
427 attr->info.pt = (pj_uint8_t)id->pt;
428 attr->info.channel_cnt = ipp_codec[i].channel_count;
429 attr->info.clock_rate = ipp_codec[i].clock_rate;
430 attr->info.avg_bps = ipp_codec[i].def_bitrate;
431 attr->info.max_bps = ipp_codec[i].max_bitrate;
432 attr->info.pcm_bits_per_sample = 16;
433 attr->info.frm_ptime = (pj_uint16_t)
434 (ipp_codec[i].samples_per_frame * 1000 /
435 ipp_codec[i].channel_count /
436 ipp_codec[i].clock_rate);
437 attr->setting.frm_per_pkt = ipp_codec[i].frm_per_pkt;
438
439 /* Default flags. */
440 attr->setting.cng = 0;
441 attr->setting.plc = 1;
442 attr->setting.penh= 0;
443 attr->setting.vad = 1; /* Always disable for now */
444
445 return PJ_SUCCESS;
446 }
447 }
448
449 return PJMEDIA_CODEC_EUNSUP;
450}
451
452/*
453 * Enum codecs supported by this factory.
454 */
455static pj_status_t ipp_enum_codecs(pjmedia_codec_factory *factory,
456 unsigned *count,
457 pjmedia_codec_info codecs[])
458{
459 unsigned max;
460 unsigned i;
461
462 PJ_UNUSED_ARG(factory);
463 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
464
465 max = *count;
466
467 for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(ipp_codec) && *count < max; ++i)
468 {
469 if (!ipp_codec[i].enabled)
470 continue;
471
472 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
473 codecs[*count].encoding_name = pj_str((char*)ipp_codec[i].name);
474 codecs[*count].pt = ipp_codec[i].pt;
475 codecs[*count].type = PJMEDIA_TYPE_AUDIO;
476 codecs[*count].clock_rate = ipp_codec[i].clock_rate;
477 codecs[*count].channel_cnt = ipp_codec[i].channel_count;
478
479 ++*count;
480 }
481
482 return PJ_SUCCESS;
483}
484
485/*
486 * Allocate a new codec instance.
487 */
488static pj_status_t ipp_alloc_codec( pjmedia_codec_factory *factory,
489 const pjmedia_codec_info *id,
490 pjmedia_codec **p_codec)
491{
492 ipp_private_t *codec_data;
493 pjmedia_codec *codec;
494 int idx;
495 pj_pool_t *pool;
496 unsigned i;
497
498 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
499 PJ_ASSERT_RETURN(factory == &ipp_factory.base, PJ_EINVAL);
500
501 pj_mutex_lock(ipp_factory.mutex);
502
503 /* Find codec's index */
504 idx = -1;
505 for (i = 0; i < PJ_ARRAY_SIZE(ipp_codec); ++i) {
506 pj_str_t name = pj_str((char*)ipp_codec[i].name);
507 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
508 (id->clock_rate == (unsigned)ipp_codec[i].clock_rate) &&
509 (id->channel_cnt == (unsigned)ipp_codec[i].channel_count) &&
510 (ipp_codec[i].enabled))
511 {
512 idx = i;
513 break;
514 }
515 }
516 if (idx == -1) {
517 *p_codec = NULL;
518 return PJMEDIA_CODEC_EFAILED;
519 }
520
521 /* Create pool for codec instance */
522 pool = pjmedia_endpt_create_pool(ipp_factory.endpt, "IPP codec inst",
523 4000, 4000);
524 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
525 PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM);
526 codec->op = &ipp_op;
527 codec->factory = factory;
528 codec->codec_data = PJ_POOL_ZALLOC_T(pool, ipp_private_t);
529 codec_data = (ipp_private_t*) codec->codec_data;
530
531 /* Create PLC if codec has no internal PLC */
532 if (!ipp_codec[idx].has_native_plc) {
533 pj_status_t status;
534 status = pjmedia_plc_create(pool, ipp_codec[idx].clock_rate,
535 ipp_codec[idx].samples_per_frame, 0,
536 &codec_data->plc);
537 if (status != PJ_SUCCESS) {
538 pj_pool_release(pool);
539 pj_mutex_unlock(ipp_factory.mutex);
540 return status;
541 }
542 }
543
544 /* Create silence detector if codec has no internal VAD */
545 if (!ipp_codec[idx].has_native_vad) {
546 pj_status_t status;
547 status = pjmedia_silence_det_create(pool,
548 ipp_codec[idx].clock_rate,
549 ipp_codec[idx].samples_per_frame,
550 &codec_data->vad);
551 if (status != PJ_SUCCESS) {
552 pj_pool_release(pool);
553 pj_mutex_unlock(ipp_factory.mutex);
554 return status;
555 }
556 }
557
558 codec_data->pool = pool;
559 codec_data->codec_idx = idx;
560
561 pj_mutex_unlock(ipp_factory.mutex);
562
563 *p_codec = codec;
564 return PJ_SUCCESS;
565}
566
567/*
568 * Free codec.
569 */
570static pj_status_t ipp_dealloc_codec( pjmedia_codec_factory *factory,
571 pjmedia_codec *codec )
572{
573 ipp_private_t *codec_data;
574
575 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
576 PJ_ASSERT_RETURN(factory == &ipp_factory.base, PJ_EINVAL);
577
578 /* Close codec, if it's not closed. */
579 codec_data = (ipp_private_t*) codec->codec_data;
580 if (codec_data->enc != NULL || codec_data->dec != NULL) {
581 ipp_codec_close(codec);
582 }
583
584 pj_pool_release(codec_data->pool);
585
586 return PJ_SUCCESS;
587}
588
589/*
590 * Init codec.
591 */
592static pj_status_t ipp_codec_init( pjmedia_codec *codec,
593 pj_pool_t *pool )
594{
595 PJ_UNUSED_ARG(codec);
596 PJ_UNUSED_ARG(pool);
597 return PJ_SUCCESS;
598}
599
600/*
601 * Open codec.
602 */
603static pj_status_t ipp_codec_open( pjmedia_codec *codec,
604 pjmedia_codec_param *attr )
605{
606 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
607 int info_size;
608 pj_pool_t *pool;
609 int i, j, idx;
610 USC_MemBank *membanks;
611 int nb_membanks;
612
613 pool = codec_data->pool;
614 idx = codec_data->codec_idx;
615
616 /* Get the codec info size */
617 if (USC_NoError != ipp_codec[idx].fxns->std.GetInfoSize(&info_size)) {
618 PJ_LOG(3,(THIS_FILE, "Error getting codec info size"));
619 goto on_error;
620 }
621 /* Get the codec info */
622 codec_data->info = pj_pool_zalloc(pool, info_size);
623 if (USC_NoError != ipp_codec[idx].fxns->std.GetInfo((USC_Handle)NULL,
624 codec_data->info))
625 {
626 PJ_LOG(3,(THIS_FILE, "Error getting codec info"));
627 goto on_error;
628 }
629
630 /* PREPARING THE ENCODER */
631
632 /* Setting the encoder params */
633 codec_data->info->params.direction = USC_ENCODE;
Nanang Izzuddinf216f822008-08-15 18:35:50 +0000634 codec_data->info->params.modes.vad = attr->setting.vad &&
635 ipp_codec[codec_data->codec_idx].has_native_vad;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000636 codec_data->info->params.modes.bitrate = attr->info.avg_bps;
637 codec_data->info->params.law = 0; /* Linear PCM input */
638
639 /* Get number of memory blocks needed by the encoder */
640 if (USC_NoError !=
641 ipp_codec[idx].fxns->std.NumAlloc(&codec_data->info->params,
642 &nb_membanks))
643 {
644 PJ_LOG(3,(THIS_FILE, "Error getting no of memory blocks of encoder"));
645 goto on_error;
646 }
647
648 /* Allocate memory blocks table */
649 membanks = (USC_MemBank*) pj_pool_zalloc(pool,
650 sizeof(USC_MemBank) * nb_membanks);
651 /* Get size of each memory block */
652 if (USC_NoError !=
653 ipp_codec[idx].fxns->std.MemAlloc(&codec_data->info->params, membanks))
654 {
655 PJ_LOG(3,(THIS_FILE, "Error getting memory blocks size of encoder"));
656 goto on_error;
657 }
658
659 /* Allocate memory for each block */
660 for (i = 0; i < nb_membanks; i++) {
661 membanks[i].pMem = (char*) pj_pool_zalloc(pool, membanks[i].nbytes);
662 }
663
664 /* Create encoder instance */
665 if (USC_NoError != ipp_codec[idx].fxns->std.Init(&codec_data->info->params,
666 membanks,
667 &codec_data->enc))
668 {
669 PJ_LOG(3,(THIS_FILE, "Error initializing encoder"));
670 goto on_error;
671 }
672
673 /* PREPARING THE DECODER */
674
675 /* Setting the decoder params */
676 codec_data->info->params.direction = USC_DECODE;
677
678 /* Get number of memory blocks needed by the decoder */
679 if (USC_NoError !=
680 ipp_codec[idx].fxns->std.NumAlloc(&codec_data->info->params,
681 &nb_membanks))
682 {
683 PJ_LOG(3,(THIS_FILE, "Error getting no of memory blocks of decoder"));
684 goto on_error;
685 }
686
687 /* Allocate memory blocks table */
688 membanks = (USC_MemBank*) pj_pool_zalloc(pool,
689 sizeof(USC_MemBank) * nb_membanks);
690 /* Get size of each memory block */
691 if (USC_NoError !=
692 ipp_codec[idx].fxns->std.MemAlloc(&codec_data->info->params, membanks))
693 {
694 PJ_LOG(3,(THIS_FILE, "Error getting memory blocks size of decoder"));
695 goto on_error;
696 }
697
698 /* Allocate memory for each block */
699 for (i = 0; i < nb_membanks; i++) {
700 membanks[i].pMem = (char*) pj_pool_zalloc(pool, membanks[i].nbytes);
701 }
702
703 /* Create decoder instance */
704 if (USC_NoError != ipp_codec[idx].fxns->std.Init(&codec_data->info->params,
705 membanks,
706 &codec_data->dec))
707 {
708 PJ_LOG(3,(THIS_FILE, "Error initializing decoder"));
709 goto on_error;
710 }
711
712 /* Update codec info */
713 ipp_codec[idx].fxns->std.GetInfo((USC_Handle)codec_data->enc, codec_data->info);
714
715 /* Get bitstream size */
716 i = codec_data->info->params.modes.bitrate * ipp_codec[idx].samples_per_frame;
717 j = ipp_codec[idx].clock_rate << 3;
718 codec_data->frame_size = (pj_uint16_t)(i / j);
719 if (i % j) ++codec_data->frame_size;
720
721 codec_data->vad_enabled = (attr->setting.vad != 0);
722 codec_data->plc_enabled = (attr->setting.plc != 0);
723
724 return PJ_SUCCESS;
725
726on_error:
727 return PJMEDIA_CODEC_EFAILED;
728}
729
730/*
731 * Close codec.
732 */
733static pj_status_t ipp_codec_close( pjmedia_codec *codec )
734{
735 PJ_UNUSED_ARG(codec);
736
737 return PJ_SUCCESS;
738}
739
740
741/*
742 * Modify codec settings.
743 */
744static pj_status_t ipp_codec_modify(pjmedia_codec *codec,
745 const pjmedia_codec_param *attr )
746{
747 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
748
749 codec_data->vad_enabled = (attr->setting.vad != 0);
750 codec_data->plc_enabled = (attr->setting.plc != 0);
751
752 if (ipp_codec[codec_data->codec_idx].has_native_vad) {
753 USC_Modes modes;
754
755 modes = codec_data->info->params.modes;
756 modes.vad = codec_data->vad_enabled;
757 ipp_codec[codec_data->codec_idx].fxns->std.Control(&modes, codec_data->enc);
758 }
759
760 return PJ_SUCCESS;
761}
762
763/*
764 * Get frames in the packet.
765 */
766static pj_status_t ipp_codec_parse( pjmedia_codec *codec,
767 void *pkt,
768 pj_size_t pkt_size,
769 const pj_timestamp *ts,
770 unsigned *frame_cnt,
771 pjmedia_frame frames[])
772{
773 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
774 unsigned count = 0;
775
776 PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
777
778 if (ipp_codec[codec_data->codec_idx].parse != NULL) {
779 return ipp_codec[codec_data->codec_idx].parse(codec_data, pkt,
780 pkt_size, ts, frame_cnt, frames);
781 }
782
783 while (pkt_size >= codec_data->frame_size && count < *frame_cnt) {
784 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
785 frames[count].buf = pkt;
786 frames[count].size = codec_data->frame_size;
787 frames[count].timestamp.u64 = ts->u64 + count *
788 ipp_codec[codec_data->codec_idx].samples_per_frame;
789
790 pkt = ((char*)pkt) + codec_data->frame_size;
791 pkt_size -= codec_data->frame_size;
792
793 ++count;
794 }
795
796 if (pkt_size && count < *frame_cnt) {
797 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
798 frames[count].buf = pkt;
799 frames[count].size = pkt_size;
800 frames[count].timestamp.u64 = ts->u64 + count *
801 ipp_codec[codec_data->codec_idx].samples_per_frame;
802 ++count;
803 }
804
805 *frame_cnt = count;
806 return PJ_SUCCESS;
807}
808
809/*
810 * Encode frames.
811 */
812static pj_status_t ipp_codec_encode( pjmedia_codec *codec,
813 const struct pjmedia_frame *input,
814 unsigned output_buf_len,
815 struct pjmedia_frame *output)
816{
817 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
818 unsigned samples_per_frame;
819 unsigned nsamples;
820 pj_size_t tx = 0;
821 pj_int16_t *pcm_in = (pj_int16_t*)input->buf;
822 pj_int8_t *bits_out = (pj_int8_t*) output->buf;
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000823 pj_uint8_t pt;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000824
825 /* Invoke external VAD if codec has no internal VAD */
826 if (codec_data->vad && codec_data->vad_enabled) {
827 pj_bool_t is_silence;
828 pj_int32_t silence_duration;
829
830 silence_duration = pj_timestamp_diff32(&codec_data->last_tx,
831 &input->timestamp);
832
833 is_silence = pjmedia_silence_det_detect(codec_data->vad,
834 (const pj_int16_t*) input->buf,
835 (input->size >> 1),
836 NULL);
837 if (is_silence &&
838 PJMEDIA_CODEC_MAX_SILENCE_PERIOD != -1 &&
839 silence_duration < (PJMEDIA_CODEC_MAX_SILENCE_PERIOD*
840 (int)ipp_codec[codec_data->codec_idx].clock_rate/1000))
841 {
842 output->type = PJMEDIA_FRAME_TYPE_NONE;
843 output->buf = NULL;
844 output->size = 0;
845 output->timestamp = input->timestamp;
846 return PJ_SUCCESS;
847 } else {
848 codec_data->last_tx = input->timestamp;
849 }
850 }
851
852 nsamples = input->size >> 1;
853 samples_per_frame=ipp_codec[codec_data->codec_idx].samples_per_frame;
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000854 pt = ipp_codec[codec_data->codec_idx].pt;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000855
856 PJ_ASSERT_RETURN(nsamples % samples_per_frame == 0,
857 PJMEDIA_CODEC_EPCMFRMINLEN);
858
859 /* Encode the frames */
860 while (nsamples >= samples_per_frame) {
861 USC_PCMStream in;
862 USC_Bitstream out;
863
864 in.bitrate = codec_data->info->params.modes.bitrate;
865 in.nbytes = samples_per_frame << 1;
866 in.pBuffer = (char*)pcm_in;
867 in.pcmType.bitPerSample = codec_data->info->params.pcmType.bitPerSample;
868 in.pcmType.nChannels = codec_data->info->params.pcmType.nChannels;
869 in.pcmType.sample_frequency = codec_data->info->params.pcmType.sample_frequency;
870
871 out.pBuffer = bits_out;
872
873#if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_AMR) && PJMEDIA_HAS_INTEL_IPP_CODEC_AMR != 0
874 /* For AMR: reserve the first byte for frame info */
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000875 if (pt == PJMEDIA_RTP_PT_AMR || pt == PJMEDIA_RTP_PT_AMRWB) {
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000876 ++out.pBuffer;
877 }
878#endif
879
880 if (USC_NoError != ipp_codec[codec_data->codec_idx].fxns->Encode(codec_data->enc, &in, &out)) {
881 break;
882 }
883
884#if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_AMR) && PJMEDIA_HAS_INTEL_IPP_CODEC_AMR != 0
885 /* For AMR: put info (frametype, degraded, last frame) in the first byte */
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000886 if (pt == PJMEDIA_RTP_PT_AMR || pt == PJMEDIA_RTP_PT_AMRWB) {
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000887 pj_uint8_t *info = (pj_uint8_t*)bits_out;
888
889 ++out.nbytes;
890
891 /* One byte AMR frame type & quality flag:
892 * bit 0-3 : frame type
893 * bit 6 : last frame flag
894 * bit 7 : quality flag
895 */
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000896 if (out.frametype == 0 || out.frametype == 4 ||
897 (pt == PJMEDIA_RTP_PT_AMR && out.frametype == 5) ||
898 (pt == PJMEDIA_RTP_PT_AMRWB && out.frametype == 6))
899 {
900 /* Speech */
901 *info = (char)amr_get_mode(out.bitrate);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000902 /* Degraded */
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000903 if (out.frametype == 5 || out.frametype == 6)
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000904 *info |= 0x80;
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000905 } else if (out.frametype == 1 || out.frametype == 2 ||
906 (pt == PJMEDIA_RTP_PT_AMR && out.frametype == 6) ||
907 (pt == PJMEDIA_RTP_PT_AMRWB && out.frametype == 7))
908 {
909 /* SID */
910 *info = pt == PJMEDIA_RTP_PT_AMRWB? 9 : 8;
911 /* Degraded */
912 if (out.frametype == 6 || out.frametype == 7)
913 *info |= 0x80;
914 } else {
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000915 /* Untransmited */
916 *info = 15;
917 out.nbytes = 1;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000918 }
919
920 /* Last frame flag */
921 if (nsamples == samples_per_frame)
922 *info |= 0x40;
923 }
924
925 pcm_in += samples_per_frame;
926 nsamples -= samples_per_frame;
927 tx += out.nbytes;
928 bits_out += out.nbytes;
929 }
930#endif
931
932 if (ipp_codec[codec_data->codec_idx].pack != NULL) {
933 ipp_codec[codec_data->codec_idx].pack(codec_data, output->buf,
934 &tx, output_buf_len);
935 }
936
937 /* Check if we don't need to transmit the frame (DTX) */
938 if (tx == 0) {
939 output->buf = NULL;
940 output->size = 0;
941 output->timestamp.u64 = input->timestamp.u64;
942 output->type = PJMEDIA_FRAME_TYPE_NONE;
943 return PJ_SUCCESS;
944 }
945
946 output->size = tx;
947 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
948 output->timestamp = input->timestamp;
949
950 return PJ_SUCCESS;
951}
952
953/*
954 * Decode frame.
955 */
956static pj_status_t ipp_codec_decode( pjmedia_codec *codec,
957 const struct pjmedia_frame *input,
958 unsigned output_buf_len,
959 struct pjmedia_frame *output)
960{
961 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
962 unsigned samples_per_frame;
963 USC_PCMStream out;
964 USC_Bitstream in;
965 pj_uint8_t pt;
966
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000967 pt = ipp_codec[codec_data->codec_idx].pt;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000968 samples_per_frame = ipp_codec[codec_data->codec_idx].samples_per_frame;
969
970 PJ_ASSERT_RETURN(output_buf_len >= samples_per_frame << 1,
971 PJMEDIA_CODEC_EPCMTOOSHORT);
972
973 if (input->type == PJMEDIA_FRAME_TYPE_AUDIO) {
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000974 if (ipp_codec[codec_data->codec_idx].predecode) {
975 ipp_codec[codec_data->codec_idx].predecode(codec_data, input, &in);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000976 } else {
977 /* Most IPP codecs have frametype==0 for speech frame */
Nanang Izzuddindf361e02008-08-16 06:46:08 +0000978 in.pBuffer = (char*)input->buf;
979 in.nbytes = input->size;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000980 in.frametype = 0;
981 in.bitrate = codec_data->info->params.modes.bitrate;
982 }
983
984 out.pBuffer = output->buf;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000985 }
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000986
987 if (input->type != PJMEDIA_FRAME_TYPE_AUDIO ||
988 USC_NoError != ipp_codec[codec_data->codec_idx].fxns->Decode(
989 codec_data->dec, &in, &out))
990 {
991 pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame);
992 output->size = samples_per_frame << 1;
993 output->timestamp.u64 = input->timestamp.u64;
994 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
995 return PJ_SUCCESS;
996 }
997
998#if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_G726) && PJMEDIA_HAS_INTEL_IPP_CODEC_G726 != 0
999 /* For G.726: amplify decoding result (USC G.726 encoder deamplified it) */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001000 if (pt == PJMEDIA_RTP_PT_G726_16 || pt == PJMEDIA_RTP_PT_G726_24 ||
1001 pt == PJMEDIA_RTP_PT_G726_32 || pt == PJMEDIA_RTP_PT_G726_40)
1002 {
1003 unsigned i;
1004 pj_int16_t *s = (pj_int16_t*)output->buf;
1005
1006 for (i = 0; i < samples_per_frame; ++i)
1007 s[i] <<= 2;
1008 }
1009#endif
1010
1011 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1012 output->size = samples_per_frame << 1;
1013 output->timestamp.u64 = input->timestamp.u64;
1014
1015 /* Invoke external PLC if codec has no internal PLC */
1016 if (codec_data->plc && codec_data->plc_enabled)
1017 pjmedia_plc_save(codec_data->plc, (pj_int16_t*)output->buf);
1018
1019 return PJ_SUCCESS;
1020}
1021
1022/*
1023 * Recover lost frame.
1024 */
1025static pj_status_t ipp_codec_recover(pjmedia_codec *codec,
1026 unsigned output_buf_len,
1027 struct pjmedia_frame *output)
1028{
1029 ipp_private_t *codec_data = (ipp_private_t*) codec->codec_data;
1030 unsigned samples_per_frame;
1031
1032 PJ_UNUSED_ARG(output_buf_len);
1033
1034 samples_per_frame = ipp_codec[codec_data->codec_idx].samples_per_frame;
1035
1036 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1037 output->size = samples_per_frame << 1;
1038
1039 if (codec_data->plc_enabled) {
1040 if (codec_data->plc) {
1041 pjmedia_plc_generate(codec_data->plc, (pj_int16_t*)output->buf);
1042 } else {
1043 USC_PCMStream out;
1044 out.pBuffer = output->buf;
1045 ipp_codec[codec_data->codec_idx].fxns->Decode(codec_data->dec,
1046 NULL, &out);
1047 }
1048 } else {
1049 pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame);
1050 }
1051
1052 return PJ_SUCCESS;
1053}
1054
1055#if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_G729) && PJMEDIA_HAS_INTEL_IPP_CODEC_G729 != 0
1056
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001057static void predecode_g729( ipp_private_t *codec_data,
1058 const pjmedia_frame *rtp_frame,
1059 USC_Bitstream *usc_frame)
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001060{
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001061 switch (rtp_frame->size) {
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001062 case 2:
1063 /* SID */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001064 usc_frame->frametype = 1;
1065 usc_frame->bitrate = codec_data->info->params.modes.bitrate;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001066 break;
1067 case 8:
1068 /* G729D */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001069 usc_frame->frametype = 2;
1070 usc_frame->bitrate = 6400;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001071 break;
1072 case 10:
1073 /* G729 */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001074 usc_frame->frametype = 3;
1075 usc_frame->bitrate = 8000;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001076 break;
1077 case 15:
1078 /* G729E */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001079 usc_frame->frametype = 4;
1080 usc_frame->bitrate = 11800;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001081 break;
1082 default:
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001083 usc_frame->frametype = 0;
1084 usc_frame->bitrate = 0;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001085 break;
1086 }
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001087
1088 usc_frame->pBuffer = rtp_frame->buf;
1089 usc_frame->nbytes = rtp_frame->size;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001090}
1091
1092#endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G729 */
1093
1094
1095#if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_G723) && PJMEDIA_HAS_INTEL_IPP_CODEC_G723 != 0
1096
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001097static void predecode_g723( ipp_private_t *codec_data,
1098 const pjmedia_frame *rtp_frame,
1099 USC_Bitstream *usc_frame)
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001100{
1101 int i, HDR = 0;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001102 pj_uint8_t *f = (pj_uint8_t*)rtp_frame->buf;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001103
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001104 PJ_UNUSED_ARG(codec_data);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001105
1106 for (i = 0; i < 2; ++i){
1107 int tmp;
1108 tmp = (f[0] >> (i & 0x7)) & 1;
1109 HDR += tmp << i ;
1110 }
1111
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001112 usc_frame->pBuffer = rtp_frame->buf;
1113 usc_frame->nbytes = rtp_frame->size;
1114 usc_frame->bitrate = HDR == 0? 6300 : 5300;
1115 usc_frame->frametype = 0;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001116}
1117
1118static pj_status_t parse_g723(ipp_private_t *codec_data, void *pkt,
1119 pj_size_t pkt_size, const pj_timestamp *ts,
1120 unsigned *frame_cnt, pjmedia_frame frames[])
1121{
1122 unsigned count = 0;
1123 pj_uint8_t *f = (pj_uint8_t*)pkt;
1124
1125 while (pkt_size && count < *frame_cnt) {
1126 int framesize, i, j;
1127 int HDR = 0;
1128
1129 for (i = 0; i < 2; ++i){
1130 j = (f[0] >> (i & 0x7)) & 1;
1131 HDR += j << i ;
1132 }
1133
1134 if (HDR == 0)
1135 framesize = 24;
1136 else if (HDR == 1)
1137 framesize = 20;
1138 else if (HDR == 2)
1139 framesize = 4;
1140 else if (HDR == 3)
1141 framesize = 1;
1142 else {
1143 pj_assert(!"Unknown G723.1 frametype, bitstream may be corrupted!");
1144 return PJMEDIA_CODEC_EINMODE;
1145 }
1146
1147 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
1148 frames[count].buf = f;
1149 frames[count].size = framesize;
1150 frames[count].timestamp.u64 = ts->u64 + count *
1151 ipp_codec[codec_data->codec_idx].samples_per_frame;
1152
1153 f += framesize;
1154 pkt_size -= framesize;
1155
1156 ++count;
1157 }
1158
1159 *frame_cnt = count;
1160 return PJ_SUCCESS;
1161}
1162
1163#endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_G723 */
1164
1165
1166#if defined(PJMEDIA_HAS_INTEL_IPP_CODEC_AMR) && PJMEDIA_HAS_INTEL_IPP_CODEC_AMR != 0
1167
1168/* AMR bitstream sensitivity order map */
1169static pj_int16_t AMRNB_ordermap122[244] =
1170{
1171 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1172 10, 11, 12, 13, 14, 23, 15, 16, 17, 18,
1173 19, 20, 21, 22, 24, 25, 26, 27, 28, 38,
1174 141, 39, 142, 40, 143, 41, 144, 42, 145, 43,
1175 146, 44, 147, 45, 148, 46, 149, 47, 97, 150,
1176 200, 48, 98, 151, 201, 49, 99, 152, 202, 86,
1177 136, 189, 239, 87, 137, 190, 240, 88, 138, 191,
1178 241, 91, 194, 92, 195, 93, 196, 94, 197, 95,
1179 198, 29, 30, 31, 32, 33, 34, 35, 50, 100,
1180 153, 203, 89, 139, 192, 242, 51, 101, 154, 204,
1181 55, 105, 158, 208, 90, 140, 193, 243, 59, 109,
1182 162, 212, 63, 113, 166, 216, 67, 117, 170, 220,
1183 36, 37, 54, 53, 52, 58, 57, 56, 62, 61,
1184 60, 66, 65, 64, 70, 69, 68, 104, 103, 102,
1185 108, 107, 106, 112, 111, 110, 116, 115, 114, 120,
1186 119, 118, 157, 156, 155, 161, 160, 159, 165, 164,
1187 163, 169, 168, 167, 173, 172, 171, 207, 206, 205,
1188 211, 210, 209, 215, 214, 213, 219, 218, 217, 223,
1189 222, 221, 73, 72, 71, 76, 75, 74, 79, 78,
1190 77, 82, 81, 80, 85, 84, 83, 123, 122, 121,
1191 126, 125, 124, 129, 128, 127, 132, 131, 130, 135,
1192 134, 133, 176, 175, 174, 179, 178, 177, 182, 181,
1193 180, 185, 184, 183, 188, 187, 186, 226, 225, 224,
1194 229, 228, 227, 232, 231, 230, 235, 234, 233, 238,
1195 237, 236, 96, 199
1196};
1197
1198static pj_int16_t AMRNB_ordermap102[204] =
1199{
1200 7, 6, 5, 4, 3, 2, 1, 0, 16, 15,
1201 14, 13, 12, 11, 10, 9, 8, 26, 27, 28,
1202 29, 30, 31, 115, 116, 117, 118, 119, 120, 72,
1203 73, 161, 162, 65, 68, 69, 108, 111, 112, 154,
1204 157, 158, 197, 200, 201, 32, 33, 121, 122, 74,
1205 75, 163, 164, 66, 109, 155, 198, 19, 23, 21,
1206 22, 18, 17, 20, 24, 25, 37, 36, 35, 34,
1207 80, 79, 78, 77, 126, 125, 124, 123, 169, 168,
1208 167, 166, 70, 67, 71, 113, 110, 114, 159, 156,
1209 160, 202, 199, 203, 76, 165, 81, 82, 92, 91,
1210 93, 83, 95, 85, 84, 94, 101, 102, 96, 104,
1211 86, 103, 87, 97, 127, 128, 138, 137, 139, 129,
1212 141, 131, 130, 140, 147, 148, 142, 150, 132, 149,
1213 133, 143, 170, 171, 181, 180, 182, 172, 184, 174,
1214 173, 183, 190, 191, 185, 193, 175, 192, 176, 186,
1215 38, 39, 49, 48, 50, 40, 52, 42, 41, 51,
1216 58, 59, 53, 61, 43, 60, 44, 54, 194, 179,
1217 189, 196, 177, 195, 178, 187, 188, 151, 136, 146,
1218 153, 134, 152, 135, 144, 145, 105, 90, 100, 107,
1219 88, 106, 89, 98, 99, 62, 47, 57, 64, 45,
1220 63, 46, 55, 56
1221};
1222
1223static pj_int16_t AMRNB_ordermap795[159] =
1224{
1225 8, 7, 6, 5, 4, 3, 2, 14, 16, 9,
1226 10, 12, 13, 15, 11, 17, 20, 22, 24, 23,
1227 19, 18, 21, 56, 88, 122, 154, 57, 89, 123,
1228 155, 58, 90, 124, 156, 52, 84, 118, 150, 53,
1229 85, 119, 151, 27, 93, 28, 94, 29, 95, 30,
1230 96, 31, 97, 61, 127, 62, 128, 63, 129, 59,
1231 91, 125, 157, 32, 98, 64, 130, 1, 0, 25,
1232 26, 33, 99, 34, 100, 65, 131, 66, 132, 54,
1233 86, 120, 152, 60, 92, 126, 158, 55, 87, 121,
1234 153, 117, 116, 115, 46, 78, 112, 144, 43, 75,
1235 109, 141, 40, 72, 106, 138, 36, 68, 102, 134,
1236 114, 149, 148, 147, 146, 83, 82, 81, 80, 51,
1237 50, 49, 48, 47, 45, 44, 42, 39, 35, 79,
1238 77, 76, 74, 71, 67, 113, 111, 110, 108, 105,
1239 101, 145, 143, 142, 140, 137, 133, 41, 73, 107,
1240 139, 37, 69, 103, 135, 38, 70, 104, 136
1241
1242};
1243
1244static pj_int16_t AMRNB_ordermap74[148] =
1245{
1246 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1247 10, 11, 12, 13, 14, 15, 16, 26, 87, 27,
1248 88, 28, 89, 29, 90, 30, 91, 51, 80, 112,
1249 141, 52, 81, 113, 142, 54, 83, 115, 144, 55,
1250 84, 116, 145, 58, 119, 59, 120, 21, 22, 23,
1251 17, 18, 19, 31, 60, 92, 121, 56, 85, 117,
1252 146, 20, 24, 25, 50, 79, 111, 140, 57, 86,
1253 118, 147, 49, 78, 110, 139, 48, 77, 53, 82,
1254 114, 143, 109, 138, 47, 76, 108, 137, 32, 33,
1255 61, 62, 93, 94, 122, 123, 41, 42, 43, 44,
1256 45, 46, 70, 71, 72, 73, 74, 75, 102, 103,
1257 104, 105, 106, 107, 131, 132, 133, 134, 135, 136,
1258 34, 63, 95, 124, 35, 64, 96, 125, 36, 65,
1259 97, 126, 37, 66, 98, 127, 38, 67, 99, 128,
1260 39, 68, 100, 129, 40, 69, 101, 130
1261};
1262
1263static pj_int16_t AMRNB_ordermap67[134] =
1264{
1265 0, 1, 4, 3, 5, 6, 13, 7, 2, 8,
1266 9, 11, 15, 12, 14, 10, 28, 82, 29, 83,
1267 27, 81, 26, 80, 30, 84, 16, 55, 109, 56,
1268 110, 31, 85, 57, 111, 48, 73, 102, 127, 32,
1269 86, 51, 76, 105, 130, 52, 77, 106, 131, 58,
1270 112, 33, 87, 19, 23, 53, 78, 107, 132, 21,
1271 22, 18, 17, 20, 24, 25, 50, 75, 104, 129,
1272 47, 72, 101, 126, 54, 79, 108, 133, 46, 71,
1273 100, 125, 128, 103, 74, 49, 45, 70, 99, 124,
1274 42, 67, 96, 121, 39, 64, 93, 118, 38, 63,
1275 92, 117, 35, 60, 89, 114, 34, 59, 88, 113,
1276 44, 69, 98, 123, 43, 68, 97, 122, 41, 66,
1277 95, 120, 40, 65, 94, 119, 37, 62, 91, 116,
1278 36, 61, 90, 115
1279};
1280
1281static pj_int16_t AMRNB_ordermap59[118] =
1282{
1283 0, 1, 4, 5, 3, 6, 7, 2, 13, 15,
1284 8, 9, 11, 12, 14, 10, 16, 28, 74, 29,
1285 75, 27, 73, 26, 72, 30, 76, 51, 97, 50,
1286 71, 96, 117, 31, 77, 52, 98, 49, 70, 95,
1287 116, 53, 99, 32, 78, 33, 79, 48, 69, 94,
1288 115, 47, 68, 93, 114, 46, 67, 92, 113, 19,
1289 21, 23, 22, 18, 17, 20, 24, 111, 43, 89,
1290 110, 64, 65, 44, 90, 25, 45, 66, 91, 112,
1291 54, 100, 40, 61, 86, 107, 39, 60, 85, 106,
1292 36, 57, 82, 103, 35, 56, 81, 102, 34, 55,
1293 80, 101, 42, 63, 88, 109, 41, 62, 87, 108,
1294 38, 59, 84, 105, 37, 58, 83, 104
1295};
1296
1297static pj_int16_t AMRNB_ordermap515[103] =
1298{
1299 7, 6, 5, 4, 3, 2, 1, 0, 15, 14,
1300 13, 12, 11, 10, 9, 8, 23, 24, 25, 26,
1301 27, 46, 65, 84, 45, 44, 43, 64, 63, 62,
1302 83, 82, 81, 102, 101, 100, 42, 61, 80, 99,
1303 28, 47, 66, 85, 18, 41, 60, 79, 98, 29,
1304 48, 67, 17, 20, 22, 40, 59, 78, 97, 21,
1305 30, 49, 68, 86, 19, 16, 87, 39, 38, 58,
1306 57, 77, 35, 54, 73, 92, 76, 96, 95, 36,
1307 55, 74, 93, 32, 51, 33, 52, 70, 71, 89,
1308 90, 31, 50, 69, 88, 37, 56, 75, 94, 34,
1309 53, 72, 91
1310};
1311
1312static pj_int16_t AMRNB_ordermap475[95] =
1313{
1314 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1315 10, 11, 12, 13, 14, 15, 23, 24, 25, 26,
1316 27, 28, 48, 49, 61, 62, 82, 83, 47, 46,
1317 45, 44, 81, 80, 79, 78, 17, 18, 20, 22,
1318 77, 76, 75, 74, 29, 30, 43, 42, 41, 40,
1319 38, 39, 16, 19, 21, 50, 51, 59, 60, 63,
1320 64, 72, 73, 84, 85, 93, 94, 32, 33, 35,
1321 36, 53, 54, 56, 57, 66, 67, 69, 70, 87,
1322 88, 90, 91, 34, 55, 68, 89, 37, 58, 71,
1323 92, 31, 52, 65, 86
1324};
1325
1326static pj_int16_t *AMRNB_ordermaps[8] =
1327{
1328 AMRNB_ordermap475,
1329 AMRNB_ordermap515,
1330 AMRNB_ordermap59,
1331 AMRNB_ordermap67,
1332 AMRNB_ordermap74,
1333 AMRNB_ordermap795,
1334 AMRNB_ordermap102,
1335 AMRNB_ordermap122
1336};
1337
1338static pj_int16_t AMRWB_ordermap_660[] =
1339{
1340 0, 5, 6, 7, 61, 84, 107, 130, 62, 85,
1341 8, 4, 37, 38, 39, 40, 58, 81, 104, 127,
1342 60, 83, 106, 129, 108, 131, 128, 41, 42, 80,
1343 126, 1, 3, 57, 103, 82, 105, 59, 2, 63,
1344 109, 110, 86, 19, 22, 23, 64, 87, 18, 20,
1345 21, 17, 13, 88, 43, 89, 65, 111, 14, 24,
1346 25, 26, 27, 28, 15, 16, 44, 90, 66, 112,
1347 9, 11, 10, 12, 67, 113, 29, 30, 31, 32,
1348 34, 33, 35, 36, 45, 51, 68, 74, 91, 97,
1349 114, 120, 46, 69, 92, 115, 52, 75, 98, 121,
1350 47, 70, 93, 116, 53, 76, 99, 122, 48, 71,
1351 94, 117, 54, 77, 100, 123, 49, 72, 95, 118,
1352 55, 78, 101, 124, 50, 73, 96, 119, 56, 79,
1353 102, 125
1354};
1355
1356static pj_int16_t AMRWB_ordermap_885[] =
1357{
1358 0, 4, 6, 7, 5, 3, 47, 48, 49, 112,
1359 113, 114, 75, 106, 140, 171, 80, 111, 145, 176,
1360 77, 108, 142, 173, 78, 109, 143, 174, 79, 110,
1361 144, 175, 76, 107, 141, 172, 50, 115, 51, 2,
1362 1, 81, 116, 146, 19, 21, 12, 17, 18, 20,
1363 16, 25, 13, 10, 14, 24, 23, 22, 26, 8,
1364 15, 52, 117, 31, 82, 147, 9, 33, 11, 83,
1365 148, 53, 118, 28, 27, 84, 149, 34, 35, 29,
1366 46, 32, 30, 54, 119, 37, 36, 39, 38, 40,
1367 85, 150, 41, 42, 43, 44, 45, 55, 60, 65,
1368 70, 86, 91, 96, 101, 120, 125, 130, 135, 151,
1369 156, 161, 166, 56, 87, 121, 152, 61, 92, 126,
1370 157, 66, 97, 131, 162, 71, 102, 136, 167, 57,
1371 88, 122, 153, 62, 93, 127, 158, 67, 98, 132,
1372 163, 72, 103, 137, 168, 58, 89, 123, 154, 63,
1373 94, 128, 159, 68, 99, 133, 164, 73, 104, 138,
1374 169, 59, 90, 124, 155, 64, 95, 129, 160, 69,
1375 100, 134, 165, 74, 105, 139, 170
1376};
1377
1378static pj_int16_t AMRWB_ordermap_1265[] =
1379{
1380 0, 4, 6, 93, 143, 196, 246, 7, 5, 3,
1381 47, 48, 49, 50, 51, 150, 151, 152, 153, 154,
1382 94, 144, 197, 247, 99, 149, 202, 252, 96, 146,
1383 199, 249, 97, 147, 200, 250, 100, 203, 98, 148,
1384 201, 251, 95, 145, 198, 248, 52, 2, 1, 101,
1385 204, 155, 19, 21, 12, 17, 18, 20, 16, 25,
1386 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
1387 156, 31, 102, 205, 9, 33, 11, 103, 206, 54,
1388 157, 28, 27, 104, 207, 34, 35, 29, 46, 32,
1389 30, 55, 158, 37, 36, 39, 38, 40, 105, 208,
1390 41, 42, 43, 44, 45, 56, 106, 159, 209, 57,
1391 66, 75, 84, 107, 116, 125, 134, 160, 169, 178,
1392 187, 210, 219, 228, 237, 58, 108, 161, 211, 62,
1393 112, 165, 215, 67, 117, 170, 220, 71, 121, 174,
1394 224, 76, 126, 179, 229, 80, 130, 183, 233, 85,
1395 135, 188, 238, 89, 139, 192, 242, 59, 109, 162,
1396 212, 63, 113, 166, 216, 68, 118, 171, 221, 72,
1397 122, 175, 225, 77, 127, 180, 230, 81, 131, 184,
1398 234, 86, 136, 189, 239, 90, 140, 193, 243, 60,
1399 110, 163, 213, 64, 114, 167, 217, 69, 119, 172,
1400 222, 73, 123, 176, 226, 78, 128, 181, 231, 82,
1401 132, 185, 235, 87, 137, 190, 240, 91, 141, 194,
1402 244, 61, 111, 164, 214, 65, 115, 168, 218, 70,
1403 120, 173, 223, 74, 124, 177, 227, 79, 129, 182,
1404 232, 83, 133, 186, 236, 88, 138, 191, 241, 92,
1405 142, 195, 245
1406};
1407
1408static pj_int16_t AMRWB_ordermap_1425[] =
1409{
1410 0, 4, 6, 101, 159, 220, 278, 7, 5, 3,
1411 47, 48, 49, 50, 51, 166, 167, 168, 169, 170,
1412 102, 160, 221, 279, 107, 165, 226, 284, 104, 162,
1413 223, 281, 105, 163, 224, 282, 108, 227, 106, 164,
1414 225, 283, 103, 161, 222, 280, 52, 2, 1, 109,
1415 228, 171, 19, 21, 12, 17, 18, 20, 16, 25,
1416 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
1417 172, 31, 110, 229, 9, 33, 11, 111, 230, 54,
1418 173, 28, 27, 112, 231, 34, 35, 29, 46, 32,
1419 30, 55, 174, 37, 36, 39, 38, 40, 113, 232,
1420 41, 42, 43, 44, 45, 56, 114, 175, 233, 62,
1421 120, 181, 239, 75, 133, 194, 252, 57, 115, 176,
1422 234, 63, 121, 182, 240, 70, 128, 189, 247, 76,
1423 134, 195, 253, 83, 141, 202, 260, 92, 150, 211,
1424 269, 84, 142, 203, 261, 93, 151, 212, 270, 85,
1425 143, 204, 262, 94, 152, 213, 271, 86, 144, 205,
1426 263, 95, 153, 214, 272, 64, 122, 183, 241, 77,
1427 135, 196, 254, 65, 123, 184, 242, 78, 136, 197,
1428 255, 87, 145, 206, 264, 96, 154, 215, 273, 58,
1429 116, 177, 235, 66, 124, 185, 243, 71, 129, 190,
1430 248, 79, 137, 198, 256, 88, 146, 207, 265, 97,
1431 155, 216, 274, 59, 117, 178, 236, 67, 125, 186,
1432 244, 72, 130, 191, 249, 80, 138, 199, 257, 89,
1433 147, 208, 266, 98, 156, 217, 275, 60, 118, 179,
1434 237, 68, 126, 187, 245, 73, 131, 192, 250, 81,
1435 139, 200, 258, 90, 148, 209, 267, 99, 157, 218,
1436 276, 61, 119, 180, 238, 69, 127, 188, 246, 74,
1437 132, 193, 251, 82, 140, 201, 259, 91, 149, 210,
1438 268, 100, 158, 219, 277
1439};
1440
1441static pj_int16_t AMRWB_ordermap_1585[] =
1442{
1443 0, 4, 6, 109, 175, 244, 310, 7, 5, 3,
1444 47, 48, 49, 50, 51, 182, 183, 184, 185, 186,
1445 110, 176, 245, 311, 115, 181, 250, 316, 112, 178,
1446 247, 313, 113, 179, 248, 314, 116, 251, 114, 180,
1447 249, 315, 111, 177, 246, 312, 52, 2, 1, 117,
1448 252, 187, 19, 21, 12, 17, 18, 20, 16, 25,
1449 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
1450 188, 31, 118, 253, 9, 33, 11, 119, 254, 54,
1451 189, 28, 27, 120, 255, 34, 35, 29, 46, 32,
1452 30, 55, 190, 37, 36, 39, 38, 40, 121, 256,
1453 41, 42, 43, 44, 45, 56, 122, 191, 257, 63,
1454 129, 198, 264, 76, 142, 211, 277, 89, 155, 224,
1455 290, 102, 168, 237, 303, 57, 123, 192, 258, 70,
1456 136, 205, 271, 83, 149, 218, 284, 96, 162, 231,
1457 297, 62, 128, 197, 263, 75, 141, 210, 276, 88,
1458 154, 223, 289, 101, 167, 236, 302, 58, 124, 193,
1459 259, 71, 137, 206, 272, 84, 150, 219, 285, 97,
1460 163, 232, 298, 59, 125, 194, 260, 64, 130, 199,
1461 265, 67, 133, 202, 268, 72, 138, 207, 273, 77,
1462 143, 212, 278, 80, 146, 215, 281, 85, 151, 220,
1463 286, 90, 156, 225, 291, 93, 159, 228, 294, 98,
1464 164, 233, 299, 103, 169, 238, 304, 106, 172, 241,
1465 307, 60, 126, 195, 261, 65, 131, 200, 266, 68,
1466 134, 203, 269, 73, 139, 208, 274, 78, 144, 213,
1467 279, 81, 147, 216, 282, 86, 152, 221, 287, 91,
1468 157, 226, 292, 94, 160, 229, 295, 99, 165, 234,
1469 300, 104, 170, 239, 305, 107, 173, 242, 308, 61,
1470 127, 196, 262, 66, 132, 201, 267, 69, 135, 204,
1471 270, 74, 140, 209, 275, 79, 145, 214, 280, 82,
1472 148, 217, 283, 87, 153, 222, 288, 92, 158, 227,
1473 293, 95, 161, 230, 296, 100, 166, 235, 301, 105,
1474 171, 240, 306, 108, 174, 243, 309
1475};
1476
1477static pj_int16_t AMRWB_ordermap_1825[] =
1478{
1479 0, 4, 6, 121, 199, 280, 358, 7, 5, 3,
1480 47, 48, 49, 50, 51, 206, 207, 208, 209, 210,
1481 122, 200, 281, 359, 127, 205, 286, 364, 124, 202,
1482 283, 361, 125, 203, 284, 362, 128, 287, 126, 204,
1483 285, 363, 123, 201, 282, 360, 52, 2, 1, 129,
1484 288, 211, 19, 21, 12, 17, 18, 20, 16, 25,
1485 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
1486 212, 31, 130, 289, 9, 33, 11, 131, 290, 54,
1487 213, 28, 27, 132, 291, 34, 35, 29, 46, 32,
1488 30, 55, 214, 37, 36, 39, 38, 40, 133, 292,
1489 41, 42, 43, 44, 45, 56, 134, 215, 293, 198,
1490 299, 136, 120, 138, 60, 279, 58, 62, 357, 139,
1491 140, 295, 156, 57, 219, 297, 63, 217, 137, 170,
1492 300, 222, 64, 106, 61, 78, 294, 92, 142, 141,
1493 135, 221, 296, 301, 343, 59, 298, 184, 329, 315,
1494 220, 216, 265, 251, 218, 237, 352, 223, 157, 86,
1495 171, 87, 164, 351, 111, 302, 65, 178, 115, 323,
1496 72, 192, 101, 179, 93, 73, 193, 151, 337, 309,
1497 143, 274, 69, 324, 165, 150, 97, 338, 110, 310,
1498 330, 273, 68, 107, 175, 245, 114, 79, 113, 189,
1499 246, 259, 174, 71, 185, 96, 344, 100, 322, 83,
1500 334, 316, 333, 252, 161, 348, 147, 82, 269, 232,
1501 260, 308, 353, 347, 163, 231, 306, 320, 188, 270,
1502 146, 177, 266, 350, 256, 85, 149, 116, 191, 160,
1503 238, 258, 336, 305, 255, 88, 224, 99, 339, 230,
1504 228, 227, 272, 242, 241, 319, 233, 311, 102, 74,
1505 180, 275, 66, 194, 152, 325, 172, 247, 244, 261,
1506 117, 158, 166, 354, 75, 144, 108, 312, 94, 186,
1507 303, 80, 234, 89, 195, 112, 340, 181, 345, 317,
1508 326, 276, 239, 167, 118, 313, 70, 355, 327, 253,
1509 190, 176, 271, 104, 98, 153, 103, 90, 76, 267,
1510 277, 248, 225, 262, 182, 84, 154, 235, 335, 168,
1511 331, 196, 341, 249, 162, 307, 148, 349, 263, 321,
1512 257, 243, 229, 356, 159, 119, 67, 187, 173, 145,
1513 240, 77, 304, 332, 314, 342, 109, 254, 81, 278,
1514 105, 91, 346, 318, 183, 250, 197, 328, 95, 155,
1515 169, 268, 226, 236, 264
1516};
1517
1518static pj_int16_t AMRWB_ordermap_1985[] =
1519{
1520 0, 4, 6, 129, 215, 304, 390, 7, 5, 3,
1521 47, 48, 49, 50, 51, 222, 223, 224, 225, 226,
1522 130, 216, 305, 391, 135, 221, 310, 396, 132, 218,
1523 307, 393, 133, 219, 308, 394, 136, 311, 134, 220,
1524 309, 395, 131, 217, 306, 392, 52, 2, 1, 137,
1525 312, 227, 19, 21, 12, 17, 18, 20, 16, 25,
1526 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
1527 228, 31, 138, 313, 9, 33, 11, 139, 314, 54,
1528 229, 28, 27, 140, 315, 34, 35, 29, 46, 32,
1529 30, 55, 230, 37, 36, 39, 38, 40, 141, 316,
1530 41, 42, 43, 44, 45, 56, 142, 231, 317, 63,
1531 73, 92, 340, 82, 324, 149, 353, 159, 334, 165,
1532 338, 178, 163, 254, 77, 168, 257, 153, 343, 57,
1533 248, 238, 79, 252, 166, 67, 80, 201, 101, 267,
1534 143, 164, 341, 255, 339, 187, 376, 318, 78, 328,
1535 362, 115, 232, 242, 253, 290, 276, 62, 58, 158,
1536 68, 93, 179, 319, 148, 169, 154, 72, 385, 329,
1537 333, 344, 102, 83, 144, 233, 323, 124, 243, 192,
1538 354, 237, 64, 247, 202, 209, 150, 116, 335, 268,
1539 239, 299, 188, 196, 298, 94, 195, 258, 123, 363,
1540 384, 109, 325, 371, 170, 370, 84, 110, 295, 180,
1541 74, 210, 191, 106, 291, 205, 367, 381, 377, 206,
1542 355, 122, 119, 120, 383, 160, 105, 108, 277, 380,
1543 294, 284, 285, 345, 208, 269, 249, 366, 386, 300,
1544 297, 259, 125, 369, 197, 97, 194, 286, 211, 281,
1545 280, 183, 372, 87, 155, 283, 59, 348, 327, 184,
1546 76, 111, 330, 203, 349, 69, 98, 152, 145, 189,
1547 66, 320, 337, 173, 358, 251, 198, 174, 263, 262,
1548 126, 241, 193, 88, 388, 117, 95, 387, 112, 359,
1549 287, 244, 103, 272, 301, 171, 162, 234, 273, 127,
1550 373, 181, 292, 85, 378, 302, 121, 107, 364, 346,
1551 356, 212, 278, 213, 65, 382, 288, 207, 113, 175,
1552 99, 296, 374, 368, 199, 260, 185, 336, 331, 161,
1553 270, 264, 250, 240, 75, 350, 151, 60, 89, 321,
1554 156, 274, 360, 326, 70, 282, 167, 146, 352, 81,
1555 91, 389, 266, 245, 177, 235, 190, 256, 204, 342,
1556 128, 118, 303, 104, 379, 182, 114, 375, 200, 96,
1557 293, 172, 214, 365, 279, 86, 289, 351, 347, 357,
1558 261, 186, 176, 271, 90, 100, 147, 322, 275, 361,
1559 71, 332, 61, 265, 157, 246, 236
1560};
1561
1562static pj_int16_t AMRWB_ordermap_2305[] =
1563{
1564 0, 4, 6, 145, 247, 352, 454, 7, 5, 3,
1565 47, 48, 49, 50, 51, 254, 255, 256, 257, 258,
1566 146, 248, 353, 455, 151, 253, 358, 460, 148, 250,
1567 355, 457, 149, 251, 356, 458, 152, 359, 150, 252,
1568 357, 459, 147, 249, 354, 456, 52, 2, 1, 153,
1569 360, 259, 19, 21, 12, 17, 18, 20, 16, 25,
1570 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
1571 260, 31, 154, 361, 9, 33, 11, 155, 362, 54,
1572 261, 28, 27, 156, 363, 34, 35, 29, 46, 32,
1573 30, 55, 262, 37, 36, 39, 38, 40, 157, 364,
1574 41, 42, 43, 44, 45, 56, 158, 263, 365, 181,
1575 192, 170, 79, 57, 399, 90, 159, 297, 377, 366,
1576 275, 68, 183, 388, 286, 194, 299, 92, 70, 182,
1577 401, 172, 59, 91, 58, 400, 368, 161, 81, 160,
1578 264, 171, 80, 389, 390, 378, 379, 193, 298, 69,
1579 266, 265, 367, 277, 288, 276, 287, 184, 60, 195,
1580 82, 93, 71, 369, 402, 173, 162, 444, 300, 391,
1581 98, 76, 278, 61, 267, 374, 135, 411, 167, 102,
1582 380, 200, 87, 178, 65, 94, 204, 124, 72, 342,
1583 189, 305, 381, 396, 433, 301, 226, 407, 289, 237,
1584 113, 215, 185, 128, 309, 403, 116, 320, 196, 331,
1585 370, 422, 174, 64, 392, 83, 425, 219, 134, 188,
1586 432, 112, 427, 139, 279, 163, 436, 208, 447, 218,
1587 236, 229, 97, 294, 385, 230, 166, 268, 177, 443,
1588 225, 426, 101, 272, 138, 127, 290, 117, 347, 199,
1589 414, 95, 140, 240, 410, 395, 209, 129, 283, 346,
1590 105, 241, 437, 86, 308, 448, 203, 345, 186, 107,
1591 220, 415, 334, 319, 106, 313, 118, 123, 73, 207,
1592 421, 214, 384, 373, 438, 62, 371, 341, 75, 449,
1593 168, 323, 164, 242, 416, 324, 304, 197, 335, 404,
1594 271, 63, 191, 325, 96, 169, 231, 280, 312, 187,
1595 406, 84, 201, 100, 67, 382, 175, 336, 202, 330,
1596 269, 393, 376, 383, 293, 307, 409, 179, 285, 314,
1597 302, 372, 398, 190, 180, 89, 99, 103, 232, 78,
1598 88, 77, 136, 387, 165, 198, 394, 125, 176, 428,
1599 74, 375, 238, 227, 66, 273, 282, 141, 306, 412,
1600 114, 85, 130, 348, 119, 291, 296, 386, 233, 397,
1601 303, 405, 284, 445, 423, 221, 210, 205, 450, 108,
1602 274, 434, 216, 343, 337, 142, 243, 321, 408, 451,
1603 310, 292, 120, 109, 281, 439, 270, 429, 332, 295,
1604 418, 211, 315, 222, 326, 131, 430, 244, 327, 349,
1605 417, 316, 143, 338, 440, 234, 110, 212, 452, 245,
1606 121, 419, 350, 223, 132, 441, 328, 413, 317, 339,
1607 126, 104, 137, 446, 344, 239, 435, 115, 333, 206,
1608 322, 217, 228, 424, 453, 311, 351, 111, 442, 224,
1609 213, 122, 431, 340, 235, 246, 133, 144, 420, 329,
1610 318
1611};
1612
1613static pj_int16_t AMRWB_ordermap_2385[] =
1614{
1615 0, 4, 6, 145, 251, 360, 466, 7, 5, 3,
1616 47, 48, 49, 50, 51, 262, 263, 264, 265, 266,
1617 146, 252, 361, 467, 151, 257, 366, 472, 148, 254,
1618 363, 469, 149, 255, 364, 470, 156, 371, 150, 256,
1619 365, 471, 147, 253, 362, 468, 52, 2, 1, 157,
1620 372, 267, 19, 21, 12, 17, 18, 20, 16, 25,
1621 13, 10, 14, 24, 23, 22, 26, 8, 15, 53,
1622 268, 31, 152, 153, 154, 155, 258, 259, 260, 261,
1623 367, 368, 369, 370, 473, 474, 475, 476, 158, 373,
1624 9, 33, 11, 159, 374, 54, 269, 28, 27, 160,
1625 375, 34, 35, 29, 46, 32, 30, 55, 270, 37,
1626 36, 39, 38, 40, 161, 376, 41, 42, 43, 44,
1627 45, 56, 162, 271, 377, 185, 196, 174, 79, 57,
1628 411, 90, 163, 305, 389, 378, 283, 68, 187, 400,
1629 294, 198, 307, 92, 70, 186, 413, 176, 59, 91,
1630 58, 412, 380, 165, 81, 164, 272, 175, 80, 401,
1631 402, 390, 391, 197, 306, 69, 274, 273, 379, 285,
1632 296, 284, 295, 188, 60, 199, 82, 93, 71, 381,
1633 414, 177, 166, 456, 308, 403, 98, 76, 286, 61,
1634 275, 386, 135, 423, 171, 102, 392, 204, 87, 182,
1635 65, 94, 208, 124, 72, 350, 193, 313, 393, 408,
1636 445, 309, 230, 419, 297, 241, 113, 219, 189, 128,
1637 317, 415, 116, 328, 200, 339, 382, 434, 178, 64,
1638 404, 83, 437, 223, 134, 192, 444, 112, 439, 139,
1639 287, 167, 448, 212, 459, 222, 240, 233, 97, 302,
1640 397, 234, 170, 276, 181, 455, 229, 438, 101, 280,
1641 138, 127, 298, 117, 355, 203, 426, 95, 140, 244,
1642 422, 407, 213, 129, 291, 354, 105, 245, 449, 86,
1643 316, 460, 207, 353, 190, 107, 224, 427, 342, 327,
1644 106, 321, 118, 123, 73, 211, 433, 218, 396, 385,
1645 450, 62, 383, 349, 75, 461, 172, 331, 168, 246,
1646 428, 332, 312, 201, 343, 416, 279, 63, 195, 333,
1647 96, 173, 235, 288, 320, 191, 418, 84, 205, 100,
1648 67, 394, 179, 344, 206, 338, 277, 405, 388, 395,
1649 301, 315, 421, 183, 293, 322, 310, 384, 410, 194,
1650 184, 89, 99, 103, 236, 78, 88, 77, 136, 399,
1651 169, 202, 406, 125, 180, 440, 74, 387, 242, 231,
1652 66, 281, 290, 141, 314, 424, 114, 85, 130, 356,
1653 119, 299, 304, 398, 237, 409, 311, 417, 292, 457,
1654 435, 225, 214, 209, 462, 108, 282, 446, 220, 351,
1655 345, 142, 247, 329, 420, 463, 318, 300, 120, 109,
1656 289, 451, 278, 441, 340, 303, 430, 215, 323, 226,
1657 334, 131, 442, 248, 335, 357, 429, 324, 143, 346,
1658 452, 238, 110, 216, 464, 249, 121, 431, 358, 227,
1659 132, 453, 336, 425, 325, 347, 126, 104, 137, 458,
1660 352, 243, 447, 115, 341, 210, 330, 221, 232, 436,
1661 465, 319, 359, 111, 454, 228, 217, 122, 443, 348,
1662 239, 250, 133, 144, 432, 337, 326
1663};
1664
1665static pj_int16_t *AMRWB_ordermaps[9] =
1666{
1667 AMRWB_ordermap_660,
1668 AMRWB_ordermap_885,
1669 AMRWB_ordermap_1265,
1670 AMRWB_ordermap_1425,
1671 AMRWB_ordermap_1585,
1672 AMRWB_ordermap_1825,
1673 AMRWB_ordermap_1985,
1674 AMRWB_ordermap_2305,
1675 AMRWB_ordermap_2385
1676};
1677
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001678static pj_uint8_t AMRNB_framelen[16] =
1679 {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 5};
1680static pj_uint16_t AMRNB_framelenbits[9] =
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001681 {95, 103, 118, 134, 148, 159, 204, 244, 39};
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001682static pj_uint16_t AMRNB_bitrates[8] =
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001683 {4750, 5150, 5900, 6700, 7400, 7950, 10200, 12200};
1684
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001685static pj_uint8_t AMRWB_framelen[16] =
1686 {17, 23, 32, 37, 40, 46, 50, 58, 60, 5, 0, 0, 0, 0, 0, 5};
1687static pj_uint16_t AMRWB_framelenbits[10] =
1688 {132, 177, 253, 285, 317, 365, 397, 461, 477, 40};
1689static pj_uint16_t AMRWB_bitrates[9] =
1690 {6600, 8850, 12650, 14250, 15850, 18250, 19850, 23050, 23850};
1691
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001692
1693/* Get mode based on bitrate */
1694static int amr_get_mode(unsigned bitrate)
1695{
1696 int mode = -1;
1697
1698 if(bitrate==4750){
1699 mode = 0;
1700 } else if(bitrate==5150){
1701 mode = 1;
1702 } else if(bitrate==5900){
1703 mode = 2;
1704 } else if(bitrate==6700){
1705 mode = 3;
1706 } else if(bitrate==7400){
1707 mode = 4;
1708 } else if(bitrate==7950){
1709 mode = 5;
1710 } else if(bitrate==10200){
1711 mode = 6;
1712 } else if(bitrate==12200){
1713 mode = 7;
1714
1715 /* AMRWB */
1716 } else if(bitrate==6600){
1717 mode = 0;
1718 } else if(bitrate==8850){
1719 mode = 1;
1720 } else if(bitrate==12650){
1721 mode = 2;
1722 } else if(bitrate==14250){
1723 mode = 3;
1724 } else if(bitrate==15850){
1725 mode = 4;
1726 } else if(bitrate==18250){
1727 mode = 5;
1728 } else if(bitrate==19850){
1729 mode = 6;
1730 } else if(bitrate==23050){
1731 mode = 7;
1732 } else if(bitrate==23850){
1733 mode = 8;
1734 }
1735 return mode;
1736}
1737
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001738/* Rearrange AMR bitstream of rtp_frame:
1739 * - make the start_bit to be 0
1740 * - if it is speech frame, reorder bitstream from sensitivity bits order
1741 * to encoder bits order.
1742 */
1743static void predecode_amr( ipp_private_t *codec_data,
1744 const pjmedia_frame *rtp_frame,
1745 USC_Bitstream *usc_frame)
1746{
1747 pj_uint8_t FT, Q;
1748 pj_int8_t amr_bits[477 + 7] = {0};
1749 pj_int8_t *p_amr_bits = &amr_bits[0];
1750 unsigned i;
1751 /* read cursor */
1752 pj_uint8_t *r = (pj_uint8_t*)rtp_frame->buf;
1753 pj_uint8_t start_bit;
1754 /* write cursor */
1755 pj_uint8_t *w = (pj_uint8_t*)rtp_frame->buf;
1756 /* env vars for AMR or AMRWB */
1757 pj_bool_t AMRWB;
1758 pj_uint8_t SID_FT = 8;
1759 pj_uint8_t *framelen_tbl = AMRNB_framelen;
1760 pj_uint16_t *framelenbit_tbl = AMRNB_framelenbits;
1761 pj_uint16_t *bitrate_tbl = AMRNB_bitrates;
1762 pj_int16_t **order_map = AMRNB_ordermaps;
1763
1764 AMRWB = ipp_codec[codec_data->codec_idx].pt == PJMEDIA_RTP_PT_AMRWB;
1765 if (AMRWB) {
1766 SID_FT = 9;
1767 framelen_tbl = AMRWB_framelen;
1768 framelenbit_tbl = AMRWB_framelenbits;
1769 bitrate_tbl = AMRWB_bitrates;
1770 order_map = AMRWB_ordermaps;
1771 }
1772
1773 start_bit = (pj_uint8_t)((rtp_frame->bit_info & 0x0700) >> 8);
1774 FT = (pj_uint8_t)(rtp_frame->bit_info & 0x0F);
1775 Q = (pj_uint8_t)((rtp_frame->bit_info >> 16) & 0x01);
1776
1777 /* unpack AMR bitstream if there is any data */
1778 if (FT <= SID_FT) {
1779 i = 0;
1780 if (start_bit) {
1781 for (; i < (unsigned)(8-start_bit); ++i)
1782 *p_amr_bits++ = (pj_uint8_t)(*r >> (7-start_bit-i)) & 1;
1783 ++r;
1784 }
1785 for(; i < framelenbit_tbl[FT]; i += 8) {
1786 *p_amr_bits++ = (*r >> 7) & 1;
1787 *p_amr_bits++ = (*r >> 6) & 1;
1788 *p_amr_bits++ = (*r >> 5) & 1;
1789 *p_amr_bits++ = (*r >> 4) & 1;
1790 *p_amr_bits++ = (*r >> 3) & 1;
1791 *p_amr_bits++ = (*r >> 2) & 1;
1792 *p_amr_bits++ = (*r >> 1) & 1;
1793 *p_amr_bits++ = (*r ) & 1;
1794 ++r;
1795 }
1796 }
1797
1798 if (FT < SID_FT) {
1799 /* Speech */
1800 pj_int16_t *order_map_;
1801
1802 order_map_ = order_map[FT];
1803 pj_bzero(rtp_frame->buf, rtp_frame->size);
1804 for(i = 0; i < framelenbit_tbl[FT]; ++i) {
1805 if (amr_bits[i]) {
1806 pj_uint16_t bitpos;
1807 bitpos = order_map_[i];
1808 w[bitpos>>3] |= 1 << (7 - (bitpos % 8));
1809 }
1810 }
1811 usc_frame->nbytes = framelen_tbl[FT];
1812 if (Q)
1813 usc_frame->frametype = 0;
1814 else
1815 usc_frame->frametype = AMRWB ? 6 : 5;
1816 usc_frame->bitrate = bitrate_tbl[FT];
1817 } else if (FT == SID_FT) {
1818 /* SID */
1819 pj_uint8_t w_bitptr = 0;
1820 pj_uint8_t STI;
1821 pj_uint8_t FT_;
1822
1823 STI = amr_bits[35];
1824 if (AMRWB)
1825 FT_ = (amr_bits[36] << 3) | (amr_bits[37] << 2) | (amr_bits[38] << 1) | amr_bits[39];
1826 else
1827 FT_ = (amr_bits[36] << 2) | (amr_bits[37] << 1) | amr_bits[38];
1828
1829 pj_bzero(rtp_frame->buf, rtp_frame->size);
1830 for(i = 0; i < framelenbit_tbl[FT]; ++i) {
1831 if (amr_bits[i])
1832 *w |= (1 << (7-w_bitptr));
1833
1834 if (++w_bitptr == 8) {
1835 ++w;
1836 w_bitptr = 0;
1837 }
1838 }
1839
1840 usc_frame->nbytes = 5;
1841 if (Q)
1842 usc_frame->frametype = STI? 2 : 1;
1843 else
1844 usc_frame->frametype = AMRWB ? 7 : 6;
1845
1846 usc_frame->bitrate = bitrate_tbl[FT_];
1847 } else {
1848 /* NO DATA */
1849 usc_frame->nbytes = 0;
1850 usc_frame->frametype = 3;
1851 usc_frame->bitrate = 0;
1852 }
1853
1854 usc_frame->pBuffer = rtp_frame->buf;
1855}
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001856
1857static pj_status_t pack_amr(ipp_private_t *codec_data, void *pkt,
1858 pj_size_t *pkt_size, pj_size_t max_pkt_size)
1859{
1860 /* Settings */
1861 pj_uint8_t CMR = 15; /* We don't request any code mode */
1862 pj_uint8_t octet_aligned = 0; /* default==0 when SDP not specifying */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001863 /* Write cursor */
1864 pj_uint8_t *w = (pj_uint8_t*)pkt;
1865 pj_uint8_t w_bitptr = 0;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001866 /* Read cursor */
1867 pj_uint8_t *r;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001868 /* env vars for AMR or AMRWB */
1869 pj_bool_t AMRWB;
1870 pj_uint8_t SID_FT = 8;
1871 pj_uint8_t *framelen_tbl = AMRNB_framelen;
1872 pj_uint16_t *framelenbit_tbl = AMRNB_framelenbits;
1873 pj_uint16_t *bitrate_tbl = AMRNB_bitrates;
1874 pj_int16_t **order_map = AMRNB_ordermaps;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001875
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001876 AMRWB = ipp_codec[codec_data->codec_idx].pt == PJMEDIA_RTP_PT_AMRWB;
1877 if (AMRWB) {
1878 SID_FT = 9;
1879 framelen_tbl = AMRWB_framelen;
1880 framelenbit_tbl = AMRWB_framelenbits;
1881 bitrate_tbl = AMRWB_bitrates;
1882 order_map = AMRWB_ordermaps;
1883 }
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001884
1885 PJ_TODO(Make_sure_buffer_is_enough_for_packing_AMR_packet);
1886
1887 r = (pj_uint8_t*)pkt + max_pkt_size - *pkt_size;
1888
1889 /* Align pkt buf right */
1890 pj_memmove(r, w, *pkt_size);
1891
1892 /* Code Mode Request, 4 bits */
1893 *w = (CMR << 4);
1894 w_bitptr = 4;
1895 if (octet_aligned) {
1896 ++w;
1897 w_bitptr = 0;
1898 }
1899
1900 /* Table Of Contents, 6 bits each */
1901 for (;;) {
1902 pj_uint8_t TOC;
1903 pj_uint8_t F, FT, Q;
1904
1905 F = (*r & 0x40) == 0;
1906 FT = (*r & 0x0F);
1907 Q = (*r & 0x80) == 0;
1908
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001909 pj_assert(FT <= SID_FT || FT == 14 || FT == 15);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001910 TOC = (pj_uint8_t)((F<<5) | (FT<<1) | Q);
1911
1912 if (w_bitptr == 0) {
1913 *w = TOC<<2;
1914 w_bitptr = 6;
1915 } else if (w_bitptr == 2) {
1916 *w++ |= TOC;
1917 w_bitptr = 0;
1918 } else if (w_bitptr == 4) {
1919 *w++ |= TOC>>2;
1920 *w = TOC<<6;
1921 w_bitptr = 2;
1922 } else if (w_bitptr == 6) {
1923 *w++ |= TOC>>4;
1924 *w = TOC<<4;
1925 w_bitptr = 4;
1926 }
1927
1928 if (octet_aligned) {
1929 ++w;
1930 w_bitptr = 0;
1931 }
1932
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001933 if (FT > SID_FT)
1934 /* NO DATA */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001935 r += 1;
1936 else
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001937 r += framelen_tbl[FT] + 1;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001938
1939 /* Last frame */
1940 if (!F)
1941 break;
1942 }
1943
1944 /* Speech frames */
1945 r = (pj_uint8_t*)pkt + max_pkt_size - *pkt_size;
1946
1947 for (;;) {
1948 pj_uint8_t F, FT;
1949 pj_int8_t amr_bits[477 + 7] = {0};
1950 pj_int8_t *p_amr_bits = &amr_bits[0];
1951 unsigned i;
1952
1953 F = (*r & 0x40) == 0;
1954 FT = (*r & 0x0F);
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001955 pj_assert(FT <= SID_FT || FT == 14 || FT == 15);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001956
1957 ++r;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001958 if (FT > SID_FT) {
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001959 if (!F)
1960 break;
1961 continue;
1962 }
1963
1964 /* Unpack bits */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001965 for(i = 0; i < framelen_tbl[FT]; ++i) {
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001966 *p_amr_bits++ = (*r >> 7) & 1;
1967 *p_amr_bits++ = (*r >> 6) & 1;
1968 *p_amr_bits++ = (*r >> 5) & 1;
1969 *p_amr_bits++ = (*r >> 4) & 1;
1970 *p_amr_bits++ = (*r >> 3) & 1;
1971 *p_amr_bits++ = (*r >> 2) & 1;
1972 *p_amr_bits++ = (*r >> 1) & 1;
1973 *p_amr_bits++ = (*r ) & 1;
1974 ++r;
1975 }
1976
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001977 if (FT < SID_FT) {
1978 /* Speech */
1979 pj_int16_t *order_map_;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001980
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001981 /* Put bits in the packet, sensitivity descending ordered */
1982 order_map_ = order_map[FT];
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001983 if (w_bitptr == 0) *w = 0;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00001984 for(i = 0; i < framelenbit_tbl[FT]; ++i) {
1985 pj_uint8_t bit;
1986 bit = amr_bits[order_map_[i]];
1987
1988 if (bit)
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00001989 *w |= (1 << (7-w_bitptr));
1990
1991 if (++w_bitptr == 8) {
1992 w_bitptr = 0;
1993 ++w;
1994 *w = 0;
1995 }
1996 }
1997
1998 if (octet_aligned) {
1999 ++w;
2000 w_bitptr = 0;
2001 }
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002002 } else if (FT == SID_FT) {
2003 /* SID */
2004 pj_uint8_t STI = 0;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002005
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002006 amr_bits[35] = (STI & 1);
2007
2008 if (AMRWB) {
2009 amr_bits[36] = (FT >> 3) & 1;
2010 amr_bits[37] = (FT >> 2) & 1;
2011 amr_bits[38] = (FT >> 1) & 1;
2012 amr_bits[39] = (FT) & 1;
2013 } else {
2014 amr_bits[36] = (FT >> 2) & 1;
2015 amr_bits[37] = (FT >> 1) & 1;
2016 amr_bits[38] = (FT) & 1;
2017 }
2018
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002019 if (w_bitptr == 0) *w = 0;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002020 for(i = 0; i < framelenbit_tbl[FT]; ++i) {
2021 if (amr_bits[i])
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002022 *w |= (1 << (7-w_bitptr));
2023
2024 if (++w_bitptr == 8) {
2025 w_bitptr = 0;
2026 ++w;
2027 *w = 0;
2028 }
2029 }
2030
2031 if (octet_aligned) {
2032 ++w;
2033 w_bitptr = 0;
2034 }
2035 }
2036
2037 if (!F)
2038 break;
2039 }
2040
2041 *pkt_size = w - (pj_uint8_t*)pkt;
2042 if (w_bitptr)
2043 *pkt_size += 1;
2044
2045 pj_assert(*pkt_size <= max_pkt_size);
2046
2047 return PJ_SUCCESS;
2048}
2049
2050
2051/* Parse AMR payload into frames. Frame.bit_info will contain start_bit and
2052 * AMR frame type, it is mapped as below (bit 0:MSB - bit 31:LSB)
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002053 * - bit 0-16: degraded quality flag (Q)
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002054 * - bit 17-24: start_bit
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002055 * - bit 25-32: frame_type (FT)
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002056 */
2057static pj_status_t parse_amr(ipp_private_t *codec_data, void *pkt,
2058 pj_size_t pkt_size, const pj_timestamp *ts,
2059 unsigned *frame_cnt, pjmedia_frame frames[])
2060{
2061 unsigned cnt = 0;
2062 pj_timestamp ts_ = *ts;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002063 /* Settings */
2064 pj_uint8_t CMR = 15; /* See if remote request code mode */
2065 pj_uint8_t octet_aligned = 0; /* default==0 when SDP not specifying */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002066 /* Read cursor */
2067 pj_uint8_t r_bitptr = 0;
2068 pj_uint8_t *r = (pj_uint8_t*)pkt;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002069 /* env vars for AMR or AMRWB */
2070 pj_bool_t AMRWB;
2071 pj_uint8_t SID_FT = 8;
2072 pj_uint8_t *framelen_tbl = AMRNB_framelen;
2073 pj_uint16_t *framelenbit_tbl = AMRNB_framelenbits;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002074
2075 PJ_UNUSED_ARG(pkt_size);
2076
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002077 AMRWB = ipp_codec[codec_data->codec_idx].pt == PJMEDIA_RTP_PT_AMRWB;
2078 if (AMRWB) {
2079 SID_FT = 9;
2080 framelen_tbl = AMRWB_framelen;
2081 framelenbit_tbl = AMRWB_framelenbits;
2082 }
2083
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002084 *frame_cnt = 0;
2085
2086 /* Code Mode Request, 4 bits */
2087 CMR = (*r >> 4) & 0x0F;
2088 r_bitptr = 4;
2089 if (octet_aligned) {
2090 ++r;
2091 r_bitptr = 0;
2092 }
2093
2094 /* Table Of Contents, 6 bits each */
2095 for (;;) {
2096 pj_uint8_t TOC = 0;
2097 pj_uint8_t F, FT, Q;
2098
2099 if (r_bitptr == 0) {
2100 TOC = *r >> 2;
2101 r_bitptr = 6;
2102 } else if (r_bitptr == 2) {
2103 TOC = *r++ & 0x3F;
2104 r_bitptr = 0;
2105 } else if (r_bitptr == 4) {
2106 TOC = (*r++ & 0x0f) << 2;
2107 TOC |= *r >> 6;
2108 r_bitptr = 2;
2109 } else if (r_bitptr == 6) {
2110 TOC = (*r++ & 0x03) << 4;
2111 TOC |= *r >> 4;
2112 r_bitptr = 4;
2113 }
2114
2115 F = TOC >> 5;
2116 FT = (TOC >> 1) & 0x0F;
2117 Q = TOC & 1;
2118
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002119 if (FT > SID_FT && FT < 14) {
2120 pj_assert(!"Invalid AMR frametype, stream may be corrupted!");
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002121 break;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002122 }
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002123
2124 if (octet_aligned) {
2125 ++r;
2126 r_bitptr = 0;
2127 }
2128
2129 /* Set frame attributes */
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002130 frames[cnt].bit_info = FT | (Q << 16);
2131 frames[cnt].timestamp = ts_;
2132 frames[cnt].type = PJMEDIA_FRAME_TYPE_AUDIO;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002133
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002134 ts_.u64 += ipp_codec[codec_data->codec_idx].samples_per_frame;
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002135 ++cnt;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002136
2137 if (!F || cnt == *frame_cnt)
2138 break;
2139 }
2140 *frame_cnt = cnt;
2141
2142 cnt = 0;
2143
2144 /* Speech frames */
2145 while (cnt < *frame_cnt) {
2146 unsigned FT;
2147
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002148 FT = frames[cnt].bit_info & 0x0F;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002149
2150 frames[cnt].bit_info |= (r_bitptr << 8);
2151 frames[cnt].buf = r;
2152
2153 if (octet_aligned) {
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002154 r += framelen_tbl[FT];
2155 frames[cnt].size = framelen_tbl[FT];
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002156 } else {
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002157 if (FT == 14 || FT == 15) {
2158 /* NO DATA */
2159 frames[cnt].size = 0;
2160 } else {
2161 unsigned adv_bit;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002162
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002163 adv_bit = framelenbit_tbl[FT] + r_bitptr;
2164 r += adv_bit >> 3;
2165 r_bitptr = (pj_uint8_t)(adv_bit % 8);
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002166
Nanang Izzuddindf361e02008-08-16 06:46:08 +00002167 frames[cnt].size = adv_bit >> 3;
2168 if (r_bitptr)
2169 ++frames[cnt].size;
2170 }
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002171 }
2172 ++cnt;
2173 }
2174
2175 return PJ_SUCCESS;
2176}
2177
Nanang Izzuddin493a8db2008-08-15 13:17:39 +00002178#endif /* PJMEDIA_HAS_INTEL_IPP_CODEC_AMR */
2179
2180
2181#ifdef _MSC_VER
2182# pragma comment( lib, "ippcore.lib")
2183# pragma comment( lib, "ipps.lib")
2184# pragma comment( lib, "ippsc.lib")
2185# pragma comment( lib, "ippsr.lib")
2186# pragma comment( lib, "usc.lib")
2187#endif
2188
2189
2190#endif /* PJMEDIA_HAS_INTEL_IPP_CODECS */
2191