Browse code

Deluxe Paint Animation demuxer

Originally committed as revision 21118 to svn://svn.ffmpeg.org/ffmpeg/trunk

Peter Ross authored on 2010/01/10 14:47:50
Showing 5 changed files
... ...
@@ -47,6 +47,7 @@ version <next>:
47 47
 - CDG demuxer and decoder
48 48
 - R210 decoder
49 49
 - Auravision Aura 1 and 2 decoders
50
+- Deluxe Paint Animation playback system
50 51
 
51 52
 
52 53
 
... ...
@@ -73,6 +73,7 @@ library:
73 73
 @item CRYO APC                  @tab   @tab X
74 74
     @tab Audio format used in some games by CRYO Interactive Entertainment.
75 75
 @item D-Cinema audio            @tab X @tab X
76
+@item Deluxe Paint Animation    @tab   @tab X
76 77
 @item DV video                  @tab X @tab X
77 78
 @item DXA                       @tab   @tab X
78 79
     @tab This format is used in the non-Windows version of the Feeble Files
... ...
@@ -25,6 +25,7 @@ OBJS-$(CONFIG_AIFF_DEMUXER)              += aiffdec.o riff.o raw.o
25 25
 OBJS-$(CONFIG_AIFF_MUXER)                += aiffenc.o riff.o
26 26
 OBJS-$(CONFIG_AMR_DEMUXER)               += amr.o
27 27
 OBJS-$(CONFIG_AMR_MUXER)                 += amr.o
28
+OBJS-$(CONFIG_ANM_DEMUXER)               += anm.o
28 29
 OBJS-$(CONFIG_APC_DEMUXER)               += apc.o
29 30
 OBJS-$(CONFIG_APE_DEMUXER)               += ape.o apetag.o
30 31
 OBJS-$(CONFIG_ASF_DEMUXER)               += asfdec.o asf.o asfcrypt.o \
... ...
@@ -53,6 +53,7 @@ void av_register_all(void)
53 53
     REGISTER_DEMUXER  (AEA, aea);
54 54
     REGISTER_MUXDEMUX (AIFF, aiff);
55 55
     REGISTER_MUXDEMUX (AMR, amr);
56
+    REGISTER_DEMUXER  (ANM, anm);
56 57
     REGISTER_DEMUXER  (APC, apc);
57 58
     REGISTER_DEMUXER  (APE, ape);
58 59
     REGISTER_MUXDEMUX (ASF, asf);
