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