FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegaudioenc_template.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 395 410 96.3%
Branches: 166 183 90.7%

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 "libavutil/channel_layout.h"
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 {
49 PutBitContext pb;
50 int nb_channels;
51 int lsf; /* 1 if mpeg2 low bitrate selected */
52 int bitrate_index; /* bit rate */
53 int freq_index;
54 int frame_size; /* frame size, in bits, without padding */
55 /* padding computation */
56 int frame_frac, frame_frac_incr, do_padding;
57 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
58 int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
59 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
60 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
61 /* code to group 3 scale factors */
62 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
63 int sblimit; /* number of used subbands */
64 const unsigned char *alloc_table;
65 int16_t filter_bank[512];
66 int scale_factor_table[64];
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 */
75 } MpegAudioContext;
76
77 23 static av_cold int MPA_encode_init(AVCodecContext *avctx)
78 {
79 23 MpegAudioContext *s = avctx->priv_data;
80 23 int freq = avctx->sample_rate;
81 23 int bitrate = avctx->bit_rate;
82 23 int channels = avctx->channels;
83 int i, v, table;
84 float a;
85
86
2/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
23 if (channels <= 0 || channels > 2){
87 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
88 return AVERROR(EINVAL);
89 }
90 23 bitrate = bitrate / 1000;
91 23 s->nb_channels = channels;
92 23 avctx->frame_size = MPA_FRAME_SIZE;
93 23 avctx->initial_padding = 512 - 32 + 1;
94
95 /* encoding freq */
96 23 s->lsf = 0;
97
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 for(i=0;i<3;i++) {
98
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (avpriv_mpa_freq_tab[i] == freq)
99 23 break;
100 if ((avpriv_mpa_freq_tab[i] / 2) == freq) {
101 s->lsf = 1;
102 break;
103 }
104 }
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 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 23 s->freq_index = i;
110
111 /* encoding bitrate & frequency */
112
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 11 times.
227 for(i=1;i<15;i++) {
113
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 204 times.
216 if (avpriv_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
114 12 break;
115 }
116
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
23 if (i == 15 && !avctx->bit_rate) {
117 11 i = 14;
118 11 bitrate = avpriv_mpa_bitrate_tab[s->lsf][1][i];
119 11 avctx->bit_rate = bitrate * 1000;
120 }
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 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 23 s->bitrate_index = i;
126
127 /* compute total header size & pad bit */
128
129 23 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
130 23 s->frame_size = ((int)a) * 8;
131
132 /* frame fractional size to compute padding */
133 23 s->frame_frac = 0;
134 23 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
135
136 /* select the right allocation table */
137 23 table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
138
139 /* number of used subbands */
140 23 s->sblimit = ff_mpa_sblimit_table[table];
141 23 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 25 times.
✓ Branch 1 taken 23 times.
48 for(i=0;i<s->nb_channels;i++)
147 25 s->samples_offset[i] = 0;
148
149
2/2
✓ Branch 0 taken 5911 times.
✓ Branch 1 taken 23 times.
5934 for(i=0;i<257;i++) {
150 int v;
151 5911 v = ff_mpa_enwindow[i];
152 #if WFRAC_BITS != 16
153 5911 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
154 #endif
155 5911 s->filter_bank[i] = v;
156
2/2
✓ Branch 0 taken 5796 times.
✓ Branch 1 taken 115 times.
5911 if ((i & 63) != 0)
157 5796 v = -v;
158
2/2
✓ Branch 0 taken 5888 times.
✓ Branch 1 taken 23 times.
5911 if (i != 0)
159 5888 s->filter_bank[512 - i] = v;
160 }
161
162
2/2
✓ Branch 0 taken 1472 times.
✓ Branch 1 taken 23 times.
1495 for(i=0;i<64;i++) {
163 1472 v = (int)(exp2((3 - i) / 3.0) * (1 << 20));
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1472 times.
1472 if (v <= 0)
165 v = 1;
166 1472 s->scale_factor_table[i] = v;
167 #if USE_FLOATS
168 704 s->scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20);
169 #else
170 #define P 15
171 768 s->scale_factor_shift[i] = 21 - P - (i / 3);
172 768 s->scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0);
173 #endif
174 }
175
2/2
✓ Branch 0 taken 2944 times.
✓ Branch 1 taken 23 times.
2967 for(i=0;i<128;i++) {
176 2944 v = i - 64;
177
2/2
✓ Branch 0 taken 1426 times.
✓ Branch 1 taken 1518 times.
2944 if (v <= -3)
178 1426 v = 0;
179
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1472 times.
1518 else if (v < 0)
180 46 v = 1;
181
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1449 times.
1472 else if (v == 0)
182 23 v = 2;
183
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1403 times.
1449 else if (v < 3)
184 46 v = 3;
185 else
186 1403 v = 4;
187 2944 s->scale_diff_table[i] = v;
188 }
189
190
2/2
✓ Branch 0 taken 391 times.
✓ Branch 1 taken 23 times.
414 for(i=0;i<17;i++) {
191 391 v = ff_mpa_quant_bits[i];
192
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 322 times.
391 if (v < 0)
193 69 v = -v;
194 else
195 322 v = v * 3;
196 391 s->total_quant_bits[i] = 12 * v;
197 }
198
199 23 return 0;
200 }
201
202 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
203 317448 static void idct32(int *out, int *tab)
204 {
205 int i, j;
206 int *t, *t1, xr;
207 317448 const int *xp = costab32;
208
209
2/2
✓ Branch 0 taken 4761720 times.
✓ Branch 1 taken 317448 times.
5079168 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
210
211 317448 t = tab + 30;
212 317448 t1 = tab + 2;
213 do {
214 2222136 t[0] += t[-4];
215 2222136 t[1] += t[1 - 4];
216 2222136 t -= 4;
217
2/2
✓ Branch 0 taken 1904688 times.
✓ Branch 1 taken 317448 times.
2222136 } while (t != t1);
218
219 317448 t = tab + 28;
220 317448 t1 = tab + 4;
221 do {
222 952344 t[0] += t[-8];
223 952344 t[1] += t[1-8];
224 952344 t[2] += t[2-8];
225 952344 t[3] += t[3-8];
226 952344 t -= 8;
227
2/2
✓ Branch 0 taken 634896 times.
✓ Branch 1 taken 317448 times.
952344 } while (t != t1);
228
229 317448 t = tab;
230 317448 t1 = tab + 32;
231 do {
232 634896 t[ 3] = -t[ 3];
233 634896 t[ 6] = -t[ 6];
234
235 634896 t[11] = -t[11];
236 634896 t[12] = -t[12];
237 634896 t[13] = -t[13];
238 634896 t[15] = -t[15];
239 634896 t += 16;
240
2/2
✓ Branch 0 taken 317448 times.
✓ Branch 1 taken 317448 times.
634896 } while (t != t1);
241
242
243 317448 t = tab;
244 317448 t1 = tab + 8;
245 do {
246 int x1, x2, x3, x4;
247
248 2539584 x3 = MUL(t[16], FIX(M_SQRT2*0.5));
249 2539584 x4 = t[0] - x3;
250 2539584 x3 = t[0] + x3;
251
252 2539584 x2 = MUL(-(t[24] + t[8]), FIX(M_SQRT2*0.5));
253 2539584 x1 = MUL((t[8] - x2), xp[0]);
254 2539584 x2 = MUL((t[8] + x2), xp[1]);
255
256 2539584 t[ 0] = x3 + x1;
257 2539584 t[ 8] = x4 - x2;
258 2539584 t[16] = x4 + x2;
259 2539584 t[24] = x3 - x1;
260 2539584 t++;
261
2/2
✓ Branch 0 taken 2222136 times.
✓ Branch 1 taken 317448 times.
2539584 } while (t != t1);
262
263 317448 xp += 2;
264 317448 t = tab;
265 317448 t1 = tab + 4;
266 do {
267 1269792 xr = MUL(t[28],xp[0]);
268 1269792 t[28] = (t[0] - xr);
269 1269792 t[0] = (t[0] + xr);
270
271 1269792 xr = MUL(t[4],xp[1]);
272 1269792 t[ 4] = (t[24] - xr);
273 1269792 t[24] = (t[24] + xr);
274
275 1269792 xr = MUL(t[20],xp[2]);
276 1269792 t[20] = (t[8] - xr);
277 1269792 t[ 8] = (t[8] + xr);
278
279 1269792 xr = MUL(t[12],xp[3]);
280 1269792 t[12] = (t[16] - xr);
281 1269792 t[16] = (t[16] + xr);
282 1269792 t++;
283
2/2
✓ Branch 0 taken 952344 times.
✓ Branch 1 taken 317448 times.
1269792 } while (t != t1);
284 317448 xp += 4;
285
286
2/2
✓ Branch 0 taken 1269792 times.
✓ Branch 1 taken 317448 times.
1587240 for (i = 0; i < 4; i++) {
287 1269792 xr = MUL(tab[30-i*4],xp[0]);
288 1269792 tab[30-i*4] = (tab[i*4] - xr);
289 1269792 tab[ i*4] = (tab[i*4] + xr);
290
291 1269792 xr = MUL(tab[ 2+i*4],xp[1]);
292 1269792 tab[ 2+i*4] = (tab[28-i*4] - xr);
293 1269792 tab[28-i*4] = (tab[28-i*4] + xr);
294
295 1269792 xr = MUL(tab[31-i*4],xp[0]);
296 1269792 tab[31-i*4] = (tab[1+i*4] - xr);
297 1269792 tab[ 1+i*4] = (tab[1+i*4] + xr);
298
299 1269792 xr = MUL(tab[ 3+i*4],xp[1]);
300 1269792 tab[ 3+i*4] = (tab[29-i*4] - xr);
301 1269792 tab[29-i*4] = (tab[29-i*4] + xr);
302
303 1269792 xp += 2;
304 }
305
306 317448 t = tab + 30;
307 317448 t1 = tab + 1;
308 do {
309 5079168 xr = MUL(t1[0], *xp);
310 5079168 t1[0] = (t[0] - xr);
311 5079168 t[0] = (t[0] + xr);
312 5079168 t -= 2;
313 5079168 t1 += 2;
314 5079168 xp++;
315
2/2
✓ Branch 0 taken 4761720 times.
✓ Branch 1 taken 317448 times.
5079168 } while (t >= tab);
316
317
2/2
✓ Branch 0 taken 10158336 times.
✓ Branch 1 taken 317448 times.
10475784 for(i=0;i<32;i++) {
318 10158336 out[i] = tab[bitinv32[i]];
319 }
320 317448 }
321
322 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
323
324 8818 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 8818 offset = s->samples_offset[ch];
333 8818 out = &s->sb_samples[ch][0][0][0];
334
2/2
✓ Branch 0 taken 317448 times.
✓ Branch 1 taken 8818 times.
326266 for(j=0;j<36;j++) {
335 /* 32 samples at once */
336
2/2
✓ Branch 0 taken 10158336 times.
✓ Branch 1 taken 317448 times.
10475784 for(i=0;i<32;i++) {
337 10158336 s->samples_buf[ch][offset + (31 - i)] = samples[0];
338 10158336 samples += incr;
339 }
340
341 /* filter */
342 317448 p = s->samples_buf[ch] + offset;
343 317448 q = s->filter_bank;
344 /* maxsum = 23169 */
345
2/2
✓ Branch 0 taken 20316672 times.
✓ Branch 1 taken 317448 times.
20634120 for(i=0;i<64;i++) {
346 20316672 sum = p[0*64] * q[0*64];
347 20316672 sum += p[1*64] * q[1*64];
348 20316672 sum += p[2*64] * q[2*64];
349 20316672 sum += p[3*64] * q[3*64];
350 20316672 sum += p[4*64] * q[4*64];
351 20316672 sum += p[5*64] * q[5*64];
352 20316672 sum += p[6*64] * q[6*64];
353 20316672 sum += p[7*64] * q[7*64];
354 20316672 tmp[i] = sum;
355 20316672 p++;
356 20316672 q++;
357 }
358 317448 tmp1[0] = tmp[16] >> WSHIFT;
359
2/2
✓ Branch 0 taken 5079168 times.
✓ Branch 1 taken 317448 times.
5396616 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
360
2/2
✓ Branch 0 taken 4761720 times.
✓ Branch 1 taken 317448 times.
5079168 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
361
362 317448 idct32(out, tmp1);
363
364 /* advance of 32 samples */
365 317448 offset -= 32;
366 317448 out += 32;
367 /* handle the wrap around */
368
2/2
✓ Branch 0 taken 2828 times.
✓ Branch 1 taken 314620 times.
317448 if (offset < 0) {
369 2828 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
370 2828 s->samples_buf[ch], (512 - 32) * 2);
371 2828 offset = SAMPLES_BUF_SIZE - 512;
372 }
373 }
374 8818 s->samples_offset[ch] = offset;
375 8818 }
376
377 8818 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 8818 unsigned char *sf = &scale_factors[0][0];
386
387
2/2
✓ Branch 0 taken 261990 times.
✓ Branch 1 taken 8818 times.
270808 for(j=0;j<sblimit;j++) {
388
2/2
✓ Branch 0 taken 785970 times.
✓ Branch 1 taken 261990 times.
1047960 for(i=0;i<3;i++) {
389 /* find the max absolute value */
390 785970 p = &sb_samples[i][0][j];
391 785970 vmax = abs(*p);
392
2/2
✓ Branch 0 taken 8645670 times.
✓ Branch 1 taken 785970 times.
9431640 for(k=1;k<12;k++) {
393 8645670 p += SBLIMIT;
394 8645670 v = abs(*p);
395
2/2
✓ Branch 0 taken 1442693 times.
✓ Branch 1 taken 7202977 times.
8645670 if (v > vmax)
396 1442693 vmax = v;
397 }
398 /* compute the scale factor index using log 2 computations */
399
2/2
✓ Branch 0 taken 785900 times.
✓ Branch 1 taken 70 times.
785970 if (vmax > 1) {
400 785900 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 785900 index = (21 - n) * 3 - 3;
404
1/2
✓ Branch 0 taken 785900 times.
✗ Branch 1 not taken.
785900 if (index >= 0) {
405
2/2
✓ Branch 0 taken 974979 times.
✓ Branch 1 taken 785900 times.
1760879 while (vmax <= s->scale_factor_table[index+1])
406 974979 index++;
407 } else {
408 index = 0; /* very unlikely case of overflow */
409 }
410 } else {
411 70 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 785970 sf[i] = index;
419 }
420
421 /* compute the transmission factor : look if the scale factors
422 are close enough to each other */
423 261990 d1 = s->scale_diff_table[sf[0] - sf[1] + 64];
424 261990 d2 = s->scale_diff_table[sf[1] - sf[2] + 64];
425
426 /* handle the 25 cases */
427
8/9
✓ Branch 0 taken 4123 times.
✓ Branch 1 taken 4657 times.
✓ Branch 2 taken 3579 times.
✓ Branch 3 taken 4294 times.
✓ Branch 4 taken 122581 times.
✓ Branch 5 taken 34807 times.
✓ Branch 6 taken 57486 times.
✓ Branch 7 taken 30463 times.
✗ Branch 8 not taken.
261990 switch(d1 * 5 + d2) {
428 4123 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 4123 code = 0;
434 4123 break;
435 4657 case 0*5+1:
436 case 0*5+2:
437 case 4*5+1:
438 case 4*5+2:
439 4657 code = 3;
440 4657 sf[2] = sf[1];
441 4657 break;
442 3579 case 0*5+3:
443 case 4*5+3:
444 3579 code = 3;
445 3579 sf[1] = sf[2];
446 3579 break;
447 4294 case 1*5+0:
448 case 1*5+4:
449 case 2*5+4:
450 4294 code = 1;
451 4294 sf[1] = sf[0];
452 4294 break;
453 122581 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 122581 code = 2;
459 122581 sf[1] = sf[2] = sf[0];
460 122581 break;
461 34807 case 2*5+3:
462 case 3*5+3:
463 34807 code = 2;
464 34807 sf[0] = sf[1] = sf[2];
465 34807 break;
466 57486 case 3*5+0:
467 case 3*5+1:
468 case 3*5+2:
469 57486 code = 2;
470 57486 sf[0] = sf[2] = sf[1];
471 57486 break;
472 30463 case 1*5+3:
473 30463 code = 2;
474
2/2
✓ Branch 0 taken 4873 times.
✓ Branch 1 taken 25590 times.
30463 if (sf[0] > sf[2])
475 4873 sf[0] = sf[2];
476 30463 sf[1] = sf[2] = sf[0];
477 30463 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 261990 scale_code[j] = code;
486 261990 sf += 3;
487 }
488 8818 }
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 8818 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
494 {
495 int i;
496
497
2/2
✓ Branch 0 taken 261990 times.
✓ Branch 1 taken 8818 times.
270808 for(i=0;i<s->sblimit;i++) {
498 261990 smr[i] = (int)(fixed_smr[i] * 10);
499 }
500 8818 }
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 8358 static void 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 8358 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
522 8358 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
523 8358 memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
524
525 /* compute frame size and padding */
526 8358 max_frame_size = s->frame_size;
527 8358 s->frame_frac += s->frame_frac_incr;
528
2/2
✓ Branch 0 taken 7387 times.
✓ Branch 1 taken 971 times.
8358 if (s->frame_frac >= 65536) {
529 7387 s->frame_frac -= 65536;
530 7387 s->do_padding = 1;
531 7387 max_frame_size += 8;
532 } else {
533 971 s->do_padding = 0;
534 }
535
536 /* compute the header + bit alloc size */
537 8358 current_frame_size = 32;
538 8358 alloc = s->alloc_table;
539
2/2
✓ Branch 0 taken 248880 times.
✓ Branch 1 taken 8358 times.
257238 for(i=0;i<s->sblimit;i++) {
540 248880 incr = alloc[0];
541 248880 current_frame_size += incr * s->nb_channels;
542 248880 alloc += 1 << incr;
543 }
544 for(;;) {
545 /* look for the subband with the largest signal to mask ratio */
546 1819479 max_sb = -1;
547 1819479 max_ch = -1;
548 1819479 max_smr = INT_MIN;
549
2/2
✓ Branch 0 taken 1912980 times.
✓ Branch 1 taken 1819479 times.
3732459 for(ch=0;ch<s->nb_channels;ch++) {
550
2/2
✓ Branch 0 taken 57162234 times.
✓ Branch 1 taken 1912980 times.
59075214 for(i=0;i<s->sblimit;i++) {
551
4/4
✓ Branch 0 taken 8154552 times.
✓ Branch 1 taken 49007682 times.
✓ Branch 2 taken 4820286 times.
✓ Branch 3 taken 3334266 times.
57162234 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
552 4820286 max_smr = smr[ch][i];
553 4820286 max_sb = i;
554 4820286 max_ch = ch;
555 }
556 }
557 }
558
2/2
✓ Branch 0 taken 8358 times.
✓ Branch 1 taken 1811121 times.
1819479 if (max_sb < 0)
559 8358 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 1811121 alloc = s->alloc_table;
567
2/2
✓ Branch 0 taken 16255933 times.
✓ Branch 1 taken 1811121 times.
18067054 for(i=0;i<max_sb;i++) {
568 16255933 alloc += 1 << alloc[0];
569 }
570
571
2/2
✓ Branch 0 taken 261990 times.
✓ Branch 1 taken 1549131 times.
1811121 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
572 /* nothing was coded for this band: add the necessary bits */
573 261990 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
574 261990 incr += s->total_quant_bits[alloc[1]];
575 } else {
576 /* increments bit allocation */
577 1549131 b = bit_alloc[max_ch][max_sb];
578 1549131 incr = s->total_quant_bits[alloc[b + 1]] -
579 1549131 s->total_quant_bits[alloc[b]];
580 }
581
582
2/2
✓ Branch 0 taken 1610342 times.
✓ Branch 1 taken 200779 times.
1811121 if (current_frame_size + incr <= max_frame_size) {
583 /* can increase size */
584 1610342 b = ++bit_alloc[max_ch][max_sb];
585 1610342 current_frame_size += incr;
586 /* decrease smr by the resolution we added */
587 1610342 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 61211 times.
✓ Branch 1 taken 1549131 times.
1610342 if (b == ((1 << alloc[0]) - 1))
590 61211 subband_status[max_ch][max_sb] = SB_NOMORE;
591 else
592 1549131 subband_status[max_ch][max_sb] = SB_ALLOCATED;
593 } else {
594 /* cannot increase the size of this subband */
595 200779 subband_status[max_ch][max_sb] = SB_NOMORE;
596 }
597 }
598 8358 *padding = max_frame_size - current_frame_size;
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8358 times.
8358 av_assert0(*padding >= 0);
600 8358 }
601
602 /*
603 * Output the MPEG audio layer 2 frame. Note how the code is small
604 * compared to other encoders :-)
605 */
606 8358 static void encode_frame(MpegAudioContext *s,
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 8358 PutBitContext *p = &s->pb;
614
615 /* header */
616
617 8358 put_bits(p, 12, 0xfff);
618 8358 put_bits(p, 1, 1 - s->lsf); /* 1 = MPEG-1 ID, 0 = MPEG-2 lsf ID */
619 8358 put_bits(p, 2, 4-2); /* layer 2 */
620 8358 put_bits(p, 1, 1); /* no error protection */
621 8358 put_bits(p, 4, s->bitrate_index);
622 8358 put_bits(p, 2, s->freq_index);
623 8358 put_bits(p, 1, s->do_padding); /* use padding */
624 8358 put_bits(p, 1, 0); /* private_bit */
625
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 7898 times.
8358 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
626 8358 put_bits(p, 2, 0); /* mode_ext */
627 8358 put_bits(p, 1, 0); /* no copyright */
628 8358 put_bits(p, 1, 1); /* original */
629 8358 put_bits(p, 2, 0); /* no emphasis */
630
631 /* bit allocation */
632 8358 j = 0;
633
2/2
✓ Branch 0 taken 248880 times.
✓ Branch 1 taken 8358 times.
257238 for(i=0;i<s->sblimit;i++) {
634 248880 bit_alloc_bits = s->alloc_table[j];
635
2/2
✓ Branch 0 taken 261990 times.
✓ Branch 1 taken 248880 times.
510870 for(ch=0;ch<s->nb_channels;ch++) {
636 261990 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
637 }
638 248880 j += 1 << bit_alloc_bits;
639 }
640
641 /* scale codes */
642
2/2
✓ Branch 0 taken 248880 times.
✓ Branch 1 taken 8358 times.
257238 for(i=0;i<s->sblimit;i++) {
643
2/2
✓ Branch 0 taken 261990 times.
✓ Branch 1 taken 248880 times.
510870 for(ch=0;ch<s->nb_channels;ch++) {
644
2/2
✓ Branch 0 taken 187506 times.
✓ Branch 1 taken 74484 times.
261990 if (bit_alloc[ch][i])
645 187506 put_bits(p, 2, s->scale_code[ch][i]);
646 }
647 }
648
649 /* scale factors */
650
2/2
✓ Branch 0 taken 248880 times.
✓ Branch 1 taken 8358 times.
257238 for(i=0;i<s->sblimit;i++) {
651
2/2
✓ Branch 0 taken 261990 times.
✓ Branch 1 taken 248880 times.
510870 for(ch=0;ch<s->nb_channels;ch++) {
652
2/2
✓ Branch 0 taken 187506 times.
✓ Branch 1 taken 74484 times.
261990 if (bit_alloc[ch][i]) {
653 187506 sf = &s->scale_factors[ch][i][0];
654
3/4
✓ Branch 0 taken 2625 times.
✓ Branch 1 taken 7938 times.
✓ Branch 2 taken 176943 times.
✗ Branch 3 not taken.
187506 switch(s->scale_code[ch][i]) {
655 2625 case 0:
656 2625 put_bits(p, 6, sf[0]);
657 2625 put_bits(p, 6, sf[1]);
658 2625 put_bits(p, 6, sf[2]);
659 2625 break;
660 7938 case 3:
661 case 1:
662 7938 put_bits(p, 6, sf[0]);
663 7938 put_bits(p, 6, sf[2]);
664 7938 break;
665 176943 case 2:
666 176943 put_bits(p, 6, sf[0]);
667 176943 break;
668 }
669 }
670 }
671 }
672
673 /* quantization & write sub band samples */
674
675
2/2
✓ Branch 0 taken 25074 times.
✓ Branch 1 taken 8358 times.
33432 for(k=0;k<3;k++) {
676
2/2
✓ Branch 0 taken 100296 times.
✓ Branch 1 taken 25074 times.
125370 for(l=0;l<12;l+=3) {
677 100296 j = 0;
678
2/2
✓ Branch 0 taken 2986560 times.
✓ Branch 1 taken 100296 times.
3086856 for(i=0;i<s->sblimit;i++) {
679 2986560 bit_alloc_bits = s->alloc_table[j];
680
2/2
✓ Branch 0 taken 3143880 times.
✓ Branch 1 taken 2986560 times.
6130440 for(ch=0;ch<s->nb_channels;ch++) {
681 3143880 b = bit_alloc[ch][i];
682
2/2
✓ Branch 0 taken 2250072 times.
✓ Branch 1 taken 893808 times.
3143880 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 2250072 qindex = s->alloc_table[j+b];
686 2250072 steps = ff_mpa_quant_steps[qindex];
687
2/2
✓ Branch 0 taken 6750216 times.
✓ Branch 1 taken 2250072 times.
9000288 for(m=0;m<3;m++) {
688 6750216 sample = s->sb_samples[ch][k][l + m][i];
689 /* divide by scale factor */
690 #if USE_FLOATS
691 {
692 float a;
693 489240 a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]];
694 489240 q[m] = (int)((a + 1.0) * steps * 0.5);
695 }
696 #else
697 {
698 int q1, e, shift, mult;
699 6260976 e = s->scale_factors[ch][i][k];
700 6260976 shift = s->scale_factor_shift[e];
701 6260976 mult = s->scale_factor_mult[e];
702
703 /* normalize to P bits */
704
2/2
✓ Branch 0 taken 5709696 times.
✓ Branch 1 taken 551280 times.
6260976 if (shift < 0)
705 5709696 q1 = sample * (1 << -shift);
706 else
707 551280 q1 = sample >> shift;
708 6260976 q1 = (q1 * mult) >> P;
709 6260976 q1 += 1 << P;
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6260976 times.
6260976 if (q1 < 0)
711 q1 = 0;
712 6260976 q[m] = (q1 * (unsigned)steps) >> (P + 1);
713 }
714 #endif
715
2/2
✓ Branch 0 taken 11490 times.
✓ Branch 1 taken 6738726 times.
6750216 if (q[m] >= steps)
716 11490 q[m] = steps - 1;
717 av_assert2(q[m] >= 0 && q[m] < steps);
718 }
719 2250072 bits = ff_mpa_quant_bits[qindex];
720
2/2
✓ Branch 0 taken 307956 times.
✓ Branch 1 taken 1942116 times.
2250072 if (bits < 0) {
721 /* group the 3 values to save bits */
722 307956 put_bits(p, -bits,
723 307956 q[0] + steps * (q[1] + steps * q[2]));
724 } else {
725 1942116 put_bits(p, bits, q[0]);
726 1942116 put_bits(p, bits, q[1]);
727 1942116 put_bits(p, bits, q[2]);
728 }
729 }
730 }
731 /* next subband in alloc table */
732 2986560 j += 1 << bit_alloc_bits;
733 }
734 }
735 }
736
737 /* padding */
738
2/2
✓ Branch 0 taken 65684 times.
✓ Branch 1 taken 8358 times.
74042 for(i=0;i<padding;i++)
739 65684 put_bits(p, 1, 0);
740 8358 }
741
742 8358 static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
743 const AVFrame *frame, int *got_packet_ptr)
744 {
745 8358 MpegAudioContext *s = avctx->priv_data;
746 8358 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
2/2
✓ Branch 0 taken 8818 times.
✓ Branch 1 taken 8358 times.
17176 for(i=0;i<s->nb_channels;i++) {
752 8818 filter(s, i, samples + i, s->nb_channels);
753 }
754
755
2/2
✓ Branch 0 taken 8818 times.
✓ Branch 1 taken 8358 times.
17176 for(i=0;i<s->nb_channels;i++) {
756 8818 compute_scale_factors(s, s->scale_code[i], s->scale_factors[i],
757 8818 s->sb_samples[i], s->sblimit);
758 }
759
2/2
✓ Branch 0 taken 8818 times.
✓ Branch 1 taken 8358 times.
17176 for(i=0;i<s->nb_channels;i++) {
760 8818 psycho_acoustic_model(s, smr[i]);
761 }
762 8358 compute_bit_allocation(s, smr, bit_alloc, &padding);
763
764
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8358 times.
8358 if ((ret = ff_alloc_packet(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
765 return ret;
766
767 8358 init_put_bits(&s->pb, avpkt->data, avpkt->size);
768
769 8358 encode_frame(s, bit_alloc, padding);
770
771 /* flush */
772 8358 flush_put_bits(&s->pb);
773 8358 avpkt->size = put_bytes_output(&s->pb);
774
775
1/2
✓ Branch 0 taken 8358 times.
✗ Branch 1 not taken.
8358 if (frame->pts != AV_NOPTS_VALUE)
776 8358 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
777
778 8358 *got_packet_ptr = 1;
779 8358 return 0;
780 }
781
782 static const AVCodecDefault mp2_defaults[] = {
783 { "b", "0" },
784 { NULL },
785 };
786
787