• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 ALT_BITSTREAM_READER_LE
00033 #include "bitstream.h"
00034 #include "libavutil/lzo.h"
00035 
00036 #define EA_PREAMBLE_SIZE    8
00037 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
00038 
00039 typedef struct TgvContext {
00040     AVCodecContext *avctx;
00041     AVFrame frame;
00042     AVFrame last_frame;
00043     int width,height;
00044     unsigned int palette[AVPALETTE_COUNT];
00045 
00046     int (*mv_codebook)[2];
00047     unsigned char (*block_codebook)[16];
00048     int num_mvs;           
00049     int num_blocks_packed; 
00050 } TgvContext;
00051 
00052 static av_cold int tgv_decode_init(AVCodecContext *avctx){
00053     TgvContext *s = avctx->priv_data;
00054     s->avctx = avctx;
00055     avctx->time_base = (AVRational){1, 15};
00056     avctx->pix_fmt = PIX_FMT_PAL8;
00057     return 0;
00058 }
00059 
00064 static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) {
00065     unsigned char *dst_end = dst + width*height;
00066     int size, size1, size2, av_uninit(offset), run;
00067     unsigned char *dst_start = dst;
00068 
00069     if (src[0] & 0x01)
00070         src += 5;
00071     else
00072         src += 2;
00073 
00074     if (src+3>src_end)
00075         return -1;
00076     size = AV_RB24(src);
00077     src += 3;
00078 
00079     while(size>0 && src<src_end) {
00080 
00081         /* determine size1 and size2 */
00082         size1 = (src[0] & 3);
00083         if ( src[0] & 0x80 ) {  // 1
00084             if (src[0] & 0x40 ) {  // 11
00085                 if ( src[0] & 0x20 ) {  // 111
00086                     if ( src[0] < 0xFC )  // !(111111)
00087                         size1 = (((src[0] & 31) + 1) << 2);
00088                     src++;
00089                     size2 = 0;
00090                 } else {  // 110
00091                     offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
00092                     size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
00093                     src += 4;
00094                 }
00095             } else {  // 10
00096                 size1 = ( ( src[1] & 0xC0) >> 6 );
00097                 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
00098                 size2 = (src[0] & 0x3F) + 4;
00099                 src += 3;
00100             }
00101         } else {  // 0
00102             offset = ((src[0] & 0x60) << 3) + src[1] + 1;
00103             size2 = ((src[0] & 0x1C) >> 2) + 3;
00104             src += 2;
00105         }
00106 
00107 
00108         /* fetch strip from src */
00109         if (size1>src_end-src)
00110             break;
00111 
00112         if (size1>0) {
00113             size -= size1;
00114             run = FFMIN(size1, dst_end-dst);
00115             memcpy(dst, src, run);
00116             dst += run;
00117             src += run;
00118         }
00119 
00120         if (size2>0) {
00121             if (dst-dst_start<offset)
00122                 return 0;
00123             size -= size2;
00124             run = FFMIN(size2, dst_end-dst);
00125             av_memcpy_backptr(dst, offset, run);
00126             dst += run;
00127         }
00128     }
00129 
00130     return 0;
00131 }
00132 
00137 static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){
00138     unsigned char *frame0_end = s->last_frame.data[0] + s->avctx->width*s->last_frame.linesize[0];
00139     int num_mvs;
00140     int num_blocks_raw;
00141     int num_blocks_packed;
00142     int vector_bits;
00143     int i,j,x,y;
00144     GetBitContext gb;
00145     int mvbits;
00146     const unsigned char *blocks_raw;
00147 
00148     if(buf+12>buf_end)
00149         return -1;
00150 
00151     num_mvs           = AV_RL16(&buf[0]);
00152     num_blocks_raw    = AV_RL16(&buf[2]);
00153     num_blocks_packed = AV_RL16(&buf[4]);
00154     vector_bits       = AV_RL16(&buf[6]);
00155     buf += 12;
00156 
00157     /* allocate codebook buffers as neccessary */
00158     if (num_mvs > s->num_mvs) {
00159         s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
00160         s->num_mvs = num_mvs;
00161     }
00162 
00163     if (num_blocks_packed > s->num_blocks_packed) {
00164         s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
00165         s->num_blocks_packed = num_blocks_packed;
00166     }
00167 
00168     /* read motion vectors */
00169     mvbits = (num_mvs*2*10+31) & ~31;
00170 
00171     if (buf+(mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed>buf_end)
00172         return -1;
00173 
00174     init_get_bits(&gb, buf, mvbits);
00175     for (i=0; i<num_mvs; i++) {
00176         s->mv_codebook[i][0] = get_sbits(&gb, 10);
00177         s->mv_codebook[i][1] = get_sbits(&gb, 10);
00178     }
00179     buf += mvbits>>3;
00180 
00181     /* note ptr to uncompressed blocks */
00182     blocks_raw = buf;
00183     buf += num_blocks_raw*16;
00184 
00185     /* read compressed blocks */
00186     init_get_bits(&gb, buf, (buf_end-buf)<<3);
00187     for (i=0; i<num_blocks_packed; i++) {
00188         int tmp[4];
00189         for(j=0; j<4; j++)
00190             tmp[j] = get_bits(&gb, 8);
00191         for(j=0; j<16; j++)
00192             s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
00193     }
00194 
00195     /* read vectors and build frame */
00196     for(y=0; y<s->avctx->height/4; y++)
00197     for(x=0; x<s->avctx->width/4; x++) {
00198         unsigned int vector = get_bits(&gb, vector_bits);
00199         const unsigned char *src;
00200         int src_stride;
00201 
00202         if (vector < num_mvs) {
00203             src = s->last_frame.data[0] +
00204                   (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
00205                    x*4 + s->mv_codebook[vector][0];
00206             src_stride = s->last_frame.linesize[0];
00207             if (src+3*src_stride+3>=frame0_end)
00208                 continue;
00209         }else{
00210             int offset = vector - num_mvs;
00211             if (offset<num_blocks_raw)
00212                 src = blocks_raw + 16*offset;
00213             else if (offset-num_blocks_raw<num_blocks_packed)
00214                 src = s->block_codebook[offset-num_blocks_raw];
00215             else
00216                 continue;
00217             src_stride = 4;
00218         }
00219 
00220         for(j=0; j<4; j++)
00221         for(i=0; i<4; i++)
00222             s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i)  ] =
00223                src[j*src_stride + i];
00224     }
00225 
00226     return 0;
00227 }
00228 
00230 static void cond_release_buffer(AVFrame *pic)
00231 {
00232     if (pic->data[0]) {
00233         av_freep(&pic->data[0]);
00234         av_free(pic->data[1]);
00235     }
00236 }
00237 
00238 static int tgv_decode_frame(AVCodecContext *avctx,
00239                             void *data, int *data_size,
00240                             const uint8_t *buf, int buf_size)
00241 {
00242     TgvContext *s = avctx->priv_data;
00243     const uint8_t *buf_end = buf + buf_size;
00244     int chunk_type;
00245 
00246     chunk_type = AV_RL32(&buf[0]);
00247     buf += EA_PREAMBLE_SIZE;
00248 
00249     if (chunk_type==kVGT_TAG) {
00250         int pal_count, i;
00251         if(buf+12>buf_end) {
00252             av_log(avctx, AV_LOG_WARNING, "truncated header\n");
00253             return -1;
00254         }
00255 
00256         s->width  = AV_RL16(&buf[0]);
00257         s->height = AV_RL16(&buf[2]);
00258         if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
00259             avcodec_set_dimensions(s->avctx, s->width, s->height);
00260             cond_release_buffer(&s->frame);
00261             cond_release_buffer(&s->last_frame);
00262         }
00263 
00264         pal_count = AV_RL16(&buf[6]);
00265         buf += 12;
00266         for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) {
00267             s->palette[i] = AV_RB24(buf);
00268             buf += 3;
00269         }
00270     }
00271 
00272     if (avcodec_check_dimensions(avctx, s->width, s->height))
00273         return -1;
00274 
00275     /* shuffle */
00276     FFSWAP(AVFrame, s->frame, s->last_frame);
00277     if (!s->frame.data[0]) {
00278         s->frame.reference = 1;
00279         s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
00280         s->frame.linesize[0] = s->width;
00281 
00282         /* allocate additional 12 bytes to accomodate av_memcpy_backptr() OUTBUF_PADDED optimisation */
00283         s->frame.data[0] = av_malloc(s->width*s->height + 12);
00284         if (!s->frame.data[0])
00285             return AVERROR_NOMEM;
00286         s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
00287         if (!s->frame.data[1]) {
00288             av_freep(&s->frame.data[0]);
00289             return AVERROR_NOMEM;
00290         }
00291     }
00292     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00293 
00294     if(chunk_type==kVGT_TAG) {
00295         s->frame.key_frame = 1;
00296         s->frame.pict_type = FF_I_TYPE;
00297         if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
00298             av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
00299             return -1;
00300         }
00301     }else{
00302         if (!s->last_frame.data[0]) {
00303             av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
00304             return buf_size;
00305         }
00306         s->frame.key_frame = 0;
00307         s->frame.pict_type = FF_P_TYPE;
00308         if (tgv_decode_inter(s, buf, buf_end)<0) {
00309             av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
00310             return -1;
00311         }
00312     }
00313 
00314     *data_size = sizeof(AVFrame);
00315     *(AVFrame*)data = s->frame;
00316 
00317     return buf_size;
00318 }
00319 
00320 static av_cold int tgv_decode_end(AVCodecContext *avctx)
00321 {
00322     TgvContext *s = avctx->priv_data;
00323     cond_release_buffer(&s->frame);
00324     cond_release_buffer(&s->last_frame);
00325     av_free(s->mv_codebook);
00326     av_free(s->block_codebook);
00327     return 0;
00328 }
00329 
00330 AVCodec eatgv_decoder = {
00331     "eatgv",
00332     CODEC_TYPE_VIDEO,
00333     CODEC_ID_TGV,
00334     sizeof(TgvContext),
00335     tgv_decode_init,
00336     NULL,
00337     tgv_decode_end,
00338     tgv_decode_frame,
00339     .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
00340 };

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1