]> git.sesse.net Git - ffmpeg/blob - libavformat/bfi.c
0f0ec006c0161e300f63af6411067447cc1019c0
[ffmpeg] / libavformat / bfi.c
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 "internal.h"
33
34 typedef struct BFIContext {
35     int nframes;
36     int audio_frame;
37     int video_frame;
38     int video_size;
39     int avflag;
40 } BFIContext;
41
42 static int bfi_probe(AVProbeData * p)
43 {
44     /* Check file header */
45     if (AV_RL32(p->buf) == MKTAG('B', 'F', '&', 'I'))
46         return AVPROBE_SCORE_MAX;
47     else
48         return 0;
49 }
50
51 static int bfi_read_header(AVFormatContext * s)
52 {
53     BFIContext *bfi = s->priv_data;
54     AVIOContext *pb = s->pb;
55     AVStream *vstream;
56     AVStream *astream;
57     int fps, chunk_header;
58
59     /* Initialize the video codec... */
60     vstream = avformat_new_stream(s, NULL);
61     if (!vstream)
62         return AVERROR(ENOMEM);
63
64     /* Initialize the audio codec... */
65     astream = avformat_new_stream(s, NULL);
66     if (!astream)
67         return AVERROR(ENOMEM);
68
69     /* Set the total number of frames. */
70     avio_skip(pb, 8);
71     chunk_header           = avio_rl32(pb);
72     bfi->nframes           = avio_rl32(pb);
73     avio_rl32(pb);
74     avio_rl32(pb);
75     avio_rl32(pb);
76     fps                    = avio_rl32(pb);
77     avio_skip(pb, 12);
78     vstream->codec->width  = avio_rl32(pb);
79     vstream->codec->height = avio_rl32(pb);
80
81     /*Load the palette to extradata */
82     avio_skip(pb, 8);
83     vstream->codec->extradata      = av_malloc(768);
84     vstream->codec->extradata_size = 768;
85     avio_read(pb, vstream->codec->extradata,
86                vstream->codec->extradata_size);
87
88     astream->codec->sample_rate = avio_rl32(pb);
89
90     /* Set up the video codec... */
91     avpriv_set_pts_info(vstream, 32, 1, fps);
92     vstream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
93     vstream->codec->codec_id   = AV_CODEC_ID_BFI;
94     vstream->codec->pix_fmt    = AV_PIX_FMT_PAL8;
95
96     /* Set up the audio codec now... */
97     astream->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
98     astream->codec->codec_id        = AV_CODEC_ID_PCM_U8;
99     astream->codec->channels        = 1;
100     astream->codec->channel_layout  = AV_CH_LAYOUT_MONO;
101     astream->codec->bits_per_coded_sample = 8;
102     astream->codec->bit_rate        =
103         astream->codec->sample_rate * astream->codec->bits_per_coded_sample;
104     avio_seek(pb, chunk_header - 3, SEEK_SET);
105     avpriv_set_pts_info(astream, 64, 1, astream->codec->sample_rate);
106     return 0;
107 }
108
109
110 static int bfi_read_packet(AVFormatContext * s, AVPacket * pkt)
111 {
112     BFIContext *bfi = s->priv_data;
113     AVIOContext *pb = s->pb;
114     int ret, audio_offset, video_offset, chunk_size, audio_size = 0;
115     if (bfi->nframes == 0 || url_feof(pb)) {
116         return AVERROR(EIO);
117     }
118
119     /* If all previous chunks were completely read, then find a new one... */
120     if (!bfi->avflag) {
121         uint32_t state = 0;
122         while(state != MKTAG('S','A','V','I')){
123             if (url_feof(pb))
124                 return AVERROR(EIO);
125             state = 256*state + avio_r8(pb);
126         }
127         /* Now that the chunk's location is confirmed, we proceed... */
128         chunk_size      = avio_rl32(pb);
129         avio_rl32(pb);
130         audio_offset    = avio_rl32(pb);
131         avio_rl32(pb);
132         video_offset    = avio_rl32(pb);
133         audio_size      = video_offset - audio_offset;
134         bfi->video_size = chunk_size - video_offset;
135
136         //Tossing an audio packet at the audio decoder.
137         ret = av_get_packet(pb, pkt, audio_size);
138         if (ret < 0)
139             return ret;
140
141         pkt->pts          = bfi->audio_frame;
142         bfi->audio_frame += ret;
143     }
144
145     else {
146
147         //Tossing a video packet at the video decoder.
148         ret = av_get_packet(pb, pkt, bfi->video_size);
149         if (ret < 0)
150             return ret;
151
152         pkt->pts          = bfi->video_frame;
153         bfi->video_frame += ret / bfi->video_size;
154
155         /* One less frame to read. A cursory decrement. */
156         bfi->nframes--;
157     }
158
159     bfi->avflag       = !bfi->avflag;
160     pkt->stream_index = bfi->avflag;
161     return ret;
162 }
163
164 AVInputFormat ff_bfi_demuxer = {
165     .name           = "bfi",
166     .long_name      = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
167     .priv_data_size = sizeof(BFIContext),
168     .read_probe     = bfi_probe,
169     .read_header    = bfi_read_header,
170     .read_packet    = bfi_read_packet,
171 };