Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/riffdec.c
Line
Count
Source
1
/*
2
 * RIFF demuxing functions and data
3
 * Copyright (c) 2000 Fabrice Bellard
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/avassert.h"
23
#include "libavutil/dict.h"
24
#include "libavutil/error.h"
25
#include "libavutil/intreadwrite.h"
26
#include "libavutil/log.h"
27
#include "libavutil/mem.h"
28
#include "avformat.h"
29
#include "avio_internal.h"
30
#include "demux.h"
31
#include "riff.h"
32
33
int ff_get_guid(AVIOContext *s, ff_asf_guid *g)
34
2.44M
{
35
2.44M
    int ret;
36
2.44M
    av_assert0(sizeof(*g) == 16); //compiler will optimize this out
37
2.44M
    ret = ffio_read_size(s, *g, sizeof(*g));
38
2.44M
    if (ret < 0) {
39
982k
        memset(*g, 0, sizeof(*g));
40
982k
        return ret;
41
982k
    }
42
1.46M
    return 0;
43
2.44M
}
44
45
enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
46
19.9k
{
47
19.9k
    int i;
48
133k
    for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++)
49
119k
        if (!ff_guidcmp(guids[i].guid, guid))
50
5.57k
            return guids[i].id;
51
14.3k
    return AV_CODEC_ID_NONE;
52
19.9k
}
53
54
/* We could be given one of the three possible structures here:
55
 * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
56
 * is an expansion of the previous one with the fields added
57
 * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
58
 * WAVEFORMATEX adds 'WORD  cbSize' and basically makes itself
59
 * an openended structure.
60
 */
61
62
static void parse_waveformatex(void *logctx, AVIOContext *pb, AVCodecParameters *par)
63
30.9k
{
64
30.9k
    ff_asf_guid subformat;
65
30.9k
    int bps;
66
30.9k
    uint64_t mask;
67
68
30.9k
    bps = avio_rl16(pb);
69
30.9k
    if (bps)
70
12.5k
        par->bits_per_coded_sample = bps;
71
72
30.9k
    mask = avio_rl32(pb); /* dwChannelMask */
73
30.9k
    av_channel_layout_from_mask(&par->ch_layout, mask);
74
75
30.9k
    ff_get_guid(pb, &subformat);
76
30.9k
    if (!memcmp(subformat + 4,
77
30.9k
                (const uint8_t[]){ FF_AMBISONIC_BASE_GUID }, 12) ||
78
28.1k
        !memcmp(subformat + 4,
79
28.1k
                (const uint8_t[]){ FF_BROKEN_BASE_GUID }, 12) ||
80
24.9k
        !memcmp(subformat + 4,
81
24.9k
                (const uint8_t[]){ FF_MEDIASUBTYPE_BASE_GUID }, 12)) {
82
11.7k
        par->codec_tag = AV_RL32(subformat);
83
11.7k
        par->codec_id  = ff_wav_codec_get_id(par->codec_tag,
84
11.7k
                                             par->bits_per_coded_sample);
85
19.1k
    } else {
86
19.1k
        par->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat);
87
19.1k
        if (!par->codec_id)
88
14.0k
            av_log(logctx, AV_LOG_WARNING,
89
14.0k
                   "unknown subformat:"FF_PRI_GUID"\n",
90
14.0k
                   FF_ARG_GUID(subformat));
91
19.1k
    }
92
30.9k
}
93
94
/* "big_endian" values are needed for RIFX file format */
95
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
96
                      AVCodecParameters *par, int size, int big_endian)
97
203k
{
98
203k
    int id, channels = 0, ret;
99
203k
    uint64_t bitrate = 0;
100
101
203k
    if (size < 14) {
102
267
        avpriv_request_sample(s, "wav header size < 14");
103
267
        return AVERROR_INVALIDDATA;
104
267
    }
105
106
203k
    av_channel_layout_uninit(&par->ch_layout);
107
108
203k
    par->codec_type  = AVMEDIA_TYPE_AUDIO;
109
203k
    if (!big_endian) {
110
203k
        id                 = avio_rl16(pb);
111
203k
        if (id != 0x0165) {
112
200k
            channels         = avio_rl16(pb);
113
200k
            par->sample_rate = avio_rl32(pb);
114
200k
            bitrate            = avio_rl32(pb) * 8LL;
115
200k
            par->block_align = avio_rl16(pb);
116
200k
        }
117
203k
    } else {
118
99
        id                 = avio_rb16(pb);
119
99
        channels           = avio_rb16(pb);
120
99
        par->sample_rate = avio_rb32(pb);
121
99
        bitrate            = avio_rb32(pb) * 8LL;
122
99
        par->block_align = avio_rb16(pb);
123
99
    }
124
203k
    if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */
125
7.24k
        par->bits_per_coded_sample = 8;
126
195k
    } else {
127
195k
        if (!big_endian) {
128
195k
            par->bits_per_coded_sample = avio_rl16(pb);
129
195k
        } else {
130
99
            par->bits_per_coded_sample = avio_rb16(pb);
131
99
        }
132
195k
    }
