FFmpeg  2.1.1
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 "internal.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/mem.h"
37 
38 #define EA_PREAMBLE_SIZE 8
39 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
40 
41 typedef struct TgvContext {
47 
48  int (*mv_codebook)[2];
50  int num_mvs; ///< current length of mv_codebook
51  int num_blocks_packed; ///< current length of block_codebook
52 } TgvContext;
53 
55 {
56  TgvContext *s = avctx->priv_data;
57  s->avctx = avctx;
58  avctx->time_base = (AVRational){1, 15};
59  avctx->pix_fmt = AV_PIX_FMT_PAL8;
61  return 0;
62 }
63 
64 /**
65  * Unpack buffer
66  * @return 0 on success, -1 on critical buffer underflow
67  */
68 static int unpack(const uint8_t *src, const uint8_t *src_end,
69  uint8_t *dst, int width, int height)
70 {
71  uint8_t *dst_end = dst + width*height;
72  int size, size1, size2, offset, run;
73  uint8_t *dst_start = dst;
74 
75  if (src[0] & 0x01)
76  src += 5;
77  else
78  src += 2;
79 
80  if (src_end - src < 3)
81  return AVERROR_INVALIDDATA;
82  size = AV_RB24(src);
83  src += 3;
84 
85  while (size > 0 && src < src_end) {
86 
87  /* determine size1 and size2 */
88  size1 = (src[0] & 3);
89  if (src[0] & 0x80) { // 1
90  if (src[0] & 0x40 ) { // 11
91  if (src[0] & 0x20) { // 111
92  if (src[0] < 0xFC) // !(111111)
93  size1 = (((src[0] & 31) + 1) << 2);
94  src++;
95  size2 = 0;
96  } else { // 110
97  offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
98  size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
99  src += 4;
100  }
101  } else { // 10
102  size1 = ((src[1] & 0xC0) >> 6);
103  offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
104  size2 = (src[0] & 0x3F) + 4;
105  src += 3;
106  }
107  } else { // 0
108  offset = ((src[0] & 0x60) << 3) + src[1] + 1;
109  size2 = ((src[0] & 0x1C) >> 2) + 3;
110  src += 2;
111  }
112 
113 
114  /* fetch strip from src */
115  if (size1 > src_end - src)
116  break;
117 
118  if (size1 > 0) {
119  size -= size1;
120  run = FFMIN(size1, dst_end - dst);
121  memcpy(dst, src, run);
122  dst += run;
123  src += run;
124  }
125 
126  if (size2 > 0) {
127  if (dst - dst_start < offset)
128  return 0;
129  size -= size2;
130  run = FFMIN(size2, dst_end - dst);
131  av_memcpy_backptr(dst, offset, run);
132  dst += run;
133  }
134  }
135 
136  return 0;
137 }
138 
139 /**
140  * Decode inter-frame
141  * @return 0 on success, -1 on critical buffer underflow
142  */
144  const uint8_t *buf, const uint8_t *buf_end)
145 {
146  int num_mvs;
147  int num_blocks_raw;
148  int num_blocks_packed;
149  int vector_bits;
150  int i,j,x,y;
151  GetBitContext gb;
152  int mvbits;
153  const uint8_t *blocks_raw;
154 
155  if(buf_end - buf < 12)
156  return AVERROR_INVALIDDATA;
157 
158  num_mvs = AV_RL16(&buf[0]);
159  num_blocks_raw = AV_RL16(&buf[2]);
160  num_blocks_packed = AV_RL16(&buf[4]);
161  vector_bits = AV_RL16(&buf[6]);
162  buf += 12;
163 
164  if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
166  "Invalid value for motion vector bits: %d\n", vector_bits);
167  return AVERROR_INVALIDDATA;
168  }
169 
170  /* allocate codebook buffers as necessary */
171  if (num_mvs > s->num_mvs) {
172  if (av_reallocp_array(&s->mv_codebook, num_mvs, sizeof(*s->mv_codebook))) {
173  s->num_mvs = 0;
174  return AVERROR(ENOMEM);
175  }
176  s->num_mvs = num_mvs;
177  }
178 
179  if (num_blocks_packed > s->num_blocks_packed) {
180  if (av_reallocp_array(&s->block_codebook, num_blocks_packed, sizeof(*s->block_codebook))) {
181  s->num_blocks_packed = 0;
182  return AVERROR(ENOMEM);
183  }
184  s->num_blocks_packed = num_blocks_packed;
185  }
186 
187  /* read motion vectors */
188  mvbits = (num_mvs * 2 * 10 + 31) & ~31;
189 
190  if (buf_end - buf < (mvbits>>3) + 16*num_blocks_raw + 8*num_blocks_packed)
191  return AVERROR_INVALIDDATA;
192 
193  init_get_bits(&gb, buf, mvbits);
194  for (i = 0; i < num_mvs; i++) {
195  s->mv_codebook[i][0] = get_sbits(&gb, 10);
196  s->mv_codebook[i][1] = get_sbits(&gb, 10);
197  }
198  buf += mvbits >> 3;
199 
200  /* note ptr to uncompressed blocks */
201  blocks_raw = buf;
202  buf += num_blocks_raw * 16;
203 
204  /* read compressed blocks */
205  init_get_bits(&gb, buf, (buf_end - buf) << 3);
206  for (i = 0; i < num_blocks_packed; i++) {
207  int tmp[4];
208  for (j = 0; j < 4; j++)
209  tmp[j] = get_bits(&gb, 8);
210  for (j = 0; j < 16; j++)
211  s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
212  }
213 
214  if (get_bits_left(&gb) < vector_bits *
215  (s->avctx->height / 4) * (s->avctx->width / 4))
216  return AVERROR_INVALIDDATA;
217 
218  /* read vectors and build frame */
219  for (y = 0; y < s->avctx->height / 4; y++)
220  for (x = 0; x < s->avctx->width / 4; x++) {
221  unsigned int vector = get_bits(&gb, vector_bits);
222  const uint8_t *src;
223  int src_stride;
224 
225  if (vector < num_mvs) {
226  int mx = x * 4 + s->mv_codebook[vector][0];
227  int my = y * 4 + s->mv_codebook[vector][1];
228 
229  if (mx < 0 || mx + 4 > s->avctx->width ||
230  my < 0 || my + 4 > s->avctx->height) {
231  av_log(s->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);
232  continue;
233  }
234 
235  src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
236  src_stride = s->last_frame.linesize[0];
237  } else {
238  int offset = vector - num_mvs;
239  if (offset < num_blocks_raw)
240  src = blocks_raw + 16*offset;
241  else if (offset - num_blocks_raw < num_blocks_packed)
242  src = s->block_codebook[offset - num_blocks_raw];
243  else
244  continue;
245  src_stride = 4;
246  }
247 
248  for (j = 0; j < 4; j++)
249  for (i = 0; i < 4; i++)
250  frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
251  src[j * src_stride + i];
252  }
253 
254  return 0;
255 }
256 
258  void *data, int *got_frame,
259  AVPacket *avpkt)
260 {
261  const uint8_t *buf = avpkt->data;
262  int buf_size = avpkt->size;
263  TgvContext *s = avctx->priv_data;
264  const uint8_t *buf_end = buf + buf_size;
265  AVFrame *frame = data;
266  int chunk_type, ret;
267 
268  if (buf_end - buf < EA_PREAMBLE_SIZE)
269  return AVERROR_INVALIDDATA;
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_end - buf < 12) {
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) {
285  av_freep(&s->frame_buffer);
287  }
288 
289  pal_count = AV_RL16(&buf[6]);
290  buf += 12;
291  for(i = 0; i < pal_count && i < AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
292  s->palette[i] = 0xFFU << 24 | AV_RB24(buf);
293  buf += 3;
294  }
295  }
296 
297  if ((ret = av_image_check_size(s->width, s->height, 0, avctx)) < 0)
298  return ret;
299 
300  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
301  return ret;
302 
303  memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
304 
305  if (chunk_type == kVGT_TAG) {
306  int y;
307  frame->key_frame = 1;
308  frame->pict_type = AV_PICTURE_TYPE_I;
309 
310  if (!s->frame_buffer &&
311  !(s->frame_buffer = av_malloc(s->width * s->height)))
312  return AVERROR(ENOMEM);
313 
314  if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
315  av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
316  return AVERROR_INVALIDDATA;
317  }
318  for (y = 0; y < s->height; y++)
319  memcpy(frame->data[0] + y * frame->linesize[0],
320  s->frame_buffer + y * s->width,
321  s->width);
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  frame->key_frame = 0;
328  frame->pict_type = AV_PICTURE_TYPE_P;
329  if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
330  av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
331  return AVERROR_INVALIDDATA;
332  }
333  }
334 
336  if ((ret = av_frame_ref(&s->last_frame, frame)) < 0)
337  return ret;
338 
339  *got_frame = 1;
340 
341  return buf_size;
342 }
343 
345 {
346  TgvContext *s = avctx->priv_data;
348  av_freep(&s->frame_buffer);
349  av_free(s->mv_codebook);
351  return 0;
352 }
353 
355  .name = "eatgv",
356  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
357  .type = AVMEDIA_TYPE_VIDEO,
358  .id = AV_CODEC_ID_TGV,
359  .priv_data_size = sizeof(TgvContext),
363  .capabilities = CODEC_CAP_DR1,
364 };
int height
Definition: eatgv.c:45
const char * s
Definition: avisynth_c.h:668
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:255
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
int width
Definition: eatgv.c:45
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_RL16(x)
Definition: intreadwrite.h:245
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:233
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
uint8_t run
Definition: svq3.c:145
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:240
Predicted.
Definition: avcodec.h:2305
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1265
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
#define AV_RB16(x)
Definition: intreadwrite.h:232
uint8_t
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
#define kVGT_TAG
Definition: eatgv.c:39
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
AVCodecContext * avctx
Definition: eatgv.c:42
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by a given frame.
Definition: frame.c:247
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
const char data[16]
Definition: mxf.c:68
bitstream reader API header.
static AVFrame * frame
Definition: demuxing.c:51
int(* mv_codebook)[2]
Definition: eatgv.c:48
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:583
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
static av_cold int tgv_decode_end(AVCodecContext *avctx)
Definition: eatgv.c:344
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
int num_mvs
current length of mv_codebook
Definition: eatgv.c:50
#define AVPALETTE_COUNT
Definition: avcodec.h:4500
Libavcodec external API header.
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:231
uint32_t palette[AVPALETTE_COUNT]
Definition: eatgv.c:46
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:351
float y
uint8_t(* block_codebook)[16]
Definition: eatgv.c:49
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define AVPALETTE_SIZE
Definition: avcodec.h:4499
#define FFMIN(a, b)
Definition: avcodec.h:925
AVCodec ff_eatgv_decoder
Definition: eatgv.c:354
uint8_t * frame_buffer
Definition: eatgv.c:44
int num_blocks_packed
current length of block_codebook
Definition: eatgv.c:51
static int width
Definition: utils.c:158
#define EA_PREAMBLE_SIZE
Definition: eatgv.c:38
AVS_Value src
Definition: avisynth_c.h:523
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
void * buf
Definition: avisynth_c.h:594
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:1046
static int tgv_decode_inter(TgvContext *s, AVFrame *frame, const uint8_t *buf, const uint8_t *buf_end)
Decode inter-frame.
Definition: eatgv.c:143
rational number numerator/denominator
Definition: rational.h:43
uint8_t * data
Definition: avcodec.h:1063
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:405
static av_cold int tgv_decode_init(AVCodecContext *avctx)
Definition: eatgv.c:54
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
#define AV_RB24(x)
Definition: intreadwrite.h:436
#define MIN_CACHE_BITS
Definition: get_bits.h:123
void * priv_data
Definition: avcodec.h:1182
static int unpack(const uint8_t *src, const uint8_t *src_end, uint8_t *dst, int width, int height)
Unpack buffer.
Definition: eatgv.c:68
common internal api header.
static const int num_mvs[RV34_MB_TYPES]
number of motion vectors in each macroblock type
Definition: rv34.c:836
AVFrame last_frame
Definition: eatgv.c:43
#define AVERROR_INVALIDDATA
#define AV_RL32(x)
Definition: intreadwrite.h:275
int key_frame
1 -&gt; keyframe, 0-&gt; not
Definition: frame.h:162
#define AVERROR(e)
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:416
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
static int tgv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: eatgv.c:257
8 bit with PIX_FMT_RGB32 palette
Definition: avcodec.h:4545
This structure stores compressed data.
Definition: avcodec.h:1040
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:910
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107