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
32
33 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
34 #define WFRAC_BITS 14 /* fractional bits for window */
35
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
48 int lsf;
/* 1 if mpeg2 low bitrate selected */
51 int frame_size;
/* frame size, in bits, without padding */
52 /* padding computation */
58 /* code to group 3 scale factors */
60 int sblimit;
/* number of used subbands */
63
64 /* define it to use floats in quantization (I don't like floats !) */
66
69
71 {
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);
82 }
83 bitrate = bitrate / 1000;
86 avctx->
delay = 512 - 32 + 1;
87
88 /* encoding freq */
90 for(i=0;i<3;i++) {
92 break;
95 break;
96 }
97 }
98 if (i == 3){
101 }
103
104 /* encoding bitrate & frequency */
105 for(i=0;i<15;i++) {
107 break;
108 }
109 if (i == 15){
112 }
114
115 /* compute total header size & pad bit */
116
119
120 /* frame fractional size to compute padding */
123
124 /* select the right allocation table */
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",
133
136
137 for(i=0;i<257;i++) {
138 int v;
140 #if WFRAC_BITS != 16
142 #endif
144 if ((i & 63) != 0)
145 v = -v;
146 if (i != 0)
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
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;
176 }
177
178 for(i=0;i<17;i++) {
180 if (v < 0)
181 v = -v;
182 else
183 v = v * 3;
185 }
186
187 #if FF_API_OLD_ENCODE_AUDIO
191 #endif
192
193 return 0;
194 }
195
196 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
198 {
199 int i, j;
202
203 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
204
205 t = tab + 30;
206 t1 = tab + 2;
207 do {
208 t[0] += t[-4];
209 t[1] += t[1 - 4];
210 t -= 4;
211 } while (t != t1);
212
213 t = tab + 28;
214 t1 = tab + 4;
215 do {
216 t[0] += t[-8];
217 t[1] += t[1-8];
218 t[2] += t[2-8];
219 t[3] += t[3-8];
220 t -= 8;
221 } while (t != t1);
222
224 t1 = tab + 32;
225 do {
226 t[ 3] = -t[ 3];
227 t[ 6] = -t[ 6];
228
229 t[11] = -t[11];
230 t[12] = -t[12];
231 t[13] = -t[13];
232 t[15] = -t[15];
233 t += 16;
234 } while (t != t1);
235
236
238 t1 = tab + 8;
239 do {
240 int x1, x2, x3, x4;
241
243 x4 = t[0] - x3;
244 x3 = t[0] + x3;
245
247 x1 =
MUL((t[8] - x2), xp[0]);
248 x2 =
MUL((t[8] + x2), xp[1]);
249
250 t[ 0] = x3 + x1;
251 t[ 8] = x4 - x2;
252 t[16] = x4 + x2;
253 t[24] = x3 - x1;
254 t++;
255 } while (t != t1);
256
257 xp += 2;
259 t1 = tab + 4;
260 do {
261 xr =
MUL(t[28],xp[0]);
262 t[28] = (t[0] - xr);
263 t[0] = (t[0] + xr);
264
265 xr =
MUL(t[4],xp[1]);
266 t[ 4] = (t[24] - xr);
267 t[24] = (t[24] + xr);
268
269 xr =
MUL(t[20],xp[2]);
270 t[20] = (t[8] - xr);
271 t[ 8] = (t[8] + xr);
272
273 xr =
MUL(t[12],xp[3]);
274 t[12] = (t[16] - xr);
275 t[16] = (t[16] + xr);
276 t++;
277 } while (t != t1);
278 xp += 4;
279
280 for (i = 0; i < 4; i++) {
281 xr =
MUL(tab[30-i*4],xp[0]);
282 tab[30-i*4] = (tab[i*4] - xr);
283 tab[ i*4] = (tab[i*4] + xr);
284
285 xr =
MUL(tab[ 2+i*4],xp[1]);
286 tab[ 2+i*4] = (tab[28-i*4] - xr);
287 tab[28-i*4] = (tab[28-i*4] + xr);
288
289 xr =
MUL(tab[31-i*4],xp[0]);
290 tab[31-i*4] = (tab[1+i*4] - xr);
291 tab[ 1+i*4] = (tab[1+i*4] + xr);
292
293 xr =
MUL(tab[ 3+i*4],xp[1]);
294 tab[ 3+i*4] = (tab[29-i*4] - xr);
295 tab[29-i*4] = (tab[29-i*4] + xr);
296
297 xp += 2;
298 }
299
300 t = tab + 30;
301 t1 = tab + 1;
302 do {
303 xr =
MUL(t1[0], *xp);
304 t1[0] = (t[0] - xr);
305 t[0] = (t[0] + xr);
306 t -= 2;
307 t1 += 2;
308 xp++;
309 } while (t >= tab);
310
311 for(i=0;i<32;i++) {
313 }
314 }
315
316 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
317
319 {
320 short *p, *q;
322 int tmp[64];
323 int tmp1[32];
324 int *out;
325
328 for(j=0;j<36;j++) {
329 /* 32 samples at once */
330 for(i=0;i<32;i++) {
331 s->
samples_buf[ch][offset + (31 - i)] = samples[0];
332 samples += incr;
333 }
334
335 /* filter */
338 /* maxsum = 23169 */
339 for(i=0;i<64;i++) {
340 sum = p[0*64] * q[0*64];
341 sum += p[1*64] * q[1*64];
342 sum += p[2*64] * q[2*64];
343 sum += p[3*64] * q[3*64];
344 sum += p[4*64] * q[4*64];
345 sum += p[5*64] * q[5*64];
346 sum += p[6*64] * q[6*64];
347 sum += p[7*64] * q[7*64];
348 tmp[i] = sum;
349 p++;
350 q++;
351 }
352 tmp1[0] = tmp[16] >>
WSHIFT;
353 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >>
WSHIFT;
354 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >>
WSHIFT;
355
357
358 /* advance of 32 samples */
359 offset -= 32;
360 out += 32;
361 /* handle the wrap around */
362 if (offset < 0) {
366 }
367 }
369 }
370
372 unsigned char scale_factors[SBLIMIT][3],
373 int sb_samples[3][12][SBLIMIT],
374 int sblimit)
375 {
376 int *p, vmax, v, n, i, j, k, code;
378 unsigned char *sf = &scale_factors[0][0];
379
380 for(j=0;j<sblimit;j++) {
381 for(i=0;i<3;i++) {
382 /* find the max absolute value */
383 p = &sb_samples[i][0][j];
384 vmax = abs(*p);
385 for(k=1;k<12;k++) {
387 v = abs(*p);
388 if (v > vmax)
389 vmax = v;
390 }
391 /* compute the scale factor index using log 2 computations */
392 if (vmax > 1) {
394 /* n is the position of the MSB of vmax. now
395 use at most 2 compares to find the index */
396 index = (21 - n) * 3 - 3;
397 if (index >= 0) {
399 index++;
400 } else {
401 index = 0; /* very unlikely case of overflow */
402 }
403 } else {
404 index = 62; /* value 63 is not allowed */
405 }
406
409 /* store the scale factor */
412 }
413
414 /* compute the transmission factor : look if the scale factors
415 are close enough to each other */
418
419 /* handle the 25 cases */
420 switch(d1 * 5 + d2) {
421 case 0*5+0:
422 case 0*5+4:
423 case 3*5+4:
424 case 4*5+0:
425 case 4*5+4:
426 code = 0;
427 break;
428 case 0*5+1:
429 case 0*5+2:
430 case 4*5+1:
431 case 4*5+2:
432 code = 3;
433 sf[2] = sf[1];
434 break;
435 case 0*5+3:
436 case 4*5+3:
437 code = 3;
438 sf[1] = sf[2];
439 break;
440 case 1*5+0:
441 case 1*5+4:
442 case 2*5+4:
443 code = 1;
444 sf[1] = sf[0];
445 break;
446 case 1*5+1:
447 case 1*5+2:
448 case 2*5+0:
449 case 2*5+1:
450 case 2*5+2:
451 code = 2;
452 sf[1] = sf[2] = sf[0];
453 break;
454 case 2*5+3:
455 case 3*5+3:
456 code = 2;
457 sf[0] = sf[1] = sf[2];
458 break;
459 case 3*5+0:
460 case 3*5+1:
461 case 3*5+2:
462 code = 2;
463 sf[0] = sf[2] = sf[1];
464 break;
465 case 1*5+3:
466 code = 2;
467 if (sf[0] > sf[2])
468 sf[0] = sf[2];
469 sf[1] = sf[2] = sf[0];
470 break;
471 default:
473 code = 0; /* kill warning */
474 }
475
477 sf[0], sf[1], sf[2], d1, d2, code);
478 scale_code[j] = code;
479 sf += 3;
480 }
481 }
482
483 /* The most important function : psycho acoustic module. In this
484 encoder there is basically none, so this is the worst you can do,
485 but also this is the simpler. */
487 {
488 int i;
489
492 }
493 }
494
495
496 #define SB_NOTALLOCATED 0
497 #define SB_ALLOCATED 1
499
500 /* Try to maximize the smr while using a number of bits inferior to
501 the frame size. I tried to make the code simpler, faster and
502 smaller than other encoders :-) */
506 int *padding)
507 {
508 int i, ch,
b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
509 int incr;
512 const unsigned char *alloc;
513
514 memcpy(smr, smr1, s->
nb_channels *
sizeof(
short) * SBLIMIT);
517
518 /* compute frame size and padding */
524 max_frame_size += 8;
525 } else {
527 }
528
529 /* compute the header + bit alloc size */
530 current_frame_size = 32;
533 incr = alloc[0];
535 alloc += 1 << incr;
536 }
537 for(;;) {
538 /* look for the subband with the largest signal to mask ratio */
539 max_sb = -1;
540 max_ch = -1;
541 max_smr = INT_MIN;
544 if (smr[ch][i] > max_smr && subband_status[ch][i] !=
SB_NOMORE) {
545 max_smr = smr[ch][i];
546 max_sb = i;
547 max_ch = ch;
548 }
549 }
550 }
551 if (max_sb < 0)
552 break;
553 av_dlog(
NULL,
"current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n",
554 current_frame_size, max_frame_size, max_sb, max_ch,
556
557 /* find alloc table entry (XXX: not optimal, should use
558 pointer table) */
560 for(i=0;i<max_sb;i++) {
561 alloc += 1 << alloc[0];
562 }
563
565 /* nothing was coded for this band: add the necessary bits */
568 } else {
569 /* increments bit allocation */
573 }
574
575 if (current_frame_size + incr <= max_frame_size) {
576 /* can increase size */
578 current_frame_size += incr;
579 /* decrease smr by the resolution we added */
580 smr[max_ch][max_sb] = smr1[max_ch][max_sb] -
quant_snr[alloc[
b]];
581 /* max allocation size reached ? */
582 if (b == ((1 << alloc[0]) - 1))
583 subband_status[max_ch][max_sb] =
SB_NOMORE;
584 else
586 } else {
587 /* cannot increase the size of this subband */
588 subband_status[max_ch][max_sb] =
SB_NOMORE;
589 }
590 }
591 *padding = max_frame_size - current_frame_size;
593 }
594
595 /*
596 * Output the mpeg audio layer 2 frame. Note how the code is small
597 * compared to other encoders :-)
598 */
601 int padding)
602 {
603 int i, j, k, l, bit_alloc_bits,
b, ch;
604 unsigned char *sf;
605 int q[3];
607
608 /* header */
609
611 put_bits(p, 1, 1 - s->
lsf);
/* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
613 put_bits(p, 1, 1);
/* no error protection */
617 put_bits(p, 1, 0);
/* private_bit */
620 put_bits(p, 1, 0);
/* no copyright */
622 put_bits(p, 2, 0);
/* no emphasis */
623
624 /* bit allocation */
625 j = 0;
630 }
631 j += 1 << bit_alloc_bits;
632 }
633
634 /* scale codes */
639 }
640 }
641
642 /* scale factors */
648 case 0:
652 break;
653 case 3:
654 case 1:
657 break;
658 case 2:
660 break;
661 }
662 }
663 }
664 }
665
666 /* quantization & write sub band samples */
667
668 for(k=0;k<3;k++) {
669 for(l=0;l<12;l+=3) {
670 j = 0;
675 if (b) {
677 /* we encode 3 sub band samples of the same sub band at a time */
680 for(m=0;m<3;m++) {
682 /* divide by scale factor */
683 #ifdef USE_FLOATS
684 {
686 a = (float)sample * scale_factor_inv_table[s->
scale_factors[ch][i][k]];
687 q[m] = (int)((a + 1.0) * steps * 0.5);
688 }
689 #else
690 {
695
696 /* normalize to P bits */
697 if (shift < 0)
698 q1 = sample << (-
shift);
699 else
700 q1 = sample >>
shift;
701 q1 = (q1 *
mult) >> P;
702 q[
m] = ((q1 + (1 << P)) * steps) >> (P + 1);
703 }
704 #endif
705 if (q[m] >= steps)
708 }
710 if (bits < 0) {
711 /* group the 3 values to save bits */
713 q[0] + steps * (q[1] + steps * q[2]));
714 } else {
718 }
719 }
720 }
721 /* next subband in alloc table */
722 j += 1 << bit_alloc_bits;
723 }
724 }
725 }
726
727 /* padding */
728 for(i=0;i<padding;i++)
730
731 /* flush */
733 }
734
737 {
739 const int16_t *
samples = (
const int16_t *)frame->
data[0];
742 int padding, i, ret;
743
746 }
747
751 }
754 }
756
758 return ret;
759
761
763
766
768 *got_packet_ptr = 1;
769 return 0;
770 }
771
773 {
774 #if FF_API_OLD_ENCODE_AUDIO
776 #endif
777 return 0;
778 }
779
781 { "b", "128k" },
783 };
784
795 .supported_samplerates = (const int[]){
796 44100, 48000, 32000, 22050, 24000, 16000, 0
797 },
800 0 },
803 };