FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
eatgv.c
Go to the documentation of this file.
1 /*
2  * Electronic Arts TGV Video Decoder
3  * Copyright (c) 2007-2008 Peter Ross
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Electronic Arts TGV Video Decoder
25  * by Peter Ross (pross@xvid.org)
26  *
27  * Technical details here:
28  * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGV
29  */
30 
31 #include "avcodec.h"
32 #define BITSTREAM_READER_LE
33 #include "get_bits.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/mem.h"
36 
37 #define EA_PREAMBLE_SIZE 8
38 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
39 
40 typedef struct TgvContext {
45  unsigned int palette[AVPALETTE_COUNT];
46 
47  int (*mv_codebook)[2];
48  unsigned char (*block_codebook)[16];
49  int num_mvs; ///< current length of mv_codebook
50  int num_blocks_packed; ///< current length of block_codebook
51 } TgvContext;
52 
54  TgvContext *s = avctx->priv_data;
55  s->avctx = avctx;
56  avctx->time_base = (AVRational){1, 15};
57  avctx->pix_fmt = AV_PIX_FMT_PAL8;
60  return 0;
61 }
62 
63 /**
64  * Unpack buffer
65  * @return 0 on success, -1 on critical buffer underflow
66  */
67 static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) {
68  unsigned char *dst_end = dst + width*height;
69  int size, size1, size2, offset, run;
70  unsigned char *dst_start = dst;
71 
72  if (src[0] & 0x01)
73  src += 5;
74  else
75  src += 2;
76 
77  if (src_end - src < 3)
78  return -1;
79  size = AV_RB24(src);
80  src += 3;
81 
82  while(size>0 && src<src_end) {
83 
84  /* determine size1 and size2 */
85  size1 = (src[0] & 3);
86  if ( src[0] & 0x80 ) { // 1
87  if (src[0] & 0x40 ) { // 11
88  if ( src[0] & 0x20 ) { // 111
89  if ( src[0] < 0xFC ) // !(111111)
90  size1 = (((src[0] & 31) + 1) << 2);
91  src++;
92  size2 = 0;
93  } else { // 110
94  offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
95  size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
96  src += 4;
97  }
98  } else { // 10
99  size1 = ( ( src[1] & 0xC0) >> 6 );
100  offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
101  size2 = (src[0] & 0x3F) + 4;
102  src += 3;
103  }
104  } else { // 0
105  offset = ((src[0] & 0x60) << 3) + src[1] + 1;
106  size2 = ((src[0] & 0x1C) >> 2) + 3;
107  src += 2;
108  }
109 
110 
111  /* fetch strip from src */
112  if (size1>src_end-src)
113  break;
114 
115  if (size1>0) {
116  size -= size1;
117  run = FFMIN(size1, dst_end-dst);
118  memcpy(dst, src, run);
119  dst += run;
120  src += run;
121  }
122 
123  if (size2>0) {
124  if (dst-dst_start<offset)
125  return 0;
126  size -= size2;
127  run = FFMIN(size2, dst_end-dst);
128  av_memcpy_backptr(dst, offset, run);
129  dst += run;
130  }
131  }
132 
133  return 0;
134 }
135 
136 /**
137  * Decode inter-frame
138  * @return 0 on success, -1 on critical buffer underflow
139  */
140 static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){
141  int num_mvs;
142  int num_blocks_raw;
143  int num_blocks_packed;
144  int vector_bits;
145  int i,j,x,y;
146  GetBitContext gb;
147  int mvbits;
148  const unsigned char *blocks_raw;
149 
150  if(buf_end - buf < 12)
151  return -1;
152 
153  num_mvs = AV_RL16(&buf[0]);
154  num_blocks_raw = AV_RL16(&buf[2]);
155  num_blocks_packed = AV_RL16(&buf[4]);
156  vector_bits = AV_RL16(&buf[6]);
157  buf += 12;
158 
159  if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
161  "Invalid value for motion vector bits: %d\n", vector_bits);
162  return AVERROR_INVALIDDATA;
163  }
164 
165  /* allocate codebook buffers as necessary */
166  if (num_mvs > s->num_mvs) {
167  s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
168  s->num_mvs = num_mvs;
169  }
170 
171  if (num_blocks_packed > s->num_blocks_packed) {
172  s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
173  s->num_blocks_packed = num_blocks_packed;
174  }
175 
176  /* read motion vectors */
177  mvbits = (num_mvs*2*10+31) & ~31;
178 
179  if (buf_end - buf < (mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed)
180  return -1;
181 
182  init_get_bits(&gb, buf, mvbits);
183  for (i=0; i<num_mvs; i++) {
184  s->mv_codebook[i][0] = get_sbits(&gb, 10);
185  s->mv_codebook[i][1] = get_sbits(&gb, 10);
186  }
187  buf += mvbits>>3;
188 
189  /* note ptr to uncompressed blocks */
190  blocks_raw = buf;
191  buf += num_blocks_raw*16;
192 
193  /* read compressed blocks */
194  init_get_bits(&gb, buf, (buf_end-buf)<<3);
195  for (i=0; i<num_blocks_packed; i++) {
196  int tmp[4];
197  for(j=0; j<4; j++)
198  tmp[j] = get_bits(&gb, 8);
199  for(j=0; j<16; j++)
200  s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
201  }
202 
203  if (get_bits_left(&gb) < vector_bits *
204  (s->avctx->height/4) * (s->avctx->width/4))
205  return -1;
206 
207  /* read vectors and build frame */
208  for(y=0; y<s->avctx->height/4; y++)
209  for(x=0; x<s->avctx->width/4; x++) {
210  unsigned int vector = get_bits(&gb, vector_bits);
211  const unsigned char *src;
212  int src_stride;
213 
214  if (vector < num_mvs) {
215  int mx = x * 4 + s->mv_codebook[vector][0];
216  int my = y * 4 + s->mv_codebook[vector][1];
217 
218  if ( mx < 0 || mx + 4 > s->avctx->width
219  || my < 0 || my + 4 > s->avctx->height) {
220  av_log(s->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);
221  continue;
222  }
223 
224  src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
225  src_stride = s->last_frame.linesize[0];
226  }else{
227  int offset = vector - num_mvs;
228  if (offset<num_blocks_raw)
229  src = blocks_raw + 16*offset;
230  else if (offset-num_blocks_raw<num_blocks_packed)
231  src = s->block_codebook[offset-num_blocks_raw];
232  else
233  continue;
234  src_stride = 4;
235  }
236 
237  for(j=0; j<4; j++)
238  for(i=0; i<4; i++)
239  s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
240  src[j*src_stride + i];
241  }
242 
243  return 0;
244 }
245 
246 /** release AVFrame buffers if allocated */
247 static void cond_release_buffer(AVFrame *pic)
248 {
249  if (pic->data[0]) {
250  av_freep(&pic->data[0]);
251  av_free(pic->data[1]);
252  }
253 }
254 
256  void *data, int *got_frame,
257  AVPacket *avpkt)
258 {
259  const uint8_t *buf = avpkt->data;
260  int buf_size = avpkt->size;
261  TgvContext *s = avctx->priv_data;
262  const uint8_t *buf_end = buf + buf_size;
263  int chunk_type;
264 
265  if (buf_end - buf < EA_PREAMBLE_SIZE)
266  return AVERROR_INVALIDDATA;
267 
268  chunk_type = AV_RL32(&buf[0]);
269  buf += EA_PREAMBLE_SIZE;
270 
271  if (chunk_type==kVGT_TAG) {
272  int pal_count, i;
273  if(buf_end - buf < 12) {
274  av_log(avctx, AV_LOG_WARNING, "truncated header\n");
275  return -1;
276  }
277 
278  s->width = AV_RL16(&buf[0]);
279  s->height = AV_RL16(&buf[2]);
280  if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
284  }
285 
286  pal_count = AV_RL16(&buf[6]);
287  buf += 12;
288  for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
289  s->palette[i] = 0xFFU << 24 | AV_RB24(buf);
290  buf += 3;
291  }
292  }
293 
294  if (av_image_check_size(s->width, s->height, 0, avctx))
295  return -1;
296 
297  /* shuffle */
298  FFSWAP(AVFrame, s->frame, s->last_frame);
299  if (!s->frame.data[0]) {
300  s->frame.reference = 3;
302  s->frame.linesize[0] = s->width;
303 
304  s->frame.data[0] = av_malloc(s->width * s->height);
305  if (!s->frame.data[0])
306  return AVERROR(ENOMEM);
308  if (!s->frame.data[1]) {
309  av_freep(&s->frame.data[0]);
310  return AVERROR(ENOMEM);
311  }
312  }
313  memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
314 
315  if(chunk_type==kVGT_TAG) {
316  s->frame.key_frame = 1;
318  if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
319  av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
320  return -1;
321  }
322  }else{
323  if (!s->last_frame.data[0]) {
324  av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
325  return buf_size;
326  }
327  s->frame.key_frame = 0;
329  if (tgv_decode_inter(s, buf, buf_end)<0) {
330  av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
331  return -1;
332  }
333  }
334 
335  *got_frame = 1;
336  *(AVFrame*)data = s->frame;
337 
338  return buf_size;
339 }
340 
342 {
343  TgvContext *s = avctx->priv_data;
346  av_free(s->mv_codebook);
348  return 0;
349 }
350 
352  .name = "eatgv",
353  .type = AVMEDIA_TYPE_VIDEO,
354  .id = AV_CODEC_ID_TGV,
355  .priv_data_size = sizeof(TgvContext),
359  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
360 };