2 * Wing Commander/Xan Video Decoder
3 * Copyright (C) 2011 Konstantin Shishkov
4 * based on work by Mike Melanson
6 * This file is part of Libav.
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.
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.
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
24 #include "libavutil/intreadwrite.h"
25 #include "bytestream.h"
26 #define BITSTREAM_READER_LE
28 // for av_memcpy_backptr
29 #include "libavutil/lzo.h"
31 typedef struct XanContext {
32 AVCodecContext *avctx;
36 uint8_t *scratch_buffer;
41 static av_cold int xan_decode_init(AVCodecContext *avctx)
43 XanContext *s = avctx->priv_data;
47 avctx->pix_fmt = PIX_FMT_YUV420P;
49 s->buffer_size = avctx->width * avctx->height;
50 s->y_buffer = av_malloc(s->buffer_size);
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);
62 static int xan_unpack_luma(XanContext *s,
63 uint8_t *dst, const int dst_size)
68 const uint8_t *dst_end = dst + dst_size;
69 GetByteContext tree = s->gb;
70 int start_off = bytestream2_tell(&tree);
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);
78 bits = bytestream2_get_byte(&s->gb);
81 int bit = !!(bits & mask);
83 bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
84 node = bytestream2_get_byte(&tree);
94 if (bytestream2_get_bytes_left(&s->gb) <= 0)
96 bits = bytestream2_get_byteu(&s->gb);
100 return dst != dst_end ? AVERROR_INVALIDDATA : 0;
103 /* almost the same as in xan_wc3 decoder */
104 static int xan_unpack(XanContext *s,
105 uint8_t *dest, const int dest_len)
109 uint8_t *orig_dest = dest;
110 const uint8_t *dest_end = dest + dest_len;
112 while (dest < dest_end) {
113 if (bytestream2_get_bytes_left(&s->gb) <= 0)
114 return AVERROR_INVALIDDATA;
116 opcode = bytestream2_get_byteu(&s->gb);
120 if ((opcode & 0x80) == 0) {
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;
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)
135 if (dest + size + size2 > dest_end ||
136 dest - orig_dest + size < back)
138 bytestream2_get_buffer(&s->gb, dest, size);
140 av_memcpy_backptr(dest, back, size2);
143 int finish = opcode >= 0xfc;
145 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
146 if (dest_end - dest < size)
148 bytestream2_get_buffer(&s->gb, dest, size);
154 return dest - orig_dest;
157 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
159 XanContext *s = avctx->priv_data;
163 const uint8_t *src, *src_end;
164 const uint8_t *table;
165 int mode, offset, dec_size, table_size;
169 if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
170 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
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;
180 if (offset >= bytestream2_get_bytes_left(&s->gb)) {
181 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
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);
189 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
195 src = s->scratch_buffer;
196 src_end = src + dec_size;
198 for (j = 0; j < avctx->height >> 1; j++) {
199 for (i = 0; i < avctx->width >> 1; i++) {
201 if (val && val < table_size) {
202 val = AV_RL16(table + (val << 1));
203 uval = (val >> 3) & 0xF8;
204 vval = (val >> 8) & 0xF8;
205 U[i] = uval | (uval >> 5);
206 V[i] = vval | (vval >> 5);
211 U += s->pic.linesize[1];
212 V += s->pic.linesize[2];
215 uint8_t *U2 = U + s->pic.linesize[1];
216 uint8_t *V2 = V + s->pic.linesize[2];
218 for (j = 0; j < avctx->height >> 2; j++) {
219 for (i = 0; i < avctx->width >> 1; i += 2) {
221 if (val && val < table_size) {
222 val = AV_RL16(table + (val << 1));
223 uval = (val >> 3) & 0xF8;
224 vval = (val >> 8) & 0xF8;
225 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
226 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
229 U += s->pic.linesize[1] * 2;
230 V += s->pic.linesize[2] * 2;
231 U2 += s->pic.linesize[1] * 2;
232 V2 += s->pic.linesize[2] * 2;
239 static int xan_decode_frame_type0(AVCodecContext *avctx)
241 XanContext *s = avctx->priv_data;
242 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
243 unsigned chroma_off, corr_off;
248 chroma_off = bytestream2_get_le32(&s->gb);
249 corr_off = bytestream2_get_le32(&s->gb);
251 if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
254 if (corr_off >= (s->gb.buffer_end - s->gb.buffer_start)) {
255 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
258 bytestream2_seek(&s->gb, 12, SEEK_SET);
259 ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
261 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
268 for (j = 1; j < avctx->width - 1; j += 2) {
269 cur = (last + *src++) & 0x1F;
270 ybuf[j] = last + cur;
271 ybuf[j+1] = cur << 1;
276 ybuf += avctx->width;
278 for (i = 1; i < avctx->height; i++) {
279 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
281 for (j = 1; j < avctx->width - 1; j += 2) {
282 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
283 ybuf[j] = last + cur;
284 ybuf[j+1] = cur << 1;
289 ybuf += avctx->width;
295 bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
296 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
299 for (i = 0; i < dec_size; i++)
300 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
304 ybuf = s->pic.data[0];
305 for (j = 0; j < avctx->height; j++) {
306 for (i = 0; i < avctx->width; i++)
307 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
309 ybuf += s->pic.linesize[0];
315 static int xan_decode_frame_type1(AVCodecContext *avctx)
317 XanContext *s = avctx->priv_data;
318 uint8_t *ybuf, *src = s->scratch_buffer;
323 if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
326 bytestream2_seek(&s->gb, 16, SEEK_SET);
327 ret = xan_unpack_luma(s, src,
328 s->buffer_size >> 1);
330 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
335 for (i = 0; i < avctx->height; i++) {
336 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
338 for (j = 1; j < avctx->width - 1; j += 2) {
339 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
340 ybuf[j] = (last + cur) >> 1;
345 ybuf += avctx->width;
349 ybuf = s->pic.data[0];
350 for (j = 0; j < avctx->height; j++) {
351 for (i = 0; i < avctx->width; i++)
352 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
354 ybuf += s->pic.linesize[0];
360 static int xan_decode_frame(AVCodecContext *avctx,
361 void *data, int *data_size,
364 XanContext *s = avctx->priv_data;
368 s->pic.reference = 1;
369 s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
370 FF_BUFFER_HINTS_PRESERVE |
371 FF_BUFFER_HINTS_REUSABLE;
372 if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
373 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
377 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
378 ftype = bytestream2_get_le32(&s->gb);
381 ret = xan_decode_frame_type0(avctx);
384 ret = xan_decode_frame_type1(avctx);
387 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
393 *data_size = sizeof(AVFrame);
394 *(AVFrame*)data = s->pic;
399 static av_cold int xan_decode_end(AVCodecContext *avctx)
401 XanContext *s = avctx->priv_data;
404 avctx->release_buffer(avctx, &s->pic);
406 av_freep(&s->y_buffer);
407 av_freep(&s->scratch_buffer);
412 AVCodec ff_xan_wc4_decoder = {
414 .type = AVMEDIA_TYPE_VIDEO,
415 .id = AV_CODEC_ID_XAN_WC4,
416 .priv_data_size = sizeof(XanContext),
417 .init = xan_decode_init,
418 .close = xan_decode_end,
419 .decode = xan_decode_frame,
420 .capabilities = CODEC_CAP_DR1,
421 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),