]> git.sesse.net Git - ffmpeg/blob - libavformat/flic.c
Update licensing information: The FSF changed postal address.
[ffmpeg] / libavformat / flic.c
1 /*
2  * FLI/FLC Animation File Demuxer
3  * Copyright (c) 2003 The ffmpeg Project
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 /**
21  * @file flic.c
22  * FLI/FLC file demuxer
23  * by Mike Melanson (melanson@pcisys.net)
24  * for more information on the .fli/.flc file format and all of its many
25  * variations, visit:
26  *   http://www.compuphase.com/flic.htm
27  *
28  * This demuxer handles standard 0xAF11- and 0xAF12-type FLIs. It also
29  * handles special FLIs from the PC game "Magic Carpet".
30  */
31
32 #include "avformat.h"
33
34 #define FLIC_FILE_MAGIC_1 0xAF11
35 #define FLIC_FILE_MAGIC_2 0xAF12
36 #define FLIC_FILE_MAGIC_3 0xAF44  /* Flic Type for Extended FLX Format which
37                                      originated in Dave's Targa Animator (DTA) */
38 #define FLIC_CHUNK_MAGIC_1 0xF1FA
39 #define FLIC_CHUNK_MAGIC_2 0xF5FA
40 #define FLIC_MC_PTS_INC 6000  /* pts increment for Magic Carpet game FLIs */
41 #define FLIC_DEFAULT_PTS_INC 6000  /* for FLIs that have 0 speed */
42
43 #define FLIC_HEADER_SIZE 128
44 #define FLIC_PREAMBLE_SIZE 6
45
46 typedef struct FlicDemuxContext {
47     int frame_pts_inc;
48     int64_t pts;
49     int video_stream_index;
50 } FlicDemuxContext;
51
52 static int flic_probe(AVProbeData *p)
53 {
54     int magic_number;
55
56     if (p->buf_size < 6)
57         return 0;
58
59     magic_number = LE_16(&p->buf[4]);
60     if ((magic_number != FLIC_FILE_MAGIC_1) &&
61         (magic_number != FLIC_FILE_MAGIC_2) &&
62         (magic_number != FLIC_FILE_MAGIC_3))
63         return 0;
64
65     return AVPROBE_SCORE_MAX;
66 }
67
68 static int flic_read_header(AVFormatContext *s,
69                             AVFormatParameters *ap)
70 {
71     FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
72     ByteIOContext *pb = &s->pb;
73     unsigned char header[FLIC_HEADER_SIZE];
74     AVStream *st;
75     int speed;
76     int magic_number;
77
78     flic->pts = 0;
79
80     /* load the whole header and pull out the width and height */
81     if (get_buffer(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE)
82         return AVERROR_IO;
83
84     magic_number = LE_16(&header[4]);
85     speed = LE_32(&header[0x10]);
86
87     /* initialize the decoder streams */
88     st = av_new_stream(s, 0);
89     if (!st)
90         return AVERROR_NOMEM;
91     flic->video_stream_index = st->index;
92     st->codec->codec_type = CODEC_TYPE_VIDEO;
93     st->codec->codec_id = CODEC_ID_FLIC;
94     st->codec->codec_tag = 0;  /* no fourcc */
95     st->codec->width = LE_16(&header[0x08]);
96     st->codec->height = LE_16(&header[0x0A]);
97
98     if (!st->codec->width || !st->codec->height)
99         return AVERROR_INVALIDDATA;
100
101     /* send over the whole 128-byte FLIC header */
102     st->codec->extradata_size = FLIC_HEADER_SIZE;
103     st->codec->extradata = av_malloc(FLIC_HEADER_SIZE);
104     memcpy(st->codec->extradata, header, FLIC_HEADER_SIZE);
105
106     av_set_pts_info(st, 33, 1, 90000);
107
108     /* Time to figure out the framerate: If there is a FLIC chunk magic
109      * number at offset 0x10, assume this is from the Bullfrog game,
110      * Magic Carpet. */
111     if (LE_16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) {
112
113         flic->frame_pts_inc = FLIC_MC_PTS_INC;
114
115         /* rewind the stream since the first chunk is at offset 12 */
116         url_fseek(pb, 12, SEEK_SET);
117
118         /* send over abbreviated FLIC header chunk */
119         av_free(st->codec->extradata);
120         st->codec->extradata_size = 12;
121         st->codec->extradata = av_malloc(12);
122         memcpy(st->codec->extradata, header, 12);
123
124     } else if (magic_number == FLIC_FILE_MAGIC_1) {
125         /*
126          * in this case, the speed (n) is number of 1/70s ticks between frames:
127          *
128          *    pts        n * frame #
129          *  --------  =  -----------  => pts = n * (90000/70) * frame #
130          *   90000           70
131          *
132          *  therefore, the frame pts increment = n * 1285.7
133          */
134         flic->frame_pts_inc = speed * 1285.7;
135     } else if ((magic_number == FLIC_FILE_MAGIC_2) ||
136                (magic_number == FLIC_FILE_MAGIC_3)) {
137         /*
138          * in this case, the speed (n) is number of milliseconds between frames:
139          *
140          *    pts        n * frame #
141          *  --------  =  -----------  => pts = n * 90 * frame #
142          *   90000          1000
143          *
144          *  therefore, the frame pts increment = n * 90
145          */
146         flic->frame_pts_inc = speed * 90;
147     } else
148         return AVERROR_INVALIDDATA;
149
150     if (flic->frame_pts_inc == 0)
151         flic->frame_pts_inc = FLIC_DEFAULT_PTS_INC;
152
153     return 0;
154 }
155
156 static int flic_read_packet(AVFormatContext *s,
157                             AVPacket *pkt)
158 {
159     FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
160     ByteIOContext *pb = &s->pb;
161     int packet_read = 0;
162     unsigned int size;
163     int magic;
164     int ret = 0;
165     unsigned char preamble[FLIC_PREAMBLE_SIZE];
166
167     while (!packet_read) {
168
169         if ((ret = get_buffer(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
170             FLIC_PREAMBLE_SIZE) {
171             ret = AVERROR_IO;
172             break;
173         }
174
175         size = LE_32(&preamble[0]);
176         magic = LE_16(&preamble[4]);
177
178         if (((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) && size > FLIC_PREAMBLE_SIZE) {
179             if (av_new_packet(pkt, size)) {
180                 ret = AVERROR_IO;
181                 break;
182             }
183             pkt->stream_index = flic->video_stream_index;
184             pkt->pts = flic->pts;
185             pkt->pos = url_ftell(pb);
186             memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
187             ret = get_buffer(pb, pkt->data + FLIC_PREAMBLE_SIZE,
188                 size - FLIC_PREAMBLE_SIZE);
189             if (ret != size - FLIC_PREAMBLE_SIZE) {
190                 av_free_packet(pkt);
191                 ret = AVERROR_IO;
192             }
193             flic->pts += flic->frame_pts_inc;
194             packet_read = 1;
195         } else {
196             /* not interested in this chunk */
197             url_fseek(pb, size - 6, SEEK_CUR);
198         }
199     }
200
201     return ret;
202 }
203
204 static int flic_read_close(AVFormatContext *s)
205 {
206 //    FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
207
208     return 0;
209 }
210
211 static AVInputFormat flic_iformat = {
212     "flic",
213     "FLI/FLC/FLX animation format",
214     sizeof(FlicDemuxContext),
215     flic_probe,
216     flic_read_header,
217     flic_read_packet,
218     flic_read_close,
219 };
220
221 int flic_init(void)
222 {
223     av_register_input_format(&flic_iformat);
224     return 0;
225 }