FFmpeg  4.0
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 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 #include "internal.h"
30 
31 typedef struct AnmContext {
35  int x; ///< x coordinate position
36 } AnmContext;
37 
39 {
40  AnmContext *s = avctx->priv_data;
41  int i;
42 
43  avctx->pix_fmt = AV_PIX_FMT_PAL8;
44 
45  s->frame = av_frame_alloc();
46  if (!s->frame)
47  return AVERROR(ENOMEM);
48 
49  bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
50  if (bytestream2_get_bytes_left(&s->gb) < 16 * 8 + 4 * 256) {
51  av_frame_free(&s->frame);
52  return AVERROR_INVALIDDATA;
53  }
54 
55  bytestream2_skipu(&s->gb, 16 * 8);
56  for (i = 0; i < 256; i++)
57  s->palette[i] = bytestream2_get_le32u(&s->gb);
58 
59  return 0;
60 }
61 
62 /**
63  * Perform decode operation
64  * @param dst pointer to destination image buffer
65  * @param dst_end pointer to end of destination image buffer
66  * @param gb GetByteContext (optional, see below)
67  * @param pixel Fill color (optional, see below)
68  * @param count Pixel count
69  * @param x Pointer to x-axis counter
70  * @param width Image width
71  * @param linesize Destination image buffer linesize
72  * @return non-zero if destination buffer is exhausted
73  *
74  * a copy operation is achieved when 'gb' is set
75  * a fill operation is achieved when 'gb' is null and pixel is >= 0
76  * a skip operation is achieved when 'gb' is null and pixel is < 0
77  */
78 static inline int op(uint8_t **dst, const uint8_t *dst_end,
80  int pixel, int count,
81  int *x, int width, int linesize)
82 {
83  int remaining = width - *x;
84  while(count > 0) {
85  int striplen = FFMIN(count, remaining);
86  if (gb) {
87  if (bytestream2_get_bytes_left(gb) < striplen)
88  goto exhausted;
89  bytestream2_get_bufferu(gb, *dst, striplen);
90  } else if (pixel >= 0)
91  memset(*dst, pixel, striplen);
92  *dst += striplen;
93  remaining -= striplen;
94  count -= striplen;
95  if (remaining <= 0) {
96  *dst += linesize - width;
97  remaining = width;
98  }
99  if (linesize > 0) {
100  if (*dst >= dst_end) goto exhausted;
101  } else {
102  if (*dst <= dst_end) goto exhausted;
103  }
104  }
105  *x = width - remaining;
106  return 0;
107 
108 exhausted:
109  *x = width - remaining;
110  return 1;
111 }
112 
113 static int decode_frame(AVCodecContext *avctx,
114  void *data, int *got_frame,
115  AVPacket *avpkt)
116 {
117  AnmContext *s = avctx->priv_data;
118  const int buf_size = avpkt->size;
119  uint8_t *dst, *dst_end;
120  int count, ret;
121 
122  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
123  return ret;
124  dst = s->frame->data[0];
125  dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;
126 
127  bytestream2_init(&s->gb, avpkt->data, buf_size);
128 
129  if (bytestream2_get_byte(&s->gb) != 0x42) {
130  avpriv_request_sample(avctx, "Unknown record type");
131  return AVERROR_INVALIDDATA;
132  }
133  if (bytestream2_get_byte(&s->gb)) {
134  avpriv_request_sample(avctx, "Padding bytes");
135  return AVERROR_PATCHWELCOME;
136  }
137  bytestream2_skip(&s->gb, 2);
138 
139  s->x = 0;
140  do {
141  /* if statements are ordered by probability */
142 #define OP(gb, pixel, count) \
143  op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame->linesize[0])
144 
145  int type = bytestream2_get_byte(&s->gb);
146  count = type & 0x7F;
147  type >>= 7;
148  if (count) {
149  if (OP(type ? NULL : &s->gb, -1, count)) break;
150  } else if (!type) {
151  int pixel;
152  count = bytestream2_get_byte(&s->gb); /* count==0 gives nop */
153  pixel = bytestream2_get_byte(&s->gb);
154  if (OP(NULL, pixel, count)) break;
155  } else {
156  int pixel;
157  type = bytestream2_get_le16(&s->gb);
158  count = type & 0x3FFF;
159  type >>= 14;
160  if (!count) {
161  if (type == 0)
162  break; // stop
163  if (type == 2) {
164  avpriv_request_sample(avctx, "Unknown opcode");
165  return AVERROR_PATCHWELCOME;
166  }
167  continue;
168  }
169  pixel = type == 3 ? bytestream2_get_byte(&s->gb) : -1;
170  if (type == 1) count += 0x4000;
171  if (OP(type == 2 ? &s->gb : NULL, pixel, count)) break;
172  }
173  } while (bytestream2_get_bytes_left(&s->gb) > 0);
174 
175  memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
176 
177  *got_frame = 1;
178  if ((ret = av_frame_ref(data, s->frame)) < 0)
179  return ret;
180 
181  return buf_size;
182 }
183 
185 {
186  AnmContext *s = avctx->priv_data;
187 
188  av_frame_free(&s->frame);
189  return 0;
190 }
191 
193  .name = "anm",
194  .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
195  .type = AVMEDIA_TYPE_VIDEO,
196  .id = AV_CODEC_ID_ANM,
197  .priv_data_size = sizeof(AnmContext),
198  .init = decode_init,
199  .close = decode_end,
200  .decode = decode_frame,
201  .capabilities = AV_CODEC_CAP_DR1,
202  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
203 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Definition: anm.c:31
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1431
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
AVCodec.
Definition: avcodec.h:3408
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:273
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: decode.c:1938
#define OP(gb, pixel, count)
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:441
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1618
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1430
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:170
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
uint16_t width
Definition: gdv.c:47
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
#define FFMIN(a, b)
Definition: common.h:96
AVCodec ff_anm_decoder
Definition: anm.c:192
AVFrame * frame
Definition: anm.c:32
static av_cold int decode_init(AVCodecContext *avctx)
Definition: anm.c:38
int palette[AVPALETTE_COUNT]
Definition: anm.c:33
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: anm.c:113
main external API structure.
Definition: avcodec.h:1518
int extradata_size
Definition: avcodec.h:1619
GetByteContext gb
Definition: anm.c:34
cl_device_type type
uint8_t pixel
Definition: tiny_ssim.c:42
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
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:78
common internal api header.
int x
x coordinate position
Definition: anm.c:35
static av_cold int decode_end(AVCodecContext *avctx)
Definition: anm.c:184
void * priv_data
Definition: avcodec.h:1545
void INT64 INT64 count
Definition: avisynth_c.h:690
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959