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