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