]> git.sesse.net Git - ffmpeg/blob - libavcodec/paf.c
Merge commit '373a6dda5422186bc057297342a9e559a564595e'
[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/imgutils.h"
23 #include "libavutil/intreadwrite.h"
24
25 #include "libavcodec/paf.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "copy_block.h"
29 #include "internal.h"
30 #include "mathops.h"
31
32 static const uint8_t block_sequences[16][8] = {
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 width;
56     int height;
57
58     int current_frame;
59     uint8_t *frame[4];
60     int frame_size;
61     int video_size;
62
63     uint8_t *opcodes;
64 } PAFVideoDecContext;
65
66 static av_cold int paf_video_close(AVCodecContext *avctx)
67 {
68     PAFVideoDecContext *c = avctx->priv_data;
69     int i;
70
71     av_frame_free(&c->pic);
72
73     for (i = 0; i < 4; i++)
74         av_freep(&c->frame[i]);
75
76     return 0;
77 }
78
79 static av_cold int paf_video_init(AVCodecContext *avctx)
80 {
81     PAFVideoDecContext *c = avctx->priv_data;
82     int i;
83
84     c->width  = avctx->width;
85     c->height = avctx->height;
86
87     if (avctx->height & 3 || avctx->width & 3) {
88         av_log(avctx, AV_LOG_ERROR,
89                "width %d and height %d must be multiplie of 4.\n",
90                avctx->width, avctx->height);
91         return AVERROR_INVALIDDATA;
92     }
93
94     avctx->pix_fmt = AV_PIX_FMT_PAL8;
95
96     c->pic = av_frame_alloc();
97     if (!c->pic)
98         return AVERROR(ENOMEM);
99
100     c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
101     c->video_size = avctx->width * avctx->height;
102     for (i = 0; i < 4; i++) {
103         c->frame[i] = av_mallocz(c->frame_size);
104         if (!c->frame[i]) {
105             paf_video_close(avctx);
106             return AVERROR(ENOMEM);
107         }
108     }
109
110     return 0;
111 }
112
113 static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
114 {
115     int i;
116
117     for (i = 0; i < 4; i++) {
118         bytestream2_get_buffer(&c->gb, dst, 4);
119         dst += width;
120     }
121 }
122
123 static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
124 {
125     int i;
126
127     for (i = 0; i < 4; i++) {
128         if (mask & (1 << 7 - i))
129             dst[i] = color;
130         if (mask & (1 << 3 - i))
131             dst[width + i] = color;
132     }
133 }
134
135 static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
136 {
137     int i;
138
139     for (i = 0; i < 4; i++) {
140         if (mask & (1 << 7 - i))
141             dst[i] = src[i];
142         if (mask & (1 << 3 - i))
143             dst[width + i] = src[width + i];
144     }
145 }
146
147 static void set_src_position(PAFVideoDecContext *c,
148                              const uint8_t **p,
149                              const uint8_t **pend)
150 {
151     int val  = bytestream2_get_be16(&c->gb);
152     int page = val >> 14;
153     int x    = (val & 0x7F);
154     int y    = ((val >> 7) & 0x7F);
155
156     *p    = c->frame[page] + x * 2 + y * 2 * c->width;
157     *pend = c->frame[page] + c->frame_size;
158 }
159
160 static int decode_0(PAFVideoDecContext *c, uint8_t *pkt, uint8_t code)
161 {
162     uint32_t opcode_size, offset;
163     uint8_t *dst, *dend, mask = 0, color = 0;
164     const uint8_t *src, *send, *opcodes;
165     int i, j, op = 0;
166
167     i = bytestream2_get_byte(&c->gb);
168     if (i) {
169         if (code & 0x10) {
170             int align;
171
172             align = bytestream2_tell(&c->gb) & 3;
173             if (align)
174                 bytestream2_skip(&c->gb, 4 - align);
175         }
176         do {
177             int page, val, x, y;
178             val    = bytestream2_get_be16(&c->gb);
179             page   = val >> 14;
180             x      = (val & 0x7F) * 2;
181             y      = ((val >> 7) & 0x7F) * 2;
182             dst    = c->frame[page] + x + y * c->width;
183             dend   = c->frame[page] + c->frame_size;
184             offset = (x & 0x7F) * 2;
185             j      = bytestream2_get_le16(&c->gb) + offset;
186             do {
187                 offset++;
188                 if (dst + 3 * c->width + 4 > dend)
189                     return AVERROR_INVALIDDATA;
190                 read4x4block(c, dst, c->width);
191                 if ((offset & 0x3F) == 0)
192                     dst += c->width * 3;
193                 dst += 4;
194             } while (offset < j);
195         } while (--i);
196     }
197
198     dst  = c->frame[c->current_frame];
199     dend = c->frame[c->current_frame] + c->frame_size;
200     do {
201         set_src_position(c, &src, &send);
202         if ((src + 3 * c->width + 4 > send) ||
203             (dst + 3 * c->width + 4 > dend))
204             return AVERROR_INVALIDDATA;
205         copy_block4(dst, src, c->width, c->width, 4);
206         i++;
207         if ((i & 0x3F) == 0)
208             dst += c->width * 3;
209         dst += 4;
210     } while (i < c->video_size / 16);
211
212     opcode_size = bytestream2_get_le16(&c->gb);
213     bytestream2_skip(&c->gb, 2);
214
215     if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
216         return AVERROR_INVALIDDATA;
217
218     opcodes = pkt + bytestream2_tell(&c->gb);
219     bytestream2_skipu(&c->gb, opcode_size);
220
221     dst = c->frame[c->current_frame];
222
223     for (i = 0; i < c->height; i += 4, dst += c->width * 3)
224         for (j = 0; j < c->width; j += 4, dst += 4) {
225             int opcode, k = 0;
226             if (op > opcode_size)
227                 return AVERROR_INVALIDDATA;
228             if (j & 4) {
229                 opcode = opcodes[op] & 15;
230                 op++;
231             } else {
232                 opcode = opcodes[op] >> 4;
233             }
234
235             while (block_sequences[opcode][k]) {
236                 offset = c->width * 2;
237                 code   = block_sequences[opcode][k++];
238
239                 switch (code) {
240                 case 2:
241                     offset = 0;
242                 case 3:
243                     color = bytestream2_get_byte(&c->gb);
244                 case 4:
245                     mask = bytestream2_get_byte(&c->gb);
246                     copy_color_mask(dst + offset, c->width, mask, color);
247                     break;
248                 case 5:
249                     offset = 0;
250                 case 6:
251                     set_src_position(c, &src, &send);
252                 case 7:
253                     if (src + offset + c->width + 4 > send)
254                         return AVERROR_INVALIDDATA;
255                     mask = bytestream2_get_byte(&c->gb);
256                     copy_src_mask(dst + offset, c->width, mask, src + offset);
257                     break;
258                 }
259             }
260         }
261
262     return 0;
263 }
264
265 static int paf_video_decode(AVCodecContext *avctx, void *data,
266                             int *got_frame, AVPacket *pkt)
267 {
268     PAFVideoDecContext *c = avctx->priv_data;
269     uint8_t code, *dst, *end;
270     int i, frame, ret;
271
272     if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
273         return ret;
274
275     bytestream2_init(&c->gb, pkt->data, pkt->size);
276
277     code = bytestream2_get_byte(&c->gb);
278     if (code & 0x20) {  // frame is keyframe
279         for (i = 0; i < 4; i++)
280             memset(c->frame[i], 0, c->frame_size);
281
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;
286     } else {
287         c->pic->key_frame = 0;
288         c->pic->pict_type = AV_PICTURE_TYPE_P;
289     }
290
291     if (code & 0x40) {  // palette update
292         uint32_t *out = (uint32_t *)c->pic->data[1];
293         int index, count;
294
295         index = bytestream2_get_byte(&c->gb);
296         count = bytestream2_get_byte(&c->gb) + 1;
297
298         if (index + count > 256)
299             return AVERROR_INVALIDDATA;
300         if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
301             return AVERROR_INVALIDDATA;
302
303         out += index;
304         for (i = 0; i < count; i++) {
305             unsigned r, g, b;
306
307             r = bytestream2_get_byteu(&c->gb);
308             r = r << 2 | r >> 4;
309             g = bytestream2_get_byteu(&c->gb);
310             g = g << 2 | g >> 4;
311             b = bytestream2_get_byteu(&c->gb);
312             b = b << 2 | b >> 4;
313             *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
314         }
315         c->pic->palette_has_changed = 1;
316     }
317
318     switch (code & 0x0F) {
319     case 0:
320         /* Block-based motion compensation using 4x4 blocks with either
321          * horizontal or vertical vectors; might incorporate VQ as well. */
322         if ((ret = decode_0(c, pkt->data, code)) < 0)
323             return ret;
324         break;
325     case 1:
326         /* Uncompressed data. This mode specifies that (width * height) bytes
327          * should be copied directly from the encoded buffer into the output. */
328         dst = c->frame[c->current_frame];
329         // possibly chunk length data
330         bytestream2_skip(&c->gb, 2);
331         if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
332             return AVERROR_INVALIDDATA;
333         bytestream2_get_bufferu(&c->gb, dst, c->video_size);
334         break;
335     case 2:
336         /* Copy reference frame: Consume the next byte in the stream as the
337          * reference frame (which should be 0, 1, 2, or 3, and should not be
338          * the same as the current frame number). */
339         frame = bytestream2_get_byte(&c->gb);
340         if (frame > 3)
341             return AVERROR_INVALIDDATA;
342         if (frame != c->current_frame)
343             memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
344         break;
345     case 4:
346         /* Run length encoding.*/
347         dst = c->frame[c->current_frame];
348         end = dst + c->video_size;
349
350         bytestream2_skip(&c->gb, 2);
351
352         while (dst < end) {
353             int8_t code;
354             int count;
355
356             if (bytestream2_get_bytes_left(&c->gb) < 2)
357                 return AVERROR_INVALIDDATA;
358
359             code  = bytestream2_get_byteu(&c->gb);
360             count = FFABS(code) + 1;
361
362             if (dst + count > end)
363                 return AVERROR_INVALIDDATA;
364             if (code < 0)
365                 memset(dst, bytestream2_get_byteu(&c->gb), count);
366             else
367                 bytestream2_get_buffer(&c->gb, dst, count);
368             dst += count;
369         }
370         break;
371     default:
372         avpriv_request_sample(avctx, "unknown/invalid code");
373         return AVERROR_INVALIDDATA;
374     }
375
376     av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
377                         c->frame[c->current_frame], c->width,
378                         c->width, c->height);
379
380     c->current_frame = (c->current_frame + 1) & 3;
381     if ((ret = av_frame_ref(data, c->pic)) < 0)
382         return ret;
383
384     *got_frame = 1;
385
386     return pkt->size;
387 }
388
389 static av_cold int paf_audio_init(AVCodecContext *avctx)
390 {
391     if (avctx->channels != 2) {
392         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
393         return AVERROR_INVALIDDATA;
394     }
395
396     avctx->channel_layout = AV_CH_LAYOUT_STEREO;
397     avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
398
399     return 0;
400 }
401
402 static int paf_audio_decode(AVCodecContext *avctx, void *data,
403                             int *got_frame, AVPacket *pkt)
404 {
405     AVFrame *frame = data;
406     int16_t *output_samples;
407     const uint8_t *src = pkt->data;
408     int frames, ret, i, j;
409     int16_t cb[256];
410
411     frames = pkt->size / PAF_SOUND_FRAME_SIZE;
412     if (frames < 1)
413         return AVERROR_INVALIDDATA;
414
415     frame->nb_samples = PAF_SOUND_SAMPLES * frames;
416     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
417         return ret;
418
419     output_samples = (int16_t *)frame->data[0];
420     // codebook of 256 16-bit samples and 8-bit indices to it
421     for (j = 0; j < frames; j++) {
422         for (i = 0; i < 256; i++)
423             cb[i] = sign_extend(AV_RL16(src + i * 2), 16);
424         src += 256 * 2;
425         // always 2 channels
426         for (i = 0; i < PAF_SOUND_SAMPLES * 2; i++)
427             *output_samples++ = cb[*src++];
428     }
429     *got_frame = 1;
430
431     return pkt->size;
432 }
433
434 #if CONFIG_PAF_VIDEO_DECODER
435 AVCodec ff_paf_video_decoder = {
436     .name           = "paf_video",
437     .long_name      = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
438     .type           = AVMEDIA_TYPE_VIDEO,
439     .id             = AV_CODEC_ID_PAF_VIDEO,
440     .priv_data_size = sizeof(PAFVideoDecContext),
441     .init           = paf_video_init,
442     .close          = paf_video_close,
443     .decode         = paf_video_decode,
444     .capabilities   = CODEC_CAP_DR1,
445 };
446 #endif
447
448 #if CONFIG_PAF_AUDIO_DECODER
449 AVCodec ff_paf_audio_decoder = {
450     .name         = "paf_audio",
451     .long_name    = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
452     .type         = AVMEDIA_TYPE_AUDIO,
453     .id           = AV_CODEC_ID_PAF_AUDIO,
454     .init         = paf_audio_init,
455     .decode       = paf_audio_decode,
456     .capabilities = CODEC_CAP_DR1,
457 };
458 #endif