00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "avcodec.h"
00024 #include "libavutil/intreadwrite.h"
00025 #include "bytestream.h"
00026 #define BITSTREAM_READER_LE
00027 #include "get_bits.h"
00028
00029 #include "libavutil/lzo.h"
00030
00031 typedef struct XanContext {
00032 AVCodecContext *avctx;
00033 AVFrame pic;
00034
00035 uint8_t *y_buffer;
00036 uint8_t *scratch_buffer;
00037 int buffer_size;
00038 GetByteContext gb;
00039 } XanContext;
00040
00041 static av_cold int xan_decode_init(AVCodecContext *avctx)
00042 {
00043 XanContext *s = avctx->priv_data;
00044
00045 s->avctx = avctx;
00046
00047 avctx->pix_fmt = PIX_FMT_YUV420P;
00048
00049 s->buffer_size = avctx->width * avctx->height;
00050 s->y_buffer = av_malloc(s->buffer_size);
00051 if (!s->y_buffer)
00052 return AVERROR(ENOMEM);
00053 s->scratch_buffer = av_malloc(s->buffer_size + 130);
00054 if (!s->scratch_buffer) {
00055 av_freep(&s->y_buffer);
00056 return AVERROR(ENOMEM);
00057 }
00058
00059 return 0;
00060 }
00061
00062 static int xan_unpack_luma(XanContext *s,
00063 uint8_t *dst, const int dst_size)
00064 {
00065 int tree_size, eof;
00066 int bits, mask;
00067 int tree_root, node;
00068 const uint8_t *dst_end = dst + dst_size;
00069 GetByteContext tree = s->gb;
00070 int start_off = bytestream2_tell(&tree);
00071
00072 tree_size = bytestream2_get_byte(&s->gb);
00073 eof = bytestream2_get_byte(&s->gb);
00074 tree_root = eof + tree_size;
00075 bytestream2_skip(&s->gb, tree_size * 2);
00076
00077 node = tree_root;
00078 bits = bytestream2_get_byte(&s->gb);
00079 mask = 0x80;
00080 for (;;) {
00081 int bit = !!(bits & mask);
00082 mask >>= 1;
00083 bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
00084 node = bytestream2_get_byte(&tree);
00085 if (node == eof)
00086 break;
00087 if (node < eof) {
00088 *dst++ = node;
00089 if (dst > dst_end)
00090 break;
00091 node = tree_root;
00092 }
00093 if (!mask) {
00094 if (bytestream2_get_bytes_left(&s->gb) <= 0)
00095 break;
00096 bits = bytestream2_get_byteu(&s->gb);
00097 mask = 0x80;
00098 }
00099 }
00100 return dst != dst_end ? AVERROR_INVALIDDATA : 0;
00101 }
00102
00103
00104 static int xan_unpack(XanContext *s,
00105 uint8_t *dest, const int dest_len)
00106 {
00107 uint8_t opcode;
00108 int size;
00109 uint8_t *orig_dest = dest;
00110 const uint8_t *dest_end = dest + dest_len;
00111
00112 while (dest < dest_end) {
00113 if (bytestream2_get_bytes_left(&s->gb) <= 0)
00114 return AVERROR_INVALIDDATA;
00115
00116 opcode = bytestream2_get_byteu(&s->gb);
00117
00118 if (opcode < 0xe0) {
00119 int size2, back;
00120 if ((opcode & 0x80) == 0) {
00121 size = opcode & 3;
00122 back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
00123 size2 = ((opcode & 0x1c) >> 2) + 3;
00124 } else if ((opcode & 0x40) == 0) {
00125 size = bytestream2_peek_byte(&s->gb) >> 6;
00126 back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
00127 size2 = (opcode & 0x3f) + 4;
00128 } else {
00129 size = opcode & 3;
00130 back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
00131 size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
00132 if (size + size2 > dest_end - dest)
00133 break;
00134 }
00135 if (dest + size + size2 > dest_end ||
00136 dest - orig_dest + size < back)
00137 return -1;
00138 bytestream2_get_buffer(&s->gb, dest, size);
00139 dest += size;
00140 av_memcpy_backptr(dest, back, size2);
00141 dest += size2;
00142 } else {
00143 int finish = opcode >= 0xfc;
00144
00145 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
00146 if (dest_end - dest < size)
00147 return -1;
00148 bytestream2_get_buffer(&s->gb, dest, size);
00149 dest += size;
00150 if (finish)
00151 break;
00152 }
00153 }
00154 return dest - orig_dest;
00155 }
00156
00157 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
00158 {
00159 XanContext *s = avctx->priv_data;
00160 uint8_t *U, *V;
00161 int val, uval, vval;
00162 int i, j;
00163 const uint8_t *src, *src_end;
00164 const uint8_t *table;
00165 int mode, offset, dec_size, table_size;
00166
00167 if (!chroma_off)
00168 return 0;
00169 if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
00170 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
00171 return -1;
00172 }
00173 bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
00174 mode = bytestream2_get_le16(&s->gb);
00175 table = s->gb.buffer;
00176 table_size = bytestream2_get_le16(&s->gb);
00177 offset = table_size * 2;
00178 table_size += 1;
00179
00180 if (offset >= bytestream2_get_bytes_left(&s->gb)) {
00181 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
00182 return -1;
00183 }
00184
00185 bytestream2_skip(&s->gb, offset);
00186 memset(s->scratch_buffer, 0, s->buffer_size);
00187 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
00188 if (dec_size < 0) {
00189 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
00190 return -1;
00191 }
00192
00193 U = s->pic.data[1];
00194 V = s->pic.data[2];
00195 src = s->scratch_buffer;
00196 src_end = src + dec_size;
00197 if (mode) {
00198 for (j = 0; j < avctx->height >> 1; j++) {
00199 for (i = 0; i < avctx->width >> 1; i++) {
00200 if (src_end - src < 1)
00201 return 0;
00202 val = *src++;
00203 if (val) {
00204 if (val >= table_size)
00205 return AVERROR_INVALIDDATA;
00206 val = AV_RL16(table + (val << 1));
00207 uval = (val >> 3) & 0xF8;
00208 vval = (val >> 8) & 0xF8;
00209 U[i] = uval | (uval >> 5);
00210 V[i] = vval | (vval >> 5);
00211 }
00212 }
00213 U += s->pic.linesize[1];
00214 V += s->pic.linesize[2];
00215 }
00216 } else {
00217 uint8_t *U2 = U + s->pic.linesize[1];
00218 uint8_t *V2 = V + s->pic.linesize[2];
00219
00220 for (j = 0; j < avctx->height >> 2; j++) {
00221 for (i = 0; i < avctx->width >> 1; i += 2) {
00222 if (src_end - src < 1)
00223 return 0;
00224 val = *src++;
00225 if (val) {
00226 if (val >= table_size)
00227 return AVERROR_INVALIDDATA;
00228 val = AV_RL16(table + (val << 1));
00229 uval = (val >> 3) & 0xF8;
00230 vval = (val >> 8) & 0xF8;
00231 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
00232 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
00233 }
00234 }
00235 U += s->pic.linesize[1] * 2;
00236 V += s->pic.linesize[2] * 2;
00237 U2 += s->pic.linesize[1] * 2;
00238 V2 += s->pic.linesize[2] * 2;
00239 }
00240 }
00241
00242 return 0;
00243 }
00244
00245 static int xan_decode_frame_type0(AVCodecContext *avctx)
00246 {
00247 XanContext *s = avctx->priv_data;
00248 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
00249 unsigned chroma_off, corr_off;
00250 int cur, last;
00251 int i, j;
00252 int ret;
00253
00254 chroma_off = bytestream2_get_le32(&s->gb);
00255 corr_off = bytestream2_get_le32(&s->gb);
00256
00257 if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
00258 return ret;
00259
00260 if (corr_off >= bytestream2_size(&s->gb)) {
00261 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
00262 corr_off = 0;
00263 }
00264 bytestream2_seek(&s->gb, 12, SEEK_SET);
00265 ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
00266 if (ret) {
00267 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
00268 return ret;
00269 }
00270
00271 ybuf = s->y_buffer;
00272 last = *src++;
00273 ybuf[0] = last << 1;
00274 for (j = 1; j < avctx->width - 1; j += 2) {
00275 cur = (last + *src++) & 0x1F;
00276 ybuf[j] = last + cur;
00277 ybuf[j+1] = cur << 1;
00278 last = cur;
00279 }
00280 ybuf[j] = last << 1;
00281 prev_buf = ybuf;
00282 ybuf += avctx->width;
00283
00284 for (i = 1; i < avctx->height; i++) {
00285 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
00286 ybuf[0] = last << 1;
00287 for (j = 1; j < avctx->width - 1; j += 2) {
00288 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
00289 ybuf[j] = last + cur;
00290 ybuf[j+1] = cur << 1;
00291 last = cur;
00292 }
00293 ybuf[j] = last << 1;
00294 prev_buf = ybuf;
00295 ybuf += avctx->width;
00296 }
00297
00298 if (corr_off) {
00299 int dec_size;
00300
00301 bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
00302 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
00303 if (dec_size < 0)
00304 dec_size = 0;
00305 else
00306 dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
00307
00308 for (i = 0; i < dec_size; i++)
00309 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
00310 }
00311
00312 src = s->y_buffer;
00313 ybuf = s->pic.data[0];
00314 for (j = 0; j < avctx->height; j++) {
00315 for (i = 0; i < avctx->width; i++)
00316 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
00317 src += avctx->width;
00318 ybuf += s->pic.linesize[0];
00319 }
00320
00321 return 0;
00322 }
00323
00324 static int xan_decode_frame_type1(AVCodecContext *avctx)
00325 {
00326 XanContext *s = avctx->priv_data;
00327 uint8_t *ybuf, *src = s->scratch_buffer;
00328 int cur, last;
00329 int i, j;
00330 int ret;
00331
00332 if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
00333 return ret;
00334
00335 bytestream2_seek(&s->gb, 16, SEEK_SET);
00336 ret = xan_unpack_luma(s, src,
00337 s->buffer_size >> 1);
00338 if (ret) {
00339 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
00340 return ret;
00341 }
00342
00343 ybuf = s->y_buffer;
00344 for (i = 0; i < avctx->height; i++) {
00345 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
00346 ybuf[0] = last;
00347 for (j = 1; j < avctx->width - 1; j += 2) {
00348 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
00349 ybuf[j] = (last + cur) >> 1;
00350 ybuf[j+1] = cur;
00351 last = cur;
00352 }
00353 ybuf[j] = last;
00354 ybuf += avctx->width;
00355 }
00356
00357 src = s->y_buffer;
00358 ybuf = s->pic.data[0];
00359 for (j = 0; j < avctx->height; j++) {
00360 for (i = 0; i < avctx->width; i++)
00361 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
00362 src += avctx->width;
00363 ybuf += s->pic.linesize[0];
00364 }
00365
00366 return 0;
00367 }
00368
00369 static int xan_decode_frame(AVCodecContext *avctx,
00370 void *data, int *data_size,
00371 AVPacket *avpkt)
00372 {
00373 XanContext *s = avctx->priv_data;
00374 int ftype;
00375 int ret;
00376
00377 s->pic.reference = 3;
00378 s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
00379 FF_BUFFER_HINTS_PRESERVE |
00380 FF_BUFFER_HINTS_REUSABLE;
00381 if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
00382 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00383 return ret;
00384 }
00385
00386 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
00387 ftype = bytestream2_get_le32(&s->gb);
00388 switch (ftype) {
00389 case 0:
00390 ret = xan_decode_frame_type0(avctx);
00391 break;
00392 case 1:
00393 ret = xan_decode_frame_type1(avctx);
00394 break;
00395 default:
00396 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
00397 return -1;
00398 }
00399 if (ret)
00400 return ret;
00401
00402 *data_size = sizeof(AVFrame);
00403 *(AVFrame*)data = s->pic;
00404
00405 return avpkt->size;
00406 }
00407
00408 static av_cold int xan_decode_end(AVCodecContext *avctx)
00409 {
00410 XanContext *s = avctx->priv_data;
00411
00412 if (s->pic.data[0])
00413 avctx->release_buffer(avctx, &s->pic);
00414
00415 av_freep(&s->y_buffer);
00416 av_freep(&s->scratch_buffer);
00417
00418 return 0;
00419 }
00420
00421 AVCodec ff_xan_wc4_decoder = {
00422 .name = "xan_wc4",
00423 .type = AVMEDIA_TYPE_VIDEO,
00424 .id = AV_CODEC_ID_XAN_WC4,
00425 .priv_data_size = sizeof(XanContext),
00426 .init = xan_decode_init,
00427 .close = xan_decode_end,
00428 .decode = xan_decode_frame,
00429 .capabilities = CODEC_CAP_DR1,
00430 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
00431 };