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