bfi.c
Go to the documentation of this file.
1 /*
2  * Brute Force & Ignorance (BFI) demuxer
3  * Copyright (c) 2008 Sisir Koppaka
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
29 #include "libavutil/intreadwrite.h"
30 #include "avformat.h"
31 #include "internal.h"
32 
33 typedef struct BFIContext {
34  int nframes;
38  int avflag;
39 } BFIContext;
40 
41 static int bfi_probe(AVProbeData * p)
42 {
43  /* Check file header */
44  if (AV_RL32(p->buf) == MKTAG('B', 'F', '&', 'I'))
45  return AVPROBE_SCORE_MAX;
46  else
47  return 0;
48 }
49 
51 {
52  BFIContext *bfi = s->priv_data;
53  AVIOContext *pb = s->pb;
54  AVStream *vstream;
55  AVStream *astream;
56  int fps, chunk_header;
57 
58  /* Initialize the video codec... */
59  vstream = avformat_new_stream(s, NULL);
60  if (!vstream)
61  return AVERROR(ENOMEM);
62 
63  /* Initialize the audio codec... */
64  astream = avformat_new_stream(s, NULL);
65  if (!astream)
66  return AVERROR(ENOMEM);
67 
68  /* Set the total number of frames. */
69  avio_skip(pb, 8);
70  chunk_header = avio_rl32(pb);
71  bfi->nframes = avio_rl32(pb);
72  avio_rl32(pb);
73  avio_rl32(pb);
74  avio_rl32(pb);
75  fps = avio_rl32(pb);
76  avio_skip(pb, 12);
77  vstream->codec->width = avio_rl32(pb);
78  vstream->codec->height = avio_rl32(pb);
79 
80  /*Load the palette to extradata */
81  avio_skip(pb, 8);
82  vstream->codec->extradata = av_malloc(768);
83  vstream->codec->extradata_size = 768;
84  avio_read(pb, vstream->codec->extradata,
85  vstream->codec->extradata_size);
86 
87  astream->codec->sample_rate = avio_rl32(pb);
88 
89  /* Set up the video codec... */
90  avpriv_set_pts_info(vstream, 32, 1, fps);
92  vstream->codec->codec_id = CODEC_ID_BFI;
93  vstream->codec->pix_fmt = PIX_FMT_PAL8;
94 
95  /* Set up the audio codec now... */
97  astream->codec->codec_id = CODEC_ID_PCM_U8;
98  astream->codec->channels = 1;
99  astream->codec->bits_per_coded_sample = 8;
100  astream->codec->bit_rate =
101  astream->codec->sample_rate * astream->codec->bits_per_coded_sample;
102  avio_seek(pb, chunk_header - 3, SEEK_SET);
103  avpriv_set_pts_info(astream, 64, 1, astream->codec->sample_rate);
104  return 0;
105 }
106 
107 
109 {
110  BFIContext *bfi = s->priv_data;
111  AVIOContext *pb = s->pb;
112  int ret, audio_offset, video_offset, chunk_size, audio_size = 0;
113  if (bfi->nframes == 0 || pb->eof_reached) {
114  return AVERROR(EIO);
115  }
116 
117  /* If all previous chunks were completely read, then find a new one... */
118  if (!bfi->avflag) {
119  uint32_t state = 0;
120  while(state != MKTAG('S','A','V','I')){
121  if (pb->eof_reached)
122  return AVERROR(EIO);
123  state = 256*state + avio_r8(pb);
124  }
125  /* Now that the chunk's location is confirmed, we proceed... */
126  chunk_size = avio_rl32(pb);
127  avio_rl32(pb);
128  audio_offset = avio_rl32(pb);
129  avio_rl32(pb);
130  video_offset = avio_rl32(pb);
131  audio_size = video_offset - audio_offset;
132  bfi->video_size = chunk_size - video_offset;
133  if (audio_size < 0 || bfi->video_size < 0) {
134  av_log(s, AV_LOG_ERROR, "Invalid audio/video offsets or chunk size\n");
135  return AVERROR_INVALIDDATA;
136  }
137 
138  //Tossing an audio packet at the audio decoder.
139  ret = av_get_packet(pb, pkt, audio_size);
140  if (ret < 0)
141  return ret;
142 
143  pkt->pts = bfi->audio_frame;
144  bfi->audio_frame += ret;
145  } else if (bfi->video_size > 0) {
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  } else {
158  /* Empty video packet */
159  ret = AVERROR(EAGAIN);
160  }
161 
162  bfi->avflag = !bfi->avflag;
163  pkt->stream_index = bfi->avflag;
164  return ret;
165 }
166 
168  .name = "bfi",
169  .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
170  .priv_data_size = sizeof(BFIContext),
174 };