FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dss.c
Go to the documentation of this file.
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/attributes.h"
23 #include "libavutil/bswap.h"
25 #include "libavutil/intreadwrite.h"
26 
27 #include "avformat.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_HEADER_SIZE (DSS_BLOCK_SIZE * 2)
46 #define DSS_AUDIO_BLOCK_HEADER_SIZE 6
47 #define DSS_FRAME_SIZE 42
48 
49 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
50 
51 typedef struct DSSDemuxContext {
52  unsigned int audio_codec;
53  int counter;
54  int swap;
56  int8_t *dss_sp_buf;
57 
60 
61 static int dss_probe(AVProbeData *p)
62 {
63  if (AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's'))
64  return 0;
65 
66  return AVPROBE_SCORE_MAX;
67 }
68 
69 static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
70  const char *key)
71 {
72  AVIOContext *pb = s->pb;
73  char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
74  int y, month, d, h, minute, sec;
75  int ret;
76 
77  avio_seek(pb, offset, SEEK_SET);
78 
79  ret = avio_read(s->pb, string, DSS_TIME_SIZE);
80  if (ret < DSS_TIME_SIZE)
81  return ret < 0 ? ret : AVERROR_EOF;
82 
83  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  snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
88  y + 2000, month, d, h, minute, sec);
89  return av_dict_set(&s->metadata, key, datetime, 0);
90 }
91 
93  unsigned int size, const char *key)
94 {
95  AVIOContext *pb = s->pb;
96  char *value;
97  int ret;
98 
99  avio_seek(pb, offset, SEEK_SET);
100 
101  value = av_mallocz(size + 1);
102  if (!value)
103  return AVERROR(ENOMEM);
104 
105  ret = avio_read(s->pb, value, size);
106  if (ret < size) {
107  ret = ret < 0 ? ret : AVERROR_EOF;
108  goto exit;
109  }
110 
111  ret = av_dict_set(&s->metadata, key, value, 0);
112 
113 exit:
114  av_free(value);
115  return ret;
116 }
117 
119 {
120  DSSDemuxContext *ctx = s->priv_data;
121  AVIOContext *pb = s->pb;
122  AVStream *st;
123  int ret;
124 
125  st = avformat_new_stream(s, NULL);
126  if (!st)
127  return AVERROR(ENOMEM);
128 
130  DSS_AUTHOR_SIZE, "author");
131  if (ret)
132  return ret;
133 
135  if (ret)
136  return ret;
137 
139  DSS_COMMENT_SIZE, "comment");
140  if (ret)
141  return ret;
142 
143  avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
144  ctx->audio_codec = avio_r8(pb);
145 
146  if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
148  st->codec->sample_rate = 11025;
149  } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
151  st->codec->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 
160  st->codec->channels = 1;
161 
162  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
163  st->start_time = 0;
164 
165  /* Jump over header */
166 
167  if (avio_seek(pb, DSS_HEADER_SIZE, SEEK_SET) != DSS_HEADER_SIZE)
168  return AVERROR(EIO);
169 
170  ctx->counter = 0;
171  ctx->swap = 0;
172 
173  ctx->dss_sp_buf = av_malloc(DSS_FRAME_SIZE + 1);
174  if (!ctx->dss_sp_buf)
175  return AVERROR(ENOMEM);
176 
177  return 0;
178 }
179 
181 {
182  DSSDemuxContext *ctx = s->priv_data;
183  AVIOContext *pb = s->pb;
184 
187 }
188 
190  uint8_t *dst, const uint8_t *src)
191 {
192  int i;
193 
194  if (ctx->swap) {
195  for (i = 3; i < DSS_FRAME_SIZE; i += 2)
196  dst[i] = src[i];
197 
198  for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
199  dst[i] = src[i + 4];
200 
201  dst[1] = ctx->dss_sp_swap_byte;
202  } else {
203  memcpy(dst, src, DSS_FRAME_SIZE);
204  ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
205  }
206 
207  /* make sure byte 40 is always 0 */
208  dst[DSS_FRAME_SIZE - 2] = 0;
209  ctx->swap ^= 1;
210 }
211 
213 {
214  DSSDemuxContext *ctx = s->priv_data;
215  AVStream *st = s->streams[0];
216  int read_size, ret, offset = 0, buff_offset = 0;
217  int64_t pos = avio_tell(s->pb);
218 
219  if (ctx->counter == 0)
220  dss_skip_audio_header(s, pkt);
221 
222  if (ctx->swap) {
223  read_size = DSS_FRAME_SIZE - 2;
224  buff_offset = 3;
225  } else
226  read_size = DSS_FRAME_SIZE;
227 
228  ctx->counter -= read_size;
229  ctx->packet_size = DSS_FRAME_SIZE - 1;
230 
231  ret = av_new_packet(pkt, DSS_FRAME_SIZE);
232  if (ret < 0)
233  return ret;
234 
235  pkt->duration = 264;
236  pkt->pos = pos;
237  pkt->stream_index = 0;
238  s->bit_rate = 8LL * ctx->packet_size * st->codec->sample_rate * 512 / (506 * pkt->duration);
239 
240  if (ctx->counter < 0) {
241  int size2 = ctx->counter + read_size;
242 
243  ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
244  size2 - offset);
245  if (ret < size2 - offset)
246  goto error_eof;
247 
248  dss_skip_audio_header(s, pkt);
249  offset = size2;
250  }
251 
252  ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
253  read_size - offset);
254  if (ret < read_size - offset)
255  goto error_eof;
256 
257  dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
258 
259  if (ctx->dss_sp_swap_byte < 0) {
260  ret = AVERROR(EAGAIN);
261  goto error_eof;
262  }
263 
264  if (pkt->data[0] == 0xff)
265  return AVERROR_INVALIDDATA;
266 
267  return pkt->size;
268 
269 error_eof:
270  av_free_packet(pkt);
271  return ret < 0 ? ret : AVERROR_EOF;
272 }
273 
275 {
276  DSSDemuxContext *ctx = s->priv_data;
277  AVStream *st = s->streams[0];
278  int size, byte, ret, offset;
279  int64_t pos = avio_tell(s->pb);
280 
281  if (ctx->counter == 0)
282  dss_skip_audio_header(s, pkt);
283 
284  /* We make one byte-step here. Don't forget to add offset. */
285  byte = avio_r8(s->pb);
286  if (byte == 0xff)
287  return AVERROR_INVALIDDATA;
288 
289  size = frame_size[byte & 3];
290 
291  ctx->packet_size = size;
292  ctx->counter -= size;
293 
294  ret = av_new_packet(pkt, size);
295  if (ret < 0)
296  return ret;
297  pkt->pos = pos;
298 
299  pkt->data[0] = byte;
300  offset = 1;
301  pkt->duration = 240;
302  s->bit_rate = 8LL * size * st->codec->sample_rate * 512 / (506 * pkt->duration);
303 
304  pkt->stream_index = 0;
305 
306  if (ctx->counter < 0) {
307  int size2 = ctx->counter + size;
308 
309  ret = avio_read(s->pb, pkt->data + offset,
310  size2 - offset);
311  if (ret < size2 - offset) {
312  av_free_packet(pkt);
313  return ret < 0 ? ret : AVERROR_EOF;
314  }
315 
316  dss_skip_audio_header(s, pkt);
317  offset = size2;
318  }
319 
320  ret = avio_read(s->pb, pkt->data + offset, size - offset);
321  if (ret < size - offset) {
322  av_free_packet(pkt);
323  return ret < 0 ? ret : AVERROR_EOF;
324  }
325 
326  return pkt->size;
327 }
328 
330 {
331  DSSDemuxContext *ctx = s->priv_data;
332 
333  if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
334  return dss_sp_read_packet(s, pkt);
335  else
336  return dss_723_1_read_packet(s, pkt);
337 }
338 
340 {
341  DSSDemuxContext *ctx = s->priv_data;
342 
343  av_freep(&ctx->dss_sp_buf);
344 
345  return 0;
346 }
347 
348 static int dss_read_seek(AVFormatContext *s, int stream_index,
349  int64_t timestamp, int flags)
350 {
351  DSSDemuxContext *ctx = s->priv_data;
352  int64_t ret, seekto;
354  int offset;
355 
356  if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
357  seekto = timestamp / 264 * 41 / 506 * 512;
358  else
359  seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
360 
361  if (seekto < 0)
362  seekto = 0;
363 
364  seekto += DSS_HEADER_SIZE;
365 
366  ret = avio_seek(s->pb, seekto, SEEK_SET);
367  if (ret < 0)
368  return ret;
369 
371  ctx->swap = !!(header[0] & 0x80);
372  offset = 2*header[1] + 2*ctx->swap;
373  if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
374  return AVERROR_INVALIDDATA;
375  if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
376  ctx->counter = 0;
378  } else {
379  ctx->counter = DSS_BLOCK_SIZE - offset;
380  offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
381  }
382  ctx->dss_sp_swap_byte = -1;
383  return 0;
384 }
385 
386 
388  .name = "dss",
389  .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
390  .priv_data_size = sizeof(DSSDemuxContext),
396  .extensions = "dss"
397 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1448
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4083
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
AVInputFormat ff_dss_demuxer
Definition: dss.c:387
int size
Definition: avcodec.h:1424
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
#define DSS_AUDIO_BLOCK_HEADER_SIZE
Definition: dss.c:46
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:277
static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:180
static AVPacket pkt
Macro definitions for various function/variable attributes.
Format I/O context.
Definition: avformat.h:1273
#define DSS_HEAD_OFFSET_AUTHOR
Definition: dss.c:30
static int dss_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: dss.c:348
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
#define av_malloc(s)
static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:274
#define DSS_BLOCK_SIZE
Definition: dss.c:44
static int dss_read_close(AVFormatContext *s)
Definition: dss.c:339
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
static int dss_read_header(AVFormatContext *s)
Definition: dss.c:118
uint8_t * data
Definition: avcodec.h:1423
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int8_t * dss_sp_buf
Definition: dss.c:56
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
static const uint8_t header[24]
Definition: sdr2.c:67
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1441
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:538
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1485
#define DSS_AUTHOR_SIZE
Definition: dss.c:31
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:212
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:329
int counter
Definition: dss.c:53
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2323
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:529
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
audio channel layout utility functions
float y
#define DSS_HEAD_OFFSET_END_TIME
Definition: dss.c:34
static int dss_probe(AVProbeData *p)
Definition: dss.c:61
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
#define DSS_COMMENT_SIZE
Definition: dss.c:42
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *dst, const uint8_t *src)
Definition: dss.c:189
#define DSS_HEAD_OFFSET_COMMENT
Definition: dss.c:41
static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset, unsigned int size, const char *key)
Definition: dss.c:92
#define DSS_TIME_SIZE
Definition: dss.c:35
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:623
Stream structure.
Definition: avformat.h:842
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVS_Value src
Definition: avisynth_c.h:482
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:87
int sample_rate
samples per second
Definition: avcodec.h:2262
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
#define DSS_HEAD_OFFSET_ACODEC
Definition: dss.c:37
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:69
byte swapping routines
static const uint8_t frame_size[4]
Definition: dss.c:49
#define snprintf
Definition: snprintf.h:34
int packet_size
Definition: dss.c:58
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
int swap
Definition: dss.c:54
#define DSS_HEADER_SIZE
Definition: dss.c:45
static int flags
Definition: cpu.c:47
int dss_sp_swap_byte
Definition: dss.c:55
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
Main libavformat public API header.
#define DSS_FRAME_SIZE
Definition: dss.c:47
#define DSS_ACODEC_G723_1
Definition: dss.c:39
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:894
#define DSS_ACODEC_DSS_SP
Definition: dss.c:38
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:2263
void * priv_data
Format private data.
Definition: avformat.h:1301
static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset, const char *key)
Definition: dss.c:69
int bit_rate
Total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:1375
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
unsigned int audio_codec
Definition: dss.c:52
int stream_index
Definition: avcodec.h:1425
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:330
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1400
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252