FFmpeg
Loading...
Searching...
No Matches
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
24#include "libavutil/mem.h"
25#include "libavutil/timecode.h"
26#include "avformat.h"
27#include "avio_internal.h"
28#include "demux.h"
29#include "rawdec.h"
30
31static int wsd_probe(const AVProbeData *p)
32{
33 if (p->buf_size < 45 || memcmp(p->buf, "1bit", 4) ||
34 !AV_RB32(p->buf + 36) || !p->buf[44] ||
35 (p->buf[0] >= 0x10 && (AV_RB32(p->buf + 20) < 0x80 || AV_RB32(p->buf + 24) < 0x80)))
36 return 0;
37 return AVPROBE_SCORE_MAX;
38}
39
40static int empty_string(const char *buf, unsigned size)
41{
42 while (size--) {
43 if (*buf++ != ' ')
44 return 0;
45 }
46 return 1;
47}
48
50{
51 switch (bit) {
52 case 2: return AV_CH_BACK_RIGHT;
53 case 3:
54 avpriv_request_sample(s, "Rr-middle");
55 break;
56 case 4: return AV_CH_BACK_CENTER;
57 case 5:
58 avpriv_request_sample(s, "Lr-middle");
59 break;
60 case 6: return AV_CH_BACK_LEFT;
61 case 24: return AV_CH_LOW_FREQUENCY;
62 case 26: return AV_CH_FRONT_RIGHT;
63 case 27: return AV_CH_FRONT_RIGHT_OF_CENTER;
64 case 28: return AV_CH_FRONT_CENTER;
65 case 29: return AV_CH_FRONT_LEFT_OF_CENTER;
66 case 30: return AV_CH_FRONT_LEFT;
67 default:
68 av_log(s, AV_LOG_WARNING, "reserved channel assignment\n");
69 break;
70 }
71 return 0;
72}
73
74static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned size)
75{
76 uint8_t *buf;
77 int ret;
78 if (!(size + 1))
79 return AVERROR(ENOMEM);
80
81 buf = av_malloc(size + 1);
82 if (!buf)
83 return AVERROR(ENOMEM);
84
85 if ((ret = ffio_read_size(s->pb, buf, size)) < 0) {
86 av_free(buf);
87 return ret;
88 }
89
90 if (empty_string(buf, size)) {
91 av_free(buf);
92 return 0;
93 }
94
95 buf[size] = 0;
96 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
97 return 0;
98}
99
101{
102 AVIOContext *pb = s->pb;
103 AVStream *st;
104 int version;
105 uint32_t text_offset, data_offset, channel_assign;
106 char playback_time[AV_TIMECODE_STR_SIZE];
107
109 if (!st)
110 return AVERROR(ENOMEM);
111
112 avio_skip(pb, 8);
113 version = avio_r8(pb);
114 av_log(s, AV_LOG_DEBUG, "version: %i.%i\n", version >> 4, version & 0xF);
115 avio_skip(pb, 11);
116
117 if (version < 0x10) {
118 text_offset = 0x80;
119 data_offset = 0x800;
120 avio_skip(pb, 8);
121 } else {
122 text_offset = avio_rb32(pb);
123 data_offset = avio_rb32(pb);
124 }
125
126 avio_skip(pb, 4);
127 av_timecode_make_smpte_tc_string2(playback_time, (AVRational){1,1}, avio_rb32(pb) & 0x00ffffffU, 1, 1);
128 av_dict_set(&s->metadata, "playback_time", playback_time, 0);
129
132 st->codecpar->sample_rate = avio_rb32(pb) / 8;
133 avio_skip(pb, 4);
134 st->codecpar->ch_layout.nb_channels = avio_r8(pb) & 0xF;
136 st->codecpar->sample_rate * 8LL;
138 return AVERROR_INVALIDDATA;
139
140 avio_skip(pb, 3);
141 channel_assign = avio_rb32(pb);
142 if (!(channel_assign & 1)) {
143 uint64_t ch_mask = 0;
144 int i;
145 for (i = 1; i < 32; i++)
146 if ((channel_assign >> i) & 1)
147 ch_mask |= wsd_to_av_channel_layoyt(s, i);
149 }
150
151 avio_skip(pb, 16);
152 if (avio_rb32(pb))
153 avpriv_request_sample(s, "emphasis");
154
155 if (avio_seek(pb, text_offset, SEEK_SET) >= 0) {
156 get_metadata(s, "title", 128);
157 get_metadata(s, "composer", 128);
158 get_metadata(s, "song_writer", 128);
159 get_metadata(s, "artist", 128);
160 get_metadata(s, "album", 128);
161 get_metadata(s, "genre", 32);
162 get_metadata(s, "date", 32);
163 get_metadata(s, "location", 32);
164 get_metadata(s, "comment", 512);
165 get_metadata(s, "user", 512);
166 }
167
168 return avio_seek(pb, data_offset, SEEK_SET);
169}
170
172 .p.name = "wsd",
173 .p.long_name = NULL_IF_CONFIG_SMALL("Wideband Single-bit Data (WSD)"),
174 .p.extensions = "wsd",
176 .p.priv_class = &ff_raw_demuxer_class,
177 .read_probe = wsd_probe,
178 .read_header = wsd_read_header,
179 .read_packet = ff_raw_read_partial_packet,
180 .raw_codec_id = AV_CODEC_ID_DSD_MSBF,
181 .priv_data_size = sizeof(FFRawDemuxerContext),
182};
const FFInputFormat ff_wsd_demuxer
Definition wsddec.c:171
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition avformat.h:506
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bit(string, value)
Definition cbs_mpeg2.c:56
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define AV_CH_FRONT_RIGHT
#define AV_CH_FRONT_RIGHT_OF_CENTER
#define AV_CH_BACK_CENTER
#define AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_BACK_RIGHT
#define AV_CH_FRONT_CENTER
#define AV_CH_BACK_LEFT
#define AV_CH_LOW_FREQUENCY
#define AV_CH_FRONT_LEFT
@ AV_CODEC_ID_DSD_MSBF
Definition codec_id.h:527
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
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.
#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
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:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RB32(p)
const AVClass ff_raw_demuxer_class
Definition rawdec.c:141
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawdec.c:33
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
version
Definition libkvazaar.c:313
Memory handling functions.
uint32_t tag
Definition movenc.c:2073
#define av_malloc(s)
Definition ops_static.c:52
int nb_channels
Number of channels in this layout.
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
#define av_free(p)
#define avpriv_request_sample(...)
#define av_log(a,...)
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:131
Timecode helpers header.
#define AV_TIMECODE_STR_SIZE
Definition timecode.h:33
int size
static int wsd_probe(const AVProbeData *p)
Definition wsddec.c:31
static int wsd_to_av_channel_layoyt(AVFormatContext *s, int bit)
Definition wsddec.c:49
static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned size)
Definition wsddec.c:74
static int empty_string(const char *buf, unsigned size)
Definition wsddec.c:40
static int wsd_read_header(AVFormatContext *s)
Definition wsddec.c:100