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     if (avctx->width & 1) {
00050         av_log(avctx, AV_LOG_ERROR, "Invalid frame width: %d.\n", avctx->width);
00051         return AVERROR(EINVAL);
00052     }
00053 
00054     s->buffer_size = avctx->width * avctx->height;
00055     s->y_buffer = av_malloc(s->buffer_size);
00056     if (!s->y_buffer)
00057         return AVERROR(ENOMEM);
00058     s->scratch_buffer = av_malloc(s->buffer_size + 130);
00059     if (!s->scratch_buffer) {
00060         av_freep(&s->y_buffer);
00061         return AVERROR(ENOMEM);
00062     }
00063 
00064     return 0;
00065 }
00066 
00067 static int xan_unpack_luma(XanContext *s,
00068                            uint8_t *dst, const int dst_size)
00069 {
00070    int tree_size, eof;
00071    int bits, mask;
00072    int tree_root, node;
00073    const uint8_t *dst_end = dst + dst_size;
00074    GetByteContext tree = s->gb;
00075    int start_off = bytestream2_tell(&tree);
00076 
00077    tree_size = bytestream2_get_byte(&s->gb);
00078    eof       = bytestream2_get_byte(&s->gb);
00079    tree_root = eof + tree_size;
00080    bytestream2_skip(&s->gb, tree_size * 2);
00081 
00082    node = tree_root;
00083    bits = bytestream2_get_byte(&s->gb);
00084    mask = 0x80;
00085    for (;;) {
00086        int bit = !!(bits & mask);
00087        mask >>= 1;
00088        bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
00089        node = bytestream2_get_byte(&tree);
00090        if (node == eof)
00091            break;
00092        if (node < eof) {
00093            *dst++ = node;
00094            if (dst > dst_end)
00095                break;
00096            node = tree_root;
00097        }
00098        if (!mask) {
00099            if (bytestream2_get_bytes_left(&s->gb) <= 0)
00100                break;
00101            bits = bytestream2_get_byteu(&s->gb);
00102            mask = 0x80;
00103        }
00104    }
00105    return dst != dst_end ? AVERROR_INVALIDDATA : 0;
00106 }
00107 
00108 /* almost the same as in xan_wc3 decoder */
00109 static int xan_unpack(XanContext *s,
00110                       uint8_t *dest, const int dest_len)
00111 {
00112     uint8_t opcode;
00113     int size;
00114     uint8_t *orig_dest = dest;
00115     const uint8_t *dest_end = dest + dest_len;
00116 
00117     while (dest < dest_end) {
00118         if (bytestream2_get_bytes_left(&s->gb) <= 0)
00119             return AVERROR_INVALIDDATA;
00120 
00121         opcode = bytestream2_get_byteu(&s->gb);
00122 
00123         if (opcode < 0xe0) {
00124             int size2, back;
00125             if ((opcode & 0x80) == 0) {
00126                 size  = opcode & 3;
00127                 back  = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
00128                 size2 = ((opcode & 0x1c) >> 2) + 3;
00129             } else if ((opcode & 0x40) == 0) {
00130                 size  = bytestream2_peek_byte(&s->gb) >> 6;
00131                 back  = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
00132                 size2 = (opcode & 0x3f) + 4;
00133             } else {
00134                 size  = opcode & 3;
00135                 back  = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
00136                 size2 = ((opcode & 0x0c) <<  6) + bytestream2_get_byte(&s->gb) + 5;
00137                 if (size + size2 > dest_end - dest)
00138                     break;
00139             }
00140             if (dest + size + size2 > dest_end ||
00141                 dest - orig_dest + size < back)
00142                 return -1;
00143             bytestream2_get_buffer(&s->gb, dest, size);
00144             dest += size;
00145             av_memcpy_backptr(dest, back, size2);
00146             dest += size2;
00147         } else {
00148             int finish = opcode >= 0xfc;
00149 
00150             size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
00151             if (dest_end - dest < size)
00152                 return -1;
00153             bytestream2_get_buffer(&s->gb, dest, size);
00154             dest += size;
00155             if (finish)
00156                 break;
00157         }
00158     }
00159     return dest - orig_dest;
00160 }
00161 
00162 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
00163 {
00164     XanContext *s = avctx->priv_data;
00165     uint8_t *U, *V;
00166     int val, uval, vval;
00167     int i, j;
00168     const uint8_t *src, *src_end;
00169     const uint8_t *table;
00170     int mode, offset, dec_size, table_size;
00171 
00172     if (!chroma_off)
00173         return 0;
00174     if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
00175         av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
00176         return -1;
00177     }
00178     bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
00179     mode        = bytestream2_get_le16(&s->gb);
00180     table       = s->gb.buffer;
00181     table_size  = bytestream2_get_le16(&s->gb);
00182     offset      = table_size * 2;
00183     table_size += 1;
00184 
00185     if (offset >= bytestream2_get_bytes_left(&s->gb)) {
00186         av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
00187         return -1;
00188     }
00189 
00190     bytestream2_skip(&s->gb, offset);
00191     memset(s->scratch_buffer, 0, s->buffer_size);
00192     dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
00193     if (dec_size < 0) {
00194         av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
00195         return -1;
00196     }
00197 
00198     U = s->pic.data[1];
00199     V = s->pic.data[2];
00200     src     = s->scratch_buffer;
00201     src_end = src + dec_size;
00202     if (mode) {
00203         for (j = 0; j < avctx->height >> 1; j++) {
00204             for (i = 0; i < avctx->width >> 1; i++) {
00205                 val = *src++;
00206                 if (val && val < table_size) {
00207                     val  = AV_RL16(table + (val << 1));
00208                     uval = (val >> 3) & 0xF8;
00209                     vval = (val >> 8) & 0xF8;
00210                     U[i] = uval | (uval >> 5);
00211                     V[i] = vval | (vval >> 5);
00212                 }
00213                 if (src == src_end)
00214                     return 0;
00215             }
00216             U += s->pic.linesize[1];
00217             V += s->pic.linesize[2];
00218         }
00219     } else {
00220         uint8_t *U2 = U + s->pic.linesize[1];
00221         uint8_t *V2 = V + s->pic.linesize[2];
00222 
00223         for (j = 0; j < avctx->height >> 2; j++) {
00224             for (i = 0; i < avctx->width >> 1; i += 2) {
00225                 val = *src++;
00226                 if (val && val < table_size) {
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 >= (s->gb.buffer_end - s->gb.buffer_start)) {
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     ybuf[j]  = last << 1;
00280     prev_buf = ybuf;
00281     ybuf += avctx->width;
00282 
00283     for (i = 1; i < avctx->height; i++) {
00284         last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
00285         ybuf[0] = last << 1;
00286         for (j = 1; j < avctx->width - 1; j += 2) {
00287             cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
00288             ybuf[j]   = last + cur;
00289             ybuf[j+1] = cur << 1;
00290             last = cur;
00291         }
00292         ybuf[j] = last << 1;
00293         prev_buf = ybuf;
00294         ybuf += avctx->width;
00295     }
00296 
00297     if (corr_off) {
00298         int corr_end, dec_size;
00299 
00300         corr_end = (s->gb.buffer_end - s->gb.buffer_start);
00301         if (chroma_off > corr_off)
00302             corr_end = chroma_off;
00303         bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
00304         dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
00305         if (dec_size < 0)
00306             dec_size = 0;
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 = 1;
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