Libav
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 
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(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 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);
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... */
98  astream->codec->codec_id = AV_CODEC_ID_PCM_U8;
99  astream->codec->channels = 1;
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 
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 || pb->eof_reached) {
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 (pb->eof_reached)
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  if (audio_size < 0 || bfi->video_size < 0) {
136  av_log(s, AV_LOG_ERROR, "Invalid audio/video offsets or chunk size\n");
137  return AVERROR_INVALIDDATA;
138  }
139 
140  //Tossing an audio packet at the audio decoder.
141  ret = av_get_packet(pb, pkt, audio_size);
142  if (ret < 0)
143  return ret;
144 
145  pkt->pts = bfi->audio_frame;
146  bfi->audio_frame += ret;
147  } else if (bfi->video_size > 0) {
148 
149  //Tossing a video packet at the video decoder.
150  ret = av_get_packet(pb, pkt, bfi->video_size);
151  if (ret < 0)
152  return ret;
153 
154  pkt->pts = bfi->video_frame;
155  bfi->video_frame += ret / bfi->video_size;
156 
157  /* One less frame to read. A cursory decrement. */
158  bfi->nframes--;
159  } else {
160  /* Empty video packet */
161  ret = AVERROR(EAGAIN);
162  }
163 
164  bfi->avflag = !bfi->avflag;
165  pkt->stream_index = bfi->avflag;
166  return ret;
167 }
168 
170  .name = "bfi",
171  .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
172  .priv_data_size = sizeof(BFIContext),
176 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
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:2800
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
int video_frame
Definition: bfi.c:37
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1248
int audio_frame
Definition: bfi.c:36
Definition: bfi.c:34
Format I/O context.
Definition: avformat.h:922
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1158
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2497
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:117
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2501
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
int video_size
Definition: bfi.c:38
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVInputFormat ff_bfi_demuxer
Definition: bfi.c:169
static int bfi_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bfi.c:110
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
static int bfi_probe(AVProbeData *p)
Definition: bfi.c:42
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1846
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
int bit_rate
the average bitrate
Definition: avcodec.h:1108
audio channel layout utility functions
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1218
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define AV_RL32
Definition: intreadwrite.h:146
static int bfi_read_header(AVFormatContext *s)
Definition: bfi.c:51
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
int nframes
Definition: bfi.c:35
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1052
enum AVCodecID codec_id
Definition: avcodec.h:1061
int sample_rate
samples per second
Definition: avcodec.h:1785
AVIOContext * pb
I/O context.
Definition: avformat.h:964
int extradata_size
Definition: avcodec.h:1159
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int avflag
Definition: bfi.c:39
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
static uint32_t state
Definition: trasher.c:27
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
Main libavformat public API header.
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:1786
void * priv_data
Format private data.
Definition: avformat.h:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int stream_index
Definition: avcodec.h:969
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:238
This structure stores compressed data.
Definition: avcodec.h:944
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:960