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 {
34  int x;
35 } AnmContext;
36 
38 {
39  AnmContext *s = avctx->priv_data;
40  int i;
41 
42  avctx->pix_fmt = AV_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 
72 static inline int op(uint8_t **dst, const uint8_t *dst_end,
73  GetByteContext *gb,
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 (gb) {
81  if (bytestream2_get_bytes_left(gb) < striplen)
82  goto exhausted;
83  bytestream2_get_bufferu(gb, *dst, 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 *got_frame,
109  AVPacket *avpkt)
110 {
111  AnmContext *s = avctx->priv_data;
112  const int buf_size = avpkt->size;
113  uint8_t *dst, *dst_end;
114  int count;
115 
116  if(avctx->reget_buffer(avctx, &s->frame) < 0){
117  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
118  return -1;
119  }
120  dst = s->frame.data[0];
121  dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
122 
123  bytestream2_init(&s->gb, avpkt->data, buf_size);
124 
125  if (bytestream2_get_byte(&s->gb) != 0x42) {
126  av_log_ask_for_sample(avctx, "unknown record type\n");
127  return buf_size;
128  }
129  if (bytestream2_get_byte(&s->gb)) {
130  av_log_ask_for_sample(avctx, "padding bytes not supported\n");
131  return buf_size;
132  }
133  bytestream2_skip(&s->gb, 2);
134 
135  s->x = 0;
136  do {
137  /* if statements are ordered by probability */
138 #define OP(gb, pixel, count) \
139  op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
140 
141  int type = bytestream2_get_byte(&s->gb);
142  count = type & 0x7F;
143  type >>= 7;
144  if (count) {
145  if (OP(type ? NULL : &s->gb, -1, count)) break;
146  } else if (!type) {
147  int pixel;
148  count = bytestream2_get_byte(&s->gb); /* count==0 gives nop */
149  pixel = bytestream2_get_byte(&s->gb);
150  if (OP(NULL, pixel, count)) break;
151  } else {
152  int pixel;
153  type = bytestream2_get_le16(&s->gb);
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_PATCHWELCOME;
162  }
163  continue;
164  }
165  pixel = type == 3 ? bytestream2_get_byte(&s->gb) : -1;
166  if (type == 1) count += 0x4000;
167  if (OP(type == 2 ? &s->gb : NULL, pixel, count)) break;
168  }
169  } while (bytestream2_get_bytes_left(&s->gb) > 0);
170 
171  memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
172 
173  *got_frame = 1;
174  *(AVFrame*)data = s->frame;
175  return buf_size;
176 }
177 
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 
187  .name = "anm",
188  .type = AVMEDIA_TYPE_VIDEO,
189  .id = AV_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 };
Definition: anm.c:30
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
AVCodec.
Definition: avcodec.h:2960
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:268
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2273
#define OP(gb, pixel, count)
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:165
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
void av_log_ask_for_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message asking for a sample.
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:1132
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
AVCodec ff_anm_decoder
Definition: anm.c:186
static av_cold int decode_init(AVCodecContext *avctx)
Definition: anm.c:37
#define pixel
int palette[AVPALETTE_COUNT]
Definition: anm.c:32
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: anm.c:107
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
int extradata_size
Definition: avcodec.h:1455
GetByteContext gb
Definition: anm.c:33
AVFrame frame
Definition: anm.c:31
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:72
int x
x coordinate position
Definition: anm.c:34
static av_cold int decode_end(AVCodecContext *avctx)
Definition: anm.c:178
struct AnmContext AnmContext
void * priv_data
Definition: avcodec.h:1382
This structure stores compressed data.
Definition: avcodec.h:898