Check if there is enough bytes before reading the buffer in the EA ADPCM
[ffmpeg-lucabe.git] / libavcodec / mpegaudiodecheader.c
blobe29b9b9ee858b590ac69a9b1f0116b0855feca2c
1 /*
2 * MPEG Audio header decoder
3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file libavcodec/mpegaudiodecheader.c
24 * MPEG Audio header decoder.
27 //#define DEBUG
28 #include "avcodec.h"
29 #include "mpegaudio.h"
30 #include "mpegaudiodata.h"
33 int ff_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
35 int sample_rate, frame_size, mpeg25, padding;
36 int sample_rate_index, bitrate_index;
37 if (header & (1<<20)) {
38 s->lsf = (header & (1<<19)) ? 0 : 1;
39 mpeg25 = 0;
40 } else {
41 s->lsf = 1;
42 mpeg25 = 1;
45 s->layer = 4 - ((header >> 17) & 3);
46 /* extract frequency */
47 sample_rate_index = (header >> 10) & 3;
48 sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
49 sample_rate_index += 3 * (s->lsf + mpeg25);
50 s->sample_rate_index = sample_rate_index;
51 s->error_protection = ((header >> 16) & 1) ^ 1;
52 s->sample_rate = sample_rate;
54 bitrate_index = (header >> 12) & 0xf;
55 padding = (header >> 9) & 1;
56 //extension = (header >> 8) & 1;
57 s->mode = (header >> 6) & 3;
58 s->mode_ext = (header >> 4) & 3;
59 //copyright = (header >> 3) & 1;
60 //original = (header >> 2) & 1;
61 //emphasis = header & 3;
63 if (s->mode == MPA_MONO)
64 s->nb_channels = 1;
65 else
66 s->nb_channels = 2;
68 if (bitrate_index != 0) {
69 frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
70 s->bit_rate = frame_size * 1000;
71 switch(s->layer) {
72 case 1:
73 frame_size = (frame_size * 12000) / sample_rate;
74 frame_size = (frame_size + padding) * 4;
75 break;
76 case 2:
77 frame_size = (frame_size * 144000) / sample_rate;
78 frame_size += padding;
79 break;
80 default:
81 case 3:
82 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
83 frame_size += padding;
84 break;
86 s->frame_size = frame_size;
87 } else {
88 /* if no frame size computed, signal it */
89 return 1;
92 #if defined(DEBUG)
93 dprintf(s->avctx, "layer%d, %d Hz, %d kbits/s, ",
94 s->layer, s->sample_rate, s->bit_rate);
95 if (s->nb_channels == 2) {
96 if (s->layer == 3) {
97 if (s->mode_ext & MODE_EXT_MS_STEREO)
98 dprintf(s->avctx, "ms-");
99 if (s->mode_ext & MODE_EXT_I_STEREO)
100 dprintf(s->avctx, "i-");
102 dprintf(s->avctx, "stereo");
103 } else {
104 dprintf(s->avctx, "mono");
106 dprintf(s->avctx, "\n");
107 #endif
108 return 0;