1 /*
2 * WMA compatible encoder
3 * Copyright (c) 2007 Michael Niedermayer
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
27
28
30 {
32 int i, flags1, flags2, block_align;
34
36
41 }
42
47 }
48
53 }
54
55 /* extract flag infos */
56 flags1 = 0;
57 flags2 = 1;
68 }else
76
78
79 /* init MDCT */
82
87
89
90 return 0;
91 }
92
93
95 {
101 int ch;
102 const float * win = s->
windows[window_index];
104 float n = 2.0 * 32768.0 / window_len;
105
112 }
113 }
114
115 //FIXME use for decoding too
118 const uint16_t *ptr;
119 float v, *q, max_scale, *q_end;
120
124 max_scale = 0;
125 while (q < q_end) {
126 /* XXX: use a table */
127 v = pow(10, *exp_param++ * (1.0 / 16.0));
128 max_scale=
FFMAX(max_scale, v);
129 n = *ptr++;
130 do {
132 } while (--n);
133 }
135 }
136
138 int last_exp;
139 const uint16_t *ptr;
140 float *q, *q_end;
141
146 last_exp= *exp_param++;
147 av_assert0(last_exp-10 >= 0 && last_exp-10 < 32);
149 q+= *ptr++;
150 }else
151 last_exp = 36;
152 while (q < q_end) {
153 int exp = *exp_param++;
154 int code = exp - last_exp + 60;
157 /* XXX: use a table */
158 q+= *ptr++;
159 last_exp= exp;
160 }
161 }
162
164 int v, bsize, ch, coef_nb_bits, parse_exponents;
165 float mdct_norm;
167 static const int fixed_exp[25]={20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20};
168
169 //FIXME remove duplication relative to decoder
172 }else{
173 /* fixed block len */
177 }
178
180 // av_assert0((s->block_pos + s->block_len) <= s->frame_len);
182
183 //FIXME factor
186 nb_coefs[ch] = v;
187 {
189 mdct_norm = 1.0 / (float)n4;
191 mdct_norm *= sqrt(n4);
192 }
193 }
194
197 }
198
200 s->
channel_coded[ch] = 1;
//FIXME only set channel_coded when needed, instead of always
203 }
204 }
205
209 float *coefs, *exponents,
mult;
211
214 mult = pow(10, total_gain * 0.05) / s->
max_exponent[ch];
215 mult *= mdct_norm;
216 coefs = src_coefs[ch];
219 } else {
221 n = nb_coefs[ch];
222 for(i = 0;i <
n; i++){
223 double t= *coefs++ / (exponents[i] *
mult);
224 if(t<-32768 || t>32767)
225 return -1;
226
227 coefs1[i] =
lrint(t);
228 }
229 }
230 }
231 }
232
233 v = 0;
238 }
239
240 if (!v)
241 return 1;
242
243 for(v= total_gain-1; v>=127; v-= 127)
246
248
256 if (0)
258 }
259 }
260 }
261 }
262
263 parse_exponents = 1;
266 }
267
268 if (parse_exponents) {
273 } else {
275 // encode_exp_lsp(s, ch);
276 }
277 }
278 }
279 } else {
281 }
282
289 eptr = ptr + nb_coefs[ch];
290
291 run=0;
292 for(;ptr < eptr; ptr++){
293 if(*ptr){
295 int abs_level=
FFABS(level);
296 int code= 0;
297 if(abs_level <= s->
coef_vlcs[tindex]->max_level){
298 if(run < s->
coef_vlcs[tindex]->levels[abs_level-1])
299 code= run + s->
int_table[tindex][abs_level-1];
300 }
301
304
305 if(code == 0){
306 if(1<<coef_nb_bits <= abs_level)
307 return -1;
308
311 }
312 put_bits(&s->
pb, 1, level < 0);
//FIXME the sign is fliped somewhere
313 run=0;
314 }else{
315 run++;
316 }
317 }
318 if(run)
320 }
323 }
324 }
325 return 0;
326 }
327
330
333 }else{
335 return INT_MAX;
336 }
337
339
341 }
342
345 {
347 int i, total_gain,
ret, error;
348
351
353
356 int i;
357
359 a = s->
coefs[0][i]*0.5;
360 b = s->
coefs[1][i]*0.5;
363 }
364 }
365
368
369 total_gain= 128;
370 for(i=64; i; i>>=1){
372 total_gain - i);
373 if(error<=0)
374 total_gain-= i;
375 }
376
377 while(total_gain <= 128 && error > 0)
379 if (error > 0) {
380 av_log(avctx,
AV_LOG_ERROR,
"Invalid input data or requested bitrate too low, cannot encode\n");
383 }
387 while(i--)
389
392
395
397 *got_packet_ptr = 1;
398 return 0;
399 }
400
401 #if CONFIG_WMAV1_ENCODER
413 };
414 #endif
415 #if CONFIG_WMAV2_ENCODER
427 };
428 #endif