FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ac3_parser.c
Date: 2024-11-20 23:03:26
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 8468 int ff_ac3_find_syncword(const uint8_t *buf, int buf_size)
57 {
58 int i;
59
60
2/2
✓ Branch 0 taken 9866 times.
✓ Branch 1 taken 10 times.
9876 for (i = 1; i < buf_size; i += 2) {
61
3/4
✓ Branch 0 taken 1400 times.
✓ Branch 1 taken 8466 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1400 times.
9866 if (buf[i] == 0x77 || buf[i] == 0x0B) {
62
2/2
✓ Branch 0 taken 8458 times.
✓ Branch 1 taken 8 times.
8466 if ((buf[i] ^ buf[i-1]) == (0x77 ^ 0x0B)) {
63 8458 i--;
64 8458 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 8458 times.
8468 if (i >= buf_size)
71 10 return AVERROR_INVALIDDATA;
72
73 8458 return i;
74 }
75
76 189205 int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
77 {
78 int frame_size_code;
79
80 189205 memset(hdr, 0, sizeof(*hdr));
81
82 189205 hdr->sync_word = get_bits(gbc, 16);
83
2/2
✓ Branch 0 taken 55564 times.
✓ Branch 1 taken 133641 times.
189205 if(hdr->sync_word != 0x0B77)
84 55564 return AC3_PARSE_ERROR_SYNC;
85
86 /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
87 133641 hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
88
2/2
✓ Branch 0 taken 8036 times.
✓ Branch 1 taken 125605 times.
133641 if(hdr->bitstream_id > 16)
89 8036 return AC3_PARSE_ERROR_BSID;
90
91 125605 hdr->num_blocks = 6;
92 125605 hdr->ac3_bit_rate_code = -1;
93
94 /* set default mix levels */
95 125605 hdr->center_mix_level = 5; // -4.5dB
96 125605 hdr->surround_mix_level = 6; // -6.0dB
97
98 /* set default dolby surround mode */
99 125605 hdr->dolby_surround_mode = AC3_DSURMOD_NOTINDICATED;
100
101
2/2
✓ Branch 0 taken 16517 times.
✓ Branch 1 taken 109088 times.
125605 if(hdr->bitstream_id <= 10) {
102 /* Normal AC-3 */
103 16517 hdr->crc1 = get_bits(gbc, 16);
104 16517 hdr->sr_code = get_bits(gbc, 2);
105
2/2
✓ Branch 0 taken 536 times.
✓ Branch 1 taken 15981 times.
16517 if(hdr->sr_code == 3)
106 536 return AC3_PARSE_ERROR_SAMPLE_RATE;
107
108 15981 frame_size_code = get_bits(gbc, 6);
109
2/2
✓ Branch 0 taken 1228 times.
✓ Branch 1 taken 14753 times.
15981 if(frame_size_code > 37)
110 1228 return AC3_PARSE_ERROR_FRAME_SIZE;
111
112 14753 hdr->ac3_bit_rate_code = (frame_size_code >> 1);
113
114 14753 skip_bits(gbc, 5); // skip bsid, already got it
115
116 14753 hdr->bitstream_mode = get_bits(gbc, 3);
117 14753 hdr->channel_mode = get_bits(gbc, 3);
118
119
2/2
✓ Branch 0 taken 2410 times.
✓ Branch 1 taken 12343 times.
14753 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 6763 times.
✓ Branch 1 taken 5580 times.
✓ Branch 2 taken 6002 times.
✓ Branch 3 taken 761 times.
12343 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
123 6002 hdr-> center_mix_level = center_levels[get_bits(gbc, 2)];
124
2/2
✓ Branch 0 taken 6046 times.
✓ Branch 1 taken 6297 times.
12343 if(hdr->channel_mode & 4)
125 6046 hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
126 }
127 14753 hdr->lfe_on = get_bits1(gbc);
128
129 14753 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
130 14753 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
131 14753 hdr->bit_rate = (ff_ac3_bitrate_tab[hdr->ac3_bit_rate_code] * 1000) >> hdr->sr_shift;
132 14753 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
133 14753 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
134 14753 hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
135 14753 hdr->substreamid = 0;
136 } else {
137 /* Enhanced AC-3 */
138 109088 hdr->crc1 = 0;
139 109088 hdr->frame_type = get_bits(gbc, 2);
140
2/2
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 108810 times.
109088 if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
141 278 return AC3_PARSE_ERROR_FRAME_TYPE;
142
143 108810 hdr->substreamid = get_bits(gbc, 3);
144
145 108810 hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
146
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 108808 times.
108810 if(hdr->frame_size < AC3_HEADER_SIZE)
147 2 return AC3_PARSE_ERROR_FRAME_SIZE;
148
149 108808 hdr->sr_code = get_bits(gbc, 2);
150
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 108724 times.
108808 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 108724 hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
158 108724 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
159 108724 hdr->sr_shift = 0;
160 }
161
162 108790 hdr->channel_mode = get_bits(gbc, 3);
163 108790 hdr->lfe_on = get_bits1(gbc);
164
165 108790 hdr->bit_rate = 8LL * hdr->frame_size * hdr->sample_rate /
166 108790 (hdr->num_blocks * 256);
167 108790 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
168 }
169 123543 hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode];
170
2/2
✓ Branch 0 taken 13982 times.
✓ Branch 1 taken 109561 times.
123543 if (hdr->lfe_on)
171 13982 hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
172
173 123543 return 0;
174 }
175
176 // TODO: Better way to pass AC3HeaderInfo fields to mov muxer.
177 7498 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 6459 times.
7498 if (!*phdr)
185 1039 *phdr = av_mallocz(sizeof(AC3HeaderInfo));
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7498 times.
7498 if (!*phdr)
187 return AVERROR(ENOMEM);
188 7498 hdr = *phdr;
189
190 7498 err = init_get_bits8(&gb, buf, size);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7498 times.
7498 if (err < 0)
192 return AVERROR_INVALIDDATA;
193 7498 err = ff_ac3_parse_header(&gb, hdr);
194
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7496 times.
7498 if (err < 0)
195 2 return AVERROR_INVALIDDATA;
196
197 7496 return get_bits_count(&gb);
198 }
199
200 131246 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 131246 err = init_get_bits8(&gb, buf, size);
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131246 times.
131246 if (err < 0)
209 return AVERROR_INVALIDDATA;
210 131246 err = ff_ac3_parse_header(&gb, &hdr);
211
2/2
✓ Branch 0 taken 24506 times.
✓ Branch 1 taken 106740 times.
131246 if (err < 0)
212 24506 return AVERROR_INVALIDDATA;
213
214 106740 *bitstream_id = hdr.bitstream_id;
215 106740 *frame_size = hdr.frame_size;
216
217 106740 return 0;
218 }
219
220 47674 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 47674 } tmp = { av_be2ne64(state) };
227 AC3HeaderInfo hdr;
228 GetBitContext gbc;
229
230
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 47669 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
47674 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 47674 init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
237 47674 err = ff_ac3_parse_header(&gbc, &hdr);
238
239
2/2
✓ Branch 0 taken 41154 times.
✓ Branch 1 taken 6520 times.
47674 if(err < 0)
240 41154 return 0;
241
242 6520 *new_frame_start = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
243
3/4
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 6206 times.
✓ Branch 2 taken 314 times.
✗ Branch 3 not taken.
6520 *need_next_header = *new_frame_start || (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
244 6520 return hdr.frame_size;
245 }
246
247 76 static av_cold int ac3_parse_init(AVCodecParserContext *s1)
248 {
249 76 AACAC3ParseContext *s = s1->priv_data;
250 76 s->header_size = AC3_HEADER_SIZE;
251 76 s->crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
252 76 s->sync = ac3_sync;
253 76 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