2 * AMR Audio decoder stub
3 * Copyright (c) 2003 The FFmpeg project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "config_components.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/opt.h"
32 #include "audio_frame_queue.h"
33 #include "codec_internal.h"
37 #if CONFIG_LIBOPENCORE_AMRNB_DECODER || CONFIG_LIBOPENCORE_AMRWB_DECODER
38 static int amr_decode_fix_avctx(AVCodecContext
*avctx
)
40 const int is_amr_wb
= 1 + (avctx
->codec_id
== AV_CODEC_ID_AMR_WB
);
42 if (!avctx
->sample_rate
)
43 avctx
->sample_rate
= 8000 * is_amr_wb
;
45 if (avctx
->ch_layout
.nb_channels
> 1) {
46 avpriv_report_missing_feature(avctx
, "multi-channel AMR");
47 return AVERROR_PATCHWELCOME
;
50 av_channel_layout_uninit(&avctx
->ch_layout
);
51 avctx
->ch_layout
= (AVChannelLayout
)AV_CHANNEL_LAYOUT_MONO
;
52 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16
;
57 #if CONFIG_LIBOPENCORE_AMRNB
59 #include <opencore-amrnb/interf_dec.h>
60 #include <opencore-amrnb/interf_enc.h>
62 typedef struct AMRContext
{
73 #if CONFIG_LIBOPENCORE_AMRNB_DECODER
74 static av_cold
int amr_nb_decode_init(AVCodecContext
*avctx
)
76 AMRContext
*s
= avctx
->priv_data
;
79 if ((ret
= amr_decode_fix_avctx(avctx
)) < 0)
82 s
->dec_state
= Decoder_Interface_init();
84 av_log(avctx
, AV_LOG_ERROR
, "Decoder_Interface_init error\n");
91 static av_cold
int amr_nb_decode_close(AVCodecContext
*avctx
)
93 AMRContext
*s
= avctx
->priv_data
;
95 Decoder_Interface_exit(s
->dec_state
);
100 static int amr_nb_decode_frame(AVCodecContext
*avctx
, AVFrame
*frame
,
101 int *got_frame_ptr
, AVPacket
*avpkt
)
103 const uint8_t *buf
= avpkt
->data
;
104 int buf_size
= avpkt
->size
;
105 AMRContext
*s
= avctx
->priv_data
;
106 static const uint8_t block_size
[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
108 int packet_size
, ret
;
110 ff_dlog(avctx
, "amr_decode_frame buf=%p buf_size=%d frame_count=%"PRId64
"!!\n",
111 buf
, buf_size
, avctx
->frame_num
);
113 /* get output buffer */
114 frame
->nb_samples
= 160;
115 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
118 dec_mode
= (buf
[0] >> 3) & 0x000F;
119 packet_size
= block_size
[dec_mode
] + 1;
121 if (packet_size
> buf_size
) {
122 av_log(avctx
, AV_LOG_ERROR
, "AMR frame too short (%d, should be %d)\n",
123 buf_size
, packet_size
);
124 return AVERROR_INVALIDDATA
;
127 ff_dlog(avctx
, "packet_size=%d buf= 0x%"PRIx8
" %"PRIx8
" %"PRIx8
" %"PRIx8
"\n",
128 packet_size
, buf
[0], buf
[1], buf
[2], buf
[3]);
130 Decoder_Interface_Decode(s
->dec_state
, buf
, (short *)frame
->data
[0], 0);
137 const FFCodec ff_libopencore_amrnb_decoder
= {
138 .p
.name
= "libopencore_amrnb",
139 CODEC_LONG_NAME("OpenCORE AMR-NB (Adaptive Multi-Rate Narrow-Band)"),
140 .p
.type
= AVMEDIA_TYPE_AUDIO
,
141 .p
.id
= AV_CODEC_ID_AMR_NB
,
142 .caps_internal
= FF_CODEC_CAP_NOT_INIT_THREADSAFE
,
143 .priv_data_size
= sizeof(AMRContext
),
144 .init
= amr_nb_decode_init
,
145 .close
= amr_nb_decode_close
,
146 FF_CODEC_DECODE_CB(amr_nb_decode_frame
),
147 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_CHANNEL_CONF
,
149 #endif /* CONFIG_LIBOPENCORE_AMRNB_DECODER */
151 #if CONFIG_LIBOPENCORE_AMRNB_ENCODER
152 /* Common code for fixed and float version*/
153 typedef struct AMR_bitrates
{
158 /* Match desired bitrate */
159 static int get_bitrate_mode(int bitrate
, void *log_ctx
)
161 /* make the correspondence between bitrate and mode */
162 static const AMR_bitrates rates
[] = {
163 { 4750, MR475
}, { 5150, MR515
}, { 5900, MR59
}, { 6700, MR67
},
164 { 7400, MR74
}, { 7950, MR795
}, { 10200, MR102
}, { 12200, MR122
}
166 int i
, best
= -1, min_diff
= 0;
169 for (i
= 0; i
< 8; i
++) {
170 if (rates
[i
].rate
== bitrate
)
171 return rates
[i
].mode
;
172 if (best
< 0 || abs(rates
[i
].rate
- bitrate
) < min_diff
) {
174 min_diff
= abs(rates
[i
].rate
- bitrate
);
177 /* no bitrate matching exactly, log a warning */
178 snprintf(log_buf
, sizeof(log_buf
), "bitrate not supported: use one of ");
179 for (i
= 0; i
< 8; i
++)
180 av_strlcatf(log_buf
, sizeof(log_buf
), "%.2fk, ", rates
[i
].rate
/ 1000.f
);
181 av_strlcatf(log_buf
, sizeof(log_buf
), "using %.2fk", rates
[best
].rate
/ 1000.f
);
182 av_log(log_ctx
, AV_LOG_WARNING
, "%s\n", log_buf
);
187 static const AVOption options
[] = {
188 { "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
},
192 static const AVClass amrnb_class
= {
193 .class_name
= "libopencore_amrnb",
194 .item_name
= av_default_item_name
,
196 .version
= LIBAVUTIL_VERSION_INT
,
199 static av_cold
int amr_nb_encode_init(AVCodecContext
*avctx
)
201 AMRContext
*s
= avctx
->priv_data
;
203 if (avctx
->sample_rate
!= 8000 && avctx
->strict_std_compliance
> FF_COMPLIANCE_UNOFFICIAL
) {
204 av_log(avctx
, AV_LOG_ERROR
, "Only 8000Hz sample rate supported\n");
205 return AVERROR(ENOSYS
);
208 if (avctx
->ch_layout
.nb_channels
!= 1) {
209 av_log(avctx
, AV_LOG_ERROR
, "Only mono supported\n");
210 return AVERROR(ENOSYS
);
213 avctx
->frame_size
= 160;
214 avctx
->initial_padding
= 50;
215 ff_af_queue_init(avctx
, &s
->afq
);
217 s
->enc_state
= Encoder_Interface_init(s
->enc_dtx
);
219 av_log(avctx
, AV_LOG_ERROR
, "Encoder_Interface_init error\n");
223 s
->enc_mode
= get_bitrate_mode(avctx
->bit_rate
, avctx
);
224 s
->enc_bitrate
= avctx
->bit_rate
;
229 static av_cold
int amr_nb_encode_close(AVCodecContext
*avctx
)
231 AMRContext
*s
= avctx
->priv_data
;
233 Encoder_Interface_exit(s
->enc_state
);
234 ff_af_queue_close(&s
->afq
);
238 static int amr_nb_encode_frame(AVCodecContext
*avctx
, AVPacket
*avpkt
,
239 const AVFrame
*frame
, int *got_packet_ptr
)
241 AMRContext
*s
= avctx
->priv_data
;
243 int16_t *flush_buf
= NULL
;
244 const int16_t *samples
= frame
? (const int16_t *)frame
->data
[0] : NULL
;
246 if (s
->enc_bitrate
!= avctx
->bit_rate
) {
247 s
->enc_mode
= get_bitrate_mode(avctx
->bit_rate
, avctx
);
248 s
->enc_bitrate
= avctx
->bit_rate
;
251 if ((ret
= ff_alloc_packet(avctx
, avpkt
, 32)) < 0)
255 if (frame
->nb_samples
< avctx
->frame_size
) {
256 flush_buf
= av_calloc(avctx
->frame_size
, sizeof(*flush_buf
));
258 return AVERROR(ENOMEM
);
259 memcpy(flush_buf
, samples
, frame
->nb_samples
* sizeof(*flush_buf
));
261 if (frame
->nb_samples
< avctx
->frame_size
- avctx
->initial_padding
)
262 s
->enc_last_frame
= -1;
264 if ((ret
= ff_af_queue_add(&s
->afq
, frame
)) < 0) {
265 av_freep(&flush_buf
);
269 if (s
->enc_last_frame
< 0)
271 flush_buf
= av_calloc(avctx
->frame_size
, sizeof(*flush_buf
));
273 return AVERROR(ENOMEM
);
275 s
->enc_last_frame
= -1;
278 written
= Encoder_Interface_Encode(s
->enc_state
, s
->enc_mode
, samples
,
280 ff_dlog(avctx
, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
281 written
, s
->enc_mode
, avpkt
->data
[0]);
283 /* Get the next frame pts/duration */
284 ff_af_queue_remove(&s
->afq
, avctx
->frame_size
, &avpkt
->pts
,
287 avpkt
->size
= written
;
289 av_freep(&flush_buf
);
293 const FFCodec ff_libopencore_amrnb_encoder
= {
294 .p
.name
= "libopencore_amrnb",
295 CODEC_LONG_NAME("OpenCORE AMR-NB (Adaptive Multi-Rate Narrow-Band)"),
296 .p
.type
= AVMEDIA_TYPE_AUDIO
,
297 .p
.id
= AV_CODEC_ID_AMR_NB
,
298 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_DELAY
|
299 AV_CODEC_CAP_SMALL_LAST_FRAME
,
300 .caps_internal
= FF_CODEC_CAP_NOT_INIT_THREADSAFE
,
301 .priv_data_size
= sizeof(AMRContext
),
302 .init
= amr_nb_encode_init
,
303 FF_CODEC_ENCODE_CB(amr_nb_encode_frame
),
304 .close
= amr_nb_encode_close
,
305 .p
.sample_fmts
= (const enum AVSampleFormat
[]){ AV_SAMPLE_FMT_S16
,
306 AV_SAMPLE_FMT_NONE
},
307 .p
.priv_class
= &amrnb_class
,
309 #endif /* CONFIG_LIBOPENCORE_AMRNB_ENCODER */
311 #endif /* CONFIG_LIBOPENCORE_AMRNB */
313 /* -----------AMR wideband ------------*/
314 #if CONFIG_LIBOPENCORE_AMRWB_DECODER
316 #include <opencore-amrwb/dec_if.h>
317 #include <opencore-amrwb/if_rom.h>
319 typedef struct AMRWBContext
{
323 static av_cold
int amr_wb_decode_init(AVCodecContext
*avctx
)
325 AMRWBContext
*s
= avctx
->priv_data
;
328 if ((ret
= amr_decode_fix_avctx(avctx
)) < 0)
331 s
->state
= D_IF_init();
336 static int amr_wb_decode_frame(AVCodecContext
*avctx
, AVFrame
*frame
,
337 int *got_frame_ptr
, AVPacket
*avpkt
)
339 const uint8_t *buf
= avpkt
->data
;
340 int buf_size
= avpkt
->size
;
341 AMRWBContext
*s
= avctx
->priv_data
;
344 static const uint8_t block_size
[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
346 /* get output buffer */
347 frame
->nb_samples
= 320;
348 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
351 mode
= (buf
[0] >> 3) & 0x000F;
352 packet_size
= block_size
[mode
];
354 if (packet_size
> buf_size
) {
355 av_log(avctx
, AV_LOG_ERROR
, "AMR frame too short (%d, should be %d)\n",
356 buf_size
, packet_size
+ 1);
357 return AVERROR_INVALIDDATA
;
360 av_log(avctx
, AV_LOG_ERROR
, "amr packet_size invalid\n");
361 return AVERROR_INVALIDDATA
;
364 D_IF_decode(s
->state
, buf
, (short *)frame
->data
[0], _good_frame
);
371 static int amr_wb_decode_close(AVCodecContext
*avctx
)
373 AMRWBContext
*s
= avctx
->priv_data
;
379 const FFCodec ff_libopencore_amrwb_decoder
= {
380 .p
.name
= "libopencore_amrwb",
381 CODEC_LONG_NAME("OpenCORE AMR-WB (Adaptive Multi-Rate Wide-Band)"),
382 .p
.type
= AVMEDIA_TYPE_AUDIO
,
383 .p
.id
= AV_CODEC_ID_AMR_WB
,
384 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_CHANNEL_CONF
,
385 .p
.wrapper_name
= "libopencore_amrwb",
386 .caps_internal
= FF_CODEC_CAP_NOT_INIT_THREADSAFE
,
387 .priv_data_size
= sizeof(AMRWBContext
),
388 .init
= amr_wb_decode_init
,
389 .close
= amr_wb_decode_close
,
390 FF_CODEC_DECODE_CB(amr_wb_decode_frame
),
393 #endif /* CONFIG_LIBOPENCORE_AMRWB_DECODER */