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
32
33 #define BITSTREAM_READER_LE
38
39 #define EA_PREAMBLE_SIZE 8
40 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
41
48
51 int num_mvs;
///< current length of mv_codebook
54
56 {
61
65
66 return 0;
67 }
68
69 /**
70 * Unpack buffer
71 * @return 0 on success, -1 on critical buffer underflow
72 */
73 static int unpack(
const uint8_t *
src,
const uint8_t *src_end,
75 {
78 uint8_t *dst_start =
dst;
79
82 else
84
85 if (src_end -
src < 3)
89
90 while (
size > 0 &&
src < src_end) {
91
92 /* determine size1 and size2 */
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);
100 size2 = 0;
101 } else { // 110
103 size2 = ((
src[0] & 0xC) << 6) +
src[3] + 5;
105 }
106 } else { // 10
107 size1 = ((
src[1] & 0xC0) >> 6);
109 size2 = (
src[0] & 0x3F) + 4;
111 }
112 } else { // 0
114 size2 = ((
src[0] & 0x1C) >> 2) + 3;
116 }
117
118
119 /* fetch strip from src */
120 if (size1 > src_end -
src)
121 break;
122
123 if (size1 > 0) {
129 }
130
131 if (size2 > 0) {
133 return 0;
138 }
139 }
140
141 return 0;
142 }
143
144 /**
145 * Decode inter-frame
146 * @return 0 on success, -1 on critical buffer underflow
147 */
149 const uint8_t *buf, const uint8_t *buf_end)
150 {
152 int num_blocks_raw;
153 int num_blocks_packed;
154 int vector_bits;
157 int mvbits;
158 const uint8_t *blocks_raw;
159
160 if(buf_end - buf < 12)
162
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
171 "Invalid value for motion vector bits: %d\n", vector_bits);
173 }
174
175 /* allocate codebook buffers as necessary */
178 if (err < 0) {
180 return err;
181 }
183 }
184
185 if (num_blocks_packed >
s->num_blocks_packed) {
186 int err;
187 if ((err =
av_reallocp(&
s->block_codebook, num_blocks_packed * 16)) < 0) {
188 s->num_blocks_packed = 0;
189 return err;
190 }
191 s->num_blocks_packed = num_blocks_packed;
192 }
193
194 /* read motion vectors */
195 mvbits = (
num_mvs * 2 * 10 + 31) & ~31;
196
197 if (buf_end - buf < (mvbits>>3) + 16*num_blocks_raw + 8*num_blocks_packed)
199
204 }
205 buf += mvbits >> 3;
206
207 /* note ptr to uncompressed blocks */
208 blocks_raw = buf;
209 buf += num_blocks_raw * 16;
210
211 /* read compressed blocks */
213 for (
i = 0;
i < num_blocks_packed;
i++) {
215 for (j = 0; j < 4; j++)
217 for (j = 0; j < 16; j++)
219 }
220
222 (
s->avctx->height / 4) * (
s->avctx->width / 4))
224
225 /* read vectors and build frame */
226 for (y = 0; y <
s->avctx->height / 4; y++)
227 for (x = 0; x <
s->avctx->width / 4; x++) {
228 unsigned int vector =
get_bits(&gb, vector_bits);
230 ptrdiff_t src_stride;
231
233 int mx = x * 4 +
s->mv_codebook[vector][0];
234 int my = y * 4 +
s->mv_codebook[vector][1];
235
236 if (mx < 0 || mx + 4 >
s->avctx->width ||
237 my < 0 || my + 4 >
s->avctx->height) {
239 continue;
240 }
241
242 src =
s->last_frame->data[0] +
mx +
my *
s->last_frame->linesize[0];
243 src_stride =
s->last_frame->linesize[0];
244 } else {
246 if (
offset < num_blocks_raw)
248 else if (
offset - num_blocks_raw < num_blocks_packed)
249 src =
s->block_codebook[
offset - num_blocks_raw];
250 else
251 continue;
252 src_stride = 4;
253 }
254
255 for (j = 0; j < 4; j++)
256 for (
i = 0;
i < 4;
i++)
257 frame->data[0][(y * 4 + j) *
frame->linesize[0] + (x * 4 +
i)] =
258 src[j * src_stride +
i];
259 }
260
261 return 0;
262 }
263
266 {
267 const uint8_t *buf = avpkt->
data;
268 int buf_size = avpkt->
size;
270 const uint8_t *buf_end = buf + buf_size;
272
275
278
281 if(buf_end - buf < 12) {
284 }
285
288 if (
s->avctx->width !=
s->width ||
s->avctx->height !=
s->height) {
293 }
294
296 buf += 12;
299 buf += 3;
300 }
301 if (buf_end - buf < 5) {
303 }
304 }
305
308
310
312 int y;
315
316 if (!
s->frame_buffer &&
319
320 if (
unpack(buf, buf_end,
s->frame_buffer,
s->avctx->width,
s->avctx->height) < 0) {
323 }
324 for (y = 0; y <
s->height; y++)
325 memcpy(
frame->data[0] + y *
frame->linesize[0],
326 s->frame_buffer + y *
s->width,
328 } else {
329 if (!
s->last_frame->data[0]) {
331 return buf_size;
332 }
338 }
339 }
340
343
344 *got_frame = 1;
345
346 return buf_size;
347 }
348
350 {
356 return 0;
357 }
358
369 };