annotate eatgv.c @ 12403:62e41fdef734 libavcodec

enabling codec and muxer by registering it in allcodec.c and allformat.c and adding files to the build-system
author bindhammer
date Mon, 23 Aug 2010 11:52:34 +0000
parents 914f484bb476
children ffb3668ff7af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
1 /*
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
2 * Electronic Arts TGV Video Decoder
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
3 * Copyright (c) 2007-2008 Peter Ross
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
4 *
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
5 * This file is part of FFmpeg.
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
6 *
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
11 *
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
15 * Lesser General Public License for more details.
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
16 *
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
20 */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
21
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
22 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11581
diff changeset
23 * @file
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
24 * Electronic Arts TGV Video Decoder
10825
dff431441c9c update my email address
pross
parents: 9428
diff changeset
25 * by Peter Ross (pross@xvid.org)
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
26 *
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
27 * Technical details here:
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
28 * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGV
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
29 */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
30
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
31 #include "avcodec.h"
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
32 #define ALT_BITSTREAM_READER_LE
9428
0dce4fe6e6f3 Rename bitstream.h to get_bits.h.
stefano
parents: 9355
diff changeset
33 #include "get_bits.h"
8597
573d2361d2ec libavutil/lzo.h is a local header
aurel
parents: 7510
diff changeset
34 #include "libavutil/lzo.h"
12372
914f484bb476 Remove use of the deprecated function avcodec_check_dimensions(), use
stefano
parents: 11644
diff changeset
35 #include "libavcore/imgutils.h"
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
36
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
37 #define EA_PREAMBLE_SIZE 8
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
38 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
39
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
40 typedef struct TgvContext {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
41 AVCodecContext *avctx;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
42 AVFrame frame;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
43 AVFrame last_frame;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
44 int width,height;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
45 unsigned int palette[AVPALETTE_COUNT];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
46
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
47 int (*mv_codebook)[2];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
48 unsigned char (*block_codebook)[16];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
49 int num_mvs; ///< current length of mv_codebook
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
50 int num_blocks_packed; ///< current length of block_codebook
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
51 } TgvContext;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
52
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
53 static av_cold int tgv_decode_init(AVCodecContext *avctx){
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
54 TgvContext *s = avctx->priv_data;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
55 s->avctx = avctx;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
56 avctx->time_base = (AVRational){1, 15};
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
57 avctx->pix_fmt = PIX_FMT_PAL8;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
58 return 0;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
59 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
60
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
61 /**
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
62 * Unpack buffer
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
63 * @return 0 on success, -1 on critical buffer underflow
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
64 */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
65 static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
66 unsigned char *dst_end = dst + width*height;
8801
154d81241ac0 Add av_uninit macro to variable to avoid false positive warning:
diego
parents: 8718
diff changeset
67 int size, size1, size2, av_uninit(offset), run;
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
68 unsigned char *dst_start = dst;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
69
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
70 if (src[0] & 0x01)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
71 src += 5;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
72 else
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
73 src += 2;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
74
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
75 if (src+3>src_end)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
76 return -1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
77 size = AV_RB24(src);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
78 src += 3;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
79
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
80 while(size>0 && src<src_end) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
81
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
82 /* determine size1 and size2 */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
83 size1 = (src[0] & 3);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
84 if ( src[0] & 0x80 ) { // 1
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
85 if (src[0] & 0x40 ) { // 11
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
86 if ( src[0] & 0x20 ) { // 111
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
87 if ( src[0] < 0xFC ) // !(111111)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
88 size1 = (((src[0] & 31) + 1) << 2);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
89 src++;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
90 size2 = 0;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
91 } else { // 110
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
92 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
93 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
94 src += 4;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
95 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
96 } else { // 10
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
97 size1 = ( ( src[1] & 0xC0) >> 6 );
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
98 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
99 size2 = (src[0] & 0x3F) + 4;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
100 src += 3;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
101 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
102 } else { // 0
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
103 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
104 size2 = ((src[0] & 0x1C) >> 2) + 3;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
105 src += 2;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
106 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
107
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
108
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
109 /* fetch strip from src */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
110 if (size1>src_end-src)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
111 break;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
112
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
113 if (size1>0) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
114 size -= size1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
115 run = FFMIN(size1, dst_end-dst);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
116 memcpy(dst, src, run);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
117 dst += run;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
118 src += run;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
119 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
120
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
121 if (size2>0) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
122 if (dst-dst_start<offset)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
123 return 0;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
124 size -= size2;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
125 run = FFMIN(size2, dst_end-dst);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
126 av_memcpy_backptr(dst, offset, run);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
127 dst += run;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
128 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
129 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
130
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
131 return 0;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
132 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
133
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
134 /**
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
135 * Decode inter-frame
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
136 * @return 0 on success, -1 on critical buffer underflow
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
137 */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
138 static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
139 unsigned char *frame0_end = s->last_frame.data[0] + s->avctx->width*s->last_frame.linesize[0];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
140 int num_mvs;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
141 int num_blocks_raw;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
142 int num_blocks_packed;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
143 int vector_bits;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
144 int i,j,x,y;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
145 GetBitContext gb;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
146 int mvbits;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
147 const unsigned char *blocks_raw;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
148
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
149 if(buf+12>buf_end)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
150 return -1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
151
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
152 num_mvs = AV_RL16(&buf[0]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
153 num_blocks_raw = AV_RL16(&buf[2]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
154 num_blocks_packed = AV_RL16(&buf[4]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
155 vector_bits = AV_RL16(&buf[6]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
156 buf += 12;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
157
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
158 /* allocate codebook buffers as neccessary */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
159 if (num_mvs > s->num_mvs) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
160 s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
161 s->num_mvs = num_mvs;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
162 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
163
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
164 if (num_blocks_packed > s->num_blocks_packed) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
165 s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
166 s->num_blocks_packed = num_blocks_packed;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
167 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
168
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
169 /* read motion vectors */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
170 mvbits = (num_mvs*2*10+31) & ~31;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
171
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
172 if (buf+(mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed>buf_end)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
173 return -1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
174
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
175 init_get_bits(&gb, buf, mvbits);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
176 for (i=0; i<num_mvs; i++) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
177 s->mv_codebook[i][0] = get_sbits(&gb, 10);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
178 s->mv_codebook[i][1] = get_sbits(&gb, 10);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
179 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
180 buf += mvbits>>3;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
181
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
182 /* note ptr to uncompressed blocks */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
183 blocks_raw = buf;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
184 buf += num_blocks_raw*16;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
185
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
186 /* read compressed blocks */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
187 init_get_bits(&gb, buf, (buf_end-buf)<<3);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
188 for (i=0; i<num_blocks_packed; i++) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
189 int tmp[4];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
190 for(j=0; j<4; j++)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
191 tmp[j] = get_bits(&gb, 8);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
192 for(j=0; j<16; j++)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
193 s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
194 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
195
11303
b0cb1f7efcbc Do not read beyond end of input in EA-TGV. This should avoid FATE test #362
vitor
parents: 10825
diff changeset
196 if (get_bits_left(&gb) < vector_bits *
b0cb1f7efcbc Do not read beyond end of input in EA-TGV. This should avoid FATE test #362
vitor
parents: 10825
diff changeset
197 (s->avctx->height/4) * (s->avctx->width/4))
b0cb1f7efcbc Do not read beyond end of input in EA-TGV. This should avoid FATE test #362
vitor
parents: 10825
diff changeset
198 return -1;
b0cb1f7efcbc Do not read beyond end of input in EA-TGV. This should avoid FATE test #362
vitor
parents: 10825
diff changeset
199
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
200 /* read vectors and build frame */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
201 for(y=0; y<s->avctx->height/4; y++)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
202 for(x=0; x<s->avctx->width/4; x++) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
203 unsigned int vector = get_bits(&gb, vector_bits);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
204 const unsigned char *src;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
205 int src_stride;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
206
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
207 if (vector < num_mvs) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
208 src = s->last_frame.data[0] +
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
209 (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
210 x*4 + s->mv_codebook[vector][0];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
211 src_stride = s->last_frame.linesize[0];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
212 if (src+3*src_stride+3>=frame0_end)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
213 continue;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
214 }else{
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
215 int offset = vector - num_mvs;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
216 if (offset<num_blocks_raw)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
217 src = blocks_raw + 16*offset;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
218 else if (offset-num_blocks_raw<num_blocks_packed)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
219 src = s->block_codebook[offset-num_blocks_raw];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
220 else
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
221 continue;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
222 src_stride = 4;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
223 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
224
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
225 for(j=0; j<4; j++)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
226 for(i=0; i<4; i++)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
227 s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
228 src[j*src_stride + i];
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
229 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
230
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
231 return 0;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
232 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
233
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
234 /** release AVFrame buffers if allocated */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
235 static void cond_release_buffer(AVFrame *pic)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
236 {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
237 if (pic->data[0]) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
238 av_freep(&pic->data[0]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
239 av_free(pic->data[1]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
240 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
241 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
242
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
243 static int tgv_decode_frame(AVCodecContext *avctx,
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
244 void *data, int *data_size,
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9083
diff changeset
245 AVPacket *avpkt)
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
246 {
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9083
diff changeset
247 const uint8_t *buf = avpkt->data;
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9083
diff changeset
248 int buf_size = avpkt->size;
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
249 TgvContext *s = avctx->priv_data;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
250 const uint8_t *buf_end = buf + buf_size;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
251 int chunk_type;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
252
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
253 chunk_type = AV_RL32(&buf[0]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
254 buf += EA_PREAMBLE_SIZE;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
255
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
256 if (chunk_type==kVGT_TAG) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
257 int pal_count, i;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
258 if(buf+12>buf_end) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
259 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
260 return -1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
261 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
262
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
263 s->width = AV_RL16(&buf[0]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
264 s->height = AV_RL16(&buf[2]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
265 if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
266 avcodec_set_dimensions(s->avctx, s->width, s->height);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
267 cond_release_buffer(&s->frame);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
268 cond_release_buffer(&s->last_frame);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
269 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
270
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
271 pal_count = AV_RL16(&buf[6]);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
272 buf += 12;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
273 for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
274 s->palette[i] = AV_RB24(buf);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
275 buf += 3;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
276 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
277 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
278
12372
914f484bb476 Remove use of the deprecated function avcodec_check_dimensions(), use
stefano
parents: 11644
diff changeset
279 if (av_check_image_size(s->width, s->height, 0, avctx))
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
280 return -1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
281
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
282 /* shuffle */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
283 FFSWAP(AVFrame, s->frame, s->last_frame);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
284 if (!s->frame.data[0]) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
285 s->frame.reference = 1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
286 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
287 s->frame.linesize[0] = s->width;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
288
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
289 /* allocate additional 12 bytes to accomodate av_memcpy_backptr() OUTBUF_PADDED optimisation */
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
290 s->frame.data[0] = av_malloc(s->width*s->height + 12);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
291 if (!s->frame.data[0])
11581
998691d7f8f5 Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents: 11560
diff changeset
292 return AVERROR(ENOMEM);
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
293 s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
294 if (!s->frame.data[1]) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
295 av_freep(&s->frame.data[0]);
11581
998691d7f8f5 Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents: 11560
diff changeset
296 return AVERROR(ENOMEM);
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
297 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
298 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
299 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
300
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
301 if(chunk_type==kVGT_TAG) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
302 s->frame.key_frame = 1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
303 s->frame.pict_type = FF_I_TYPE;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
304 if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
305 av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
306 return -1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
307 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
308 }else{
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
309 if (!s->last_frame.data[0]) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
310 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
311 return buf_size;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
312 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
313 s->frame.key_frame = 0;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
314 s->frame.pict_type = FF_P_TYPE;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
315 if (tgv_decode_inter(s, buf, buf_end)<0) {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
316 av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
317 return -1;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
318 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
319 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
320
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
321 *data_size = sizeof(AVFrame);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
322 *(AVFrame*)data = s->frame;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
323
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
324 return buf_size;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
325 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
326
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
327 static av_cold int tgv_decode_end(AVCodecContext *avctx)
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
328 {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
329 TgvContext *s = avctx->priv_data;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
330 cond_release_buffer(&s->frame);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
331 cond_release_buffer(&s->last_frame);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
332 av_free(s->mv_codebook);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
333 av_free(s->block_codebook);
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
334 return 0;
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
335 }
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
336
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
337 AVCodec eatgv_decoder = {
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
338 "eatgv",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 11303
diff changeset
339 AVMEDIA_TYPE_VIDEO,
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
340 CODEC_ID_TGV,
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
341 sizeof(TgvContext),
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
342 tgv_decode_init,
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
343 NULL,
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
344 tgv_decode_end,
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
345 tgv_decode_frame,
9083
bf274494b66e Change a bunch of codec long_names to be more consistent and descriptive.
diego
parents: 8801
diff changeset
346 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
7510
212e72dbb838 Electronic Arts TGV decoder
pross
parents:
diff changeset
347 };