• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/timecode.h"
25 #include "avformat.h"
26 #include "internal.h"
27 #include "rawdec.h"
28 
wsd_probe(const AVProbeData * p)29 static int wsd_probe(const AVProbeData *p)
30 {
31     if (p->buf_size < 45 || memcmp(p->buf, "1bit", 4) ||
32         !AV_RB32(p->buf + 36) || !p->buf[44] ||
33         (p->buf[0] >= 0x10 && (AV_RB32(p->buf + 20) < 0x80 || AV_RB32(p->buf + 24) < 0x80)))
34        return 0;
35     return AVPROBE_SCORE_MAX;
36 }
37 
empty_string(const char * buf,unsigned size)38 static int empty_string(const char *buf, unsigned size)
39 {
40     while (size--) {
41        if (*buf++ != ' ')
42            return 0;
43     }
44     return 1;
45 }
46 
wsd_to_av_channel_layoyt(AVFormatContext * s,int bit)47 static int wsd_to_av_channel_layoyt(AVFormatContext *s, int bit)
48 {
49     switch (bit) {
50     case 2: return AV_CH_BACK_RIGHT;
51     case 3:
52         avpriv_request_sample(s, "Rr-middle");
53         break;
54     case 4: return AV_CH_BACK_CENTER;
55     case 5:
56         avpriv_request_sample(s, "Lr-middle");
57         break;
58     case 6: return AV_CH_BACK_LEFT;
59     case 24: return AV_CH_LOW_FREQUENCY;
60     case 26: return AV_CH_FRONT_RIGHT;
61     case 27: return AV_CH_FRONT_RIGHT_OF_CENTER;
62     case 28: return AV_CH_FRONT_CENTER;
63     case 29: return AV_CH_FRONT_LEFT_OF_CENTER;
64     case 30: return AV_CH_FRONT_LEFT;
65     default:
66         av_log(s, AV_LOG_WARNING, "reserved channel assignment\n");
67         break;
68     }
69     return 0;
70 }
71 
get_metadata(AVFormatContext * s,const char * const tag,const unsigned size)72 static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned size)
73 {
74     uint8_t *buf;
75     if (!(size + 1))
76         return AVERROR(ENOMEM);
77 
78     buf = av_malloc(size + 1);
79     if (!buf)
80         return AVERROR(ENOMEM);
81 
82     if (avio_read(s->pb, buf, size) != size) {
83         av_free(buf);
84         return AVERROR(EIO);
85     }
86 
87     if (empty_string(buf, size)) {
88         av_free(buf);
89         return 0;
90     }
91 
92     buf[size] = 0;
93     av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
94     return 0;
95 }
96 
wsd_read_header(AVFormatContext * s)97 static int wsd_read_header(AVFormatContext *s)
98 {
99     AVIOContext *pb = s->pb;
100     AVStream *st;
101     int version;
102     uint32_t text_offset, data_offset, channel_assign;
103     char playback_time[AV_TIMECODE_STR_SIZE];
104 
105     st = avformat_new_stream(s, NULL);
106     if (!st)
107         return AVERROR(ENOMEM);
108 
109     avio_skip(pb, 8);
110     version = avio_r8(pb);
111     av_log(s, AV_LOG_DEBUG, "version: %i.%i\n", version >> 4, version & 0xF);
112     avio_skip(pb, 11);
113 
114     if (version < 0x10) {
115         text_offset = 0x80;
116         data_offset = 0x800;
117         avio_skip(pb, 8);
118     } else {
119         text_offset = avio_rb32(pb);
120         data_offset = avio_rb32(pb);
121     }
122 
123     avio_skip(pb, 4);
124     av_timecode_make_smpte_tc_string2(playback_time, (AVRational){1,1}, avio_rb32(pb) & 0x00ffffffU, 1, 1);
125     av_dict_set(&s->metadata, "playback_time", playback_time, 0);
126 
127     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
128     st->codecpar->codec_id    = s->iformat->raw_codec_id;
129     st->codecpar->sample_rate = avio_rb32(pb) / 8;
130     avio_skip(pb, 4);
131     st->codecpar->ch_layout.nb_channels = avio_r8(pb) & 0xF;
132     st->codecpar->bit_rate    = (int64_t)st->codecpar->ch_layout.nb_channels *
133                                 st->codecpar->sample_rate * 8LL;
134     if (!st->codecpar->ch_layout.nb_channels)
135         return AVERROR_INVALIDDATA;
136 
137     avio_skip(pb, 3);
138     channel_assign         = avio_rb32(pb);
139     if (!(channel_assign & 1)) {
140         uint64_t ch_mask = 0;
141         int i;
142         for (i = 1; i < 32; i++)
143             if ((channel_assign >> i) & 1)
144                 ch_mask |= wsd_to_av_channel_layoyt(s, i);
145         av_channel_layout_from_mask(&st->codecpar->ch_layout, ch_mask);
146     }
147 
148     avio_skip(pb, 16);
149     if (avio_rb32(pb))
150        avpriv_request_sample(s, "emphasis");
151 
152     if (avio_seek(pb, text_offset, SEEK_SET) >= 0) {
153         get_metadata(s, "title",       128);
154         get_metadata(s, "composer",    128);
155         get_metadata(s, "song_writer", 128);
156         get_metadata(s, "artist",      128);
157         get_metadata(s, "album",       128);
158         get_metadata(s, "genre",        32);
159         get_metadata(s, "date",         32);
160         get_metadata(s, "location",     32);
161         get_metadata(s, "comment",     512);
162         get_metadata(s, "user",        512);
163     }
164 
165     return avio_seek(pb, data_offset, SEEK_SET);
166 }
167 
168 const AVInputFormat ff_wsd_demuxer = {
169     .name         = "wsd",
170     .long_name    = NULL_IF_CONFIG_SMALL("Wideband Single-bit Data (WSD)"),
171     .read_probe   = wsd_probe,
172     .read_header  = wsd_read_header,
173     .read_packet  = ff_raw_read_partial_packet,
174     .extensions   = "wsd",
175     .flags        = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
176     .raw_codec_id = AV_CODEC_ID_DSD_MSBF,
177     .priv_data_size = sizeof(FFRawDemuxerContext),
178     .priv_class     = &ff_raw_demuxer_class,
179 };
180