Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavformat/lvfdec.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * LVF demuxer
3
 * Copyright (c) 2012 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/intreadwrite.h"
23
#include "avformat.h"
24
#include "demux.h"
25
#include "riff.h"
26
27
static int lvf_probe(const AVProbeData *p)
28
358k
{
29
358k
    if (AV_RL32(p->buf) != MKTAG('L', 'V', 'F', 'F'))
30
358k
        return 0;
31
32
146
    if (!AV_RL32(p->buf + 16) || AV_RL32(p->buf + 16) > 256)
33
90
        return AVPROBE_SCORE_MAX / 8;
34
35
56
    return AVPROBE_SCORE_EXTENSION;
36
146
}
37
38
static int lvf_read_header(AVFormatContext *s)
39
7.23k
{
40
7.23k
    AVStream *st;
41
7.23k
    int64_t next_offset;
42
7.23k
    unsigned size, nb_streams, id;
43
44
7.23k
    avio_skip(s->pb, 16);
45
7.23k
    nb_streams = avio_rl32(s->pb);
46
7.23k
    if (!nb_streams)
47
4.16k
        return AVERROR_INVALIDDATA;
48
3.06k
    if (nb_streams > 2) {
49
422
        avpriv_request_sample(s, "%d streams", nb_streams);
50
422
        return AVERROR_PATCHWELCOME;
51
422
    }
52
53
2.64k
    avio_skip(s->pb, 1012);
54
55
30.8k
    while (!avio_feof(s->pb)) {
56
30.6k
        id          = avio_rl32(s->pb);
57
30.6k
        size        = avio_rl32(s->pb);
58
30.6k
        next_offset = avio_tell(s->pb) + size;
59
60
30.6k
        switch (id) {
61
14.2k
        case MKTAG('0', '0', 'f', 'm'):
62
14.2k
            st = avformat_new_stream(s, 0);
63
14.2k
            if (!st)
64
1
                return AVERROR(ENOMEM);
65
66
14.2k
            st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
67
14.2k
            avio_skip(s->pb, 4);
68
14.2k
            st->codecpar->width      = avio_rl32(s->pb);
69
14.2k
            st->codecpar->height     = avio_rl32(s->pb);
70
14.2k
            avio_skip(s->pb, 4);
71
14.2k
            st->codecpar->codec_tag  = avio_rl32(s->pb);
72
14.2k
            st->codecpar->codec_id   = ff_codec_get_id(ff_codec_bmp_tags,
73
14.2k
                                                       st->codecpar->codec_tag);
74
14.2k
            avpriv_set_pts_info(st, 32, 1, 1000);
75
14.2k
            break;
76
13.9k
        case MKTAG('0', '1', 'f', 'm'):
77
13.9k
            st = avformat_new_stream(s, 0);
78
13.9k
            if (!st)
79
1
                return AVERROR(ENOMEM);
80
81
13.9k
            st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
82
13.9k
            st->codecpar->codec_tag   = avio_rl16(s->pb);
83
13.9k
            st->codecpar->ch_layout.nb_channels = avio_rl16(s->pb);
84
13.9k
            st->codecpar->sample_rate = avio_rl16(s->pb);
85
13.9k
            avio_skip(s->pb, 8);
86
13.9k
            st->codecpar->bits_per_coded_sample = avio_r8(s->pb);
87
13.9k
            st->codecpar->codec_id    = ff_codec_get_id(ff_codec_wav_tags,
88
13.9k
                                                        st->codecpar->codec_tag);
89
13.9k
            avpriv_set_pts_info(st, 32, 1, 1000);
90
13.9k
            break;
91
2.40k
        case 0:
92
2.40k
            avio_seek(s->pb, 2048 + 8, SEEK_SET);
93
2.40k
            return 0;
94
91
        default:
95
91
            avpriv_request_sample(s, "id %d", id);
96
91
            return AVERROR_PATCHWELCOME;
97
30.6k
        }
98
99
28.1k
        avio_seek(s->pb, next_offset, SEEK_SET);
100
28.1k
    }
101
102
149
    return AVERROR_EOF;
103
2.64k
}
104
105
static int lvf_read_packet(AVFormatContext *s, AVPacket *pkt)
106
42.3k
{
107
42.3k
    unsigned size, flags, timestamp, id;
108
42.3k
    int64_t pos;
109
42.3k
    int ret, is_video = 0;
110
42.3k
    int stream_index;
111
112
42.3k
    pos = avio_tell(s->pb);
113
91.3k
    while (!avio_feof(s->pb)) {
114
87.9k
        id    = avio_rl32(s->pb);
115
87.9k
        size  = avio_rl32(s->pb);
116
117
87.9k
        if (size == 0xFFFFFFFFu)
118
111
            return AVERROR_EOF;
119
120
87.8k
        switch (id) {
121
38.0k
        case MKTAG('0', '0', 'd', 'c'):
122
38.0k
            is_video = 1;
123
38.2k
        case MKTAG('0', '1', 'w', 'b'):
124
38.2k
            if (size < 8)
125
10
                return AVERROR_INVALIDDATA;
126
38.2k
            stream_index = is_video ? 0 : 1;
127
38.2k
            if (stream_index >= s->nb_streams)
128
4
                return AVERROR_INVALIDDATA;
129
38.2k
            timestamp = avio_rl32(s->pb);
130
38.2k
            flags = avio_rl32(s->pb);
131
38.2k
            ret = av_get_packet(s->pb, pkt, size - 8);
132
38.2k
            if (flags & (1 << 12))
133
21.7k
                pkt->flags |= AV_PKT_FLAG_KEY;
134
38.2k
            pkt->stream_index = stream_index;
135
38.2k
            pkt->pts          = timestamp;
136
38.2k
            pkt->pos          = pos;
137
38.2k
            return ret;
138
49.5k
        default:
139
49.5k
            ret = avio_skip(s->pb, size);
140
87.8k
        }
141
142
49.5k
        if (ret < 0)
143
653
            return ret;
144
49.5k
    }
145
146
3.38k
    return AVERROR_EOF;
147
42.3k
}
148
149
const FFInputFormat ff_lvf_demuxer = {
150
    .p.name         = "lvf",
151
    .p.long_name    = NULL_IF_CONFIG_SMALL("LVF"),
152
    .p.extensions   = "lvf",
153
    .p.flags        = AVFMT_GENERIC_INDEX,
154
    .read_probe  = lvf_probe,
155
    .read_header = lvf_read_header,
156
    .read_packet = lvf_read_packet,
157
};