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