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 "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;
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;
60 
62  if (!s->last_frame)
63  return AVERROR(ENOMEM);
64 
65  return 0;
66 }
67 
72 static int unpack(const uint8_t *src, const uint8_t *src_end,
73  uint8_t *dst, int width, int height)
74 {
75  uint8_t *dst_end = dst + width*height;
76  int size, size1, size2, offset, run;
77  uint8_t *dst_start = dst;
78 
79  if (src[0] & 0x01)
80  src += 5;
81  else
82  src += 2;
83 
84  if (src + 3 > src_end)
85  return AVERROR_INVALIDDATA;
86  size = AV_RB24(src);
87  src += 3;
88 
89  while (size > 0 && src < src_end) {
90 
91  /* determine size1 and size2 */
92  size1 = (src[0] & 3);
93  if (src[0] & 0x80) { // 1
94  if (src[0] & 0x40 ) { // 11
95  if (src[0] & 0x20) { // 111
96  if (src[0] < 0xFC) // !(111111)
97  size1 = (((src[0] & 31) + 1) << 2);
98  src++;
99  size2 = 0;
100  } else { // 110
101  offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
102  size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
103  src += 4;
104  }
105  } else { // 10
106  size1 = ((src[1] & 0xC0) >> 6);
107  offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
108  size2 = (src[0] & 0x3F) + 4;
109  src += 3;
110  }
111  } else { // 0
112  offset = ((src[0] & 0x60) << 3) + src[1] + 1;
113  size2 = ((src[0] & 0x1C) >> 2) + 3;
114  src += 2;
115  }
116 
117 
118  /* fetch strip from src */
119  if (size1 > src_end - src)
120  break;
121 
122  if (size1 > 0) {
123  size -= size1;
124  run = FFMIN(size1, dst_end - dst);
125  memcpy(dst, src, run);
126  dst += run;
127  src += run;
128  }
129 
130  if (size2 > 0) {
131  if (dst - dst_start < offset)
132  return 0;
133  size -= size2;
134  run = FFMIN(size2, dst_end - dst);
135  av_memcpy_backptr(dst, offset, run);
136  dst += run;
137  }
138  }
139 
140  return 0;
141 }
142 
147 static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
148  const uint8_t *buf, const uint8_t *buf_end)
149 {
150  int num_mvs;
151  int num_blocks_raw;
152  int num_blocks_packed;
153  int vector_bits;
154  int i,j,x,y;
155  GetBitContext gb;
156  int mvbits;
157  const uint8_t *blocks_raw;
158 
159  if (buf + 12 > buf_end)
160  return AVERROR_INVALIDDATA;
161 
162  num_mvs = AV_RL16(&buf[0]);
163  num_blocks_raw = AV_RL16(&buf[2]);
164  num_blocks_packed = AV_RL16(&buf[4]);
165  vector_bits = AV_RL16(&buf[6]);
166  buf += 12;
167 
168  if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
170  "Invalid value for motion vector bits: %d\n", vector_bits);
171  return AVERROR_INVALIDDATA;
172  }
173 
174  /* allocate codebook buffers as necessary */
175  if (num_mvs > s->num_mvs) {
176  s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
177  s->num_mvs = num_mvs;
178  }
179 
180  if (num_blocks_packed > s->num_blocks_packed) {
181  int err;
182  if ((err = av_reallocp(&s->block_codebook, num_blocks_packed * 16)) < 0) {
183  s->num_blocks_packed = 0;
184  return err;
185  }
187  }
188 
189  /* read motion vectors */
190  mvbits = (num_mvs * 2 * 10 + 31) & ~31;
191 
192  if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
193  return AVERROR_INVALIDDATA;
194 
195  init_get_bits(&gb, buf, mvbits);
196  for (i = 0; i < num_mvs; i++) {
197  s->mv_codebook[i][0] = get_sbits(&gb, 10);
198  s->mv_codebook[i][1] = get_sbits(&gb, 10);
199  }
200  buf += mvbits >> 3;
201 
202  /* note ptr to uncompressed blocks */
203  blocks_raw = buf;
204  buf += num_blocks_raw * 16;
205 
206  /* read compressed blocks */
207  init_get_bits(&gb, buf, (buf_end - buf) << 3);
208  for (i = 0; i < num_blocks_packed; i++) {
209  int tmp[4];
210  for (j = 0; j < 4; j++)
211  tmp[j] = get_bits(&gb, 8);
212  for (j = 0; j < 16; j++)
213  s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
214  }
215 
216  if (get_bits_left(&gb) < vector_bits *
217  (s->avctx->height / 4) * (s->avctx->width / 4))
218  return AVERROR_INVALIDDATA;
219 
220  /* read vectors and build frame */
221  for (y = 0; y < s->avctx->height / 4; y++)
222  for (x = 0; x < s->avctx->width / 4; x++) {
223  unsigned int vector = get_bits(&gb, vector_bits);
224  const uint8_t *src;
225  int src_stride;
226 
227  if (vector < num_mvs) {
228  int mx = x * 4 + s->mv_codebook[vector][0];
229  int my = y * 4 + s->mv_codebook[vector][1];
230 
231  if (mx < 0 || mx + 4 > s->avctx->width ||
232  my < 0 || my + 4 > s->avctx->height)
233  continue;
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  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 + 12 > buf_end) {
274  av_log(avctx, AV_LOG_WARNING, "truncated header\n");
275  return AVERROR_INVALIDDATA;
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) {
281  av_freep(&s->frame_buffer);
283  if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
284  return ret;
285  }
286 
287  pal_count = AV_RL16(&buf[6]);
288  buf += 12;
289  for (i = 0; i < pal_count && i < AVPALETTE_COUNT && buf + 2 < buf_end; i++) {
290  s->palette[i] = AV_RB24(buf);
291  buf += 3;
292  }
293  }
294 
295  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
296  return ret;
297 
298  memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
299 
300  if (chunk_type == kVGT_TAG) {
301  int y;
302  frame->key_frame = 1;
303  frame->pict_type = AV_PICTURE_TYPE_I;
304 
305  if (!s->frame_buffer &&
306  !(s->frame_buffer = av_malloc(s->width * s->height)))
307  return AVERROR(ENOMEM);
308 
309  if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
310  av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
311  return AVERROR_INVALIDDATA;
312  }
313  for (y = 0; y < s->height; y++)
314  memcpy(frame->data[0] + y * frame->linesize[0],
315  s->frame_buffer + y * s->width,
316  s->width);
317  } else {
318  if (!s->last_frame->data[0]) {
319  av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
320  return buf_size;
321  }
322  frame->key_frame = 0;
323  frame->pict_type = AV_PICTURE_TYPE_P;
324  if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
325  av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
326  return AVERROR_INVALIDDATA;
327  }
328  }
329 
331  if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
332  return ret;
333 
334  *got_frame = 1;
335 
336  return buf_size;
337 }
338 
340 {
341  TgvContext *s = avctx->priv_data;
343  av_freep(&s->frame_buffer);
344  av_free(s->mv_codebook);
346  return 0;
347 }
348 
350  .name = "eatgv",
351  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
352  .type = AVMEDIA_TYPE_VIDEO,
353  .id = AV_CODEC_ID_TGV,
354  .priv_data_size = sizeof(TgvContext),
358  .capabilities = CODEC_CAP_DR1,
359 };
#define AVPALETTE_SIZE
Definition: avcodec.h:3051
int height
Definition: eatgv.c:45
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:43
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int width
Definition: eatgv.c:45
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:133
int size
Definition: avcodec.h:974
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
#define AV_RL16
Definition: intreadwrite.h:42
#define AVPALETTE_COUNT
Definition: avcodec.h:3052
uint8_t run
Definition: svq3.c:146
AVCodec.
Definition: avcodec.h:2812
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:226
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1175
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:198
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
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:57
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
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:188
#define kVGT_TAG
Definition: eatgv.c:39
AVCodecContext * avctx
Definition: eatgv.c:42
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
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:48
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:555
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#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:339
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
int num_mvs
current length of mv_codebook
Definition: eatgv.c:50
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
uint32_t palette[AVPALETTE_COUNT]
Definition: eatgv.c:46
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
#define FFMIN(a, b)
Definition: common.h:57
uint8_t(* block_codebook)[16]
Definition: eatgv.c:49
int width
picture width / height.
Definition: avcodec.h:1229
#define AV_RL32
Definition: intreadwrite.h:146
AVCodec ff_eatgv_decoder
Definition: eatgv.c:349
uint8_t * frame_buffer
Definition: eatgv.c:44
int num_blocks_packed
current length of block_codebook
Definition: eatgv.c:51
#define EA_PREAMBLE_SIZE
Definition: eatgv.c:38
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
static int tgv_decode_inter(TgvContext *s, AVFrame *frame, const uint8_t *buf, const uint8_t *buf_end)
Decode inter-frame.
Definition: eatgv.c:147
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:375
static av_cold int tgv_decode_init(AVCodecContext *avctx)
Definition: eatgv.c:54
#define MIN_CACHE_BITS
Definition: get_bits.h:123
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:283
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
static int unpack(const uint8_t *src, const uint8_t *src_end, uint8_t *dst, int width, int height)
Unpack buffer.
Definition: eatgv.c:72
common internal api header.
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:319
static int tgv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: eatgv.c:257
This structure stores compressed data.
Definition: avcodec.h:950
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:850
Predicted.
Definition: avutil.h:254