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