FFmpeg
Loading...
Searching...
No Matches
libopencore-amr.c
Go to the documentation of this file.
1/*
2 * AMR Audio decoder stub
3 * Copyright (c) 2003 The FFmpeg project
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "config_components.h"
23
24#include <inttypes.h>
25
26#include "libavutil/avstring.h"
28#include "libavutil/common.h"
29#include "libavutil/mem.h"
30#include "libavutil/opt.h"
31#include "avcodec.h"
32#include "audio_frame_queue.h"
33#include "codec_internal.h"
34#include "decode.h"
35#include "encode.h"
36
37#if CONFIG_LIBOPENCORE_AMRNB_DECODER || CONFIG_LIBOPENCORE_AMRWB_DECODER
38static int amr_decode_fix_avctx(AVCodecContext *avctx)
39{
40 const int is_amr_wb = 1 + (avctx->codec_id == AV_CODEC_ID_AMR_WB);
41
42 if (!avctx->sample_rate)
43 avctx->sample_rate = 8000 * is_amr_wb;
44
45 if (avctx->ch_layout.nb_channels > 1) {
46 avpriv_report_missing_feature(avctx, "multi-channel AMR");
48 }
49
53 return 0;
54}
55#endif
56
57#if CONFIG_LIBOPENCORE_AMRNB
58
59#include <opencore-amrnb/interf_dec.h>
60#include <opencore-amrnb/interf_enc.h>
61
62typedef struct AMRContext {
63 AVClass *av_class;
64 void *dec_state;
65 void *enc_state;
66 int enc_bitrate;
67 int enc_mode;
68 int enc_dtx;
69 int enc_last_frame;
70 AudioFrameQueue afq;
72
73#if CONFIG_LIBOPENCORE_AMRNB_DECODER
74static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
75{
76 AMRContext *s = avctx->priv_data;
77 int ret;
78
79 if ((ret = amr_decode_fix_avctx(avctx)) < 0)
80 return ret;
81
82 s->dec_state = Decoder_Interface_init();
83 if (!s->dec_state) {
84 av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\n");
85 return -1;
86 }
87
88 return 0;
89}
90
91static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
92{
93 AMRContext *s = avctx->priv_data;
94
95 Decoder_Interface_exit(s->dec_state);
96
97 return 0;
98}
99
100static int amr_nb_decode_frame(AVCodecContext *avctx, AVFrame *frame,
101 int *got_frame_ptr, AVPacket *avpkt)
102{
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 };
107 enum Mode dec_mode;
108 int packet_size, ret;
109
110 ff_dlog(avctx, "amr_decode_frame buf=%p buf_size=%d frame_count=%"PRId64"!!\n",
111 buf, buf_size, avctx->frame_num);
112
113 /* get output buffer */
114 frame->nb_samples = 160;
115 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
116 return ret;
117
118 dec_mode = (buf[0] >> 3) & 0x000F;
119 packet_size = block_size[dec_mode] + 1;
120
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;
125 }
126
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]);
129 /* call decoder */
130 Decoder_Interface_Decode(s->dec_state, buf, (short *)frame->data[0], 0);
131
132 *got_frame_ptr = 1;
133
134 return packet_size;
135}
136
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),
148};
149#endif /* CONFIG_LIBOPENCORE_AMRNB_DECODER */
150
151#if CONFIG_LIBOPENCORE_AMRNB_ENCODER
152/* Common code for fixed and float version*/
153typedef struct AMR_bitrates {
154 int rate;
155 enum Mode mode;
156} AMR_bitrates;
157
158/* Match desired bitrate */
159static int get_bitrate_mode(int bitrate, void *log_ctx)
160{
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 }
165 };
166 int i, best = -1, min_diff = 0;
167 char log_buf[200];
168
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) {
173 best = i;
174 min_diff = abs(rates[i].rate - bitrate);
175 }
176 }
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);
183
184 return best;
185}
186
187static 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 },
189 { NULL }
190};
191
192static const AVClass amrnb_class = {
193 .class_name = "libopencore_amrnb",
194 .item_name = av_default_item_name,
195 .option = options,
196 .version = LIBAVUTIL_VERSION_INT,
197};
198
199static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
200{
201 AMRContext *s = avctx->priv_data;
202
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);
206 }
207
208 if (avctx->ch_layout.nb_channels != 1) {
209 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
210 return AVERROR(ENOSYS);
211 }
212
213 avctx->frame_size = 160;
214 avctx->initial_padding = 50;
215 ff_af_queue_init(avctx, &s->afq);
216
217 s->enc_state = Encoder_Interface_init(s->enc_dtx);
218 if (!s->enc_state) {
219 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
220 return -1;
221 }
222
223 s->enc_mode = get_bitrate_mode(avctx->bit_rate, avctx);
224 s->enc_bitrate = avctx->bit_rate;
225
226 return 0;
227}
228
229static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
230{
231 AMRContext *s = avctx->priv_data;
232
233 Encoder_Interface_exit(s->enc_state);
234 ff_af_queue_close(&s->afq);
235 return 0;
236}
237
238static int amr_nb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
239 const AVFrame *frame, int *got_packet_ptr)
240{
241 AMRContext *s = avctx->priv_data;
242 int written, ret;
243 int16_t *flush_buf = NULL;
244 const int16_t *samples = frame ? (const int16_t *)frame->data[0] : NULL;
245
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;
249 }
250
251 if ((ret = ff_alloc_packet(avctx, avpkt, 32)) < 0)
252 return ret;
253
254 if (frame) {
255 if (frame->nb_samples < avctx->frame_size) {
256 flush_buf = av_calloc(avctx->frame_size, sizeof(*flush_buf));
257 if (!flush_buf)
258 return AVERROR(ENOMEM);
259 memcpy(flush_buf, samples, frame->nb_samples * sizeof(*flush_buf));
260 samples = flush_buf;
261 if (frame->nb_samples < avctx->frame_size - avctx->initial_padding)
262 s->enc_last_frame = -1;
263 }
264 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) {
265 av_freep(&flush_buf);
266 return ret;
267 }
268 } else {
269 if (s->enc_last_frame < 0)
270 return 0;
271 flush_buf = av_calloc(avctx->frame_size, sizeof(*flush_buf));
272 if (!flush_buf)
273 return AVERROR(ENOMEM);
274 samples = flush_buf;
275 s->enc_last_frame = -1;
276 }
277
278 written = Encoder_Interface_Encode(s->enc_state, s->enc_mode, samples,
279 avpkt->data, 0);
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]);
282
283 /* Get the next frame pts/duration */
284 ret = ff_af_queue_remove(&s->afq, avctx->frame_size, avpkt);
285 if (ret < 0)
286 return ret;
287
288 avpkt->size = written;
289 *got_packet_ptr = 1;
290 av_freep(&flush_buf);
291 return 0;
292}
293
295 .p.name = "libopencore_amrnb",
296 CODEC_LONG_NAME("OpenCORE AMR-NB (Adaptive Multi-Rate Narrow-Band)"),
297 .p.type = AVMEDIA_TYPE_AUDIO,
298 .p.id = AV_CODEC_ID_AMR_NB,
299 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
301 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
302 .priv_data_size = sizeof(AMRContext),
303 .init = amr_nb_encode_init,
304 FF_CODEC_ENCODE_CB(amr_nb_encode_frame),
305 .close = amr_nb_encode_close,
307 .p.priv_class = &amrnb_class,
308};
309#endif /* CONFIG_LIBOPENCORE_AMRNB_ENCODER */
310
311#endif /* CONFIG_LIBOPENCORE_AMRNB */
312
313/* -----------AMR wideband ------------*/
314#if CONFIG_LIBOPENCORE_AMRWB_DECODER
315
316#include <opencore-amrwb/dec_if.h>
317#include <opencore-amrwb/if_rom.h>
318
319typedef struct AMRWBContext {
320 void *state;
322
323static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
324{
325 AMRWBContext *s = avctx->priv_data;
326 int ret;
327
328 if ((ret = amr_decode_fix_avctx(avctx)) < 0)
329 return ret;
330
331 s->state = D_IF_init();
332
333 return 0;
334}
335
336static int amr_wb_decode_frame(AVCodecContext *avctx, AVFrame *frame,
337 int *got_frame_ptr, AVPacket *avpkt)
338{
339 const uint8_t *buf = avpkt->data;
340 int buf_size = avpkt->size;
341 AMRWBContext *s = avctx->priv_data;
342 int mode, ret;
343 int packet_size;
344 static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
345
346 /* get output buffer */
347 frame->nb_samples = 320;
348 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
349 return ret;
350
351 mode = (buf[0] >> 3) & 0x000F;
352 packet_size = block_size[mode];
353
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;
358 }
359 if (!packet_size) {
360 av_log(avctx, AV_LOG_ERROR, "amr packet_size invalid\n");
361 return AVERROR_INVALIDDATA;
362 }
363
364 D_IF_decode(s->state, buf, (short *)frame->data[0], _good_frame);
365
366 *got_frame_ptr = 1;
367
368 return packet_size;
369}
370
371static int amr_wb_decode_close(AVCodecContext *avctx)
372{
373 AMRWBContext *s = avctx->priv_data;
374
375 D_IF_exit(s->state);
376 return 0;
377}
378
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,
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),
391};
392
393#endif /* CONFIG_LIBOPENCORE_AMRWB_DECODER */
const FFCodec ff_libopencore_amrnb_encoder
const FFCodec ff_libopencore_amrnb_decoder
const FFCodec ff_libopencore_amrwb_decoder
Mode
Frame type (Table 1a in 3GPP TS 26.101)
Definition amrnbdata.h:39
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Libavcodec external API header.
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition avstring.c:103
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_DECODE_CB(func)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
common internal and external API header
#define NULL
Definition coverity.c:32
#define abs(x)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition defs.h:61
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
mode
Use these values in ebur128_init (or'ed).
Definition ebur128.h:83
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
#define AV_OPT_FLAG_AUDIO_PARAM
Definition opt.h:356
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition opt.h:351
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition codec.h:84
@ AV_CODEC_ID_AMR_NB
Definition codec_id.h:434
@ AV_CODEC_ID_AMR_WB
Definition codec_id.h:435
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_cold
Definition attributes.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static const AVClass av_class
Definition options.c:133
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
#define snprintf
Definition snprintf.h:34
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition avcodec.h:1375
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int initial_padding
Audio only.
Definition avcodec.h:1114
int sample_rate
samples per second
Definition avcodec.h:1040
enum AVCodecID codec_id
Definition avcodec.h:453
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
Definition swscale.c:71
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
int64_t bitrate
Definition av1_levels.c:47
static const int rates[]
Definition swresample.c:99