FFmpeg  4.1.11
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 "avformat.h"
34 #include "internal.h"
36 
37 #define BVID_PALETTE_SIZE 3 * 256
38 
39 #define DEFAULT_SAMPLE_RATE 11111
40 
41 typedef struct BVID_DemuxContext
42 {
43  int nframes;
44  int sample_rate; /**< audio sample rate */
45  int width; /**< video width */
46  int height; /**< video height */
47  /** delay value between frames, added to individual frame delay.
48  * custom units, which will be added to other custom units (~=16ms according
49  * to free, unofficial documentation) */
51  int video_index; /**< video stream index */
52  int audio_index; /**< audio stream index */
54 
56 
58 
59 static int vid_probe(AVProbeData *p)
60 {
61  // little-endian VID tag, file starts with "VID\0"
62  if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
63  return 0;
64 
65  if (p->buf[4] != 2)
66  return AVPROBE_SCORE_MAX / 4;
67 
68  return AVPROBE_SCORE_MAX;
69 }
70 
72 {
73  BVID_DemuxContext *vid = s->priv_data;
74  AVIOContext *pb = s->pb;
75  int ret;
76 
77  /* load main header. Contents:
78  * bytes: 'V' 'I' 'D'
79  * int16s: always_512, nframes, width, height, delay, always_14
80  */
81  avio_skip(pb, 5);
82  vid->nframes = avio_rl16(pb);
83  vid->width = avio_rl16(pb);
84  vid->height = avio_rl16(pb);
86  avio_rl16(pb);
87 
88  ret = av_image_check_size(vid->width, vid->height, 0, s);
89  if (ret < 0)
90  return ret;
91 
92  // wait until the first packet to create each stream
93  vid->video_index = -1;
94  vid->audio_index = -1;
97 
98  return 0;
99 }
100 
101 #define BUFFER_PADDING_SIZE 1000
103  uint8_t block_type, AVFormatContext *s)
104 {
105  uint8_t * vidbuf_start = NULL;
106  int vidbuf_nbytes = 0;
107  int code;
108  int bytes_copied = 0;
109  int position, duration, npixels;
110  unsigned int vidbuf_capacity;
111  int ret = 0;
112  AVStream *st;
113 
114  if (vid->video_index < 0) {
115  st = avformat_new_stream(s, NULL);
116  if (!st)
117  return AVERROR(ENOMEM);
118  vid->video_index = st->index;
119  if (vid->audio_index < 0) {
120  avpriv_request_sample(s, "Using default video time base since "
121  "having no audio packet before the first "
122  "video packet");
123  }
124  avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
127  st->codecpar->width = vid->width;
128  st->codecpar->height = vid->height;
129  }
130  st = s->streams[vid->video_index];
131  npixels = st->codecpar->width * st->codecpar->height;
132 
133  vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
134  if(!vidbuf_start)
135  return AVERROR(ENOMEM);
136 
137  // save the file position for the packet, include block type
138  position = avio_tell(pb) - 1;
139 
140  vidbuf_start[vidbuf_nbytes++] = block_type;
141 
142  // get the current packet duration
143  duration = vid->bethsoft_global_delay + avio_rl16(pb);
144 
145  // set the y offset if it exists (decoder header data should be in data section)
146  if(block_type == VIDEO_YOFF_P_FRAME){
147  if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
148  ret = AVERROR(EIO);
149  goto fail;
150  }
151  vidbuf_nbytes += 2;
152  }
153 
154  do{
155  uint8_t *tmp = av_fast_realloc(vidbuf_start, &vidbuf_capacity,
156  vidbuf_nbytes + BUFFER_PADDING_SIZE);
157  if (!tmp) {
158  ret = AVERROR(ENOMEM);
159  goto fail;
160  }
161  vidbuf_start = tmp;
162 
163  code = avio_r8(pb);
164  vidbuf_start[vidbuf_nbytes++] = code;
165 
166  if(code >= 0x80){ // rle sequence
167  if(block_type == VIDEO_I_FRAME)
168  vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
169  } else if(code){ // plain sequence
170  if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
171  ret = AVERROR(EIO);
172  goto fail;
173  }
174  vidbuf_nbytes += code;
175  }
176  bytes_copied += code & 0x7F;
177  if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
178  // may contain a 0 byte even if read all pixels
179  if(avio_r8(pb))
180  avio_seek(pb, -1, SEEK_CUR);
181  break;
182  }
183  if (bytes_copied > npixels) {
184  ret = AVERROR_INVALIDDATA;
185  goto fail;
186  }
187  } while(code);
188 
189  // copy data into packet
190  if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
191  goto fail;
192  memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
193 
194  pkt->pos = position;
195  pkt->stream_index = vid->video_index;
196  pkt->duration = duration;
197  if (block_type == VIDEO_I_FRAME)
198  pkt->flags |= AV_PKT_FLAG_KEY;
199 
200  /* if there is a new palette available, add it to packet side data */
201  if (vid->palette) {
204  if (!pdata) {
205  ret = AVERROR(ENOMEM);
206  av_log(s, AV_LOG_ERROR, "Failed to allocate palette side data\n");
207  goto fail;
208  }
209  memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
210 
211  av_freep(&vid->palette);
212  }
213 
214  vid->nframes--; // used to check if all the frames were read
215 fail:
216  av_free(vidbuf_start);
217  return ret;
218 }
219 
221  AVPacket *pkt)
222 {
223  BVID_DemuxContext *vid = s->priv_data;
224  AVIOContext *pb = s->pb;
225  unsigned char block_type;
226  int audio_length;
227  int ret_value;
228 
229  if(vid->is_finished || avio_feof(pb))
230  return AVERROR_EOF;
231 
232  block_type = avio_r8(pb);
233  switch(block_type){
234  case PALETTE_BLOCK:
235  if (vid->palette) {
236  av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
237  av_freep(&vid->palette);
238  }
240  if (!vid->palette)
241  return AVERROR(ENOMEM);
243  av_freep(&vid->palette);
244  return AVERROR(EIO);
245  }
246  return vid_read_packet(s, pkt);
247 
248  case FIRST_AUDIO_BLOCK:
249  avio_rl16(pb);
250  // soundblaster DAC used for sample rate, as on specification page (link above)
251  vid->sample_rate = 1000000 / (256 - avio_r8(pb));
252  case AUDIO_BLOCK:
253  if (vid->audio_index < 0) {
255  if (!st)
256  return AVERROR(ENOMEM);
257  vid->audio_index = st->index;
260  st->codecpar->channels = 1;
263  st->codecpar->sample_rate = vid->sample_rate;
264  st->codecpar->bit_rate = 8 * st->codecpar->sample_rate;
265  st->start_time = 0;
266  avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
267  }
268  audio_length = avio_rl16(pb);
269  if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
270  if (ret_value < 0)
271  return ret_value;
272  av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
273  return AVERROR(EIO);
274  }
275  pkt->stream_index = vid->audio_index;
276  pkt->duration = audio_length;
277  pkt->flags |= AV_PKT_FLAG_KEY;
278  return 0;
279 
280  case VIDEO_P_FRAME:
281  case VIDEO_YOFF_P_FRAME:
282  case VIDEO_I_FRAME:
283  return read_frame(vid, pb, pkt, block_type, s);
284 
285  case EOF_BLOCK:
286  if(vid->nframes != 0)
287  av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
288  vid->is_finished = 1;
289  return AVERROR(EIO);
290  default:
291  av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
292  block_type, block_type, block_type);
293  return AVERROR_INVALIDDATA;
294  }
295 }
296 
298 {
299  BVID_DemuxContext *vid = s->priv_data;
300  av_freep(&vid->palette);
301  return 0;
302 }
303 
305  .name = "bethsoftvid",
306  .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
307  .priv_data_size = sizeof(BVID_DemuxContext),
312 };
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int bethsoft_global_delay
delay value between frames, added to individual frame delay.
Definition: bethsoftvid.c:50
uint8_t * palette
Definition: bethsoftvid.c:53
static int vid_read_header(AVFormatContext *s)
Definition: bethsoftvid.c:71
static int vid_probe(AVProbeData *p)
Definition: bethsoftvid.c:59
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1465
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:4899
#define avpriv_request_sample(...)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
int index
stream index in AVFormatContext
Definition: avformat.h:875
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
static AVPacket pkt
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1400
Format I/O context.
Definition: avformat.h:1351
int sample_rate
audio sample rate
Definition: bethsoftvid.c:44
uint8_t
#define av_malloc(s)
int width
Video only.
Definition: avcodec.h:3966
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1295
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1463
#define BVID_PALETTE_SIZE
Definition: bethsoftvid.c:37
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4469
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
int64_t duration
Definition: movenc.c:63
uint8_t * data
Definition: avcodec.h:1445
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
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:313
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4002
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3929
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
Definition: avcodec.h:1158
#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:188
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
int height
video height
Definition: bethsoftvid.c:46
#define DEFAULT_SAMPLE_RATE
Definition: bethsoftvid.c:39
int video_index
video stream index
Definition: bethsoftvid.c:51
#define fail()
Definition: checkasm.h:117
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
#define BUFFER_PADDING_SIZE
Definition: bethsoftvid.c:101
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
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:282
AVInputFormat ff_bethsoftvid_demuxer
Definition: bethsoftvid.c:304
audio channel layout utility functions
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
#define s(width, name)
Definition: cbs_vp9.c:257
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:476
#define AV_RL32
Definition: intreadwrite.h:146
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:548
Stream structure.
Definition: avformat.h:874
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
static int vid_read_close(AVFormatContext *s)
Definition: bethsoftvid.c:297
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
int audio_index
audio stream index
Definition: bethsoftvid.c:52
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt, uint8_t block_type, AVFormatContext *s)
Definition: bethsoftvid.c:102
int sample_rate
Audio only.
Definition: avcodec.h:4010
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:753
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:913
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1379
static int vid_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bethsoftvid.c:220
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3942
int channels
Audio only.
Definition: avcodec.h:4006
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:329
int stream_index
Definition: avcodec.h:1447
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:366
This structure stores compressed data.
Definition: avcodec.h:1422
int width
video width
Definition: bethsoftvid.c:45
static uint8_t tmp[11]
Definition: aes_ctr.c:26