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
83 /* only return half certainty since this check is a bit sketchy */
85}
86
88{
89 VmdDemuxContext *vmd = s->priv_data;
90 AVIOContext *pb = s->pb;
91 AVStream *st = NULL, *vst = NULL;
92 unsigned int toc_offset;
93 unsigned char *raw_frame_table;
94 int raw_frame_table_size;
95 int64_t current_offset;
96 int i, j, ret;
97 int width, height;
98 unsigned int total_frames;
99 int64_t current_audio_pts = 0;
100 unsigned char chunk[BYTES_PER_FRAME_RECORD];
101 int num, den;
102 int sound_buffers;
103
104 /* fetch the main header, including the 2 header length bytes */
105 avio_seek(pb, 0, SEEK_SET);
106 if ((ret = ffio_read_size(pb, vmd->vmd_header, VMD_HEADER_SIZE)) < 0)
107 return ret;
108
109 width = AV_RL16(&vmd->vmd_header[12]);
110 height = AV_RL16(&vmd->vmd_header[14]);
111 if (width && height) {
112 if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3') {
113 vmd->is_indeo3 = 1;
114 } else {
115 vmd->is_indeo3 = 0;
116 }
117 /* start up the decoders */
118 vst = avformat_new_stream(s, NULL);
119 if (!vst)
120 return AVERROR(ENOMEM);
121 avpriv_set_pts_info(vst, 33, 1, 10);
122 vmd->video_stream_index = vst->index;
123 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
124 vst->codecpar->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
125 vst->codecpar->codec_tag = 0; /* no fourcc */
126 vst->codecpar->width = width;
127 vst->codecpar->height = height;
128 if(vmd->is_indeo3 && vst->codecpar->width > 320){
129 vst->codecpar->width >>= 1;
130 vst->codecpar->height >>= 1;
131 }
132 if ((ret = ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE)) < 0)
133 return ret;
134 memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
135 }
136
137 /* if sample rate is 0, assume no audio */
138 vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
139 if (vmd->sample_rate) {
140 int channels;
142 if (!st)
143 return AVERROR(ENOMEM);
144 vmd->audio_stream_index = st->index;
147 st->codecpar->codec_tag = 0; /* no fourcc */
148 st->codecpar->sample_rate = vmd->sample_rate;
149 st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
150 if (st->codecpar->block_align & 0x8000) {
152 st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
153 } else {
155 }
156 if (vmd->vmd_header[811] & 0x80) {
157 channels = 2;
158 } else if (vmd->vmd_header[811] & 0x2) {
159 /* Shivers 2 stereo audio */
160 /* Frame length is for 1 channel */
161 channels = 2;
162 st->codecpar->block_align = st->codecpar->block_align << 1;
163 } else {
164 channels = 1;
165 }
169
170 /* calculate pts */
171 num = st->codecpar->block_align;
172 den = st->codecpar->sample_rate * channels;
173 av_reduce(&num, &den, num, den, (1UL<<31)-1);
174 if (vst)
175 avpriv_set_pts_info(vst, 33, num, den);
176 avpriv_set_pts_info(st, 33, num, den);
177 }
178 if (!s->nb_streams)
179 return AVERROR_INVALIDDATA;
180
181 toc_offset = AV_RL32(&vmd->vmd_header[812]);
182 vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
183 vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
184 avio_seek(pb, toc_offset, SEEK_SET);
185
186 raw_frame_table = NULL;
187 vmd->frame_table = NULL;
188 sound_buffers = AV_RL16(&vmd->vmd_header[808]);
189 raw_frame_table_size = vmd->frame_count * 6;
190 raw_frame_table = av_malloc(raw_frame_table_size);
191 vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame));
192 if (!raw_frame_table || !vmd->frame_table) {
193 ret = AVERROR(ENOMEM);
194 goto error;
195 }
196 if ((ret = ffio_read_size(pb, raw_frame_table, raw_frame_table_size)) < 0)
197 goto error;
198
199 total_frames = 0;
200 for (i = 0; i < vmd->frame_count; i++) {
201
202 current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
203
204 /* handle each entry in index block */
205 for (j = 0; j < vmd->frames_per_block; j++) {
206 int type;
207 uint32_t size;
208
209 if ((ret = avio_read(pb, chunk, BYTES_PER_FRAME_RECORD)) != BYTES_PER_FRAME_RECORD) {
210 av_log(s, AV_LOG_ERROR, "Failed to read frame record\n");
211 if (ret >= 0)
213 goto error;
214 }
215 type = chunk[0];
216 size = AV_RL32(&chunk[2]);
217 if (size > INT_MAX / 2) {
218 av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
220 goto error;
221 }
222 if(!size && type != 1)
223 continue;
224 switch(type) {
225 case 1: /* Audio Chunk */
226 if (!st) break;
227 /* first audio chunk contains several audio buffers */
228 vmd->frame_table[total_frames].frame_offset = current_offset;
229 vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
230 vmd->frame_table[total_frames].frame_size = size;
231 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
232 vmd->frame_table[total_frames].pts = current_audio_pts;
233 total_frames++;
234 if(!current_audio_pts)
235 current_audio_pts += sound_buffers - 1;
236 else
237 current_audio_pts++;
238 break;
239 case 2: /* Video Chunk */
240 if (!vst)
241 break;
242 vmd->frame_table[total_frames].frame_offset = current_offset;
243 vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
244 vmd->frame_table[total_frames].frame_size = size;
245 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
246 vmd->frame_table[total_frames].pts = i;
247 total_frames++;
248 break;
249 }
250 current_offset += size;
251 }
252 }
253
254
255 vmd->current_frame = 0;
256 vmd->frame_count = total_frames;
257
258 ret = 0;
259error:
260 av_freep(&raw_frame_table);
261 return ret;
262}
263
265 AVPacket *pkt)
266{
267 VmdDemuxContext *vmd = s->priv_data;
268 AVIOContext *pb = s->pb;
269 int ret = 0;
271
272 if (vmd->current_frame >= vmd->frame_count)
273 return AVERROR_EOF;
274
275 frame = &vmd->frame_table[vmd->current_frame];
276 /* position the stream (will probably be there already) */
277 avio_seek(pb, frame->frame_offset, SEEK_SET);
278
279 if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
280 return AVERROR_INVALIDDATA;
281 ret = av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD);
282 if (ret < 0)
283 return ret;
284 pkt->pos= avio_tell(pb);
285 memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
286 if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
287 ret = ffio_read_size(pb, pkt->data, frame->frame_size);
288 else
289 ret = ffio_read_size(pb, pkt->data + BYTES_PER_FRAME_RECORD,
290 frame->frame_size);
291
292 pkt->stream_index = frame->stream_index;
293 pkt->pts = frame->pts;
294 av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
295 (frame->frame_record[0] == 0x02) ? "video" : "audio",
296 frame->frame_size + BYTES_PER_FRAME_RECORD,
297 pkt->pts);
298
299 vmd->current_frame++;
300
301 return ret;
302}
303
305{
306 VmdDemuxContext *vmd = s->priv_data;
307
308 av_freep(&vmd->frame_table);
309
310 return 0;
311}
312
314 .p.name = "vmd",
315 .p.long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
316 .priv_data_size = sizeof(VmdDemuxContext),
317 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
322};
const FFInputFormat ff_vmd_demuxer
Definition sierravmd.c:313
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:264
#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:304
static int vmd_read_header(AVFormatContext *s)
Definition sierravmd.c:87
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