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 "demux.h"
36 #include "internal.h"
37 
38 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
39 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
40 #define PC__TAG MKTAG('_', 'P', 'C', '_')
41 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
42 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
43 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
44 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
45 #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
46 #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
47 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
48 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
49 #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
50 #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
51 
52 /* video resolution unless otherwise specified */
53 #define WC3_DEFAULT_WIDTH 320
54 #define WC3_DEFAULT_HEIGHT 165
55 
56 /* always use the same PCM audio parameters */
57 #define WC3_SAMPLE_RATE 22050
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  av_packet_free(&wc3->vpkt);
81 
82  return 0;
83 }
84 
85 static int wc3_probe(const AVProbeData *p)
86 {
87  if (p->buf_size < 12)
88  return 0;
89 
90  if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
91  (AV_RL32(&p->buf[8]) != MOVE_TAG))
92  return 0;
93 
94  return AVPROBE_SCORE_MAX;
95 }
96 
98 {
99  Wc3DemuxContext *wc3 = s->priv_data;
100  AVIOContext *pb = s->pb;
101  unsigned int fourcc_tag;
102  unsigned int size;
103  AVStream *st;
104  int ret = 0;
105  char *buffer;
106 
107  /* default context members */
108  wc3->width = WC3_DEFAULT_WIDTH;
109  wc3->height = WC3_DEFAULT_HEIGHT;
110  wc3->pts = 0;
111  wc3->video_stream_index = wc3->audio_stream_index = 0;
112  wc3->vpkt = av_packet_alloc();
113  if (!wc3->vpkt)
114  return AVERROR(ENOMEM);
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  return AVERROR(ENOMEM);
143  if ((ret = avio_read(pb, buffer, size)) != size) {
144  av_freep(&buffer);
145  return AVERROR(EIO);
146  }
147  buffer[size] = 0;
148  av_dict_set(&s->metadata, "title", buffer,
150  break;
151 
152  case SIZE_TAG:
153  /* video resolution override */
154  wc3->width = avio_rl32(pb);
155  wc3->height = avio_rl32(pb);
156  break;
157 
158  case PALT_TAG:
159  /* one of several palettes */
160  avio_seek(pb, -8, SEEK_CUR);
161  av_append_packet(pb, wc3->vpkt, 8 + PALETTE_SIZE);
162  break;
163 
164  default:
165  av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
166  av_fourcc2str(fourcc_tag));
167  return AVERROR_INVALIDDATA;
168  }
169 
170  fourcc_tag = avio_rl32(pb);
171  /* chunk sizes are 16-bit aligned */
172  size = (avio_rb32(pb) + 1) & (~1);
173  if (avio_feof(pb))
174  return AVERROR(EIO);
175 
176  } while (fourcc_tag != BRCH_TAG);
177 
178  /* initialize the decoder streams */
179  st = avformat_new_stream(s, NULL);
180  if (!st)
181  return AVERROR(ENOMEM);
183  wc3->video_stream_index = st->index;
186  st->codecpar->codec_tag = 0; /* no fourcc */
187  st->codecpar->width = wc3->width;
188  st->codecpar->height = wc3->height;
189 
190  st = avformat_new_stream(s, NULL);
191  if (!st)
192  return AVERROR(ENOMEM);
194  wc3->audio_stream_index = st->index;
197  st->codecpar->codec_tag = 1;
204 
205  return 0;
206 }
207 
209  AVPacket *pkt)
210 {
211  Wc3DemuxContext *wc3 = s->priv_data;
212  AVIOContext *pb = s->pb;
213  unsigned int fourcc_tag;
214  unsigned int size;
215  int packet_read = 0;
216  int ret = 0;
217  unsigned char text[1024];
218 
219  while (!packet_read) {
220 
221  fourcc_tag = avio_rl32(pb);
222  /* chunk sizes are 16-bit aligned */
223  size = (avio_rb32(pb) + 1) & (~1);
224  if (avio_feof(pb))
225  return AVERROR(EIO);
226 
227  switch (fourcc_tag) {
228 
229  case BRCH_TAG:
230  /* no-op */
231  break;
232 
233  case SHOT_TAG:
234  /* load up new palette */
235  avio_seek(pb, -8, SEEK_CUR);
236  av_append_packet(pb, wc3->vpkt, 8 + 4);
237  break;
238 
239  case VGA__TAG:
240  /* send out video chunk */
241  avio_seek(pb, -8, SEEK_CUR);
242  ret= av_append_packet(pb, wc3->vpkt, 8 + size);
243  // ignore error if we have some data
244  if (wc3->vpkt->size > 0)
245  ret = 0;
246  av_packet_move_ref(pkt, wc3->vpkt);
248  pkt->pts = wc3->pts;
249  packet_read = 1;
250  break;
251 
252  case TEXT_TAG:
253  /* subtitle chunk */
254  if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
255  ret = AVERROR(EIO);
256  else {
257  int i = 0;
258  av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
259  if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
260  return AVERROR_INVALIDDATA;
261  av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
262  i += text[i] + 1;
263  if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
264  return AVERROR_INVALIDDATA;
265  av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
266  i += text[i] + 1;
267  if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
268  return AVERROR_INVALIDDATA;
269  av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
270  }
271  break;
272 
273  case AUDI_TAG:
274  /* send out audio chunk */
275  ret= av_get_packet(pb, pkt, size);
277  pkt->pts = wc3->pts;
278 
279  /* time to advance pts */
280  wc3->pts++;
281 
282  packet_read = 1;
283  break;
284 
285  default:
286  av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
287  av_fourcc2str(fourcc_tag));
289  packet_read = 1;
290  break;
291  }
292  }
293 
294  return ret;
295 }
296 
298  .p.name = "wc3movie",
299  .p.long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
300  .priv_data_size = sizeof(Wc3DemuxContext),
301  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
306 };
Wc3DemuxContext::audio_stream_index
int audio_stream_index
Definition: wc3movie.c:70
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:328
AVERROR
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
ff_wc3_demuxer
const FFInputFormat ff_wc3_demuxer
Definition: wc3movie.c:297
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
FORM_TAG
#define FORM_TAG
Definition: wc3movie.c:38
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:74
PC__TAG
#define PC__TAG
Definition: wc3movie.c:40
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
Wc3DemuxContext::pts
int64_t pts
Definition: wc3movie.c:68
Wc3DemuxContext
Definition: wc3movie.c:65
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:853
WC3_SAMPLE_RATE
#define WC3_SAMPLE_RATE
Definition: wc3movie.c:57
AUDI_TAG
#define AUDI_TAG
Definition: wc3movie.c:50
wc3_read_header
static int wc3_read_header(AVFormatContext *s)
Definition: wc3movie.c:97
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
AV_DICT_DONT_STRDUP_VAL
#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:79
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:760
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
TEXT_TAG
#define TEXT_TAG
Definition: wc3movie.c:49
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
FF_INFMT_FLAG_INIT_CLEANUP
#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
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
SOND_TAG
#define SOND_TAG
Definition: wc3movie.c:41
NULL
#define NULL
Definition: coverity.c:32
av_strnlen
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
VGA__TAG
#define VGA__TAG
Definition: wc3movie.c:48
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
MOVE_TAG
#define MOVE_TAG
Definition: wc3movie.c:39
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
av_packet_move_ref
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:484
WC3_DEFAULT_HEIGHT
#define WC3_DEFAULT_HEIGHT
Definition: wc3movie.c:54
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
BRCH_TAG
#define BRCH_TAG
Definition: wc3movie.c:46
SHOT_TAG
#define SHOT_TAG
Definition: wc3movie.c:47
BNAM_TAG
#define BNAM_TAG
Definition: wc3movie.c:42
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:729
Wc3DemuxContext::vpkt
AVPacket * vpkt
Definition: wc3movie.c:72
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:523
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
size
int size
Definition: twinvq_data.h:10344
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
WC3_AUDIO_BITS
#define WC3_AUDIO_BITS
Definition: wc3movie.c:58
AV_CODEC_ID_XAN_WC3
@ AV_CODEC_ID_XAN_WC3
Definition: codec_id.h:92
wc3_probe
static int wc3_probe(const AVProbeData *p)
Definition: wc3movie.c:85
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
AVCodecParameters::height
int height
Definition: codec_par.h:135
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
wc3_read_packet
static int wc3_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wc3movie.c:208
demux.h
wc3_read_close
static int wc3_read_close(AVFormatContext *s)
Definition: wc3movie.c:76
Wc3DemuxContext::width
int width
Definition: wc3movie.c:66
av_get_packet
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:103
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
av_append_packet
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:119
avformat.h
dict.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:749
channel_layout.h
PALETTE_SIZE
#define PALETTE_SIZE
Definition: wc3movie.c:63
SIZE_TAG
#define SIZE_TAG
Definition: wc3movie.c:43
buffer
the frame and frame 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 AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:611
AVPacket::stream_index
int stream_index
Definition: packet.h:524
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:317
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
PALT_TAG
#define PALT_TAG
Definition: wc3movie.c:44
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
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:88
FFInputFormat
Definition: demux.h:37
INDX_TAG
#define INDX_TAG
Definition: wc3movie.c:45
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
Wc3DemuxContext::video_stream_index
int video_stream_index
Definition: wc3movie.c:69
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
Wc3DemuxContext::height
int height
Definition: wc3movie.c:67
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
WC3_DEFAULT_WIDTH
#define WC3_DEFAULT_WIDTH
Definition: wc3movie.c:53
avstring.h
WC3_FRAME_FPS
#define WC3_FRAME_FPS
Definition: wc3movie.c:61
av_fourcc2str
#define av_fourcc2str(fourcc)
Definition: avutil.h:345
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345