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 "internal.h"
00024 #include "libavutil/avstring.h"
00025 #include "libavutil/opt.h"
00026
00027 static void amr_decode_fix_avctx(AVCodecContext *avctx)
00028 {
00029 const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
00030
00031 if (!avctx->sample_rate)
00032 avctx->sample_rate = 8000 * is_amr_wb;
00033
00034 if (!avctx->channels)
00035 avctx->channels = 1;
00036
00037 avctx->frame_size = 160 * is_amr_wb;
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 } AMRContext;
00090
00091 static const AVOption options[] = {
00092 { "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 },
00093 { NULL }
00094 };
00095
00096 static const AVClass class = {
00097 "libopencore_amrnb", av_default_item_name, options, LIBAVUTIL_VERSION_INT
00098 };
00099
00100 static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
00101 {
00102 AMRContext *s = avctx->priv_data;
00103
00104 s->dec_state = Decoder_Interface_init();
00105 if (!s->dec_state) {
00106 av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\n");
00107 return -1;
00108 }
00109
00110 amr_decode_fix_avctx(avctx);
00111
00112 if (avctx->channels > 1) {
00113 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
00114 return AVERROR(ENOSYS);
00115 }
00116
00117 avcodec_get_frame_defaults(&s->frame);
00118 avctx->coded_frame = &s->frame;
00119
00120 return 0;
00121 }
00122
00123 static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
00124 {
00125 AMRContext *s = avctx->priv_data;
00126
00127 Decoder_Interface_exit(s->dec_state);
00128
00129 return 0;
00130 }
00131
00132 static int amr_nb_decode_frame(AVCodecContext *avctx, void *data,
00133 int *got_frame_ptr, AVPacket *avpkt)
00134 {
00135 const uint8_t *buf = avpkt->data;
00136 int buf_size = avpkt->size;
00137 AMRContext *s = avctx->priv_data;
00138 static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
00139 enum Mode dec_mode;
00140 int packet_size, ret;
00141
00142 av_dlog(avctx, "amr_decode_frame buf=%p buf_size=%d frame_count=%d!!\n",
00143 buf, buf_size, avctx->frame_number);
00144
00145
00146 s->frame.nb_samples = 160;
00147 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
00148 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00149 return ret;
00150 }
00151
00152 dec_mode = (buf[0] >> 3) & 0x000F;
00153 packet_size = block_size[dec_mode] + 1;
00154
00155 if (packet_size > buf_size) {
00156 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
00157 buf_size, packet_size);
00158 return AVERROR_INVALIDDATA;
00159 }
00160
00161 av_dlog(avctx, "packet_size=%d buf= 0x%X %X %X %X\n",
00162 packet_size, buf[0], buf[1], buf[2], buf[3]);
00163
00164 Decoder_Interface_Decode(s->dec_state, buf, (short *)s->frame.data[0], 0);
00165
00166 *got_frame_ptr = 1;
00167 *(AVFrame *)data = s->frame;
00168
00169 return packet_size;
00170 }
00171
00172 AVCodec ff_libopencore_amrnb_decoder = {
00173 .name = "libopencore_amrnb",
00174 .type = AVMEDIA_TYPE_AUDIO,
00175 .id = CODEC_ID_AMR_NB,
00176 .priv_data_size = sizeof(AMRContext),
00177 .init = amr_nb_decode_init,
00178 .close = amr_nb_decode_close,
00179 .decode = amr_nb_decode_frame,
00180 .capabilities = CODEC_CAP_DR1,
00181 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
00182 };
00183
00184 static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
00185 {
00186 AMRContext *s = avctx->priv_data;
00187
00188 if (avctx->sample_rate != 8000) {
00189 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
00190 return AVERROR(ENOSYS);
00191 }
00192
00193 if (avctx->channels != 1) {
00194 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
00195 return AVERROR(ENOSYS);
00196 }
00197
00198 avctx->frame_size = 160;
00199 avctx->coded_frame = avcodec_alloc_frame();
00200
00201 s->enc_state = Encoder_Interface_init(s->enc_dtx);
00202 if (!s->enc_state) {
00203 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
00204 return -1;
00205 }
00206
00207 s->enc_mode = get_bitrate_mode(avctx->bit_rate, avctx);
00208 s->enc_bitrate = avctx->bit_rate;
00209
00210 return 0;
00211 }
00212
00213 static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
00214 {
00215 AMRContext *s = avctx->priv_data;
00216
00217 Encoder_Interface_exit(s->enc_state);
00218 av_freep(&avctx->coded_frame);
00219 return 0;
00220 }
00221
00222 static int amr_nb_encode_frame(AVCodecContext *avctx,
00223 unsigned char *frame,
00224 int buf_size, void *data)
00225 {
00226 AMRContext *s = avctx->priv_data;
00227 int written;
00228
00229 if (s->enc_bitrate != avctx->bit_rate) {
00230 s->enc_mode = get_bitrate_mode(avctx->bit_rate, avctx);
00231 s->enc_bitrate = avctx->bit_rate;
00232 }
00233
00234 written = Encoder_Interface_Encode(s->enc_state, s->enc_mode, data,
00235 frame, 0);
00236 av_dlog(avctx, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
00237 written, s->enc_mode, frame[0]);
00238
00239 return written;
00240 }
00241
00242 AVCodec ff_libopencore_amrnb_encoder = {
00243 .name = "libopencore_amrnb",
00244 .type = AVMEDIA_TYPE_AUDIO,
00245 .id = CODEC_ID_AMR_NB,
00246 .priv_data_size = sizeof(AMRContext),
00247 .init = amr_nb_encode_init,
00248 .encode = amr_nb_encode_frame,
00249 .close = amr_nb_encode_close,
00250 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00251 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
00252 .priv_class = &class,
00253 };
00254
00255 #endif
00256
00257
00258 #if CONFIG_LIBOPENCORE_AMRWB
00259
00260 #include <opencore-amrwb/dec_if.h>
00261 #include <opencore-amrwb/if_rom.h>
00262
00263 typedef struct AMRWBContext {
00264 AVFrame frame;
00265 void *state;
00266 } AMRWBContext;
00267
00268 static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
00269 {
00270 AMRWBContext *s = avctx->priv_data;
00271
00272 s->state = D_IF_init();
00273
00274 amr_decode_fix_avctx(avctx);
00275
00276 if (avctx->channels > 1) {
00277 av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
00278 return AVERROR(ENOSYS);
00279 }
00280
00281 avcodec_get_frame_defaults(&s->frame);
00282 avctx->coded_frame = &s->frame;
00283
00284 return 0;
00285 }
00286
00287 static int amr_wb_decode_frame(AVCodecContext *avctx, void *data,
00288 int *got_frame_ptr, AVPacket *avpkt)
00289 {
00290 const uint8_t *buf = avpkt->data;
00291 int buf_size = avpkt->size;
00292 AMRWBContext *s = avctx->priv_data;
00293 int mode, ret;
00294 int packet_size;
00295 static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
00296
00297
00298 s->frame.nb_samples = 320;
00299 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
00300 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00301 return ret;
00302 }
00303
00304 mode = (buf[0] >> 3) & 0x000F;
00305 packet_size = block_size[mode];
00306
00307 if (packet_size > buf_size) {
00308 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
00309 buf_size, packet_size + 1);
00310 return AVERROR_INVALIDDATA;
00311 }
00312
00313 D_IF_decode(s->state, buf, (short *)s->frame.data[0], _good_frame);
00314
00315 *got_frame_ptr = 1;
00316 *(AVFrame *)data = s->frame;
00317
00318 return packet_size;
00319 }
00320
00321 static int amr_wb_decode_close(AVCodecContext *avctx)
00322 {
00323 AMRWBContext *s = avctx->priv_data;
00324
00325 D_IF_exit(s->state);
00326 return 0;
00327 }
00328
00329 AVCodec ff_libopencore_amrwb_decoder = {
00330 .name = "libopencore_amrwb",
00331 .type = AVMEDIA_TYPE_AUDIO,
00332 .id = CODEC_ID_AMR_WB,
00333 .priv_data_size = sizeof(AMRWBContext),
00334 .init = amr_wb_decode_init,
00335 .close = amr_wb_decode_close,
00336 .decode = amr_wb_decode_frame,
00337 .capabilities = CODEC_CAP_DR1,
00338 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Wide-Band"),
00339 };
00340
00341 #endif