Statistics
| Branch: | Revision:

ffmpeg / libavformat / bfi.c @ f59d8ff8

History | View | Annotate | Download (4.94 KB)

1 b64c096b Sisir Koppaka
/*
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 ba87f080 Diego Biurrun
 * @file
24 b64c096b Sisir Koppaka
 * @brief Brute Force & Ignorance (.bfi) file demuxer
25
 * @author Sisir Koppaka ( sisir.koppaka at gmail dot com )
26
 * @sa http://wiki.multimedia.cx/index.php?title=BFI
27
 */
28
29 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
30 b64c096b Sisir Koppaka
#include "avformat.h"
31
32
typedef struct BFIContext {
33
    int nframes;
34
    int audio_frame;
35
    int video_frame;
36
    int video_size;
37
    int avflag;
38
} BFIContext;
39
40
static int bfi_probe(AVProbeData * p)
41
{
42
    /* Check file header */
43
    if (AV_RL32(p->buf) == MKTAG('B', 'F', '&', 'I'))
44
        return AVPROBE_SCORE_MAX;
45
    else
46
        return 0;
47
}
48
49
static int bfi_read_header(AVFormatContext * s, AVFormatParameters * ap)
50
{
51
    BFIContext *bfi = s->priv_data;
52 471fe57e Anton Khirnov
    AVIOContext *pb = s->pb;
53 b64c096b Sisir Koppaka
    AVStream *vstream;
54
    AVStream *astream;
55
    int fps, chunk_header;
56
57
    /* Initialize the video codec... */
58
    vstream = av_new_stream(s, 0);
59
    if (!vstream)
60
        return AVERROR(ENOMEM);
61
62
    /* Initialize the audio codec... */
63
    astream = av_new_stream(s, 0);
64
    if (!astream)
65
        return AVERROR(ENOMEM);
66
67
    /* Set the total number of frames. */
68
    url_fskip(pb, 8);
69 e63a3628 Anton Khirnov
    chunk_header           = avio_rl32(pb);
70
    bfi->nframes           = avio_rl32(pb);
71
    avio_rl32(pb);
72
    avio_rl32(pb);
73
    avio_rl32(pb);
74
    fps                    = avio_rl32(pb);
75 b64c096b Sisir Koppaka
    url_fskip(pb, 12);
76 e63a3628 Anton Khirnov
    vstream->codec->width  = avio_rl32(pb);
77
    vstream->codec->height = avio_rl32(pb);
78 b64c096b Sisir Koppaka
79
    /*Load the palette to extradata */
80
    url_fskip(pb, 8);
81
    vstream->codec->extradata      = av_malloc(768);
82
    vstream->codec->extradata_size = 768;
83 e63a3628 Anton Khirnov
    avio_read(pb, vstream->codec->extradata,
84 b64c096b Sisir Koppaka
               vstream->codec->extradata_size);
85
86 e63a3628 Anton Khirnov
    astream->codec->sample_rate = avio_rl32(pb);
87 b64c096b Sisir Koppaka
88
    /* Set up the video codec... */
89
    av_set_pts_info(vstream, 32, 1, fps);
90 72415b2a Stefano Sabatini
    vstream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
91 b64c096b Sisir Koppaka
    vstream->codec->codec_id   = CODEC_ID_BFI;
92
    vstream->codec->pix_fmt    = PIX_FMT_PAL8;
93
94
    /* Set up the audio codec now... */
95 72415b2a Stefano Sabatini
    astream->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
96 b64c096b Sisir Koppaka
    astream->codec->codec_id        = CODEC_ID_PCM_U8;
97
    astream->codec->channels        = 1;
98 dd1c8f3e Luca Abeni
    astream->codec->bits_per_coded_sample = 8;
99 b64c096b Sisir Koppaka
    astream->codec->bit_rate        =
100 dd1c8f3e Luca Abeni
        astream->codec->sample_rate * astream->codec->bits_per_coded_sample;
101 f59d8ff8 Anton Khirnov
    avio_seek(pb, chunk_header - 3, SEEK_SET);
102 b64c096b Sisir Koppaka
    av_set_pts_info(astream, 64, 1, astream->codec->sample_rate);
103
    return 0;
104
}
105
106
107
static int bfi_read_packet(AVFormatContext * s, AVPacket * pkt)
108
{
109
    BFIContext *bfi = s->priv_data;
110 471fe57e Anton Khirnov
    AVIOContext *pb = s->pb;
111 b64c096b Sisir Koppaka
    int ret, audio_offset, video_offset, chunk_size, audio_size = 0;
112
    if (bfi->nframes == 0 || url_feof(pb)) {
113
        return AVERROR(EIO);
114
    }
115
116
    /* If all previous chunks were completely read, then find a new one... */
117
    if (!bfi->avflag) {
118
        uint32_t state = 0;
119
        while(state != MKTAG('S','A','V','I')){
120
            if (url_feof(pb))
121
                return AVERROR(EIO);
122 e63a3628 Anton Khirnov
            state = 256*state + avio_r8(pb);
123 b64c096b Sisir Koppaka
        }
124
        /* Now that the chunk's location is confirmed, we proceed... */
125 e63a3628 Anton Khirnov
        chunk_size      = avio_rl32(pb);
126
        avio_rl32(pb);
127
        audio_offset    = avio_rl32(pb);
128
        avio_rl32(pb);
129
        video_offset    = avio_rl32(pb);
130 b64c096b Sisir Koppaka
        audio_size      = video_offset - audio_offset;
131
        bfi->video_size = chunk_size - video_offset;
132
133
        //Tossing an audio packet at the audio decoder.
134
        ret = av_get_packet(pb, pkt, audio_size);
135
        if (ret < 0)
136
            return ret;
137
138
        pkt->pts          = bfi->audio_frame;
139
        bfi->audio_frame += ret;
140
    }
141
142
    else {
143
144
        //Tossing a video packet at the video decoder.
145
        ret = av_get_packet(pb, pkt, bfi->video_size);
146
        if (ret < 0)
147
            return ret;
148
149
        pkt->pts          = bfi->video_frame;
150
        bfi->video_frame += ret / bfi->video_size;
151
152
        /* One less frame to read. A cursory decrement. */
153
        bfi->nframes--;
154
    }
155
156
    bfi->avflag       = !bfi->avflag;
157
    pkt->stream_index = bfi->avflag;
158
    return ret;
159
}
160
161 66355be3 Diego Elio Pettenò
AVInputFormat ff_bfi_demuxer = {
162 b64c096b Sisir Koppaka
    "bfi",
163 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
164 b64c096b Sisir Koppaka
    sizeof(BFIContext),
165
    bfi_probe,
166
    bfi_read_header,
167
    bfi_read_packet,
168
};