FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegaudiodec_template.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 676 1005 67.3%
Branches: 348 548 63.5%

Line Branch Exec Source
1 /*
2 * MPEG Audio decoder
3 * Copyright (c) 2001, 2002 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 * MPEG Audio decoder
25 */
26
27 #include "libavutil/attributes.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/float_dsp.h"
32 #include "libavutil/libm.h"
33 #include "libavutil/mem_internal.h"
34 #include "libavutil/thread.h"
35
36 #include "avcodec.h"
37 #include "get_bits.h"
38 #include "internal.h"
39 #include "mathops.h"
40 #include "mpegaudiodsp.h"
41
42 /*
43 * TODO:
44 * - test lsf / mpeg25 extensively.
45 */
46
47 #include "mpegaudio.h"
48 #include "mpegaudiodecheader.h"
49
50 #define BACKSTEP_SIZE 512
51 #define EXTRABYTES 24
52 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
53
54 /* layer 3 "granule" */
55 typedef struct GranuleDef {
56 uint8_t scfsi;
57 int part2_3_length;
58 int big_values;
59 int global_gain;
60 int scalefac_compress;
61 uint8_t block_type;
62 uint8_t switch_point;
63 int table_select[3];
64 int subblock_gain[3];
65 uint8_t scalefac_scale;
66 uint8_t count1table_select;
67 int region_size[3]; /* number of huffman codes in each region */
68 int preflag;
69 int short_start, long_end; /* long/short band indexes */
70 uint8_t scale_factors[40];
71 DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
72 } GranuleDef;
73
74 typedef struct MPADecodeContext {
75 MPA_DECODE_HEADER
76 uint8_t last_buf[LAST_BUF_SIZE];
77 int last_buf_size;
78 int extrasize;
79 /* next header (used in free format parsing) */
80 uint32_t free_format_next_header;
81 GetBitContext gb;
82 GetBitContext in_gb;
83 DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
84 int synth_buf_offset[MPA_MAX_CHANNELS];
85 DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
86 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
87 GranuleDef granules[2][2]; /* Used in Layer 3 */
88 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
89 int dither_state;
90 int err_recognition;
91 AVCodecContext* avctx;
92 MPADSPContext mpadsp;
93 void (*butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len);
94 AVFrame *frame;
95 uint32_t crc;
96 } MPADecodeContext;
97
98 #define HEADER_SIZE 4
99
100 #include "mpegaudiodata.h"
101
102 #include "mpegaudio_tablegen.h"
103 /* intensity stereo coef table */
104 static INTFLOAT is_table_lsf[2][2][16];
105
106 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
107 static int32_t scale_factor_mult[15][3];
108 /* mult table for layer 2 group quantization */
109
110 #define SCALE_GEN(v) \
111 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
112
113 static const int32_t scale_factor_mult2[3][3] = {
114 SCALE_GEN(4.0 / 3.0), /* 3 steps */
115 SCALE_GEN(4.0 / 5.0), /* 5 steps */
116 SCALE_GEN(4.0 / 9.0), /* 9 steps */
117 };
118
119 /**
120 * Convert region offsets to region sizes and truncate
121 * size to big_values.
122 */
123 8138 static void region_offset2size(GranuleDef *g)
124 {
125 8138 int i, k, j = 0;
126 8138 g->region_size[2] = 576 / 2;
127
2/2
✓ Branch 0 taken 24414 times.
✓ Branch 1 taken 8138 times.
32552 for (i = 0; i < 3; i++) {
128 24414 k = FFMIN(g->region_size[i], g->big_values);
129 24414 g->region_size[i] = k - j;
130 24414 j = k;
131 }
132 8138 }
133
134 855 static void init_short_region(MPADecodeContext *s, GranuleDef *g)
135 {
136
2/2
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 545 times.
855 if (g->block_type == 2) {
137
1/2
✓ Branch 0 taken 310 times.
✗ Branch 1 not taken.
310 if (s->sample_rate_index != 8)
138 310 g->region_size[0] = (36 / 2);
139 else
140 g->region_size[0] = (72 / 2);
141 } else {
142
2/2
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 1 times.
545 if (s->sample_rate_index <= 2)
143 544 g->region_size[0] = (36 / 2);
144
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (s->sample_rate_index != 8)
145 1 g->region_size[0] = (54 / 2);
146 else
147 g->region_size[0] = (108 / 2);
148 }
149 855 g->region_size[1] = (576 / 2);
150 855 }
151
152 7283 static void init_long_region(MPADecodeContext *s, GranuleDef *g,
153 int ra1, int ra2)
154 {
155 int l;
156 7283 g->region_size[0] = ff_band_index_long[s->sample_rate_index][ra1 + 1];
157 /* should not overflow */
158 7283 l = FFMIN(ra1 + ra2 + 2, 22);
159 7283 g->region_size[1] = ff_band_index_long[s->sample_rate_index][ l];
160 7283 }
161
162 8138 static void compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
163 {
164
2/2
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 7828 times.
8138 if (g->block_type == 2) {
165
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 297 times.
310 if (g->switch_point) {
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if(s->sample_rate_index == 8)
167 avpriv_request_sample(s->avctx, "switch point in 8khz");
168 /* if switched mode, we handle the 36 first samples as
169 long blocks. For 8000Hz, we handle the 72 first
170 exponents as long blocks */
171
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (s->sample_rate_index <= 2)
172 13 g->long_end = 8;
173 else
174 g->long_end = 6;
175
176 13 g->short_start = 3;
177 } else {
178 297 g->long_end = 0;
179 297 g->short_start = 0;
180 }
181 } else {
182 7828 g->short_start = 13;
183 7828 g->long_end = 22;
184 }
185 8138 }
186
187 /* layer 1 unscaling */
188 /* n = number of bits of the mantissa minus 1 */
189 5035752 static inline int l1_unscale(int n, int mant, int scale_factor)
190 {
191 int shift, mod;
192 int64_t val;
193
194 5035752 shift = ff_scale_factor_modshift[scale_factor];
195 5035752 mod = shift & 3;
196 5035752 shift >>= 2;
197 5035752 val = MUL64((int)(mant + (-1U << n) + 1), scale_factor_mult[n-1][mod]);
198 5035752 shift += n;
199 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
200 5035752 return (int)((val + (1LL << (shift - 1))) >> shift);
201 }
202
203 908856 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
204 {
205 int shift, mod, val;
206
207 908856 shift = ff_scale_factor_modshift[scale_factor];
208 908856 mod = shift & 3;
209 908856 shift >>= 2;
210
211 908856 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
212 /* NOTE: at this point, 0 <= shift <= 21 */
213
1/2
✓ Branch 0 taken 908856 times.
✗ Branch 1 not taken.
908856 if (shift > 0)
214 908856 val = (val + (1 << (shift - 1))) >> shift;
215 908856 return val;
216 }
217
218 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
219 56735 static inline int l3_unscale(int value, int exponent)
220 {
221 unsigned int m;
222 int e;
223
224 56735 e = ff_table_4_3_exp [4 * value + (exponent & 3)];
225 56735 m = ff_table_4_3_value[4 * value + (exponent & 3)];
226 56735 e -= exponent >> 2;
227 #ifdef DEBUG
228 if(e < 1)
229 av_log(NULL, AV_LOG_WARNING, "l3_unscale: e is %d\n", e);
230 #endif
231
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 56725 times.
56735 if (e > (SUINT)31)
232 10 return 0;
233 56725 m = (m + ((1U << e) >> 1)) >> e;
234
235 56725 return m;
236 }
237
238 99 static av_cold void decode_init_static(void)
239 {
240 int i, j;
241
242 /* scale factor multiply for layer 1 */
243
2/2
✓ Branch 0 taken 1485 times.
✓ Branch 1 taken 99 times.
1584 for (i = 0; i < 15; i++) {
244 int n, norm;
245 1485 n = i + 2;
246 1485 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
247 1485 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
248 1485 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
249 1485 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
250 ff_dlog(NULL, "%d: norm=%x s=%"PRIx32" %"PRIx32" %"PRIx32"\n", i,
251 (unsigned)norm,
252 scale_factor_mult[i][0],
253 scale_factor_mult[i][1],
254 scale_factor_mult[i][2]);
255 }
256
257 /* compute n ^ (4/3) and store it in mantissa/exp format */
258
259 99 mpegaudio_tableinit();
260
261
2/2
✓ Branch 0 taken 1584 times.
✓ Branch 1 taken 99 times.
1683 for (i = 0; i < 16; i++) {
262 double f;
263 int e, k;
264
265
2/2
✓ Branch 0 taken 3168 times.
✓ Branch 1 taken 1584 times.
4752 for (j = 0; j < 2; j++) {
266 3168 e = -(j + 1) * ((i + 1) >> 1);
267 3168 f = exp2(e / 4.0);
268 3168 k = i & 1;
269 3168 is_table_lsf[j][k ^ 1][i] = FIXR(f);
270 3168 is_table_lsf[j][k ][i] = FIXR(1.0);
271 ff_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
272 i, j, (float) is_table_lsf[j][0][i],
273 (float) is_table_lsf[j][1][i]);
274 }
275 }
276 99 RENAME(ff_mpa_synth_init)();
277 99 ff_mpegaudiodec_common_init_static();
278 99 }
279
280 146 static av_cold int decode_init(AVCodecContext * avctx)
281 {
282 static AVOnce init_static_once = AV_ONCE_INIT;
283 146 MPADecodeContext *s = avctx->priv_data;
284
285 146 s->avctx = avctx;
286
287 #if USE_FLOATS
288 {
289 AVFloatDSPContext *fdsp;
290 75 fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (!fdsp)
292 return AVERROR(ENOMEM);
293 75 s->butterflies_float = fdsp->butterflies_float;
294 75 av_free(fdsp);
295 }
296 #endif
297
298 146 ff_mpadsp_init(&s->mpadsp);
299
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 if (avctx->request_sample_fmt == OUT_FMT &&
301 avctx->codec_id != AV_CODEC_ID_MP3ON4)
302 avctx->sample_fmt = OUT_FMT;
303 else
304 146 avctx->sample_fmt = OUT_FMT_P;
305 146 s->err_recognition = avctx->err_recognition;
306
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 if (avctx->codec_id == AV_CODEC_ID_MP3ADU)
308 s->adu_mode = 1;
309
310 146 ff_thread_once(&init_static_once, decode_init_static);
311
312 146 return 0;
313 }
314
315 #define C3 FIXHR(0.86602540378443864676/2)
316 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
317 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
318 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
319
320 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
321 cases. */
322 19692 static void imdct12(INTFLOAT *out, SUINTFLOAT *in)
323 {
324 SUINTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
325
326 19692 in0 = in[0*3];
327 19692 in1 = in[1*3] + in[0*3];
328 19692 in2 = in[2*3] + in[1*3];
329 19692 in3 = in[3*3] + in[2*3];
330 19692 in4 = in[4*3] + in[3*3];
331 19692 in5 = in[5*3] + in[4*3];
332 19692 in5 += in3;
333 19692 in3 += in1;
334
335 19692 in2 = MULH3(in2, C3, 2);
336 19692 in3 = MULH3(in3, C3, 4);
337
338 19692 t1 = in0 - in4;
339 19692 t2 = MULH3(in1 - in5, C4, 2);
340
341 19692 out[ 7] =
342 19692 out[10] = t1 + t2;
343 19692 out[ 1] =
344 19692 out[ 4] = t1 - t2;
345
346 19692 in0 += SHR(in4, 1);
347 19692 in4 = in0 + in2;
348 19692 in5 += 2*in1;
349 19692 in1 = MULH3(in5 + in3, C5, 1);
350 19692 out[ 8] =
351 19692 out[ 9] = in4 + in1;
352 19692 out[ 2] =
353 19692 out[ 3] = in4 - in1;
354
355 19692 in0 -= in2;
356 19692 in5 = MULH3(in5 - in3, C6, 2);
357 19692 out[ 0] =
358 19692 out[ 5] = in0 - in5;
359 19692 out[ 6] =
360 19692 out[11] = in0 + in5;
361 19692 }
362
363 10137 static int handle_crc(MPADecodeContext *s, int sec_len)
364 {
365
3/4
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 9931 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 206 times.
10137 if (s->error_protection && (s->err_recognition & AV_EF_CRCCHECK)) {
366 const uint8_t *buf = s->gb.buffer - HEADER_SIZE;
367 int sec_byte_len = sec_len >> 3;
368 int sec_rem_bits = sec_len & 7;
369 const AVCRC *crc_tab = av_crc_get_table(AV_CRC_16_ANSI);
370 uint8_t tmp_buf[4];
371 uint32_t crc_val = av_crc(crc_tab, UINT16_MAX, &buf[2], 2);
372 crc_val = av_crc(crc_tab, crc_val, &buf[6], sec_byte_len);
373
374 AV_WB32(tmp_buf,
375 ((buf[6 + sec_byte_len] & (0xFF00 >> sec_rem_bits)) << 24) +
376 ((s->crc << 16) >> sec_rem_bits));
377
378 crc_val = av_crc(crc_tab, crc_val, tmp_buf, 3);
379
380 if (crc_val) {
381 av_log(s->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc_val);
382 if (s->err_recognition & AV_EF_EXPLODE)
383 return AVERROR_INVALIDDATA;
384 }
385 }
386 10137 return 0;
387 }
388
389 /* return the number of decoded frames */
390 static int mp_decode_layer1(MPADecodeContext *s)
391 {
392 int bound, i, v, n, ch, j, mant;
393 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
394 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
395 int ret;
396
397 ret = handle_crc(s, (s->nb_channels == 1) ? 8*16 : 8*32);
398 if (ret < 0)
399 return ret;
400
401 if (s->mode == MPA_JSTEREO)
402 bound = (s->mode_ext + 1) * 4;
403 else
404 bound = SBLIMIT;
405
406 /* allocation bits */
407 for (i = 0; i < bound; i++) {
408 for (ch = 0; ch < s->nb_channels; ch++) {
409 allocation[ch][i] = get_bits(&s->gb, 4);
410 }
411 }
412 for (i = bound; i < SBLIMIT; i++)
413 allocation[0][i] = get_bits(&s->gb, 4);
414
415 /* scale factors */
416 for (i = 0; i < bound; i++) {
417 for (ch = 0; ch < s->nb_channels; ch++) {
418 if (allocation[ch][i])
419 scale_factors[ch][i] = get_bits(&s->gb, 6);
420 }
421 }
422 for (i = bound; i < SBLIMIT; i++) {
423 if (allocation[0][i]) {
424 scale_factors[0][i] = get_bits(&s->gb, 6);
425 scale_factors[1][i] = get_bits(&s->gb, 6);
426 }
427 }
428
429 /* compute samples */
430 for (j = 0; j < 12; j++) {
431 for (i = 0; i < bound; i++) {
432 for (ch = 0; ch < s->nb_channels; ch++) {
433 n = allocation[ch][i];
434 if (n) {
435 mant = get_bits(&s->gb, n + 1);
436 v = l1_unscale(n, mant, scale_factors[ch][i]);
437 } else {
438 v = 0;
439 }
440 s->sb_samples[ch][j][i] = v;
441 }
442 }
443 for (i = bound; i < SBLIMIT; i++) {
444 n = allocation[0][i];
445 if (n) {
446 mant = get_bits(&s->gb, n + 1);
447 v = l1_unscale(n, mant, scale_factors[0][i]);
448 s->sb_samples[0][j][i] = v;
449 v = l1_unscale(n, mant, scale_factors[1][i]);
450 s->sb_samples[1][j][i] = v;
451 } else {
452 s->sb_samples[0][j][i] = 0;
453 s->sb_samples[1][j][i] = 0;
454 }
455 }
456 }
457 return 12;
458 }
459
460 7327 static int mp_decode_layer2(MPADecodeContext *s)
461 {
462 int sblimit; /* number of used subbands */
463 const unsigned char *alloc_table;
464 int table, bit_alloc_bits, i, j, ch, bound, v;
465 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
466 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
467 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
468 int scale, qindex, bits, steps, k, l, m, b;
469 int ret;
470
471 /* select decoding table */
472 7327 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
473 s->sample_rate, s->lsf);
474 7327 sblimit = ff_mpa_sblimit_table[table];
475 7327 alloc_table = ff_mpa_alloc_tables[table];
476
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7327 times.
7327 if (s->mode == MPA_JSTEREO)
478 bound = (s->mode_ext + 1) * 4;
479 else
480 7327 bound = sblimit;
481
482 ff_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
483
484 /* sanity check */
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7327 times.
7327 if (bound > sblimit)
486 bound = sblimit;
487
488 /* parse bit allocation */
489 7327 j = 0;
490
2/2
✓ Branch 0 taken 217062 times.
✓ Branch 1 taken 7327 times.
224389 for (i = 0; i < bound; i++) {
491 217062 bit_alloc_bits = alloc_table[j];
492
2/2
✓ Branch 0 taken 237603 times.
✓ Branch 1 taken 217062 times.
454665 for (ch = 0; ch < s->nb_channels; ch++)
493 237603 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
494 217062 j += 1 << bit_alloc_bits;
495 }
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7327 times.
7327 for (i = bound; i < sblimit; i++) {
497 bit_alloc_bits = alloc_table[j];
498 v = get_bits(&s->gb, bit_alloc_bits);
499 bit_alloc[0][i] = v;
500 bit_alloc[1][i] = v;
501 j += 1 << bit_alloc_bits;
502 }
503
504 /* scale codes */
505
2/2
✓ Branch 0 taken 217062 times.
✓ Branch 1 taken 7327 times.
224389 for (i = 0; i < sblimit; i++) {
506
2/2
✓ Branch 0 taken 237603 times.
✓ Branch 1 taken 217062 times.
454665 for (ch = 0; ch < s->nb_channels; ch++) {
507
2/2
✓ Branch 0 taken 165128 times.
✓ Branch 1 taken 72475 times.
237603 if (bit_alloc[ch][i])
508 165128 scale_code[ch][i] = get_bits(&s->gb, 2);
509 }
510 }
511
512 7327 ret = handle_crc(s, get_bits_count(&s->gb) - 16);
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7327 times.
7327 if (ret < 0)
514 return ret;
515
516 /* scale factors */
517
2/2
✓ Branch 0 taken 217062 times.
✓ Branch 1 taken 7327 times.
224389 for (i = 0; i < sblimit; i++) {
518
2/2
✓ Branch 0 taken 237603 times.
✓ Branch 1 taken 217062 times.
454665 for (ch = 0; ch < s->nb_channels; ch++) {
519
2/2
✓ Branch 0 taken 165128 times.
✓ Branch 1 taken 72475 times.
237603 if (bit_alloc[ch][i]) {
520 165128 sf = scale_factors[ch][i];
521
4/4
✓ Branch 0 taken 2940 times.
✓ Branch 1 taken 154617 times.
✓ Branch 2 taken 2542 times.
✓ Branch 3 taken 5029 times.
165128 switch (scale_code[ch][i]) {
522 2940 default:
523 case 0:
524 2940 sf[0] = get_bits(&s->gb, 6);
525 2940 sf[1] = get_bits(&s->gb, 6);
526 2940 sf[2] = get_bits(&s->gb, 6);
527 2940 break;
528 154617 case 2:
529 154617 sf[0] = get_bits(&s->gb, 6);
530 154617 sf[1] = sf[0];
531 154617 sf[2] = sf[0];
532 154617 break;
533 2542 case 1:
534 2542 sf[0] = get_bits(&s->gb, 6);
535 2542 sf[2] = get_bits(&s->gb, 6);
536 2542 sf[1] = sf[0];
537 2542 break;
538 5029 case 3:
539 5029 sf[0] = get_bits(&s->gb, 6);
540 5029 sf[2] = get_bits(&s->gb, 6);
541 5029 sf[1] = sf[2];
542 5029 break;
543 }
544 }
545 }
546 }
547
548 /* samples */
549
2/2
✓ Branch 0 taken 21981 times.
✓ Branch 1 taken 7327 times.
29308 for (k = 0; k < 3; k++) {
550
2/2
✓ Branch 0 taken 87924 times.
✓ Branch 1 taken 21981 times.
109905 for (l = 0; l < 12; l += 3) {
551 87924 j = 0;
552
2/2
✓ Branch 0 taken 2604744 times.
✓ Branch 1 taken 87924 times.
2692668 for (i = 0; i < bound; i++) {
553 2604744 bit_alloc_bits = alloc_table[j];
554
2/2
✓ Branch 0 taken 2851236 times.
✓ Branch 1 taken 2604744 times.
5455980 for (ch = 0; ch < s->nb_channels; ch++) {
555 2851236 b = bit_alloc[ch][i];
556
2/2
✓ Branch 0 taken 1981536 times.
✓ Branch 1 taken 869700 times.
2851236 if (b) {
557 1981536 scale = scale_factors[ch][i][k];
558 1981536 qindex = alloc_table[j+b];
559 1981536 bits = ff_mpa_quant_bits[qindex];
560
2/2
✓ Branch 0 taken 302952 times.
✓ Branch 1 taken 1678584 times.
1981536 if (bits < 0) {
561 int v2;
562 /* 3 values at the same time */
563 302952 v = get_bits(&s->gb, -bits);
564 302952 v2 = ff_division_tabs[qindex][v];
565 302952 steps = ff_mpa_quant_steps[qindex];
566
567 603624 s->sb_samples[ch][k * 12 + l + 0][i] =
568 302952 l2_unscale_group(steps, v2 & 15, scale);
569 603624 s->sb_samples[ch][k * 12 + l + 1][i] =
570 302952 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
571 302952 s->sb_samples[ch][k * 12 + l + 2][i] =
572 302952 l2_unscale_group(steps, v2 >> 8 , scale);
573 } else {
574
2/2
✓ Branch 0 taken 5035752 times.
✓ Branch 1 taken 1678584 times.
6714336 for (m = 0; m < 3; m++) {
575 5035752 v = get_bits(&s->gb, bits);
576 5035752 v = l1_unscale(bits - 1, v, scale);
577 5035752 s->sb_samples[ch][k * 12 + l + m][i] = v;
578 }
579 }
580 } else {
581 869700 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
582 869700 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
583 869700 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
584 }
585 }
586 /* next subband in alloc table */
587 2604744 j += 1 << bit_alloc_bits;
588 }
589 /* XXX: find a way to avoid this duplication of code */
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87924 times.
87924 for (i = bound; i < sblimit; i++) {
591 bit_alloc_bits = alloc_table[j];
592 b = bit_alloc[0][i];
593 if (b) {
594 int mant, scale0, scale1;
595 scale0 = scale_factors[0][i][k];
596 scale1 = scale_factors[1][i][k];
597 qindex = alloc_table[j + b];
598 bits = ff_mpa_quant_bits[qindex];
599 if (bits < 0) {
600 /* 3 values at the same time */
601 v = get_bits(&s->gb, -bits);
602 steps = ff_mpa_quant_steps[qindex];
603 mant = v % steps;
604 v = v / steps;
605 s->sb_samples[0][k * 12 + l + 0][i] =
606 l2_unscale_group(steps, mant, scale0);
607 s->sb_samples[1][k * 12 + l + 0][i] =
608 l2_unscale_group(steps, mant, scale1);
609 mant = v % steps;
610 v = v / steps;
611 s->sb_samples[0][k * 12 + l + 1][i] =
612 l2_unscale_group(steps, mant, scale0);
613 s->sb_samples[1][k * 12 + l + 1][i] =
614 l2_unscale_group(steps, mant, scale1);
615 s->sb_samples[0][k * 12 + l + 2][i] =
616 l2_unscale_group(steps, v, scale0);
617 s->sb_samples[1][k * 12 + l + 2][i] =
618 l2_unscale_group(steps, v, scale1);
619 } else {
620 for (m = 0; m < 3; m++) {
621 mant = get_bits(&s->gb, bits);
622 s->sb_samples[0][k * 12 + l + m][i] =
623 l1_unscale(bits - 1, mant, scale0);
624 s->sb_samples[1][k * 12 + l + m][i] =
625 l1_unscale(bits - 1, mant, scale1);
626 }
627 }
628 } else {
629 s->sb_samples[0][k * 12 + l + 0][i] = 0;
630 s->sb_samples[0][k * 12 + l + 1][i] = 0;
631 s->sb_samples[0][k * 12 + l + 2][i] = 0;
632 s->sb_samples[1][k * 12 + l + 0][i] = 0;
633 s->sb_samples[1][k * 12 + l + 1][i] = 0;
634 s->sb_samples[1][k * 12 + l + 2][i] = 0;
635 }
636 /* next subband in alloc table */
637 j += 1 << bit_alloc_bits;
638 }
639 /* fill remaining samples to zero */
640
2/2
✓ Branch 0 taken 208824 times.
✓ Branch 1 taken 87924 times.
296748 for (i = sblimit; i < SBLIMIT; i++) {
641
2/2
✓ Branch 0 taken 244572 times.
✓ Branch 1 taken 208824 times.
453396 for (ch = 0; ch < s->nb_channels; ch++) {
642 244572 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
643 244572 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
644 244572 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
645 }
646 }
647 }
648 }
649 7327 return 3 * 12;
650 }
651
652 #define SPLIT(dst,sf,n) \
653 if (n == 3) { \
654 int m = (sf * 171) >> 9; \
655 dst = sf - 3 * m; \
656 sf = m; \
657 } else if (n == 4) { \
658 dst = sf & 3; \
659 sf >>= 2; \
660 } else if (n == 5) { \
661 int m = (sf * 205) >> 10; \
662 dst = sf - 5 * m; \
663 sf = m; \
664 } else if (n == 6) { \
665 int m = (sf * 171) >> 10; \
666 dst = sf - 6 * m; \
667 sf = m; \
668 } else { \
669 dst = 0; \
670 }
671
672 4 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
673 int n3)
674 {
675
5/8
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
4 SPLIT(slen[3], sf, n3)
676
5/8
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
4 SPLIT(slen[2], sf, n2)
677
5/8
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
4 SPLIT(slen[1], sf, n1)
678 4 slen[0] = sf;
679 4 }
680
681 8134 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
682 int16_t *exponents)
683 {
684 const uint8_t *bstab, *pretab;
685 int len, i, j, k, l, v0, shift, gain, gains[3];
686 int16_t *exp_ptr;
687
688 8134 exp_ptr = exponents;
689 8134 gain = g->global_gain - 210;
690 8134 shift = g->scalefac_scale + 1;
691
692 8134 bstab = ff_band_size_long[s->sample_rate_index];
693 8134 pretab = ff_mpa_pretab[g->preflag];
694
2/2
✓ Branch 0 taken 172254 times.
✓ Branch 1 taken 8134 times.
180388 for (i = 0; i < g->long_end; i++) {
695 172254 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
696 172254 len = bstab[i];
697
2/2
✓ Branch 0 taken 4507668 times.
✓ Branch 1 taken 172254 times.
4679922 for (j = len; j > 0; j--)
698 4507668 *exp_ptr++ = v0;
699 }
700
701
2/2
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 7825 times.
8134 if (g->short_start < 13) {
702 309 bstab = ff_band_size_short[s->sample_rate_index];
703 309 gains[0] = gain - (g->subblock_gain[0] << 3);
704 309 gains[1] = gain - (g->subblock_gain[1] << 3);
705 309 gains[2] = gain - (g->subblock_gain[2] << 3);
706 309 k = g->long_end;
707
2/2
✓ Branch 0 taken 3978 times.
✓ Branch 1 taken 309 times.
4287 for (i = g->short_start; i < 13; i++) {
708 3978 len = bstab[i];
709
2/2
✓ Branch 0 taken 11934 times.
✓ Branch 1 taken 3978 times.
15912 for (l = 0; l < 3; l++) {
710 11934 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
711
2/2
✓ Branch 0 taken 177516 times.
✓ Branch 1 taken 11934 times.
189450 for (j = len; j > 0; j--)
712 177516 *exp_ptr++ = v0;
713 }
714 }
715 }
716 8134 }
717
718 16843 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
719 int *end_pos2)
720 {
721
4/4
✓ Branch 0 taken 13826 times.
✓ Branch 1 taken 3017 times.
✓ Branch 2 taken 939 times.
✓ Branch 3 taken 12887 times.
16843 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits - s->extrasize * 8) {
722 939 s->gb = s->in_gb;
723 939 s->in_gb.buffer = NULL;
724 939 s->extrasize = 0;
725 av_assert2((get_bits_count(&s->gb) & 7) == 0);
726 939 skip_bits_long(&s->gb, *pos - *end_pos);
727 939 *end_pos2 =
728 939 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
729 939 *pos = get_bits_count(&s->gb);
730 }
731 16843 }
732
733 /* Following is an optimized code for
734 INTFLOAT v = *src
735 if(get_bits1(&s->gb))
736 v = -v;
737 *dst = v;
738 */
739 #if USE_FLOATS
740 #define READ_FLIP_SIGN(dst,src) \
741 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
742 AV_WN32A(dst, v);
743 #else
744 #define READ_FLIP_SIGN(dst,src) \
745 v = -get_bits1(&s->gb); \
746 *(dst) = (*(src) ^ v) - v;
747 #endif
748
749 8134 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
750 int16_t *exponents, int end_pos2)
751 {
752 int s_index;
753 int i;
754 int last_pos, bits_left;
755 VLC *vlc;
756 8134 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits - s->extrasize * 8);
757
758 /* low frequencies (called big values) */
759 8134 s_index = 0;
760
2/2
✓ Branch 0 taken 24402 times.
✓ Branch 1 taken 8134 times.
32536 for (i = 0; i < 3; i++) {
761 int j, k, l, linbits;
762 24402 j = g->region_size[i];
763
2/2
✓ Branch 0 taken 1341 times.
✓ Branch 1 taken 23061 times.
24402 if (j == 0)
764 1341 continue;
765 /* select vlc table */
766 23061 k = g->table_select[i];
767 23061 l = ff_mpa_huff_data[k][0];
768 23061 linbits = ff_mpa_huff_data[k][1];
769 23061 vlc = &ff_huff_vlc[l];
770
771
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 22969 times.
23061 if (!l) {
772 92 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
773 92 s_index += 2 * j;
774 92 continue;
775 }
776
777 /* read huffcode and compute each couple */
778
2/2
✓ Branch 0 taken 887510 times.
✓ Branch 1 taken 22969 times.
910479 for (; j > 0; j--) {
779 int exponent, x, y;
780 int v;
781 887510 int pos = get_bits_count(&s->gb);
782
783
2/2
✓ Branch 0 taken 789 times.
✓ Branch 1 taken 886721 times.
887510 if (pos >= end_pos){
784 789 switch_buffer(s, &pos, &end_pos, &end_pos2);
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 789 times.
789 if (pos >= end_pos)
786 break;
787 }
788 887510 y = get_vlc2(&s->gb, vlc->table, 7, 3);
789
790
2/2
✓ Branch 0 taken 216065 times.
✓ Branch 1 taken 671445 times.
887510 if (!y) {
791 216065 g->sb_hybrid[s_index ] =
792 216065 g->sb_hybrid[s_index + 1] = 0;
793 216065 s_index += 2;
794 216065 continue;
795 }
796
797 671445 exponent= exponents[s_index];
798
799 ff_dlog(s->avctx, "region=%d n=%d y=%d exp=%d\n",
800 i, g->region_size[i] - j, y, exponent);
801
2/2
✓ Branch 0 taken 426550 times.
✓ Branch 1 taken 244895 times.
671445 if (y & 16) {
802 426550 x = y >> 5;
803 426550 y = y & 0x0f;
804
2/2
✓ Branch 0 taken 400602 times.
✓ Branch 1 taken 25948 times.
426550 if (x < 15) {
805 400602 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
806 } else {
807 25948 x += get_bitsz(&s->gb, linbits);
808 25948 v = l3_unscale(x, exponent);
809
2/2
✓ Branch 1 taken 12440 times.
✓ Branch 2 taken 13508 times.
25948 if (get_bits1(&s->gb))
810 12440 v = -v;
811 25948 g->sb_hybrid[s_index] = v;
812 }
813
2/2
✓ Branch 0 taken 401568 times.
✓ Branch 1 taken 24982 times.
426550 if (y < 15) {
814 401568 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
815 } else {
816 24982 y += get_bitsz(&s->gb, linbits);
817 24982 v = l3_unscale(y, exponent);
818
2/2
✓ Branch 1 taken 11889 times.
✓ Branch 2 taken 13093 times.
24982 if (get_bits1(&s->gb))
819 11889 v = -v;
820 24982 g->sb_hybrid[s_index + 1] = v;
821 }
822 } else {
823 244895 x = y >> 5;
824 244895 y = y & 0x0f;
825 244895 x += y;
826
2/2
✓ Branch 0 taken 239090 times.
✓ Branch 1 taken 5805 times.
244895 if (x < 15) {
827
2/2
✓ Branch 1 taken 117587 times.
✓ Branch 2 taken 121503 times.
239090 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
828 } else {
829 5805 x += get_bitsz(&s->gb, linbits);
830 5805 v = l3_unscale(x, exponent);
831
2/2
✓ Branch 1 taken 2114 times.
✓ Branch 2 taken 3691 times.
5805 if (get_bits1(&s->gb))
832 2114 v = -v;
833 5805 g->sb_hybrid[s_index+!!y] = v;
834 }
835 244895 g->sb_hybrid[s_index + !y] = 0;
836 }
837 671445 s_index += 2;
838 }
839 }
840
841 /* high frequencies */
842 8134 vlc = &ff_huff_quad_vlc[g->count1table_select];
843 8134 last_pos = 0;
844
2/2
✓ Branch 0 taken 226912 times.
✓ Branch 1 taken 301 times.
227213 while (s_index <= 572) {
845 int pos, code;
846 226912 pos = get_bits_count(&s->gb);
847
2/2
✓ Branch 0 taken 7920 times.
✓ Branch 1 taken 218992 times.
226912 if (pos >= end_pos) {
848
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7920 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7920 if (pos > end_pos2 && last_pos) {
849 /* some encoders generate an incorrect size for this
850 part. We must go back into the data */
851 s_index -= 4;
852 skip_bits_long(&s->gb, last_pos - pos);
853 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
854 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
855 s_index=0;
856 7833 break;
857 }
858 7920 switch_buffer(s, &pos, &end_pos, &end_pos2);
859
2/2
✓ Branch 0 taken 7833 times.
✓ Branch 1 taken 87 times.
7920 if (pos >= end_pos)
860 7833 break;
861 }
862 219079 last_pos = pos;
863
864 219079 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
865 ff_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
866 219079 g->sb_hybrid[s_index + 0] =
867 219079 g->sb_hybrid[s_index + 1] =
868 219079 g->sb_hybrid[s_index + 2] =
869 219079 g->sb_hybrid[s_index + 3] = 0;
870
2/2
✓ Branch 0 taken 148921 times.
✓ Branch 1 taken 219079 times.
368000 while (code) {
871 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
872 int v;
873 148921 int pos = s_index + idxtab[code];
874 148921 code ^= 8 >> idxtab[code];
875 148921 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
876 }
877 219079 s_index += 4;
878 }
879 /* skip extension bits */
880 8134 bits_left = end_pos2 - get_bits_count(&s->gb);
881
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8134 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8134 if (bits_left < 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_COMPLIANT))) {
882 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
883 s_index=0;
884
3/4
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 7836 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 298 times.
8134 } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE))) {
885 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
886 s_index = 0;
887 }
888 8134 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
889 8134 skip_bits_long(&s->gb, bits_left);
890
891 8134 i = get_bits_count(&s->gb);
892 8134 switch_buffer(s, &i, &end_pos, &end_pos2);
893
894 8134 return 0;
895 }
896
897 /* Reorder short blocks from bitstream order to interleaved order. It
898 would be faster to do it in parsing, but the code would be far more
899 complicated */
900 8134 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
901 {
902 int i, j, len;
903 INTFLOAT *ptr, *dst, *ptr1;
904 INTFLOAT tmp[576];
905
906
2/2
✓ Branch 0 taken 7825 times.
✓ Branch 1 taken 309 times.
8134 if (g->block_type != 2)
907 7825 return;
908
909
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 296 times.
309 if (g->switch_point) {
910
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (s->sample_rate_index != 8)
911 13 ptr = g->sb_hybrid + 36;
912 else
913 ptr = g->sb_hybrid + 72;
914 } else {
915 296 ptr = g->sb_hybrid;
916 }
917
918
2/2
✓ Branch 0 taken 3978 times.
✓ Branch 1 taken 309 times.
4287 for (i = g->short_start; i < 13; i++) {
919 3978 len = ff_band_size_short[s->sample_rate_index][i];
920 3978 ptr1 = ptr;
921 3978 dst = tmp;
922
2/2
✓ Branch 0 taken 59172 times.
✓ Branch 1 taken 3978 times.
63150 for (j = len; j > 0; j--) {
923 59172 *dst++ = ptr[0*len];
924 59172 *dst++ = ptr[1*len];
925 59172 *dst++ = ptr[2*len];
926 59172 ptr++;
927 }
928 3978 ptr += 2 * len;
929 3978 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
930 }
931 }
932
933 #define ISQRT2 FIXR(0.70710678118654752440)
934
935 2455 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
936 {
937 int i, j, k, l;
938 int sf_max, sf, len, non_zero_found;
939 INTFLOAT *tab0, *tab1, v1, v2;
940 const INTFLOAT (*is_tab)[16];
941 SUINTFLOAT tmp0, tmp1;
942 int non_zero_found_short[3];
943
944 /* intensity stereo */
945
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2454 times.
2455 if (s->mode_ext & MODE_EXT_I_STEREO) {
946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->lsf) {
947 is_tab = is_table;
948 sf_max = 7;
949 } else {
950 1 is_tab = is_table_lsf[g1->scalefac_compress & 1];
951 1 sf_max = 16;
952 }
953
954 1 tab0 = g0->sb_hybrid + 576;
955 1 tab1 = g1->sb_hybrid + 576;
956
957 1 non_zero_found_short[0] = 0;
958 1 non_zero_found_short[1] = 0;
959 1 non_zero_found_short[2] = 0;
960 1 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
961
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (i = 12; i >= g1->short_start; i--) {
962 /* for last band, use previous scale factor */
963 if (i != 11)
964 k -= 3;
965 len = ff_band_size_short[s->sample_rate_index][i];
966 for (l = 2; l >= 0; l--) {
967 tab0 -= len;
968 tab1 -= len;
969 if (!non_zero_found_short[l]) {
970 /* test if non zero band. if so, stop doing i-stereo */
971 for (j = 0; j < len; j++) {
972 if (tab1[j] != 0) {
973 non_zero_found_short[l] = 1;
974 goto found1;
975 }
976 }
977 sf = g1->scale_factors[k + l];
978 if (sf >= sf_max)
979 goto found1;
980
981 v1 = is_tab[0][sf];
982 v2 = is_tab[1][sf];
983 for (j = 0; j < len; j++) {
984 tmp0 = tab0[j];
985 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
986 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
987 }
988 } else {
989 found1:
990 if (s->mode_ext & MODE_EXT_MS_STEREO) {
991 /* lower part of the spectrum : do ms stereo
992 if enabled */
993 for (j = 0; j < len; j++) {
994 tmp0 = tab0[j];
995 tmp1 = tab1[j];
996 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
997 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
998 }
999 }
1000 }
1001 }
1002 }
1003
1004 1 non_zero_found = non_zero_found_short[0] |
1005 1 non_zero_found_short[1] |
1006 1 non_zero_found_short[2];
1007
1008
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1 times.
23 for (i = g1->long_end - 1;i >= 0;i--) {
1009 22 len = ff_band_size_long[s->sample_rate_index][i];
1010 22 tab0 -= len;
1011 22 tab1 -= len;
1012 /* test if non zero band. if so, stop doing i-stereo */
1013
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (!non_zero_found) {
1014
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 22 times.
598 for (j = 0; j < len; j++) {
1015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 576 times.
576 if (tab1[j] != 0) {
1016 non_zero_found = 1;
1017 goto found2;
1018 }
1019 }
1020 /* for last band, use previous scale factor */
1021
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1 times.
22 k = (i == 21) ? 20 : i;
1022 22 sf = g1->scale_factors[k];
1023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (sf >= sf_max)
1024 goto found2;
1025 22 v1 = is_tab[0][sf];
1026 22 v2 = is_tab[1][sf];
1027
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 22 times.
598 for (j = 0; j < len; j++) {
1028 576 tmp0 = tab0[j];
1029 576 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1030 576 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1031 }
1032 } else {
1033 found2:
1034 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1035 /* lower part of the spectrum : do ms stereo
1036 if enabled */
1037 for (j = 0; j < len; j++) {
1038 tmp0 = tab0[j];
1039 tmp1 = tab1[j];
1040 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1041 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1042 }
1043 }
1044 }
1045 }
1046
2/2
✓ Branch 0 taken 2234 times.
✓ Branch 1 taken 220 times.
2454 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1047 /* ms stereo ONLY */
1048 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1049 global gain */
1050 #if USE_FLOATS
1051 58 s->butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
1052 #else
1053 2176 tab0 = g0->sb_hybrid;
1054 2176 tab1 = g1->sb_hybrid;
1055
2/2
✓ Branch 0 taken 1253376 times.
✓ Branch 1 taken 2176 times.
1255552 for (i = 0; i < 576; i++) {
1056 1253376 tmp0 = tab0[i];
1057 1253376 tmp1 = tab1[i];
1058 1253376 tab0[i] = tmp0 + tmp1;
1059 1253376 tab1[i] = tmp0 - tmp1;
1060 }
1061 #endif
1062 }
1063 2455 }
1064
1065 #if USE_FLOATS
1066 #if HAVE_MIPSFPU
1067 # include "mips/compute_antialias_float.h"
1068 #endif /* HAVE_MIPSFPU */
1069 #else
1070 #if HAVE_MIPSDSP
1071 # include "mips/compute_antialias_fixed.h"
1072 #endif /* HAVE_MIPSDSP */
1073 #endif /* USE_FLOATS */
1074
1075 #ifndef compute_antialias
1076 #if USE_FLOATS
1077 #define AA(j) do { \
1078 float tmp0 = ptr[-1-j]; \
1079 float tmp1 = ptr[ j]; \
1080 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1081 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1082 } while (0)
1083 #else
1084 #define AA(j) do { \
1085 SUINT tmp0 = ptr[-1-j]; \
1086 SUINT tmp1 = ptr[ j]; \
1087 SUINT tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1088 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1089 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1090 } while (0)
1091 #endif
1092
1093 8134 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1094 {
1095 INTFLOAT *ptr;
1096 int n, i;
1097
1098 /* we antialias only "long" bands */
1099
2/2
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 7825 times.
8134 if (g->block_type == 2) {
1100
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 13 times.
309 if (!g->switch_point)
1101 296 return;
1102 /* XXX: check this for 8000Hz case */
1103 13 n = 1;
1104 } else {
1105 7825 n = SBLIMIT - 1;
1106 }
1107
1108 7838 ptr = g->sb_hybrid + 18;
1109
2/2
✓ Branch 0 taken 242588 times.
✓ Branch 1 taken 7838 times.
250426 for (i = n; i > 0; i--) {
1110 242588 AA(0);
1111 242588 AA(1);
1112 242588 AA(2);
1113 242588 AA(3);
1114 242588 AA(4);
1115 242588 AA(5);
1116 242588 AA(6);
1117 242588 AA(7);
1118
1119 242588 ptr += 18;
1120 }
1121 }
1122 #endif /* compute_antialias */
1123
1124 8138 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1125 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1126 {
1127 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1128 INTFLOAT out2[12];
1129 int i, j, mdct_long_end, sblimit;
1130
1131 /* find last non zero block */
1132 8138 ptr = g->sb_hybrid + 576;
1133 8138 ptr1 = g->sb_hybrid + 2 * 18;
1134
2/2
✓ Branch 0 taken 313251 times.
✓ Branch 1 taken 194 times.
313445 while (ptr >= ptr1) {
1135 int32_t *p;
1136 313251 ptr -= 6;
1137 313251 p = (int32_t*)ptr;
1138
2/2
✓ Branch 0 taken 7944 times.
✓ Branch 1 taken 305307 times.
313251 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1139 7944 break;
1140 }
1141 8138 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1142
1143
2/2
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 7828 times.
8138 if (g->block_type == 2) {
1144 /* XXX: check for 8000 Hz */
1145
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 297 times.
310 if (g->switch_point)
1146 13 mdct_long_end = 2;
1147 else
1148 297 mdct_long_end = 0;
1149 } else {
1150 7828 mdct_long_end = sblimit;
1151 }
1152
1153 8138 s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
1154 8138 mdct_long_end, g->switch_point,
1155 8138 g->block_type);
1156
1157 8138 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1158 8138 ptr = g->sb_hybrid + 18 * mdct_long_end;
1159
1160
2/2
✓ Branch 0 taken 6564 times.
✓ Branch 1 taken 8138 times.
14702 for (j = mdct_long_end; j < sblimit; j++) {
1161 /* select frequency inversion */
1162 6564 win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1163 6564 out_ptr = sb_samples + j;
1164
1165
2/2
✓ Branch 0 taken 39384 times.
✓ Branch 1 taken 6564 times.
45948 for (i = 0; i < 6; i++) {
1166 39384 *out_ptr = buf[4*i];
1167 39384 out_ptr += SBLIMIT;
1168 }
1169 6564 imdct12(out2, ptr + 0);
1170
2/2
✓ Branch 0 taken 39384 times.
✓ Branch 1 taken 6564 times.
45948 for (i = 0; i < 6; i++) {
1171 39384 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1172 39384 buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
1173 39384 out_ptr += SBLIMIT;
1174 }
1175 6564 imdct12(out2, ptr + 1);
1176
2/2
✓ Branch 0 taken 39384 times.
✓ Branch 1 taken 6564 times.
45948 for (i = 0; i < 6; i++) {
1177 39384 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1178 39384 buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
1179 39384 out_ptr += SBLIMIT;
1180 }
1181 6564 imdct12(out2, ptr + 2);
1182
2/2
✓ Branch 0 taken 39384 times.
✓ Branch 1 taken 6564 times.
45948 for (i = 0; i < 6; i++) {
1183 39384 buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1184 39384 buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
1185 39384 buf[4*(i + 6*2)] = 0;
1186 }
1187 6564 ptr += 18;
1188
2/2
✓ Branch 0 taken 5058 times.
✓ Branch 1 taken 1506 times.
6564 buf += (j&3) != 3 ? 1 : (4*18-3);
1189 }
1190 /* zero bands */
1191
2/2
✓ Branch 0 taken 98027 times.
✓ Branch 1 taken 8138 times.
106165 for (j = sblimit; j < SBLIMIT; j++) {
1192 /* overlap */
1193 98027 out_ptr = sb_samples + j;
1194
2/2
✓ Branch 0 taken 1764486 times.
✓ Branch 1 taken 98027 times.
1862513 for (i = 0; i < 18; i++) {
1195 1764486 *out_ptr = buf[4*i];
1196 1764486 buf[4*i] = 0;
1197 1764486 out_ptr += SBLIMIT;
1198 }
1199
2/2
✓ Branch 0 taken 69848 times.
✓ Branch 1 taken 28179 times.
98027 buf += (j&3) != 3 ? 1 : (4*18-3);
1200 }
1201 8138 }
1202
1203 /* main layer3 decoding function */
1204 2810 static int mp_decode_layer3(MPADecodeContext *s)
1205 {
1206 int nb_granules, main_data_begin;
1207 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1208 GranuleDef *g;
1209 int16_t exponents[576]; //FIXME try INTFLOAT
1210 int ret;
1211
1212 /* read side info */
1213
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2807 times.
2810 if (s->lsf) {
1214
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 ret = handle_crc(s, ((s->nb_channels == 1) ? 8*9 : 8*17));
1215 3 main_data_begin = get_bits(&s->gb, 8);
1216 3 skip_bits(&s->gb, s->nb_channels);
1217 3 nb_granules = 1;
1218 } else {
1219
2/2
✓ Branch 0 taken 1547 times.
✓ Branch 1 taken 1260 times.
2807 ret = handle_crc(s, ((s->nb_channels == 1) ? 8*17 : 8*32));
1220 2807 main_data_begin = get_bits(&s->gb, 9);
1221
2/2
✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 1547 times.
2807 if (s->nb_channels == 2)
1222 1260 skip_bits(&s->gb, 3);
1223 else
1224 1547 skip_bits(&s->gb, 5);
1225 2807 nb_granules = 2;
1226
2/2
✓ Branch 0 taken 4067 times.
✓ Branch 1 taken 2807 times.
6874 for (ch = 0; ch < s->nb_channels; ch++) {
1227 4067 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1228 4067 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1229 }
1230 }
1231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2810 times.
2810 if (ret < 0)
1232 return ret;
1233
1234
2/2
✓ Branch 0 taken 5617 times.
✓ Branch 1 taken 2810 times.
8427 for (gr = 0; gr < nb_granules; gr++) {
1235
2/2
✓ Branch 0 taken 8138 times.
✓ Branch 1 taken 5617 times.
13755 for (ch = 0; ch < s->nb_channels; ch++) {
1236 ff_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1237 8138 g = &s->granules[ch][gr];
1238 8138 g->part2_3_length = get_bits(&s->gb, 12);
1239 8138 g->big_values = get_bits(&s->gb, 9);
1240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8138 times.
8138 if (g->big_values > 288) {
1241 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1242 return AVERROR_INVALIDDATA;
1243 }
1244
1245 8138 g->global_gain = get_bits(&s->gb, 8);
1246 /* if MS stereo only is selected, we precompute the
1247 1/sqrt(2) renormalization factor */
1248
2/2
✓ Branch 0 taken 4468 times.
✓ Branch 1 taken 3670 times.
8138 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1249 MODE_EXT_MS_STEREO)
1250 4468 g->global_gain -= 2;
1251
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8134 times.
8138 if (s->lsf)
1252 4 g->scalefac_compress = get_bits(&s->gb, 9);
1253 else
1254 8134 g->scalefac_compress = get_bits(&s->gb, 4);
1255 8138 blocksplit_flag = get_bits1(&s->gb);
1256
2/2
✓ Branch 0 taken 855 times.
✓ Branch 1 taken 7283 times.
8138 if (blocksplit_flag) {
1257 855 g->block_type = get_bits(&s->gb, 2);
1258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 855 times.
855 if (g->block_type == 0) {
1259 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1260 return AVERROR_INVALIDDATA;
1261 }
1262 855 g->switch_point = get_bits1(&s->gb);
1263
2/2
✓ Branch 0 taken 1710 times.
✓ Branch 1 taken 855 times.
2565 for (i = 0; i < 2; i++)
1264 1710 g->table_select[i] = get_bits(&s->gb, 5);
1265
2/2
✓ Branch 0 taken 2565 times.
✓ Branch 1 taken 855 times.
3420 for (i = 0; i < 3; i++)
1266 2565 g->subblock_gain[i] = get_bits(&s->gb, 3);
1267 855 init_short_region(s, g);
1268 } else {
1269 int region_address1, region_address2;
1270 7283 g->block_type = 0;
1271 7283 g->switch_point = 0;
1272
2/2
✓ Branch 0 taken 21849 times.
✓ Branch 1 taken 7283 times.
29132 for (i = 0; i < 3; i++)
1273 21849 g->table_select[i] = get_bits(&s->gb, 5);
1274 /* compute huffman coded region sizes */
1275 7283 region_address1 = get_bits(&s->gb, 4);
1276 7283 region_address2 = get_bits(&s->gb, 3);
1277 ff_dlog(s->avctx, "region1=%d region2=%d\n",
1278 region_address1, region_address2);
1279 7283 init_long_region(s, g, region_address1, region_address2);
1280 }
1281 8138 region_offset2size(g);
1282 8138 compute_band_indexes(s, g);
1283
1284 8138 g->preflag = 0;
1285
2/2
✓ Branch 0 taken 8134 times.
✓ Branch 1 taken 4 times.
8138 if (!s->lsf)
1286 8134 g->preflag = get_bits1(&s->gb);
1287 8138 g->scalefac_scale = get_bits1(&s->gb);
1288 8138 g->count1table_select = get_bits1(&s->gb);
1289 ff_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1290 g->block_type, g->switch_point);
1291 }
1292 }
1293
1294
1/2
✓ Branch 0 taken 2810 times.
✗ Branch 1 not taken.
2810 if (!s->adu_mode) {
1295 int skip;
1296 2810 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb) >> 3);
1297 2810 s->extrasize = av_clip((get_bits_left(&s->gb) >> 3) - s->extrasize, 0,
1298 2810 FFMAX(0, LAST_BUF_SIZE - s->last_buf_size));
1299 av_assert1((get_bits_count(&s->gb) & 7) == 0);
1300 /* now we get bits from the main_data_begin offset */
1301 ff_dlog(s->avctx, "seekback:%d, lastbuf:%d\n",
1302 main_data_begin, s->last_buf_size);
1303
1304 2810 memcpy(s->last_buf + s->last_buf_size, ptr, s->extrasize);
1305 2810 s->in_gb = s->gb;
1306 2810 init_get_bits(&s->gb, s->last_buf, (s->last_buf_size + s->extrasize) * 8);
1307 2810 s->last_buf_size <<= 3;
1308
4/4
✓ Branch 0 taken 2813 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2809 times.
2814 for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
1309
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (ch = 0; ch < s->nb_channels; ch++) {
1310 4 g = &s->granules[ch][gr];
1311 4 s->last_buf_size += g->part2_3_length;
1312 4 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1313 4 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1314 }
1315 }
1316 2810 skip = s->last_buf_size - 8 * main_data_begin;
1317
3/4
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 2658 times.
✓ Branch 2 taken 152 times.
✗ Branch 3 not taken.
2810 if (skip >= s->gb.size_in_bits - s->extrasize * 8 && s->in_gb.buffer) {
1318 152 skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits + s->extrasize * 8);
1319 152 s->gb = s->in_gb;
1320 152 s->in_gb.buffer = NULL;
1321 152 s->extrasize = 0;
1322 } else {
1323 2658 skip_bits_long(&s->gb, skip);
1324 }
1325 } else {
1326 gr = 0;
1327 s->extrasize = 0;
1328 }
1329
1330
2/2
✓ Branch 0 taken 5613 times.
✓ Branch 1 taken 2810 times.
8423 for (; gr < nb_granules; gr++) {
1331
2/2
✓ Branch 0 taken 8134 times.
✓ Branch 1 taken 5613 times.
13747 for (ch = 0; ch < s->nb_channels; ch++) {
1332 8134 g = &s->granules[ch][gr];
1333 8134 bits_pos = get_bits_count(&s->gb);
1334
1335
2/2
✓ Branch 0 taken 8130 times.
✓ Branch 1 taken 4 times.
8134 if (!s->lsf) {
1336 uint8_t *sc;
1337 int slen, slen1, slen2;
1338
1339 /* MPEG-1 scale factors */
1340 8130 slen1 = ff_slen_table[0][g->scalefac_compress];
1341 8130 slen2 = ff_slen_table[1][g->scalefac_compress];
1342 ff_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1343
2/2
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 7821 times.
8130 if (g->block_type == 2) {
1344
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 296 times.
309 n = g->switch_point ? 17 : 18;
1345 309 j = 0;
1346
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 266 times.
309 if (slen1) {
1347
2/2
✓ Branch 0 taken 770 times.
✓ Branch 1 taken 43 times.
813 for (i = 0; i < n; i++)
1348 770 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1349 } else {
1350
2/2
✓ Branch 0 taken 4779 times.
✓ Branch 1 taken 266 times.
5045 for (i = 0; i < n; i++)
1351 4779 g->scale_factors[j++] = 0;
1352 }
1353
2/2
✓ Branch 0 taken 257 times.
✓ Branch 1 taken 52 times.
309 if (slen2) {
1354
2/2
✓ Branch 0 taken 4626 times.
✓ Branch 1 taken 257 times.
4883 for (i = 0; i < 18; i++)
1355 4626 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1356
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 257 times.
1028 for (i = 0; i < 3; i++)
1357 771 g->scale_factors[j++] = 0;
1358 } else {
1359
2/2
✓ Branch 0 taken 1092 times.
✓ Branch 1 taken 52 times.
1144 for (i = 0; i < 21; i++)
1360 1092 g->scale_factors[j++] = 0;
1361 }
1362 } else {
1363 7821 sc = s->granules[ch][0].scale_factors;
1364 7821 j = 0;
1365
2/2
✓ Branch 0 taken 31284 times.
✓ Branch 1 taken 7821 times.
39105 for (k = 0; k < 4; k++) {
1366
2/2
✓ Branch 0 taken 7821 times.
✓ Branch 1 taken 23463 times.
31284 n = k == 0 ? 6 : 5;
1367
2/2
✓ Branch 0 taken 26914 times.
✓ Branch 1 taken 4370 times.
31284 if ((g->scfsi & (0x8 >> k)) == 0) {
1368
2/2
✓ Branch 0 taken 12759 times.
✓ Branch 1 taken 14155 times.
26914 slen = (k < 2) ? slen1 : slen2;
1369
2/2
✓ Branch 0 taken 14382 times.
✓ Branch 1 taken 12532 times.
26914 if (slen) {
1370
2/2
✓ Branch 0 taken 74572 times.
✓ Branch 1 taken 14382 times.
88954 for (i = 0; i < n; i++)
1371 74572 g->scale_factors[j++] = get_bits(&s->gb, slen);
1372 } else {
1373
2/2
✓ Branch 0 taken 66404 times.
✓ Branch 1 taken 12532 times.
78936 for (i = 0; i < n; i++)
1374 66404 g->scale_factors[j++] = 0;
1375 }
1376 } else {
1377 /* simply copy from last granule */
1378
2/2
✓ Branch 0 taken 23265 times.
✓ Branch 1 taken 4370 times.
27635 for (i = 0; i < n; i++) {
1379 23265 g->scale_factors[j] = sc[j];
1380 23265 j++;
1381 }
1382 }
1383 }
1384 7821 g->scale_factors[j++] = 0;
1385 }
1386 } else {
1387 int tindex, tindex2, slen[4], sl, sf;
1388
1389 /* LSF scale factors */
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (g->block_type == 2)
1391 tindex = g->switch_point ? 2 : 1;
1392 else
1393 4 tindex = 0;
1394
1395 4 sf = g->scalefac_compress;
1396
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1397 /* intensity stereo case */
1398 1 sf >>= 1;
1399
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (sf < 180) {
1400 1 lsf_sf_expand(slen, sf, 6, 6, 0);
1401 1 tindex2 = 3;
1402 } else if (sf < 244) {
1403 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1404 tindex2 = 4;
1405 } else {
1406 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1407 tindex2 = 5;
1408 }
1409 } else {
1410 /* normal case */
1411
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (sf < 400) {
1412 1 lsf_sf_expand(slen, sf, 5, 4, 4);
1413 1 tindex2 = 0;
1414
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (sf < 500) {
1415 2 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1416 2 tindex2 = 1;
1417 } else {
1418 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1419 tindex2 = 2;
1420 g->preflag = 1;
1421 }
1422 }
1423
1424 4 j = 0;
1425
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (k = 0; k < 4; k++) {
1426 16 n = ff_lsf_nsf_table[tindex2][tindex][k];
1427 16 sl = slen[k];
1428
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
16 if (sl) {
1429
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 6 times.
45 for (i = 0; i < n; i++)
1430 39 g->scale_factors[j++] = get_bits(&s->gb, sl);
1431 } else {
1432
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 10 times.
55 for (i = 0; i < n; i++)
1433 45 g->scale_factors[j++] = 0;
1434 }
1435 }
1436 /* XXX: should compute exact size */
1437
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 4 times.
80 for (; j < 40; j++)
1438 76 g->scale_factors[j] = 0;
1439 }
1440
1441 8134 exponents_from_scale_factors(s, g, exponents);
1442
1443 /* read Huffman coded residue */
1444 8134 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1445 } /* ch */
1446
1447
2/2
✓ Branch 0 taken 2455 times.
✓ Branch 1 taken 3158 times.
5613 if (s->mode == MPA_JSTEREO)
1448 2455 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1449
1450
2/2
✓ Branch 0 taken 8134 times.
✓ Branch 1 taken 5613 times.
13747 for (ch = 0; ch < s->nb_channels; ch++) {
1451 8134 g = &s->granules[ch][gr];
1452
1453 8134 reorder_block(s, g);
1454 8134 compute_antialias(s, g);
1455 8134 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1456 }
1457 } /* gr */
1458
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2810 times.
2810 if (get_bits_count(&s->gb) < 0)
1459 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1460 2810 return nb_granules * 18;
1461 }
1462
1463 10137 static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples,
1464 const uint8_t *buf, int buf_size)
1465 {
1466 int i, nb_frames, ch, ret;
1467 OUT_INT *samples_ptr;
1468
1469 10137 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1470
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 9931 times.
10137 if (s->error_protection)
1471 206 s->crc = get_bits(&s->gb, 16);
1472
1473
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7327 times.
✓ Branch 2 taken 2810 times.
✗ Branch 3 not taken.
10137 switch(s->layer) {
1474 case 1:
1475 s->avctx->frame_size = 384;
1476 nb_frames = mp_decode_layer1(s);
1477 break;
1478 7327 case 2:
1479 7327 s->avctx->frame_size = 1152;
1480 7327 nb_frames = mp_decode_layer2(s);
1481 7327 break;
1482 2810 case 3:
1483
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2807 times.
2810 s->avctx->frame_size = s->lsf ? 576 : 1152;
1484 2810 default:
1485 2810 nb_frames = mp_decode_layer3(s);
1486
1487 2810 s->last_buf_size=0;
1488
2/2
✓ Branch 0 taken 1719 times.
✓ Branch 1 taken 1091 times.
2810 if (s->in_gb.buffer) {
1489 1719 align_get_bits(&s->gb);
1490 1719 i = (get_bits_left(&s->gb) >> 3) - s->extrasize;
1491
2/4
✓ Branch 0 taken 1719 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1719 times.
✗ Branch 3 not taken.
1719 if (i >= 0 && i <= BACKSTEP_SIZE) {
1492 1719 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb) >> 3), i);
1493 1719 s->last_buf_size=i;
1494 } else
1495 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1496 1719 s->gb = s->in_gb;
1497 1719 s->in_gb.buffer = NULL;
1498 1719 s->extrasize = 0;
1499 }
1500
1501 2810 align_get_bits(&s->gb);
1502 av_assert1((get_bits_count(&s->gb) & 7) == 0);
1503 2810 i = (get_bits_left(&s->gb) >> 3) - s->extrasize;
1504
4/6
✓ Branch 0 taken 2810 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2580 times.
✓ Branch 3 taken 230 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2580 times.
2810 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 if (i < 0)
1506 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1507 230 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1508 }
1509 av_assert1(i <= buf_size - HEADER_SIZE && i >= 0);
1510 2810 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1511 2810 s->last_buf_size += i;
1512 }
1513
1514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10137 times.
10137 if(nb_frames < 0)
1515 return nb_frames;
1516
1517 /* get output buffer */
1518
1/2
✓ Branch 0 taken 10137 times.
✗ Branch 1 not taken.
10137 if (!samples) {
1519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10137 times.
10137 av_assert0(s->frame);
1520 10137 s->frame->nb_samples = s->avctx->frame_size;
1521
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10137 times.
10137 if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0)
1522 return ret;
1523 10137 samples = (OUT_INT **)s->frame->extended_data;
1524 }
1525
1526 /* apply the synthesis filter */
1527
2/2
✓ Branch 0 taken 12133 times.
✓ Branch 1 taken 10137 times.
22270 for (ch = 0; ch < s->nb_channels; ch++) {
1528 int sample_stride;
1529
1/2
✓ Branch 0 taken 12133 times.
✗ Branch 1 not taken.
12133 if (s->avctx->sample_fmt == OUT_FMT_P) {
1530 12133 samples_ptr = samples[ch];
1531 12133 sample_stride = 1;
1532 } else {
1533 samples_ptr = samples[0] + ch;
1534 sample_stride = s->nb_channels;
1535 }
1536
2/2
✓ Branch 0 taken 436716 times.
✓ Branch 1 taken 12133 times.
448849 for (i = 0; i < nb_frames; i++) {
1537 436716 RENAME(ff_mpa_synth_filter)(&s->mpadsp, s->synth_buf[ch],
1538 &(s->synth_buf_offset[ch]),
1539 RENAME(ff_mpa_synth_window),
1540 &s->dither_state, samples_ptr,
1541 436716 sample_stride, s->sb_samples[ch][i]);
1542 436716 samples_ptr += 32 * sample_stride;
1543 }
1544 }
1545
1546 10137 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1547 }
1548
1549 10139 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1550 AVPacket *avpkt)
1551 {
1552 10139 const uint8_t *buf = avpkt->data;
1553 10139 int buf_size = avpkt->size;
1554 10139 MPADecodeContext *s = avctx->priv_data;
1555 uint32_t header;
1556 int ret;
1557
1558 10139 int skipped = 0;
1559
2/4
✓ Branch 0 taken 10139 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10139 times.
10139 while(buf_size && !*buf){
1560 buf++;
1561 buf_size--;
1562 skipped++;
1563 }
1564
1565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10139 times.
10139 if (buf_size < HEADER_SIZE)
1566 return AVERROR_INVALIDDATA;
1567
1568 10139 header = AV_RB32(buf);
1569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10139 times.
10139 if (header >> 8 == AV_RB32("TAG") >> 8) {
1570 av_log(avctx, AV_LOG_DEBUG, "discarding ID3 tag\n");
1571 return buf_size + skipped;
1572 }
1573 10139 ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1574
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10137 times.
10139 if (ret < 0) {
1575 2 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1576 2 return AVERROR_INVALIDDATA;
1577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10137 times.
10137 } else if (ret == 1) {
1578 /* free format: prepare to compute frame size */
1579 s->frame_size = -1;
1580 return AVERROR_INVALIDDATA;
1581 }
1582 /* update codec info */
1583 10137 avctx->channels = s->nb_channels;
1584
2/2
✓ Branch 0 taken 8141 times.
✓ Branch 1 taken 1996 times.
10137 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1585
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10133 times.
10137 if (!avctx->bit_rate)
1586 4 avctx->bit_rate = s->bit_rate;
1587
1588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10137 times.
10137 if (s->frame_size <= 0) {
1589 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1590 return AVERROR_INVALIDDATA;
1591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10137 times.
10137 } else if (s->frame_size < buf_size) {
1592 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1593 buf_size= s->frame_size;
1594 }
1595
1596 10137 s->frame = data;
1597
1598 10137 ret = mp_decode_frame(s, NULL, buf, buf_size);
1599
1/2
✓ Branch 0 taken 10137 times.
✗ Branch 1 not taken.
10137 if (ret >= 0) {
1600 10137 s->frame->nb_samples = avctx->frame_size;
1601 10137 *got_frame_ptr = 1;
1602 10137 avctx->sample_rate = s->sample_rate;
1603 //FIXME maybe move the other codec info stuff from above here too
1604 } else {
1605 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1606 /* Only return an error if the bad frame makes up the whole packet or
1607 * the error is related to buffer management.
1608 * If there is more data in the packet, just consume the bad frame
1609 * instead of returning an error, which would discard the whole
1610 * packet. */
1611 *got_frame_ptr = 0;
1612 if (buf_size == avpkt->size || ret != AVERROR_INVALIDDATA)
1613 return ret;
1614 }
1615 10137 s->frame_size = 0;
1616 10137 return buf_size + skipped;
1617 }
1618
1619 static void mp_flush(MPADecodeContext *ctx)
1620 {
1621 memset(ctx->synth_buf, 0, sizeof(ctx->synth_buf));
1622 memset(ctx->mdct_buf, 0, sizeof(ctx->mdct_buf));
1623 ctx->last_buf_size = 0;
1624 ctx->dither_state = 0;
1625 }
1626
1627 static void flush(AVCodecContext *avctx)
1628 {
1629 mp_flush(avctx->priv_data);
1630 }
1631
1632 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1633 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1634 int *got_frame_ptr, AVPacket *avpkt)
1635 {
1636 const uint8_t *buf = avpkt->data;
1637 int buf_size = avpkt->size;
1638 MPADecodeContext *s = avctx->priv_data;
1639 uint32_t header;
1640 int len, ret;
1641 int av_unused out_size;
1642
1643 len = buf_size;
1644
1645 // Discard too short frames
1646 if (buf_size < HEADER_SIZE) {
1647 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1648 return AVERROR_INVALIDDATA;
1649 }
1650
1651
1652 if (len > MPA_MAX_CODED_FRAME_SIZE)
1653 len = MPA_MAX_CODED_FRAME_SIZE;
1654
1655 // Get header and restore sync word
1656 header = AV_RB32(buf) | 0xffe00000;
1657
1658 ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1659 if (ret < 0) {
1660 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1661 return ret;
1662 }
1663 /* update codec info */
1664 avctx->sample_rate = s->sample_rate;
1665 avctx->channels = s->nb_channels;
1666 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1667 if (!avctx->bit_rate)
1668 avctx->bit_rate = s->bit_rate;
1669
1670 s->frame_size = len;
1671
1672 s->frame = data;
1673
1674 ret = mp_decode_frame(s, NULL, buf, buf_size);
1675 if (ret < 0) {
1676 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1677 return ret;
1678 }
1679
1680 *got_frame_ptr = 1;
1681
1682 return buf_size;
1683 }
1684 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1685
1686 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1687
1688 /**
1689 * Context for MP3On4 decoder
1690 */
1691 typedef struct MP3On4DecodeContext {
1692 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1693 int syncword; ///< syncword patch
1694 const uint8_t *coff; ///< channel offsets in output buffer
1695 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1696 } MP3On4DecodeContext;
1697
1698 #include "mpeg4audio.h"
1699
1700 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1701
1702 /* number of mp3 decoder instances */
1703 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1704
1705 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1706 static const uint8_t chan_offset[8][5] = {
1707 { 0 },
1708 { 0 }, // C
1709 { 0 }, // FLR
1710 { 2, 0 }, // C FLR
1711 { 2, 0, 3 }, // C FLR BS
1712 { 2, 0, 3 }, // C FLR BLRS
1713 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1714 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1715 };
1716
1717 /* mp3on4 channel layouts */
1718 static const int16_t chan_layout[8] = {
1719 0,
1720 AV_CH_LAYOUT_MONO,
1721 AV_CH_LAYOUT_STEREO,
1722 AV_CH_LAYOUT_SURROUND,
1723 AV_CH_LAYOUT_4POINT0,
1724 AV_CH_LAYOUT_5POINT0,
1725 AV_CH_LAYOUT_5POINT1,
1726 AV_CH_LAYOUT_7POINT1
1727 };
1728
1729 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1730 {
1731 MP3On4DecodeContext *s = avctx->priv_data;
1732 int i;
1733
1734 for (i = 0; i < s->frames; i++)
1735 av_freep(&s->mp3decctx[i]);
1736
1737 return 0;
1738 }
1739
1740
1741 static av_cold int decode_init_mp3on4(AVCodecContext * avctx)
1742 {
1743 MP3On4DecodeContext *s = avctx->priv_data;
1744 MPEG4AudioConfig cfg;
1745 int i, ret;
1746
1747 if ((avctx->extradata_size < 2) || !avctx->extradata) {
1748 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1749 return AVERROR_INVALIDDATA;
1750 }
1751
1752 avpriv_mpeg4audio_get_config2(&cfg, avctx->extradata,
1753 avctx->extradata_size, 1, avctx);
1754 if (!cfg.chan_config || cfg.chan_config > 7) {
1755 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1756 return AVERROR_INVALIDDATA;
1757 }
1758 s->frames = mp3Frames[cfg.chan_config];
1759 s->coff = chan_offset[cfg.chan_config];
1760 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1761 avctx->channel_layout = chan_layout[cfg.chan_config];
1762
1763 if (cfg.sample_rate < 16000)
1764 s->syncword = 0xffe00000;
1765 else
1766 s->syncword = 0xfff00000;
1767
1768 /* Init the first mp3 decoder in standard way, so that all tables get builded
1769 * We replace avctx->priv_data with the context of the first decoder so that
1770 * decode_init() does not have to be changed.
1771 * Other decoders will be initialized here copying data from the first context
1772 */
1773 // Allocate zeroed memory for the first decoder context
1774 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1775 if (!s->mp3decctx[0])
1776 return AVERROR(ENOMEM);
1777 // Put decoder context in place to make init_decode() happy
1778 avctx->priv_data = s->mp3decctx[0];
1779 ret = decode_init(avctx);
1780 // Restore mp3on4 context pointer
1781 avctx->priv_data = s;
1782 if (ret < 0)
1783 return ret;
1784 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1785
1786 /* Create a separate codec/context for each frame (first is already ok).
1787 * Each frame is 1 or 2 channels - up to 5 frames allowed
1788 */
1789 for (i = 1; i < s->frames; i++) {
1790 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1791 if (!s->mp3decctx[i])
1792 return AVERROR(ENOMEM);
1793 s->mp3decctx[i]->adu_mode = 1;
1794 s->mp3decctx[i]->avctx = avctx;
1795 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1796 s->mp3decctx[i]->butterflies_float = s->mp3decctx[0]->butterflies_float;
1797 }
1798
1799 return 0;
1800 }
1801
1802
1803 static void flush_mp3on4(AVCodecContext *avctx)
1804 {
1805 int i;
1806 MP3On4DecodeContext *s = avctx->priv_data;
1807
1808 for (i = 0; i < s->frames; i++)
1809 mp_flush(s->mp3decctx[i]);
1810 }
1811
1812
1813 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1814 int *got_frame_ptr, AVPacket *avpkt)
1815 {
1816 AVFrame *frame = data;
1817 const uint8_t *buf = avpkt->data;
1818 int buf_size = avpkt->size;
1819 MP3On4DecodeContext *s = avctx->priv_data;
1820 MPADecodeContext *m;
1821 int fsize, len = buf_size, out_size = 0;
1822 uint32_t header;
1823 OUT_INT **out_samples;
1824 OUT_INT *outptr[2];
1825 int fr, ch, ret;
1826
1827 /* get output buffer */
1828 frame->nb_samples = MPA_FRAME_SIZE;
1829 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1830 return ret;
1831 out_samples = (OUT_INT **)frame->extended_data;
1832
1833 // Discard too short frames
1834 if (buf_size < HEADER_SIZE)
1835 return AVERROR_INVALIDDATA;
1836
1837 avctx->bit_rate = 0;
1838
1839 ch = 0;
1840 for (fr = 0; fr < s->frames; fr++) {
1841 fsize = AV_RB16(buf) >> 4;
1842 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1843 m = s->mp3decctx[fr];
1844 av_assert1(m);
1845
1846 if (fsize < HEADER_SIZE) {
1847 av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
1848 return AVERROR_INVALIDDATA;
1849 }
1850 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1851
1852 ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1853 if (ret < 0) {
1854 av_log(avctx, AV_LOG_ERROR, "Bad header, discard block\n");
1855 return AVERROR_INVALIDDATA;
1856 }
1857
1858 if (ch + m->nb_channels > avctx->channels ||
1859 s->coff[fr] + m->nb_channels > avctx->channels) {
1860 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1861 "channel count\n");
1862 return AVERROR_INVALIDDATA;
1863 }
1864 ch += m->nb_channels;
1865
1866 outptr[0] = out_samples[s->coff[fr]];
1867 if (m->nb_channels > 1)
1868 outptr[1] = out_samples[s->coff[fr] + 1];
1869
1870 if ((ret = mp_decode_frame(m, outptr, buf, fsize)) < 0) {
1871 av_log(avctx, AV_LOG_ERROR, "failed to decode channel %d\n", ch);
1872 memset(outptr[0], 0, MPA_FRAME_SIZE*sizeof(OUT_INT));
1873 if (m->nb_channels > 1)
1874 memset(outptr[1], 0, MPA_FRAME_SIZE*sizeof(OUT_INT));
1875 ret = m->nb_channels * MPA_FRAME_SIZE*sizeof(OUT_INT);
1876 }
1877
1878 out_size += ret;
1879 buf += fsize;
1880 len -= fsize;
1881
1882 avctx->bit_rate += m->bit_rate;
1883 }
1884 if (ch != avctx->channels) {
1885 av_log(avctx, AV_LOG_ERROR, "failed to decode all channels\n");
1886 return AVERROR_INVALIDDATA;
1887 }
1888
1889 /* update codec info */
1890 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
1891
1892 frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
1893 *got_frame_ptr = 1;
1894
1895 return buf_size;
1896 }
1897 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
1898