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_close(AVCodecContext *avctx)
64 PAFVideoDecContext *c = avctx->priv_data;
67 av_frame_free(&c->pic);
69 for (i = 0; i < 4; i++)
70 av_freep(&c->frame[i]);
75 static av_cold int paf_vid_init(AVCodecContext *avctx)
77 PAFVideoDecContext *c = avctx->priv_data;
80 if (avctx->height & 3 || avctx->width & 3) {
81 av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
82 return AVERROR_INVALIDDATA;
85 avctx->pix_fmt = AV_PIX_FMT_PAL8;
87 c->pic = av_frame_alloc();
89 return AVERROR(ENOMEM);
91 c->frame_size = FFALIGN(avctx->height, 256) * avctx->width;
92 c->video_size = avctx->height * avctx->width;
93 for (i = 0; i < 4; i++) {
94 c->frame[i] = av_mallocz(c->frame_size);
97 return AVERROR(ENOMEM);
104 static int get_video_page_offset(AVCodecContext *avctx, uint8_t a, uint8_t b)
109 y = ((a & 0x3F) << 1) | (b >> 7 & 1);
111 return y * 2 * avctx->width + x * 2;
114 static void copy4h(AVCodecContext *avctx, uint8_t *dst)
116 PAFVideoDecContext *c = avctx->priv_data;
119 for (i = 0; i < 4; i++) {
120 bytestream2_get_buffer(&c->gb, dst, 4);
125 static void copy_color_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, uint8_t color)
129 for (i = 0; i < 4; i++) {
130 if ((mask >> 4) & (1 << (3 - i)))
132 if ((mask & 15) & (1 << (3 - i)))
133 dst[avctx->width + i] = color;
137 static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
141 for (i = 0; i < 4; i++) {
142 if ((mask >> 4) & (1 << (3 - i)))
144 if ((mask & 15) & (1 << (3 - i)))
145 dst[avctx->width + i] = src[avctx->width + i];
149 static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
151 PAFVideoDecContext *c = avctx->priv_data;
152 uint32_t opcode_size, offset;
153 uint8_t *dst, *dend, mask = 0, color = 0, a, b, p;
154 const uint8_t *src, *send, *opcodes;
157 i = bytestream2_get_byte(&c->gb);
162 align = bytestream2_tell(&c->gb) & 3;
164 bytestream2_skip(&c->gb, 4 - align);
167 a = bytestream2_get_byte(&c->gb);
168 b = bytestream2_get_byte(&c->gb);
170 dst = c->frame[p] + get_video_page_offset(avctx, a, b);
171 dend = c->frame[p] + c->frame_size;
172 offset = (b & 0x7F) * 2;
173 j = bytestream2_get_le16(&c->gb) + offset;
177 if (dst + 3 * avctx->width + 4 > dend)
178 return AVERROR_INVALIDDATA;
180 if ((offset & 0x3F) == 0)
181 dst += avctx->width * 3;
183 } while (offset < j);
187 dst = c->frame[c->current_frame];
188 dend = c->frame[c->current_frame] + c->frame_size;
190 a = bytestream2_get_byte(&c->gb);
191 b = bytestream2_get_byte(&c->gb);
193 src = c->frame[p] + get_video_page_offset(avctx, a, b);
194 send = c->frame[p] + c->frame_size;
195 if ((src + 3 * avctx->width + 4 > send) ||
196 (dst + 3 * avctx->width + 4 > dend))
197 return AVERROR_INVALIDDATA;
198 copy_block4(dst, src, avctx->width, avctx->width, 4);
201 dst += avctx->width * 3;
203 } while (i < c->video_size / 16);
205 opcode_size = bytestream2_get_le16(&c->gb);
206 bytestream2_skip(&c->gb, 2);
208 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
209 return AVERROR_INVALIDDATA;
211 opcodes = pkt + bytestream2_tell(&c->gb);
212 bytestream2_skipu(&c->gb, opcode_size);
214 dst = c->frame[c->current_frame];
216 for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
217 for (j = 0; j < avctx->width; j += 4, dst += 4) {
221 return AVERROR_INVALIDDATA;
223 opcode = opcodes[x] & 15;
226 opcode = opcodes[x] >> 4;
229 while (block_sequences[opcode][k]) {
231 offset = avctx->width * 2;
232 code = block_sequences[opcode][k++];
238 color = bytestream2_get_byte(&c->gb);
240 mask = bytestream2_get_byte(&c->gb);
241 copy_color_mask(avctx, mask, dst + offset, color);
246 a = bytestream2_get_byte(&c->gb);
247 b = bytestream2_get_byte(&c->gb);
249 src = c->frame[p] + get_video_page_offset(avctx, a, b);
250 send = c->frame[p] + c->frame_size;
252 if (src + offset + avctx->width + 4 > send)
253 return AVERROR_INVALIDDATA;
254 mask = bytestream2_get_byte(&c->gb);
255 copy_src_mask(avctx, mask, dst + offset, src + offset);
265 static int paf_vid_decode(AVCodecContext *avctx, void *data,
266 int *got_frame, AVPacket *pkt)
268 PAFVideoDecContext *c = avctx->priv_data;
269 uint8_t code, *dst, *src, *end;
272 if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
275 bytestream2_init(&c->gb, pkt->data, pkt->size);
277 code = bytestream2_get_byte(&c->gb);
279 for (i = 0; i < 4; i++)
280 memset(c->frame[i], 0, c->frame_size);
282 memset(c->pic->data[1], 0, AVPALETTE_SIZE);
283 c->current_frame = 0;
284 c->pic->key_frame = 1;
285 c->pic->pict_type = AV_PICTURE_TYPE_I;
287 c->pic->key_frame = 0;
288 c->pic->pict_type = AV_PICTURE_TYPE_P;
292 uint32_t *out = (uint32_t *)c->pic->data[1];
295 index = bytestream2_get_byte(&c->gb);
296 count = bytestream2_get_byte(&c->gb) + 1;
298 if (index + count > 256)
299 return AVERROR_INVALIDDATA;
300 if (bytestream2_get_bytes_left(&c->gb) < 3*count)
301 return AVERROR_INVALIDDATA;
304 for (i = 0; i < count; i++) {
307 r = bytestream2_get_byteu(&c->gb);
309 g = bytestream2_get_byteu(&c->gb);
311 b = bytestream2_get_byteu(&c->gb);
313 *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
315 c->pic->palette_has_changed = 1;
318 switch (code & 0x0F) {
320 if ((ret = decode_0(avctx, code, pkt->data)) < 0)
324 dst = c->frame[c->current_frame];
325 bytestream2_skip(&c->gb, 2);
326 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
327 return AVERROR_INVALIDDATA;
328 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
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 dst = c->frame[c->current_frame];
339 end = dst + c->video_size;
341 bytestream2_skip(&c->gb, 2);
347 if (bytestream2_get_bytes_left(&c->gb) < 2)
348 return AVERROR_INVALIDDATA;
350 code = bytestream2_get_byteu(&c->gb);
351 count = FFABS(code) + 1;
353 if (dst + count > end)
354 return AVERROR_INVALIDDATA;
356 memset(dst, bytestream2_get_byteu(&c->gb), count);
358 bytestream2_get_buffer(&c->gb, dst, count);
363 avpriv_request_sample(avctx, "unknown/invalid code");
364 return AVERROR_INVALIDDATA;
367 dst = c->pic->data[0];
368 src = c->frame[c->current_frame];
369 for (i = 0; i < avctx->height; i++) {
370 memcpy(dst, src, avctx->width);
371 dst += c->pic->linesize[0];
375 c->current_frame = (c->current_frame + 1) & 3;
376 if ((ret = av_frame_ref(data, c->pic)) < 0)
384 static av_cold int paf_aud_init(AVCodecContext *avctx)
386 if (avctx->channels != 2) {
387 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
388 return AVERROR_INVALIDDATA;
391 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
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 AVFrame *frame = 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 frame->nb_samples = PAF_SOUND_SAMPLES * frames;
411 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
414 output_samples = (int16_t *)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);
423 buf += PAF_SOUND_FRAME_SIZE;
431 AVCodec ff_paf_video_decoder = {
433 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
434 .type = AVMEDIA_TYPE_VIDEO,
435 .id = AV_CODEC_ID_PAF_VIDEO,
436 .priv_data_size = sizeof(PAFVideoDecContext),
437 .init = paf_vid_init,
438 .close = paf_vid_close,
439 .decode = paf_vid_decode,
440 .capabilities = CODEC_CAP_DR1,
443 AVCodec ff_paf_audio_decoder = {
445 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
446 .type = AVMEDIA_TYPE_AUDIO,
447 .id = AV_CODEC_ID_PAF_AUDIO,
448 .init = paf_aud_init,
449 .decode = paf_aud_decode,
450 .capabilities = CODEC_CAP_DR1,