FFmpeg
Loading...
Searching...
No Matches
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"
33#include "libavutil/dict.h"
34#include "libavutil/mem.h"
35#include "avformat.h"
36#include "avio_internal.h"
37#include "demux.h"
38#include "internal.h"
39
40#define FORM_TAG MKTAG('F', 'O', 'R', 'M')
41#define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
42#define PC__TAG MKTAG('_', 'P', 'C', '_')
43#define SOND_TAG MKTAG('S', 'O', 'N', 'D')
44#define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
45#define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
46#define PALT_TAG MKTAG('P', 'A', 'L', 'T')
47#define INDX_TAG MKTAG('I', 'N', 'D', 'X')
48#define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
49#define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
50#define VGA__TAG MKTAG('V', 'G', 'A', ' ')
51#define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
52#define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
53
54/* video resolution unless otherwise specified */
55#define WC3_DEFAULT_WIDTH 320
56#define WC3_DEFAULT_HEIGHT 165
57
58/* always use the same PCM audio parameters */
59#define WC3_SAMPLE_RATE 22050
60#define WC3_AUDIO_BITS 16
61
62/* nice, constant framerate */
63#define WC3_FRAME_FPS 15
64
65#define PALETTE_SIZE (256 * 3)
66
77
79{
80 Wc3DemuxContext *wc3 = s->priv_data;
81
82 av_packet_free(&wc3->vpkt);
83
84 return 0;
85}
86
87static int wc3_probe(const AVProbeData *p)
88{
89 if (p->buf_size < 12)
90 return 0;
91
92 if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
93 (AV_RL32(&p->buf[8]) != MOVE_TAG))
94 return 0;
95
96 return AVPROBE_SCORE_MAX;
97}
98
100{
101 Wc3DemuxContext *wc3 = s->priv_data;
102 AVIOContext *pb = s->pb;
103 unsigned int fourcc_tag;
104 unsigned int size;
105 AVStream *st;
106 int ret = 0;
107 char *buffer;
108
109 /* default context members */
112 wc3->pts = 0;
114 wc3->vpkt = av_packet_alloc();
115 if (!wc3->vpkt)
116 return AVERROR(ENOMEM);
117
118 /* skip the first 3 32-bit numbers */
119 avio_skip(pb, 12);
120
121 /* traverse through the chunks and load the header information before
122 * the first BRCH tag */
123 fourcc_tag = avio_rl32(pb);
124 size = (avio_rb32(pb) + 1) & (~1);
125
126 do {
127 switch (fourcc_tag) {
128
129 case SOND_TAG:
130 case INDX_TAG:
131 /* SOND unknown, INDX unnecessary; ignore both */
132 avio_skip(pb, size);
133 break;
134
135 case PC__TAG:
136 /* number of palettes, unneeded */
137 avio_skip(pb, 12);
138 break;
139
140 case BNAM_TAG:
141 /* load up the name */
142 buffer = av_malloc(size+1);
143 if (!buffer)
144 return AVERROR(ENOMEM);
145 if ((ret = ffio_read_size(pb, buffer, size)) < 0) {
147 return ret;
148 }
149 buffer[size] = 0;
150 av_dict_set(&s->metadata, "title", buffer,
152 break;
153
154 case SIZE_TAG:
155 /* video resolution override */
156 wc3->width = avio_rl32(pb);
157 wc3->height = avio_rl32(pb);
158 break;
159
160 case PALT_TAG:
161 /* one of several palettes */
162 avio_seek(pb, -8, SEEK_CUR);
163 av_append_packet(pb, wc3->vpkt, 8 + PALETTE_SIZE);
164 break;
165
166 default:
167 av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
168 av_fourcc2str(fourcc_tag));
169 return AVERROR_INVALIDDATA;
170 }
171
172 fourcc_tag = avio_rl32(pb);
173 /* chunk sizes are 16-bit aligned */
174 size = (avio_rb32(pb) + 1) & (~1);
175 if (avio_feof(pb))
176 return AVERROR_INVALIDDATA;
177
178 } while (fourcc_tag != BRCH_TAG);
179
180 /* initialize the decoder streams */
182 if (!st)
183 return AVERROR(ENOMEM);
185 wc3->video_stream_index = st->index;
188 st->codecpar->codec_tag = 0; /* no fourcc */
189 st->codecpar->width = wc3->width;
190 st->codecpar->height = wc3->height;
191
193 if (!st)
194 return AVERROR(ENOMEM);
196 wc3->audio_stream_index = st->index;
199 st->codecpar->codec_tag = 1;
206
207 return 0;
208}
209
211 AVPacket *pkt)
212{
213 Wc3DemuxContext *wc3 = s->priv_data;
214 AVIOContext *pb = s->pb;
215 unsigned int fourcc_tag;
216 unsigned int size;
217 int packet_read = 0;
218 int ret = 0;
219 unsigned char text[1024];
220
221 while (!packet_read) {
222
223 fourcc_tag = avio_rl32(pb);
224 /* chunk sizes are 16-bit aligned */
225 size = (avio_rb32(pb) + 1) & (~1);
226 if (avio_feof(pb))
227 return AVERROR_INVALIDDATA;
228
229 switch (fourcc_tag) {
230
231 case BRCH_TAG:
232 /* no-op */
233 break;
234
235 case SHOT_TAG:
236 /* load up new palette */
237 avio_seek(pb, -8, SEEK_CUR);
238 av_append_packet(pb, wc3->vpkt, 8 + 4);
239 break;
240
241 case VGA__TAG:
242 /* send out video chunk */
243 avio_seek(pb, -8, SEEK_CUR);
244 ret= av_append_packet(pb, wc3->vpkt, 8 + size);
245 // ignore error if we have some data
246 if (wc3->vpkt->size > 0)
247 ret = 0;
249 pkt->stream_index = wc3->video_stream_index;
250 pkt->pts = wc3->pts;
251 packet_read = 1;
252 break;
253
254 case TEXT_TAG:
255 /* subtitle chunk */
256 if ((unsigned)size > sizeof(text))
258 else if ((ret = ffio_read_size(pb, text, size)) == size) {
259 int i = 0;
260 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
261 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
262 return AVERROR_INVALIDDATA;
263 av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
264 i += text[i] + 1;
265 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
266 return AVERROR_INVALIDDATA;
267 av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
268 i += text[i] + 1;
269 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
270 return AVERROR_INVALIDDATA;
271 av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
272 }
273 break;
274
275 case AUDI_TAG:
276 /* send out audio chunk */
277 ret= av_get_packet(pb, pkt, size);
278 pkt->stream_index = wc3->audio_stream_index;
279 pkt->pts = wc3->pts;
280
281 /* time to advance pts */
282 wc3->pts++;
283
284 packet_read = 1;
285 break;
286
287 default:
288 av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
289 av_fourcc2str(fourcc_tag));
291 packet_read = 1;
292 break;
293 }
294 }
295
296 return ret;
297}
298
300 .p.name = "wc3movie",
301 .p.long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
302 .priv_data_size = sizeof(Wc3DemuxContext),
303 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
308};
const FFInputFormat ff_wc3_demuxer
Definition wc3movie.c:299
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.
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:114
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
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:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
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
Public dictionary API.
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_XAN_WC3
Definition codec_id.h:90
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:491
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_MONO
#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
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:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#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
#define av_fourcc2str(fourcc)
Definition avutil.h:323
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
#define AV_RL32(p)
#define FORM_TAG
Definition rl2.c:46
#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
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
#define PALETTE_SIZE
Definition sanm.c:34
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
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
int width
The width of the video frame in pixels.
Definition codec_par.h:143
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
int size
Definition packet.h:604
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
int video_stream_index
Definition wc3movie.c:71
int64_t pts
Definition wc3movie.c:70
int audio_stream_index
Definition wc3movie.c:72
AVPacket * vpkt
Definition wc3movie.c:74
#define av_freep(p)
#define av_log(a,...)
static char buffer[20]
Definition seek.c:32
int size
#define SIZE_TAG
Definition wc3movie.c:45
#define MOVE_TAG
Definition wc3movie.c:41
#define AUDI_TAG
Definition wc3movie.c:52
#define WC3_FRAME_FPS
Definition wc3movie.c:63
#define TEXT_TAG
Definition wc3movie.c:51
static int wc3_probe(const AVProbeData *p)
Definition wc3movie.c:87
#define WC3_AUDIO_BITS
Definition wc3movie.c:60
static int wc3_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition wc3movie.c:210
static int wc3_read_close(AVFormatContext *s)
Definition wc3movie.c:78
static int wc3_read_header(AVFormatContext *s)
Definition wc3movie.c:99
#define WC3_DEFAULT_WIDTH
Definition wc3movie.c:55
#define WC3_SAMPLE_RATE
Definition wc3movie.c:59
#define WC3_DEFAULT_HEIGHT
Definition wc3movie.c:56
#define INDX_TAG
Definition wc3movie.c:47
#define BRCH_TAG
Definition wc3movie.c:48
#define BNAM_TAG
Definition wc3movie.c:44
#define SOND_TAG
Definition wc3movie.c:43
#define PC__TAG
Definition wc3movie.c:42
#define VGA__TAG
Definition xan.c:45
#define SHOT_TAG
Definition xan.c:47
#define PALT_TAG
Definition xan.c:46