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