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