FFmpeg
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 
30 #include "libavutil/avstring.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/dict.h"
34 #include "avformat.h"
35 #include "internal.h"
36 
37 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
38 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
39 #define PC__TAG MKTAG('_', 'P', 'C', '_')
40 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
41 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
42 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
43 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
44 #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
45 #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
46 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
47 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
48 #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
49 #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
50 
51 /* video resolution unless otherwise specified */
52 #define WC3_DEFAULT_WIDTH 320
53 #define WC3_DEFAULT_HEIGHT 165
54 
55 /* always use the same PCM audio parameters */
56 #define WC3_SAMPLE_RATE 22050
57 #define WC3_AUDIO_CHANNELS 1
58 #define WC3_AUDIO_BITS 16
59 
60 /* nice, constant framerate */
61 #define WC3_FRAME_FPS 15
62 
63 #define PALETTE_SIZE (256 * 3)
64 
65 typedef struct Wc3DemuxContext {
66  int width;
67  int height;
68  int64_t pts;
71 
73 
75 
77 {
78  Wc3DemuxContext *wc3 = s->priv_data;
79 
80  if (wc3->vpkt.size > 0)
81  av_packet_unref(&wc3->vpkt);
82 
83  return 0;
84 }
85 
86 static int wc3_probe(const AVProbeData *p)
87 {
88  if (p->buf_size < 12)
89  return 0;
90 
91  if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
92  (AV_RL32(&p->buf[8]) != MOVE_TAG))
93  return 0;
94 
95  return AVPROBE_SCORE_MAX;
96 }
97 
99 {
100  Wc3DemuxContext *wc3 = s->priv_data;
101  AVIOContext *pb = s->pb;
102  unsigned int fourcc_tag;
103  unsigned int size;
104  AVStream *st;
105  int ret = 0;
106  char *buffer;
107 
108  /* default context members */
109  wc3->width = WC3_DEFAULT_WIDTH;
110  wc3->height = WC3_DEFAULT_HEIGHT;
111  wc3->pts = 0;
112  wc3->video_stream_index = wc3->audio_stream_index = 0;
113  av_init_packet(&wc3->vpkt);
114  wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
115 
116  /* skip the first 3 32-bit numbers */
117  avio_skip(pb, 12);
118 
119  /* traverse through the chunks and load the header information before
120  * the first BRCH tag */
121  fourcc_tag = avio_rl32(pb);
122  size = (avio_rb32(pb) + 1) & (~1);
123 
124  do {
125  switch (fourcc_tag) {
126 
127  case SOND_TAG:
128  case INDX_TAG:
129  /* SOND unknown, INDX unnecessary; ignore both */
130  avio_skip(pb, size);
131  break;
132 
133  case PC__TAG:
134  /* number of palettes, unneeded */
135  avio_skip(pb, 12);
136  break;
137 
138  case BNAM_TAG:
139  /* load up the name */
140  buffer = av_malloc(size+1);
141  if (!buffer)
142  if (!buffer) {
143  ret = AVERROR(ENOMEM);
144  goto fail;
145  }
146  if ((ret = avio_read(pb, buffer, size)) != size) {
147  av_freep(&buffer);
148  ret = AVERROR(EIO);
149  goto fail;
150  }
151  buffer[size] = 0;
152  av_dict_set(&s->metadata, "title", buffer,
154  break;
155 
156  case SIZE_TAG:
157  /* video resolution override */
158  wc3->width = avio_rl32(pb);
159  wc3->height = avio_rl32(pb);
160  break;
161 
162  case PALT_TAG:
163  /* one of several palettes */
164  avio_seek(pb, -8, SEEK_CUR);
165  av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
166  break;
167 
168  default:
169  av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
170  av_fourcc2str(fourcc_tag));
171  ret = AVERROR_INVALIDDATA;
172  goto fail;
173  }
174 
175  fourcc_tag = avio_rl32(pb);
176  /* chunk sizes are 16-bit aligned */
177  size = (avio_rb32(pb) + 1) & (~1);
178  if (avio_feof(pb)) {
179  ret = AVERROR(EIO);
180  goto fail;
181  }
182 
183  } while (fourcc_tag != BRCH_TAG);
184 
185  /* initialize the decoder streams */
186  st = avformat_new_stream(s, NULL);
187  if (!st) {
188  ret = AVERROR(ENOMEM);
189  goto fail;
190  }
192  wc3->video_stream_index = st->index;
195  st->codecpar->codec_tag = 0; /* no fourcc */
196  st->codecpar->width = wc3->width;
197  st->codecpar->height = wc3->height;
198 
199  st = avformat_new_stream(s, NULL);
200  if (!st) {
201  ret = AVERROR(ENOMEM);
202  goto fail;
203  }
205  wc3->audio_stream_index = st->index;
208  st->codecpar->codec_tag = 1;
216 
217  return 0;
218 fail:
219  wc3_read_close(s);
220  return ret;
221 }
222 
224  AVPacket *pkt)
225 {
226  Wc3DemuxContext *wc3 = s->priv_data;
227  AVIOContext *pb = s->pb;
228  unsigned int fourcc_tag;
229  unsigned int size;
230  int packet_read = 0;
231  int ret = 0;
232  unsigned char text[1024];
233 
234  while (!packet_read) {
235 
236  fourcc_tag = avio_rl32(pb);
237  /* chunk sizes are 16-bit aligned */
238  size = (avio_rb32(pb) + 1) & (~1);
239  if (avio_feof(pb))
240  return AVERROR(EIO);
241 
242  switch (fourcc_tag) {
243 
244  case BRCH_TAG:
245  /* no-op */
246  break;
247 
248  case SHOT_TAG:
249  /* load up new palette */
250  avio_seek(pb, -8, SEEK_CUR);
251  av_append_packet(pb, &wc3->vpkt, 8 + 4);
252  break;
253 
254  case VGA__TAG:
255  /* send out video chunk */
256  avio_seek(pb, -8, SEEK_CUR);
257  ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
258  // ignore error if we have some data
259  if (wc3->vpkt.size > 0)
260  ret = 0;
261  *pkt = wc3->vpkt;
262  wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
263  pkt->stream_index = wc3->video_stream_index;
264  pkt->pts = wc3->pts;
265  packet_read = 1;
266  break;
267 
268  case TEXT_TAG:
269  /* subtitle chunk */
270  if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
271  ret = AVERROR(EIO);
272  else {
273  int i = 0;
274  av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
275  if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
276  return AVERROR_INVALIDDATA;
277  av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
278  i += text[i] + 1;
279  if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
280  return AVERROR_INVALIDDATA;
281  av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
282  i += text[i] + 1;
283  if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
284  return AVERROR_INVALIDDATA;
285  av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
286  }
287  break;
288 
289  case AUDI_TAG:
290  /* send out audio chunk */
291  ret= av_get_packet(pb, pkt, size);
292  pkt->stream_index = wc3->audio_stream_index;
293  pkt->pts = wc3->pts;
294 
295  /* time to advance pts */
296  wc3->pts++;
297 
298  packet_read = 1;
299  break;
300 
301  default:
302  av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
303  av_fourcc2str(fourcc_tag));
304  ret = AVERROR_INVALIDDATA;
305  packet_read = 1;
306  break;
307  }
308  }
309 
310  return ret;
311 }
312 
314  .name = "wc3movie",
315  .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
316  .priv_data_size = sizeof(Wc3DemuxContext),
321 };
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define WC3_AUDIO_BITS
Definition: wc3movie.c:58
#define BRCH_TAG
Definition: wc3movie.c:45
size_t static size_t av_strnlen(const char *s, size_t len)
Get the count of continuous non zero chars starting from the beginning.
Definition: avstring.h:141
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:4869
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int index
stream index in AVFormatContext
Definition: avformat.h:877
int size
Definition: packet.h:364
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
#define SIZE_TAG
Definition: wc3movie.c:42
#define WC3_DEFAULT_HEIGHT
Definition: wc3movie.c:53
#define INDX_TAG
Definition: wc3movie.c:44
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:341
static AVPacket pkt
int video_stream_index
Definition: wc3movie.c:69
Format I/O context.
Definition: avformat.h:1351
Public dictionary API.
int64_t pts
Definition: wc3movie.c:68
#define SHOT_TAG
Definition: wc3movie.c:46
#define TEXT_TAG
Definition: wc3movie.c:48
#define SOND_TAG
Definition: wc3movie.c:40
#define av_malloc(s)
int width
Video only.
Definition: codec_par.h:126
static int wc3_read_close(AVFormatContext *s)
Definition: wc3movie.c:76
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:790
AVInputFormat ff_wc3_demuxer
Definition: wc3movie.c:313
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4450
uint8_t * data
Definition: packet.h:363
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.
Definition: utils.c:305
ptrdiff_t size
Definition: opengl_enc.c:100
#define BNAM_TAG
Definition: wc3movie.c:41
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition: utils.c:315
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
#define PALT_TAG
Definition: wc3movie.c:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
#define AUDI_TAG
Definition: wc3movie.c:49
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
#define fail()
Definition: checkasm.h:123
#define WC3_DEFAULT_WIDTH
Definition: wc3movie.c:52
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
int block_align
Audio only.
Definition: codec_par.h:177
audio channel layout utility functions
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
#define s(width, name)
Definition: cbs_vp9.c:257
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
Stream structure.
Definition: avformat.h:876
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
int audio_stream_index
Definition: wc3movie.c:70
#define PALETTE_SIZE
Definition: wc3movie.c:63
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:606
#define PC__TAG
Definition: wc3movie.c:39
static int wc3_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wc3movie.c:223
#define WC3_FRAME_FPS
Definition: wc3movie.c:61
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:70
#define FORM_TAG
Definition: wc3movie.c:37
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
#define WC3_AUDIO_CHANNELS
Definition: wc3movie.c:57
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int sample_rate
Audio only.
Definition: codec_par.h:170
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
Main libavformat public API header.
#define WC3_SAMPLE_RATE
Definition: wc3movie.c:56
static int wc3_read_header(AVFormatContext *s)
Definition: wc3movie.c:98
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:35
AVPacket vpkt
Definition: wc3movie.c:72
void * priv_data
Format private data.
Definition: avformat.h:1379
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:650
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:368
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
#define VGA__TAG
Definition: wc3movie.c:47
static int wc3_probe(const AVProbeData *p)
Definition: wc3movie.c:86
int stream_index
Definition: packet.h:365
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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.Frame references ownership and permissions
#define AV_CH_LAYOUT_MONO
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: packet.h:340
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356
int i
Definition: input.c:407
GLuint buffer
Definition: opengl_enc.c:101
#define MOVE_TAG
Definition: wc3movie.c:38