]> git.sesse.net Git - ffmpeg/blob - libavcodec/eatgv.c
h263: Drop uninitialized variable use from log message
[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 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
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 "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 {
42     AVCodecContext *avctx;
43     AVFrame *last_frame;
44     uint8_t *frame_buffer;
45     int width,height;
46     uint32_t palette[AVPALETTE_COUNT];
47
48     int (*mv_codebook)[2];
49     uint8_t (*block_codebook)[16];
50     int num_mvs;           ///< current length of mv_codebook
51     int num_blocks_packed; ///< current length of block_codebook
52 } TgvContext;
53
54 static av_cold int tgv_decode_init(AVCodecContext *avctx)
55 {
56     TgvContext *s = avctx->priv_data;
57     s->avctx         = avctx;
58     avctx->framerate = (AVRational){ 15, 1 };
59     avctx->pix_fmt   = AV_PIX_FMT_PAL8;
60
61     s->last_frame = av_frame_alloc();
62     if (!s->last_frame)
63         return AVERROR(ENOMEM);
64
65     return 0;
66 }
67
68 /**
69  * Unpack buffer
70  * @return 0 on success, -1 on critical buffer underflow
71  */
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
143 /**
144  * Decode inter-frame
145  * @return 0 on success, -1 on critical buffer underflow
146  */
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) {
169         av_log(s->avctx, AV_LOG_ERROR,
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         int err = av_reallocp(&s->mv_codebook, num_mvs * 2 * sizeof(int));
177         if (err < 0)
178             return err;
179         s->num_mvs = num_mvs;
180     }
181
182     if (num_blocks_packed > s->num_blocks_packed) {
183         int err;
184         if ((err = av_reallocp(&s->block_codebook, num_blocks_packed * 16)) < 0) {
185             s->num_blocks_packed = 0;
186             return err;
187         }
188         s->num_blocks_packed = num_blocks_packed;
189     }
190
191     /* read motion vectors */
192     mvbits = (num_mvs * 2 * 10 + 31) & ~31;
193
194     if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
195         return AVERROR_INVALIDDATA;
196
197     init_get_bits(&gb, buf, mvbits);
198     for (i = 0; i < num_mvs; i++) {
199         s->mv_codebook[i][0] = get_sbits(&gb, 10);
200         s->mv_codebook[i][1] = get_sbits(&gb, 10);
201     }
202     buf += mvbits >> 3;
203
204     /* note ptr to uncompressed blocks */
205     blocks_raw = buf;
206     buf       += num_blocks_raw * 16;
207
208     /* read compressed blocks */
209     init_get_bits(&gb, buf, (buf_end - buf) << 3);
210     for (i = 0; i < num_blocks_packed; i++) {
211         int tmp[4];
212         for (j = 0; j < 4; j++)
213             tmp[j] = get_bits(&gb, 8);
214         for (j = 0; j < 16; j++)
215             s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
216     }
217
218     if (get_bits_left(&gb) < vector_bits *
219         (s->avctx->height / 4) * (s->avctx->width / 4))
220         return AVERROR_INVALIDDATA;
221
222     /* read vectors and build frame */
223     for (y = 0; y < s->avctx->height / 4; y++)
224         for (x = 0; x < s->avctx->width / 4; x++) {
225             unsigned int vector = get_bits(&gb, vector_bits);
226             const uint8_t *src;
227             int src_stride;
228
229             if (vector < num_mvs) {
230                 int mx = x * 4 + s->mv_codebook[vector][0];
231                 int my = y * 4 + s->mv_codebook[vector][1];
232
233                 if (mx < 0 || mx + 4 > s->avctx->width ||
234                     my < 0 || my + 4 > s->avctx->height)
235                     continue;
236
237                 src = s->last_frame->data[0] + mx + my * s->last_frame->linesize[0];
238                 src_stride = s->last_frame->linesize[0];
239             } else {
240                 int offset = vector - num_mvs;
241                 if (offset < num_blocks_raw)
242                     src = blocks_raw + 16*offset;
243                 else if (offset - num_blocks_raw < num_blocks_packed)
244                     src = s->block_codebook[offset - num_blocks_raw];
245                 else
246                     continue;
247                 src_stride = 4;
248             }
249
250             for (j = 0; j < 4; j++)
251                 for (i = 0; i < 4; i++)
252                     frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
253                         src[j * src_stride + i];
254     }
255
256     return 0;
257 }
258
259 static int tgv_decode_frame(AVCodecContext *avctx,
260                             void *data, int *got_frame,
261                             AVPacket *avpkt)
262 {
263     const uint8_t *buf     = avpkt->data;
264     int buf_size           = avpkt->size;
265     TgvContext *s          = avctx->priv_data;
266     const uint8_t *buf_end = buf + buf_size;
267     AVFrame *frame         = data;
268     int chunk_type, ret;
269
270     chunk_type = AV_RL32(&buf[0]);
271     buf       += EA_PREAMBLE_SIZE;
272
273     if (chunk_type == kVGT_TAG) {
274         int pal_count, i;
275         if (buf + 12 > buf_end) {
276             av_log(avctx, AV_LOG_WARNING, "truncated header\n");
277             return AVERROR_INVALIDDATA;
278         }
279
280         s->width  = AV_RL16(&buf[0]);
281         s->height = AV_RL16(&buf[2]);
282         if (s->avctx->width != s->width || s->avctx->height != s->height) {
283             av_freep(&s->frame_buffer);
284             av_frame_unref(s->last_frame);
285             if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
286                 return ret;
287         }
288
289         pal_count = AV_RL16(&buf[6]);
290         buf += 12;
291         for (i = 0; i < pal_count && i < AVPALETTE_COUNT && buf + 2 < buf_end; i++) {
292             s->palette[i] = AV_RB24(buf);
293             buf += 3;
294         }
295     }
296
297     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
298         return ret;
299
300     memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
301
302     if (chunk_type == kVGT_TAG) {
303         int y;
304         frame->key_frame = 1;
305         frame->pict_type = AV_PICTURE_TYPE_I;
306
307         if (!s->frame_buffer &&
308             !(s->frame_buffer = av_malloc(s->width * s->height)))
309             return AVERROR(ENOMEM);
310
311         if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
312             av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
313             return AVERROR_INVALIDDATA;
314         }
315         for (y = 0; y < s->height; y++)
316             memcpy(frame->data[0]  + y * frame->linesize[0],
317                    s->frame_buffer + y * s->width,
318                    s->width);
319     } else {
320         if (!s->last_frame->data[0]) {
321             av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
322             return buf_size;
323         }
324         frame->key_frame = 0;
325         frame->pict_type = AV_PICTURE_TYPE_P;
326         if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
327             av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
328             return AVERROR_INVALIDDATA;
329         }
330     }
331
332     av_frame_unref(s->last_frame);
333     if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
334         return ret;
335
336     *got_frame = 1;
337
338     return buf_size;
339 }
340
341 static av_cold int tgv_decode_end(AVCodecContext *avctx)
342 {
343     TgvContext *s = avctx->priv_data;
344     av_frame_free(&s->last_frame);
345     av_freep(&s->frame_buffer);
346     av_free(s->mv_codebook);
347     av_free(s->block_codebook);
348     return 0;
349 }
350
351 AVCodec ff_eatgv_decoder = {
352     .name           = "eatgv",
353     .long_name      = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
354     .type           = AVMEDIA_TYPE_VIDEO,
355     .id             = AV_CODEC_ID_TGV,
356     .priv_data_size = sizeof(TgvContext),
357     .init           = tgv_decode_init,
358     .close          = tgv_decode_end,
359     .decode         = tgv_decode_frame,
360     .capabilities   = AV_CODEC_CAP_DR1,
361 };