• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/flic.c

Go to the documentation of this file.
00001 /*
00002  * FLI/FLC Animation File Demuxer
00003  * Copyright (c) 2003 The ffmpeg Project
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00034 #include "libavutil/intreadwrite.h"
00035 #include "libavutil/audioconvert.h"
00036 #include "avformat.h"
00037 #include "internal.h"
00038 
00039 #define FLIC_FILE_MAGIC_1 0xAF11
00040 #define FLIC_FILE_MAGIC_2 0xAF12
00041 #define FLIC_FILE_MAGIC_3 0xAF44  /* Flic Type for Extended FLX Format which
00042                                      originated in Dave's Targa Animator (DTA) */
00043 #define FLIC_CHUNK_MAGIC_1 0xF1FA
00044 #define FLIC_CHUNK_MAGIC_2 0xF5FA
00045 #define FLIC_MC_SPEED 5  /* speed for Magic Carpet game FLIs */
00046 #define FLIC_DEFAULT_SPEED 5  /* for FLIs that have 0 speed */
00047 #define FLIC_TFTD_CHUNK_AUDIO 0xAAAA /* Audio chunk. Used in Terror from the Deep.
00048                                         Has 10 B extra header not accounted for in the chunk header */
00049 #define FLIC_TFTD_SAMPLE_RATE 22050
00050 
00051 #define FLIC_HEADER_SIZE 128
00052 #define FLIC_PREAMBLE_SIZE 6
00053 
00054 typedef struct FlicDemuxContext {
00055     int video_stream_index;
00056     int audio_stream_index;
00057     int frame_number;
00058 } FlicDemuxContext;
00059 
00060 static int flic_probe(AVProbeData *p)
00061 {
00062     int magic_number;
00063 
00064     if(p->buf_size < FLIC_HEADER_SIZE)
00065         return 0;
00066 
00067     magic_number = AV_RL16(&p->buf[4]);
00068     if ((magic_number != FLIC_FILE_MAGIC_1) &&
00069         (magic_number != FLIC_FILE_MAGIC_2) &&
00070         (magic_number != FLIC_FILE_MAGIC_3))
00071         return 0;
00072 
00073     if(AV_RL16(&p->buf[0x10]) != FLIC_CHUNK_MAGIC_1){
00074         if(AV_RL32(&p->buf[0x10]) > 2000)
00075             return 0;
00076     }
00077 
00078     if(   AV_RL16(&p->buf[0x08]) > 4096
00079        || AV_RL16(&p->buf[0x0A]) > 4096)
00080         return 0;
00081 
00082 
00083     return AVPROBE_SCORE_MAX;
00084 }
00085 
00086 static int flic_read_header(AVFormatContext *s,
00087                             AVFormatParameters *ap)
00088 {
00089     FlicDemuxContext *flic = s->priv_data;
00090     AVIOContext *pb = s->pb;
00091     unsigned char header[FLIC_HEADER_SIZE];
00092     AVStream *st, *ast;
00093     int speed;
00094     int magic_number;
00095     unsigned char preamble[FLIC_PREAMBLE_SIZE];
00096 
00097     flic->frame_number = 0;
00098 
00099     /* load the whole header and pull out the width and height */
00100     if (avio_read(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE)
00101         return AVERROR(EIO);
00102 
00103     magic_number = AV_RL16(&header[4]);
00104     speed = AV_RL32(&header[0x10]);
00105     if (speed == 0)
00106         speed = FLIC_DEFAULT_SPEED;
00107 
00108     /* initialize the decoder streams */
00109     st = avformat_new_stream(s, NULL);
00110     if (!st)
00111         return AVERROR(ENOMEM);
00112     flic->video_stream_index = st->index;
00113     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00114     st->codec->codec_id = CODEC_ID_FLIC;
00115     st->codec->codec_tag = 0;  /* no fourcc */
00116     st->codec->width = AV_RL16(&header[0x08]);
00117     st->codec->height = AV_RL16(&header[0x0A]);
00118 
00119     if (!st->codec->width || !st->codec->height) {
00120         /* Ugly hack needed for the following sample: */
00121         /* http://samples.libav.org/fli-flc/fli-bugs/specular.flc */
00122         av_log(s, AV_LOG_WARNING,
00123                "File with no specified width/height. Trying 640x480.\n");
00124         st->codec->width  = 640;
00125         st->codec->height = 480;
00126     }
00127 
00128     /* send over the whole 128-byte FLIC header */
00129     st->codec->extradata_size = FLIC_HEADER_SIZE;
00130     st->codec->extradata = av_malloc(FLIC_HEADER_SIZE);
00131     memcpy(st->codec->extradata, header, FLIC_HEADER_SIZE);
00132 
00133     /* peek at the preamble to detect TFTD videos - they seem to always start with an audio chunk */
00134     if (avio_read(pb, preamble, FLIC_PREAMBLE_SIZE) != FLIC_PREAMBLE_SIZE) {
00135         av_log(s, AV_LOG_ERROR, "Failed to peek at preamble\n");
00136         return AVERROR(EIO);
00137     }
00138 
00139     avio_seek(pb, -FLIC_PREAMBLE_SIZE, SEEK_CUR);
00140 
00141     /* Time to figure out the framerate:
00142      * If the first preamble's magic number is 0xAAAA then this file is from
00143      * X-COM: Terror from the Deep. If on the other hand there is a FLIC chunk
00144      * magic number at offset 0x10 assume this file is from Magic Carpet instead.
00145      * If neither of the above is true then this is a normal FLIC file.
00146      */
00147     if (AV_RL16(&preamble[4]) == FLIC_TFTD_CHUNK_AUDIO) {
00148         /* TFTD videos have an extra 22050 Hz 8-bit mono audio stream */
00149         ast = avformat_new_stream(s, NULL);
00150         if (!ast)
00151             return AVERROR(ENOMEM);
00152 
00153         flic->audio_stream_index = ast->index;
00154 
00155         /* all audio frames are the same size, so use the size of the first chunk for block_align */
00156         ast->codec->block_align = AV_RL32(&preamble[0]);
00157         ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00158         ast->codec->codec_id = CODEC_ID_PCM_U8;
00159         ast->codec->codec_tag = 0;
00160         ast->codec->sample_rate = FLIC_TFTD_SAMPLE_RATE;
00161         ast->codec->channels = 1;
00162         ast->codec->sample_fmt = AV_SAMPLE_FMT_U8;
00163         ast->codec->bit_rate = st->codec->sample_rate * 8;
00164         ast->codec->bits_per_coded_sample = 8;
00165         ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
00166         ast->codec->extradata_size = 0;
00167 
00168         /* Since the header information is incorrect we have to figure out the
00169          * framerate using block_align and the fact that the audio is 22050 Hz.
00170          * We usually have two cases: 2205 -> 10 fps and 1470 -> 15 fps */
00171         avpriv_set_pts_info(st, 64, ast->codec->block_align, FLIC_TFTD_SAMPLE_RATE);
00172         avpriv_set_pts_info(ast, 64, 1, FLIC_TFTD_SAMPLE_RATE);
00173     } else if (AV_RL16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) {
00174         avpriv_set_pts_info(st, 64, FLIC_MC_SPEED, 70);
00175 
00176         /* rewind the stream since the first chunk is at offset 12 */
00177         avio_seek(pb, 12, SEEK_SET);
00178 
00179         /* send over abbreviated FLIC header chunk */
00180         av_free(st->codec->extradata);
00181         st->codec->extradata_size = 12;
00182         st->codec->extradata = av_malloc(12);
00183         memcpy(st->codec->extradata, header, 12);
00184 
00185     } else if (magic_number == FLIC_FILE_MAGIC_1) {
00186         avpriv_set_pts_info(st, 64, speed, 70);
00187     } else if ((magic_number == FLIC_FILE_MAGIC_2) ||
00188                (magic_number == FLIC_FILE_MAGIC_3)) {
00189         avpriv_set_pts_info(st, 64, speed, 1000);
00190     } else {
00191         av_log(s, AV_LOG_INFO, "Invalid or unsupported magic chunk in file\n");
00192         return AVERROR_INVALIDDATA;
00193     }
00194 
00195     return 0;
00196 }
00197 
00198 static int flic_read_packet(AVFormatContext *s,
00199                             AVPacket *pkt)
00200 {
00201     FlicDemuxContext *flic = s->priv_data;
00202     AVIOContext *pb = s->pb;
00203     int packet_read = 0;
00204     unsigned int size;
00205     int magic;
00206     int ret = 0;
00207     unsigned char preamble[FLIC_PREAMBLE_SIZE];
00208 
00209     while (!packet_read) {
00210 
00211         if ((ret = avio_read(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
00212             FLIC_PREAMBLE_SIZE) {
00213             ret = AVERROR(EIO);
00214             break;
00215         }
00216 
00217         size = AV_RL32(&preamble[0]);
00218         magic = AV_RL16(&preamble[4]);
00219 
00220         if (((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) && size > FLIC_PREAMBLE_SIZE) {
00221             if (av_new_packet(pkt, size)) {
00222                 ret = AVERROR(EIO);
00223                 break;
00224             }
00225             pkt->stream_index = flic->video_stream_index;
00226             pkt->pts = flic->frame_number++;
00227             pkt->pos = avio_tell(pb);
00228             memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
00229             ret = avio_read(pb, pkt->data + FLIC_PREAMBLE_SIZE,
00230                 size - FLIC_PREAMBLE_SIZE);
00231             if (ret != size - FLIC_PREAMBLE_SIZE) {
00232                 av_free_packet(pkt);
00233                 ret = AVERROR(EIO);
00234             }
00235             packet_read = 1;
00236         } else if (magic == FLIC_TFTD_CHUNK_AUDIO) {
00237             if (av_new_packet(pkt, size)) {
00238                 ret = AVERROR(EIO);
00239                 break;
00240             }
00241 
00242             /* skip useless 10B sub-header (yes, it's not accounted for in the chunk header) */
00243             avio_skip(pb, 10);
00244 
00245             pkt->stream_index = flic->audio_stream_index;
00246             pkt->pos = avio_tell(pb);
00247             ret = avio_read(pb, pkt->data, size);
00248 
00249             if (ret != size) {
00250                 av_free_packet(pkt);
00251                 ret = AVERROR(EIO);
00252             }
00253 
00254             packet_read = 1;
00255         } else {
00256             /* not interested in this chunk */
00257             avio_skip(pb, size - 6);
00258         }
00259     }
00260 
00261     return ret;
00262 }
00263 
00264 AVInputFormat ff_flic_demuxer = {
00265     .name           = "flic",
00266     .long_name      = NULL_IF_CONFIG_SMALL("FLI/FLC/FLX animation format"),
00267     .priv_data_size = sizeof(FlicDemuxContext),
00268     .read_probe     = flic_probe,
00269     .read_header    = flic_read_header,
00270     .read_packet    = flic_read_packet,
00271 };
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1