]> git.sesse.net Git - ffmpeg/blob - libavcodec/paf.c
Merge commit '97bf7c03b1338a867da52c159a2afecbdedcfa88'
[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
28 static const uint8_t block_sequences[16][8] =
29 {
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 }
46 };
47
48 typedef struct PAFVideoDecContext {
49     AVFrame  pic;
50     GetByteContext gb;
51
52     int     current_frame;
53     uint8_t *frame[4];
54     int     frame_size;
55     int     video_size;
56
57     uint8_t *opcodes;
58 } PAFVideoDecContext;
59
60 static av_cold int paf_vid_init(AVCodecContext *avctx)
61 {
62     PAFVideoDecContext *c = avctx->priv_data;
63     int i;
64
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;
68     }
69
70     avctx->pix_fmt = AV_PIX_FMT_PAL8;
71
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);
77         if (!c->frame[i])
78             return AVERROR(ENOMEM);
79     }
80
81     return 0;
82 }
83
84 static int get_video_page_offset(AVCodecContext *avctx, uint8_t a, uint8_t b)
85 {
86     int x, y;
87
88     x = b & 0x7F;
89     y = ((a & 0x3F) << 1) | (b >> 7 & 1);
90
91     return y * 2 * avctx->width + x * 2;
92 }
93
94 static void copy4h(AVCodecContext *avctx, uint8_t *dst)
95 {
96     PAFVideoDecContext *c = avctx->priv_data;
97     int i;
98
99     for (i = 0; i < 4; i++) {
100         bytestream2_get_buffer(&c->gb, dst, 4);
101         dst += avctx->width;
102     }
103 }
104
105 static void copy_color_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, uint8_t color)
106 {
107     int i;
108
109     for (i = 0; i < 4; i++) {
110         if ((mask >> 4) & (1 << (3 - i)))
111             dst[i] = color;
112         if ((mask & 15) & (1 << (3 - i)))
113             dst[avctx->width + i] = color;
114     }
115 }
116
117 static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
118 {
119     int i;
120
121     for (i = 0; i < 4; i++) {
122         if ((mask >> 4) & (1 << (3 - i)))
123             dst[i] = src[i];
124         if ((mask & 15) & (1 << (3 - i)))
125             dst[avctx->width + i] = src[avctx->width + i];
126     }
127 }
128
129 static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
130 {
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;
135     int  i, j, x = 0;
136
137     i = bytestream2_get_byte(&c->gb);
138     if (i) {
139         if (code & 0x10) {
140             int align;
141
142             align = bytestream2_tell(&c->gb) & 3;
143             if (align)
144                 bytestream2_skip(&c->gb, 4 - align);
145         }
146         do {
147             a      = bytestream2_get_byte(&c->gb);
148             b      = bytestream2_get_byte(&c->gb);
149             p      = (a & 0xC0) >> 6;
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;
154
155             do {
156                 offset++;
157                 if (dst + 3 * avctx->width + 4 > dend)
158                     return AVERROR_INVALIDDATA;
159                 copy4h(avctx, dst);
160                 if ((offset & 0x3F) == 0)
161                     dst += avctx->width * 3;
162                 dst += 4;
163             } while (offset < j);
164         } while (--i);
165     }
166
167     dst  = c->frame[c->current_frame];
168     dend = c->frame[c->current_frame] + c->frame_size;
169     do {
170         a    = bytestream2_get_byte(&c->gb);
171         b    = bytestream2_get_byte(&c->gb);
172         p    = (a & 0xC0) >> 6;
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);
179         i++;
180         if ((i & 0x3F) == 0)
181             dst += avctx->width * 3;
182         dst += 4;
183     } while (i < c->video_size / 16);
184
185     opcode_size = bytestream2_get_le16(&c->gb);
186     bytestream2_skip(&c->gb, 2);
187
188     if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
189         return AVERROR_INVALIDDATA;
190
191     opcodes = pkt + bytestream2_tell(&c->gb);
192     bytestream2_skipu(&c->gb, opcode_size);
193
194     dst = c->frame[c->current_frame];
195
196     for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
197         for (j = 0; j < avctx->width; j += 4, dst += 4) {
198             int opcode, k = 0;
199
200             if (x > opcode_size)
201                 return AVERROR_INVALIDDATA;
202             if (j & 4) {
203                 opcode = opcodes[x] & 15;
204                 x++;
205             } else {
206                 opcode = opcodes[x] >> 4;
207             }
208
209             while (block_sequences[opcode][k]) {
210
211                 offset = avctx->width * 2;
212                 code   = block_sequences[opcode][k++];
213
214                 switch (code) {
215                 case 2:
216                     offset = 0;
217                 case 3:
218                     color  = bytestream2_get_byte(&c->gb);
219                 case 4:
220                     mask   = bytestream2_get_byte(&c->gb);
221                     copy_color_mask(avctx, mask, dst + offset, color);
222                     break;
223                 case 5:
224                     offset = 0;
225                 case 6:
226                     a    = bytestream2_get_byte(&c->gb);
227                     b    = bytestream2_get_byte(&c->gb);
228                     p    = (a & 0xC0) >> 6;
229                     src  = c->frame[p] + get_video_page_offset(avctx, a, b);
230                     send = c->frame[p] + c->frame_size;
231                 case 7:
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);
236                     break;
237                 }
238             }
239         }
240     }
241
242     return 0;
243 }
244
245 static int paf_vid_decode(AVCodecContext *avctx, void *data,
246                           int *data_size, AVPacket *pkt)
247 {
248     PAFVideoDecContext *c = avctx->priv_data;
249     uint8_t code, *dst, *src, *end;
250     int i, frame, ret;
251
252     c->pic.reference = 3;
253     if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
254         return ret;
255
256     bytestream2_init(&c->gb, pkt->data, pkt->size);
257
258     code = bytestream2_get_byte(&c->gb);
259     if (code & 0x20) {
260         for (i = 0; i < 4; i++)
261             memset(c->frame[i], 0, c->frame_size);
262
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;
267     } else {
268         c->pic.key_frame = 0;
269         c->pic.pict_type = AV_PICTURE_TYPE_P;
270     }
271
272     if (code & 0x40) {
273         uint32_t *out = (uint32_t *)c->pic.data[1];
274         int index, count;
275
276         index = bytestream2_get_byte(&c->gb);
277         count = bytestream2_get_byte(&c->gb) + 1;
278
279         if (index + count > 256)
280             return AVERROR_INVALIDDATA;
281         if (bytestream2_get_bytes_left(&c->gb) < 3*count)
282             return AVERROR_INVALIDDATA;
283
284         out += index;
285         for (i = 0; i < count; i++) {
286             unsigned r, g, b;
287
288             r = bytestream2_get_byteu(&c->gb);
289             r = r << 2 | r >> 4;
290             g = bytestream2_get_byteu(&c->gb);
291             g = g << 2 | g >> 4;
292             b = bytestream2_get_byteu(&c->gb);
293             b = b << 2 | b >> 4;
294             *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
295         }
296         c->pic.palette_has_changed = 1;
297     }
298
299     switch (code & 0x0F) {
300     case 0:
301         if ((ret = decode_0(avctx, code, pkt->data)) < 0)
302             return ret;
303         break;
304     case 1:
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);
310         break;
311     case 2:
312         frame = bytestream2_get_byte(&c->gb);
313         if (frame > 3)
314             return AVERROR_INVALIDDATA;
315         if (frame != c->current_frame)
316             memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
317         break;
318     case 4:
319         dst = c->frame[c->current_frame];
320         end = dst + c->video_size;
321
322         bytestream2_skip(&c->gb, 2);
323
324         while (dst < end) {
325             int8_t code;
326             int count;
327
328             if (bytestream2_get_bytes_left(&c->gb) < 2)
329                 return AVERROR_INVALIDDATA;
330
331             code  = bytestream2_get_byteu(&c->gb);
332             count = FFABS(code) + 1;
333
334             if (dst + count > end)
335                 return AVERROR_INVALIDDATA;
336             if (code < 0)
337                 memset(dst, bytestream2_get_byteu(&c->gb), count);
338             else
339                 bytestream2_get_buffer(&c->gb, dst, count);
340             dst += count;
341         }
342         break;
343     default:
344         av_log_ask_for_sample(avctx, "unknown/invalid code\n");
345         return AVERROR_INVALIDDATA;
346     }
347
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];
353         src += avctx->width;
354     }
355
356     c->current_frame = (c->current_frame + 1) & 3;
357
358     *data_size       = sizeof(AVFrame);
359     *(AVFrame *)data = c->pic;
360
361     return pkt->size;
362 }
363
364 static av_cold int paf_vid_close(AVCodecContext *avctx)
365 {
366     PAFVideoDecContext *c = avctx->priv_data;
367     int i;
368
369     if (c->pic.data[0])
370         avctx->release_buffer(avctx, &c->pic);
371
372     for (i = 0; i < 4; i++)
373         av_freep(&c->frame[i]);
374
375     return 0;
376 }
377
378 typedef struct PAFAudioDecContext {
379     AVFrame frame;
380 } PAFAudioDecContext;
381
382 static av_cold int paf_aud_init(AVCodecContext *avctx)
383 {
384     PAFAudioDecContext *c = avctx->priv_data;
385
386     if (avctx->channels != 2) {
387         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
388         return AVERROR_INVALIDDATA;
389     }
390
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;
395
396     return 0;
397 }
398
399 static int paf_aud_decode(AVCodecContext *avctx, void *data,
400                           int *got_frame_ptr, AVPacket *pkt)
401 {
402     PAFAudioDecContext *c = avctx->priv_data;
403     uint8_t *buf = pkt->data;
404     int16_t *output_samples;
405     const uint8_t *t;
406     int frames, ret, i, j, k;
407
408     frames = pkt->size / PAF_SOUND_FRAME_SIZE;
409     if (frames < 1)
410         return AVERROR_INVALIDDATA;
411
412     c->frame.nb_samples = PAF_SOUND_SAMPLES * frames;
413     if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0)
414         return ret;
415
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);
422                 t++;
423             }
424         }
425         buf += PAF_SOUND_FRAME_SIZE;
426     }
427
428     *got_frame_ptr   = 1;
429     *(AVFrame *)data = c->frame;
430
431     return pkt->size;
432 }
433
434 AVCodec ff_paf_video_decoder = {
435     .name           = "paf_video",
436     .type           = AVMEDIA_TYPE_VIDEO,
437     .id             = AV_CODEC_ID_PAF_VIDEO,
438     .priv_data_size = sizeof(PAFVideoDecContext),
439     .init           = paf_vid_init,
440     .close          = paf_vid_close,
441     .decode         = paf_vid_decode,
442     .capabilities   = CODEC_CAP_DR1,
443     .long_name      = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
444 };
445
446 AVCodec ff_paf_audio_decoder = {
447     .name           = "paf_audio",
448     .type           = AVMEDIA_TYPE_AUDIO,
449     .id             = AV_CODEC_ID_PAF_AUDIO,
450     .priv_data_size = sizeof(PAFAudioDecContext),
451     .init           = paf_aud_init,
452     .decode         = paf_aud_decode,
453     .capabilities   = CODEC_CAP_DR1,
454     .long_name      = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
455 };