FFmpeg
Loading...
Searching...
No Matches
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
32#include "libavutil/imgutils.h"
34#include "libavutil/mem.h"
35#include "avformat.h"
36#include "avio_internal.h"
37#include "demux.h"
38#include "internal.h"
40
41#define BVID_PALETTE_SIZE 3 * 256
42
43#define DEFAULT_SAMPLE_RATE 11111
44
45typedef struct BVID_DemuxContext
46{
48 int sample_rate; /**< audio sample rate */
49 int width; /**< video width */
50 int height; /**< video height */
51 /** delay value between frames, added to individual frame delay.
52 * custom units, which will be added to other custom units (~=16ms according
53 * to free, unofficial documentation) */
55 int video_index; /**< video stream index */
56 int audio_index; /**< audio stream index */
59
61
63
64static int vid_probe(const AVProbeData *p)
65{
66 // little-endian VID tag, file starts with "VID\0"
67 if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
68 return 0;
69
70 if (p->buf[4] != 2)
71 return AVPROBE_SCORE_MAX / 4;
72
73 return AVPROBE_SCORE_MAX;
74}
75
77{
78 BVID_DemuxContext *vid = s->priv_data;
79 AVIOContext *pb = s->pb;
80 int ret;
81
82 /* load main header. Contents:
83 * bytes: 'V' 'I' 'D'
84 * int16s: always_512, nframes, width, height, delay, always_14
85 */
86 avio_skip(pb, 5);
87 vid->nframes = avio_rl16(pb);
88 vid->width = avio_rl16(pb);
89 vid->height = avio_rl16(pb);
91 avio_rl16(pb);
92
93 ret = av_image_check_size(vid->width, vid->height, 0, s);
94 if (ret < 0)
95 return ret;
96
97 // wait until the first packet to create each stream
98 vid->video_index = -1;
99 vid->audio_index = -1;
101 s->ctx_flags |= AVFMTCTX_NOHEADER;
102
103 return 0;
104}
105
106#define BUFFER_PADDING_SIZE 1000
108 uint8_t block_type, AVFormatContext *s)
109{
110 uint8_t * vidbuf_start = NULL;
111 int vidbuf_nbytes = 0;
112 int code;
113 int bytes_copied = 0;
114 int position, duration, npixels;
115 unsigned int vidbuf_capacity;
116 int ret = 0;
117 AVStream *st;
118
119 if (vid->video_index < 0) {
121 if (!st)
122 return AVERROR(ENOMEM);
123 vid->video_index = st->index;
124 if (vid->audio_index < 0) {
125 avpriv_request_sample(s, "Using default video time base since "
126 "having no audio packet before the first "
127 "video packet");
128 }
129 avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
132 st->codecpar->width = vid->width;
133 st->codecpar->height = vid->height;
134 }
135 st = s->streams[vid->video_index];
136 npixels = st->codecpar->width * st->codecpar->height;
137
138 vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
139 if(!vidbuf_start)
140 return AVERROR(ENOMEM);
141
142 // save the file position for the packet, include block type
143 position = avio_tell(pb) - 1;
144
145 vidbuf_start[vidbuf_nbytes++] = block_type;
146
147 // get the current packet duration
149
150 // set the y offset if it exists (decoder header data should be in data section)
151 if(block_type == VIDEO_YOFF_P_FRAME){
152 ret = ffio_read_size(pb, &vidbuf_start[vidbuf_nbytes], 2);
153 if (ret < 0)
154 goto fail;
155 vidbuf_nbytes += 2;
156 }
157
158 do{
159 uint8_t *tmp = av_fast_realloc(vidbuf_start, &vidbuf_capacity,
160 vidbuf_nbytes + BUFFER_PADDING_SIZE);
161 if (!tmp) {
162 ret = AVERROR(ENOMEM);
163 goto fail;
164 }
165 vidbuf_start = tmp;
166
167 code = avio_r8(pb);
168 vidbuf_start[vidbuf_nbytes++] = code;
169
170 if(code >= 0x80){ // rle sequence
171 if(block_type == VIDEO_I_FRAME)
172 vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
173 } else if(code){ // plain sequence
174 ret = ffio_read_size(pb, &vidbuf_start[vidbuf_nbytes], code);
175 if (ret < 0)
176 goto fail;
177 vidbuf_nbytes += code;
178 }
179 bytes_copied += code & 0x7F;
180 if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
181 // may contain a 0 byte even if read all pixels
182 if(avio_r8(pb))
183 avio_seek(pb, -1, SEEK_CUR);
184 break;
185 }
186 if (bytes_copied > npixels) {
188 goto fail;
189 }
190 } while(code);
191
192 // copy data into packet
193 if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
194 goto fail;
195 memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
196
197 pkt->pos = position;
198 pkt->stream_index = vid->video_index;
199 pkt->duration = duration;
200 if (block_type == VIDEO_I_FRAME)
201 pkt->flags |= AV_PKT_FLAG_KEY;
202
203 /* if there is a new palette available, add it to packet side data */
204 if (vid->has_palette) {
207 if (!pdata) {
208 ret = AVERROR(ENOMEM);
209 av_log(s, AV_LOG_ERROR, "Failed to allocate palette side data\n");
210 goto fail;
211 }
212 memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
213 vid->has_palette = 0;
214 }
215
216 vid->nframes--; // used to check if all the frames were read
217fail:
218 av_free(vidbuf_start);
219 return ret;
220}
221
223 AVPacket *pkt)
224{
225 BVID_DemuxContext *vid = s->priv_data;
226 AVIOContext *pb = s->pb;
227 unsigned char block_type;
228 int audio_length;
229 int ret_value;
230
231 if(vid->is_finished || avio_feof(pb))
232 return AVERROR_EOF;
233
234 block_type = avio_r8(pb);
235 switch(block_type){
236 case PALETTE_BLOCK:
237 if (vid->has_palette) {
238 av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
239 vid->has_palette = 0;
240 }
241 ret_value = ffio_read_size(pb, vid->palette, BVID_PALETTE_SIZE);
242 if (ret_value < 0)
243 return ret_value;
244 vid->has_palette = 1;
245 return vid_read_packet(s, pkt);
246
248 avio_rl16(pb);
249 // soundblaster DAC used for sample rate, as on specification page (link above)
250 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;
262 st->codecpar->sample_rate = vid->sample_rate;
263 st->codecpar->bit_rate = 8 * st->codecpar->sample_rate;
264 st->start_time = 0;
265 avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
266 }
267 audio_length = avio_rl16(pb);
268 if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
269 if (ret_value < 0)
270 return ret_value;
271 av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
272 return AVERROR_INVALIDDATA;
273 }
274 pkt->stream_index = vid->audio_index;
275 pkt->duration = audio_length;
276 pkt->flags |= AV_PKT_FLAG_KEY;
277 return 0;
278
279 case VIDEO_P_FRAME:
281 case VIDEO_I_FRAME:
282 return read_frame(vid, pb, pkt, block_type, s);
283
284 case EOF_BLOCK:
285 if(vid->nframes != 0)
286 av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
287 vid->is_finished = 1;
288 return AVERROR_INVALIDDATA;
289 default:
290 av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
291 block_type, block_type, block_type);
292 return AVERROR_INVALIDDATA;
293 }
294}
295
297 .p.name = "bethsoftvid",
298 .p.long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
299 .priv_data_size = sizeof(BVID_DemuxContext),
303};
const FFInputFormat ff_bethsoftvid_demuxer
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
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
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt, uint8_t block_type, AVFormatContext *s)
static int vid_probe(const AVProbeData *p)
Definition bethsoftvid.c:64
static int vid_read_packet(AVFormatContext *s, AVPacket *pkt)
static int vid_read_header(AVFormatContext *s)
Definition bethsoftvid.c:76
#define DEFAULT_SAMPLE_RATE
Definition bethsoftvid.c:43
#define BVID_PALETTE_SIZE
Definition bethsoftvid.c:41
#define BUFFER_PADDING_SIZE
@ FIRST_AUDIO_BLOCK
@ VIDEO_I_FRAME
@ VIDEO_YOFF_P_FRAME
@ PALETTE_BLOCK
@ AUDIO_BLOCK
@ VIDEO_P_FRAME
@ EOF_BLOCK
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static int64_t duration
Definition ffplay.c:330
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define fail
Definition test.h:479
@ AV_CODEC_ID_BETHSOFTVID
Definition codec_id.h:153
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
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
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
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
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
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:495
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
misc image utilities
#define AV_RL32(p)
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
const uint8_t * code
Definition spdifenc.c:433
An AVChannelLayout holds information about the channel layout of audio data.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
int video_index
video stream index
Definition bethsoftvid.c:55
int height
video height
Definition bethsoftvid.c:50
int sample_rate
audio sample rate
Definition bethsoftvid.c:48
int audio_index
audio stream index
Definition bethsoftvid.c:56
int width
video width
Definition bethsoftvid.c:49
uint8_t palette[BVID_PALETTE_SIZE]
Definition bethsoftvid.c:58
int bethsoft_global_delay
delay value between frames, added to individual frame delay.
Definition bethsoftvid.c:54
#define av_free(p)
#define avpriv_request_sample(...)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52