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