FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegaudioenc.c
Date: 2026-09-21 20:56:46
Exec Total Coverage
Lines: 404 412 98.1%
Functions: 10 10 100.0%
Branches: 171 181 94.5%

Line Branch Exec Source
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"
31 #include "libavutil/channel_layout.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
52 typedef struct MpegAudioContext {
53 int nb_channels;
54 int lsf; /* 1 if mpeg2 low bitrate selected */
55 int bitrate_index; /* bit rate */
56 int freq_index;
57 int frame_size; /* frame size, in bits, without padding */
58 int is_fixed;
59 /* padding computation */
60 int frame_frac, frame_frac_incr, do_padding;
61 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
62 int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
63 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
64 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
65 /* code to group 3 scale factors */
66 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
67 int sblimit; /* number of used subbands */
68 const unsigned char *alloc_table;
69 int16_t filter_bank[512];
70 int scale_factor_table[64];
71 unsigned char scale_diff_table[128];
72 union {
73 float scale_factor_inv_table[64];
74 struct {
75 int8_t scale_factor_shift[64];
76 unsigned short scale_factor_mult[64];
77 };
78 };
79 unsigned short total_quant_bits[17]; /* total number of bits per allocation group */
80 } MpegAudioContext;
81
82 #define IS_FIXED(s) (CONFIG_MP2_ENCODER && CONFIG_MP2FIXED_ENCODER ? (s)->is_fixed : CONFIG_MP2FIXED_ENCODER)
83
84 27 static av_cold int mpa_encode_init(AVCodecContext *avctx)
85 {
86 27 MpegAudioContext *s = avctx->priv_data;
87 27 int freq = avctx->sample_rate;
88 27 int bitrate = avctx->bit_rate;
89 27 int channels = avctx->ch_layout.nb_channels;
90 int i, table;
91 float a;
92
93 27 bitrate = bitrate / 1000;
94 27 s->nb_channels = channels;
95 27 avctx->frame_size = MPA_FRAME_SIZE;
96 27 avctx->initial_padding = 512 - 32 + 1;
97
98 /* encoding freq */
99 27 s->lsf = 0;
100 27 for (i = 0;; i++) {
101 2 av_assert1(i < 3);
102
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 3 times.
29 if (ff_mpa_freq_tab[i] == freq)
103 26 break;
104
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if ((ff_mpa_freq_tab[i] / 2) == freq) {
105 1 s->lsf = 1;
106 1 break;
107 }
108 }
109 27 s->freq_index = i;
110
111 /* encoding bitrate & frequency */
112
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 15 times.
287 for(i=1;i<15;i++) {
113
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 260 times.
272 if (ff_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
114 12 break;
115 }
116
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
27 if (i == 15 && !avctx->bit_rate) {
117 15 i = 14;
118 15 bitrate = ff_mpa_bitrate_tab[s->lsf][1][i];
119 15 avctx->bit_rate = bitrate * 1000;
120 }
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 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 27 s->bitrate_index = i;
126
127 /* compute total header size & pad bit */
128
129 27 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
130 27 s->frame_size = ((int)a) * 8;
131
132 /* frame fractional size to compute padding */
133 27 s->frame_frac = 0;
134 27 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
135
136 /* select the right allocation table */
137 27 table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
138
139 /* number of used subbands */
140 27 s->sblimit = ff_mpa_sblimit_table[table];
141 27 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
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 27 times.
57 for(i=0;i<s->nb_channels;i++)
147 30 s->samples_offset[i] = 0;
148
149
2/2
✓ Branch 0 taken 6939 times.
✓ Branch 1 taken 27 times.
6966 for(i=0;i<257;i++) {
150 int v;
151 6939 v = ff_mpa_enwindow[i];
152 #if WFRAC_BITS != 16
153 6939 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
154 #endif
155 6939 s->filter_bank[i] = v;
156
2/2
✓ Branch 0 taken 6804 times.
✓ Branch 1 taken 135 times.
6939 if ((i & 63) != 0)
157 6804 v = -v;
158
2/2
✓ Branch 0 taken 6912 times.
✓ Branch 1 taken 27 times.
6939 if (i != 0)
159 6912 s->filter_bank[512 - i] = v;
160 }
161
162
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 27 times.
1755 for(i=0;i<64;i++) {
163 1728 int v = (int)(exp2((3 - i) / 3.0) * (1 << 20));
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1728 times.
1728 if (v <= 0)
165 v = 1;
166 1728 s->scale_factor_table[i] = v;
167
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 768 times.
1728 if (IS_FIXED(s)) {
168 #define P 15
169 960 s->scale_factor_shift[i] = 21 - P - (i / 3);
170 960 s->scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0);
171 } else {
172 768 s->scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20);
173 }
174 }
175
2/2
✓ Branch 0 taken 3456 times.
✓ Branch 1 taken 27 times.
3483 for(i=0;i<128;i++) {
176 3456 int v = i - 64;
177
2/2
✓ Branch 0 taken 1674 times.
✓ Branch 1 taken 1782 times.
3456 if (v <= -3)
178 1674 v = 0;
179
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1728 times.
1782 else if (v < 0)
180 54 v = 1;
181
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1701 times.
1728 else if (v == 0)
182 27 v = 2;
183
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1647 times.
1701 else if (v < 3)
184 54 v = 3;
185 else
186 1647 v = 4;
187 3456 s->scale_diff_table[i] = v;
188 }
189
190
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 27 times.
486 for(i=0;i<17;i++) {
191 459 int v = ff_mpa_quant_bits[i];
192
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 378 times.
459 if (v < 0)
193 81 v = -v;
194 else
195 378 v = v * 3;
196 459 s->total_quant_bits[i] = 12 * v;
197 }
198
199 27 return 0;
200 }
201
202 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
203 357732 static void idct32(int *out, int *tab)
204 {
205 int i, j;
206 int *t, *t1, xr;
207 357732 const int *xp = costab32;
208
209
2/2
✓ Branch 0 taken 5365980 times.
✓ Branch 1 taken 357732 times.
5723712 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
210
211 357732 t = tab + 30;
212 357732 t1 = tab + 2;
213 do {
214 2504124 t[0] += t[-4];
215 2504124 t[1] += t[1 - 4];
216 2504124 t -= 4;
217
2/2
✓ Branch 0 taken 2146392 times.
✓ Branch 1 taken 357732 times.
2504124 } while (t != t1);
218
219 357732 t = tab + 28;
220 357732 t1 = tab + 4;
221 do {
222 1073196 t[0] += t[-8];
223 1073196 t[1] += t[1-8];
224 1073196 t[2] += t[2-8];
225 1073196 t[3] += t[3-8];
226 1073196 t -= 8;
227
2/2
✓ Branch 0 taken 715464 times.
✓ Branch 1 taken 357732 times.
1073196 } while (t != t1);
228
229 357732 t = tab;
230 357732 t1 = tab + 32;
231 do {
232 715464 t[ 3] = -t[ 3];
233 715464 t[ 6] = -t[ 6];
234
235 715464 t[11] = -t[11];
236 715464 t[12] = -t[12];
237 715464 t[13] = -t[13];
238 715464 t[15] = -t[15];
239 715464 t += 16;
240
2/2
✓ Branch 0 taken 357732 times.
✓ Branch 1 taken 357732 times.
715464 } while (t != t1);
241
242
243 357732 t = tab;
244 357732 t1 = tab + 8;
245 do {
246 int x1, x2, x3, x4;
247
248 2861856 x3 = MUL(t[16], FIX(M_SQRT2*0.5));
249 2861856 x4 = t[0] - x3;
250 2861856 x3 = t[0] + x3;
251
252 2861856 x2 = MUL(-(t[24] + t[8]), FIX(M_SQRT2*0.5));
253 2861856 x1 = MUL((t[8] - x2), xp[0]);
254 2861856 x2 = MUL((t[8] + x2), xp[1]);
255
256 2861856 t[ 0] = x3 + x1;
257 2861856 t[ 8] = x4 - x2;
258 2861856 t[16] = x4 + x2;
259 2861856 t[24] = x3 - x1;
260 2861856 t++;
261
2/2
✓ Branch 0 taken 2504124 times.
✓ Branch 1 taken 357732 times.
2861856 } while (t != t1);
262
263 357732 xp += 2;
264 357732 t = tab;
265 357732 t1 = tab + 4;
266 do {
267 1430928 xr = MUL(t[28],xp[0]);
268 1430928 t[28] = (t[0] - xr);
269 1430928 t[0] = (t[0] + xr);
270
271 1430928 xr = MUL(t[4],xp[1]);
272 1430928 t[ 4] = (t[24] - xr);
273 1430928 t[24] = (t[24] + xr);
274
275 1430928 xr = MUL(t[20],xp[2]);
276 1430928 t[20] = (t[8] - xr);
277 1430928 t[ 8] = (t[8] + xr);
278
279 1430928 xr = MUL(t[12],xp[3]);
280 1430928 t[12] = (t[16] - xr);
281 1430928 t[16] = (t[16] + xr);
282 1430928 t++;
283
2/2
✓ Branch 0 taken 1073196 times.
✓ Branch 1 taken 357732 times.
1430928 } while (t != t1);
284 357732 xp += 4;
285
286
2/2
✓ Branch 0 taken 1430928 times.
✓ Branch 1 taken 357732 times.
1788660 for (i = 0; i < 4; i++) {
287 1430928 xr = MUL(tab[30-i*4],xp[0]);
288 1430928 tab[30-i*4] = (tab[i*4] - xr);
289 1430928 tab[ i*4] = (tab[i*4] + xr);
290
291 1430928 xr = MUL(tab[ 2+i*4],xp[1]);
292 1430928 tab[ 2+i*4] = (tab[28-i*4] - xr);
293 1430928 tab[28-i*4] = (tab[28-i*4] + xr);
294
295 1430928 xr = MUL(tab[31-i*4],xp[0]);
296 1430928 tab[31-i*4] = (tab[1+i*4] - xr);
297 1430928 tab[ 1+i*4] = (tab[1+i*4] + xr);
298
299 1430928 xr = MUL(tab[ 3+i*4],xp[1]);
300 1430928 tab[ 3+i*4] = (tab[29-i*4] - xr);
301 1430928 tab[29-i*4] = (tab[29-i*4] + xr);
302
303 1430928 xp += 2;
304 }
305
306 357732 t = tab + 30;
307 357732 t1 = tab + 1;
308 do {
309 5723712 xr = MUL(t1[0], *xp);
310 5723712 t1[0] = (t[0] - xr);
311 5723712 t[0] = (t[0] + xr);
312 5723712 t -= 2;
313 5723712 t1 += 2;
314 5723712 xp++;
315
2/2
✓ Branch 0 taken 5365980 times.
✓ Branch 1 taken 357732 times.
5723712 } while (t >= tab);
316
317
2/2
✓ Branch 0 taken 11447424 times.
✓ Branch 1 taken 357732 times.
11805156 for(i=0;i<32;i++) {
318 11447424 out[i] = tab[bitinv32[i]];
319 }
320 357732 }
321
322 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
323
324 9937 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 9937 offset = s->samples_offset[ch];
333 9937 out = &s->sb_samples[ch][0][0][0];
334
2/2
✓ Branch 0 taken 357732 times.
✓ Branch 1 taken 9937 times.
367669 for(j=0;j<36;j++) {
335 /* 32 samples at once */
336
2/2
✓ Branch 0 taken 11447424 times.
✓ Branch 1 taken 357732 times.
11805156 for(i=0;i<32;i++) {
337 11447424 s->samples_buf[ch][offset + (31 - i)] = samples[0];
338 11447424 samples += incr;
339 }
340
341 /* filter */
342 357732 p = s->samples_buf[ch] + offset;
343 357732 q = s->filter_bank;
344 /* maxsum = 23169 */
345
2/2
✓ Branch 0 taken 22894848 times.
✓ Branch 1 taken 357732 times.
23252580 for(i=0;i<64;i++) {
346 22894848 sum = p[0*64] * q[0*64];
347 22894848 sum += p[1*64] * q[1*64];
348 22894848 sum += p[2*64] * q[2*64];
349 22894848 sum += p[3*64] * q[3*64];
350 22894848 sum += p[4*64] * q[4*64];
351 22894848 sum += p[5*64] * q[5*64];
352 22894848 sum += p[6*64] * q[6*64];
353 22894848 sum += p[7*64] * q[7*64];
354 22894848 tmp[i] = sum;
355 22894848 p++;
356 22894848 q++;
357 }
358 357732 tmp1[0] = tmp[16] >> WSHIFT;
359
2/2
✓ Branch 0 taken 5723712 times.
✓ Branch 1 taken 357732 times.
6081444 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
360
2/2
✓ Branch 0 taken 5365980 times.
✓ Branch 1 taken 357732 times.
5723712 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
361
362 357732 idct32(out, tmp1);
363
364 /* advance of 32 samples */
365 357732 offset -= 32;
366 357732 out += 32;
367 /* handle the wrap around */
368
2/2
✓ Branch 0 taken 3188 times.
✓ Branch 1 taken 354544 times.
357732 if (offset < 0) {
369 3188 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
370 3188 s->samples_buf[ch], (512 - 32) * 2);
371 3188 offset = SAMPLES_BUF_SIZE - 512;
372 }
373 }
374 9937 s->samples_offset[ch] = offset;
375 9937 }
376
377 9937 static void compute_scale_factors(MpegAudioContext *s,
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 9937 unsigned char *sf = &scale_factors[0][0];
386
387
2/2
✓ Branch 0 taken 295518 times.
✓ Branch 1 taken 9937 times.
305455 for(j=0;j<sblimit;j++) {
388
2/2
✓ Branch 0 taken 886554 times.
✓ Branch 1 taken 295518 times.
1182072 for(i=0;i<3;i++) {
389 /* find the max absolute value */
390 886554 p = &sb_samples[i][0][j];
391 886554 vmax = abs(*p);
392
2/2
✓ Branch 0 taken 9752094 times.
✓ Branch 1 taken 886554 times.
10638648 for(k=1;k<12;k++) {
393 9752094 p += SBLIMIT;
394 9752094 v = abs(*p);
395
2/2
✓ Branch 0 taken 1627280 times.
✓ Branch 1 taken 8124814 times.
9752094 if (v > vmax)
396 1627280 vmax = v;
397 }
398 /* compute the scale factor index using log 2 computations */
399
2/2
✓ Branch 0 taken 886454 times.
✓ Branch 1 taken 100 times.
886554 if (vmax > 1) {
400 886454 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 886454 index = (21 - n) * 3 - 3;
404
1/2
✓ Branch 0 taken 886454 times.
✗ Branch 1 not taken.
886454 if (index >= 0) {
405
2/2
✓ Branch 0 taken 1099646 times.
✓ Branch 1 taken 886454 times.
1986100 while (vmax <= s->scale_factor_table[index+1])
406 1099646 index++;
407 } else {
408 index = 0; /* very unlikely case of overflow */
409 }
410 } else {
411 100 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 886554 sf[i] = index;
419 }
420
421 /* compute the transmission factor : look if the scale factors
422 are close enough to each other */
423 295518 d1 = s->scale_diff_table[sf[0] - sf[1] + 64];
424 295518 d2 = s->scale_diff_table[sf[1] - sf[2] + 64];
425
426 /* handle the 25 cases */
427
8/9
✓ Branch 0 taken 4644 times.
✓ Branch 1 taken 5321 times.
✓ Branch 2 taken 4073 times.
✓ Branch 3 taken 4864 times.
✓ Branch 4 taken 138068 times.
✓ Branch 5 taken 39274 times.
✓ Branch 6 taken 64868 times.
✓ Branch 7 taken 34406 times.
✗ Branch 8 not taken.
295518 switch(d1 * 5 + d2) {
428 4644 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 4644 code = 0;
434 4644 break;
435 5321 case 0*5+1:
436 case 0*5+2:
437 case 4*5+1:
438 case 4*5+2:
439 5321 code = 3;
440 5321 sf[2] = sf[1];
441 5321 break;
442 4073 case 0*5+3:
443 case 4*5+3:
444 4073 code = 3;
445 4073 sf[1] = sf[2];
446 4073 break;
447 4864 case 1*5+0:
448 case 1*5+4:
449 case 2*5+4:
450 4864 code = 1;
451 4864 sf[1] = sf[0];
452 4864 break;
453 138068 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 138068 code = 2;
459 138068 sf[1] = sf[2] = sf[0];
460 138068 break;
461 39274 case 2*5+3:
462 case 3*5+3:
463 39274 code = 2;
464 39274 sf[0] = sf[1] = sf[2];
465 39274 break;
466 64868 case 3*5+0:
467 case 3*5+1:
468 case 3*5+2:
469 64868 code = 2;
470 64868 sf[0] = sf[2] = sf[1];
471 64868 break;
472 34406 case 1*5+3:
473 34406 code = 2;
474
2/2
✓ Branch 0 taken 5449 times.
✓ Branch 1 taken 28957 times.
34406 if (sf[0] > sf[2])
475 5449 sf[0] = sf[2];
476 34406 sf[1] = sf[2] = sf[0];
477 34406 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 295518 scale_code[j] = code;
486 295518 sf += 3;
487 }
488 9937 }
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. */
493 9937 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
494 {
495 int i;
496
497
2/2
✓ Branch 0 taken 295518 times.
✓ Branch 1 taken 9937 times.
305455 for(i=0;i<s->sblimit;i++) {
498 295518 smr[i] = (int)(fixed_smr[i] * 10);
499 }
500 9937 }
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 :-) */
510 9470 static unsigned compute_bit_allocation(MpegAudioContext *s,
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 9470 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
522 9470 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
523 9470 memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
524
525 /* compute frame size and padding */
526 9470 max_frame_size = s->frame_size;
527 9470 s->frame_frac += s->frame_frac_incr;
528
2/2
✓ Branch 0 taken 8260 times.
✓ Branch 1 taken 1210 times.
9470 if (s->frame_frac >= 65536) {
529 8260 s->frame_frac -= 65536;
530 8260 s->do_padding = 1;
531 8260 max_frame_size += 8;
532 } else {
533 1210 s->do_padding = 0;
534 }
535
536 /* compute the header + bit alloc size */
537 9470 current_frame_size = 32;
538 9470 alloc = s->alloc_table;
539
2/2
✓ Branch 0 taken 282219 times.
✓ Branch 1 taken 9470 times.
291689 for(i=0;i<s->sblimit;i++) {
540 282219 incr = alloc[0];
541 282219 current_frame_size += incr * s->nb_channels;
542 282219 alloc += 1 << incr;
543 }
544 for(;;) {
545 /* look for the subband with the largest signal to mask ratio */
546 2065634 max_sb = -1;
547 2065634 max_ch = -1;
548 2065634 max_smr = INT_MIN;
549
2/2
✓ Branch 0 taken 2161113 times.
✓ Branch 1 taken 2065634 times.
4226747 for(ch=0;ch<s->nb_channels;ch++) {
550
2/2
✓ Branch 0 taken 64594356 times.
✓ Branch 1 taken 2161113 times.
66755469 for(i=0;i<s->sblimit;i++) {
551
4/4
✓ Branch 0 taken 9277198 times.
✓ Branch 1 taken 55317158 times.
✓ Branch 2 taken 5463768 times.
✓ Branch 3 taken 3813430 times.
64594356 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
552 5463768 max_smr = smr[ch][i];
553 5463768 max_sb = i;
554 5463768 max_ch = ch;
555 }
556 }
557 }
558
2/2
✓ Branch 0 taken 9470 times.
✓ Branch 1 taken 2056164 times.
2065634 if (max_sb < 0)
559 9470 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 2056164 alloc = s->alloc_table;
567
2/2
✓ Branch 0 taken 18459155 times.
✓ Branch 1 taken 2056164 times.
20515319 for(i=0;i<max_sb;i++) {
568 18459155 alloc += 1 << alloc[0];
569 }
570
571
2/2
✓ Branch 0 taken 295518 times.
✓ Branch 1 taken 1760646 times.
2056164 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
572 /* nothing was coded for this band: add the necessary bits */
573 295518 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
574 295518 incr += s->total_quant_bits[alloc[1]];
575 } else {
576 /* increments bit allocation */
577 1760646 b = bit_alloc[max_ch][max_sb];
578 1760646 incr = s->total_quant_bits[alloc[b + 1]] -
579 1760646 s->total_quant_bits[alloc[b]];
580 }
581
582
2/2
✓ Branch 0 taken 1833116 times.
✓ Branch 1 taken 223048 times.
2056164 if (current_frame_size + incr <= max_frame_size) {
583 /* can increase size */
584 1833116 b = ++bit_alloc[max_ch][max_sb];
585 1833116 current_frame_size += incr;
586 /* decrease smr by the resolution we added */
587 1833116 smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
588 /* max allocation size reached ? */
589
2/2
✓ Branch 0 taken 72470 times.
✓ Branch 1 taken 1760646 times.
1833116 if (b == ((1 << alloc[0]) - 1))
590 72470 subband_status[max_ch][max_sb] = SB_NOMORE;
591 else
592 1760646 subband_status[max_ch][max_sb] = SB_ALLOCATED;
593 } else {
594 /* cannot increase the size of this subband */
595 223048 subband_status[max_ch][max_sb] = SB_NOMORE;
596 }
597 }
598 9470 *padding = max_frame_size - current_frame_size;
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9470 times.
9470 av_assert0(*padding >= 0);
600 9470 return max_frame_size / 8U;
601 }
602
603 /// Quantization & write sub band samples
604 9470 static av_always_inline void encode_subbands(MpegAudioContext *const s,
605 PutBitContext *const p,
606 const uint8_t bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
607 int is_fixed)
608 {
609
2/2
✓ Branch 0 taken 28410 times.
✓ Branch 1 taken 9470 times.
37880 for (int k = 0; k < 3; ++k) {
610
2/2
✓ Branch 0 taken 113640 times.
✓ Branch 1 taken 28410 times.
142050 for (int l = 0; l < 12; l += 3) {
611
2/2
✓ Branch 0 taken 3386628 times.
✓ Branch 1 taken 113640 times.
3500268 for (int i = 0, j = 0; i < s->sblimit; ++i) {
612 3386628 const int bit_alloc_bits = s->alloc_table[j];
613
2/2
✓ Branch 0 taken 3546216 times.
✓ Branch 1 taken 3386628 times.
6932844 for (int ch = 0; ch < s->nb_channels; ++ch) {
614 3546216 const int b = bit_alloc[ch][i];
615
2/2
✓ Branch 0 taken 2555448 times.
✓ Branch 1 taken 990768 times.
3546216 if (b) {
616 /* we encode 3 sub band samples of the same sub band at a time */
617 2555448 const int qindex = s->alloc_table[j + b];
618 2555448 const int steps = ff_mpa_quant_steps[qindex];
619 int q[3];
620
621
2/2
✓ Branch 0 taken 7666344 times.
✓ Branch 1 taken 2555448 times.
10221792 for (int m = 0; m < 3; ++m) {
622 7666344 const int sample = s->sb_samples[ch][k][l + m][i];
623 /* divide by scale factor */
624
2/2
✓ Branch 0 taken 606960 times.
✓ Branch 1 taken 7059384 times.
7666344 if (!is_fixed) {
625 606960 float a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]];
626 606960 q[m] = (int)((a + 1.0) * steps * 0.5);
627 } else {
628 7059384 const int e = s->scale_factors[ch][i][k];
629 7059384 const int shift = s->scale_factor_shift[e];
630 7059384 const int mult = s->scale_factor_mult[e];
631 int q1;
632
633 /* normalize to P bits */
634
2/2
✓ Branch 0 taken 6445308 times.
✓ Branch 1 taken 614076 times.
7059384 if (shift < 0)
635 6445308 q1 = sample * (1 << -shift);
636 else
637 614076 q1 = sample >> shift;
638 7059384 q1 = (q1 * mult) >> P;
639 7059384 q1 += 1 << P;
640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7059384 times.
7059384 if (q1 < 0)
641 q1 = 0;
642 7059384 q[m] = (q1 * (unsigned)steps) >> (P + 1);
643 }
644
2/2
✓ Branch 0 taken 13295 times.
✓ Branch 1 taken 7653049 times.
7666344 if (q[m] >= steps)
645 13295 q[m] = steps - 1;
646 av_assert2(q[m] >= 0 && q[m] < steps);
647 }
648 2555448 const int bits = ff_mpa_quant_bits[qindex];
649
2/2
✓ Branch 0 taken 357012 times.
✓ Branch 1 taken 2198436 times.
2555448 if (bits < 0) {
650 /* group the 3 values to save bits */
651 357012 put_bits(p, -bits,
652 357012 q[0] + steps * (q[1] + steps * q[2]));
653 } else {
654 2198436 put_bits(p, bits, q[0]);
655 2198436 put_bits(p, bits, q[1]);
656 2198436 put_bits(p, bits, q[2]);
657 }
658 }
659 }
660 /* next subband in alloc table */
661 3386628 j += 1 << bit_alloc_bits;
662 }
663 }
664 }
665 9470 }
666
667 /*
668 * Output the MPEG audio layer 2 frame. Note how the code is small
669 * compared to other encoders :-)
670 */
671 9470 static 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 9470 PutBitContext p0, *p = &p0;
678
679 9470 init_put_bits(p, buf, buf_size);
680
681 /* header */
682
683 9470 put_bits(p, 12, 0xfff);
684 9470 put_bits(p, 1, 1 - s->lsf); /* 1 = MPEG-1 ID, 0 = MPEG-2 lsf ID */
685 9470 put_bits(p, 2, 4-2); /* layer 2 */
686 9470 put_bits(p, 1, 1); /* no error protection */
687 9470 put_bits(p, 4, s->bitrate_index);
688 9470 put_bits(p, 2, s->freq_index);
689 9470 put_bits(p, 1, s->do_padding); /* use padding */
690 9470 put_bits(p, 1, 0); /* private_bit */
691
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 9003 times.
9470 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
692 9470 put_bits(p, 2, 0); /* mode_ext */
693 9470 put_bits(p, 1, 0); /* no copyright */
694 9470 put_bits(p, 1, 1); /* original */
695 9470 put_bits(p, 2, 0); /* no emphasis */
696
697 /* bit allocation */
698 9470 j = 0;
699
2/2
✓ Branch 0 taken 282219 times.
✓ Branch 1 taken 9470 times.
291689 for(i=0;i<s->sblimit;i++) {
700 282219 bit_alloc_bits = s->alloc_table[j];
701
2/2
✓ Branch 0 taken 295518 times.
✓ Branch 1 taken 282219 times.
577737 for(ch=0;ch<s->nb_channels;ch++) {
702 295518 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
703 }
704 282219 j += 1 << bit_alloc_bits;
705 }
706
707 /* scale codes */
708
2/2
✓ Branch 0 taken 282219 times.
✓ Branch 1 taken 9470 times.
291689 for(i=0;i<s->sblimit;i++) {
709
2/2
✓ Branch 0 taken 295518 times.
✓ Branch 1 taken 282219 times.
577737 for(ch=0;ch<s->nb_channels;ch++) {
710
2/2
✓ Branch 0 taken 212954 times.
✓ Branch 1 taken 82564 times.
295518 if (bit_alloc[ch][i])
711 212954 put_bits(p, 2, s->scale_code[ch][i]);
712 }
713 }
714
715 /* scale factors */
716
2/2
✓ Branch 0 taken 282219 times.
✓ Branch 1 taken 9470 times.
291689 for(i=0;i<s->sblimit;i++) {
717
2/2
✓ Branch 0 taken 295518 times.
✓ Branch 1 taken 282219 times.
577737 for(ch=0;ch<s->nb_channels;ch++) {
718
2/2
✓ Branch 0 taken 212954 times.
✓ Branch 1 taken 82564 times.
295518 if (bit_alloc[ch][i]) {
719 212954 sf = &s->scale_factors[ch][i][0];
720
3/4
✓ Branch 0 taken 3032 times.
✓ Branch 1 taken 9191 times.
✓ Branch 2 taken 200731 times.
✗ Branch 3 not taken.
212954 switch(s->scale_code[ch][i]) {
721 3032 case 0:
722 3032 put_bits(p, 18, sf[0] << 12 | sf[1] << 6 | sf[2]);
723 3032 break;
724 9191 case 3:
725 case 1:
726 9191 put_bits(p, 12, sf[0] << 6 | sf[2]);
727 9191 break;
728 200731 case 2:
729 200731 put_bits(p, 6, sf[0]);
730 200731 break;
731 }
732 }
733 }
734 }
735
736 #if CONFIG_SMALL
737 encode_subbands(s, p, bit_alloc, IS_FIXED(s));
738 #else
739
2/2
✓ Branch 0 taken 8741 times.
✓ Branch 1 taken 729 times.
9470 if (IS_FIXED(s))
740 8741 encode_subbands(s, p, bit_alloc, 1);
741 else
742 729 encode_subbands(s, p, bit_alloc, 0);
743 #endif
744
745 av_assert1(put_bits_left(p) == padding);
746
747 /* flush */
748 9470 flush_put_bits(p);
749
750 /* padding */
751
2/2
✓ Branch 1 taken 6041 times.
✓ Branch 2 taken 3429 times.
9470 if (put_bytes_left(p, 0))
752 6041 memset(put_bits_ptr(p), 0, put_bytes_left(p, 0));
753 9470 }
754
755 9470 static int mpa_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
756 const AVFrame *frame, int *got_packet_ptr)
757 {
758 9470 MpegAudioContext *s = avctx->priv_data;
759 9470 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
2/2
✓ Branch 0 taken 9937 times.
✓ Branch 1 taken 9470 times.
19407 for(i=0;i<s->nb_channels;i++) {
765 9937 filter(s, i, samples + i, s->nb_channels);
766 }
767
768
2/2
✓ Branch 0 taken 9937 times.
✓ Branch 1 taken 9470 times.
19407 for(i=0;i<s->nb_channels;i++) {
769 9937 compute_scale_factors(s, s->scale_code[i], s->scale_factors[i],
770 9937 s->sb_samples[i], s->sblimit);
771 }
772
2/2
✓ Branch 0 taken 9937 times.
✓ Branch 1 taken 9470 times.
19407 for(i=0;i<s->nb_channels;i++) {
773 9937 psycho_acoustic_model(s, smr[i]);
774 }
775 9470 unsigned frame_size = compute_bit_allocation(s, smr, bit_alloc, &padding);
776
777 9470 ret = ff_get_encode_buffer(avctx, avpkt, frame_size, 0);
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9470 times.
9470 if (ret < 0)
779 return ret;
780
781 9470 encode_frame(s, avpkt->data, frame_size, bit_alloc, padding);
782
783
1/2
✓ Branch 0 taken 9470 times.
✗ Branch 1 not taken.
9470 if (frame->pts != AV_NOPTS_VALUE)
784 9470 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
785
786 9470 *got_packet_ptr = 1;
787 9470 return 0;
788 }
789
790 static const FFCodecDefault mp2_defaults[] = {
791 { "b", "0" },
792 { NULL },
793 };
794
795 #if CONFIG_MP2_ENCODER
796 const 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,
801 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
802 .priv_data_size = sizeof(MpegAudioContext),
803 .init = mpa_encode_init,
804 FF_CODEC_ENCODE_CB(mpa_encode_frame),
805 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
806 CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
807 CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO),
808 .defaults = mp2_defaults,
809 };
810 #endif
811
812 #if CONFIG_MP2FIXED_ENCODER
813 15 static av_cold int mpa_fixed_encode_init(AVCodecContext *avctx)
814 {
815 15 MpegAudioContext *s = avctx->priv_data;
816
817 15 s->is_fixed = 1;
818 15 return mpa_encode_init(avctx);
819 }
820
821 const FFCodec ff_mp2fixed_encoder = {
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,
826 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
827 .priv_data_size = sizeof(MpegAudioContext),
828 .init = mpa_fixed_encode_init,
829 FF_CODEC_ENCODE_CB(mpa_encode_frame),
830 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
831 CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
832 CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO),
833 .defaults = mp2_defaults,
834 };
835 #endif
836