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 };
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
Definition: avcodec.h:1726
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Definition: anm.c:30
Audio Video Frame.
Definition: avcodec.h:985
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
int size
Definition: avcodec.h:909
int width
Definition: rotozoom.c:164
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: anm.c:111
AVCodec.
Definition: avcodec.h:3189
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2336
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
#define av_cold
Definition: attributes.h:71
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
#define FFMIN(a, b)
Definition: common.h:55
struct AVFrame AVFrame
Audio Video Frame.
AVCodec ff_anm_decoder
Definition: anm.c:188
static av_cold int decode_init(AVCodecContext *avctx)
Definition: anm.c:35
#define pixel
NULL
Definition: eval.c:50
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
void av_log_ask_for_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message asking for a sample.
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
int extradata_size
Definition: avcodec.h:1388
AVFrame frame
Definition: anm.c:31
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int x
x coordinate position
Definition: anm.c:32
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:75
static av_cold int decode_end(AVCodecContext *avctx)
Definition: anm.c:180
struct AnmContext AnmContext
void * priv_data
Definition: avcodec.h:1531
static int op(uint8_t **dst, const uint8_t *dst_end, const uint8_t **buf, const uint8_t *buf_end, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:74
#define OP(buf, pixel, count)