42cad81a |
/*
* FLI/FLC Animation File Demuxer
* Copyright (c) 2003 The ffmpeg Project
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
42cad81a |
* 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. |
42cad81a |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
42cad81a |
* 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 |
42cad81a |
*/
/** |
ba87f080 |
* @file |
42cad81a |
* FLI/FLC file demuxer
* by Mike Melanson (melanson@pcisys.net)
* for more information on the .fli/.flc file format and all of its many
* variations, visit:
* http://www.compuphase.com/flic.htm
* |
951776c4 |
* This demuxer handles standard 0xAF11- and 0xAF12-type FLIs. It also handles
* special FLIs from the PC games "Magic Carpet" and "X-COM: Terror from the Deep". |
42cad81a |
*/
|
a903f8f0 |
#include "libavutil/channel_layout.h" |
6a5d31ac |
#include "libavutil/intreadwrite.h" |
42cad81a |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
42cad81a |
#define FLIC_FILE_MAGIC_1 0xAF11
#define FLIC_FILE_MAGIC_2 0xAF12 |
115329f1 |
#define FLIC_FILE_MAGIC_3 0xAF44 /* Flic Type for Extended FLX Format which |
515ae476 |
originated in Dave's Targa Animator (DTA) */ |
42cad81a |
#define FLIC_CHUNK_MAGIC_1 0xF1FA
#define FLIC_CHUNK_MAGIC_2 0xF5FA |
80a16ccf |
#define FLIC_MC_SPEED 5 /* speed for Magic Carpet game FLIs */
#define FLIC_DEFAULT_SPEED 5 /* for FLIs that have 0 speed */ |
951776c4 |
#define FLIC_TFTD_CHUNK_AUDIO 0xAAAA /* Audio chunk. Used in Terror from the Deep.
Has 10 B extra header not accounted for in the chunk header */
#define FLIC_TFTD_SAMPLE_RATE 22050 |
42cad81a |
#define FLIC_HEADER_SIZE 128
#define FLIC_PREAMBLE_SIZE 6
typedef struct FlicDemuxContext {
int video_stream_index; |
951776c4 |
int audio_stream_index; |
80a16ccf |
int frame_number; |
42cad81a |
} FlicDemuxContext;
static int flic_probe(AVProbeData *p)
{
int magic_number;
|
154dffd0 |
if(p->buf_size < FLIC_HEADER_SIZE)
return 0;
|
fead30d4 |
magic_number = AV_RL16(&p->buf[4]); |
42cad81a |
if ((magic_number != FLIC_FILE_MAGIC_1) && |
515ae476 |
(magic_number != FLIC_FILE_MAGIC_2) &&
(magic_number != FLIC_FILE_MAGIC_3)) |
42cad81a |
return 0;
|
154dffd0 |
if(AV_RL16(&p->buf[0x10]) != FLIC_CHUNK_MAGIC_1){
if(AV_RL32(&p->buf[0x10]) > 2000)
return 0;
}
if( AV_RL16(&p->buf[0x08]) > 4096
|| AV_RL16(&p->buf[0x0A]) > 4096)
return 0;
|
86ea021b |
return AVPROBE_SCORE_MAX - 1; |
42cad81a |
}
|
6e9651d1 |
static int flic_read_header(AVFormatContext *s) |
42cad81a |
{ |
e4141433 |
FlicDemuxContext *flic = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
42cad81a |
unsigned char header[FLIC_HEADER_SIZE]; |
951776c4 |
AVStream *st, *ast; |
42cad81a |
int speed;
int magic_number; |
951776c4 |
unsigned char preamble[FLIC_PREAMBLE_SIZE]; |
42cad81a |
|
80a16ccf |
flic->frame_number = 0; |
42cad81a |
/* load the whole header and pull out the width and height */ |
b7effd4e |
if (avio_read(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE) |
6f3e0b21 |
return AVERROR(EIO); |
42cad81a |
|
fead30d4 |
magic_number = AV_RL16(&header[4]);
speed = AV_RL32(&header[0x10]); |
80a16ccf |
if (speed == 0)
speed = FLIC_DEFAULT_SPEED; |
42cad81a |
/* initialize the decoder streams */ |
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
42cad81a |
if (!st) |
769e10f0 |
return AVERROR(ENOMEM); |
42cad81a |
flic->video_stream_index = st->index; |
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
36ef5369 |
st->codec->codec_id = AV_CODEC_ID_FLIC; |
01f4895c |
st->codec->codec_tag = 0; /* no fourcc */ |
fead30d4 |
st->codec->width = AV_RL16(&header[0x08]);
st->codec->height = AV_RL16(&header[0x0A]); |
42cad81a |
|
77cb22fa |
if (!st->codec->width || !st->codec->height) {
/* Ugly hack needed for the following sample: */
/* http://samples.mplayerhq.hu/fli-flc/fli-bugs/specular.flc */
av_log(s, AV_LOG_WARNING,
"File with no specified width/height. Trying 640x480.\n");
st->codec->width = 640;
st->codec->height = 480;
} |
42cad81a |
/* send over the whole 128-byte FLIC header */ |
a807c682 |
if (ff_alloc_extradata(st->codec, FLIC_HEADER_SIZE)) |
00e1bf8a |
return AVERROR(ENOMEM); |
01f4895c |
memcpy(st->codec->extradata, header, FLIC_HEADER_SIZE); |
42cad81a |
|
951776c4 |
/* peek at the preamble to detect TFTD videos - they seem to always start with an audio chunk */ |
b7effd4e |
if (avio_read(pb, preamble, FLIC_PREAMBLE_SIZE) != FLIC_PREAMBLE_SIZE) { |
951776c4 |
av_log(s, AV_LOG_ERROR, "Failed to peek at preamble\n");
return AVERROR(EIO);
} |
42cad81a |
|
6b4aa5da |
avio_seek(pb, -FLIC_PREAMBLE_SIZE, SEEK_CUR); |
951776c4 |
/* Time to figure out the framerate:
* If the first preamble's magic number is 0xAAAA then this file is from
* X-COM: Terror from the Deep. If on the other hand there is a FLIC chunk
* magic number at offset 0x10 assume this file is from Magic Carpet instead.
* If neither of the above is true then this is a normal FLIC file.
*/
if (AV_RL16(&preamble[4]) == FLIC_TFTD_CHUNK_AUDIO) {
/* TFTD videos have an extra 22050 Hz 8-bit mono audio stream */ |
84ad31ff |
ast = avformat_new_stream(s, NULL); |
951776c4 |
if (!ast)
return AVERROR(ENOMEM);
flic->audio_stream_index = ast->index;
/* all audio frames are the same size, so use the size of the first chunk for block_align */
ast->codec->block_align = AV_RL32(&preamble[0]); |
b9f9e59a |
ast->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
36ef5369 |
ast->codec->codec_id = AV_CODEC_ID_PCM_U8; |
951776c4 |
ast->codec->codec_tag = 0;
ast->codec->sample_rate = FLIC_TFTD_SAMPLE_RATE;
ast->codec->channels = 1;
ast->codec->bit_rate = st->codec->sample_rate * 8;
ast->codec->bits_per_coded_sample = 8; |
c2fcd0a7 |
ast->codec->channel_layout = AV_CH_LAYOUT_MONO; |
951776c4 |
ast->codec->extradata_size = 0;
/* Since the header information is incorrect we have to figure out the
* framerate using block_align and the fact that the audio is 22050 Hz.
* We usually have two cases: 2205 -> 10 fps and 1470 -> 15 fps */ |
c3f9ebf7 |
avpriv_set_pts_info(st, 64, ast->codec->block_align, FLIC_TFTD_SAMPLE_RATE);
avpriv_set_pts_info(ast, 64, 1, FLIC_TFTD_SAMPLE_RATE); |
951776c4 |
} else if (AV_RL16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) { |
c3f9ebf7 |
avpriv_set_pts_info(st, 64, FLIC_MC_SPEED, 70); |
42cad81a |
/* rewind the stream since the first chunk is at offset 12 */ |
6b4aa5da |
avio_seek(pb, 12, SEEK_SET); |
42cad81a |
/* send over abbreviated FLIC header chunk */ |
01f4895c |
av_free(st->codec->extradata); |
a807c682 |
if (ff_alloc_extradata(st->codec, 12)) |
00e1bf8a |
return AVERROR(ENOMEM); |
01f4895c |
memcpy(st->codec->extradata, header, 12); |
42cad81a |
} else if (magic_number == FLIC_FILE_MAGIC_1) { |
c3f9ebf7 |
avpriv_set_pts_info(st, 64, speed, 70); |
515ae476 |
} else if ((magic_number == FLIC_FILE_MAGIC_2) ||
(magic_number == FLIC_FILE_MAGIC_3)) { |
c3f9ebf7 |
avpriv_set_pts_info(st, 64, speed, 1000); |
89cf4098 |
} else { |
50591626 |
av_log(s, AV_LOG_ERROR, "Invalid or unsupported magic chunk in file\n"); |
42cad81a |
return AVERROR_INVALIDDATA; |
89cf4098 |
} |
42cad81a |
return 0;
}
static int flic_read_packet(AVFormatContext *s,
AVPacket *pkt)
{ |
e4141433 |
FlicDemuxContext *flic = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
42cad81a |
int packet_read = 0;
unsigned int size;
int magic;
int ret = 0;
unsigned char preamble[FLIC_PREAMBLE_SIZE];
while (!packet_read) {
|
b7effd4e |
if ((ret = avio_read(pb, preamble, FLIC_PREAMBLE_SIZE)) != |
42cad81a |
FLIC_PREAMBLE_SIZE) { |
6f3e0b21 |
ret = AVERROR(EIO); |
42cad81a |
break;
}
|
fead30d4 |
size = AV_RL32(&preamble[0]);
magic = AV_RL16(&preamble[4]); |
42cad81a |
|
0ecca7a4 |
if (((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) && size > FLIC_PREAMBLE_SIZE) { |
42cad81a |
if (av_new_packet(pkt, size)) { |
6f3e0b21 |
ret = AVERROR(EIO); |
42cad81a |
break;
}
pkt->stream_index = flic->video_stream_index; |
80a16ccf |
pkt->pts = flic->frame_number++; |
a2704c97 |
pkt->pos = avio_tell(pb); |
42cad81a |
memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE); |
b7effd4e |
ret = avio_read(pb, pkt->data + FLIC_PREAMBLE_SIZE, |
42cad81a |
size - FLIC_PREAMBLE_SIZE);
if (ret != size - FLIC_PREAMBLE_SIZE) {
av_free_packet(pkt); |
6f3e0b21 |
ret = AVERROR(EIO); |
42cad81a |
}
packet_read = 1; |
951776c4 |
} else if (magic == FLIC_TFTD_CHUNK_AUDIO) {
if (av_new_packet(pkt, size)) {
ret = AVERROR(EIO);
break;
}
/* skip useless 10B sub-header (yes, it's not accounted for in the chunk header) */ |
45a8a02a |
avio_skip(pb, 10); |
951776c4 |
pkt->stream_index = flic->audio_stream_index; |
a2704c97 |
pkt->pos = avio_tell(pb); |
b7effd4e |
ret = avio_read(pb, pkt->data, size); |
951776c4 |
if (ret != size) {
av_free_packet(pkt);
ret = AVERROR(EIO);
}
packet_read = 1; |
42cad81a |
} else {
/* not interested in this chunk */ |
45a8a02a |
avio_skip(pb, size - 6); |
42cad81a |
}
}
return ret;
}
|
c6610a21 |
AVInputFormat ff_flic_demuxer = { |
dfc2c4d9 |
.name = "flic", |
6774247a |
.long_name = NULL_IF_CONFIG_SMALL("FLI/FLC/FLX animation"), |
dfc2c4d9 |
.priv_data_size = sizeof(FlicDemuxContext),
.read_probe = flic_probe,
.read_header = flic_read_header,
.read_packet = flic_read_packet, |
42cad81a |
}; |