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 "put_bits.h" | ||
32 | |||
33 | #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */ | ||
34 | #define WFRAC_BITS 14 /* fractional bits for window */ | ||
35 | |||
36 | #include "mpegaudio.h" | ||
37 | #include "mpegaudiodsp.h" | ||
38 | #include "mpegaudiodata.h" | ||
39 | #include "mpegaudiotab.h" | ||
40 | |||
41 | /* currently, cannot change these constants (need to modify | ||
42 | quantization stage) */ | ||
43 | #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS) | ||
44 | |||
45 | #define SAMPLES_BUF_SIZE 4096 | ||
46 | |||
47 | typedef struct MpegAudioContext { | ||
48 | PutBitContext pb; | ||
49 | int nb_channels; | ||
50 | int lsf; /* 1 if mpeg2 low bitrate selected */ | ||
51 | int bitrate_index; /* bit rate */ | ||
52 | int freq_index; | ||
53 | int frame_size; /* frame size, in bits, without padding */ | ||
54 | /* padding computation */ | ||
55 | int frame_frac, frame_frac_incr, do_padding; | ||
56 | short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */ | ||
57 | int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */ | ||
58 | int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]; | ||
59 | unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */ | ||
60 | /* code to group 3 scale factors */ | ||
61 | unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; | ||
62 | int sblimit; /* number of used subbands */ | ||
63 | const unsigned char *alloc_table; | ||
64 | int16_t filter_bank[512]; | ||
65 | int scale_factor_table[64]; | ||
66 | unsigned char scale_diff_table[128]; | ||
67 | #if USE_FLOATS | ||
68 | float scale_factor_inv_table[64]; | ||
69 | #else | ||
70 | int8_t scale_factor_shift[64]; | ||
71 | unsigned short scale_factor_mult[64]; | ||
72 | #endif | ||
73 | unsigned short total_quant_bits[17]; /* total number of bits per allocation group */ | ||
74 | } MpegAudioContext; | ||
75 | |||
76 | 24 | static av_cold int MPA_encode_init(AVCodecContext *avctx) | |
77 | { | ||
78 | 24 | MpegAudioContext *s = avctx->priv_data; | |
79 | 24 | int freq = avctx->sample_rate; | |
80 | 24 | int bitrate = avctx->bit_rate; | |
81 | 24 | int channels = avctx->ch_layout.nb_channels; | |
82 | int i, v, table; | ||
83 | float a; | ||
84 | |||
85 | 24 | bitrate = bitrate / 1000; | |
86 | 24 | s->nb_channels = channels; | |
87 | 24 | avctx->frame_size = MPA_FRAME_SIZE; | |
88 | 24 | avctx->initial_padding = 512 - 32 + 1; | |
89 | |||
90 | /* encoding freq */ | ||
91 | 24 | s->lsf = 0; | |
92 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | for(i=0;i<3;i++) { |
93 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
|
25 | if (ff_mpa_freq_tab[i] == freq) |
94 | 24 | break; | |
95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if ((ff_mpa_freq_tab[i] / 2) == freq) { |
96 | ✗ | s->lsf = 1; | |
97 | ✗ | break; | |
98 | } | ||
99 | } | ||
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (i == 3){ |
101 | ✗ | av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq); | |
102 | ✗ | return AVERROR(EINVAL); | |
103 | } | ||
104 | 24 | s->freq_index = i; | |
105 | |||
106 | /* encoding bitrate & frequency */ | ||
107 |
2/2✓ Branch 0 taken 230 times.
✓ Branch 1 taken 12 times.
|
242 | for(i=1;i<15;i++) { |
108 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 218 times.
|
230 | if (ff_mpa_bitrate_tab[s->lsf][1][i] == bitrate) |
109 | 12 | break; | |
110 | } | ||
111 |
3/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
24 | if (i == 15 && !avctx->bit_rate) { |
112 | 12 | i = 14; | |
113 | 12 | bitrate = ff_mpa_bitrate_tab[s->lsf][1][i]; | |
114 | 12 | avctx->bit_rate = bitrate * 1000; | |
115 | } | ||
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (i == 15){ |
117 | ✗ | av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate); | |
118 | ✗ | return AVERROR(EINVAL); | |
119 | } | ||
120 | 24 | s->bitrate_index = i; | |
121 | |||
122 | /* compute total header size & pad bit */ | ||
123 | |||
124 | 24 | a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0); | |
125 | 24 | s->frame_size = ((int)a) * 8; | |
126 | |||
127 | /* frame fractional size to compute padding */ | ||
128 | 24 | s->frame_frac = 0; | |
129 | 24 | s->frame_frac_incr = (int)((a - floor(a)) * 65536.0); | |
130 | |||
131 | /* select the right allocation table */ | ||
132 | 24 | table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf); | |
133 | |||
134 | /* number of used subbands */ | ||
135 | 24 | s->sblimit = ff_mpa_sblimit_table[table]; | |
136 | 24 | s->alloc_table = ff_mpa_alloc_tables[table]; | |
137 | |||
138 | ff_dlog(avctx, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n", | ||
139 | bitrate, freq, s->frame_size, table, s->frame_frac_incr); | ||
140 | |||
141 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 24 times.
|
51 | for(i=0;i<s->nb_channels;i++) |
142 | 27 | s->samples_offset[i] = 0; | |
143 | |||
144 |
2/2✓ Branch 0 taken 6168 times.
✓ Branch 1 taken 24 times.
|
6192 | for(i=0;i<257;i++) { |
145 | int v; | ||
146 | 6168 | v = ff_mpa_enwindow[i]; | |
147 | #if WFRAC_BITS != 16 | ||
148 | 6168 | v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS); | |
149 | #endif | ||
150 | 6168 | s->filter_bank[i] = v; | |
151 |
2/2✓ Branch 0 taken 6048 times.
✓ Branch 1 taken 120 times.
|
6168 | if ((i & 63) != 0) |
152 | 6048 | v = -v; | |
153 |
2/2✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 24 times.
|
6168 | if (i != 0) |
154 | 6144 | s->filter_bank[512 - i] = v; | |
155 | } | ||
156 | |||
157 |
2/2✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 24 times.
|
1560 | for(i=0;i<64;i++) { |
158 | 1536 | v = (int)(exp2((3 - i) / 3.0) * (1 << 20)); | |
159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1536 times.
|
1536 | if (v <= 0) |
160 | ✗ | v = 1; | |
161 | 1536 | s->scale_factor_table[i] = v; | |
162 | #if USE_FLOATS | ||
163 | 704 | s->scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20); | |
164 | #else | ||
165 | #define P 15 | ||
166 | 832 | s->scale_factor_shift[i] = 21 - P - (i / 3); | |
167 | 832 | s->scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0); | |
168 | #endif | ||
169 | } | ||
170 |
2/2✓ Branch 0 taken 3072 times.
✓ Branch 1 taken 24 times.
|
3096 | for(i=0;i<128;i++) { |
171 | 3072 | v = i - 64; | |
172 |
2/2✓ Branch 0 taken 1488 times.
✓ Branch 1 taken 1584 times.
|
3072 | if (v <= -3) |
173 | 1488 | v = 0; | |
174 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1536 times.
|
1584 | else if (v < 0) |
175 | 48 | v = 1; | |
176 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1512 times.
|
1536 | else if (v == 0) |
177 | 24 | v = 2; | |
178 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1464 times.
|
1512 | else if (v < 3) |
179 | 48 | v = 3; | |
180 | else | ||
181 | 1464 | v = 4; | |
182 | 3072 | s->scale_diff_table[i] = v; | |
183 | } | ||
184 | |||
185 |
2/2✓ Branch 0 taken 408 times.
✓ Branch 1 taken 24 times.
|
432 | for(i=0;i<17;i++) { |
186 | 408 | v = ff_mpa_quant_bits[i]; | |
187 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 336 times.
|
408 | if (v < 0) |
188 | 72 | v = -v; | |
189 | else | ||
190 | 336 | v = v * 3; | |
191 | 408 | s->total_quant_bits[i] = 12 * v; | |
192 | } | ||
193 | |||
194 | 24 | return 0; | |
195 | } | ||
196 | |||
197 | /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */ | ||
198 | 317952 | static void idct32(int *out, int *tab) | |
199 | { | ||
200 | int i, j; | ||
201 | int *t, *t1, xr; | ||
202 | 317952 | const int *xp = costab32; | |
203 | |||
204 |
2/2✓ Branch 0 taken 4769280 times.
✓ Branch 1 taken 317952 times.
|
5087232 | for(j=31;j>=3;j-=2) tab[j] += tab[j - 2]; |
205 | |||
206 | 317952 | t = tab + 30; | |
207 | 317952 | t1 = tab + 2; | |
208 | do { | ||
209 | 2225664 | t[0] += t[-4]; | |
210 | 2225664 | t[1] += t[1 - 4]; | |
211 | 2225664 | t -= 4; | |
212 |
2/2✓ Branch 0 taken 1907712 times.
✓ Branch 1 taken 317952 times.
|
2225664 | } while (t != t1); |
213 | |||
214 | 317952 | t = tab + 28; | |
215 | 317952 | t1 = tab + 4; | |
216 | do { | ||
217 | 953856 | t[0] += t[-8]; | |
218 | 953856 | t[1] += t[1-8]; | |
219 | 953856 | t[2] += t[2-8]; | |
220 | 953856 | t[3] += t[3-8]; | |
221 | 953856 | t -= 8; | |
222 |
2/2✓ Branch 0 taken 635904 times.
✓ Branch 1 taken 317952 times.
|
953856 | } while (t != t1); |
223 | |||
224 | 317952 | t = tab; | |
225 | 317952 | t1 = tab + 32; | |
226 | do { | ||
227 | 635904 | t[ 3] = -t[ 3]; | |
228 | 635904 | t[ 6] = -t[ 6]; | |
229 | |||
230 | 635904 | t[11] = -t[11]; | |
231 | 635904 | t[12] = -t[12]; | |
232 | 635904 | t[13] = -t[13]; | |
233 | 635904 | t[15] = -t[15]; | |
234 | 635904 | t += 16; | |
235 |
2/2✓ Branch 0 taken 317952 times.
✓ Branch 1 taken 317952 times.
|
635904 | } while (t != t1); |
236 | |||
237 | |||
238 | 317952 | t = tab; | |
239 | 317952 | t1 = tab + 8; | |
240 | do { | ||
241 | int x1, x2, x3, x4; | ||
242 | |||
243 | 2543616 | x3 = MUL(t[16], FIX(M_SQRT2*0.5)); | |
244 | 2543616 | x4 = t[0] - x3; | |
245 | 2543616 | x3 = t[0] + x3; | |
246 | |||
247 | 2543616 | x2 = MUL(-(t[24] + t[8]), FIX(M_SQRT2*0.5)); | |
248 | 2543616 | x1 = MUL((t[8] - x2), xp[0]); | |
249 | 2543616 | x2 = MUL((t[8] + x2), xp[1]); | |
250 | |||
251 | 2543616 | t[ 0] = x3 + x1; | |
252 | 2543616 | t[ 8] = x4 - x2; | |
253 | 2543616 | t[16] = x4 + x2; | |
254 | 2543616 | t[24] = x3 - x1; | |
255 | 2543616 | t++; | |
256 |
2/2✓ Branch 0 taken 2225664 times.
✓ Branch 1 taken 317952 times.
|
2543616 | } while (t != t1); |
257 | |||
258 | 317952 | xp += 2; | |
259 | 317952 | t = tab; | |
260 | 317952 | t1 = tab + 4; | |
261 | do { | ||
262 | 1271808 | xr = MUL(t[28],xp[0]); | |
263 | 1271808 | t[28] = (t[0] - xr); | |
264 | 1271808 | t[0] = (t[0] + xr); | |
265 | |||
266 | 1271808 | xr = MUL(t[4],xp[1]); | |
267 | 1271808 | t[ 4] = (t[24] - xr); | |
268 | 1271808 | t[24] = (t[24] + xr); | |
269 | |||
270 | 1271808 | xr = MUL(t[20],xp[2]); | |
271 | 1271808 | t[20] = (t[8] - xr); | |
272 | 1271808 | t[ 8] = (t[8] + xr); | |
273 | |||
274 | 1271808 | xr = MUL(t[12],xp[3]); | |
275 | 1271808 | t[12] = (t[16] - xr); | |
276 | 1271808 | t[16] = (t[16] + xr); | |
277 | 1271808 | t++; | |
278 |
2/2✓ Branch 0 taken 953856 times.
✓ Branch 1 taken 317952 times.
|
1271808 | } while (t != t1); |
279 | 317952 | xp += 4; | |
280 | |||
281 |
2/2✓ Branch 0 taken 1271808 times.
✓ Branch 1 taken 317952 times.
|
1589760 | for (i = 0; i < 4; i++) { |
282 | 1271808 | xr = MUL(tab[30-i*4],xp[0]); | |
283 | 1271808 | tab[30-i*4] = (tab[i*4] - xr); | |
284 | 1271808 | tab[ i*4] = (tab[i*4] + xr); | |
285 | |||
286 | 1271808 | xr = MUL(tab[ 2+i*4],xp[1]); | |
287 | 1271808 | tab[ 2+i*4] = (tab[28-i*4] - xr); | |
288 | 1271808 | tab[28-i*4] = (tab[28-i*4] + xr); | |
289 | |||
290 | 1271808 | xr = MUL(tab[31-i*4],xp[0]); | |
291 | 1271808 | tab[31-i*4] = (tab[1+i*4] - xr); | |
292 | 1271808 | tab[ 1+i*4] = (tab[1+i*4] + xr); | |
293 | |||
294 | 1271808 | xr = MUL(tab[ 3+i*4],xp[1]); | |
295 | 1271808 | tab[ 3+i*4] = (tab[29-i*4] - xr); | |
296 | 1271808 | tab[29-i*4] = (tab[29-i*4] + xr); | |
297 | |||
298 | 1271808 | xp += 2; | |
299 | } | ||
300 | |||
301 | 317952 | t = tab + 30; | |
302 | 317952 | t1 = tab + 1; | |
303 | do { | ||
304 | 5087232 | xr = MUL(t1[0], *xp); | |
305 | 5087232 | t1[0] = (t[0] - xr); | |
306 | 5087232 | t[0] = (t[0] + xr); | |
307 | 5087232 | t -= 2; | |
308 | 5087232 | t1 += 2; | |
309 | 5087232 | xp++; | |
310 |
2/2✓ Branch 0 taken 4769280 times.
✓ Branch 1 taken 317952 times.
|
5087232 | } while (t >= tab); |
311 | |||
312 |
2/2✓ Branch 0 taken 10174464 times.
✓ Branch 1 taken 317952 times.
|
10492416 | for(i=0;i<32;i++) { |
313 | 10174464 | out[i] = tab[bitinv32[i]]; | |
314 | } | ||
315 | 317952 | } | |
316 | |||
317 | #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS) | ||
318 | |||
319 | 8832 | static void filter(MpegAudioContext *s, int ch, const short *samples, int incr) | |
320 | { | ||
321 | short *p, *q; | ||
322 | int sum, offset, i, j; | ||
323 | int tmp[64]; | ||
324 | int tmp1[32]; | ||
325 | int *out; | ||
326 | |||
327 | 8832 | offset = s->samples_offset[ch]; | |
328 | 8832 | out = &s->sb_samples[ch][0][0][0]; | |
329 |
2/2✓ Branch 0 taken 317952 times.
✓ Branch 1 taken 8832 times.
|
326784 | for(j=0;j<36;j++) { |
330 | /* 32 samples at once */ | ||
331 |
2/2✓ Branch 0 taken 10174464 times.
✓ Branch 1 taken 317952 times.
|
10492416 | for(i=0;i<32;i++) { |
332 | 10174464 | s->samples_buf[ch][offset + (31 - i)] = samples[0]; | |
333 | 10174464 | samples += incr; | |
334 | } | ||
335 | |||
336 | /* filter */ | ||
337 | 317952 | p = s->samples_buf[ch] + offset; | |
338 | 317952 | q = s->filter_bank; | |
339 | /* maxsum = 23169 */ | ||
340 |
2/2✓ Branch 0 taken 20348928 times.
✓ Branch 1 taken 317952 times.
|
20666880 | for(i=0;i<64;i++) { |
341 | 20348928 | sum = p[0*64] * q[0*64]; | |
342 | 20348928 | sum += p[1*64] * q[1*64]; | |
343 | 20348928 | sum += p[2*64] * q[2*64]; | |
344 | 20348928 | sum += p[3*64] * q[3*64]; | |
345 | 20348928 | sum += p[4*64] * q[4*64]; | |
346 | 20348928 | sum += p[5*64] * q[5*64]; | |
347 | 20348928 | sum += p[6*64] * q[6*64]; | |
348 | 20348928 | sum += p[7*64] * q[7*64]; | |
349 | 20348928 | tmp[i] = sum; | |
350 | 20348928 | p++; | |
351 | 20348928 | q++; | |
352 | } | ||
353 | 317952 | tmp1[0] = tmp[16] >> WSHIFT; | |
354 |
2/2✓ Branch 0 taken 5087232 times.
✓ Branch 1 taken 317952 times.
|
5405184 | for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT; |
355 |
2/2✓ Branch 0 taken 4769280 times.
✓ Branch 1 taken 317952 times.
|
5087232 | for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT; |
356 | |||
357 | 317952 | idct32(out, tmp1); | |
358 | |||
359 | /* advance of 32 samples */ | ||
360 | 317952 | offset -= 32; | |
361 | 317952 | out += 32; | |
362 | /* handle the wrap around */ | ||
363 |
2/2✓ Branch 0 taken 2834 times.
✓ Branch 1 taken 315118 times.
|
317952 | if (offset < 0) { |
364 | 2834 | memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32), | |
365 | 2834 | s->samples_buf[ch], (512 - 32) * 2); | |
366 | 2834 | offset = SAMPLES_BUF_SIZE - 512; | |
367 | } | ||
368 | } | ||
369 | 8832 | s->samples_offset[ch] = offset; | |
370 | 8832 | } | |
371 | |||
372 | 8832 | static void compute_scale_factors(MpegAudioContext *s, | |
373 | unsigned char scale_code[SBLIMIT], | ||
374 | unsigned char scale_factors[SBLIMIT][3], | ||
375 | int sb_samples[3][12][SBLIMIT], | ||
376 | int sblimit) | ||
377 | { | ||
378 | int *p, vmax, v, n, i, j, k, code; | ||
379 | int index, d1, d2; | ||
380 | 8832 | unsigned char *sf = &scale_factors[0][0]; | |
381 | |||
382 |
2/2✓ Branch 0 taken 262368 times.
✓ Branch 1 taken 8832 times.
|
271200 | for(j=0;j<sblimit;j++) { |
383 |
2/2✓ Branch 0 taken 787104 times.
✓ Branch 1 taken 262368 times.
|
1049472 | for(i=0;i<3;i++) { |
384 | /* find the max absolute value */ | ||
385 | 787104 | p = &sb_samples[i][0][j]; | |
386 | 787104 | vmax = abs(*p); | |
387 |
2/2✓ Branch 0 taken 8658144 times.
✓ Branch 1 taken 787104 times.
|
9445248 | for(k=1;k<12;k++) { |
388 | 8658144 | p += SBLIMIT; | |
389 | 8658144 | v = abs(*p); | |
390 |
2/2✓ Branch 0 taken 1445189 times.
✓ Branch 1 taken 7212955 times.
|
8658144 | if (v > vmax) |
391 | 1445189 | vmax = v; | |
392 | } | ||
393 | /* compute the scale factor index using log 2 computations */ | ||
394 |
2/2✓ Branch 0 taken 787034 times.
✓ Branch 1 taken 70 times.
|
787104 | if (vmax > 1) { |
395 | 787034 | n = av_log2(vmax); | |
396 | /* n is the position of the MSB of vmax. now | ||
397 | use at most 2 compares to find the index */ | ||
398 | 787034 | index = (21 - n) * 3 - 3; | |
399 |
1/2✓ Branch 0 taken 787034 times.
✗ Branch 1 not taken.
|
787034 | if (index >= 0) { |
400 |
2/2✓ Branch 0 taken 976384 times.
✓ Branch 1 taken 787034 times.
|
1763418 | while (vmax <= s->scale_factor_table[index+1]) |
401 | 976384 | index++; | |
402 | } else { | ||
403 | ✗ | index = 0; /* very unlikely case of overflow */ | |
404 | } | ||
405 | } else { | ||
406 | 70 | index = 62; /* value 63 is not allowed */ | |
407 | } | ||
408 | |||
409 | ff_dlog(NULL, "%2d:%d in=%x %x %d\n", | ||
410 | j, i, vmax, s->scale_factor_table[index], index); | ||
411 | /* store the scale factor */ | ||
412 | av_assert2(index >=0 && index <= 63); | ||
413 | 787104 | sf[i] = index; | |
414 | } | ||
415 | |||
416 | /* compute the transmission factor : look if the scale factors | ||
417 | are close enough to each other */ | ||
418 | 262368 | d1 = s->scale_diff_table[sf[0] - sf[1] + 64]; | |
419 | 262368 | d2 = s->scale_diff_table[sf[1] - sf[2] + 64]; | |
420 | |||
421 | /* handle the 25 cases */ | ||
422 |
8/9✓ Branch 0 taken 4157 times.
✓ Branch 1 taken 4698 times.
✓ Branch 2 taken 3611 times.
✓ Branch 3 taken 4331 times.
✓ Branch 4 taken 122670 times.
✓ Branch 5 taken 34842 times.
✓ Branch 6 taken 57553 times.
✓ Branch 7 taken 30506 times.
✗ Branch 8 not taken.
|
262368 | switch(d1 * 5 + d2) { |
423 | 4157 | case 0*5+0: | |
424 | case 0*5+4: | ||
425 | case 3*5+4: | ||
426 | case 4*5+0: | ||
427 | case 4*5+4: | ||
428 | 4157 | code = 0; | |
429 | 4157 | break; | |
430 | 4698 | case 0*5+1: | |
431 | case 0*5+2: | ||
432 | case 4*5+1: | ||
433 | case 4*5+2: | ||
434 | 4698 | code = 3; | |
435 | 4698 | sf[2] = sf[1]; | |
436 | 4698 | break; | |
437 | 3611 | case 0*5+3: | |
438 | case 4*5+3: | ||
439 | 3611 | code = 3; | |
440 | 3611 | sf[1] = sf[2]; | |
441 | 3611 | break; | |
442 | 4331 | case 1*5+0: | |
443 | case 1*5+4: | ||
444 | case 2*5+4: | ||
445 | 4331 | code = 1; | |
446 | 4331 | sf[1] = sf[0]; | |
447 | 4331 | break; | |
448 | 122670 | case 1*5+1: | |
449 | case 1*5+2: | ||
450 | case 2*5+0: | ||
451 | case 2*5+1: | ||
452 | case 2*5+2: | ||
453 | 122670 | code = 2; | |
454 | 122670 | sf[1] = sf[2] = sf[0]; | |
455 | 122670 | break; | |
456 | 34842 | case 2*5+3: | |
457 | case 3*5+3: | ||
458 | 34842 | code = 2; | |
459 | 34842 | sf[0] = sf[1] = sf[2]; | |
460 | 34842 | break; | |
461 | 57553 | case 3*5+0: | |
462 | case 3*5+1: | ||
463 | case 3*5+2: | ||
464 | 57553 | code = 2; | |
465 | 57553 | sf[0] = sf[2] = sf[1]; | |
466 | 57553 | break; | |
467 | 30506 | case 1*5+3: | |
468 | 30506 | code = 2; | |
469 |
2/2✓ Branch 0 taken 4880 times.
✓ Branch 1 taken 25626 times.
|
30506 | if (sf[0] > sf[2]) |
470 | 4880 | sf[0] = sf[2]; | |
471 | 30506 | sf[1] = sf[2] = sf[0]; | |
472 | 30506 | break; | |
473 | ✗ | default: | |
474 | av_assert2(0); //cannot happen | ||
475 | ✗ | code = 0; /* kill warning */ | |
476 | } | ||
477 | |||
478 | ff_dlog(NULL, "%d: %2d %2d %2d %d %d -> %d\n", j, | ||
479 | sf[0], sf[1], sf[2], d1, d2, code); | ||
480 | 262368 | scale_code[j] = code; | |
481 | 262368 | sf += 3; | |
482 | } | ||
483 | 8832 | } | |
484 | |||
485 | /* The most important function : psycho acoustic module. In this | ||
486 | encoder there is basically none, so this is the worst you can do, | ||
487 | but also this is the simpler. */ | ||
488 | 8832 | static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT]) | |
489 | { | ||
490 | int i; | ||
491 | |||
492 |
2/2✓ Branch 0 taken 262368 times.
✓ Branch 1 taken 8832 times.
|
271200 | for(i=0;i<s->sblimit;i++) { |
493 | 262368 | smr[i] = (int)(fixed_smr[i] * 10); | |
494 | } | ||
495 | 8832 | } | |
496 | |||
497 | |||
498 | #define SB_NOTALLOCATED 0 | ||
499 | #define SB_ALLOCATED 1 | ||
500 | #define SB_NOMORE 2 | ||
501 | |||
502 | /* Try to maximize the smr while using a number of bits inferior to | ||
503 | the frame size. I tried to make the code simpler, faster and | ||
504 | smaller than other encoders :-) */ | ||
505 | 8365 | static void compute_bit_allocation(MpegAudioContext *s, | |
506 | short smr1[MPA_MAX_CHANNELS][SBLIMIT], | ||
507 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], | ||
508 | int *padding) | ||
509 | { | ||
510 | int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size; | ||
511 | int incr; | ||
512 | short smr[MPA_MAX_CHANNELS][SBLIMIT]; | ||
513 | unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT]; | ||
514 | const unsigned char *alloc; | ||
515 | |||
516 | 8365 | memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT); | |
517 | 8365 | memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT); | |
518 | 8365 | memset(bit_alloc, 0, s->nb_channels * SBLIMIT); | |
519 | |||
520 | /* compute frame size and padding */ | ||
521 | 8365 | max_frame_size = s->frame_size; | |
522 | 8365 | s->frame_frac += s->frame_frac_incr; | |
523 |
2/2✓ Branch 0 taken 7387 times.
✓ Branch 1 taken 978 times.
|
8365 | if (s->frame_frac >= 65536) { |
524 | 7387 | s->frame_frac -= 65536; | |
525 | 7387 | s->do_padding = 1; | |
526 | 7387 | max_frame_size += 8; | |
527 | } else { | ||
528 | 978 | s->do_padding = 0; | |
529 | } | ||
530 | |||
531 | /* compute the header + bit alloc size */ | ||
532 | 8365 | current_frame_size = 32; | |
533 | 8365 | alloc = s->alloc_table; | |
534 |
2/2✓ Branch 0 taken 249069 times.
✓ Branch 1 taken 8365 times.
|
257434 | for(i=0;i<s->sblimit;i++) { |
535 | 249069 | incr = alloc[0]; | |
536 | 249069 | current_frame_size += incr * s->nb_channels; | |
537 | 249069 | alloc += 1 << incr; | |
538 | } | ||
539 | for(;;) { | ||
540 | /* look for the subband with the largest signal to mask ratio */ | ||
541 | 1821457 | max_sb = -1; | |
542 | 1821457 | max_ch = -1; | |
543 | 1821457 | max_smr = INT_MIN; | |
544 |
2/2✓ Branch 0 taken 1916936 times.
✓ Branch 1 taken 1821457 times.
|
3738393 | for(ch=0;ch<s->nb_channels;ch++) { |
545 |
2/2✓ Branch 0 taken 57269046 times.
✓ Branch 1 taken 1916936 times.
|
59185982 | for(i=0;i<s->sblimit;i++) { |
546 |
4/4✓ Branch 0 taken 8170261 times.
✓ Branch 1 taken 49098785 times.
✓ Branch 2 taken 4826226 times.
✓ Branch 3 taken 3344035 times.
|
57269046 | if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) { |
547 | 4826226 | max_smr = smr[ch][i]; | |
548 | 4826226 | max_sb = i; | |
549 | 4826226 | max_ch = ch; | |
550 | } | ||
551 | } | ||
552 | } | ||
553 |
2/2✓ Branch 0 taken 8365 times.
✓ Branch 1 taken 1813092 times.
|
1821457 | if (max_sb < 0) |
554 | 8365 | break; | |
555 | ff_dlog(NULL, "current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n", | ||
556 | current_frame_size, max_frame_size, max_sb, max_ch, | ||
557 | bit_alloc[max_ch][max_sb]); | ||
558 | |||
559 | /* find alloc table entry (XXX: not optimal, should use | ||
560 | pointer table) */ | ||
561 | 1813092 | alloc = s->alloc_table; | |
562 |
2/2✓ Branch 0 taken 16273668 times.
✓ Branch 1 taken 1813092 times.
|
18086760 | for(i=0;i<max_sb;i++) { |
563 | 16273668 | alloc += 1 << alloc[0]; | |
564 | } | ||
565 | |||
566 |
2/2✓ Branch 0 taken 262368 times.
✓ Branch 1 taken 1550724 times.
|
1813092 | if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) { |
567 | /* nothing was coded for this band: add the necessary bits */ | ||
568 | 262368 | incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6; | |
569 | 262368 | incr += s->total_quant_bits[alloc[1]]; | |
570 | } else { | ||
571 | /* increments bit allocation */ | ||
572 | 1550724 | b = bit_alloc[max_ch][max_sb]; | |
573 | 1550724 | incr = s->total_quant_bits[alloc[b + 1]] - | |
574 | 1550724 | s->total_quant_bits[alloc[b]]; | |
575 | } | ||
576 | |||
577 |
2/2✓ Branch 0 taken 1611956 times.
✓ Branch 1 taken 201136 times.
|
1813092 | if (current_frame_size + incr <= max_frame_size) { |
578 | /* can increase size */ | ||
579 | 1611956 | b = ++bit_alloc[max_ch][max_sb]; | |
580 | 1611956 | current_frame_size += incr; | |
581 | /* decrease smr by the resolution we added */ | ||
582 | 1611956 | smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]]; | |
583 | /* max allocation size reached ? */ | ||
584 |
2/2✓ Branch 0 taken 61232 times.
✓ Branch 1 taken 1550724 times.
|
1611956 | if (b == ((1 << alloc[0]) - 1)) |
585 | 61232 | subband_status[max_ch][max_sb] = SB_NOMORE; | |
586 | else | ||
587 | 1550724 | subband_status[max_ch][max_sb] = SB_ALLOCATED; | |
588 | } else { | ||
589 | /* cannot increase the size of this subband */ | ||
590 | 201136 | subband_status[max_ch][max_sb] = SB_NOMORE; | |
591 | } | ||
592 | } | ||
593 | 8365 | *padding = max_frame_size - current_frame_size; | |
594 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8365 times.
|
8365 | av_assert0(*padding >= 0); |
595 | 8365 | } | |
596 | |||
597 | /* | ||
598 | * Output the MPEG audio layer 2 frame. Note how the code is small | ||
599 | * compared to other encoders :-) | ||
600 | */ | ||
601 | 8365 | static void encode_frame(MpegAudioContext *s, | |
602 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], | ||
603 | int padding) | ||
604 | { | ||
605 | int i, j, k, l, bit_alloc_bits, b, ch; | ||
606 | unsigned char *sf; | ||
607 | int q[3]; | ||
608 | 8365 | PutBitContext *p = &s->pb; | |
609 | |||
610 | /* header */ | ||
611 | |||
612 | 8365 | put_bits(p, 12, 0xfff); | |
613 | 8365 | put_bits(p, 1, 1 - s->lsf); /* 1 = MPEG-1 ID, 0 = MPEG-2 lsf ID */ | |
614 | 8365 | put_bits(p, 2, 4-2); /* layer 2 */ | |
615 | 8365 | put_bits(p, 1, 1); /* no error protection */ | |
616 | 8365 | put_bits(p, 4, s->bitrate_index); | |
617 | 8365 | put_bits(p, 2, s->freq_index); | |
618 | 8365 | put_bits(p, 1, s->do_padding); /* use padding */ | |
619 | 8365 | put_bits(p, 1, 0); /* private_bit */ | |
620 |
2/2✓ Branch 0 taken 467 times.
✓ Branch 1 taken 7898 times.
|
8365 | put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO); |
621 | 8365 | put_bits(p, 2, 0); /* mode_ext */ | |
622 | 8365 | put_bits(p, 1, 0); /* no copyright */ | |
623 | 8365 | put_bits(p, 1, 1); /* original */ | |
624 | 8365 | put_bits(p, 2, 0); /* no emphasis */ | |
625 | |||
626 | /* bit allocation */ | ||
627 | 8365 | j = 0; | |
628 |
2/2✓ Branch 0 taken 249069 times.
✓ Branch 1 taken 8365 times.
|
257434 | for(i=0;i<s->sblimit;i++) { |
629 | 249069 | bit_alloc_bits = s->alloc_table[j]; | |
630 |
2/2✓ Branch 0 taken 262368 times.
✓ Branch 1 taken 249069 times.
|
511437 | for(ch=0;ch<s->nb_channels;ch++) { |
631 | 262368 | put_bits(p, bit_alloc_bits, bit_alloc[ch][i]); | |
632 | } | ||
633 | 249069 | j += 1 << bit_alloc_bits; | |
634 | } | ||
635 | |||
636 | /* scale codes */ | ||
637 |
2/2✓ Branch 0 taken 249069 times.
✓ Branch 1 taken 8365 times.
|
257434 | for(i=0;i<s->sblimit;i++) { |
638 |
2/2✓ Branch 0 taken 262368 times.
✓ Branch 1 taken 249069 times.
|
511437 | for(ch=0;ch<s->nb_channels;ch++) { |
639 |
2/2✓ Branch 0 taken 187772 times.
✓ Branch 1 taken 74596 times.
|
262368 | if (bit_alloc[ch][i]) |
640 | 187772 | put_bits(p, 2, s->scale_code[ch][i]); | |
641 | } | ||
642 | } | ||
643 | |||
644 | /* scale factors */ | ||
645 |
2/2✓ Branch 0 taken 249069 times.
✓ Branch 1 taken 8365 times.
|
257434 | for(i=0;i<s->sblimit;i++) { |
646 |
2/2✓ Branch 0 taken 262368 times.
✓ Branch 1 taken 249069 times.
|
511437 | for(ch=0;ch<s->nb_channels;ch++) { |
647 |
2/2✓ Branch 0 taken 187772 times.
✓ Branch 1 taken 74596 times.
|
262368 | if (bit_alloc[ch][i]) { |
648 | 187772 | sf = &s->scale_factors[ch][i][0]; | |
649 |
3/4✓ Branch 0 taken 2643 times.
✓ Branch 1 taken 8019 times.
✓ Branch 2 taken 177110 times.
✗ Branch 3 not taken.
|
187772 | switch(s->scale_code[ch][i]) { |
650 | 2643 | case 0: | |
651 | 2643 | put_bits(p, 6, sf[0]); | |
652 | 2643 | put_bits(p, 6, sf[1]); | |
653 | 2643 | put_bits(p, 6, sf[2]); | |
654 | 2643 | break; | |
655 | 8019 | case 3: | |
656 | case 1: | ||
657 | 8019 | put_bits(p, 6, sf[0]); | |
658 | 8019 | put_bits(p, 6, sf[2]); | |
659 | 8019 | break; | |
660 | 177110 | case 2: | |
661 | 177110 | put_bits(p, 6, sf[0]); | |
662 | 177110 | break; | |
663 | } | ||
664 | } | ||
665 | } | ||
666 | } | ||
667 | |||
668 | /* quantization & write sub band samples */ | ||
669 | |||
670 |
2/2✓ Branch 0 taken 25095 times.
✓ Branch 1 taken 8365 times.
|
33460 | for(k=0;k<3;k++) { |
671 |
2/2✓ Branch 0 taken 100380 times.
✓ Branch 1 taken 25095 times.
|
125475 | for(l=0;l<12;l+=3) { |
672 | 100380 | j = 0; | |
673 |
2/2✓ Branch 0 taken 2988828 times.
✓ Branch 1 taken 100380 times.
|
3089208 | for(i=0;i<s->sblimit;i++) { |
674 | 2988828 | bit_alloc_bits = s->alloc_table[j]; | |
675 |
2/2✓ Branch 0 taken 3148416 times.
✓ Branch 1 taken 2988828 times.
|
6137244 | for(ch=0;ch<s->nb_channels;ch++) { |
676 | 3148416 | b = bit_alloc[ch][i]; | |
677 |
2/2✓ Branch 0 taken 2253264 times.
✓ Branch 1 taken 895152 times.
|
3148416 | if (b) { |
678 | int qindex, steps, m, sample, bits; | ||
679 | /* we encode 3 sub band samples of the same sub band at a time */ | ||
680 | 2253264 | qindex = s->alloc_table[j+b]; | |
681 | 2253264 | steps = ff_mpa_quant_steps[qindex]; | |
682 |
2/2✓ Branch 0 taken 6759792 times.
✓ Branch 1 taken 2253264 times.
|
9013056 | for(m=0;m<3;m++) { |
683 | 6759792 | sample = s->sb_samples[ch][k][l + m][i]; | |
684 | /* divide by scale factor */ | ||
685 | #if USE_FLOATS | ||
686 | { | ||
687 | float a; | ||
688 | 489240 | a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]]; | |
689 | 489240 | q[m] = (int)((a + 1.0) * steps * 0.5); | |
690 | } | ||
691 | #else | ||
692 | { | ||
693 | int q1, e, shift, mult; | ||
694 | 6270552 | e = s->scale_factors[ch][i][k]; | |
695 | 6270552 | shift = s->scale_factor_shift[e]; | |
696 | 6270552 | mult = s->scale_factor_mult[e]; | |
697 | |||
698 | /* normalize to P bits */ | ||
699 |
2/2✓ Branch 0 taken 5718264 times.
✓ Branch 1 taken 552288 times.
|
6270552 | if (shift < 0) |
700 | 5718264 | q1 = sample * (1 << -shift); | |
701 | else | ||
702 | 552288 | q1 = sample >> shift; | |
703 | 6270552 | q1 = (q1 * mult) >> P; | |
704 | 6270552 | q1 += 1 << P; | |
705 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6270552 times.
|
6270552 | if (q1 < 0) |
706 | ✗ | q1 = 0; | |
707 | 6270552 | q[m] = (q1 * (unsigned)steps) >> (P + 1); | |
708 | } | ||
709 | #endif | ||
710 |
2/2✓ Branch 0 taken 11499 times.
✓ Branch 1 taken 6748293 times.
|
6759792 | if (q[m] >= steps) |
711 | 11499 | q[m] = steps - 1; | |
712 | av_assert2(q[m] >= 0 && q[m] < steps); | ||
713 | } | ||
714 | 2253264 | bits = ff_mpa_quant_bits[qindex]; | |
715 |
2/2✓ Branch 0 taken 308256 times.
✓ Branch 1 taken 1945008 times.
|
2253264 | if (bits < 0) { |
716 | /* group the 3 values to save bits */ | ||
717 | 308256 | put_bits(p, -bits, | |
718 | 308256 | q[0] + steps * (q[1] + steps * q[2])); | |
719 | } else { | ||
720 | 1945008 | put_bits(p, bits, q[0]); | |
721 | 1945008 | put_bits(p, bits, q[1]); | |
722 | 1945008 | put_bits(p, bits, q[2]); | |
723 | } | ||
724 | } | ||
725 | } | ||
726 | /* next subband in alloc table */ | ||
727 | 2988828 | j += 1 << bit_alloc_bits; | |
728 | } | ||
729 | } | ||
730 | } | ||
731 | |||
732 | /* padding */ | ||
733 |
2/2✓ Branch 0 taken 65742 times.
✓ Branch 1 taken 8365 times.
|
74107 | for(i=0;i<padding;i++) |
734 | 65742 | put_bits(p, 1, 0); | |
735 | 8365 | } | |
736 | |||
737 | 8365 | static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, | |
738 | const AVFrame *frame, int *got_packet_ptr) | ||
739 | { | ||
740 | 8365 | MpegAudioContext *s = avctx->priv_data; | |
741 | 8365 | const int16_t *samples = (const int16_t *)frame->data[0]; | |
742 | short smr[MPA_MAX_CHANNELS][SBLIMIT]; | ||
743 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; | ||
744 | int padding, i, ret; | ||
745 | |||
746 |
2/2✓ Branch 0 taken 8832 times.
✓ Branch 1 taken 8365 times.
|
17197 | for(i=0;i<s->nb_channels;i++) { |
747 | 8832 | filter(s, i, samples + i, s->nb_channels); | |
748 | } | ||
749 | |||
750 |
2/2✓ Branch 0 taken 8832 times.
✓ Branch 1 taken 8365 times.
|
17197 | for(i=0;i<s->nb_channels;i++) { |
751 | 8832 | compute_scale_factors(s, s->scale_code[i], s->scale_factors[i], | |
752 | 8832 | s->sb_samples[i], s->sblimit); | |
753 | } | ||
754 |
2/2✓ Branch 0 taken 8832 times.
✓ Branch 1 taken 8365 times.
|
17197 | for(i=0;i<s->nb_channels;i++) { |
755 | 8832 | psycho_acoustic_model(s, smr[i]); | |
756 | } | ||
757 | 8365 | compute_bit_allocation(s, smr, bit_alloc, &padding); | |
758 | |||
759 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8365 times.
|
8365 | if ((ret = ff_alloc_packet(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0) |
760 | ✗ | return ret; | |
761 | |||
762 | 8365 | init_put_bits(&s->pb, avpkt->data, avpkt->size); | |
763 | |||
764 | 8365 | encode_frame(s, bit_alloc, padding); | |
765 | |||
766 | /* flush */ | ||
767 | 8365 | flush_put_bits(&s->pb); | |
768 | 8365 | avpkt->size = put_bytes_output(&s->pb); | |
769 | |||
770 |
1/2✓ Branch 0 taken 8365 times.
✗ Branch 1 not taken.
|
8365 | if (frame->pts != AV_NOPTS_VALUE) |
771 | 8365 | avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding); | |
772 | |||
773 | 8365 | *got_packet_ptr = 1; | |
774 | 8365 | return 0; | |
775 | } | ||
776 | |||
777 | static const FFCodecDefault mp2_defaults[] = { | ||
778 | { "b", "0" }, | ||
779 | { NULL }, | ||
780 | }; | ||
781 | |||
782 |