libavcodec/xxan.c
Go to the documentation of this file.
00001 /*
00002  * Wing Commander/Xan Video Decoder
00003  * Copyright (C) 2011 Konstantin Shishkov
00004  * based on work by Mike Melanson
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 // for av_memcpy_backptr
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 /* almost the same as in xan_wc3 decoder */
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                 val = *src++;
00201                 if (val && val < table_size) {
00202                     val  = AV_RL16(table + (val << 1));
00203                     uval = (val >> 3) & 0xF8;
00204                     vval = (val >> 8) & 0xF8;
00205                     U[i] = uval | (uval >> 5);
00206                     V[i] = vval | (vval >> 5);
00207                 }
00208                 if (src == src_end)
00209                     return 0;
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                 val = *src++;
00221                 if (val && val < table_size) {
00222                     val  = AV_RL16(table + (val << 1));
00223                     uval = (val >> 3) & 0xF8;
00224                     vval = (val >> 8) & 0xF8;
00225                     U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
00226                     V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
00227                 }
00228             }
00229             U  += s->pic.linesize[1] * 2;
00230             V  += s->pic.linesize[2] * 2;
00231             U2 += s->pic.linesize[1] * 2;
00232             V2 += s->pic.linesize[2] * 2;
00233         }
00234     }
00235 
00236     return 0;
00237 }
00238 
00239 static int xan_decode_frame_type0(AVCodecContext *avctx)
00240 {
00241     XanContext *s = avctx->priv_data;
00242     uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
00243     unsigned  chroma_off, corr_off;
00244     int cur, last;
00245     int i, j;
00246     int ret;
00247 
00248     chroma_off = bytestream2_get_le32(&s->gb);
00249     corr_off   = bytestream2_get_le32(&s->gb);
00250 
00251     if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
00252         return ret;
00253 
00254     if (corr_off >= (s->gb.buffer_end - s->gb.buffer_start)) {
00255         av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
00256         corr_off = 0;
00257     }
00258     bytestream2_seek(&s->gb, 12, SEEK_SET);
00259     ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
00260     if (ret) {
00261         av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
00262         return ret;
00263     }
00264 
00265     ybuf = s->y_buffer;
00266     last = *src++;
00267     ybuf[0] = last << 1;
00268     for (j = 1; j < avctx->width - 1; j += 2) {
00269         cur = (last + *src++) & 0x1F;
00270         ybuf[j]   = last + cur;
00271         ybuf[j+1] = cur << 1;
00272         last = cur;
00273     }
00274     ybuf[j]  = last << 1;
00275     prev_buf = ybuf;
00276     ybuf += avctx->width;
00277 
00278     for (i = 1; i < avctx->height; i++) {
00279         last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
00280         ybuf[0] = last << 1;
00281         for (j = 1; j < avctx->width - 1; j += 2) {
00282             cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
00283             ybuf[j]   = last + cur;
00284             ybuf[j+1] = cur << 1;
00285             last = cur;
00286         }
00287         ybuf[j] = last << 1;
00288         prev_buf = ybuf;
00289         ybuf += avctx->width;
00290     }
00291 
00292     if (corr_off) {
00293         int corr_end, dec_size;
00294 
00295         corr_end = (s->gb.buffer_end - s->gb.buffer_start);
00296         if (chroma_off > corr_off)
00297             corr_end = chroma_off;
00298         bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
00299         dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
00300         if (dec_size < 0)
00301             dec_size = 0;
00302         for (i = 0; i < dec_size; i++)
00303             s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
00304     }
00305 
00306     src  = s->y_buffer;
00307     ybuf = s->pic.data[0];
00308     for (j = 0; j < avctx->height; j++) {
00309         for (i = 0; i < avctx->width; i++)
00310             ybuf[i] = (src[i] << 2) | (src[i] >> 3);
00311         src  += avctx->width;
00312         ybuf += s->pic.linesize[0];
00313     }
00314 
00315     return 0;
00316 }
00317 
00318 static int xan_decode_frame_type1(AVCodecContext *avctx)
00319 {
00320     XanContext *s = avctx->priv_data;
00321     uint8_t *ybuf, *src = s->scratch_buffer;
00322     int cur, last;
00323     int i, j;
00324     int ret;
00325 
00326     if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
00327         return ret;
00328 
00329     bytestream2_seek(&s->gb, 16, SEEK_SET);
00330     ret = xan_unpack_luma(s, src,
00331                           s->buffer_size >> 1);
00332     if (ret) {
00333         av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
00334         return ret;
00335     }
00336 
00337     ybuf = s->y_buffer;
00338     for (i = 0; i < avctx->height; i++) {
00339         last = (ybuf[0] + (*src++ << 1)) & 0x3F;
00340         ybuf[0] = last;
00341         for (j = 1; j < avctx->width - 1; j += 2) {
00342             cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
00343             ybuf[j]   = (last + cur) >> 1;
00344             ybuf[j+1] = cur;
00345             last = cur;
00346         }
00347         ybuf[j] = last;
00348         ybuf += avctx->width;
00349     }
00350 
00351     src = s->y_buffer;
00352     ybuf = s->pic.data[0];
00353     for (j = 0; j < avctx->height; j++) {
00354         for (i = 0; i < avctx->width; i++)
00355             ybuf[i] = (src[i] << 2) | (src[i] >> 3);
00356         src  += avctx->width;
00357         ybuf += s->pic.linesize[0];
00358     }
00359 
00360     return 0;
00361 }
00362 
00363 static int xan_decode_frame(AVCodecContext *avctx,
00364                             void *data, int *data_size,
00365                             AVPacket *avpkt)
00366 {
00367     XanContext *s = avctx->priv_data;
00368     int ftype;
00369     int ret;
00370 
00371     s->pic.reference = 1;
00372     s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
00373                           FF_BUFFER_HINTS_PRESERVE |
00374                           FF_BUFFER_HINTS_REUSABLE;
00375     if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
00376         av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00377         return ret;
00378     }
00379 
00380     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
00381     ftype = bytestream2_get_le32(&s->gb);
00382     switch (ftype) {
00383     case 0:
00384         ret = xan_decode_frame_type0(avctx);
00385         break;
00386     case 1:
00387         ret = xan_decode_frame_type1(avctx);
00388         break;
00389     default:
00390         av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
00391         return -1;
00392     }
00393     if (ret)
00394         return ret;
00395 
00396     *data_size = sizeof(AVFrame);
00397     *(AVFrame*)data = s->pic;
00398 
00399     return avpkt->size;
00400 }
00401 
00402 static av_cold int xan_decode_end(AVCodecContext *avctx)
00403 {
00404     XanContext *s = avctx->priv_data;
00405 
00406     if (s->pic.data[0])
00407         avctx->release_buffer(avctx, &s->pic);
00408 
00409     av_freep(&s->y_buffer);
00410     av_freep(&s->scratch_buffer);
00411 
00412     return 0;
00413 }
00414 
00415 AVCodec ff_xan_wc4_decoder = {
00416     .name           = "xan_wc4",
00417     .type           = AVMEDIA_TYPE_VIDEO,
00418     .id             = CODEC_ID_XAN_WC4,
00419     .priv_data_size = sizeof(XanContext),
00420     .init           = xan_decode_init,
00421     .close          = xan_decode_end,
00422     .decode         = xan_decode_frame,
00423     .capabilities   = CODEC_CAP_DR1,
00424     .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
00425 };
00426