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