Libav
Main Page
Related Pages
Modules
Data Structures
Files
Examples
File List
Globals
libavcodec
ac3_parser.c
Go to the documentation of this file.
1
/*
2
* AC-3 parser
3
* Copyright (c) 2003 Fabrice Bellard
4
* Copyright (c) 2003 Michael Niedermayer
5
*
6
* This file is part of Libav.
7
*
8
* Libav 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
* Libav 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 Libav; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
*/
22
23
#include "
libavutil/channel_layout.h
"
24
#include "
parser.h
"
25
#include "
ac3_parser.h
"
26
#include "
aac_ac3_parser.h
"
27
#include "
get_bits.h
"
28
29
30
#define AC3_HEADER_SIZE 7
31
32
33
static
const
uint8_t
eac3_blocks
[4] = {
34
1, 2, 3, 6
35
};
36
41
static
const
uint8_t
center_levels
[4] = { 4, 5, 6, 5 };
42
47
static
const
uint8_t
surround_levels
[4] = { 4, 6, 7, 6 };
48
49
50
int
avpriv_ac3_parse_header
(
GetBitContext
*gbc,
AC3HeaderInfo
*hdr)
51
{
52
int
frame_size_code;
53
54
memset(hdr, 0,
sizeof
(*hdr));
55
56
hdr->
sync_word
=
get_bits
(gbc, 16);
57
if
(hdr->
sync_word
!= 0x0B77)
58
return
AAC_AC3_PARSE_ERROR_SYNC
;
59
60
/* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
61
hdr->
bitstream_id
=
show_bits_long
(gbc, 29) & 0x1F;
62
if
(hdr->
bitstream_id
> 16)
63
return
AAC_AC3_PARSE_ERROR_BSID
;
64
65
hdr->
num_blocks
= 6;
66
67
/* set default mix levels */
68
hdr->
center_mix_level
= 5;
// -4.5dB
69
hdr->
surround_mix_level
= 6;
// -6.0dB
70
71
/* set default dolby surround mode */
72
hdr->
dolby_surround_mode
=
AC3_DSURMOD_NOTINDICATED
;
73
74
if
(hdr->
bitstream_id
<= 10) {
75
/* Normal AC-3 */
76
hdr->
crc1
=
get_bits
(gbc, 16);
77
hdr->
sr_code
=
get_bits
(gbc, 2);
78
if
(hdr->
sr_code
== 3)
79
return
AAC_AC3_PARSE_ERROR_SAMPLE_RATE
;
80
81
frame_size_code =
get_bits
(gbc, 6);
82
if
(frame_size_code > 37)
83
return
AAC_AC3_PARSE_ERROR_FRAME_SIZE
;
84
85
skip_bits
(gbc, 5);
// skip bsid, already got it
86
87
hdr->
bitstream_mode
=
get_bits
(gbc, 3);
88
hdr->
channel_mode
=
get_bits
(gbc, 3);
89
90
if
(hdr->
channel_mode
==
AC3_CHMODE_STEREO
) {
91
hdr->
dolby_surround_mode
=
get_bits
(gbc, 2);
92
}
else
{
93
if
((hdr->
channel_mode
& 1) && hdr->
channel_mode
!=
AC3_CHMODE_MONO
)
94
hdr-> center_mix_level =
center_levels
[
get_bits
(gbc, 2)];
95
if
(hdr->
channel_mode
& 4)
96
hdr->
surround_mix_level
=
surround_levels
[
get_bits
(gbc, 2)];
97
}
98
hdr->
lfe_on
=
get_bits1
(gbc);
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;
105
hdr->
frame_type
=
EAC3_FRAME_TYPE_AC3_CONVERT
;
//EAC3_FRAME_TYPE_INDEPENDENT;
106
hdr->
substreamid
= 0;
107
}
else
{
108
/* Enhanced AC-3 */
109
hdr->
crc1
= 0;
110
hdr->
frame_type
=
get_bits
(gbc, 2);
111
if
(hdr->
frame_type
==
EAC3_FRAME_TYPE_RESERVED
)
112
return
AAC_AC3_PARSE_ERROR_FRAME_TYPE
;
113
114
hdr->
substreamid
=
get_bits
(gbc, 3);
115
116
hdr->
frame_size
= (
get_bits
(gbc, 11) + 1) << 1;
117
if
(hdr->
frame_size
<
AC3_HEADER_SIZE
)
118
return
AAC_AC3_PARSE_ERROR_FRAME_SIZE
;
119
120
hdr->
sr_code
=
get_bits
(gbc, 2);
121
if
(hdr->
sr_code
== 3) {
122
int
sr_code2 =
get_bits
(gbc, 2);
123
if
(sr_code2 == 3)
124
return
AAC_AC3_PARSE_ERROR_SAMPLE_RATE
;
125
hdr->
sample_rate
=
ff_ac3_sample_rate_tab
[sr_code2] / 2;
126
hdr->
sr_shift
= 1;
127
}
else
{
128
hdr->
num_blocks
=
eac3_blocks
[
get_bits
(gbc, 2)];
129
hdr->
sample_rate
=
ff_ac3_sample_rate_tab
[hdr->
sr_code
];
130
hdr->
sr_shift
= 0;
131
}
132
133
hdr->
channel_mode
=
get_bits
(gbc, 3);
134
hdr->
lfe_on
=
get_bits1
(gbc);
135
136
hdr->
bit_rate
= (uint32_t)(8.0 * hdr->
frame_size
* hdr->
sample_rate
/
137
(hdr->
num_blocks
* 256.0));
138
hdr->
channels
=
ff_ac3_channels_tab
[hdr->
channel_mode
] + hdr->
lfe_on
;
139
}
140
hdr->
channel_layout
=
avpriv_ac3_channel_layout_tab
[hdr->
channel_mode
];
141
if
(hdr->
lfe_on
)
142
hdr->
channel_layout
|=
AV_CH_LOW_FREQUENCY
;
143
144
return
0;
145
}
146
147
static
int
ac3_sync
(uint64_t
state
,
AACAC3ParseContext
*hdr_info,
148
int
*need_next_header,
int
*new_frame_start)
149
{
150
int
err;
151
union
{
152
uint64_t u64;
153
uint8_t
u8[8];
154
} tmp = {
av_be2ne64
(state) };
155
AC3HeaderInfo
hdr;
156
GetBitContext
gbc;
157
158
init_get_bits
(&gbc, tmp.u8+8-
AC3_HEADER_SIZE
, 54);
159
err =
avpriv_ac3_parse_header
(&gbc, &hdr);
160
161
if
(err < 0)
162
return
0;
163
164
hdr_info->
sample_rate
= hdr.
sample_rate
;
165
hdr_info->
bit_rate
= hdr.
bit_rate
;
166
hdr_info->
channels
= hdr.
channels
;
167
hdr_info->
channel_layout
= hdr.
channel_layout
;
168
hdr_info->
samples
= hdr.
num_blocks
* 256;
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
;
172
if
(hdr.
bitstream_id
>10)
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
;
176
177
*need_next_header = (hdr.
frame_type
!=
EAC3_FRAME_TYPE_AC3_CONVERT
);
178
*new_frame_start = (hdr.
frame_type
!=
EAC3_FRAME_TYPE_DEPENDENT
);
179
return
hdr.
frame_size
;
180
}
181
182
static
av_cold
int
ac3_parse_init
(
AVCodecParserContext
*s1)
183
{
184
AACAC3ParseContext
*s = s1->
priv_data
;
185
s->
header_size
=
AC3_HEADER_SIZE
;
186
s->
sync
=
ac3_sync
;
187
return
0;
188
}
189
190
191
AVCodecParser
ff_ac3_parser
= {
192
.
codec_ids
= {
AV_CODEC_ID_AC3
,
AV_CODEC_ID_EAC3
},
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
,
197
};
Generated on Sun Jun 1 2014 17:55:28 for Libav by
1.8.1.2