1 /*
2 * Duck TrueMotion 1.0 Decoder
3 * Copyright (C) 2003 Alex Beregszaszi & Mike Melanson
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Duck TrueMotion v1 Video Decoder by
25 * Alex Beregszaszi and
26 * Mike Melanson (melanson@pcisys.net)
27 *
28 * The TrueMotion v1 decoder presently only decodes 16-bit TM1 data and
29 * outputs RGB555 (or RGB565) data. 24-bit TM1 data is not supported yet.
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
41
43
47
50
55
58
63
68
73
75
78
80
81 #define FLAG_SPRITE 32
82 #define FLAG_KEYFRAME 16
83 #define FLAG_INTERFRAME 8
84 #define FLAG_INTERPOLATED 4
85
102 };
103
105 #define ALGO_RGB16V 1
106 #define ALGO_RGB16H 2
107 #define ALGO_RGB24H 3
108
109 /* these are the various block sizes that can occupy a 4x4 block */
114
121
122 /* { valid for metatype }, algorithm, num of deltas, vert res, horiz res */
125
130
135
140
145 };
146
148 {
149 int i;
150
151 if (delta_table_index > 3)
152 return;
153
154 memcpy(s->
ydt,
ydts[delta_table_index], 8 *
sizeof(int16_t));
155 memcpy(s->
cdt,
cdts[delta_table_index], 8 *
sizeof(int16_t));
158
159 /* Y skinny deltas need to be halved for some reason; maybe the
160 * skinny Y deltas should be modified */
161 for (i = 0; i < 8; i++)
162 {
163 /* drop the lsb before dividing by 2-- net effect: round down
164 * when dividing a negative number (e.g., -3/2 = -2, not -1) */
167 }
168 }
169
170 #if HAVE_BIGENDIAN
172 #else
174 #endif
175 {
176 int lo, hi;
177
178 lo = ydt[p1];
179 lo += (lo << 5) + (lo << 10);
180 hi = ydt[p2];
181 hi += (hi << 5) + (hi << 10);
182 return (lo + (hi << 16)) << 1;
183 }
184
186 {
188
189 b = cdt[p2];
190 r = cdt[p1] << 10;
192 return (lo + (lo << 16)) << 1;
193 }
194
195 #if HAVE_BIGENDIAN
197 #else
199 #endif
200 {
201 int lo, hi;
202
203 lo = ydt[p1];
204 lo += (lo << 6) + (lo << 11);
205 hi = ydt[p2];
206 hi += (hi << 6) + (hi << 11);
207 return (lo + (hi << 16)) << 1;
208 }
209
211 {
213
214 b = cdt[p2];
215 r = cdt[p1] << 11;
217 return (lo + (lo << 16)) << 1;
218 }
219
221 {
222 int lo, hi;
223
224 lo = ydt[p1];
225 hi = ydt[p2];
226 return (lo + (hi << 8) + (hi << 16)) << 1;
227 }
228
230 {
232
233 b = cdt[p2];
234 r = cdt[p1]<<16;
235 return (b+r) << 1;
236 }
237
239 {
241 unsigned char delta_pair;
242
243 for (i = 0; i < 1024; i += 4)
244 {
245 len = *sel_vector_table++ / 2;
246 for (j = 0; j <
len; j++)
247 {
248 delta_pair = *sel_vector_table++;
253 }
256 }
257 }
258
260 {
262 unsigned char delta_pair;
263
264 for (i = 0; i < 1024; i += 4)
265 {
266 len = *sel_vector_table++ / 2;
267 for (j = 0; j <
len; j++)
268 {
269 delta_pair = *sel_vector_table++;
274 }
277 }
278 }
279
281 {
283 unsigned char delta_pair;
284
285 for (i = 0; i < 1024; i += 4)
286 {
287 len = *sel_vector_table++ / 2;
288 for (j = 0; j <
len; j++)
289 {
290 delta_pair = *sel_vector_table++;
299 }
304 }
305 }
306
307 /* Returns the number of bytes consumed from the bytestream. Returns -1 if
308 * there was an error while decoding the header */
310 {
311 int i;
312 int width_shift = 0;
313 int new_pix_fmt;
315 uint8_t header_buffer[128] = { 0 };
/* logical maximum size of the header */
316 const uint8_t *sel_vector_table;
317
320 {
322 return -1;
323 }
324
325 /* unscramble the header bytes with a XOR operation */
327 header_buffer[i - 1] = s->
buf[i] ^ s->
buf[i + 1];
328
335 header.
version = header_buffer[9];
337 header.
flags = header_buffer[11];
338 header.
control = header_buffer[12];
339
340 /* Version 2 */
342 {
344 {
346 return -1;
351 } else
353 } else /* Version 1 */
355
358 /* FIXME header.width, height, xoffset and yoffset aren't initialized */
360 } else {
364 if ((s->
w < 213) && (s->
h >= 176))
365 {
368 }
369 }
370 }
371
374 return -1;
375 }
376
380
383 else {
386 else {
388 return -1;
389 }
390 }
391
394 width_shift = 1;
395 } else
397
398 s->
w >>= width_shift;
400 return -1;
401
410 }
411
412 /* There is 1 change bit per 4 pixels, so each change byte represents
413 * 32 pixels; divide width by 4 to obtain the number of change bits and
414 * then round up to the nearest byte. */
416
418 {
421 else
424 else
426 }
427
428 /* set up pointers to the other key data chunks */
431 /* no change bits specified for a keyframe; only index bytes */
433 } else {
434 /* one change bit per 4x4 block */
437 }
439
446
455
457 }
458
460 {
462
464
465 // FIXME: it may change ?
466 // if (avctx->bits_per_sample == 24)
467 // avctx->pix_fmt = AV_PIX_FMT_RGB24;
468 // else
469 // avctx->pix_fmt = AV_PIX_FMT_RGB555;
470
473
474 /* there is a vertical predictor for each pixel in a line; each vertical
475 * predictor is 0 to start with */
477
478 return 0;
479 }
480
481 /*
482 Block decoding order:
483
484 dxi: Y-Y
485 dxic: Y-C-Y
486 dxic2: Y-C-Y-C
487
488 hres,vres,i,i%vres (0 < i < 4)
489 2x2 0: 0 dxic2
490 2x2 1: 1 dxi
491 2x2 2: 0 dxic2
492 2x2 3: 1 dxi
493 2x4 0: 0 dxic2
494 2x4 1: 1 dxi
495 2x4 2: 2 dxi
496 2x4 3: 3 dxi
497 4x2 0: 0 dxic
498 4x2 1: 1 dxi
499 4x2 2: 0 dxic
500 4x2 3: 1 dxi
501 4x4 0: 0 dxic
502 4x4 1: 1 dxi
503 4x4 2: 2 dxi
504 4x4 3: 3 dxi
505 */
506
507 #define GET_NEXT_INDEX() \
508 {\
509 if (index_stream_index >= s->index_stream_size) { \
510 av_log(s->avctx, AV_LOG_INFO, " help! truemotion1 decoder went out of bounds\n"); \
511 return; \
512 } \
513 index = s->index_stream[index_stream_index++] * 4; \
514 }
515
516 #define APPLY_C_PREDICTOR() \
517 if(index > 1023){\
518 av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \
519 return; \
520 }\
521 predictor_pair = s->c_predictor_table[index]; \
522 horiz_pred += (predictor_pair >> 1); \
523 if (predictor_pair & 1) { \
524 GET_NEXT_INDEX() \
525 if (!index) { \
526 GET_NEXT_INDEX() \
527 predictor_pair = s->c_predictor_table[index]; \
528 horiz_pred += ((predictor_pair >> 1) * 5); \
529 if (predictor_pair & 1) \
530 GET_NEXT_INDEX() \
531 else \
532 index++; \
533 } \
534 } else \
535 index++;
536
537 #define APPLY_C_PREDICTOR_24() \
538 if(index > 1023){\
539 av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \
540 return; \
541 }\
542 predictor_pair = s->c_predictor_table[index]; \
543 horiz_pred += (predictor_pair >> 1); \
544 if (predictor_pair & 1) { \
545 GET_NEXT_INDEX() \
546 if (!index) { \
547 GET_NEXT_INDEX() \
548 predictor_pair = s->fat_c_predictor_table[index]; \
549 horiz_pred += (predictor_pair >> 1); \
550 if (predictor_pair & 1) \
551 GET_NEXT_INDEX() \
552 else \
553 index++; \
554 } \
555 } else \
556 index++;
557
558
559 #define APPLY_Y_PREDICTOR() \
560 if(index > 1023){\
561 av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \
562 return; \
563 }\
564 predictor_pair = s->y_predictor_table[index]; \
565 horiz_pred += (predictor_pair >> 1); \
566 if (predictor_pair & 1) { \
567 GET_NEXT_INDEX() \
568 if (!index) { \
569 GET_NEXT_INDEX() \
570 predictor_pair = s->y_predictor_table[index]; \
571 horiz_pred += ((predictor_pair >> 1) * 5); \
572 if (predictor_pair & 1) \
573 GET_NEXT_INDEX() \
574 else \
575 index++; \
576 } \
577 } else \
578 index++;
579
580 #define APPLY_Y_PREDICTOR_24() \
581 if(index > 1023){\
582 av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \
583 return; \
584 }\
585 predictor_pair = s->y_predictor_table[index]; \
586 horiz_pred += (predictor_pair >> 1); \
587 if (predictor_pair & 1) { \
588 GET_NEXT_INDEX() \
589 if (!index) { \
590 GET_NEXT_INDEX() \
591 predictor_pair = s->fat_y_predictor_table[index]; \
592 horiz_pred += (predictor_pair >> 1); \
593 if (predictor_pair & 1) \
594 GET_NEXT_INDEX() \
595 else \
596 index++; \
597 } \
598 } else \
599 index++;
600
601 #define OUTPUT_PIXEL_PAIR() \
602 *current_pixel_pair = *vert_pred + horiz_pred; \
603 *vert_pred++ = *current_pixel_pair++;
604
606 {
607 int y;
608 int pixels_left; /* remaining pixels on this line */
609 unsigned int predictor_pair;
610 unsigned int horiz_pred;
611 unsigned int *vert_pred;
612 unsigned int *current_pixel_pair;
613 unsigned char *current_line = s->
frame.
data[0];
615
616 /* these variables are for managing the stream of macroblock change bits */
618 unsigned char mb_change_byte;
619 unsigned char mb_change_byte_mask;
620 int mb_change_index;
621
622 /* these variables are for managing the main index stream */
623 int index_stream_index = 0; /* yes, the index into the index stream */
625
626 /* clean out the line buffer */
628
630
632
633 /* re-init variables for the next line iteration */
634 horiz_pred = 0;
635 current_pixel_pair = (unsigned int *)current_line;
637 mb_change_index = 0;
638 mb_change_byte = mb_change_bits[mb_change_index++];
639 mb_change_byte_mask = 0x01;
641
642 while (pixels_left > 0) {
643
644 if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
645
646 switch (y & 3) {
647 case 0:
648 /* if macroblock width is 2, apply C-Y-C-Y; else
649 * apply C-Y-Y */
657 } else {
663 }
664 break;
665
666 case 1:
667 case 3:
668 /* always apply 2 Y predictors on these iterations */
673 break;
674
675 case 2:
676 /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
677 * depending on the macroblock type */
691 } else {
696 }
697 break;
698 }
699
700 } else {
701
702 /* skip (copy) four pixels, but reassign the horizontal
703 * predictor */
704 *vert_pred++ = *current_pixel_pair++;
705 horiz_pred = *current_pixel_pair - *vert_pred;
706 *vert_pred++ = *current_pixel_pair++;
707
708 }
709
710 if (!keyframe) {
711 mb_change_byte_mask <<= 1;
712
713 /* next byte */
714 if (!mb_change_byte_mask) {
715 mb_change_byte = mb_change_bits[mb_change_index++];
716 mb_change_byte_mask = 0x01;
717 }
718 }
719
720 pixels_left -= 4;
721 }
722
723 /* next change row */
724 if (((y + 1) & 3) == 0)
726
728 }
729 }
730
732 {
733 int y;
734 int pixels_left; /* remaining pixels on this line */
735 unsigned int predictor_pair;
736 unsigned int horiz_pred;
737 unsigned int *vert_pred;
738 unsigned int *current_pixel_pair;
739 unsigned char *current_line = s->
frame.
data[0];
741
742 /* these variables are for managing the stream of macroblock change bits */
744 unsigned char mb_change_byte;
745 unsigned char mb_change_byte_mask;
746 int mb_change_index;
747
748 /* these variables are for managing the main index stream */
749 int index_stream_index = 0; /* yes, the index into the index stream */
751
752 /* clean out the line buffer */
754
756
758
759 /* re-init variables for the next line iteration */
760 horiz_pred = 0;
761 current_pixel_pair = (unsigned int *)current_line;
763 mb_change_index = 0;
764 mb_change_byte = mb_change_bits[mb_change_index++];
765 mb_change_byte_mask = 0x01;
767
768 while (pixels_left > 0) {
769
770 if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
771
772 switch (y & 3) {
773 case 0:
774 /* if macroblock width is 2, apply C-Y-C-Y; else
775 * apply C-Y-Y */
783 } else {
789 }
790 break;
791
792 case 1:
793 case 3:
794 /* always apply 2 Y predictors on these iterations */
799 break;
800
801 case 2:
802 /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
803 * depending on the macroblock type */
817 } else {
822 }
823 break;
824 }
825
826 } else {
827
828 /* skip (copy) four pixels, but reassign the horizontal
829 * predictor */
830 *vert_pred++ = *current_pixel_pair++;
831 horiz_pred = *current_pixel_pair - *vert_pred;
832 *vert_pred++ = *current_pixel_pair++;
833
834 }
835
836 if (!keyframe) {
837 mb_change_byte_mask <<= 1;
838
839 /* next byte */
840 if (!mb_change_byte_mask) {
841 mb_change_byte = mb_change_bits[mb_change_index++];
842 mb_change_byte_mask = 0x01;
843 }
844 }
845
846 pixels_left -= 2;
847 }
848
849 /* next change row */
850 if (((y + 1) & 3) == 0)
852
854 }
855 }
856
857
859 void *
data,
int *got_frame,
861 {
863 int buf_size = avpkt->
size;
865
868
870 return -1;
871
877 return -1;
878 }
879
884 }
885
886 *got_frame = 1;
888
889 /* report that the buffer was completely consumed */
890 return buf_size;
891 }
892
894 {
896
899
901
902 return 0;
903 }
904
906 .
name =
"truemotion1",
915 };