00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00052 #include "avcodec.h"
00053
00054 static void amr_decode_fix_avctx(AVCodecContext *avctx)
00055 {
00056 const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
00057
00058 if (!avctx->sample_rate)
00059 avctx->sample_rate = 8000 * is_amr_wb;
00060
00061 if (!avctx->channels)
00062 avctx->channels = 1;
00063
00064 avctx->frame_size = 160 * is_amr_wb;
00065 avctx->sample_fmt = SAMPLE_FMT_S16;
00066 }
00067
00068 #if CONFIG_LIBAMR_NB
00069
00070 #include <amrnb/interf_dec.h>
00071 #include <amrnb/interf_enc.h>
00072
00073 static const char nb_bitrate_unsupported[] =
00074 "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n";
00075
00076 typedef struct AMR_bitrates {
00077 int rate;
00078 enum Mode mode;
00079 } AMR_bitrates;
00080
00081
00082 static int getBitrateMode(int bitrate)
00083 {
00084
00085 AMR_bitrates rates[] = { { 4750, MR475},
00086 { 5150, MR515},
00087 { 5900, MR59},
00088 { 6700, MR67},
00089 { 7400, MR74},
00090 { 7950, MR795},
00091 {10200, MR102},
00092 {12200, MR122}, };
00093 int i;
00094
00095 for (i = 0; i < 8; i++)
00096 if (rates[i].rate == bitrate)
00097 return rates[i].mode;
00098
00099 return -1;
00100 }
00101
00102 typedef struct AMRContext {
00103 int frameCount;
00104 void *decState;
00105 int *enstate;
00106 int enc_bitrate;
00107 } AMRContext;
00108
00109 static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
00110 {
00111 AMRContext *s = avctx->priv_data;
00112
00113 s->frameCount = 0;
00114 s->decState = Decoder_Interface_init();
00115 if (!s->decState) {
00116 av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
00117 return -1;
00118 }
00119
00120 amr_decode_fix_avctx(avctx);
00121
00122 if (avctx->channels > 1) {
00123 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
00124 return -1;
00125 }
00126
00127 return 0;
00128 }
00129
00130 static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
00131 {
00132 AMRContext *s = avctx->priv_data;
00133
00134 Decoder_Interface_exit(s->decState);
00135 return 0;
00136 }
00137
00138 static int amr_nb_decode_frame(AVCodecContext *avctx, void *data,
00139 int *data_size,
00140 const uint8_t *buf, int buf_size)
00141 {
00142 AMRContext *s = avctx->priv_data;
00143 const uint8_t *amrData = buf;
00144 static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
00145 enum Mode dec_mode;
00146 int packet_size;
00147
00148
00149
00150
00151 dec_mode = (buf[0] >> 3) & 0x000F;
00152 packet_size = block_size[dec_mode] + 1;
00153
00154 if (packet_size > buf_size) {
00155 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
00156 buf_size, packet_size);
00157 return -1;
00158 }
00159
00160 s->frameCount++;
00161
00162
00163
00164 Decoder_Interface_Decode(s->decState, amrData, data, 0);
00165 *data_size = 160 * 2;
00166
00167 return packet_size;
00168 }
00169
00170 AVCodec libamr_nb_decoder = {
00171 "libamr_nb",
00172 CODEC_TYPE_AUDIO,
00173 CODEC_ID_AMR_NB,
00174 sizeof(AMRContext),
00175 amr_nb_decode_init,
00176 NULL,
00177 amr_nb_decode_close,
00178 amr_nb_decode_frame,
00179 .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
00180 };
00181
00182 static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
00183 {
00184 AMRContext *s = avctx->priv_data;
00185
00186 s->frameCount = 0;
00187
00188 if (avctx->sample_rate != 8000) {
00189 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
00190 return -1;
00191 }
00192
00193 if (avctx->channels != 1) {
00194 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
00195 return -1;
00196 }
00197
00198 avctx->frame_size = 160;
00199 avctx->coded_frame = avcodec_alloc_frame();
00200
00201 s->enstate=Encoder_Interface_init(0);
00202 if (!s->enstate) {
00203 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
00204 return -1;
00205 }
00206
00207 if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
00208 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
00209 return -1;
00210 }
00211
00212 return 0;
00213 }
00214
00215 static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
00216 {
00217 AMRContext *s = avctx->priv_data;
00218
00219 Encoder_Interface_exit(s->enstate);
00220 av_freep(&avctx->coded_frame);
00221 return 0;
00222 }
00223
00224 static int amr_nb_encode_frame(AVCodecContext *avctx,
00225 unsigned char *frame,
00226 int buf_size, void *data)
00227 {
00228 AMRContext *s = avctx->priv_data;
00229 int written;
00230
00231 if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
00232 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
00233 return -1;
00234 }
00235
00236 written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data,
00237 frame, 0);
00238
00239
00240
00241 return written;
00242 }
00243
00244 AVCodec libamr_nb_encoder = {
00245 "libamr_nb",
00246 CODEC_TYPE_AUDIO,
00247 CODEC_ID_AMR_NB,
00248 sizeof(AMRContext),
00249 amr_nb_encode_init,
00250 amr_nb_encode_frame,
00251 amr_nb_encode_close,
00252 NULL,
00253 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
00254 .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
00255 };
00256
00257 #endif
00258
00259
00260 #if CONFIG_LIBAMR_WB
00261
00262 #ifdef _TYPEDEF_H
00263
00264 #define typedef_h
00265 #endif
00266
00267 #include <amrwb/dec_if.h>
00268 #include <amrwb/if_rom.h>
00269
00270 static const char wb_bitrate_unsupported[] =
00271 "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";
00272
00273 typedef struct AMRWB_bitrates {
00274 int rate;
00275 int mode;
00276 } AMRWB_bitrates;
00277
00278 typedef struct AMRWBContext {
00279 int frameCount;
00280 void *state;
00281 int mode;
00282 Word16 allow_dtx;
00283 } AMRWBContext;
00284
00285 #if CONFIG_LIBAMR_WB_ENCODER
00286
00287 #include <amrwb/enc_if.h>
00288
00289 static int getWBBitrateMode(int bitrate)
00290 {
00291
00292 AMRWB_bitrates rates[] = { { 6600, 0},
00293 { 8850, 1},
00294 {12650, 2},
00295 {14250, 3},
00296 {15850, 4},
00297 {18250, 5},
00298 {19850, 6},
00299 {23050, 7},
00300 {23850, 8}, };
00301 int i;
00302
00303 for (i = 0; i < 9; i++)
00304 if (rates[i].rate == bitrate)
00305 return rates[i].mode;
00306
00307 return -1;
00308 }
00309
00310 static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
00311 {
00312 AMRWBContext *s = avctx->priv_data;
00313
00314 s->frameCount = 0;
00315
00316 if (avctx->sample_rate != 16000) {
00317 av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
00318 return -1;
00319 }
00320
00321 if (avctx->channels != 1) {
00322 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
00323 return -1;
00324 }
00325
00326 if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
00327 av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
00328 return -1;
00329 }
00330
00331 avctx->frame_size = 320;
00332 avctx->coded_frame = avcodec_alloc_frame();
00333
00334 s->state = E_IF_init();
00335 s->allow_dtx = 0;
00336
00337 return 0;
00338 }
00339
00340 static int amr_wb_encode_close(AVCodecContext *avctx)
00341 {
00342 AMRWBContext *s = avctx->priv_data;
00343
00344 E_IF_exit(s->state);
00345 av_freep(&avctx->coded_frame);
00346 s->frameCount++;
00347 return 0;
00348 }
00349
00350 static int amr_wb_encode_frame(AVCodecContext *avctx,
00351 unsigned char *frame,
00352 int buf_size, void *data)
00353 {
00354 AMRWBContext *s = avctx->priv_data;
00355 int size;
00356
00357 if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
00358 av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
00359 return -1;
00360 }
00361 size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
00362 return size;
00363 }
00364
00365 AVCodec libamr_wb_encoder = {
00366 "libamr_wb",
00367 CODEC_TYPE_AUDIO,
00368 CODEC_ID_AMR_WB,
00369 sizeof(AMRWBContext),
00370 amr_wb_encode_init,
00371 amr_wb_encode_frame,
00372 amr_wb_encode_close,
00373 NULL,
00374 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
00375 .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
00376 };
00377
00378 #endif
00379
00380 static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
00381 {
00382 AMRWBContext *s = avctx->priv_data;
00383
00384 s->frameCount = 0;
00385 s->state = D_IF_init();
00386
00387 amr_decode_fix_avctx(avctx);
00388
00389 if (avctx->channels > 1) {
00390 av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
00391 return -1;
00392 }
00393
00394 return 0;
00395 }
00396
00397 static int amr_wb_decode_frame(AVCodecContext *avctx,
00398 void *data, int *data_size,
00399 const uint8_t *buf, int buf_size)
00400 {
00401 AMRWBContext *s = avctx->priv_data;
00402 const uint8_t *amrData = buf;
00403 int mode;
00404 int packet_size;
00405 static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
00406
00407 if (!buf_size)
00408
00409 return 0;
00410
00411 mode = (amrData[0] >> 3) & 0x000F;
00412 packet_size = block_size[mode];
00413
00414 if (packet_size > buf_size) {
00415 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
00416 buf_size, packet_size + 1);
00417 return -1;
00418 }
00419
00420 s->frameCount++;
00421 D_IF_decode(s->state, amrData, data, _good_frame);
00422 *data_size = 320 * 2;
00423 return packet_size;
00424 }
00425
00426 static int amr_wb_decode_close(AVCodecContext *avctx)
00427 {
00428 AMRWBContext *s = avctx->priv_data;
00429
00430 D_IF_exit(s->state);
00431 return 0;
00432 }
00433
00434 AVCodec libamr_wb_decoder = {
00435 "libamr_wb",
00436 CODEC_TYPE_AUDIO,
00437 CODEC_ID_AMR_WB,
00438 sizeof(AMRWBContext),
00439 amr_wb_decode_init,
00440 NULL,
00441 amr_wb_decode_close,
00442 amr_wb_decode_frame,
00443 .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
00444 };
00445
00446 #endif //CONFIG_LIBAMR_WB