2 * Duck TrueMotion 1.0 Decoder
3 * Copyright (C) 2003 Alex Beregszaszi & Mike Melanson
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.
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.
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
22 * Duck TrueMotion v1 Video Decoder by
23 * Alex Beregszaszi (alex@fsn.hu) and
24 * Mike Melanson (melanson@pcisys.net)
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.
39 #include "truemotion1data.h"
41 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
43 typedef struct TrueMotion1Context {
44 AVCodecContext *avctx;
51 unsigned char *mb_change_bits;
52 int mb_change_bits_row_size;
53 unsigned char *index_stream;
54 int index_stream_size;
59 uint32_t y_predictor_table[1024];
60 uint32_t c_predictor_table[1024];
72 int last_deltaset, last_vectable;
74 unsigned int *vert_pred;
78 #define FLAG_SPRITE 32
79 #define FLAG_KEYFRAME 16
80 #define FLAG_INTERFRAME 8
81 #define FLAG_INTERPOLATED 4
102 #define ALGO_RGB16V 1
103 #define ALGO_RGB16H 2
104 #define ALGO_RGB24H 3
106 /* these are the various block sizes that can occupy a 4x4 block */
112 typedef struct comp_types {
119 /* { valid for metatype }, algorithm, num of deltas, horiz res, vert res */
120 static comp_types compression_types[17] = {
121 { ALGO_NOP, 0, 0, 0 },
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 },
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 },
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 },
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 }
144 static void select_delta_tables(TrueMotion1Context *s, int delta_table_index)
148 if (delta_table_index > 3)
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];
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++)
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) */
167 #ifdef WORDS_BIGENDIAN
168 static int make_ydt_entry(int p2, int p1, int16_t *ydt)
170 static int make_ydt_entry(int p1, int p2, int16_t *ydt)
176 lo += (lo << 5) + (lo << 10);
178 hi += (hi << 5) + (hi << 10);
179 return ((lo + (hi << 16)) << 1);
182 #ifdef WORDS_BIGENDIAN
183 static int make_cdt_entry(int p2, int p1, int16_t *cdt)
185 static int make_cdt_entry(int p1, int p2, int16_t *cdt)
193 return ((lo + (lo << 16)) << 1);
196 static void gen_vector_table(TrueMotion1Context *s, uint8_t *sel_vector_table)
199 unsigned char delta_pair;
201 for (i = 0; i < 1024; i += 4)
203 len = *sel_vector_table++ / 2;
204 for (j = 0; j < len; j++)
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);
212 s->y_predictor_table[i+(j-1)] |= 1;
213 s->c_predictor_table[i+(j-1)] |= 1;
217 /* Returns the number of bytes consumed from the bytestream. Returns -1 if
218 * there was an error while decoding the header */
219 static int truemotion1_decode_header(TrueMotion1Context *s)
222 struct frame_header header;
223 uint8_t header_buffer[128]; /* logical maximum size of the header */
224 uint8_t *sel_vector_table;
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;
231 header.header_size = ((s->buf[0] >> 5) | (s->buf[0] << 3)) & 0x7f;
232 if (s->buf[0] < 0x10)
234 printf("invalid header size\n");
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];
254 if (header.version >= 2)
256 if (header.header_type > 3)
258 av_log(s->avctx, AV_LOG_ERROR, "truemotion1: invalid header type\n");
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;
265 s->flags = FLAG_KEYFRAME;
266 } else /* Version 1 */
267 s->flags = FLAG_KEYFRAME;
269 if (s->flags & FLAG_SPRITE) {
271 s->h = header.height;
272 s->x = header.xoffset;
273 s->y = header.yoffset;
277 if (header.header_type < 2) {
278 if ((s->w < 213) && (s->h >= 176))
279 s->flags |= FLAG_INTERPOLATED;
283 if (header.compression > 17) {
284 printf("invalid compression type (%d)\n", header.compression);
288 if ((header.deltaset != s->last_deltaset) ||
289 (header.vectable != s->last_vectable))
290 select_delta_tables(s, header.deltaset);
292 if ((header.compression & 1) && header.header_type)
293 sel_vector_table = pc_tbl2;
295 if (header.vectable < 4)
296 sel_vector_table = tables[header.vectable - 1];
298 printf("invalid vector table id (%d)\n", header.vectable);
303 if ((header.deltaset != s->last_deltaset) || (header.vectable != s->last_vectable))
305 if (compression_types[header.compression].algorithm == ALGO_RGB24H)
307 printf("24bit compression not yet supported\n");
310 gen_vector_table(s, sel_vector_table);
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;
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));
323 s->index_stream_size = s->size - (s->index_stream - s->buf);
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;
332 return header.header_size;
335 static int truemotion1_decode_init(AVCodecContext *avctx)
337 TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
341 avctx->pix_fmt = PIX_FMT_RGB555;
342 avctx->has_b_frames = 0;
343 s->frame.data[0] = s->prev_frame.data[0] = NULL;
345 /* there is a vertical predictor for each pixel in a line; each vertical
346 * predictor is 0 to start with */
348 (unsigned int *)av_malloc(s->avctx->width * sizeof(unsigned short));
353 #define GET_NEXT_INDEX() \
355 if (index_stream_index >= s->index_stream_size) { \
356 printf (" help! truemotion1 decoder went out of bounds\n"); \
359 index = s->index_stream[index_stream_index++] * 4; \
362 #define APPLY_C_PREDICTOR() \
363 predictor_pair = s->c_predictor_table[index]; \
364 horiz_pred += (predictor_pair >> 1); \
365 if (predictor_pair & 1) { \
369 predictor_pair = s->c_predictor_table[index]; \
370 horiz_pred += ((predictor_pair >> 1) * 5); \
371 if (predictor_pair & 1) \
379 #define APPLY_Y_PREDICTOR() \
380 predictor_pair = s->y_predictor_table[index]; \
381 horiz_pred += (predictor_pair >> 1); \
382 if (predictor_pair & 1) { \
386 predictor_pair = s->y_predictor_table[index]; \
387 horiz_pred += ((predictor_pair >> 1) * 5); \
388 if (predictor_pair & 1) \
396 #define OUTPUT_PIXEL_PAIR() \
397 *current_pixel_pair = *vert_pred + horiz_pred; \
398 *vert_pred++ = *current_pixel_pair++; \
401 static void truemotion1_decode_16bit(TrueMotion1Context *s)
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;
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;
420 /* these variables are for managing the main index stream */
421 int index_stream_index = 0; /* yes, the index into the index stream */
424 /* clean out the line buffer */
425 memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned short));
429 for (y = 0; y < s->avctx->height; y++) {
431 /* re-init variables for the next line iteration */
433 current_pixel_pair = (unsigned int *)current_line;
434 prev_pixel_pair = (unsigned int *)prev_line;
435 vert_pred = s->vert_pred;
437 mb_change_byte = mb_change_bits[mb_change_index++];
438 mb_change_byte_mask = 0x01;
439 pixels_left = s->avctx->width;
441 while (pixels_left > 0) {
443 if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
447 /* if macroblock width is 2, apply C-Y-C-Y; else
449 if (s->block_width == 2) {
467 /* always apply 2 Y predictors on these iterations */
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) {
484 } else if (s->block_type == BLOCK_4x2) {
501 /* skip (copy) four pixels, but reassign the horizontal
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++;
512 mb_change_byte_mask <<= 1;
515 if (!mb_change_byte_mask) {
516 mb_change_byte = mb_change_bits[mb_change_index++];
517 mb_change_byte_mask = 0x01;
524 /* next change row */
525 if (((y + 1) & 3) == 0)
526 mb_change_bits += s->mb_change_bits_row_size;
528 current_line += s->frame.linesize[0];
529 prev_line += s->prev_frame.linesize[0];
533 static int truemotion1_decode_frame(AVCodecContext *avctx,
534 void *data, int *data_size,
535 uint8_t *buf, int buf_size)
537 TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
542 s->frame.reference = 1;
543 if (avctx->get_buffer(avctx, &s->frame) < 0) {
544 fprintf(stderr, "truemotion1: get_buffer() failed\n");
548 /* no supplementary picture */
554 if (truemotion1_decode_header(s) == -1)
557 /* check for a do-nothing frame and copy the previous frame */
558 if (compression_types[s->compression].algorithm == ALGO_NOP)
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");
565 truemotion1_decode_16bit(s);
568 if (s->prev_frame.data[0])
569 avctx->release_buffer(avctx, &s->prev_frame);
572 s->prev_frame = s->frame;
574 *data_size = sizeof(AVFrame);
575 *(AVFrame*)data = s->frame;
577 /* report that the buffer was completely consumed */
581 static int truemotion1_decode_end(AVCodecContext *avctx)
583 TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
585 /* release the last frame */
586 if (s->prev_frame.data[0])
587 avctx->release_buffer(avctx, &s->prev_frame);
589 av_free(s->vert_pred);
594 AVCodec truemotion1_decoder = {
597 CODEC_ID_TRUEMOTION1,
598 sizeof(TrueMotion1Context),
599 truemotion1_decode_init,
601 truemotion1_decode_end,
602 truemotion1_decode_frame,