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];
168 dend = c->frame[c->current_frame] + c->frame_size;
170 a = bytestream2_get_byte(&c->gb);
171 b = bytestream2_get_byte(&c->gb);
173 src = c->frame[p] + get_video_page_offset(avctx, a, b);
174 send = c->frame[p] + c->frame_size;
175 if ((src + 3 * avctx->width + 4 > send) ||
176 (dst + 3 * avctx->width + 4 > dend))
177 return AVERROR_INVALIDDATA;
178 copy_block4(dst, src, avctx->width, avctx->width, 4);
181 dst += avctx->width * 3;
183 } while (i < c->video_size / 16);
185 opcode_size = bytestream2_get_le16(&c->gb);
186 bytestream2_skip(&c->gb, 2);
188 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
189 return AVERROR_INVALIDDATA;
191 opcodes = pkt + bytestream2_tell(&c->gb);
192 bytestream2_skipu(&c->gb, opcode_size);
194 dst = c->frame[c->current_frame];
196 for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
197 for (j = 0; j < avctx->width; j += 4, dst += 4) {
201 return AVERROR_INVALIDDATA;
203 opcode = opcodes[x] & 15;
206 opcode = opcodes[x] >> 4;
209 while (block_sequences[opcode][k]) {
211 offset = avctx->width * 2;
212 code = block_sequences[opcode][k++];
218 color = bytestream2_get_byte(&c->gb);
220 mask = bytestream2_get_byte(&c->gb);
221 copy_color_mask(avctx, mask, dst + offset, color);
226 a = bytestream2_get_byte(&c->gb);
227 b = bytestream2_get_byte(&c->gb);
229 src = c->frame[p] + get_video_page_offset(avctx, a, b);
230 send = c->frame[p] + c->frame_size;
232 if (src + offset + avctx->width + 4 > send)
233 return AVERROR_INVALIDDATA;
234 mask = bytestream2_get_byte(&c->gb);
235 copy_src_mask(avctx, mask, dst + offset, src + offset);
245 static int paf_vid_decode(AVCodecContext *avctx, void *data,
246 int *data_size, AVPacket *pkt)
248 PAFVideoDecContext *c = avctx->priv_data;
249 uint8_t code, *dst, *src, *end;
252 c->pic.reference = 3;
253 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
256 bytestream2_init(&c->gb, pkt->data, pkt->size);
258 code = bytestream2_get_byte(&c->gb);
260 for (i = 0; i < 4; i++)
261 memset(c->frame[i], 0, c->frame_size);
263 memset(c->pic.data[1], 0, AVPALETTE_SIZE);
264 c->current_frame = 0;
265 c->pic.key_frame = 1;
266 c->pic.pict_type = AV_PICTURE_TYPE_I;
268 c->pic.key_frame = 0;
269 c->pic.pict_type = AV_PICTURE_TYPE_P;
273 uint32_t *out = (uint32_t *)c->pic.data[1];
276 index = bytestream2_get_byte(&c->gb);
277 count = bytestream2_get_byte(&c->gb) + 1;
279 if (index + count > 256)
280 return AVERROR_INVALIDDATA;
281 if (bytestream2_get_bytes_left(&c->gb) < 3*count)
282 return AVERROR_INVALIDDATA;
285 for (i = 0; i < count; i++) {
288 r = bytestream2_get_byteu(&c->gb);
290 g = bytestream2_get_byteu(&c->gb);
292 b = bytestream2_get_byteu(&c->gb);
294 *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
296 c->pic.palette_has_changed = 1;
299 switch (code & 0x0F) {
301 if ((ret = decode_0(avctx, code, pkt->data)) < 0)
305 dst = c->frame[c->current_frame];
306 bytestream2_skip(&c->gb, 2);
307 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
308 return AVERROR_INVALIDDATA;
309 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
312 frame = bytestream2_get_byte(&c->gb);
314 return AVERROR_INVALIDDATA;
315 if (frame != c->current_frame)
316 memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
319 dst = c->frame[c->current_frame];
320 end = dst + c->video_size;
322 bytestream2_skip(&c->gb, 2);
328 if (bytestream2_get_bytes_left(&c->gb) < 2)
329 return AVERROR_INVALIDDATA;
331 code = bytestream2_get_byteu(&c->gb);
332 count = FFABS(code) + 1;
334 if (dst + count > end)
335 return AVERROR_INVALIDDATA;
337 memset(dst, bytestream2_get_byteu(&c->gb), count);
339 bytestream2_get_buffer(&c->gb, dst, count);
344 av_log_ask_for_sample(avctx, "unknown/invalid code\n");
345 return AVERROR_INVALIDDATA;
348 dst = c->pic.data[0];
349 src = c->frame[c->current_frame];
350 for (i = 0; i < avctx->height; i++) {
351 memcpy(dst, src, avctx->width);
352 dst += c->pic.linesize[0];
356 c->current_frame = (c->current_frame + 1) & 3;
358 *data_size = sizeof(AVFrame);
359 *(AVFrame *)data = c->pic;
364 static av_cold int paf_vid_close(AVCodecContext *avctx)
366 PAFVideoDecContext *c = avctx->priv_data;
370 avctx->release_buffer(avctx, &c->pic);
372 for (i = 0; i < 4; i++)
373 av_freep(&c->frame[i]);
378 typedef struct PAFAudioDecContext {
380 } PAFAudioDecContext;
382 static av_cold int paf_aud_init(AVCodecContext *avctx)
384 PAFAudioDecContext *c = avctx->priv_data;
386 if (avctx->channels != 2) {
387 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
388 return AVERROR_INVALIDDATA;
391 avcodec_get_frame_defaults(&c->frame);
392 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
393 avctx->coded_frame = &c->frame;
394 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
399 static int paf_aud_decode(AVCodecContext *avctx, void *data,
400 int *got_frame_ptr, AVPacket *pkt)
402 PAFAudioDecContext *c = avctx->priv_data;
403 uint8_t *buf = pkt->data;
404 int16_t *output_samples;
406 int frames, ret, i, j, k;
408 frames = pkt->size / PAF_SOUND_FRAME_SIZE;
410 return AVERROR_INVALIDDATA;
412 c->frame.nb_samples = PAF_SOUND_SAMPLES * frames;
413 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0)
416 output_samples = (int16_t *)c->frame.data[0];
417 for (i = 0; i < frames; i++) {
418 t = buf + 256 * sizeof(uint16_t);
419 for (j = 0; j < PAF_SOUND_SAMPLES; j++) {
420 for (k = 0; k < 2; k++) {
421 *output_samples++ = AV_RL16(buf + *t++ * 2);
424 buf += PAF_SOUND_FRAME_SIZE;
428 *(AVFrame *)data = c->frame;
433 AVCodec ff_paf_video_decoder = {
435 .type = AVMEDIA_TYPE_VIDEO,
436 .id = AV_CODEC_ID_PAF_VIDEO,
437 .priv_data_size = sizeof(PAFVideoDecContext),
438 .init = paf_vid_init,
439 .close = paf_vid_close,
440 .decode = paf_vid_decode,
441 .capabilities = CODEC_CAP_DR1,
442 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
445 AVCodec ff_paf_audio_decoder = {
447 .type = AVMEDIA_TYPE_AUDIO,
448 .id = AV_CODEC_ID_PAF_AUDIO,
449 .priv_data_size = sizeof(PAFAudioDecContext),
450 .init = paf_aud_init,
451 .decode = paf_aud_decode,
452 .capabilities = CODEC_CAP_DR1,
453 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),