fix initialization bug in which execution overwrites essential data
[libav.git] / libavcodec / truemotion1.c
1 /*
2 * Duck TrueMotion 1.0 Decoder
3 * Copyright (C) 2003 Alex Beregszaszi & Mike Melanson
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /**
21 * @file truemotion1.c
22 * Duck TrueMotion v1 Video Decoder by
23 * Alex Beregszaszi (alex@fsn.hu) and
24 * Mike Melanson (melanson@pcisys.net)
25 *
26 * The TrueMotion v1 decoder presently only decodes 16-bit TM1 data and
27 * outputs RGB555 data. 24-bit TM1 data is not supported yet.
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include "common.h"
36 #include "avcodec.h"
37 #include "dsputil.h"
38
39 #include "truemotion1data.h"
40
41 typedef struct TrueMotion1Context {
42 AVCodecContext *avctx;
43 AVFrame frame;
44 AVFrame prev_frame;
45
46 unsigned char *buf;
47 int size;
48
49 unsigned char *mb_change_bits;
50 int mb_change_bits_row_size;
51 unsigned char *index_stream;
52 int index_stream_size;
53
54 int flags;
55 int x, y, w, h;
56
57 uint32_t y_predictor_table[1024];
58 uint32_t c_predictor_table[1024];
59
60 int compression;
61 int block_type;
62 int block_width;
63 int block_height;
64
65 int16_t ydt[8];
66 int16_t cdt[8];
67 int16_t fat_ydt[8];
68 int16_t fat_cdt[8];
69
70 int last_deltaset, last_vectable;
71
72 unsigned int *vert_pred;
73
74 } TrueMotion1Context;
75
76 #define FLAG_SPRITE 32
77 #define FLAG_KEYFRAME 16
78 #define FLAG_INTERFRAME 8
79 #define FLAG_INTERPOLATED 4
80
81 struct frame_header {
82 uint8_t header_size;
83 uint8_t compression;
84 uint8_t deltaset;
85 uint8_t vectable;
86 uint16_t ysize;
87 uint16_t xsize;
88 uint16_t checksum;
89 uint8_t version;
90 uint8_t header_type;
91 uint8_t flags;
92 uint8_t control;
93 uint16_t xoffset;
94 uint16_t yoffset;
95 uint16_t width;
96 uint16_t height;
97 };
98
99 #define ALGO_NOP 0
100 #define ALGO_RGB16V 1
101 #define ALGO_RGB16H 2
102 #define ALGO_RGB24H 3
103
104 /* these are the various block sizes that can occupy a 4x4 block */
105 #define BLOCK_2x2 0
106 #define BLOCK_2x4 1
107 #define BLOCK_4x2 2
108 #define BLOCK_4x4 3
109
110 typedef struct comp_types {
111 int algorithm;
112 int block_width;
113 int block_height;
114 int block_type;
115 } comp_types;
116
117 /* { valid for metatype }, algorithm, num of deltas, horiz res, vert res */
118 static comp_types compression_types[17] = {
119 { ALGO_NOP, 0, 0, 0 },
120
121 { ALGO_RGB16V, 4, 4, BLOCK_4x4 },
122 { ALGO_RGB16H, 4, 4, BLOCK_4x4 },
123 { ALGO_RGB16V, 4, 2, BLOCK_4x2 },
124 { ALGO_RGB16H, 4, 2, BLOCK_4x2 },
125
126 { ALGO_RGB16V, 2, 4, BLOCK_2x4 },
127 { ALGO_RGB16H, 2, 4, BLOCK_2x4 },
128 { ALGO_RGB16V, 2, 2, BLOCK_2x2 },
129 { ALGO_RGB16H, 2, 2, BLOCK_2x2 },
130
131 { ALGO_NOP, 4, 4, BLOCK_4x4 },
132 { ALGO_RGB24H, 4, 4, BLOCK_4x4 },
133 { ALGO_NOP, 4, 2, BLOCK_4x2 },
134 { ALGO_RGB24H, 4, 2, BLOCK_4x2 },
135
136 { ALGO_NOP, 2, 4, BLOCK_2x4 },
137 { ALGO_RGB24H, 2, 4, BLOCK_2x4 },
138 { ALGO_NOP, 2, 2, BLOCK_2x2 },
139 { ALGO_RGB24H, 2, 2, BLOCK_2x2 }
140 };
141
142 static void select_delta_tables(TrueMotion1Context *s, int delta_table_index)
143 {
144 int i;
145
146 if (delta_table_index > 3)
147 return;
148
149 memcpy(s->ydt, ydts[delta_table_index], 8 * sizeof(int16_t));
150 memcpy(s->cdt, cdts[delta_table_index], 8 * sizeof(int16_t));
151 memcpy(s->fat_ydt, fat_ydts[delta_table_index], 8 * sizeof(int16_t));
152 memcpy(s->fat_cdt, fat_cdts[delta_table_index], 8 * sizeof(int16_t));
153
154 /* Y skinny deltas need to be halved for some reason; maybe the
155 * skinny Y deltas should be modified */
156 for (i = 0; i < 8; i++)
157 {
158 /* drop the lsb before dividing by 2-- net effect: round down
159 * when dividing a negative number (e.g., -3/2 = -2, not -1) */
160 s->ydt[i] &= 0xFFFE;
161 s->ydt[i] /= 2;
162 }
163 }
164
165 #ifdef WORDS_BIGENDIAN
166 static int make_ydt_entry(int p2, int p1, int16_t *ydt)
167 #else
168 static int make_ydt_entry(int p1, int p2, int16_t *ydt)
169 #endif
170 {
171 int lo, hi;
172
173 lo = ydt[p1];
174 lo += (lo << 5) + (lo << 10);
175 hi = ydt[p2];
176 hi += (hi << 5) + (hi << 10);
177 return ((lo + (hi << 16)) << 1);
178 }
179
180 #ifdef WORDS_BIGENDIAN
181 static int make_cdt_entry(int p2, int p1, int16_t *cdt)
182 #else
183 static int make_cdt_entry(int p1, int p2, int16_t *cdt)
184 #endif
185 {
186 int r, b, lo;
187
188 b = cdt[p2];
189 r = cdt[p1] << 10;
190 lo = b + r;
191 return ((lo + (lo << 16)) << 1);
192 }
193
194 static void gen_vector_table(TrueMotion1Context *s, uint8_t *sel_vector_table)
195 {
196 int len, i, j;
197 unsigned char delta_pair;
198
199 for (i = 0; i < 1024; i += 4)
200 {
201 len = *sel_vector_table++ / 2;
202 for (j = 0; j < len; j++)
203 {
204 delta_pair = *sel_vector_table++;
205 s->y_predictor_table[i+j] = 0xfffffffe &
206 make_ydt_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
207 s->c_predictor_table[i+j] = 0xfffffffe &
208 make_cdt_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
209 }
210 s->y_predictor_table[i+(j-1)] |= 1;
211 s->c_predictor_table[i+(j-1)] |= 1;
212 }
213 }
214
215 /* Returns the number of bytes consumed from the bytestream. Returns -1 if
216 * there was an error while decoding the header */
217 static int truemotion1_decode_header(TrueMotion1Context *s)
218 {
219 int i;
220 struct frame_header header;
221 uint8_t header_buffer[128]; /* logical maximum size of the header */
222 uint8_t *sel_vector_table;
223
224 /* There is 1 change bit per 4 pixels, so each change byte represents
225 * 32 pixels; divide width by 4 to obtain the number of change bits and
226 * then round up to the nearest byte. */
227 s->mb_change_bits_row_size = ((s->avctx->width >> 2) + 7) >> 3;
228
229 header.header_size = ((s->buf[0] >> 5) | (s->buf[0] << 3)) & 0x7f;
230 if (s->buf[0] < 0x10)
231 {
232 av_log(s->avctx, AV_LOG_ERROR, "invalid header size\n");
233 return -1;
234 }
235
236 /* unscramble the header bytes with a XOR operation */
237 memset(header_buffer, 0, 128);
238 for (i = 1; i < header.header_size; i++)
239 header_buffer[i - 1] = s->buf[i] ^ s->buf[i + 1];
240 header.compression = header_buffer[0];
241 header.deltaset = header_buffer[1];
242 header.vectable = header_buffer[2];
243 header.ysize = LE_16(&header_buffer[3]);
244 header.xsize = LE_16(&header_buffer[5]);
245 header.checksum = LE_16(&header_buffer[7]);
246 header.version = header_buffer[9];
247 header.header_type = header_buffer[10];
248 header.flags = header_buffer[11];
249 header.control = header_buffer[12];
250
251 /* Version 2 */
252 if (header.version >= 2)
253 {
254 if (header.header_type > 3)
255 {
256 av_log(s->avctx, AV_LOG_ERROR, "truemotion1: invalid header type\n");
257 return -1;
258 } else if ((header.header_type == 2) || (header.header_type == 3)) {
259 s->flags = header.flags;
260 if (!(s->flags & FLAG_INTERFRAME))
261 s->flags |= FLAG_KEYFRAME;
262 } else
263 s->flags = FLAG_KEYFRAME;
264 } else /* Version 1 */
265 s->flags = FLAG_KEYFRAME;
266
267 if (s->flags & FLAG_SPRITE) {
268 s->w = header.width;
269 s->h = header.height;
270 s->x = header.xoffset;
271 s->y = header.yoffset;
272 } else {
273 s->w = header.xsize;
274 s->h = header.ysize;
275 if (header.header_type < 2) {
276 if ((s->w < 213) && (s->h >= 176))
277 s->flags |= FLAG_INTERPOLATED;
278 }
279 }
280
281 if (header.compression > 17) {
282 av_log(s->avctx, AV_LOG_ERROR, "invalid compression type (%d)\n", header.compression);
283 return -1;
284 }
285
286 if ((header.deltaset != s->last_deltaset) ||
287 (header.vectable != s->last_vectable))
288 select_delta_tables(s, header.deltaset);
289
290 if ((header.compression & 1) && header.header_type)
291 sel_vector_table = pc_tbl2;
292 else {
293 if (header.vectable < 4)
294 sel_vector_table = tables[header.vectable - 1];
295 else {
296 av_log(s->avctx, AV_LOG_ERROR, "invalid vector table id (%d)\n", header.vectable);
297 return -1;
298 }
299 }
300
301 if ((header.deltaset != s->last_deltaset) || (header.vectable != s->last_vectable))
302 {
303 if (compression_types[header.compression].algorithm == ALGO_RGB24H)
304 {
305 av_log(s->avctx, AV_LOG_ERROR, "24bit compression not yet supported\n");
306 }
307 else
308 gen_vector_table(s, sel_vector_table);
309 }
310
311 /* set up pointers to the other key data chunks */
312 s->mb_change_bits = s->buf + header.header_size;
313 if (s->flags & FLAG_KEYFRAME) {
314 /* no change bits specified for a keyframe; only index bytes */
315 s->index_stream = s->mb_change_bits;
316 } else {
317 /* one change bit per 4x4 block */
318 s->index_stream = s->mb_change_bits +
319 (s->mb_change_bits_row_size * (s->avctx->height >> 2));
320 }
321 s->index_stream_size = s->size - (s->index_stream - s->buf);
322
323 s->last_deltaset = header.deltaset;
324 s->last_vectable = header.vectable;
325 s->compression = header.compression;
326 s->block_width = compression_types[header.compression].block_width;
327 s->block_height = compression_types[header.compression].block_height;
328 s->block_type = compression_types[header.compression].block_type;
329
330 return header.header_size;
331 }
332
333 static int truemotion1_decode_init(AVCodecContext *avctx)
334 {
335 TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
336
337 s->avctx = avctx;
338
339 avctx->pix_fmt = PIX_FMT_RGB555;
340 avctx->has_b_frames = 0;
341 s->frame.data[0] = s->prev_frame.data[0] = NULL;
342
343 /* there is a vertical predictor for each pixel in a line; each vertical
344 * predictor is 0 to start with */
345 s->vert_pred =
346 (unsigned int *)av_malloc(s->avctx->width * sizeof(unsigned short));
347
348 return 0;
349 }
350
351 #define GET_NEXT_INDEX() \
352 {\
353 if (index_stream_index >= s->index_stream_size) { \
354 av_log(s->avctx, AV_LOG_INFO, " help! truemotion1 decoder went out of bounds\n"); \
355 return; \
356 } \
357 index = s->index_stream[index_stream_index++] * 4; \
358 }
359
360 #define APPLY_C_PREDICTOR() \
361 predictor_pair = s->c_predictor_table[index]; \
362 horiz_pred += (predictor_pair >> 1); \
363 if (predictor_pair & 1) { \
364 GET_NEXT_INDEX() \
365 if (!index) { \
366 GET_NEXT_INDEX() \
367 predictor_pair = s->c_predictor_table[index]; \
368 horiz_pred += ((predictor_pair >> 1) * 5); \
369 if (predictor_pair & 1) \
370 GET_NEXT_INDEX() \
371 else \
372 index++; \
373 } \
374 } else \
375 index++;
376
377 #define APPLY_Y_PREDICTOR() \
378 predictor_pair = s->y_predictor_table[index]; \
379 horiz_pred += (predictor_pair >> 1); \
380 if (predictor_pair & 1) { \
381 GET_NEXT_INDEX() \
382 if (!index) { \
383 GET_NEXT_INDEX() \
384 predictor_pair = s->y_predictor_table[index]; \
385 horiz_pred += ((predictor_pair >> 1) * 5); \
386 if (predictor_pair & 1) \
387 GET_NEXT_INDEX() \
388 else \
389 index++; \
390 } \
391 } else \
392 index++;
393
394 #define OUTPUT_PIXEL_PAIR() \
395 *current_pixel_pair = *vert_pred + horiz_pred; \
396 *vert_pred++ = *current_pixel_pair++; \
397 prev_pixel_pair++;
398
399 static void truemotion1_decode_16bit(TrueMotion1Context *s)
400 {
401 int y;
402 int pixels_left; /* remaining pixels on this line */
403 unsigned int predictor_pair;
404 unsigned int horiz_pred;
405 unsigned int *vert_pred;
406 unsigned int *current_pixel_pair;
407 unsigned int *prev_pixel_pair;
408 unsigned char *current_line = s->frame.data[0];
409 unsigned char *prev_line = s->prev_frame.data[0];
410 int keyframe = s->flags & FLAG_KEYFRAME;
411
412 /* these variables are for managing the stream of macroblock change bits */
413 unsigned char *mb_change_bits = s->mb_change_bits;
414 unsigned char mb_change_byte;
415 unsigned char mb_change_byte_mask;
416 int mb_change_index;
417
418 /* these variables are for managing the main index stream */
419 int index_stream_index = 0; /* yes, the index into the index stream */
420 int index;
421
422 /* clean out the line buffer */
423 memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned short));
424
425 GET_NEXT_INDEX();
426
427 for (y = 0; y < s->avctx->height; y++) {
428
429 /* re-init variables for the next line iteration */
430 horiz_pred = 0;
431 current_pixel_pair = (unsigned int *)current_line;
432 prev_pixel_pair = (unsigned int *)prev_line;
433 vert_pred = s->vert_pred;
434 mb_change_index = 0;
435 mb_change_byte = mb_change_bits[mb_change_index++];
436 mb_change_byte_mask = 0x01;
437 pixels_left = s->avctx->width;
438
439 while (pixels_left > 0) {
440
441 if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
442
443 switch (y & 3) {
444 case 0:
445 /* if macroblock width is 2, apply C-Y-C-Y; else
446 * apply C-Y-Y */
447 if (s->block_width == 2) {
448 APPLY_C_PREDICTOR();
449 APPLY_Y_PREDICTOR();
450 OUTPUT_PIXEL_PAIR();
451 APPLY_C_PREDICTOR();
452 APPLY_Y_PREDICTOR();
453 OUTPUT_PIXEL_PAIR();
454 } else {
455 APPLY_C_PREDICTOR();
456 APPLY_Y_PREDICTOR();
457 OUTPUT_PIXEL_PAIR();
458 APPLY_Y_PREDICTOR();
459 OUTPUT_PIXEL_PAIR();
460 }
461 break;
462
463 case 1:
464 case 3:
465 /* always apply 2 Y predictors on these iterations */
466 APPLY_Y_PREDICTOR();
467 OUTPUT_PIXEL_PAIR();
468 APPLY_Y_PREDICTOR();
469 OUTPUT_PIXEL_PAIR();
470 break;
471
472 case 2:
473 /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
474 * depending on the macroblock type */
475 if (s->block_type == BLOCK_2x2) {
476 APPLY_C_PREDICTOR();
477 APPLY_Y_PREDICTOR();
478 OUTPUT_PIXEL_PAIR();
479 APPLY_C_PREDICTOR();
480 APPLY_Y_PREDICTOR();
481 OUTPUT_PIXEL_PAIR();
482 } else if (s->block_type == BLOCK_4x2) {
483 APPLY_C_PREDICTOR();
484 APPLY_Y_PREDICTOR();
485 OUTPUT_PIXEL_PAIR();
486 APPLY_Y_PREDICTOR();
487 OUTPUT_PIXEL_PAIR();
488 } else {
489 APPLY_Y_PREDICTOR();
490 OUTPUT_PIXEL_PAIR();
491 APPLY_Y_PREDICTOR();
492 OUTPUT_PIXEL_PAIR();
493 }
494 break;
495 }
496
497 } else {
498
499 /* skip (copy) four pixels, but reassign the horizontal
500 * predictor */
501 *current_pixel_pair = *prev_pixel_pair++;
502 *vert_pred++ = *current_pixel_pair++;
503 *current_pixel_pair = *prev_pixel_pair++;
504 horiz_pred = *current_pixel_pair - *vert_pred;
505 *vert_pred++ = *current_pixel_pair++;
506
507 }
508
509 if (!keyframe) {
510 mb_change_byte_mask <<= 1;
511
512 /* next byte */
513 if (!mb_change_byte_mask) {
514 mb_change_byte = mb_change_bits[mb_change_index++];
515 mb_change_byte_mask = 0x01;
516 }
517 }
518
519 pixels_left -= 4;
520 }
521
522 /* next change row */
523 if (((y + 1) & 3) == 0)
524 mb_change_bits += s->mb_change_bits_row_size;
525
526 current_line += s->frame.linesize[0];
527 prev_line += s->prev_frame.linesize[0];
528 }
529 }
530
531 static int truemotion1_decode_frame(AVCodecContext *avctx,
532 void *data, int *data_size,
533 uint8_t *buf, int buf_size)
534 {
535 TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
536
537 s->buf = buf;
538 s->size = buf_size;
539
540 s->frame.reference = 1;
541 if (avctx->get_buffer(avctx, &s->frame) < 0) {
542 av_log(s->avctx, AV_LOG_ERROR, "truemotion1: get_buffer() failed\n");
543 return -1;
544 }
545
546 /* no supplementary picture */
547 if (buf_size == 0)
548 return 0;
549
550 if (truemotion1_decode_header(s) == -1)
551 return -1;
552
553 /* check for a do-nothing frame and copy the previous frame */
554 if (compression_types[s->compression].algorithm == ALGO_NOP)
555 {
556 memcpy(s->frame.data[0], s->prev_frame.data[0],
557 s->frame.linesize[0] * s->avctx->height);
558 } else if (compression_types[s->compression].algorithm == ALGO_RGB24H) {
559 av_log(s->avctx, AV_LOG_ERROR, "24bit compression not yet supported\n");
560 } else {
561 truemotion1_decode_16bit(s);
562 }
563
564 if (s->prev_frame.data[0])
565 avctx->release_buffer(avctx, &s->prev_frame);
566
567 /* shuffle frames */
568 s->prev_frame = s->frame;
569
570 *data_size = sizeof(AVFrame);
571 *(AVFrame*)data = s->frame;
572
573 /* report that the buffer was completely consumed */
574 return buf_size;
575 }
576
577 static int truemotion1_decode_end(AVCodecContext *avctx)
578 {
579 TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
580
581 /* release the last frame */
582 if (s->prev_frame.data[0])
583 avctx->release_buffer(avctx, &s->prev_frame);
584
585 av_free(s->vert_pred);
586
587 return 0;
588 }
589
590 AVCodec truemotion1_decoder = {
591 "truemotion1",
592 CODEC_TYPE_VIDEO,
593 CODEC_ID_TRUEMOTION1,
594 sizeof(TrueMotion1Context),
595 truemotion1_decode_init,
596 NULL,
597 truemotion1_decode_end,
598 truemotion1_decode_frame,
599 CODEC_CAP_DR1,
600 };