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"
28 static const uint8_t block_sequences[16][8] =
30 { 0, 0, 0, 0, 0, 0, 0, 0 },
31 { 2, 0, 0, 0, 0, 0, 0, 0 },
32 { 5, 7, 0, 0, 0, 0, 0, 0 },
33 { 5, 0, 0, 0, 0, 0, 0, 0 },
34 { 6, 0, 0, 0, 0, 0, 0, 0 },
35 { 5, 7, 5, 7, 0, 0, 0, 0 },
36 { 5, 7, 5, 0, 0, 0, 0, 0 },
37 { 5, 7, 6, 0, 0, 0, 0, 0 },
38 { 5, 5, 0, 0, 0, 0, 0, 0 },
39 { 3, 0, 0, 0, 0, 0, 0, 0 },
40 { 6, 6, 0, 0, 0, 0, 0, 0 },
41 { 2, 4, 0, 0, 0, 0, 0, 0 },
42 { 2, 4, 5, 7, 0, 0, 0, 0 },
43 { 2, 4, 5, 0, 0, 0, 0, 0 },
44 { 2, 4, 6, 0, 0, 0, 0, 0 },
45 { 2, 4, 5, 7, 5, 7, 0, 0 }
48 typedef struct PAFVideoDecContext {
60 static av_cold int paf_vid_init(AVCodecContext *avctx)
62 PAFVideoDecContext *c = avctx->priv_data;
65 if (avctx->height & 3 || avctx->width & 3) {
66 av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
67 return AVERROR_INVALIDDATA;
70 avctx->pix_fmt = PIX_FMT_PAL8;
72 avcodec_get_frame_defaults(&c->pic);
73 c->frame_size = FFALIGN(avctx->height, 256) * avctx->width;
74 c->video_size = avctx->height * avctx->width;
75 for (i = 0; i < 4; i++) {
76 c->frame[i] = av_mallocz(c->frame_size);
78 return AVERROR(ENOMEM);
84 static int get_video_page_offset(AVCodecContext *avctx, uint8_t a, uint8_t b)
89 y = ((a & 0x3F) << 1) | (b >> 7 & 1);
91 return y * 2 * avctx->width + x * 2;
94 static void copy4h(AVCodecContext *avctx, uint8_t *dst)
96 PAFVideoDecContext *c = avctx->priv_data;
99 for (i = 0; i < 4; i++) {
100 bytestream2_get_buffer(&c->gb, dst, 4);
105 static void copy_color_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, uint8_t color)
109 for (i = 0; i < 4; i++) {
110 if ((mask >> 4) & (1 << (3 - i)))
112 if ((mask & 15) & (1 << (3 - i)))
113 dst[avctx->width + i] = color;
117 static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
121 for (i = 0; i < 4; i++) {
122 if ((mask >> 4) & (1 << (3 - i)))
124 if ((mask & 15) & (1 << (3 - i)))
125 dst[avctx->width + i] = src[avctx->width + i];
129 static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
131 PAFVideoDecContext *c = avctx->priv_data;
132 uint32_t opcode_size, offset;
133 uint8_t *dst, *dend, mask = 0, color = 0, a, b, p;
134 const uint8_t *src, *send, *opcodes;
137 i = bytestream2_get_byte(&c->gb);
142 align = bytestream2_tell(&c->gb) & 3;
144 bytestream2_skip(&c->gb, 4 - align);
147 a = bytestream2_get_byte(&c->gb);
148 b = bytestream2_get_byte(&c->gb);
150 dst = c->frame[p] + get_video_page_offset(avctx, a, b);
151 dend = c->frame[p] + c->frame_size;
152 offset = (b & 0x7F) * 2;
153 j = bytestream2_get_le16(&c->gb) + offset;
157 if (dst + 3 * avctx->width + 4 > dend)
158 return AVERROR_INVALIDDATA;
160 if ((offset & 0x3F) == 0)
161 dst += avctx->width * 3;
163 } while (offset < j);
167 dst = c->frame[c->current_frame];
169 a = bytestream2_get_byte(&c->gb);
170 b = bytestream2_get_byte(&c->gb);
172 src = c->frame[p] + get_video_page_offset(avctx, a, b);
173 send = c->frame[p] + c->frame_size;
174 if (src + 3 * avctx->width + 4 > send)
175 return AVERROR_INVALIDDATA;
176 copy_block4(dst, src, avctx->width, avctx->width, 4);
179 dst += avctx->width * 3;
181 } while (i < c->video_size / 16);
183 opcode_size = bytestream2_get_le16(&c->gb);
184 bytestream2_skip(&c->gb, 2);
186 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
187 return AVERROR_INVALIDDATA;
189 opcodes = pkt + bytestream2_tell(&c->gb);
190 bytestream2_skipu(&c->gb, opcode_size);
192 dst = c->frame[c->current_frame];
194 for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
195 for (j = 0; j < avctx->width; j += 4, dst += 4) {
199 return AVERROR_INVALIDDATA;
201 opcode = opcodes[x] & 15;
204 opcode = opcodes[x] >> 4;
207 while (block_sequences[opcode][k]) {
209 offset = avctx->width * 2;
210 code = block_sequences[opcode][k++];
216 color = bytestream2_get_byte(&c->gb);
218 mask = bytestream2_get_byte(&c->gb);
219 copy_color_mask(avctx, mask, dst + offset, color);
224 a = bytestream2_get_byte(&c->gb);
225 b = bytestream2_get_byte(&c->gb);
227 src = c->frame[p] + get_video_page_offset(avctx, a, b);
228 send = c->frame[p] + c->frame_size;
230 if (src + offset + avctx->width + 4 > send)
231 return AVERROR_INVALIDDATA;
232 mask = bytestream2_get_byte(&c->gb);
233 copy_src_mask(avctx, mask, dst + offset, src + offset);
243 static int paf_vid_decode(AVCodecContext *avctx, void *data,
244 int *data_size, AVPacket *pkt)
246 PAFVideoDecContext *c = avctx->priv_data;
247 uint8_t code, *dst, *src, *end;
250 c->pic.reference = 3;
251 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
254 bytestream2_init(&c->gb, pkt->data, pkt->size);
256 code = bytestream2_get_byte(&c->gb);
258 for (i = 0; i < 4; i++)
259 memset(c->frame[i], 0, c->frame_size);
261 memset(c->pic.data[1], 0, AVPALETTE_SIZE);
262 c->current_frame = 0;
263 c->pic.key_frame = 1;
264 c->pic.pict_type = AV_PICTURE_TYPE_I;
266 c->pic.key_frame = 0;
267 c->pic.pict_type = AV_PICTURE_TYPE_P;
271 uint32_t *out = (uint32_t *)c->pic.data[1];
274 index = bytestream2_get_byte(&c->gb);
275 count = bytestream2_get_byte(&c->gb) + 1;
277 if (index + count > AVPALETTE_SIZE)
278 return AVERROR_INVALIDDATA;
279 if (bytestream2_get_bytes_left(&c->gb) < 3 * AVPALETTE_SIZE)
280 return AVERROR_INVALIDDATA;
283 for (i = 0; i < count; i++) {
286 r = bytestream2_get_byteu(&c->gb);
288 g = bytestream2_get_byteu(&c->gb);
290 b = bytestream2_get_byteu(&c->gb);
292 *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
294 c->pic.palette_has_changed = 1;
297 switch (code & 0x0F) {
299 if ((ret = decode_0(avctx, code, pkt->data)) < 0)
303 dst = c->frame[c->current_frame];
304 bytestream2_skip(&c->gb, 2);
305 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
306 return AVERROR_INVALIDDATA;
307 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
310 frame = bytestream2_get_byte(&c->gb);
312 return AVERROR_INVALIDDATA;
313 if (frame != c->current_frame)
314 memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
317 dst = c->frame[c->current_frame];
318 end = dst + c->video_size;
320 bytestream2_skip(&c->gb, 2);
326 if (bytestream2_get_bytes_left(&c->gb) < 2)
327 return AVERROR_INVALIDDATA;
329 code = bytestream2_get_byteu(&c->gb);
330 count = FFABS(code) + 1;
332 if (dst + count > end)
333 return AVERROR_INVALIDDATA;
335 memset(dst, bytestream2_get_byteu(&c->gb), count);
337 bytestream2_get_buffer(&c->gb, dst, count);
342 av_log_ask_for_sample(avctx, "unknown/invalid code\n");
343 return AVERROR_INVALIDDATA;
346 dst = c->pic.data[0];
347 src = c->frame[c->current_frame];
348 for (i = 0; i < avctx->height; i++) {
349 memcpy(dst, src, avctx->width);
350 dst += c->pic.linesize[0];
354 c->current_frame = (c->current_frame + 1) & 3;
356 *data_size = sizeof(AVFrame);
357 *(AVFrame *)data = c->pic;
362 static av_cold int paf_vid_close(AVCodecContext *avctx)
364 PAFVideoDecContext *c = avctx->priv_data;
368 avctx->release_buffer(avctx, &c->pic);
370 for (i = 0; i < 4; i++)
371 av_freep(&c->frame[i]);
376 typedef struct PAFAudioDecContext {
378 } PAFAudioDecContext;
380 static av_cold int paf_aud_init(AVCodecContext *avctx)
382 PAFAudioDecContext *c = avctx->priv_data;
384 if (avctx->channels != 2) {
385 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
386 return AVERROR_INVALIDDATA;
389 avcodec_get_frame_defaults(&c->frame);
390 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
391 avctx->coded_frame = &c->frame;
392 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
397 static int paf_aud_decode(AVCodecContext *avctx, void *data,
398 int *got_frame_ptr, AVPacket *pkt)
400 PAFAudioDecContext *c = avctx->priv_data;
401 uint8_t *buf = pkt->data;
402 int16_t *output_samples;
404 int frames, ret, i, j, k;
406 frames = pkt->size / PAF_SOUND_FRAME_SIZE;
408 return AVERROR_INVALIDDATA;
410 c->frame.nb_samples = PAF_SOUND_SAMPLES * frames;
411 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0)
414 output_samples = (int16_t *)c->frame.data[0];
415 for (i = 0; i < frames; i++) {
416 t = buf + 256 * sizeof(uint16_t);
417 for (j = 0; j < PAF_SOUND_SAMPLES; j++) {
418 for (k = 0; k < 2; k++) {
419 *output_samples++ = AV_RL16(buf + *t++ * 2);
422 buf += PAF_SOUND_FRAME_SIZE;
426 *(AVFrame *)data = c->frame;
431 AVCodec ff_paf_video_decoder = {
433 .type = AVMEDIA_TYPE_VIDEO,
434 .id = CODEC_ID_PAF_VIDEO,
435 .priv_data_size = sizeof(PAFVideoDecContext),
436 .init = paf_vid_init,
437 .close = paf_vid_close,
438 .decode = paf_vid_decode,
439 .capabilities = CODEC_CAP_DR1,
440 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
443 AVCodec ff_paf_audio_decoder = {
445 .type = AVMEDIA_TYPE_AUDIO,
446 .id = CODEC_ID_PAF_AUDIO,
447 .priv_data_size = sizeof(PAFAudioDecContext),
448 .init = paf_aud_init,
449 .decode = paf_aud_decode,
450 .capabilities = CODEC_CAP_DR1,
451 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),