ac3_parser: add required padding for GetBitContext buffer
[libav.git] / libavcodec / ac3_parser.c
CommitLineData
99ff31dc 1/*
14b70628 2 * AC-3 parser
406792e7
DB
3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2003 Michael Niedermayer
99ff31dc 5 *
2912e87a 6 * This file is part of Libav.
99ff31dc 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
99ff31dc
AJ
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
99ff31dc
AJ
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
99ff31dc
AJ
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
a903f8f0 23#include "libavutil/channel_layout.h"
99ff31dc
AJ
24#include "parser.h"
25#include "ac3_parser.h"
26#include "aac_ac3_parser.h"
9106a698 27#include "get_bits.h"
99ff31dc
AJ
28
29
30#define AC3_HEADER_SIZE 7
31
32
33static const uint8_t eac3_blocks[4] = {
34 1, 2, 3, 6
35};
36
e6d9fa66
MN
37/**
38 * Table for center mix levels
39 * reference: Section 5.4.2.4 cmixlev
40 */
41static const uint8_t center_levels[4] = { 4, 5, 6, 5 };
42
43/**
44 * Table for surround mix levels
45 * reference: Section 5.4.2.5 surmixlev
46 */
47static const uint8_t surround_levels[4] = { 4, 6, 7, 6 };
48
55736cfb 49
6f89efea 50int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
99ff31dc 51{
86ad4c7e 52 int frame_size_code;
99ff31dc
AJ
53
54 memset(hdr, 0, sizeof(*hdr));
55
55736cfb 56 hdr->sync_word = get_bits(gbc, 16);
99ff31dc 57 if(hdr->sync_word != 0x0B77)
11d6f38c 58 return AAC_AC3_PARSE_ERROR_SYNC;
99ff31dc 59
646800f7 60 /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
55736cfb 61 hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
646800f7 62 if(hdr->bitstream_id > 16)
11d6f38c 63 return AAC_AC3_PARSE_ERROR_BSID;
99ff31dc 64
6730e9f3
JR
65 hdr->num_blocks = 6;
66
30cc3ac2 67 /* set default mix levels */
e6d9fa66
MN
68 hdr->center_mix_level = 5; // -4.5dB
69 hdr->surround_mix_level = 6; // -6.0dB
30cc3ac2 70
13345fc1
TW
71 /* set default dolby surround mode */
72 hdr->dolby_surround_mode = AC3_DSURMOD_NOTINDICATED;
73
646800f7 74 if(hdr->bitstream_id <= 10) {
1d8cfa31 75 /* Normal AC-3 */
55736cfb
BW
76 hdr->crc1 = get_bits(gbc, 16);
77 hdr->sr_code = get_bits(gbc, 2);
1d8cfa31 78 if(hdr->sr_code == 3)
11d6f38c 79 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
1d8cfa31 80
55736cfb 81 frame_size_code = get_bits(gbc, 6);
1d8cfa31 82 if(frame_size_code > 37)
11d6f38c 83 return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
1d8cfa31 84
55736cfb
BW
85 skip_bits(gbc, 5); // skip bsid, already got it
86
be187388 87 hdr->bitstream_mode = get_bits(gbc, 3);
55736cfb
BW
88 hdr->channel_mode = get_bits(gbc, 3);
89
1d8cfa31 90 if(hdr->channel_mode == AC3_CHMODE_STEREO) {
13345fc1 91 hdr->dolby_surround_mode = get_bits(gbc, 2);
55736cfb
BW
92 } else {
93 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
e6d9fa66 94 hdr-> center_mix_level = center_levels[get_bits(gbc, 2)];
55736cfb 95 if(hdr->channel_mode & 4)
e6d9fa66 96 hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
1d8cfa31 97 }
55736cfb 98 hdr->lfe_on = get_bits1(gbc);
1d8cfa31
JR
99
100 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
101 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
102 hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
103 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
104 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
144b5bdc 105 hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
3596aa6f 106 hdr->substreamid = 0;
646800f7
JR
107 } else {
108 /* Enhanced AC-3 */
109 hdr->crc1 = 0;
55736cfb 110 hdr->frame_type = get_bits(gbc, 2);
79b25a27 111 if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
11d6f38c 112 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
4e6eeaf0 113
3596aa6f 114 hdr->substreamid = get_bits(gbc, 3);
646800f7 115
55736cfb 116 hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
646800f7 117 if(hdr->frame_size < AC3_HEADER_SIZE)
11d6f38c 118 return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
646800f7 119
55736cfb 120 hdr->sr_code = get_bits(gbc, 2);
646800f7 121 if (hdr->sr_code == 3) {
55736cfb 122 int sr_code2 = get_bits(gbc, 2);
646800f7 123 if(sr_code2 == 3)
11d6f38c 124 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
646800f7
JR
125 hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
126 hdr->sr_shift = 1;
646800f7 127 } else {
6730e9f3 128 hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
646800f7
JR
129 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
130 hdr->sr_shift = 0;
131 }
132
55736cfb
BW
133 hdr->channel_mode = get_bits(gbc, 3);
134 hdr->lfe_on = get_bits1(gbc);
646800f7
JR
135
136 hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
6730e9f3 137 (hdr->num_blocks * 256.0));
646800f7
JR
138 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
139 }
3798205a 140 hdr->channel_layout = avpriv_ac3_channel_layout_tab[hdr->channel_mode];
bfeca7be 141 if (hdr->lfe_on)
c2fcd0a7 142 hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
99ff31dc
AJ
143
144 return 0;
145}
146
133ac890
MN
147static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
148 int *need_next_header, int *new_frame_start)
99ff31dc
AJ
149{
150 int err;
6c414bb6
MR
151 union {
152 uint64_t u64;
09447f2b 153 uint8_t u8[8 + FF_INPUT_BUFFER_PADDING_SIZE];
8fc0162a 154 } tmp = { av_be2ne64(state) };
99ff31dc 155 AC3HeaderInfo hdr;
55736cfb 156 GetBitContext gbc;
99ff31dc 157
6c414bb6 158 init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
6f89efea 159 err = avpriv_ac3_parse_header(&gbc, &hdr);
99ff31dc 160
646800f7 161 if(err < 0)
99ff31dc
AJ
162 return 0;
163
c599e297
JR
164 hdr_info->sample_rate = hdr.sample_rate;
165 hdr_info->bit_rate = hdr.bit_rate;
166 hdr_info->channels = hdr.channels;
bfeca7be 167 hdr_info->channel_layout = hdr.channel_layout;
a169df2f 168 hdr_info->samples = hdr.num_blocks * 256;
be187388
JR
169 hdr_info->service_type = hdr.bitstream_mode;
170 if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
171 hdr_info->service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
4cff8dc8 172 if(hdr.bitstream_id>10)
36ef5369
AK
173 hdr_info->codec_id = AV_CODEC_ID_EAC3;
174 else if (hdr_info->codec_id == AV_CODEC_ID_NONE)
175 hdr_info->codec_id = AV_CODEC_ID_AC3;
be1e2a2b 176
144b5bdc
MN
177 *need_next_header = (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
178 *new_frame_start = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
1d8cfa31 179 return hdr.frame_size;
99ff31dc
AJ
180}
181
98a6fff9 182static av_cold int ac3_parse_init(AVCodecParserContext *s1)
99ff31dc 183{
679c2294 184 AACAC3ParseContext *s = s1->priv_data;
99ff31dc
AJ
185 s->header_size = AC3_HEADER_SIZE;
186 s->sync = ac3_sync;
187 return 0;
188}
189
190
d36beb3f 191AVCodecParser ff_ac3_parser = {
36ef5369 192 .codec_ids = { AV_CODEC_ID_AC3, AV_CODEC_ID_EAC3 },
5511ad14
AK
193 .priv_data_size = sizeof(AACAC3ParseContext),
194 .parser_init = ac3_parse_init,
195 .parser_parse = ff_aac_ac3_parse,
196 .parser_close = ff_parse_close,
99ff31dc 197};