]> git.sesse.net Git - ffmpeg/blob - libavcodec/paf.c
g723.1dec: Make postfilter user switchable
[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 = 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     do {
169         a    = bytestream2_get_byte(&c->gb);
170         b    = bytestream2_get_byte(&c->gb);
171         p    = (a & 0xC0) >> 6;
172         src  = c->frame[p] + get_video_page_offset(avctx, a, b);
173         send = c->frame[p] + c->frame_size;
174         if (src + 3 * avctx->width + 4 > send)
175             return AVERROR_INVALIDDATA;
176         copy_block4(dst, src, avctx->width, avctx->width, 4);
177         i++;
178         if ((i & 0x3F) == 0)
179             dst += avctx->width * 3;
180         dst += 4;
181     } while (i < c->video_size / 16);
182
183     opcode_size = bytestream2_get_le16(&c->gb);
184     bytestream2_skip(&c->gb, 2);
185
186     if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
187         return AVERROR_INVALIDDATA;
188
189     opcodes = pkt + bytestream2_tell(&c->gb);
190     bytestream2_skipu(&c->gb, opcode_size);
191
192     dst = c->frame[c->current_frame];
193
194     for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
195         for (j = 0; j < avctx->width; j += 4, dst += 4) {
196             int opcode, k = 0;
197
198             if (x > opcode_size)
199                 return AVERROR_INVALIDDATA;
200             if (j & 4) {
201                 opcode = opcodes[x] & 15;
202                 x++;
203             } else {
204                 opcode = opcodes[x] >> 4;
205             }
206
207             while (block_sequences[opcode][k]) {
208
209                 offset = avctx->width * 2;
210                 code   = block_sequences[opcode][k++];
211
212                 switch (code) {
213                 case 2:
214                     offset = 0;
215                 case 3:
216                     color  = bytestream2_get_byte(&c->gb);
217                 case 4:
218                     mask   = bytestream2_get_byte(&c->gb);
219                     copy_color_mask(avctx, mask, dst + offset, color);
220                     break;
221                 case 5:
222                     offset = 0;
223                 case 6:
224                     a    = bytestream2_get_byte(&c->gb);
225                     b    = bytestream2_get_byte(&c->gb);
226                     p    = (a & 0xC0) >> 6;
227                     src  = c->frame[p] + get_video_page_offset(avctx, a, b);
228                     send = c->frame[p] + c->frame_size;
229                 case 7:
230                     if (src + offset + avctx->width + 4 > send)
231                         return AVERROR_INVALIDDATA;
232                     mask = bytestream2_get_byte(&c->gb);
233                     copy_src_mask(avctx, mask, dst + offset, src + offset);
234                     break;
235                 }
236             }
237         }
238     }
239
240     return 0;
241 }
242
243 static int paf_vid_decode(AVCodecContext *avctx, void *data,
244                           int *data_size, AVPacket *pkt)
245 {
246     PAFVideoDecContext *c = avctx->priv_data;
247     uint8_t code, *dst, *src, *end;
248     int i, frame, ret;
249
250     if (c->pic.data[0])
251         avctx->release_buffer(avctx, &c->pic);
252
253     c->pic.reference = 0;
254     if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0)
255         return ret;
256
257     bytestream2_init(&c->gb, pkt->data, pkt->size);
258
259     code = bytestream2_get_byte(&c->gb);
260     if (code & 0x20) {
261         for (i = 0; i < 4; i++)
262             memset(c->frame[i], 0, c->frame_size);
263
264         memset(c->pic.data[1], 0, AVPALETTE_SIZE);
265         c->current_frame = 0;
266         c->pic.key_frame = 1;
267         c->pic.pict_type = AV_PICTURE_TYPE_I;
268     } else {
269         c->pic.key_frame = 0;
270         c->pic.pict_type = AV_PICTURE_TYPE_P;
271     }
272
273     if (code & 0x40) {
274         uint32_t *out = (uint32_t *)c->pic.data[1];
275         int index, count;
276
277         index = bytestream2_get_byte(&c->gb);
278         count = bytestream2_get_byte(&c->gb) + 1;
279
280         if (index + count > AVPALETTE_SIZE)
281             return AVERROR_INVALIDDATA;
282         if (bytestream2_get_bytes_left(&c->gb) < 3 * AVPALETTE_SIZE)
283             return AVERROR_INVALIDDATA;
284
285         out += index;
286         for (i = 0; i < count; i++) {
287             unsigned r, g, b;
288
289             r = bytestream2_get_byteu(&c->gb);
290             r = r << 2 | r >> 4;
291             g = bytestream2_get_byteu(&c->gb);
292             g = g << 2 | g >> 4;
293             b = bytestream2_get_byteu(&c->gb);
294             b = b << 2 | b >> 4;
295             *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
296         }
297         c->pic.palette_has_changed = 1;
298     }
299
300     switch (code & 0x0F) {
301     case 0:
302         if ((ret = decode_0(avctx, code, pkt->data)) < 0)
303             return ret;
304         break;
305     case 1:
306         dst = c->frame[c->current_frame];
307         bytestream2_skip(&c->gb, 2);
308         if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
309             return AVERROR_INVALIDDATA;
310         bytestream2_get_bufferu(&c->gb, dst, c->video_size);
311         break;
312     case 2:
313         frame = bytestream2_get_byte(&c->gb);
314         if (frame > 3)
315             return AVERROR_INVALIDDATA;
316         if (frame != c->current_frame)
317             memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
318         break;
319     case 4:
320         dst = c->frame[c->current_frame];
321         end = dst + c->video_size;
322
323         bytestream2_skip(&c->gb, 2);
324
325         while (dst < end) {
326             int8_t code;
327             int count;
328
329             if (bytestream2_get_bytes_left(&c->gb) < 2)
330                 return AVERROR_INVALIDDATA;
331
332             code  = bytestream2_get_byteu(&c->gb);
333             count = FFABS(code) + 1;
334
335             if (dst + count > end)
336                 return AVERROR_INVALIDDATA;
337             if (code < 0)
338                 memset(dst, bytestream2_get_byteu(&c->gb), count);
339             else
340                 bytestream2_get_buffer(&c->gb, dst, count);
341             dst += count;
342         }
343         break;
344     default:
345         av_log_ask_for_sample(avctx, "unknown/invalid code\n");
346         return AVERROR_INVALIDDATA;
347     }
348
349     dst = c->pic.data[0];
350     src = c->frame[c->current_frame];
351     for (i = 0; i < avctx->height; i++) {
352         memcpy(dst, src, avctx->width);
353         dst += c->pic.linesize[0];
354         src += avctx->width;
355     }
356
357     c->current_frame = (c->current_frame + 1) & 3;
358
359     *data_size       = sizeof(AVFrame);
360     *(AVFrame *)data = c->pic;
361
362     return pkt->size;
363 }
364
365 static av_cold int paf_vid_close(AVCodecContext *avctx)
366 {
367     PAFVideoDecContext *c = avctx->priv_data;
368     int i;
369
370     if (c->pic.data[0])
371         avctx->release_buffer(avctx, &c->pic);
372
373     for (i = 0; i < 4; i++)
374         av_freep(&c->frame[i]);
375
376     return 0;
377 }
378
379 typedef struct PAFAudioDecContext {
380     AVFrame frame;
381 } PAFAudioDecContext;
382
383 static av_cold int paf_aud_init(AVCodecContext *avctx)
384 {
385     PAFAudioDecContext *c = avctx->priv_data;
386
387     if (avctx->channels != 2) {
388         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
389         return AVERROR_INVALIDDATA;
390     }
391
392     avcodec_get_frame_defaults(&c->frame);
393     avctx->channel_layout = AV_CH_LAYOUT_STEREO;
394     avctx->coded_frame    = &c->frame;
395     avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
396
397     return 0;
398 }
399
400 static int paf_aud_decode(AVCodecContext *avctx, void *data,
401                           int *got_frame_ptr, AVPacket *pkt)
402 {
403     PAFAudioDecContext *c = avctx->priv_data;
404     uint8_t *buf = pkt->data;
405     int16_t *output_samples;
406     const uint8_t *t;
407     int frames, ret, i, j, k;
408
409     frames = pkt->size / PAF_SOUND_FRAME_SIZE;
410     if (frames < 1)
411         return AVERROR_INVALIDDATA;
412
413     c->frame.nb_samples = PAF_SOUND_SAMPLES * frames;
414     if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0)
415         return ret;
416
417     output_samples = (int16_t *)c->frame.data[0];
418     for (i = 0; i < frames; i++) {
419         t = buf + 256 * sizeof(uint16_t);
420         for (j = 0; j < PAF_SOUND_SAMPLES; j++) {
421             for (k = 0; k < 2; k++) {
422                 *output_samples++ = AV_RL16(buf + *t++ * 2);
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             = 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             = 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 };