1 /*
2 * Deluxe Paint Animation demuxer
3 * Copyright (c) 2009 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Deluxe Paint Animation demuxer
25 */
26
27 #include "libavutil/intreadwrite.h"
28 #include "avformat.h"
29 #include "demux.h"
30 #include "internal.h"
31
32 typedef struct Page {
33 int base_record;
34 unsigned int nb_records;
35 int size;
36 } Page;
37
38 typedef struct AnmDemuxContext {
39 unsigned int nb_pages; /**< total pages in file */
40 unsigned int nb_records; /**< total records in file */
41 int page_table_offset;
42 #define MAX_PAGES 256 /**< Deluxe Paint hardcoded value */
43 Page pt[MAX_PAGES]; /**< page table */
44 int page; /**< current page (or AVERROR_xxx code) */
45 int record; /**< current record (with in page) */
46 } AnmDemuxContext;
47
48 #define LPF_TAG MKTAG('L','P','F',' ')
49 #define ANIM_TAG MKTAG('A','N','I','M')
50
probe(const AVProbeData * p)51 static int probe(const AVProbeData *p)
52 {
53 /* verify tags and video dimensions */
54 if (AV_RL32(&p->buf[0]) == LPF_TAG &&
55 AV_RL32(&p->buf[16]) == ANIM_TAG &&
56 AV_RL16(&p->buf[20]) && AV_RL16(&p->buf[22]))
57 return AVPROBE_SCORE_MAX;
58 return 0;
59 }
60
61 /**
62 * @return page containing the requested record or AVERROR_XXX
63 */
find_record(const AnmDemuxContext * anm,int record)64 static int find_record(const AnmDemuxContext *anm, int record)
65 {
66 int i;
67
68 if (record >= anm->nb_records)
69 return AVERROR_EOF;
70
71 for (i = 0; i < MAX_PAGES; i++) {
72 const Page *p = &anm->pt[i];
73 if (p->nb_records > 0 && record >= p->base_record && record < p->base_record + p->nb_records)
74 return i;
75 }
76
77 return AVERROR_INVALIDDATA;
78 }
79
read_header(AVFormatContext * s)80 static int read_header(AVFormatContext *s)
81 {
82 AnmDemuxContext *anm = s->priv_data;
83 AVIOContext *pb = s->pb;
84 AVStream *st;
85 int i, ret;
86
87 avio_skip(pb, 4); /* magic number */
88 if (avio_rl16(pb) != MAX_PAGES) {
89 avpriv_request_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES));
90 return AVERROR_PATCHWELCOME;
91 }
92
93 anm->nb_pages = avio_rl16(pb);
94 anm->nb_records = avio_rl32(pb);
95 avio_skip(pb, 2); /* max records per page */
96 anm->page_table_offset = avio_rl16(pb);
97 if (avio_rl32(pb) != ANIM_TAG)
98 return AVERROR_INVALIDDATA;
99
100 /* video stream */
101 st = avformat_new_stream(s, NULL);
102 if (!st)
103 return AVERROR(ENOMEM);
104 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
105 st->codecpar->codec_id = AV_CODEC_ID_ANM;
106 st->codecpar->codec_tag = 0; /* no fourcc */
107 st->codecpar->width = avio_rl16(pb);
108 st->codecpar->height = avio_rl16(pb);
109 if (avio_r8(pb) != 0)
110 goto invalid;
111 avio_skip(pb, 1); /* frame rate multiplier info */
112
113 /* ignore last delta record (used for looping) */
114 if (avio_r8(pb)) /* has_last_delta */
115 anm->nb_records = FFMAX(anm->nb_records - 1, 0);
116
117 avio_skip(pb, 1); /* last_delta_valid */
118
119 if (avio_r8(pb) != 0)
120 goto invalid;
121
122 if (avio_r8(pb) != 1)
123 goto invalid;
124
125 avio_skip(pb, 1); /* other recs per frame */
126
127 if (avio_r8(pb) != 1)
128 goto invalid;
129
130 avio_skip(pb, 32); /* record_types */
131 st->nb_frames = avio_rl32(pb);
132 avpriv_set_pts_info(st, 64, 1, avio_rl16(pb));
133 avio_skip(pb, 58);
134
135 /* color cycling and palette data */
136 ret = ff_get_extradata(s, st->codecpar, s->pb, 16*8 + 4*256);
137 if (ret < 0)
138 return ret;
139
140 /* read page table */
141 ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
142 if (ret < 0)
143 return ret;
144
145 for (i = 0; i < MAX_PAGES; i++) {
146 Page *p = &anm->pt[i];
147 p->base_record = avio_rl16(pb);
148 p->nb_records = avio_rl16(pb);
149 p->size = avio_rl16(pb);
150 }
151
152 /* find page of first frame */
153 anm->page = find_record(anm, 0);
154 if (anm->page < 0) {
155 return anm->page;
156 }
157
158 anm->record = -1;
159 return 0;
160
161 invalid:
162 avpriv_request_sample(s, "Invalid header element");
163 return AVERROR_PATCHWELCOME;
164 }
165
read_packet(AVFormatContext * s,AVPacket * pkt)166 static int read_packet(AVFormatContext *s,
167 AVPacket *pkt)
168 {
169 AnmDemuxContext *anm = s->priv_data;
170 AVIOContext *pb = s->pb;
171 Page *p;
172 int tmp, record_size;
173
174 if (avio_feof(s->pb))
175 return AVERROR(EIO);
176
177 if (anm->page < 0)
178 return anm->page;
179
180 repeat:
181 p = &anm->pt[anm->page];
182
183 /* parse page header */
184 if (anm->record < 0) {
185 avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
186 avio_skip(pb, 8 + 2*p->nb_records);
187 anm->record = 0;
188 }
189
190 /* if we have fetched all records in this page, then find the
191 next page and repeat */
192 if (anm->record >= p->nb_records) {
193 anm->page = find_record(anm, p->base_record + p->nb_records);
194 if (anm->page < 0)
195 return anm->page;
196 anm->record = -1;
197 goto repeat;
198 }
199
200 /* fetch record size */
201 tmp = avio_tell(pb);
202 avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
203 8 + anm->record * 2, SEEK_SET);
204 record_size = avio_rl16(pb);
205 avio_seek(pb, tmp, SEEK_SET);
206
207 /* fetch record */
208 pkt->size = av_get_packet(s->pb, pkt, record_size);
209 if (pkt->size < 0)
210 return pkt->size;
211 if (p->base_record + anm->record == 0)
212 pkt->flags |= AV_PKT_FLAG_KEY;
213
214 anm->record++;
215 return 0;
216 }
217
218 const AVInputFormat ff_anm_demuxer = {
219 .name = "anm",
220 .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
221 .priv_data_size = sizeof(AnmDemuxContext),
222 .read_probe = probe,
223 .read_header = read_header,
224 .read_packet = read_packet,
225 };
226