tools/sofa2wavs: fix build on Windows
[ffmpeg.git] / libavformat / wsddec.c
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
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
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
31 static 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
40 static int empty_string(const char *buf, unsigned size)
41 {
42 while (size--) {
43 if (*buf++ != ' ')
44 return 0;
45 }
46 return 1;
47 }
48
49 static int wsd_to_av_channel_layoyt(AVFormatContext *s, int bit)
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
74 static 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 = avio_read(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
100 static int wsd_read_header(AVFormatContext *s)
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
108 st = avformat_new_stream(s, NULL);
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
130 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
131 st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF;
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;
135 st->codecpar->bit_rate = (int64_t)st->codecpar->ch_layout.nb_channels *
136 st->codecpar->sample_rate * 8LL;
137 if (!st->codecpar->ch_layout.nb_channels)
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);
148 av_channel_layout_from_mask(&st->codecpar->ch_layout, ch_mask);
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
171 const FFInputFormat ff_wsd_demuxer = {
172 .p.name = "wsd",
173 .p.long_name = NULL_IF_CONFIG_SMALL("Wideband Single-bit Data (WSD)"),
174 .p.extensions = "wsd",
175 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
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 };