FFmpeg
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 "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 "codec_internal.h"
38 #include "internal.h"
39 
40 #define EA_PREAMBLE_SIZE 8
41 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
42 
43 typedef struct TgvContext {
46  uint8_t *frame_buffer;
49 
51  uint8_t (*block_codebook)[16];
52  int num_mvs; ///< current length of mv_codebook
53  int num_blocks_packed; ///< current length of block_codebook
54 } TgvContext;
55 
57 {
58  TgvContext *s = avctx->priv_data;
59  s->avctx = avctx;
60  avctx->framerate = (AVRational){ 15, 1 };
61  avctx->pix_fmt = AV_PIX_FMT_PAL8;
62 
63  s->last_frame = av_frame_alloc();
64  if (!s->last_frame)
65  return AVERROR(ENOMEM);
66 
67  return 0;
68 }
69 
70 /**
71  * Unpack buffer
72  * @return 0 on success, -1 on critical buffer underflow
73  */
74 static int unpack(const uint8_t *src, const uint8_t *src_end,
75  uint8_t *dst, int width, int height)
76 {
77  uint8_t *dst_end = dst + width*height;
78  int size, size1, size2, offset, run;
79  uint8_t *dst_start = dst;
80 
81  if (src[0] & 0x01)
82  src += 5;
83  else
84  src += 2;
85 
86  if (src_end - src < 3)
87  return AVERROR_INVALIDDATA;
88  size = AV_RB24(src);
89  src += 3;
90 
91  while (size > 0 && src < src_end) {
92 
93  /* determine size1 and size2 */
94  size1 = (src[0] & 3);
95  if (src[0] & 0x80) { // 1
96  if (src[0] & 0x40 ) { // 11
97  if (src[0] & 0x20) { // 111
98  if (src[0] < 0xFC) // !(111111)
99  size1 = (((src[0] & 31) + 1) << 2);
100  src++;
101  size2 = 0;
102  } else { // 110
103  offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
104  size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
105  src += 4;
106  }
107  } else { // 10
108  size1 = ((src[1] & 0xC0) >> 6);
109  offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
110  size2 = (src[0] & 0x3F) + 4;
111  src += 3;
112  }
113  } else { // 0
114  offset = ((src[0] & 0x60) << 3) + src[1] + 1;
115  size2 = ((src[0] & 0x1C) >> 2) + 3;
116  src += 2;
117  }
118 
119 
120  /* fetch strip from src */
121  if (size1 > src_end - src)
122  break;
123 
124  if (size1 > 0) {
125  size -= size1;
126  run = FFMIN(size1, dst_end - dst);
127  memcpy(dst, src, run);
128  dst += run;
129  src += run;
130  }
131 
132  if (size2 > 0) {
133  if (dst - dst_start < offset)
134  return 0;
135  size -= size2;
136  run = FFMIN(size2, dst_end - dst);
138  dst += run;
139  }
140  }
141 
142  return 0;
143 }
144 
145 /**
146  * Decode inter-frame
147  * @return 0 on success, -1 on critical buffer underflow
148  */
150  const uint8_t *buf, const uint8_t *buf_end)
151 {
152  int num_mvs;
153  int num_blocks_raw;
154  int num_blocks_packed;
155  int vector_bits;
156  int i,j,x,y;
157  GetBitContext gb;
158  int mvbits;
159  const uint8_t *blocks_raw;
160 
161  if(buf_end - buf < 12)
162  return AVERROR_INVALIDDATA;
163 
164  num_mvs = AV_RL16(&buf[0]);
165  num_blocks_raw = AV_RL16(&buf[2]);
166  num_blocks_packed = AV_RL16(&buf[4]);
167  vector_bits = AV_RL16(&buf[6]);
168  buf += 12;
169 
170  if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
171  av_log(s->avctx, AV_LOG_ERROR,
172  "Invalid value for motion vector bits: %d\n", vector_bits);
173  return AVERROR_INVALIDDATA;
174  }
175 
176  /* allocate codebook buffers as necessary */
177  if (num_mvs > s->num_mvs) {
178  int err = av_reallocp_array(&s->mv_codebook, num_mvs, sizeof(*s->mv_codebook));
179  if (err < 0) {
180  s->num_mvs = 0;
181  return err;
182  }
183  s->num_mvs = num_mvs;
184  }
185 
186  if (num_blocks_packed > s->num_blocks_packed) {
187  int err;
188  if ((err = av_reallocp(&s->block_codebook, num_blocks_packed * 16)) < 0) {
189  s->num_blocks_packed = 0;
190  return err;
191  }
192  s->num_blocks_packed = num_blocks_packed;
193  }
194 
195  /* read motion vectors */
196  mvbits = (num_mvs * 2 * 10 + 31) & ~31;
197 
198  if (buf_end - buf < (mvbits>>3) + 16*num_blocks_raw + 8*num_blocks_packed)
199  return AVERROR_INVALIDDATA;
200 
201  init_get_bits(&gb, buf, mvbits);
202  for (i = 0; i < num_mvs; i++) {
203  s->mv_codebook[i][0] = get_sbits(&gb, 10);
204  s->mv_codebook[i][1] = get_sbits(&gb, 10);
205  }
206  buf += mvbits >> 3;
207 
208  /* note ptr to uncompressed blocks */
209  blocks_raw = buf;
210  buf += num_blocks_raw * 16;
211 
212  /* read compressed blocks */
213  init_get_bits(&gb, buf, (buf_end - buf) << 3);
214  for (i = 0; i < num_blocks_packed; i++) {
215  int tmp[4];
216  for (j = 0; j < 4; j++)
217  tmp[j] = get_bits(&gb, 8);
218  for (j = 0; j < 16; j++)
219  s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
220  }
221 
222  if (get_bits_left(&gb) < vector_bits *
223  (s->avctx->height / 4) * (s->avctx->width / 4))
224  return AVERROR_INVALIDDATA;
225 
226  /* read vectors and build frame */
227  for (y = 0; y < s->avctx->height / 4; y++)
228  for (x = 0; x < s->avctx->width / 4; x++) {
229  unsigned int vector = get_bits(&gb, vector_bits);
230  const uint8_t *src;
231  ptrdiff_t src_stride;
232 
233  if (vector < num_mvs) {
234  int mx = x * 4 + s->mv_codebook[vector][0];
235  int my = y * 4 + s->mv_codebook[vector][1];
236 
237  if (mx < 0 || mx + 4 > s->avctx->width ||
238  my < 0 || my + 4 > s->avctx->height) {
239  av_log(s->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);
240  continue;
241  }
242 
243  src = s->last_frame->data[0] + mx + my * s->last_frame->linesize[0];
244  src_stride = s->last_frame->linesize[0];
245  } else {
246  int offset = vector - num_mvs;
247  if (offset < num_blocks_raw)
248  src = blocks_raw + 16*offset;
249  else if (offset - num_blocks_raw < num_blocks_packed)
250  src = s->block_codebook[offset - num_blocks_raw];
251  else
252  continue;
253  src_stride = 4;
254  }
255 
256  for (j = 0; j < 4; j++)
257  for (i = 0; i < 4; i++)
258  frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
259  src[j * src_stride + i];
260  }
261 
262  return 0;
263 }
264 
266  int *got_frame, AVPacket *avpkt)
267 {
268  const uint8_t *buf = avpkt->data;
269  int buf_size = avpkt->size;
270  TgvContext *s = avctx->priv_data;
271  const uint8_t *buf_end = buf + buf_size;
272  int chunk_type, ret;
273 
274  if (buf_end - buf < EA_PREAMBLE_SIZE)
275  return AVERROR_INVALIDDATA;
276 
277  chunk_type = AV_RL32(&buf[0]);
278  buf += EA_PREAMBLE_SIZE;
279 
280  if (chunk_type == kVGT_TAG) {
281  int pal_count, i;
282  if(buf_end - buf < 12) {
283  av_log(avctx, AV_LOG_WARNING, "truncated header\n");
284  return AVERROR_INVALIDDATA;
285  }
286 
287  s->width = AV_RL16(&buf[0]);
288  s->height = AV_RL16(&buf[2]);
289  if (s->avctx->width != s->width || s->avctx->height != s->height) {
290  av_freep(&s->frame_buffer);
291  av_frame_unref(s->last_frame);
292  if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
293  return ret;
294  }
295 
296  pal_count = AV_RL16(&buf[6]);
297  buf += 12;
298  for(i = 0; i < pal_count && i < AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
299  s->palette[i] = 0xFFU << 24 | AV_RB24(buf);
300  buf += 3;
301  }
302  if (buf_end - buf < 5) {
303  return AVERROR_INVALIDDATA;
304  }
305  }
306 
307  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
308  return ret;
309 
310  memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
311 
312  if (chunk_type == kVGT_TAG) {
313  int y;
314  frame->key_frame = 1;
315  frame->pict_type = AV_PICTURE_TYPE_I;
316 
317  if (!s->frame_buffer &&
318  !(s->frame_buffer = av_mallocz(s->width * s->height)))
319  return AVERROR(ENOMEM);
320 
321  if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
322  av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
323  return AVERROR_INVALIDDATA;
324  }
325  for (y = 0; y < s->height; y++)
326  memcpy(frame->data[0] + y * frame->linesize[0],
327  s->frame_buffer + y * s->width,
328  s->width);
329  } else {
330  if (!s->last_frame->data[0]) {
331  av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
332  return buf_size;
333  }
334  frame->key_frame = 0;
335  frame->pict_type = AV_PICTURE_TYPE_P;
336  if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
337  av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
338  return AVERROR_INVALIDDATA;
339  }
340  }
341 
342  av_frame_unref(s->last_frame);
343  if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
344  return ret;
345 
346  *got_frame = 1;
347 
348  return buf_size;
349 }
350 
352 {
353  TgvContext *s = avctx->priv_data;
354  av_frame_free(&s->last_frame);
355  av_freep(&s->frame_buffer);
356  av_freep(&s->mv_codebook);
357  av_freep(&s->block_codebook);
358  return 0;
359 }
360 
362  .p.name = "eatgv",
363  .p.long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
364  .p.type = AVMEDIA_TYPE_VIDEO,
365  .p.id = AV_CODEC_ID_TGV,
366  .priv_data_size = sizeof(TgvContext),
368  .close = tgv_decode_end,
370  .p.capabilities = AV_CODEC_CAP_DR1,
371 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
tgv_decode_init
static av_cold int tgv_decode_init(AVCodecContext *avctx)
Definition: eatgv.c:56
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:839
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
num_mvs
static const int num_mvs[RV34_MB_TYPES]
number of motion vectors in each macroblock type
Definition: rv34.c:847
TgvContext::height
int height
Definition: eatgv.c:47
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:116
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:118
TgvContext::num_blocks_packed
int num_blocks_packed
current length of block_codebook
Definition: eatgv.c:53
ff_eatgv_decoder
const FFCodec ff_eatgv_decoder
Definition: eatgv.c:361
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:649
TgvContext::avctx
AVCodecContext * avctx
Definition: eatgv.c:44
init
static int init
Definition: av_tx.c:47
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1741
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
TgvContext::last_frame
AVFrame * last_frame
Definition: eatgv.c:45
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:122
GetBitContext
Definition: get_bits.h:61
tgv_decode_frame
static int tgv_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: eatgv.c:265
TgvContext::width
int width
Definition: eatgv.c:47
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:104
TgvContext::num_mvs
int num_mvs
current length of mv_codebook
Definition: eatgv.c:52
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
av_memcpy_backptr
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:455
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:260
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:376
tgv_decode_inter
static int tgv_decode_inter(TgvContext *s, AVFrame *frame, const uint8_t *buf, const uint8_t *buf_end)
Decode inter-frame.
Definition: eatgv.c:149
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:359
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
TgvContext::block_codebook
uint8_t(* block_codebook)[16]
Definition: eatgv.c:51
run
uint8_t run
Definition: svq3.c:205
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
AVPALETTE_COUNT
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
kVGT_TAG
#define kVGT_TAG
Definition: eatgv.c:41
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1462
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
av_frame_ref
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:353
codec_internal.h
size
int size
Definition: twinvq_data.h:10344
av_reallocp
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:186
TgvContext::palette
uint32_t palette[AVPALETTE_COUNT]
Definition: eatgv.c:48
EA_PREAMBLE_SIZE
#define EA_PREAMBLE_SIZE
Definition: eatgv.c:40
height
#define height
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:233
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
TgvContext::frame_buffer
uint8_t * frame_buffer
Definition: eatgv.c:46
AV_CODEC_ID_TGV
@ AV_CODEC_ID_TGV
Definition: codec_id.h:170
unpack
static int unpack(const uint8_t *src, const uint8_t *src_end, uint8_t *dst, int width, int height)
Unpack buffer.
Definition: eatgv.c:74
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
MIN_CACHE_BITS
#define MIN_CACHE_BITS
Definition: get_bits.h:128
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:487
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:264
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:608
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:398
tgv_decode_end
static av_cold int tgv_decode_end(AVCodecContext *avctx)
Definition: eatgv.c:351
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
ff_set_dimensions
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:90
TgvContext::mv_codebook
int(* mv_codebook)[2]
Definition: eatgv.c:50
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
imgutils.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97
int
int
Definition: ffmpeg_filter.c:155
TgvContext
Definition: eatgv.c:43
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98