]> git.sesse.net Git - ffmpeg/blob - libavcodec/xxan.c
vp9: split superframes in the filtering stage before actual decoding
[ffmpeg] / libavcodec / xxan.c
1 /*
2  * Wing Commander/Xan Video Decoder
3  * Copyright (C) 2011 Konstantin Shishkov
4  * based on work by Mike Melanson
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "internal.h"
29
30 typedef struct XanContext {
31     AVCodecContext *avctx;
32     AVFrame *pic;
33
34     uint8_t *y_buffer;
35     uint8_t *scratch_buffer;
36     int     buffer_size;
37     GetByteContext gb;
38 } XanContext;
39
40 static av_cold int xan_decode_end(AVCodecContext *avctx)
41 {
42     XanContext *s = avctx->priv_data;
43
44     av_frame_free(&s->pic);
45
46     av_freep(&s->y_buffer);
47     av_freep(&s->scratch_buffer);
48
49     return 0;
50 }
51
52 static av_cold int xan_decode_init(AVCodecContext *avctx)
53 {
54     XanContext *s = avctx->priv_data;
55
56     s->avctx = avctx;
57
58     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
59
60     if (avctx->height < 8) {
61         av_log(avctx, AV_LOG_ERROR, "Invalid frame height: %d.\n", avctx->height);
62         return AVERROR(EINVAL);
63     }
64     if (avctx->width & 1) {
65         av_log(avctx, AV_LOG_ERROR, "Invalid frame width: %d.\n", avctx->width);
66         return AVERROR(EINVAL);
67     }
68
69     s->buffer_size = avctx->width * avctx->height;
70     s->y_buffer = av_malloc(s->buffer_size);
71     if (!s->y_buffer)
72         return AVERROR(ENOMEM);
73     s->scratch_buffer = av_malloc(s->buffer_size + 130);
74     if (!s->scratch_buffer) {
75         av_freep(&s->y_buffer);
76         return AVERROR(ENOMEM);
77     }
78
79     s->pic = av_frame_alloc();
80     if (!s->pic) {
81         xan_decode_end(avctx);
82         return AVERROR(ENOMEM);
83     }
84
85     return 0;
86 }
87
88 static int xan_unpack_luma(XanContext *s,
89                            uint8_t *dst, const int dst_size)
90 {
91     int tree_size, eof;
92     int bits, mask;
93     int tree_root, node;
94     const uint8_t *dst_end = dst + dst_size;
95     GetByteContext tree = s->gb;
96     int start_off = bytestream2_tell(&tree);
97
98     tree_size = bytestream2_get_byte(&s->gb);
99     eof       = bytestream2_get_byte(&s->gb);
100     tree_root = eof + tree_size;
101     bytestream2_skip(&s->gb, tree_size * 2);
102
103     node = tree_root;
104     bits = bytestream2_get_byte(&s->gb);
105     mask = 0x80;
106     for (;;) {
107         int bit = !!(bits & mask);
108         mask >>= 1;
109         bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
110         node = bytestream2_get_byte(&tree);
111         if (node == eof)
112             break;
113         if (node < eof) {
114             *dst++ = node;
115             if (dst > dst_end)
116                 break;
117             node = tree_root;
118         }
119         if (!mask) {
120             if (bytestream2_get_bytes_left(&s->gb) <= 0)
121                 break;
122             bits = bytestream2_get_byteu(&s->gb);
123             mask = 0x80;
124         }
125     }
126     return dst != dst_end ? AVERROR_INVALIDDATA : 0;
127 }
128
129 /* almost the same as in xan_wc3 decoder */
130 static int xan_unpack(XanContext *s,
131                       uint8_t *dest, const int dest_len)
132 {
133     uint8_t opcode;
134     int size;
135     uint8_t *orig_dest = dest;
136     const uint8_t *dest_end = dest + dest_len;
137
138     while (dest < dest_end) {
139         if (bytestream2_get_bytes_left(&s->gb) <= 0)
140             return AVERROR_INVALIDDATA;
141
142         opcode = bytestream2_get_byteu(&s->gb);
143
144         if (opcode < 0xe0) {
145             int size2, back;
146             if ((opcode & 0x80) == 0) {
147                 size  = opcode & 3;
148                 back  = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
149                 size2 = ((opcode & 0x1c) >> 2) + 3;
150             } else if ((opcode & 0x40) == 0) {
151                 size  = bytestream2_peek_byte(&s->gb) >> 6;
152                 back  = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
153                 size2 = (opcode & 0x3f) + 4;
154             } else {
155                 size  = opcode & 3;
156                 back  = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
157                 size2 = ((opcode & 0x0c) <<  6) + bytestream2_get_byte(&s->gb) + 5;
158                 if (size + size2 > dest_end - dest)
159                     break;
160             }
161             if (dest + size + size2 > dest_end ||
162                 dest - orig_dest + size < back)
163                 return AVERROR_INVALIDDATA;
164             bytestream2_get_buffer(&s->gb, dest, size);
165             dest += size;
166             av_memcpy_backptr(dest, back, size2);
167             dest += size2;
168         } else {
169             int finish = opcode >= 0xfc;
170
171             size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
172             if (dest_end - dest < size)
173                 return AVERROR_INVALIDDATA;
174             bytestream2_get_buffer(&s->gb, dest, size);
175             dest += size;
176             if (finish)
177                 break;
178         }
179     }
180     return dest - orig_dest;
181 }
182
183 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
184 {
185     XanContext *s = avctx->priv_data;
186     uint8_t *U, *V;
187     int val, uval, vval;
188     int i, j;
189     const uint8_t *src, *src_end;
190     const uint8_t *table;
191     int mode, offset, dec_size, table_size;
192
193     if (!chroma_off)
194         return 0;
195     if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
196         av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
197         return AVERROR_INVALIDDATA;
198     }
199     bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
200     mode        = bytestream2_get_le16(&s->gb);
201     table       = s->gb.buffer;
202     table_size  = bytestream2_get_le16(&s->gb);
203     offset      = table_size * 2;
204     table_size += 1;
205
206     if (offset >= bytestream2_get_bytes_left(&s->gb)) {
207         av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
208         return AVERROR_INVALIDDATA;
209     }
210
211     bytestream2_skip(&s->gb, offset);
212     memset(s->scratch_buffer, 0, s->buffer_size);
213     dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
214     if (dec_size < 0) {
215         av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
216         return dec_size;
217     }
218
219     U = s->pic->data[1];
220     V = s->pic->data[2];
221     src     = s->scratch_buffer;
222     src_end = src + dec_size;
223     if (mode) {
224         for (j = 0; j < avctx->height >> 1; j++) {
225             for (i = 0; i < avctx->width >> 1; i++) {
226                 val = *src++;
227                 if (val && val < table_size) {
228                     val  = AV_RL16(table + (val << 1));
229                     uval = (val >> 3) & 0xF8;
230                     vval = (val >> 8) & 0xF8;
231                     U[i] = uval | (uval >> 5);
232                     V[i] = vval | (vval >> 5);
233                 }
234                 if (src == src_end)
235                     return 0;
236             }
237             U += s->pic->linesize[1];
238             V += s->pic->linesize[2];
239         }
240         if (avctx->height & 1) {
241             memcpy(U, U - s->pic->linesize[1], avctx->width >> 1);
242             memcpy(V, V - s->pic->linesize[2], avctx->width >> 1);
243         }
244     } else {
245         uint8_t *U2 = U + s->pic->linesize[1];
246         uint8_t *V2 = V + s->pic->linesize[2];
247
248         for (j = 0; j < avctx->height >> 2; j++) {
249             for (i = 0; i < avctx->width >> 1; i += 2) {
250                 val = *src++;
251                 if (val && val < table_size) {
252                     val  = AV_RL16(table + (val << 1));
253                     uval = (val >> 3) & 0xF8;
254                     vval = (val >> 8) & 0xF8;
255                     U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
256                     V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
257                 }
258             }
259             U  += s->pic->linesize[1] * 2;
260             V  += s->pic->linesize[2] * 2;
261             U2 += s->pic->linesize[1] * 2;
262             V2 += s->pic->linesize[2] * 2;
263         }
264         if (avctx->height & 3) {
265             int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
266
267             memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]);
268             memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]);
269         }
270     }
271
272     return 0;
273 }
274
275 static int xan_decode_frame_type0(AVCodecContext *avctx)
276 {
277     XanContext *s = avctx->priv_data;
278     uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
279     unsigned  chroma_off, corr_off;
280     int cur, last;
281     int i, j;
282     int ret;
283
284     chroma_off = bytestream2_get_le32(&s->gb);
285     corr_off   = bytestream2_get_le32(&s->gb);
286
287     if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
288         return ret;
289
290     if (corr_off >= (s->gb.buffer_end - s->gb.buffer_start)) {
291         av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
292         corr_off = 0;
293     }
294     bytestream2_seek(&s->gb, 12, SEEK_SET);
295     ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
296     if (ret) {
297         av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
298         return ret;
299     }
300
301     ybuf = s->y_buffer;
302     last = *src++;
303     ybuf[0] = last << 1;
304     for (j = 1; j < avctx->width - 1; j += 2) {
305         cur = (last + *src++) & 0x1F;
306         ybuf[j]   = last + cur;
307         ybuf[j+1] = cur << 1;
308         last = cur;
309     }
310     ybuf[j]  = last << 1;
311     prev_buf = ybuf;
312     ybuf += avctx->width;
313
314     for (i = 1; i < avctx->height; i++) {
315         last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
316         ybuf[0] = last << 1;
317         for (j = 1; j < avctx->width - 1; j += 2) {
318             cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
319             ybuf[j]   = last + cur;
320             ybuf[j+1] = cur << 1;
321             last = cur;
322         }
323         ybuf[j] = last << 1;
324         prev_buf = ybuf;
325         ybuf += avctx->width;
326     }
327
328     if (corr_off) {
329         int dec_size;
330
331         bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
332         dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
333         if (dec_size < 0)
334             dec_size = 0;
335         for (i = 0; i < dec_size; i++)
336             s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
337     }
338
339     src  = s->y_buffer;
340     ybuf = s->pic->data[0];
341     for (j = 0; j < avctx->height; j++) {
342         for (i = 0; i < avctx->width; i++)
343             ybuf[i] = (src[i] << 2) | (src[i] >> 3);
344         src  += avctx->width;
345         ybuf += s->pic->linesize[0];
346     }
347
348     return 0;
349 }
350
351 static int xan_decode_frame_type1(AVCodecContext *avctx)
352 {
353     XanContext *s = avctx->priv_data;
354     uint8_t *ybuf, *src = s->scratch_buffer;
355     int cur, last;
356     int i, j;
357     int ret;
358
359     if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
360         return ret;
361
362     bytestream2_seek(&s->gb, 16, SEEK_SET);
363     ret = xan_unpack_luma(s, src,
364                           s->buffer_size >> 1);
365     if (ret) {
366         av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
367         return ret;
368     }
369
370     ybuf = s->y_buffer;
371     for (i = 0; i < avctx->height; i++) {
372         last = (ybuf[0] + (*src++ << 1)) & 0x3F;
373         ybuf[0] = last;
374         for (j = 1; j < avctx->width - 1; j += 2) {
375             cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
376             ybuf[j]   = (last + cur) >> 1;
377             ybuf[j+1] = cur;
378             last = cur;
379         }
380         ybuf[j] = last;
381         ybuf += avctx->width;
382     }
383
384     src = s->y_buffer;
385     ybuf = s->pic->data[0];
386     for (j = 0; j < avctx->height; j++) {
387         for (i = 0; i < avctx->width; i++)
388             ybuf[i] = (src[i] << 2) | (src[i] >> 3);
389         src  += avctx->width;
390         ybuf += s->pic->linesize[0];
391     }
392
393     return 0;
394 }
395
396 static int xan_decode_frame(AVCodecContext *avctx,
397                             void *data, int *got_frame,
398                             AVPacket *avpkt)
399 {
400     XanContext *s = avctx->priv_data;
401     int ftype;
402     int ret;
403
404     if ((ret = ff_reget_buffer(avctx, s->pic))) {
405         av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
406         return ret;
407     }
408
409     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
410     ftype = bytestream2_get_le32(&s->gb);
411     switch (ftype) {
412     case 0:
413         ret = xan_decode_frame_type0(avctx);
414         break;
415     case 1:
416         ret = xan_decode_frame_type1(avctx);
417         break;
418     default:
419         av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
420         return AVERROR_INVALIDDATA;
421     }
422     if (ret)
423         return ret;
424
425     if ((ret = av_frame_ref(data, s->pic)) < 0)
426         return ret;
427
428     *got_frame = 1;
429
430     return avpkt->size;
431 }
432
433 AVCodec ff_xan_wc4_decoder = {
434     .name           = "xan_wc4",
435     .long_name      = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
436     .type           = AVMEDIA_TYPE_VIDEO,
437     .id             = AV_CODEC_ID_XAN_WC4,
438     .priv_data_size = sizeof(XanContext),
439     .init           = xan_decode_init,
440     .close          = xan_decode_end,
441     .decode         = xan_decode_frame,
442     .capabilities   = AV_CODEC_CAP_DR1,
443 };