Changeset 4331 for pjproject/trunk/pjmedia/src/pjmedia-codec/opencore_amr.c
- Timestamp:
- Jan 23, 2013 6:18:18 AM (12 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia-codec/opencore_amr.c
r4314 r4331 1 1 /* $Id$ */ 2 2 /* 3 * Copyright (C) 2011 Teluu Inc. (http://www.teluu.com)3 * Copyright (C) 2011-2013 Teluu Inc. (http://www.teluu.com) 4 4 * Copyright (C) 2011 Dan Arrhenius <dan@keystream.se> 5 5 * … … 20 20 21 21 /* 22 * AMR -NB codec implementation with OpenCORE AMRNBlibrary22 * AMR codec implementation with OpenCORE AMR library 23 23 */ 24 24 #include <pjmedia-codec/g722.h> … … 39 39 #if defined(PJMEDIA_HAS_OPENCORE_AMRNB_CODEC) && \ 40 40 (PJMEDIA_HAS_OPENCORE_AMRNB_CODEC != 0) 41 41 #define USE_AMRNB 42 #endif 43 44 #if defined(PJMEDIA_HAS_OPENCORE_AMRWB_CODEC) && \ 45 (PJMEDIA_HAS_OPENCORE_AMRWB_CODEC != 0) 46 #define USE_AMRWB 47 #endif 48 49 #if defined(USE_AMRNB) || defined(USE_AMRWB) 50 51 #ifdef USE_AMRNB 42 52 #include <opencore-amrnb/interf_enc.h> 43 53 #include <opencore-amrnb/interf_dec.h> 54 #endif 55 56 #ifdef USE_AMRWB 57 #include <vo-amrwbenc/enc_if.h> 58 #include <opencore-amrwb/dec_if.h> 59 #endif 60 44 61 #include <pjmedia-codec/amr_helper.h> 45 #include <pjmedia-codec/opencore_amr nb.h>46 47 #define THIS_FILE "opencore_amr nb.c"62 #include <pjmedia-codec/opencore_amr.h> 63 64 #define THIS_FILE "opencore_amr.c" 48 65 49 66 /* Tracing */ … … 59 76 #define USE_PJMEDIA_PLC 1 60 77 61 62 63 /* Prototypes for AMR-NB factory */ 78 #define FRAME_LENGTH_MS 20 79 80 81 /* Prototypes for AMR factory */ 64 82 static pj_status_t amr_test_alloc(pjmedia_codec_factory *factory, 65 83 const pjmedia_codec_info *id ); … … 76 94 pjmedia_codec *codec ); 77 95 78 /* Prototypes for AMR -NBimplementation. */96 /* Prototypes for AMR implementation. */ 79 97 static pj_status_t amr_codec_init(pjmedia_codec *codec, 80 98 pj_pool_t *pool ); … … 104 122 105 123 106 /* Definition for AMR -NBcodec operations. */124 /* Definition for AMR codec operations. */ 107 125 static pjmedia_codec_op amr_op = 108 126 { … … 117 135 }; 118 136 119 /* Definition for AMR -NBcodec factory operations. */137 /* Definition for AMR codec factory operations. */ 120 138 static pjmedia_codec_factory_op amr_factory_op = 121 139 { … … 129 147 130 148 131 /* AMR -NBfactory */149 /* AMR factory */ 132 150 static struct amr_codec_factory 133 151 { … … 135 153 pjmedia_endpt *endpt; 136 154 pj_pool_t *pool; 155 pj_bool_t init[2]; 137 156 } amr_codec_factory; 138 157 139 158 140 /* AMR -NBcodec private data. */159 /* AMR codec private data. */ 141 160 struct amr_data 142 161 { 143 162 pj_pool_t *pool; 163 unsigned clock_rate; 144 164 void *encoder; 145 165 void *decoder; … … 155 175 }; 156 176 157 static pjmedia_codec_amrnb_config def_config = 158 { 177 /* Index for AMR tables. */ 178 enum 179 { 180 IDX_AMR_NB, /* Index for narrowband. */ 181 IDX_AMR_WB /* Index for wideband. */ 182 }; 183 184 static pjmedia_codec_amr_config def_config[2] = 185 {{ /* AMR-NB */ 159 186 PJ_FALSE, /* octet align */ 160 187 5900 /* bitrate */ 161 }; 162 163 164 165 /* 166 * Initialize and register AMR-NB codec factory to pjmedia endpoint. 167 */ 168 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_init( pjmedia_endpt *endpt ) 188 }, 189 { /* AMR-WB */ 190 PJ_FALSE, /* octet align */ 191 12650 /* bitrate */ 192 }}; 193 194 static const pj_uint16_t* amr_bitrates[2] = 195 {pjmedia_codec_amrnb_bitrates, pjmedia_codec_amrwb_bitrates}; 196 197 198 /* 199 * Initialize and register AMR codec factory to pjmedia endpoint. 200 */ 201 static pj_status_t amr_init( pjmedia_endpt *endpt ) 169 202 { 170 203 pjmedia_codec_mgr *codec_mgr; … … 175 208 return PJ_SUCCESS; 176 209 177 /* Create AMR -NBcodec factory. */210 /* Create AMR codec factory. */ 178 211 amr_codec_factory.base.op = &amr_factory_op; 179 212 amr_codec_factory.base.factory_data = NULL; 180 213 amr_codec_factory.endpt = endpt; 181 214 182 amr_codec_factory.pool = pjmedia_endpt_create_pool(endpt, "amr nb", 1000,215 amr_codec_factory.pool = pjmedia_endpt_create_pool(endpt, "amr", 1000, 183 216 1000); 184 217 if (!amr_codec_factory.pool) … … 215 248 } 216 249 217 218 /* 219 * Unregister AMR-NB codec factory from pjmedia endpoint and deinitialize 220 * the AMR-NB codec library. 221 */ 222 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_deinit(void) 250 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_init( pjmedia_endpt *endpt ) 251 { 252 amr_codec_factory.init[IDX_AMR_NB] = PJ_TRUE; 253 254 return amr_init(endpt); 255 } 256 257 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrwb_init( pjmedia_endpt *endpt ) 258 { 259 amr_codec_factory.init[IDX_AMR_WB] = PJ_TRUE; 260 261 return amr_init(endpt); 262 } 263 264 265 /* 266 * Unregister AMR codec factory from pjmedia endpoint and deinitialize 267 * the AMR codec library. 268 */ 269 static pj_status_t amr_deinit(void) 223 270 { 224 271 pjmedia_codec_mgr *codec_mgr; 225 272 pj_status_t status; 226 273 274 if (amr_codec_factory.init[IDX_AMR_NB] || 275 amr_codec_factory.init[IDX_AMR_WB]) 276 { 277 return PJ_SUCCESS; 278 } 279 227 280 if (amr_codec_factory.pool == NULL) 228 281 return PJ_SUCCESS; … … 236 289 } 237 290 238 /* Unregister AMR -NBcodec factory. */291 /* Unregister AMR codec factory. */ 239 292 status = pjmedia_codec_mgr_unregister_factory(codec_mgr, 240 293 &amr_codec_factory.base); … … 247 300 } 248 301 249 250 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_set_config( 251 const pjmedia_codec_amrnb_config *config) 302 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_deinit(void) 303 { 304 amr_codec_factory.init[IDX_AMR_NB] = PJ_FALSE; 305 306 return amr_deinit(); 307 } 308 309 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrwb_deinit(void) 310 { 311 amr_codec_factory.init[IDX_AMR_WB] = PJ_FALSE; 312 313 return amr_deinit(); 314 } 315 316 static pj_status_t 317 amr_set_config(unsigned idx, const pjmedia_codec_amr_config *config) 252 318 { 253 319 unsigned nbitrates; 254 320 255 256 def_config = *config; 321 def_config[idx] = *config; 257 322 258 323 /* Normalize bitrate. */ 259 nbitrates = PJ_ARRAY_SIZE( pjmedia_codec_amrnb_bitrates);260 if (def_config .bitrate < pjmedia_codec_amrnb_bitrates[0])261 def_config .bitrate = pjmedia_codec_amrnb_bitrates[0];262 else if (def_config.bitrate > pjmedia_codec_amrnb_bitrates[nbitrates-1])263 def_config .bitrate = pjmedia_codec_amrnb_bitrates[nbitrates-1];264 else324 nbitrates = PJ_ARRAY_SIZE(amr_bitrates[idx]); 325 if (def_config[idx].bitrate < amr_bitrates[idx][0]) { 326 def_config[idx].bitrate = amr_bitrates[idx][0]; 327 } else if (def_config[idx].bitrate > amr_bitrates[idx][nbitrates-1]) { 328 def_config[idx].bitrate = amr_bitrates[idx][nbitrates-1]; 329 } else 265 330 { 266 331 unsigned i; 267 332 268 333 for (i = 0; i < nbitrates; ++i) { 269 if (def_config .bitrate <= pjmedia_codec_amrnb_bitrates[i])334 if (def_config[idx].bitrate <= amr_bitrates[idx][i]) 270 335 break; 271 336 } 272 def_config.bitrate = pjmedia_codec_amrnb_bitrates[i]; 273 } 274 275 return PJ_SUCCESS; 276 } 277 278 /* 279 * Check if factory can allocate the specified codec. 337 def_config[idx].bitrate = amr_bitrates[idx][i]; 338 } 339 340 return PJ_SUCCESS; 341 } 342 343 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_set_config( 344 const pjmedia_codec_amrnb_config *config) 345 { 346 return amr_set_config(IDX_AMR_NB, (const pjmedia_codec_amr_config *)config); 347 } 348 349 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrwb_set_config( 350 const pjmedia_codec_amrwb_config *config) 351 { 352 return amr_set_config(IDX_AMR_WB, (const pjmedia_codec_amr_config *)config); 353 } 354 355 /* 356 * Check if factory can allocate the specified codec. 280 357 */ 281 358 static pj_status_t amr_test_alloc( pjmedia_codec_factory *factory, 282 359 const pjmedia_codec_info *info ) 283 360 { 361 const pj_str_t amr_tag = { "AMR", 3}; 362 const pj_str_t amrwb_tag = { "AMR-WB", 6}; 284 363 PJ_UNUSED_ARG(factory); 285 364 365 /* Type MUST be audio. */ 366 if (info->type != PJMEDIA_TYPE_AUDIO) 367 return PJMEDIA_CODEC_EUNSUP; 368 286 369 /* Check payload type. */ 287 if (info->pt != PJMEDIA_RTP_PT_AMR )370 if (info->pt != PJMEDIA_RTP_PT_AMR && info->pt != PJMEDIA_RTP_PT_AMRWB) 288 371 return PJMEDIA_CODEC_EUNSUP; 289 290 /* Ignore the rest, since it's static payload type. */ 291 292 return PJ_SUCCESS; 372 373 /* Check encoding name. */ 374 if (pj_stricmp(&info->encoding_name, &amr_tag) != 0 && 375 pj_stricmp(&info->encoding_name, &amrwb_tag) != 0) 376 { 377 return PJMEDIA_CODEC_EUNSUP; 378 } 379 380 /* Check clock-rate */ 381 if ((info->clock_rate == 8000 && amr_codec_factory.init[IDX_AMR_NB]) || 382 (info->clock_rate == 16000 && amr_codec_factory.init[IDX_AMR_WB])) 383 { 384 return PJ_SUCCESS; 385 } 386 387 /* Unsupported or disabled. */ 388 return PJMEDIA_CODEC_EUNSUP; 293 389 } 294 390 … … 300 396 pjmedia_codec_param *attr ) 301 397 { 398 unsigned idx; 399 302 400 PJ_UNUSED_ARG(factory); 303 PJ_UNUSED_ARG(id); 304 401 402 idx = (id->clock_rate <= 8000? IDX_AMR_NB: IDX_AMR_WB); 305 403 pj_bzero(attr, sizeof(pjmedia_codec_param)); 306 attr->info.clock_rate = 8000;404 attr->info.clock_rate = (id->clock_rate <= 8000? 8000: 16000); 307 405 attr->info.channel_cnt = 1; 308 attr->info.avg_bps = def_config .bitrate;309 attr->info.max_bps = pjmedia_codec_amrnb_bitrates[7];406 attr->info.avg_bps = def_config[idx].bitrate; 407 attr->info.max_bps = amr_bitrates[idx][PJ_ARRAY_SIZE(amr_bitrates[idx])-1]; 310 408 attr->info.pcm_bits_per_sample = 16; 311 409 attr->info.frm_ptime = 20; 312 attr->info.pt = PJMEDIA_RTP_PT_AMR;410 attr->info.pt = (pj_uint8_t)id->pt; 313 411 314 412 attr->setting.frm_per_pkt = 2; … … 316 414 attr->setting.plc = 1; 317 415 318 if (def_config .octet_align) {416 if (def_config[idx].octet_align) { 319 417 attr->setting.dec_fmtp.cnt = 1; 320 418 attr->setting.dec_fmtp.param[0].name = pj_str("octet-align"); … … 329 427 330 428 /* 331 * Enum codecs supported by this factory (i.e. only AMR-NB!).429 * Enum codecs supported by this factory (i.e. AMR-NB and AMR-WB). 332 430 */ 333 431 static pj_status_t amr_enum_codecs( pjmedia_codec_factory *factory, … … 338 436 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL); 339 437 340 pj_bzero(&codecs[0], sizeof(pjmedia_codec_info)); 341 codecs[0].encoding_name = pj_str("AMR"); 342 codecs[0].pt = PJMEDIA_RTP_PT_AMR; 343 codecs[0].type = PJMEDIA_TYPE_AUDIO; 344 codecs[0].clock_rate = 8000; 345 codecs[0].channel_cnt = 1; 346 347 *count = 1; 348 349 return PJ_SUCCESS; 350 } 351 352 353 /* 354 * Allocate a new AMR-NB codec instance. 438 *count = 0; 439 440 if (amr_codec_factory.init[IDX_AMR_NB]) { 441 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info)); 442 codecs[*count].encoding_name = pj_str("AMR"); 443 codecs[*count].pt = PJMEDIA_RTP_PT_AMR; 444 codecs[*count].type = PJMEDIA_TYPE_AUDIO; 445 codecs[*count].clock_rate = 8000; 446 codecs[*count].channel_cnt = 1; 447 (*count)++; 448 } 449 450 if (amr_codec_factory.init[IDX_AMR_NB]) { 451 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info)); 452 codecs[*count].encoding_name = pj_str("AMR-WB"); 453 codecs[*count].pt = PJMEDIA_RTP_PT_AMRWB; 454 codecs[*count].type = PJMEDIA_TYPE_AUDIO; 455 codecs[*count].clock_rate = 16000; 456 codecs[*count].channel_cnt = 1; 457 (*count)++; 458 } 459 460 return PJ_SUCCESS; 461 } 462 463 464 /* 465 * Allocate a new AMR codec instance. 355 466 */ 356 467 static pj_status_t amr_alloc_codec( pjmedia_codec_factory *factory, … … 366 477 PJ_ASSERT_RETURN(factory == &amr_codec_factory.base, PJ_EINVAL); 367 478 368 pool = pjmedia_endpt_create_pool(amr_codec_factory.endpt, "amr nb-inst",479 pool = pjmedia_endpt_create_pool(amr_codec_factory.endpt, "amr-inst", 369 480 512, 512); 370 481 … … 380 491 #if USE_PJMEDIA_PLC 381 492 /* Create PLC */ 382 status = pjmedia_plc_create(pool, 8000, 160, 0, &amr_data->plc); 493 status = pjmedia_plc_create(pool, id->clock_rate, 494 id->clock_rate * FRAME_LENGTH_MS / 1000, 0, 495 &amr_data->plc); 383 496 if (status != PJ_SUCCESS) { 384 497 return status; … … 438 551 pj_int8_t enc_mode; 439 552 const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11}; 553 unsigned idx; 440 554 441 555 PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL); 442 556 PJ_ASSERT_RETURN(amr_data != NULL, PJ_EINVALIDOP); 443 557 558 idx = (attr->info.clock_rate <= 8000? IDX_AMR_NB: IDX_AMR_WB); 444 559 enc_mode = pjmedia_codec_amr_get_mode(attr->info.avg_bps); 445 pj_assert(enc_mode >= 0 && enc_mode <= 7); 560 pj_assert(enc_mode >= 0 && 561 enc_mode < PJ_ARRAY_SIZE(amr_bitrates[idx])); 446 562 447 563 /* Check octet-align */ … … 477 593 l = pj_strlen(&attr->setting.enc_fmtp.param[i].val); 478 594 while (l--) { 479 if (*p>='0' && *p<='7') { 595 if (*p>='0' && 596 *p<=('0'+PJ_ARRAY_SIZE(amr_bitrates[idx])-1)) 597 { 480 598 pj_int8_t tmp = *p - '0' - enc_mode; 481 599 … … 497 615 } 498 616 617 amr_data->clock_rate = attr->info.clock_rate; 499 618 amr_data->vad_enabled = (attr->setting.vad != 0); 500 619 amr_data->plc_enabled = (attr->setting.plc != 0); 501 620 amr_data->enc_mode = enc_mode; 502 621 503 amr_data->encoder = Encoder_Interface_init(amr_data->vad_enabled); 622 if (idx == IDX_AMR_NB) { 623 #ifdef USE_AMRNB 624 amr_data->encoder = Encoder_Interface_init(amr_data->vad_enabled); 625 #endif 626 } else { 627 #ifdef USE_AMRWB 628 amr_data->encoder = E_IF_init(); 629 #endif 630 } 504 631 if (amr_data->encoder == NULL) { 505 TRACE_((THIS_FILE, "Encoder _Interface_init()failed"));632 TRACE_((THIS_FILE, "Encoder initialization failed")); 506 633 amr_codec_close(codec); 507 634 return PJMEDIA_CODEC_EFAILED; … … 509 636 setting = &amr_data->enc_setting; 510 637 pj_bzero(setting, sizeof(pjmedia_codec_amr_pack_setting)); 511 setting->amr_nb = 1;638 setting->amr_nb = (idx == IDX_AMR_NB? 1: 0); 512 639 setting->reorder = 0; 513 640 setting->octet_aligned = octet_align; 514 641 setting->cmr = 15; 515 642 516 amr_data->decoder = Decoder_Interface_init(); 643 if (idx == IDX_AMR_NB) { 644 #ifdef USE_AMRNB 645 amr_data->decoder = Decoder_Interface_init(); 646 #endif 647 } else { 648 #ifdef USE_AMRWB 649 amr_data->decoder = D_IF_init(); 650 #endif 651 } 517 652 if (amr_data->decoder == NULL) { 518 TRACE_((THIS_FILE, "Decoder _Interface_init()failed"));653 TRACE_((THIS_FILE, "Decoder initialization failed")); 519 654 amr_codec_close(codec); 520 655 return PJMEDIA_CODEC_EFAILED; … … 522 657 setting = &amr_data->dec_setting; 523 658 pj_bzero(setting, sizeof(pjmedia_codec_amr_pack_setting)); 524 setting->amr_nb = 1;659 setting->amr_nb = (idx == IDX_AMR_NB? 1: 0); 525 660 setting->reorder = 0; 526 661 setting->octet_aligned = octet_align; 527 662 528 TRACE_((THIS_FILE, "AMR-NB codec allocated: vad=%d, plc=%d, bitrate=%d", 663 TRACE_((THIS_FILE, "AMR codec allocated: clockrate=%d vad=%d, plc=%d," 664 " bitrate=%d", amr_data->clock_rate, 529 665 amr_data->vad_enabled, amr_data->plc_enabled, 530 pjmedia_codec_amrnb_bitrates[amr_data->enc_mode]));666 amr_bitrates[idx][amr_data->enc_mode])); 531 667 return PJ_SUCCESS; 532 668 } … … 546 682 547 683 if (amr_data->encoder) { 548 Encoder_Interface_exit(amr_data->encoder); 684 if (amr_data->enc_setting.amr_nb) { 685 #ifdef USE_AMRNB 686 Encoder_Interface_exit(amr_data->encoder); 687 #endif 688 } else { 689 #ifdef USE_AMRWB 690 E_IF_exit(amr_data->encoder); 691 #endif 692 } 549 693 amr_data->encoder = NULL; 550 694 } 551 695 552 696 if (amr_data->decoder) { 553 Decoder_Interface_exit(amr_data->decoder); 697 if (amr_data->dec_setting.amr_nb) { 698 #ifdef USE_AMRNB 699 Decoder_Interface_exit(amr_data->decoder); 700 #endif 701 } else { 702 #ifdef USE_AMRWB 703 D_IF_exit(amr_data->decoder); 704 #endif 705 } 554 706 amr_data->decoder = NULL; 555 707 } 556 708 557 TRACE_((THIS_FILE, "AMR -NBcodec closed"));709 TRACE_((THIS_FILE, "AMR codec closed")); 558 710 return PJ_SUCCESS; 559 711 } … … 576 728 amr_data->plc_enabled = (attr->setting.plc != 0); 577 729 578 if (prev_vad_state != amr_data->vad_enabled) { 730 if (amr_data->enc_setting.amr_nb && 731 prev_vad_state != amr_data->vad_enabled) 732 { 579 733 /* Reinit AMR encoder to update VAD setting */ 580 734 TRACE_((THIS_FILE, "Reiniting AMR encoder to update VAD setting.")); 735 #ifdef USE_AMRNB 581 736 Encoder_Interface_exit(amr_data->encoder); 582 737 amr_data->encoder = Encoder_Interface_init(amr_data->vad_enabled); 738 #endif 583 739 if (amr_data->encoder == NULL) { 584 740 TRACE_((THIS_FILE, "Encoder_Interface_init() failed")); … … 588 744 } 589 745 590 TRACE_((THIS_FILE, "AMR -NBcodec modified: vad=%d, plc=%d",746 TRACE_((THIS_FILE, "AMR codec modified: vad=%d, plc=%d", 591 747 amr_data->vad_enabled, amr_data->plc_enabled)); 592 748 return PJ_SUCCESS; … … 607 763 pj_uint8_t cmr; 608 764 pj_status_t status; 765 unsigned idx = (amr_data->enc_setting.amr_nb? 0: 1); 609 766 610 767 status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, &amr_data->dec_setting, … … 614 771 615 772 /* Check for Change Mode Request. */ 616 if (cmr < = 7&& amr_data->enc_mode != cmr) {773 if (cmr < PJ_ARRAY_SIZE(amr_bitrates[idx]) && amr_data->enc_mode != cmr) { 617 774 amr_data->enc_mode = cmr; 618 TRACE_((THIS_FILE, "AMR -NBencoder switched mode to %d (%dbps)",619 620 pjmedia_codec_amrnb_bitrates[amr_data->enc_mode]));775 TRACE_((THIS_FILE, "AMR encoder switched mode to %d (%dbps)", 776 amr_data->enc_mode, 777 amr_bitrates[idx][amr_data->enc_mode])); 621 778 } 622 779 … … 650 807 651 808 nsamples = input->size >> 1; 652 samples_per_frame = 160;809 samples_per_frame = amr_data->clock_rate * FRAME_LENGTH_MS / 1000; 653 810 PJ_ASSERT_RETURN(nsamples % samples_per_frame == 0, 654 811 PJMEDIA_CODEC_EPCMFRMINLEN); … … 662 819 bitstream = (unsigned char*)output->buf; 663 820 while (nsamples >= samples_per_frame) { 664 size = Encoder_Interface_Encode (amr_data->encoder, amr_data->enc_mode, 665 speech, bitstream, 0); 821 if (amr_data->enc_setting.amr_nb) { 822 #ifdef USE_AMRNB 823 size = Encoder_Interface_Encode (amr_data->encoder, 824 amr_data->enc_mode, 825 speech, bitstream, 0); 826 #endif 827 } else { 828 #ifdef USE_AMRWB 829 size = E_IF_encode (amr_data->encoder, amr_data->enc_mode, 830 speech, bitstream, 0); 831 #endif 832 } 666 833 if (size == 0) { 667 834 output->size = 0; 668 835 output->buf = NULL; 669 836 output->type = PJMEDIA_FRAME_TYPE_NONE; 670 TRACE_((THIS_FILE, "AMR -NBencode() failed"));837 TRACE_((THIS_FILE, "AMR encode() failed")); 671 838 return PJMEDIA_CODEC_EFAILED; 672 839 } 673 nsamples -= 160;840 nsamples -= samples_per_frame; 674 841 speech += samples_per_frame; 675 842 bitstream += size; 676 843 out_size += size; 677 TRACE_((THIS_FILE, "AMR -NBencode(): mode=%d, size=%d",844 TRACE_((THIS_FILE, "AMR encode(): mode=%d, size=%d", 678 845 amr_data->enc_mode, out_size)); 679 846 } … … 691 858 info->start_bit = 0; 692 859 frames[i].buf = p + 1; 693 frames[i].size = (info->frame_type <= 8)? 694 pjmedia_codec_amrnb_framelen[info->frame_type] : 0; 860 if (amr_data->enc_setting.amr_nb) { 861 frames[i].size = (info->frame_type <= 8)? 862 pjmedia_codec_amrnb_framelen[info->frame_type] : 0; 863 } else { 864 frames[i].size = (info->frame_type <= 9)? 865 pjmedia_codec_amrwb_framelen[info->frame_type] : 0; 866 } 695 867 p += frames[i].size + 1; 696 868 … … 714 886 &input->timestamp); 715 887 if (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 || 716 dtx_duration < PJMEDIA_CODEC_MAX_SILENCE_PERIOD*8000/1000) 888 dtx_duration < PJMEDIA_CODEC_MAX_SILENCE_PERIOD* 889 amr_data->clock_rate/1000) 717 890 { 718 891 output->size = 0; … … 756 929 pjmedia_frame input_; 757 930 pjmedia_codec_amr_bit_info *info; 758 /* VA AMR-NB decoding buffer: AMR-NB max frame size + 1 byte header. */ 759 unsigned char bitstream[32]; 931 unsigned out_size; 932 /* AMR decoding buffer: AMR max frame size + 1 byte header. */ 933 unsigned char bitstream[61]; 760 934 761 935 pj_assert(amr_data != NULL); 762 936 PJ_ASSERT_RETURN(input && output, PJ_EINVAL); 763 937 764 if (output_buf_len < 320) 938 out_size = amr_data->clock_rate * FRAME_LENGTH_MS / 1000 * 2; 939 if (output_buf_len < out_size) 765 940 return PJMEDIA_CODEC_EPCMTOOSHORT; 766 941 767 942 input_.buf = &bitstream[1]; 768 input_.size = 31; /* AMR-NB max frame size */ 943 /* AMR max frame size */ 944 input_.size = (amr_data->dec_setting.amr_nb? 31: 60); 769 945 pjmedia_codec_amr_predecode(input, &amr_data->dec_setting, &input_); 770 946 info = (pjmedia_codec_amr_bit_info*)&input_.bit_info; 771 947 772 /* VA AMR NBdecoder requires frame info in the first byte. */948 /* VA AMR decoder requires frame info in the first byte. */ 773 949 bitstream[0] = (info->frame_type << 3) | (info->good_quality << 2); 774 950 775 TRACE_((THIS_FILE, "AMR -NBdecode(): mode=%d, ft=%d, size=%d",951 TRACE_((THIS_FILE, "AMR decode(): mode=%d, ft=%d, size=%d", 776 952 info->mode, info->frame_type, input_.size)); 777 953 778 954 /* Decode */ 779 Decoder_Interface_Decode(amr_data->decoder, bitstream, 780 (pj_int16_t*)output->buf, 0); 781 782 output->size = 320; 955 if (amr_data->dec_setting.amr_nb) { 956 #ifdef USE_AMRNB 957 Decoder_Interface_Decode(amr_data->decoder, bitstream, 958 (pj_int16_t*)output->buf, 0); 959 #endif 960 } else { 961 #ifdef USE_AMRWB 962 D_IF_decode(amr_data->decoder, bitstream, 963 (pj_int16_t*)output->buf, 0); 964 #endif 965 } 966 967 output->size = out_size; 783 968 output->type = PJMEDIA_FRAME_TYPE_AUDIO; 784 969 output->timestamp = input->timestamp; … … 805 990 { 806 991 struct amr_data *amr_data = codec->codec_data; 992 unsigned out_size = amr_data->clock_rate * FRAME_LENGTH_MS / 1000 * 2; 807 993 808 994 TRACE_((THIS_FILE, "amr_codec_recover")); … … 810 996 PJ_ASSERT_RETURN(amr_data->plc_enabled, PJ_EINVALIDOP); 811 997 812 PJ_ASSERT_RETURN(output_buf_len >= 320, PJMEDIA_CODEC_EPCMTOOSHORT);998 PJ_ASSERT_RETURN(output_buf_len >= out_size, PJMEDIA_CODEC_EPCMTOOSHORT); 813 999 814 1000 pjmedia_plc_generate(amr_data->plc, (pj_int16_t*)output->buf); 815 1001 816 output->size = 320;1002 output->size = out_size; 817 1003 output->type = PJMEDIA_FRAME_TYPE_AUDIO; 818 1004 … … 822 1008 823 1009 #if defined(_MSC_VER) && PJMEDIA_AUTO_LINK_OPENCORE_AMR_LIBS 824 # if PJMEDIA_OPENCORE_AMR_BUILT_WITH_GCC 825 # pragma comment( lib, "libopencore-amrnb.a") 826 # else 827 # error Unsupported OpenCORE AMR library, fix here 828 # endif 829 #endif 830 831 #endif 1010 # if PJMEDIA_OPENCORE_AMR_BUILT_WITH_GCC 1011 # ifdef USE_AMRNB 1012 # pragma comment( lib, "libopencore-amrnb.a") 1013 # endif 1014 # ifdef USE_AMRWB 1015 # pragma comment( lib, "libopencore-amrwb.a") 1016 # pragma comment( lib, "libvo-amrwbenc.a") 1017 # endif 1018 # else 1019 # error Unsupported OpenCORE AMR library, fix here 1020 # endif 1021 #endif 1022 1023 #endif
Note: See TracChangeset
for help on using the changeset viewer.