remove unneeded variables from AC3HeaderInfo
[libav.git] / libavcodec / ac3_parser.c
CommitLineData
99ff31dc
AJ
1/*
2 * AC3 parser
3 * Copyright (c) 2003 Fabrice Bellard.
4 * Copyright (c) 2003 Michael Niedermayer.
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "parser.h"
24#include "ac3_parser.h"
25#include "aac_ac3_parser.h"
26#include "bitstream.h"
27
28
29#define AC3_HEADER_SIZE 7
30
31
32static const uint8_t eac3_blocks[4] = {
33 1, 2, 3, 6
34};
35
36
37int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr)
38{
39 GetBitContext gbc;
86ad4c7e 40 int frame_size_code;
99ff31dc
AJ
41
42 memset(hdr, 0, sizeof(*hdr));
43
44 init_get_bits(&gbc, buf, 54);
45
46 hdr->sync_word = get_bits(&gbc, 16);
47 if(hdr->sync_word != 0x0B77)
3df88093 48 return AC3_PARSE_ERROR_SYNC;
99ff31dc
AJ
49
50 /* read ahead to bsid to make sure this is AC-3, not E-AC-3 */
e59cc205
JR
51 hdr->bitstream_id = show_bits_long(&gbc, 29) & 0x1F;
52 if(hdr->bitstream_id > 10)
3df88093 53 return AC3_PARSE_ERROR_BSID;
99ff31dc
AJ
54
55 hdr->crc1 = get_bits(&gbc, 16);
5ce21342
JR
56 hdr->sr_code = get_bits(&gbc, 2);
57 if(hdr->sr_code == 3)
3df88093 58 return AC3_PARSE_ERROR_SAMPLE_RATE;
99ff31dc 59
86ad4c7e
JR
60 frame_size_code = get_bits(&gbc, 6);
61 if(frame_size_code > 37)
3df88093 62 return AC3_PARSE_ERROR_FRAME_SIZE;
99ff31dc
AJ
63
64 skip_bits(&gbc, 5); // skip bsid, already got it
65
86ad4c7e 66 skip_bits(&gbc, 3); // skip bitstream mode
e59cc205
JR
67 hdr->channel_mode = get_bits(&gbc, 3);
68 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO) {
86ad4c7e 69 skip_bits(&gbc, 2); // skip center mix level
99ff31dc 70 }
e59cc205 71 if(hdr->channel_mode & 4) {
86ad4c7e 72 skip_bits(&gbc, 2); // skip surround mix level
99ff31dc 73 }
e59cc205 74 if(hdr->channel_mode == AC3_CHMODE_STEREO) {
86ad4c7e 75 skip_bits(&gbc, 2); // skip dolby surround mode
99ff31dc 76 }
e59cc205 77 hdr->lfe_on = get_bits1(&gbc);
99ff31dc 78
e59cc205 79 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
5ce21342 80 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
86ad4c7e 81 hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
e59cc205 82 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
86ad4c7e 83 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
99ff31dc
AJ
84
85 return 0;
86}
87
88static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
89 int *bit_rate, int *samples)
90{
91 int err;
e59cc205
JR
92 unsigned int sr_code, channel_mode, bitstream_id, lfe_on;
93 unsigned int stream_type, substream_id, frame_size, sr_code2, num_blocks_code;
99ff31dc
AJ
94 GetBitContext bits;
95 AC3HeaderInfo hdr;
96
97 err = ff_ac3_parse_header(buf, &hdr);
98
99 if(err < 0 && err != -2)
100 return 0;
101
e59cc205
JR
102 bitstream_id = hdr.bitstream_id;
103 if(bitstream_id <= 10) { /* Normal AC-3 */
99ff31dc
AJ
104 *sample_rate = hdr.sample_rate;
105 *bit_rate = hdr.bit_rate;
106 *channels = hdr.channels;
107 *samples = AC3_FRAME_SIZE;
108 return hdr.frame_size;
e59cc205 109 } else if (bitstream_id > 10 && bitstream_id <= 16) { /* Enhanced AC-3 */
99ff31dc 110 init_get_bits(&bits, &buf[2], (AC3_HEADER_SIZE-2) * 8);
e59cc205
JR
111 stream_type = get_bits(&bits, 2);
112 substream_id = get_bits(&bits, 3);
99ff31dc 113
e59cc205 114 if (stream_type != 0 || substream_id != 0)
99ff31dc
AJ
115 return 0; /* Currently don't support additional streams */
116
e59cc205
JR
117 frame_size = get_bits(&bits, 11) + 1;
118 if(frame_size*2 < AC3_HEADER_SIZE)
4d570f94
MN
119 return 0;
120
5ce21342
JR
121 sr_code = get_bits(&bits, 2);
122 if (sr_code == 3) {
123 sr_code2 = get_bits(&bits, 2);
e59cc205 124 num_blocks_code = 3;
99ff31dc 125
5ce21342 126 if(sr_code2 == 3)
99ff31dc
AJ
127 return 0;
128
5ce21342 129 *sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
99ff31dc 130 } else {
e59cc205 131 num_blocks_code = get_bits(&bits, 2);
99ff31dc 132
5ce21342 133 *sample_rate = ff_ac3_sample_rate_tab[sr_code];
99ff31dc
AJ
134 }
135
e59cc205
JR
136 channel_mode = get_bits(&bits, 3);
137 lfe_on = get_bits1(&bits);
99ff31dc 138
e59cc205
JR
139 *samples = eac3_blocks[num_blocks_code] * 256;
140 *bit_rate = frame_size * (*sample_rate) * 16 / (*samples);
141 *channels = ff_ac3_channels_tab[channel_mode] + lfe_on;
99ff31dc 142
e59cc205 143 return frame_size * 2;
99ff31dc
AJ
144 }
145
146 /* Unsupported bitstream version */
147 return 0;
148}
149
150static int ac3_parse_init(AVCodecParserContext *s1)
151{
679c2294 152 AACAC3ParseContext *s = s1->priv_data;
99ff31dc
AJ
153 s->inbuf_ptr = s->inbuf;
154 s->header_size = AC3_HEADER_SIZE;
155 s->sync = ac3_sync;
156 return 0;
157}
158
159
160AVCodecParser ac3_parser = {
161 { CODEC_ID_AC3 },
679c2294 162 sizeof(AACAC3ParseContext),
99ff31dc 163 ac3_parse_init,
679c2294 164 ff_aac_ac3_parse,
99ff31dc
AJ
165 NULL,
166};