2 * AMR Audio decoder stub
3 * Copyright (c) 2003 the ffmpeg project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 This code implements amr-nb audio encoder/decoder through external reference
21 code from www.3gpp.org. The licence of the code from 3gpp is unclear so you
22 have to download the code separately. Two versions exists: One fixed-point
23 and one with floats. For some reason the float-encoder is significant faster
24 atleast on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip at MR102).
26 The fixed-point (TS26.073) can be downloaded from:
27 http://www.3gpp.org/ftp/Specs/latest/Rel-5/26_series/26073-510.zip
28 Extract the soure into ffmpeg/libavcodec/amr
29 To use the float version run "./configure" with "--enable-amr-nb-fixed"
31 The float version (default) can be downloaded from:
32 http://www.3gpp.org/ftp/Specs/latest/Rel-5/26_series/26104-510.zip
33 Extract the soure into ffmpeg/libavcodec/amr_float
35 The specification for amr-nb can be found in TS 26.071
36 (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
37 info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm
39 In the future support for AMR-WB might also be included here.
40 Reference code exist in TS26.173 and TS 26.204.
44 //#define AMR_NB_FIXED
45 #include "../config.h"
52 #include "amr/sp_dec.h"
53 #include "amr/d_homing.h"
54 #include "amr/typedef.h"
55 #include "amr/sp_enc.h"
56 #include "amr/sid_sync.h"
57 #include "amr/e_homing.h"
60 #include "amr_float/interf_dec.h"
61 #include "amr_float/interf_enc.h"
64 /* Common code for fixed and float version*/
65 typedef struct AMR_bitrates
73 /* Match desired bitrate with closest one*/
74 static enum Mode getBitrateMode(int bitrate)
76 /* Adjusted so that all bitrates can be used from commandline where
77 only a multiple of 1000 can be specified*/
78 AMR_bitrates rates[]={ {0,4999,MR475}, //4
84 {10000,11999,MR102},//10
85 {12000,64000,MR122},//12
89 for(i=0;i<sizeof(rates);i++)
91 if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
93 return(rates[i].mode);
96 /*Return highest possible*/
101 /* fixed point version*/
102 /* frame size in serial bitstream file (frame type + serial stream + flags) */
103 #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
105 typedef struct AMRContext {
107 Speech_Decode_FrameState *speech_decoder_state;
108 enum RXFrameType rx_type;
111 Word16 reset_flag_old;
113 enum Mode enc_bitrate;
114 Speech_Encode_FrameState *enstate;
115 sid_syncState *sidstate;
116 enum TXFrameType tx_frametype;
121 static int amr_nb_decode_init(AVCodecContext * avctx)
123 AMRContext *s = avctx->priv_data;
125 s->speech_decoder_state=NULL;
126 s->rx_type = (enum RXFrameType)0;
127 s->mode= (enum Mode)0;
131 if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
133 printf("Speech_Decode_Frame_init error\n");
139 static int amr_nb_encode_init(AVCodecContext * avctx)
141 AMRContext *s = avctx->priv_data;
143 s->speech_decoder_state=NULL;
144 s->rx_type = (enum RXFrameType)0;
145 s->mode= (enum Mode)0;
149 if(avctx->sample_rate!=8000)
152 printf("Only 8000Hz sample rate supported\n");
157 if(avctx->channels!=1)
160 printf("Only mono supported\n");
165 avctx->frame_size=160;
166 avctx->coded_frame= avcodec_alloc_frame();
168 if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
171 printf("Speech_Encode_Frame_init error\n");
176 s->enc_bitrate=getBitrateMode(avctx->bit_rate);
181 static int amr_nb_encode_close(AVCodecContext * avctx)
183 AMRContext *s = avctx->priv_data;
184 Speech_Encode_Frame_exit(&s->enstate);
185 sid_sync_exit (&s->sidstate);
186 av_freep(&avctx->coded_frame);
190 static int amr_nb_decode_close(AVCodecContext * avctx)
192 AMRContext *s = avctx->priv_data;
193 Speech_Decode_Frame_exit(&s->speech_decoder_state);
197 static int amr_nb_decode_frame(AVCodecContext * avctx,
198 void *data, int *data_size,
199 uint8_t * buf, int buf_size)
201 AMRContext *s = avctx->priv_data;
208 Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
213 static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
216 //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
220 // while(offset<buf_size)
223 /* read rest of the frame based on ToC byte */
224 q = (toc >> 2) & 0x01;
225 ft = (toc >> 3) & 0x0F;
227 //printf("offset=%d, packet_size=%d amrData= 0x%X %X %X %X\n",offset,packed_size[ft],amrData[offset],amrData[offset+1],amrData[offset+2],amrData[offset+3]);
231 packed_bits=amrData+offset;
233 offset+=packed_size[ft];
235 //Unsort and unpack bits
236 s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
238 //We have a new frame
241 if (s->rx_type == RX_NO_DATA)
243 s->mode = s->speech_decoder_state->prev_mode;
246 s->speech_decoder_state->prev_mode = s->mode;
249 /* if homed: check if this frame is another homing frame */
250 if (s->reset_flag_old == 1)
252 /* only check until end of first subframe */
253 s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
255 /* produce encoder homing frame if homed & input=decoder homing frame */
256 if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
258 for (i = 0; i < L_FRAME; i++)
266 Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
269 //Each AMR-frame results in 160 16-bit samples
273 /* if not homed: check whether current frame is a homing frame */
274 if (s->reset_flag_old == 0)
276 /* check whole frame */
277 s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
279 /* reset decoder if current frame is a homing frame */
280 if (s->reset_flag != 0)
282 Speech_Decode_Frame_reset(s->speech_decoder_state);
284 s->reset_flag_old = s->reset_flag;
291 static int amr_nb_encode_frame(AVCodecContext *avctx,
292 unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
294 short serial_data[250] = {0};
296 AMRContext *s = avctx->priv_data;
299 s->reset_flag = encoder_homing_frame_test(data);
301 Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode);
303 /* add frame type and mode */
304 sid_sync (s->sidstate, s->mode, &s->tx_frametype);
306 written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
308 if (s->reset_flag != 0)
310 Speech_Encode_Frame_reset(s->enstate);
311 sid_sync_reset(s->sidstate);
317 #else /* Float point version*/
319 typedef struct AMRContext {
323 enum Mode enc_bitrate;
326 static int amr_nb_decode_init(AVCodecContext * avctx)
328 AMRContext *s = avctx->priv_data;
330 s->decState=Decoder_Interface_init();
333 printf("Decoder_Interface_init error\r\n");
339 static int amr_nb_encode_init(AVCodecContext * avctx)
341 AMRContext *s = avctx->priv_data;
344 if(avctx->sample_rate!=8000)
347 printf("Only 8000Hz sample rate supported\n");
352 if(avctx->channels!=1)
355 printf("Only mono supported\n");
360 avctx->frame_size=160;
361 avctx->coded_frame= avcodec_alloc_frame();
363 s->enstate=Encoder_Interface_init(0);
366 printf("Encoder_Interface_init error\n");
370 s->enc_bitrate=getBitrateMode(avctx->bit_rate);
375 static int amr_nb_decode_close(AVCodecContext * avctx)
377 AMRContext *s = avctx->priv_data;
378 Decoder_Interface_exit(s->decState);
382 static int amr_nb_encode_close(AVCodecContext * avctx)
384 AMRContext *s = avctx->priv_data;
385 Encoder_Interface_exit(s->enstate);
386 av_freep(&avctx->coded_frame);
390 static int amr_nb_decode_frame(AVCodecContext * avctx,
391 void *data, int *data_size,
392 uint8_t * buf, int buf_size)
394 AMRContext *s = (AMRContext*)avctx->priv_data;
398 static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
403 //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
405 while(offset<buf_size)
407 dec_mode = (amrData[offset] >> 3) & 0x000F;
408 packet_size = block_size[dec_mode];
411 //printf("offset=%d, packet_size=%d amrData= 0x%X %X %X %X\n",offset,packet_size,amrData[offset],amrData[offset+1],amrData[offset+2],amrData[offset+3]);
413 Decoder_Interface_Decode(s->decState, &amrData[offset], data+*data_size, 0);
416 offset+=packet_size+1;
421 static int amr_nb_encode_frame(AVCodecContext *avctx,
422 unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
424 AMRContext *s = (AMRContext*)avctx->priv_data;
427 written = Encoder_Interface_Encode(s->enstate,
438 AVCodec amr_nb_decoder =
450 AVCodec amr_nb_encoder =