133
203k
    if (id == 0xFFFE) {
134
31.8k
        par->codec_tag = 0;
135
171k
    } else {
136
171k
        par->codec_tag = id;
137
171k
        par->codec_id  = ff_wav_codec_get_id(id,
138
171k
                                             par->bits_per_coded_sample);
139
171k
    }
140
203k
    if (size >= 18 && id != 0x0165) {  /* We're obviously dealing with WAVEFORMATEX */
141
160k
        int cbSize = avio_rl16(pb); /* cbSize */
142
160k
        if (big_endian) {
143
3
            avpriv_report_missing_feature(s, "WAVEFORMATEX support for RIFX files");
144
3
            return AVERROR_PATCHWELCOME;
145
3
        }
146
160k
        size  -= 18;
147
160k
        cbSize = FFMIN(size, cbSize);
148
160k
        if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
149
30.9k
            parse_waveformatex(s, pb, par);
150
30.9k
            cbSize -= 22;
151
30.9k
            size   -= 22;
152
129k
        } else if (cbSize >= 12 && id == 0x1610) { /* HEAACWAVEFORMAT */
153
292
            int wPayloadType = avio_rl16(pb);
154
292
            if (wPayloadType == 3)
155
75
                par->codec_id = AV_CODEC_ID_AAC_LATM;
156
292
            avio_skip(pb, 2); // wAudioProfileLevelIndication
157
292
            int wStructType = avio_rl16(pb);
158
292
            if (wStructType) {
159
49
                avpriv_report_missing_feature(s, "HEAACWAVEINFO wStructType \"%d\"", wStructType);
160
49
                return AVERROR_PATCHWELCOME;
161
49
            }
162
243
            avio_skip(pb, 2); // wReserved1
163
243
            avio_skip(pb, 4); // dwReserved2
164
243
            cbSize -= 12;
165
243
            size   -= 12;
166
243
        }
167
160k
        if (cbSize > 0) {
168
142k
            ret = ff_get_extradata(s, par, pb, cbSize);
169
142k
            if (ret < 0)
170
1.16k
                return ret;
171
141k
            size -= cbSize;
172
141k
        }
173
174
        /* It is possible for the chunk to contain garbage at the end */
175
158k
        if (size > 0)
176
32.5k
            avio_skip(pb, size);
177
158k
    } else if (id == 0x0165 && size >= 32) {
178
2.79k
        int nb_streams, i;
179
180
2.79k
        size -= 4;
181
2.79k
        ret = ff_get_extradata(s, par, pb, size);
182
2.79k
        if (ret < 0)
183
345
            return ret;
184
2.44k
        nb_streams         = AV_RL16(par->extradata + 4);
185
2.44k
        par->sample_rate   = AV_RL32(par->extradata + 12);
186
2.44k
        channels           = 0;
187
2.44k
        bitrate            = 0;
188
2.44k
        if (size < 8 + nb_streams * 20)
189
48
            return AVERROR_INVALIDDATA;
190
98.7k
        for (i = 0; i < nb_streams; i++)
191
96.3k
            channels += par->extradata[8 + i * 20 + 17];
192
2.40k
    }
193
194
201k
    par->bit_rate = bitrate;
195
196
201k
    if (par->sample_rate <= 0) {
197
1.38k
        av_log(s, AV_LOG_ERROR,
198
1.38k
               "Invalid sample rate: %d\n", par->sample_rate);
199
1.38k
        return AVERROR_INVALIDDATA;
200
1.38k
    }
201
200k
    if (par->codec_id == AV_CODEC_ID_AAC_LATM) {
202
        /* Channels and sample_rate values are those prior to applying SBR
203
         * and/or PS. */
204
6.85k
        channels         = 0;
205
6.85k
        par->sample_rate = 0;
206
6.85k
    }
207
    /* override bits_per_coded_sample for G.726 */
208
200k
    if (par->codec_id == AV_CODEC_ID_ADPCM_G726 && par->sample_rate)
209
3.12k
        par->bits_per_coded_sample = par->bit_rate / par->sample_rate;
210
211
    /* ignore WAVEFORMATEXTENSIBLE layout if different from channel count */
212
200k
    if (channels != par->ch_layout.nb_channels) {
213
144k
        av_channel_layout_uninit(&par->ch_layout);
214
144k
        par->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
215
144k
        par->ch_layout.nb_channels = channels;
216
144k
    }
217
218
200k
    return 0;
