FFmpeg  4.3.8
pafvideo.c
Go to the documentation of this file.
1 /*
2  * Packed Animation File video decoder
3  * Copyright (c) 2012 Paul B Mahol
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 #include "libavutil/imgutils.h"
23 
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "copy_block.h"
27 #include "internal.h"
28 
29 
30 static const uint8_t block_sequences[16][8] = {
31  { 0, 0, 0, 0, 0, 0, 0, 0 },
32  { 2, 0, 0, 0, 0, 0, 0, 0 },
33  { 5, 7, 0, 0, 0, 0, 0, 0 },
34  { 5, 0, 0, 0, 0, 0, 0, 0 },
35  { 6, 0, 0, 0, 0, 0, 0, 0 },
36  { 5, 7, 5, 7, 0, 0, 0, 0 },
37  { 5, 7, 5, 0, 0, 0, 0, 0 },
38  { 5, 7, 6, 0, 0, 0, 0, 0 },
39  { 5, 5, 0, 0, 0, 0, 0, 0 },
40  { 3, 0, 0, 0, 0, 0, 0, 0 },
41  { 6, 6, 0, 0, 0, 0, 0, 0 },
42  { 2, 4, 0, 0, 0, 0, 0, 0 },
43  { 2, 4, 5, 7, 0, 0, 0, 0 },
44  { 2, 4, 5, 0, 0, 0, 0, 0 },
45  { 2, 4, 6, 0, 0, 0, 0, 0 },
46  { 2, 4, 5, 7, 5, 7, 0, 0 },
47 };
48 
49 typedef struct PAFVideoDecContext {
52 
53  int width;
54  int height;
55 
58  int dirty[4];
61 
64 
66 {
67  PAFVideoDecContext *c = avctx->priv_data;
68  int i;
69 
70  av_frame_free(&c->pic);
71 
72  for (i = 0; i < 4; i++)
73  av_freep(&c->frame[i]);
74 
75  return 0;
76 }
77 
79 {
80  PAFVideoDecContext *c = avctx->priv_data;
81  int i;
82  int ret;
83 
84  c->width = avctx->width;
85  c->height = avctx->height;
86 
87  if (avctx->height & 3 || avctx->width & 3) {
88  av_log(avctx, AV_LOG_ERROR,
89  "width %d and height %d must be multiplie of 4.\n",
90  avctx->width, avctx->height);
91  return AVERROR_INVALIDDATA;
92  }
93 
94  avctx->pix_fmt = AV_PIX_FMT_PAL8;
95  ret = av_image_check_size2(avctx->width, FFALIGN(avctx->height, 256), avctx->max_pixels, avctx->pix_fmt, 0, avctx);
96  if (ret < 0)
97  return ret;
98 
99  c->pic = av_frame_alloc();
100  if (!c->pic)
101  return AVERROR(ENOMEM);
102 
103  c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
104  c->video_size = avctx->width * avctx->height;
105  for (i = 0; i < 4; i++) {
106  c->frame[i] = av_mallocz(c->frame_size);
107  if (!c->frame[i]) {
108  paf_video_close(avctx);
109  return AVERROR(ENOMEM);
110  }
111  }
112 
113  return 0;
114 }
115 
117 {
118  int i;
119 
120  for (i = 0; i < 4; i++) {
121  bytestream2_get_buffer(&c->gb, dst, 4);
122  dst += width;
123  }
124 }
125 
127 {
128  int i;
129 
130  for (i = 0; i < 4; i++) {
131  if (mask & (1 << 7 - i))
132  dst[i] = color;
133  if (mask & (1 << 3 - i))
134  dst[width + i] = color;
135  }
136 }
137 
138 static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
139 {
140  int i;
141 
142  for (i = 0; i < 4; i++) {
143  if (mask & (1 << 7 - i))
144  dst[i] = src[i];
145  if (mask & (1 << 3 - i))
146  dst[width + i] = src[width + i];
147  }
148 }
149 
151  const uint8_t **p,
152  const uint8_t **pend)
153 {
154  int val = bytestream2_get_be16(&c->gb);
155  int page = val >> 14;
156  int x = (val & 0x7F);
157  int y = ((val >> 7) & 0x7F);
158 
159  *p = c->frame[page] + x * 2 + y * 2 * c->width;
160  *pend = c->frame[page] + c->frame_size;
161 }
162 
164 {
165  uint32_t opcode_size, offset;
166  uint8_t *dst, *dend, mask = 0, color = 0;
167  const uint8_t *src, *send, *opcodes;
168  int i, j, op = 0;
169 
170  i = bytestream2_get_byte(&c->gb);
171  if (i) {
172  if (code & 0x10) {
173  int align;
174 
175  align = bytestream2_tell(&c->gb) & 3;
176  if (align)
177  bytestream2_skip(&c->gb, 4 - align);
178  }
179  do {
180  int page, val, x, y;
181  val = bytestream2_get_be16(&c->gb);
182  page = val >> 14;
183  x = (val & 0x7F) * 2;
184  y = ((val >> 7) & 0x7F) * 2;
185  dst = c->frame[page] + x + y * c->width;
186  dend = c->frame[page] + c->frame_size;
187  offset = (x & 0x7F) * 2;
188  j = bytestream2_get_le16(&c->gb) + offset;
189  if (bytestream2_get_bytes_left(&c->gb) < (j - offset) * 16)
190  return AVERROR_INVALIDDATA;
191  c->dirty[page] = 1;
192  do {
193  offset++;
194  if (dst + 3 * c->width + 4 > dend)
195  return AVERROR_INVALIDDATA;
196  read4x4block(c, dst, c->width);
197  if ((offset & 0x3F) == 0)
198  dst += c->width * 3;
199  dst += 4;
200  } while (offset < j);
201  } while (--i);
202  }
203 
204  dst = c->frame[c->current_frame];
205  dend = c->frame[c->current_frame] + c->frame_size;
206  do {
207  set_src_position(c, &src, &send);
208  if ((src + 3 * c->width + 4 > send) ||
209  (dst + 3 * c->width + 4 > dend) ||
211  return AVERROR_INVALIDDATA;
212  copy_block4(dst, src, c->width, c->width, 4);
213  i++;
214  if ((i & 0x3F) == 0)
215  dst += c->width * 3;
216  dst += 4;
217  } while (i < c->video_size / 16);
218 
219  opcode_size = bytestream2_get_le16(&c->gb);
220  bytestream2_skip(&c->gb, 2);
221 
222  if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
223  return AVERROR_INVALIDDATA;
224 
225  opcodes = pkt + bytestream2_tell(&c->gb);
226  bytestream2_skipu(&c->gb, opcode_size);
227 
228  dst = c->frame[c->current_frame];
229 
230  for (i = 0; i < c->height; i += 4, dst += c->width * 3)
231  for (j = 0; j < c->width; j += 4, dst += 4) {
232  int opcode, k = 0;
233  if (op > opcode_size)
234  return AVERROR_INVALIDDATA;
235  if (j & 4) {
236  opcode = opcodes[op] & 15;
237  op++;
238  } else {
239  opcode = opcodes[op] >> 4;
240  }
241 
242  while (block_sequences[opcode][k]) {
243  offset = c->width * 2;
244  code = block_sequences[opcode][k++];
245 
246  switch (code) {
247  case 2:
248  offset = 0;
249  case 3:
250  color = bytestream2_get_byte(&c->gb);
251  case 4:
252  mask = bytestream2_get_byte(&c->gb);
253  copy_color_mask(dst + offset, c->width, mask, color);
254  break;
255  case 5:
256  offset = 0;
257  case 6:
258  set_src_position(c, &src, &send);
259  case 7:
260  if (src + offset + c->width + 4 > send)
261  return AVERROR_INVALIDDATA;
262  mask = bytestream2_get_byte(&c->gb);
263  copy_src_mask(dst + offset, c->width, mask, src + offset);
264  break;
265  }
266  }
267  }
268 
269  return 0;
270 }
271 
272 static int paf_video_decode(AVCodecContext *avctx, void *data,
273  int *got_frame, AVPacket *pkt)
274 {
275  PAFVideoDecContext *c = avctx->priv_data;
276  uint8_t code, *dst, *end;
277  int i, frame, ret;
278 
279  if (pkt->size < 2)
280  return AVERROR_INVALIDDATA;
281 
282  bytestream2_init(&c->gb, pkt->data, pkt->size);
283 
284  code = bytestream2_get_byte(&c->gb);
285  if ((code & 0xF) > 4 || (code & 0xF) == 3) {
286  avpriv_request_sample(avctx, "unknown/invalid code");
287  return AVERROR_INVALIDDATA;
288  }
289 
290  if ((code & 0xF) == 0 &&
292  return AVERROR_INVALIDDATA;
293 
294  if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
295  return ret;
296 
297  if (code & 0x20) { // frame is keyframe
298  memset(c->pic->data[1], 0, AVPALETTE_SIZE);
299  c->current_frame = 0;
300  c->pic->key_frame = 1;
302  } else {
303  c->pic->key_frame = 0;
305  }
306 
307  if (code & 0x40) { // palette update
308  uint32_t *out = (uint32_t *)c->pic->data[1];
309  int index, count;
310 
311  index = bytestream2_get_byte(&c->gb);
312  count = bytestream2_get_byte(&c->gb) + 1;
313 
314  if (index + count > 256)
315  return AVERROR_INVALIDDATA;
316  if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
317  return AVERROR_INVALIDDATA;
318 
319  out += index;
320  for (i = 0; i < count; i++) {
321  unsigned r, g, b;
322 
323  r = bytestream2_get_byteu(&c->gb);
324  r = r << 2 | r >> 4;
325  g = bytestream2_get_byteu(&c->gb);
326  g = g << 2 | g >> 4;
327  b = bytestream2_get_byteu(&c->gb);
328  b = b << 2 | b >> 4;
329  *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
330  }
331  c->pic->palette_has_changed = 1;
332  }
333 
334  c->dirty[c->current_frame] = 1;
335  if (code & 0x20)
336  for (i = 0; i < 4; i++) {
337  if (c->dirty[i])
338  memset(c->frame[i], 0, c->frame_size);
339  c->dirty[i] = 0;
340  }
341 
342  switch (code & 0x0F) {
343  case 0:
344  /* Block-based motion compensation using 4x4 blocks with either
345  * horizontal or vertical vectors; might incorporate VQ as well. */
346  if ((ret = decode_0(c, pkt->data, code)) < 0)
347  return ret;
348  break;
349  case 1:
350  /* Uncompressed data. This mode specifies that (width * height) bytes
351  * should be copied directly from the encoded buffer into the output. */
352  dst = c->frame[c->current_frame];
353  // possibly chunk length data
354  bytestream2_skip(&c->gb, 2);
356  return AVERROR_INVALIDDATA;
357  bytestream2_get_bufferu(&c->gb, dst, c->video_size);
358  break;
359  case 2:
360  /* Copy reference frame: Consume the next byte in the stream as the
361  * reference frame (which should be 0, 1, 2, or 3, and should not be
362  * the same as the current frame number). */
363  frame = bytestream2_get_byte(&c->gb);
364  if (frame > 3)
365  return AVERROR_INVALIDDATA;
366  if (frame != c->current_frame)
367  memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
368  break;
369  case 4:
370  /* Run length encoding.*/
371  dst = c->frame[c->current_frame];
372  end = dst + c->video_size;
373 
374  bytestream2_skip(&c->gb, 2);
375 
376  while (dst < end) {
377  int8_t code;
378  int count;
379 
380  if (bytestream2_get_bytes_left(&c->gb) < 2)
381  return AVERROR_INVALIDDATA;
382 
383  code = bytestream2_get_byteu(&c->gb);
384  count = FFABS(code) + 1;
385 
386  if (dst + count > end)
387  return AVERROR_INVALIDDATA;
388  if (code < 0)
389  memset(dst, bytestream2_get_byteu(&c->gb), count);
390  else
391  bytestream2_get_buffer(&c->gb, dst, count);
392  dst += count;
393  }
394  break;
395  default:
396  av_assert0(0);
397  }
398 
399  av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
400  c->frame[c->current_frame], c->width,
401  c->width, c->height);
402 
403  c->current_frame = (c->current_frame + 1) & 3;
404  if ((ret = av_frame_ref(data, c->pic)) < 0)
405  return ret;
406 
407  *got_frame = 1;
408 
409  return pkt->size;
410 }
411 
413  .name = "paf_video",
414  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
415  .type = AVMEDIA_TYPE_VIDEO,
416  .id = AV_CODEC_ID_PAF_VIDEO,
417  .priv_data_size = sizeof(PAFVideoDecContext),
418  .init = paf_video_init,
419  .close = paf_video_close,
421  .capabilities = AV_CODEC_CAP_DR1,
422 };
static av_cold int paf_video_close(AVCodecContext *avctx)
Definition: pafvideo.c:65
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
GetByteContext gb
Definition: pafvideo.c:51
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
misc image utilities
uint8_t * frame[4]
Definition: pafvideo.c:57
const char * g
Definition: vf_curves.c:115
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define avpriv_request_sample(...)
int size
Definition: packet.h:356
const char * b
Definition: vf_curves.c:116
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
static AVPacket pkt
AVCodec.
Definition: codec.h:190
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:273
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_cold
Definition: attributes.h:88
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:92
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
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:444
const char data[16]
Definition: mxf.c:91
uint8_t * data
Definition: packet.h:355
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:170
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
uint8_t * opcodes
Definition: pafvideo.c:62
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
#define U(x)
Definition: vp56_arith.h:37
#define src
Definition: vp8dsp.c:254
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static const uint16_t mask[17]
Definition: lzw.c:38
#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:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available...
Definition: decode.c:1961
const uint8_t * code
Definition: spdifenc.c:413
const char * r
Definition: vf_curves.c:114
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:263
static int paf_video_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
Definition: pafvideo.c:272
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
const char * name
Name of the codec implementation.
Definition: codec.h:197
AVFrame * pic
Definition: pafvideo.c:50
int64_t max_pixels
The number of pixels per image to maximally accept.
Definition: avcodec.h:2256
static const uint8_t offset[127][2]
Definition: vf_spp.c:93
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:383
static const uint8_t block_sequences[16][8]
Definition: pafvideo.c:30
int width
picture width / height.
Definition: avcodec.h:699
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static void set_src_position(PAFVideoDecContext *c, const uint8_t **p, const uint8_t **pend)
Definition: pafvideo.c:150
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
int discard_damaged_percentage
The percentage of damaged samples to discard a frame.
Definition: avcodec.h:2336
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
static void copy_block4(uint8_t *dst, const uint8_t *src, ptrdiff_t dstStride, ptrdiff_t srcStride, int h)
Definition: copy_block.h:37
main external API structure.
Definition: avcodec.h:526
long long int64_t
Definition: coverity.c:34
AVCodec ff_paf_video_decoder
Definition: pafvideo.c:412
int index
Definition: gxfenc.c:89
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:457
static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
Definition: pafvideo.c:138
static av_cold int paf_video_init(AVCodecContext *avctx)
Definition: pafvideo.c:78
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
Definition: pafvideo.c:126
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:75
common internal api header.
static double c[64]
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition: imgutils.c:253
void * priv_data
Definition: avcodec.h:553
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
FILE * out
Definition: movenc.c:54
#define av_freep(p)
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:338
static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
Definition: pafvideo.c:116
static int decode_0(PAFVideoDecContext *c, uint8_t *pkt, uint8_t code)
Definition: pafvideo.c:163
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:332
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
Predicted.
Definition: avutil.h:275