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