FFmpeg
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 
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;
53 
57 
58 static int dss_probe(const AVProbeData *p)
59 {
60  if ( AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
61  && AV_RL32(p->buf) != MKTAG(0x3, 'd', 's', 's'))
62  return 0;
63 
64  return AVPROBE_SCORE_MAX;
65 }
66 
67 static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
68  const char *key)
69 {
70  AVIOContext *pb = s->pb;
71  char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
72  int y, month, d, h, minute, sec;
73  int ret;
74 
75  avio_seek(pb, offset, SEEK_SET);
76 
77  ret = avio_read(s->pb, string, DSS_TIME_SIZE);
78  if (ret < DSS_TIME_SIZE)
79  return ret < 0 ? ret : AVERROR_EOF;
80 
81  if (sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec) != 6)
82  return AVERROR_INVALIDDATA;
83  /* We deal with a two-digit year here, so set the default date to 2000
84  * and hope it will never be used in the next century. */
85  snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
86  y + 2000, month, d, h, minute, sec);
87  return av_dict_set(&s->metadata, key, datetime, 0);
88 }
89 
91  unsigned int size, const char *key)
92 {
93  AVIOContext *pb = s->pb;
94  char *value;
95  int ret;
96 
97  avio_seek(pb, offset, SEEK_SET);
98 
99  value = av_mallocz(size + 1);
100  if (!value)
101  return AVERROR(ENOMEM);
102 
103  ret = avio_read(s->pb, value, size);
104  if (ret < size) {
105  av_free(value);
106  return ret < 0 ? ret : AVERROR_EOF;
107  }
108 
109  return av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
110 }
111 
113 {
115  AVIOContext *pb = s->pb;
116  AVStream *st;
117  int ret, version;
118 
119  st = avformat_new_stream(s, NULL);
120  if (!st)
121  return AVERROR(ENOMEM);
122 
123  version = avio_r8(pb);
124  ctx->dss_header_size = version * DSS_BLOCK_SIZE;
125 
127  DSS_AUTHOR_SIZE, "author");
128  if (ret)
129  return ret;
130 
132  if (ret)
133  return ret;
134 
136  DSS_COMMENT_SIZE, "comment");
137  if (ret)
138  return ret;
139 
140  avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
141  ctx->audio_codec = avio_r8(pb);
142 
143  if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
145  st->codecpar->sample_rate = 11025;
146  s->bit_rate = 8 * (DSS_FRAME_SIZE - 1) * st->codecpar->sample_rate
147  * 512 / (506 * 264);
148  } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
150  st->codecpar->sample_rate = 8000;
151  } else {
152  avpriv_request_sample(s, "Support for codec %x in DSS",
153  ctx->audio_codec);
154  return AVERROR_PATCHWELCOME;
155  }
156 
159  st->codecpar->channels = 1;
160 
161  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
162  st->start_time = 0;
163 
164  /* Jump over header */
165 
166  if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
167  return AVERROR(EIO);
168 
169  ctx->counter = 0;
170  ctx->swap = 0;
171 
172  return 0;
173 }
174 
176 {
178  AVIOContext *pb = s->pb;
179 
182 }
183 
185 {
186  int i;
187 
188  if (ctx->swap) {
189  for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
190  data[i] = data[i + 4];
191 
192  /* Zero the padding. */
193  data[DSS_FRAME_SIZE] = 0;
194  data[1] = ctx->dss_sp_swap_byte;
195  } else {
196  ctx->dss_sp_swap_byte = data[DSS_FRAME_SIZE - 2];
197  }
198 
199  /* make sure byte 40 is always 0 */
200  data[DSS_FRAME_SIZE - 2] = 0;
201  ctx->swap ^= 1;
202 }
203 
205 {
207  int read_size, ret, offset = 0, buff_offset = 0;
208  int64_t pos = avio_tell(s->pb);
209 
210  if (ctx->counter == 0)
211  dss_skip_audio_header(s, pkt);
212 
213  if (ctx->swap) {
214  read_size = DSS_FRAME_SIZE - 2;
215  buff_offset = 3;
216  } else
217  read_size = DSS_FRAME_SIZE;
218 
219  ret = av_new_packet(pkt, DSS_FRAME_SIZE);
220  if (ret < 0)
221  return ret;
222 
223  pkt->duration = 264;
224  pkt->pos = pos;
225  pkt->stream_index = 0;
226 
227  if (ctx->counter < read_size) {
228  ret = avio_read(s->pb, pkt->data + buff_offset,
229  ctx->counter);
230  if (ret < ctx->counter)
231  goto error_eof;
232 
233  offset = ctx->counter;
234  dss_skip_audio_header(s, pkt);
235  }
236  ctx->counter -= read_size;
237 
238  /* This will write one byte into pkt's padding if buff_offset == 3 */
239  ret = avio_read(s->pb, pkt->data + offset + buff_offset,
240  read_size - offset);
241  if (ret < read_size - offset)
242  goto error_eof;
243 
244  dss_sp_byte_swap(ctx, pkt->data);
245 
246  if (ctx->dss_sp_swap_byte < 0) {
247  return AVERROR(EAGAIN);
248  }
249 
250  return 0;
251 
252 error_eof:
253  return ret < 0 ? ret : AVERROR_EOF;
254 }
255 
257 {
259  AVStream *st = s->streams[0];
260  int size, byte, ret, offset;
261  int64_t pos = avio_tell(s->pb);
262 
263  if (ctx->counter == 0)
264  dss_skip_audio_header(s, pkt);
265 
266  /* We make one byte-step here. Don't forget to add offset. */
267  byte = avio_r8(s->pb);
268  if (byte == 0xff)
269  return AVERROR_INVALIDDATA;
270 
271  size = frame_size[byte & 3];
272 
273  ctx->packet_size = size;
274  ctx->counter--;
275 
276  ret = av_new_packet(pkt, size);
277  if (ret < 0)
278  return ret;
279  pkt->pos = pos;
280 
281  pkt->data[0] = byte;
282  offset = 1;
283  pkt->duration = 240;
284  s->bit_rate = 8LL * size-- * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
285 
286  pkt->stream_index = 0;
287 
288  if (ctx->counter < size) {
289  ret = avio_read(s->pb, pkt->data + offset,
290  ctx->counter);
291  if (ret < ctx->counter)
292  return ret < 0 ? ret : AVERROR_EOF;
293 
294  offset += ctx->counter;
295  size -= ctx->counter;
296  ctx->counter = 0;
297  dss_skip_audio_header(s, pkt);
298  }
299  ctx->counter -= size;
300 
301  ret = avio_read(s->pb, pkt->data + offset, size);
302  if (ret < size)
303  return ret < 0 ? ret : AVERROR_EOF;
304 
305  return 0;
306 }
307 
309 {
311 
312  if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
313  return dss_sp_read_packet(s, pkt);
314  else
315  return dss_723_1_read_packet(s, pkt);
316 }
317 
318 static int dss_read_seek(AVFormatContext *s, int stream_index,
319  int64_t timestamp, int flags)
320 {
322  int64_t ret, seekto;
324  int offset;
325 
326  if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
327  seekto = timestamp / 264 * 41 / 506 * 512;
328  else
329  seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
330 
331  if (seekto < 0)
332  seekto = 0;
333 
334  seekto += ctx->dss_header_size;
335 
336  ret = avio_seek(s->pb, seekto, SEEK_SET);
337  if (ret < 0)
338  return ret;
339 
341  ctx->swap = !!(header[0] & 0x80);
342  offset = 2*header[1] + 2*ctx->swap;
343  if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
344  return AVERROR_INVALIDDATA;
345  if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
346  ctx->counter = 0;
348  } else {
349  ctx->counter = DSS_BLOCK_SIZE - offset;
350  offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
351  }
352  ctx->dss_sp_swap_byte = -1;
353  return 0;
354 }
355 
356 
358  .name = "dss",
359  .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
360  .priv_data_size = sizeof(DSSDemuxContext),
365  .extensions = "dss"
366 };
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
version
Definition: libkvazaar.c:320
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
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:4999
#define avpriv_request_sample(...)
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:357
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
int64_t bit_rate
Total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:1369
#define DSS_AUDIO_BLOCK_HEADER_SIZE
Definition: dss.c:43
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
const char * key
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:175
AVPacket * pkt
Definition: movenc.c:59
Format I/O context.
Definition: avformat.h:1247
#define DSS_HEAD_OFFSET_AUTHOR
Definition: dss.c:28
static int dss_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: dss.c:318
uint8_t
static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:256
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
#define DSS_BLOCK_SIZE
Definition: dss.c:42
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4553
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1315
static int dss_read_header(AVFormatContext *s)
Definition: dss.c:112
uint8_t * data
Definition: packet.h:369
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
static const uint8_t header[24]
Definition: sdr2.c:67
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1489
#define DSS_AUTHOR_SIZE
Definition: dss.c:29
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:117
unsigned int pos
Definition: spdifenc.c:412
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:204
static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:308
int counter
Definition: dss.c:50
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:624
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
audio channel layout utility functions
#define DSS_HEAD_OFFSET_END_TIME
Definition: dss.c:32
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
#define DSS_COMMENT_SIZE
Definition: dss.c:40
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
#define DSS_HEAD_OFFSET_COMMENT
Definition: dss.c:39
static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset, unsigned int size, const char *key)
Definition: dss.c:90
#define DSS_TIME_SIZE
Definition: dss.c:33
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
Stream structure.
Definition: avformat.h:884
#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
static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *data)
Definition: dss.c:184
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:91
AVIOContext * pb
I/O context.
Definition: avformat.h:1289
#define DSS_HEAD_OFFSET_ACODEC
Definition: dss.c:35
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:70
static const uint8_t frame_size[4]
Definition: dss.c:46
#define snprintf
Definition: snprintf.h:34
int packet_size
Definition: dss.c:54
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int swap
Definition: dss.c:51
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int dss_sp_swap_byte
Definition: dss.c:52
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int sample_rate
Audio only.
Definition: codec_par.h:170
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
Main libavformat public API header.
#define DSS_FRAME_SIZE
Definition: dss.c:44
#define DSS_ACODEC_G723_1
Definition: dss.c:37
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:923
static int dss_probe(const AVProbeData *p)
Definition: dss.c:58
#define DSS_ACODEC_DSS_SP
Definition: dss.c:36
#define av_free(p)
int dss_header_size
Definition: dss.c:55
void * priv_data
Format private data.
Definition: avformat.h:1275
static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset, const char *key)
Definition: dss.c:67
int channels
Audio only.
Definition: codec_par.h:166
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
unsigned int audio_codec
Definition: dss.c:49
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1049
int stream_index
Definition: packet.h:371
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:478
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:91
This structure stores compressed data.
Definition: packet.h:346
int i
Definition: input.c:407