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 "libavutil/mem.h"
26 #include "bytestream.h"
27 #define BITSTREAM_READER_LE
31 typedef struct XanContext {
32 AVCodecContext *avctx;
36 uint8_t *scratch_buffer;
41 static av_cold int xan_decode_end(AVCodecContext *avctx)
43 XanContext *s = avctx->priv_data;
45 av_frame_free(&s->pic);
47 av_freep(&s->y_buffer);
48 av_freep(&s->scratch_buffer);
53 static av_cold int xan_decode_init(AVCodecContext *avctx)
55 XanContext *s = avctx->priv_data;
59 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
61 if (avctx->height < 8) {
62 av_log(avctx, AV_LOG_ERROR, "Invalid frame height: %d.\n", avctx->height);
63 return AVERROR(EINVAL);
65 if (avctx->width & 1) {
66 av_log(avctx, AV_LOG_ERROR, "Invalid frame width: %d.\n", avctx->width);
67 return AVERROR(EINVAL);
70 s->buffer_size = avctx->width * avctx->height;
71 s->y_buffer = av_malloc(s->buffer_size);
73 return AVERROR(ENOMEM);
74 s->scratch_buffer = av_malloc(s->buffer_size + 130);
75 if (!s->scratch_buffer) {
76 av_freep(&s->y_buffer);
77 return AVERROR(ENOMEM);
80 s->pic = av_frame_alloc();
82 xan_decode_end(avctx);
83 return AVERROR(ENOMEM);
89 static int xan_unpack_luma(XanContext *s,
90 uint8_t *dst, const int dst_size)
95 const uint8_t *dst_end = dst + dst_size;
96 GetByteContext tree = s->gb;
97 int start_off = bytestream2_tell(&tree);
99 tree_size = bytestream2_get_byte(&s->gb);
100 eof = bytestream2_get_byte(&s->gb);
101 tree_root = eof + tree_size;
102 bytestream2_skip(&s->gb, tree_size * 2);
105 bits = bytestream2_get_byte(&s->gb);
108 int bit = !!(bits & mask);
110 bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
111 node = bytestream2_get_byte(&tree);
121 if (bytestream2_get_bytes_left(&s->gb) <= 0)
123 bits = bytestream2_get_byteu(&s->gb);
127 return dst != dst_end ? AVERROR_INVALIDDATA : 0;
130 /* almost the same as in xan_wc3 decoder */
131 static int xan_unpack(XanContext *s,
132 uint8_t *dest, const int dest_len)
136 uint8_t *orig_dest = dest;
137 const uint8_t *dest_end = dest + dest_len;
139 while (dest < dest_end) {
140 if (bytestream2_get_bytes_left(&s->gb) <= 0)
141 return AVERROR_INVALIDDATA;
143 opcode = bytestream2_get_byteu(&s->gb);
147 if ((opcode & 0x80) == 0) {
149 back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
150 size2 = ((opcode & 0x1c) >> 2) + 3;
151 } else if ((opcode & 0x40) == 0) {
152 size = bytestream2_peek_byte(&s->gb) >> 6;
153 back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
154 size2 = (opcode & 0x3f) + 4;
157 back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
158 size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
159 if (size + size2 > dest_end - dest)
162 if (dest + size + size2 > dest_end ||
163 dest - orig_dest + size < back)
164 return AVERROR_INVALIDDATA;
165 bytestream2_get_buffer(&s->gb, dest, size);
167 av_memcpy_backptr(dest, back, size2);
170 int finish = opcode >= 0xfc;
172 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
173 if (dest_end - dest < size)
174 return AVERROR_INVALIDDATA;
175 bytestream2_get_buffer(&s->gb, dest, size);
181 return dest - orig_dest;
184 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
186 XanContext *s = avctx->priv_data;
190 const uint8_t *src, *src_end;
191 const uint8_t *table;
192 int mode, offset, dec_size, table_size;
196 if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
197 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
198 return AVERROR_INVALIDDATA;
200 bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
201 mode = bytestream2_get_le16(&s->gb);
202 table = s->gb.buffer;
203 table_size = bytestream2_get_le16(&s->gb);
204 offset = table_size * 2;
207 if (offset >= bytestream2_get_bytes_left(&s->gb)) {
208 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
209 return AVERROR_INVALIDDATA;
212 bytestream2_skip(&s->gb, offset);
213 memset(s->scratch_buffer, 0, s->buffer_size);
214 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
216 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
222 src = s->scratch_buffer;
223 src_end = src + dec_size;
225 for (j = 0; j < avctx->height >> 1; j++) {
226 for (i = 0; i < avctx->width >> 1; i++) {
228 if (val && val < table_size) {
229 val = AV_RL16(table + (val << 1));
230 uval = (val >> 3) & 0xF8;
231 vval = (val >> 8) & 0xF8;
232 U[i] = uval | (uval >> 5);
233 V[i] = vval | (vval >> 5);
238 U += s->pic->linesize[1];
239 V += s->pic->linesize[2];
241 if (avctx->height & 1) {
242 memcpy(U, U - s->pic->linesize[1], avctx->width >> 1);
243 memcpy(V, V - s->pic->linesize[2], avctx->width >> 1);
246 uint8_t *U2 = U + s->pic->linesize[1];
247 uint8_t *V2 = V + s->pic->linesize[2];
249 for (j = 0; j < avctx->height >> 2; j++) {
250 for (i = 0; i < avctx->width >> 1; i += 2) {
252 if (val && val < table_size) {
253 val = AV_RL16(table + (val << 1));
254 uval = (val >> 3) & 0xF8;
255 vval = (val >> 8) & 0xF8;
256 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
257 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
260 U += s->pic->linesize[1] * 2;
261 V += s->pic->linesize[2] * 2;
262 U2 += s->pic->linesize[1] * 2;
263 V2 += s->pic->linesize[2] * 2;
265 if (avctx->height & 3) {
266 int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
268 memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]);
269 memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]);
276 static int xan_decode_frame_type0(AVCodecContext *avctx)
278 XanContext *s = avctx->priv_data;
279 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
280 unsigned chroma_off, corr_off;
285 chroma_off = bytestream2_get_le32(&s->gb);
286 corr_off = bytestream2_get_le32(&s->gb);
288 if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
291 if (corr_off >= (s->gb.buffer_end - s->gb.buffer_start)) {
292 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
295 bytestream2_seek(&s->gb, 12, SEEK_SET);
296 ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
298 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
305 for (j = 1; j < avctx->width - 1; j += 2) {
306 cur = (last + *src++) & 0x1F;
307 ybuf[j] = last + cur;
308 ybuf[j+1] = cur << 1;
313 ybuf += avctx->width;
315 for (i = 1; i < avctx->height; i++) {
316 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
318 for (j = 1; j < avctx->width - 1; j += 2) {
319 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
320 ybuf[j] = last + cur;
321 ybuf[j+1] = cur << 1;
326 ybuf += avctx->width;
332 bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
333 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
336 for (i = 0; i < dec_size; i++)
337 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
341 ybuf = s->pic->data[0];
342 for (j = 0; j < avctx->height; j++) {
343 for (i = 0; i < avctx->width; i++)
344 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
346 ybuf += s->pic->linesize[0];
352 static int xan_decode_frame_type1(AVCodecContext *avctx)
354 XanContext *s = avctx->priv_data;
355 uint8_t *ybuf, *src = s->scratch_buffer;
360 if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
363 bytestream2_seek(&s->gb, 16, SEEK_SET);
364 ret = xan_unpack_luma(s, src,
365 s->buffer_size >> 1);
367 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
372 for (i = 0; i < avctx->height; i++) {
373 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
375 for (j = 1; j < avctx->width - 1; j += 2) {
376 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
377 ybuf[j] = (last + cur) >> 1;
382 ybuf += avctx->width;
386 ybuf = s->pic->data[0];
387 for (j = 0; j < avctx->height; j++) {
388 for (i = 0; i < avctx->width; i++)
389 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
391 ybuf += s->pic->linesize[0];
397 static int xan_decode_frame(AVCodecContext *avctx,
398 void *data, int *got_frame,
401 XanContext *s = avctx->priv_data;
405 if ((ret = ff_reget_buffer(avctx, s->pic))) {
406 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
410 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
411 ftype = bytestream2_get_le32(&s->gb);
414 ret = xan_decode_frame_type0(avctx);
417 ret = xan_decode_frame_type1(avctx);
420 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
421 return AVERROR_INVALIDDATA;
426 if ((ret = av_frame_ref(data, s->pic)) < 0)
434 AVCodec ff_xan_wc4_decoder = {
436 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
437 .type = AVMEDIA_TYPE_VIDEO,
438 .id = AV_CODEC_ID_XAN_WC4,
439 .priv_data_size = sizeof(XanContext),
440 .init = xan_decode_init,
441 .close = xan_decode_end,
442 .decode = xan_decode_frame,
443 .capabilities = CODEC_CAP_DR1,