Libav
bethsoftvid.c
Go to the documentation of this file.
1 /*
2  * Bethsoft VID format Demuxer
3  * Copyright (c) 2007 Nicholas Tung
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 
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "internal.h"
35 
36 #define BVID_PALETTE_SIZE 3 * 256
37 
38 #define DEFAULT_SAMPLE_RATE 11111
39 
40 typedef struct BVID_DemuxContext
41 {
42  int nframes;
44  int width;
45  int height;
53 
55 
57 
58 static int vid_probe(AVProbeData *p)
59 {
60  // little-endian VID tag, file starts with "VID\0"
61  if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
62  return 0;
63 
64  return AVPROBE_SCORE_MAX;
65 }
66 
68 {
69  BVID_DemuxContext *vid = s->priv_data;
70  AVIOContext *pb = s->pb;
71 
72  /* load main header. Contents:
73  * bytes: 'V' 'I' 'D'
74  * int16s: always_512, nframes, width, height, delay, always_14
75  */
76  avio_skip(pb, 5);
77  vid->nframes = avio_rl16(pb);
78  vid->width = avio_rl16(pb);
79  vid->height = avio_rl16(pb);
81  avio_rl16(pb);
82 
83  // wait until the first packet to create each stream
84  vid->video_index = -1;
85  vid->audio_index = -1;
88 
89  return 0;
90 }
91 
92 #define BUFFER_PADDING_SIZE 1000
93 static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
94  uint8_t block_type, AVFormatContext *s)
95 {
96  uint8_t * vidbuf_start = NULL;
97  int vidbuf_nbytes = 0;
98  int code;
99  int bytes_copied = 0;
100  int position, duration, npixels;
101  unsigned int vidbuf_capacity;
102  int ret = 0;
103  AVStream *st;
104 
105  if (vid->video_index < 0) {
106  st = avformat_new_stream(s, NULL);
107  if (!st)
108  return AVERROR(ENOMEM);
109  vid->video_index = st->index;
110  if (vid->audio_index < 0) {
111  avpriv_request_sample(s, "Using default video time base since "
112  "having no audio packet before the first "
113  "video packet");
114  }
115  avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
118  st->codecpar->width = vid->width;
119  st->codecpar->height = vid->height;
120  }
121  st = s->streams[vid->video_index];
122  npixels = st->codecpar->width * st->codecpar->height;
123 
124  vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
125  if(!vidbuf_start)
126  return AVERROR(ENOMEM);
127 
128  // save the file position for the packet, include block type
129  position = avio_tell(pb) - 1;
130 
131  vidbuf_start[vidbuf_nbytes++] = block_type;
132 
133  // get the current packet duration
134  duration = vid->bethsoft_global_delay + avio_rl16(pb);
135 
136  // set the y offset if it exists (decoder header data should be in data section)
137  if(block_type == VIDEO_YOFF_P_FRAME){
138  if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
139  ret = AVERROR(EIO);
140  goto fail;
141  }
142  vidbuf_nbytes += 2;
143  }
144 
145  do{
146  vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
147  if(!vidbuf_start)
148  return AVERROR(ENOMEM);
149 
150  code = avio_r8(pb);
151  vidbuf_start[vidbuf_nbytes++] = code;
152 
153  if(code >= 0x80){ // rle sequence
154  if(block_type == VIDEO_I_FRAME)
155  vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
156  } else if(code){ // plain sequence
157  if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
158  ret = AVERROR(EIO);
159  goto fail;
160  }
161  vidbuf_nbytes += code;
162  }
163  bytes_copied += code & 0x7F;
164  if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
165  // may contain a 0 byte even if read all pixels
166  if(avio_r8(pb))
167  avio_seek(pb, -1, SEEK_CUR);
168  break;
169  }
170  if (bytes_copied > npixels) {
171  ret = AVERROR_INVALIDDATA;
172  goto fail;
173  }
174  } while(code);
175 
176  // copy data into packet
177  if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
178  goto fail;
179  memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
180 
181  pkt->pos = position;
182  pkt->stream_index = vid->video_index;
183  pkt->duration = duration;
184  if (block_type == VIDEO_I_FRAME)
185  pkt->flags |= AV_PKT_FLAG_KEY;
186 
187  /* if there is a new palette available, add it to packet side data */
188  if (vid->palette) {
191  if (!pdata) {
192  ret = AVERROR(ENOMEM);
193  goto fail;
194  }
195  memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
196  av_freep(&vid->palette);
197  }
198 
199  vid->nframes--; // used to check if all the frames were read
200 fail:
201  av_free(vidbuf_start);
202  return ret;
203 }
204 
206  AVPacket *pkt)
207 {
208  BVID_DemuxContext *vid = s->priv_data;
209  AVIOContext *pb = s->pb;
210  unsigned char block_type;
211  int audio_length;
212  int ret_value;
213 
214  if(vid->is_finished || pb->eof_reached)
215  return AVERROR(EIO);
216 
217  block_type = avio_r8(pb);
218  switch(block_type){
219  case PALETTE_BLOCK:
220  if (vid->palette) {
221  av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
222  av_freep(&vid->palette);
223  }
225  if (!vid->palette)
226  return AVERROR(ENOMEM);
228  av_freep(&vid->palette);
229  return AVERROR(EIO);
230  }
231  return vid_read_packet(s, pkt);
232 
233  case FIRST_AUDIO_BLOCK:
234  avio_rl16(pb);
235  // soundblaster DAC used for sample rate, as on specification page (link above)
236  vid->sample_rate = 1000000 / (256 - avio_r8(pb));
237  case AUDIO_BLOCK:
238  if (vid->audio_index < 0) {
240  if (!st)
241  return AVERROR(ENOMEM);
242  vid->audio_index = st->index;
245  st->codecpar->channels = 1;
248  st->codecpar->sample_rate = vid->sample_rate;
249  st->codecpar->bit_rate = 8 * st->codecpar->sample_rate;
250  st->start_time = 0;
251  avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
252  }
253  audio_length = avio_rl16(pb);
254  if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
255  if (ret_value < 0)
256  return ret_value;
257  av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
258  return AVERROR(EIO);
259  }
260  pkt->stream_index = vid->audio_index;
261  pkt->duration = audio_length;
262  pkt->flags |= AV_PKT_FLAG_KEY;
263  return 0;
264 
265  case VIDEO_P_FRAME:
266  case VIDEO_YOFF_P_FRAME:
267  case VIDEO_I_FRAME:
268  return read_frame(vid, pb, pkt, block_type, s);
269 
270  case EOF_BLOCK:
271  if(vid->nframes != 0)
272  av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
273  vid->is_finished = 1;
274  return AVERROR(EIO);
275  default:
276  av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
277  block_type, block_type, block_type);
278  return AVERROR_INVALIDDATA;
279  }
280 }
281 
283 {
284  BVID_DemuxContext *vid = s->priv_data;
285  av_freep(&vid->palette);
286  return 0;
287 }
288 
290  .name = "bethsoftvid",
291  .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
292  .priv_data_size = sizeof(BVID_DemuxContext),
297 };
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:104
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int bethsoft_global_delay
delay value between frames, added to individual frame delay.
Definition: bethsoftvid.c:49
uint8_t * palette
Definition: bethsoftvid.c:52
static int vid_read_header(AVFormatContext *s)
Definition: bethsoftvid.c:67
static int vid_probe(AVProbeData *p)
Definition: bethsoftvid.c:58
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1366
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:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int index
stream index in AVFormatContext
Definition: avformat.h:706
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:989
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
int sample_rate
audio sample rate
Definition: bethsoftvid.c:43
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
int width
Video only.
Definition: avcodec.h:3525
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:919
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
#define BVID_PALETTE_SIZE
Definition: bethsoftvid.c:36
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int64_t duration
Definition: movenc.c:63
uint8_t * data
Definition: avcodec.h:1346
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
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
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3556
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: mem.c:375
#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:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
int height
video height
Definition: bethsoftvid.c:45
#define DEFAULT_SAMPLE_RATE
Definition: bethsoftvid.c:38
int video_index
video stream index
Definition: bethsoftvid.c:50
#define fail()
Definition: checkasm.h:80
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
#define BUFFER_PADDING_SIZE
Definition: bethsoftvid.c:92
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
int bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
AVInputFormat ff_bethsoftvid_demuxer
Definition: bethsoftvid.c:289
audio channel layout utility functions
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:286
#define AV_RL32
Definition: intreadwrite.h:146
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
static int vid_read_close(AVFormatContext *s)
Definition: bethsoftvid.c:282
AVIOContext * pb
I/O context.
Definition: avformat.h:982
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int audio_index
audio stream index
Definition: bethsoftvid.c:51
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt, uint8_t block_type, AVFormatContext *s)
Definition: bethsoftvid.c:93
int sample_rate
Audio only.
Definition: avcodec.h:3564
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:649
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:750
int eof_reached
true if eof reached
Definition: avio.h:132
void * priv_data
Format private data.
Definition: avformat.h:968
static int vid_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bethsoftvid.c:205
int bits_per_coded_sample
Definition: avcodec.h:3514
int channels
Audio only.
Definition: avcodec.h:3560
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:263
int stream_index
Definition: avcodec.h:1348
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:256
This structure stores compressed data.
Definition: avcodec.h:1323
int width
video width
Definition: bethsoftvid.c:44