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