flic.c
Go to the documentation of this file.
1 /*
2  * FLI/FLC Animation File Demuxer
3  * Copyright (c) 2003 The ffmpeg Project
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/audioconvert.h"
36 #include "avformat.h"
37 #include "internal.h"
38 
39 #define FLIC_FILE_MAGIC_1 0xAF11
40 #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) */
43 #define FLIC_CHUNK_MAGIC_1 0xF1FA
44 #define FLIC_CHUNK_MAGIC_2 0xF5FA
45 #define FLIC_MC_SPEED 5 /* speed for Magic Carpet game FLIs */
46 #define FLIC_DEFAULT_SPEED 5 /* for FLIs that have 0 speed */
47 #define FLIC_TFTD_CHUNK_AUDIO 0xAAAA /* Audio chunk. Used in Terror from the Deep.
48  Has 10 B extra header not accounted for in the chunk header */
49 #define FLIC_TFTD_SAMPLE_RATE 22050
50 
51 #define FLIC_HEADER_SIZE 128
52 #define FLIC_PREAMBLE_SIZE 6
53 
54 typedef struct FlicDemuxContext {
57  int frame_number;
59 
60 static int flic_probe(AVProbeData *p)
61 {
62  int magic_number;
63 
64  if(p->buf_size < FLIC_HEADER_SIZE)
65  return 0;
66 
67  magic_number = AV_RL16(&p->buf[4]);
68  if ((magic_number != FLIC_FILE_MAGIC_1) &&
69  (magic_number != FLIC_FILE_MAGIC_2) &&
70  (magic_number != FLIC_FILE_MAGIC_3))
71  return 0;
72 
73  if(AV_RL16(&p->buf[0x10]) != FLIC_CHUNK_MAGIC_1){
74  if(AV_RL32(&p->buf[0x10]) > 2000)
75  return 0;
76  }
77 
78  if( AV_RL16(&p->buf[0x08]) > 4096
79  || AV_RL16(&p->buf[0x0A]) > 4096)
80  return 0;
81 
82 
83  return AVPROBE_SCORE_MAX;
84 }
85 
86 static int flic_read_header(AVFormatContext *s,
88 {
89  FlicDemuxContext *flic = s->priv_data;
90  AVIOContext *pb = s->pb;
91  unsigned char header[FLIC_HEADER_SIZE];
92  AVStream *st, *ast;
93  int speed;
94  int magic_number;
95  unsigned char preamble[FLIC_PREAMBLE_SIZE];
96 
97  flic->frame_number = 0;
98 
99  /* load the whole header and pull out the width and height */
100  if (avio_read(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE)
101  return AVERROR(EIO);
102 
103  magic_number = AV_RL16(&header[4]);
104  speed = AV_RL32(&header[0x10]);
105  if (speed == 0)
106  speed = FLIC_DEFAULT_SPEED;
107 
108  /* initialize the decoder streams */
109  st = avformat_new_stream(s, NULL);
110  if (!st)
111  return AVERROR(ENOMEM);
112  flic->video_stream_index = st->index;
115  st->codec->codec_tag = 0; /* no fourcc */
116  st->codec->width = AV_RL16(&header[0x08]);
117  st->codec->height = AV_RL16(&header[0x0A]);
118 
119  if (!st->codec->width || !st->codec->height) {
120  /* Ugly hack needed for the following sample: */
121  /* http://samples.libav.org/fli-flc/fli-bugs/specular.flc */
123  "File with no specified width/height. Trying 640x480.\n");
124  st->codec->width = 640;
125  st->codec->height = 480;
126  }
127 
128  /* send over the whole 128-byte FLIC header */
131  memcpy(st->codec->extradata, header, FLIC_HEADER_SIZE);
132 
133  /* peek at the preamble to detect TFTD videos - they seem to always start with an audio chunk */
134  if (avio_read(pb, preamble, FLIC_PREAMBLE_SIZE) != FLIC_PREAMBLE_SIZE) {
135  av_log(s, AV_LOG_ERROR, "Failed to peek at preamble\n");
136  return AVERROR(EIO);
137  }
138 
139  avio_seek(pb, -FLIC_PREAMBLE_SIZE, SEEK_CUR);
140 
141  /* Time to figure out the framerate:
142  * If the first preamble's magic number is 0xAAAA then this file is from
143  * X-COM: Terror from the Deep. If on the other hand there is a FLIC chunk
144  * magic number at offset 0x10 assume this file is from Magic Carpet instead.
145  * If neither of the above is true then this is a normal FLIC file.
146  */
147  if (AV_RL16(&preamble[4]) == FLIC_TFTD_CHUNK_AUDIO) {
148  /* TFTD videos have an extra 22050 Hz 8-bit mono audio stream */
149  ast = avformat_new_stream(s, NULL);
150  if (!ast)
151  return AVERROR(ENOMEM);
152 
153  flic->audio_stream_index = ast->index;
154 
155  /* all audio frames are the same size, so use the size of the first chunk for block_align */
156  ast->codec->block_align = AV_RL32(&preamble[0]);
159  ast->codec->codec_tag = 0;
161  ast->codec->channels = 1;
163  ast->codec->bit_rate = st->codec->sample_rate * 8;
164  ast->codec->bits_per_coded_sample = 8;
166  ast->codec->extradata_size = 0;
167 
168  /* Since the header information is incorrect we have to figure out the
169  * framerate using block_align and the fact that the audio is 22050 Hz.
170  * We usually have two cases: 2205 -> 10 fps and 1470 -> 15 fps */
173  } else if (AV_RL16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) {
174  avpriv_set_pts_info(st, 64, FLIC_MC_SPEED, 70);
175 
176  /* rewind the stream since the first chunk is at offset 12 */
177  avio_seek(pb, 12, SEEK_SET);
178 
179  /* send over abbreviated FLIC header chunk */
180  av_free(st->codec->extradata);
181  st->codec->extradata_size = 12;
182  st->codec->extradata = av_malloc(12);
183  memcpy(st->codec->extradata, header, 12);
184 
185  } else if (magic_number == FLIC_FILE_MAGIC_1) {
186  avpriv_set_pts_info(st, 64, speed, 70);
187  } else if ((magic_number == FLIC_FILE_MAGIC_2) ||
188  (magic_number == FLIC_FILE_MAGIC_3)) {
189  avpriv_set_pts_info(st, 64, speed, 1000);
190  } else {
191  av_log(s, AV_LOG_INFO, "Invalid or unsupported magic chunk in file\n");
192  return AVERROR_INVALIDDATA;
193  }
194 
195  return 0;
196 }
197 
198 static int flic_read_packet(AVFormatContext *s,
199  AVPacket *pkt)
200 {
201  FlicDemuxContext *flic = s->priv_data;
202  AVIOContext *pb = s->pb;
203  int packet_read = 0;
204  unsigned int size;
205  int magic;
206  int ret = 0;
207  unsigned char preamble[FLIC_PREAMBLE_SIZE];
208 
209  while (!packet_read) {
210 
211  if ((ret = avio_read(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
213  ret = AVERROR(EIO);
214  break;
215  }
216 
217  size = AV_RL32(&preamble[0]);
218  magic = AV_RL16(&preamble[4]);
219 
220  if (((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) && size > FLIC_PREAMBLE_SIZE) {
221  if (av_new_packet(pkt, size)) {
222  ret = AVERROR(EIO);
223  break;
224  }
225  pkt->stream_index = flic->video_stream_index;
226  pkt->pts = flic->frame_number++;
227  pkt->pos = avio_tell(pb);
228  memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
229  ret = avio_read(pb, pkt->data + FLIC_PREAMBLE_SIZE,
230  size - FLIC_PREAMBLE_SIZE);
231  if (ret != size - FLIC_PREAMBLE_SIZE) {
232  av_free_packet(pkt);
233  ret = AVERROR(EIO);
234  }
235  packet_read = 1;
236  } else if (magic == FLIC_TFTD_CHUNK_AUDIO) {
237  if (av_new_packet(pkt, size)) {
238  ret = AVERROR(EIO);
239  break;
240  }
241 
242  /* skip useless 10B sub-header (yes, it's not accounted for in the chunk header) */
243  avio_skip(pb, 10);
244 
245  pkt->stream_index = flic->audio_stream_index;
246  pkt->pos = avio_tell(pb);
247  ret = avio_read(pb, pkt->data, size);
248 
249  if (ret != size) {
250  av_free_packet(pkt);
251  ret = AVERROR(EIO);
252  }
253 
254  packet_read = 1;
255  } else {
256  /* not interested in this chunk */
257  avio_skip(pb, size - 6);
258  }
259  }
260 
261  return ret;
262 }
263 
265  .name = "flic",
266  .long_name = NULL_IF_CONFIG_SMALL("FLI/FLC/FLX animation format"),
267  .priv_data_size = sizeof(FlicDemuxContext),
271 };