59 60
new file mode 100644
... ...
@@ -0,0 +1,235 @@
0
+/*
1
+ * Deluxe Paint Animation demuxer
2
+ * Copyright (c) 2009 Peter Ross
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file libavformat/anm.c
23
+ * Deluxe Paint Animation demuxer
24
+ */
25
+
26
+#include "libavutil/intreadwrite.h"
27
+#include "avformat.h"
28
+
29
+typedef struct {
30
+    int base_record;
31
+    unsigned int nb_records;
32
+    int size;
33
+} Page;
34
+
35
+typedef struct {
36
+    unsigned int nb_pages;    /** total pages in file */
37
+    unsigned int nb_records;  /** total records in file */
38
+    int page_table_offset;
39
+#define MAX_PAGES  256        /** Deluxe Paint hardcoded value */
40
+    Page pt[MAX_PAGES];       /** page table */
41
+    int page;                 /** current page */
42
+    int record;               /** current record (with in page) */
43
+} AnmDemuxContext;
44
+
45
+#define LPF_TAG  MKTAG('L','P','F',' ')
46
+#define ANIM_TAG MKTAG('A','N','I','M')
47
+
48
+static int probe(AVProbeData *p)
49
+{
50
+    /* verify tags and video dimensions */
51
+    if (AV_RL32(&p->buf[0])  == LPF_TAG &&
52
+        AV_RL32(&p->buf[16]) == ANIM_TAG &&
53
+        AV_RL16(&p->buf[20]) && AV_RL16(&p->buf[22]))
54
+        return AVPROBE_SCORE_MAX;
55
+    return 0;
56
+}
57
+
58
+/**
59
+ * @return page containing the requested record or AVERROR_XXX
60
+ */
61
+static int find_record(const AnmDemuxContext *anm, int record)
62
+{
63
+    int i;
64
+
65
+    if (record >= anm->nb_records)
66
+        return AVERROR_EOF;
67
+
68
+    for (i = 0; i < MAX_PAGES; i++) {
69
+        const Page *p = &anm->pt[i];
70
+        if (p->nb_records > 0 && record >= p->base_record && record < p->base_record + p->nb_records)
71
+            return i;
72
+    }
73
+
74
+    return AVERROR_INVALIDDATA;
75
+}
76
+
77
+static int read_header(AVFormatContext *s,
78
+                       AVFormatParameters *ap)
79
+{
80
+    AnmDemuxContext *anm = s->priv_data;
81
+    ByteIOContext *pb = s->pb;
82
+    AVStream *st;
83
+    int i, ret;
84
+
85
+    url_fskip(pb, 4); /* magic number */
86
+    if (get_le16(pb) != MAX_PAGES) {
87
+        av_log_ask_for_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES) "\n");
88
+        return AVERROR_INVALIDDATA;
89
+    }
90
+
91
+    anm->nb_pages   = get_le16(pb);
92
+    anm->nb_records = get_le32(pb);
93
+    url_fskip(pb, 2); /* max records per page */
94
+    anm->page_table_offset = get_le16(pb);
95
+    if (get_le32(pb) != ANIM_TAG)
96
+        return AVERROR_INVALIDDATA;
97
+
98
+    /* video stream */
99
+    st = av_new_stream(s, 0);
100
+    if (!st)
101
+        return AVERROR(ENOMEM);
102
+    st->codec->codec_type = CODEC_TYPE_VIDEO;
103
+    st->codec->codec_id   = CODEC_ID_ANM;
104
+    st->codec->codec_tag  = 0; /* no fourcc */
105
+    st->codec->width      = get_le16(pb);
106
+    st->codec->height     = get_le16(pb);
107
+    if (get_byte(pb) != 0)
108
+        goto invalid;
109
+    url_fskip(pb, 1); /* frame rate multiplier info */
110
+
111
+    /* ignore last delta record (used for looping) */
112
+    if (get_byte(pb))  /* has_last_delta */
113
+        anm->nb_records = FFMAX(anm->nb_records - 1, 0);
114
+
115
+    url_fskip(pb, 1); /* last_delta_valid */
116
+
117
+    if (get_byte(pb) != 0)
118
+        goto invalid;
119
+
120
+    if (get_byte(pb) != 1)
121
+        goto invalid;
122
+
123
+    url_fskip(pb, 1); /* other recs per frame */
124
+
125
+    if (get_byte(pb) != 1)
126
+        goto invalid;
127
+
128
+    url_fskip(pb, 32); /* record_types */
129
+    st->nb_frames = get_le32(pb);
130
+    av_set_pts_info(st, 64, 1, get_le16(pb));
131
+    url_fskip(pb, 58);
132
+
133
+    /* color cycling and palette data */
134
+    st->codec->extradata_size = 16*8 + 4*256;
135
+    st->codec->extradata      = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
136
+    if (!st->codec->extradata) {
137
+        ret = AVERROR(ENOMEM);
138
+        goto close_and_return;
139
+    }
140
+    ret = get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
141
+    if (ret < 0)
142
+        goto close_and_return;
143
+
144
+    /* read page table */
145
+    ret = url_fseek(pb, anm->page_table_offset, SEEK_SET);
146
+    if (ret < 0)
147
+        goto close_and_return;
148
+
149
+    for (i = 0; i < MAX_PAGES; i++) {
150
+        Page *p = &anm->pt[i];
151
+        p->base_record = get_le16(pb);
152
+        p->nb_records  = get_le16(pb);
153
+        p->size        = get_le16(pb);
154
+    }
155
+
156
+    /* find page of first frame */
157
+    anm->page = find_record(anm, 0);
158
+    if (anm->page < 0) {
159
+        ret = anm->page;
160
+        goto close_and_return;
161
+    }
162
+
163
+    anm->record = -1;
164
+    return 0;
165
+
166
+invalid:
167
+    av_log_ask_for_sample(s, NULL);
168
+    ret = AVERROR_INVALIDDATA;
169
+
170
+close_and_return:
171
+    av_close_input_stream(s);
172
+    return ret;
173
+}
174
+
175
+static int read_packet(AVFormatContext *s,
176
+                       AVPacket *pkt)
177
+{
178
+    AnmDemuxContext *anm = s->priv_data;
179
+    ByteIOContext *pb = s->pb;
180
+    Page *p;
181
+    int tmp, record_size;
182
+
183
+    if (url_feof(s->pb))
184
+        return AVERROR(EIO);
185
+
186
+    if (anm->page < 0)
187
+        return 0;
188
+
189
+repeat:
190
+    p = &anm->pt[anm->page];
191
+
192
+    /* parse page header */
193
+    if (anm->record < 0) {
194
+        url_fseek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
195
+        url_fskip(pb, 8 + 2*p->nb_records);
196
+        anm->record = 0;
197
+    }
198
+
199
+    /* if we have fetched all records in this page, then find the
200
+       next page and repeat */
201
+    if (anm->record >= p->nb_records) {
202
+        anm->page = find_record(anm, p->base_record + p->nb_records);
203
+        if (anm->page < 0)
204
+            return anm->page;
205
+        anm->record = -1;
206
+        goto repeat;
207
+    }
208
+
209
+    /* fetch record size */
210
+    tmp = url_ftell(pb);
211
+    url_fseek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
212
+              8 + anm->record * 2, SEEK_SET);
213
+    record_size = get_le16(pb);
214
+    url_fseek(pb, tmp, SEEK_SET);
215
+
216
+    /* fetch record */
217
+    pkt->size = av_get_packet(s->pb, pkt, record_size);
218
+    if (pkt->size < 0)
219
+        return pkt->size;
220
+    if (p->base_record + anm->record == 0)
221
+        pkt->flags |= PKT_FLAG_KEY;
222
+
223
+    anm->record++;
224
+    return 0;
225
+}
226
+
227
+AVInputFormat anm_demuxer = {
228
+    "anm",
229
+    NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
230
+    sizeof(AnmDemuxContext),
231
+    probe,
232
+    read_header,
233
+    read_packet,
234
+};