2 * Packed Animation File video and audio decoder
3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/paf.h"
24 #include "bytestream.h"
26 #include "copy_block.h"
30 static const uint8_t block_sequences[16][8] =
32 { 0, 0, 0, 0, 0, 0, 0, 0 },
33 { 2, 0, 0, 0, 0, 0, 0, 0 },
34 { 5, 7, 0, 0, 0, 0, 0, 0 },
35 { 5, 0, 0, 0, 0, 0, 0, 0 },
36 { 6, 0, 0, 0, 0, 0, 0, 0 },
37 { 5, 7, 5, 7, 0, 0, 0, 0 },
38 { 5, 7, 5, 0, 0, 0, 0, 0 },
39 { 5, 7, 6, 0, 0, 0, 0, 0 },
40 { 5, 5, 0, 0, 0, 0, 0, 0 },
41 { 3, 0, 0, 0, 0, 0, 0, 0 },
42 { 6, 6, 0, 0, 0, 0, 0, 0 },
43 { 2, 4, 0, 0, 0, 0, 0, 0 },
44 { 2, 4, 5, 7, 0, 0, 0, 0 },
45 { 2, 4, 5, 0, 0, 0, 0, 0 },
46 { 2, 4, 6, 0, 0, 0, 0, 0 },
47 { 2, 4, 5, 7, 5, 7, 0, 0 }
50 typedef struct PAFVideoDecContext {
62 static av_cold int paf_vid_init(AVCodecContext *avctx)
64 PAFVideoDecContext *c = avctx->priv_data;
67 if (avctx->height & 3 || avctx->width & 3) {
68 av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
69 return AVERROR_INVALIDDATA;
72 avctx->pix_fmt = AV_PIX_FMT_PAL8;
74 avcodec_get_frame_defaults(&c->pic);
75 c->frame_size = FFALIGN(avctx->height, 256) * avctx->width;
76 c->video_size = avctx->height * avctx->width;
77 for (i = 0; i < 4; i++) {
78 c->frame[i] = av_mallocz(c->frame_size);
80 return AVERROR(ENOMEM);
86 static int get_video_page_offset(AVCodecContext *avctx, uint8_t a, uint8_t b)
91 y = ((a & 0x3F) << 1) | (b >> 7 & 1);
93 return y * 2 * avctx->width + x * 2;
96 static void copy4h(AVCodecContext *avctx, uint8_t *dst)
98 PAFVideoDecContext *c = avctx->priv_data;
101 for (i = 0; i < 4; i++) {
102 bytestream2_get_buffer(&c->gb, dst, 4);
107 static void copy_color_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, uint8_t color)
111 for (i = 0; i < 4; i++) {
112 if ((mask >> 4) & (1 << (3 - i)))
114 if ((mask & 15) & (1 << (3 - i)))
115 dst[avctx->width + i] = color;
119 static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
123 for (i = 0; i < 4; i++) {
124 if ((mask >> 4) & (1 << (3 - i)))
126 if ((mask & 15) & (1 << (3 - i)))
127 dst[avctx->width + i] = src[avctx->width + i];
131 static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
133 PAFVideoDecContext *c = avctx->priv_data;
134 uint32_t opcode_size, offset;
135 uint8_t *dst, *dend, mask = 0, color = 0, a, b, p;
136 const uint8_t *src, *send, *opcodes;
139 i = bytestream2_get_byte(&c->gb);
144 align = bytestream2_tell(&c->gb) & 3;
146 bytestream2_skip(&c->gb, 4 - align);
149 a = bytestream2_get_byte(&c->gb);
150 b = bytestream2_get_byte(&c->gb);
152 dst = c->frame[p] + get_video_page_offset(avctx, a, b);
153 dend = c->frame[p] + c->frame_size;
154 offset = (b & 0x7F) * 2;
155 j = bytestream2_get_le16(&c->gb) + offset;
159 if (dst + 3 * avctx->width + 4 > dend)
160 return AVERROR_INVALIDDATA;
162 if ((offset & 0x3F) == 0)
163 dst += avctx->width * 3;
165 } while (offset < j);
169 dst = c->frame[c->current_frame];
170 dend = c->frame[c->current_frame] + c->frame_size;
172 a = bytestream2_get_byte(&c->gb);
173 b = bytestream2_get_byte(&c->gb);
175 src = c->frame[p] + get_video_page_offset(avctx, a, b);
176 send = c->frame[p] + c->frame_size;
177 if ((src + 3 * avctx->width + 4 > send) ||
178 (dst + 3 * avctx->width + 4 > dend))
179 return AVERROR_INVALIDDATA;
180 copy_block4(dst, src, avctx->width, avctx->width, 4);
183 dst += avctx->width * 3;
185 } while (i < c->video_size / 16);
187 opcode_size = bytestream2_get_le16(&c->gb);
188 bytestream2_skip(&c->gb, 2);
190 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
191 return AVERROR_INVALIDDATA;
193 opcodes = pkt + bytestream2_tell(&c->gb);
194 bytestream2_skipu(&c->gb, opcode_size);
196 dst = c->frame[c->current_frame];
198 for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
199 for (j = 0; j < avctx->width; j += 4, dst += 4) {
203 return AVERROR_INVALIDDATA;
205 opcode = opcodes[x] & 15;
208 opcode = opcodes[x] >> 4;
211 while (block_sequences[opcode][k]) {
213 offset = avctx->width * 2;
214 code = block_sequences[opcode][k++];
220 color = bytestream2_get_byte(&c->gb);
222 mask = bytestream2_get_byte(&c->gb);
223 copy_color_mask(avctx, mask, dst + offset, color);
228 a = bytestream2_get_byte(&c->gb);
229 b = bytestream2_get_byte(&c->gb);
231 src = c->frame[p] + get_video_page_offset(avctx, a, b);
232 send = c->frame[p] + c->frame_size;
234 if (src + offset + avctx->width + 4 > send)
235 return AVERROR_INVALIDDATA;
236 mask = bytestream2_get_byte(&c->gb);
237 copy_src_mask(avctx, mask, dst + offset, src + offset);
247 static int paf_vid_decode(AVCodecContext *avctx, void *data,
248 int *got_frame, AVPacket *pkt)
250 PAFVideoDecContext *c = avctx->priv_data;
251 uint8_t code, *dst, *src, *end;
254 c->pic.reference = 3;
255 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
258 bytestream2_init(&c->gb, pkt->data, pkt->size);
260 code = bytestream2_get_byte(&c->gb);
262 for (i = 0; i < 4; i++)
263 memset(c->frame[i], 0, c->frame_size);
265 memset(c->pic.data[1], 0, AVPALETTE_SIZE);
266 c->current_frame = 0;
267 c->pic.key_frame = 1;
268 c->pic.pict_type = AV_PICTURE_TYPE_I;
270 c->pic.key_frame = 0;
271 c->pic.pict_type = AV_PICTURE_TYPE_P;
275 uint32_t *out = (uint32_t *)c->pic.data[1];
278 index = bytestream2_get_byte(&c->gb);
279 count = bytestream2_get_byte(&c->gb) + 1;
281 if (index + count > 256)
282 return AVERROR_INVALIDDATA;
283 if (bytestream2_get_bytes_left(&c->gb) < 3*count)
284 return AVERROR_INVALIDDATA;
287 for (i = 0; i < count; i++) {
290 r = bytestream2_get_byteu(&c->gb);
292 g = bytestream2_get_byteu(&c->gb);
294 b = bytestream2_get_byteu(&c->gb);
296 *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
298 c->pic.palette_has_changed = 1;
301 switch (code & 0x0F) {
303 if ((ret = decode_0(avctx, code, pkt->data)) < 0)
307 dst = c->frame[c->current_frame];
308 bytestream2_skip(&c->gb, 2);
309 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
310 return AVERROR_INVALIDDATA;
311 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
314 frame = bytestream2_get_byte(&c->gb);
316 return AVERROR_INVALIDDATA;
317 if (frame != c->current_frame)
318 memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
321 dst = c->frame[c->current_frame];
322 end = dst + c->video_size;
324 bytestream2_skip(&c->gb, 2);
330 if (bytestream2_get_bytes_left(&c->gb) < 2)
331 return AVERROR_INVALIDDATA;
333 code = bytestream2_get_byteu(&c->gb);
334 count = FFABS(code) + 1;
336 if (dst + count > end)
337 return AVERROR_INVALIDDATA;
339 memset(dst, bytestream2_get_byteu(&c->gb), count);
341 bytestream2_get_buffer(&c->gb, dst, count);
346 av_log_ask_for_sample(avctx, "unknown/invalid code\n");
347 return AVERROR_INVALIDDATA;
350 dst = c->pic.data[0];
351 src = c->frame[c->current_frame];
352 for (i = 0; i < avctx->height; i++) {
353 memcpy(dst, src, avctx->width);
354 dst += c->pic.linesize[0];
358 c->current_frame = (c->current_frame + 1) & 3;
361 *(AVFrame *)data = c->pic;
366 static av_cold int paf_vid_close(AVCodecContext *avctx)
368 PAFVideoDecContext *c = avctx->priv_data;
372 avctx->release_buffer(avctx, &c->pic);
374 for (i = 0; i < 4; i++)
375 av_freep(&c->frame[i]);
380 typedef struct PAFAudioDecContext {
382 } PAFAudioDecContext;
384 static av_cold int paf_aud_init(AVCodecContext *avctx)
386 PAFAudioDecContext *c = avctx->priv_data;
388 if (avctx->channels != 2) {
389 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
390 return AVERROR_INVALIDDATA;
393 avcodec_get_frame_defaults(&c->frame);
394 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
395 avctx->coded_frame = &c->frame;
396 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
401 static int paf_aud_decode(AVCodecContext *avctx, void *data,
402 int *got_frame_ptr, AVPacket *pkt)
404 PAFAudioDecContext *c = avctx->priv_data;
405 uint8_t *buf = pkt->data;
406 int16_t *output_samples;
408 int frames, ret, i, j, k;
410 frames = pkt->size / PAF_SOUND_FRAME_SIZE;
412 return AVERROR_INVALIDDATA;
414 c->frame.nb_samples = PAF_SOUND_SAMPLES * frames;
415 if ((ret = ff_get_buffer(avctx, &c->frame)) < 0)
418 output_samples = (int16_t *)c->frame.data[0];
419 for (i = 0; i < frames; i++) {
420 t = buf + 256 * sizeof(uint16_t);
421 for (j = 0; j < PAF_SOUND_SAMPLES; j++) {
422 for (k = 0; k < 2; k++) {
423 *output_samples++ = AV_RL16(buf + *t * 2);
427 buf += PAF_SOUND_FRAME_SIZE;
431 *(AVFrame *)data = c->frame;
436 AVCodec ff_paf_video_decoder = {
438 .type = AVMEDIA_TYPE_VIDEO,
439 .id = AV_CODEC_ID_PAF_VIDEO,
440 .priv_data_size = sizeof(PAFVideoDecContext),
441 .init = paf_vid_init,
442 .close = paf_vid_close,
443 .decode = paf_vid_decode,
444 .capabilities = CODEC_CAP_DR1,
445 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
448 AVCodec ff_paf_audio_decoder = {
450 .type = AVMEDIA_TYPE_AUDIO,
451 .id = AV_CODEC_ID_PAF_AUDIO,
452 .priv_data_size = sizeof(PAFAudioDecContext),
453 .init = paf_aud_init,
454 .decode = paf_aud_decode,
455 .capabilities = CODEC_CAP_DR1,
456 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),