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