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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * The simplest mpeg audio layer 2 encoder.
25  */
26 
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 */
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)(exp2((3 - i) / 3.0) * (1 << 20));
152  if (v <= 0)
153  v = 1;
155 #ifdef USE_FLOATS
156  scale_factor_inv_table[i] = exp2(-(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) * exp2((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  return 0;
188 }
189 
190 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
191 static void idct32(int *out, int *tab)
192 {
193  int i, j;
194  int *t, *t1, xr;
195  const int *xp = costab32;
196 
197  for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
198 
199  t = tab + 30;
200  t1 = tab + 2;
201  do {
202  t[0] += t[-4];
203  t[1] += t[1 - 4];
204  t -= 4;
205  } while (t != t1);
206 
207  t = tab + 28;
208  t1 = tab + 4;
209  do {
210  t[0] += t[-8];
211  t[1] += t[1-8];
212  t[2] += t[2-8];
213  t[3] += t[3-8];
214  t -= 8;
215  } while (t != t1);
216 
217  t = tab;
218  t1 = tab + 32;
219  do {
220  t[ 3] = -t[ 3];
221  t[ 6] = -t[ 6];
222 
223  t[11] = -t[11];
224  t[12] = -t[12];
225  t[13] = -t[13];
226  t[15] = -t[15];
227  t += 16;
228  } while (t != t1);
229 
230 
231  t = tab;
232  t1 = tab + 8;
233  do {
234  int x1, x2, x3, x4;
235 
236  x3 = MUL(t[16], FIX(SQRT2*0.5));
237  x4 = t[0] - x3;
238  x3 = t[0] + x3;
239 
240  x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
241  x1 = MUL((t[8] - x2), xp[0]);
242  x2 = MUL((t[8] + x2), xp[1]);
243 
244  t[ 0] = x3 + x1;
245  t[ 8] = x4 - x2;
246  t[16] = x4 + x2;
247  t[24] = x3 - x1;
248  t++;
249  } while (t != t1);
250 
251  xp += 2;
252  t = tab;
253  t1 = tab + 4;
254  do {
255  xr = MUL(t[28],xp[0]);
256  t[28] = (t[0] - xr);
257  t[0] = (t[0] + xr);
258 
259  xr = MUL(t[4],xp[1]);
260  t[ 4] = (t[24] - xr);
261  t[24] = (t[24] + xr);
262 
263  xr = MUL(t[20],xp[2]);
264  t[20] = (t[8] - xr);
265  t[ 8] = (t[8] + xr);
266 
267  xr = MUL(t[12],xp[3]);
268  t[12] = (t[16] - xr);
269  t[16] = (t[16] + xr);
270  t++;
271  } while (t != t1);
272  xp += 4;
273 
274  for (i = 0; i < 4; i++) {
275  xr = MUL(tab[30-i*4],xp[0]);
276  tab[30-i*4] = (tab[i*4] - xr);
277  tab[ i*4] = (tab[i*4] + xr);
278 
279  xr = MUL(tab[ 2+i*4],xp[1]);
280  tab[ 2+i*4] = (tab[28-i*4] - xr);
281  tab[28-i*4] = (tab[28-i*4] + xr);
282 
283  xr = MUL(tab[31-i*4],xp[0]);
284  tab[31-i*4] = (tab[1+i*4] - xr);
285  tab[ 1+i*4] = (tab[1+i*4] + xr);
286 
287  xr = MUL(tab[ 3+i*4],xp[1]);
288  tab[ 3+i*4] = (tab[29-i*4] - xr);
289  tab[29-i*4] = (tab[29-i*4] + xr);
290 
291  xp += 2;
292  }
293 
294  t = tab + 30;
295  t1 = tab + 1;
296  do {
297  xr = MUL(t1[0], *xp);
298  t1[0] = (t[0] - xr);
299  t[0] = (t[0] + xr);
300  t -= 2;
301  t1 += 2;
302  xp++;
303  } while (t >= tab);
304 
305  for(i=0;i<32;i++) {
306  out[i] = tab[bitinv32[i]];
307  }
308 }
309 
310 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
311 
312 static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
313 {
314  short *p, *q;
315  int sum, offset, i, j;
316  int tmp[64];
317  int tmp1[32];
318  int *out;
319 
320  offset = s->samples_offset[ch];
321  out = &s->sb_samples[ch][0][0][0];
322  for(j=0;j<36;j++) {
323  /* 32 samples at once */
324  for(i=0;i<32;i++) {
325  s->samples_buf[ch][offset + (31 - i)] = samples[0];
326  samples += incr;
327  }
328 
329  /* filter */
330  p = s->samples_buf[ch] + offset;
331  q = filter_bank;
332  /* maxsum = 23169 */
333  for(i=0;i<64;i++) {
334  sum = p[0*64] * q[0*64];
335  sum += p[1*64] * q[1*64];
336  sum += p[2*64] * q[2*64];
337  sum += p[3*64] * q[3*64];
338  sum += p[4*64] * q[4*64];
339  sum += p[5*64] * q[5*64];
340  sum += p[6*64] * q[6*64];
341  sum += p[7*64] * q[7*64];
342  tmp[i] = sum;
343  p++;
344  q++;
345  }
346  tmp1[0] = tmp[16] >> WSHIFT;
347  for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
348  for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
349 
350  idct32(out, tmp1);
351 
352  /* advance of 32 samples */
353  offset -= 32;
354  out += 32;
355  /* handle the wrap around */
356  if (offset < 0) {
357  memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
358  s->samples_buf[ch], (512 - 32) * 2);
359  offset = SAMPLES_BUF_SIZE - 512;
360  }
361  }
362  s->samples_offset[ch] = offset;
363 }
364 
365 static 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 > 1) {
387  n = av_log2(vmax);
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 {
398  index = 62; /* value 63 is not allowed */
399  }
400 
401  av_dlog(NULL, "%2d:%d in=%x %x %d\n",
402  j, i, vmax, scale_factor_table[index], index);
403  /* store the scale factor */
404  av_assert2(index >=0 && index <= 63);
405  sf[i] = index;
406  }
407 
408  /* compute the transmission factor : look if the scale factors
409  are close enough to each other */
410  d1 = scale_diff_table[sf[0] - sf[1] + 64];
411  d2 = scale_diff_table[sf[1] - sf[2] + 64];
412 
413  /* handle the 25 cases */
414  switch(d1 * 5 + d2) {
415  case 0*5+0:
416  case 0*5+4:
417  case 3*5+4:
418  case 4*5+0:
419  case 4*5+4:
420  code = 0;
421  break;
422  case 0*5+1:
423  case 0*5+2:
424  case 4*5+1:
425  case 4*5+2:
426  code = 3;
427  sf[2] = sf[1];
428  break;
429  case 0*5+3:
430  case 4*5+3:
431  code = 3;
432  sf[1] = sf[2];
433  break;
434  case 1*5+0:
435  case 1*5+4:
436  case 2*5+4:
437  code = 1;
438  sf[1] = sf[0];
439  break;
440  case 1*5+1:
441  case 1*5+2:
442  case 2*5+0:
443  case 2*5+1:
444  case 2*5+2:
445  code = 2;
446  sf[1] = sf[2] = sf[0];
447  break;
448  case 2*5+3:
449  case 3*5+3:
450  code = 2;
451  sf[0] = sf[1] = sf[2];
452  break;
453  case 3*5+0:
454  case 3*5+1:
455  case 3*5+2:
456  code = 2;
457  sf[0] = sf[2] = sf[1];
458  break;
459  case 1*5+3:
460  code = 2;
461  if (sf[0] > sf[2])
462  sf[0] = sf[2];
463  sf[1] = sf[2] = sf[0];
464  break;
465  default:
466  av_assert2(0); //cannot happen
467  code = 0; /* kill warning */
468  }
469 
470  av_dlog(NULL, "%d: %2d %2d %2d %d %d -> %d\n", j,
471  sf[0], sf[1], sf[2], d1, d2, code);
472  scale_code[j] = code;
473  sf += 3;
474  }
475 }
476 
477 /* The most important function : psycho acoustic module. In this
478  encoder there is basically none, so this is the worst you can do,
479  but also this is the simpler. */
481 {
482  int i;
483 
484  for(i=0;i<s->sblimit;i++) {
485  smr[i] = (int)(fixed_smr[i] * 10);
486  }
487 }
488 
489 
490 #define SB_NOTALLOCATED 0
491 #define SB_ALLOCATED 1
492 #define SB_NOMORE 2
493 
494 /* Try to maximize the smr while using a number of bits inferior to
495  the frame size. I tried to make the code simpler, faster and
496  smaller than other encoders :-) */
498  short smr1[MPA_MAX_CHANNELS][SBLIMIT],
499  unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
500  int *padding)
501 {
502  int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
503  int incr;
504  short smr[MPA_MAX_CHANNELS][SBLIMIT];
505  unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
506  const unsigned char *alloc;
507 
508  memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
509  memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
510  memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
511 
512  /* compute frame size and padding */
513  max_frame_size = s->frame_size;
514  s->frame_frac += s->frame_frac_incr;
515  if (s->frame_frac >= 65536) {
516  s->frame_frac -= 65536;
517  s->do_padding = 1;
518  max_frame_size += 8;
519  } else {
520  s->do_padding = 0;
521  }
522 
523  /* compute the header + bit alloc size */
524  current_frame_size = 32;
525  alloc = s->alloc_table;
526  for(i=0;i<s->sblimit;i++) {
527  incr = alloc[0];
528  current_frame_size += incr * s->nb_channels;
529  alloc += 1 << incr;
530  }
531  for(;;) {
532  /* look for the subband with the largest signal to mask ratio */
533  max_sb = -1;
534  max_ch = -1;
535  max_smr = INT_MIN;
536  for(ch=0;ch<s->nb_channels;ch++) {
537  for(i=0;i<s->sblimit;i++) {
538  if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
539  max_smr = smr[ch][i];
540  max_sb = i;
541  max_ch = ch;
542  }
543  }
544  }
545  if (max_sb < 0)
546  break;
547  av_dlog(NULL, "current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n",
548  current_frame_size, max_frame_size, max_sb, max_ch,
549  bit_alloc[max_ch][max_sb]);
550 
551  /* find alloc table entry (XXX: not optimal, should use
552  pointer table) */
553  alloc = s->alloc_table;
554  for(i=0;i<max_sb;i++) {
555  alloc += 1 << alloc[0];
556  }
557 
558  if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
559  /* nothing was coded for this band: add the necessary bits */
560  incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
561  incr += total_quant_bits[alloc[1]];
562  } else {
563  /* increments bit allocation */
564  b = bit_alloc[max_ch][max_sb];
565  incr = total_quant_bits[alloc[b + 1]] -
566  total_quant_bits[alloc[b]];
567  }
568 
569  if (current_frame_size + incr <= max_frame_size) {
570  /* can increase size */
571  b = ++bit_alloc[max_ch][max_sb];
572  current_frame_size += incr;
573  /* decrease smr by the resolution we added */
574  smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
575  /* max allocation size reached ? */
576  if (b == ((1 << alloc[0]) - 1))
577  subband_status[max_ch][max_sb] = SB_NOMORE;
578  else
579  subband_status[max_ch][max_sb] = SB_ALLOCATED;
580  } else {
581  /* cannot increase the size of this subband */
582  subband_status[max_ch][max_sb] = SB_NOMORE;
583  }
584  }
585  *padding = max_frame_size - current_frame_size;
586  av_assert0(*padding >= 0);
587 }
588 
589 /*
590  * Output the mpeg audio layer 2 frame. Note how the code is small
591  * compared to other encoders :-)
592  */
594  unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
595  int padding)
596 {
597  int i, j, k, l, bit_alloc_bits, b, ch;
598  unsigned char *sf;
599  int q[3];
600  PutBitContext *p = &s->pb;
601 
602  /* header */
603 
604  put_bits(p, 12, 0xfff);
605  put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
606  put_bits(p, 2, 4-2); /* layer 2 */
607  put_bits(p, 1, 1); /* no error protection */
608  put_bits(p, 4, s->bitrate_index);
609  put_bits(p, 2, s->freq_index);
610  put_bits(p, 1, s->do_padding); /* use padding */
611  put_bits(p, 1, 0); /* private_bit */
612  put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
613  put_bits(p, 2, 0); /* mode_ext */
614  put_bits(p, 1, 0); /* no copyright */
615  put_bits(p, 1, 1); /* original */
616  put_bits(p, 2, 0); /* no emphasis */
617 
618  /* bit allocation */
619  j = 0;
620  for(i=0;i<s->sblimit;i++) {
621  bit_alloc_bits = s->alloc_table[j];
622  for(ch=0;ch<s->nb_channels;ch++) {
623  put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
624  }
625  j += 1 << bit_alloc_bits;
626  }
627 
628  /* scale codes */
629  for(i=0;i<s->sblimit;i++) {
630  for(ch=0;ch<s->nb_channels;ch++) {
631  if (bit_alloc[ch][i])
632  put_bits(p, 2, s->scale_code[ch][i]);
633  }
634  }
635 
636  /* scale factors */
637  for(i=0;i<s->sblimit;i++) {
638  for(ch=0;ch<s->nb_channels;ch++) {
639  if (bit_alloc[ch][i]) {
640  sf = &s->scale_factors[ch][i][0];
641  switch(s->scale_code[ch][i]) {
642  case 0:
643  put_bits(p, 6, sf[0]);
644  put_bits(p, 6, sf[1]);
645  put_bits(p, 6, sf[2]);
646  break;
647  case 3:
648  case 1:
649  put_bits(p, 6, sf[0]);
650  put_bits(p, 6, sf[2]);
651  break;
652  case 2:
653  put_bits(p, 6, sf[0]);
654  break;
655  }
656  }
657  }
658  }
659 
660  /* quantization & write sub band samples */
661 
662  for(k=0;k<3;k++) {
663  for(l=0;l<12;l+=3) {
664  j = 0;
665  for(i=0;i<s->sblimit;i++) {
666  bit_alloc_bits = s->alloc_table[j];
667  for(ch=0;ch<s->nb_channels;ch++) {
668  b = bit_alloc[ch][i];
669  if (b) {
670  int qindex, steps, m, sample, bits;
671  /* we encode 3 sub band samples of the same sub band at a time */
672  qindex = s->alloc_table[j+b];
673  steps = ff_mpa_quant_steps[qindex];
674  for(m=0;m<3;m++) {
675  sample = s->sb_samples[ch][k][l + m][i];
676  /* divide by scale factor */
677 #ifdef USE_FLOATS
678  {
679  float a;
680  a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
681  q[m] = (int)((a + 1.0) * steps * 0.5);
682  }
683 #else
684  {
685  int q1, e, shift, mult;
686  e = s->scale_factors[ch][i][k];
687  shift = scale_factor_shift[e];
688  mult = scale_factor_mult[e];
689 
690  /* normalize to P bits */
691  if (shift < 0)
692  q1 = sample << (-shift);
693  else
694  q1 = sample >> shift;
695  q1 = (q1 * mult) >> P;
696  q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
697  }
698 #endif
699  if (q[m] >= steps)
700  q[m] = steps - 1;
701  av_assert2(q[m] >= 0 && q[m] < steps);
702  }
703  bits = ff_mpa_quant_bits[qindex];
704  if (bits < 0) {
705  /* group the 3 values to save bits */
706  put_bits(p, -bits,
707  q[0] + steps * (q[1] + steps * q[2]));
708  } else {
709  put_bits(p, bits, q[0]);
710  put_bits(p, bits, q[1]);
711  put_bits(p, bits, q[2]);
712  }
713  }
714  }
715  /* next subband in alloc table */
716  j += 1 << bit_alloc_bits;
717  }
718  }
719  }
720 
721  /* padding */
722  for(i=0;i<padding;i++)
723  put_bits(p, 1, 0);
724 
725  /* flush */
726  flush_put_bits(p);
727 }
728 
729 static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
730  const AVFrame *frame, int *got_packet_ptr)
731 {
732  MpegAudioContext *s = avctx->priv_data;
733  const int16_t *samples = (const int16_t *)frame->data[0];
734  short smr[MPA_MAX_CHANNELS][SBLIMIT];
735  unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
736  int padding, i, ret;
737 
738  for(i=0;i<s->nb_channels;i++) {
739  filter(s, i, samples + i, s->nb_channels);
740  }
741 
742  for(i=0;i<s->nb_channels;i++) {
744  s->sb_samples[i], s->sblimit);
745  }
746  for(i=0;i<s->nb_channels;i++) {
747  psycho_acoustic_model(s, smr[i]);
748  }
749  compute_bit_allocation(s, smr, bit_alloc, &padding);
750 
751  if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
752  return ret;
753 
754  init_put_bits(&s->pb, avpkt->data, avpkt->size);
755 
756  encode_frame(s, bit_alloc, padding);
757 
758  if (frame->pts != AV_NOPTS_VALUE)
759  avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
760 
761  avpkt->size = put_bits_count(&s->pb) / 8;
762  *got_packet_ptr = 1;
763  return 0;
764 }
765 
766 static const AVCodecDefault mp2_defaults[] = {
767  { "b", "128k" },
768  { NULL },
769 };
770 
772  .name = "mp2",
773  .type = AVMEDIA_TYPE_AUDIO,
774  .id = AV_CODEC_ID_MP2,
775  .priv_data_size = sizeof(MpegAudioContext),
777  .encode2 = MPA_encode_frame,
778  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
780  .supported_samplerates = (const int[]){
781  44100, 48000, 32000, 22050, 24000, 16000, 0
782  },
783  .channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
785  0 },
786  .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
787  .defaults = mp2_defaults,
788 };
#define MPA_STEREO
Definition: mpegaudio.h:45
#define MPA_MAX_CODED_FRAME_SIZE
Definition: mpegaudio.h:39
float v
const char * s
Definition: avisynth_c.h:668
static int shift(int a, int b)
Definition: sonic.c:86
#define SB_ALLOCATED
Definition: mpegaudioenc.c:491
#define SBLIMIT
Definition: mpegaudio.h:43
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define WSHIFT
Definition: mpegaudioenc.c:310
static const unsigned char nb_scale_factors[4]
Definition: mpegaudiotab.h:116
const int ff_mpa_quant_bits[17]
Definition: mpegaudiodata.c:55
x1
Definition: genspecsines3.m:7
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
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:365
mpeg audio layer common tables.
#define WFRAC_BITS
Definition: mpegaudioenc.c:34
const int32_t ff_mpa_enwindow[257]
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t bits
Definition: crc.c:216
#define av_cold
Definition: attributes.h:78
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
static int32_t scale_factor_mult[15][3]
Definition: mpegaudiodec.c:153
window constants for m
static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: mpegaudioenc.c:729
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:191
const uint16_t avpriv_mpa_freq_tab[3]
Definition: mpegaudiodata.c:40
#define b
Definition: input.c:42
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:159
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
#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 int16_t filter_bank[512]
Definition: mpegaudiotab.h:82
frame
Definition: stft.m:14
AVCodec ff_mp2_encoder
Definition: mpegaudioenc.c:771
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define t1
Definition: regdef.h:29
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
init variable d2
const char * name
Name of the codec implementation.
static const int bitinv32[32]
Definition: mpegaudiotab.h:74
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
external API header
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:73
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:312
const unsigned char * alloc_table
Definition: mpegaudioenc.c:61
#define MPA_MAX_CHANNELS
Definition: mpegaudio.h:41
int bit_rate
the average bitrate
audio channel layout utility functions
ret
Definition: avfilter.c:821
t
Definition: genspecsines3.m:6
PutBitContext pb
Definition: mpegaudioenc.c:46
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
#define SB_NOTALLOCATED
Definition: mpegaudioenc.c:490
for k
int frame_size
Number of samples per channel in an audio frame.
static void encode_frame(MpegAudioContext *s, unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int padding)
Definition: mpegaudioenc.c:593
NULL
Definition: eval.c:55
or the Software in violation of any applicable export control laws in any jurisdiction Except as provided by mandatorily applicable UPF has no obligation to provide you with source code to the Software In the event Software contains any source code
int samples_offset[MPA_MAX_CHANNELS]
Definition: mpegaudioenc.c:55
int sample_rate
samples per second
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
Definition: mpegaudioenc.c:480
static const float fixed_smr[SBLIMIT]
Definition: mpegaudiotab.h:109
main external API structure.
struct MpegAudioContext MpegAudioContext
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
static int16_t mult(Float11 *f1, Float11 *f2)
Definition: g726.c:56
#define MUL(a, b)
Definition: mpegaudioenc.c:41
x2
Definition: genspecsines3.m:8
int index
Definition: gxfenc.c:89
synthesis window for stochastic i
#define MPA_MONO
Definition: mpegaudio.h:48
short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]
Definition: mpegaudioenc.c:54
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
static const AVCodecDefault mp2_defaults[]
Definition: mpegaudioenc.c:766
#define SB_NOMORE
Definition: mpegaudioenc.c:492
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
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:81
#define exp2(x)
Definition: libm.h:77
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:54
const int ff_mpa_sblimit_table[5]
Definition: mpegaudiodata.c:45
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
#define av_log2
Definition: intmath.h:89
static const struct twinvq_data tab
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
Filter the word “frame” indicates either a video frame or a group of audio samples
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
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.
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:497
x4
Definition: Lab2 1d.m:1
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.
int delay
Codec delay.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
bitstream writer API