FFmpeg
mpegaudioenc_template.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 "encode.h"
31 #include "internal.h"
32 #include "put_bits.h"
33 
34 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
35 #define WFRAC_BITS 14 /* fractional bits for window */
36 
37 #include "mpegaudio.h"
38 #include "mpegaudiodsp.h"
39 #include "mpegaudiodata.h"
40 #include "mpegaudiotab.h"
41 
42 /* currently, cannot change these constants (need to modify
43  quantization stage) */
44 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
45 
46 #define SAMPLES_BUF_SIZE 4096
47 
48 typedef struct MpegAudioContext {
51  int lsf; /* 1 if mpeg2 low bitrate selected */
52  int bitrate_index; /* bit rate */
54  int frame_size; /* frame size, in bits, without padding */
55  /* padding computation */
57  short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
58  int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
60  unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
61  /* code to group 3 scale factors */
63  int sblimit; /* number of used subbands */
64  const unsigned char *alloc_table;
65  int16_t filter_bank[512];
67  unsigned char scale_diff_table[128];
68 #if USE_FLOATS
69  float scale_factor_inv_table[64];
70 #else
71  int8_t scale_factor_shift[64];
72  unsigned short scale_factor_mult[64];
73 #endif
74  unsigned short total_quant_bits[17]; /* total number of bits per allocation group */
76 
78 {
79  MpegAudioContext *s = avctx->priv_data;
80  int freq = avctx->sample_rate;
81  int bitrate = avctx->bit_rate;
82  int channels = avctx->channels;
83  int i, v, table;
84  float a;
85 
87  av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
88  return AVERROR(EINVAL);
89  }
90  bitrate = bitrate / 1000;
91  s->nb_channels = channels;
92  avctx->frame_size = MPA_FRAME_SIZE;
93  avctx->initial_padding = 512 - 32 + 1;
94 
95  /* encoding freq */
96  s->lsf = 0;
97  for(i=0;i<3;i++) {
98  if (ff_mpa_freq_tab[i] == freq)
99  break;
100  if ((ff_mpa_freq_tab[i] / 2) == freq) {
101  s->lsf = 1;
102  break;
103  }
104  }
105  if (i == 3){
106  av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
107  return AVERROR(EINVAL);
108  }
109  s->freq_index = i;
110 
111  /* encoding bitrate & frequency */
112  for(i=1;i<15;i++) {
113  if (ff_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
114  break;
115  }
116  if (i == 15 && !avctx->bit_rate) {
117  i = 14;
118  bitrate = ff_mpa_bitrate_tab[s->lsf][1][i];
119  avctx->bit_rate = bitrate * 1000;
120  }
121  if (i == 15){
122  av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
123  return AVERROR(EINVAL);
124  }
125  s->bitrate_index = i;
126 
127  /* compute total header size & pad bit */
128 
129  a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
130  s->frame_size = ((int)a) * 8;
131 
132  /* frame fractional size to compute padding */
133  s->frame_frac = 0;
134  s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
135 
136  /* select the right allocation table */
137  table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
138 
139  /* number of used subbands */
140  s->sblimit = ff_mpa_sblimit_table[table];
141  s->alloc_table = ff_mpa_alloc_tables[table];
142 
143  ff_dlog(avctx, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
144  bitrate, freq, s->frame_size, table, s->frame_frac_incr);
145 
146  for(i=0;i<s->nb_channels;i++)
147  s->samples_offset[i] = 0;
148 
149  for(i=0;i<257;i++) {
150  int v;
151  v = ff_mpa_enwindow[i];
152 #if WFRAC_BITS != 16
153  v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
154 #endif
155  s->filter_bank[i] = v;
156  if ((i & 63) != 0)
157  v = -v;
158  if (i != 0)
159  s->filter_bank[512 - i] = v;
160  }
161 
162  for(i=0;i<64;i++) {
163  v = (int)(exp2((3 - i) / 3.0) * (1 << 20));
164  if (v <= 0)
165  v = 1;
166  s->scale_factor_table[i] = v;
167 #if USE_FLOATS
168  s->scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20);
169 #else
170 #define P 15
171  s->scale_factor_shift[i] = 21 - P - (i / 3);
172  s->scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0);
173 #endif
174  }
175  for(i=0;i<128;i++) {
176  v = i - 64;
177  if (v <= -3)
178  v = 0;
179  else if (v < 0)
180  v = 1;
181  else if (v == 0)
182  v = 2;
183  else if (v < 3)
184  v = 3;
185  else
186  v = 4;
187  s->scale_diff_table[i] = v;
188  }
189 
190  for(i=0;i<17;i++) {
191  v = ff_mpa_quant_bits[i];
192  if (v < 0)
193  v = -v;
194  else
195  v = v * 3;
196  s->total_quant_bits[i] = 12 * v;
197  }
198 
199  return 0;
200 }
201 
202 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
203 static void idct32(int *out, int *tab)
204 {
205  int i, j;
206  int *t, *t1, xr;
207  const int *xp = costab32;
208 
209  for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
210 
211  t = tab + 30;
212  t1 = tab + 2;
213  do {
214  t[0] += t[-4];
215  t[1] += t[1 - 4];
216  t -= 4;
217  } while (t != t1);
218 
219  t = tab + 28;
220  t1 = tab + 4;
221  do {
222  t[0] += t[-8];
223  t[1] += t[1-8];
224  t[2] += t[2-8];
225  t[3] += t[3-8];
226  t -= 8;
227  } while (t != t1);
228 
229  t = tab;
230  t1 = tab + 32;
231  do {
232  t[ 3] = -t[ 3];
233  t[ 6] = -t[ 6];
234 
235  t[11] = -t[11];
236  t[12] = -t[12];
237  t[13] = -t[13];
238  t[15] = -t[15];
239  t += 16;
240  } while (t != t1);
241 
242 
243  t = tab;
244  t1 = tab + 8;
245  do {
246  int x1, x2, x3, x4;
247 
248  x3 = MUL(t[16], FIX(M_SQRT2*0.5));
249  x4 = t[0] - x3;
250  x3 = t[0] + x3;
251 
252  x2 = MUL(-(t[24] + t[8]), FIX(M_SQRT2*0.5));
253  x1 = MUL((t[8] - x2), xp[0]);
254  x2 = MUL((t[8] + x2), xp[1]);
255 
256  t[ 0] = x3 + x1;
257  t[ 8] = x4 - x2;
258  t[16] = x4 + x2;
259  t[24] = x3 - x1;
260  t++;
261  } while (t != t1);
262 
263  xp += 2;
264  t = tab;
265  t1 = tab + 4;
266  do {
267  xr = MUL(t[28],xp[0]);
268  t[28] = (t[0] - xr);
269  t[0] = (t[0] + xr);
270 
271  xr = MUL(t[4],xp[1]);
272  t[ 4] = (t[24] - xr);
273  t[24] = (t[24] + xr);
274 
275  xr = MUL(t[20],xp[2]);
276  t[20] = (t[8] - xr);
277  t[ 8] = (t[8] + xr);
278 
279  xr = MUL(t[12],xp[3]);
280  t[12] = (t[16] - xr);
281  t[16] = (t[16] + xr);
282  t++;
283  } while (t != t1);
284  xp += 4;
285 
286  for (i = 0; i < 4; i++) {
287  xr = MUL(tab[30-i*4],xp[0]);
288  tab[30-i*4] = (tab[i*4] - xr);
289  tab[ i*4] = (tab[i*4] + xr);
290 
291  xr = MUL(tab[ 2+i*4],xp[1]);
292  tab[ 2+i*4] = (tab[28-i*4] - xr);
293  tab[28-i*4] = (tab[28-i*4] + xr);
294 
295  xr = MUL(tab[31-i*4],xp[0]);
296  tab[31-i*4] = (tab[1+i*4] - xr);
297  tab[ 1+i*4] = (tab[1+i*4] + xr);
298 
299  xr = MUL(tab[ 3+i*4],xp[1]);
300  tab[ 3+i*4] = (tab[29-i*4] - xr);
301  tab[29-i*4] = (tab[29-i*4] + xr);
302 
303  xp += 2;
304  }
305 
306  t = tab + 30;
307  t1 = tab + 1;
308  do {
309  xr = MUL(t1[0], *xp);
310  t1[0] = (t[0] - xr);
311  t[0] = (t[0] + xr);
312  t -= 2;
313  t1 += 2;
314  xp++;
315  } while (t >= tab);
316 
317  for(i=0;i<32;i++) {
318  out[i] = tab[bitinv32[i]];
319  }
320 }
321 
322 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
323 
324 static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
325 {
326  short *p, *q;
327  int sum, offset, i, j;
328  int tmp[64];
329  int tmp1[32];
330  int *out;
331 
332  offset = s->samples_offset[ch];
333  out = &s->sb_samples[ch][0][0][0];
334  for(j=0;j<36;j++) {
335  /* 32 samples at once */
336  for(i=0;i<32;i++) {
337  s->samples_buf[ch][offset + (31 - i)] = samples[0];
338  samples += incr;
339  }
340 
341  /* filter */
342  p = s->samples_buf[ch] + offset;
343  q = s->filter_bank;
344  /* maxsum = 23169 */
345  for(i=0;i<64;i++) {
346  sum = p[0*64] * q[0*64];
347  sum += p[1*64] * q[1*64];
348  sum += p[2*64] * q[2*64];
349  sum += p[3*64] * q[3*64];
350  sum += p[4*64] * q[4*64];
351  sum += p[5*64] * q[5*64];
352  sum += p[6*64] * q[6*64];
353  sum += p[7*64] * q[7*64];
354  tmp[i] = sum;
355  p++;
356  q++;
357  }
358  tmp1[0] = tmp[16] >> WSHIFT;
359  for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
360  for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
361 
362  idct32(out, tmp1);
363 
364  /* advance of 32 samples */
365  offset -= 32;
366  out += 32;
367  /* handle the wrap around */
368  if (offset < 0) {
369  memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
370  s->samples_buf[ch], (512 - 32) * 2);
371  offset = SAMPLES_BUF_SIZE - 512;
372  }
373  }
374  s->samples_offset[ch] = offset;
375 }
376 
378  unsigned char scale_code[SBLIMIT],
379  unsigned char scale_factors[SBLIMIT][3],
380  int sb_samples[3][12][SBLIMIT],
381  int sblimit)
382 {
383  int *p, vmax, v, n, i, j, k, code;
384  int index, d1, d2;
385  unsigned char *sf = &scale_factors[0][0];
386 
387  for(j=0;j<sblimit;j++) {
388  for(i=0;i<3;i++) {
389  /* find the max absolute value */
390  p = &sb_samples[i][0][j];
391  vmax = abs(*p);
392  for(k=1;k<12;k++) {
393  p += SBLIMIT;
394  v = abs(*p);
395  if (v > vmax)
396  vmax = v;
397  }
398  /* compute the scale factor index using log 2 computations */
399  if (vmax > 1) {
400  n = av_log2(vmax);
401  /* n is the position of the MSB of vmax. now
402  use at most 2 compares to find the index */
403  index = (21 - n) * 3 - 3;
404  if (index >= 0) {
405  while (vmax <= s->scale_factor_table[index+1])
406  index++;
407  } else {
408  index = 0; /* very unlikely case of overflow */
409  }
410  } else {
411  index = 62; /* value 63 is not allowed */
412  }
413 
414  ff_dlog(NULL, "%2d:%d in=%x %x %d\n",
415  j, i, vmax, s->scale_factor_table[index], index);
416  /* store the scale factor */
417  av_assert2(index >=0 && index <= 63);
418  sf[i] = index;
419  }
420 
421  /* compute the transmission factor : look if the scale factors
422  are close enough to each other */
423  d1 = s->scale_diff_table[sf[0] - sf[1] + 64];
424  d2 = s->scale_diff_table[sf[1] - sf[2] + 64];
425 
426  /* handle the 25 cases */
427  switch(d1 * 5 + d2) {
428  case 0*5+0:
429  case 0*5+4:
430  case 3*5+4:
431  case 4*5+0:
432  case 4*5+4:
433  code = 0;
434  break;
435  case 0*5+1:
436  case 0*5+2:
437  case 4*5+1:
438  case 4*5+2:
439  code = 3;
440  sf[2] = sf[1];
441  break;
442  case 0*5+3:
443  case 4*5+3:
444  code = 3;
445  sf[1] = sf[2];
446  break;
447  case 1*5+0:
448  case 1*5+4:
449  case 2*5+4:
450  code = 1;
451  sf[1] = sf[0];
452  break;
453  case 1*5+1:
454  case 1*5+2:
455  case 2*5+0:
456  case 2*5+1:
457  case 2*5+2:
458  code = 2;
459  sf[1] = sf[2] = sf[0];
460  break;
461  case 2*5+3:
462  case 3*5+3:
463  code = 2;
464  sf[0] = sf[1] = sf[2];
465  break;
466  case 3*5+0:
467  case 3*5+1:
468  case 3*5+2:
469  code = 2;
470  sf[0] = sf[2] = sf[1];
471  break;
472  case 1*5+3:
473  code = 2;
474  if (sf[0] > sf[2])
475  sf[0] = sf[2];
476  sf[1] = sf[2] = sf[0];
477  break;
478  default:
479  av_assert2(0); //cannot happen
480  code = 0; /* kill warning */
481  }
482 
483  ff_dlog(NULL, "%d: %2d %2d %2d %d %d -> %d\n", j,
484  sf[0], sf[1], sf[2], d1, d2, code);
485  scale_code[j] = code;
486  sf += 3;
487  }
488 }
489 
490 /* The most important function : psycho acoustic module. In this
491  encoder there is basically none, so this is the worst you can do,
492  but also this is the simpler. */
494 {
495  int i;
496 
497  for(i=0;i<s->sblimit;i++) {
498  smr[i] = (int)(fixed_smr[i] * 10);
499  }
500 }
501 
502 
503 #define SB_NOTALLOCATED 0
504 #define SB_ALLOCATED 1
505 #define SB_NOMORE 2
506 
507 /* Try to maximize the smr while using a number of bits inferior to
508  the frame size. I tried to make the code simpler, faster and
509  smaller than other encoders :-) */
511  short smr1[MPA_MAX_CHANNELS][SBLIMIT],
512  unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
513  int *padding)
514 {
515  int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
516  int incr;
517  short smr[MPA_MAX_CHANNELS][SBLIMIT];
518  unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
519  const unsigned char *alloc;
520 
521  memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
522  memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
523  memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
524 
525  /* compute frame size and padding */
526  max_frame_size = s->frame_size;
527  s->frame_frac += s->frame_frac_incr;
528  if (s->frame_frac >= 65536) {
529  s->frame_frac -= 65536;
530  s->do_padding = 1;
531  max_frame_size += 8;
532  } else {
533  s->do_padding = 0;
534  }
535 
536  /* compute the header + bit alloc size */
537  current_frame_size = 32;
538  alloc = s->alloc_table;
539  for(i=0;i<s->sblimit;i++) {
540  incr = alloc[0];
541  current_frame_size += incr * s->nb_channels;
542  alloc += 1 << incr;
543  }
544  for(;;) {
545  /* look for the subband with the largest signal to mask ratio */
546  max_sb = -1;
547  max_ch = -1;
548  max_smr = INT_MIN;
549  for(ch=0;ch<s->nb_channels;ch++) {
550  for(i=0;i<s->sblimit;i++) {
551  if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
552  max_smr = smr[ch][i];
553  max_sb = i;
554  max_ch = ch;
555  }
556  }
557  }
558  if (max_sb < 0)
559  break;
560  ff_dlog(NULL, "current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n",
561  current_frame_size, max_frame_size, max_sb, max_ch,
562  bit_alloc[max_ch][max_sb]);
563 
564  /* find alloc table entry (XXX: not optimal, should use
565  pointer table) */
566  alloc = s->alloc_table;
567  for(i=0;i<max_sb;i++) {
568  alloc += 1 << alloc[0];
569  }
570 
571  if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
572  /* nothing was coded for this band: add the necessary bits */
573  incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
574  incr += s->total_quant_bits[alloc[1]];
575  } else {
576  /* increments bit allocation */
577  b = bit_alloc[max_ch][max_sb];
578  incr = s->total_quant_bits[alloc[b + 1]] -
579  s->total_quant_bits[alloc[b]];
580  }
581 
582  if (current_frame_size + incr <= max_frame_size) {
583  /* can increase size */
584  b = ++bit_alloc[max_ch][max_sb];
585  current_frame_size += incr;
586  /* decrease smr by the resolution we added */
587  smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
588  /* max allocation size reached ? */
589  if (b == ((1 << alloc[0]) - 1))
590  subband_status[max_ch][max_sb] = SB_NOMORE;
591  else
592  subband_status[max_ch][max_sb] = SB_ALLOCATED;
593  } else {
594  /* cannot increase the size of this subband */
595  subband_status[max_ch][max_sb] = SB_NOMORE;
596  }
597  }
598  *padding = max_frame_size - current_frame_size;
599  av_assert0(*padding >= 0);
600 }
601 
602 /*
603  * Output the MPEG audio layer 2 frame. Note how the code is small
604  * compared to other encoders :-)
605  */
607  unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
608  int padding)
609 {
610  int i, j, k, l, bit_alloc_bits, b, ch;
611  unsigned char *sf;
612  int q[3];
613  PutBitContext *p = &s->pb;
614 
615  /* header */
616 
617  put_bits(p, 12, 0xfff);
618  put_bits(p, 1, 1 - s->lsf); /* 1 = MPEG-1 ID, 0 = MPEG-2 lsf ID */
619  put_bits(p, 2, 4-2); /* layer 2 */
620  put_bits(p, 1, 1); /* no error protection */
621  put_bits(p, 4, s->bitrate_index);
622  put_bits(p, 2, s->freq_index);
623  put_bits(p, 1, s->do_padding); /* use padding */
624  put_bits(p, 1, 0); /* private_bit */
625  put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
626  put_bits(p, 2, 0); /* mode_ext */
627  put_bits(p, 1, 0); /* no copyright */
628  put_bits(p, 1, 1); /* original */
629  put_bits(p, 2, 0); /* no emphasis */
630 
631  /* bit allocation */
632  j = 0;
633  for(i=0;i<s->sblimit;i++) {
634  bit_alloc_bits = s->alloc_table[j];
635  for(ch=0;ch<s->nb_channels;ch++) {
636  put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
637  }
638  j += 1 << bit_alloc_bits;
639  }
640 
641  /* scale codes */
642  for(i=0;i<s->sblimit;i++) {
643  for(ch=0;ch<s->nb_channels;ch++) {
644  if (bit_alloc[ch][i])
645  put_bits(p, 2, s->scale_code[ch][i]);
646  }
647  }
648 
649  /* scale factors */
650  for(i=0;i<s->sblimit;i++) {
651  for(ch=0;ch<s->nb_channels;ch++) {
652  if (bit_alloc[ch][i]) {
653  sf = &s->scale_factors[ch][i][0];
654  switch(s->scale_code[ch][i]) {
655  case 0:
656  put_bits(p, 6, sf[0]);
657  put_bits(p, 6, sf[1]);
658  put_bits(p, 6, sf[2]);
659  break;
660  case 3:
661  case 1:
662  put_bits(p, 6, sf[0]);
663  put_bits(p, 6, sf[2]);
664  break;
665  case 2:
666  put_bits(p, 6, sf[0]);
667  break;
668  }
669  }
670  }
671  }
672 
673  /* quantization & write sub band samples */
674 
675  for(k=0;k<3;k++) {
676  for(l=0;l<12;l+=3) {
677  j = 0;
678  for(i=0;i<s->sblimit;i++) {
679  bit_alloc_bits = s->alloc_table[j];
680  for(ch=0;ch<s->nb_channels;ch++) {
681  b = bit_alloc[ch][i];
682  if (b) {
683  int qindex, steps, m, sample, bits;
684  /* we encode 3 sub band samples of the same sub band at a time */
685  qindex = s->alloc_table[j+b];
686  steps = ff_mpa_quant_steps[qindex];
687  for(m=0;m<3;m++) {
688  sample = s->sb_samples[ch][k][l + m][i];
689  /* divide by scale factor */
690 #if USE_FLOATS
691  {
692  float a;
693  a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]];
694  q[m] = (int)((a + 1.0) * steps * 0.5);
695  }
696 #else
697  {
698  int q1, e, shift, mult;
699  e = s->scale_factors[ch][i][k];
700  shift = s->scale_factor_shift[e];
701  mult = s->scale_factor_mult[e];
702 
703  /* normalize to P bits */
704  if (shift < 0)
705  q1 = sample * (1 << -shift);
706  else
707  q1 = sample >> shift;
708  q1 = (q1 * mult) >> P;
709  q1 += 1 << P;
710  if (q1 < 0)
711  q1 = 0;
712  q[m] = (q1 * (unsigned)steps) >> (P + 1);
713  }
714 #endif
715  if (q[m] >= steps)
716  q[m] = steps - 1;
717  av_assert2(q[m] >= 0 && q[m] < steps);
718  }
719  bits = ff_mpa_quant_bits[qindex];
720  if (bits < 0) {
721  /* group the 3 values to save bits */
722  put_bits(p, -bits,
723  q[0] + steps * (q[1] + steps * q[2]));
724  } else {
725  put_bits(p, bits, q[0]);
726  put_bits(p, bits, q[1]);
727  put_bits(p, bits, q[2]);
728  }
729  }
730  }
731  /* next subband in alloc table */
732  j += 1 << bit_alloc_bits;
733  }
734  }
735  }
736 
737  /* padding */
738  for(i=0;i<padding;i++)
739  put_bits(p, 1, 0);
740 }
741 
742 static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
743  const AVFrame *frame, int *got_packet_ptr)
744 {
745  MpegAudioContext *s = avctx->priv_data;
746  const int16_t *samples = (const int16_t *)frame->data[0];
747  short smr[MPA_MAX_CHANNELS][SBLIMIT];
748  unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
749  int padding, i, ret;
750 
751  for(i=0;i<s->nb_channels;i++) {
752  filter(s, i, samples + i, s->nb_channels);
753  }
754 
755  for(i=0;i<s->nb_channels;i++) {
756  compute_scale_factors(s, s->scale_code[i], s->scale_factors[i],
757  s->sb_samples[i], s->sblimit);
758  }
759  for(i=0;i<s->nb_channels;i++) {
760  psycho_acoustic_model(s, smr[i]);
761  }
762  compute_bit_allocation(s, smr, bit_alloc, &padding);
763 
764  if ((ret = ff_alloc_packet(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
765  return ret;
766 
767  init_put_bits(&s->pb, avpkt->data, avpkt->size);
768 
769  encode_frame(s, bit_alloc, padding);
770 
771  /* flush */
772  flush_put_bits(&s->pb);
773  avpkt->size = put_bytes_output(&s->pb);
774 
775  if (frame->pts != AV_NOPTS_VALUE)
776  avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
777 
778  *got_packet_ptr = 1;
779  return 0;
780 }
781 
782 static const AVCodecDefault mp2_defaults[] = {
783  { "b", "0" },
784  { NULL },
785 };
786 
idct32
static void idct32(int *out, int *tab)
Definition: mpegaudioenc_template.c:203
MPA_MONO
#define MPA_MONO
Definition: mpegaudio.h:49
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1012
q1
static const uint8_t q1[256]
Definition: twofish.c:96
MpegAudioContext::sb_samples
int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]
Definition: mpegaudioenc_template.c:59
ff_mpa_l2_select_table
int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
Definition: mpegaudio.c:31
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
MpegAudioContext::freq_index
int freq_index
Definition: mpegaudioenc_template.c:53
out
FILE * out
Definition: movenc.c:54
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:88
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:992
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:61
compute_scale_factors
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_template.c:377
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:220
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
index
fg index
Definition: ffmpeg_filter.c:167
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
encode.h
b
#define b
Definition: input.c:40
table
static const uint16_t table[]
Definition: prosumer.c:206
ff_mpa_quant_bits
const int ff_mpa_quant_bits[17]
Definition: mpegaudiodata.c:42
t1
#define t1
Definition: regdef.h:29
MpegAudioContext::scale_diff_table
unsigned char scale_diff_table[128]
Definition: mpegaudioenc_template.c:67
MpegAudioContext::bitrate_index
int bitrate_index
Definition: mpegaudioenc_template.c:52
filter
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc_template.c:324
MpegAudioContext::scale_code
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]
Definition: mpegaudioenc_template.c:62
MpegAudioContext::frame_frac_incr
int frame_frac_incr
Definition: mpegaudioenc_template.c:56
MpegAudioContext::lsf
int lsf
Definition: mpegaudioenc_template.c:51
MpegAudioContext::scale_factor_mult
unsigned short scale_factor_mult[64]
Definition: mpegaudioenc_template.c:72
mp2_defaults
static const AVCodecDefault mp2_defaults[]
Definition: mpegaudioenc_template.c:782
MpegAudioContext::total_quant_bits
unsigned short total_quant_bits[17]
Definition: mpegaudioenc_template.c:74
WSHIFT
#define WSHIFT
Definition: mpegaudioenc_template.c:322
MpegAudioContext::frame_size
int frame_size
Definition: mpegaudioenc_template.c:54
nb_scale_factors
static const unsigned char nb_scale_factors[4]
Definition: mpegaudiotab.h:100
MpegAudioContext::do_padding
int do_padding
Definition: mpegaudioenc_template.c:56
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1701
tab
static const struct twinvq_data tab
Definition: twinvq_data.h:10345
SB_NOTALLOCATED
#define SB_NOTALLOCATED
Definition: mpegaudioenc_template.c:503
WFRAC_BITS
#define WFRAC_BITS
Definition: mpegaudioenc_template.c:35
bit_alloc
static int bit_alloc(AC3EncodeContext *s, int snr_offset)
Run the bit allocation with a given SNR offset.
Definition: ac3enc.c:1139
MPA_FRAME_SIZE
#define MPA_FRAME_SIZE
Definition: mpegaudio.h:37
mult
static int16_t mult(Float11 *f1, Float11 *f2)
Definition: g726.c:56
ff_samples_to_time_base
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:242
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
ff_mpa_quant_steps
const int ff_mpa_quant_steps[17]
Definition: mpegaudiodata.c:34
av_cold
#define av_cold
Definition: attributes.h:90
MpegAudioContext::scale_factor_table
int scale_factor_table[64]
Definition: mpegaudioenc_template.c:66
s
#define s(width, name)
Definition: cbs_vp9.c:257
MpegAudioContext::scale_factors
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]
Definition: mpegaudioenc_template.c:60
floor
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
SB_NOMORE
#define SB_NOMORE
Definition: mpegaudioenc_template.c:505
bits
uint8_t bits
Definition: vp3data.h:141
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
channels
channels
Definition: aptx.h:33
MpegAudioContext::nb_channels
int nb_channels
Definition: mpegaudioenc_template.c:50
MPA_encode_frame
static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: mpegaudioenc_template.c:742
costab32
static const int costab32[30]
Definition: mpegaudiotab.h:36
PutBitContext
Definition: put_bits.h:49
ff_mpa_alloc_tables
const unsigned char *const ff_mpa_alloc_tables[5]
Definition: mpegaudiodata.c:132
AVCodecDefault
Definition: internal.h:215
MpegAudioContext::pb
PutBitContext pb
Definition: mpegaudioenc_template.c:49
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:433
FIX
#define FIX(x)
Definition: jrevdct.c:146
abs
#define abs(x)
Definition: cuda_runtime.h:35
fixed_smr
static const float fixed_smr[SBLIMIT]
Definition: mpegaudiotab.h:93
bitinv32
static const int bitinv32[32]
Definition: mpegaudiotab.h:72
quant_snr
static const unsigned short quant_snr[17]
Definition: mpegaudiotab.h:83
SBLIMIT
#define SBLIMIT
Definition: mpegaudio.h:44
MpegAudioContext::frame_frac
int frame_frac
Definition: mpegaudioenc_template.c:56
ff_mpa_enwindow
const int32_t ff_mpa_enwindow[257]
Definition: mpegaudiodsp_data.c:22
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:29
encode_frame
static void encode_frame(MpegAudioContext *s, unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int padding)
Definition: mpegaudioenc_template.c:606
AVPacket::size
int size
Definition: packet.h:374
MPA_STEREO
#define MPA_STEREO
Definition: mpegaudio.h:46
MpegAudioContext::alloc_table
const unsigned char * alloc_table
Definition: mpegaudioenc_template.c:64
P
#define P
MpegAudioContext::sblimit
int sblimit
Definition: mpegaudioenc_template.c:63
sample
#define sample
Definition: flacdsp_template.c:44
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
psycho_acoustic_model
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
Definition: mpegaudioenc_template.c:493
MpegAudioContext::samples_buf
short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]
Definition: mpegaudioenc_template.c:57
compute_bit_allocation
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_template.c:510
MpegAudioContext::scale_factor_shift
int8_t scale_factor_shift[64]
Definition: mpegaudioenc_template.c:71
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
MpegAudioContext::filter_bank
int16_t filter_bank[512]
Definition: mpegaudioenc_template.c:65
bitrate
int64_t bitrate
Definition: h264_levels.c:131
MUL
#define MUL(a, b)
Definition: mpegaudioenc_template.c:44
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
ff_mpa_sblimit_table
const int ff_mpa_sblimit_table[5]
Definition: mpegaudiodata.c:32
exp2
#define exp2(x)
Definition: libm.h:288
ff_mpa_bitrate_tab
const uint16_t ff_mpa_bitrate_tab[2][3][15]
Definition: mpegaudiotabs.h:27
MPA_encode_init
static av_cold int MPA_encode_init(AVCodecContext *avctx)
Definition: mpegaudioenc_template.c:77
mpegaudio.h
avcodec.h
SB_ALLOCATED
#define SB_ALLOCATED
Definition: mpegaudioenc_template.c:504
ret
ret
Definition: filter_design.txt:187
MpegAudioContext::samples_offset
int samples_offset[MPA_MAX_CHANNELS]
Definition: mpegaudioenc_template.c:58
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:383
MpegAudioContext
Definition: mpegaudioenc_template.c:48
channel_layout.h
ff_mpa_freq_tab
const uint16_t ff_mpa_freq_tab[3]
Definition: mpegaudiotabs.h:37
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
mpegaudiodata.h
shift
static int shift(int a, int b)
Definition: sonic.c:83
mpegaudiodsp.h
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:142
M_SQRT2
#define M_SQRT2
Definition: mathematics.h:61
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
MPA_MAX_CHANNELS
#define MPA_MAX_CHANNELS
Definition: mpegaudio.h:42
SAMPLES_BUF_SIZE
#define SAMPLES_BUF_SIZE
Definition: mpegaudioenc_template.c:46
MPA_MAX_CODED_FRAME_SIZE
#define MPA_MAX_CODED_FRAME_SIZE
Definition: mpegaudio.h:40
int
int
Definition: ffmpeg_filter.c:153
put_bits.h
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
mpegaudiotab.h
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:34
nb_channels
int nb_channels
Definition: channel_layout.c:81