]> git.sesse.net Git - ffmpeg/blob - libavcodec/amr.c
AMR-NB audio support patch by (<joca at rixmail dot se>)
[ffmpeg] / 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 //#define DEBUG
20 #define MMS_IO
21 #include "avcodec.h"
22
23 #include "amr/sp_dec.h"
24 #include "amr/d_homing.h"
25 #include "amr/typedef.h"
26
27 /* frame size in serial bitstream file (frame type + serial stream + flags) */
28 #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
29
30 typedef struct AMRDecodeContext {
31     int frameCount;
32     Speech_Decode_FrameState *speech_decoder_state;
33     enum RXFrameType rx_type;
34     enum Mode mode;
35     Word16 reset_flag;
36     Word16 reset_flag_old;
37
38 } AMRDecodeContext;
39
40 static int amr_nb_decode_init(AVCodecContext * avctx)
41 {
42     AMRDecodeContext *s = avctx->priv_data;
43     s->frameCount=0;
44     s->speech_decoder_state=NULL;
45     s->rx_type = (enum RXFrameType)0;
46     s->mode= (enum Mode)0;
47     s->reset_flag=0;
48     s->reset_flag_old=1;
49     
50     if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
51     {
52         printf("error\r\n");
53         return -1;
54     }
55     return 0;
56 }
57
58 static int amr_decode_close(AVCodecContext * avctx)
59 {
60     AMRDecodeContext *s = avctx->priv_data;
61     Speech_Decode_Frame_exit(&s->speech_decoder_state);
62 }
63
64 static int amr_nb_decode_frame(AVCodecContext * avctx,
65             void *data, int *data_size,
66             uint8_t * buf, int buf_size)
67 {
68     AMRDecodeContext *s = avctx->priv_data;
69
70     uint8_t*amrData=buf;
71     int offset=0;
72
73     UWord8 toc, q, ft;
74     
75     Word16 serial[SERIAL_FRAMESIZE];   /* coded bits */
76     Word16 *synth;
77     UWord8 *packed_bits;
78
79     static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
80     int i;
81
82     //printf("amr_decode_frame data=0x%X data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",data,&data_size,buf,buf_size,s->frameCount);
83
84     synth=data;
85
86     while(offset<buf_size)
87     {
88         toc=amrData[offset++];
89         /* read rest of the frame based on ToC byte */
90         q  = (toc >> 2) & 0x01;
91         ft = (toc >> 3) & 0x0F;
92
93         packed_bits=amrData+offset;
94
95         offset+=packed_size[ft];
96
97                 //Unsort and unpack bits
98         s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
99
100                 //We have a new frame
101         s->frameCount++;
102
103         if (s->rx_type == RX_NO_DATA) 
104         {
105             s->mode = s->speech_decoder_state->prev_mode;
106         }
107         else {
108             s->speech_decoder_state->prev_mode = s->mode;
109         }
110         
111         /* if homed: check if this frame is another homing frame */
112         if (s->reset_flag_old == 1)
113         {
114             /* only check until end of first subframe */
115             s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
116         }
117         /* produce encoder homing frame if homed & input=decoder homing frame */
118         if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
119         {
120             for (i = 0; i < L_FRAME; i++)
121             {
122                 synth[i] = EHF_MASK;
123             }
124         }
125         else
126         {     
127             /* decode frame */
128             Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
129         }
130
131                 //Each AMR-frame results in 160 16-bit samples
132         *data_size+=160*2;
133         synth+=160;
134         
135         /* if not homed: check whether current frame is a homing frame */
136         if (s->reset_flag_old == 0)
137         {
138             /* check whole frame */
139             s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
140         }
141         /* reset decoder if current frame is a homing frame */
142         if (s->reset_flag != 0)
143         {
144             Speech_Decode_Frame_reset(s->speech_decoder_state);
145         }
146         s->reset_flag_old = s->reset_flag;
147         
148     }
149     return buf_size;
150 }
151
152 AVCodec amr_nb_decoder =
153 {
154     "amr_nb",
155     CODEC_TYPE_AUDIO,
156     CODEC_ID_AMR_NB,
157     sizeof(AMRDecodeContext),
158     amr_nb_decode_init,
159     NULL,
160     amr_decode_close,
161     amr_nb_decode_frame,
162 };
163