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 FFmpeg.
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.
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.
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
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 ||
133 dest + size + size2 > dest_end ||
134 dest + size - orig_dest < back )
136 bytestream_get_buffer(&src, dest, size);
138 av_memcpy_backptr(dest, back, size2);
141 int finish = opcode >= 0xfc;
143 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
144 if (src + size > src_end || dest + size > dest_end)
146 bytestream_get_buffer(&src, dest, size);
152 return dest - orig_dest;
155 static int xan_decode_chroma(AVCodecContext *avctx, AVPacket *avpkt)
157 const uint8_t *buf = avpkt->data;
158 XanContext *s = avctx->priv_data;
163 const uint8_t *src, *src_end;
164 const uint8_t *table;
165 int mode, offset, dec_size;
167 chroma_off = AV_RL32(buf + 4);
170 if (chroma_off + 10 >= avpkt->size) {
171 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
174 src = avpkt->data + 4 + chroma_off;
176 mode = bytestream_get_le16(&src);
177 offset = bytestream_get_le16(&src) * 2;
179 if (src - avpkt->data >= avpkt->size - offset) {
180 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
184 memset(s->scratch_buffer, 0, s->buffer_size);
185 dec_size = xan_unpack(s->scratch_buffer, s->buffer_size, src + offset,
186 avpkt->size - offset - (src - avpkt->data));
188 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
194 src = s->scratch_buffer;
195 src_end = src + dec_size;
197 for (j = 0; j < avctx->height >> 1; j++) {
198 for (i = 0; i < avctx->width >> 1; i++) {
199 if (src_end - src < 1)
203 val = AV_RL16(table + (val << 1));
204 uval = (val >> 3) & 0xF8;
205 vval = (val >> 8) & 0xF8;
206 U[i] = uval | (uval >> 5);
207 V[i] = vval | (vval >> 5);
210 U += s->pic.linesize[1];
211 V += s->pic.linesize[2];
214 uint8_t *U2 = U + s->pic.linesize[1];
215 uint8_t *V2 = V + s->pic.linesize[2];
217 for (j = 0; j < avctx->height >> 2; j++) {
218 for (i = 0; i < avctx->width >> 1; i += 2) {
219 if (src_end - src < 1)
223 val = AV_RL16(table + (val << 1));
224 uval = (val >> 3) & 0xF8;
225 vval = (val >> 8) & 0xF8;
226 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
227 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
230 U += s->pic.linesize[1] * 2;
231 V += s->pic.linesize[2] * 2;
232 U2 += s->pic.linesize[1] * 2;
233 V2 += s->pic.linesize[2] * 2;
240 static int xan_decode_frame_type0(AVCodecContext *avctx, AVPacket *avpkt)
242 const uint8_t *buf = avpkt->data;
243 XanContext *s = avctx->priv_data;
244 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
245 unsigned chroma_off, corr_off;
250 corr_off = AV_RL32(buf + 8);
251 chroma_off = AV_RL32(buf + 4);
253 if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
256 size = avpkt->size - 4;
257 if (corr_off >= avpkt->size) {
258 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
264 size = FFMIN(size, chroma_off);
265 ret = xan_unpack_luma(buf + 12, size, src, s->buffer_size >> 1);
267 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
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;
282 ybuf += avctx->width;
284 for (i = 1; i < avctx->height; i++) {
285 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
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;
295 ybuf += avctx->width;
299 int corr_end, dec_size;
301 corr_end = avpkt->size;
302 if (chroma_off > corr_off)
303 corr_end = chroma_off;
304 dec_size = xan_unpack(s->scratch_buffer, s->buffer_size,
305 avpkt->data + 8 + corr_off,
306 corr_end - corr_off);
310 dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
312 for (i = 0; i < dec_size; i++)
313 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
317 ybuf = s->pic.data[0];
318 for (j = 0; j < avctx->height; j++) {
319 for (i = 0; i < avctx->width; i++)
320 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
322 ybuf += s->pic.linesize[0];
328 static int xan_decode_frame_type1(AVCodecContext *avctx, AVPacket *avpkt)
330 const uint8_t *buf = avpkt->data;
331 XanContext *s = avctx->priv_data;
332 uint8_t *ybuf, *src = s->scratch_buffer;
337 if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
340 ret = xan_unpack_luma(buf + 16, avpkt->size - 16, src,
341 s->buffer_size >> 1);
343 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
348 for (i = 0; i < avctx->height; i++) {
349 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
351 for (j = 1; j < avctx->width - 1; j += 2) {
352 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
353 ybuf[j] = (last + cur) >> 1;
358 ybuf += avctx->width;
362 ybuf = s->pic.data[0];
363 for (j = 0; j < avctx->height; j++) {
364 for (i = 0; i < avctx->width; i++)
365 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
367 ybuf += s->pic.linesize[0];
373 static int xan_decode_frame(AVCodecContext *avctx,
374 void *data, int *data_size,
377 XanContext *s = avctx->priv_data;
381 s->pic.reference = 3;
382 s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
383 FF_BUFFER_HINTS_PRESERVE |
384 FF_BUFFER_HINTS_REUSABLE;
385 if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
386 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
390 ftype = AV_RL32(avpkt->data);
393 ret = xan_decode_frame_type0(avctx, avpkt);
396 ret = xan_decode_frame_type1(avctx, avpkt);
399 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
405 *data_size = sizeof(AVFrame);
406 *(AVFrame*)data = s->pic;
411 static av_cold int xan_decode_end(AVCodecContext *avctx)
413 XanContext *s = avctx->priv_data;
416 avctx->release_buffer(avctx, &s->pic);
418 av_freep(&s->y_buffer);
419 av_freep(&s->scratch_buffer);
424 AVCodec ff_xan_wc4_decoder = {
426 .type = AVMEDIA_TYPE_VIDEO,
427 .id = 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"),