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 && val < table_size) {
00204 val = AV_RL16(table + (val << 1));
00205 uval = (val >> 3) & 0xF8;
00206 vval = (val >> 8) & 0xF8;
00207 U[i] = uval | (uval >> 5);
00208 V[i] = vval | (vval >> 5);
00209 }
00210 }
00211 U += s->pic.linesize[1];
00212 V += s->pic.linesize[2];
00213 }
00214 } else {
00215 uint8_t *U2 = U + s->pic.linesize[1];
00216 uint8_t *V2 = V + s->pic.linesize[2];
00217
00218 for (j = 0; j < avctx->height >> 2; j++) {
00219 for (i = 0; i < avctx->width >> 1; i += 2) {
00220 if (src_end - src < 1)
00221 return 0;
00222 val = *src++;
00223 if (val && val < table_size) {
00224 val = AV_RL16(table + (val << 1));
00225 uval = (val >> 3) & 0xF8;
00226 vval = (val >> 8) & 0xF8;
00227 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
00228 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
00229 }
00230 }
00231 U += s->pic.linesize[1] * 2;
00232 V += s->pic.linesize[2] * 2;
00233 U2 += s->pic.linesize[1] * 2;
00234 V2 += s->pic.linesize[2] * 2;
00235 }
00236 }
00237
00238 return 0;
00239 }
00240
00241 static int xan_decode_frame_type0(AVCodecContext *avctx)
00242 {
00243 XanContext *s = avctx->priv_data;
00244 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
00245 unsigned chroma_off, corr_off;
00246 int cur, last;
00247 int i, j;
00248 int ret;
00249
00250 chroma_off = bytestream2_get_le32(&s->gb);
00251 corr_off = bytestream2_get_le32(&s->gb);
00252
00253 if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
00254 return ret;
00255
00256 if (corr_off >= (s->gb.buffer_end - s->gb.buffer_start)) {
00257 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
00258 corr_off = 0;
00259 }
00260 bytestream2_seek(&s->gb, 12, SEEK_SET);
00261 ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
00262 if (ret) {
00263 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
00264 return ret;
00265 }
00266
00267 ybuf = s->y_buffer;
00268 last = *src++;
00269 ybuf[0] = last << 1;
00270 for (j = 1; j < avctx->width - 1; j += 2) {
00271 cur = (last + *src++) & 0x1F;
00272 ybuf[j] = last + cur;
00273 ybuf[j+1] = cur << 1;
00274 last = cur;
00275 }
00276 ybuf[j] = last << 1;
00277 prev_buf = ybuf;
00278 ybuf += avctx->width;
00279
00280 for (i = 1; i < avctx->height; i++) {
00281 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
00282 ybuf[0] = last << 1;
00283 for (j = 1; j < avctx->width - 1; j += 2) {
00284 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
00285 ybuf[j] = last + cur;
00286 ybuf[j+1] = cur << 1;
00287 last = cur;
00288 }
00289 ybuf[j] = last << 1;
00290 prev_buf = ybuf;
00291 ybuf += avctx->width;
00292 }
00293
00294 if (corr_off) {
00295 int corr_end, dec_size;
00296
00297 corr_end = (s->gb.buffer_end - s->gb.buffer_start);
00298 if (chroma_off > corr_off)
00299 corr_end = chroma_off;
00300 bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
00301 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
00302 if (dec_size < 0)
00303 dec_size = 0;
00304 else
00305 dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
00306
00307 for (i = 0; i < dec_size; i++)
00308 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
00309 }
00310
00311 src = s->y_buffer;
00312 ybuf = s->pic.data[0];
00313 for (j = 0; j < avctx->height; j++) {
00314 for (i = 0; i < avctx->width; i++)
00315 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
00316 src += avctx->width;
00317 ybuf += s->pic.linesize[0];
00318 }
00319
00320 return 0;
00321 }
00322
00323 static int xan_decode_frame_type1(AVCodecContext *avctx)
00324 {
00325 XanContext *s = avctx->priv_data;
00326 uint8_t *ybuf, *src = s->scratch_buffer;
00327 int cur, last;
00328 int i, j;
00329 int ret;
00330
00331 if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
00332 return ret;
00333
00334 bytestream2_seek(&s->gb, 16, SEEK_SET);
00335 ret = xan_unpack_luma(s, src,
00336 s->buffer_size >> 1);
00337 if (ret) {
00338 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
00339 return ret;
00340 }
00341
00342 ybuf = s->y_buffer;
00343 for (i = 0; i < avctx->height; i++) {
00344 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
00345 ybuf[0] = last;
00346 for (j = 1; j < avctx->width - 1; j += 2) {
00347 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
00348 ybuf[j] = (last + cur) >> 1;
00349 ybuf[j+1] = cur;
00350 last = cur;
00351 }
00352 ybuf[j] = last;
00353 ybuf += avctx->width;
00354 }
00355
00356 src = s->y_buffer;
00357 ybuf = s->pic.data[0];
00358 for (j = 0; j < avctx->height; j++) {
00359 for (i = 0; i < avctx->width; i++)
00360 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
00361 src += avctx->width;
00362 ybuf += s->pic.linesize[0];
00363 }
00364
00365 return 0;
00366 }
00367
00368 static int xan_decode_frame(AVCodecContext *avctx,
00369 void *data, int *data_size,
00370 AVPacket *avpkt)
00371 {
00372 XanContext *s = avctx->priv_data;
00373 int ftype;
00374 int ret;
00375
00376 s->pic.reference = 3;
00377 s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
00378 FF_BUFFER_HINTS_PRESERVE |
00379 FF_BUFFER_HINTS_REUSABLE;
00380 if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
00381 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00382 return ret;
00383 }
00384
00385 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
00386 ftype = bytestream2_get_le32(&s->gb);
00387 switch (ftype) {
00388 case 0:
00389 ret = xan_decode_frame_type0(avctx);
00390 break;
00391 case 1:
00392 ret = xan_decode_frame_type1(avctx);
00393 break;
00394 default:
00395 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
00396 return -1;
00397 }
00398 if (ret)
00399 return ret;
00400
00401 *data_size = sizeof(AVFrame);
00402 *(AVFrame*)data = s->pic;
00403
00404 return avpkt->size;
00405 }
00406
00407 static av_cold int xan_decode_end(AVCodecContext *avctx)
00408 {
00409 XanContext *s = avctx->priv_data;
00410
00411 if (s->pic.data[0])
00412 avctx->release_buffer(avctx, &s->pic);
00413
00414 av_freep(&s->y_buffer);
00415 av_freep(&s->scratch_buffer);
00416
00417 return 0;
00418 }
00419
00420 AVCodec ff_xan_wc4_decoder = {
00421 .name = "xan_wc4",
00422 .type = AVMEDIA_TYPE_VIDEO,
00423 .id = CODEC_ID_XAN_WC4,
00424 .priv_data_size = sizeof(XanContext),
00425 .init = xan_decode_init,
00426 .close = xan_decode_end,
00427 .decode = xan_decode_frame,
00428 .capabilities = CODEC_CAP_DR1,
00429 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
00430 };
00431