flic.c 9.34 KB
Newer Older
Mike Melanson's avatar
Mike Melanson committed
1 2
/*
 * FLI/FLC Animation File Demuxer
3
 * Copyright (c) 2003 The FFmpeg project
Mike Melanson's avatar
Mike Melanson committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
Mike Melanson's avatar
Mike Melanson committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Mike Melanson's avatar
Mike Melanson committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Mike Melanson's avatar
Mike Melanson committed
13 14 15 16 17
 * 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
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
Mike Melanson's avatar
Mike Melanson committed
20 21 22
 */

/**
23
 * @file
Mike Melanson's avatar
Mike Melanson committed
24 25 26 27 28 29
 * 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
 *
30 31
 * 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".
Mike Melanson's avatar
Mike Melanson committed
32 33
 */

34
#include "libavutil/channel_layout.h"
35
#include "libavutil/intreadwrite.h"
Mike Melanson's avatar
Mike Melanson committed
36
#include "avformat.h"
37
#include "internal.h"
Mike Melanson's avatar
Mike Melanson committed
38 39 40

#define FLIC_FILE_MAGIC_1 0xAF11
#define FLIC_FILE_MAGIC_2 0xAF12
41
#define FLIC_FILE_MAGIC_3 0xAF44  /* Flic Type for Extended FLX Format which
42
                                     originated in Dave's Targa Animator (DTA) */
Mike Melanson's avatar
Mike Melanson committed
43 44
#define FLIC_CHUNK_MAGIC_1 0xF1FA
#define FLIC_CHUNK_MAGIC_2 0xF5FA
Mike Melanson's avatar
Mike Melanson committed
45 46
#define FLIC_MC_SPEED 5  /* speed for Magic Carpet game FLIs */
#define FLIC_DEFAULT_SPEED 5  /* for FLIs that have 0 speed */
47 48 49
#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
Mike Melanson's avatar
Mike Melanson committed
50 51 52 53 54 55

#define FLIC_HEADER_SIZE 128
#define FLIC_PREAMBLE_SIZE 6

typedef struct FlicDemuxContext {
    int video_stream_index;
56
    int audio_stream_index;
Mike Melanson's avatar
Mike Melanson committed
57
    int frame_number;
Mike Melanson's avatar
Mike Melanson committed
58 59 60 61 62 63
} FlicDemuxContext;

static int flic_probe(AVProbeData *p)
{
    int magic_number;

64 65 66
    if(p->buf_size < FLIC_HEADER_SIZE)
        return 0;

67
    magic_number = AV_RL16(&p->buf[4]);
Mike Melanson's avatar
Mike Melanson committed
68
    if ((magic_number != FLIC_FILE_MAGIC_1) &&
69 70
        (magic_number != FLIC_FILE_MAGIC_2) &&
        (magic_number != FLIC_FILE_MAGIC_3))
Mike Melanson's avatar
Mike Melanson committed
71 72
        return 0;

73 74 75 76 77 78 79 80 81 82
    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;


83
    return AVPROBE_SCORE_MAX - 1;
Mike Melanson's avatar
Mike Melanson committed
84 85
}

