FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ac3_parser.c
Date: 2024-03-29 01:21:52
Exec Total Coverage
Lines: 111 117 94.9%
Functions: 6 6 100.0%
Branches: 54 60 90.0%

Line Branch Exec Source
1 /*
2 * AC-3 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 "config.h"
24 #include "config_components.h"
25
26 #include "libavutil/channel_layout.h"
27 #include "parser.h"
28 #include "ac3defs.h"
29 #include "ac3tab.h"
30 #include "ac3_parser.h"
31 #include "ac3_parser_internal.h"
32 #include "aac_ac3_parser.h"
33 #include "get_bits.h"
34
35
36 #define AC3_HEADER_SIZE 7
37
38 #if CONFIG_AC3_PARSER
39
40 static const uint8_t eac3_blocks[4] = {
41 1, 2, 3, 6
42 };
43
44 /**
45 * Table for center mix levels
46 * reference: Section 5.4.2.4 cmixlev
47 */
48 static const uint8_t center_levels[4] = { 4, 5, 6, 5 };
49
50 /**
51 * Table for surround mix levels
52 * reference: Section 5.4.2.5 surmixlev
53 */
54 static const uint8_t surround_levels[4] = { 4, 6, 7, 6 };
55
56 8456 int ff_ac3_find_syncword(const uint8_t *buf, int buf_size)
57 {
58 int i;
59
60
2/2
✓ Branch 0 taken 9854 times.
✓ Branch 1 taken 10 times.
9864 for (i = 1; i < buf_size; i += 2) {
61
3/4
✓ Branch 0 taken 1400 times.
✓ Branch 1 taken 8454 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1400 times.
9854 if (buf[i] == 0x77 || buf[i] == 0x0B) {
62
2/2
✓ Branch 0 taken 8446 times.
✓ Branch 1 taken 8 times.
8454 if ((buf[i] ^ buf[i-1]) == (0x77 ^ 0x0B)) {
63 8446 i--;
64 8446 break;
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 } else if ((buf[i] ^ buf[i+1]) == (0x77 ^ 0x0B)) {
66 break;
67 }
68 }
69 }
70
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 8446 times.
8456 if (i >= buf_size)
71 10 return AVERROR_INVALIDDATA;
72
73 8446 return i;
74 }
75
76 188200 int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
77 {
78 int frame_size_code;
79
80 188200 memset(hdr, 0, sizeof(*hdr));
81
82 188200 hdr->sync_word = get_bits(gbc, 16);
83
2/2
✓ Branch 0 taken 55362 times.
✓ Branch 1 taken 132838 times.
188200 if(hdr->sync_word != 0x0B77)
84 55362 return AAC_AC3_PARSE_ERROR_SYNC;
85
86 /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
87 132838 hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
88
2/2
✓ Branch 0 taken 7960 times.
✓ Branch 1 taken 124878 times.
132838 if(hdr->bitstream_id > 16)
89 7960 return AAC_AC3_PARSE_ERROR_BSID;
90
91 124878 hdr->num_blocks = 6;
92 124878 hdr->ac3_bit_rate_code = -1;
93
94 /* set default mix levels */
95 124878 hdr->center_mix_level = 5; // -4.5dB
96 124878 hdr->surround_mix_level = 6; // -6.0dB
97
98 /* set default dolby surround mode */
99 124878 hdr->dolby_surround_mode = AC3_DSURMOD_NOTINDICATED;
100
101
2/2
✓ Branch 0 taken 16432 times.
✓ Branch 1 taken 108446 times.
124878 if(hdr->bitstream_id <= 10) {
102 /* Normal AC-3 */
103 16432 hdr->crc1 = get_bits(gbc, 16);
104 16432 hdr->sr_code = get_bits(gbc, 2);
105
2/2
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 15898 times.
16432 if(hdr->sr_code == 3)
106 534 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
107
108 15898 frame_size_code = get_bits(gbc, 6);
109
2/2
✓ Branch 0 taken 1214 times.
✓ Branch 1 taken 14684 times.
15898 if(frame_size_code > 37)
110 1214 return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
111
112 14684 hdr->ac3_bit_rate_code = (frame_size_code >> 1);
113
114 14684 skip_bits(gbc, 5); // skip bsid, already got it
115
116 14684 hdr->bitstream_mode = get_bits(gbc, 3);
117 14684 hdr->channel_mode = get_bits(gbc, 3);
118
119
2/2
✓ Branch 0 taken 2391 times.
✓ Branch 1 taken 12293 times.
14684 if(hdr->channel_mode == AC3_CHMODE_STEREO) {
120 2391 hdr->dolby_surround_mode = get_bits(gbc, 2);
121 } else {
122
4/4
✓ Branch 0 taken 6753 times.
✓ Branch 1 taken 5540 times.
✓ Branch 2 taken 5998 times.
✓ Branch 3 taken 755 times.
12293 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
123 5998 hdr-> center_mix_level = center_levels[get_bits(gbc, 2)];
124
2/2
✓ Branch 0 taken 6042 times.
✓ Branch 1 taken 6251 times.
12293 if(hdr->channel_mode & 4)
125 6042 hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
126 }
127 14684 hdr->lfe_on = get_bits1(gbc);
128
129 14684 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
130 14684 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
131 14684 hdr->bit_rate = (ff_ac3_bitrate_tab[hdr->ac3_bit_rate_code] * 1000) >> hdr->sr_shift;
132 14684 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
133 14684 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
134 14684 hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
135 14684 hdr->substreamid = 0;
136 } else {
137 /* Enhanced AC-3 */
138 108446 hdr->crc1 = 0;
139 108446 hdr->frame_type = get_bits(gbc, 2);
140
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 108176 times.
108446 if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
141 270 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
142
143 108176 hdr->substreamid = get_bits(gbc, 3);
144
145 108176 hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
146
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 108174 times.
108176 if(hdr->frame_size < AC3_HEADER_SIZE)
147 2 return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
148
149 108174 hdr->sr_code = get_bits(gbc, 2);
150
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 108090 times.
108174 if (hdr->sr_code == 3) {
151 84 int sr_code2 = get_bits(gbc, 2);
152
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 66 times.
84 if(sr_code2 == 3)
153 18 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
154 66 hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
155 66 hdr->sr_shift = 1;
156 } else {
157 108090 hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
158 108090 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
159 108090 hdr->sr_shift = 0;
160 }
161
162 108156 hdr->channel_mode = get_bits(gbc, 3);
163 108156 hdr->lfe_on = get_bits1(gbc);
164
165 108156 hdr->bit_rate = 8LL * hdr->frame_size * hdr->sample_rate /
166 108156 (hdr->num_blocks * 256);
167 108156 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
168 }
169 122840 hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode];
170
2/2
✓ Branch 0 taken 13338 times.
✓ Branch 1 taken 109502 times.
122840 if (hdr->lfe_on)
171 13338 hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
172
173 122840 return 0;
174 }
175
176 // TODO: Better way to pass AC3HeaderInfo fields to mov muxer.
177 7490 int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf,
178 size_t size)
179 {
180 GetBitContext gb;
181 AC3HeaderInfo *hdr;
182 int err;
183
184
2/2
✓ Branch 0 taken 1039 times.
✓ Branch 1 taken 6451 times.
7490 if (!*phdr)
185 1039 *phdr = av_mallocz(sizeof(AC3HeaderInfo));
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7490 times.
7490 if (!*phdr)
187 return AVERROR(ENOMEM);
188 7490 hdr = *phdr;
189
190 7490 err = init_get_bits8(&gb, buf, size);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7490 times.
7490 if (err < 0)
192 return AVERROR_INVALIDDATA;
193 7490 err = ff_ac3_parse_header(&gb, hdr);
194
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7488 times.
7490 if (err < 0)
195 2 return AVERROR_INVALIDDATA;
196
197 7488 return get_bits_count(&gb);
198 }
199
200 130344 int av_ac3_parse_header(const uint8_t *buf, size_t size,
201 uint8_t *bitstream_id, uint16_t *frame_size)
202 {
203 GetBitContext gb;
204 AC3HeaderInfo hdr;
205 int err;
206
207 130344 init_get_bits8(&gb, buf, size);
208 130344 err = ff_ac3_parse_header(&gb, &hdr);
209
2/2
✓ Branch 0 taken 24282 times.
✓ Branch 1 taken 106062 times.
130344 if (err < 0)
210 24282 return AVERROR_INVALIDDATA;
211
212 106062 *bitstream_id = hdr.bitstream_id;
213 106062 *frame_size = hdr.frame_size;
214
215 106062 return 0;
216 }
217
218 47583 static int ac3_sync(uint64_t state, int *need_next_header, int *new_frame_start)
219 {
220 int err;
221 union {
222 uint64_t u64;
223 uint8_t u8[8 + AV_INPUT_BUFFER_PADDING_SIZE];
224 47583 } tmp = { av_be2ne64(state) };
225 AC3HeaderInfo hdr;
226 GetBitContext gbc;
227
228
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 47578 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
47583 if (tmp.u8[1] == 0x77 && tmp.u8[2] == 0x0b) {
229 FFSWAP(uint8_t, tmp.u8[1], tmp.u8[2]);
230 FFSWAP(uint8_t, tmp.u8[3], tmp.u8[4]);
231 FFSWAP(uint8_t, tmp.u8[5], tmp.u8[6]);
232 }
233
234 47583 init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
235 47583 err = ff_ac3_parse_header(&gbc, &hdr);
236
237
2/2
✓ Branch 0 taken 41076 times.
✓ Branch 1 taken 6507 times.
47583 if(err < 0)
238 41076 return 0;
239
240 6507 *new_frame_start = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
241
3/4
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 6193 times.
✓ Branch 2 taken 314 times.
✗ Branch 3 not taken.
6507 *need_next_header = *new_frame_start || (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
242 6507 return hdr.frame_size;
243 }
244
245 71 static av_cold int ac3_parse_init(AVCodecParserContext *s1)
246 {
247 71 AACAC3ParseContext *s = s1->priv_data;
248 71 s->header_size = AC3_HEADER_SIZE;
249 71 s->crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
250 71 s->sync = ac3_sync;
251 71 return 0;
252 }
253
254
255 const AVCodecParser ff_ac3_parser = {
256 .codec_ids = { AV_CODEC_ID_AC3, AV_CODEC_ID_EAC3 },
257 .priv_data_size = sizeof(AACAC3ParseContext),
258 .parser_init = ac3_parse_init,
259 .parser_parse = ff_aac_ac3_parse,
260 .parser_close = ff_parse_close,
261 };
262
263 #else
264
265 int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf,
266 size_t size)
267 {
268 return AVERROR(ENOSYS);
269 }
270
271 int av_ac3_parse_header(const uint8_t *buf, size_t size,
272 uint8_t *bitstream_id, uint16_t *frame_size)
273 {
274 return AVERROR(ENOSYS);
275 }
276 #endif
277