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