yading@10
|
1 /*
|
yading@10
|
2 * Deluxe Paint Animation decoder
|
yading@10
|
3 * Copyright (c) 2009 Peter Ross
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 /**
|
yading@10
|
23 * @file
|
yading@10
|
24 * Deluxe Paint Animation decoder
|
yading@10
|
25 */
|
yading@10
|
26
|
yading@10
|
27 #include "avcodec.h"
|
yading@10
|
28 #include "bytestream.h"
|
yading@10
|
29 #include "internal.h"
|
yading@10
|
30
|
yading@10
|
31 typedef struct AnmContext {
|
yading@10
|
32 AVFrame *frame;
|
yading@10
|
33 int palette[AVPALETTE_COUNT];
|
yading@10
|
34 GetByteContext gb;
|
yading@10
|
35 int x; ///< x coordinate position
|
yading@10
|
36 } AnmContext;
|
yading@10
|
37
|
yading@10
|
38 static av_cold int decode_init(AVCodecContext *avctx)
|
yading@10
|
39 {
|
yading@10
|
40 AnmContext *s = avctx->priv_data;
|
yading@10
|
41 int i;
|
yading@10
|
42
|
yading@10
|
43 avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
yading@10
|
44
|
yading@10
|
45 s->frame = av_frame_alloc();
|
yading@10
|
46 if (!s->frame)
|
yading@10
|
47 return AVERROR(ENOMEM);
|
yading@10
|
48
|
yading@10
|
49 bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
|
yading@10
|
50 if (bytestream2_get_bytes_left(&s->gb) < 16 * 8 + 4 * 256)
|
yading@10
|
51 return AVERROR_INVALIDDATA;
|
yading@10
|
52
|
yading@10
|
53 bytestream2_skipu(&s->gb, 16 * 8);
|
yading@10
|
54 for (i = 0; i < 256; i++)
|
yading@10
|
55 s->palette[i] = bytestream2_get_le32u(&s->gb);
|
yading@10
|
56
|
yading@10
|
57 return 0;
|
yading@10
|
58 }
|
yading@10
|
59
|
yading@10
|
60 /**
|
yading@10
|
61 * Perform decode operation
|
yading@10
|
62 * @param dst pointer to destination image buffer
|
yading@10
|
63 * @param dst_end pointer to end of destination image buffer
|
yading@10
|
64 * @param gb GetByteContext (optional, see below)
|
yading@10
|
65 * @param pixel Fill color (optional, see below)
|
yading@10
|
66 * @param count Pixel count
|
yading@10
|
67 * @param x Pointer to x-axis counter
|
yading@10
|
68 * @param width Image width
|
yading@10
|
69 * @param linesize Destination image buffer linesize
|
yading@10
|
70 * @return non-zero if destination buffer is exhausted
|
yading@10
|
71 *
|
yading@10
|
72 * a copy operation is achieved when 'gb' is set
|
yading@10
|
73 * a fill operation is achieved when 'gb' is null and pixel is >= 0
|
yading@10
|
74 * a skip operation is achieved when 'gb' is null and pixel is < 0
|
yading@10
|
75 */
|
yading@10
|
76 static inline int op(uint8_t **dst, const uint8_t *dst_end,
|
yading@10
|
77 GetByteContext *gb,
|
yading@10
|
78 int pixel, int count,
|
yading@10
|
79 int *x, int width, int linesize)
|
yading@10
|
80 {
|
yading@10
|
81 int remaining = width - *x;
|
yading@10
|
82 while(count > 0) {
|
yading@10
|
83 int striplen = FFMIN(count, remaining);
|
yading@10
|
84 if (gb) {
|
yading@10
|
85 if (bytestream2_get_bytes_left(gb) < striplen)
|
yading@10
|
86 goto exhausted;
|
yading@10
|
87 bytestream2_get_bufferu(gb, *dst, striplen);
|
yading@10
|
88 } else if (pixel >= 0)
|
yading@10
|
89 memset(*dst, pixel, striplen);
|
yading@10
|
90 *dst += striplen;
|
yading@10
|
91 remaining -= striplen;
|
yading@10
|
92 count -= striplen;
|
yading@10
|
93 if (remaining <= 0) {
|
yading@10
|
94 *dst += linesize - width;
|
yading@10
|
95 remaining = width;
|
yading@10
|
96 }
|
yading@10
|
97 if (linesize > 0) {
|
yading@10
|
98 if (*dst >= dst_end) goto exhausted;
|
yading@10
|
99 } else {
|
yading@10
|
100 if (*dst <= dst_end) goto exhausted;
|
yading@10
|
101 }
|
yading@10
|
102 }
|
yading@10
|
103 *x = width - remaining;
|
yading@10
|
104 return 0;
|
yading@10
|
105
|
yading@10
|
106 exhausted:
|
yading@10
|
107 *x = width - remaining;
|
yading@10
|
108 return 1;
|
yading@10
|
109 }
|
yading@10
|
110
|
yading@10
|
111 static int decode_frame(AVCodecContext *avctx,
|
yading@10
|
112 void *data, int *got_frame,
|
yading@10
|
113 AVPacket *avpkt)
|
yading@10
|
114 {
|
yading@10
|
115 AnmContext *s = avctx->priv_data;
|
yading@10
|
116 const int buf_size = avpkt->size;
|
yading@10
|
117 uint8_t *dst, *dst_end;
|
yading@10
|
118 int count, ret;
|
yading@10
|
119
|
yading@10
|
120 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
|
yading@10
|
121 return ret;
|
yading@10
|
122 dst = s->frame->data[0];
|
yading@10
|
123 dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;
|
yading@10
|
124
|
yading@10
|
125 bytestream2_init(&s->gb, avpkt->data, buf_size);
|
yading@10
|
126
|
yading@10
|
127 if (bytestream2_get_byte(&s->gb) != 0x42) {
|
yading@10
|
128 avpriv_request_sample(avctx, "Unknown record type");
|
yading@10
|
129 return AVERROR_INVALIDDATA;
|
yading@10
|
130 }
|
yading@10
|
131 if (bytestream2_get_byte(&s->gb)) {
|
yading@10
|
132 avpriv_request_sample(avctx, "Padding bytes");
|
yading@10
|
133 return AVERROR_PATCHWELCOME;
|
yading@10
|
134 }
|
yading@10
|
135 bytestream2_skip(&s->gb, 2);
|
yading@10
|
136
|
yading@10
|
137 s->x = 0;
|
yading@10
|
138 do {
|
yading@10
|
139 /* if statements are ordered by probability */
|
yading@10
|
140 #define OP(gb, pixel, count) \
|
yading@10
|
141 op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame->linesize[0])
|
yading@10
|
142
|
yading@10
|
143 int type = bytestream2_get_byte(&s->gb);
|
yading@10
|
144 count = type & 0x7F;
|
yading@10
|
145 type >>= 7;
|
yading@10
|
146 if (count) {
|
yading@10
|
147 if (OP(type ? NULL : &s->gb, -1, count)) break;
|
yading@10
|
148 } else if (!type) {
|
yading@10
|
149 int pixel;
|
yading@10
|
150 count = bytestream2_get_byte(&s->gb); /* count==0 gives nop */
|
yading@10
|
151 pixel = bytestream2_get_byte(&s->gb);
|
yading@10
|
152 if (OP(NULL, pixel, count)) break;
|
yading@10
|
153 } else {
|
yading@10
|
154 int pixel;
|
yading@10
|
155 type = bytestream2_get_le16(&s->gb);
|
yading@10
|
156 count = type & 0x3FFF;
|
yading@10
|
157 type >>= 14;
|
yading@10
|
158 if (!count) {
|
yading@10
|
159 if (type == 0)
|
yading@10
|
160 break; // stop
|
yading@10
|
161 if (type == 2) {
|
yading@10
|
162 avpriv_request_sample(avctx, "Unknown opcode");
|
yading@10
|
163 return AVERROR_PATCHWELCOME;
|
yading@10
|
164 }
|
yading@10
|
165 continue;
|
yading@10
|
166 }
|
yading@10
|
167 pixel = type == 3 ? bytestream2_get_byte(&s->gb) : -1;
|
yading@10
|
168 if (type == 1) count += 0x4000;
|
yading@10
|
169 if (OP(type == 2 ? &s->gb : NULL, pixel, count)) break;
|
yading@10
|
170 }
|
yading@10
|
171 } while (bytestream2_get_bytes_left(&s->gb) > 0);
|
yading@10
|
172
|
yading@10
|
173 memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
|
yading@10
|
174
|
yading@10
|
175 *got_frame = 1;
|
yading@10
|
176 if ((ret = av_frame_ref(data, s->frame)) < 0)
|
yading@10
|
177 return ret;
|
yading@10
|
178
|
yading@10
|
179 return buf_size;
|
yading@10
|
180 }
|
yading@10
|
181
|
yading@10
|
182 static av_cold int decode_end(AVCodecContext *avctx)
|
yading@10
|
183 {
|
yading@10
|
184 AnmContext *s = avctx->priv_data;
|
yading@10
|
185
|
yading@10
|
186 av_frame_free(&s->frame);
|
yading@10
|
187 return 0;
|
yading@10
|
188 }
|
yading@10
|
189
|
yading@10
|
190 AVCodec ff_anm_decoder = {
|
yading@10
|
191 .name = "anm",
|
yading@10
|
192 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
193 .id = AV_CODEC_ID_ANM,
|
yading@10
|
194 .priv_data_size = sizeof(AnmContext),
|
yading@10
|
195 .init = decode_init,
|
yading@10
|
196 .close = decode_end,
|
yading@10
|
197 .decode = decode_frame,
|
yading@10
|
198 .capabilities = CODEC_CAP_DR1,
|
yading@10
|
199 .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
|
yading@10
|
200 };
|