FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dsfdec.c
Go to the documentation of this file.
1 /*
2  * DSD Stream File (DSF) demuxer
3  * Copyright (c) 2014 Peter Ross
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/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "id3v2.h"
26 
27 typedef struct {
28  uint64_t data_end;
29 } DSFContext;
30 
31 static int dsf_probe(AVProbeData *p)
32 {
33  if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
34  return 0;
35  return AVPROBE_SCORE_MAX;
36 }
37 
38 static const uint64_t dsf_channel_layout[] = {
39  0,
47 };
48 
49 static void read_id3(AVFormatContext *s, uint64_t id3pos)
50 {
51  ID3v2ExtraMeta *id3v2_extra_meta = NULL;
52  if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
53  return;
54 
55  ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
56  if (id3v2_extra_meta)
57  ff_id3v2_parse_apic(s, &id3v2_extra_meta);
58  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
59 }
60 
62 {
63  DSFContext *dsf = s->priv_data;
64  AVIOContext *pb = s->pb;
65  AVStream *st;
66  uint64_t id3pos;
67  unsigned int channel_type;
68 
69  avio_skip(pb, 4);
70  if (avio_rl64(pb) != 28)
71  return AVERROR_INVALIDDATA;
72 
73  /* create primary stream before any id3 coverart streams */
74  st = avformat_new_stream(s, NULL);
75  if (!st)
76  return AVERROR(ENOMEM);
77 
78  avio_skip(pb, 8);
79  id3pos = avio_rl64(pb);
80  if (pb->seekable) {
81  read_id3(s, id3pos);
82  avio_seek(pb, 28, SEEK_SET);
83  }
84 
85  /* fmt chunk */
86 
87  if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
88  return AVERROR_INVALIDDATA;
89 
90  if (avio_rl32(pb) != 1) {
91  avpriv_request_sample(s, "unknown format version");
92  return AVERROR_INVALIDDATA;
93  }
94 
95  if (avio_rl32(pb)) {
96  avpriv_request_sample(s, "unknown format id");
97  return AVERROR_INVALIDDATA;
98  }
99 
100  channel_type = avio_rl32(pb);
101  if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
102  st->codec->channel_layout = dsf_channel_layout[channel_type];
103  if (!st->codec->channel_layout)
104  avpriv_request_sample(s, "channel type %i", channel_type);
105 
107  st->codec->channels = avio_rl32(pb);
108  st->codec->sample_rate = avio_rl32(pb) / 8;
109 
110  switch(avio_rl32(pb)) {
111  case 1: st->codec->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
112  case 8: st->codec->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
113  default:
114  avpriv_request_sample(s, "unknown most significant bit");
115  return AVERROR_INVALIDDATA;
116  }
117 
118  avio_skip(pb, 8);
119  st->codec->block_align = avio_rl32(pb);
120  if (st->codec->block_align > INT_MAX / st->codec->channels) {
121  avpriv_request_sample(s, "block_align overflow");
122  return AVERROR_INVALIDDATA;
123  }
124  st->codec->block_align *= st->codec->channels;
125  avio_skip(pb, 4);
126 
127  /* data chunk */
128 
129  dsf->data_end = avio_tell(pb);
130  if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
131  return AVERROR_INVALIDDATA;
132  dsf->data_end += avio_rl64(pb);
133 
134  return 0;
135 }
136 
138 {
139  DSFContext *dsf = s->priv_data;
140  AVIOContext *pb = s->pb;
141  AVStream *st = s->streams[0];
142  int64_t pos = avio_tell(pb);
143 
144  if (pos >= dsf->data_end)
145  return AVERROR_EOF;
146 
147  pkt->stream_index = 0;
148  return av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codec->block_align));
149 }
150 
152  .name = "dsf",
153  .long_name = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
154  .priv_data_size = sizeof(DSFContext),
159 };
AVInputFormat ff_dsf_demuxer
Definition: dsfdec.c:151
#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
#define AV_CH_LAYOUT_SURROUND
static int dsf_read_header(AVFormatContext *s)
Definition: dsfdec.c:61
static const uint64_t dsf_channel_layout[]
Definition: dsfdec.c:38
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:208
#define AV_CH_LAYOUT_4POINT0
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:282
uint64_t_TMPL AV_RL64
Definition: bytestream.h:87
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header. ...
Definition: id3v2.c:1078
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2324
Format I/O context.
Definition: avformat.h:1314
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dsfdec.c:137
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3805
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
#define AVERROR_EOF
End of file.
Definition: error.h:55
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:252
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:442
static int dsf_probe(AVProbeData *p)
Definition: dsfdec.c:31
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:667
#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:176
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:1062
#define AV_CH_LAYOUT_QUAD
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2338
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:207
#define FFMIN(a, b)
Definition: common.h:96
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
#define AV_CH_LAYOUT_5POINT1_BACK
#define FF_ARRAY_ELEMS(a)
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:638
Stream structure.
Definition: avformat.h:877
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
enum AVMediaType codec_type
Definition: avcodec.h:1540
enum AVCodecID codec_id
Definition: avcodec.h:1549
int sample_rate
samples per second
Definition: avcodec.h:2287
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:486
#define AV_CH_LAYOUT_5POINT0_BACK
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
static int flags
Definition: cpu.c:47
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
Main libavformat public API header.
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
Read an ID3v2 tag, including supported extra metadata and chapters.
Definition: id3v2.c:1056
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:493
int channels
number of audio channels
Definition: avcodec.h:2288
void * priv_data
Format private data.
Definition: avformat.h:1342
static void read_id3(AVFormatContext *s, uint64_t id3pos)
Definition: dsfdec.c:49
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
int stream_index
Definition: avcodec.h:1469
uint64_t data_end
Definition: dsfdec.c:28
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:342
This structure stores compressed data.
Definition: avcodec.h:1444
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:675