Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / modules / mod_libavcodec / libavcodec / amr.c
1 /*
2  * AMR Audio decoder stub
3  * Copyright (c) 2003 the ffmpeg project
4  *
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.
9  *
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.
14  *
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
18  */
19  /*
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).
25     
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"
30     
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
34     
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
38     
39     In the future support for AMR-WB might also be included here.
40     Reference code exist in TS26.173 and TS 26.204.
41  
42  */
43 #define DEBUG
44 //#define AMR_NB_FIXED
45 #include "../config.h"
46 #include "avcodec.h"
47
48 #ifdef AMR_NB_FIXED
49
50 #define MMS_IO
51
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"
58
59 #else
60 #include "amr_float/interf_dec.h"
61 #include "amr_float/interf_enc.h"
62 #endif
63
64 /* Common code for fixed and float version*/
65 typedef struct AMR_bitrates
66 {
67     int startrate;
68     int stoprate;
69     enum Mode mode;
70     
71 } AMR_bitrates;
72
73 /* Match desired bitrate with closest one*/
74 static enum Mode getBitrateMode(int bitrate)
75 {
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
79                            {5000,5899,MR515},//5
80                            {5900,6699,MR59},//6
81                            {6700,7000,MR67},//7
82                            {7001,7949,MR74},//8
83                            {7950,9999,MR795},//9
84                            {10000,11999,MR102},//10
85                            {12000,64000,MR122},//12
86                            
87                          };
88     int i;
89     for(i=0;i<sizeof(rates);i++)
90     {
91         if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
92         {
93             return(rates[i].mode);
94         }
95     }
96     /*Return highest possible*/
97     return(MR122);
98 }
99
100 #ifdef AMR_NB_FIXED
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)
104
105 typedef struct AMRContext {
106     int frameCount;
107     Speech_Decode_FrameState *speech_decoder_state;
108     enum RXFrameType rx_type;
109     enum Mode mode;
110     Word16 reset_flag;
111     Word16 reset_flag_old;
112
113     enum Mode enc_bitrate;
114     Speech_Encode_FrameState *enstate;
115     sid_syncState *sidstate;
116     enum TXFrameType tx_frametype;
117     
118
119 } AMRContext;
120
121 static int amr_nb_decode_init(AVCodecContext * avctx)
122 {
123     AMRContext *s = avctx->priv_data;
124     s->frameCount=0;
125     s->speech_decoder_state=NULL;
126     s->rx_type = (enum RXFrameType)0;
127     s->mode= (enum Mode)0;
128     s->reset_flag=0;
129     s->reset_flag_old=1;
130     
131     if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
132     {
133         printf("Speech_Decode_Frame_init error\n");
134         return -1;
135     }
136     return 0;
137 }
138
139 static int amr_nb_encode_init(AVCodecContext * avctx)
140 {
141     AMRContext *s = avctx->priv_data;
142     s->frameCount=0;
143     s->speech_decoder_state=NULL;
144     s->rx_type = (enum RXFrameType)0;
145     s->mode= (enum Mode)0;
146     s->reset_flag=0;
147     s->reset_flag_old=1;
148     
149     if(avctx->sample_rate!=8000)
150     {
151 #ifdef DEBUG
152         printf("Only 8000Hz sample rate supported\n");
153 #endif
154         return -1;
155     }
156
157     if(avctx->channels!=1)
158     {
159 #ifdef DEBUG
160         printf("Only mono supported\n");
161 #endif
162         return -1;
163     }
164
165     avctx->frame_size=160;
166     avctx->coded_frame= avcodec_alloc_frame();
167
168     if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
169     {
170 #ifdef DEBUG
171         printf("Speech_Encode_Frame_init error\n");
172 #endif
173         return -1;
174     }
175
176     s->enc_bitrate=getBitrateMode(avctx->bit_rate);
177
178     return 0;
179 }
180
181 static int amr_nb_encode_close(AVCodecContext * avctx)
182 {
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);
187     return 0;
188 }
189
190 static int amr_nb_decode_close(AVCodecContext * avctx)
191 {
192     AMRContext *s = avctx->priv_data;
193     Speech_Decode_Frame_exit(&s->speech_decoder_state);
194     return 0;
195 }
196
197 static int amr_nb_decode_frame(AVCodecContext * avctx,
198             void *data, int *data_size,
199             uint8_t * buf, int buf_size)
200 {
201     AMRContext *s = avctx->priv_data;
202
203     uint8_t*amrData=buf;
204     int offset=0;
205
206     UWord8 toc, q, ft;
207     
208     Word16 serial[SERIAL_FRAMESIZE];   /* coded bits */
209     Word16 *synth;
210     UWord8 *packed_bits;
211     *data_size=0;
212
213     static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
214     int i;
215
216     //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
217
218     synth=data;
219
220 //    while(offset<buf_size)
221     {
222         toc=amrData[offset];
223         /* read rest of the frame based on ToC byte */
224         q  = (toc >> 2) & 0x01;
225         ft = (toc >> 3) & 0x0F;
226
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]);
228
229         offset++;
230
231         packed_bits=amrData+offset;
232
233         offset+=packed_size[ft];
234
235         //Unsort and unpack bits
236         s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
237
238         //We have a new frame
239         s->frameCount++;
240
241         if (s->rx_type == RX_NO_DATA) 
242         {
243             s->mode = s->speech_decoder_state->prev_mode;
244         }
245         else {
246             s->speech_decoder_state->prev_mode = s->mode;
247         }
248         
249         /* if homed: check if this frame is another homing frame */
250         if (s->reset_flag_old == 1)
251         {
252             /* only check until end of first subframe */
253             s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
254         }
255         /* produce encoder homing frame if homed & input=decoder homing frame */
256         if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
257         {
258             for (i = 0; i < L_FRAME; i++)
259             {
260                 synth[i] = EHF_MASK;
261             }
262         }
263         else
264         {     
265             /* decode frame */
266             Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
267         }
268
269         //Each AMR-frame results in 160 16-bit samples
270         *data_size+=160*2;
271         synth+=160;
272         
273         /* if not homed: check whether current frame is a homing frame */
274         if (s->reset_flag_old == 0)
275         {
276             /* check whole frame */
277             s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
278         }
279         /* reset decoder if current frame is a homing frame */
280         if (s->reset_flag != 0)
281         {
282             Speech_Decode_Frame_reset(s->speech_decoder_state);
283         }
284         s->reset_flag_old = s->reset_flag;
285         
286     }
287     return offset;
288 }
289
290
291 static int amr_nb_encode_frame(AVCodecContext *avctx,
292                             unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
293 {
294     short serial_data[250] = {0};
295
296     AMRContext *s = avctx->priv_data;
297     int written;
298    
299     s->reset_flag = encoder_homing_frame_test(data);
300     
301     Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode); 
302     
303     /* add frame type and mode */
304     sid_sync (s->sidstate, s->mode, &s->tx_frametype);
305     
306     written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
307     
308     if (s->reset_flag != 0)
309     {
310         Speech_Encode_Frame_reset(s->enstate);
311         sid_sync_reset(s->sidstate);
312     }
313     return written;
314 }
315
316
317 #else /* Float point version*/
318
319 typedef struct AMRContext {
320     int frameCount;
321     void * decState;
322     int *enstate;
323     enum Mode enc_bitrate;
324 } AMRContext;
325
326 static int amr_nb_decode_init(AVCodecContext * avctx)
327 {
328     AMRContext *s = avctx->priv_data;
329     s->frameCount=0;
330     s->decState=Decoder_Interface_init();
331     if(!s->decState)
332     {
333         printf("Decoder_Interface_init error\r\n");
334         return -1;
335     }
336     return 0;
337 }
338
339 static int amr_nb_encode_init(AVCodecContext * avctx)
340 {
341     AMRContext *s = avctx->priv_data;
342     s->frameCount=0;
343     
344     if(avctx->sample_rate!=8000)
345     {
346 #ifdef DEBUG
347         printf("Only 8000Hz sample rate supported\n");
348 #endif
349         return -1;
350     }
351
352     if(avctx->channels!=1)
353     {
354 #ifdef DEBUG
355         printf("Only mono supported\n");
356 #endif
357         return -1;
358     }
359
360     avctx->frame_size=160;
361     avctx->coded_frame= avcodec_alloc_frame();
362
363     s->enstate=Encoder_Interface_init(0);
364     if(!s->enstate)
365     {
366         printf("Encoder_Interface_init error\n");
367         return -1;
368     }
369
370     s->enc_bitrate=getBitrateMode(avctx->bit_rate);
371
372     return 0;
373 }
374
375 static int amr_nb_decode_close(AVCodecContext * avctx)
376 {
377     AMRContext *s = avctx->priv_data;
378     Decoder_Interface_exit(s->decState);
379     return 0;
380 }
381
382 static int amr_nb_encode_close(AVCodecContext * avctx)
383 {
384     AMRContext *s = avctx->priv_data;
385     Encoder_Interface_exit(s->enstate);
386     av_freep(&avctx->coded_frame);
387     return 0;
388 }
389
390 static int amr_nb_decode_frame(AVCodecContext * avctx,
391             void *data, int *data_size,
392             uint8_t * buf, int buf_size)
393 {
394     AMRContext *s = (AMRContext*)avctx->priv_data;
395
396     uint8_t*amrData=buf;
397     int offset=0;
398     static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
399     enum Mode dec_mode;
400     int packet_size;
401     *data_size=0;
402
403     //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
404
405     while(offset<buf_size)
406     {
407         dec_mode = (amrData[offset] >> 3) & 0x000F;
408         packet_size = block_size[dec_mode];
409     
410         s->frameCount++;
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]);
412         /* call decoder */
413         Decoder_Interface_Decode(s->decState, &amrData[offset], data+*data_size, 0);
414         *data_size+=160*2;
415    
416         offset+=packet_size+1; 
417     }
418     return buf_size;
419 }
420
421 static int amr_nb_encode_frame(AVCodecContext *avctx,
422                             unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
423 {
424     AMRContext *s = (AMRContext*)avctx->priv_data;
425     int written;
426
427     written = Encoder_Interface_Encode(s->enstate, 
428         s->enc_bitrate, 
429         data, 
430         frame, 
431         0);
432
433     return written;
434 }
435
436 #endif
437
438 AVCodec amr_nb_decoder =
439 {
440     "amr_nb",
441     CODEC_TYPE_AUDIO,
442     CODEC_ID_AMR_NB,
443     sizeof(AMRContext),
444     amr_nb_decode_init,
445     NULL,
446     amr_nb_decode_close,
447     amr_nb_decode_frame,
448 };
449
450 AVCodec amr_nb_encoder =
451 {
452     "amr_nb",
453     CODEC_TYPE_AUDIO,
454     CODEC_ID_AMR_NB,
455     sizeof(AMRContext),
456     amr_nb_encode_init,
457     amr_nb_encode_frame,
458     amr_nb_encode_close,
459     NULL,
460 };