FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/dss.c
Date: 2024-03-28 04:31:58
Exec Total Coverage
Lines: 130 175 74.3%
Functions: 9 10 90.0%
Branches: 39 78 50.0%

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