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