FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegaudiodec_template.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 689 1004 68.6%
Functions: 26 32 81.2%
Branches: 350 546 64.1%

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