]> git.sesse.net Git - ffmpeg/blob - libavcodec/anm.c
anm decoder: move buffer allocation from decode_init() to decode_frame()
[ffmpeg] / libavcodec / anm.c
1 /*
2  * Deluxe Paint Animation decoder
3  * Copyright (c) 2009 Peter Ross
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
22 /**
23  * @file
24  * Deluxe Paint Animation decoder
25  */
26
27 #include "avcodec.h"
28 #include "bytestream.h"
29
30 typedef struct AnmContext {
31     AVFrame frame;
32     int palette[AVPALETTE_COUNT];
33     int x;  ///< x coordinate position
34 } AnmContext;
35
36 static av_cold int decode_init(AVCodecContext *avctx)
37 {
38     AnmContext *s = avctx->priv_data;
39     const uint8_t *buf;
40     int i;
41
42     avctx->pix_fmt = PIX_FMT_PAL8;
43
44     if (avctx->extradata_size != 16*8 + 4*256)
45         return -1;
46
47     s->frame.reference = 1;
48
49     buf = avctx->extradata + 16*8;
50     for (i = 0; i < 256; i++)
51         s->palette[i] = bytestream_get_le32(&buf);
52
53     return 0;
54 }
55
56 /**
57  * Perform decode operation
58  * @param dst, dst_end Destination image buffer
59  * @param buf, buf_end Source buffer (optional, see below)
60  * @param pixel Fill color (optional, see below)
61  * @param count Pixel count
62  * @param x Pointer to x-axis counter
63  * @param width Image width
64  * @param linesize Destination image buffer linesize
65  * @return non-zero if destination buffer is exhausted
66  *
67  * a copy operation is achieved when 'buf' is set
68  * a fill operation is acheived when 'buf' is null and pixel is >= 0
69  * a skip operation is acheived when 'buf' is null and pixel is < 0
70  */
71 static inline int op(uint8_t **dst, const uint8_t *dst_end,
72                      const uint8_t **buf, const uint8_t *buf_end,
73                      int pixel, int count,
74                      int *x, int width, int linesize)
75 {
76     int remaining = width - *x;
77     while(count > 0) {
78         int striplen = FFMIN(count, remaining);
79         if (buf) {
80             striplen = FFMIN(striplen, buf_end - *buf);
81             if (*buf >= buf_end)
82                 goto exhausted;
83             memcpy(*dst, *buf, striplen);
84             *buf += striplen;
85         } else if (pixel >= 0)
86             memset(*dst, pixel, striplen);
87         *dst      += striplen;
88         remaining -= striplen;
89         count     -= striplen;
90         if (remaining <= 0) {
91             *dst      += linesize - width;
92             remaining  = width;
93         }
94         if (linesize > 0) {
95             if (*dst >= dst_end) goto exhausted;
96         } else {
97             if (*dst <= dst_end) goto exhausted;
98         }
99     }
100     *x = width - remaining;
101     return 0;
102
103 exhausted:
104     *x = width - remaining;
105     return 1;
106 }
107
108 static int decode_frame(AVCodecContext *avctx,
109                         void *data, int *data_size,
110                         AVPacket *avpkt)
111 {
112     AnmContext *s = avctx->priv_data;
113     const uint8_t *buf = avpkt->data;
114     const int buf_size = avpkt->size;
115     const uint8_t *buf_end = buf + buf_size;
116     uint8_t *dst, *dst_end;
117     int count;
118
119     if(avctx->reget_buffer(avctx, &s->frame) < 0){
120         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
121         return -1;
122     }
123     dst     = s->frame.data[0];
124     dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
125
126     if (buf[0] != 0x42) {
127         av_log_ask_for_sample(avctx, "unknown record type\n");
128         return buf_size;
129     }
130     if (buf[1]) {
131         av_log_ask_for_sample(avctx, "padding bytes not supported\n");
132         return buf_size;
133     }
134     buf += 4;
135
136     s->x = 0;
137     do {
138         /* if statements are ordered by probability */
139 #define OP(buf, pixel, count) \
140     op(&dst, dst_end, (buf), buf_end, (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
141
142         int type = bytestream_get_byte(&buf);
143         count = type & 0x7F;
144         type >>= 7;
145         if (count) {
146             if (OP(type ? NULL : &buf, -1, count)) break;
147         } else if (!type) {
148             int pixel;
149             count = bytestream_get_byte(&buf);  /* count==0 gives nop */
150             pixel = bytestream_get_byte(&buf);
151             if (OP(NULL, pixel, count)) break;
152         } else {
153             int pixel;
154             type = bytestream_get_le16(&buf);
155             count = type & 0x3FFF;
156             type >>= 14;
157             if (!count) {
158                 if (type == 0)
159                     break; // stop
160                 if (type == 2) {
161                     av_log_ask_for_sample(avctx, "unknown opcode");
162                     return AVERROR_INVALIDDATA;
163                 }
164                 continue;
165             }
166             pixel = type == 3 ? bytestream_get_byte(&buf) : -1;
167             if (type == 1) count += 0x4000;
168             if (OP(type == 2 ? &buf : NULL, pixel, count)) break;
169         }
170     } while (buf + 1 < buf_end);
171
172     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
173
174     *data_size = sizeof(AVFrame);
175     *(AVFrame*)data = s->frame;
176     return buf_size;
177 }
178
179 static av_cold int decode_end(AVCodecContext *avctx)
180 {
181     AnmContext *s = avctx->priv_data;
182     if (s->frame.data[0])
183         avctx->release_buffer(avctx, &s->frame);
184     return 0;
185 }
186
187 AVCodec ff_anm_decoder = {
188     .name           = "anm",
189     .type           = AVMEDIA_TYPE_VIDEO,
190     .id             = CODEC_ID_ANM,
191     .priv_data_size = sizeof(AnmContext),
192     .init           = decode_init,
193     .close          = decode_end,
194     .decode         = decode_frame,
195     .capabilities   = CODEC_CAP_DR1,
196     .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
197 };