]> git.sesse.net Git - ffmpeg/blob - libavformat/dss.c
avformat: Remove unnecessary av_packet_unref()
[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;
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         ret = ret < 0 ? ret : AVERROR_EOF;
107         goto exit;
108     }
109
110     ret = av_dict_set(&s->metadata, key, value, 0);
111
112 exit:
113     av_free(value);
114     return ret;
115 }
116
117 static int dss_read_header(AVFormatContext *s)
118 {
119     DSSDemuxContext *ctx = s->priv_data;
120     AVIOContext *pb = s->pb;
121     AVStream *st;
122     int ret, version;
123
124     st = avformat_new_stream(s, NULL);
125     if (!st)
126         return AVERROR(ENOMEM);
127
128     version = avio_r8(pb);
129     ctx->dss_header_size = version * DSS_BLOCK_SIZE;
130
131     ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_AUTHOR,
132                                    DSS_AUTHOR_SIZE, "author");
133     if (ret)
134         return ret;
135
136     ret = dss_read_metadata_date(s, DSS_HEAD_OFFSET_END_TIME, "date");
137     if (ret)
138         return ret;
139
140     ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_COMMENT,
141                                    DSS_COMMENT_SIZE, "comment");
142     if (ret)
143         return ret;
144
145     avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
146     ctx->audio_codec = avio_r8(pb);
147
148     if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
149         st->codecpar->codec_id    = AV_CODEC_ID_DSS_SP;
150         st->codecpar->sample_rate = 11025;
151     } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
152         st->codecpar->codec_id    = AV_CODEC_ID_G723_1;
153         st->codecpar->sample_rate = 8000;
154     } else {
155         avpriv_request_sample(s, "Support for codec %x in DSS",
156                               ctx->audio_codec);
157         return AVERROR_PATCHWELCOME;
158     }
159
160     st->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
161     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
162     st->codecpar->channels       = 1;
163
164     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
165     st->start_time = 0;
166
167     /* Jump over header */
168
169     if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
170         return AVERROR(EIO);
171
172     ctx->counter = 0;
173     ctx->swap    = 0;
174
175     ctx->dss_sp_buf = av_malloc(DSS_FRAME_SIZE + 1);
176     if (!ctx->dss_sp_buf)
177         return AVERROR(ENOMEM);
178
179     return 0;
180 }
181
182 static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
183 {
184     DSSDemuxContext *ctx = s->priv_data;
185     AVIOContext *pb = s->pb;
186
187     avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
188     ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
189 }
190
191 static void dss_sp_byte_swap(DSSDemuxContext *ctx,
192                              uint8_t *dst, const uint8_t *src)
193 {
194     int i;
195
196     if (ctx->swap) {
197         for (i = 3; i < DSS_FRAME_SIZE; i += 2)
198             dst[i] = src[i];
199
200         for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
201             dst[i] = src[i + 4];
202
203         dst[1] = ctx->dss_sp_swap_byte;
204     } else {
205         memcpy(dst, src, DSS_FRAME_SIZE);
206         ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
207     }
208
209     /* make sure byte 40 is always 0 */
210     dst[DSS_FRAME_SIZE - 2] = 0;
211     ctx->swap             ^= 1;
212 }
213
214 static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
215 {
216     DSSDemuxContext *ctx = s->priv_data;
217     AVStream *st = s->streams[0];
218     int read_size, ret, offset = 0, buff_offset = 0;
219     int64_t pos = avio_tell(s->pb);
220
221     if (ctx->counter == 0)
222         dss_skip_audio_header(s, pkt);
223
224     if (ctx->swap) {
225         read_size   = DSS_FRAME_SIZE - 2;
226         buff_offset = 3;
227     } else
228         read_size = DSS_FRAME_SIZE;
229
230     ctx->counter -= read_size;
231     ctx->packet_size = DSS_FRAME_SIZE - 1;
232
233     ret = av_new_packet(pkt, DSS_FRAME_SIZE);
234     if (ret < 0)
235         return ret;
236
237     pkt->duration     = 264;
238     pkt->pos = pos;
239     pkt->stream_index = 0;
240     s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
241
242     if (ctx->counter < 0) {
243         int size2 = ctx->counter + read_size;
244
245         ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
246                         size2 - offset);
247         if (ret < size2 - offset)
248             goto error_eof;
249
250         dss_skip_audio_header(s, pkt);
251         offset = size2;
252     }
253
254     ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
255                     read_size - offset);
256     if (ret < read_size - offset)
257         goto error_eof;
258
259     dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
260
261     if (ctx->dss_sp_swap_byte < 0) {
262         return AVERROR(EAGAIN);
263     }
264
265     return pkt->size;
266
267 error_eof:
268     return ret < 0 ? ret : AVERROR_EOF;
269 }
270
271 static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
272 {
273     DSSDemuxContext *ctx = s->priv_data;
274     AVStream *st = s->streams[0];
275     int size, byte, ret, offset;
276     int64_t pos = avio_tell(s->pb);
277
278     if (ctx->counter == 0)
279         dss_skip_audio_header(s, pkt);
280
281     /* We make one byte-step here. Don't forget to add offset. */
282     byte = avio_r8(s->pb);
283     if (byte == 0xff)
284         return AVERROR_INVALIDDATA;
285
286     size = frame_size[byte & 3];
287
288     ctx->packet_size = size;
289     ctx->counter -= size;
290
291     ret = av_new_packet(pkt, size);
292     if (ret < 0)
293         return ret;
294     pkt->pos = pos;
295
296     pkt->data[0]  = byte;
297     offset        = 1;
298     pkt->duration = 240;
299     s->bit_rate = 8LL * size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
300
301     pkt->stream_index = 0;
302
303     if (ctx->counter < 0) {
304         int size2 = ctx->counter + size;
305
306         ret = avio_read(s->pb, pkt->data + offset,
307                         size2 - offset);
308         if (ret < size2 - offset) {
309             return ret < 0 ? ret : AVERROR_EOF;
310         }
311
312         dss_skip_audio_header(s, pkt);
313         offset = size2;
314     }
315
316     ret = avio_read(s->pb, pkt->data + offset, size - offset);
317     if (ret < size - offset) {
318         return ret < 0 ? ret : AVERROR_EOF;
319     }
320
321     return pkt->size;
322 }
323
324 static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
325 {
326     DSSDemuxContext *ctx = s->priv_data;
327
328     if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
329         return dss_sp_read_packet(s, pkt);
330     else
331         return dss_723_1_read_packet(s, pkt);
332 }
333
334 static int dss_read_close(AVFormatContext *s)
335 {
336     DSSDemuxContext *ctx = s->priv_data;
337
338     av_freep(&ctx->dss_sp_buf);
339
340     return 0;
341 }
342
343 static int dss_read_seek(AVFormatContext *s, int stream_index,
344                          int64_t timestamp, int flags)
345 {
346     DSSDemuxContext *ctx = s->priv_data;
347     int64_t ret, seekto;
348     uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
349     int offset;
350
351     if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
352         seekto = timestamp / 264 * 41 / 506 * 512;
353     else
354         seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
355
356     if (seekto < 0)
357         seekto = 0;
358
359     seekto += ctx->dss_header_size;
360
361     ret = avio_seek(s->pb, seekto, SEEK_SET);
362     if (ret < 0)
363         return ret;
364
365     avio_read(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
366     ctx->swap = !!(header[0] & 0x80);
367     offset = 2*header[1] + 2*ctx->swap;
368     if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
369         return AVERROR_INVALIDDATA;
370     if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
371         ctx->counter = 0;
372         offset = avio_skip(s->pb, -DSS_AUDIO_BLOCK_HEADER_SIZE);
373     } else {
374         ctx->counter = DSS_BLOCK_SIZE - offset;
375         offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
376     }
377     ctx->dss_sp_swap_byte = -1;
378     return 0;
379 }
380
381
382 AVInputFormat ff_dss_demuxer = {
383     .name           = "dss",
384     .long_name      = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
385     .priv_data_size = sizeof(DSSDemuxContext),
386     .read_probe     = dss_probe,
387     .read_header    = dss_read_header,
388     .read_packet    = dss_read_packet,
389     .read_close     = dss_read_close,
390     .read_seek      = dss_read_seek,
391     .extensions     = "dss"
392 };