libavcodec/eatgv.c
Go to the documentation of this file.
00001 /*
00002  * Electronic Arts TGV Video Decoder
00003  * Copyright (c) 2007-2008 Peter Ross
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00031 #include "avcodec.h"
00032 #define BITSTREAM_READER_LE
00033 #include "get_bits.h"
00034 #include "libavutil/lzo.h"
00035 #include "libavutil/imgutils.h"
00036 
00037 #define EA_PREAMBLE_SIZE    8
00038 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
00039 
00040 typedef struct TgvContext {
00041     AVCodecContext *avctx;
00042     AVFrame frame;
00043     AVFrame last_frame;
00044     int width,height;
00045     unsigned int palette[AVPALETTE_COUNT];
00046 
00047     int (*mv_codebook)[2];
00048     unsigned char (*block_codebook)[16];
00049     int num_mvs;           
00050     int num_blocks_packed; 
00051 } TgvContext;
00052 
00053 static av_cold int tgv_decode_init(AVCodecContext *avctx){
00054     TgvContext *s = avctx->priv_data;
00055     s->avctx = avctx;
00056     avctx->time_base = (AVRational){1, 15};
00057     avctx->pix_fmt = PIX_FMT_PAL8;
00058     avcodec_get_frame_defaults(&s->frame);
00059     avcodec_get_frame_defaults(&s->last_frame);
00060     return 0;
00061 }
00062 
00067 static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) {
00068     unsigned char *dst_end = dst + width*height;
00069     int size, size1, size2, av_uninit(offset), run;
00070     unsigned char *dst_start = dst;
00071 
00072     if (src[0] & 0x01)
00073         src += 5;
00074     else
00075         src += 2;
00076 
00077     if (src_end - src < 3)
00078         return -1;
00079     size = AV_RB24(src);
00080     src += 3;
00081 
00082     while(size>0 && src<src_end) {
00083 
00084         /* determine size1 and size2 */
00085         size1 = (src[0] & 3);
00086         if ( src[0] & 0x80 ) {  // 1
00087             if (src[0] & 0x40 ) {  // 11
00088                 if ( src[0] & 0x20 ) {  // 111
00089                     if ( src[0] < 0xFC )  // !(111111)
00090                         size1 = (((src[0] & 31) + 1) << 2);
00091                     src++;
00092                     size2 = 0;
00093                 } else {  // 110
00094                     offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
00095                     size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
00096                     src += 4;
00097                 }
00098             } else {  // 10
00099                 size1 = ( ( src[1] & 0xC0) >> 6 );
00100                 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
00101                 size2 = (src[0] & 0x3F) + 4;
00102                 src += 3;
00103             }
00104         } else {  // 0
00105             offset = ((src[0] & 0x60) << 3) + src[1] + 1;
00106             size2 = ((src[0] & 0x1C) >> 2) + 3;
00107             src += 2;
00108         }
00109 
00110 
00111         /* fetch strip from src */
00112         if (size1>src_end-src)
00113             break;
00114 
00115         if (size1>0) {
00116             size -= size1;
00117             run = FFMIN(size1, dst_end-dst);
00118             memcpy(dst, src, run);
00119             dst += run;
00120             src += run;
00121         }
00122 
00123         if (size2>0) {
00124             if (dst-dst_start<offset)
00125                 return 0;
00126             size -= size2;
00127             run = FFMIN(size2, dst_end-dst);
00128             av_memcpy_backptr(dst, offset, run);
00129             dst += run;
00130         }
00131     }
00132 
00133     return 0;
00134 }
00135 
00140 static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){
00141     unsigned last_frame_size = s->avctx->height*s->last_frame.linesize[0];
00142     int num_mvs;
00143     int num_blocks_raw;
00144     int num_blocks_packed;
00145     int vector_bits;
00146     int i,j,x,y;
00147     GetBitContext gb;
00148     int mvbits;
00149     const unsigned char *blocks_raw;
00150 
00151     if(buf_end - buf < 12)
00152         return -1;
00153 
00154     num_mvs           = AV_RL16(&buf[0]);
00155     num_blocks_raw    = AV_RL16(&buf[2]);
00156     num_blocks_packed = AV_RL16(&buf[4]);
00157     vector_bits       = AV_RL16(&buf[6]);
00158     buf += 12;
00159 
00160     /* allocate codebook buffers as necessary */
00161     if (num_mvs > s->num_mvs) {
00162         s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
00163         s->num_mvs = num_mvs;
00164     }
00165 
00166     if (num_blocks_packed > s->num_blocks_packed) {
00167         s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
00168         s->num_blocks_packed = num_blocks_packed;
00169     }
00170 
00171     /* read motion vectors */
00172     mvbits = (num_mvs*2*10+31) & ~31;
00173 
00174     if (buf_end - buf < (mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed)
00175         return -1;
00176 
00177     init_get_bits(&gb, buf, mvbits);
00178     for (i=0; i<num_mvs; i++) {
00179         s->mv_codebook[i][0] = get_sbits(&gb, 10);
00180         s->mv_codebook[i][1] = get_sbits(&gb, 10);
00181     }
00182     buf += mvbits>>3;
00183 
00184     /* note ptr to uncompressed blocks */
00185     blocks_raw = buf;
00186     buf += num_blocks_raw*16;
00187 
00188     /* read compressed blocks */
00189     init_get_bits(&gb, buf, (buf_end-buf)<<3);
00190     for (i=0; i<num_blocks_packed; i++) {
00191         int tmp[4];
00192         for(j=0; j<4; j++)
00193             tmp[j] = get_bits(&gb, 8);
00194         for(j=0; j<16; j++)
00195             s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
00196     }
00197 
00198     if (get_bits_left(&gb) < vector_bits *
00199         (s->avctx->height/4) * (s->avctx->width/4))
00200         return -1;
00201 
00202     /* read vectors and build frame */
00203     for(y=0; y<s->avctx->height/4; y++)
00204     for(x=0; x<s->avctx->width/4; x++) {
00205         unsigned int vector = get_bits(&gb, vector_bits);
00206         const unsigned char *src;
00207         int src_stride;
00208 
00209         if (vector < num_mvs) {
00210             unsigned offset =
00211                 (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
00212                  x*4 + s->mv_codebook[vector][0];
00213 
00214             src_stride = s->last_frame.linesize[0];
00215             if (offset >= last_frame_size - (3*src_stride+3))
00216                 continue;
00217             src = s->last_frame.data[0] + offset;
00218         }else{
00219             int offset = vector - num_mvs;
00220             if (offset<num_blocks_raw)
00221                 src = blocks_raw + 16*offset;
00222             else if (offset-num_blocks_raw<num_blocks_packed)
00223                 src = s->block_codebook[offset-num_blocks_raw];
00224             else
00225                 continue;
00226             src_stride = 4;
00227         }
00228 
00229         for(j=0; j<4; j++)
00230         for(i=0; i<4; i++)
00231             s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i)  ] =
00232                src[j*src_stride + i];
00233     }
00234 
00235     return 0;
00236 }
00237 
00239 static void cond_release_buffer(AVFrame *pic)
00240 {
00241     if (pic->data[0]) {
00242         av_freep(&pic->data[0]);
00243         av_free(pic->data[1]);
00244     }
00245 }
00246 
00247 static int tgv_decode_frame(AVCodecContext *avctx,
00248                             void *data, int *data_size,
00249                             AVPacket *avpkt)
00250 {
00251     const uint8_t *buf = avpkt->data;
00252     int buf_size = avpkt->size;
00253     TgvContext *s = avctx->priv_data;
00254     const uint8_t *buf_end = buf + buf_size;
00255     int chunk_type;
00256 
00257     if (buf_end - buf < EA_PREAMBLE_SIZE)
00258         return AVERROR_INVALIDDATA;
00259 
00260     chunk_type = AV_RL32(&buf[0]);
00261     buf += EA_PREAMBLE_SIZE;
00262 
00263     if (chunk_type==kVGT_TAG) {
00264         int pal_count, i;
00265         if(buf_end - buf < 12) {
00266             av_log(avctx, AV_LOG_WARNING, "truncated header\n");
00267             return -1;
00268         }
00269 
00270         s->width  = AV_RL16(&buf[0]);
00271         s->height = AV_RL16(&buf[2]);
00272         if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
00273             avcodec_set_dimensions(s->avctx, s->width, s->height);
00274             cond_release_buffer(&s->frame);
00275             cond_release_buffer(&s->last_frame);
00276         }
00277 
00278         pal_count = AV_RL16(&buf[6]);
00279         buf += 12;
00280         for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
00281             s->palette[i] = 0xFF << 24 | AV_RB24(buf);
00282             buf += 3;
00283         }
00284     }
00285 
00286     if (av_image_check_size(s->width, s->height, 0, avctx))
00287         return -1;
00288 
00289     /* shuffle */
00290     FFSWAP(AVFrame, s->frame, s->last_frame);
00291     if (!s->frame.data[0]) {
00292         s->frame.reference = 3;
00293         s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
00294         s->frame.linesize[0] = s->width;
00295 
00296         /* allocate additional 12 bytes to accommodate av_memcpy_backptr() OUTBUF_PADDED optimisation */
00297         s->frame.data[0] = av_malloc(s->width*s->height + 12);
00298         if (!s->frame.data[0])
00299             return AVERROR(ENOMEM);
00300         s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
00301         if (!s->frame.data[1]) {
00302             av_freep(&s->frame.data[0]);
00303             return AVERROR(ENOMEM);
00304         }
00305     }
00306     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00307 
00308     if(chunk_type==kVGT_TAG) {
00309         s->frame.key_frame = 1;
00310         s->frame.pict_type = AV_PICTURE_TYPE_I;
00311         if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
00312             av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
00313             return -1;
00314         }
00315     }else{
00316         if (!s->last_frame.data[0]) {
00317             av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
00318             return buf_size;
00319         }
00320         s->frame.key_frame = 0;
00321         s->frame.pict_type = AV_PICTURE_TYPE_P;
00322         if (tgv_decode_inter(s, buf, buf_end)<0) {
00323             av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
00324             return -1;
00325         }
00326     }
00327 
00328     *data_size = sizeof(AVFrame);
00329     *(AVFrame*)data = s->frame;
00330 
00331     return buf_size;
00332 }
00333 
00334 static av_cold int tgv_decode_end(AVCodecContext *avctx)
00335 {
00336     TgvContext *s = avctx->priv_data;
00337     cond_release_buffer(&s->frame);
00338     cond_release_buffer(&s->last_frame);
00339     av_free(s->mv_codebook);
00340     av_free(s->block_codebook);
00341     return 0;
00342 }
00343 
00344 AVCodec ff_eatgv_decoder = {
00345     .name           = "eatgv",
00346     .type           = AVMEDIA_TYPE_VIDEO,
00347     .id             = CODEC_ID_TGV,
00348     .priv_data_size = sizeof(TgvContext),
00349     .init           = tgv_decode_init,
00350     .close          = tgv_decode_end,
00351     .decode         = tgv_decode_frame,
00352     .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
00353 };