FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
60 
63 
65 {
66  PAFVideoDecContext *c = avctx->priv_data;
67  int i;
68 
69  av_frame_free(&c->pic);
70 
71  for (i = 0; i < 4; i++)
72  av_freep(&c->frame[i]);
73 
74  return 0;
75 }
76 
78 {
79  PAFVideoDecContext *c = avctx->priv_data;
80  int i;
81 
82  c->width = avctx->width;
83  c->height = avctx->height;
84 
85  if (avctx->height & 3 || avctx->width & 3) {
86  av_log(avctx, AV_LOG_ERROR,
87  "width %d and height %d must be multiplie of 4.\n",
88  avctx->width, avctx->height);
89  return AVERROR_INVALIDDATA;
90  }
91 
92  avctx->pix_fmt = AV_PIX_FMT_PAL8;
93 
94  c->pic = av_frame_alloc();
95  if (!c->pic)
96  return AVERROR(ENOMEM);
97 
98  c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
99  c->video_size = avctx->width * avctx->height;
100  for (i = 0; i < 4; i++) {
101  c->frame[i] = av_mallocz(c->frame_size);
102  if (!c->frame[i]) {
103  paf_video_close(avctx);
104  return AVERROR(ENOMEM);
105  }
106  }
107 
108  return 0;
109 }
110 
112 {
113  int i;
114 
115  for (i = 0; i < 4; i++) {
116  bytestream2_get_buffer(&c->gb, dst, 4);
117  dst += width;
118  }
119 }
120 
122 {
123  int i;
124 
125  for (i = 0; i < 4; i++) {
126  if (mask & (1 << 7 - i))
127  dst[i] = color;
128  if (mask & (1 << 3 - i))
129  dst[width + i] = color;
130  }
131 }
132 
133 static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
134 {
135  int i;
136 
137  for (i = 0; i < 4; i++) {
138  if (mask & (1 << 7 - i))
139  dst[i] = src[i];
140  if (mask & (1 << 3 - i))
141  dst[width + i] = src[width + i];
142  }
143 }
144 
146  const uint8_t **p,
147  const uint8_t **pend)
148 {
149  int val = bytestream2_get_be16(&c->gb);
150  int page = val >> 14;
151  int x = (val & 0x7F);
152  int y = ((val >> 7) & 0x7F);
153 
154  *p = c->frame[page] + x * 2 + y * 2 * c->width;
155  *pend = c->frame[page] + c->frame_size;
156 }
157 
159 {
160  uint32_t opcode_size, offset;
161  uint8_t *dst, *dend, mask = 0, color = 0;
162  const uint8_t *src, *send, *opcodes;
163  int i, j, op = 0;
164 
165  i = bytestream2_get_byte(&c->gb);
166  if (i) {
167  if (code & 0x10) {
168  int align;
169 
170  align = bytestream2_tell(&c->gb) & 3;
171  if (align)
172  bytestream2_skip(&c->gb, 4 - align);
173  }
174  do {
175  int page, val, x, y;
176  val = bytestream2_get_be16(&c->gb);
177  page = val >> 14;
178  x = (val & 0x7F) * 2;
179  y = ((val >> 7) & 0x7F) * 2;
180  dst = c->frame[page] + x + y * c->width;
181  dend = c->frame[page] + c->frame_size;
182  offset = (x & 0x7F) * 2;
183  j = bytestream2_get_le16(&c->gb) + offset;
184  if (bytestream2_get_bytes_left(&c->gb) < (j - offset) * 16)
185  return AVERROR_INVALIDDATA;
186  do {
187  offset++;
188  if (dst + 3 * c->width + 4 > dend)
189  return AVERROR_INVALIDDATA;
190  read4x4block(c, dst, c->width);
191  if ((offset & 0x3F) == 0)
192  dst += c->width * 3;
193  dst += 4;
194  } while (offset < j);
195  } while (--i);
196  }
197 
198  dst = c->frame[c->current_frame];
199  dend = c->frame[c->current_frame] + c->frame_size;
200  do {
201  set_src_position(c, &src, &send);
202  if ((src + 3 * c->width + 4 > send) ||
203  (dst + 3 * c->width + 4 > dend) ||
205  return AVERROR_INVALIDDATA;
206  copy_block4(dst, src, c->width, c->width, 4);
207  i++;
208  if ((i & 0x3F) == 0)
209  dst += c->width * 3;
210  dst += 4;
211  } while (i < c->video_size / 16);
212 
213  opcode_size = bytestream2_get_le16(&c->gb);
214  bytestream2_skip(&c->gb, 2);
215 
216  if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
217  return AVERROR_INVALIDDATA;
218 
219  opcodes = pkt + bytestream2_tell(&c->gb);
220  bytestream2_skipu(&c->gb, opcode_size);
221 
222  dst = c->frame[c->current_frame];
223 
224  for (i = 0; i < c->height; i += 4, dst += c->width * 3)
225  for (j = 0; j < c->width; j += 4, dst += 4) {
226  int opcode, k = 0;
227  if (op > opcode_size)
228  return AVERROR_INVALIDDATA;
229  if (j & 4) {
230  opcode = opcodes[op] & 15;
231  op++;
232  } else {
233  opcode = opcodes[op] >> 4;
234  }
235 
236  while (block_sequences[opcode][k]) {
237  offset = c->width * 2;
238  code = block_sequences[opcode][k++];
239 
240  switch (code) {
241  case 2:
242  offset = 0;
243  case 3:
244  color = bytestream2_get_byte(&c->gb);
245  case 4:
246  mask = bytestream2_get_byte(&c->gb);
247  copy_color_mask(dst + offset, c->width, mask, color);
248  break;
249  case 5:
250  offset = 0;
251  case 6:
252  set_src_position(c, &src, &send);
253  case 7:
254  if (src + offset + c->width + 4 > send)
255  return AVERROR_INVALIDDATA;
256  mask = bytestream2_get_byte(&c->gb);
257  copy_src_mask(dst + offset, c->width, mask, src + offset);
258  break;
259  }
260  }
261  }
262 
263  return 0;
264 }
265 
266 static int paf_video_decode(AVCodecContext *avctx, void *data,
267  int *got_frame, AVPacket *pkt)
268 {
269  PAFVideoDecContext *c = avctx->priv_data;
270  uint8_t code, *dst, *end;
271  int i, frame, ret;
272 
273  if (pkt->size < 2)
274  return AVERROR_INVALIDDATA;
275 
276  bytestream2_init(&c->gb, pkt->data, pkt->size);
277 
278  code = bytestream2_get_byte(&c->gb);
279  if ((code & 0xF) > 4 || (code & 0xF) == 3) {
280  avpriv_request_sample(avctx, "unknown/invalid code");
281  return AVERROR_INVALIDDATA;
282  }
283 
284  if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
285  return ret;
286 
287  if (code & 0x20) { // frame is keyframe
288  for (i = 0; i < 4; i++)
289  memset(c->frame[i], 0, c->frame_size);
290 
291  memset(c->pic->data[1], 0, AVPALETTE_SIZE);
292  c->current_frame = 0;
293  c->pic->key_frame = 1;
295  } else {
296  c->pic->key_frame = 0;
298  }
299 
300  if (code & 0x40) { // palette update
301  uint32_t *out = (uint32_t *)c->pic->data[1];
302  int index, count;
303 
304  index = bytestream2_get_byte(&c->gb);
305  count = bytestream2_get_byte(&c->gb) + 1;
306 
307  if (index + count > 256)
308  return AVERROR_INVALIDDATA;
309  if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
310  return AVERROR_INVALIDDATA;
311 
312  out += index;
313  for (i = 0; i < count; i++) {
314  unsigned r, g, b;
315 
316  r = bytestream2_get_byteu(&c->gb);
317  r = r << 2 | r >> 4;
318  g = bytestream2_get_byteu(&c->gb);
319  g = g << 2 | g >> 4;
320  b = bytestream2_get_byteu(&c->gb);
321  b = b << 2 | b >> 4;
322  *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
323  }
324  c->pic->palette_has_changed = 1;
325  }
326 
327  switch (code & 0x0F) {
328  case 0:
329  /* Block-based motion compensation using 4x4 blocks with either
330  * horizontal or vertical vectors; might incorporate VQ as well. */
331  if ((ret = decode_0(c, pkt->data, code)) < 0)
332  return ret;
333  break;
334  case 1:
335  /* Uncompressed data. This mode specifies that (width * height) bytes
336  * should be copied directly from the encoded buffer into the output. */
337  dst = c->frame[c->current_frame];
338  // possibly chunk length data
339  bytestream2_skip(&c->gb, 2);
341  return AVERROR_INVALIDDATA;
342  bytestream2_get_bufferu(&c->gb, dst, c->video_size);
343  break;
344  case 2:
345  /* Copy reference frame: Consume the next byte in the stream as the
346  * reference frame (which should be 0, 1, 2, or 3, and should not be
347  * the same as the current frame number). */
348  frame = bytestream2_get_byte(&c->gb);
349  if (frame > 3)
350  return AVERROR_INVALIDDATA;
351  if (frame != c->current_frame)
352  memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
353  break;
354  case 4:
355  /* Run length encoding.*/
356  dst = c->frame[c->current_frame];
357  end = dst + c->video_size;
358 
359  bytestream2_skip(&c->gb, 2);
360 
361  while (dst < end) {
362  int8_t code;
363  int count;
364 
365  if (bytestream2_get_bytes_left(&c->gb) < 2)
366  return AVERROR_INVALIDDATA;
367 
368  code = bytestream2_get_byteu(&c->gb);
369  count = FFABS(code) + 1;
370 
371  if (dst + count > end)
372  return AVERROR_INVALIDDATA;
373  if (code < 0)
374  memset(dst, bytestream2_get_byteu(&c->gb), count);
375  else
376  bytestream2_get_buffer(&c->gb, dst, count);
377  dst += count;
378  }
379  break;
380  default:
381  av_assert0(0);
382  }
383 
384  av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
385  c->frame[c->current_frame], c->width,
386  c->width, c->height);
387 
388  c->current_frame = (c->current_frame + 1) & 3;
389  if ((ret = av_frame_ref(data, c->pic)) < 0)
390  return ret;
391 
392  *got_frame = 1;
393 
394  return pkt->size;
395 }
396 
398  .name = "paf_video",
399  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
400  .type = AVMEDIA_TYPE_VIDEO,
401  .id = AV_CODEC_ID_PAF_VIDEO,
402  .priv_data_size = sizeof(PAFVideoDecContext),
403  .init = paf_video_init,
404  .close = paf_video_close,
406  .capabilities = AV_CODEC_CAP_DR1,
407 };
static av_cold int paf_video_close(AVCodecContext *avctx)
Definition: pafvideo.c:64
const char const char void * val
Definition: avisynth_c.h:771
#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:201
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
misc image utilities
uint8_t * frame[4]
Definition: pafvideo.c:57
const char * g
Definition: vf_curves.c:112
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1680
const char * b
Definition: vf_curves.c:113
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1989
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static AVPacket pkt
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3739
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:1718
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
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:150
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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:395
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1679
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:61
#define U(x)
Definition: vp56_arith.h:37
#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:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
const char * r
Definition: vf_curves.c:111
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:266
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:3746
AVFrame * pic
Definition: pafvideo.c:50
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:284
static const uint8_t block_sequences[16][8]
Definition: pafvideo.c:30
int width
picture width / height.
Definition: avcodec.h:1948
#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:145
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
const AVS_VideoInfo int align
Definition: avisynth_c.h:795
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
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:1761
AVCodec ff_paf_video_decoder
Definition: pafvideo.c:397
int index
Definition: gxfenc.c:89
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:358
static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
Definition: pafvideo.c:133
static av_cold int paf_video_init(AVCodecContext *avctx)
Definition: pafvideo.c:77
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
Definition: pafvideo.c:121
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.
static double c[64]
void * priv_data
Definition: avcodec.h:1803
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:279
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:337
static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
Definition: pafvideo.c:111
static int decode_0(PAFVideoDecContext *c, uint8_t *pkt, uint8_t code)
Definition: pafvideo.c:158
This structure stores compressed data.
Definition: avcodec.h:1656
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:1002
Predicted.
Definition: avutil.h:275