FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/ac3dec.c
Date: 2026-08-26 01:27:24
Exec Total Coverage
Lines: 49 51 96.1%
Functions: 3 3 100.0%
Branches: 49 54 90.7%

Line Branch Exec Source
1 /*
2 * RAW AC-3 and E-AC-3 demuxer
3 * Copyright (c) 2007 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config_components.h"
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/crc.h"
26 #include "libavcodec/ac3_parser.h"
27 #include "avformat.h"
28 #include "demux.h"
29 #include "rawdec.h"
30
31 15868 static int ac3_eac3_probe(const AVProbeData *p, enum AVCodecID expected_codec_id)
32 {
33 15868 int max_frames, first_frames = 0, frames;
34 const uint8_t *buf, *buf2, *end;
35 15868 enum AVCodecID codec_id = AV_CODEC_ID_AC3;
36
37 15868 max_frames = 0;
38 15868 buf = p->buf;
39 15868 end = buf + p->buf_size;
40
41
2/2
✓ Branch 0 taken 983217584 times.
✓ Branch 1 taken 15868 times.
983233452 for(; buf < end; buf++) {
42
6/6
✓ Branch 0 taken 983201716 times.
✓ Branch 1 taken 15868 times.
✓ Branch 2 taken 6262934 times.
✓ Branch 3 taken 976938782 times.
✓ Branch 4 taken 6247786 times.
✓ Branch 5 taken 15148 times.
983217584 if(buf > p->buf && !(buf[0] == 0x0B && buf[1] == 0x77)
43
4/4
✓ Branch 0 taken 1681896 times.
✓ Branch 1 taken 981504672 times.
✓ Branch 2 taken 1659474 times.
✓ Branch 3 taken 22422 times.
983186568 && !(buf[0] == 0x77 && buf[1] == 0x0B) )
44 983164146 continue;
45 53438 buf2 = buf;
46
47
2/2
✓ Branch 0 taken 143386 times.
✓ Branch 1 taken 184 times.
143570 for(frames = 0; buf2 < end; frames++) {
48 uint8_t buf3[4096];
49 uint8_t bitstream_id;
50 uint16_t frame_size;
51 int i, ret;
52
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143386 times.
143386 if(!memcmp(buf2, "\x1\x10", 2)) {
54 if (buf2 + 16 > end)
55 53254 break;
56 buf2+=16;
57 }
58
4/4
✓ Branch 0 taken 22670 times.
✓ Branch 1 taken 120716 times.
✓ Branch 2 taken 22600 times.
✓ Branch 3 taken 70 times.
143386 if (buf[0] == 0x77 && buf[1] == 0x0B) {
59
2/2
✓ Branch 0 taken 90400 times.
✓ Branch 1 taken 22600 times.
113000 for(i=0; i<8; i+=2) {
60 90400 buf3[i ] = buf2[i+1];
61 90400 buf3[i+1] = buf2[i ];
62 }
63 22600 ret = av_ac3_parse_header(buf3, 8, &bitstream_id,
64 &frame_size);
65 }else
66 120786 ret = av_ac3_parse_header(buf2, end - buf2, &bitstream_id,
67 &frame_size);
68
2/2
✓ Branch 0 taken 42118 times.
✓ Branch 1 taken 101268 times.
143386 if (ret < 0)
69 42118 break;
70
2/2
✓ Branch 0 taken 1850 times.
✓ Branch 1 taken 99418 times.
101268 if(buf2 + frame_size > end)
71 1850 break;
72
3/4
✓ Branch 0 taken 6016 times.
✓ Branch 1 taken 93402 times.
✓ Branch 2 taken 6016 times.
✗ Branch 3 not taken.
99418 if (buf[0] == 0x77 && buf[1] == 0x0B) {
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6016 times.
6016 av_assert0(frame_size <= sizeof(buf3));
74
2/2
✓ Branch 0 taken 3507184 times.
✓ Branch 1 taken 6016 times.
3513200 for(i = 8; i < frame_size; i += 2) {
75 3507184 buf3[i ] = buf2[i+1];
76 3507184 buf3[i+1] = buf2[i ];
77 }
78
2/2
✓ Branch 1 taken 5838 times.
✓ Branch 2 taken 178 times.
6016 if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf3 + 2, frame_size - 2))
79 5838 break;
80 } else {
81
2/2
✓ Branch 1 taken 3448 times.
✓ Branch 2 taken 89954 times.
93402 if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, frame_size - 2))
82 3448 break;
83 }
84
2/2
✓ Branch 0 taken 88034 times.
✓ Branch 1 taken 2098 times.
90132 if (bitstream_id > 10)
85 88034 codec_id = AV_CODEC_ID_EAC3;
86 90132 buf2 += frame_size;
87 }
88 53438 max_frames = FFMAX(max_frames, frames);
89
2/2
✓ Branch 0 taken 15868 times.
✓ Branch 1 taken 37570 times.
53438 if(buf == p->buf)
90 15868 first_frames = frames;
91 }
92
2/2
✓ Branch 0 taken 7934 times.
✓ Branch 1 taken 7934 times.
15868 if(codec_id != expected_codec_id) return 0;
93 // keep this in sync with mp3 probe, both need to avoid
94 // issues with MPEG-files!
95
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 7908 times.
7934 if (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 2;
96
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7907 times.
7908 else if(max_frames>200)return AVPROBE_SCORE_EXTENSION;
97
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7879 times.
7907 else if(max_frames>=4) return AVPROBE_SCORE_EXTENSION/2;
98
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 7819 times.
7879 else if(max_frames>=1) return 1;
99 7819 else return 0;
100 }
101
102 #if CONFIG_AC3_DEMUXER
103 7934 static int ac3_probe(const AVProbeData *p)
104 {
105 7934 return ac3_eac3_probe(p, AV_CODEC_ID_AC3);
106 }
107
108 const FFInputFormat ff_ac3_demuxer = {
109 .p.name = "ac3",
110 .p.long_name = NULL_IF_CONFIG_SMALL("raw AC-3"),
111 .p.flags = AVFMT_GENERIC_INDEX,
112 .p.extensions = "ac3",
113 .p.priv_class = &ff_raw_demuxer_class,
114 .read_probe = ac3_probe,
115 .read_header = ff_raw_audio_read_header,
116 .read_packet = ff_raw_read_partial_packet,
117 .raw_codec_id = AV_CODEC_ID_AC3,
118 .priv_data_size = sizeof(FFRawDemuxerContext),
119 };
120 #endif
121
122 #if CONFIG_EAC3_DEMUXER
123 7934 static int eac3_probe(const AVProbeData *p)
124 {
125 7934 return ac3_eac3_probe(p, AV_CODEC_ID_EAC3);
126 }
127
128 const FFInputFormat ff_eac3_demuxer = {
129 .p.name = "eac3",
130 .p.long_name = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
131 .p.flags = AVFMT_GENERIC_INDEX,
132 .p.extensions = "eac3,ec3",
133 .p.priv_class = &ff_raw_demuxer_class,
134 .read_probe = eac3_probe,
135 .read_header = ff_raw_audio_read_header,
136 .read_packet = ff_raw_read_partial_packet,
137 .raw_codec_id = AV_CODEC_ID_EAC3,
138 .priv_data_size = sizeof(FFRawDemuxerContext),
139 };
140 #endif
141