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