]> git.sesse.net Git - ffmpeg/blob - libavformat/bethsoftvid.c
23c6d1dfdb3011c9e90d9857dc1167cd07118070
[ffmpeg] / libavformat / bethsoftvid.c
1 /*
2  * Bethsoft VID format Demuxer
3  * Copyright (c) 2007 Nicholas Tung
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
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
30 #include "libavutil/intreadwrite.h"
31 #include "avformat.h"
32 #include "internal.h"
33 #include "libavcodec/bethsoftvideo.h"
34
35 #define BVID_PALETTE_SIZE 3 * 256
36
37 typedef struct BVID_DemuxContext
38 {
39     int nframes;
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;
44
45     /** video presentation time stamp.
46      * delay = 16 milliseconds * (global_delay + per_frame_delay) */
47     int video_pts;
48     uint8_t *palette;
49
50     int is_finished;
51
52 } BVID_DemuxContext;
53
54 static int vid_probe(AVProbeData *p)
55 {
56     // little endian VID tag, file starts with "VID\0"
57     if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
58         return 0;
59
60     return AVPROBE_SCORE_MAX;
61 }
62
63 static int vid_read_header(AVFormatContext *s)
64 {
65     BVID_DemuxContext *vid = s->priv_data;
66     AVIOContext *pb = s->pb;
67     AVStream *stream;
68
69     /* load main header. Contents:
70     *    bytes: 'V' 'I' 'D'
71     *    int16s: always_512, nframes, width, height, delay, always_14
72     */
73     avio_skip(pb, 5);
74     vid->nframes = avio_rl16(pb);
75
76     stream = avformat_new_stream(s, NULL);
77     if (!stream)
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);
86     avio_rl16(pb);
87
88     // done with video codec, set up audio codec
89     stream = avformat_new_stream(s, NULL);
90     if (!stream)
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;
98
99     return 0;
100 }
101
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)
105 {
106     uint8_t * vidbuf_start = NULL;
107     int vidbuf_nbytes = 0;
108     int code;
109     int bytes_copied = 0;
110     int position;
111     unsigned int vidbuf_capacity;
112
113     vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
114     if(!vidbuf_start)
115         return AVERROR(ENOMEM);
116
117     // save the file position for the packet, include block type
118     position = avio_tell(pb) - 1;
119
120     vidbuf_start[vidbuf_nbytes++] = block_type;
121
122     // get the video delay (next int16), and set the presentation time
123     vid->video_pts += vid->bethsoft_global_delay + avio_rl16(pb);
124
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)
128             goto fail;
129         vidbuf_nbytes += 2;
130     }
131
132     do{
133         vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
134         if(!vidbuf_start)
135             return AVERROR(ENOMEM);
136
137         code = avio_r8(pb);
138         vidbuf_start[vidbuf_nbytes++] = code;
139
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)
145                 goto fail;
146             vidbuf_nbytes += code;
147         }
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
151             if(avio_r8(pb))
152                 avio_seek(pb, -1, SEEK_CUR);
153             break;
154         }
155         if(bytes_copied > npixels)
156             goto fail;
157     } while(code);
158
159     // copy data into packet
160     if(av_new_packet(pkt, vidbuf_nbytes) < 0)
161         goto fail;
162     memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
163     av_free(vidbuf_start);
164
165     pkt->pos = position;
166     pkt->stream_index = 0;  // use the video decoder, which was initialized as the first stream
167     pkt->pts = vid->video_pts;
168
169     /* if there is a new palette available, add it to packet side data */
170     if (vid->palette) {
171         uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
172                                                  BVID_PALETTE_SIZE);
173         memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
174         av_freep(&vid->palette);
175     }
176
177     vid->nframes--;  // used to check if all the frames were read
178     return vidbuf_nbytes;
179 fail:
180     av_free(vidbuf_start);
181     return -1;
182 }
183
184 static int vid_read_packet(AVFormatContext *s,
185                            AVPacket *pkt)
186 {
187     BVID_DemuxContext *vid = s->priv_data;
188     AVIOContext *pb = s->pb;
189     unsigned char block_type;
190     int audio_length;
191     int ret_value;
192
193     if(vid->is_finished || pb->eof_reached)
194         return AVERROR(EIO);
195
196     block_type = avio_r8(pb);
197     switch(block_type){
198         case PALETTE_BLOCK:
199             if (vid->palette) {
200                 av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
201                 av_freep(&vid->palette);
202             }
203             vid->palette = av_malloc(BVID_PALETTE_SIZE);
204             if (!vid->palette)
205                 return AVERROR(ENOMEM);
206             if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
207                 av_freep(&vid->palette);
208                 return AVERROR(EIO);
209             }
210             return vid_read_packet(s, pkt);
211
212         case FIRST_AUDIO_BLOCK:
213             avio_rl16(pb);
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;
217         case AUDIO_BLOCK:
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;
222
223         case VIDEO_P_FRAME:
224         case VIDEO_YOFF_P_FRAME:
225         case VIDEO_I_FRAME:
226             return read_frame(vid, pb, pkt, block_type, s,
227                               s->streams[0]->codec->width * s->streams[0]->codec->height);
228
229         case EOF_BLOCK:
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;
233             return AVERROR(EIO);
234         default:
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;
237     }
238 }
239
240 static int vid_read_close(AVFormatContext *s)
241 {
242     BVID_DemuxContext *vid = s->priv_data;
243     av_freep(&vid->palette);
244     return 0;
245 }
246
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,
255 };