219
201k
}
220
221
enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
222
341k
{
223
341k
    enum AVCodecID id;
224
341k
    id = ff_codec_get_id(ff_codec_wav_tags, tag);
225
341k
    if (id <= 0)
226
178k
        return id;
227
228
162k
    if (id == AV_CODEC_ID_PCM_S16LE)
229
76.5k
        id = ff_get_pcm_codec_id(bps, 0, 0, ~1);
230
86.3k
    else if (id == AV_CODEC_ID_PCM_F32LE)
231
12.7k
        id = ff_get_pcm_codec_id(bps, 1, 0,  0);
232
233
162k
    if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
234
631
        id = AV_CODEC_ID_ADPCM_ZORK;
235
162k
    return id;
236
341k
}
237
238
int ff_get_bmp_header(AVIOContext *pb, AVStream *st, uint32_t *size)
239
14.1k
{
240
14.1k
    int tag1;
241
14.1k
    uint32_t size_ = avio_rl32(pb);
242
14.1k
    if (size)
243
13.9k
        *size = size_;
244
14.1k
    st->codecpar->width  = avio_rl32(pb);
245
14.1k
    st->codecpar->height = (int32_t)avio_rl32(pb);
246
14.1k
    avio_rl16(pb); /* planes */
247
14.1k
    st->codecpar->bits_per_coded_sample = avio_rl16(pb); /* depth */
248
14.1k
    tag1                                = avio_rl32(pb);
249
14.1k
    avio_rl32(pb); /* ImageSize */
250
14.1k
    avio_rl32(pb); /* XPelsPerMeter */
251
14.1k
    avio_rl32(pb); /* YPelsPerMeter */
252
14.1k
    avio_rl32(pb); /* ClrUsed */
253
14.1k
    avio_rl32(pb); /* ClrImportant */
254
14.1k
    return tag1;
255
14.1k
}
256
257
int ff_read_riff_info(AVFormatContext *s, int64_t size)
258
4.90k
{
259
4.90k
    int64_t start, end, cur;
260
4.90k
    AVIOContext *pb = s->pb;
261
262
4.90k
    start = avio_tell(pb);
263
4.90k
    end   = start + size;
264
265
36.8k
    while ((cur = avio_tell(pb)) >= 0 &&
266
36.8k
           cur <= end - 8 /* = tag + size */) {
267
32.8k
        uint32_t chunk_code;
268
32.8k
        int64_t chunk_size;
269
32.8k
        char key[5] = { 0 };
270
32.8k
        char *value;
271
272
32.8k
        chunk_code = avio_rl32(pb);
273
32.8k
        chunk_size = avio_rl32(pb);
274
32.8k
        if (avio_feof(pb)) {
275
182
            if (chunk_code || chunk_size) {
276
56
                av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
277
56
                return AVERROR_INVALIDDATA;
278
56
            }
279
126
            return AVERROR_EOF;
280
182
        }
281
32.6k
        if (chunk_size > end ||
282
31.5k
            end - chunk_size < cur ||
283
30.9k
            chunk_size == UINT_MAX) {
284
1.68k
            avio_seek(pb, -9, SEEK_CUR);
285
1.68k
            chunk_code = avio_rl32(pb);
286
1.68k
            chunk_size = avio_rl32(pb);
287
1.68k
            if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
288
713
                av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
289
713
                return AVERROR_INVALIDDATA;
290
713
            }
291
1.68k
        }
292
293
31.9k
        chunk_size += (chunk_size & 1);
294
295
31.9k
        if (!chunk_code) {
296
8.14k
            if (chunk_size)
297
301
                avio_skip(pb, chunk_size);
298
7.84k
            else if (pb->eof_reached) {
299
6
                av_log(s, AV_LOG_WARNING, "truncated file\n");
300
6
                return AVERROR_EOF;
301
6
            }
302
8.14k
            continue;
303
8.14k
        }
304
305
23.8k
        value = av_mallocz(chunk_size + 1);
306
23.8k
        if (!value) {
307
18
            av_log(s, AV_LOG_ERROR,
308
18
                   "out of memory, unable to read INFO tag\n");
309
18
            return AVERROR(ENOMEM);
310
18
        }
311
312
23.7k
        AV_WL32(key, chunk_code);
313
        // Work around VC++ 2015 Update 1 code-gen bug:
314
        // https://connect.microsoft.com/VisualStudio/feedback/details/2291638
315
23.7k
        key[4] = 0;
316
317
23.7k
        if (avio_read(pb, value, chunk_size) != chunk_size) {
318
91
            av_log(s, AV_LOG_WARNING,
319
91
                   "premature end of file while reading INFO tag\n");
320
91
        }
321
322
23.7k
        av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
323
23.7k
    }
324
325
3.99k
    return 0;
326
4.90k
}