2 * Wideband Single-bit Data (WSD) demuxer
3 * Copyright (c) 2014 Peter Ross
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/timecode.h"
28 static int wsd_probe(AVProbeData *p)
30 if (p->buf_size < 45 || memcmp(p->buf, "1bit", 4) ||
31 !AV_RB32(p->buf + 36) || !p->buf[44] ||
32 (p->buf[0] >= 0x10 && (AV_RB32(p->buf + 20) < 0x80 || AV_RB32(p->buf + 24) < 0x80)))
34 return AVPROBE_SCORE_MAX;
37 static int empty_string(const char *buf, unsigned size)
46 static int wsd_to_av_channel_layoyt(AVFormatContext *s, int bit)
49 case 2: return AV_CH_BACK_RIGHT;
51 avpriv_request_sample(s, "Rr-middle");
53 case 4: return AV_CH_BACK_CENTER;
55 avpriv_request_sample(s, "Lr-middle");
57 case 6: return AV_CH_BACK_LEFT;
58 case 24: return AV_CH_LOW_FREQUENCY;
59 case 26: return AV_CH_FRONT_RIGHT;
60 case 27: return AV_CH_FRONT_RIGHT_OF_CENTER;
61 case 28: return AV_CH_FRONT_CENTER;
62 case 29: return AV_CH_FRONT_LEFT_OF_CENTER;
63 case 30: return AV_CH_FRONT_LEFT;
65 av_log(s, AV_LOG_WARNING, "reserved channel assignment\n");
71 static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned size)
75 return AVERROR(ENOMEM);
77 buf = av_malloc(size + 1);
79 return AVERROR(ENOMEM);
81 if (avio_read(s->pb, buf, size) != size) {
86 if (empty_string(buf, size)) {
92 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
96 static int wsd_read_header(AVFormatContext *s)
98 AVIOContext *pb = s->pb;
101 uint32_t text_offset, data_offset, channel_assign;
102 char playback_time[AV_TIMECODE_STR_SIZE];
104 st = avformat_new_stream(s, NULL);
106 return AVERROR(ENOMEM);
109 version = avio_r8(pb);
110 av_log(s, AV_LOG_DEBUG, "version: %i.%i\n", version >> 4, version & 0xF);
113 if (version < 0x10) {
118 text_offset = avio_rb32(pb);
119 data_offset = avio_rb32(pb);
123 av_timecode_make_smpte_tc_string(playback_time, avio_rb32(pb), 0);
124 av_dict_set(&s->metadata, "playback_time", playback_time, 0);
126 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
127 st->codecpar->codec_id = s->iformat->raw_codec_id;
128 st->codecpar->sample_rate = avio_rb32(pb) / 8;
130 st->codecpar->channels = avio_r8(pb) & 0xF;
131 st->codecpar->bit_rate = (int64_t)st->codecpar->channels * st->codecpar->sample_rate * 8LL;
132 if (!st->codecpar->channels)
133 return AVERROR_INVALIDDATA;
136 channel_assign = avio_rb32(pb);
137 if (!(channel_assign & 1)) {
139 for (i = 1; i < 32; i++)
140 if (channel_assign & (1 << i))
141 st->codecpar->channel_layout |= wsd_to_av_channel_layoyt(s, i);
146 avpriv_request_sample(s, "emphasis");
148 if (avio_seek(pb, text_offset, SEEK_SET) >= 0) {
149 get_metadata(s, "title", 128);
150 get_metadata(s, "composer", 128);
151 get_metadata(s, "song_writer", 128);
152 get_metadata(s, "artist", 128);
153 get_metadata(s, "album", 128);
154 get_metadata(s, "genre", 32);
155 get_metadata(s, "date", 32);
156 get_metadata(s, "location", 32);
157 get_metadata(s, "comment", 512);
158 get_metadata(s, "user", 512);
161 return avio_seek(pb, data_offset, SEEK_SET);
164 AVInputFormat ff_wsd_demuxer = {
166 .long_name = NULL_IF_CONFIG_SMALL("Wideband Single-bit Data (WSD)"),
167 .read_probe = wsd_probe,
168 .read_header = wsd_read_header,
169 .read_packet = ff_raw_read_partial_packet,
171 .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
172 .raw_codec_id = AV_CODEC_ID_DSD_MSBF,