QDM2 compatible decoder
[libav.git] / libavcodec / mpegaudio.c
CommitLineData
de6d9b64
FB
1/*
2 * The simplest mpeg audio layer 2 encoder
ff4ec49e 3 * Copyright (c) 2000, 2001 Fabrice Bellard.
de6d9b64 4 *
ff4ec49e
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
ff4ec49e
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
de6d9b64 18 */
983e3246
MN
19
20/**
21 * @file mpegaudio.c
22 * The simplest mpeg audio layer 2 encoder.
23 */
24
de6d9b64 25#include "avcodec.h"
caa336b4 26#include "bitstream.h"
de6d9b64
FB
27#include "mpegaudio.h"
28
afa982fd
FB
29/* currently, cannot change these constants (need to modify
30 quantization stage) */
0c1a9eda 31#define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
afa982fd 32#define FIX(a) ((int)((a) * (1 << FRAC_BITS)))
2456e28d
FB
33
34#define SAMPLES_BUF_SIZE 4096
35
36typedef struct MpegAudioContext {
37 PutBitContext pb;
38 int nb_channels;
39 int freq, bit_rate;
40 int lsf; /* 1 if mpeg2 low bitrate selected */
41 int bitrate_index; /* bit rate */
42 int freq_index;
43 int frame_size; /* frame size, in bits, without padding */
0c1a9eda 44 int64_t nb_samples; /* total number of samples encoded */
2456e28d
FB
45 /* padding computation */
46 int frame_frac, frame_frac_incr, do_padding;
47 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
48 int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
49 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
50 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
51 /* code to group 3 scale factors */
52 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
53 int sblimit; /* number of used subbands */
54 const unsigned char *alloc_table;
55} MpegAudioContext;
56
de6d9b64
FB
57/* define it to use floats in quantization (I don't like floats !) */
58//#define USE_FLOATS
59
de6d9b64
FB
60#include "mpegaudiotab.h"
61
5c91a675 62static int MPA_encode_init(AVCodecContext *avctx)
de6d9b64
FB
63{
64 MpegAudioContext *s = avctx->priv_data;
65 int freq = avctx->sample_rate;
66 int bitrate = avctx->bit_rate;
67 int channels = avctx->channels;
2456e28d 68 int i, v, table;
de6d9b64
FB
69 float a;
70
71 if (channels > 2)
72 return -1;
73 bitrate = bitrate / 1000;
74 s->nb_channels = channels;
75 s->freq = freq;
76 s->bit_rate = bitrate * 1000;
77 avctx->frame_size = MPA_FRAME_SIZE;
de6d9b64
FB
78
79 /* encoding freq */
80 s->lsf = 0;
81 for(i=0;i<3;i++) {
2456e28d 82 if (mpa_freq_tab[i] == freq)
de6d9b64 83 break;
2456e28d 84 if ((mpa_freq_tab[i] / 2) == freq) {
de6d9b64
FB
85 s->lsf = 1;
86 break;
87 }
88 }
bb0b93ba
MN
89 if (i == 3){
90 av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
de6d9b64 91 return -1;
bb0b93ba 92 }
de6d9b64
FB
93 s->freq_index = i;
94
95 /* encoding bitrate & frequency */
96 for(i=0;i<15;i++) {
2456e28d 97 if (mpa_bitrate_tab[s->lsf][1][i] == bitrate)
de6d9b64
FB
98 break;
99 }
bb0b93ba
MN
100 if (i == 15){
101 av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
de6d9b64 102 return -1;
bb0b93ba 103 }
de6d9b64
FB
104 s->bitrate_index = i;
105
106 /* compute total header size & pad bit */
107
108 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
109 s->frame_size = ((int)a) * 8;
110
111 /* frame fractional size to compute padding */
112 s->frame_frac = 0;
113 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
114
115 /* select the right allocation table */
2456e28d
FB
116 table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
117
de6d9b64
FB
118 /* number of used subbands */
119 s->sblimit = sblimit_table[table];
120 s->alloc_table = alloc_tables[table];
121
122#ifdef DEBUG
3d0ef6dd 123 av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
de6d9b64
FB
124 bitrate, freq, s->frame_size, table, s->frame_frac_incr);
125#endif
126
127 for(i=0;i<s->nb_channels;i++)
128 s->samples_offset[i] = 0;
129
2456e28d
FB
130 for(i=0;i<257;i++) {
131 int v;
afa982fd
FB
132 v = mpa_enwindow[i];
133#if WFRAC_BITS != 16
134 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
135#endif
2456e28d
FB
136 filter_bank[i] = v;
137 if ((i & 63) != 0)
138 v = -v;
139 if (i != 0)
140 filter_bank[512 - i] = v;
de6d9b64 141 }
2456e28d 142
de6d9b64
FB
143 for(i=0;i<64;i++) {
144 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
145 if (v <= 0)
146 v = 1;
147 scale_factor_table[i] = v;
148#ifdef USE_FLOATS
149 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
150#else
151#define P 15
152 scale_factor_shift[i] = 21 - P - (i / 3);
153 scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
154#endif
155 }
156 for(i=0;i<128;i++) {
157 v = i - 64;
158 if (v <= -3)
159 v = 0;
160 else if (v < 0)
161 v = 1;
162 else if (v == 0)
163 v = 2;
164 else if (v < 3)
165 v = 3;
166 else
167 v = 4;
168 scale_diff_table[i] = v;
169 }
170
171 for(i=0;i<17;i++) {
172 v = quant_bits[i];
173 if (v < 0)
174 v = -v;
175 else
176 v = v * 3;
177 total_quant_bits[i] = 12 * v;
178 }
179
492cd3a9
MN
180 avctx->coded_frame= avcodec_alloc_frame();
181 avctx->coded_frame->key_frame= 1;
182
de6d9b64
FB
183 return 0;
184}
185
2456e28d 186/* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
afa982fd 187static void idct32(int *out, int *tab)
de6d9b64
FB
188{
189 int i, j;
190 int *t, *t1, xr;
191 const int *xp = costab32;
192
193 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
194
195 t = tab + 30;
196 t1 = tab + 2;
197 do {
198 t[0] += t[-4];
199 t[1] += t[1 - 4];
200 t -= 4;
201 } while (t != t1);
202
203 t = tab + 28;
204 t1 = tab + 4;
205 do {
206 t[0] += t[-8];
207 t[1] += t[1-8];
208 t[2] += t[2-8];
209 t[3] += t[3-8];
210 t -= 8;
211 } while (t != t1);
212
213 t = tab;
214 t1 = tab + 32;
215 do {
216 t[ 3] = -t[ 3];
217 t[ 6] = -t[ 6];
218
219 t[11] = -t[11];
220 t[12] = -t[12];
221 t[13] = -t[13];
222 t[15] = -t[15];
223 t += 16;
224 } while (t != t1);
225
226
227 t = tab;
228 t1 = tab + 8;
229 do {
230 int x1, x2, x3, x4;
231
232 x3 = MUL(t[16], FIX(SQRT2*0.5));
233 x4 = t[0] - x3;
234 x3 = t[0] + x3;
235
236 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
237 x1 = MUL((t[8] - x2), xp[0]);
238 x2 = MUL((t[8] + x2), xp[1]);
239
240 t[ 0] = x3 + x1;
241 t[ 8] = x4 - x2;
242 t[16] = x4 + x2;
243 t[24] = x3 - x1;
244 t++;
245 } while (t != t1);
246
247 xp += 2;
248 t = tab;
249 t1 = tab + 4;
250 do {
251 xr = MUL(t[28],xp[0]);
252 t[28] = (t[0] - xr);
253 t[0] = (t[0] + xr);
254
255 xr = MUL(t[4],xp[1]);
256 t[ 4] = (t[24] - xr);
257 t[24] = (t[24] + xr);
258
259 xr = MUL(t[20],xp[2]);
260 t[20] = (t[8] - xr);
261 t[ 8] = (t[8] + xr);
262
263 xr = MUL(t[12],xp[3]);
264 t[12] = (t[16] - xr);
265 t[16] = (t[16] + xr);
266 t++;
267 } while (t != t1);
268 xp += 4;
269
270 for (i = 0; i < 4; i++) {
271 xr = MUL(tab[30-i*4],xp[0]);
272 tab[30-i*4] = (tab[i*4] - xr);
273 tab[ i*4] = (tab[i*4] + xr);
274
275 xr = MUL(tab[ 2+i*4],xp[1]);
276 tab[ 2+i*4] = (tab[28-i*4] - xr);
277 tab[28-i*4] = (tab[28-i*4] + xr);
278
279 xr = MUL(tab[31-i*4],xp[0]);
280 tab[31-i*4] = (tab[1+i*4] - xr);
281 tab[ 1+i*4] = (tab[1+i*4] + xr);
282
283 xr = MUL(tab[ 3+i*4],xp[1]);
284 tab[ 3+i*4] = (tab[29-i*4] - xr);
285 tab[29-i*4] = (tab[29-i*4] + xr);
286
287 xp += 2;
288 }
289
290 t = tab + 30;
291 t1 = tab + 1;
292 do {
293 xr = MUL(t1[0], *xp);
294 t1[0] = (t[0] - xr);
295 t[0] = (t[0] + xr);
296 t -= 2;
297 t1 += 2;
298 xp++;
299 } while (t >= tab);
300
301 for(i=0;i<32;i++) {
afa982fd 302 out[i] = tab[bitinv32[i]];
de6d9b64
FB
303 }
304}
305
afa982fd
FB
306#define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
307
de6d9b64
FB
308static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
309{
310 short *p, *q;
afa982fd
FB
311 int sum, offset, i, j;
312 int tmp[64];
de6d9b64
FB
313 int tmp1[32];
314 int *out;
315
316 // print_pow1(samples, 1152);
317
318 offset = s->samples_offset[ch];
319 out = &s->sb_samples[ch][0][0][0];
320 for(j=0;j<36;j++) {
321 /* 32 samples at once */
322 for(i=0;i<32;i++) {
323 s->samples_buf[ch][offset + (31 - i)] = samples[0];
324 samples += incr;
325 }
326
327 /* filter */
328 p = s->samples_buf[ch] + offset;
329 q = filter_bank;
330 /* maxsum = 23169 */
331 for(i=0;i<64;i++) {
332 sum = p[0*64] * q[0*64];
333 sum += p[1*64] * q[1*64];
334 sum += p[2*64] * q[2*64];
335 sum += p[3*64] * q[3*64];
336 sum += p[4*64] * q[4*64];
337 sum += p[5*64] * q[5*64];
338 sum += p[6*64] * q[6*64];
339 sum += p[7*64] * q[7*64];
afa982fd 340 tmp[i] = sum;
de6d9b64
FB
341 p++;
342 q++;
343 }
afa982fd
FB
344 tmp1[0] = tmp[16] >> WSHIFT;
345 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
346 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
de6d9b64 347
afa982fd 348 idct32(out, tmp1);
de6d9b64
FB
349
350 /* advance of 32 samples */
351 offset -= 32;
352 out += 32;
353 /* handle the wrap around */
354 if (offset < 0) {
355 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
356 s->samples_buf[ch], (512 - 32) * 2);
357 offset = SAMPLES_BUF_SIZE - 512;
358 }
359 }
360 s->samples_offset[ch] = offset;
361
362 // print_pow(s->sb_samples, 1152);
363}
364
365static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
366 unsigned char scale_factors[SBLIMIT][3],
367 int sb_samples[3][12][SBLIMIT],
368 int sblimit)
369{
370 int *p, vmax, v, n, i, j, k, code;
371 int index, d1, d2;
372 unsigned char *sf = &scale_factors[0][0];
373
374 for(j=0;j<sblimit;j++) {
375 for(i=0;i<3;i++) {
376 /* find the max absolute value */
377 p = &sb_samples[i][0][j];
378 vmax = abs(*p);
379 for(k=1;k<12;k++) {
380 p += SBLIMIT;
381 v = abs(*p);
382 if (v > vmax)
383 vmax = v;
384 }
385 /* compute the scale factor index using log 2 computations */
386 if (vmax > 0) {
935442b5 387 n = av_log2(vmax);
de6d9b64
FB
388 /* n is the position of the MSB of vmax. now
389 use at most 2 compares to find the index */
390 index = (21 - n) * 3 - 3;
391 if (index >= 0) {
392 while (vmax <= scale_factor_table[index+1])
393 index++;
394 } else {
395 index = 0; /* very unlikely case of overflow */
396 }
397 } else {
afa982fd 398 index = 62; /* value 63 is not allowed */
de6d9b64 399 }
afa982fd 400
de6d9b64
FB
401#if 0
402 printf("%2d:%d in=%x %x %d\n",
403 j, i, vmax, scale_factor_table[index], index);
404#endif
405 /* store the scale factor */
406 assert(index >=0 && index <= 63);
407 sf[i] = index;
408 }
409
410 /* compute the transmission factor : look if the scale factors
411 are close enough to each other */
412 d1 = scale_diff_table[sf[0] - sf[1] + 64];
413 d2 = scale_diff_table[sf[1] - sf[2] + 64];
414
415 /* handle the 25 cases */
416 switch(d1 * 5 + d2) {
417 case 0*5+0:
418 case 0*5+4:
419 case 3*5+4:
420 case 4*5+0:
421 case 4*5+4:
422 code = 0;
423 break;
424 case 0*5+1:
425 case 0*5+2:
426 case 4*5+1:
427 case 4*5+2:
428 code = 3;
429 sf[2] = sf[1];
430 break;
431 case 0*5+3:
432 case 4*5+3:
433 code = 3;
434 sf[1] = sf[2];
435 break;
436 case 1*5+0:
437 case 1*5+4:
438 case 2*5+4:
439 code = 1;
440 sf[1] = sf[0];
441 break;
442 case 1*5+1:
443 case 1*5+2:
444 case 2*5+0:
445 case 2*5+1:
446 case 2*5+2:
447 code = 2;
448 sf[1] = sf[2] = sf[0];
449 break;
450 case 2*5+3:
451 case 3*5+3:
452 code = 2;
453 sf[0] = sf[1] = sf[2];
454 break;
455 case 3*5+0:
456 case 3*5+1:
457 case 3*5+2:
458 code = 2;
459 sf[0] = sf[2] = sf[1];
460 break;
461 case 1*5+3:
462 code = 2;
463 if (sf[0] > sf[2])
464 sf[0] = sf[2];
465 sf[1] = sf[2] = sf[0];
466 break;
467 default:
9fe5a7b8 468 assert(0); //cant happen
88730be6 469 code = 0; /* kill warning */
de6d9b64
FB
470 }
471
472#if 0
473 printf("%d: %2d %2d %2d %d %d -> %d\n", j,
474 sf[0], sf[1], sf[2], d1, d2, code);
475#endif
476 scale_code[j] = code;
477 sf += 3;
478 }
479}
480
481/* The most important function : psycho acoustic module. In this
482 encoder there is basically none, so this is the worst you can do,
483 but also this is the simpler. */
484static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
485{
486 int i;
487
488 for(i=0;i<s->sblimit;i++) {
489 smr[i] = (int)(fixed_smr[i] * 10);
490 }
491}
492
493
494#define SB_NOTALLOCATED 0
495#define SB_ALLOCATED 1
496#define SB_NOMORE 2
497
498/* Try to maximize the smr while using a number of bits inferior to
499 the frame size. I tried to make the code simpler, faster and
500 smaller than other encoders :-) */
501static void compute_bit_allocation(MpegAudioContext *s,
502 short smr1[MPA_MAX_CHANNELS][SBLIMIT],
503 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
504 int *padding)
505{
506 int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
507 int incr;
508 short smr[MPA_MAX_CHANNELS][SBLIMIT];
509 unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
510 const unsigned char *alloc;
511
512 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
513 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
514 memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
515
516 /* compute frame size and padding */
517 max_frame_size = s->frame_size;
518 s->frame_frac += s->frame_frac_incr;
519 if (s->frame_frac >= 65536) {
520 s->frame_frac -= 65536;
521 s->do_padding = 1;
522 max_frame_size += 8;
523 } else {
524 s->do_padding = 0;
525 }
526
527 /* compute the header + bit alloc size */
528 current_frame_size = 32;
529 alloc = s->alloc_table;
530 for(i=0;i<s->sblimit;i++) {
531 incr = alloc[0];
532 current_frame_size += incr * s->nb_channels;
533 alloc += 1 << incr;
534 }
535 for(;;) {
536 /* look for the subband with the largest signal to mask ratio */
537 max_sb = -1;
538 max_ch = -1;
539 max_smr = 0x80000000;
540 for(ch=0;ch<s->nb_channels;ch++) {
541 for(i=0;i<s->sblimit;i++) {
542 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
543 max_smr = smr[ch][i];
544 max_sb = i;
545 max_ch = ch;
546 }
547 }
548 }
549#if 0
550 printf("current=%d max=%d max_sb=%d alloc=%d\n",
551 current_frame_size, max_frame_size, max_sb,
552 bit_alloc[max_sb]);
553#endif
554 if (max_sb < 0)
555 break;
556
557 /* find alloc table entry (XXX: not optimal, should use
558 pointer table) */
559 alloc = s->alloc_table;
560 for(i=0;i<max_sb;i++) {
561 alloc += 1 << alloc[0];
562 }
563
564 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
565 /* nothing was coded for this band: add the necessary bits */
566 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
567 incr += total_quant_bits[alloc[1]];
568 } else {
569 /* increments bit allocation */
570 b = bit_alloc[max_ch][max_sb];
571 incr = total_quant_bits[alloc[b + 1]] -
572 total_quant_bits[alloc[b]];
573 }
574
575 if (current_frame_size + incr <= max_frame_size) {
576 /* can increase size */
577 b = ++bit_alloc[max_ch][max_sb];
578 current_frame_size += incr;
579 /* decrease smr by the resolution we added */
580 smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
581 /* max allocation size reached ? */
582 if (b == ((1 << alloc[0]) - 1))
583 subband_status[max_ch][max_sb] = SB_NOMORE;
584 else
585 subband_status[max_ch][max_sb] = SB_ALLOCATED;
586 } else {
587 /* cannot increase the size of this subband */
588 subband_status[max_ch][max_sb] = SB_NOMORE;
589 }
590 }
591 *padding = max_frame_size - current_frame_size;
592 assert(*padding >= 0);
593
594#if 0
595 for(i=0;i<s->sblimit;i++) {
596 printf("%d ", bit_alloc[i]);
597 }
598 printf("\n");
599#endif
600}
601
602/*
603 * Output the mpeg audio layer 2 frame. Note how the code is small
604 * compared to other encoders :-)
605 */
606static void encode_frame(MpegAudioContext *s,
607 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
608 int padding)
609{
610 int i, j, k, l, bit_alloc_bits, b, ch;
611 unsigned char *sf;
612 int q[3];
613 PutBitContext *p = &s->pb;
614
615 /* header */
616
617 put_bits(p, 12, 0xfff);
618 put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
619 put_bits(p, 2, 4-2); /* layer 2 */
620 put_bits(p, 1, 1); /* no error protection */
621 put_bits(p, 4, s->bitrate_index);
622 put_bits(p, 2, s->freq_index);
623 put_bits(p, 1, s->do_padding); /* use padding */
624 put_bits(p, 1, 0); /* private_bit */
625 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
626 put_bits(p, 2, 0); /* mode_ext */
627 put_bits(p, 1, 0); /* no copyright */
628 put_bits(p, 1, 1); /* original */
629 put_bits(p, 2, 0); /* no emphasis */
630
631 /* bit allocation */
632 j = 0;
633 for(i=0;i<s->sblimit;i++) {
634 bit_alloc_bits = s->alloc_table[j];
635 for(ch=0;ch<s->nb_channels;ch++) {
636 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
637 }
638 j += 1 << bit_alloc_bits;
639 }
640
641 /* scale codes */
642 for(i=0;i<s->sblimit;i++) {
643 for(ch=0;ch<s->nb_channels;ch++) {
644 if (bit_alloc[ch][i])
645 put_bits(p, 2, s->scale_code[ch][i]);
646 }
647 }
648
649 /* scale factors */
650 for(i=0;i<s->sblimit;i++) {
651 for(ch=0;ch<s->nb_channels;ch++) {
652 if (bit_alloc[ch][i]) {
653 sf = &s->scale_factors[ch][i][0];
654 switch(s->scale_code[ch][i]) {
655 case 0:
656 put_bits(p, 6, sf[0]);
657 put_bits(p, 6, sf[1]);
658 put_bits(p, 6, sf[2]);
659 break;
660 case 3:
661 case 1:
662 put_bits(p, 6, sf[0]);
663 put_bits(p, 6, sf[2]);
664 break;
665 case 2:
666 put_bits(p, 6, sf[0]);
667 break;
668 }
669 }
670 }
671 }
672
673 /* quantization & write sub band samples */
674
675 for(k=0;k<3;k++) {
676 for(l=0;l<12;l+=3) {
677 j = 0;
678 for(i=0;i<s->sblimit;i++) {
679 bit_alloc_bits = s->alloc_table[j];
680 for(ch=0;ch<s->nb_channels;ch++) {
681 b = bit_alloc[ch][i];
682 if (b) {
683 int qindex, steps, m, sample, bits;
684 /* we encode 3 sub band samples of the same sub band at a time */
685 qindex = s->alloc_table[j+b];
686 steps = quant_steps[qindex];
687 for(m=0;m<3;m++) {
688 sample = s->sb_samples[ch][k][l + m][i];
689 /* divide by scale factor */
690#ifdef USE_FLOATS
691 {
692 float a;
693 a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
694 q[m] = (int)((a + 1.0) * steps * 0.5);
695 }
696#else
697 {
698 int q1, e, shift, mult;
699 e = s->scale_factors[ch][i][k];
700 shift = scale_factor_shift[e];
701 mult = scale_factor_mult[e];
702
703 /* normalize to P bits */
704 if (shift < 0)
705 q1 = sample << (-shift);
706 else
707 q1 = sample >> shift;
708 q1 = (q1 * mult) >> P;
709 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
710 }
711#endif
712 if (q[m] >= steps)
713 q[m] = steps - 1;
714 assert(q[m] >= 0 && q[m] < steps);
715 }
716 bits = quant_bits[qindex];
717 if (bits < 0) {
718 /* group the 3 values to save bits */
719 put_bits(p, -bits,
720 q[0] + steps * (q[1] + steps * q[2]));
721#if 0
722 printf("%d: gr1 %d\n",
723 i, q[0] + steps * (q[1] + steps * q[2]));
724#endif
725 } else {
726#if 0
727 printf("%d: gr3 %d %d %d\n",
728 i, q[0], q[1], q[2]);
729#endif
730 put_bits(p, bits, q[0]);
731 put_bits(p, bits, q[1]);
732 put_bits(p, bits, q[2]);
733 }
734 }
735 }
736 /* next subband in alloc table */
737 j += 1 << bit_alloc_bits;
738 }
739 }
740 }
741
742 /* padding */
743 for(i=0;i<padding;i++)
744 put_bits(p, 1, 0);
745
746 /* flush */
747 flush_put_bits(p);
748}
749
5c91a675
ZK
750static int MPA_encode_frame(AVCodecContext *avctx,
751 unsigned char *frame, int buf_size, void *data)
de6d9b64
FB
752{
753 MpegAudioContext *s = avctx->priv_data;
754 short *samples = data;
755 short smr[MPA_MAX_CHANNELS][SBLIMIT];
756 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
757 int padding, i;
758
759 for(i=0;i<s->nb_channels;i++) {
760 filter(s, i, samples + i, s->nb_channels);
761 }
762
763 for(i=0;i<s->nb_channels;i++) {
764 compute_scale_factors(s->scale_code[i], s->scale_factors[i],
765 s->sb_samples[i], s->sblimit);
766 }
767 for(i=0;i<s->nb_channels;i++) {
768 psycho_acoustic_model(s, smr[i]);
769 }
770 compute_bit_allocation(s, smr, bit_alloc, &padding);
771
ed7debda 772 init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
de6d9b64
FB
773
774 encode_frame(s, bit_alloc, padding);
775
776 s->nb_samples += MPA_FRAME_SIZE;
17592475 777 return pbBufPtr(&s->pb) - s->pb.buf;
de6d9b64
FB
778}
779
492cd3a9
MN
780static int MPA_encode_close(AVCodecContext *avctx)
781{
782 av_freep(&avctx->coded_frame);
8e1e6f31 783 return 0;
492cd3a9 784}
de6d9b64 785
983ea0bc 786#ifdef CONFIG_MP2_ENCODER
de6d9b64
FB
787AVCodec mp2_encoder = {
788 "mp2",
789 CODEC_TYPE_AUDIO,
790 CODEC_ID_MP2,
791 sizeof(MpegAudioContext),
792 MPA_encode_init,
793 MPA_encode_frame,
492cd3a9 794 MPA_encode_close,
de6d9b64
FB
795 NULL,
796};
983ea0bc 797#endif // CONFIG_MP2_ENCODER
cd4af68a
ZK
798
799#undef FIX