Mercurial > libavcodec.hg
annotate amr.c @ 1417:89dacc2b9bf0 libavcodec
avoid warning
author | bellard |
---|---|
date | Sun, 24 Aug 2003 22:15:37 +0000 |
parents | 1db67cd7808a |
children | f22010affdce |
rev | line source |
---|---|
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
1 /* |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
2 * AMR Audio decoder stub |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
3 * Copyright (c) 2003 the ffmpeg project |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
4 * |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
5 * This library is free software; you can redistribute it and/or |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
6 * modify it under the terms of the GNU Lesser General Public |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
7 * License as published by the Free Software Foundation; either |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
8 * version 2 of the License, or (at your option) any later version. |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
9 * |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
10 * This library is distributed in the hope that it will be useful, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
13 * Lesser General Public License for more details. |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
14 * |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
15 * You should have received a copy of the GNU Lesser General Public |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
16 * License along with this library; if not, write to the Free Software |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
18 */ |
1297 | 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 | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
50 #define MMS_IO |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
51 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
52 #include "amr/sp_dec.h" |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
53 #include "amr/d_homing.h" |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
54 #include "amr/typedef.h" |
1297 | 55 #include "amr/sp_enc.h" |
56 #include "amr/sid_sync.h" | |
57 #include "amr/e_homing.h" | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
58 |
1297 | 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*/ | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
102 /* frame size in serial bitstream file (frame type + serial stream + flags) */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
103 #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
104 |
1297 | 105 typedef struct AMRContext { |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
106 int frameCount; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
107 Speech_Decode_FrameState *speech_decoder_state; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
108 enum RXFrameType rx_type; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
109 enum Mode mode; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
110 Word16 reset_flag; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
111 Word16 reset_flag_old; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
112 |
1297 | 113 enum Mode enc_bitrate; |
114 Speech_Encode_FrameState *enstate; | |
115 sid_syncState *sidstate; | |
116 enum TXFrameType tx_frametype; | |
117 | |
118 | |
119 } AMRContext; | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
120 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
121 static int amr_nb_decode_init(AVCodecContext * avctx) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
122 { |
1297 | 123 AMRContext *s = avctx->priv_data; |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
124 s->frameCount=0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
125 s->speech_decoder_state=NULL; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
126 s->rx_type = (enum RXFrameType)0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
127 s->mode= (enum Mode)0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
128 s->reset_flag=0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
129 s->reset_flag_old=1; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
130 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
131 if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder")) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
132 { |
1297 | 133 printf("Speech_Decode_Frame_init error\n"); |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
134 return -1; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
135 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
136 return 0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
137 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
138 |
1297 | 139 static int amr_nb_encode_init(AVCodecContext * avctx) |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
140 { |
1297 | 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; | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
193 Speech_Decode_Frame_exit(&s->speech_decoder_state); |
1297 | 194 return 0; |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
195 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
196 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
197 static int amr_nb_decode_frame(AVCodecContext * avctx, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
198 void *data, int *data_size, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
199 uint8_t * buf, int buf_size) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
200 { |
1297 | 201 AMRContext *s = avctx->priv_data; |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
202 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
203 uint8_t*amrData=buf; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
204 int offset=0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
205 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
206 UWord8 toc, q, ft; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
207 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
208 Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
209 Word16 *synth; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
210 UWord8 *packed_bits; |
1297 | 211 *data_size=0; |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
212 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
213 static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0}; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
214 int i; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
215 |
1297 | 216 //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount); |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
217 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
218 synth=data; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
219 |
1297 | 220 // while(offset<buf_size) |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
221 { |
1297 | 222 toc=amrData[offset]; |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
223 /* read rest of the frame based on ToC byte */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
224 q = (toc >> 2) & 0x01; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
225 ft = (toc >> 3) & 0x0F; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
226 |
1297 | 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 | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
231 packed_bits=amrData+offset; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
232 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
233 offset+=packed_size[ft]; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
234 |
1297 | 235 //Unsort and unpack bits |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
236 s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]); |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
237 |
1297 | 238 //We have a new frame |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
239 s->frameCount++; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
240 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
241 if (s->rx_type == RX_NO_DATA) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
242 { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
243 s->mode = s->speech_decoder_state->prev_mode; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
244 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
245 else { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
246 s->speech_decoder_state->prev_mode = s->mode; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
247 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
248 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
249 /* if homed: check if this frame is another homing frame */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
250 if (s->reset_flag_old == 1) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
251 { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
252 /* only check until end of first subframe */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
253 s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode); |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
254 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
255 /* produce encoder homing frame if homed & input=decoder homing frame */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
256 if ((s->reset_flag != 0) && (s->reset_flag_old != 0)) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
257 { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
258 for (i = 0; i < L_FRAME; i++) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
259 { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
260 synth[i] = EHF_MASK; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
261 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
262 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
263 else |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
264 { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
265 /* decode frame */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
266 Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth); |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
267 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
268 |
1297 | 269 //Each AMR-frame results in 160 16-bit samples |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
270 *data_size+=160*2; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
271 synth+=160; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
272 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
273 /* if not homed: check whether current frame is a homing frame */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
274 if (s->reset_flag_old == 0) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
275 { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
276 /* check whole frame */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
277 s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode); |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
278 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
279 /* reset decoder if current frame is a homing frame */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
280 if (s->reset_flag != 0) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
281 { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
282 Speech_Decode_Frame_reset(s->speech_decoder_state); |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
283 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
284 s->reset_flag_old = s->reset_flag; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
285 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
286 } |
1297 | 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 } | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
418 return buf_size; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
419 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
420 |
1297 | 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 | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
438 AVCodec amr_nb_decoder = |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
439 { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
440 "amr_nb", |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
441 CODEC_TYPE_AUDIO, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
442 CODEC_ID_AMR_NB, |
1297 | 443 sizeof(AMRContext), |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
444 amr_nb_decode_init, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
445 NULL, |
1297 | 446 amr_nb_decode_close, |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
447 amr_nb_decode_frame, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
448 }; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
449 |
1297 | 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 }; |