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 xan_decode_end(avctx);
76 return AVERROR(ENOMEM);
79 s->pic = av_frame_alloc();
81 xan_decode_end(avctx);
82 return AVERROR(ENOMEM);
88 static int xan_unpack_luma(XanContext *s,
89 uint8_t *dst, const int dst_size)
94 const uint8_t *dst_end = dst + dst_size;
95 GetByteContext tree = s->gb;
96 int start_off = bytestream2_tell(&tree);
98 tree_size = bytestream2_get_byte(&s->gb);
99 eof = bytestream2_get_byte(&s->gb);
100 tree_root = eof + tree_size;
101 bytestream2_skip(&s->gb, tree_size * 2);
104 bits = bytestream2_get_byte(&s->gb);
107 int bit = !!(bits & mask);
109 bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
110 node = bytestream2_get_byte(&tree);
120 if (bytestream2_get_bytes_left(&s->gb) <= 0)
122 bits = bytestream2_get_byteu(&s->gb);
126 return dst != dst_end ? AVERROR_INVALIDDATA : 0;
129 /* almost the same as in xan_wc3 decoder */
130 static int xan_unpack(XanContext *s,
131 uint8_t *dest, const int dest_len)
135 uint8_t *orig_dest = dest;
136 const uint8_t *dest_end = dest + dest_len;
138 while (dest < dest_end) {
139 if (bytestream2_get_bytes_left(&s->gb) <= 0)
140 return AVERROR_INVALIDDATA;
142 opcode = bytestream2_get_byteu(&s->gb);
146 if ((opcode & 0x80) == 0) {
148 back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
149 size2 = ((opcode & 0x1c) >> 2) + 3;
150 } else if ((opcode & 0x40) == 0) {
151 size = bytestream2_peek_byte(&s->gb) >> 6;
152 back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
153 size2 = (opcode & 0x3f) + 4;
156 back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
157 size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
158 if (size + size2 > dest_end - dest)
161 if (dest + size + size2 > dest_end ||
162 dest - orig_dest + size < back)
163 return AVERROR_INVALIDDATA;
164 bytestream2_get_buffer(&s->gb, dest, size);
166 av_memcpy_backptr(dest, back, size2);
169 int finish = opcode >= 0xfc;
171 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
172 if (dest_end - dest < size)
173 return AVERROR_INVALIDDATA;
174 bytestream2_get_buffer(&s->gb, dest, size);
180 return dest - orig_dest;
183 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
185 XanContext *s = avctx->priv_data;
189 const uint8_t *src, *src_end;
190 const uint8_t *table;
191 int mode, offset, dec_size, table_size;
195 if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
196 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
197 return AVERROR_INVALIDDATA;
199 bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
200 mode = bytestream2_get_le16(&s->gb);
201 table = s->gb.buffer;
202 table_size = bytestream2_get_le16(&s->gb);
203 offset = table_size * 2;
206 if (offset >= bytestream2_get_bytes_left(&s->gb)) {
207 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
208 return AVERROR_INVALIDDATA;
211 bytestream2_skip(&s->gb, offset);
212 memset(s->scratch_buffer, 0, s->buffer_size);
213 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
215 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
221 src = s->scratch_buffer;
222 src_end = src + dec_size;
224 for (j = 0; j < avctx->height >> 1; j++) {
225 for (i = 0; i < avctx->width >> 1; i++) {
226 if (src_end - src < 1)
230 if (val >= table_size)
231 return AVERROR_INVALIDDATA;
232 val = AV_RL16(table + (val << 1));
233 uval = (val >> 3) & 0xF8;
234 vval = (val >> 8) & 0xF8;
235 U[i] = uval | (uval >> 5);
236 V[i] = vval | (vval >> 5);
239 U += s->pic->linesize[1];
240 V += s->pic->linesize[2];
242 if (avctx->height & 1) {
243 memcpy(U, U - s->pic->linesize[1], avctx->width >> 1);
244 memcpy(V, V - s->pic->linesize[2], avctx->width >> 1);
247 uint8_t *U2 = U + s->pic->linesize[1];
248 uint8_t *V2 = V + s->pic->linesize[2];
250 for (j = 0; j < avctx->height >> 2; j++) {
251 for (i = 0; i < avctx->width >> 1; i += 2) {
252 if (src_end - src < 1)
256 if (val >= table_size)
257 return AVERROR_INVALIDDATA;
258 val = AV_RL16(table + (val << 1));
259 uval = (val >> 3) & 0xF8;
260 vval = (val >> 8) & 0xF8;
261 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
262 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
265 U += s->pic->linesize[1] * 2;
266 V += s->pic->linesize[2] * 2;
267 U2 += s->pic->linesize[1] * 2;
268 V2 += s->pic->linesize[2] * 2;
270 if (avctx->height & 3) {
271 int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
273 memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]);
274 memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]);
281 static int xan_decode_frame_type0(AVCodecContext *avctx)
283 XanContext *s = avctx->priv_data;
284 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
285 unsigned chroma_off, corr_off;
290 chroma_off = bytestream2_get_le32(&s->gb);
291 corr_off = bytestream2_get_le32(&s->gb);
293 if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
296 if (corr_off >= bytestream2_size(&s->gb)) {
297 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
300 bytestream2_seek(&s->gb, 12, SEEK_SET);
301 ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
303 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
310 for (j = 1; j < avctx->width - 1; j += 2) {
311 cur = (last + *src++) & 0x1F;
312 ybuf[j] = last + cur;
313 ybuf[j+1] = cur << 1;
318 ybuf += avctx->width;
320 for (i = 1; i < avctx->height; i++) {
321 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
323 for (j = 1; j < avctx->width - 1; j += 2) {
324 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
325 ybuf[j] = last + cur;
326 ybuf[j+1] = cur << 1;
331 ybuf += avctx->width;
337 bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
338 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
342 dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
344 for (i = 0; i < dec_size; i++)
345 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
349 ybuf = s->pic->data[0];
350 for (j = 0; j < avctx->height; j++) {
351 for (i = 0; i < avctx->width; i++)
352 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
354 ybuf += s->pic->linesize[0];
360 static int xan_decode_frame_type1(AVCodecContext *avctx)
362 XanContext *s = avctx->priv_data;
363 uint8_t *ybuf, *src = s->scratch_buffer;
368 if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
371 bytestream2_seek(&s->gb, 16, SEEK_SET);
372 ret = xan_unpack_luma(s, src,
373 s->buffer_size >> 1);
375 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
380 for (i = 0; i < avctx->height; i++) {
381 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
383 for (j = 1; j < avctx->width - 1; j += 2) {
384 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
385 ybuf[j] = (last + cur) >> 1;
390 ybuf += avctx->width;
394 ybuf = s->pic->data[0];
395 for (j = 0; j < avctx->height; j++) {
396 for (i = 0; i < avctx->width; i++)
397 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
399 ybuf += s->pic->linesize[0];
405 static int xan_decode_frame(AVCodecContext *avctx,
406 void *data, int *got_frame,
409 XanContext *s = avctx->priv_data;
413 if ((ret = ff_reget_buffer(avctx, s->pic)) < 0)
416 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
417 ftype = bytestream2_get_le32(&s->gb);
420 ret = xan_decode_frame_type0(avctx);
423 ret = xan_decode_frame_type1(avctx);
426 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
427 return AVERROR_INVALIDDATA;
432 if ((ret = av_frame_ref(data, s->pic)) < 0)
440 AVCodec ff_xan_wc4_decoder = {
442 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
443 .type = AVMEDIA_TYPE_VIDEO,
444 .id = AV_CODEC_ID_XAN_WC4,
445 .priv_data_size = sizeof(XanContext),
446 .init = xan_decode_init,
447 .close = xan_decode_end,
448 .decode = xan_decode_frame,
449 .capabilities = AV_CODEC_CAP_DR1,