FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/spdifdec.c
Date: 2026-07-19 01:11:25
Exec Total Coverage
Lines: 100 138 72.5%
Functions: 6 6 100.0%
Branches: 50 67 74.6%

Line Branch Exec Source
1 /*
2 * IEC 61937 demuxer
3 * Copyright (c) 2010 Anssi Hannula <anssi.hannula at iki.fi>
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 /**
23 * @file
24 * IEC 61937 demuxer, used for compressed data in S/PDIF
25 * @author Anssi Hannula
26 */
27
28 #include "libavutil/bswap.h"
29
30 #include "libavcodec/ac3defs.h"
31 #include "libavcodec/adts_parser.h"
32
33 #include "avformat.h"
34 #include "demux.h"
35 #include "internal.h"
36 #include "spdif.h"
37
38 2194 static int spdif_get_offset_and_codec(AVFormatContext *s,
39 enum IEC61937DataType data_type,
40 const char *buf, int *offset,
41 enum AVCodecID *codec)
42 {
43 uint32_t samples;
44 uint8_t frames;
45 int ret;
46
47
6/13
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 114 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 102 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1189 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 662 times.
✓ Branch 12 taken 20 times.
2194 switch (data_type & 0xff) {
48 107 case IEC61937_AC3:
49 107 *offset = AC3_FRAME_SIZE << 2;
50 107 *codec = AV_CODEC_ID_AC3;
51 107 break;
52 case IEC61937_MPEG1_LAYER1:
53 *offset = spdif_mpeg_pkt_offset[1][0];
54 *codec = AV_CODEC_ID_MP1;
55 break;
56 114 case IEC61937_MPEG1_LAYER23:
57 114 *offset = spdif_mpeg_pkt_offset[1][0];
58 114 *codec = AV_CODEC_ID_MP3;
59 114 break;
60 case IEC61937_MPEG2_EXT:
61 *offset = 4608;
62 *codec = AV_CODEC_ID_MP3;
63 break;
64 102 case IEC61937_MPEG2_AAC:
65 102 ret = av_adts_header_parse(buf, &samples, &frames);
66
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 86 times.
102 if (ret < 0) {
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (s) /* be silent during a probe */
68 av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
69 16 return ret;
70 }
71 86 *offset = samples << 2;
72 86 *codec = AV_CODEC_ID_AAC;
73 86 break;
74 case IEC61937_MPEG2_LAYER1_LSF:
75 *offset = spdif_mpeg_pkt_offset[0][0];
76 *codec = AV_CODEC_ID_MP1;
77 break;
78 case IEC61937_MPEG2_LAYER2_LSF:
79 *offset = spdif_mpeg_pkt_offset[0][1];
80 *codec = AV_CODEC_ID_MP2;
81 break;
82 case IEC61937_MPEG2_LAYER3_LSF:
83 *offset = spdif_mpeg_pkt_offset[0][2];
84 *codec = AV_CODEC_ID_MP3;
85 break;
86 1189 case IEC61937_DTS1:
87 1189 *offset = 2048;
88 1189 *codec = AV_CODEC_ID_DTS;
89 1189 break;
90 case IEC61937_DTS2:
91 *offset = 4096;
92 *codec = AV_CODEC_ID_DTS;
93 break;
94 case IEC61937_DTS3:
95 *offset = 8192;
96 *codec = AV_CODEC_ID_DTS;
97 break;
98 662 case IEC61937_EAC3:
99 662 *offset = 24576;
100 662 *codec = AV_CODEC_ID_EAC3;
101 662 break;
102 20 default:
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (s) { /* be silent during a probe */
104 avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
105 data_type);
106 }
107 20 return AVERROR_PATCHWELCOME;
108 }
109 2158 return 0;
110 }
111
112 /* Largest offset between bursts we currently handle, i.e. AAC with
113 samples = 4096 */
114 #define SPDIF_MAX_OFFSET 16384
115
116 7778 static int spdif_probe(const AVProbeData *p)
117 {
118 enum AVCodecID codec;
119 7778 return ff_spdif_probe (p->buf, p->buf_size, &codec);
120 }
121
122 8341 int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
123 {
124 8341 const uint8_t *buf = p_buf;
125
2/2
✓ Branch 0 taken 6271 times.
✓ Branch 1 taken 2070 times.
8341 const uint8_t *probe_end = p_buf + FFMIN(2 * SPDIF_MAX_OFFSET, buf_size - 1);
126 8341 const uint8_t *expected_code = buf + 7;
127 8341 uint32_t state = 0;
128 8341 int sync_codes = 0;
129 8341 int consecutive_codes = 0;
130 int offset;
131
132
2/2
✓ Branch 0 taken 109955557 times.
✓ Branch 1 taken 8323 times.
109963880 for (; buf < probe_end; buf++) {
133 109955557 state = (state << 8) | *buf;
134
135
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 109955456 times.
109955557 if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
136
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 && buf[1] < 0x37) {
137 101 sync_codes++;
138
139
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 90 times.
101 if (buf == expected_code) {
140
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
11 if (++consecutive_codes >= 2)
141 4 return AVPROBE_SCORE_MAX;
142 } else
143 90 consecutive_codes = 0;
144
145 /* spdif_get_offset_and_codec() parses AV_AAC_ADTS_HEADER_SIZE
146 * bytes starting at buf[5] (the payload after the 4 byte sync and
147 * the Pc/Pd burst header), so that many bytes must be available. */
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97 times.
97 if (buf + 5 + AV_AAC_ADTS_HEADER_SIZE > p_buf + buf_size)
149 break;
150
151 /* continue probing to find more sync codes */
152 97 probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p_buf + buf_size - 1);
153
154 /* skip directly to the next sync code */
155
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 36 times.
97 if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
156 97 &buf[5], &offset, codec)) {
157
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 47 times.
61 if (buf + offset >= p_buf + buf_size)
158 14 break;
159 47 expected_code = buf + offset;
160 47 buf = expected_code - 7;
161 }
162 }
163 }
164
165
2/2
✓ Branch 0 taken 8294 times.
✓ Branch 1 taken 43 times.
8337 if (!sync_codes)
166 8294 return 0;
167
168
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 40 times.
43 if (sync_codes >= 6)
169 /* good amount of sync codes but with unexpected offsets */
170 3 return AVPROBE_SCORE_EXTENSION;
171
172 /* some sync codes were found */
173 40 return AVPROBE_SCORE_EXTENSION / 4;
174 }
175
176 7 static int spdif_read_header(AVFormatContext *s)
177 {
178 7 s->ctx_flags |= AVFMTCTX_NOHEADER;
179 7 return 0;
180 }
181
182 2097 static int spdif_get_pkt_size_bits(int type, int code)
183 {
184
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 1445 times.
2097 switch (type & 0xff) {
185 652 case IEC61937_EAC3:
186 652 return code << 3;
187 1445 default:
188 1445 return code;
189 }
190 }
191
192 2113 int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
193 {
194 2113 AVIOContext *pb = s->pb;
195 enum IEC61937DataType data_type;
196 enum AVCodecID codec_id;
197 2113 uint32_t state = 0;
198 int pkt_size_bits, offset, ret;
199
200
2/2
✓ Branch 0 taken 260308 times.
✓ Branch 1 taken 2097 times.
262405 while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
201 260308 state = (state << 8) | avio_r8(pb);
202
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 260292 times.
260308 if (avio_feof(pb))
203 16 return AVERROR_EOF;
204 }
205
206 2097 data_type = avio_rl16(pb);
207 2097 pkt_size_bits = spdif_get_pkt_size_bits(data_type, avio_rl16(pb));
208
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2097 times.
2097 if (pkt_size_bits % 16)
210 avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
211
212 2097 ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2097 times.
2097 if (ret)
214 return ret;
215
216 2097 pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
217
218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2097 times.
2097 if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
219 return AVERROR_EOF;
220 }
221 2097 ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
222
223 2097 ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
224 &offset, &codec_id);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2097 times.
2097 if (ret < 0) {
226 return ret;
227 }
228
229 /* skip over the padding to the beginning of the next frame */
230 2097 avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
231
232
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2090 times.
2097 if (!s->nb_streams) {
233 /* first packet, create a stream */
234 7 AVStream *st = avformat_new_stream(s, NULL);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!st) {
236 return AVERROR(ENOMEM);
237 }
238 7 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
239 7 st->codecpar->codec_id = codec_id;
240
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if (codec_id == AV_CODEC_ID_EAC3)
241 1 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
242 else
243 6 ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2090 times.
2090 } else if (codec_id != s->streams[0]->codecpar->codec_id) {
245 avpriv_report_missing_feature(s, "Codec change in IEC 61937");
246 return AVERROR_PATCHWELCOME;
247 }
248
249
4/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2078 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 11 times.
2097 if (!s->bit_rate && s->streams[0]->codecpar->sample_rate)
250 /* stream bitrate matches 16-bit stereo PCM bitrate for currently
251 supported codecs */
252 8 s->bit_rate = 2 * 16LL * s->streams[0]->codecpar->sample_rate;
253
254 2097 return 0;
255 }
256
257 const FFInputFormat ff_spdif_demuxer = {
258 .p.name = "spdif",
259 .p.long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
260 .p.flags = AVFMT_GENERIC_INDEX,
261 .read_probe = spdif_probe,
262 .read_header = spdif_read_header,
263 .read_packet = ff_spdif_read_packet,
264 };
265