Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/wavarc.c
Line
Count
Source
1
/*
2
 * WavArc demuxer
3
 * Copyright (c) 2023 Paul B Mahol
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 "avformat.h"
25
#include "avio_internal.h"
26
#include "demux.h"
27
#include "internal.h"
28
29
typedef struct WavArcContext {
30
    int64_t data_end;
31
} WavArcContext;
32
33
static int wavarc_probe(const AVProbeData *p)
34
958k
{
35
958k
    int len = p->buf[0];
36
958k
    uint32_t id;
37
38
958k
    if (len == 0 || len + 6 >= p->buf_size)
39
470k
        return 0;
40
41
488k
    if (p->buf[len + 1] != 0)
42
349k
        return 0;
43
44
138k
    id = AV_RL32(p->buf + len + 2);
45
138k
    if (id != MKTAG('0','C','P','Y') &&
46
138k
        id != MKTAG('1','D','I','F') &&
47
138k
        id != MKTAG('2','S','L','P') &&
48
138k
        id != MKTAG('3','N','L','P') &&
49
138k
        id != MKTAG('4','A','L','P') &&
50
137k
        id != MKTAG('5','E','L','P'))
51
137k
        return 0;
52
53
718
    return AVPROBE_SCORE_MAX / 3 * 2;
54
138k
}
55
56
static int wavarc_read_header(AVFormatContext *s)
57
1.49k
{
58
1.49k
    WavArcContext *w = s->priv_data;
59
1.49k
    AVIOContext *pb = s->pb;
60
1.49k
    AVCodecParameters *par;
61
1.49k
    int filename_len, fmt_len, ret;
62
1.49k
    uint8_t data[36];
63
1.49k
    AVStream *st;
64
1.49k
    uint32_t id;
65
66
1.49k
    filename_len = avio_r8(pb);
67
1.49k
    if (filename_len == 0)
68
16
        return AVERROR_INVALIDDATA;
69
1.48k
    avio_skip(pb, filename_len);
70
1.48k
    if (avio_r8(pb))
71
58
        return AVERROR_INVALIDDATA;
72
1.42k
    id = avio_rl32(pb);
73
1.42k
    w->data_end = avio_tell(pb);
74
1.42k
    if ((ret = ffio_read_size(pb, data, sizeof(data))) < 0)
75
135
        return ret;
76
1.28k
    w->data_end += 16LL + AV_RL32(data + 4);
77
1.28k
    fmt_len = AV_RL32(data + 32);
78
1.28k
    if (fmt_len < 12)
79
43
        return AVERROR_INVALIDDATA;
80
81
1.24k
    st = avformat_new_stream(s, NULL);
82
1.24k
    if (!st)
83
0
        return AVERROR(ENOMEM);
84
1.24k
    par = st->codecpar;
85
86
1.24k
    ret = ff_alloc_extradata(par, fmt_len + sizeof(data));
87
1.24k
    if (ret < 0)
88
2
        return ret;
89
1.24k
    memcpy(par->extradata, data, sizeof(data));
90
1.24k
    ret = ffio_read_size(pb, par->extradata + sizeof(data), fmt_len);
91
1.24k
    if (ret < 0)
92
176
        return ret;
93
94
1.06k
    par->codec_type = AVMEDIA_TYPE_AUDIO;
95
1.06k
    par->codec_id   = AV_CODEC_ID_WAVARC;
96
1.06k
    par->codec_tag  = id;
97
98
404k
    do {
99
404k
        id = avio_rl32(pb);
100
404k
        if (id != MKTAG('d','a','t','a'))
101
404k
            avio_skip(pb, avio_rl32(pb));
102
404k
    } while (id != MKTAG('d','a','t','a') && !avio_feof(pb));
103
1.06k
    avio_skip(pb, 4);
104
105
1.06k
    if (AV_RL32(par->extradata + 16) != MKTAG('R','I','F','F'))
106
138
        return AVERROR_INVALIDDATA;
107
928
    if (AV_RL32(par->extradata + 24) != MKTAG('W','A','V','E'))
108
70
        return AVERROR_INVALIDDATA;
109
858
    if (AV_RL32(par->extradata + 28) != MKTAG('f','m','t',' '))
110
62
        return AVERROR_INVALIDDATA;
111
112
796
    av_channel_layout_default(&par->ch_layout, AV_RL16(par->extradata + 38));
113
796
    par->sample_rate = AV_RL32(par->extradata + 40);
114
796
    avpriv_set_pts_info(st, 64, 1, par->sample_rate);
115
796
    st->start_time = 0;
116
117
796
    return 0;
118
858
}
119
120
static int wavarc_read_packet(AVFormatContext *s, AVPacket *pkt)
121
12.9k
{
122
12.9k
    WavArcContext *w = s->priv_data;
123
12.9k
    AVIOContext *pb = s->pb;
124
12.9k
    int64_t size, left = w->data_end - avio_tell(pb);
125
12.9k
    int ret;
126
127
12.9k
    size = FFMIN(left, 1024);
128
12.9k
    if (size <= 0)
129
460
        return AVERROR_EOF;
130
131
12.5k
    ret = av_get_packet(pb, pkt, size);
132
12.5k
    pkt->stream_index = 0;
133
12.5k
    return ret;
134
12.9k
}
135
136
const FFInputFormat ff_wavarc_demuxer = {
137
    .p.name         = "wavarc",
138
    .p.long_name    = NULL_IF_CONFIG_SMALL("Waveform Archiver"),
139
    .p.flags        = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS,
140
    .p.extensions   = "wa",
141
    .priv_data_size = sizeof(WavArcContext),
142
    .read_probe     = wavarc_probe,
143
    .read_packet    = wavarc_read_packet,
144
    .read_header    = wavarc_read_header,
145
    .raw_codec_id   = AV_CODEC_ID_WAVARC,
146
};