wc3movie.c
Go to the documentation of this file.
1 /*
2  * Wing Commander III Movie (.mve) File Demuxer
3  * Copyright (c) 2003 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  * Wing Commander III Movie file demuxer
25  * by Mike Melanson (melanson@pcisys.net)
26  * for more information on the WC3 .mve file format, visit:
27  * http://www.pcisys.net/~melanson/codecs/
28  */
29 
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/dict.h"
33 #include "avformat.h"
34 #include "internal.h"
35 
36 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
37 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
38 #define PC__TAG MKTAG('_', 'P', 'C', '_')
39 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
40 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
41 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
42 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
43 #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
44 #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
45 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
46 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
47 #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
48 #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
49 
50 /* video resolution unless otherwise specified */
51 #define WC3_DEFAULT_WIDTH 320
52 #define WC3_DEFAULT_HEIGHT 165
53 
54 /* always use the same PCM audio parameters */
55 #define WC3_SAMPLE_RATE 22050
56 #define WC3_AUDIO_CHANNELS 1
57 #define WC3_AUDIO_BITS 16
58 
59 /* nice, constant framerate */
60 #define WC3_FRAME_FPS 15
61 
62 #define PALETTE_SIZE (256 * 3)
63 
64 typedef struct Wc3DemuxContext {
65  int width;
66  int height;
67  int64_t pts;
70 
72 
74 
75 static int wc3_probe(AVProbeData *p)
76 {
77  if (p->buf_size < 12)
78  return 0;
79 
80  if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
81  (AV_RL32(&p->buf[8]) != MOVE_TAG))
82  return 0;
83 
84  return AVPROBE_SCORE_MAX;
85 }
86 
88 {
89  Wc3DemuxContext *wc3 = s->priv_data;
90  AVIOContext *pb = s->pb;
91  unsigned int fourcc_tag;
92  unsigned int size;
93  AVStream *st;
94  int ret = 0;
95  char *buffer;
96 
97  /* default context members */
98  wc3->width = WC3_DEFAULT_WIDTH;
100  wc3->pts = 0;
101  wc3->video_stream_index = wc3->audio_stream_index = 0;
102  av_init_packet(&wc3->vpkt);
103  wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
104 
105  /* skip the first 3 32-bit numbers */
106  avio_skip(pb, 12);
107 
108  /* traverse through the chunks and load the header information before
109  * the first BRCH tag */
110  fourcc_tag = avio_rl32(pb);
111  size = (avio_rb32(pb) + 1) & (~1);
112 
113  do {
114  switch (fourcc_tag) {
115 
116  case SOND_TAG:
117  case INDX_TAG:
118  /* SOND unknown, INDX unnecessary; ignore both */
119  avio_skip(pb, size);
120  break;
121 
122  case PC__TAG:
123  /* number of palettes, unneeded */
124  avio_skip(pb, 12);
125  break;
126 
127  case BNAM_TAG:
128  /* load up the name */
129  buffer = av_malloc(size+1);
130  if (!buffer)
131  return AVERROR(ENOMEM);
132  if ((ret = avio_read(pb, buffer, size)) != size)
133  return AVERROR(EIO);
134  buffer[size] = 0;
135  av_dict_set(&s->metadata, "title", buffer,
137  break;
138 
139  case SIZE_TAG:
140  /* video resolution override */
141  wc3->width = avio_rl32(pb);
142  wc3->height = avio_rl32(pb);
143  break;
144 
145  case PALT_TAG:
146  /* one of several palettes */
147  avio_seek(pb, -8, SEEK_CUR);
148  av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
149  break;
150 
151  default:
152  av_log(s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
153  (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
154  (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
155  return AVERROR_INVALIDDATA;
156  }
157 
158  fourcc_tag = avio_rl32(pb);
159  /* chunk sizes are 16-bit aligned */
160  size = (avio_rb32(pb) + 1) & (~1);
161  if (url_feof(pb))
162  return AVERROR(EIO);
163 
164  } while (fourcc_tag != BRCH_TAG);
165 
166  /* initialize the decoder streams */
167  st = avformat_new_stream(s, NULL);
168  if (!st)
169  return AVERROR(ENOMEM);
171  wc3->video_stream_index = st->index;
174  st->codec->codec_tag = 0; /* no fourcc */
175  st->codec->width = wc3->width;
176  st->codec->height = wc3->height;
177 
178  st = avformat_new_stream(s, NULL);
179  if (!st)
180  return AVERROR(ENOMEM);
182  wc3->audio_stream_index = st->index;
185  st->codec->codec_tag = 1;
190  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
193 
194  return 0;
195 }
196 
198  AVPacket *pkt)
199 {
200  Wc3DemuxContext *wc3 = s->priv_data;
201  AVIOContext *pb = s->pb;
202  unsigned int fourcc_tag;
203  unsigned int size;
204  int packet_read = 0;
205  int ret = 0;
206  unsigned char text[1024];
207 
208  while (!packet_read) {
209 
210  fourcc_tag = avio_rl32(pb);
211  /* chunk sizes are 16-bit aligned */
212  size = (avio_rb32(pb) + 1) & (~1);
213  if (url_feof(pb))
214  return AVERROR(EIO);
215 
216  switch (fourcc_tag) {
217 
218  case BRCH_TAG:
219  /* no-op */
220  break;
221 
222  case SHOT_TAG:
223  /* load up new palette */
224  avio_seek(pb, -8, SEEK_CUR);
225  av_append_packet(pb, &wc3->vpkt, 8 + 4);
226  break;
227 
228  case VGA__TAG:
229  /* send out video chunk */
230  avio_seek(pb, -8, SEEK_CUR);
231  ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
232  // ignore error if we have some data
233  if (wc3->vpkt.size > 0)
234  ret = 0;
235  *pkt = wc3->vpkt;
236  wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
237  pkt->stream_index = wc3->video_stream_index;
238  pkt->pts = wc3->pts;
239  packet_read = 1;
240  break;
241 
242  case TEXT_TAG:
243  /* subtitle chunk */
244 #if 0
245  avio_skip(pb, size);
246 #else
247  if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
248  ret = AVERROR(EIO);
249  else {
250  int i = 0;
251  av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
252  av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
253  i += text[i] + 1;
254  av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
255  i += text[i] + 1;
256  av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
257  }
258 #endif
259  break;
260 
261  case AUDI_TAG:
262  /* send out audio chunk */
263  ret= av_get_packet(pb, pkt, size);
264  pkt->stream_index = wc3->audio_stream_index;
265  pkt->pts = wc3->pts;
266 
267  /* time to advance pts */
268  wc3->pts++;
269 
270  packet_read = 1;
271  break;
272 
273  default:
274  av_log (s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
275  (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
276  (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
277  ret = AVERROR_INVALIDDATA;
278  packet_read = 1;
279  break;
280  }
281  }
282 
283  return ret;
284 }
285 
287 {
288  Wc3DemuxContext *wc3 = s->priv_data;
289 
290  if (wc3->vpkt.size > 0)
291  av_free_packet(&wc3->vpkt);
292 
293  return 0;
294 }
295 
297  .name = "wc3movie",
298  .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
299  .priv_data_size = sizeof(Wc3DemuxContext),
304 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define WC3_AUDIO_BITS
Definition: wc3movie.c:57
#define BRCH_TAG
Definition: wc3movie.c:44
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
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.
struct Wc3DemuxContext Wc3DemuxContext
int index
stream index in AVFormatContext
Definition: avformat.h:644
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
#define SIZE_TAG
Definition: wc3movie.c:41
#define WC3_DEFAULT_HEIGHT
Definition: wc3movie.c:52
#define INDX_TAG
Definition: wc3movie.c:43
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
text(-8, 1,'a)')
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
int video_stream_index
Definition: wc3movie.c:68
Format I/O context.
Definition: avformat.h:944
Public dictionary API.
int64_t pts
Definition: wc3movie.c:67
#define SHOT_TAG
Definition: wc3movie.c:45
#define TEXT_TAG
Definition: wc3movie.c:47
uint8_t
#define SOND_TAG
Definition: wc3movie.c:39
static int wc3_read_close(AVFormatContext *s)
Definition: wc3movie.c:286
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
static AVPacket pkt
Definition: demuxing.c:56
AVInputFormat ff_wc3_demuxer
Definition: wc3movie.c:296
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
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.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
#define BNAM_TAG
Definition: wc3movie.c:40
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
#define PALT_TAG
Definition: wc3movie.c:42
AVDictionary * metadata
Definition: avformat.h:1092
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AUDI_TAG
Definition: wc3movie.c:48
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
uint64_t channel_layout
Audio channel layout.
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
#define WC3_DEFAULT_WIDTH
Definition: wc3movie.c:51
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
int bit_rate
the average bitrate
audio channel layout utility functions
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and chilren.
Definition: dict.h:72
static int read_probe(AVProbeData *pd)
ret
Definition: avfilter.c:821
int width
picture width / height.
#define AV_RL32
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
int audio_stream_index
Definition: wc3movie.c:69
#define PALETTE_SIZE
Definition: wc3movie.c:62
#define PC__TAG
Definition: wc3movie.c:38
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
static int wc3_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wc3movie.c:197
#define WC3_FRAME_FPS
Definition: wc3movie.c:60
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:62
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
#define FORM_TAG
Definition: wc3movie.c:36
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define WC3_AUDIO_CHANNELS
Definition: wc3movie.c:56
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
Main libavformat public API header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
#define WC3_SAMPLE_RATE
Definition: wc3movie.c:55
static int wc3_read_header(AVFormatContext *s)
Definition: wc3movie.c:87
static int wc3_probe(AVProbeData *p)
Definition: wc3movie.c:75
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:56
AVPacket vpkt
Definition: wc3movie.c:71
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
#define VGA__TAG
Definition: wc3movie.c:46
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define MOVE_TAG
Definition: wc3movie.c:37