86
static int flic_read_header(AVFormatContext *s)
Mike Melanson's avatar
Mike Melanson committed
87
{
88
    FlicDemuxContext *flic = s->priv_data;
89
    AVIOContext *pb = s->pb;
Mike Melanson's avatar
Mike Melanson committed
90
    unsigned char header[FLIC_HEADER_SIZE];
91
    AVStream *st, *ast;
Mike Melanson's avatar
Mike Melanson committed
92 93
    int speed;
    int magic_number;
94
    unsigned char preamble[FLIC_PREAMBLE_SIZE];
Mike Melanson's avatar
Mike Melanson committed
95

Mike Melanson's avatar
Mike Melanson committed
96
    flic->frame_number = 0;
Mike Melanson's avatar
Mike Melanson committed
97 98

    /* load the whole header and pull out the width and height */
99
    if (avio_read(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE)
100
        return AVERROR(EIO);
Mike Melanson's avatar
Mike Melanson committed
101

102 103
    magic_number = AV_RL16(&header[4]);
    speed = AV_RL32(&header[0x10]);
Mike Melanson's avatar
Mike Melanson committed
104 105
    if (speed == 0)
        speed = FLIC_DEFAULT_SPEED;
Mike Melanson's avatar
Mike Melanson committed
106 107

    /* initialize the decoder streams */
108
    st = avformat_new_stream(s, NULL);
Mike Melanson's avatar
Mike Melanson committed
109
    if (!st)
110
        return AVERROR(ENOMEM);
Mike Melanson's avatar
Mike Melanson committed
111
    flic->video_stream_index = st->index;
112 113 114 115 116
    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    st->codecpar->codec_id = AV_CODEC_ID_FLIC;
    st->codecpar->codec_tag = 0;  /* no fourcc */
    st->codecpar->width = AV_RL16(&header[0x08]);
    st->codecpar->height = AV_RL16(&header[0x0A]);
Mike Melanson's avatar
Mike Melanson committed
117

118
    if (!st->codecpar->width || !st->codecpar->height) {
119 120 121 122
        /* 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");
123 124
        st->codecpar->width  = 640;
        st->codecpar->height = 480;
125
    }
Mike Melanson's avatar
Mike Melanson committed
126 127

    /* send over the whole 128-byte FLIC header */
128
    if (ff_alloc_extradata(st->codecpar, FLIC_HEADER_SIZE))
129
        return AVERROR(ENOMEM);
130
    memcpy(st->codecpar->extradata, header, FLIC_HEADER_SIZE);
Mike Melanson's avatar
Mike Melanson committed
131

132
    /* peek at the preamble to detect TFTD videos - they seem to always start with an audio chunk */
133
    if (avio_read(pb, preamble, FLIC_PREAMBLE_SIZE) != FLIC_PREAMBLE_SIZE) {
134 135 136
        av_log(s, AV_LOG_ERROR, "Failed to peek at preamble\n");
        return AVERROR(EIO);
    }
Mike Melanson's avatar
Mike Melanson committed
137

138
    avio_seek(pb, -FLIC_PREAMBLE_SIZE, SEEK_CUR);
139 140 141 142 143 144 145 146 147

    /* 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 */
148
        ast = avformat_new_stream(s, NULL);
149 150 151 152 153 154
        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 */
155 156 157 158 159 160 161 162 163 164
        ast->codecpar->block_align = AV_RL32(&preamble[0]);
        ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
        ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
        ast->codecpar->codec_tag = 0;
        ast->codecpar->sample_rate = FLIC_TFTD_SAMPLE_RATE;
        ast->codecpar->channels = 1;
        ast->codecpar->bit_rate = st->codecpar->sample_rate * 8;
        ast->codecpar->bits_per_coded_sample = 8;
        ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
        ast->codecpar->extradata_size = 0;
165 166 167 168

        /* 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 */
169
        avpriv_set_pts_info(st, 64, ast->codecpar->block_align, FLIC_TFTD_SAMPLE_RATE);
170
        avpriv_set_pts_info(ast, 64, 1, FLIC_TFTD_SAMPLE_RATE);
171
    } else if (AV_RL16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) {
172
        avpriv_set_pts_info(st, 64, FLIC_MC_SPEED, 70);
Mike Melanson's avatar
Mike Melanson committed
173 174

        /* rewind the stream since the first chunk is at offset 12 */
175
        avio_seek(pb, 12, SEEK_SET);
Mike Melanson's avatar
Mike Melanson committed
176 177

        /* send over abbreviated FLIC header chunk */
178 179
        av_freep(&st->codecpar->extradata);
        if (ff_alloc_extradata(st->codecpar, 12))
180
            return AVERROR(ENOMEM);
181
        memcpy(st->codecpar->extradata, header, 12);
Mike Melanson's avatar
Mike Melanson committed
182 183

    } else if (magic_number == FLIC_FILE_MAGIC_1) {
184
        avpriv_set_pts_info(st, 64, speed, 70);
185 186
    } else if ((magic_number == FLIC_FILE_MAGIC_2) ||
               (magic_number == FLIC_FILE_MAGIC_3)) {
187
        avpriv_set_pts_info(st, 64, speed, 1000);
188
    } else {
189
        av_log(s, AV_LOG_ERROR, "Invalid or unsupported magic chunk in file\n");
Mike Melanson's avatar
Mike Melanson committed
190
        return AVERROR_INVALIDDATA;
191
    }
Mike Melanson's avatar
Mike Melanson committed
192 193 194 195 196 197 198

    return 0;
}

static int flic_read_packet(AVFormatContext *s,
                            AVPacket *pkt)
{
199
    FlicDemuxContext *flic = s->priv_data;
200
    AVIOContext *pb = s->pb;
Mike Melanson's avatar
Mike Melanson committed
201 202 203 204 205 206
    int packet_read = 0;
    unsigned int size;
    int magic;
    int ret = 0;
    unsigned char preamble[FLIC_PREAMBLE_SIZE];

207
    while (!packet_read && !avio_feof(pb)) {
Mike Melanson's avatar
Mike Melanson committed
208

209
        if ((ret = avio_read(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
Mike Melanson's avatar
Mike Melanson committed
210
            FLIC_PREAMBLE_SIZE) {
211
            ret = AVERROR(EIO);
Mike Melanson's avatar
Mike Melanson committed
212 213 214
            break;
        }

215 216
        size = AV_RL32(&preamble[0]);
        magic = AV_RL16(&preamble[4]);
Mike Melanson's avatar
Mike Melanson committed
217

218
        if (((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) && size > FLIC_PREAMBLE_SIZE) {
Mike Melanson's avatar
Mike Melanson committed
219
            if (av_new_packet(pkt, size)) {
220
                ret = AVERROR(EIO);
Mike Melanson's avatar
Mike Melanson committed
221 222 223
                break;
            }
            pkt->stream_index = flic->video_stream_index;
Mike Melanson's avatar
Mike Melanson committed
224
            pkt->pts = flic->frame_number++;
225
            pkt->pos = avio_tell(pb);
Mike Melanson's avatar
Mike Melanson committed
226
            memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
227
            ret = avio_read(pb, pkt->data + FLIC_PREAMBLE_SIZE,
Mike Melanson's avatar
Mike Melanson committed
228 229
                size - FLIC_PREAMBLE_SIZE);
            if (ret != size - FLIC_PREAMBLE_SIZE) {
230
                av_packet_unref(pkt);
231
                ret = AVERROR(EIO);
Mike Melanson's avatar
Mike Melanson committed
232
            }
233 234 235 236 237 238 239 240
            packet_read = 1;
        } 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) */
241
            avio_skip(pb, 10);
242 243

            pkt->stream_index = flic->audio_stream_index;
244
            pkt->pos = avio_tell(pb);
245
            ret = avio_read(pb, pkt->data, size);
246 247

            if (ret != size) {
248
                av_packet_unref(pkt);
249 250 251
                ret = AVERROR(EIO);
            }

Mike Melanson's avatar
Mike Melanson committed
252 253 254
            packet_read = 1;
        } else {
            /* not interested in this chunk */
255
            avio_skip(pb, size - 6);
Mike Melanson's avatar
Mike Melanson committed
256 257 258
        }
    }

259
    return avio_feof(pb) ? AVERROR_EOF : ret;
Mike Melanson's avatar
Mike Melanson committed
260 261
}

262
AVInputFormat ff_flic_demuxer = {
263
    .name           = "flic",
264
    .long_name      = NULL_IF_CONFIG_SMALL("FLI/FLC/FLX animation"),
265 266 267 268
    .priv_data_size = sizeof(FlicDemuxContext),
    .read_probe     = flic_probe,
    .read_header    = flic_read_header,
    .read_packet    = flic_read_packet,
Mike Melanson's avatar
Mike Melanson committed
269
};