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