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/dsputil.h"
24 #include "libavcodec/paf.h"
25 #include "bytestream.h"
27 #include "copy_block.h"
31 static const uint8_t block_sequences[16][8] =
33 { 0, 0, 0, 0, 0, 0, 0, 0 },
34 { 2, 0, 0, 0, 0, 0, 0, 0 },
35 { 5, 7, 0, 0, 0, 0, 0, 0 },
36 { 5, 0, 0, 0, 0, 0, 0, 0 },
37 { 6, 0, 0, 0, 0, 0, 0, 0 },
38 { 5, 7, 5, 7, 0, 0, 0, 0 },
39 { 5, 7, 5, 0, 0, 0, 0, 0 },
40 { 5, 7, 6, 0, 0, 0, 0, 0 },
41 { 5, 5, 0, 0, 0, 0, 0, 0 },
42 { 3, 0, 0, 0, 0, 0, 0, 0 },
43 { 6, 6, 0, 0, 0, 0, 0, 0 },
44 { 2, 4, 0, 0, 0, 0, 0, 0 },
45 { 2, 4, 5, 7, 0, 0, 0, 0 },
46 { 2, 4, 5, 0, 0, 0, 0, 0 },
47 { 2, 4, 6, 0, 0, 0, 0, 0 },
48 { 2, 4, 5, 7, 5, 7, 0, 0 }
51 typedef struct PAFVideoDecContext {
63 static av_cold int paf_vid_init(AVCodecContext *avctx)
65 PAFVideoDecContext *c = avctx->priv_data;
68 if (avctx->height & 3 || avctx->width & 3) {
69 av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
70 return AVERROR_INVALIDDATA;
73 avctx->pix_fmt = AV_PIX_FMT_PAL8;
75 avcodec_get_frame_defaults(&c->pic);
76 c->frame_size = FFALIGN(avctx->height, 256) * avctx->width;
77 c->video_size = avctx->height * avctx->width;
78 for (i = 0; i < 4; i++) {
79 c->frame[i] = av_mallocz(c->frame_size);
81 return AVERROR(ENOMEM);
87 static int get_video_page_offset(AVCodecContext *avctx, uint8_t a, uint8_t b)
92 y = ((a & 0x3F) << 1) | (b >> 7 & 1);
94 return y * 2 * avctx->width + x * 2;
97 static void copy4h(AVCodecContext *avctx, uint8_t *dst)
99 PAFVideoDecContext *c = avctx->priv_data;
102 for (i = 0; i < 4; i++) {
103 bytestream2_get_buffer(&c->gb, dst, 4);
108 static void copy_color_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, uint8_t color)
112 for (i = 0; i < 4; i++) {
113 if ((mask >> 4) & (1 << (3 - i)))
115 if ((mask & 15) & (1 << (3 - i)))
116 dst[avctx->width + i] = color;
120 static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
124 for (i = 0; i < 4; i++) {
125 if ((mask >> 4) & (1 << (3 - i)))
127 if ((mask & 15) & (1 << (3 - i)))
128 dst[avctx->width + i] = src[avctx->width + i];
132 static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
134 PAFVideoDecContext *c = avctx->priv_data;
135 uint32_t opcode_size, offset;
136 uint8_t *dst, *dend, mask = 0, color = 0, a, b, p;
137 const uint8_t *src, *send, *opcodes;
140 i = bytestream2_get_byte(&c->gb);
145 align = bytestream2_tell(&c->gb) & 3;
147 bytestream2_skip(&c->gb, 4 - align);
150 a = bytestream2_get_byte(&c->gb);
151 b = bytestream2_get_byte(&c->gb);
153 dst = c->frame[p] + get_video_page_offset(avctx, a, b);
154 dend = c->frame[p] + c->frame_size;
155 offset = (b & 0x7F) * 2;
156 j = bytestream2_get_le16(&c->gb) + offset;
160 if (dst + 3 * avctx->width + 4 > dend)
161 return AVERROR_INVALIDDATA;
163 if ((offset & 0x3F) == 0)
164 dst += avctx->width * 3;
166 } while (offset < j);
170 dst = c->frame[c->current_frame];
171 dend = c->frame[c->current_frame] + c->frame_size;
173 a = bytestream2_get_byte(&c->gb);
174 b = bytestream2_get_byte(&c->gb);
176 src = c->frame[p] + get_video_page_offset(avctx, a, b);
177 send = c->frame[p] + c->frame_size;
178 if ((src + 3 * avctx->width + 4 > send) ||
179 (dst + 3 * avctx->width + 4 > dend))
180 return AVERROR_INVALIDDATA;
181 copy_block4(dst, src, avctx->width, avctx->width, 4);
184 dst += avctx->width * 3;
186 } while (i < c->video_size / 16);
188 opcode_size = bytestream2_get_le16(&c->gb);
189 bytestream2_skip(&c->gb, 2);
191 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
192 return AVERROR_INVALIDDATA;
194 opcodes = pkt + bytestream2_tell(&c->gb);
195 bytestream2_skipu(&c->gb, opcode_size);
197 dst = c->frame[c->current_frame];
199 for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
200 for (j = 0; j < avctx->width; j += 4, dst += 4) {
204 return AVERROR_INVALIDDATA;
206 opcode = opcodes[x] & 15;
209 opcode = opcodes[x] >> 4;
212 while (block_sequences[opcode][k]) {
214 offset = avctx->width * 2;
215 code = block_sequences[opcode][k++];
221 color = bytestream2_get_byte(&c->gb);
223 mask = bytestream2_get_byte(&c->gb);
224 copy_color_mask(avctx, mask, dst + offset, color);
229 a = bytestream2_get_byte(&c->gb);
230 b = bytestream2_get_byte(&c->gb);
232 src = c->frame[p] + get_video_page_offset(avctx, a, b);
233 send = c->frame[p] + c->frame_size;
235 if (src + offset + avctx->width + 4 > send)
236 return AVERROR_INVALIDDATA;
237 mask = bytestream2_get_byte(&c->gb);
238 copy_src_mask(avctx, mask, dst + offset, src + offset);
248 static int paf_vid_decode(AVCodecContext *avctx, void *data,
249 int *got_frame, AVPacket *pkt)
251 PAFVideoDecContext *c = avctx->priv_data;
252 uint8_t code, *dst, *src, *end;
255 c->pic.reference = 3;
256 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
259 bytestream2_init(&c->gb, pkt->data, pkt->size);
261 code = bytestream2_get_byte(&c->gb);
263 for (i = 0; i < 4; i++)
264 memset(c->frame[i], 0, c->frame_size);
266 memset(c->pic.data[1], 0, AVPALETTE_SIZE);
267 c->current_frame = 0;
268 c->pic.key_frame = 1;
269 c->pic.pict_type = AV_PICTURE_TYPE_I;
271 c->pic.key_frame = 0;
272 c->pic.pict_type = AV_PICTURE_TYPE_P;
276 uint32_t *out = (uint32_t *)c->pic.data[1];
279 index = bytestream2_get_byte(&c->gb);
280 count = bytestream2_get_byte(&c->gb) + 1;
282 if (index + count > 256)
283 return AVERROR_INVALIDDATA;
284 if (bytestream2_get_bytes_left(&c->gb) < 3*count)
285 return AVERROR_INVALIDDATA;
288 for (i = 0; i < count; i++) {
291 r = bytestream2_get_byteu(&c->gb);
293 g = bytestream2_get_byteu(&c->gb);
295 b = bytestream2_get_byteu(&c->gb);
297 *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
299 c->pic.palette_has_changed = 1;
302 switch (code & 0x0F) {
304 if ((ret = decode_0(avctx, code, pkt->data)) < 0)
308 dst = c->frame[c->current_frame];
309 bytestream2_skip(&c->gb, 2);
310 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
311 return AVERROR_INVALIDDATA;
312 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
315 frame = bytestream2_get_byte(&c->gb);
317 return AVERROR_INVALIDDATA;
318 if (frame != c->current_frame)
319 memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
322 dst = c->frame[c->current_frame];
323 end = dst + c->video_size;
325 bytestream2_skip(&c->gb, 2);
331 if (bytestream2_get_bytes_left(&c->gb) < 2)
332 return AVERROR_INVALIDDATA;
334 code = bytestream2_get_byteu(&c->gb);
335 count = FFABS(code) + 1;
337 if (dst + count > end)
338 return AVERROR_INVALIDDATA;
340 memset(dst, bytestream2_get_byteu(&c->gb), count);
342 bytestream2_get_buffer(&c->gb, dst, count);
347 av_log_ask_for_sample(avctx, "unknown/invalid code\n");
348 return AVERROR_INVALIDDATA;
351 dst = c->pic.data[0];
352 src = c->frame[c->current_frame];
353 for (i = 0; i < avctx->height; i++) {
354 memcpy(dst, src, avctx->width);
355 dst += c->pic.linesize[0];
359 c->current_frame = (c->current_frame + 1) & 3;
362 *(AVFrame *)data = c->pic;
367 static av_cold int paf_vid_close(AVCodecContext *avctx)
369 PAFVideoDecContext *c = avctx->priv_data;
373 avctx->release_buffer(avctx, &c->pic);
375 for (i = 0; i < 4; i++)
376 av_freep(&c->frame[i]);
381 typedef struct PAFAudioDecContext {
383 } PAFAudioDecContext;
385 static av_cold int paf_aud_init(AVCodecContext *avctx)
387 PAFAudioDecContext *c = avctx->priv_data;
389 if (avctx->channels != 2) {
390 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
391 return AVERROR_INVALIDDATA;
394 avcodec_get_frame_defaults(&c->frame);
395 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
396 avctx->coded_frame = &c->frame;
397 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
402 static int paf_aud_decode(AVCodecContext *avctx, void *data,
403 int *got_frame_ptr, AVPacket *pkt)
405 PAFAudioDecContext *c = avctx->priv_data;
406 uint8_t *buf = pkt->data;
407 int16_t *output_samples;
409 int frames, ret, i, j, k;
411 frames = pkt->size / PAF_SOUND_FRAME_SIZE;
413 return AVERROR_INVALIDDATA;
415 c->frame.nb_samples = PAF_SOUND_SAMPLES * frames;
416 if ((ret = ff_get_buffer(avctx, &c->frame)) < 0)
419 output_samples = (int16_t *)c->frame.data[0];
420 for (i = 0; i < frames; i++) {
421 t = buf + 256 * sizeof(uint16_t);
422 for (j = 0; j < PAF_SOUND_SAMPLES; j++) {
423 for (k = 0; k < 2; k++) {
424 *output_samples++ = AV_RL16(buf + *t * 2);
428 buf += PAF_SOUND_FRAME_SIZE;
432 *(AVFrame *)data = c->frame;
437 AVCodec ff_paf_video_decoder = {
439 .type = AVMEDIA_TYPE_VIDEO,
440 .id = AV_CODEC_ID_PAF_VIDEO,
441 .priv_data_size = sizeof(PAFVideoDecContext),
442 .init = paf_vid_init,
443 .close = paf_vid_close,
444 .decode = paf_vid_decode,
445 .capabilities = CODEC_CAP_DR1,
446 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
449 AVCodec ff_paf_audio_decoder = {
451 .type = AVMEDIA_TYPE_AUDIO,
452 .id = AV_CODEC_ID_PAF_AUDIO,
453 .priv_data_size = sizeof(PAFAudioDecContext),
454 .init = paf_aud_init,
455 .decode = paf_aud_decode,
456 .capabilities = CODEC_CAP_DR1,
457 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),