Mercurial > libavcodec.hg
annotate amr.c @ 4818:2137206961c9 libavcodec
align vertically
author | michael |
---|---|
date | Sat, 07 Apr 2007 23:31:30 +0000 |
parents | e69d9ecaaf84 |
children | c3b0eb402862 |
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 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3463
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3463
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3463
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
8 * 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
|
9 * License as published by the Free Software Foundation; either |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3463
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3463
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
13 * 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
|
14 * 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
|
15 * Lesser General Public License for more details. |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
16 * |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3463
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
20 */ |
2967 | 21 |
4575 | 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 with floats. For some reason the float-encoder is significant faster | |
30 * at least on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip at MR102). | |
31 * Both float and fixed point are supported for AMR-NB, but only float for | |
32 * AMR-WB. | |
33 * | |
34 * \section AMR-NB | |
35 * | |
36 * \subsection Float | |
37 * The float version (default) can be downloaded from: | |
4746
4aedb3b6fa4e
Update specification and reference implementation links.
diego
parents:
4744
diff
changeset
|
38 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-610.zip |
4575 | 39 * Extract the source into \c "ffmpeg/libavcodec/amr_float". |
4778
4297f75202d9
Explain which configure options are necessary for which AMR variant.
diego
parents:
4746
diff
changeset
|
40 * Enable it by passing \c "--enable-amr-nb" to \c "./configure". |
4575 | 41 * |
42 * \subsection Fixed-point | |
43 * The fixed-point (TS26.073) can be downloaded from: | |
44 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-510.zip. | |
45 * Extract the source into \c "ffmpeg/libavcodec/amr". | |
4778
4297f75202d9
Explain which configure options are necessary for which AMR variant.
diego
parents:
4746
diff
changeset
|
46 * Enable it by passing \c "--enable-amr-nb-fixed" to \c "./configure". |
4575 | 47 * |
48 * \subsection Specification | |
49 * The specification for AMR-NB can be found in TS 26.071 | |
50 * (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other | |
51 * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm. | |
52 * | |
53 * \section AMR-WB | |
54 * \subsection Float | |
55 * The reference code can be downloaded from: | |
4746
4aedb3b6fa4e
Update specification and reference implementation links.
diego
parents:
4744
diff
changeset
|
56 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip |
4778
4297f75202d9
Explain which configure options are necessary for which AMR variant.
diego
parents:
4746
diff
changeset
|
57 * It should be extracted to \c "ffmpeg/libavcodec/amrwb_float". |
4297f75202d9
Explain which configure options are necessary for which AMR variant.
diego
parents:
4746
diff
changeset
|
58 * Enable it by passing \c "--enable-amr-wb" to \c "./configure". |
4575 | 59 * |
60 * \subsection Fixed-point | |
61 * If someone wants to use the fixed point version it can be downloaded from: | |
62 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.173/26173-571.zip. | |
63 * | |
64 * \subsection Specification | |
4746
4aedb3b6fa4e
Update specification and reference implementation links.
diego
parents:
4744
diff
changeset
|
65 * The specification for AMR-WB can be found in TS 26.171 |
4aedb3b6fa4e
Update specification and reference implementation links.
diego
parents:
4744
diff
changeset
|
66 * (http://www.3gpp.org/ftp/Specs/html-info/26171.htm) and some other |
4aedb3b6fa4e
Update specification and reference implementation links.
diego
parents:
4744
diff
changeset
|
67 * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm. |
4575 | 68 * |
1297 | 69 */ |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
70 |
1297 | 71 #include "avcodec.h" |
72 | |
3463 | 73 #ifdef CONFIG_AMR_NB_FIXED |
1297 | 74 |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
75 #define MMS_IO |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
76 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
77 #include "amr/sp_dec.h" |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
78 #include "amr/d_homing.h" |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
79 #include "amr/typedef.h" |
1297 | 80 #include "amr/sp_enc.h" |
81 #include "amr/sid_sync.h" | |
82 #include "amr/e_homing.h" | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
83 |
1297 | 84 #else |
85 #include "amr_float/interf_dec.h" | |
86 #include "amr_float/interf_enc.h" | |
87 #endif | |
88 | |
4793
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
89 static const char *nb_bitrate_unsupported = |
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
90 "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n"; |
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
91 static const char *wb_bitrate_unsupported = |
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
92 "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"; |
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
93 |
1297 | 94 /* Common code for fixed and float version*/ |
95 typedef struct AMR_bitrates | |
96 { | |
4744 | 97 int rate; |
1297 | 98 enum Mode mode; |
99 } AMR_bitrates; | |
100 | |
4744 | 101 /* Match desired bitrate */ |
102 static int getBitrateMode(int bitrate) | |
1297 | 103 { |
4744 | 104 /* make the correspondance between bitrate and mode */ |
105 AMR_bitrates rates[]={ {4750,MR475}, | |
106 {5150,MR515}, | |
107 {5900,MR59}, | |
108 {6700,MR67}, | |
109 {7400,MR74}, | |
110 {7950,MR795}, | |
111 {10200,MR102}, | |
112 {12200,MR122}, | |
1297 | 113 }; |
114 int i; | |
4633 | 115 |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
116 for(i=0;i<8;i++) |
1297 | 117 { |
4744 | 118 if(rates[i].rate==bitrate) |
1297 | 119 { |
120 return(rates[i].mode); | |
121 } | |
122 } | |
4744 | 123 /* no bitrate matching, return an error */ |
124 return -1; | |
1297 | 125 } |
126 | |
4094 | 127 static void amr_decode_fix_avctx(AVCodecContext * avctx) |
128 { | |
129 const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB); | |
130 | |
131 if(avctx->sample_rate == 0) | |
132 { | |
133 avctx->sample_rate = 8000 * is_amr_wb; | |
134 } | |
135 | |
136 if(avctx->channels == 0) | |
137 { | |
138 avctx->channels = 1; | |
139 } | |
140 | |
141 avctx->frame_size = 160 * is_amr_wb; | |
142 } | |
143 | |
3463 | 144 #ifdef CONFIG_AMR_NB_FIXED |
1297 | 145 /* fixed point version*/ |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
146 /* 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
|
147 #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
148 |
1297 | 149 typedef struct AMRContext { |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
150 int frameCount; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
151 Speech_Decode_FrameState *speech_decoder_state; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
152 enum RXFrameType rx_type; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
153 enum Mode mode; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
154 Word16 reset_flag; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
155 Word16 reset_flag_old; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
156 |
4744 | 157 int enc_bitrate; |
1297 | 158 Speech_Encode_FrameState *enstate; |
159 sid_syncState *sidstate; | |
160 enum TXFrameType tx_frametype; | |
161 } AMRContext; | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
162 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
163 static int amr_nb_decode_init(AVCodecContext * avctx) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
164 { |
1297 | 165 AMRContext *s = avctx->priv_data; |
4633 | 166 |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
167 s->frameCount=0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
168 s->speech_decoder_state=NULL; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
169 s->rx_type = (enum RXFrameType)0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
170 s->mode= (enum Mode)0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
171 s->reset_flag=0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
172 s->reset_flag_old=1; |
2967 | 173 |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
174 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
|
175 { |
1825 | 176 av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n"); |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
177 return -1; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
178 } |
4094 | 179 |
180 amr_decode_fix_avctx(avctx); | |
181 | |
182 if(avctx->channels > 1) | |
183 { | |
184 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n"); | |
185 return -1; | |
186 } | |
187 | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
188 return 0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
189 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
190 |
1297 | 191 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
|
192 { |
1297 | 193 AMRContext *s = avctx->priv_data; |
4633 | 194 |
1297 | 195 s->frameCount=0; |
196 s->speech_decoder_state=NULL; | |
197 s->rx_type = (enum RXFrameType)0; | |
198 s->mode= (enum Mode)0; | |
199 s->reset_flag=0; | |
200 s->reset_flag_old=1; | |
2967 | 201 |
1297 | 202 if(avctx->sample_rate!=8000) |
203 { | |
4309
1f95aa362562
always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents:
4221
diff
changeset
|
204 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n"); |
1297 | 205 return -1; |
206 } | |
207 | |
208 if(avctx->channels!=1) | |
209 { | |
4309
1f95aa362562
always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents:
4221
diff
changeset
|
210 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); |
1297 | 211 return -1; |
212 } | |
213 | |
214 avctx->frame_size=160; | |
215 avctx->coded_frame= avcodec_alloc_frame(); | |
216 | |
217 if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate)) | |
218 { | |
4309
1f95aa362562
always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents:
4221
diff
changeset
|
219 av_log(avctx, AV_LOG_ERROR, "Speech_Encode_Frame_init error\n"); |
1297 | 220 return -1; |
221 } | |
222 | |
4744 | 223 if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0) |
224 { | |
4793
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
225 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); |
4744 | 226 return -1; |
227 } | |
1297 | 228 |
229 return 0; | |
230 } | |
231 | |
232 static int amr_nb_encode_close(AVCodecContext * avctx) | |
233 { | |
234 AMRContext *s = avctx->priv_data; | |
4633 | 235 |
1297 | 236 Speech_Encode_Frame_exit(&s->enstate); |
237 sid_sync_exit (&s->sidstate); | |
238 av_freep(&avctx->coded_frame); | |
239 return 0; | |
240 } | |
241 | |
242 static int amr_nb_decode_close(AVCodecContext * avctx) | |
243 { | |
244 AMRContext *s = avctx->priv_data; | |
4633 | 245 |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
246 Speech_Decode_Frame_exit(&s->speech_decoder_state); |
1297 | 247 return 0; |
1258
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 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
250 static int amr_nb_decode_frame(AVCodecContext * avctx, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
251 void *data, int *data_size, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
252 uint8_t * buf, int buf_size) |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
253 { |
1297 | 254 AMRContext *s = avctx->priv_data; |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
255 uint8_t*amrData=buf; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
256 int offset=0; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
257 UWord8 toc, q, ft; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
258 Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */ |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
259 Word16 *synth; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
260 UWord8 *packed_bits; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
261 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
|
262 int i; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
263 |
1297 | 264 //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
|
265 |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
266 synth=data; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
267 |
4781 | 268 toc=amrData[offset]; |
269 /* read rest of the frame based on ToC byte */ | |
270 q = (toc >> 2) & 0x01; | |
271 ft = (toc >> 3) & 0x0F; | |
272 | |
273 //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]); | |
274 | |
275 offset++; | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
276 |
4781 | 277 packed_bits=amrData+offset; |
1297 | 278 |
4781 | 279 offset+=packed_size[ft]; |
1297 | 280 |
4781 | 281 //Unsort and unpack bits |
282 s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]); | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
283 |
4781 | 284 //We have a new frame |
285 s->frameCount++; | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
286 |
4781 | 287 if (s->rx_type == RX_NO_DATA) |
288 { | |
289 s->mode = s->speech_decoder_state->prev_mode; | |
290 } | |
291 else { | |
292 s->speech_decoder_state->prev_mode = s->mode; | |
293 } | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
294 |
4781 | 295 /* if homed: check if this frame is another homing frame */ |
296 if (s->reset_flag_old == 1) | |
297 { | |
298 /* only check until end of first subframe */ | |
299 s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode); | |
300 } | |
301 /* produce encoder homing frame if homed & input=decoder homing frame */ | |
302 if ((s->reset_flag != 0) && (s->reset_flag_old != 0)) | |
303 { | |
304 for (i = 0; i < L_FRAME; i++) | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
305 { |
4781 | 306 synth[i] = EHF_MASK; |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
307 } |
4781 | 308 } |
309 else | |
310 { | |
311 /* decode frame */ | |
312 Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth); | |
313 } | |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
314 |
4781 | 315 //Each AMR-frame results in 160 16-bit samples |
4789
bf62c8a0d2fb
Return correct decoded size, decoder is called with only one frame at a time.
diego
parents:
4781
diff
changeset
|
316 *data_size=160*2; |
2967 | 317 |
4781 | 318 /* if not homed: check whether current frame is a homing frame */ |
319 if (s->reset_flag_old == 0) | |
320 { | |
321 /* check whole frame */ | |
322 s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode); | |
323 } | |
324 /* reset decoder if current frame is a homing frame */ | |
325 if (s->reset_flag != 0) | |
326 { | |
327 Speech_Decode_Frame_reset(s->speech_decoder_state); | |
328 } | |
329 s->reset_flag_old = s->reset_flag; | |
2967 | 330 |
1297 | 331 return offset; |
332 } | |
333 | |
334 | |
335 static int amr_nb_encode_frame(AVCodecContext *avctx, | |
2979 | 336 unsigned char *frame/*out*/, int buf_size, void *data/*in*/) |
1297 | 337 { |
338 short serial_data[250] = {0}; | |
339 AMRContext *s = avctx->priv_data; | |
340 int written; | |
2967 | 341 |
1297 | 342 s->reset_flag = encoder_homing_frame_test(data); |
2967 | 343 |
344 Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode); | |
345 | |
1297 | 346 /* add frame type and mode */ |
347 sid_sync (s->sidstate, s->mode, &s->tx_frametype); | |
2967 | 348 |
1297 | 349 written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame); |
2967 | 350 |
1297 | 351 if (s->reset_flag != 0) |
352 { | |
353 Speech_Encode_Frame_reset(s->enstate); | |
354 sid_sync_reset(s->sidstate); | |
355 } | |
356 return written; | |
357 } | |
358 | |
359 | |
3463 | 360 #elif defined(CONFIG_AMR_NB) /* Float point version*/ |
1297 | 361 |
362 typedef struct AMRContext { | |
363 int frameCount; | |
364 void * decState; | |
365 int *enstate; | |
4744 | 366 int enc_bitrate; |
1297 | 367 } AMRContext; |
368 | |
369 static int amr_nb_decode_init(AVCodecContext * avctx) | |
370 { | |
371 AMRContext *s = avctx->priv_data; | |
4633 | 372 |
1297 | 373 s->frameCount=0; |
374 s->decState=Decoder_Interface_init(); | |
375 if(!s->decState) | |
376 { | |
1825 | 377 av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n"); |
1297 | 378 return -1; |
379 } | |
4094 | 380 |
381 amr_decode_fix_avctx(avctx); | |
382 | |
383 if(avctx->channels > 1) | |
384 { | |
385 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n"); | |
386 return -1; | |
387 } | |
388 | |
1297 | 389 return 0; |
390 } | |
391 | |
392 static int amr_nb_encode_init(AVCodecContext * avctx) | |
393 { | |
394 AMRContext *s = avctx->priv_data; | |
4633 | 395 |
1297 | 396 s->frameCount=0; |
2967 | 397 |
1297 | 398 if(avctx->sample_rate!=8000) |
399 { | |
4309
1f95aa362562
always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents:
4221
diff
changeset
|
400 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n"); |
1297 | 401 return -1; |
402 } | |
403 | |
404 if(avctx->channels!=1) | |
405 { | |
4309
1f95aa362562
always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents:
4221
diff
changeset
|
406 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); |
1297 | 407 return -1; |
408 } | |
409 | |
410 avctx->frame_size=160; | |
411 avctx->coded_frame= avcodec_alloc_frame(); | |
412 | |
413 s->enstate=Encoder_Interface_init(0); | |
414 if(!s->enstate) | |
415 { | |
4309
1f95aa362562
always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents:
4221
diff
changeset
|
416 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n"); |
1297 | 417 return -1; |
418 } | |
419 | |
4744 | 420 if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0) |
421 { | |
4793
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
422 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); |
4744 | 423 return -1; |
424 } | |
1297 | 425 |
426 return 0; | |
427 } | |
428 | |
429 static int amr_nb_decode_close(AVCodecContext * avctx) | |
430 { | |
431 AMRContext *s = avctx->priv_data; | |
4633 | 432 |
1297 | 433 Decoder_Interface_exit(s->decState); |
434 return 0; | |
435 } | |
436 | |
437 static int amr_nb_encode_close(AVCodecContext * avctx) | |
438 { | |
439 AMRContext *s = avctx->priv_data; | |
4633 | 440 |
1297 | 441 Encoder_Interface_exit(s->enstate); |
442 av_freep(&avctx->coded_frame); | |
443 return 0; | |
444 } | |
445 | |
446 static int amr_nb_decode_frame(AVCodecContext * avctx, | |
447 void *data, int *data_size, | |
448 uint8_t * buf, int buf_size) | |
449 { | |
4634 | 450 AMRContext *s = avctx->priv_data; |
1297 | 451 uint8_t*amrData=buf; |
452 static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; | |
453 enum Mode dec_mode; | |
454 int packet_size; | |
455 | |
2287
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
456 /* av_log(NULL,AV_LOG_DEBUG,"amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",buf,buf_size,s->frameCount); */ |
2967 | 457 |
2287
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
458 dec_mode = (buf[0] >> 3) & 0x000F; |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
459 packet_size = block_size[dec_mode]+1; |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
460 |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
461 if(packet_size > buf_size) { |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
462 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size); |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
463 return -1; |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
464 } |
2967 | 465 |
2287
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
466 s->frameCount++; |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
467 /* 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]); */ |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
468 /* call decoder */ |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
469 Decoder_Interface_Decode(s->decState, amrData, data, 0); |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
470 *data_size=160*2; |
2967 | 471 |
2287
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
472 return packet_size; |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
473 } |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
474 |
1297 | 475 static int amr_nb_encode_frame(AVCodecContext *avctx, |
2979 | 476 unsigned char *frame/*out*/, int buf_size, void *data/*in*/) |
1297 | 477 { |
4634 | 478 AMRContext *s = avctx->priv_data; |
1297 | 479 int written; |
480 | |
4744 | 481 if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0) |
482 { | |
4793
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
483 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); |
4744 | 484 return -1; |
485 } | |
4220
4115a044e31d
allow amr bitrate to be changed "on the fly" through avctx->bitrate.
gpoirier
parents:
4094
diff
changeset
|
486 |
2967 | 487 written = Encoder_Interface_Encode(s->enstate, |
488 s->enc_bitrate, | |
489 data, | |
490 frame, | |
1297 | 491 0); |
2571
b17d868a8f39
04_AMR_no_debug_output.patch by (Calcium | calcium nurs or jp)
michael
parents:
2287
diff
changeset
|
492 /* 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] ); */ |
1297 | 493 |
494 return written; | |
495 } | |
496 | |
497 #endif | |
498 | |
3463 | 499 #if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED) |
2895 | 500 |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
501 AVCodec amr_nb_decoder = |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
502 { |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
503 "amr_nb", |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
504 CODEC_TYPE_AUDIO, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
505 CODEC_ID_AMR_NB, |
1297 | 506 sizeof(AMRContext), |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
507 amr_nb_decode_init, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
508 NULL, |
1297 | 509 amr_nb_decode_close, |
1258
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
510 amr_nb_decode_frame, |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
511 }; |
802614404398
AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff
changeset
|
512 |
1297 | 513 AVCodec amr_nb_encoder = |
514 { | |
515 "amr_nb", | |
516 CODEC_TYPE_AUDIO, | |
517 CODEC_ID_AMR_NB, | |
518 sizeof(AMRContext), | |
519 amr_nb_encode_init, | |
520 amr_nb_encode_frame, | |
521 amr_nb_encode_close, | |
522 NULL, | |
523 }; | |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
524 |
2895 | 525 #endif |
526 | |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
527 /* -----------AMR wideband ------------*/ |
3463 | 528 #ifdef CONFIG_AMR_WB |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
529 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
530 #ifdef _TYPEDEF_H |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
531 //To avoid duplicate typedefs from typdef in amr-nb |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
532 #define typedef_h |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
533 #endif |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
534 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
535 #include "amrwb_float/enc_if.h" |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
536 #include "amrwb_float/dec_if.h" |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
537 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
538 /* Common code for fixed and float version*/ |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
539 typedef struct AMRWB_bitrates |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
540 { |
4744 | 541 int rate; |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
542 int mode; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
543 } AMRWB_bitrates; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
544 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
545 static int getWBBitrateMode(int bitrate) |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
546 { |
4744 | 547 /* make the correspondance between bitrate and mode */ |
548 AMRWB_bitrates rates[]={ {6600,0}, | |
549 {8850,1}, | |
550 {12650,2}, | |
551 {14250,3}, | |
552 {15850,4}, | |
553 {18250,5}, | |
554 {19850,6}, | |
555 {23050,7}, | |
556 {23850,8}, | |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
557 }; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
558 int i; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
559 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
560 for(i=0;i<9;i++) |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
561 { |
4744 | 562 if(rates[i].rate==bitrate) |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
563 { |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
564 return(rates[i].mode); |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
565 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
566 } |
4744 | 567 /* no bitrate matching, return an error */ |
568 return -1; | |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
569 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
570 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
571 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
572 typedef struct AMRWBContext { |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
573 int frameCount; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
574 void *state; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
575 int mode; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
576 Word16 allow_dtx; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
577 } AMRWBContext; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
578 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
579 static int amr_wb_encode_init(AVCodecContext * avctx) |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
580 { |
4634 | 581 AMRWBContext *s = avctx->priv_data; |
4633 | 582 |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
583 s->frameCount=0; |
2967 | 584 |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
585 if(avctx->sample_rate!=16000) |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
586 { |
4309
1f95aa362562
always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents:
4221
diff
changeset
|
587 av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n"); |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
588 return -1; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
589 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
590 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
591 if(avctx->channels!=1) |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
592 { |
4309
1f95aa362562
always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents:
4221
diff
changeset
|
593 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
594 return -1; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
595 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
596 |
4744 | 597 if((s->mode=getWBBitrateMode(avctx->bit_rate))<0) |
598 { | |
4793
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
599 av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported); |
4744 | 600 return -1; |
601 } | |
602 | |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
603 avctx->frame_size=320; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
604 avctx->coded_frame= avcodec_alloc_frame(); |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
605 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
606 s->state = E_IF_init(); |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
607 s->allow_dtx=0; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
608 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
609 return 0; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
610 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
611 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
612 static int amr_wb_encode_close(AVCodecContext * avctx) |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
613 { |
4634 | 614 AMRWBContext *s = avctx->priv_data; |
4633 | 615 |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
616 E_IF_exit(s->state); |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
617 av_freep(&avctx->coded_frame); |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
618 s->frameCount++; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
619 return 0; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
620 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
621 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
622 static int amr_wb_encode_frame(AVCodecContext *avctx, |
2979 | 623 unsigned char *frame/*out*/, int buf_size, void *data/*in*/) |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
624 { |
4634 | 625 AMRWBContext *s = avctx->priv_data; |
4220
4115a044e31d
allow amr bitrate to be changed "on the fly" through avctx->bitrate.
gpoirier
parents:
4094
diff
changeset
|
626 int size; |
4632
260b5f667458
Move the initialization on context definition. In amr.c, only the
takis
parents:
4575
diff
changeset
|
627 |
4744 | 628 if((s->mode=getWBBitrateMode(avctx->bit_rate))<0) |
629 { | |
4793
e69d9ecaaf84
Print a list of valid AMR bitrates if a wrong one is used.
diego
parents:
4789
diff
changeset
|
630 av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported); |
4744 | 631 return -1; |
632 } | |
4220
4115a044e31d
allow amr bitrate to be changed "on the fly" through avctx->bitrate.
gpoirier
parents:
4094
diff
changeset
|
633 size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx); |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
634 return size; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
635 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
636 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
637 static int amr_wb_decode_init(AVCodecContext * avctx) |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
638 { |
4634 | 639 AMRWBContext *s = avctx->priv_data; |
4633 | 640 |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
641 s->frameCount=0; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
642 s->state = D_IF_init(); |
4094 | 643 |
644 amr_decode_fix_avctx(avctx); | |
645 | |
646 if(avctx->channels > 1) | |
647 { | |
648 av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n"); | |
649 return -1; | |
650 } | |
651 | |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
652 return 0; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
653 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
654 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
655 extern const UWord8 block_size[]; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
656 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
657 static int amr_wb_decode_frame(AVCodecContext * avctx, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
658 void *data, int *data_size, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
659 uint8_t * buf, int buf_size) |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
660 { |
4634 | 661 AMRWBContext *s = avctx->priv_data; |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
662 uint8_t*amrData=buf; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
663 int mode; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
664 int packet_size; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
665 |
2287
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
666 if(buf_size==0) { |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
667 /* nothing to do */ |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
668 return 0; |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
669 } |
2287
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
670 |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
671 mode = (amrData[0] >> 3) & 0x000F; |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
672 packet_size = block_size[mode]; |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
673 |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
674 if(packet_size > buf_size) { |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
675 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1); |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
676 return -1; |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
677 } |
2967 | 678 |
2287
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
679 s->frameCount++; |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
680 D_IF_decode( s->state, amrData, data, _good_frame); |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
681 *data_size=320*2; |
679642dc54cf
segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents:
2028
diff
changeset
|
682 return packet_size; |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
683 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
684 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
685 static int amr_wb_decode_close(AVCodecContext * avctx) |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
686 { |
4634 | 687 AMRWBContext *s = avctx->priv_data; |
4633 | 688 |
1486
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
689 D_IF_exit(s->state); |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
690 return 0; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
691 } |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
692 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
693 AVCodec amr_wb_decoder = |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
694 { |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
695 "amr_wb", |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
696 CODEC_TYPE_AUDIO, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
697 CODEC_ID_AMR_WB, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
698 sizeof(AMRWBContext), |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
699 amr_wb_decode_init, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
700 NULL, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
701 amr_wb_decode_close, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
702 amr_wb_decode_frame, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
703 }; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
704 |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
705 AVCodec amr_wb_encoder = |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
706 { |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
707 "amr_wb", |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
708 CODEC_TYPE_AUDIO, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
709 CODEC_ID_AMR_WB, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
710 sizeof(AMRWBContext), |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
711 amr_wb_encode_init, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
712 amr_wb_encode_frame, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
713 amr_wb_encode_close, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
714 NULL, |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
715 }; |
f22010affdce
AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents:
1297
diff
changeset
|
716 |
3463 | 717 #endif //CONFIG_AMR_WB |