00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "libavutil/avstring.h"
00024 #include "libavutil/common.h"
00025 #include "libavutil/opt.h"
00026 #include "audio_frame_queue.h"
00027 #include "internal.h"
00028
00029 static void amr_decode_fix_avctx(AVCodecContext *avctx)
00030 {
00031 const int is_amr_wb = 1 + (avctx->codec_id == AV_CODEC_ID_AMR_WB);
00032
00033 if (!avctx->sample_rate)
00034 avctx->sample_rate = 8000 * is_amr_wb;
00035
00036 if (!avctx->channels)
00037 avctx->channels = 1;
00038
00039 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00040 }
00041
00042 #if CONFIG_LIBOPENCORE_AMRNB
00043
00044 #include <opencore-amrnb/interf_dec.h>
00045 #include <opencore-amrnb/interf_enc.h>
00046
00047
00048 typedef struct AMR_bitrates {
00049 int rate;
00050 enum Mode mode;
00051 } AMR_bitrates;
00052
00053
00054 static int get_bitrate_mode(int bitrate, void *log_ctx)
00055 {
00056
00057 static const AMR_bitrates rates[] = {
00058 { 4750, MR475 }, { 5150, MR515 }, { 5900, MR59 }, { 6700, MR67 },
00059 { 7400, MR74 }, { 7950, MR795 }, { 10200, MR102 }, { 12200, MR122 }
00060 };
00061 int i, best = -1, min_diff = 0;
00062 char log_buf[200];
00063
00064 for (i = 0; i < 8; i++) {
00065 if (rates[i].rate == bitrate)
00066 return rates[i].mode;
00067 if (best < 0 || abs(rates[i].rate - bitrate) < min_diff) {
00068 best = i;
00069 min_diff = abs(rates[i].rate - bitrate);
00070 }
00071 }
00072
00073 snprintf(log_buf, sizeof(log_buf), "bitrate not supported: use one of ");
00074 for (i = 0; i < 8; i++)
00075 av_strlcatf(log_buf, sizeof(log_buf), "%.2fk, ", rates[i].rate / 1000.f);
00076 av_strlcatf(log_buf, sizeof(log_buf), "using %.2fk", rates[best].rate / 1000.f);
00077 av_log(log_ctx, AV_LOG_WARNING, "%s\n", log_buf);
00078
00079 return best;
00080 }
00081
00082 typedef struct AMRContext {
00083 AVClass *av_class;
00084 AVFrame frame;
00085 void *dec_state;
00086 void *enc_state;
00087 int enc_bitrate;
00088 int enc_mode;
00089 int enc_dtx;
00090 int enc_last_frame;
00091 AudioFrameQueue afq;
00092 } AMRContext;
00093
00094 static const AVOption options[] = {
00095 { "dtx", "Allow DTX (generate comfort noise)", offsetof(AMRContext, enc_dtx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
00096 { NULL }
00097 };
00098
00099 static const AVClass class = {
00100 "libopencore_amrnb", av_default_item_name, options, LIBAVUTIL_VERSION_INT
00101 };
00102
00103 static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
00104 {
00105 AMRContext *s = avctx->priv_data;
00106
00107 s->dec_state = Decoder_Interface_init();
00108 if (!s->dec_state) {
00109 av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\n");
00110 return -1;
00111 }
00112
00113 amr_decode_fix_avctx(avctx);
00114
00115 if (avctx->channels > 1) {
00116 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
00117 return AVERROR(ENOSYS);
00118 }
00119
00120 avcodec_get_frame_defaults(&s->frame);
00121 avctx->coded_frame = &s->frame;
00122
00123 return 0;
00124 }
00125
00126 static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
00127 {
00128 AMRContext *s = avctx->priv_data;
00129
00130 Decoder_Interface_exit(s->dec_state);
00131
00132 return 0;
00133 }
00134
00135 static int amr_nb_decode_frame(AVCodecContext *avctx, void *data,
00136 int *got_frame_ptr, AVPacket *avpkt)
00137 {
00138 const uint8_t *buf = avpkt->data;
00139 int buf_size = avpkt->size;
00140 AMRContext *s = avctx->priv_data;
00141 static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
00142 enum Mode dec_mode;
00143 int packet_size, ret;
00144
00145 av_dlog(avctx, "amr_decode_frame buf=%p buf_size=%d frame_count=%d!!\n",
00146 buf, buf_size, avctx->frame_number);
00147
00148
00149 s->frame.nb_samples = 160;
00150 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00151 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00152 return ret;
00153 }
00154
00155 dec_mode = (buf[0] >> 3) & 0x000F;
00156 packet_size = block_size[dec_mode] + 1;
00157
00158 if (packet_size > buf_size) {
00159 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
00160 buf_size, packet_size);
00161 return AVERROR_INVALIDDATA;
00162 }
00163
00164 av_dlog(avctx, "packet_size=%d buf= 0x%X %X %X %X\n",
00165 packet_size, buf[0], buf[1], buf[2], buf[3]);
00166
00167 Decoder_Interface_Decode(s->dec_state, buf, (short *)s->frame.data[0], 0);
00168
00169 *got_frame_ptr = 1;
00170 *(AVFrame *)data = s->frame;
00171
00172 return packet_size;
00173 }
00174
00175 AVCodec ff_libopencore_amrnb_decoder = {
00176 .name = "libopencore_amrnb",
00177 .type = AVMEDIA_TYPE_AUDIO,
00178 .id = AV_CODEC_ID_AMR_NB,
00179 .priv_data_size = sizeof(AMRContext),
00180 .init = amr_nb_decode_init,
00181 .close = amr_nb_decode_close,
00182 .decode = amr_nb_decode_frame,
00183 .capabilities = CODEC_CAP_DR1,
00184 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE AMR-NB (Adaptive Multi-Rate Narrow-Band)"),
00185 };
00186
00187 static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
00188 {
00189 AMRContext *s = avctx->priv_data;
00190
00191 if (avctx->sample_rate != 8000) {
00192 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
00193 return AVERROR(ENOSYS);
00194 }
00195
00196 if (avctx->channels != 1) {
00197 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
00198 return AVERROR(ENOSYS);
00199 }
00200
00201 avctx->frame_size = 160;
00202 avctx->delay = 50;
00203 ff_af_queue_init(avctx, &s->afq);
00204 #if FF_API_OLD_ENCODE_AUDIO
00205 avctx->coded_frame = avcodec_alloc_frame();
00206 if (!avctx->coded_frame)
00207 return AVERROR(ENOMEM);
00208 #endif
00209
00210 s->enc_state = Encoder_Interface_init(s->enc_dtx);
00211 if (!s->enc_state) {
00212 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
00213 av_freep(&avctx->coded_frame);
00214 return -1;
00215 }
00216
00217 s->enc_mode = get_bitrate_mode(avctx->bit_rate, avctx);
00218 s->enc_bitrate = avctx->bit_rate;
00219
00220 return 0;
00221 }
00222
00223 static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
00224 {
00225 AMRContext *s = avctx->priv_data;
00226
00227 Encoder_Interface_exit(s->enc_state);
00228 ff_af_queue_close(&s->afq);
00229 #if FF_API_OLD_ENCODE_AUDIO
00230 av_freep(&avctx->coded_frame);
00231 #endif
00232 return 0;
00233 }
00234
00235 static int amr_nb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00236 const AVFrame *frame, int *got_packet_ptr)
00237 {
00238 AMRContext *s = avctx->priv_data;
00239 int written, ret;
00240 int16_t *flush_buf = NULL;
00241 const int16_t *samples = frame ? (const int16_t *)frame->data[0] : NULL;
00242
00243 if (s->enc_bitrate != avctx->bit_rate) {
00244 s->enc_mode = get_bitrate_mode(avctx->bit_rate, avctx);
00245 s->enc_bitrate = avctx->bit_rate;
00246 }
00247
00248 if ((ret = ff_alloc_packet2(avctx, avpkt, 32)))
00249 return ret;
00250
00251 if (frame) {
00252 if (frame->nb_samples < avctx->frame_size) {
00253 flush_buf = av_mallocz(avctx->frame_size * sizeof(*flush_buf));
00254 if (!flush_buf)
00255 return AVERROR(ENOMEM);
00256 memcpy(flush_buf, samples, frame->nb_samples * sizeof(*flush_buf));
00257 samples = flush_buf;
00258 if (frame->nb_samples < avctx->frame_size - avctx->delay)
00259 s->enc_last_frame = -1;
00260 }
00261 if ((ret = ff_af_queue_add(&s->afq, frame) < 0)) {
00262 av_freep(&flush_buf);
00263 return ret;
00264 }
00265 } else {
00266 if (s->enc_last_frame < 0)
00267 return 0;
00268 flush_buf = av_mallocz(avctx->frame_size * sizeof(*flush_buf));
00269 if (!flush_buf)
00270 return AVERROR(ENOMEM);
00271 samples = flush_buf;
00272 s->enc_last_frame = -1;
00273 }
00274
00275 written = Encoder_Interface_Encode(s->enc_state, s->enc_mode, samples,
00276 avpkt->data, 0);
00277 av_dlog(avctx, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
00278 written, s->enc_mode, frame[0]);
00279
00280
00281 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
00282 &avpkt->duration);
00283
00284 avpkt->size = written;
00285 *got_packet_ptr = 1;
00286 av_freep(&flush_buf);
00287 return 0;
00288 }
00289
00290 AVCodec ff_libopencore_amrnb_encoder = {
00291 .name = "libopencore_amrnb",
00292 .type = AVMEDIA_TYPE_AUDIO,
00293 .id = AV_CODEC_ID_AMR_NB,
00294 .priv_data_size = sizeof(AMRContext),
00295 .init = amr_nb_encode_init,
00296 .encode2 = amr_nb_encode_frame,
00297 .close = amr_nb_encode_close,
00298 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
00299 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00300 AV_SAMPLE_FMT_NONE },
00301 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE AMR-NB (Adaptive Multi-Rate Narrow-Band)"),
00302 .priv_class = &class,
00303 };
00304
00305 #endif
00306
00307
00308 #if CONFIG_LIBOPENCORE_AMRWB
00309
00310 #include <opencore-amrwb/dec_if.h>
00311 #include <opencore-amrwb/if_rom.h>
00312
00313 typedef struct AMRWBContext {
00314 AVFrame frame;
00315 void *state;
00316 } AMRWBContext;
00317
00318 static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
00319 {
00320 AMRWBContext *s = avctx->priv_data;
00321
00322 s->state = D_IF_init();
00323
00324 amr_decode_fix_avctx(avctx);
00325
00326 if (avctx->channels > 1) {
00327 av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
00328 return AVERROR(ENOSYS);
00329 }
00330
00331 avcodec_get_frame_defaults(&s->frame);
00332 avctx->coded_frame = &s->frame;
00333
00334 return 0;
00335 }
00336
00337 static int amr_wb_decode_frame(AVCodecContext *avctx, void *data,
00338 int *got_frame_ptr, AVPacket *avpkt)
00339 {
00340 const uint8_t *buf = avpkt->data;
00341 int buf_size = avpkt->size;
00342 AMRWBContext *s = avctx->priv_data;
00343 int mode, ret;
00344 int packet_size;
00345 static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
00346
00347
00348 s->frame.nb_samples = 320;
00349 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00350 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00351 return ret;
00352 }
00353
00354 mode = (buf[0] >> 3) & 0x000F;
00355 packet_size = block_size[mode];
00356
00357 if (packet_size > buf_size) {
00358 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
00359 buf_size, packet_size + 1);
00360 return AVERROR_INVALIDDATA;
00361 }
00362
00363 D_IF_decode(s->state, buf, (short *)s->frame.data[0], _good_frame);
00364
00365 *got_frame_ptr = 1;
00366 *(AVFrame *)data = s->frame;
00367
00368 return packet_size;
00369 }
00370
00371 static int amr_wb_decode_close(AVCodecContext *avctx)
00372 {
00373 AMRWBContext *s = avctx->priv_data;
00374
00375 D_IF_exit(s->state);
00376 return 0;
00377 }
00378
00379 AVCodec ff_libopencore_amrwb_decoder = {
00380 .name = "libopencore_amrwb",
00381 .type = AVMEDIA_TYPE_AUDIO,
00382 .id = AV_CODEC_ID_AMR_WB,
00383 .priv_data_size = sizeof(AMRWBContext),
00384 .init = amr_wb_decode_init,
00385 .close = amr_wb_decode_close,
00386 .decode = amr_wb_decode_frame,
00387 .capabilities = CODEC_CAP_DR1,
00388 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE AMR-WB (Adaptive Multi-Rate Wide-Band)"),
00389 };
00390
00391 #endif