00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00066 #include "avcodec.h"
00067
00068 #ifdef CONFIG_LIBAMR_NB_FIXED
00069
00070 #define MMS_IO
00071
00072 #include "amr/sp_dec.h"
00073 #include "amr/d_homing.h"
00074 #include "amr/typedef.h"
00075 #include "amr/sp_enc.h"
00076 #include "amr/sid_sync.h"
00077 #include "amr/e_homing.h"
00078
00079 #else
00080 #include <amrnb/interf_dec.h>
00081 #include <amrnb/interf_enc.h>
00082 #endif
00083
00084 static const char *nb_bitrate_unsupported =
00085 "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n";
00086 static const char *wb_bitrate_unsupported =
00087 "bitrate not supported: use one of 6.6k, 8.85k, 12.65k, 14.25k, 15.85k, 18.25k, 19.85k, 23.05k, or 23.85k\n";
00088
00089
00090 typedef struct AMR_bitrates
00091 {
00092 int rate;
00093 enum Mode mode;
00094 } AMR_bitrates;
00095
00096
00097 static int getBitrateMode(int bitrate)
00098 {
00099
00100 AMR_bitrates rates[]={ {4750,MR475},
00101 {5150,MR515},
00102 {5900,MR59},
00103 {6700,MR67},
00104 {7400,MR74},
00105 {7950,MR795},
00106 {10200,MR102},
00107 {12200,MR122},
00108 };
00109 int i;
00110
00111 for(i=0;i<8;i++)
00112 {
00113 if(rates[i].rate==bitrate)
00114 {
00115 return(rates[i].mode);
00116 }
00117 }
00118
00119 return -1;
00120 }
00121
00122 static void amr_decode_fix_avctx(AVCodecContext * avctx)
00123 {
00124 const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
00125
00126 if(avctx->sample_rate == 0)
00127 {
00128 avctx->sample_rate = 8000 * is_amr_wb;
00129 }
00130
00131 if(avctx->channels == 0)
00132 {
00133 avctx->channels = 1;
00134 }
00135
00136 avctx->frame_size = 160 * is_amr_wb;
00137 }
00138
00139 #ifdef CONFIG_LIBAMR_NB_FIXED
00140
00141
00142 #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
00143
00144 typedef struct AMRContext {
00145 int frameCount;
00146 Speech_Decode_FrameState *speech_decoder_state;
00147 enum RXFrameType rx_type;
00148 enum Mode mode;
00149 Word16 reset_flag;
00150 Word16 reset_flag_old;
00151
00152 int enc_bitrate;
00153 Speech_Encode_FrameState *enstate;
00154 sid_syncState *sidstate;
00155 enum TXFrameType tx_frametype;
00156 } AMRContext;
00157
00158 static int amr_nb_decode_init(AVCodecContext * avctx)
00159 {
00160 AMRContext *s = avctx->priv_data;
00161
00162 s->frameCount=0;
00163 s->speech_decoder_state=NULL;
00164 s->rx_type = (enum RXFrameType)0;
00165 s->mode= (enum Mode)0;
00166 s->reset_flag=0;
00167 s->reset_flag_old=1;
00168
00169 if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
00170 {
00171 av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n");
00172 return -1;
00173 }
00174
00175 amr_decode_fix_avctx(avctx);
00176
00177 if(avctx->channels > 1)
00178 {
00179 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
00180 return -1;
00181 }
00182
00183 return 0;
00184 }
00185
00186 static int amr_nb_encode_init(AVCodecContext * avctx)
00187 {
00188 AMRContext *s = avctx->priv_data;
00189
00190 s->frameCount=0;
00191 s->speech_decoder_state=NULL;
00192 s->rx_type = (enum RXFrameType)0;
00193 s->mode= (enum Mode)0;
00194 s->reset_flag=0;
00195 s->reset_flag_old=1;
00196
00197 if(avctx->sample_rate!=8000)
00198 {
00199 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
00200 return -1;
00201 }
00202
00203 if(avctx->channels!=1)
00204 {
00205 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
00206 return -1;
00207 }
00208
00209 avctx->frame_size=160;
00210 avctx->coded_frame= avcodec_alloc_frame();
00211
00212 if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
00213 {
00214 av_log(avctx, AV_LOG_ERROR, "Speech_Encode_Frame_init error\n");
00215 return -1;
00216 }
00217
00218 if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0)
00219 {
00220 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
00221 return -1;
00222 }
00223
00224 return 0;
00225 }
00226
00227 static int amr_nb_encode_close(AVCodecContext * avctx)
00228 {
00229 AMRContext *s = avctx->priv_data;
00230
00231 Speech_Encode_Frame_exit(&s->enstate);
00232 sid_sync_exit (&s->sidstate);
00233 av_freep(&avctx->coded_frame);
00234 return 0;
00235 }
00236
00237 static int amr_nb_decode_close(AVCodecContext * avctx)
00238 {
00239 AMRContext *s = avctx->priv_data;
00240
00241 Speech_Decode_Frame_exit(&s->speech_decoder_state);
00242 return 0;
00243 }
00244
00245 static int amr_nb_decode_frame(AVCodecContext * avctx,
00246 void *data, int *data_size,
00247 uint8_t * buf, int buf_size)
00248 {
00249 AMRContext *s = avctx->priv_data;
00250 uint8_t*amrData=buf;
00251 int offset=0;
00252 UWord8 toc, q, ft;
00253 Word16 serial[SERIAL_FRAMESIZE];
00254 Word16 *synth;
00255 UWord8 *packed_bits;
00256 static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
00257 int i;
00258
00259
00260
00261 synth=data;
00262
00263 toc=amrData[offset];
00264
00265 q = (toc >> 2) & 0x01;
00266 ft = (toc >> 3) & 0x0F;
00267
00268
00269
00270 offset++;
00271
00272 packed_bits=amrData+offset;
00273
00274 offset+=packed_size[ft];
00275
00276
00277 s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
00278
00279
00280 s->frameCount++;
00281
00282 if (s->rx_type == RX_NO_DATA)
00283 {
00284 s->mode = s->speech_decoder_state->prev_mode;
00285 }
00286 else {
00287 s->speech_decoder_state->prev_mode = s->mode;
00288 }
00289
00290
00291 if (s->reset_flag_old == 1)
00292 {
00293
00294 s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
00295 }
00296
00297 if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
00298 {
00299 for (i = 0; i < L_FRAME; i++)
00300 {
00301 synth[i] = EHF_MASK;
00302 }
00303 }
00304 else
00305 {
00306
00307 Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
00308 }
00309
00310
00311 *data_size=160*2;
00312
00313
00314 if (s->reset_flag_old == 0)
00315 {
00316
00317 s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
00318 }
00319
00320 if (s->reset_flag != 0)
00321 {
00322 Speech_Decode_Frame_reset(s->speech_decoder_state);
00323 }
00324 s->reset_flag_old = s->reset_flag;
00325
00326 return offset;
00327 }
00328
00329
00330 static int amr_nb_encode_frame(AVCodecContext *avctx,
00331 unsigned char *frame, int buf_size, void *data)
00332 {
00333 short serial_data[250] = {0};
00334 AMRContext *s = avctx->priv_data;
00335 int written;
00336
00337 s->reset_flag = encoder_homing_frame_test(data);
00338
00339 Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode);
00340
00341
00342 sid_sync (s->sidstate, s->mode, &s->tx_frametype);
00343
00344 written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
00345
00346 if (s->reset_flag != 0)
00347 {
00348 Speech_Encode_Frame_reset(s->enstate);
00349 sid_sync_reset(s->sidstate);
00350 }
00351 return written;
00352 }
00353
00354
00355 #elif defined(CONFIG_LIBAMR_NB)
00356
00357 typedef struct AMRContext {
00358 int frameCount;
00359 void * decState;
00360 int *enstate;
00361 int enc_bitrate;
00362 } AMRContext;
00363
00364 static int amr_nb_decode_init(AVCodecContext * avctx)
00365 {
00366 AMRContext *s = avctx->priv_data;
00367
00368 s->frameCount=0;
00369 s->decState=Decoder_Interface_init();
00370 if(!s->decState)
00371 {
00372 av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
00373 return -1;
00374 }
00375
00376 amr_decode_fix_avctx(avctx);
00377
00378 if(avctx->channels > 1)
00379 {
00380 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
00381 return -1;
00382 }
00383
00384 return 0;
00385 }
00386
00387 static int amr_nb_encode_init(AVCodecContext * avctx)
00388 {
00389 AMRContext *s = avctx->priv_data;
00390
00391 s->frameCount=0;
00392
00393 if(avctx->sample_rate!=8000)
00394 {
00395 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
00396 return -1;
00397 }
00398
00399 if(avctx->channels!=1)
00400 {
00401 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
00402 return -1;
00403 }
00404
00405 avctx->frame_size=160;
00406 avctx->coded_frame= avcodec_alloc_frame();
00407
00408 s->enstate=Encoder_Interface_init(0);
00409 if(!s->enstate)
00410 {
00411 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
00412 return -1;
00413 }
00414
00415 if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0)
00416 {
00417 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
00418 return -1;
00419 }
00420
00421 return 0;
00422 }
00423
00424 static int amr_nb_decode_close(AVCodecContext * avctx)
00425 {
00426 AMRContext *s = avctx->priv_data;
00427
00428 Decoder_Interface_exit(s->decState);
00429 return 0;
00430 }
00431
00432 static int amr_nb_encode_close(AVCodecContext * avctx)
00433 {
00434 AMRContext *s = avctx->priv_data;
00435
00436 Encoder_Interface_exit(s->enstate);
00437 av_freep(&avctx->coded_frame);
00438 return 0;
00439 }
00440
00441 static int amr_nb_decode_frame(AVCodecContext * avctx,
00442 void *data, int *data_size,
00443 uint8_t * buf, int buf_size)
00444 {
00445 AMRContext *s = avctx->priv_data;
00446 uint8_t*amrData=buf;
00447 static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
00448 enum Mode dec_mode;
00449 int packet_size;
00450
00451
00452
00453 dec_mode = (buf[0] >> 3) & 0x000F;
00454 packet_size = block_size[dec_mode]+1;
00455
00456 if(packet_size > buf_size) {
00457 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size);
00458 return -1;
00459 }
00460
00461 s->frameCount++;
00462
00463
00464 Decoder_Interface_Decode(s->decState, amrData, data, 0);
00465 *data_size=160*2;
00466
00467 return packet_size;
00468 }
00469
00470 static int amr_nb_encode_frame(AVCodecContext *avctx,
00471 unsigned char *frame, int buf_size, void *data)
00472 {
00473 AMRContext *s = avctx->priv_data;
00474 int written;
00475
00476 if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0)
00477 {
00478 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
00479 return -1;
00480 }
00481
00482 written = Encoder_Interface_Encode(s->enstate,
00483 s->enc_bitrate,
00484 data,
00485 frame,
00486 0);
00487
00488
00489 return written;
00490 }
00491
00492 #endif
00493
00494 #if defined(CONFIG_LIBAMR_NB) || defined(CONFIG_LIBAMR_NB_FIXED)
00495
00496 AVCodec libamr_nb_decoder =
00497 {
00498 "libamr_nb",
00499 CODEC_TYPE_AUDIO,
00500 CODEC_ID_AMR_NB,
00501 sizeof(AMRContext),
00502 amr_nb_decode_init,
00503 NULL,
00504 amr_nb_decode_close,
00505 amr_nb_decode_frame,
00506 };
00507
00508 AVCodec libamr_nb_encoder =
00509 {
00510 "libamr_nb",
00511 CODEC_TYPE_AUDIO,
00512 CODEC_ID_AMR_NB,
00513 sizeof(AMRContext),
00514 amr_nb_encode_init,
00515 amr_nb_encode_frame,
00516 amr_nb_encode_close,
00517 NULL,
00518 };
00519
00520 #endif
00521
00522
00523 #ifdef CONFIG_LIBAMR_WB
00524
00525 #ifdef _TYPEDEF_H
00526
00527 #define typedef_h
00528 #endif
00529
00530 #include <amrwb/enc_if.h>
00531 #include <amrwb/dec_if.h>
00532 #include <amrwb/if_rom.h>
00533
00534
00535 typedef struct AMRWB_bitrates
00536 {
00537 int rate;
00538 int mode;
00539 } AMRWB_bitrates;
00540
00541 static int getWBBitrateMode(int bitrate)
00542 {
00543
00544 AMRWB_bitrates rates[]={ {6600,0},
00545 {8850,1},
00546 {12650,2},
00547 {14250,3},
00548 {15850,4},
00549 {18250,5},
00550 {19850,6},
00551 {23050,7},
00552 {23850,8},
00553 };
00554 int i;
00555
00556 for(i=0;i<9;i++)
00557 {
00558 if(rates[i].rate==bitrate)
00559 {
00560 return(rates[i].mode);
00561 }
00562 }
00563
00564 return -1;
00565 }
00566
00567
00568 typedef struct AMRWBContext {
00569 int frameCount;
00570 void *state;
00571 int mode;
00572 Word16 allow_dtx;
00573 } AMRWBContext;
00574
00575 static int amr_wb_encode_init(AVCodecContext * avctx)
00576 {
00577 AMRWBContext *s = avctx->priv_data;
00578
00579 s->frameCount=0;
00580
00581 if(avctx->sample_rate!=16000)
00582 {
00583 av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
00584 return -1;
00585 }
00586
00587 if(avctx->channels!=1)
00588 {
00589 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
00590 return -1;
00591 }
00592
00593 if((s->mode=getWBBitrateMode(avctx->bit_rate))<0)
00594 {
00595 av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
00596 return -1;
00597 }
00598
00599 avctx->frame_size=320;
00600 avctx->coded_frame= avcodec_alloc_frame();
00601
00602 s->state = E_IF_init();
00603 s->allow_dtx=0;
00604
00605 return 0;
00606 }
00607
00608 static int amr_wb_encode_close(AVCodecContext * avctx)
00609 {
00610 AMRWBContext *s = avctx->priv_data;
00611
00612 E_IF_exit(s->state);
00613 av_freep(&avctx->coded_frame);
00614 s->frameCount++;
00615 return 0;
00616 }
00617
00618 static int amr_wb_encode_frame(AVCodecContext *avctx,
00619 unsigned char *frame, int buf_size, void *data)
00620 {
00621 AMRWBContext *s = avctx->priv_data;
00622 int size;
00623
00624 if((s->mode=getWBBitrateMode(avctx->bit_rate))<0)
00625 {
00626 av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
00627 return -1;
00628 }
00629 size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
00630 return size;
00631 }
00632
00633 static int amr_wb_decode_init(AVCodecContext * avctx)
00634 {
00635 AMRWBContext *s = avctx->priv_data;
00636
00637 s->frameCount=0;
00638 s->state = D_IF_init();
00639
00640 amr_decode_fix_avctx(avctx);
00641
00642 if(avctx->channels > 1)
00643 {
00644 av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
00645 return -1;
00646 }
00647
00648 return 0;
00649 }
00650
00651 static int amr_wb_decode_frame(AVCodecContext * avctx,
00652 void *data, int *data_size,
00653 uint8_t * buf, int buf_size)
00654 {
00655 AMRWBContext *s = avctx->priv_data;
00656 uint8_t*amrData=buf;
00657 int mode;
00658 int packet_size;
00659
00660 if(buf_size==0) {
00661
00662 return 0;
00663 }
00664
00665 mode = (amrData[0] >> 3) & 0x000F;
00666 packet_size = block_size[mode];
00667
00668 if(packet_size > buf_size) {
00669 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1);
00670 return -1;
00671 }
00672
00673 s->frameCount++;
00674 D_IF_decode( s->state, amrData, data, _good_frame);
00675 *data_size=320*2;
00676 return packet_size;
00677 }
00678
00679 static int amr_wb_decode_close(AVCodecContext * avctx)
00680 {
00681 AMRWBContext *s = avctx->priv_data;
00682
00683 D_IF_exit(s->state);
00684 return 0;
00685 }
00686
00687 AVCodec libamr_wb_decoder =
00688 {
00689 "libamr_wb",
00690 CODEC_TYPE_AUDIO,
00691 CODEC_ID_AMR_WB,
00692 sizeof(AMRWBContext),
00693 amr_wb_decode_init,
00694 NULL,
00695 amr_wb_decode_close,
00696 amr_wb_decode_frame,
00697 };
00698
00699 AVCodec libamr_wb_encoder =
00700 {
00701 "libamr_wb",
00702 CODEC_TYPE_AUDIO,
00703 CODEC_ID_AMR_WB,
00704 sizeof(AMRWBContext),
00705 amr_wb_encode_init,
00706 amr_wb_encode_frame,
00707 amr_wb_encode_close,
00708 NULL,
00709 };
00710
00711 #endif //CONFIG_LIBAMR_WB