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