Libav
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 "libavutil/imgutils.h"
32 #include "libavutil/mem.h"
33 
34 #define BITSTREAM_READER_LE
35 #include "avcodec.h"
36 #include "get_bits.h"
37 #include "internal.h"
38 
39 #define EA_PREAMBLE_SIZE 8
40 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
41 
42 typedef struct TgvContext {
48 
49  int (*mv_codebook)[2];
51  int num_mvs;
53 } TgvContext;
54 
56 {
57  TgvContext *s = avctx->priv_data;
58  s->avctx = avctx;
59  avctx->framerate = (AVRational){ 15, 1 };
60  avctx->pix_fmt = AV_PIX_FMT_PAL8;
61 
63  if (!s->last_frame)
64  return AVERROR(ENOMEM);
65 
66  return 0;
67 }
68 
73 static int unpack(const uint8_t *src, const uint8_t *src_end,
74  uint8_t *dst, int width, int height)
75 {
76  uint8_t *dst_end = dst + width*height;
77  int size, size1, size2, offset, run;
78  uint8_t *dst_start = dst;
79 
80  if (src[0] & 0x01)
81  src += 5;
82  else
83  src += 2;
84 
85  if (src + 3 > src_end)
86  return AVERROR_INVALIDDATA;
87  size = AV_RB24(src);
88  src += 3;
89 
90  while (size > 0 && src < src_end) {
91 
92  /* determine size1 and size2 */
93  size1 = (src[0] & 3);
94  if (src[0] & 0x80) { // 1
95  if (src[0] & 0x40 ) { // 11
96  if (src[0] & 0x20) { // 111
97  if (src[0] < 0xFC) // !(111111)
98  size1 = (((src[0] & 31) + 1) << 2);
99  src++;
100  size2 = 0;
101  } else { // 110
102  offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
103  size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
104  src += 4;
105  }
106  } else { // 10
107  size1 = ((src[1] & 0xC0) >> 6);
108  offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
109  size2 = (src[0] & 0x3F) + 4;
110  src += 3;
111  }
112  } else { // 0
113  offset = ((src[0] & 0x60) << 3) + src[1] + 1;
114  size2 = ((src[0] & 0x1C) >> 2) + 3;
115  src += 2;
116  }
117 
118 
119  /* fetch strip from src */
120  if (size1 > src_end - src)
121  break;
122 
123  if (size1 > 0) {
124  size -= size1;
125  run = FFMIN(size1, dst_end - dst);
126  memcpy(dst, src, run);
127  dst += run;
128  src += run;
129  }
130 
131  if (size2 > 0) {
132  if (dst - dst_start < offset)
133  return 0;
134  size -= size2;
135  run = FFMIN(size2, dst_end - dst);
136  av_memcpy_backptr(dst, offset, run);
137  dst += run;
138  }
139  }
140 
141  return 0;
142 }
143 
148 static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
149  const uint8_t *buf, const uint8_t *buf_end)
150 {
151  int num_mvs;
152  int num_blocks_raw;
153  int num_blocks_packed;
154  int vector_bits;
155  int i,j,x,y;
156  GetBitContext gb;
157  int mvbits;
158  const uint8_t *blocks_raw;
159 
160  if (buf + 12 > buf_end)
161  return AVERROR_INVALIDDATA;
162 
163  num_mvs = AV_RL16(&buf[0]);
164  num_blocks_raw = AV_RL16(&buf[2]);
165  num_blocks_packed = AV_RL16(&buf[4]);
166  vector_bits = AV_RL16(&buf[6]);
167  buf += 12;
168 
169  if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
171  "Invalid value for motion vector bits: %d\n", vector_bits);
172  return AVERROR_INVALIDDATA;
173  }
174 
175  /* allocate codebook buffers as necessary */
176  if (num_mvs > s->num_mvs) {
177  int err = av_reallocp(&s->mv_codebook, num_mvs * 2 * sizeof(int));
178  if (err < 0)
179  return err;
180  s->num_mvs = num_mvs;
181  }
182 
183  if (num_blocks_packed > s->num_blocks_packed) {
184  int err;
185  if ((err = av_reallocp(&s->block_codebook, num_blocks_packed * 16)) < 0) {
186  s->num_blocks_packed = 0;
187  return err;
188  }
190  }
191 
192  /* read motion vectors */
193  mvbits = (num_mvs * 2 * 10 + 31) & ~31;
194 
195  if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
196  return AVERROR_INVALIDDATA;
197 
198  init_get_bits(&gb, buf, mvbits);
199  for (i = 0; i < num_mvs; i++) {
200  s->mv_codebook[i][0] = get_sbits(&gb, 10);
201  s->mv_codebook[i][1] = get_sbits(&gb, 10);
202  }
203  buf += mvbits >> 3;
204 
205  /* note ptr to uncompressed blocks */
206  blocks_raw = buf;
207  buf += num_blocks_raw * 16;
208 
209  /* read compressed blocks */
210  init_get_bits(&gb, buf, (buf_end - buf) << 3);
211  for (i = 0; i < num_blocks_packed; i++) {
212  int tmp[4];
213  for (j = 0; j < 4; j++)
214  tmp[j] = get_bits(&gb, 8);
215  for (j = 0; j < 16; j++)
216  s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
217  }
218 
219  if (get_bits_left(&gb) < vector_bits *
220  (s->avctx->height / 4) * (s->avctx->width / 4))
221  return AVERROR_INVALIDDATA;
222 
223  /* read vectors and build frame */
224  for (y = 0; y < s->avctx->height / 4; y++)
225  for (x = 0; x < s->avctx->width / 4; x++) {
226  unsigned int vector = get_bits(&gb, vector_bits);
227  const uint8_t *src;
228  int src_stride;
229 
230  if (vector < num_mvs) {
231  int mx = x * 4 + s->mv_codebook[vector][0];
232  int my = y * 4 + s->mv_codebook[vector][1];
233 
234  if (mx < 0 || mx + 4 > s->avctx->width ||
235  my < 0 || my + 4 > s->avctx->height)
236  continue;
237 
238  src = s->last_frame->data[0] + mx + my * s->last_frame->linesize[0];
239  src_stride = s->last_frame->linesize[0];
240  } else {
241  int offset = vector - num_mvs;
242  if (offset < num_blocks_raw)
243  src = blocks_raw + 16*offset;
244  else if (offset - num_blocks_raw < num_blocks_packed)
245  src = s->block_codebook[offset - num_blocks_raw];
246  else
247  continue;
248  src_stride = 4;
249  }
250 
251  for (j = 0; j < 4; j++)
252  for (i = 0; i < 4; i++)
253  frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
254  src[j * src_stride + i];
255  }
256 
257  return 0;
258 }
259 
261  void *data, int *got_frame,
262  AVPacket *avpkt)
263 {
264  const uint8_t *buf = avpkt->data;
265  int buf_size = avpkt->size;
266  TgvContext *s = avctx->priv_data;
267  const uint8_t *buf_end = buf + buf_size;
268  AVFrame *frame = data;
269  int chunk_type, ret;
270 
271  chunk_type = AV_RL32(&buf[0]);
272  buf += EA_PREAMBLE_SIZE;
273 
274  if (chunk_type == kVGT_TAG) {
275  int pal_count, i;
276  if (buf + 12 > buf_end) {
277  av_log(avctx, AV_LOG_WARNING, "truncated header\n");
278  return AVERROR_INVALIDDATA;
279  }
280 
281  s->width = AV_RL16(&buf[0]);
282  s->height = AV_RL16(&buf[2]);
283  if (s->avctx->width != s->width || s->avctx->height != s->height) {
284  av_freep(&s->frame_buffer);
286  if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
287  return ret;
288  }
289 
290  pal_count = AV_RL16(&buf[6]);
291  buf += 12;
292  for (i = 0; i < pal_count && i < AVPALETTE_COUNT && buf + 2 < buf_end; i++) {
293  s->palette[i] = AV_RB24(buf);
294  buf += 3;
295  }
296  }
297 
298  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
299  return ret;
300 
301  memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
302 
303  if (chunk_type == kVGT_TAG) {
304  int y;
305  frame->key_frame = 1;
306  frame->pict_type = AV_PICTURE_TYPE_I;
307 
308  if (!s->frame_buffer &&
309  !(s->frame_buffer = av_malloc(s->width * s->height)))
310  return AVERROR(ENOMEM);
311 
312  if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
313  av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
314  return AVERROR_INVALIDDATA;
315  }
316  for (y = 0; y < s->height; y++)
317  memcpy(frame->data[0] + y * frame->linesize[0],
318  s->frame_buffer + y * s->width,
319  s->width);
320  } else {
321  if (!s->last_frame->data[0]) {
322  av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
323  return buf_size;
324  }
325  frame->key_frame = 0;
326  frame->pict_type = AV_PICTURE_TYPE_P;
327  if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
328  av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
329  return AVERROR_INVALIDDATA;
330  }
331  }
332 
334  if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
335  return ret;
336 
337  *got_frame = 1;
338 
339  return buf_size;
340 }
341 
343 {
344  TgvContext *s = avctx->priv_data;
346  av_freep(&s->frame_buffer);
347  av_free(s->mv_codebook);
349  return 0;
350 }
351 
353  .name = "eatgv",
354  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
355  .type = AVMEDIA_TYPE_VIDEO,
356  .id = AV_CODEC_ID_TGV,
357  .priv_data_size = sizeof(TgvContext),
359  .close = tgv_decode_end,
361  .capabilities = AV_CODEC_CAP_DR1,
362 };
#define AVPALETTE_SIZE
Definition: avcodec.h:3402
int height
Definition: eatgv.c:46
AVRational framerate
Definition: avcodec.h:3063
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:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
AVFrame * last_frame
Definition: eatgv.c:44
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
int width
Definition: eatgv.c:46
memory handling functions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:134
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
#define AV_RL16
Definition: intreadwrite.h:42
#define AVPALETTE_COUNT
Definition: avcodec.h:3403
uint8_t run
Definition: svq3.c:203
AVCodec.
Definition: avcodec.h:3120
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:214
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:202
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:70
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:199
#define kVGT_TAG
Definition: eatgv.c:40
AVCodecContext * avctx
Definition: eatgv.c:43
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
bitstream reader API header.
int(* mv_codebook)[2]
Definition: eatgv.c:49
#define src
Definition: vp8dsp.c:254
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
static av_cold int tgv_decode_end(AVCodecContext *avctx)
Definition: eatgv.c:342
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
int num_mvs
current length of mv_codebook
Definition: eatgv.c:51
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
uint32_t palette[AVPALETTE_COUNT]
Definition: eatgv.c:47
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
#define FFMIN(a, b)
Definition: common.h:66
uint8_t(* block_codebook)[16]
Definition: eatgv.c:50
int width
picture width / height.
Definition: avcodec.h:1580
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
#define AV_RL32
Definition: intreadwrite.h:146
AVCodec ff_eatgv_decoder
Definition: eatgv.c:352
uint8_t * frame_buffer
Definition: eatgv.c:45
int num_blocks_packed
current length of block_codebook
Definition: eatgv.c:52
#define EA_PREAMBLE_SIZE
Definition: eatgv.c:39
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
static int tgv_decode_inter(TgvContext *s, AVFrame *frame, const uint8_t *buf, const uint8_t *buf_end)
Decode inter-frame.
Definition: eatgv.c:148
rational number numerator/denominator
Definition: rational.h:43
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
static av_cold int tgv_decode_init(AVCodecContext *avctx)
Definition: eatgv.c:55
#define MIN_CACHE_BITS
Definition: get_bits.h:110
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:302
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
static int unpack(const uint8_t *src, const uint8_t *src_end, uint8_t *dst, int width, int height)
Unpack buffer.
Definition: eatgv.c:73
common internal api header.
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
void * priv_data
Definition: avcodec.h:1451
static uint8_t tmp[8]
Definition: des.c:38
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:325
static int tgv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: eatgv.c:260
This structure stores compressed data.
Definition: avcodec.h:1323
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1183
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
Predicted.
Definition: avutil.h:261