2 * Bethsoft VID format Demuxer
3 * Copyright (c) 2007 Nicholas Tung
5 * This file is part of FFmpeg.
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.
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.
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
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
30 #include "libavutil/intreadwrite.h"
33 #include "libavcodec/bethsoftvideo.h"
35 #define BVID_PALETTE_SIZE 3 * 256
37 #define DEFAULT_SAMPLE_RATE 11111
39 typedef struct BVID_DemuxContext
42 int sample_rate; /**< audio sample rate */
43 int width; /**< video width */
44 int height; /**< video height */
45 /** delay value between frames, added to individual frame delay.
46 * custom units, which will be added to other custom units (~=16ms according
47 * to free, unofficial documentation) */
48 int bethsoft_global_delay;
49 int video_index; /**< video stream index */
50 int audio_index; /**< audio stream index */
57 static int vid_probe(AVProbeData *p)
59 // little endian VID tag, file starts with "VID\0"
60 if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
63 return AVPROBE_SCORE_MAX;
66 static int vid_read_header(AVFormatContext *s)
68 BVID_DemuxContext *vid = s->priv_data;
69 AVIOContext *pb = s->pb;
71 /* load main header. Contents:
73 * int16s: always_512, nframes, width, height, delay, always_14
76 vid->nframes = avio_rl16(pb);
77 vid->width = avio_rl16(pb);
78 vid->height = avio_rl16(pb);
79 vid->bethsoft_global_delay = avio_rl16(pb);
82 // wait until the first packet to create each stream
83 vid->video_index = -1;
84 vid->audio_index = -1;
85 vid->sample_rate = DEFAULT_SAMPLE_RATE;
86 s->ctx_flags |= AVFMTCTX_NOHEADER;
91 #define BUFFER_PADDING_SIZE 1000
92 static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
93 uint8_t block_type, AVFormatContext *s)
95 uint8_t * vidbuf_start = NULL;
96 int vidbuf_nbytes = 0;
99 int position, duration, npixels;
100 unsigned int vidbuf_capacity;
104 if (vid->video_index < 0) {
105 st = avformat_new_stream(s, NULL);
107 return AVERROR(ENOMEM);
108 vid->video_index = st->index;
109 if (vid->audio_index < 0) {
110 av_log_ask_for_sample(s, "No audio packet before first video "
111 "packet. Using default video time base.\n");
113 avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
114 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
115 st->codec->codec_id = AV_CODEC_ID_BETHSOFTVID;
116 st->codec->width = vid->width;
117 st->codec->height = vid->height;
119 st = s->streams[vid->video_index];
120 npixels = st->codec->width * st->codec->height;
122 vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
124 return AVERROR(ENOMEM);
126 // save the file position for the packet, include block type
127 position = avio_tell(pb) - 1;
129 vidbuf_start[vidbuf_nbytes++] = block_type;
131 // get the current packet duration
132 duration = vid->bethsoft_global_delay + avio_rl16(pb);
134 // set the y offset if it exists (decoder header data should be in data section)
135 if(block_type == VIDEO_YOFF_P_FRAME){
136 if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
144 vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
146 return AVERROR(ENOMEM);
149 vidbuf_start[vidbuf_nbytes++] = code;
151 if(code >= 0x80){ // rle sequence
152 if(block_type == VIDEO_I_FRAME)
153 vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
154 } else if(code){ // plain sequence
155 if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
159 vidbuf_nbytes += code;
161 bytes_copied += code & 0x7F;
162 if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
163 // may contain a 0 byte even if read all pixels
165 avio_seek(pb, -1, SEEK_CUR);
168 if (bytes_copied > npixels) {
169 ret = AVERROR_INVALIDDATA;
174 // copy data into packet
175 if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
177 memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
178 av_free(vidbuf_start);
181 pkt->stream_index = vid->video_index;
182 pkt->duration = duration;
183 if (block_type == VIDEO_I_FRAME)
184 pkt->flags |= AV_PKT_FLAG_KEY;
186 /* if there is a new palette available, add it to packet side data */
188 uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
191 memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
192 av_freep(&vid->palette);
195 vid->nframes--; // used to check if all the frames were read
198 av_free(vidbuf_start);
202 static int vid_read_packet(AVFormatContext *s,
205 BVID_DemuxContext *vid = s->priv_data;
206 AVIOContext *pb = s->pb;
207 unsigned char block_type;
211 if(vid->is_finished || url_feof(pb))
214 block_type = avio_r8(pb);
218 av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
219 av_freep(&vid->palette);
221 vid->palette = av_malloc(BVID_PALETTE_SIZE);
223 return AVERROR(ENOMEM);
224 if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
225 av_freep(&vid->palette);
228 return vid_read_packet(s, pkt);
230 case FIRST_AUDIO_BLOCK:
232 // soundblaster DAC used for sample rate, as on specification page (link above)
233 vid->sample_rate = 1000000 / (256 - avio_r8(pb));
235 if (vid->audio_index < 0) {
236 AVStream *st = avformat_new_stream(s, NULL);
238 return AVERROR(ENOMEM);
239 vid->audio_index = st->index;
240 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
241 st->codec->codec_id = AV_CODEC_ID_PCM_U8;
242 st->codec->channels = 1;
243 st->codec->bits_per_coded_sample = 8;
244 st->codec->sample_rate = vid->sample_rate;
245 st->codec->bit_rate = 8 * st->codec->sample_rate;
247 avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
249 audio_length = avio_rl16(pb);
250 if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
253 av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
256 pkt->stream_index = vid->audio_index;
257 pkt->duration = audio_length;
258 pkt->flags |= AV_PKT_FLAG_KEY;
262 case VIDEO_YOFF_P_FRAME:
264 return read_frame(vid, pb, pkt, block_type, s);
267 if(vid->nframes != 0)
268 av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
269 vid->is_finished = 1;
272 av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
273 block_type, block_type, block_type);
274 return AVERROR_INVALIDDATA;
278 static int vid_read_close(AVFormatContext *s)
280 BVID_DemuxContext *vid = s->priv_data;
281 av_freep(&vid->palette);
285 AVInputFormat ff_bethsoftvid_demuxer = {
286 .name = "bethsoftvid",
287 .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
288 .priv_data_size = sizeof(BVID_DemuxContext),
289 .read_probe = vid_probe,
290 .read_header = vid_read_header,
291 .read_packet = vid_read_packet,
292 .read_close = vid_read_close,