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 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 
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "internal.h"
35 
36 #define BVID_PALETTE_SIZE 3 * 256
37 
38 #define DEFAULT_SAMPLE_RATE 11111
39 
40 typedef struct BVID_DemuxContext
41 {
42  int nframes;
44  int width;
45  int height;
53 
55 
57 
58 static int vid_probe(AVProbeData *p)
59 {
60  // little-endian VID tag, file starts with "VID\0"
61  if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
62  return 0;
63 
64  return AVPROBE_SCORE_MAX;
65 }
66 
68 {
69  BVID_DemuxContext *vid = s->priv_data;
70  AVIOContext *pb = s->pb;
71 
72  /* load main header. Contents:
73  * bytes: 'V' 'I' 'D'
74  * int16s: always_512, nframes, width, height, delay, always_14
75  */
76  avio_skip(pb, 5);
77  vid->nframes = avio_rl16(pb);
78  vid->width = avio_rl16(pb);
79  vid->height = avio_rl16(pb);
81  avio_rl16(pb);
82 
83  // wait until the first packet to create each stream
84  vid->video_index = -1;
85  vid->audio_index = -1;
88 
89  return 0;
90 }
91 
92 #define BUFFER_PADDING_SIZE 1000
93 static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
94  uint8_t block_type, AVFormatContext *s)
95 {
96  uint8_t * vidbuf_start = NULL;
97  int vidbuf_nbytes = 0;
98  int code;
99  int bytes_copied = 0;
100  int position, duration, npixels;
101  unsigned int vidbuf_capacity;
102  int ret = 0;
103  AVStream *st;
104 
105  if (vid->video_index < 0) {
106  st = avformat_new_stream(s, NULL);
107  if (!st)
108  return AVERROR(ENOMEM);
109  vid->video_index = st->index;
110  if (vid->audio_index < 0) {
111  av_log_ask_for_sample(s, "No audio packet before first video "
112  "packet. Using default video time base.\n");
113  }
114  avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
117  st->codec->width = vid->width;
118  st->codec->height = vid->height;
119  }
120  st = s->streams[vid->video_index];
121  npixels = st->codec->width * st->codec->height;
122 
123  vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
124  if(!vidbuf_start)
125  return AVERROR(ENOMEM);
126 
127  // save the file position for the packet, include block type
128  position = avio_tell(pb) - 1;
129 
130  vidbuf_start[vidbuf_nbytes++] = block_type;
131 
132  // get the current packet duration
133  duration = vid->bethsoft_global_delay + avio_rl16(pb);
134 
135  // set the y offset if it exists (decoder header data should be in data section)
136  if(block_type == VIDEO_YOFF_P_FRAME){
137  if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
138  ret = AVERROR(EIO);
139  goto fail;
140  }
141  vidbuf_nbytes += 2;
142  }
143 
144  do{
145  vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
146  if(!vidbuf_start)
147  return AVERROR(ENOMEM);
148 
149  code = avio_r8(pb);
150  vidbuf_start[vidbuf_nbytes++] = code;
151 
152  if(code >= 0x80){ // rle sequence
153  if(block_type == VIDEO_I_FRAME)
154  vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
155  } else if(code){ // plain sequence
156  if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
157  ret = AVERROR(EIO);
158  goto fail;
159  }
160  vidbuf_nbytes += code;
161  }
162  bytes_copied += code & 0x7F;
163  if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
164  // may contain a 0 byte even if read all pixels
165  if(avio_r8(pb))
166  avio_seek(pb, -1, SEEK_CUR);
167  break;
168  }
169  if (bytes_copied > npixels) {
170  ret = AVERROR_INVALIDDATA;
171  goto fail;
172  }
173  } while(code);
174 
175  // copy data into packet
176  if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
177  goto fail;
178  memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
179  av_free(vidbuf_start);
180 
181  pkt->pos = position;
182  pkt->stream_index = vid->video_index;
183  pkt->duration = duration;
184  if (block_type == VIDEO_I_FRAME)
185  pkt->flags |= AV_PKT_FLAG_KEY;
186 
187  /* if there is a new palette available, add it to packet side data */
188  if (vid->palette) {
191  memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
192  av_freep(&vid->palette);
193  }
194 
195  vid->nframes--; // used to check if all the frames were read
196  return 0;
197 fail:
198  av_free(vidbuf_start);
199  return ret;
200 }
201 
203  AVPacket *pkt)
204 {
205  BVID_DemuxContext *vid = s->priv_data;
206  AVIOContext *pb = s->pb;
207  unsigned char block_type;
208  int audio_length;
209  int ret_value;
210 
211  if(vid->is_finished || pb->eof_reached)
212  return AVERROR(EIO);
213 
214  block_type = avio_r8(pb);
215  switch(block_type){
216  case PALETTE_BLOCK:
217  if (vid->palette) {
218  av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
219  av_freep(&vid->palette);
220  }
222  if (!vid->palette)
223  return AVERROR(ENOMEM);
225  av_freep(&vid->palette);
226  return AVERROR(EIO);
227  }
228  return vid_read_packet(s, pkt);
229 
230  case FIRST_AUDIO_BLOCK:
231  avio_rl16(pb);
232  // soundblaster DAC used for sample rate, as on specification page (link above)
233  vid->sample_rate = 1000000 / (256 - avio_r8(pb));
234  case AUDIO_BLOCK:
235  if (vid->audio_index < 0) {
237  if (!st)
238  return AVERROR(ENOMEM);
239  vid->audio_index = st->index;
242  st->codec->channels = 1;
244  st->codec->bits_per_coded_sample = 8;
245  st->codec->sample_rate = vid->sample_rate;
246  st->codec->bit_rate = 8 * st->codec->sample_rate;
247  st->start_time = 0;
248  avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
249  }
250  audio_length = avio_rl16(pb);
251  if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
252  if (ret_value < 0)
253  return ret_value;
254  av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
255  return AVERROR(EIO);
256  }
257  pkt->stream_index = vid->audio_index;
258  pkt->duration = audio_length;
259  pkt->flags |= AV_PKT_FLAG_KEY;
260  return 0;
261 
262  case VIDEO_P_FRAME:
263  case VIDEO_YOFF_P_FRAME:
264  case VIDEO_I_FRAME:
265  return read_frame(vid, pb, pkt, block_type, s);
266 
267  case EOF_BLOCK:
268  if(vid->nframes != 0)
269  av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
270  vid->is_finished = 1;
271  return AVERROR(EIO);
272  default:
273  av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
274  block_type, block_type, block_type);
275  return AVERROR_INVALIDDATA;
276  }
277 }
278 
280 {
281  BVID_DemuxContext *vid = s->priv_data;
282  av_freep(&vid->palette);
283  return 0;
284 }
285 
287  .name = "bethsoftvid",
288  .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
289  .priv_data_size = sizeof(BVID_DemuxContext),
294 };