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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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;
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;
58  return 0;
59 }
60 
65 static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) {
66  unsigned char *dst_end = dst + width*height;
67  int size, size1, size2, offset, run;
68  unsigned char *dst_start = dst;
69 
70  if (src[0] & 0x01)
71  src += 5;
72  else
73  src += 2;
74 
75  if (src+3>src_end)
76  return -1;
77  size = AV_RB24(src);
78  src += 3;
79 
80  while(size>0 && src<src_end) {
81 
82  /* determine size1 and size2 */
83  size1 = (src[0] & 3);
84  if ( src[0] & 0x80 ) { // 1
85  if (src[0] & 0x40 ) { // 11
86  if ( src[0] & 0x20 ) { // 111
87  if ( src[0] < 0xFC ) // !(111111)
88  size1 = (((src[0] & 31) + 1) << 2);
89  src++;
90  size2 = 0;
91  } else { // 110
92  offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
93  size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
94  src += 4;
95  }
96  } else { // 10
97  size1 = ( ( src[1] & 0xC0) >> 6 );
98  offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
99  size2 = (src[0] & 0x3F) + 4;
100  src += 3;
101  }
102  } else { // 0
103  offset = ((src[0] & 0x60) << 3) + src[1] + 1;
104  size2 = ((src[0] & 0x1C) >> 2) + 3;
105  src += 2;
106  }
107 
108 
109  /* fetch strip from src */
110  if (size1>src_end-src)
111  break;
112 
113  if (size1>0) {
114  size -= size1;
115  run = FFMIN(size1, dst_end-dst);
116  memcpy(dst, src, run);
117  dst += run;
118  src += run;
119  }
120 
121  if (size2>0) {
122  if (dst-dst_start<offset)
123  return 0;
124  size -= size2;
125  run = FFMIN(size2, dst_end-dst);
126  av_memcpy_backptr(dst, offset, run);
127  dst += run;
128  }
129  }
130 
131  return 0;
132 }
133 
138 static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){
139  int num_mvs;
140  int num_blocks_raw;
141  int num_blocks_packed;
142  int vector_bits;
143  int i,j,x,y;
144  GetBitContext gb;
145  int mvbits;
146  const unsigned char *blocks_raw;
147 
148  if(buf+12>buf_end)
149  return -1;
150 
151  num_mvs = AV_RL16(&buf[0]);
152  num_blocks_raw = AV_RL16(&buf[2]);
153  num_blocks_packed = AV_RL16(&buf[4]);
154  vector_bits = AV_RL16(&buf[6]);
155  buf += 12;
156 
157  if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
159  "Invalid value for motion vector bits: %d\n", vector_bits);
160  return AVERROR_INVALIDDATA;
161  }
162 
163  /* allocate codebook buffers as necessary */
164  if (num_mvs > s->num_mvs) {
165  s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
166  s->num_mvs = num_mvs;
167  }
168 
169  if (num_blocks_packed > s->num_blocks_packed) {
170  s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
171  s->num_blocks_packed = num_blocks_packed;
172  }
173 
174  /* read motion vectors */
175  mvbits = (num_mvs*2*10+31) & ~31;
176 
177  if (buf+(mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed>buf_end)
178  return -1;
179 
180  init_get_bits(&gb, buf, mvbits);
181  for (i=0; i<num_mvs; i++) {
182  s->mv_codebook[i][0] = get_sbits(&gb, 10);
183  s->mv_codebook[i][1] = get_sbits(&gb, 10);
184  }
185  buf += mvbits>>3;
186 
187  /* note ptr to uncompressed blocks */
188  blocks_raw = buf;
189  buf += num_blocks_raw*16;
190 
191  /* read compressed blocks */
192  init_get_bits(&gb, buf, (buf_end-buf)<<3);
193  for (i=0; i<num_blocks_packed; i++) {
194  int tmp[4];
195  for(j=0; j<4; j++)
196  tmp[j] = get_bits(&gb, 8);
197  for(j=0; j<16; j++)
198  s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
199  }
200 
201  if (get_bits_left(&gb) < vector_bits *
202  (s->avctx->height/4) * (s->avctx->width/4))
203  return -1;
204 
205  /* read vectors and build frame */
206  for(y=0; y<s->avctx->height/4; y++)
207  for(x=0; x<s->avctx->width/4; x++) {
208  unsigned int vector = get_bits(&gb, vector_bits);
209  const unsigned char *src;
210  int src_stride;
211 
212  if (vector < num_mvs) {
213  int mx = x * 4 + s->mv_codebook[vector][0];
214  int my = y * 4 + s->mv_codebook[vector][1];
215 
216  if ( mx < 0 || mx + 4 > s->avctx->width
217  || my < 0 || my + 4 > s->avctx->height)
218  continue;
219 
220  src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
221  src_stride = s->last_frame.linesize[0];
222  }else{
223  int offset = vector - num_mvs;
224  if (offset<num_blocks_raw)
225  src = blocks_raw + 16*offset;
226  else if (offset-num_blocks_raw<num_blocks_packed)
227  src = s->block_codebook[offset-num_blocks_raw];
228  else
229  continue;
230  src_stride = 4;
231  }
232 
233  for(j=0; j<4; j++)
234  for(i=0; i<4; i++)
235  s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
236  src[j*src_stride + i];
237  }
238 
239  return 0;
240 }
241 
243 static void cond_release_buffer(AVFrame *pic)
244 {
245  if (pic->data[0]) {
246  av_freep(&pic->data[0]);
247  av_free(pic->data[1]);
248  }
249 }
250 
252  void *data, int *got_frame,
253  AVPacket *avpkt)
254 {
255  const uint8_t *buf = avpkt->data;
256  int buf_size = avpkt->size;
257  TgvContext *s = avctx->priv_data;
258  const uint8_t *buf_end = buf + buf_size;
259  int chunk_type;
260 
261  chunk_type = AV_RL32(&buf[0]);
262  buf += EA_PREAMBLE_SIZE;
263 
264  if (chunk_type==kVGT_TAG) {
265  int pal_count, i;
266  if(buf+12>buf_end) {
267  av_log(avctx, AV_LOG_WARNING, "truncated header\n");
268  return -1;
269  }
270 
271  s->width = AV_RL16(&buf[0]);
272  s->height = AV_RL16(&buf[2]);
273  if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
277  }
278 
279  pal_count = AV_RL16(&buf[6]);
280  buf += 12;
281  for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) {
282  s->palette[i] = AV_RB24(buf);
283  buf += 3;
284  }
285  }
286 
287  if (av_image_check_size(s->width, s->height, 0, avctx))
288  return -1;
289 
290  /* shuffle */
291  FFSWAP(AVFrame, s->frame, s->last_frame);
292  if (!s->frame.data[0]) {
293  s->frame.reference = 1;
295  s->frame.linesize[0] = s->width;
296 
297  s->frame.data[0] = av_malloc(s->width * s->height);
298  if (!s->frame.data[0])
299  return AVERROR(ENOMEM);
301  if (!s->frame.data[1]) {
302  av_freep(&s->frame.data[0]);
303  return AVERROR(ENOMEM);
304  }
305  }
306  memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
307 
308  if(chunk_type==kVGT_TAG) {
309  s->frame.key_frame = 1;
311  if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
312  av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
313  return -1;
314  }
315  }else{
316  if (!s->last_frame.data[0]) {
317  av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
318  return buf_size;
319  }
320  s->frame.key_frame = 0;
322  if (tgv_decode_inter(s, buf, buf_end)<0) {
323  av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
324  return -1;
325  }
326  }
327 
328  *got_frame = 1;
329  *(AVFrame*)data = s->frame;
330 
331  return buf_size;
332 }
333 
335 {
336  TgvContext *s = avctx->priv_data;
339  av_free(s->mv_codebook);
341  return 0;
342 }
343 
345  .name = "eatgv",
346  .type = AVMEDIA_TYPE_VIDEO,
347  .id = AV_CODEC_ID_TGV,
348  .priv_data_size = sizeof(TgvContext),
352  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
353 };
int height
Definition: eatgv.c:44
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
int size
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
unsigned char(* block_codebook)[16]
Definition: eatgv.c:48
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1253
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
int width
Definition: eatgv.c:44
memory handling functions
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int size
Definition: avcodec.h:916
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
#define AV_RL16
Definition: intreadwrite.h:42
uint8_t run
Definition: svq3.c:124
AVCodec.
Definition: avcodec.h:2960
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:223
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1465
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height)
Unpack buffer.
Definition: eatgv.c:65
AVFrame frame
Definition: eatgv.c:42
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define kVGT_TAG
Definition: eatgv.c:38
AVCodecContext * avctx
Definition: eatgv.c:41
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
bitstream reader API header.
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
int(* mv_codebook)[2]
Definition: eatgv.c:47
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:547
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
#define AV_RB16
Definition: intreadwrite.h:53
static av_cold int tgv_decode_end(AVCodecContext *avctx)
Definition: eatgv.c:334
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int num_mvs
current length of mv_codebook
Definition: eatgv.c:49
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
struct AVRational AVRational
rational number numerator/denominator
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:220
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
unsigned int palette[AVPALETTE_COUNT]
Definition: eatgv.c:45
int width
picture width / height.
Definition: avcodec.h:1508
#define AV_RL32
Definition: intreadwrite.h:146
AVCodec ff_eatgv_decoder
Definition: eatgv.c:344
int num_blocks_packed
current length of block_codebook
Definition: eatgv.c:50
static int width
Definition: utils.c:156
#define EA_PREAMBLE_SIZE
Definition: eatgv.c:37
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
static int tgv_decode_inter(TgvContext *s, const uint8_t *buf, const uint8_t *buf_end)
Decode inter-frame.
Definition: eatgv.c:138
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
static av_cold int tgv_decode_init(AVCodecContext *avctx)
Definition: eatgv.c:53
struct TgvContext TgvContext
#define MIN_CACHE_BITS
Definition: get_bits.h:120
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:72
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:116
static const int num_mvs[RV34_MB_TYPES]
number of motion vectors in each macroblock type
Definition: rv34.c:834
AVFrame last_frame
Definition: eatgv.c:43
void * priv_data
Definition: avcodec.h:1382
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:252
static int tgv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: eatgv.c:251
This structure stores compressed data.
Definition: avcodec.h:898
static void cond_release_buffer(AVFrame *pic)
release AVFrame buffers if allocated
Definition: eatgv.c:243
Predicted.
Definition: avutil.h:246