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