00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include "avcodec.h"
00032 #define ALT_BITSTREAM_READER_LE
00033 #include "get_bits.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
00082 size1 = (src[0] & 3);
00083 if ( src[0] & 0x80 ) {
00084 if (src[0] & 0x40 ) {
00085 if ( src[0] & 0x20 ) {
00086 if ( src[0] < 0xFC )
00087 size1 = (((src[0] & 31) + 1) << 2);
00088 src++;
00089 size2 = 0;
00090 } else {
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 {
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 {
00102 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
00103 size2 = ((src[0] & 0x1C) >> 2) + 3;
00104 src += 2;
00105 }
00106
00107
00108
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
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
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
00182 blocks_raw = buf;
00183 buf += num_blocks_raw*16;
00184
00185
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 if (get_bits_left(&gb) < vector_bits *
00196 (s->avctx->height/4) * (s->avctx->width/4))
00197 return -1;
00198
00199
00200 for(y=0; y<s->avctx->height/4; y++)
00201 for(x=0; x<s->avctx->width/4; x++) {
00202 unsigned int vector = get_bits(&gb, vector_bits);
00203 const unsigned char *src;
00204 int src_stride;
00205
00206 if (vector < num_mvs) {
00207 src = s->last_frame.data[0] +
00208 (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
00209 x*4 + s->mv_codebook[vector][0];
00210 src_stride = s->last_frame.linesize[0];
00211 if (src+3*src_stride+3>=frame0_end)
00212 continue;
00213 }else{
00214 int offset = vector - num_mvs;
00215 if (offset<num_blocks_raw)
00216 src = blocks_raw + 16*offset;
00217 else if (offset-num_blocks_raw<num_blocks_packed)
00218 src = s->block_codebook[offset-num_blocks_raw];
00219 else
00220 continue;
00221 src_stride = 4;
00222 }
00223
00224 for(j=0; j<4; j++)
00225 for(i=0; i<4; i++)
00226 s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
00227 src[j*src_stride + i];
00228 }
00229
00230 return 0;
00231 }
00232
00234 static void cond_release_buffer(AVFrame *pic)
00235 {
00236 if (pic->data[0]) {
00237 av_freep(&pic->data[0]);
00238 av_free(pic->data[1]);
00239 }
00240 }
00241
00242 static int tgv_decode_frame(AVCodecContext *avctx,
00243 void *data, int *data_size,
00244 AVPacket *avpkt)
00245 {
00246 const uint8_t *buf = avpkt->data;
00247 int buf_size = avpkt->size;
00248 TgvContext *s = avctx->priv_data;
00249 const uint8_t *buf_end = buf + buf_size;
00250 int chunk_type;
00251
00252 chunk_type = AV_RL32(&buf[0]);
00253 buf += EA_PREAMBLE_SIZE;
00254
00255 if (chunk_type==kVGT_TAG) {
00256 int pal_count, i;
00257 if(buf+12>buf_end) {
00258 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
00259 return -1;
00260 }
00261
00262 s->width = AV_RL16(&buf[0]);
00263 s->height = AV_RL16(&buf[2]);
00264 if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
00265 avcodec_set_dimensions(s->avctx, s->width, s->height);
00266 cond_release_buffer(&s->frame);
00267 cond_release_buffer(&s->last_frame);
00268 }
00269
00270 pal_count = AV_RL16(&buf[6]);
00271 buf += 12;
00272 for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) {
00273 s->palette[i] = AV_RB24(buf);
00274 buf += 3;
00275 }
00276 }
00277
00278 if (avcodec_check_dimensions(avctx, s->width, s->height))
00279 return -1;
00280
00281
00282 FFSWAP(AVFrame, s->frame, s->last_frame);
00283 if (!s->frame.data[0]) {
00284 s->frame.reference = 1;
00285 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
00286 s->frame.linesize[0] = s->width;
00287
00288
00289 s->frame.data[0] = av_malloc(s->width*s->height + 12);
00290 if (!s->frame.data[0])
00291 return AVERROR(ENOMEM);
00292 s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
00293 if (!s->frame.data[1]) {
00294 av_freep(&s->frame.data[0]);
00295 return AVERROR(ENOMEM);
00296 }
00297 }
00298 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00299
00300 if(chunk_type==kVGT_TAG) {
00301 s->frame.key_frame = 1;
00302 s->frame.pict_type = FF_I_TYPE;
00303 if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
00304 av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
00305 return -1;
00306 }
00307 }else{
00308 if (!s->last_frame.data[0]) {
00309 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
00310 return buf_size;
00311 }
00312 s->frame.key_frame = 0;
00313 s->frame.pict_type = FF_P_TYPE;
00314 if (tgv_decode_inter(s, buf, buf_end)<0) {
00315 av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
00316 return -1;
00317 }
00318 }
00319
00320 *data_size = sizeof(AVFrame);
00321 *(AVFrame*)data = s->frame;
00322
00323 return buf_size;
00324 }
00325
00326 static av_cold int tgv_decode_end(AVCodecContext *avctx)
00327 {
00328 TgvContext *s = avctx->priv_data;
00329 cond_release_buffer(&s->frame);
00330 cond_release_buffer(&s->last_frame);
00331 av_free(s->mv_codebook);
00332 av_free(s->block_codebook);
00333 return 0;
00334 }
00335
00336 AVCodec eatgv_decoder = {
00337 "eatgv",
00338 AVMEDIA_TYPE_VIDEO,
00339 CODEC_ID_TGV,
00340 sizeof(TgvContext),
00341 tgv_decode_init,
00342 NULL,
00343 tgv_decode_end,
00344 tgv_decode_frame,
00345 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
00346 };