FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ac3_parser.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 112 119 94.1%
Functions: 6 6 100.0%
Branches: 55 62 88.7%

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_components.h"
24
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/mem.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 8527 int ff_ac3_find_syncword(const uint8_t *buf, int buf_size)
57 {
58 int i;
59
60
2/2
✓ Branch 0 taken 9924 times.
✓ Branch 1 taken 11 times.
9935 for (i = 1; i < buf_size; i += 2) {
61
3/4
✓ Branch 0 taken 1400 times.
✓ Branch 1 taken 8524 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1400 times.
9924 if (buf[i] == 0x77 || buf[i] == 0x0B) {
62
2/2
✓ Branch 0 taken 8516 times.
✓ Branch 1 taken 8 times.
8524 if ((buf[i] ^ buf[i-1]) == (0x77 ^ 0x0B)) {
63 8516 i--;
64 8516 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 11 times.
✓ Branch 1 taken 8516 times.
8527 if (i >= buf_size)
71 11 return AVERROR_INVALIDDATA;
72
73 8516 return i;
74 }
75
76 190034 int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
77 {
78 int frame_size_code;
79
80 190034 memset(hdr, 0, sizeof(*hdr));
81
82 190034 hdr->sync_word = get_bits(gbc, 16);
83
2/2
✓ Branch 0 taken 55592 times.
✓ Branch 1 taken 134442 times.
190034 if(hdr->sync_word != 0x0B77)
84 55592 return AC3_PARSE_ERROR_SYNC;
85
86 /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
87 134442 hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
88
2/2
✓ Branch 0 taken 8108 times.
✓ Branch 1 taken 126334 times.
134442 if(hdr->bitstream_id > 16)
89 8108 return AC3_PARSE_ERROR_BSID;
90
91 126334 hdr->num_blocks = 6;
92 126334 hdr->ac3_bit_rate_code = -1;
93
94 /* set default mix levels */
95 126334 hdr->center_mix_level = 5; // -4.5dB
96 126334 hdr->surround_mix_level = 6; // -6.0dB
97
98 /* set default dolby surround mode */
99 126334 hdr->dolby_surround_mode = AC3_DSURMOD_NOTINDICATED;
100
101
2/2
✓ Branch 0 taken 16630 times.
✓ Branch 1 taken 109704 times.
126334 if(hdr->bitstream_id <= 10) {
102 /* Normal AC-3 */
103 16630 hdr->crc1 = get_bits(gbc, 16);
104 16630 hdr->sr_code = get_bits(gbc, 2);
105
2/2
✓ Branch 0 taken 536 times.
✓ Branch 1 taken 16094 times.
16630 if(hdr->sr_code == 3)
106 536 return AC3_PARSE_ERROR_SAMPLE_RATE;
107
108 16094 frame_size_code = get_bits(gbc, 6);
109
2/2
✓ Branch 0 taken 1240 times.
✓ Branch 1 taken 14854 times.
16094 if(frame_size_code > 37)
110 1240 return AC3_PARSE_ERROR_FRAME_SIZE;
111
112 14854 hdr->ac3_bit_rate_code = (frame_size_code >> 1);
113
114 14854 skip_bits(gbc, 5); // skip bsid, already got it
115
116 14854 hdr->bitstream_mode = get_bits(gbc, 3);
117 14854 hdr->channel_mode = get_bits(gbc, 3);
118
119
2/2
✓ Branch 0 taken 2410 times.
✓ Branch 1 taken 12444 times.
14854 if(hdr->channel_mode == AC3_CHMODE_STEREO) {
120 2410 hdr->dolby_surround_mode = get_bits(gbc, 2);
121 } else {
122
4/4
✓ Branch 0 taken 6824 times.
✓ Branch 1 taken 5620 times.
✓ Branch 2 taken 6061 times.
✓ Branch 3 taken 763 times.
12444 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
123 6061 hdr-> center_mix_level = center_levels[get_bits(gbc, 2)];
124
2/2
✓ Branch 0 taken 6105 times.
✓ Branch 1 taken 6339 times.
12444 if(hdr->channel_mode & 4)
125 6105 hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
126 }
127 14854 hdr->lfe_on = get_bits1(gbc);
128
129 14854 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
130 14854 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
131 14854 hdr->bit_rate = (ff_ac3_bitrate_tab[hdr->ac3_bit_rate_code] * 1000) >> hdr->sr_shift;
132 14854 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
133 14854 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
134 14854 hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
135 14854 hdr->substreamid = 0;
136 } else {
137 /* Enhanced AC-3 */
138 109704 hdr->crc1 = 0;
139 109704 hdr->frame_type = get_bits(gbc, 2);
140
2/2
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 109420 times.
109704 if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
141 284 return AC3_PARSE_ERROR_FRAME_TYPE;
142
143 109420 hdr->substreamid = get_bits(gbc, 3);
144
145 109420 hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
146
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 109418 times.
109420 if(hdr->frame_size < AC3_HEADER_SIZE)
147 2 return AC3_PARSE_ERROR_FRAME_SIZE;
148
149 109418 hdr->sr_code = get_bits(gbc, 2);
150
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 109334 times.
109418 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 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 109334 hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
158 109334 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
159 109334 hdr->sr_shift = 0;
160 }
161
162 109400 hdr->channel_mode = get_bits(gbc, 3);
163 109400 hdr->lfe_on = get_bits1(gbc);
164
165 109400 hdr->bit_rate = 8LL * hdr->frame_size * hdr->sample_rate /
166 109400 (hdr->num_blocks * 256);
167 109400 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
168 }
169 124254 hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode];
170
2/2
✓ Branch 0 taken 14667 times.
✓ Branch 1 taken 109587 times.
124254 if (hdr->lfe_on)
171 14667 hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
172
173 124254 return 0;
174 }
175
176 // TODO: Better way to pass AC3HeaderInfo fields to mov muxer.
177 7553 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 6514 times.
7553 if (!*phdr)
185 1039 *phdr = av_mallocz(sizeof(AC3HeaderInfo));
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7553 times.
7553 if (!*phdr)
187 return AVERROR(ENOMEM);
188 7553 hdr = *phdr;
189
190 7553 err = init_get_bits8(&gb, buf, size);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7553 times.
7553 if (err < 0)
192 return AVERROR_INVALIDDATA;
193 7553 err = ff_ac3_parse_header(&gb, hdr);
194
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7551 times.
7553 if (err < 0)
195 2 return AVERROR_INVALIDDATA;
196
197 7551 return get_bits_count(&gb);
198 }
199
200 132024 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 132024 err = init_get_bits8(&gb, buf, size);
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132024 times.
132024 if (err < 0)
209 return AVERROR_INVALIDDATA;
210 132024 err = ff_ac3_parse_header(&gb, &hdr);
211
2/2
✓ Branch 0 taken 24630 times.
✓ Branch 1 taken 107394 times.
132024 if (err < 0)
212 24630 return AVERROR_INVALIDDATA;
213
214 107394 *bitstream_id = hdr.bitstream_id;
215 107394 *frame_size = hdr.frame_size;
216
217 107394 return 0;
218 }
219
220 47667 static int ac3_sync(uint64_t state, int *need_next_header, int *new_frame_start)
221 {
222 int err;
223 union {
224 uint64_t u64;
225 uint8_t u8[8 + AV_INPUT_BUFFER_PADDING_SIZE];
226 47667 } tmp = { av_be2ne64(state) };
227 AC3HeaderInfo hdr;
228 GetBitContext gbc;
229
230
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 47662 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
47667 if (tmp.u8[1] == 0x77 && tmp.u8[2] == 0x0b) {
231 FFSWAP(uint8_t, tmp.u8[1], tmp.u8[2]);
232 FFSWAP(uint8_t, tmp.u8[3], tmp.u8[4]);
233 FFSWAP(uint8_t, tmp.u8[5], tmp.u8[6]);
234 }
235
236 47667 init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
237 47667 err = ff_ac3_parse_header(&gbc, &hdr);
238
239
2/2
✓ Branch 0 taken 41148 times.
✓ Branch 1 taken 6519 times.
47667 if(err < 0)
240 41148 return 0;
241
242 6519 *new_frame_start = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
243
3/4
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 6205 times.
✓ Branch 2 taken 314 times.
✗ Branch 3 not taken.
6519 *need_next_header = *new_frame_start || (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
244 6519 return hdr.frame_size;
245 }
246
247 77 static av_cold int ac3_parse_init(AVCodecParserContext *s1)
248 {
249 77 AACAC3ParseContext *s = s1->priv_data;
250 77 s->header_size = AC3_HEADER_SIZE;
251 77 s->crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
252 77 s->sync = ac3_sync;
253 77 return 0;
254 }
255
256
257 const AVCodecParser ff_ac3_parser = {
258 .codec_ids = { AV_CODEC_ID_AC3, AV_CODEC_ID_EAC3 },
259 .priv_data_size = sizeof(AACAC3ParseContext),
260 .parser_init = ac3_parse_init,
261 .parser_parse = ff_aac_ac3_parse,
262 .parser_close = ff_parse_close,
263 };
264
265 #else
266
267 int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf,
268 size_t size)
269 {
270 return AVERROR(ENOSYS);
271 }
272
273 int av_ac3_parse_header(const uint8_t *buf, size_t size,
274 uint8_t *bitstream_id, uint16_t *frame_size)
275 {
276 return AVERROR(ENOSYS);
277 }
278 #endif
279