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 /** @file
23 * Adaptive Multi-Rate (AMR) Audio decoder stub.
24 *
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.
29 *
30 * \section AMR-NB
31 *
32 * The float version (default) can be downloaded from:
33 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-610.zip
34 *
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.
39 *
40 * \section AMR-WB
41 *
42 * The reference code can be downloaded from:
43 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip
44 *
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.
49 *
50 */
51
52#include "avcodec.h"
53
54static void amr_decode_fix_avctx(AVCodecContext *avctx)
55{
56    const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
57
58    if (!avctx->sample_rate)
59        avctx->sample_rate = 8000 * is_amr_wb;
60
61    if (!avctx->channels)
62        avctx->channels = 1;
63
64    avctx->frame_size = 160 * is_amr_wb;
65    avctx->sample_fmt = SAMPLE_FMT_S16;
66}
67
68#if CONFIG_LIBAMR_NB
69
70#include <amrnb/interf_dec.h>
71#include <amrnb/interf_enc.h>
72
73static 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";
75
76typedef struct AMR_bitrates {
77    int       rate;
78    enum Mode mode;
79} AMR_bitrates;
80
81/* Match desired bitrate */
82static int getBitrateMode(int bitrate)
83{
84    /* make the correspondance between bitrate and mode */
85    AMR_bitrates rates[] = { { 4750, MR475},
86                             { 5150, MR515},
87                             { 5900, MR59},
88                             { 6700, MR67},
89                             { 7400, MR74},
90                             { 7950, MR795},
91                             {10200, MR102},
92                             {12200, MR122}, };
93    int i;
94
95    for (i = 0; i < 8; i++)
96        if (rates[i].rate == bitrate)
97            return rates[i].mode;
98    /* no bitrate matching, return an error */
99    return -1;
100}
101
102typedef struct AMRContext {
103    int   frameCount;
104    void *decState;
105    int  *enstate;
106    int   enc_bitrate;
107} AMRContext;
108
109static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
110{
111    AMRContext *s = avctx->priv_data;
112
113    s->frameCount = 0;
114    s->decState   = Decoder_Interface_init();
115    if (!s->decState) {
116        av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
117        return -1;
118    }
119
120    amr_decode_fix_avctx(avctx);
121
122    if (avctx->channels > 1) {
123        av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
124        return -1;
125    }
126
127    return 0;
128}
129
130static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
131{
132    AMRContext *s = avctx->priv_data;
133
134    Decoder_Interface_exit(s->decState);
135    return 0;
136}
137
138static int amr_nb_decode_frame(AVCodecContext *avctx, void *data,
139                               int *data_size,
140                               const uint8_t *buf, int buf_size)
141{
142    AMRContext *s = avctx->priv_data;
143    const uint8_t *amrData = buf;
144    static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
145    enum Mode dec_mode;
146    int packet_size;
147
148    /* av_log(NULL, AV_LOG_DEBUG, "amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",
149              buf, buf_size, s->frameCount); */
150
151    dec_mode = (buf[0] >> 3) & 0x000F;
152    packet_size = block_size[dec_mode] + 1;
153
154    if (packet_size > buf_size) {
155        av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
156               buf_size, packet_size);
157        return -1;
158    }
159
160    s->frameCount++;
161    /* av_log(NULL, AV_LOG_DEBUG, "packet_size=%d amrData= 0x%X %X %X %X\n",
162              packet_size, amrData[0], amrData[1], amrData[2], amrData[3]); */
163    /* call decoder */
164    Decoder_Interface_Decode(s->decState, amrData, data, 0);
165    *data_size = 160 * 2;
166
167    return packet_size;
168}
169
170AVCodec libamr_nb_decoder = {
171    "libamr_nb",
172    CODEC_TYPE_AUDIO,
173    CODEC_ID_AMR_NB,
174    sizeof(AMRContext),
175    amr_nb_decode_init,
176    NULL,
177    amr_nb_decode_close,
178    amr_nb_decode_frame,
179    .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
180};
181
182static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
183{
184    AMRContext *s = avctx->priv_data;
185
186    s->frameCount = 0;
187
188    if (avctx->sample_rate != 8000) {
189        av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
190        return -1;
191    }
192
193    if (avctx->channels != 1) {
194        av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
195        return -1;
196    }
197
198    avctx->frame_size  = 160;
199    avctx->coded_frame = avcodec_alloc_frame();
200
201    s->enstate=Encoder_Interface_init(0);
202    if (!s->enstate) {
203        av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
204        return -1;
205    }
206
207    if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
208        av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
209        return -1;
210    }
211
212    return 0;
213}
214
215static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
216{
217    AMRContext *s = avctx->priv_data;
218
219    Encoder_Interface_exit(s->enstate);
220    av_freep(&avctx->coded_frame);
221    return 0;
222}
223
224static int amr_nb_encode_frame(AVCodecContext *avctx,
225                               unsigned char *frame/*out*/,
226                               int buf_size, void *data/*in*/)
227{
228    AMRContext *s = avctx->priv_data;
229    int written;
230
231    if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
232        av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
233        return -1;
234    }
235
236    written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data,
237                                       frame, 0);
238    /* av_log(NULL, AV_LOG_DEBUG, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
239              written, s->enc_bitrate, frame[0] ); */
240
241    return written;
242}
243
244AVCodec libamr_nb_encoder = {
245    "libamr_nb",
246    CODEC_TYPE_AUDIO,
247    CODEC_ID_AMR_NB,
248    sizeof(AMRContext),
249    amr_nb_encode_init,
250    amr_nb_encode_frame,
251    amr_nb_encode_close,
252    NULL,
253    .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
254    .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
255};
256
257#endif
258
259/* -----------AMR wideband ------------*/
260#if CONFIG_LIBAMR_WB
261
262#ifdef _TYPEDEF_H
263//To avoid duplicate typedefs from typedef in amr-nb
264#define typedef_h
265#endif
266
267#include <amrwb/dec_if.h>
268#include <amrwb/if_rom.h>
269
270static const char wb_bitrate_unsupported[] =
271    "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";
272
273typedef struct AMRWB_bitrates {
274    int rate;
275    int mode;
276} AMRWB_bitrates;
277
278typedef struct AMRWBContext {
279    int    frameCount;
280    void  *state;
281    int    mode;
282    Word16 allow_dtx;
283} AMRWBContext;
284
285#if CONFIG_LIBAMR_WB_ENCODER
286
287#include <amrwb/enc_if.h>
288
289static int getWBBitrateMode(int bitrate)
290{
291    /* make the correspondance between bitrate and mode */
292    AMRWB_bitrates rates[] = { { 6600, 0},
293                               { 8850, 1},
294                               {12650, 2},
295                               {14250, 3},
296                               {15850, 4},
297                               {18250, 5},
298                               {19850, 6},
299                               {23050, 7},
300                               {23850, 8}, };
301    int i;
302
303    for (i = 0; i < 9; i++)
304        if (rates[i].rate == bitrate)
305            return rates[i].mode;
306    /* no bitrate matching, return an error */
307    return -1;
308}
309
310static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
311{
312    AMRWBContext *s = avctx->priv_data;
313
314    s->frameCount = 0;
315
316    if (avctx->sample_rate != 16000) {
317        av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
318        return -1;
319    }
320
321    if (avctx->channels != 1) {
322        av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
323        return -1;
324    }
325
326    if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
327        av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
328        return -1;
329    }
330
331    avctx->frame_size  = 320;
332    avctx->coded_frame = avcodec_alloc_frame();
333
334    s->state     = E_IF_init();
335    s->allow_dtx = 0;
336
337    return 0;
338}
339
340static int amr_wb_encode_close(AVCodecContext *avctx)
341{
342    AMRWBContext *s = avctx->priv_data;
343
344    E_IF_exit(s->state);
345    av_freep(&avctx->coded_frame);
346    s->frameCount++;
347    return 0;
348}
349
350static int amr_wb_encode_frame(AVCodecContext *avctx,
351                               unsigned char *frame/*out*/,
352                               int buf_size, void *data/*in*/)
353{
354    AMRWBContext *s = avctx->priv_data;
355    int size;
356
357    if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
358        av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
359        return -1;
360    }
361    size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
362    return size;
363}
364
365AVCodec libamr_wb_encoder = {
366    "libamr_wb",
367    CODEC_TYPE_AUDIO,
368    CODEC_ID_AMR_WB,
369    sizeof(AMRWBContext),
370    amr_wb_encode_init,
371    amr_wb_encode_frame,
372    amr_wb_encode_close,
373    NULL,
374    .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
375    .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
376};
377
378#endif
379
380static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
381{
382    AMRWBContext *s = avctx->priv_data;
383
384    s->frameCount = 0;
385    s->state      = D_IF_init();
386
387    amr_decode_fix_avctx(avctx);
388
389    if (avctx->channels > 1) {
390        av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
391        return -1;
392    }
393
394    return 0;
395}
396
397static int amr_wb_decode_frame(AVCodecContext *avctx,
398                               void *data, int *data_size,
399                               const uint8_t *buf, int buf_size)
400{
401    AMRWBContext *s = avctx->priv_data;
402    const uint8_t *amrData = buf;
403    int mode;
404    int packet_size;
405    static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
406
407    if (!buf_size)
408        /* nothing to do */
409        return 0;
410
411    mode = (amrData[0] >> 3) & 0x000F;
412    packet_size = block_size[mode];
413
414    if (packet_size > buf_size) {
415        av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
416               buf_size, packet_size + 1);
417        return -1;
418    }
419
420    s->frameCount++;
421    D_IF_decode(s->state, amrData, data, _good_frame);
422    *data_size = 320 * 2;
423    return packet_size;
424}
425
426static int amr_wb_decode_close(AVCodecContext *avctx)
427{
428    AMRWBContext *s = avctx->priv_data;
429
430    D_IF_exit(s->state);
431    return 0;
432}
433
434AVCodec libamr_wb_decoder = {
435    "libamr_wb",
436    CODEC_TYPE_AUDIO,
437    CODEC_ID_AMR_WB,
438    sizeof(AMRWBContext),
439    amr_wb_decode_init,
440    NULL,
441    amr_wb_decode_close,
442    amr_wb_decode_frame,
443    .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
444};
445
446#endif //CONFIG_LIBAMR_WB
447