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
23 * Adaptive Multi-Rate (AMR) Audio decoder stub.
25 * This code implements both an AMR-NarrowBand (AMR-NB) and an AMR-WideBand
26 * (AMR-WB) audio encoder/decoder through external reference code from
27 * http://www.3gpp.org/. The license of the code from 3gpp is unclear so you
28 * have to download the code separately.
32 * The float version (default) can be downloaded from:
33 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-610.zip
35 * \subsection Specification
36 * The specification for AMR-NB can be found in TS 26.071
37 * (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
38 * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
42 * The reference code can be downloaded from:
43 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip
45 * \subsection Specification
46 * The specification for AMR-WB can be found in TS 26.171
47 * (http://www.3gpp.org/ftp/Specs/html-info/26171.htm) and some other
48 * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
54 static void amr_decode_fix_avctx(AVCodecContext
*avctx
)
56 const int is_amr_wb
= 1 + (avctx
->codec_id
== CODEC_ID_AMR_WB
);
58 if (!avctx
->sample_rate
)
59 avctx
->sample_rate
= 8000 * is_amr_wb
;
64 avctx
->frame_size
= 160 * is_amr_wb
;
65 avctx
->sample_fmt
= SAMPLE_FMT_S16
;
70 #include <amrnb/interf_dec.h>
71 #include <amrnb/interf_enc.h>
73 static const char nb_bitrate_unsupported
[] =
74 "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n";
76 typedef struct AMR_bitrates
{
81 /* Match desired bitrate */
82 static int getBitrateMode(int bitrate
)
84 /* make the correspondance between bitrate and mode */
85 AMR_bitrates rates
[] = { { 4750, MR475
},
95 for (i
= 0; i
< 8; i
++)
96 if (rates
[i
].rate
== bitrate
)
98 /* no bitrate matching, return an error */
102 typedef struct AMRContext
{
109 static av_cold
int amr_nb_decode_init(AVCodecContext
*avctx
)
111 AMRContext
*s
= avctx
->priv_data
;
114 s
->decState
= Decoder_Interface_init();
116 av_log(avctx
, AV_LOG_ERROR
, "Decoder_Interface_init error\r\n");
120 amr_decode_fix_avctx(avctx
);
122 if (avctx
->channels
> 1) {
123 av_log(avctx
, AV_LOG_ERROR
, "amr_nb: multichannel decoding not supported\n");
130 static av_cold
int amr_nb_decode_close(AVCodecContext
*avctx
)
132 AMRContext
*s
= avctx
->priv_data
;
134 Decoder_Interface_exit(s
->decState
);
138 static int amr_nb_decode_frame(AVCodecContext
*avctx
, void *data
,
139 int *data_size
, AVPacket
*avpkt
)
141 const uint8_t *buf
= avpkt
->data
;
142 int buf_size
= avpkt
->size
;
143 AMRContext
*s
= avctx
->priv_data
;
144 const uint8_t *amrData
= buf
;
145 static const uint8_t block_size
[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
149 /* av_log(NULL, AV_LOG_DEBUG, "amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",
150 buf, buf_size, s->frameCount); */
152 dec_mode
= (buf
[0] >> 3) & 0x000F;
153 packet_size
= block_size
[dec_mode
] + 1;
155 if (packet_size
> buf_size
) {
156 av_log(avctx
, AV_LOG_ERROR
, "amr frame too short (%u, should be %u)\n",
157 buf_size
, packet_size
);
162 /* av_log(NULL, AV_LOG_DEBUG, "packet_size=%d amrData= 0x%X %X %X %X\n",
163 packet_size, amrData[0], amrData[1], amrData[2], amrData[3]); */
165 Decoder_Interface_Decode(s
->decState
, amrData
, data
, 0);
166 *data_size
= 160 * 2;
171 AVCodec libamr_nb_decoder
= {
180 .long_name
= NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
183 static av_cold
int amr_nb_encode_init(AVCodecContext
*avctx
)
185 AMRContext
*s
= avctx
->priv_data
;
189 if (avctx
->sample_rate
!= 8000) {
190 av_log(avctx
, AV_LOG_ERROR
, "Only 8000Hz sample rate supported\n");
194 if (avctx
->channels
!= 1) {
195 av_log(avctx
, AV_LOG_ERROR
, "Only mono supported\n");
199 avctx
->frame_size
= 160;
200 avctx
->coded_frame
= avcodec_alloc_frame();
202 s
->enstate
=Encoder_Interface_init(0);
204 av_log(avctx
, AV_LOG_ERROR
, "Encoder_Interface_init error\n");
208 if ((s
->enc_bitrate
= getBitrateMode(avctx
->bit_rate
)) < 0) {
209 av_log(avctx
, AV_LOG_ERROR
, nb_bitrate_unsupported
);
216 static av_cold
int amr_nb_encode_close(AVCodecContext
*avctx
)
218 AMRContext
*s
= avctx
->priv_data
;
220 Encoder_Interface_exit(s
->enstate
);
221 av_freep(&avctx
->coded_frame
);
225 static int amr_nb_encode_frame(AVCodecContext
*avctx
,
226 unsigned char *frame
/*out*/,
227 int buf_size
, void *data
/*in*/)
229 AMRContext
*s
= avctx
->priv_data
;
232 if ((s
->enc_bitrate
= getBitrateMode(avctx
->bit_rate
)) < 0) {
233 av_log(avctx
, AV_LOG_ERROR
, nb_bitrate_unsupported
);
237 written
= Encoder_Interface_Encode(s
->enstate
, s
->enc_bitrate
, data
,
239 /* av_log(NULL, AV_LOG_DEBUG, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
240 written, s->enc_bitrate, frame[0] ); */
245 AVCodec libamr_nb_encoder
= {
254 .sample_fmts
= (enum SampleFormat
[]){SAMPLE_FMT_S16
,SAMPLE_FMT_NONE
},
255 .long_name
= NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
260 /* -----------AMR wideband ------------*/
264 //To avoid duplicate typedefs from typedef in amr-nb
268 #include <amrwb/dec_if.h>
269 #include <amrwb/if_rom.h>
271 static const char wb_bitrate_unsupported
[] =
272 "bitrate not supported: use one of 6.6k, 8.85k, 12.65k, 14.25k, 15.85k, 18.25k, 19.85k, 23.05k, or 23.85k\n";
274 typedef struct AMRWB_bitrates
{
279 typedef struct AMRWBContext
{
286 #if CONFIG_LIBAMR_WB_ENCODER
288 #include <amrwb/enc_if.h>
290 static int getWBBitrateMode(int bitrate
)
292 /* make the correspondance between bitrate and mode */
293 AMRWB_bitrates rates
[] = { { 6600, 0},
304 for (i
= 0; i
< 9; i
++)
305 if (rates
[i
].rate
== bitrate
)
306 return rates
[i
].mode
;
307 /* no bitrate matching, return an error */
311 static av_cold
int amr_wb_encode_init(AVCodecContext
*avctx
)
313 AMRWBContext
*s
= avctx
->priv_data
;
317 if (avctx
->sample_rate
!= 16000) {
318 av_log(avctx
, AV_LOG_ERROR
, "Only 16000Hz sample rate supported\n");
322 if (avctx
->channels
!= 1) {
323 av_log(avctx
, AV_LOG_ERROR
, "Only mono supported\n");
327 if ((s
->mode
= getWBBitrateMode(avctx
->bit_rate
)) < 0) {
328 av_log(avctx
, AV_LOG_ERROR
, wb_bitrate_unsupported
);
332 avctx
->frame_size
= 320;
333 avctx
->coded_frame
= avcodec_alloc_frame();
335 s
->state
= E_IF_init();
341 static int amr_wb_encode_close(AVCodecContext
*avctx
)
343 AMRWBContext
*s
= avctx
->priv_data
;
346 av_freep(&avctx
->coded_frame
);
351 static int amr_wb_encode_frame(AVCodecContext
*avctx
,
352 unsigned char *frame
/*out*/,
353 int buf_size
, void *data
/*in*/)
355 AMRWBContext
*s
= avctx
->priv_data
;
358 if ((s
->mode
= getWBBitrateMode(avctx
->bit_rate
)) < 0) {
359 av_log(avctx
, AV_LOG_ERROR
, wb_bitrate_unsupported
);
362 size
= E_IF_encode(s
->state
, s
->mode
, data
, frame
, s
->allow_dtx
);
366 AVCodec libamr_wb_encoder
= {
370 sizeof(AMRWBContext
),
375 .sample_fmts
= (enum SampleFormat
[]){SAMPLE_FMT_S16
,SAMPLE_FMT_NONE
},
376 .long_name
= NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
381 static av_cold
int amr_wb_decode_init(AVCodecContext
*avctx
)
383 AMRWBContext
*s
= avctx
->priv_data
;
386 s
->state
= D_IF_init();
388 amr_decode_fix_avctx(avctx
);
390 if (avctx
->channels
> 1) {
391 av_log(avctx
, AV_LOG_ERROR
, "amr_wb: multichannel decoding not supported\n");
398 static int amr_wb_decode_frame(AVCodecContext
*avctx
, void *data
,
399 int *data_size
, AVPacket
*avpkt
)
401 const uint8_t *buf
= avpkt
->data
;
402 int buf_size
= avpkt
->size
;
403 AMRWBContext
*s
= avctx
->priv_data
;
404 const uint8_t *amrData
= buf
;
407 static const uint8_t block_size
[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
413 mode
= (amrData
[0] >> 3) & 0x000F;
414 packet_size
= block_size
[mode
];
416 if (packet_size
> buf_size
) {
417 av_log(avctx
, AV_LOG_ERROR
, "amr frame too short (%u, should be %u)\n",
418 buf_size
, packet_size
+ 1);
423 D_IF_decode(s
->state
, amrData
, data
, _good_frame
);
424 *data_size
= 320 * 2;
428 static int amr_wb_decode_close(AVCodecContext
*avctx
)
430 AMRWBContext
*s
= avctx
->priv_data
;
436 AVCodec libamr_wb_decoder
= {
440 sizeof(AMRWBContext
),
445 .long_name
= NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
448 #endif //CONFIG_LIBAMR_WB