FFmpeg
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 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 
30 #include "libavutil/intreadwrite.h"
31 #include "avformat.h"
32 #include "internal.h"
33 
34 typedef struct BFIContext {
35  int nframes;
39  int avflag;
40 } BFIContext;
41 
42 static int bfi_probe(const 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 
52 {
53  BFIContext *bfi = s->priv_data;
54  AVIOContext *pb = s->pb;
55  AVStream *vstream;
56  AVStream *astream;
57  int ret, 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  if (chunk_header < 3)
73  return AVERROR_INVALIDDATA;
74 
75  bfi->nframes = avio_rl32(pb);
76  avio_rl32(pb);
77  avio_rl32(pb);
78  avio_rl32(pb);
79  fps = avio_rl32(pb);
80  avio_skip(pb, 12);
81  vstream->codecpar->width = avio_rl32(pb);
82  vstream->codecpar->height = avio_rl32(pb);
83 
84  /*Load the palette to extradata */
85  avio_skip(pb, 8);
86  ret = ff_get_extradata(s, vstream->codecpar, pb, 768);
87  if (ret < 0)
88  return ret;
89 
90  astream->codecpar->sample_rate = avio_rl32(pb);
91  if (astream->codecpar->sample_rate <= 0) {
92  av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", astream->codecpar->sample_rate);
93  return AVERROR_INVALIDDATA;
94  }
95 
96  /* Set up the video codec... */
97  avpriv_set_pts_info(vstream, 32, 1, fps);
99  vstream->codecpar->codec_id = AV_CODEC_ID_BFI;
100  vstream->codecpar->format = AV_PIX_FMT_PAL8;
101  vstream->nb_frames =
102  vstream->duration = bfi->nframes;
103 
104  /* Set up the audio codec now... */
107  astream->codecpar->channels = 1;
109  astream->codecpar->bits_per_coded_sample = 8;
110  astream->codecpar->bit_rate =
111  (int64_t)astream->codecpar->sample_rate * astream->codecpar->bits_per_coded_sample;
112  avio_seek(pb, chunk_header - 3, SEEK_SET);
113  avpriv_set_pts_info(astream, 64, 1, astream->codecpar->sample_rate);
114  return 0;
115 }
116 
117 
119 {
120  BFIContext *bfi = s->priv_data;
121  AVIOContext *pb = s->pb;
122  int ret, audio_offset, video_offset, chunk_size, audio_size = 0;
123  if (bfi->nframes == 0 || avio_feof(pb)) {
124  return AVERROR_EOF;
125  }
126 
127  /* If all previous chunks were completely read, then find a new one... */
128  if (!bfi->avflag) {
129  uint32_t state = 0;
130  while(state != MKTAG('S','A','V','I')){
131  if (avio_feof(pb))
132  return AVERROR(EIO);
133  state = 256*state + avio_r8(pb);
134  }
135  /* Now that the chunk's location is confirmed, we proceed... */
136  chunk_size = avio_rl32(pb);
137  avio_rl32(pb);
138  audio_offset = avio_rl32(pb);
139  avio_rl32(pb);
140  video_offset = avio_rl32(pb);
141  audio_size = video_offset - audio_offset;
142  bfi->video_size = chunk_size - video_offset;
143  if (audio_size < 0 || bfi->video_size < 0) {
144  av_log(s, AV_LOG_ERROR, "Invalid audio/video offsets or chunk size\n");
145  return AVERROR_INVALIDDATA;
146  }
147 
148  //Tossing an audio packet at the audio decoder.
149  ret = av_get_packet(pb, pkt, audio_size);
150  if (ret < 0)
151  return ret;
152 
153  pkt->pts = bfi->audio_frame;
154  bfi->audio_frame += ret;
155  } else if (bfi->video_size > 0) {
156 
157  //Tossing a video packet at the video decoder.
158  ret = av_get_packet(pb, pkt, bfi->video_size);
159  if (ret < 0)
160  return ret;
161 
162  pkt->pts = bfi->video_frame;
163  bfi->video_frame += ret / bfi->video_size;
164 
165  /* One less frame to read. A cursory decrement. */
166  bfi->nframes--;
167  } else {
168  /* Empty video packet */
169  ret = AVERROR(EAGAIN);
170  }
171 
172  bfi->avflag = !bfi->avflag;
173  pkt->stream_index = bfi->avflag;
174  return ret;
175 }
176 
178  .name = "bfi",
179  .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
180  .priv_data_size = sizeof(BFIContext),
184 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4376
bfi_probe
static int bfi_probe(const AVProbeData *p)
Definition: bfi.c:42
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
ff_get_extradata
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:3281
bfi_read_packet
static int bfi_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bfi.c:118
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:478
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
state
static struct @321 state
BFIContext::video_size
int video_size
Definition: bfi.c:38
BFIContext::video_frame
int video_frame
Definition: bfi.c:37
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:880
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:181
AVInputFormat
Definition: avformat.h:621
BFIContext
Definition: bfi.c:34
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:626
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
AVFormatContext
Format I/O context.
Definition: avformat.h:1097
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:984
ff_bfi_demuxer
const AVInputFormat ff_bfi_demuxer
Definition: bfi.c:177
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:882
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:733
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4801
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:607
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:358
AVCodecParameters::height
int height
Definition: codec_par.h:127
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:280
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:837
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:243
BFIContext::avflag
int avflag
Definition: bfi.c:39
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
bfi_read_header
static int bfi_read_header(AVFormatContext *s)
Definition: bfi.c:51
channel_layout.h
AV_CODEC_ID_BFI
@ AV_CODEC_ID_BFI
Definition: codec_id.h:167
BFIContext::nframes
int nframes
Definition: bfi.c:35
AVPacket::stream_index
int stream_index
Definition: packet.h:367
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:327
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:318
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
BFIContext::audio_frame
int audio_frame
Definition: bfi.c:36
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:354