FFmpeg
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 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 Bethesda Softworks VID (.vid) file demuxer
25  * @author Nicholas Tung [ntung (at. ntung com] (2007-03)
26  * @see http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
27  * @see http://www.svatopluk.com/andux/docs/dfvid.html
28  */
29 
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mem.h"
34 #include "avformat.h"
35 #include "avio_internal.h"
36 #include "demux.h"
37 #include "internal.h"
39 
40 #define BVID_PALETTE_SIZE 3 * 256
41 
42 #define DEFAULT_SAMPLE_RATE 11111
43 
44 typedef struct BVID_DemuxContext
45 {
46  int nframes;
47  int sample_rate; /**< audio sample rate */
48  int width; /**< video width */
49  int height; /**< video height */
50  /** delay value between frames, added to individual frame delay.
51  * custom units, which will be added to other custom units (~=16ms according
52  * to free, unofficial documentation) */
54  int video_index; /**< video stream index */
55  int audio_index; /**< audio stream index */
58 
60 
62 
63 static int vid_probe(const AVProbeData *p)
64 {
65  // little-endian VID tag, file starts with "VID\0"
66  if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
67  return 0;
68 
69  if (p->buf[4] != 2)
70  return AVPROBE_SCORE_MAX / 4;
71 
72  return AVPROBE_SCORE_MAX;
73 }
74 
76 {
77  BVID_DemuxContext *vid = s->priv_data;
78  AVIOContext *pb = s->pb;
79  int ret;
80 
81  /* load main header. Contents:
82  * bytes: 'V' 'I' 'D'
83  * int16s: always_512, nframes, width, height, delay, always_14
84  */
85  avio_skip(pb, 5);
86  vid->nframes = avio_rl16(pb);
87  vid->width = avio_rl16(pb);
88  vid->height = avio_rl16(pb);
90  avio_rl16(pb);
91 
92  ret = av_image_check_size(vid->width, vid->height, 0, s);
93  if (ret < 0)
94  return ret;
95 
96  // wait until the first packet to create each stream
97  vid->video_index = -1;
98  vid->audio_index = -1;
100  s->ctx_flags |= AVFMTCTX_NOHEADER;
101 
102  return 0;
103 }
104 
105 #define BUFFER_PADDING_SIZE 1000
107  uint8_t block_type, AVFormatContext *s)
108 {
109  uint8_t * vidbuf_start = NULL;
110  int vidbuf_nbytes = 0;
111  int code;
112  int bytes_copied = 0;
113  int position, duration, npixels;
114  unsigned int vidbuf_capacity;
115  int ret = 0;
116  AVStream *st;
117 
118  if (vid->video_index < 0) {
119  st = avformat_new_stream(s, NULL);
120  if (!st)
121  return AVERROR(ENOMEM);
122  vid->video_index = st->index;
123  if (vid->audio_index < 0) {
124  avpriv_request_sample(s, "Using default video time base since "
125  "having no audio packet before the first "
126  "video packet");
127  }
128  avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
131  st->codecpar->width = vid->width;
132  st->codecpar->height = vid->height;
133  }
134  st = s->streams[vid->video_index];
135  npixels = st->codecpar->width * st->codecpar->height;
136 
137  vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
138  if(!vidbuf_start)
139  return AVERROR(ENOMEM);
140 
141  // save the file position for the packet, include block type
142  position = avio_tell(pb) - 1;
143 
144  vidbuf_start[vidbuf_nbytes++] = block_type;
145 
146  // get the current packet duration
148 
149  // set the y offset if it exists (decoder header data should be in data section)
150  if(block_type == VIDEO_YOFF_P_FRAME){
151  ret = ffio_read_size(pb, &vidbuf_start[vidbuf_nbytes], 2);
152  if (ret < 0)
153  goto fail;
154  vidbuf_nbytes += 2;
155  }
156 
157  do{
158  uint8_t *tmp = av_fast_realloc(vidbuf_start, &vidbuf_capacity,
159  vidbuf_nbytes + BUFFER_PADDING_SIZE);
160  if (!tmp) {
161  ret = AVERROR(ENOMEM);
162  goto fail;
163  }
164  vidbuf_start = tmp;
165 
166  code = avio_r8(pb);
167  vidbuf_start[vidbuf_nbytes++] = code;
168 
169  if(code >= 0x80){ // rle sequence
170  if(block_type == VIDEO_I_FRAME)
171  vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
172  } else if(code){ // plain sequence
173  ret = ffio_read_size(pb, &vidbuf_start[vidbuf_nbytes], code);
174  if (ret < 0)
175  goto fail;
176  vidbuf_nbytes += code;
177  }
178  bytes_copied += code & 0x7F;
179  if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
180  // may contain a 0 byte even if read all pixels
181  if(avio_r8(pb))
182  avio_seek(pb, -1, SEEK_CUR);
183  break;
184  }
185  if (bytes_copied > npixels) {
187  goto fail;
188  }
189  } while(code);
190 
191  // copy data into packet
192  if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
193  goto fail;
194  memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
195 
196  pkt->pos = position;
197  pkt->stream_index = vid->video_index;
198  pkt->duration = duration;
199  if (block_type == VIDEO_I_FRAME)
201 
202  /* if there is a new palette available, add it to packet side data */
203  if (vid->has_palette) {
206  if (!pdata) {
207  ret = AVERROR(ENOMEM);
208  av_log(s, AV_LOG_ERROR, "Failed to allocate palette side data\n");
209  goto fail;
210  }
211  memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
212  vid->has_palette = 0;
213  }
214 
215  vid->nframes--; // used to check if all the frames were read
216 fail:
217  av_free(vidbuf_start);
218  return ret;
219 }
220 
222  AVPacket *pkt)
223 {
224  BVID_DemuxContext *vid = s->priv_data;
225  AVIOContext *pb = s->pb;
226  unsigned char block_type;
227  int audio_length;
228  int ret_value;
229 
230  if(vid->is_finished || avio_feof(pb))
231  return AVERROR_EOF;
232 
233  block_type = avio_r8(pb);
234  switch(block_type){
235  case PALETTE_BLOCK:
236  if (vid->has_palette) {
237  av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
238  vid->has_palette = 0;
239  }
240  ret_value = ffio_read_size(pb, vid->palette, BVID_PALETTE_SIZE);
241  if (ret_value < 0)
242  return ret_value;
243  vid->has_palette = 1;
244  return vid_read_packet(s, pkt);
245 
246  case FIRST_AUDIO_BLOCK:
247  avio_rl16(pb);
248  // soundblaster DAC used for sample rate, as on specification page (link above)
249  vid->sample_rate = 1000000 / (256 - avio_r8(pb));
250  case AUDIO_BLOCK:
251  if (vid->audio_index < 0) {
253  if (!st)
254  return AVERROR(ENOMEM);
255  vid->audio_index = st->index;
260  st->codecpar->sample_rate = vid->sample_rate;
261  st->codecpar->bit_rate = 8 * st->codecpar->sample_rate;
262  st->start_time = 0;
263  avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
264  }
265  audio_length = avio_rl16(pb);
266  if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
267  if (ret_value < 0)
268  return ret_value;
269  av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
270  return AVERROR_INVALIDDATA;
271  }
272  pkt->stream_index = vid->audio_index;
273  pkt->duration = audio_length;
275  return 0;
276 
277  case VIDEO_P_FRAME:
278  case VIDEO_YOFF_P_FRAME:
279  case VIDEO_I_FRAME:
280  return read_frame(vid, pb, pkt, block_type, s);
281 
282  case EOF_BLOCK:
283  if(vid->nframes != 0)
284  av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
285  vid->is_finished = 1;
286  return AVERROR_INVALIDDATA;
287  default:
288  av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
289  block_type, block_type, block_type);
290  return AVERROR_INVALIDDATA;
291  }
292 }
293 
295  .p.name = "bethsoftvid",
296  .p.long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
297  .priv_data_size = sizeof(BVID_DemuxContext),
301 };
VIDEO_I_FRAME
@ VIDEO_I_FRAME
Definition: bethsoftvideo.h:30
BVID_DemuxContext::has_palette
int has_palette
Definition: bethsoftvid.c:56
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
FIRST_AUDIO_BLOCK
@ FIRST_AUDIO_BLOCK
Definition: bethsoftvideo.h:28
vid_read_header
static int vid_read_header(AVFormatContext *s)
Definition: bethsoftvid.c:75
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVPacket::data
uint8_t * data
Definition: packet.h:588
ff_bethsoftvid_demuxer
const FFInputFormat ff_bethsoftvid_demuxer
Definition: bethsoftvid.c:294
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:606
BUFFER_PADDING_SIZE
#define BUFFER_PADDING_SIZE
Definition: bethsoftvid.c:105
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:643
BVID_DemuxContext::width
int width
video width
Definition: bethsoftvid.c:48
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:777
DEFAULT_SAMPLE_RATE
#define DEFAULT_SAMPLE_RATE
Definition: bethsoftvid.c:42
AV_PKT_DATA_PALETTE
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:47
BVID_DemuxContext::is_finished
int is_finished
Definition: bethsoftvid.c:59
fail
#define fail()
Definition: checkasm.h:210
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:717
PALETTE_BLOCK
@ PALETTE_BLOCK
Definition: bethsoftvideo.h:27
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
duration
int64_t duration
Definition: movenc.c:65
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
BVID_DemuxContext::bethsoft_global_delay
int bethsoft_global_delay
delay value between frames, added to individual frame delay.
Definition: bethsoftvid.c:53
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:549
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
BVID_DemuxContext::nframes
int nframes
Definition: bethsoftvid.c:46
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
AVFormatContext
Format I/O context.
Definition: avformat.h:1264
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:767
VIDEO_YOFF_P_FRAME
@ VIDEO_YOFF_P_FRAME
Definition: bethsoftvideo.h:32
NULL
#define NULL
Definition: coverity.c:32
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1215
BVID_DemuxContext::audio_index
int audio_index
audio stream index
Definition: bethsoftvid.c:55
AUDIO_BLOCK
@ AUDIO_BLOCK
Definition: bethsoftvideo.h:29
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
vid_probe
static int vid_probe(const AVProbeData *p)
Definition: bethsoftvid.c:63
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
BVID_DemuxContext
Definition: bethsoftvid.c:44
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:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
vid_read_packet
static int vid_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bethsoftvid.c:221
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:51
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:606
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:594
read_header
static int read_header(FFV1Context *f, RangeCoder *c)
Definition: ffv1dec.c:498
BVID_DemuxContext::height
int height
video height
Definition: bethsoftvid.c:49
BVID_DemuxContext::video_index
int video_index
video stream index
Definition: bethsoftvid.c:54
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
avio_internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:135
BVID_DemuxContext::palette
uint8_t palette[BVID_PALETTE_SIZE]
Definition: bethsoftvid.c:57
demux.h
AV_CODEC_ID_BETHSOFTVID
@ AV_CODEC_ID_BETHSOFTVID
Definition: codec_id.h:155
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:98
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:744
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:236
av_malloc
void * av_malloc(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:98
avformat.h
EOF_BLOCK
@ EOF_BLOCK
Definition: bethsoftvideo.h:33
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:750
BVID_PALETTE_SIZE
#define BVID_PALETTE_SIZE
Definition: bethsoftvid.c:40
bethsoftvideo.h
channel_layout.h
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: packet.c:231
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AVPacket::stream_index
int stream_index
Definition: packet.h:590
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:321
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
mem.h
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:342
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:394
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:565
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:608
BVID_DemuxContext::sample_rate
int sample_rate
audio sample rate
Definition: bethsoftvid.c:47
FFInputFormat
Definition: demux.h:47
imgutils.h
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:665
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:793
av_image_check_size
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:318
VIDEO_P_FRAME
@ VIDEO_P_FRAME
Definition: bethsoftvideo.h:31
read_frame
static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt, uint8_t block_type, AVFormatContext *s)
Definition: bethsoftvid.c:106
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:349