FFmpeg  4.3.7
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;
54 
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 
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 
114 {
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 
128  DSS_AUTHOR_SIZE, "author");
129  if (ret)
130  return ret;
131 
133  if (ret)
134  return ret;
135 
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) {
146  st->codecpar->sample_rate = 11025;
147  } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
149  st->codecpar->sample_rate = 8000;
150  } else {
151  avpriv_request_sample(s, "Support for codec %x in DSS",
152  ctx->audio_codec);
153  return AVERROR_PATCHWELCOME;
154  }
155 
158  st->codecpar->channels = 1;
159 
160  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
161  st->start_time = 0;
162 
163  /* Jump over header */
164 
165  if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
166  return AVERROR(EIO);
167 
168  ctx->counter = 0;
169  ctx->swap = 0;
170 
171  return 0;
172 }
173 
175 {
177  AVIOContext *pb = s->pb;
178 
181 }
182 
184  uint8_t *dst, const uint8_t *src)
185 {
186  int i;
187 
188  if (ctx->swap) {
189  for (i = 3; i < DSS_FRAME_SIZE; i += 2)
190  dst[i] = src[i];
191 
192  for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
193  dst[i] = src[i + 4];
194 
195  dst[1] = ctx->dss_sp_swap_byte;
196  } else {
197  memcpy(dst, src, DSS_FRAME_SIZE);
198  ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
199  }
200 
201  /* make sure byte 40 is always 0 */
202  dst[DSS_FRAME_SIZE - 2] = 0;
203  ctx->swap ^= 1;
204 }
205 
207 {
209  AVStream *st = s->streams[0];
210  int read_size, ret, offset = 0, buff_offset = 0;
211  int64_t pos = avio_tell(s->pb);
212 
213  if (ctx->counter == 0)
214  dss_skip_audio_header(s, pkt);
215 
216  if (ctx->swap) {
217  read_size = DSS_FRAME_SIZE - 2;
218  buff_offset = 3;
219  } else
220  read_size = DSS_FRAME_SIZE;
221 
222  ctx->counter -= read_size;
223  ctx->packet_size = DSS_FRAME_SIZE - 1;
224 
225  ret = av_new_packet(pkt, DSS_FRAME_SIZE);
226  if (ret < 0)
227  return ret;
228 
229  pkt->duration = 264;
230  pkt->pos = pos;
231  pkt->stream_index = 0;
232  s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
233 
234  if (ctx->counter < 0) {
235  int size2 = ctx->counter + read_size;
236 
237  ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
238  size2 - offset);
239  if (ret < size2 - offset)
240  goto error_eof;
241 
242  dss_skip_audio_header(s, pkt);
243  offset = size2;
244  }
245 
246  ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
247  read_size - offset);
248  if (ret < read_size - offset)
249  goto error_eof;
250 
251  dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
252 
253  if (ctx->dss_sp_swap_byte < 0) {
254  return AVERROR(EAGAIN);
255  }
256 
257  return pkt->size;
258 
259 error_eof:
260  return ret < 0 ? ret : AVERROR_EOF;
261 }
262 
264 {
266  AVStream *st = s->streams[0];
267  int size, byte, ret, offset;
268  int64_t pos = avio_tell(s->pb);
269 
270  if (ctx->counter == 0)
271  dss_skip_audio_header(s, pkt);
272 
273  /* We make one byte-step here. Don't forget to add offset. */
274  byte = avio_r8(s->pb);
275  if (byte == 0xff)
276  return AVERROR_INVALIDDATA;
277 
278  size = frame_size[byte & 3];
279 
280  ctx->packet_size = size;
281  ctx->counter -= size;
282 
283  ret = av_new_packet(pkt, size);
284  if (ret < 0)
285  return ret;
286  pkt->pos = pos;
287 
288  pkt->data[0] = byte;
289  offset = 1;
290  pkt->duration = 240;
291  s->bit_rate = 8LL * size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
292 
293  pkt->stream_index = 0;
294 
295  if (ctx->counter < 0) {
296  int size2 = ctx->counter + size;
297 
298  ret = avio_read(s->pb, pkt->data + offset,
299  size2 - offset);
300  if (ret < size2 - offset) {
301  return ret < 0 ? ret : AVERROR_EOF;
302  }
303 
304  dss_skip_audio_header(s, pkt);
305  offset = size2;
306  }
307 
308  ret = avio_read(s->pb, pkt->data + offset, size - offset);
309  if (ret < size - offset) {
310  return ret < 0 ? ret : AVERROR_EOF;
311  }
312 
313  return pkt->size;
314 }
315 
317 {
319 
320  if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
321  return dss_sp_read_packet(s, pkt);
322  else
323  return dss_723_1_read_packet(s, pkt);
324 }
325 
326 static int dss_read_seek(AVFormatContext *s, int stream_index,
327  int64_t timestamp, int flags)
328 {
330  int64_t ret, seekto;
332  int offset;
333 
334  if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
335  seekto = timestamp / 264 * 41 / 506 * 512;
336  else
337  seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
338 
339  if (seekto < 0)
340  seekto = 0;
341 
342  seekto += ctx->dss_header_size;
343 
344  ret = avio_seek(s->pb, seekto, SEEK_SET);
345  if (ret < 0)
346  return ret;
347 
349  ctx->swap = !!(header[0] & 0x80);
350  offset = 2*header[1] + 2*ctx->swap;
351  if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
352  return AVERROR_INVALIDDATA;
353  if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
354  ctx->counter = 0;
356  } else {
357  ctx->counter = DSS_BLOCK_SIZE - offset;
358  offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
359  }
360  ctx->dss_sp_swap_byte = -1;
361  return 0;
362 }
363 
364 
366  .name = "dss",
367  .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
368  .priv_data_size = sizeof(DSSDemuxContext),
373  .extensions = "dss"
374 };
int8_t dss_sp_buf[DSS_FRAME_SIZE+1]
Definition: dss.c:53
#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:292
int size
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:375
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:4948
#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:365
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int size
Definition: packet.h:356
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:241
int64_t bit_rate
Total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:1473
#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:329
const char * key
static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:174
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1351
#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:326
uint8_t
static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:263
#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:373
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4526
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
static int dss_read_header(AVFormatContext *s)
Definition: dss.c:113
uint8_t * data
Definition: packet.h:355
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:625
#define src
Definition: vp8dsp.c:254
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:88
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
#define DSS_AUTHOR_SIZE
Definition: dss.c:29
#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:188
unsigned int pos
Definition: spdifenc.c:412
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
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 int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:206
static const uint8_t offset[127][2]
Definition: vf_spp.c:93
static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:316
int counter
Definition: dss.c:50
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:616
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
static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *dst, const uint8_t *src)
Definition: dss.c:183
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
#define AV_RL32
Definition: intreadwrite.h:146
#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:91
#define DSS_TIME_SIZE
Definition: dss.c:33
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
Stream structure.
Definition: avformat.h:876
#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
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
#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
double value
Definition: eval.c:98
static const uint8_t frame_size[4]
Definition: dss.c:46
#define snprintf
Definition: snprintf.h:34
int packet_size
Definition: dss.c:55
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:565
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:915
static int dss_probe(const AVProbeData *p)
Definition: dss.c:59
#define DSS_ACODEC_DSS_SP
Definition: dss.c:36
#define av_free(p)
int dss_header_size
Definition: dss.c:56
void * priv_data
Format private data.
Definition: avformat.h:1379
static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset, const char *key)
Definition: dss.c:68
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:650
unsigned int audio_codec
Definition: dss.c:49
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
int stream_index
Definition: packet.h:357
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:406
This structure stores compressed data.
Definition: packet.h:332