FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/dss.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 130 177 73.4%
Functions: 9 10 90.0%
Branches: 39 80 48.8%

Line Branch Exec Source
1 /*
2 * Digital Speech Standard (DSS) demuxer
3 * Copyright (c) 2014 Oleksij Rempel <linux@rempel-privat.de>
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 "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25
26 #include "avformat.h"
27 #include "demux.h"
28 #include "internal.h"
29 #include "avio_internal.h"
30
31 #define DSS_HEAD_OFFSET_AUTHOR 0xc
32 #define DSS_AUTHOR_SIZE 16
33
34 #define DSS_HEAD_OFFSET_START_TIME 0x26
35 #define DSS_HEAD_OFFSET_END_TIME 0x32
36 #define DSS_TIME_SIZE 12
37
38 #define DSS_HEAD_OFFSET_ACODEC 0x2a4
39 #define DSS_ACODEC_DSS_SP 0x0 /* SP mode */
40 #define DSS_ACODEC_G723_1 0x2 /* LP mode */
41
42 #define DSS_HEAD_OFFSET_COMMENT 0x31e
43 #define DSS_COMMENT_SIZE 64
44
45 #define DSS_BLOCK_SIZE 512
46 #define DSS_AUDIO_BLOCK_HEADER_SIZE 6
47 #define DSS_FRAME_SIZE 42
48
49 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
50
51 typedef struct DSSDemuxContext {
52 unsigned int audio_codec;
53 int counter;
54 int swap;
55 int dss_sp_swap_byte;
56
57 int packet_size;
58 int dss_header_size;
59 } DSSDemuxContext;
60
61 7480 static int dss_probe(const AVProbeData *p)
62 {
63
2/2
✓ Branch 0 taken 7478 times.
✓ Branch 1 taken 2 times.
7480 if ( AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
64
1/2
✓ Branch 0 taken 7478 times.
✗ Branch 1 not taken.
7478 && AV_RL32(p->buf) != MKTAG(0x3, 'd', 's', 's'))
65 7478 return 0;
66
67 2 return AVPROBE_SCORE_MAX;
68 }
69
70 2 static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
71 const char *key)
72 {
73 2 AVIOContext *pb = s->pb;
74 2 char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
75 int y, month, d, h, minute, sec;
76 int ret;
77
78 2 avio_seek(pb, offset, SEEK_SET);
79
80 2 ret = avio_read(s->pb, string, DSS_TIME_SIZE);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < DSS_TIME_SIZE)
82 return ret < 0 ? ret : AVERROR_EOF;
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec) != 6)
85 return AVERROR_INVALIDDATA;
86 /* We deal with a two-digit year here, so set the default date to 2000
87 * and hope it will never be used in the next century. */
88 2 snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
89 y + 2000, month, d, h, minute, sec);
90 2 return av_dict_set(&s->metadata, key, datetime, 0);
91 }
92
93 4 static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,
94 unsigned int size, const char *key)
95 {
96 4 AVIOContext *pb = s->pb;
97 char *value;
98 int ret;
99
100 4 avio_seek(pb, offset, SEEK_SET);
101
102 4 value = av_mallocz(size + 1);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!value)
104 return AVERROR(ENOMEM);
105
106 4 ret = avio_read(s->pb, value, size);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < size) {
108 av_free(value);
109 return ret < 0 ? ret : AVERROR_EOF;
110 }
111
112 4 return av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
113 }
114
115 2 static int dss_read_header(AVFormatContext *s)
116 {
117 2 DSSDemuxContext *ctx = s->priv_data;
118 2 AVIOContext *pb = s->pb;
119 AVStream *st;
120 int64_t ret64;
121 int ret, version;
122
123 2 st = avformat_new_stream(s, NULL);
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!st)
125 return AVERROR(ENOMEM);
126
127 2 version = avio_r8(pb);
128 2 ctx->dss_header_size = version * DSS_BLOCK_SIZE;
129
130 2 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_AUTHOR,
131 DSS_AUTHOR_SIZE, "author");
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret)
133 return ret;
134
135 2 ret = dss_read_metadata_date(s, DSS_HEAD_OFFSET_END_TIME, "date");
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret)
137 return ret;
138
139 2 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_COMMENT,
140 DSS_COMMENT_SIZE, "comment");
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret)
142 return ret;
143
144 2 avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
145 2 ctx->audio_codec = avio_r8(pb);
146
147
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
148 1 st->codecpar->codec_id = AV_CODEC_ID_DSS_SP;
149 1 st->codecpar->sample_rate = 11025;
150 1 s->bit_rate = 8 * (DSS_FRAME_SIZE - 1) * st->codecpar->sample_rate
151 1 * 512 / (506 * 264);
152
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
153 1 st->codecpar->codec_id = AV_CODEC_ID_G723_1;
154 1 st->codecpar->sample_rate = 8000;
155 } else {
156 avpriv_request_sample(s, "Support for codec %x in DSS",
157 ctx->audio_codec);
158 return AVERROR_PATCHWELCOME;
159 }
160
161 2 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
162 2 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
163
164 2 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
165 2 st->start_time = 0;
166
167 /* Jump over header */
168
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret64 = avio_seek(pb, ctx->dss_header_size, SEEK_SET)) < 0)
170 return (int)ret64;
171
172 2 ctx->counter = 0;
173 2 ctx->swap = 0;
174
175 2 return 0;
176 }
177
178 8 static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
179 {
180 8 DSSDemuxContext *ctx = s->priv_data;
181 8 AVIOContext *pb = s->pb;
182
183 8 avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
184 8 ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
185 8 }
186
187 50 static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *data)
188 {
189 int i;
190
191
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
50 if (ctx->swap) {
192
2/2
✓ Branch 0 taken 500 times.
✓ Branch 1 taken 25 times.
525 for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
193 500 data[i] = data[i + 4];
194
195 /* Zero the padding. */
196 25 data[DSS_FRAME_SIZE] = 0;
197 25 data[1] = ctx->dss_sp_swap_byte;
198 } else {
199 25 ctx->dss_sp_swap_byte = data[DSS_FRAME_SIZE - 2];
200 }
201
202 /* make sure byte 40 is always 0 */
203 50 data[DSS_FRAME_SIZE - 2] = 0;
204 50 ctx->swap ^= 1;
205 50 }
206
207 50 static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
208 {
209 50 DSSDemuxContext *ctx = s->priv_data;
210 50 int read_size, ret, offset = 0, buff_offset = 0;
211 50 int64_t pos = avio_tell(s->pb);
212
213
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 48 times.
50 if (ctx->counter == 0)
214 2 dss_skip_audio_header(s, pkt);
215
216
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
50 if (ctx->swap) {
217 25 read_size = DSS_FRAME_SIZE - 2;
218 25 buff_offset = 3;
219 } else
220 25 read_size = DSS_FRAME_SIZE;
221
222 50 ret = av_new_packet(pkt, DSS_FRAME_SIZE);
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
224 return ret;
225
226 50 pkt->duration = 264;
227 50 pkt->pos = pos;
228 50 pkt->stream_index = 0;
229
230
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 47 times.
50 if (ctx->counter < read_size) {
231 3 ret = avio_read(s->pb, pkt->data + buff_offset,
232 ctx->counter);
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < ctx->counter)
234 goto error_eof;
235
236 3 offset = ctx->counter;
237 3 dss_skip_audio_header(s, pkt);
238 }
239 50 ctx->counter -= read_size;
240
241 /* This will write one byte into pkt's padding if buff_offset == 3 */
242 50 ret = avio_read(s->pb, pkt->data + offset + buff_offset,
243 read_size - offset);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < read_size - offset)
245 goto error_eof;
246
247 50 dss_sp_byte_swap(ctx, pkt->data);
248
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ctx->dss_sp_swap_byte < 0) {
250 return AVERROR(EAGAIN);
251 }
252
253 50 return 0;
254
255 error_eof:
256 return ret < 0 ? ret : AVERROR_EOF;
257 }
258
259 50 static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
260 {
261 50 DSSDemuxContext *ctx = s->priv_data;
262 50 AVStream *st = s->streams[0];
263 int size, byte, ret, offset;
264 50 int64_t pos = avio_tell(s->pb);
265
266
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 49 times.
50 if (ctx->counter == 0)
267 1 dss_skip_audio_header(s, pkt);
268
269 /* We make one byte-step here. Don't forget to add offset. */
270 50 byte = avio_r8(s->pb);
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (byte == 0xff)
272 return AVERROR_INVALIDDATA;
273
274 50 size = frame_size[byte & 3];
275
276 50 ctx->packet_size = size;
277 50 ctx->counter--;
278
279 50 ret = av_new_packet(pkt, size);
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
281 return ret;
282 50 pkt->pos = pos;
283
284 50 pkt->data[0] = byte;
285 50 offset = 1;
286 50 pkt->duration = 240;
287 50 s->bit_rate = 8LL * size-- * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
288
289 50 pkt->stream_index = 0;
290
291
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 48 times.
50 if (ctx->counter < size) {
292 2 ret = avio_read(s->pb, pkt->data + offset,
293 ctx->counter);
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < ctx->counter)
295 return ret < 0 ? ret : AVERROR_EOF;
296
297 2 offset += ctx->counter;
298 2 size -= ctx->counter;
299 2 ctx->counter = 0;
300 2 dss_skip_audio_header(s, pkt);
301 }
302 50 ctx->counter -= size;
303
304 50 ret = avio_read(s->pb, pkt->data + offset, size);
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < size)
306 return ret < 0 ? ret : AVERROR_EOF;
307
308 50 return 0;
309 }
310
311 100 static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
312 {
313 100 DSSDemuxContext *ctx = s->priv_data;
314
315
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 50 times.
100 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
316 50 return dss_sp_read_packet(s, pkt);
317 else
318 50 return dss_723_1_read_packet(s, pkt);
319 }
320
321 static int dss_read_seek(AVFormatContext *s, int stream_index,
322 int64_t timestamp, int flags)
323 {
324 DSSDemuxContext *ctx = s->priv_data;
325 int64_t ret, seekto;
326 uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
327 int offset;
328
329 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
330 seekto = timestamp / 264 * 41 / 506 * 512;
331 else
332 seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
333
334 if (seekto < 0)
335 seekto = 0;
336
337 seekto += ctx->dss_header_size;
338
339 ret = avio_seek(s->pb, seekto, SEEK_SET);
340 if (ret < 0)
341 return ret;
342
343 ret = ffio_read_size(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
344 if (ret < 0)
345 return ret;
346 ctx->swap = !!(header[0] & 0x80);
347 offset = 2*header[1] + 2*ctx->swap;
348 if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
349 return AVERROR_INVALIDDATA;
350 if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
351 ctx->counter = 0;
352 offset = avio_skip(s->pb, -DSS_AUDIO_BLOCK_HEADER_SIZE);
353 } else {
354 ctx->counter = DSS_BLOCK_SIZE - offset;
355 offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
356 }
357 ctx->dss_sp_swap_byte = -1;
358 return 0;
359 }
360
361
362 const FFInputFormat ff_dss_demuxer = {
363 .p.name = "dss",
364 .p.long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
365 .p.extensions = "dss",
366 .priv_data_size = sizeof(DSSDemuxContext),
367 .read_probe = dss_probe,
368 .read_header = dss_read_header,
369 .read_packet = dss_read_packet,
370 .read_seek = dss_read_seek,
371 };
372