2 * Bethsoft VID format Demuxer
3 * Copyright (c) 2007 Nicholas Tung
5 * This file is part of Libav.
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.
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.
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
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 typedef struct BVID_DemuxContext
40 /** delay value between frames, added to individual frame delay.
41 * custom units, which will be added to other custom units (~=16ms according
42 * to free, unofficial documentation) */
43 int bethsoft_global_delay;
45 /** video presentation time stamp.
46 * delay = 16 milliseconds * (global_delay + per_frame_delay) */
54 static int vid_probe(AVProbeData *p)
56 // little endian VID tag, file starts with "VID\0"
57 if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
60 return AVPROBE_SCORE_MAX;
63 static int vid_read_header(AVFormatContext *s)
65 BVID_DemuxContext *vid = s->priv_data;
66 AVIOContext *pb = s->pb;
69 /* load main header. Contents:
71 * int16s: always_512, nframes, width, height, delay, always_14
74 vid->nframes = avio_rl16(pb);
76 stream = avformat_new_stream(s, NULL);
78 return AVERROR(ENOMEM);
79 avpriv_set_pts_info(stream, 32, 1, 60); // 16 ms increments, i.e. 60 fps
80 stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
81 stream->codec->codec_id = CODEC_ID_BETHSOFTVID;
82 stream->codec->width = avio_rl16(pb);
83 stream->codec->height = avio_rl16(pb);
84 stream->codec->pix_fmt = PIX_FMT_PAL8;
85 vid->bethsoft_global_delay = avio_rl16(pb);
88 // done with video codec, set up audio codec
89 stream = avformat_new_stream(s, NULL);
91 return AVERROR(ENOMEM);
92 stream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
93 stream->codec->codec_id = CODEC_ID_PCM_U8;
94 stream->codec->channels = 1;
95 stream->codec->sample_rate = 11025;
96 stream->codec->bits_per_coded_sample = 8;
97 stream->codec->bit_rate = stream->codec->channels * stream->codec->sample_rate * stream->codec->bits_per_coded_sample;
102 #define BUFFER_PADDING_SIZE 1000
103 static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
104 uint8_t block_type, AVFormatContext *s, int npixels)
106 uint8_t * vidbuf_start = NULL;
107 int vidbuf_nbytes = 0;
109 int bytes_copied = 0;
111 unsigned int vidbuf_capacity;
113 vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
115 return AVERROR(ENOMEM);
117 // save the file position for the packet, include block type
118 position = avio_tell(pb) - 1;
120 vidbuf_start[vidbuf_nbytes++] = block_type;
122 // get the video delay (next int16), and set the presentation time
123 vid->video_pts += vid->bethsoft_global_delay + avio_rl16(pb);
125 // set the y offset if it exists (decoder header data should be in data section)
126 if(block_type == VIDEO_YOFF_P_FRAME){
127 if(avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2)
133 vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
135 return AVERROR(ENOMEM);
138 vidbuf_start[vidbuf_nbytes++] = code;
140 if(code >= 0x80){ // rle sequence
141 if(block_type == VIDEO_I_FRAME)
142 vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
143 } else if(code){ // plain sequence
144 if(avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code)
146 vidbuf_nbytes += code;
148 bytes_copied += code & 0x7F;
149 if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
150 // may contain a 0 byte even if read all pixels
152 avio_seek(pb, -1, SEEK_CUR);
155 if(bytes_copied > npixels)
159 // copy data into packet
160 if(av_new_packet(pkt, vidbuf_nbytes) < 0)
162 memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
163 av_free(vidbuf_start);
166 pkt->stream_index = 0; // use the video decoder, which was initialized as the first stream
167 pkt->pts = vid->video_pts;
169 /* if there is a new palette available, add it to packet side data */
171 uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
173 memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
174 av_freep(&vid->palette);
177 vid->nframes--; // used to check if all the frames were read
178 return vidbuf_nbytes;
180 av_free(vidbuf_start);
184 static int vid_read_packet(AVFormatContext *s,
187 BVID_DemuxContext *vid = s->priv_data;
188 AVIOContext *pb = s->pb;
189 unsigned char block_type;
193 if(vid->is_finished || pb->eof_reached)
196 block_type = avio_r8(pb);
200 av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
201 av_freep(&vid->palette);
203 vid->palette = av_malloc(BVID_PALETTE_SIZE);
205 return AVERROR(ENOMEM);
206 if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
207 av_freep(&vid->palette);
210 return vid_read_packet(s, pkt);
212 case FIRST_AUDIO_BLOCK:
214 // soundblaster DAC used for sample rate, as on specification page (link above)
215 s->streams[1]->codec->sample_rate = 1000000 / (256 - avio_r8(pb));
216 s->streams[1]->codec->bit_rate = s->streams[1]->codec->channels * s->streams[1]->codec->sample_rate * s->streams[1]->codec->bits_per_coded_sample;
218 audio_length = avio_rl16(pb);
219 ret_value = av_get_packet(pb, pkt, audio_length);
220 pkt->stream_index = 1;
221 return ret_value != audio_length ? AVERROR(EIO) : ret_value;
224 case VIDEO_YOFF_P_FRAME:
226 return read_frame(vid, pb, pkt, block_type, s,
227 s->streams[0]->codec->width * s->streams[0]->codec->height);
230 if(vid->nframes != 0)
231 av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
232 vid->is_finished = 1;
235 av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
236 block_type, block_type, block_type); return -1;
240 static int vid_read_close(AVFormatContext *s)
242 BVID_DemuxContext *vid = s->priv_data;
243 av_freep(&vid->palette);
247 AVInputFormat ff_bethsoftvid_demuxer = {
248 .name = "bethsoftvid",
249 .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID format"),
250 .priv_data_size = sizeof(BVID_DemuxContext),
251 .read_probe = vid_probe,
252 .read_header = vid_read_header,
253 .read_packet = vid_read_packet,
254 .read_close = vid_read_close,