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 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, 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
00085 size1 = (src[0] & 3);
00086 if ( src[0] & 0x80 ) {
00087 if (src[0] & 0x40 ) {
00088 if ( src[0] & 0x20 ) {
00089 if ( src[0] < 0xFC )
00090 size1 = (((src[0] & 31) + 1) << 2);
00091 src++;
00092 size2 = 0;
00093 } else {
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 {
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 {
00105 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
00106 size2 = ((src[0] & 0x1C) >> 2) + 3;
00107 src += 2;
00108 }
00109
00110
00111
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 int num_mvs;
00142 int num_blocks_raw;
00143 int num_blocks_packed;
00144 int vector_bits;
00145 int i,j,x,y;
00146 GetBitContext gb;
00147 int mvbits;
00148 const unsigned char *blocks_raw;
00149
00150 if(buf_end - buf < 12)
00151 return -1;
00152
00153 num_mvs = AV_RL16(&buf[0]);
00154 num_blocks_raw = AV_RL16(&buf[2]);
00155 num_blocks_packed = AV_RL16(&buf[4]);
00156 vector_bits = AV_RL16(&buf[6]);
00157 buf += 12;
00158
00159 if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
00160 av_log(s->avctx, AV_LOG_ERROR,
00161 "Invalid value for motion vector bits: %d\n", vector_bits);
00162 return AVERROR_INVALIDDATA;
00163 }
00164
00165
00166 if (num_mvs > s->num_mvs) {
00167 s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
00168 s->num_mvs = num_mvs;
00169 }
00170
00171 if (num_blocks_packed > s->num_blocks_packed) {
00172 s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
00173 s->num_blocks_packed = num_blocks_packed;
00174 }
00175
00176
00177 mvbits = (num_mvs*2*10+31) & ~31;
00178
00179 if (buf_end - buf < (mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed)
00180 return -1;
00181
00182 init_get_bits(&gb, buf, mvbits);
00183 for (i=0; i<num_mvs; i++) {
00184 s->mv_codebook[i][0] = get_sbits(&gb, 10);
00185 s->mv_codebook[i][1] = get_sbits(&gb, 10);
00186 }
00187 buf += mvbits>>3;
00188
00189
00190 blocks_raw = buf;
00191 buf += num_blocks_raw*16;
00192
00193
00194 init_get_bits(&gb, buf, (buf_end-buf)<<3);
00195 for (i=0; i<num_blocks_packed; i++) {
00196 int tmp[4];
00197 for(j=0; j<4; j++)
00198 tmp[j] = get_bits(&gb, 8);
00199 for(j=0; j<16; j++)
00200 s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
00201 }
00202
00203 if (get_bits_left(&gb) < vector_bits *
00204 (s->avctx->height/4) * (s->avctx->width/4))
00205 return -1;
00206
00207
00208 for(y=0; y<s->avctx->height/4; y++)
00209 for(x=0; x<s->avctx->width/4; x++) {
00210 unsigned int vector = get_bits(&gb, vector_bits);
00211 const unsigned char *src;
00212 int src_stride;
00213
00214 if (vector < num_mvs) {
00215 int mx = x * 4 + s->mv_codebook[vector][0];
00216 int my = y * 4 + s->mv_codebook[vector][1];
00217
00218 if ( mx < 0 || mx + 4 > s->avctx->width
00219 || my < 0 || my + 4 > s->avctx->height) {
00220 av_log(s->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);
00221 continue;
00222 }
00223
00224 src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
00225 src_stride = s->last_frame.linesize[0];
00226 }else{
00227 int offset = vector - num_mvs;
00228 if (offset<num_blocks_raw)
00229 src = blocks_raw + 16*offset;
00230 else if (offset-num_blocks_raw<num_blocks_packed)
00231 src = s->block_codebook[offset-num_blocks_raw];
00232 else
00233 continue;
00234 src_stride = 4;
00235 }
00236
00237 for(j=0; j<4; j++)
00238 for(i=0; i<4; i++)
00239 s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
00240 src[j*src_stride + i];
00241 }
00242
00243 return 0;
00244 }
00245
00247 static void cond_release_buffer(AVFrame *pic)
00248 {
00249 if (pic->data[0]) {
00250 av_freep(&pic->data[0]);
00251 av_free(pic->data[1]);
00252 }
00253 }
00254
00255 static int tgv_decode_frame(AVCodecContext *avctx,
00256 void *data, int *data_size,
00257 AVPacket *avpkt)
00258 {
00259 const uint8_t *buf = avpkt->data;
00260 int buf_size = avpkt->size;
00261 TgvContext *s = avctx->priv_data;
00262 const uint8_t *buf_end = buf + buf_size;
00263 int chunk_type;
00264
00265 if (buf_end - buf < EA_PREAMBLE_SIZE)
00266 return AVERROR_INVALIDDATA;
00267
00268 chunk_type = AV_RL32(&buf[0]);
00269 buf += EA_PREAMBLE_SIZE;
00270
00271 if (chunk_type==kVGT_TAG) {
00272 int pal_count, i;
00273 if(buf_end - buf < 12) {
00274 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
00275 return -1;
00276 }
00277
00278 s->width = AV_RL16(&buf[0]);
00279 s->height = AV_RL16(&buf[2]);
00280 if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
00281 avcodec_set_dimensions(s->avctx, s->width, s->height);
00282 cond_release_buffer(&s->frame);
00283 cond_release_buffer(&s->last_frame);
00284 }
00285
00286 pal_count = AV_RL16(&buf[6]);
00287 buf += 12;
00288 for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
00289 s->palette[i] = 0xFF << 24 | AV_RB24(buf);
00290 buf += 3;
00291 }
00292 }
00293
00294 if (av_image_check_size(s->width, s->height, 0, avctx))
00295 return -1;
00296
00297
00298 FFSWAP(AVFrame, s->frame, s->last_frame);
00299 if (!s->frame.data[0]) {
00300 s->frame.reference = 3;
00301 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
00302 s->frame.linesize[0] = s->width;
00303
00304
00305 s->frame.data[0] = av_malloc(s->width*s->height + 12);
00306 if (!s->frame.data[0])
00307 return AVERROR(ENOMEM);
00308 s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
00309 if (!s->frame.data[1]) {
00310 av_freep(&s->frame.data[0]);
00311 return AVERROR(ENOMEM);
00312 }
00313 }
00314 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00315
00316 if(chunk_type==kVGT_TAG) {
00317 s->frame.key_frame = 1;
00318 s->frame.pict_type = AV_PICTURE_TYPE_I;
00319 if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
00320 av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
00321 return -1;
00322 }
00323 }else{
00324 if (!s->last_frame.data[0]) {
00325 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
00326 return buf_size;
00327 }
00328 s->frame.key_frame = 0;
00329 s->frame.pict_type = AV_PICTURE_TYPE_P;
00330 if (tgv_decode_inter(s, buf, buf_end)<0) {
00331 av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
00332 return -1;
00333 }
00334 }
00335
00336 *data_size = sizeof(AVFrame);
00337 *(AVFrame*)data = s->frame;
00338
00339 return buf_size;
00340 }
00341
00342 static av_cold int tgv_decode_end(AVCodecContext *avctx)
00343 {
00344 TgvContext *s = avctx->priv_data;
00345 cond_release_buffer(&s->frame);
00346 cond_release_buffer(&s->last_frame);
00347 av_free(s->mv_codebook);
00348 av_free(s->block_codebook);
00349 return 0;
00350 }
00351
00352 AVCodec ff_eatgv_decoder = {
00353 .name = "eatgv",
00354 .type = AVMEDIA_TYPE_VIDEO,
00355 .id = AV_CODEC_ID_TGV,
00356 .priv_data_size = sizeof(TgvContext),
00357 .init = tgv_decode_init,
00358 .close = tgv_decode_end,
00359 .decode = tgv_decode_frame,
00360 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
00361 };