1 | /* |
---|
2 | * The simplest mpeg audio layer 2 encoder |
---|
3 | * Copyright (c) 2000, 2001 Fabrice Bellard. |
---|
4 | * |
---|
5 | * This routines are normaly part of FFmpeg and had been isolated |
---|
6 | * for use in DreamDVD by Seddi. |
---|
7 | * |
---|
8 | * FFmpeg is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU Lesser General Public |
---|
10 | * License as published by the Free Software Foundation; either |
---|
11 | * version 2.1 of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * FFmpeg is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | * Lesser General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU Lesser General Public |
---|
19 | * License along with FFmpeg; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
21 | * |
---|
22 | * part of libdreamdvd |
---|
23 | */ |
---|
24 | |
---|
25 | |
---|
26 | #include "mpegaudio_enc.h" |
---|
27 | |
---|
28 | |
---|
29 | void ddvd_mpa_init(int init_freq, int init_bitrate) |
---|
30 | { |
---|
31 | int i, v, table; |
---|
32 | float a; |
---|
33 | |
---|
34 | ddvd_mpa_freq=init_freq; |
---|
35 | ddvd_mpa_bit_rate=init_bitrate; |
---|
36 | |
---|
37 | ddvd_mpa_lsf = 0; |
---|
38 | for(i=0;i<3;i++) { |
---|
39 | if (ddvd_mpa_ff_mpa_freq_tab[i] == ddvd_mpa_freq) |
---|
40 | break; |
---|
41 | if ((ddvd_mpa_ff_mpa_freq_tab[i] / 2) == ddvd_mpa_freq) { |
---|
42 | ddvd_mpa_lsf = 1; |
---|
43 | break; |
---|
44 | } |
---|
45 | } |
---|
46 | if (i == 3){ |
---|
47 | return; |
---|
48 | } |
---|
49 | ddvd_mpa_freq_index = i; |
---|
50 | |
---|
51 | /* encoding bitrate & frequency */ |
---|
52 | for(i=0;i<15;i++) { |
---|
53 | if (ddvd_mpa_ff_mpa_bitrate_tab[ddvd_mpa_lsf][1][i] == ddvd_mpa_bit_rate/1000) |
---|
54 | break; |
---|
55 | } |
---|
56 | if (i == 15){ |
---|
57 | return; |
---|
58 | } |
---|
59 | ddvd_mpa_bitrate_index = i; |
---|
60 | |
---|
61 | /* compute total header size & pad bit */ |
---|
62 | |
---|
63 | a = (float)(ddvd_mpa_bit_rate * MPA_FRAME_SIZE) / (ddvd_mpa_freq * 8.0); |
---|
64 | ddvd_mpa_frame_size = ((int)a) * 8; |
---|
65 | |
---|
66 | /* frame fractional size to compute padding */ |
---|
67 | ddvd_mpa_frame_frac = 0; |
---|
68 | ddvd_mpa_frame_frac_incr = (int)((a - FLOOR(a)) * 65536.0); |
---|
69 | |
---|
70 | /* select the right allocation table */ |
---|
71 | table = ddvd_mpa_ff_mpa_l2_select_table(ddvd_mpa_bit_rate/1000, NB_CHANNELS, ddvd_mpa_freq, ddvd_mpa_lsf); |
---|
72 | |
---|
73 | /* number of used subbands */ |
---|
74 | ddvd_mpa_sblimit = ddvd_mpa_ff_mpa_sblimit_table[table]; |
---|
75 | ddvd_mpa_alloc_table = ddvd_mpa_ff_mpa_alloc_tables[table]; |
---|
76 | |
---|
77 | |
---|
78 | for(i=0;i<NB_CHANNELS;i++) |
---|
79 | ddvd_mpa_samples_offset[i] = 0; |
---|
80 | |
---|
81 | for(i=0;i<257;i++) { |
---|
82 | int v; |
---|
83 | v = ddvd_mpa_ff_mpa_enwindow[i]; |
---|
84 | #if WFRAC_BITS != 16 |
---|
85 | v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS); |
---|
86 | #endif |
---|
87 | ddvd_mpa_filter_bank[i] = v; |
---|
88 | if ((i & 63) != 0) |
---|
89 | v = -v; |
---|
90 | if (i != 0) |
---|
91 | ddvd_mpa_filter_bank[512 - i] = v; |
---|
92 | } |
---|
93 | for(i=0;i<64;i++) { |
---|
94 | v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20)); |
---|
95 | if (v <= 0) |
---|
96 | v = 1; |
---|
97 | ddvd_mpa_scale_factor_table[i] = v; |
---|
98 | #define P 15 |
---|
99 | ddvd_mpa_scale_factor_shift[i] = 21 - P - (i / 3); |
---|
100 | ddvd_mpa_scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0); |
---|
101 | } |
---|
102 | for(i=0;i<128;i++) { |
---|
103 | v = i - 64; |
---|
104 | if (v <= -3) |
---|
105 | v = 0; |
---|
106 | else if (v < 0) |
---|
107 | v = 1; |
---|
108 | else if (v == 0) |
---|
109 | v = 2; |
---|
110 | else if (v < 3) |
---|
111 | v = 3; |
---|
112 | else |
---|
113 | v = 4; |
---|
114 | ddvd_mpa_scale_diff_table[i] = v; |
---|
115 | } |
---|
116 | for(i=0;i<17;i++) { |
---|
117 | v = ddvd_mpa_ff_mpa_quant_bits[i]; |
---|
118 | if (v < 0) |
---|
119 | v = -v; |
---|
120 | else |
---|
121 | v = v * 3; |
---|
122 | ddvd_mpa_total_quant_bits[i] = 12 * v; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */ |
---|
128 | static void ddvd_mpa_idct32(int *out, int *tab) |
---|
129 | { |
---|
130 | int i, j; |
---|
131 | int *t, *t1, xr; |
---|
132 | const int *xp = ddvd_mpa_costab32; |
---|
133 | |
---|
134 | for(j=31;j>=3;j-=2) tab[j] += tab[j - 2]; |
---|
135 | |
---|
136 | t = tab + 30; |
---|
137 | t1 = tab + 2; |
---|
138 | do { |
---|
139 | t[0] += t[-4]; |
---|
140 | t[1] += t[1 - 4]; |
---|
141 | t -= 4; |
---|
142 | } while (t != t1); |
---|
143 | |
---|
144 | t = tab + 28; |
---|
145 | t1 = tab + 4; |
---|
146 | do { |
---|
147 | t[0] += t[-8]; |
---|
148 | t[1] += t[1-8]; |
---|
149 | t[2] += t[2-8]; |
---|
150 | t[3] += t[3-8]; |
---|
151 | t -= 8; |
---|
152 | } while (t != t1); |
---|
153 | |
---|
154 | t = tab; |
---|
155 | t1 = tab + 32; |
---|
156 | do { |
---|
157 | t[ 3] = -t[ 3]; |
---|
158 | t[ 6] = -t[ 6]; |
---|
159 | |
---|
160 | t[11] = -t[11]; |
---|
161 | t[12] = -t[12]; |
---|
162 | t[13] = -t[13]; |
---|
163 | t[15] = -t[15]; |
---|
164 | t += 16; |
---|
165 | } while (t != t1); |
---|
166 | |
---|
167 | |
---|
168 | t = tab; |
---|
169 | t1 = tab + 8; |
---|
170 | do { |
---|
171 | int x1, x2, x3, x4; |
---|
172 | |
---|
173 | x3 = MUL(t[16], FIX(SQRT2*0.5)); |
---|
174 | x4 = t[0] - x3; |
---|
175 | x3 = t[0] + x3; |
---|
176 | |
---|
177 | x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5)); |
---|
178 | x1 = MUL((t[8] - x2), xp[0]); |
---|
179 | x2 = MUL((t[8] + x2), xp[1]); |
---|
180 | |
---|
181 | t[ 0] = x3 + x1; |
---|
182 | t[ 8] = x4 - x2; |
---|
183 | t[16] = x4 + x2; |
---|
184 | t[24] = x3 - x1; |
---|
185 | t++; |
---|
186 | } while (t != t1); |
---|
187 | |
---|
188 | xp += 2; |
---|
189 | t = tab; |
---|
190 | t1 = tab + 4; |
---|
191 | do { |
---|
192 | xr = MUL(t[28],xp[0]); |
---|
193 | t[28] = (t[0] - xr); |
---|
194 | t[0] = (t[0] + xr); |
---|
195 | |
---|
196 | xr = MUL(t[4],xp[1]); |
---|
197 | t[ 4] = (t[24] - xr); |
---|
198 | t[24] = (t[24] + xr); |
---|
199 | |
---|
200 | xr = MUL(t[20],xp[2]); |
---|
201 | t[20] = (t[8] - xr); |
---|
202 | t[ 8] = (t[8] + xr); |
---|
203 | |
---|
204 | xr = MUL(t[12],xp[3]); |
---|
205 | t[12] = (t[16] - xr); |
---|
206 | t[16] = (t[16] + xr); |
---|
207 | t++; |
---|
208 | } while (t != t1); |
---|
209 | xp += 4; |
---|
210 | |
---|
211 | for (i = 0; i < 4; i++) { |
---|
212 | xr = MUL(tab[30-i*4],xp[0]); |
---|
213 | tab[30-i*4] = (tab[i*4] - xr); |
---|
214 | tab[ i*4] = (tab[i*4] + xr); |
---|
215 | |
---|
216 | xr = MUL(tab[ 2+i*4],xp[1]); |
---|
217 | tab[ 2+i*4] = (tab[28-i*4] - xr); |
---|
218 | tab[28-i*4] = (tab[28-i*4] + xr); |
---|
219 | |
---|
220 | xr = MUL(tab[31-i*4],xp[0]); |
---|
221 | tab[31-i*4] = (tab[1+i*4] - xr); |
---|
222 | tab[ 1+i*4] = (tab[1+i*4] + xr); |
---|
223 | |
---|
224 | xr = MUL(tab[ 3+i*4],xp[1]); |
---|
225 | tab[ 3+i*4] = (tab[29-i*4] - xr); |
---|
226 | tab[29-i*4] = (tab[29-i*4] + xr); |
---|
227 | |
---|
228 | xp += 2; |
---|
229 | } |
---|
230 | |
---|
231 | t = tab + 30; |
---|
232 | t1 = tab + 1; |
---|
233 | do { |
---|
234 | xr = MUL(t1[0], *xp); |
---|
235 | t1[0] = (t[0] - xr); |
---|
236 | t[0] = (t[0] + xr); |
---|
237 | t -= 2; |
---|
238 | t1 += 2; |
---|
239 | xp++; |
---|
240 | } while (t >= tab); |
---|
241 | |
---|
242 | for(i=0;i<32;i++) { |
---|
243 | out[i] = tab[ddvd_mpa_bitinv32[i]]; |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | |
---|
249 | static void ddvd_mpa_filter(int ch, short *samples, int incr) |
---|
250 | { |
---|
251 | short *p, *q; |
---|
252 | int sum, offset, i, j; |
---|
253 | int tmp[64]; |
---|
254 | int tmp1[32]; |
---|
255 | int *out; |
---|
256 | |
---|
257 | // print_pow1(samples, 1152); |
---|
258 | |
---|
259 | offset = ddvd_mpa_samples_offset[ch]; |
---|
260 | out = &ddvd_mpa_sb_samples[ch][0][0][0]; |
---|
261 | for(j=0;j<36;j++) { |
---|
262 | /* 32 samples at once */ |
---|
263 | for(i=0;i<32;i++) { |
---|
264 | ddvd_mpa_samples_buf[ch][offset + (31 - i)] = samples[0]; |
---|
265 | samples += incr; |
---|
266 | } |
---|
267 | |
---|
268 | /* filter */ |
---|
269 | p = ddvd_mpa_samples_buf[ch] + offset; |
---|
270 | q = ddvd_mpa_filter_bank; |
---|
271 | /* maxsum = 23169 */ |
---|
272 | for(i=0;i<64;i++) { |
---|
273 | sum = p[0*64] * q[0*64]; |
---|
274 | sum += p[1*64] * q[1*64]; |
---|
275 | sum += p[2*64] * q[2*64]; |
---|
276 | sum += p[3*64] * q[3*64]; |
---|
277 | sum += p[4*64] * q[4*64]; |
---|
278 | sum += p[5*64] * q[5*64]; |
---|
279 | sum += p[6*64] * q[6*64]; |
---|
280 | sum += p[7*64] * q[7*64]; |
---|
281 | tmp[i] = sum; |
---|
282 | p++; |
---|
283 | q++; |
---|
284 | } |
---|
285 | tmp1[0] = tmp[16] >> WSHIFT; |
---|
286 | for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT; |
---|
287 | for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT; |
---|
288 | |
---|
289 | ddvd_mpa_idct32(out, tmp1); |
---|
290 | |
---|
291 | /* advance of 32 samples */ |
---|
292 | offset -= 32; |
---|
293 | out += 32; |
---|
294 | /* handle the wrap around */ |
---|
295 | if (offset < 0) { |
---|
296 | memmove(ddvd_mpa_samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32), |
---|
297 | ddvd_mpa_samples_buf[ch], (512 - 32) * 2); |
---|
298 | offset = SAMPLES_BUF_SIZE - 512; |
---|
299 | } |
---|
300 | } |
---|
301 | ddvd_mpa_samples_offset[ch] = offset; |
---|
302 | |
---|
303 | // print_pow(s->sb_samples, 1152); |
---|
304 | } |
---|
305 | |
---|
306 | static void ddvd_mpa_compute_scale_factors(unsigned char ddvd_mpa_scale_code[SBLIMIT], |
---|
307 | unsigned char ddvd_mpa_scale_factors[SBLIMIT][3], |
---|
308 | int ddvd_mpa_sb_samples[3][12][SBLIMIT], |
---|
309 | int ddvd_mpa_sblimit) |
---|
310 | { |
---|
311 | int *p, vmax, v, n, i, j, k, code; |
---|
312 | int index, d1, d2; |
---|
313 | unsigned char *sf = &ddvd_mpa_scale_factors[0][0]; |
---|
314 | |
---|
315 | for(j=0;j<ddvd_mpa_sblimit;j++) { |
---|
316 | for(i=0;i<3;i++) { |
---|
317 | /* find the max absolute value */ |
---|
318 | p = &ddvd_mpa_sb_samples[i][0][j]; |
---|
319 | vmax = abs(*p); |
---|
320 | for(k=1;k<12;k++) { |
---|
321 | p += SBLIMIT; |
---|
322 | v = abs(*p); |
---|
323 | if (v > vmax) |
---|
324 | vmax = v; |
---|
325 | } |
---|
326 | /* compute the scale factor index using log 2 computations */ |
---|
327 | if (vmax > 0) { |
---|
328 | n = ddvd_mpa_av_log2(vmax); |
---|
329 | /* n is the position of the MSB of vmax. now |
---|
330 | use at most 2 compares to find the index */ |
---|
331 | index = (21 - n) * 3 - 3; |
---|
332 | if (index >= 0) { |
---|
333 | while (vmax <= ddvd_mpa_scale_factor_table[index+1]) |
---|
334 | index++; |
---|
335 | } else { |
---|
336 | index = 0; /* very unlikely case of overflow */ |
---|
337 | } |
---|
338 | } else { |
---|
339 | index = 62; /* value 63 is not allowed */ |
---|
340 | } |
---|
341 | |
---|
342 | #if 0 |
---|
343 | printf("%2d:%d in=%x %x %d\n", |
---|
344 | j, i, vmax, ddvd_mpa_scale_factor_table[index], index); |
---|
345 | #endif |
---|
346 | /* store the scale factor */ |
---|
347 | assert(index >=0 && index <= 63); |
---|
348 | sf[i] = index; |
---|
349 | } |
---|
350 | |
---|
351 | /* compute the transmission factor : look if the scale factors |
---|
352 | are close enough to each other */ |
---|
353 | d1 = ddvd_mpa_scale_diff_table[sf[0] - sf[1] + 64]; |
---|
354 | d2 = ddvd_mpa_scale_diff_table[sf[1] - sf[2] + 64]; |
---|
355 | |
---|
356 | /* handle the 25 cases */ |
---|
357 | switch(d1 * 5 + d2) { |
---|
358 | case 0*5+0: |
---|
359 | case 0*5+4: |
---|
360 | case 3*5+4: |
---|
361 | case 4*5+0: |
---|
362 | case 4*5+4: |
---|
363 | code = 0; |
---|
364 | break; |
---|
365 | case 0*5+1: |
---|
366 | case 0*5+2: |
---|
367 | case 4*5+1: |
---|
368 | case 4*5+2: |
---|
369 | code = 3; |
---|
370 | sf[2] = sf[1]; |
---|
371 | break; |
---|
372 | case 0*5+3: |
---|
373 | case 4*5+3: |
---|
374 | code = 3; |
---|
375 | sf[1] = sf[2]; |
---|
376 | break; |
---|
377 | case 1*5+0: |
---|
378 | case 1*5+4: |
---|
379 | case 2*5+4: |
---|
380 | code = 1; |
---|
381 | sf[1] = sf[0]; |
---|
382 | break; |
---|
383 | case 1*5+1: |
---|
384 | case 1*5+2: |
---|
385 | case 2*5+0: |
---|
386 | case 2*5+1: |
---|
387 | case 2*5+2: |
---|
388 | code = 2; |
---|
389 | sf[1] = sf[2] = sf[0]; |
---|
390 | break; |
---|
391 | case 2*5+3: |
---|
392 | case 3*5+3: |
---|
393 | code = 2; |
---|
394 | sf[0] = sf[1] = sf[2]; |
---|
395 | break; |
---|
396 | case 3*5+0: |
---|
397 | case 3*5+1: |
---|
398 | case 3*5+2: |
---|
399 | code = 2; |
---|
400 | sf[0] = sf[2] = sf[1]; |
---|
401 | break; |
---|
402 | case 1*5+3: |
---|
403 | code = 2; |
---|
404 | if (sf[0] > sf[2]) |
---|
405 | sf[0] = sf[2]; |
---|
406 | sf[1] = sf[2] = sf[0]; |
---|
407 | break; |
---|
408 | default: |
---|
409 | assert(0); //cannot happen |
---|
410 | code = 0; /* kill warning */ |
---|
411 | } |
---|
412 | |
---|
413 | #if 0 |
---|
414 | printf("%d: %2d %2d %2d %d %d -> %d\n", j, |
---|
415 | sf[0], sf[1], sf[2], d1, d2, code); |
---|
416 | #endif |
---|
417 | ddvd_mpa_scale_code[j] = code; |
---|
418 | sf += 3; |
---|
419 | } |
---|
420 | } |
---|
421 | |
---|
422 | /* The most important function : psycho acoustic module. In this |
---|
423 | encoder there is basically none, so this is the worst you can do, |
---|
424 | but also this is the simpler. */ |
---|
425 | static void ddvd_mpa_psycho_acoustic_model(short smr[SBLIMIT]) |
---|
426 | { |
---|
427 | int i; |
---|
428 | |
---|
429 | for(i=0;i<ddvd_mpa_sblimit;i++) { |
---|
430 | smr[i] = (int)(ddvd_mpa_fixed_smr[i] * 10); |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | /* Try to maximize the smr while using a number of bits inferior to |
---|
435 | the frame size. I tried to make the code simpler, faster and |
---|
436 | smaller than other encoders :-) */ |
---|
437 | static void ddvd_mpa_compute_bit_allocation(short smr1[MPA_MAX_CHANNELS][SBLIMIT], |
---|
438 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], |
---|
439 | int *padding) |
---|
440 | { |
---|
441 | int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size; |
---|
442 | int incr; |
---|
443 | short smr[MPA_MAX_CHANNELS][SBLIMIT]; |
---|
444 | unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT]; |
---|
445 | const unsigned char *alloc; |
---|
446 | |
---|
447 | memcpy(smr, smr1, NB_CHANNELS * sizeof(short) * SBLIMIT); |
---|
448 | memset(subband_status, SB_NOTALLOCATED, NB_CHANNELS * SBLIMIT); |
---|
449 | memset(bit_alloc, 0, NB_CHANNELS * SBLIMIT); |
---|
450 | |
---|
451 | /* compute frame size and padding */ |
---|
452 | max_frame_size = ddvd_mpa_frame_size; |
---|
453 | ddvd_mpa_frame_frac += ddvd_mpa_frame_frac_incr; |
---|
454 | if (ddvd_mpa_frame_frac >= 65536) { |
---|
455 | ddvd_mpa_frame_frac -= 65536; |
---|
456 | ddvd_mpa_do_padding = 1; |
---|
457 | max_frame_size += 8; |
---|
458 | } else { |
---|
459 | ddvd_mpa_do_padding = 0; |
---|
460 | } |
---|
461 | |
---|
462 | /* compute the header + bit alloc size */ |
---|
463 | current_frame_size = 32; |
---|
464 | alloc = ddvd_mpa_alloc_table; |
---|
465 | for(i=0;i<ddvd_mpa_sblimit;i++) { |
---|
466 | incr = alloc[0]; |
---|
467 | current_frame_size += incr * NB_CHANNELS; |
---|
468 | alloc += 1 << incr; |
---|
469 | } |
---|
470 | for(;;) { |
---|
471 | /* look for the subband with the largest signal to mask ratio */ |
---|
472 | max_sb = -1; |
---|
473 | max_ch = -1; |
---|
474 | max_smr = 0x80000000; |
---|
475 | for(ch=0;ch<NB_CHANNELS;ch++) { |
---|
476 | for(i=0;i<ddvd_mpa_sblimit;i++) { |
---|
477 | if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) { |
---|
478 | max_smr = smr[ch][i]; |
---|
479 | max_sb = i; |
---|
480 | max_ch = ch; |
---|
481 | } |
---|
482 | } |
---|
483 | } |
---|
484 | |
---|
485 | if (max_sb < 0) |
---|
486 | break; |
---|
487 | |
---|
488 | /* find alloc table entry (XXX: not optimal, should use |
---|
489 | pointer table) */ |
---|
490 | alloc = ddvd_mpa_alloc_table; |
---|
491 | for(i=0;i<max_sb;i++) { |
---|
492 | alloc += 1 << alloc[0]; |
---|
493 | } |
---|
494 | |
---|
495 | if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) { |
---|
496 | /* nothing was coded for this band: add the necessary bits */ |
---|
497 | incr = 2 + ddvd_mpa_nb_scale_factors[ddvd_mpa_scale_code[max_ch][max_sb]] * 6; |
---|
498 | incr += ddvd_mpa_total_quant_bits[alloc[1]]; |
---|
499 | } else { |
---|
500 | /* increments bit allocation */ |
---|
501 | b = bit_alloc[max_ch][max_sb]; |
---|
502 | incr = ddvd_mpa_total_quant_bits[alloc[b + 1]] - |
---|
503 | ddvd_mpa_total_quant_bits[alloc[b]]; |
---|
504 | } |
---|
505 | |
---|
506 | if (current_frame_size + incr <= max_frame_size) { |
---|
507 | /* can increase size */ |
---|
508 | b = ++bit_alloc[max_ch][max_sb]; |
---|
509 | current_frame_size += incr; |
---|
510 | /* decrease smr by the resolution we added */ |
---|
511 | smr[max_ch][max_sb] = smr1[max_ch][max_sb] - ddvd_mpa_quant_snr[alloc[b]]; |
---|
512 | /* max allocation size reached ? */ |
---|
513 | if (b == ((1 << alloc[0]) - 1)) |
---|
514 | subband_status[max_ch][max_sb] = SB_NOMORE; |
---|
515 | else |
---|
516 | subband_status[max_ch][max_sb] = SB_ALLOCATED; |
---|
517 | } else { |
---|
518 | /* cannot increase the size of this subband */ |
---|
519 | subband_status[max_ch][max_sb] = SB_NOMORE; |
---|
520 | } |
---|
521 | } |
---|
522 | *padding = max_frame_size - current_frame_size; |
---|
523 | |
---|
524 | assert(*padding >= 0); |
---|
525 | |
---|
526 | } |
---|
527 | |
---|
528 | static void ddvd_mpa_encode_frame_internal(unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], |
---|
529 | int padding) |
---|
530 | { |
---|
531 | int i, j, k, l, bit_alloc_bits, b, ch; |
---|
532 | unsigned char *sf; |
---|
533 | int q[3]; |
---|
534 | ddvd_mpa_PutBitContext *p = &pb; |
---|
535 | |
---|
536 | /* header */ |
---|
537 | |
---|
538 | ddvd_mpa_put_bits(p, 12, 0xfff); |
---|
539 | ddvd_mpa_put_bits(p, 1, 1 - ddvd_mpa_lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */ |
---|
540 | ddvd_mpa_put_bits(p, 2, 4-2); /* layer 2 */ |
---|
541 | ddvd_mpa_put_bits(p, 1, 1); /* no error protection */ |
---|
542 | ddvd_mpa_put_bits(p, 4, ddvd_mpa_bitrate_index); |
---|
543 | ddvd_mpa_put_bits(p, 2, ddvd_mpa_freq_index); |
---|
544 | ddvd_mpa_put_bits(p, 1, ddvd_mpa_do_padding); /* use padding */ |
---|
545 | ddvd_mpa_put_bits(p, 1, 0); /* private_bit */ |
---|
546 | ddvd_mpa_put_bits(p, 2, NB_CHANNELS == 2 ? MPA_STEREO : MPA_MONO); |
---|
547 | ddvd_mpa_put_bits(p, 2, 0); /* mode_ext */ |
---|
548 | ddvd_mpa_put_bits(p, 1, 0); /* no copyright */ |
---|
549 | ddvd_mpa_put_bits(p, 1, 1); /* original */ |
---|
550 | ddvd_mpa_put_bits(p, 2, 0); /* no emphasis */ |
---|
551 | |
---|
552 | /* bit allocation */ |
---|
553 | j = 0; |
---|
554 | for(i=0;i<ddvd_mpa_sblimit;i++) { |
---|
555 | bit_alloc_bits = ddvd_mpa_alloc_table[j]; |
---|
556 | for(ch=0;ch<NB_CHANNELS;ch++) { |
---|
557 | ddvd_mpa_put_bits(p, bit_alloc_bits, bit_alloc[ch][i]); |
---|
558 | } |
---|
559 | j += 1 << bit_alloc_bits; |
---|
560 | } |
---|
561 | |
---|
562 | /* scale codes */ |
---|
563 | for(i=0;i<ddvd_mpa_sblimit;i++) { |
---|
564 | for(ch=0;ch<NB_CHANNELS;ch++) { |
---|
565 | if (bit_alloc[ch][i]) |
---|
566 | ddvd_mpa_put_bits(p, 2, ddvd_mpa_scale_code[ch][i]); |
---|
567 | } |
---|
568 | } |
---|
569 | |
---|
570 | /* scale factors */ |
---|
571 | for(i=0;i<ddvd_mpa_sblimit;i++) { |
---|
572 | for(ch=0;ch<NB_CHANNELS;ch++) { |
---|
573 | if (bit_alloc[ch][i]) { |
---|
574 | sf = &ddvd_mpa_scale_factors[ch][i][0]; |
---|
575 | switch(ddvd_mpa_scale_code[ch][i]) { |
---|
576 | case 0: |
---|
577 | ddvd_mpa_put_bits(p, 6, sf[0]); |
---|
578 | ddvd_mpa_put_bits(p, 6, sf[1]); |
---|
579 | ddvd_mpa_put_bits(p, 6, sf[2]); |
---|
580 | break; |
---|
581 | case 3: |
---|
582 | case 1: |
---|
583 | ddvd_mpa_put_bits(p, 6, sf[0]); |
---|
584 | ddvd_mpa_put_bits(p, 6, sf[2]); |
---|
585 | break; |
---|
586 | case 2: |
---|
587 | ddvd_mpa_put_bits(p, 6, sf[0]); |
---|
588 | break; |
---|
589 | } |
---|
590 | } |
---|
591 | } |
---|
592 | } |
---|
593 | |
---|
594 | /* quantization & write sub band samples */ |
---|
595 | |
---|
596 | for(k=0;k<3;k++) { |
---|
597 | for(l=0;l<12;l+=3) { |
---|
598 | j = 0; |
---|
599 | for(i=0;i<ddvd_mpa_sblimit;i++) { |
---|
600 | bit_alloc_bits = ddvd_mpa_alloc_table[j]; |
---|
601 | for(ch=0;ch<NB_CHANNELS;ch++) { |
---|
602 | b = bit_alloc[ch][i]; |
---|
603 | if (b) { |
---|
604 | int qindex, steps, m, sample, bits; |
---|
605 | /* we encode 3 sub band samples of the same sub band at a time */ |
---|
606 | qindex = ddvd_mpa_alloc_table[j+b]; |
---|
607 | steps = ddvd_mpa_ff_mpa_quant_steps[qindex]; |
---|
608 | for(m=0;m<3;m++) { |
---|
609 | sample = ddvd_mpa_sb_samples[ch][k][l + m][i]; |
---|
610 | /* divide by scale factor */ |
---|
611 | |
---|
612 | { |
---|
613 | int q1, e, shift, mult; |
---|
614 | e = ddvd_mpa_scale_factors[ch][i][k]; |
---|
615 | shift = ddvd_mpa_scale_factor_shift[e]; |
---|
616 | mult = ddvd_mpa_scale_factor_mult[e]; |
---|
617 | |
---|
618 | /* normalize to P bits */ |
---|
619 | if (shift < 0) |
---|
620 | q1 = sample << (-shift); |
---|
621 | else |
---|
622 | q1 = sample >> shift; |
---|
623 | q1 = (q1 * mult) >> P; |
---|
624 | q[m] = ((q1 + (1 << P)) * steps) >> (P + 1); |
---|
625 | } |
---|
626 | |
---|
627 | if (q[m] >= steps) |
---|
628 | q[m] = steps - 1; |
---|
629 | if (q[m] <= 0) //FIXME |
---|
630 | q[m] = 0; |
---|
631 | assert(q[m] >= 0 && q[m] < steps); |
---|
632 | } |
---|
633 | bits = ddvd_mpa_ff_mpa_quant_bits[qindex]; |
---|
634 | if (bits < 0) { |
---|
635 | /* group the 3 values to save bits */ |
---|
636 | ddvd_mpa_put_bits(p, -bits, |
---|
637 | q[0] + steps * (q[1] + steps * q[2])); |
---|
638 | |
---|
639 | } else { |
---|
640 | |
---|
641 | ddvd_mpa_put_bits(p, bits, q[0]); |
---|
642 | ddvd_mpa_put_bits(p, bits, q[1]); |
---|
643 | ddvd_mpa_put_bits(p, bits, q[2]); |
---|
644 | } |
---|
645 | } |
---|
646 | } |
---|
647 | /* next subband in alloc table */ |
---|
648 | j += 1 << bit_alloc_bits; |
---|
649 | } |
---|
650 | } |
---|
651 | } |
---|
652 | |
---|
653 | /* padding */ |
---|
654 | for(i=0;i<padding;i++) |
---|
655 | ddvd_mpa_put_bits(p, 1, 0); |
---|
656 | |
---|
657 | /* flush */ |
---|
658 | ddvd_mpa_flush_put_bits(p); |
---|
659 | } |
---|
660 | |
---|
661 | int ddvd_mpa_encode_frame(unsigned char *frame, int buf_size, void *data) |
---|
662 | { |
---|
663 | short *samples = data; |
---|
664 | short smr[MPA_MAX_CHANNELS][SBLIMIT]; |
---|
665 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; |
---|
666 | int padding, i; |
---|
667 | |
---|
668 | for(i=0;i<NB_CHANNELS;i++) { |
---|
669 | ddvd_mpa_filter(i, samples + i, NB_CHANNELS); |
---|
670 | } |
---|
671 | |
---|
672 | for(i=0;i<NB_CHANNELS;i++) { |
---|
673 | ddvd_mpa_compute_scale_factors(ddvd_mpa_scale_code[i], ddvd_mpa_scale_factors[i], |
---|
674 | ddvd_mpa_sb_samples[i], ddvd_mpa_sblimit); |
---|
675 | } |
---|
676 | for(i=0;i<NB_CHANNELS;i++) { |
---|
677 | ddvd_mpa_psycho_acoustic_model(smr[i]); |
---|
678 | } |
---|
679 | ddvd_mpa_compute_bit_allocation(smr, bit_alloc, &padding); |
---|
680 | |
---|
681 | ddvd_mpa_init_put_bits(&pb, frame, MPA_MAX_CODED_FRAME_SIZE); |
---|
682 | |
---|
683 | ddvd_mpa_encode_frame_internal(bit_alloc, padding); |
---|
684 | |
---|
685 | ddvd_mpa_nb_samples += MPA_FRAME_SIZE; |
---|
686 | return ddvd_mpa_pbBufPtr(&pb) - pb.buf; |
---|
687 | } |
---|
688 | |
---|