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
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
27 #include "bytestream.h"
30 typedef struct XanContext {
31 AVCodecContext *avctx;
35 uint8_t *scratch_buffer;
40 static av_cold int xan_decode_end(AVCodecContext *avctx)
42 XanContext *s = avctx->priv_data;
44 av_frame_free(&s->pic);
46 av_freep(&s->y_buffer);
47 av_freep(&s->scratch_buffer);
52 static av_cold int xan_decode_init(AVCodecContext *avctx)
54 XanContext *s = avctx->priv_data;
58 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
60 if (avctx->height < 8) {
61 av_log(avctx, AV_LOG_ERROR, "Invalid frame height: %d.\n", avctx->height);
62 return AVERROR(EINVAL);
64 if (avctx->width & 1) {
65 av_log(avctx, AV_LOG_ERROR, "Invalid frame width: %d.\n", avctx->width);
66 return AVERROR(EINVAL);
69 s->buffer_size = avctx->width * avctx->height;
70 s->y_buffer = av_malloc(s->buffer_size);
72 return AVERROR(ENOMEM);
73 s->scratch_buffer = av_malloc(s->buffer_size + 130);
74 if (!s->scratch_buffer)
75 return AVERROR(ENOMEM);
77 s->pic = av_frame_alloc();
79 return AVERROR(ENOMEM);
84 static int xan_unpack_luma(XanContext *s,
85 uint8_t *dst, const int dst_size)
90 const uint8_t *dst_end = dst + dst_size;
91 GetByteContext tree = s->gb;
92 int start_off = bytestream2_tell(&tree);
94 tree_size = bytestream2_get_byte(&s->gb);
95 eof = bytestream2_get_byte(&s->gb);
96 tree_root = eof + tree_size;
97 bytestream2_skip(&s->gb, tree_size * 2);
100 bits = bytestream2_get_byte(&s->gb);
103 int bit = !!(bits & mask);
105 bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
106 node = bytestream2_get_byte(&tree);
116 if (bytestream2_get_bytes_left(&s->gb) <= 0)
118 bits = bytestream2_get_byteu(&s->gb);
122 return dst != dst_end ? AVERROR_INVALIDDATA : 0;
125 /* almost the same as in xan_wc3 decoder */
126 static int xan_unpack(XanContext *s,
127 uint8_t *dest, const int dest_len)
131 uint8_t *orig_dest = dest;
132 const uint8_t *dest_end = dest + dest_len;
134 while (dest < dest_end) {
135 if (bytestream2_get_bytes_left(&s->gb) <= 0)
136 return AVERROR_INVALIDDATA;
138 opcode = bytestream2_get_byteu(&s->gb);
142 if ((opcode & 0x80) == 0) {
144 back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
145 size2 = ((opcode & 0x1c) >> 2) + 3;
146 } else if ((opcode & 0x40) == 0) {
147 size = bytestream2_peek_byte(&s->gb) >> 6;
148 back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
149 size2 = (opcode & 0x3f) + 4;
152 back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
153 size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
154 if (size + size2 > dest_end - dest)
157 if (dest + size + size2 > dest_end ||
158 dest - orig_dest + size < back)
159 return AVERROR_INVALIDDATA;
160 bytestream2_get_buffer(&s->gb, dest, size);
162 av_memcpy_backptr(dest, back, size2);
165 int finish = opcode >= 0xfc;
167 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
168 if (dest_end - dest < size)
169 return AVERROR_INVALIDDATA;
170 bytestream2_get_buffer(&s->gb, dest, size);
176 return dest - orig_dest;
179 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
181 XanContext *s = avctx->priv_data;
185 const uint8_t *src, *src_end;
186 const uint8_t *table;
187 int mode, offset, dec_size, table_size;
191 if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
192 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
193 return AVERROR_INVALIDDATA;
195 bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
196 mode = bytestream2_get_le16(&s->gb);
197 table = s->gb.buffer;
198 table_size = bytestream2_get_le16(&s->gb);
199 offset = table_size * 2;
202 if (offset >= bytestream2_get_bytes_left(&s->gb)) {
203 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
204 return AVERROR_INVALIDDATA;
207 bytestream2_skip(&s->gb, offset);
208 memset(s->scratch_buffer, 0, s->buffer_size);
209 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
211 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
217 src = s->scratch_buffer;
218 src_end = src + dec_size;
220 for (j = 0; j < avctx->height >> 1; j++) {
221 for (i = 0; i < avctx->width >> 1; i++) {
222 if (src_end - src < 1)
226 if (val >= table_size)
227 return AVERROR_INVALIDDATA;
228 val = AV_RL16(table + (val << 1));
229 uval = (val >> 3) & 0xF8;
230 vval = (val >> 8) & 0xF8;
231 U[i] = uval | (uval >> 5);
232 V[i] = vval | (vval >> 5);
235 U += s->pic->linesize[1];
236 V += s->pic->linesize[2];
238 if (avctx->height & 1) {
239 memcpy(U, U - s->pic->linesize[1], avctx->width >> 1);
240 memcpy(V, V - s->pic->linesize[2], avctx->width >> 1);
243 uint8_t *U2 = U + s->pic->linesize[1];
244 uint8_t *V2 = V + s->pic->linesize[2];
246 for (j = 0; j < avctx->height >> 2; j++) {
247 for (i = 0; i < avctx->width >> 1; i += 2) {
248 if (src_end - src < 1)
252 if (val >= table_size)
253 return AVERROR_INVALIDDATA;
254 val = AV_RL16(table + (val << 1));
255 uval = (val >> 3) & 0xF8;
256 vval = (val >> 8) & 0xF8;
257 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
258 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
261 U += s->pic->linesize[1] * 2;
262 V += s->pic->linesize[2] * 2;
263 U2 += s->pic->linesize[1] * 2;
264 V2 += s->pic->linesize[2] * 2;
266 if (avctx->height & 3) {
267 int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
269 memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]);
270 memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]);
277 static int xan_decode_frame_type0(AVCodecContext *avctx)
279 XanContext *s = avctx->priv_data;
280 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
281 unsigned chroma_off, corr_off;
286 chroma_off = bytestream2_get_le32(&s->gb);
287 corr_off = bytestream2_get_le32(&s->gb);
289 if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
292 if (corr_off >= bytestream2_size(&s->gb)) {
293 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
296 bytestream2_seek(&s->gb, 12, SEEK_SET);
297 ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
299 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
306 for (j = 1; j < avctx->width - 1; j += 2) {
307 cur = (last + *src++) & 0x1F;
308 ybuf[j] = last + cur;
309 ybuf[j+1] = cur << 1;
314 ybuf += avctx->width;
316 for (i = 1; i < avctx->height; i++) {
317 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
319 for (j = 1; j < avctx->width - 1; j += 2) {
320 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
321 ybuf[j] = last + cur;
322 ybuf[j+1] = cur << 1;
327 ybuf += avctx->width;
333 bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
334 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
338 dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
340 for (i = 0; i < dec_size; i++)
341 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
345 ybuf = s->pic->data[0];
346 for (j = 0; j < avctx->height; j++) {
347 for (i = 0; i < avctx->width; i++)
348 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
350 ybuf += s->pic->linesize[0];
356 static int xan_decode_frame_type1(AVCodecContext *avctx)
358 XanContext *s = avctx->priv_data;
359 uint8_t *ybuf, *src = s->scratch_buffer;
364 if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
367 bytestream2_seek(&s->gb, 16, SEEK_SET);
368 ret = xan_unpack_luma(s, src,
369 s->buffer_size >> 1);
371 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
376 for (i = 0; i < avctx->height; i++) {
377 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
379 for (j = 1; j < avctx->width - 1; j += 2) {
380 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
381 ybuf[j] = (last + cur) >> 1;
386 ybuf += avctx->width;
390 ybuf = s->pic->data[0];
391 for (j = 0; j < avctx->height; j++) {
392 for (i = 0; i < avctx->width; i++)
393 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
395 ybuf += s->pic->linesize[0];
401 static int xan_decode_frame(AVCodecContext *avctx,
402 void *data, int *got_frame,
405 XanContext *s = avctx->priv_data;
409 if ((ret = ff_reget_buffer(avctx, s->pic, 0)) < 0)
412 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
413 ftype = bytestream2_get_le32(&s->gb);
416 ret = xan_decode_frame_type0(avctx);
419 ret = xan_decode_frame_type1(avctx);
422 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
423 return AVERROR_INVALIDDATA;
428 if ((ret = av_frame_ref(data, s->pic)) < 0)
436 AVCodec ff_xan_wc4_decoder = {
438 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
439 .type = AVMEDIA_TYPE_VIDEO,
440 .id = AV_CODEC_ID_XAN_WC4,
441 .priv_data_size = sizeof(XanContext),
442 .init = xan_decode_init,
443 .close = xan_decode_end,
444 .decode = xan_decode_frame,
445 .capabilities = AV_CODEC_CAP_DR1,
446 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,