]> git.sesse.net Git - ffmpeg/blob - libavcodec/eatgv.c
lavc: Check CODEC_CAP_VARIABLE_FRAME_SIZE && !frame
[ffmpeg] / libavcodec / eatgv.c
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 "libavutil/lzo.h"
35 #include "libavutil/imgutils.h"
36
37 #define EA_PREAMBLE_SIZE    8
38 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
39
40 typedef struct TgvContext {
41     AVCodecContext *avctx;
42     AVFrame frame;
43     AVFrame last_frame;
44     int width,height;
45     unsigned int palette[AVPALETTE_COUNT];
46
47     int (*mv_codebook)[2];
48     unsigned char (*block_codebook)[16];
49     int num_mvs;           ///< current length of mv_codebook
50     int num_blocks_packed; ///< current length of block_codebook
51 } TgvContext;
52
53 static av_cold int tgv_decode_init(AVCodecContext *avctx){
54     TgvContext *s = avctx->priv_data;
55     s->avctx = avctx;
56     avctx->time_base = (AVRational){1, 15};
57     avctx->pix_fmt = PIX_FMT_PAL8;
58     avcodec_get_frame_defaults(&s->frame);
59     avcodec_get_frame_defaults(&s->last_frame);
60     return 0;
61 }
62
63 /**
64  * Unpack buffer
65  * @return 0 on success, -1 on critical buffer underflow
66  */
67 static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) {
68     unsigned char *dst_end = dst + width*height;
69     int size, size1, size2, av_uninit(offset), run;
70     unsigned char *dst_start = dst;
71
72     if (src[0] & 0x01)
73         src += 5;
74     else
75         src += 2;
76
77     if (src_end - src < 3)
78         return -1;
79     size = AV_RB24(src);
80     src += 3;
81
82     while(size>0 && src<src_end) {
83
84         /* determine size1 and size2 */
85         size1 = (src[0] & 3);
86         if ( src[0] & 0x80 ) {  // 1
87             if (src[0] & 0x40 ) {  // 11
88                 if ( src[0] & 0x20 ) {  // 111
89                     if ( src[0] < 0xFC )  // !(111111)
90                         size1 = (((src[0] & 31) + 1) << 2);
91                     src++;
92                     size2 = 0;
93                 } else {  // 110
94                     offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
95                     size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
96                     src += 4;
97                 }
98             } else {  // 10
99                 size1 = ( ( src[1] & 0xC0) >> 6 );
100                 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
101                 size2 = (src[0] & 0x3F) + 4;
102                 src += 3;
103             }
104         } else {  // 0
105             offset = ((src[0] & 0x60) << 3) + src[1] + 1;
106             size2 = ((src[0] & 0x1C) >> 2) + 3;
107             src += 2;
108         }
109
110
111         /* fetch strip from src */
112         if (size1>src_end-src)
113             break;
114
115         if (size1>0) {
116             size -= size1;
117             run = FFMIN(size1, dst_end-dst);
118             memcpy(dst, src, run);
119             dst += run;
120             src += run;
121         }
122
123         if (size2>0) {
124             if (dst-dst_start<offset)
125                 return 0;
126             size -= size2;
127             run = FFMIN(size2, dst_end-dst);
128             av_memcpy_backptr(dst, offset, run);
129             dst += run;
130         }
131     }
132
133     return 0;
134 }
135
136 /**
137  * Decode inter-frame
138  * @return 0 on success, -1 on critical buffer underflow
139  */
140 static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){
141     unsigned last_frame_size = s->avctx->height*s->last_frame.linesize[0];
142     int num_mvs;
143     int num_blocks_raw;
144     int num_blocks_packed;
145     int vector_bits;
146     int i,j,x,y;
147     GetBitContext gb;
148     int mvbits;
149     const unsigned char *blocks_raw;
150
151     if(buf_end - buf < 12)
152         return -1;
153
154     num_mvs           = AV_RL16(&buf[0]);
155     num_blocks_raw    = AV_RL16(&buf[2]);
156     num_blocks_packed = AV_RL16(&buf[4]);
157     vector_bits       = AV_RL16(&buf[6]);
158     buf += 12;
159
160     /* allocate codebook buffers as necessary */
161     if (num_mvs > s->num_mvs) {
162         s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
163         s->num_mvs = num_mvs;
164     }
165
166     if (num_blocks_packed > s->num_blocks_packed) {
167         s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
168         s->num_blocks_packed = num_blocks_packed;
169     }
170
171     /* read motion vectors */
172     mvbits = (num_mvs*2*10+31) & ~31;
173
174     if (buf_end - buf < (mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed)
175         return -1;
176
177     init_get_bits(&gb, buf, mvbits);
178     for (i=0; i<num_mvs; i++) {
179         s->mv_codebook[i][0] = get_sbits(&gb, 10);
180         s->mv_codebook[i][1] = get_sbits(&gb, 10);
181     }
182     buf += mvbits>>3;
183
184     /* note ptr to uncompressed blocks */
185     blocks_raw = buf;
186     buf += num_blocks_raw*16;
187
188     /* read compressed blocks */
189     init_get_bits(&gb, buf, (buf_end-buf)<<3);
190     for (i=0; i<num_blocks_packed; i++) {
191         int tmp[4];
192         for(j=0; j<4; j++)
193             tmp[j] = get_bits(&gb, 8);
194         for(j=0; j<16; j++)
195             s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
196     }
197
198     if (get_bits_left(&gb) < vector_bits *
199         (s->avctx->height/4) * (s->avctx->width/4))
200         return -1;
201
202     /* read vectors and build frame */
203     for(y=0; y<s->avctx->height/4; y++)
204     for(x=0; x<s->avctx->width/4; x++) {
205         unsigned int vector = get_bits(&gb, vector_bits);
206         const unsigned char *src;
207         int src_stride;
208
209         if (vector < num_mvs) {
210             unsigned offset =
211                 (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
212                  x*4 + s->mv_codebook[vector][0];
213
214             src_stride = s->last_frame.linesize[0];
215             if (offset >= last_frame_size - (3*src_stride+3))
216                 continue;
217             src = s->last_frame.data[0] + offset;
218         }else{
219             int offset = vector - num_mvs;
220             if (offset<num_blocks_raw)
221                 src = blocks_raw + 16*offset;
222             else if (offset-num_blocks_raw<num_blocks_packed)
223                 src = s->block_codebook[offset-num_blocks_raw];
224             else
225                 continue;
226             src_stride = 4;
227         }
228
229         for(j=0; j<4; j++)
230         for(i=0; i<4; i++)
231             s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i)  ] =
232                src[j*src_stride + i];
233     }
234
235     return 0;
236 }
237
238 /** release AVFrame buffers if allocated */
239 static void cond_release_buffer(AVFrame *pic)
240 {
241     if (pic->data[0]) {
242         av_freep(&pic->data[0]);
243         av_free(pic->data[1]);
244     }
245 }
246
247 static int tgv_decode_frame(AVCodecContext *avctx,
248                             void *data, int *data_size,
249                             AVPacket *avpkt)
250 {
251     const uint8_t *buf = avpkt->data;
252     int buf_size = avpkt->size;
253     TgvContext *s = avctx->priv_data;
254     const uint8_t *buf_end = buf + buf_size;
255     int chunk_type;
256
257     if (buf_end - buf < EA_PREAMBLE_SIZE)
258         return AVERROR_INVALIDDATA;
259
260     chunk_type = AV_RL32(&buf[0]);
261     buf += EA_PREAMBLE_SIZE;
262
263     if (chunk_type==kVGT_TAG) {
264         int pal_count, i;
265         if(buf_end - buf < 12) {
266             av_log(avctx, AV_LOG_WARNING, "truncated header\n");
267             return -1;
268         }
269
270         s->width  = AV_RL16(&buf[0]);
271         s->height = AV_RL16(&buf[2]);
272         if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
273             avcodec_set_dimensions(s->avctx, s->width, s->height);
274             cond_release_buffer(&s->frame);
275             cond_release_buffer(&s->last_frame);
276         }
277
278         pal_count = AV_RL16(&buf[6]);
279         buf += 12;
280         for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
281             s->palette[i] = 0xFF << 24 | AV_RB24(buf);
282             buf += 3;
283         }
284     }
285
286     if (av_image_check_size(s->width, s->height, 0, avctx))
287         return -1;
288
289     /* shuffle */
290     FFSWAP(AVFrame, s->frame, s->last_frame);
291     if (!s->frame.data[0]) {
292         s->frame.reference = 3;
293         s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
294         s->frame.linesize[0] = s->width;
295
296         /* allocate additional 12 bytes to accommodate av_memcpy_backptr() OUTBUF_PADDED optimisation */
297         s->frame.data[0] = av_malloc(s->width*s->height + 12);
298         if (!s->frame.data[0])
299             return AVERROR(ENOMEM);
300         s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
301         if (!s->frame.data[1]) {
302             av_freep(&s->frame.data[0]);
303             return AVERROR(ENOMEM);
304         }
305     }
306     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
307
308     if(chunk_type==kVGT_TAG) {
309         s->frame.key_frame = 1;
310         s->frame.pict_type = AV_PICTURE_TYPE_I;
311         if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
312             av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
313             return -1;
314         }
315     }else{
316         if (!s->last_frame.data[0]) {
317             av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
318             return buf_size;
319         }
320         s->frame.key_frame = 0;
321         s->frame.pict_type = AV_PICTURE_TYPE_P;
322         if (tgv_decode_inter(s, buf, buf_end)<0) {
323             av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
324             return -1;
325         }
326     }
327
328     *data_size = sizeof(AVFrame);
329     *(AVFrame*)data = s->frame;
330
331     return buf_size;
332 }
333
334 static av_cold int tgv_decode_end(AVCodecContext *avctx)
335 {
336     TgvContext *s = avctx->priv_data;
337     cond_release_buffer(&s->frame);
338     cond_release_buffer(&s->last_frame);
339     av_free(s->mv_codebook);
340     av_free(s->block_codebook);
341     return 0;
342 }
343
344 AVCodec ff_eatgv_decoder = {
345     .name           = "eatgv",
346     .type           = AVMEDIA_TYPE_VIDEO,
347     .id             = CODEC_ID_TGV,
348     .priv_data_size = sizeof(TgvContext),
349     .init           = tgv_decode_init,
350     .close          = tgv_decode_end,
351     .decode         = tgv_decode_frame,
352     .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
353 };