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