]> git.sesse.net Git - ffmpeg/blob - libavcodec/anm.c
misc typo and wording fixes
[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     GetByteContext gb;
34     int x;  ///< x coordinate position
35 } AnmContext;
36
37 static av_cold int decode_init(AVCodecContext *avctx)
38 {
39     AnmContext *s = avctx->priv_data;
40     int i;
41
42     avctx->pix_fmt = PIX_FMT_PAL8;
43
44     s->frame.reference = 1;
45     bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
46     if (bytestream2_get_bytes_left(&s->gb) < 16 * 8 + 4 * 256)
47         return -1;
48
49     bytestream2_skipu(&s->gb, 16 * 8);
50     for (i = 0; i < 256; i++)
51         s->palette[i] = bytestream2_get_le32u(&s->gb);
52
53     return 0;
54 }
55
56 /**
57  * Perform decode operation
58  * @param dst, dst_end Destination image buffer
59  * @param gb, GetByteContext (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 'gb' is set
68  * a fill operation is achieved when 'gb' is null and pixel is >= 0
69  * a skip operation is achieved when 'gb' is null and pixel is < 0
70  */
71 static inline int op(uint8_t **dst, const uint8_t *dst_end,
72                      GetByteContext *gb,
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 (gb) {
80             if (bytestream2_get_bytes_left(gb) < striplen)
81                 goto exhausted;
82             bytestream2_get_bufferu(gb, *dst, striplen);
83         } else if (pixel >= 0)
84             memset(*dst, pixel, striplen);
85         *dst      += striplen;
86         remaining -= striplen;
87         count     -= striplen;
88         if (remaining <= 0) {
89             *dst      += linesize - width;
90             remaining  = width;
91         }
92         if (linesize > 0) {
93             if (*dst >= dst_end) goto exhausted;
94         } else {
95             if (*dst <= dst_end) goto exhausted;
96         }
97     }
98     *x = width - remaining;
99     return 0;
100
101 exhausted:
102     *x = width - remaining;
103     return 1;
104 }
105
106 static int decode_frame(AVCodecContext *avctx,
107                         void *data, int *data_size,
108                         AVPacket *avpkt)
109 {
110     AnmContext *s = avctx->priv_data;
111     const int buf_size = avpkt->size;
112     uint8_t *dst, *dst_end;
113     int count;
114
115     if(avctx->reget_buffer(avctx, &s->frame) < 0){
116         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
117         return -1;
118     }
119     dst     = s->frame.data[0];
120     dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
121
122     bytestream2_init(&s->gb, avpkt->data, buf_size);
123
124     if (bytestream2_get_byte(&s->gb) != 0x42) {
125         av_log_ask_for_sample(avctx, "unknown record type\n");
126         return buf_size;
127     }
128     if (bytestream2_get_byte(&s->gb)) {
129         av_log_ask_for_sample(avctx, "padding bytes not supported\n");
130         return buf_size;
131     }
132     bytestream2_skip(&s->gb, 2);
133
134     s->x = 0;
135     do {
136         /* if statements are ordered by probability */
137 #define OP(gb, pixel, count) \
138     op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
139
140         int type = bytestream2_get_byte(&s->gb);
141         count = type & 0x7F;
142         type >>= 7;
143         if (count) {
144             if (OP(type ? NULL : &s->gb, -1, count)) break;
145         } else if (!type) {
146             int pixel;
147             count = bytestream2_get_byte(&s->gb);  /* count==0 gives nop */
148             pixel = bytestream2_get_byte(&s->gb);
149             if (OP(NULL, pixel, count)) break;
150         } else {
151             int pixel;
152             type = bytestream2_get_le16(&s->gb);
153             count = type & 0x3FFF;
154             type >>= 14;
155             if (!count) {
156                 if (type == 0)
157                     break; // stop
158                 if (type == 2) {
159                     av_log_ask_for_sample(avctx, "unknown opcode");
160                     return AVERROR_INVALIDDATA;
161                 }
162                 continue;
163             }
164             pixel = type == 3 ? bytestream2_get_byte(&s->gb) : -1;
165             if (type == 1) count += 0x4000;
166             if (OP(type == 2 ? &s->gb : NULL, pixel, count)) break;
167         }
168     } while (bytestream2_get_bytes_left(&s->gb) > 0);
169
170     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
171
172     *data_size = sizeof(AVFrame);
173     *(AVFrame*)data = s->frame;
174     return buf_size;
175 }
176
177 static av_cold int decode_end(AVCodecContext *avctx)
178 {
179     AnmContext *s = avctx->priv_data;
180     if (s->frame.data[0])
181         avctx->release_buffer(avctx, &s->frame);
182     return 0;
183 }
184
185 AVCodec ff_anm_decoder = {
186     .name           = "anm",
187     .type           = AVMEDIA_TYPE_VIDEO,
188     .id             = CODEC_ID_ANM,
189     .priv_data_size = sizeof(AnmContext),
190     .init           = decode_init,
191     .close          = decode_end,
192     .decode         = decode_frame,
193     .capabilities   = CODEC_CAP_DR1,
194     .long_name      = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
195 };