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 
28 
29 #include "avcodec.h"
30 #include "internal.h"
31 #include "put_bits.h"
32 
33 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
34 #define WFRAC_BITS 14 /* fractional bits for window */
35 
36 #include "mpegaudio.h"
37 #include "mpegaudiodsp.h"
38 
39 /* currently, cannot change these constants (need to modify
40  quantization stage) */
41 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
42 
43 #define SAMPLES_BUF_SIZE 4096
44 
45 typedef struct MpegAudioContext {
48  int lsf; /* 1 if mpeg2 low bitrate selected */
49  int bitrate_index; /* bit rate */
51  int frame_size; /* frame size, in bits, without padding */
52  /* padding computation */
54  short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
55  int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
57  unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
58  /* code to group 3 scale factors */
60  int sblimit; /* number of used subbands */
61  const unsigned char *alloc_table;
63 
64 /* define it to use floats in quantization (I don't like floats !) */
65 #define USE_FLOATS
66 
67 #include "mpegaudiodata.h"
68 #include "mpegaudiotab.h"
69 
71 {
72  MpegAudioContext *s = avctx->priv_data;
73  int freq = avctx->sample_rate;
74  int bitrate = avctx->bit_rate;
75  int channels = avctx->channels;
76  int i, v, table;
77  float a;
78 
79  if (channels <= 0 || channels > 2){
80  av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
81  return AVERROR(EINVAL);
82  }
83  bitrate = bitrate / 1000;
84  s->nb_channels = channels;
85  avctx->frame_size = MPA_FRAME_SIZE;
86  avctx->delay = 512 - 32 + 1;
87 
88  /* encoding freq */
89  s->lsf = 0;
90  for(i=0;i<3;i++) {
91  if (avpriv_mpa_freq_tab[i] == freq)
92  break;
93  if ((avpriv_mpa_freq_tab[i] / 2) == freq) {
94  s->lsf = 1;
95  break;
96  }
97  }
98  if (i == 3){
99  av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
100  return AVERROR(EINVAL);
101  }
102  s->freq_index = i;
103 
104  /* encoding bitrate & frequency */
105  for(i=0;i<15;i++) {
106  if (avpriv_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
107  break;
108  }
109  if (i == 15){
110  av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
111  return AVERROR(EINVAL);
112  }
113  s->bitrate_index = i;
114 
115  /* compute total header size & pad bit */
116 
117  a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
118  s->frame_size = ((int)a) * 8;
119 
120  /* frame fractional size to compute padding */
121  s->frame_frac = 0;
122  s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
123 
124  /* select the right allocation table */
125  table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
126 
127  /* number of used subbands */
128  s->sblimit = ff_mpa_sblimit_table[table];
129  s->alloc_table = ff_mpa_alloc_tables[table];
130 
131  av_dlog(avctx, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
132  bitrate, freq, s->frame_size, table, s->frame_frac_incr);
133 
134  for(i=0;i<s->nb_channels;i++)
135  s->samples_offset[i] = 0;
136 
137  for(i=0;i<257;i++) {
138  int v;
139  v = ff_mpa_enwindow[i];
140 #if WFRAC_BITS != 16
141  v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
142 #endif
143  filter_bank[i] = v;
144  if ((i & 63) != 0)
145  v = -v;
146  if (i != 0)
147  filter_bank[512 - i] = v;
148  }
149 
150  for(i=0;i<64;i++) {
151  v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
152  if (v <= 0)
153  v = 1;
154  scale_factor_table[i] = v;
155 #ifdef USE_FLOATS
156  scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
157 #else
158 #define P 15
159  scale_factor_shift[i] = 21 - P - (i / 3);
160  scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
161 #endif
162  }
163  for(i=0;i<128;i++) {
164  v = i - 64;
165  if (v <= -3)
166  v = 0;
167  else if (v < 0)
168  v = 1;
169  else if (v == 0)
170  v = 2;
171  else if (v < 3)
172  v = 3;
173  else
174  v = 4;
175  scale_diff_table[i] = v;
176  }
177 
178  for(i=0;i<17;i++) {
179  v = ff_mpa_quant_bits[i];
180  if (v < 0)
181  v = -v;
182  else
183  v = v * 3;
184  total_quant_bits[i] = 12 * v;
185  }
186 
187 #if FF_API_OLD_ENCODE_AUDIO
189  if (!avctx->coded_frame)
190  return AVERROR(ENOMEM);
191 #endif
192 
193  return 0;
194 }
195 
196 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
197 static void idct32(int *out, int *tab)
198 {
199  int i, j;
200  int *t, *t1, xr;
201  const int *xp = costab32;
202 
203  for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
204 
205  t = tab + 30;
206  t1 = tab + 2;
207  do {
208  t[0] += t[-4];
209  t[1] += t[1 - 4];
210  t -= 4;
211  } while (t != t1);
212 
213  t = tab + 28;
214  t1 = tab + 4;
215  do {
216  t[0] += t[-8];
217  t[1] += t[1-8];
218  t[2] += t[2-8];
219  t[3] += t[3-8];
220  t -= 8;
221  } while (t != t1);
222 
223  t = tab;
224  t1 = tab + 32;
225  do {
226  t[ 3] = -t[ 3];
227  t[ 6] = -t[ 6];
228 
229  t[11] = -t[11];
230  t[12] = -t[12];
231  t[13] = -t[13];
232  t[15] = -t[15];
233  t += 16;
234  } while (t != t1);
235 
236 
237  t = tab;
238  t1 = tab + 8;
239  do {
240  int x1, x2, x3, x4;
241 
242  x3 = MUL(t[16], FIX(SQRT2*0.5));
243  x4 = t[0] - x3;
244  x3 = t[0] + x3;
245 
246  x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
247  x1 = MUL((t[8] - x2), xp[0]);
248  x2 = MUL((t[8] + x2), xp[1]);
249 
250  t[ 0] = x3 + x1;
251  t[ 8] = x4 - x2;
252  t[16] = x4 + x2;
253  t[24] = x3 - x1;
254  t++;
255  } while (t != t1);
256 
257  xp += 2;
258  t = tab;
259  t1 = tab + 4;
260  do {
261  xr = MUL(t[28],xp[0]);
262  t[28] = (t[0] - xr);
263  t[0] = (t[0] + xr);
264 
265  xr = MUL(t[4],xp[1]);
266  t[ 4] = (t[24] - xr);
267  t[24] = (t[24] + xr);
268 
269  xr = MUL(t[20],xp[2]);
270  t[20] = (t[8] - xr);
271  t[ 8] = (t[8] + xr);
272 
273  xr = MUL(t[12],xp[3]);
274  t[12] = (t[16] - xr);
275  t[16] = (t[16] + xr);
276  t++;
277  } while (t != t1);
278  xp += 4;
279 
280  for (i = 0; i < 4; i++) {
281  xr = MUL(tab[30-i*4],xp[0]);
282  tab[30-i*4] = (tab[i*4] - xr);
283  tab[ i*4] = (tab[i*4] + xr);
284 
285  xr = MUL(tab[ 2+i*4],xp[1]);
286  tab[ 2+i*4] = (tab[28-i*4] - xr);
287  tab[28-i*4] = (tab[28-i*4] + xr);
288 
289  xr = MUL(tab[31-i*4],xp[0]);
290  tab[31-i*4] = (tab[1+i*4] - xr);
291  tab[ 1+i*4] = (tab[1+i*4] + xr);
292 
293  xr = MUL(tab[ 3+i*4],xp[1]);
294  tab[ 3+i*4] = (tab[29-i*4] - xr);
295  tab[29-i*4] = (tab[29-i*4] + xr);
296 
297  xp += 2;
298  }
299 
300  t = tab + 30;
301  t1 = tab + 1;
302  do {
303  xr = MUL(t1[0], *xp);
304  t1[0] = (t[0] - xr);
305  t[0] = (t[0] + xr);
306  t -= 2;
307  t1 += 2;
308  xp++;
309  } while (t >= tab);
310 
311  for(i=0;i<32;i++) {
312  out[i] = tab[bitinv32[i]];
313  }
314 }
315 
316 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
317 
318 static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
319 {
320  short *p, *q;
321  int sum, offset, i, j;
322  int tmp[64];
323  int tmp1[32];
324  int *out;
325 
326  offset = s->samples_offset[ch];
327  out = &s->sb_samples[ch][0][0][0];
328  for(j=0;j<36;j++) {
329  /* 32 samples at once */
330  for(i=0;i<32;i++) {
331  s->samples_buf[ch][offset + (31 - i)] = samples[0];
332  samples += incr;
333  }
334 
335  /* filter */
336  p = s->samples_buf[ch] + offset;
337  q = filter_bank;
338  /* maxsum = 23169 */
339  for(i=0;i<64;i++) {
340  sum = p[0*64] * q[0*64];
341  sum += p[1*64] * q[1*64];
342  sum += p[2*64] * q[2*64];
343  sum += p[3*64] * q[3*64];
344  sum += p[4*64] * q[4*64];
345  sum += p[5*64] * q[5*64];
346  sum += p[6*64] * q[6*64];
347  sum += p[7*64] * q[7*64];
348  tmp[i] = sum;
349  p++;
350  q++;
351  }
352  tmp1[0] = tmp[16] >> WSHIFT;
353  for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
354  for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
355 
356  idct32(out, tmp1);
357 
358  /* advance of 32 samples */
359  offset -= 32;
360  out += 32;
361  /* handle the wrap around */
362  if (offset < 0) {
363  memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
364  s->samples_buf[ch], (512 - 32) * 2);
365  offset = SAMPLES_BUF_SIZE - 512;
366  }
367  }
368  s->samples_offset[ch] = offset;
369 }
370 
371 static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
372  unsigned char scale_factors[SBLIMIT][3],
373  int sb_samples[3][12][SBLIMIT],
374  int sblimit)
375 {
376  int *p, vmax, v, n, i, j, k, code;
377  int index, d1, d2;
378  unsigned char *sf = &scale_factors[0][0];
379 
380  for(j=0;j<sblimit;j++) {
381  for(i=0;i<3;i++) {
382  /* find the max absolute value */
383  p = &sb_samples[i][0][j];
384  vmax = abs(*p);
385  for(k=1;k<12;k++) {
386  p += SBLIMIT;
387  v = abs(*p);
388  if (v > vmax)
389  vmax = v;
390  }
391  /* compute the scale factor index using log 2 computations */
392  if (vmax > 1) {
393  n = av_log2(vmax);
394  /* n is the position of the MSB of vmax. now
395  use at most 2 compares to find the index */
396  index = (21 - n) * 3 - 3;
397  if (index >= 0) {
398  while (vmax <= scale_factor_table[index+1])
399  index++;
400  } else {
401  index = 0; /* very unlikely case of overflow */
402  }
403  } else {
404  index = 62; /* value 63 is not allowed */
405  }
406 
407  av_dlog(NULL, "%2d:%d in=%x %x %d\n",
408  j, i, vmax, scale_factor_table[index], index);
409  /* store the scale factor */
410  assert(index >=0 && index <= 63);
411  sf[i] = index;
412  }
413 
414  /* compute the transmission factor : look if the scale factors
415  are close enough to each other */
416  d1 = scale_diff_table[sf[0] - sf[1] + 64];
417  d2 = scale_diff_table[sf[1] - sf[2] + 64];
418 
419  /* handle the 25 cases */
420  switch(d1 * 5 + d2) {
421  case 0*5+0:
422  case 0*5+4:
423  case 3*5+4:
424  case 4*5+0:
425  case 4*5+4:
426  code = 0;
427  break;
428  case 0*5+1:
429  case 0*5+2:
430  case 4*5+1:
431  case 4*5+2:
432  code = 3;
433  sf[2] = sf[1];
434  break;
435  case 0*5+3:
436  case 4*5+3:
437  code = 3;
438  sf[1] = sf[2];
439  break;
440  case 1*5+0:
441  case 1*5+4:
442  case 2*5+4:
443  code = 1;
444  sf[1] = sf[0];
445  break;
446  case 1*5+1:
447  case 1*5+2:
448  case 2*5+0:
449  case 2*5+1:
450  case 2*5+2:
451  code = 2;
452  sf[1] = sf[2] = sf[0];
453  break;
454  case 2*5+3:
455  case 3*5+3:
456  code = 2;
457  sf[0] = sf[1] = sf[2];
458  break;
459  case 3*5+0:
460  case 3*5+1:
461  case 3*5+2:
462  code = 2;
463  sf[0] = sf[2] = sf[1];
464  break;
465  case 1*5+3:
466  code = 2;
467  if (sf[0] > sf[2])
468  sf[0] = sf[2];
469  sf[1] = sf[2] = sf[0];
470  break;
471  default:
472  assert(0); //cannot happen
473  code = 0; /* kill warning */
474  }
475 
476  av_dlog(NULL, "%d: %2d %2d %2d %d %d -> %d\n", j,
477  sf[0], sf[1], sf[2], d1, d2, code);
478  scale_code[j] = code;
479  sf += 3;
480  }
481 }
482 
483 /* The most important function : psycho acoustic module. In this
484  encoder there is basically none, so this is the worst you can do,
485  but also this is the simpler. */
486 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
487 {
488  int i;
489 
490  for(i=0;i<s->sblimit;i++) {
491  smr[i] = (int)(fixed_smr[i] * 10);
492  }
493 }
494 
495 
496 #define SB_NOTALLOCATED 0
497 #define SB_ALLOCATED 1
498 #define SB_NOMORE 2
499 
500 /* Try to maximize the smr while using a number of bits inferior to
501  the frame size. I tried to make the code simpler, faster and
502  smaller than other encoders :-) */
504  short smr1[MPA_MAX_CHANNELS][SBLIMIT],
505  unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
506  int *padding)
507 {
508  int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
509  int incr;
510  short smr[MPA_MAX_CHANNELS][SBLIMIT];
511  unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
512  const unsigned char *alloc;
513 
514  memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
515  memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
516  memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
517 
518  /* compute frame size and padding */
519  max_frame_size = s->frame_size;
520  s->frame_frac += s->frame_frac_incr;
521  if (s->frame_frac >= 65536) {
522  s->frame_frac -= 65536;
523  s->do_padding = 1;
524  max_frame_size += 8;
525  } else {
526  s->do_padding = 0;
527  }
528 
529  /* compute the header + bit alloc size */
530  current_frame_size = 32;
531  alloc = s->alloc_table;
532  for(i=0;i<s->sblimit;i++) {
533  incr = alloc[0];
534  current_frame_size += incr * s->nb_channels;
535  alloc += 1 << incr;
536  }
537  for(;;) {
538  /* look for the subband with the largest signal to mask ratio */
539  max_sb = -1;
540  max_ch = -1;
541  max_smr = INT_MIN;
542  for(ch=0;ch<s->nb_channels;ch++) {
543  for(i=0;i<s->sblimit;i++) {
544  if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
545  max_smr = smr[ch][i];
546  max_sb = i;
547  max_ch = ch;
548  }
549  }
550  }
551  if (max_sb < 0)
552  break;
553  av_dlog(NULL, "current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n",
554  current_frame_size, max_frame_size, max_sb, max_ch,
555  bit_alloc[max_ch][max_sb]);
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 
595 /*
596  * Output the mpeg audio layer 2 frame. Note how the code is small
597  * compared to other encoders :-)
598  */
600  unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
601  int padding)
602 {
603  int i, j, k, l, bit_alloc_bits, b, ch;
604  unsigned char *sf;
605  int q[3];
606  PutBitContext *p = &s->pb;
607 
608  /* header */
609 
610  put_bits(p, 12, 0xfff);
611  put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
612  put_bits(p, 2, 4-2); /* layer 2 */
613  put_bits(p, 1, 1); /* no error protection */
614  put_bits(p, 4, s->bitrate_index);
615  put_bits(p, 2, s->freq_index);
616  put_bits(p, 1, s->do_padding); /* use padding */
617  put_bits(p, 1, 0); /* private_bit */
618  put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
619  put_bits(p, 2, 0); /* mode_ext */
620  put_bits(p, 1, 0); /* no copyright */
621  put_bits(p, 1, 1); /* original */
622  put_bits(p, 2, 0); /* no emphasis */
623 
624  /* bit allocation */
625  j = 0;
626  for(i=0;i<s->sblimit;i++) {
627  bit_alloc_bits = s->alloc_table[j];
628  for(ch=0;ch<s->nb_channels;ch++) {
629  put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
630  }
631  j += 1 << bit_alloc_bits;
632  }
633 
634  /* scale codes */
635  for(i=0;i<s->sblimit;i++) {
636  for(ch=0;ch<s->nb_channels;ch++) {
637  if (bit_alloc[ch][i])
638  put_bits(p, 2, s->scale_code[ch][i]);
639  }
640  }
641 
642  /* scale factors */
643  for(i=0;i<s->sblimit;i++) {
644  for(ch=0;ch<s->nb_channels;ch++) {
645  if (bit_alloc[ch][i]) {
646  sf = &s->scale_factors[ch][i][0];
647  switch(s->scale_code[ch][i]) {
648  case 0:
649  put_bits(p, 6, sf[0]);
650  put_bits(p, 6, sf[1]);
651  put_bits(p, 6, sf[2]);
652  break;
653  case 3:
654  case 1:
655  put_bits(p, 6, sf[0]);
656  put_bits(p, 6, sf[2]);
657  break;
658  case 2:
659  put_bits(p, 6, sf[0]);
660  break;
661  }
662  }
663  }
664  }
665 
666  /* quantization & write sub band samples */
667 
668  for(k=0;k<3;k++) {
669  for(l=0;l<12;l+=3) {
670  j = 0;
671  for(i=0;i<s->sblimit;i++) {
672  bit_alloc_bits = s->alloc_table[j];
673  for(ch=0;ch<s->nb_channels;ch++) {
674  b = bit_alloc[ch][i];
675  if (b) {
676  int qindex, steps, m, sample, bits;
677  /* we encode 3 sub band samples of the same sub band at a time */
678  qindex = s->alloc_table[j+b];
679  steps = ff_mpa_quant_steps[qindex];
680  for(m=0;m<3;m++) {
681  sample = s->sb_samples[ch][k][l + m][i];
682  /* divide by scale factor */
683 #ifdef USE_FLOATS
684  {
685  float a;
686  a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
687  q[m] = (int)((a + 1.0) * steps * 0.5);
688  }
689 #else
690  {
691  int q1, e, shift, mult;
692  e = s->scale_factors[ch][i][k];
693  shift = scale_factor_shift[e];
694  mult = scale_factor_mult[e];
695 
696  /* normalize to P bits */
697  if (shift < 0)
698  q1 = sample << (-shift);
699  else
700  q1 = sample >> shift;
701  q1 = (q1 * mult) >> P;
702  q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
703  }
704 #endif
705  if (q[m] >= steps)
706  q[m] = steps - 1;
707  assert(q[m] >= 0 && q[m] < steps);
708  }
709  bits = ff_mpa_quant_bits[qindex];
710  if (bits < 0) {
711  /* group the 3 values to save bits */
712  put_bits(p, -bits,
713  q[0] + steps * (q[1] + steps * q[2]));
714  } else {
715  put_bits(p, bits, q[0]);
716  put_bits(p, bits, q[1]);
717  put_bits(p, bits, q[2]);
718  }
719  }
720  }
721  /* next subband in alloc table */
722  j += 1 << bit_alloc_bits;
723  }
724  }
725  }
726 
727  /* padding */
728  for(i=0;i<padding;i++)
729  put_bits(p, 1, 0);
730 
731  /* flush */
732  flush_put_bits(p);
733 }
734 
735 static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
736  const AVFrame *frame, int *got_packet_ptr)
737 {
738  MpegAudioContext *s = avctx->priv_data;
739  const int16_t *samples = (const int16_t *)frame->data[0];
740  short smr[MPA_MAX_CHANNELS][SBLIMIT];
741  unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
742  int padding, i, ret;
743 
744  for(i=0;i<s->nb_channels;i++) {
745  filter(s, i, samples + i, s->nb_channels);
746  }
747 
748  for(i=0;i<s->nb_channels;i++) {
750  s->sb_samples[i], s->sblimit);
751  }
752  for(i=0;i<s->nb_channels;i++) {
753  psycho_acoustic_model(s, smr[i]);
754  }
755  compute_bit_allocation(s, smr, bit_alloc, &padding);
756 
757  if ((ret = ff_alloc_packet(avpkt, MPA_MAX_CODED_FRAME_SIZE))) {
758  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
759  return ret;
760  }
761 
762  init_put_bits(&s->pb, avpkt->data, avpkt->size);
763 
764  encode_frame(s, bit_alloc, padding);
765 
766  if (frame->pts != AV_NOPTS_VALUE)
767  avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
768 
769  avpkt->size = put_bits_count(&s->pb) / 8;
770  *got_packet_ptr = 1;
771  return 0;
772 }
773 
775 {
776 #if FF_API_OLD_ENCODE_AUDIO
777  av_freep(&avctx->coded_frame);
778 #endif
779  return 0;
780 }
781 
782 static const AVCodecDefault mp2_defaults[] = {
783  { "b", "128k" },
784  { NULL },
785 };
786 
788  .name = "mp2",
789  .type = AVMEDIA_TYPE_AUDIO,
790  .id = AV_CODEC_ID_MP2,
791  .priv_data_size = sizeof(MpegAudioContext),
793  .encode2 = MPA_encode_frame,
795  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
797  .supported_samplerates = (const int[]){
798  44100, 48000, 32000, 22050, 24000, 16000, 0
799  },
800  .channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
802  0 },
803  .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
804  .defaults = mp2_defaults,
805 };
#define MPA_STEREO
Definition: mpegaudio.h:45
#define MPA_MAX_CODED_FRAME_SIZE
Definition: mpegaudio.h:39
static int16_t * samples
#define SB_ALLOCATED
Definition: mpegaudioenc.c:497
#define SBLIMIT
Definition: mpegaudio.h:43
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
#define WSHIFT
Definition: mpegaudioenc.c:316
static const unsigned char nb_scale_factors[4]
Definition: mpegaudiotab.h:116
int size
Definition: avcodec.h:916
const int ff_mpa_quant_bits[17]
Definition: mpegaudiodata.c:55
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static int scale_factor_table[64]
Definition: mpegaudiotab.h:84
#define AV_CH_LAYOUT_STEREO
signed 16 bits
Definition: samplefmt.h:52
#define sample
AVCodec.
Definition: avcodec.h:2960
static void compute_scale_factors(unsigned char scale_code[SBLIMIT], unsigned char scale_factors[SBLIMIT][3], int sb_samples[3][12][SBLIMIT], int sblimit)
Definition: mpegaudioenc.c:371
mpeg audio layer common tables.
#define WFRAC_BITS
Definition: mpegaudioenc.c:34
const int32_t ff_mpa_enwindow[257]
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
uint8_t bits
Definition: crc.c:31
static int32_t scale_factor_mult[15][3]
Definition: mpegaudiodec.c:151
static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: mpegaudioenc.c:735
static const int costab32[30]
Definition: mpegaudiotab.h:38
static unsigned char scale_diff_table[128]
Definition: mpegaudiotab.h:91
const int ff_mpa_quant_steps[17]
Definition: mpegaudiodata.c:47
static void idct32(int *out, int *tab)
Definition: mpegaudioenc.c:197
const uint16_t avpriv_mpa_freq_tab[3]
Definition: mpegaudiodata.c:40
#define b
Definition: input.c:52
const unsigned char *const ff_mpa_alloc_tables[5]
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
Definition: avcodec.h:1088
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]
Definition: mpegaudioenc.c:57
mpeg audio layer 2 tables.
static av_cold int MPA_encode_init(AVCodecContext *avctx)
Definition: mpegaudioenc.c:70
#define SQRT2
Definition: mpegaudiotab.h:36
uint8_t * data
Definition: avcodec.h:915
#define SAMPLES_BUF_SIZE
Definition: mpegaudioenc.c:43
#define FIX(x)
Definition: jrevdct.c:143
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]
Definition: mpegaudioenc.c:59
static int bit_alloc(AC3EncodeContext *s, int snr_offset)
Run the bit allocation with a given SNR offset.
Definition: ac3enc.c:1062
static const unsigned short quant_snr[17]
Definition: mpegaudiotab.h:99
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static int16_t filter_bank[512]
Definition: mpegaudiotab.h:82
AVCodec ff_mp2_encoder
Definition: mpegaudioenc.c:787
sample_fmts
Definition: avconv_filter.c:63
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
#define t1
Definition: regdef.h:29
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static const int bitinv32[32]
Definition: mpegaudiotab.h:74
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:136
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:70
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:318
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:616
const unsigned char * alloc_table
Definition: mpegaudioenc.c:61
#define MPA_MAX_CHANNELS
Definition: mpegaudio.h:41
int bit_rate
the average bitrate
Definition: avcodec.h:1404
audio channel layout utility functions
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:878
PutBitContext pb
Definition: mpegaudioenc.c:46
static av_cold int MPA_encode_close(AVCodecContext *avctx)
Definition: mpegaudioenc.c:774
#define SB_NOTALLOCATED
Definition: mpegaudioenc.c:496
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2124
static void encode_frame(MpegAudioContext *s, unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int padding)
Definition: mpegaudioenc.c:599
NULL
Definition: eval.c:52
external API header
int samples_offset[MPA_MAX_CHANNELS]
Definition: mpegaudioenc.c:55
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
int sample_rate
samples per second
Definition: avcodec.h:2104
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
Definition: mpegaudioenc.c:486
static const float fixed_smr[SBLIMIT]
Definition: mpegaudiotab.h:109
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
struct MpegAudioContext MpegAudioContext
static int16_t mult(Float11 *f1, Float11 *f2)
Definition: g726.c:56
#define MUL(a, b)
Definition: mpegaudioenc.c:41
int index
Definition: gxfenc.c:72
#define MPA_MONO
Definition: mpegaudio.h:48
short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]
Definition: mpegaudioenc.c:54
static const AVCodecDefault mp2_defaults[]
Definition: mpegaudioenc.c:782
#define SB_NOMORE
Definition: mpegaudioenc.c:498
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
static int8_t scale_factor_shift[64]
Definition: mpegaudiotab.h:88
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:86
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
mpeg audio declarations for both encoder and decoder.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:52
const int ff_mpa_sblimit_table[5]
Definition: mpegaudiodata.c:45
void * priv_data
Definition: avcodec.h:1382
int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
Definition: mpegaudio.c:31
int channels
number of audio channels
Definition: avcodec.h:2105
#define av_log2
Definition: intmath.h:85
static const struct twinvq_data tab
const uint16_t avpriv_mpa_bitrate_tab[2][3][15]
Definition: mpegaudiodata.c:30
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: internal.h:140
static void compute_bit_allocation(MpegAudioContext *s, short smr1[MPA_MAX_CHANNELS][SBLIMIT], unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int *padding)
Definition: mpegaudioenc.c:503
int nb_channels
static unsigned short total_quant_bits[17]
Definition: mpegaudiotab.h:94
int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]
Definition: mpegaudioenc.c:56
#define AV_CH_LAYOUT_MONO
#define MPA_FRAME_SIZE
Definition: mpegaudio.h:36
This structure stores compressed data.
Definition: avcodec.h:898
int delay
Codec delay.
Definition: avcodec.h:1497
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:908
bitstream writer API