Coverage Report

Created: 2024-09-06 07:53

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