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;
166 int mode, offset, dec_size;
168 chroma_off = AV_RL32(buf + 4);
171 if (chroma_off + 10 >= avpkt->size) {
172 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
175 src = avpkt->data + 4 + chroma_off;
177 table_size = avpkt->data + avpkt->size - table;
178 mode = bytestream_get_le16(&src);
179 offset = bytestream_get_le16(&src) * 2;
181 if (src - avpkt->data >= avpkt->size - offset) {
182 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
186 memset(s->scratch_buffer, 0, s->buffer_size);
187 dec_size = xan_unpack(s->scratch_buffer, s->buffer_size, src + offset,
188 avpkt->size - offset - (src - avpkt->data));
190 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
196 src = s->scratch_buffer;
197 src_end = src + dec_size;
199 for (j = 0; j < avctx->height >> 1; j++) {
200 for (i = 0; i < avctx->width >> 1; i++) {
201 if (src_end - src < 1)
205 if (val << 1 >= table_size)
206 return AVERROR_INVALIDDATA;
207 val = AV_RL16(table + (val << 1));
208 uval = (val >> 3) & 0xF8;
209 vval = (val >> 8) & 0xF8;
210 U[i] = uval | (uval >> 5);
211 V[i] = vval | (vval >> 5);
214 U += s->pic.linesize[1];
215 V += s->pic.linesize[2];
218 uint8_t *U2 = U + s->pic.linesize[1];
219 uint8_t *V2 = V + s->pic.linesize[2];
221 for (j = 0; j < avctx->height >> 2; j++) {
222 for (i = 0; i < avctx->width >> 1; i += 2) {
223 if (src_end - src < 1)
227 if (val << 1 >= table_size)
228 return AVERROR_INVALIDDATA;
229 val = AV_RL16(table + (val << 1));
230 uval = (val >> 3) & 0xF8;
231 vval = (val >> 8) & 0xF8;
232 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
233 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
236 U += s->pic.linesize[1] * 2;
237 V += s->pic.linesize[2] * 2;
238 U2 += s->pic.linesize[1] * 2;
239 V2 += s->pic.linesize[2] * 2;
246 static int xan_decode_frame_type0(AVCodecContext *avctx, AVPacket *avpkt)
248 const uint8_t *buf = avpkt->data;
249 XanContext *s = avctx->priv_data;
250 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
251 unsigned chroma_off, corr_off;
256 corr_off = AV_RL32(buf + 8);
257 chroma_off = AV_RL32(buf + 4);
259 if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
262 size = avpkt->size - 4;
263 if (corr_off >= avpkt->size) {
264 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
270 size = FFMIN(size, chroma_off);
271 ret = xan_unpack_luma(buf + 12, size, src, s->buffer_size >> 1);
273 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
280 for (j = 1; j < avctx->width - 1; j += 2) {
281 cur = (last + *src++) & 0x1F;
282 ybuf[j] = last + cur;
283 ybuf[j+1] = cur << 1;
288 ybuf += avctx->width;
290 for (i = 1; i < avctx->height; i++) {
291 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
293 for (j = 1; j < avctx->width - 1; j += 2) {
294 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
295 ybuf[j] = last + cur;
296 ybuf[j+1] = cur << 1;
301 ybuf += avctx->width;
305 int corr_end, dec_size;
307 corr_end = avpkt->size;
308 if (chroma_off > corr_off)
309 corr_end = chroma_off;
310 dec_size = xan_unpack(s->scratch_buffer, s->buffer_size,
311 avpkt->data + 8 + corr_off,
312 corr_end - corr_off);
316 dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
318 for (i = 0; i < dec_size; i++)
319 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
323 ybuf = s->pic.data[0];
324 for (j = 0; j < avctx->height; j++) {
325 for (i = 0; i < avctx->width; i++)
326 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
328 ybuf += s->pic.linesize[0];
334 static int xan_decode_frame_type1(AVCodecContext *avctx, AVPacket *avpkt)
336 const uint8_t *buf = avpkt->data;
337 XanContext *s = avctx->priv_data;
338 uint8_t *ybuf, *src = s->scratch_buffer;
343 if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
346 ret = xan_unpack_luma(buf + 16, avpkt->size - 16, src,
347 s->buffer_size >> 1);
349 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
354 for (i = 0; i < avctx->height; i++) {
355 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
357 for (j = 1; j < avctx->width - 1; j += 2) {
358 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
359 ybuf[j] = (last + cur) >> 1;
364 ybuf += avctx->width;
368 ybuf = s->pic.data[0];
369 for (j = 0; j < avctx->height; j++) {
370 for (i = 0; i < avctx->width; i++)
371 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
373 ybuf += s->pic.linesize[0];
379 static int xan_decode_frame(AVCodecContext *avctx,
380 void *data, int *data_size,
383 XanContext *s = avctx->priv_data;
387 s->pic.reference = 3;
388 s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
389 FF_BUFFER_HINTS_PRESERVE |
390 FF_BUFFER_HINTS_REUSABLE;
391 if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
392 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
396 ftype = AV_RL32(avpkt->data);
399 ret = xan_decode_frame_type0(avctx, avpkt);
402 ret = xan_decode_frame_type1(avctx, avpkt);
405 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
411 *data_size = sizeof(AVFrame);
412 *(AVFrame*)data = s->pic;
417 static av_cold int xan_decode_end(AVCodecContext *avctx)
419 XanContext *s = avctx->priv_data;
422 avctx->release_buffer(avctx, &s->pic);
424 av_freep(&s->y_buffer);
425 av_freep(&s->scratch_buffer);
430 AVCodec ff_xan_wc4_decoder = {
432 .type = AVMEDIA_TYPE_VIDEO,
433 .id = CODEC_ID_XAN_WC4,
434 .priv_data_size = sizeof(XanContext),
435 .init = xan_decode_init,
436 .close = xan_decode_end,
437 .decode = xan_decode_frame,
438 .capabilities = CODEC_CAP_DR1,
439 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),