anm.c
Go to the documentation of this file.
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 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 
30 typedef struct AnmContext {
32  int x;
33 } AnmContext;
34 
36 {
37  AnmContext *s = avctx->priv_data;
38  const uint8_t *buf;
39  int i;
40 
41  avctx->pix_fmt = PIX_FMT_PAL8;
42 
43  if (avctx->extradata_size != 16*8 + 4*256)
44  return -1;
45 
46  s->frame.reference = 1;
47  if (avctx->get_buffer(avctx, &s->frame) < 0) {
48  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
49  return -1;
50  }
51 
52  buf = avctx->extradata + 16*8;
53  for (i = 0; i < 256; i++)
54  ((uint32_t*)s->frame.data[1])[i] = bytestream_get_le32(&buf);
55 
56  return 0;
57 }
58 
74 static inline int op(uint8_t **dst, const uint8_t *dst_end,
75  const uint8_t **buf, const uint8_t *buf_end,
76  int pixel, int count,
77  int *x, int width, int linesize)
78 {
79  int remaining = width - *x;
80  while(count > 0) {
81  int striplen = FFMIN(count, remaining);
82  if (buf) {
83  striplen = FFMIN(striplen, buf_end - *buf);
84  if (*buf >= buf_end)
85  goto exhausted;
86  memcpy(*dst, *buf, striplen);
87  *buf += striplen;
88  } else if (pixel >= 0)
89  memset(*dst, pixel, striplen);
90  *dst += striplen;
91  remaining -= striplen;
92  count -= striplen;
93  if (remaining <= 0) {
94  *dst += linesize - width;
95  remaining = width;
96  }
97  if (linesize > 0) {
98  if (*dst >= dst_end) goto exhausted;
99  } else {
100  if (*dst <= dst_end) goto exhausted;
101  }
102  }
103  *x = width - remaining;
104  return 0;
105 
106 exhausted:
107  *x = width - remaining;
108  return 1;
109 }
110 
111 static int decode_frame(AVCodecContext *avctx,
112  void *data, int *data_size,
113  AVPacket *avpkt)
114 {
115  AnmContext *s = avctx->priv_data;
116  const uint8_t *buf = avpkt->data;
117  const int buf_size = avpkt->size;
118  const uint8_t *buf_end = buf + buf_size;
119  uint8_t *dst, *dst_end;
120  int count;
121 
122  if(avctx->reget_buffer(avctx, &s->frame) < 0){
123  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
124  return -1;
125  }
126  dst = s->frame.data[0];
127  dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
128 
129  if (buf[0] != 0x42) {
130  av_log_ask_for_sample(avctx, "unknown record type\n");
131  return buf_size;
132  }
133  if (buf[1]) {
134  av_log_ask_for_sample(avctx, "padding bytes not supported\n");
135  return buf_size;
136  }
137  buf += 4;
138 
139  s->x = 0;
140  do {
141  /* if statements are ordered by probability */
142 #define OP(buf, pixel, count) \
143  op(&dst, dst_end, (buf), buf_end, (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
144 
145  int type = bytestream_get_byte(&buf);
146  count = type & 0x7F;
147  type >>= 7;
148  if (count) {
149  if (OP(type ? NULL : &buf, -1, count)) break;
150  } else if (!type) {
151  int pixel;
152  count = bytestream_get_byte(&buf); /* count==0 gives nop */
153  pixel = bytestream_get_byte(&buf);
154  if (OP(NULL, pixel, count)) break;
155  } else {
156  int pixel;
157  type = bytestream_get_le16(&buf);
158  count = type & 0x3FFF;
159  type >>= 14;
160  if (!count) {
161  if (type == 0)
162  break; // stop
163  if (type == 2) {
164  av_log_ask_for_sample(avctx, "unknown opcode");
165  return AVERROR_INVALIDDATA;
166  }
167  continue;
168  }
169  pixel = type == 3 ? bytestream_get_byte(&buf) : -1;
170  if (type == 1) count += 0x4000;
171  if (OP(type == 2 ? &buf : NULL, pixel, count)) break;
172  }
173  } while (buf + 1 < buf_end);
174 
175  *data_size = sizeof(AVFrame);
176  *(AVFrame*)data = s->frame;
177  return buf_size;
178 }
179 
181 {
182  AnmContext *s = avctx->priv_data;
183  if (s->frame.data[0])
184  avctx->release_buffer(avctx, &s->frame);
185  return 0;
186 }
187 
189  .name = "anm",
190  .type = AVMEDIA_TYPE_VIDEO,
191  .id = CODEC_ID_ANM,
192  .priv_data_size = sizeof(AnmContext),
193  .init = decode_init,
194  .close = decode_end,
195  .decode = decode_frame,
196  .capabilities = CODEC_CAP_DR1,
197  .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
198 };