FFmpeg
Loading...
Searching...
No Matches
sierravmd.c
Go to the documentation of this file.
1/*
2 * Sierra VMD Format Demuxer
3 * Copyright (c) 2004 The FFmpeg project
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 * Sierra VMD file demuxer
25 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
26 * for more information on the Sierra VMD file format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
28 */
29
32#include "libavutil/mem.h"
33#include "avformat.h"
34#include "avio_internal.h"
35#include "demux.h"
36#include "internal.h"
37#include "avio_internal.h"
38
39#define VMD_HEADER_SIZE 0x0330
40#define BYTES_PER_FRAME_RECORD 16
41
49
66
67static int vmd_probe(const AVProbeData *p)
68{
69 int w, h, sample_rate;
70 if (p->buf_size < 806)
71 return 0;
72 /* check if the first 2 bytes of the file contain the appropriate size
73 * of a VMD header chunk */
74 if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
75 return 0;
76 w = AV_RL16(&p->buf[12]);
77 h = AV_RL16(&p->buf[14]);
78 sample_rate = AV_RL16(&p->buf[804]);
79 if ((!w || w > 2048 || !h || h > 2048) &&
80 sample_rate != 22050)
81 return 0;
82 /* the header of a file with a video stream carries the 6bit VGA
83 * palette that vmdvideo_decode_init() loads from offset 28 */
84 if (w && h)
85 for (int i = 28; i < 28 + 3 * 256; i++)
86 if (p->buf[i] > 0x3F)
87 return 0;
88
89 /* only return half certainty since this check is a bit sketchy */
91}
92
94{
95 VmdDemuxContext *vmd = s->priv_data;
96 AVIOContext *pb = s->pb;
97 AVStream *st = NULL, *vst = NULL;
98 unsigned int toc_offset;
99 unsigned char *raw_frame_table;
100 int raw_frame_table_size;
101 int64_t current_offset;
102 int i, j, ret;
103 int width, height;
104 unsigned int total_frames;
105 int64_t current_audio_pts = 0;
106 unsigned char chunk[BYTES_PER_FRAME_RECORD];
107 int num, den;
108 int sound_buffers;
109
110 /* fetch the main header, including the 2 header length bytes */
111 avio_seek(pb, 0, SEEK_SET);
112 if ((ret = ffio_read_size(pb, vmd->vmd_header, VMD_HEADER_SIZE)) < 0)
113 return ret;
114
115 width = AV_RL16(&vmd->vmd_header[12]);
116 height = AV_RL16(&vmd->vmd_header[14]);
117 if (width && height) {
118 if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3') {
119 vmd->is_indeo3 = 1;
120 } else {
121 vmd->is_indeo3 = 0;
122 }
123 /* start up the decoders */
124 vst = avformat_new_stream(s, NULL);
125 if (!vst)
126 return AVERROR(ENOMEM);
127 avpriv_set_pts_info(vst, 33, 1, 10);
128 vmd->video_stream_index = vst->index;
129 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
130 vst->codecpar->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
131 vst->codecpar->codec_tag = 0; /* no fourcc */
132 vst->codecpar->width = width;
133 vst->codecpar->height = height;
134 if(vmd->is_indeo3 && vst->codecpar->width > 320){
135 vst->codecpar->width >>= 1;
136 vst->codecpar->height >>= 1;
137 }
138 if ((ret = ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE)) < 0)
139 return ret;
140 memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
141 }
142
143 /* if sample rate is 0, assume no audio */
144 vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
145 if (vmd->sample_rate) {
146 int channels;
148 if (!st)
149 return AVERROR(ENOMEM);
150 vmd->audio_stream_index = st->index;
153 st->codecpar->codec_tag = 0; /* no fourcc */
154 st->codecpar->sample_rate = vmd->sample_rate;
155 st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
156 if (st->codecpar->block_align & 0x8000) {
158 st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
159 } else {
161 }
162 if (vmd->vmd_header[811] & 0x80) {
163 channels = 2;
164 } else if (vmd->vmd_header[811] & 0x2) {
165 /* Shivers 2 stereo audio */
166 /* Frame length is for 1 channel */
167 channels = 2;
168 st->codecpar->block_align = st->codecpar->block_align << 1;
169 } else {
170 channels = 1;
171 }
175
176 /* calculate pts */
177 num = st->codecpar->block_align;
178 den = st->codecpar->sample_rate * channels;
179 av_reduce(&num, &den, num, den, (1UL<<31)-1);
180 if (vst)
181 avpriv_set_pts_info(vst, 33, num, den);
182 avpriv_set_pts_info(st, 33, num, den);
183 }
184 if (!s->nb_streams)
185 return AVERROR_INVALIDDATA;
186
187 toc_offset = AV_RL32(&vmd->vmd_header[812]);
188 vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
189 vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
190 avio_seek(pb, toc_offset, SEEK_SET);
191
192 raw_frame_table = NULL;
193 vmd->frame_table = NULL;
194 sound_buffers = AV_RL16(&vmd->vmd_header[808]);
195 raw_frame_table_size = vmd->frame_count * 6;
196 raw_frame_table = av_malloc(raw_frame_table_size);
197 vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame));
198 if (!raw_frame_table || !vmd->frame_table) {
199 ret = AVERROR(ENOMEM);
200 goto error;
201 }
202 if ((ret = ffio_read_size(pb, raw_frame_table, raw_frame_table_size)) < 0)
203 goto error;
204
205 total_frames = 0;
206 for (i = 0; i < vmd->frame_count; i++) {
207
208 current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
209
210 /* handle each entry in index block */
211 for (j = 0; j < vmd->frames_per_block; j++) {
212 int type;
213 uint32_t size;
214
215 if ((ret = avio_read(pb, chunk, BYTES_PER_FRAME_RECORD)) != BYTES_PER_FRAME_RECORD) {
216 av_log(s, AV_LOG_ERROR, "Failed to read frame record\n");
217 if (ret >= 0)
219 goto error;
220 }
221 type = chunk[0];
222 size = AV_RL32(&chunk[2]);
223 if (size > INT_MAX / 2) {
224 av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
226 goto error;
227 }
228 if(!size && type != 1)
229 continue;
230 switch(type) {
231 case 1: /* Audio Chunk */
232 if (!st) break;
233 /* first audio chunk contains several audio buffers */
234 vmd->frame_table[total_frames].frame_offset = current_offset;
235 vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
236 vmd->frame_table[total_frames].frame_size = size;
237 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
238 vmd->frame_table[total_frames].pts = current_audio_pts;
239 total_frames++;
240 if(!current_audio_pts)
241 current_audio_pts += sound_buffers - 1;
242 else
243 current_audio_pts++;
244 break;
245 case 2: /* Video Chunk */
246 if (!vst)
247 break;
248 vmd->frame_table[total_frames].frame_offset = current_offset;
249 vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
250 vmd->frame_table[total_frames].frame_size = size;
251 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
252 vmd->frame_table[total_frames].pts = i;
253 total_frames++;
254 break;
255 }
256 current_offset += size;
257 }
258 }
259
260
261 vmd->current_frame = 0;
262 vmd->frame_count = total_frames;
263
264 ret = 0;
265error:
266 av_freep(&raw_frame_table);
267 return ret;
268}
269
271 AVPacket *pkt)
272{
273 VmdDemuxContext *vmd = s->priv_data;
274 AVIOContext *pb = s->pb;
275 int ret = 0;
277
278 if (vmd->current_frame >= vmd->frame_count)
279 return AVERROR_EOF;
280
281 frame = &vmd->frame_table[vmd->current_frame];
282 /* position the stream (will probably be there already) */
283 avio_seek(pb, frame->frame_offset, SEEK_SET);
284
285 if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
286 return AVERROR_INVALIDDATA;
287 ret = av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD);
288 if (ret < 0)
289 return ret;
290 pkt->pos= avio_tell(pb);
291 memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
292 if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
293 ret = ffio_read_size(pb, pkt->data, frame->frame_size);
294 else
295 ret = ffio_read_size(pb, pkt->data + BYTES_PER_FRAME_RECORD,
296 frame->frame_size);
297
298 pkt->stream_index = frame->stream_index;
299 pkt->pts = frame->pts;
300 av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
301 (frame->frame_record[0] == 0x02) ? "video" : "audio",
302 frame->frame_size + BYTES_PER_FRAME_RECORD,
303 pkt->pts);
304
305 vmd->current_frame++;
306
307 return ret;
308}
309
311{
312 VmdDemuxContext *vmd = s->priv_data;
313
314 av_freep(&vmd->frame_table);
315
316 return 0;
317}
318
320 .p.name = "vmd",
321 .p.long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
322 .priv_data_size = sizeof(VmdDemuxContext),
323 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
328};
const FFInputFormat ff_vmd_demuxer
Definition sierravmd.c:319
channels
Definition aptx.h:31
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
int ffio_limit(AVIOContext *s, int size)
Definition aviobuf.c:1064
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static AVFrame * frame
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_VMDAUDIO
Definition codec_id.h:464
@ AV_CODEC_ID_INDEO3
Definition codec_id.h:78
@ AV_CODEC_ID_VMDVIDEO
Definition codec_id.h:102
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
cl_device_type type
#define AV_RL32(p)
#define AV_RL16(p)
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
uint8_t w
Definition llvidencdsp.c:39
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
static int vmd_probe(const AVProbeData *p)
Definition sierravmd.c:67
static int vmd_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition sierravmd.c:270
#define VMD_HEADER_SIZE
Definition sierravmd.c:39
#define BYTES_PER_FRAME_RECORD
Definition sierravmd.c:40
static int vmd_read_close(AVFormatContext *s)
Definition sierravmd.c:310
static int vmd_read_header(AVFormatContext *s)
Definition sierravmd.c:93
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int index
stream index in AVFormatContext
Definition avformat.h:772
unsigned int frame_count
Definition sierravmd.c:54
int video_stream_index
Definition sierravmd.c:51
int audio_stream_index
Definition sierravmd.c:52
int64_t audio_sample_counter
Definition sierravmd.c:61
unsigned int current_frame
Definition sierravmd.c:57
unsigned int frames_per_block
Definition sierravmd.c:55
unsigned char vmd_header[VMD_HEADER_SIZE]
Definition sierravmd.c:64
vmd_frame * frame_table
Definition sierravmd.c:56
int64_t pts
Definition sierravmd.c:46
int64_t frame_offset
Definition sierravmd.c:45
unsigned int frame_size
Definition sierravmd.c:44
unsigned char frame_record[BYTES_PER_FRAME_RECORD]
Definition sierravmd.c:47
int stream_index
Definition sierravmd.c:43
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int size
#define VMD_HEADER_SIZE
Definition vmdvideo.c:47