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