2 * Packed Animation File video decoder
3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/imgutils.h"
25 #include "bytestream.h"
26 #include "copy_block.h"
29 static const uint8_t block_sequences[16][8] = {
30 { 0, 0, 0, 0, 0, 0, 0, 0 }, { 2, 0, 0, 0, 0, 0, 0, 0 },
31 { 5, 7, 0, 0, 0, 0, 0, 0 }, { 5, 0, 0, 0, 0, 0, 0, 0 },
32 { 6, 0, 0, 0, 0, 0, 0, 0 }, { 5, 7, 5, 7, 0, 0, 0, 0 },
33 { 5, 7, 5, 0, 0, 0, 0, 0 }, { 5, 7, 6, 0, 0, 0, 0, 0 },
34 { 5, 5, 0, 0, 0, 0, 0, 0 }, { 3, 0, 0, 0, 0, 0, 0, 0 },
35 { 6, 6, 0, 0, 0, 0, 0, 0 }, { 2, 4, 0, 0, 0, 0, 0, 0 },
36 { 2, 4, 5, 7, 0, 0, 0, 0 }, { 2, 4, 5, 0, 0, 0, 0, 0 },
37 { 2, 4, 6, 0, 0, 0, 0, 0 }, { 2, 4, 5, 7, 5, 7, 0, 0 },
40 typedef struct PAFVideoDecContext {
55 static av_cold int paf_video_close(AVCodecContext *avctx)
57 PAFVideoDecContext *c = avctx->priv_data;
60 av_frame_free(&c->pic);
62 for (i = 0; i < 4; i++)
63 av_freep(&c->frame[i]);
68 static av_cold int paf_video_init(AVCodecContext *avctx)
70 PAFVideoDecContext *c = avctx->priv_data;
73 c->width = avctx->width;
74 c->height = avctx->height;
76 if (avctx->height & 3 || avctx->width & 3) {
77 av_log(avctx, AV_LOG_ERROR,
78 "width %d and height %d must be multiplie of 4.\n",
79 avctx->width, avctx->height);
80 return AVERROR_INVALIDDATA;
83 avctx->pix_fmt = AV_PIX_FMT_PAL8;
85 c->pic = av_frame_alloc();
87 return AVERROR(ENOMEM);
89 c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
90 c->video_size = avctx->width * avctx->height;
91 for (i = 0; i < 4; i++) {
92 c->frame[i] = av_mallocz(c->frame_size);
94 paf_video_close(avctx);
95 return AVERROR(ENOMEM);
102 static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
106 for (i = 0; i < 4; i++) {
107 bytestream2_get_buffer(&c->gb, dst, 4);
112 static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
116 for (i = 0; i < 4; i++) {
117 if (mask & (1 << 7 - i))
119 if (mask & (1 << 3 - i))
120 dst[width + i] = color;
124 static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
128 for (i = 0; i < 4; i++) {
129 if (mask & (1 << 7 - i))
131 if (mask & (1 << 3 - i))
132 dst[width + i] = src[width + i];
136 static void set_src_position(PAFVideoDecContext *c,
138 const uint8_t **pend)
140 int val = bytestream2_get_be16(&c->gb);
141 int page = val >> 14;
142 int x = (val & 0x7F);
143 int y = ((val >> 7) & 0x7F);
145 *p = c->frame[page] + x * 2 + y * 2 * c->width;
146 *pend = c->frame[page] + c->frame_size;
149 static int decode_0(PAFVideoDecContext *c, uint8_t *pkt, uint8_t code)
151 uint32_t opcode_size, offset;
152 uint8_t *dst, *dend, mask = 0, color = 0;
153 const uint8_t *src, *send, *opcodes;
156 i = bytestream2_get_byte(&c->gb);
159 int pos = bytestream2_tell(&c->gb) & 3;
161 bytestream2_skip(&c->gb, 4 - pos);
165 val = bytestream2_get_be16(&c->gb);
167 x = (val & 0x7F) * 2;
168 y = ((val >> 7) & 0x7F) * 2;
169 dst = c->frame[page] + x + y * c->width;
170 dend = c->frame[page] + c->frame_size;
171 offset = (x & 0x7F) * 2;
172 j = bytestream2_get_le16(&c->gb) + offset;
175 if (dst + 3 * c->width + 4 > dend)
176 return AVERROR_INVALIDDATA;
177 read4x4block(c, dst, c->width);
178 if ((offset & 0x3F) == 0)
181 } while (offset < j);
185 dst = c->frame[c->current_frame];
186 dend = c->frame[c->current_frame] + c->frame_size;
188 set_src_position(c, &src, &send);
189 if ((src + 3 * c->width + 4 > send) ||
190 (dst + 3 * c->width + 4 > dend))
191 return AVERROR_INVALIDDATA;
192 copy_block4(dst, src, c->width, c->width, 4);
197 } while (i < c->video_size / 16);
199 opcode_size = bytestream2_get_le16(&c->gb);
200 bytestream2_skip(&c->gb, 2);
202 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
203 return AVERROR_INVALIDDATA;
205 opcodes = pkt + bytestream2_tell(&c->gb);
206 bytestream2_skipu(&c->gb, opcode_size);
208 dst = c->frame[c->current_frame];
210 for (i = 0; i < c->height; i += 4, dst += c->width * 3)
211 for (j = 0; j < c->width; j += 4, dst += 4) {
213 if (op > opcode_size)
214 return AVERROR_INVALIDDATA;
216 opcode = opcodes[op] & 15;
219 opcode = opcodes[op] >> 4;
222 while (block_sequences[opcode][k]) {
223 offset = c->width * 2;
224 code = block_sequences[opcode][k++];
230 color = bytestream2_get_byte(&c->gb);
232 mask = bytestream2_get_byte(&c->gb);
233 copy_color_mask(dst + offset, c->width, mask, color);
238 set_src_position(c, &src, &send);
240 if (src + offset + c->width + 4 > send)
241 return AVERROR_INVALIDDATA;
242 mask = bytestream2_get_byte(&c->gb);
243 copy_src_mask(dst + offset, c->width, mask, src + offset);
252 static int paf_video_decode(AVCodecContext *avctx, void *data,
253 int *got_frame, AVPacket *pkt)
255 PAFVideoDecContext *c = avctx->priv_data;
256 uint8_t code, *dst, *end;
259 if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
262 bytestream2_init(&c->gb, pkt->data, pkt->size);
264 code = bytestream2_get_byte(&c->gb);
265 if (code & 0x20) { // frame is keyframe
266 for (i = 0; i < 4; i++)
267 memset(c->frame[i], 0, c->frame_size);
269 memset(c->pic->data[1], 0, AVPALETTE_SIZE);
270 c->current_frame = 0;
271 c->pic->key_frame = 1;
272 c->pic->pict_type = AV_PICTURE_TYPE_I;
274 c->pic->key_frame = 0;
275 c->pic->pict_type = AV_PICTURE_TYPE_P;
278 if (code & 0x40) { // palette update
279 uint32_t *out = (uint32_t *)c->pic->data[1];
282 index = bytestream2_get_byte(&c->gb);
283 count = bytestream2_get_byte(&c->gb) + 1;
285 if (index + count > 256)
286 return AVERROR_INVALIDDATA;
287 if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
288 return AVERROR_INVALIDDATA;
291 for (i = 0; i < count; i++) {
294 r = bytestream2_get_byteu(&c->gb);
296 g = bytestream2_get_byteu(&c->gb);
298 b = bytestream2_get_byteu(&c->gb);
300 *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
302 c->pic->palette_has_changed = 1;
305 switch (code & 0x0F) {
307 /* Block-based motion compensation using 4x4 blocks with either
308 * horizontal or vertical vectors; might incorporate VQ as well. */
309 if ((ret = decode_0(c, pkt->data, code)) < 0)
313 /* Uncompressed data. This mode specifies that (width * height) bytes
314 * should be copied directly from the encoded buffer into the output. */
315 dst = c->frame[c->current_frame];
316 // possibly chunk length data
317 bytestream2_skip(&c->gb, 2);
318 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
319 return AVERROR_INVALIDDATA;
320 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
323 /* Copy reference frame: Consume the next byte in the stream as the
324 * reference frame (which should be 0, 1, 2, or 3, and should not be
325 * the same as the current frame number). */
326 frame = bytestream2_get_byte(&c->gb);
328 return AVERROR_INVALIDDATA;
329 if (frame != c->current_frame)
330 memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
333 /* Run length encoding.*/
334 dst = c->frame[c->current_frame];
335 end = dst + c->video_size;
337 bytestream2_skip(&c->gb, 2);
343 if (bytestream2_get_bytes_left(&c->gb) < 2)
344 return AVERROR_INVALIDDATA;
346 code = bytestream2_get_byteu(&c->gb);
347 count = FFABS(code) + 1;
349 if (dst + count > end)
350 return AVERROR_INVALIDDATA;
352 memset(dst, bytestream2_get_byteu(&c->gb), count);
354 bytestream2_get_buffer(&c->gb, dst, count);
359 avpriv_request_sample(avctx, "unknown/invalid code");
360 return AVERROR_INVALIDDATA;
363 av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
364 c->frame[c->current_frame], c->width,
365 c->width, c->height);
367 c->current_frame = (c->current_frame + 1) & 3;
368 if ((ret = av_frame_ref(data, c->pic)) < 0)
376 AVCodec ff_paf_video_decoder = {
378 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
379 .type = AVMEDIA_TYPE_VIDEO,
380 .id = AV_CODEC_ID_PAF_VIDEO,
381 .priv_data_size = sizeof(PAFVideoDecContext),
382 .init = paf_video_init,
383 .close = paf_video_close,
384 .decode = paf_video_decode,
385 .capabilities = CODEC_CAP_DR1,