493645eb |
/*
* Wing Commander III Movie (.mve) File Demuxer |
6dfa70f2 |
* Copyright (c) 2003 The FFmpeg Project |
493645eb |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
493645eb |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
493645eb |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
493645eb |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
493645eb |
*/
/** |
ba87f080 |
* @file |
493645eb |
* Wing Commander III Movie file demuxer
* by Mike Melanson (melanson@pcisys.net)
* for more information on the WC3 .mve file format, visit:
* http://www.pcisys.net/~melanson/codecs/
*/
|
1acd029f |
#include "libavutil/avstring.h" |
2ce7f820 |
#include "libavutil/channel_layout.h" |
6a5d31ac |
#include "libavutil/intreadwrite.h" |
d2d67e42 |
#include "libavutil/dict.h" |
493645eb |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
493645eb |
|
3a278992 |
#define FORM_TAG MKTAG('F', 'O', 'R', 'M')
#define MOVE_TAG MKTAG('M', 'O', 'V', 'E') |
7fffc879 |
#define PC__TAG MKTAG('_', 'P', 'C', '_') |
3a278992 |
#define SOND_TAG MKTAG('S', 'O', 'N', 'D')
#define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
#define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
#define PALT_TAG MKTAG('P', 'A', 'L', 'T')
#define INDX_TAG MKTAG('I', 'N', 'D', 'X')
#define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
#define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
#define VGA__TAG MKTAG('V', 'G', 'A', ' ')
#define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
#define AUDI_TAG MKTAG('A', 'U', 'D', 'I') |
493645eb |
/* video resolution unless otherwise specified */
#define WC3_DEFAULT_WIDTH 320
#define WC3_DEFAULT_HEIGHT 165
/* always use the same PCM audio parameters */
#define WC3_SAMPLE_RATE 22050
#define WC3_AUDIO_CHANNELS 1
#define WC3_AUDIO_BITS 16
/* nice, constant framerate */ |
74e21d03 |
#define WC3_FRAME_FPS 15 |
493645eb |
#define PALETTE_SIZE (256 * 3)
typedef struct Wc3DemuxContext {
int width;
int height;
int64_t pts;
int video_stream_index;
int audio_stream_index;
|
24ae353d |
AVPacket vpkt; |
493645eb |
} Wc3DemuxContext;
static int wc3_probe(AVProbeData *p)
{
if (p->buf_size < 12)
return 0;
|
fead30d4 |
if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
(AV_RL32(&p->buf[8]) != MOVE_TAG)) |
493645eb |
return 0;
return AVPROBE_SCORE_MAX;
}
|
6e9651d1 |
static int wc3_read_header(AVFormatContext *s) |
493645eb |
{ |
e4141433 |
Wc3DemuxContext *wc3 = s->priv_data; |
471fe57e |
AVIOContext *pb = s->pb; |
493645eb |
unsigned int fourcc_tag;
unsigned int size;
AVStream *st;
int ret = 0; |
12ad6671 |
char *buffer; |
493645eb |
/* default context members */
wc3->width = WC3_DEFAULT_WIDTH;
wc3->height = WC3_DEFAULT_HEIGHT;
wc3->pts = 0;
wc3->video_stream_index = wc3->audio_stream_index = 0; |
24ae353d |
av_init_packet(&wc3->vpkt);
wc3->vpkt.data = NULL; wc3->vpkt.size = 0; |
493645eb |
/* skip the first 3 32-bit numbers */ |
45a8a02a |
avio_skip(pb, 12); |
493645eb |
/* traverse through the chunks and load the header information before
* the first BRCH tag */ |
e63a3628 |
fourcc_tag = avio_rl32(pb);
size = (avio_rb32(pb) + 1) & (~1); |
493645eb |
do {
switch (fourcc_tag) {
case SOND_TAG:
case INDX_TAG:
/* SOND unknown, INDX unnecessary; ignore both */ |
45a8a02a |
avio_skip(pb, size); |
493645eb |
break;
|
7fffc879 |
case PC__TAG: |
24ae353d |
/* number of palettes, unneeded */ |
45a8a02a |
avio_skip(pb, 12); |
493645eb |
break;
case BNAM_TAG:
/* load up the name */ |
12ad6671 |
buffer = av_malloc(size+1);
if (!buffer) |
2874c81c |
return AVERROR(ENOMEM); |
e63a3628 |
if ((ret = avio_read(pb, buffer, size)) != size) |
6f3e0b21 |
return AVERROR(EIO); |
12ad6671 |
buffer[size] = 0; |
d2d67e42 |
av_dict_set(&s->metadata, "title", buffer,
AV_DICT_DONT_STRDUP_VAL); |
493645eb |
break;
case SIZE_TAG:
/* video resolution override */ |
e63a3628 |
wc3->width = avio_rl32(pb);
wc3->height = avio_rl32(pb); |
493645eb |
break;
case PALT_TAG:
/* one of several palettes */ |
f59d8ff8 |
avio_seek(pb, -8, SEEK_CUR); |
24ae353d |
av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE); |
493645eb |
break;
default: |
bc874dae |
av_log(s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n", |
7ce04209 |
(uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
(uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24)); |
493645eb |
return AVERROR_INVALIDDATA;
}
|
e63a3628 |
fourcc_tag = avio_rl32(pb); |
493645eb |
/* chunk sizes are 16-bit aligned */ |
e63a3628 |
size = (avio_rb32(pb) + 1) & (~1); |
d34ec64a |
if (avio_feof(pb)) |
f6a708f0 |
return AVERROR(EIO); |
493645eb |
} while (fourcc_tag != BRCH_TAG);
/* initialize the decoder streams */ |
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
493645eb |
if (!st) |
769e10f0 |
return AVERROR(ENOMEM); |
c3f9ebf7 |
avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS); |
493645eb |
wc3->video_stream_index = st->index; |
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
36ef5369 |
st->codec->codec_id = AV_CODEC_ID_XAN_WC3; |
01f4895c |
st->codec->codec_tag = 0; /* no fourcc */
st->codec->width = wc3->width;
st->codec->height = wc3->height; |
493645eb |
|
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
493645eb |
if (!st) |
769e10f0 |
return AVERROR(ENOMEM); |
c3f9ebf7 |
avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS); |
493645eb |
wc3->audio_stream_index = st->index; |
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
36ef5369 |
st->codec->codec_id = AV_CODEC_ID_PCM_S16LE; |
01f4895c |
st->codec->codec_tag = 1;
st->codec->channels = WC3_AUDIO_CHANNELS; |
2ce7f820 |
st->codec->channel_layout = AV_CH_LAYOUT_MONO; |
dd1c8f3e |
st->codec->bits_per_coded_sample = WC3_AUDIO_BITS; |
01f4895c |
st->codec->sample_rate = WC3_SAMPLE_RATE;
st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * |
dd1c8f3e |
st->codec->bits_per_coded_sample; |
01f4895c |
st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS; |
493645eb |
return 0;
}
static int wc3_read_packet(AVFormatContext *s,
AVPacket *pkt)
{ |
e4141433 |
Wc3DemuxContext *wc3 = s->priv_data; |
471fe57e |
AVIOContext *pb = s->pb; |
493645eb |
unsigned int fourcc_tag;
unsigned int size;
int packet_read = 0;
int ret = 0;
unsigned char text[1024];
while (!packet_read) {
|
e63a3628 |
fourcc_tag = avio_rl32(pb); |
493645eb |
/* chunk sizes are 16-bit aligned */ |
e63a3628 |
size = (avio_rb32(pb) + 1) & (~1); |
d34ec64a |
if (avio_feof(pb)) |
f6a708f0 |
return AVERROR(EIO); |
493645eb |
switch (fourcc_tag) {
case BRCH_TAG:
/* no-op */
break;
case SHOT_TAG:
/* load up new palette */ |
f59d8ff8 |
avio_seek(pb, -8, SEEK_CUR); |
24ae353d |
av_append_packet(pb, &wc3->vpkt, 8 + 4); |
493645eb |
break;
case VGA__TAG:
/* send out video chunk */ |
f59d8ff8 |
avio_seek(pb, -8, SEEK_CUR); |
24ae353d |
ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
// ignore error if we have some data
if (wc3->vpkt.size > 0)
ret = 0;
*pkt = wc3->vpkt;
wc3->vpkt.data = NULL; wc3->vpkt.size = 0; |
493645eb |
pkt->stream_index = wc3->video_stream_index;
pkt->pts = wc3->pts;
packet_read = 1;
break;
case TEXT_TAG:
/* subtitle chunk */
#if 0 |
45a8a02a |
avio_skip(pb, size); |
493645eb |
#else |
e63a3628 |
if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size) |
6f3e0b21 |
ret = AVERROR(EIO); |
493645eb |
else {
int i = 0; |
bc874dae |
av_log (s, AV_LOG_DEBUG, "Subtitle time!\n"); |
1acd029f |
if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
return AVERROR_INVALIDDATA; |
bc874dae |
av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]); |
493645eb |
i += text[i] + 1; |
1acd029f |
if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
return AVERROR_INVALIDDATA; |
bc874dae |
av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]); |
493645eb |
i += text[i] + 1; |
1acd029f |
if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
return AVERROR_INVALIDDATA; |
bc874dae |
av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]); |
493645eb |
}
#endif
break;
case AUDI_TAG:
/* send out audio chunk */ |
2692067a |
ret= av_get_packet(pb, pkt, size); |
493645eb |
pkt->stream_index = wc3->audio_stream_index;
pkt->pts = wc3->pts;
/* time to advance pts */ |
74e21d03 |
wc3->pts++; |
493645eb |
packet_read = 1;
break;
default: |
bb270c08 |
av_log (s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n", |
7ce04209 |
(uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
(uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24)); |
493645eb |
ret = AVERROR_INVALIDDATA;
packet_read = 1;
break;
}
}
return ret;
}
static int wc3_read_close(AVFormatContext *s)
{ |
e4141433 |
Wc3DemuxContext *wc3 = s->priv_data; |
493645eb |
|
24ae353d |
if (wc3->vpkt.size > 0)
av_free_packet(&wc3->vpkt); |
493645eb |
return 0;
}
|
66355be3 |
AVInputFormat ff_wc3_demuxer = { |
dfc2c4d9 |
.name = "wc3movie", |
6774247a |
.long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"), |
dfc2c4d9 |
.priv_data_size = sizeof(Wc3DemuxContext),
.read_probe = wc3_probe,
.read_header = wc3_read_header,
.read_packet = wc3_read_packet,
.read_close = wc3_read_close, |
493645eb |
}; |