FFmpeg
Loading...
Searching...
No Matches
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
27#include "config.h"
28#include "config_components.h"
29
30#include "libavutil/avassert.h"
32
33#include "avcodec.h"
34#include "codec_internal.h"
35#include "encode.h"
36#include "put_bits.h"
37
38#define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
39#define WFRAC_BITS 14 /* fractional bits for window */
40
41#include "mpegaudio.h"
42#include "mpegaudiodsp.h"
43#include "mpegaudiodata.h"
44#include "mpegaudiotab.h"
45
46/* currently, cannot change these constants (need to modify
47 quantization stage) */
48#define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
49
50#define SAMPLES_BUF_SIZE 4096
51
52typedef struct MpegAudioContext {
54 int lsf; /* 1 if mpeg2 low bitrate selected */
55 int bitrate_index; /* bit rate */
57 int frame_size; /* frame size, in bits, without padding */
59 /* padding computation */
61 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
62 int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
64 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
65 /* code to group 3 scale factors */
67 int sblimit; /* number of used subbands */
68 const unsigned char *alloc_table;
69 int16_t filter_bank[512];
71 unsigned char scale_diff_table[128];
72 union {
74 struct {
76 unsigned short scale_factor_mult[64];
77 };
78 };
79 unsigned short total_quant_bits[17]; /* total number of bits per allocation group */
81
82#define IS_FIXED(s) (CONFIG_MP2_ENCODER && CONFIG_MP2FIXED_ENCODER ? (s)->is_fixed : CONFIG_MP2FIXED_ENCODER)
83
85{
87 int freq = avctx->sample_rate;
88 int bitrate = avctx->bit_rate;
89 int channels = avctx->ch_layout.nb_channels;
90 int i, table;
91 float a;
92
93 bitrate = bitrate / 1000;
94 s->nb_channels = channels;
96 avctx->initial_padding = 512 - 32 + 1;
97
98 /* encoding freq */
99 s->lsf = 0;
100 for (i = 0;; i++) {
101 av_assert1(i < 3);
102 if (ff_mpa_freq_tab[i] == freq)
103 break;
104 if ((ff_mpa_freq_tab[i] / 2) == freq) {
105 s->lsf = 1;
106 break;
107 }
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 int 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 (IS_FIXED(s)) {
168#define P 15
169 s->scale_factor_shift[i] = 21 - P - (i / 3);
170 s->scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0);
171 } else {
172 s->scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20);
173 }
174 }
175 for(i=0;i<128;i++) {
176 int 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 int 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 */
203static 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
324static 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 return max_frame_size / 8U;
601}
602
603/// Quantization & write sub band samples
605 PutBitContext *const p,
606 const uint8_t bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
607 int is_fixed)
608{
609 for (int k = 0; k < 3; ++k) {
610 for (int l = 0; l < 12; l += 3) {
611 for (int i = 0, j = 0; i < s->sblimit; ++i) {
612 const int bit_alloc_bits = s->alloc_table[j];
613 for (int ch = 0; ch < s->nb_channels; ++ch) {
614 const int b = bit_alloc[ch][i];
615 if (b) {
616 /* we encode 3 sub band samples of the same sub band at a time */
617 const int qindex = s->alloc_table[j + b];
618 const int steps = ff_mpa_quant_steps[qindex];
619 int q[3];
620
621 for (int m = 0; m < 3; ++m) {
622 const int sample = s->sb_samples[ch][k][l + m][i];
623 /* divide by scale factor */
624 if (!is_fixed) {
625 float a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]];
626 q[m] = (int)((a + 1.0) * steps * 0.5);
627 } else {
628 const int e = s->scale_factors[ch][i][k];
629 const int shift = s->scale_factor_shift[e];
630 const int mult = s->scale_factor_mult[e];
631 int q1;
632
633 /* normalize to P bits */
634 if (shift < 0)
635 q1 = sample * (1 << -shift);
636 else
637 q1 = sample >> shift;
638 q1 = (q1 * mult) >> P;
639 q1 += 1 << P;
640 if (q1 < 0)
641 q1 = 0;
642 q[m] = (q1 * (unsigned)steps) >> (P + 1);
643 }
644 if (q[m] >= steps)
645 q[m] = steps - 1;
646 av_assert2(q[m] >= 0 && q[m] < steps);
647 }
648 const int bits = ff_mpa_quant_bits[qindex];
649 if (bits < 0) {
650 /* group the 3 values to save bits */
651 put_bits(p, -bits,
652 q[0] + steps * (q[1] + steps * q[2]));
653 } else {
654 put_bits(p, bits, q[0]);
655 put_bits(p, bits, q[1]);
656 put_bits(p, bits, q[2]);
657 }
658 }
659 }
660 /* next subband in alloc table */
661 j += 1 << bit_alloc_bits;
662 }
663 }
664 }
665}
666
667/*
668 * Output the MPEG audio layer 2 frame. Note how the code is small
669 * compared to other encoders :-)
670 */
671static void encode_frame(MpegAudioContext *s, uint8_t *buf, unsigned buf_size,
672 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
673 int padding)
674{
675 int i, j, bit_alloc_bits, ch;
676 unsigned char *sf;
677 PutBitContext p0, *p = &p0;
678
679 init_put_bits(p, buf, buf_size);
680
681 /* header */
682
683 put_bits(p, 12, 0xfff);
684 put_bits(p, 1, 1 - s->lsf); /* 1 = MPEG-1 ID, 0 = MPEG-2 lsf ID */
685 put_bits(p, 2, 4-2); /* layer 2 */
686 put_bits(p, 1, 1); /* no error protection */
687 put_bits(p, 4, s->bitrate_index);
688 put_bits(p, 2, s->freq_index);
689 put_bits(p, 1, s->do_padding); /* use padding */
690 put_bits(p, 1, 0); /* private_bit */
691 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
692 put_bits(p, 2, 0); /* mode_ext */
693 put_bits(p, 1, 0); /* no copyright */
694 put_bits(p, 1, 1); /* original */
695 put_bits(p, 2, 0); /* no emphasis */
696
697 /* bit allocation */
698 j = 0;
699 for(i=0;i<s->sblimit;i++) {
700 bit_alloc_bits = s->alloc_table[j];
701 for(ch=0;ch<s->nb_channels;ch++) {
702 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
703 }
704 j += 1 << bit_alloc_bits;
705 }
706
707 /* scale codes */
708 for(i=0;i<s->sblimit;i++) {
709 for(ch=0;ch<s->nb_channels;ch++) {
710 if (bit_alloc[ch][i])
711 put_bits(p, 2, s->scale_code[ch][i]);
712 }
713 }
714
715 /* scale factors */
716 for(i=0;i<s->sblimit;i++) {
717 for(ch=0;ch<s->nb_channels;ch++) {
718 if (bit_alloc[ch][i]) {
719 sf = &s->scale_factors[ch][i][0];
720 switch(s->scale_code[ch][i]) {
721 case 0:
722 put_bits(p, 18, sf[0] << 12 | sf[1] << 6 | sf[2]);
723 break;
724 case 3:
725 case 1:
726 put_bits(p, 12, sf[0] << 6 | sf[2]);
727 break;
728 case 2:
729 put_bits(p, 6, sf[0]);
730 break;
731 }
732 }
733 }
734 }
735
736#if CONFIG_SMALL
738#else
739 if (IS_FIXED(s))
741 else
743#endif
744
745 av_assert1(put_bits_left(p) == padding);
746
747 /* flush */
749
750 /* padding */
751 if (put_bytes_left(p, 0))
752 memset(put_bits_ptr(p), 0, put_bytes_left(p, 0));
753}
754
755static int mpa_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
756 const AVFrame *frame, int *got_packet_ptr)
757{
758 MpegAudioContext *s = avctx->priv_data;
759 const int16_t *samples = (const int16_t *)frame->data[0];
760 short smr[MPA_MAX_CHANNELS][SBLIMIT];
761 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
762 int padding, i, ret;
763
764 for(i=0;i<s->nb_channels;i++) {
765 filter(s, i, samples + i, s->nb_channels);
766 }
767
768 for(i=0;i<s->nb_channels;i++) {
769 compute_scale_factors(s, s->scale_code[i], s->scale_factors[i],
770 s->sb_samples[i], s->sblimit);
771 }
772 for(i=0;i<s->nb_channels;i++) {
774 }
775 unsigned frame_size = compute_bit_allocation(s, smr, bit_alloc, &padding);
776
777 ret = ff_get_encode_buffer(avctx, avpkt, frame_size, 0);
778 if (ret < 0)
779 return ret;
780
781 encode_frame(s, avpkt->data, frame_size, bit_alloc, padding);
782
783 if (frame->pts != AV_NOPTS_VALUE)
784 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
785
786 *got_packet_ptr = 1;
787 return 0;
788}
789
790static const FFCodecDefault mp2_defaults[] = {
791 { "b", "0" },
792 { NULL },
793};
794
795#if CONFIG_MP2_ENCODER
796const FFCodec ff_mp2_encoder = {
797 .p.name = "mp2",
798 CODEC_LONG_NAME("MP2 (MPEG audio layer 2)"),
799 .p.type = AVMEDIA_TYPE_AUDIO,
800 .p.id = AV_CODEC_ID_MP2,
802 .priv_data_size = sizeof(MpegAudioContext),
806 CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
808 .defaults = mp2_defaults,
809};
810#endif
811
812#if CONFIG_MP2FIXED_ENCODER
813static av_cold int mpa_fixed_encode_init(AVCodecContext *avctx)
814{
815 MpegAudioContext *s = avctx->priv_data;
816
817 s->is_fixed = 1;
818 return mpa_encode_init(avctx);
819}
820
822 .p.name = "mp2fixed",
823 CODEC_LONG_NAME("MP2 fixed point (MPEG audio layer 2)"),
824 .p.type = AVMEDIA_TYPE_AUDIO,
825 .p.id = AV_CODEC_ID_MP2,
827 .priv_data_size = sizeof(MpegAudioContext),
828 .init = mpa_fixed_encode_init,
831 CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
833 .defaults = mp2_defaults,
834};
835#endif
static int bit_alloc(AC3EncodeContext *s, int snr_offset)
Run the bit allocation with a given SNR offset.
Definition ac3enc.c:1365
const FFCodec ff_mp2fixed_encoder
const FFCodec ff_mp2_encoder
static FILE * out
channels
Definition aptx.h:31
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define CODEC_CH_LAYOUTS(...)
#define CODEC_SAMPLERATES(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
#define NULL
Definition coverity.c:32
#define abs(x)
static __device__ float floor(float a)
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
static av_always_inline int64_t ff_samples_to_time_base(const AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition encode.h:96
static const uint8_t bits[8]
Definition fastaudio.c:100
#define sample
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_MP2
Definition codec_id.h:453
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
int index
Definition gxfenc.c:90
int a
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
#define FIX(x)
Definition jrevdct.c:148
unsigned offset
Definition libaomenc.c:763
static const float scale_factors[25]
scale factor for each decoded exponent: 2^-exp
Definition ac3dec.c:55
static int shift(int a, int b)
Definition bonk.c:261
static int16_t mult(Float11 *f1, Float11 *f2)
Definition g726.c:60
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
#define exp2(x)
Definition libm.h:290
#define M_SQRT2
static const int16_t steps[16]
Definition misc4.c:30
int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
Definition mpegaudio.c:31
mpeg audio declarations for both encoder and decoder.
#define WFRAC_BITS
Definition mpegaudio.h:53
#define MPA_FRAME_SIZE
Definition mpegaudio.h:37
#define MPA_MONO
Definition mpegaudio.h:49
#define MPA_MAX_CHANNELS
Definition mpegaudio.h:42
#define MPA_STEREO
Definition mpegaudio.h:46
#define SBLIMIT
Definition mpegaudio.h:44
const int ff_mpa_quant_bits[17]
const unsigned char *const ff_mpa_alloc_tables[5]
const int ff_mpa_sblimit_table[5]
const int ff_mpa_quant_steps[17]
mpeg audio layer common tables.
FF_VISIBILITY_PUSH_HIDDEN const uint16_t ff_mpa_bitrate_tab[2][3][15]
const uint16_t ff_mpa_freq_tab[3]
const int32_t ff_mpa_enwindow[257]
#define WSHIFT
static void idct32(int *out, int *tab)
#define P
#define SB_NOMORE
#define SAMPLES_BUF_SIZE
#define MUL(a, b)
static av_cold int mpa_encode_init(AVCodecContext *avctx)
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)
static int mpa_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
#define IS_FIXED(s)
static av_always_inline void encode_subbands(MpegAudioContext *const s, PutBitContext *const p, const uint8_t bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int is_fixed)
Quantization & write sub band samples.
static const FFCodecDefault mp2_defaults[]
static unsigned compute_bit_allocation(MpegAudioContext *s, short smr1[MPA_MAX_CHANNELS][SBLIMIT], unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int *padding)
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
#define SB_ALLOCATED
#define SB_NOTALLOCATED
static void encode_frame(MpegAudioContext *s, uint8_t *buf, unsigned buf_size, unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int padding)
mpeg audio layer 2 tables.
static const unsigned short quant_snr[17]
static const int bitinv32[32]
static const int costab32[30]
static const unsigned char nb_scale_factors[4]
static const float fixed_smr[SBLIMIT]
static const uint16_t table[]
Definition prosumer.c:203
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition put_bits.h:402
static int put_bits_left(PutBitContext *s)
Definition put_bits.h:135
static int put_bytes_left(const PutBitContext *s, int round_up)
Definition put_bits.h:145
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
const uint8_t * code
Definition spdifenc.c:433
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int initial_padding
Audio only.
Definition avcodec.h:1114
int sample_rate
samples per second
Definition avcodec.h:1040
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:596
uint8_t * data
Definition packet.h:603
const unsigned char * alloc_table
int samples_offset[MPA_MAX_CHANNELS]
int8_t scale_factor_shift[64]
int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]
short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]
unsigned short total_quant_bits[17]
unsigned short scale_factor_mult[64]
unsigned char scale_diff_table[128]
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]
int16_t filter_bank[512]
float scale_factor_inv_table[64]
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]
int scale_factor_table[64]
#define ff_dlog(a,...)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int64_t bitrate
Definition av1_levels.c:47
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static const struct twinvq_data tab
static const uint8_t q1[256]
Definition twofish.c:100