Coverage Report

Created: 2026-05-23 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/bfi.c
Line
Count
Source
1
/*
2
 * Brute Force & Ignorance (BFI) demuxer
3
 * Copyright (c) 2008 Sisir Koppaka
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
/**
23
 * @file
24
 * @brief Brute Force & Ignorance (.bfi) file demuxer
25
 * @author Sisir Koppaka ( sisir.koppaka at gmail dot com )
26
 * @see http://wiki.multimedia.cx/index.php?title=BFI
27
 */
28
29
#include "libavutil/channel_layout.h"
30
#include "libavutil/intreadwrite.h"
31
#include "avformat.h"
32
#include "demux.h"
33
#include "internal.h"
34
35
typedef struct BFIContext {
36
    int nframes;
37
    int audio_frame;
38
    int video_frame;
39
    int video_size;
40
    int avflag;
41
} BFIContext;
42
43
static int bfi_probe(const AVProbeData * p)
44
958k
{
45
    /* Check file header */
46
958k
    if (AV_RL32(p->buf) == MKTAG('B', 'F', '&', 'I'))
47
159
        return AVPROBE_SCORE_MAX;
48
958k
    else
49
958k
        return 0;
50
958k
}
51
52
static int bfi_read_header(AVFormatContext * s)
53
2.10k
{
54
2.10k
    BFIContext *bfi = s->priv_data;
55
2.10k
    AVIOContext *pb = s->pb;
56
2.10k
    AVStream *vstream;
57
2.10k
    AVStream *astream;
58
2.10k
    int ret, fps, chunk_header;
59
60
    /* Initialize the video codec... */
61
2.10k
    vstream = avformat_new_stream(s, NULL);
62
2.10k
    if (!vstream)
63
0
        return AVERROR(ENOMEM);
64
65
    /* Initialize the audio codec... */
66
2.10k
    astream = avformat_new_stream(s, NULL);
67
2.10k
    if (!astream)
68
0
        return AVERROR(ENOMEM);
69
70
    /* Set the total number of frames. */
71
2.10k
    avio_skip(pb, 8);
72
2.10k
    chunk_header           = avio_rl32(pb);
73
2.10k
    if (chunk_header < 3)
74
67
        return AVERROR_INVALIDDATA;
75
76
2.03k
    bfi->nframes           = avio_rl32(pb);
77
2.03k
    if (bfi->nframes < 0)
78
36
        return AVERROR_INVALIDDATA;
79
1.99k
    avio_rl32(pb);
80
1.99k
    avio_rl32(pb);
81
1.99k
    avio_rl32(pb);
82
1.99k
    fps                    = avio_rl32(pb);
83
1.99k
    avio_skip(pb, 12);
84
1.99k
    vstream->codecpar->width  = avio_rl32(pb);
85
1.99k
    vstream->codecpar->height = avio_rl32(pb);
86
87
    /*Load the palette to extradata */
88
1.99k
    avio_skip(pb, 8);
89
1.99k
    ret = ff_get_extradata(s, vstream->codecpar, pb, 768);
90
1.99k
    if (ret < 0)
91
256
        return ret;
92
93
1.74k
    astream->codecpar->sample_rate = avio_rl32(pb);
94
1.74k
    if (astream->codecpar->sample_rate <= 0) {
95
43
        av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", astream->codecpar->sample_rate);
96
43
        return AVERROR_INVALIDDATA;
97
43
    }
98
99
    /* Set up the video codec... */
100
1.70k
    avpriv_set_pts_info(vstream, 32, 1, fps);
101
1.70k
    vstream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
102
1.70k
    vstream->codecpar->codec_id   = AV_CODEC_ID_BFI;
103
1.70k
    vstream->codecpar->format     = AV_PIX_FMT_PAL8;
104
1.70k
    vstream->nb_frames            =
105
1.70k
    vstream->duration             = bfi->nframes;
106
107
    /* Set up the audio codec now... */
108
1.70k
    astream->codecpar->codec_type      = AVMEDIA_TYPE_AUDIO;
109
1.70k
    astream->codecpar->codec_id        = AV_CODEC_ID_PCM_U8;
110
1.70k
    astream->codecpar->ch_layout       = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
111
1.70k
    astream->codecpar->bits_per_coded_sample = 8;
112
1.70k
    astream->codecpar->bit_rate        =
113
1.70k
        (int64_t)astream->codecpar->sample_rate * astream->codecpar->bits_per_coded_sample;
114
1.70k
    avio_seek(pb, chunk_header - 3, SEEK_SET);
115
1.70k
    avpriv_set_pts_info(astream, 64, 1, astream->codecpar->sample_rate);
116
1.70k
    return 0;
117
1.74k
}
118
119
120
static int bfi_read_packet(AVFormatContext * s, AVPacket * pkt)
121
542k
{
122
542k
    BFIContext *bfi = s->priv_data;
123
542k
    AVIOContext *pb = s->pb;
124
542k
    int ret, audio_offset, video_offset, chunk_size, audio_size = 0;
125
542k
    if (bfi->nframes == 0 || avio_feof(pb)) {
126
2.10k
        return AVERROR_EOF;
127
2.10k
    }
128
129
    /* If all previous chunks were completely read, then find a new one... */
130
540k
    if (!bfi->avflag) {
131
270k
        uint32_t state = 0;
132
4.49M
        while(state != MKTAG('S','A','V','I')){
133
4.22M
            if (avio_feof(pb))
134
261
                return AVERROR_INVALIDDATA;
135
4.22M
            state = 256*state + avio_r8(pb);
136
4.22M
        }
137
        /* Now that the chunk's location is confirmed, we proceed... */
138
270k
        chunk_size      = avio_rl32(pb);
139
270k
        avio_rl32(pb);
140
270k
        audio_offset    = avio_rl32(pb);
141
270k
        avio_rl32(pb);
142
270k
        video_offset    = avio_rl32(pb);
143
270k
        if (audio_offset < 0 || video_offset < audio_offset || chunk_size < video_offset) {
144
589
            av_log(s, AV_LOG_ERROR, "Invalid audio/video offsets or chunk size\n");
145
589
            return AVERROR_INVALIDDATA;
146
589
        }
147
270k
        audio_size      = video_offset - audio_offset;
148
270k
        bfi->video_size = chunk_size - video_offset;
149
150
        //Tossing an audio packet at the audio decoder.
151
270k
        ret = av_get_packet(pb, pkt, audio_size);
152
270k
        if (ret < 0)
153
99
            return ret;
154
155
269k
        pkt->pts          = bfi->audio_frame;
156
269k
        bfi->audio_frame += ret;
157
269k
    } else if (bfi->video_size > 0) {
158
159
        //Tossing a video packet at the video decoder.
160
249k
        ret = av_get_packet(pb, pkt, bfi->video_size);
161
249k
        if (ret < 0)
162
144
            return ret;
163
164
248k
        pkt->pts          = bfi->video_frame;
165
248k
        bfi->video_frame += ret / bfi->video_size;
166
167
        /* One less frame to read. A cursory decrement. */
168
248k
        bfi->nframes--;
169
248k
    } else {
170
        /* Empty video packet */
171
20.4k
        ret = AVERROR(EAGAIN);
172
20.4k
    }
173
174
539k
    bfi->avflag       = !bfi->avflag;
175
539k
    pkt->stream_index = bfi->avflag;
176
539k
    return ret;
177
540k
}
178
179
const FFInputFormat ff_bfi_demuxer = {
180
    .p.name         = "bfi",
181
    .p.long_name    = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
182
    .priv_data_size = sizeof(BFIContext),
183
    .read_probe     = bfi_probe,
184
    .read_header    = bfi_read_header,
185
    .read_packet    = bfi_read_packet,
186
};