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;
40 static av_cold int xan_decode_init(AVCodecContext *avctx)
42 XanContext *s = avctx->priv_data;
46 avctx->pix_fmt = PIX_FMT_YUV420P;
48 s->buffer_size = avctx->width * avctx->height;
49 s->y_buffer = av_malloc(s->buffer_size);
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);
61 static int xan_unpack_luma(const uint8_t *src, const int src_size,
62 uint8_t *dst, const int dst_size)
68 const uint8_t *dst_end = dst + dst_size;
69 const uint8_t *src_end = src + src_size;
73 tree = src - eof * 2 - 2;
74 tree_root = eof + tree_size;
81 int bit = !!(bits & mask);
83 node = tree[node*2 + bit];
99 return dst != dst_end;
102 /* almost the same as in xan_wc3 decoder */
103 static int xan_unpack(uint8_t *dest, const int dest_len,
104 const uint8_t *src, const int src_len)
108 uint8_t *orig_dest = dest;
109 const uint8_t *src_end = src + src_len;
110 const uint8_t *dest_end = dest + dest_len;
112 while (dest < dest_end) {
117 if ((opcode & 0x80) == 0) {
119 back = ((opcode & 0x60) << 3) + *src++ + 1;
120 size2 = ((opcode & 0x1c) >> 2) + 3;
121 } else if ((opcode & 0x40) == 0) {
123 back = (bytestream_get_be16(&src) & 0x3fff) + 1;
124 size2 = (opcode & 0x3f) + 4;
127 back = ((opcode & 0x10) << 12) + bytestream_get_be16(&src) + 1;
128 size2 = ((opcode & 0x0c) << 6) + *src++ + 5;
129 if (size + size2 > dest_end - dest)
132 if (src + size > src_end || dest + size + size2 > dest_end)
134 bytestream_get_buffer(&src, dest, size);
136 av_memcpy_backptr(dest, back, size2);
139 int finish = opcode >= 0xfc;
141 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
142 if (src + size > src_end || dest + size > dest_end)
144 bytestream_get_buffer(&src, dest, size);
150 return dest - orig_dest;
153 static int xan_decode_chroma(AVCodecContext *avctx, AVPacket *avpkt)
155 const uint8_t *buf = avpkt->data;
156 XanContext *s = avctx->priv_data;
161 const uint8_t *src, *src_end;
162 const uint8_t *table;
163 int mode, offset, dec_size;
165 chroma_off = AV_RL32(buf + 4);
168 if (chroma_off + 10 >= avpkt->size) {
169 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
172 src = avpkt->data + 4 + chroma_off;
174 mode = bytestream_get_le16(&src);
175 offset = bytestream_get_le16(&src) * 2;
177 if (src - avpkt->data >= avpkt->size - offset) {
178 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
182 memset(s->scratch_buffer, 0, s->buffer_size);
183 dec_size = xan_unpack(s->scratch_buffer, s->buffer_size, src + offset,
184 avpkt->size - offset - (src - avpkt->data));
186 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
192 src = s->scratch_buffer;
193 src_end = src + dec_size;
195 for (j = 0; j < avctx->height >> 1; j++) {
196 for (i = 0; i < avctx->width >> 1; i++) {
199 val = AV_RL16(table + (val << 1));
200 uval = (val >> 3) & 0xF8;
201 vval = (val >> 8) & 0xF8;
202 U[i] = uval | (uval >> 5);
203 V[i] = vval | (vval >> 5);
208 U += s->pic.linesize[1];
209 V += s->pic.linesize[2];
212 uint8_t *U2 = U + s->pic.linesize[1];
213 uint8_t *V2 = V + s->pic.linesize[2];
215 for (j = 0; j < avctx->height >> 2; j++) {
216 for (i = 0; i < avctx->width >> 1; i += 2) {
219 val = AV_RL16(table + (val << 1));
220 uval = (val >> 3) & 0xF8;
221 vval = (val >> 8) & 0xF8;
222 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
223 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
226 U += s->pic.linesize[1] * 2;
227 V += s->pic.linesize[2] * 2;
228 U2 += s->pic.linesize[1] * 2;
229 V2 += s->pic.linesize[2] * 2;
236 static int xan_decode_frame_type0(AVCodecContext *avctx, AVPacket *avpkt)
238 const uint8_t *buf = avpkt->data;
239 XanContext *s = avctx->priv_data;
240 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
241 unsigned chroma_off, corr_off;
246 corr_off = AV_RL32(buf + 8);
247 chroma_off = AV_RL32(buf + 4);
249 if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
252 size = avpkt->size - 4;
253 if (corr_off >= avpkt->size) {
254 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
260 size = FFMIN(size, chroma_off);
261 ret = xan_unpack_luma(buf + 12, size, src, s->buffer_size >> 1);
263 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
270 for (j = 1; j < avctx->width - 1; j += 2) {
271 cur = (last + *src++) & 0x1F;
272 ybuf[j] = last + cur;
273 ybuf[j+1] = cur << 1;
278 ybuf += avctx->width;
280 for (i = 1; i < avctx->height; i++) {
281 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
283 for (j = 1; j < avctx->width - 1; j += 2) {
284 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
285 ybuf[j] = last + cur;
286 ybuf[j+1] = cur << 1;
291 ybuf += avctx->width;
295 int corr_end, dec_size;
297 corr_end = avpkt->size;
298 if (chroma_off > corr_off)
299 corr_end = chroma_off;
300 dec_size = xan_unpack(s->scratch_buffer, s->buffer_size,
301 avpkt->data + 8 + corr_off,
302 corr_end - corr_off);
305 for (i = 0; i < dec_size; i++)
306 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
310 ybuf = s->pic.data[0];
311 for (j = 0; j < avctx->height; j++) {
312 for (i = 0; i < avctx->width; i++)
313 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
315 ybuf += s->pic.linesize[0];
321 static int xan_decode_frame_type1(AVCodecContext *avctx, AVPacket *avpkt)
323 const uint8_t *buf = avpkt->data;
324 XanContext *s = avctx->priv_data;
325 uint8_t *ybuf, *src = s->scratch_buffer;
330 if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
333 ret = xan_unpack_luma(buf + 16, avpkt->size - 16, src,
334 s->buffer_size >> 1);
336 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
341 for (i = 0; i < avctx->height; i++) {
342 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
344 for (j = 1; j < avctx->width - 1; j += 2) {
345 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
346 ybuf[j] = (last + cur) >> 1;
351 ybuf += avctx->width;
355 ybuf = s->pic.data[0];
356 for (j = 0; j < avctx->height; j++) {
357 for (i = 0; i < avctx->width; i++)
358 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
360 ybuf += s->pic.linesize[0];
366 static int xan_decode_frame(AVCodecContext *avctx,
367 void *data, int *data_size,
370 XanContext *s = avctx->priv_data;
374 s->pic.reference = 1;
375 s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
376 FF_BUFFER_HINTS_PRESERVE |
377 FF_BUFFER_HINTS_REUSABLE;
378 if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
379 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
383 ftype = AV_RL32(avpkt->data);
386 ret = xan_decode_frame_type0(avctx, avpkt);
389 ret = xan_decode_frame_type1(avctx, avpkt);
392 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
398 *data_size = sizeof(AVFrame);
399 *(AVFrame*)data = s->pic;
404 static av_cold int xan_decode_end(AVCodecContext *avctx)
406 XanContext *s = avctx->priv_data;
409 avctx->release_buffer(avctx, &s->pic);
411 av_freep(&s->y_buffer);
412 av_freep(&s->scratch_buffer);
417 AVCodec ff_xan_wc4_decoder = {
419 .type = AVMEDIA_TYPE_VIDEO,
420 .id = CODEC_ID_XAN_WC4,
421 .priv_data_size = sizeof(XanContext),
422 .init = xan_decode_init,
423 .close = xan_decode_end,
424 .decode = xan_decode_frame,
425 .capabilities = CODEC_CAP_DR1,
426 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),