FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegaudiodec_template.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 685 1006 68.1%
Branches: 348 548 63.5%

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