FFmpeg: libavformat/bethsoftvid.c Source File

FFmpeg
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 
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "internal.h"
34 #include "libavcodec/bethsoftvideo.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;
43   int sample_rate; /**< audio sample rate */
44   int width; /**< video width */
45   int height; /**< video height */
46  /** delay value between frames, added to individual frame delay.
47  * custom units, which will be added to other custom units (~=16ms according
48  * to free, unofficial documentation) */
49   int bethsoft_global_delay;
50   int video_index; /**< video stream index */
51   int audio_index; /**< audio stream index */
52   uint8_t *palette;
53 
54   int is_finished;
55 
56 } BVID_DemuxContext;
57 
58  static int vid_probe(AVProbeData *p)
59 {
60  // little-endian VID tag, file starts with "VID0円"
61  if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
62  return 0;
63 
64  if (p->buf[4] != 2)
65  return AVPROBE_SCORE_MAX / 4;
66 
67  return AVPROBE_SCORE_MAX;
68 }
69 
70  static int vid_read_header(AVFormatContext *s)
71 {
72  BVID_DemuxContext *vid = s->priv_data;
73  AVIOContext *pb = s->pb;
74 
75  /* load main header. Contents:
76  * bytes: 'V' 'I' 'D'
77  * int16s: always_512, nframes, width, height, delay, always_14
78  */
79  avio_skip(pb, 5);
80  vid->nframes = avio_rl16(pb);
81  vid->width = avio_rl16(pb);
82  vid->height = avio_rl16(pb);
83  vid->bethsoft_global_delay = avio_rl16(pb);
84  avio_rl16(pb);
85 
86  // wait until the first packet to create each stream
87  vid->video_index = -1;
88  vid->audio_index = -1;
89  vid->sample_rate = DEFAULT_SAMPLE_RATE;
90  s->ctx_flags |= AVFMTCTX_NOHEADER;
91 
92  return 0;
93 }
94 
95  #define BUFFER_PADDING_SIZE 1000
96  static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
97  uint8_t block_type, AVFormatContext *s)
98 {
99  uint8_t * vidbuf_start = NULL;
100  int vidbuf_nbytes = 0;
101  int code;
102  int bytes_copied = 0;
103  int position, duration, npixels;
104  unsigned int vidbuf_capacity;
105  int ret = 0;
106  AVStream *st;
107 
108  if (vid->video_index < 0) {
109  st = avformat_new_stream(s, NULL);
110  if (!st)
111  return AVERROR(ENOMEM);
112  vid->video_index = st->index;
113  if (vid->audio_index < 0) {
114  avpriv_request_sample(s, "Using default video time base since "
115  "having no audio packet before the first "
116  "video packet");
117  }
118  avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
119  st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
120  st->codec->codec_id = AV_CODEC_ID_BETHSOFTVID;
121  st->codec->width = vid->width;
122  st->codec->height = vid->height;
123  }
124  st = s->streams[vid->video_index];
125  npixels = st->codec->width * st->codec->height;
126 
127  vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
128  if(!vidbuf_start)
129  return AVERROR(ENOMEM);
130 
131  // save the file position for the packet, include block type
132  position = avio_tell(pb) - 1;
133 
134  vidbuf_start[vidbuf_nbytes++] = block_type;
135 
136  // get the current packet duration
137  duration = vid->bethsoft_global_delay + avio_rl16(pb);
138 
139  // set the y offset if it exists (decoder header data should be in data section)
140  if(block_type == VIDEO_YOFF_P_FRAME){
141  if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
142  ret = AVERROR(EIO);
143  goto fail;
144  }
145  vidbuf_nbytes += 2;
146  }
147 
148  do{
149  vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
150  if(!vidbuf_start)
151  return AVERROR(ENOMEM);
152 
153  code = avio_r8(pb);
154  vidbuf_start[vidbuf_nbytes++] = code;
155 
156  if(code >= 0x80){ // rle sequence
157  if(block_type == VIDEO_I_FRAME)
158  vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
159  } else if(code){ // plain sequence
160  if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
161  ret = AVERROR(EIO);
162  goto fail;
163  }
164  vidbuf_nbytes += code;
165  }
166  bytes_copied += code & 0x7F;
167  if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
168  // may contain a 0 byte even if read all pixels
169  if(avio_r8(pb))
170  avio_seek(pb, -1, SEEK_CUR);
171  break;
172  }
173  if (bytes_copied > npixels) {
174  ret = AVERROR_INVALIDDATA;
175  goto fail;
176  }
177  } while(code);
178 
179  // copy data into packet
180  if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
181  goto fail;
182  memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
183 
184  pkt->pos = position;
185  pkt->stream_index = vid->video_index;
186  pkt->duration = duration;
187  if (block_type == VIDEO_I_FRAME)
188  pkt->flags |= AV_PKT_FLAG_KEY;
189 
190  /* if there is a new palette available, add it to packet side data */
191  if (vid->palette) {
192  uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
193  BVID_PALETTE_SIZE);
194  if (!pdata) {
195  ret = AVERROR(ENOMEM);
196  av_log(s, AV_LOG_ERROR, "Failed to allocate palette side data\n");
197  goto fail;
198  }
199  memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
200 
201  av_freep(&vid->palette);
202  }
203 
204  vid->nframes--; // used to check if all the frames were read
205 fail:
206  av_free(vidbuf_start);
207  return ret;
208 }
209 
210  static int vid_read_packet(AVFormatContext *s,
211  AVPacket *pkt)
212 {
213  BVID_DemuxContext *vid = s->priv_data;
214  AVIOContext *pb = s->pb;
215  unsigned char block_type;
216  int audio_length;
217  int ret_value;
218 
219  if(vid->is_finished || avio_feof(pb))
220  return AVERROR_EOF;
221 
222  block_type = avio_r8(pb);
223  switch(block_type){
224  case PALETTE_BLOCK:
225  if (vid->palette) {
226  av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
227  av_freep(&vid->palette);
228  }
229  vid->palette = av_malloc(BVID_PALETTE_SIZE);
230  if (!vid->palette)
231  return AVERROR(ENOMEM);
232  if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
233  av_freep(&vid->palette);
234  return AVERROR(EIO);
235  }
236  return vid_read_packet(s, pkt);
237 
238  case FIRST_AUDIO_BLOCK:
239  avio_rl16(pb);
240  // soundblaster DAC used for sample rate, as on specification page (link above)
241  vid->sample_rate = 1000000 / (256 - avio_r8(pb));
242  case AUDIO_BLOCK:
243  if (vid->audio_index < 0) {
244  AVStream *st = avformat_new_stream(s, NULL);
245  if (!st)
246  return AVERROR(ENOMEM);
247  vid->audio_index = st->index;
248  st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
249  st->codec->codec_id = AV_CODEC_ID_PCM_U8;
250  st->codec->channels = 1;
251  st->codec->channel_layout = AV_CH_LAYOUT_MONO;
252  st->codec->bits_per_coded_sample = 8;
253  st->codec->sample_rate = vid->sample_rate;
254  st->codec->bit_rate = 8 * st->codec->sample_rate;
255  st->start_time = 0;
256  avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
257  }
258  audio_length = avio_rl16(pb);
259  if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
260  if (ret_value < 0)
261  return ret_value;
262  av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
263  return AVERROR(EIO);
264  }
265  pkt->stream_index = vid->audio_index;
266  pkt->duration = audio_length;
267  pkt->flags |= AV_PKT_FLAG_KEY;
268  return 0;
269 
270  case VIDEO_P_FRAME:
271  case VIDEO_YOFF_P_FRAME:
272  case VIDEO_I_FRAME:
273  return read_frame(vid, pb, pkt, block_type, s);
274 
275  case EOF_BLOCK:
276  if(vid->nframes != 0)
277  av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
278  vid->is_finished = 1;
279  return AVERROR(EIO);
280  default:
281  av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
282  block_type, block_type, block_type);
283  return AVERROR_INVALIDDATA;
284  }
285 }
286 
287  static int vid_read_close(AVFormatContext *s)
288 {
289  BVID_DemuxContext *vid = s->priv_data;
290  av_freep(&vid->palette);
291  return 0;
292 }
293 
294  AVInputFormat ff_bethsoftvid_demuxer = {
295  .name = "bethsoftvid",
296  .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
297  .priv_data_size = sizeof(BVID_DemuxContext),
298  .read_probe = vid_probe,
299  .read_header = vid_read_header,
300  .read_packet = vid_read_packet,
301  .read_close = vid_read_close,
302 };
NULL
#define NULL
Definition: coverity.c:32
s
const char * s
Definition: avisynth_c.h:631
AVIOContext
Bytestream IO Context.
Definition: avio.h:111
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
BVID_DemuxContext::bethsoft_global_delay
int bethsoft_global_delay
delay value between frames, added to individual frame delay.
Definition: bethsoftvid.c:49
BVID_DemuxContext::palette
uint8_t * palette
Definition: bethsoftvid.c:52
vid_read_header
static int vid_read_header(AVFormatContext *s)
Definition: bethsoftvid.c:70
vid_probe
static int vid_probe(AVProbeData *p)
Definition: bethsoftvid.c:58
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1597
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1487
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4149
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:878
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:208
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:282
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVFormatContext::ctx_flags
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1363
AVFormatContext
Format I/O context.
Definition: avformat.h:1314
BVID_DemuxContext::sample_rate
int sample_rate
audio sample rate
Definition: bethsoftvid.c:43
avpriv_request_sample
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
uint8_t
Definition: audio_convert.c:194
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1273
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1485
BVID_PALETTE_SIZE
#define BVID_PALETTE_SIZE
Definition: bethsoftvid.c:36
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3805
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
AVPacket::data
uint8_t * data
Definition: avcodec.h:1467
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
av_get_packet
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:252
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:442
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2917
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: mem.c:480
AVERROR
#define AVERROR(e)
Definition: error.h:43
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
BVID_DemuxContext::height
int height
video height
Definition: bethsoftvid.c:45
DEFAULT_SAMPLE_RATE
#define DEFAULT_SAMPLE_RATE
Definition: bethsoftvid.c:38
BVID_DemuxContext::video_index
int video_index
video stream index
Definition: bethsoftvid.c:50
fail
#define fail()
Definition: checkasm.h:80
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2338
BUFFER_PADDING_SIZE
#define BUFFER_PADDING_SIZE
Definition: bethsoftvid.c:95
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
AVStream::codec
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
ff_bethsoftvid_demuxer
AVInputFormat ff_bethsoftvid_demuxer
Definition: bethsoftvid.c:294
channel_layout.h
audio channel layout utility functions
read_probe
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:1711
duration
int64_t duration
Definition: movenc-test.c:63
AVCodecContext::height
int height
Definition: avcodec.h:1711
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:638
AVStream
Stream structure.
Definition: avformat.h:877
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
vid_read_close
static int vid_read_close(AVFormatContext *s)
Definition: bethsoftvid.c:287
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:1540
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:1549
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2287
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
BVID_DemuxContext::audio_index
int audio_index
audio stream index
Definition: bethsoftvid.c:51
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
read_frame
static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt, uint8_t block_type, AVFormatContext *s)
Definition: bethsoftvid.c:96
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:651
avformat.h
Main libavformat public API header.
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:929
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2288
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1342
vid_read_packet
static int vid_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bethsoftvid.c:210
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
avio_feof
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:306
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:299
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1469
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:342
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1444
BVID_DemuxContext::width
int width
video width
Definition: bethsoftvid.c:44

Generated on Mon Feb 15 2016 15:20:45 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /