FFmpeg
wsddec.c
Go to the documentation of this file.
1 /*
2  * Wideband Single-bit Data (WSD) 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 
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/timecode.h"
26 #include "avformat.h"
27 #include "demux.h"
28 #include "rawdec.h"
29 
30 static int wsd_probe(const AVProbeData *p)
31 {
32  if (p->buf_size < 45 || memcmp(p->buf, "1bit", 4) ||
33  !AV_RB32(p->buf + 36) || !p->buf[44] ||
34  (p->buf[0] >= 0x10 && (AV_RB32(p->buf + 20) < 0x80 || AV_RB32(p->buf + 24) < 0x80)))
35  return 0;
36  return AVPROBE_SCORE_MAX;
37 }
38 
39 static int empty_string(const char *buf, unsigned size)
40 {
41  while (size--) {
42  if (*buf++ != ' ')
43  return 0;
44  }
45  return 1;
46 }
47 
49 {
50  switch (bit) {
51  case 2: return AV_CH_BACK_RIGHT;
52  case 3:
53  avpriv_request_sample(s, "Rr-middle");
54  break;
55  case 4: return AV_CH_BACK_CENTER;
56  case 5:
57  avpriv_request_sample(s, "Lr-middle");
58  break;
59  case 6: return AV_CH_BACK_LEFT;
60  case 24: return AV_CH_LOW_FREQUENCY;
61  case 26: return AV_CH_FRONT_RIGHT;
62  case 27: return AV_CH_FRONT_RIGHT_OF_CENTER;
63  case 28: return AV_CH_FRONT_CENTER;
64  case 29: return AV_CH_FRONT_LEFT_OF_CENTER;
65  case 30: return AV_CH_FRONT_LEFT;
66  default:
67  av_log(s, AV_LOG_WARNING, "reserved channel assignment\n");
68  break;
69  }
70  return 0;
71 }
72 
73 static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned size)
74 {
75  uint8_t *buf;
76  if (!(size + 1))
77  return AVERROR(ENOMEM);
78 
79  buf = av_malloc(size + 1);
80  if (!buf)
81  return AVERROR(ENOMEM);
82 
83  if (avio_read(s->pb, buf, size) != size) {
84  av_free(buf);
85  return AVERROR(EIO);
86  }
87 
88  if (empty_string(buf, size)) {
89  av_free(buf);
90  return 0;
91  }
92 
93  buf[size] = 0;
94  av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
95  return 0;
96 }
97 
99 {
100  AVIOContext *pb = s->pb;
101  AVStream *st;
102  int version;
103  uint32_t text_offset, data_offset, channel_assign;
104  char playback_time[AV_TIMECODE_STR_SIZE];
105 
106  st = avformat_new_stream(s, NULL);
107  if (!st)
108  return AVERROR(ENOMEM);
109 
110  avio_skip(pb, 8);
111  version = avio_r8(pb);
112  av_log(s, AV_LOG_DEBUG, "version: %i.%i\n", version >> 4, version & 0xF);
113  avio_skip(pb, 11);
114 
115  if (version < 0x10) {
116  text_offset = 0x80;
117  data_offset = 0x800;
118  avio_skip(pb, 8);
119  } else {
120  text_offset = avio_rb32(pb);
121  data_offset = avio_rb32(pb);
122  }
123 
124  avio_skip(pb, 4);
125  av_timecode_make_smpte_tc_string2(playback_time, (AVRational){1,1}, avio_rb32(pb) & 0x00ffffffU, 1, 1);
126  av_dict_set(&s->metadata, "playback_time", playback_time, 0);
127 
130  st->codecpar->sample_rate = avio_rb32(pb) / 8;
131  avio_skip(pb, 4);
132  st->codecpar->ch_layout.nb_channels = avio_r8(pb) & 0xF;
134  st->codecpar->sample_rate * 8LL;
136  return AVERROR_INVALIDDATA;
137 
138  avio_skip(pb, 3);
139  channel_assign = avio_rb32(pb);
140  if (!(channel_assign & 1)) {
141  uint64_t ch_mask = 0;
142  int i;
143  for (i = 1; i < 32; i++)
144  if ((channel_assign >> i) & 1)
145  ch_mask |= wsd_to_av_channel_layoyt(s, i);
147  }
148 
149  avio_skip(pb, 16);
150  if (avio_rb32(pb))
151  avpriv_request_sample(s, "emphasis");
152 
153  if (avio_seek(pb, text_offset, SEEK_SET) >= 0) {
154  get_metadata(s, "title", 128);
155  get_metadata(s, "composer", 128);
156  get_metadata(s, "song_writer", 128);
157  get_metadata(s, "artist", 128);
158  get_metadata(s, "album", 128);
159  get_metadata(s, "genre", 32);
160  get_metadata(s, "date", 32);
161  get_metadata(s, "location", 32);
162  get_metadata(s, "comment", 512);
163  get_metadata(s, "user", 512);
164  }
165 
166  return avio_seek(pb, data_offset, SEEK_SET);
167 }
168 
170  .p.name = "wsd",
171  .p.long_name = NULL_IF_CONFIG_SMALL("Wideband Single-bit Data (WSD)"),
172  .p.extensions = "wsd",
174  .p.priv_class = &ff_raw_demuxer_class,
175  .read_probe = wsd_probe,
176  .read_header = wsd_read_header,
177  .read_packet = ff_raw_read_partial_packet,
178  .raw_codec_id = AV_CODEC_ID_DSD_MSBF,
179  .priv_data_size = sizeof(FFRawDemuxerContext),
180 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AV_TIMECODE_STR_SIZE
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
AVFMT_NO_BYTE_SEEK
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:487
AVERROR
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int64_t
long long int64_t
Definition: coverity.c:34
wsd_to_av_channel_layoyt
static int wsd_to_av_channel_layoyt(AVFormatContext *s, int bit)
Definition: wsddec.c:48
get_metadata
static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned size)
Definition: wsddec.c:73
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:328
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
empty_string
static int empty_string(const char *buf, unsigned size)
Definition: wsddec.c:39
ff_wsd_demuxer
const FFInputFormat ff_wsd_demuxer
Definition: wsddec.c:169
timecode.h
AV_CH_BACK_LEFT
#define AV_CH_BACK_LEFT
Definition: channel_layout.h:179
FFRawDemuxerContext
Definition: rawdec.h:37
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:761
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:178
ff_raw_read_partial_packet
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:33
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_channel_layout_from_mask
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:251
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
if
if(ret)
Definition: filter_design.txt:179
ff_raw_demuxer_class
const AVClass ff_raw_demuxer_class
Definition: rawdec.c:141
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
NULL
#define NULL
Definition: coverity.c:32
rawdec.h
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AV_CH_FRONT_CENTER
#define AV_CH_FRONT_CENTER
Definition: channel_layout.h:177
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_FRONT_LEFT_OF_CENTER
Definition: channel_layout.h:181
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AV_CODEC_ID_DSD_MSBF
@ AV_CODEC_ID_DSD_MSBF
Definition: codec_id.h:521
size
int size
Definition: twinvq_data.h:10344
wsd_probe
static int wsd_probe(const AVProbeData *p)
Definition: wsddec.c:30
AV_RB32
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_RB32
Definition: bytestream.h:96
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
AV_CH_FRONT_RIGHT_OF_CENTER
#define AV_CH_FRONT_RIGHT_OF_CENTER
Definition: channel_layout.h:182
version
version
Definition: libkvazaar.c:321
wsd_read_header
static int wsd_read_header(AVFormatContext *s)
Definition: wsddec.c:98
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_timecode_make_smpte_tc_string2
char * av_timecode_make_smpte_tc_string2(char *buf, AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field)
Get the timecode string from the SMPTE timecode format.
Definition: timecode.c:139
AV_CH_BACK_CENTER
#define AV_CH_BACK_CENTER
Definition: channel_layout.h:183
AV_CH_FRONT_LEFT
#define AV_CH_FRONT_LEFT
Definition: channel_layout.h:175
demux.h
tag
uint32_t tag
Definition: movenc.c:1879
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
avformat.h
U
#define U(x)
Definition: vpx_arith.h:37
channel_layout.h
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
AV_CH_FRONT_RIGHT
#define AV_CH_FRONT_RIGHT
Definition: channel_layout.h:176
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
av_dict_set
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:88
FFInputFormat
Definition: demux.h:42
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CH_BACK_RIGHT
#define AV_CH_BACK_RIGHT
Definition: channel_layout.h:180