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