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