2 * Packed Animation File video and audio 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"
23 #include "libavutil/intreadwrite.h"
26 #include "bytestream.h"
27 #include "copy_block.h"
31 #define PAF_SOUND_SAMPLES 2205
32 #define PAF_SOUND_FRAME_SIZE ((256 + PAF_SOUND_SAMPLES) * 2)
34 static const uint8_t block_sequences[16][8] = {
35 { 0, 0, 0, 0, 0, 0, 0, 0 }, { 2, 0, 0, 0, 0, 0, 0, 0 },
36 { 5, 7, 0, 0, 0, 0, 0, 0 }, { 5, 0, 0, 0, 0, 0, 0, 0 },
37 { 6, 0, 0, 0, 0, 0, 0, 0 }, { 5, 7, 5, 7, 0, 0, 0, 0 },
38 { 5, 7, 5, 0, 0, 0, 0, 0 }, { 5, 7, 6, 0, 0, 0, 0, 0 },
39 { 5, 5, 0, 0, 0, 0, 0, 0 }, { 3, 0, 0, 0, 0, 0, 0, 0 },
40 { 6, 6, 0, 0, 0, 0, 0, 0 }, { 2, 4, 0, 0, 0, 0, 0, 0 },
41 { 2, 4, 5, 7, 0, 0, 0, 0 }, { 2, 4, 5, 0, 0, 0, 0, 0 },
42 { 2, 4, 6, 0, 0, 0, 0, 0 }, { 2, 4, 5, 7, 5, 7, 0, 0 },
45 typedef struct PAFVideoDecContext {
60 static av_cold int paf_video_close(AVCodecContext *avctx)
62 PAFVideoDecContext *c = avctx->priv_data;
65 av_frame_free(&c->pic);
67 for (i = 0; i < 4; i++)
68 av_freep(&c->frame[i]);
73 static av_cold int paf_video_init(AVCodecContext *avctx)
75 PAFVideoDecContext *c = avctx->priv_data;
78 c->width = avctx->width;
79 c->height = avctx->height;
81 if (avctx->height & 3 || avctx->width & 3) {
82 av_log(avctx, AV_LOG_ERROR,
83 "width %d and height %d must be multiplie of 4.\n",
84 avctx->width, avctx->height);
85 return AVERROR_INVALIDDATA;
88 avctx->pix_fmt = AV_PIX_FMT_PAL8;
90 c->pic = av_frame_alloc();
92 return AVERROR(ENOMEM);
94 c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
95 c->video_size = avctx->width * avctx->height;
96 for (i = 0; i < 4; i++) {
97 c->frame[i] = av_mallocz(c->frame_size);
99 paf_video_close(avctx);
100 return AVERROR(ENOMEM);
107 static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
111 for (i = 0; i < 4; i++) {
112 bytestream2_get_buffer(&c->gb, dst, 4);
117 static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
121 for (i = 0; i < 4; i++) {
122 if (mask & (1 << 7 - i))
124 if (mask & (1 << 3 - i))
125 dst[width + i] = color;
129 static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
133 for (i = 0; i < 4; i++) {
134 if (mask & (1 << 7 - i))
136 if (mask & (1 << 3 - i))
137 dst[width + i] = src[width + i];
141 static void set_src_position(PAFVideoDecContext *c,
143 const uint8_t **pend)
145 int val = bytestream2_get_be16(&c->gb);
146 int page = val >> 14;
147 int x = (val & 0x7F);
148 int y = ((val >> 7) & 0x7F);
150 *p = c->frame[page] + x * 2 + y * 2 * c->width;
151 *pend = c->frame[page] + c->frame_size;
154 static int decode_0(PAFVideoDecContext *c, uint8_t *pkt, uint8_t code)
156 uint32_t opcode_size, offset;
157 uint8_t *dst, *dend, mask = 0, color = 0;
158 const uint8_t *src, *send, *opcodes;
161 i = bytestream2_get_byte(&c->gb);
164 int pos = bytestream2_tell(&c->gb) & 3;
166 bytestream2_skip(&c->gb, 4 - pos);
170 val = bytestream2_get_be16(&c->gb);
172 x = (val & 0x7F) * 2;
173 y = ((val >> 7) & 0x7F) * 2;
174 dst = c->frame[page] + x + y * c->width;
175 dend = c->frame[page] + c->frame_size;
176 offset = (x & 0x7F) * 2;
177 j = bytestream2_get_le16(&c->gb) + offset;
180 if (dst + 3 * c->width + 4 > dend)
181 return AVERROR_INVALIDDATA;
182 read4x4block(c, dst, c->width);
183 if ((offset & 0x3F) == 0)
186 } while (offset < j);
190 dst = c->frame[c->current_frame];
191 dend = c->frame[c->current_frame] + c->frame_size;
193 set_src_position(c, &src, &send);
194 if ((src + 3 * c->width + 4 > send) ||
195 (dst + 3 * c->width + 4 > dend))
196 return AVERROR_INVALIDDATA;
197 copy_block4(dst, src, c->width, c->width, 4);
202 } while (i < c->video_size / 16);
204 opcode_size = bytestream2_get_le16(&c->gb);
205 bytestream2_skip(&c->gb, 2);
207 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
208 return AVERROR_INVALIDDATA;
210 opcodes = pkt + bytestream2_tell(&c->gb);
211 bytestream2_skipu(&c->gb, opcode_size);
213 dst = c->frame[c->current_frame];
215 for (i = 0; i < c->height; i += 4, dst += c->width * 3)
216 for (j = 0; j < c->width; j += 4, dst += 4) {
218 if (op > opcode_size)
219 return AVERROR_INVALIDDATA;
221 opcode = opcodes[op] & 15;
224 opcode = opcodes[op] >> 4;
227 while (block_sequences[opcode][k]) {
228 offset = c->width * 2;
229 code = block_sequences[opcode][k++];
235 color = bytestream2_get_byte(&c->gb);
237 mask = bytestream2_get_byte(&c->gb);
238 copy_color_mask(dst + offset, c->width, mask, color);
243 set_src_position(c, &src, &send);
245 if (src + offset + c->width + 4 > send)
246 return AVERROR_INVALIDDATA;
247 mask = bytestream2_get_byte(&c->gb);
248 copy_src_mask(dst + offset, c->width, mask, src + offset);
257 static int paf_video_decode(AVCodecContext *avctx, void *data,
258 int *got_frame, AVPacket *pkt)
260 PAFVideoDecContext *c = avctx->priv_data;
261 uint8_t code, *dst, *end;
264 if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
267 bytestream2_init(&c->gb, pkt->data, pkt->size);
269 code = bytestream2_get_byte(&c->gb);
270 if (code & 0x20) { // frame is keyframe
271 for (i = 0; i < 4; i++)
272 memset(c->frame[i], 0, c->frame_size);
274 memset(c->pic->data[1], 0, AVPALETTE_SIZE);
275 c->current_frame = 0;
276 c->pic->key_frame = 1;
277 c->pic->pict_type = AV_PICTURE_TYPE_I;
279 c->pic->key_frame = 0;
280 c->pic->pict_type = AV_PICTURE_TYPE_P;
283 if (code & 0x40) { // palette update
284 uint32_t *out = (uint32_t *)c->pic->data[1];
287 index = bytestream2_get_byte(&c->gb);
288 count = bytestream2_get_byte(&c->gb) + 1;
290 if (index + count > 256)
291 return AVERROR_INVALIDDATA;
292 if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
293 return AVERROR_INVALIDDATA;
296 for (i = 0; i < count; i++) {
299 r = bytestream2_get_byteu(&c->gb);
301 g = bytestream2_get_byteu(&c->gb);
303 b = bytestream2_get_byteu(&c->gb);
305 *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
307 c->pic->palette_has_changed = 1;
310 switch (code & 0x0F) {
312 /* Block-based motion compensation using 4x4 blocks with either
313 * horizontal or vertical vectors; might incorporate VQ as well. */
314 if ((ret = decode_0(c, pkt->data, code)) < 0)
318 /* Uncompressed data. This mode specifies that (width * height) bytes
319 * should be copied directly from the encoded buffer into the output. */
320 dst = c->frame[c->current_frame];
321 // possibly chunk length data
322 bytestream2_skip(&c->gb, 2);
323 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
324 return AVERROR_INVALIDDATA;
325 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
328 /* Copy reference frame: Consume the next byte in the stream as the
329 * reference frame (which should be 0, 1, 2, or 3, and should not be
330 * the same as the current frame number). */
331 frame = bytestream2_get_byte(&c->gb);
333 return AVERROR_INVALIDDATA;
334 if (frame != c->current_frame)
335 memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
338 /* Run length encoding.*/
339 dst = c->frame[c->current_frame];
340 end = dst + c->video_size;
342 bytestream2_skip(&c->gb, 2);
348 if (bytestream2_get_bytes_left(&c->gb) < 2)
349 return AVERROR_INVALIDDATA;
351 code = bytestream2_get_byteu(&c->gb);
352 count = FFABS(code) + 1;
354 if (dst + count > end)
355 return AVERROR_INVALIDDATA;
357 memset(dst, bytestream2_get_byteu(&c->gb), count);
359 bytestream2_get_buffer(&c->gb, dst, count);
364 avpriv_request_sample(avctx, "unknown/invalid code");
365 return AVERROR_INVALIDDATA;
368 av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
369 c->frame[c->current_frame], c->width,
370 c->width, c->height);
372 c->current_frame = (c->current_frame + 1) & 3;
373 if ((ret = av_frame_ref(data, c->pic)) < 0)
381 static av_cold int paf_audio_init(AVCodecContext *avctx)
383 if (avctx->channels != 2) {
384 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
385 return AVERROR_INVALIDDATA;
388 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
389 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
394 static int paf_audio_decode(AVCodecContext *avctx, void *data,
395 int *got_frame, AVPacket *pkt)
397 AVFrame *frame = data;
398 int16_t *output_samples;
399 const uint8_t *src = pkt->data;
400 int frames, ret, i, j;
403 frames = pkt->size / PAF_SOUND_FRAME_SIZE;
405 return AVERROR_INVALIDDATA;
407 frame->nb_samples = PAF_SOUND_SAMPLES * frames;
408 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
411 output_samples = (int16_t *)frame->data[0];
412 // codebook of 256 16-bit samples and 8-bit indices to it
413 for (j = 0; j < frames; j++) {
414 for (i = 0; i < 256; i++)
415 cb[i] = sign_extend(AV_RL16(src + i * 2), 16);
418 for (i = 0; i < PAF_SOUND_SAMPLES * 2; i++)
419 *output_samples++ = cb[*src++];
426 #if CONFIG_PAF_VIDEO_DECODER
427 AVCodec ff_paf_video_decoder = {
429 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
430 .type = AVMEDIA_TYPE_VIDEO,
431 .id = AV_CODEC_ID_PAF_VIDEO,
432 .priv_data_size = sizeof(PAFVideoDecContext),
433 .init = paf_video_init,
434 .close = paf_video_close,
435 .decode = paf_video_decode,
436 .capabilities = CODEC_CAP_DR1,
440 #if CONFIG_PAF_AUDIO_DECODER
441 AVCodec ff_paf_audio_decoder = {
443 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
444 .type = AVMEDIA_TYPE_AUDIO,
445 .id = AV_CODEC_ID_PAF_AUDIO,
446 .init = paf_audio_init,
447 .decode = paf_audio_decode,
448 .capabilities = CODEC_CAP_DR1,