]> git.sesse.net Git - ffmpeg/blob - libavcodec/paf.c
Merge commit 'b9ba5253dd1232be4b48cfe61c31ff4b3de3d10a'
[ffmpeg] / libavcodec / paf.c
1 /*
2  * Packed Animation File video and audio decoder
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/dsputil.h"
24 #include "libavcodec/paf.h"
25 #include "bytestream.h"
26 #include "avcodec.h"
27 #include "copy_block.h"
28 #include "internal.h"
29
30
31 static const uint8_t block_sequences[16][8] =
32 {
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 }
49 };
50
51 typedef struct PAFVideoDecContext {
52     AVFrame  pic;
53     GetByteContext gb;
54
55     int     current_frame;
56     uint8_t *frame[4];
57     int     frame_size;
58     int     video_size;
59
60     uint8_t *opcodes;
61 } PAFVideoDecContext;
62
63 static av_cold int paf_vid_init(AVCodecContext *avctx)
64 {
65     PAFVideoDecContext *c = avctx->priv_data;
66     int i;
67
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;
71     }
72
73     avctx->pix_fmt = AV_PIX_FMT_PAL8;
74
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);
80         if (!c->frame[i])
81             return AVERROR(ENOMEM);
82     }
83
84     return 0;
85 }
86
87 static int get_video_page_offset(AVCodecContext *avctx, uint8_t a, uint8_t b)
88 {
89     int x, y;
90
91     x = b & 0x7F;
92     y = ((a & 0x3F) << 1) | (b >> 7 & 1);
93
94     return y * 2 * avctx->width + x * 2;
95 }
96
97 static void copy4h(AVCodecContext *avctx, uint8_t *dst)
98 {
99     PAFVideoDecContext *c = avctx->priv_data;
100     int i;
101
102     for (i = 0; i < 4; i++) {
103         bytestream2_get_buffer(&c->gb, dst, 4);
104         dst += avctx->width;
105     }
106 }
107
108 static void copy_color_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, uint8_t color)
109 {
110     int i;
111
112     for (i = 0; i < 4; i++) {
113         if ((mask >> 4) & (1 << (3 - i)))
114             dst[i] = color;
115         if ((mask & 15) & (1 << (3 - i)))
116             dst[avctx->width + i] = color;
117     }
118 }
119
120 static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
121 {
122     int i;
123
124     for (i = 0; i < 4; i++) {
125         if ((mask >> 4) & (1 << (3 - i)))
126             dst[i] = src[i];
127         if ((mask & 15) & (1 << (3 - i)))
128             dst[avctx->width + i] = src[avctx->width + i];
129     }
130 }
131
132 static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
133 {
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;
138     int  i, j, x = 0;
139
140     i = bytestream2_get_byte(&c->gb);
141     if (i) {
142         if (code & 0x10) {
143             int align;
144
145             align = bytestream2_tell(&c->gb) & 3;
146             if (align)
147                 bytestream2_skip(&c->gb, 4 - align);
148         }
149         do {
150             a      = bytestream2_get_byte(&c->gb);
151             b      = bytestream2_get_byte(&c->gb);
152             p      = (a & 0xC0) >> 6;
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;
157
158             do {
159                 offset++;
160                 if (dst + 3 * avctx->width + 4 > dend)
161                     return AVERROR_INVALIDDATA;
162                 copy4h(avctx, dst);
163                 if ((offset & 0x3F) == 0)
164                     dst += avctx->width * 3;
165                 dst += 4;
166             } while (offset < j);
167         } while (--i);
168     }
169
170     dst  = c->frame[c->current_frame];
171     dend = c->frame[c->current_frame] + c->frame_size;
172     do {
173         a    = bytestream2_get_byte(&c->gb);
174         b    = bytestream2_get_byte(&c->gb);
175         p    = (a & 0xC0) >> 6;
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);
182         i++;
183         if ((i & 0x3F) == 0)
184             dst += avctx->width * 3;
185         dst += 4;
186     } while (i < c->video_size / 16);
187
188     opcode_size = bytestream2_get_le16(&c->gb);
189     bytestream2_skip(&c->gb, 2);
190
191     if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
192         return AVERROR_INVALIDDATA;
193
194     opcodes = pkt + bytestream2_tell(&c->gb);
195     bytestream2_skipu(&c->gb, opcode_size);
196
197     dst = c->frame[c->current_frame];
198
199     for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
200         for (j = 0; j < avctx->width; j += 4, dst += 4) {
201             int opcode, k = 0;
202
203             if (x > opcode_size)
204                 return AVERROR_INVALIDDATA;
205             if (j & 4) {
206                 opcode = opcodes[x] & 15;
207                 x++;
208             } else {
209                 opcode = opcodes[x] >> 4;
210             }
211
212             while (block_sequences[opcode][k]) {
213
214                 offset = avctx->width * 2;
215                 code   = block_sequences[opcode][k++];
216
217                 switch (code) {
218                 case 2:
219                     offset = 0;
220                 case 3:
221                     color  = bytestream2_get_byte(&c->gb);
222                 case 4:
223                     mask   = bytestream2_get_byte(&c->gb);
224                     copy_color_mask(avctx, mask, dst + offset, color);
225                     break;
226                 case 5:
227                     offset = 0;
228                 case 6:
229                     a    = bytestream2_get_byte(&c->gb);
230                     b    = bytestream2_get_byte(&c->gb);
231                     p    = (a & 0xC0) >> 6;
232                     src  = c->frame[p] + get_video_page_offset(avctx, a, b);
233                     send = c->frame[p] + c->frame_size;
234                 case 7:
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);
239                     break;
240                 }
241             }
242         }
243     }
244
245     return 0;
246 }
247
248 static int paf_vid_decode(AVCodecContext *avctx, void *data,
249                           int *got_frame, AVPacket *pkt)
250 {
251     PAFVideoDecContext *c = avctx->priv_data;
252     uint8_t code, *dst, *src, *end;
253     int i, frame, ret;
254
255     c->pic.reference = 3;
256     if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
257         return ret;
258
259     bytestream2_init(&c->gb, pkt->data, pkt->size);
260
261     code = bytestream2_get_byte(&c->gb);
262     if (code & 0x20) {
263         for (i = 0; i < 4; i++)
264             memset(c->frame[i], 0, c->frame_size);
265
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;
270     } else {
271         c->pic.key_frame = 0;
272         c->pic.pict_type = AV_PICTURE_TYPE_P;
273     }
274
275     if (code & 0x40) {
276         uint32_t *out = (uint32_t *)c->pic.data[1];
277         int index, count;
278
279         index = bytestream2_get_byte(&c->gb);
280         count = bytestream2_get_byte(&c->gb) + 1;
281
282         if (index + count > 256)
283             return AVERROR_INVALIDDATA;
284         if (bytestream2_get_bytes_left(&c->gb) < 3*count)
285             return AVERROR_INVALIDDATA;
286
287         out += index;
288         for (i = 0; i < count; i++) {
289             unsigned r, g, b;
290
291             r = bytestream2_get_byteu(&c->gb);
292             r = r << 2 | r >> 4;
293             g = bytestream2_get_byteu(&c->gb);
294             g = g << 2 | g >> 4;
295             b = bytestream2_get_byteu(&c->gb);
296             b = b << 2 | b >> 4;
297             *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
298         }
299         c->pic.palette_has_changed = 1;
300     }
301
302     switch (code & 0x0F) {
303     case 0:
304         if ((ret = decode_0(avctx, code, pkt->data)) < 0)
305             return ret;
306         break;
307     case 1:
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);
313         break;
314     case 2:
315         frame = bytestream2_get_byte(&c->gb);
316         if (frame > 3)
317             return AVERROR_INVALIDDATA;
318         if (frame != c->current_frame)
319             memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
320         break;
321     case 4:
322         dst = c->frame[c->current_frame];
323         end = dst + c->video_size;
324
325         bytestream2_skip(&c->gb, 2);
326
327         while (dst < end) {
328             int8_t code;
329             int count;
330
331             if (bytestream2_get_bytes_left(&c->gb) < 2)
332                 return AVERROR_INVALIDDATA;
333
334             code  = bytestream2_get_byteu(&c->gb);
335             count = FFABS(code) + 1;
336
337             if (dst + count > end)
338                 return AVERROR_INVALIDDATA;
339             if (code < 0)
340                 memset(dst, bytestream2_get_byteu(&c->gb), count);
341             else
342                 bytestream2_get_buffer(&c->gb, dst, count);
343             dst += count;
344         }
345         break;
346     default:
347         av_log_ask_for_sample(avctx, "unknown/invalid code\n");
348         return AVERROR_INVALIDDATA;
349     }
350
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];
356         src += avctx->width;
357     }
358
359     c->current_frame = (c->current_frame + 1) & 3;
360
361     *got_frame       = 1;
362     *(AVFrame *)data = c->pic;
363
364     return pkt->size;
365 }
366
367 static av_cold int paf_vid_close(AVCodecContext *avctx)
368 {
369     PAFVideoDecContext *c = avctx->priv_data;
370     int i;
371
372     if (c->pic.data[0])
373         avctx->release_buffer(avctx, &c->pic);
374
375     for (i = 0; i < 4; i++)
376         av_freep(&c->frame[i]);
377
378     return 0;
379 }
380
381 typedef struct PAFAudioDecContext {
382     AVFrame frame;
383 } PAFAudioDecContext;
384
385 static av_cold int paf_aud_init(AVCodecContext *avctx)
386 {
387     PAFAudioDecContext *c = avctx->priv_data;
388
389     if (avctx->channels != 2) {
390         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
391         return AVERROR_INVALIDDATA;
392     }
393
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;
398
399     return 0;
400 }
401
402 static int paf_aud_decode(AVCodecContext *avctx, void *data,
403                           int *got_frame_ptr, AVPacket *pkt)
404 {
405     PAFAudioDecContext *c = avctx->priv_data;
406     uint8_t *buf = pkt->data;
407     int16_t *output_samples;
408     const uint8_t *t;
409     int frames, ret, i, j, k;
410
411     frames = pkt->size / PAF_SOUND_FRAME_SIZE;
412     if (frames < 1)
413         return AVERROR_INVALIDDATA;
414
415     c->frame.nb_samples = PAF_SOUND_SAMPLES * frames;
416     if ((ret = ff_get_buffer(avctx, &c->frame)) < 0)
417         return ret;
418
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);
425                 t++;
426             }
427         }
428         buf += PAF_SOUND_FRAME_SIZE;
429     }
430
431     *got_frame_ptr   = 1;
432     *(AVFrame *)data = c->frame;
433
434     return pkt->size;
435 }
436
437 AVCodec ff_paf_video_decoder = {
438     .name           = "paf_video",
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"),
447 };
448
449 AVCodec ff_paf_audio_decoder = {
450     .name           = "paf_audio",
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"),
458 };