FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
76 static int wc3_probe(AVProbeData *p)
77 {
78  if (p->buf_size < 12)
79  return 0;
80 
81  if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
82  (AV_RL32(&p->buf[8]) != MOVE_TAG))
83  return 0;
84 
85  return AVPROBE_SCORE_MAX;
86 }
87 
89 {
90  Wc3DemuxContext *wc3 = s->priv_data;
91  AVIOContext *pb = s->pb;
92  unsigned int fourcc_tag;
93  unsigned int size;
94  AVStream *st;
95  int ret = 0;
96  char *buffer;
97 
98  /* default context members */
99  wc3->width = WC3_DEFAULT_WIDTH;
100  wc3->height = WC3_DEFAULT_HEIGHT;
101  wc3->pts = 0;
102  wc3->video_stream_index = wc3->audio_stream_index = 0;
103  av_init_packet(&wc3->vpkt);
104  wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
105 
106  /* skip the first 3 32-bit numbers */
107  avio_skip(pb, 12);
108 
109  /* traverse through the chunks and load the header information before
110  * the first BRCH tag */
111  fourcc_tag = avio_rl32(pb);
112  size = (avio_rb32(pb) + 1) & (~1);
113 
114  do {
115  switch (fourcc_tag) {
116 
117  case SOND_TAG:
118  case INDX_TAG:
119  /* SOND unknown, INDX unnecessary; ignore both */
120  avio_skip(pb, size);
121  break;
122 
123  case PC__TAG:
124  /* number of palettes, unneeded */
125  avio_skip(pb, 12);
126  break;
127 
128  case BNAM_TAG:
129  /* load up the name */
130  buffer = av_malloc(size+1);
131  if (!buffer)
132  return AVERROR(ENOMEM);
133  if ((ret = avio_read(pb, buffer, size)) != size)
134  return AVERROR(EIO);
135  buffer[size] = 0;
136  av_dict_set(&s->metadata, "title", buffer,
138  break;
139 
140  case SIZE_TAG:
141  /* video resolution override */
142  wc3->width = avio_rl32(pb);
143  wc3->height = avio_rl32(pb);
144  break;
145 
146  case PALT_TAG:
147  /* one of several palettes */
148  avio_seek(pb, -8, SEEK_CUR);
149  av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
150  break;
151 
152  default:
153  av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
154  av_fourcc2str(fourcc_tag));
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 (avio_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->codecpar->codec_tag = 0; /* no fourcc */
175  st->codecpar->width = wc3->width;
176  st->codecpar->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->codecpar->codec_tag = 1;
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 (avio_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 ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
245  ret = AVERROR(EIO);
246  else {
247  int i = 0;
248  av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
249  if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
250  return AVERROR_INVALIDDATA;
251  av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
252  i += text[i] + 1;
253  if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
254  return AVERROR_INVALIDDATA;
255  av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
256  i += text[i] + 1;
257  if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
258  return AVERROR_INVALIDDATA;
259  av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
260  }
261  break;
262 
263  case AUDI_TAG:
264  /* send out audio chunk */
265  ret= av_get_packet(pb, pkt, size);
266  pkt->stream_index = wc3->audio_stream_index;
267  pkt->pts = wc3->pts;
268 
269  /* time to advance pts */
270  wc3->pts++;
271 
272  packet_read = 1;
273  break;
274 
275  default:
276  av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
277  av_fourcc2str(fourcc_tag));
278  ret = AVERROR_INVALIDDATA;
279  packet_read = 1;
280  break;
281  }
282  }
283 
284  return ret;
285 }
286 
288 {
289  Wc3DemuxContext *wc3 = s->priv_data;
290 
291  if (wc3->vpkt.size > 0)
292  av_packet_unref(&wc3->vpkt);
293 
294  return 0;
295 }
296 
298  .name = "wc3movie",
299  .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
300  .priv_data_size = sizeof(Wc3DemuxContext),
305 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
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:140
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:4737
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
int index
stream index in AVFormatContext
Definition: avformat.h:890
int size
Definition: avcodec.h:1680
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
#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:329
static AVPacket pkt
int video_stream_index
Definition: wc3movie.c:69
Format I/O context.
Definition: avformat.h:1349
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: avcodec.h:4218
static int wc3_read_close(AVFormatContext *s)
Definition: wc3movie.c:287
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:790
AVInputFormat ff_wc3_demuxer
Definition: wc3movie.c:297
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4367
uint8_t * data
Definition: avcodec.h:1679
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:289
ptrdiff_t size
Definition: opengl_enc.c:101
#define BNAM_TAG
Definition: wc3movie.c:41
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4254
#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:299
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4181
#define PALT_TAG
Definition: wc3movie.c:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1567
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
#define AUDI_TAG
Definition: wc3movie.c:49
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
#define WC3_DEFAULT_WIDTH
Definition: wc3movie.c:52
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
int block_align
Audio only.
Definition: avcodec.h:4269
audio channel layout utility functions
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:528
Stream structure.
Definition: avformat.h:889
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:1391
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:618
#define PC__TAG
Definition: wc3movie.c:39
static int wc3_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wc3movie.c:197
#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:461
#define WC3_AUDIO_CHANNELS
Definition: wc3movie.c:57
int sample_rate
Audio only.
Definition: avcodec.h:4262
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
Main libavformat public API header.
#define WC3_SAMPLE_RATE
Definition: wc3movie.c:56
static int wc3_read_header(AVFormatContext *s)
Definition: wc3movie.c:88
static int wc3_probe(AVProbeData *p)
Definition: wc3movie.c:76
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
AVPacket vpkt
Definition: wc3movie.c:72
void * priv_data
Format private data.
Definition: avformat.h:1377
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4194
int channels
Audio only.
Definition: avcodec.h:4258
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4156
#define VGA__TAG
Definition: wc3movie.c:47
int stream_index
Definition: avcodec.h:1681
#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: avcodec.h:1656
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1672
GLuint buffer
Definition: opengl_enc.c:102
#define MOVE_TAG
Definition: wc3movie.c:38