FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegaudiodec_template.c
Date: 2026-09-19 02:48:33
Exec Total Coverage
Lines: 685 1004 68.2%
Functions: 24 32 75.0%
Branches: 354 546 64.8%

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