1 /*
2 * Duck/ON2 TrueMotion 2 Decoder
3 * Copyright (c) 2005 Konstantin Shishkov
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 TrueMotion2 decoder.
25 */
26
27 #include <inttypes.h>
28
34
35 #define TM2_ESCAPE 0x80000000
37
38 /* Huffman-coded streams of different types of blocks */
48 };
49
50 /* Block types */
59 };
60
64
67
70
71 /* TM2 streams */
76 /* for blocks decoding */
81
82 /* data for current and previous frame */
88
89 /**
90 * Huffman codes for each of streams
91 */
93 VLC vlc;
///< table for FFmpeg bitstream reader
95 int *
recode;
///< table for converting from code indexes to values
98
99 /**
100 * structure for gathering Huffman codes information
101 */
106 int nodes;
///< total number of nodes in tree
107 int num;
///< current number filled
113
115 {
121 }
122
124 if (length == 0) {
125 length = 1;
126 }
130 }
132 huff->
bits[huff->
num] = prefix;
135 return 0;
136 } else { /* non-terminal node */
137 if ((ret =
tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
139 if ((ret =
tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
141 }
142 return 0;
143 }
144
146 {
148 int res = 0;
149
155
156 /* check for correct codes parameters */
162 }
163 if ((huff.
nodes <= 0) || (huff.
nodes > 0x10000)) {
165 "nodes: %i\n", huff.
nodes);
167 }
168 /* one-node tree */
171
172 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
177
180 goto fail;
181 }
182
184
189 }
190
191 /* convert codes to vlc_table */
192 if (res >= 0) {
193 int i;
194
196 huff.
lens,
sizeof(
int),
sizeof(
int),
197 huff.
bits,
sizeof(uint32_t),
sizeof(uint32_t), 0);
198 if (res < 0)
200 else {
206 goto fail;
207 }
208 for (i = 0; i < code->
length; i++)
210 }
211 }
212 fail:
213 /* free allocated memory */
217
218 return res;
219 }
220
222 {
226 }
227
229 {
232 if(val<0)
233 return -1;
235 }
236
237 #define TM2_OLD_HEADER_MAGIC 0x00000100
238 #define TM2_NEW_HEADER_MAGIC 0x00000101
239
241 {
243
244 switch (magic) {
247 return 0;
249 return 0;
250 default:
252 magic);
254 }
255 }
256
258 {
261
264
266 if ((d < 1) || (d >
TM2_DELTAS) || (mb < 1)) {
269 }
270
271 for (i = 0; i < d; i++) {
273 if (v & (1 << (mb - 1)))
274 ctx->
deltas[stream_id][i] = v - (1 <<
mb);
275 else
277 }
279 ctx->
deltas[stream_id][i] = 0;
280
281 return 0;
282 }
283
285 {
287 int skip = 0;
291
292 if (buf_size < 4) {
295 }
296
297 /* get stream length in dwords */
299 len = bytestream2_get_be32(&gb);
300 skip = len * 4 + 4;
301
302 if (len == 0)
303 return 4;
304
305 if (len >= INT_MAX/4-1 || len < 0 || skip > buf_size) {
308 }
309
310 toks = bytestream2_get_be32(&gb);
311 if (toks & 1) {
312 len = bytestream2_get_be32(&gb);
314 len = bytestream2_get_be32(&gb);
315 }
316 if (len > 0) {
318 if (skip <= pos)
324 }
325 }
326 /* skip unused fields */
327 len = bytestream2_get_be32(&gb);
328 if (len ==
TM2_ESCAPE) {
/* some unknown length - could be escaped too */
330 } else {
332 }
333
335 if (skip <= pos)
341
342 toks >>= 1;
343 /* check if we have sane number of tokens */
344 if ((toks < 0) || (toks > 0xFFFFFF)) {
348 }
350 if (ret < 0) {
353 }
355 len = bytestream2_get_be32(&gb);
356 if (len > 0) {
358 if (skip <= pos)
361 for (i = 0; i < toks; i++) {
365 }
367 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >=
TM2_DELTAS || ctx->
tokens[stream_id][i]<0) {
369 ctx->
tokens[stream_id][i], stream_id, i);
371 }
372 }
373 } else {
374 for (i = 0; i < toks; i++) {
376 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >=
TM2_DELTAS) {
378 ctx->
tokens[stream_id][i], stream_id, i);
380 }
381 }
382 }
384
385 return skip;
386 }
387
389 {
392 return 0;
393 }
397 return 0;
398 }
400 }
402 }
403
404 /* blocks decoding routines */
405
406 /* common Y, U, V pointers initialisation */
407 #define TM2_INIT_POINTERS() \
408 int *last, *clast; \
409 int *Y, *U, *V;\
410 int Ystride, Ustride, Vstride;\
411 \
412 Ystride = ctx->y_stride;\
413 Vstride = ctx->uv_stride;\
414 Ustride = ctx->uv_stride;\
415 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
416 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
417 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
418 last = ctx->last + bx * 4;\
419 clast = ctx->clast + bx * 4;
420
421 #define TM2_INIT_POINTERS_2() \
422 int *Yo, *Uo, *Vo;\
423 int oYstride, oUstride, oVstride;\
424 \
425 TM2_INIT_POINTERS();\
426 oYstride = Ystride;\
427 oVstride = Vstride;\
428 oUstride = Ustride;\
429 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
430 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
431 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
432
433 /* recalculate last and delta values for next blocks */
434 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
435 CD[0] = CHR[1] - last[1];\
436 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
437 last[0] = (int)CHR[stride + 0];\
438 last[1] = (int)CHR[stride + 1];}
439
440 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
442 {
443 int ct, d;
444 int i, j;
445
446 for (j = 0; j < 4; j++){
448 for (i = 0; i < 4; i++){
449 d = deltas[i + j * 4];
450 ct += d;
451 last[i] += ct;
452 Y[i] = av_clip_uint8(last[i]);
453 }
454 Y += stride;
456 }
457 }
458
460 {
461 int i, j;
462 for (j = 0; j < 2; j++) {
463 for (i = 0; i < 2; i++) {
464 CD[j] += deltas[i + j * 2];
465 last[i] += CD[j];
466 data[i] = last[i];
467 }
468 data += stride;
469 }
470 }
471
473 {
474 int t;
475 int l;
476 int prev;
477
478 if (bx > 0)
479 prev = clast[-3];
480 else
481 prev = 0;
482 t = (CD[0] + CD[1]) >> 1;
483 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
484 CD[1] = CD[0] + CD[1] - t;
485 CD[0] = t;
486 clast[0] = l;
487
489 }
490
492 {
493 int i;
494 int deltas[16];
496
497 /* hi-res chroma */
498 for (i = 0; i < 4; i++) {
501 }
504
505 /* hi-res luma */
506 for (i = 0; i < 16; i++)
508
510 }
511
513 {
514 int i;
515 int deltas[16];
517
518 /* low-res chroma */
520 deltas[1] = deltas[2] = deltas[3] = 0;
522
524 deltas[1] = deltas[2] = deltas[3] = 0;
526
527 /* hi-res luma */
528 for (i = 0; i < 16; i++)
530
532 }
533
535 {
536 int i;
538 int deltas[16];
540
541 /* low-res chroma */
543 deltas[1] = deltas[2] = deltas[3] = 0;
545
547 deltas[1] = deltas[2] = deltas[3] = 0;
549
550 /* low-res luma */
551 for (i = 0; i < 16; i++)
552 deltas[i] = 0;
553
558
559 if (bx > 0)
560 last[0] = (last[-1] - ctx->
D[0] - ctx->
D[1] - ctx->
D[2] - ctx->
D[3] + last[1]) >> 1;
561 else
562 last[0] = (last[1] - ctx->
D[0] - ctx->
D[1] - ctx->
D[2] - ctx->
D[3])>> 1;
563 last[2] = (last[1] + last[3]) >> 1;
564
565 t1 = ctx->
D[0] + ctx->
D[1];
567 ctx->
D[1] = t1 - (t1 >> 1);
568 t2 = ctx->
D[2] + ctx->
D[3];
570 ctx->
D[3] = t2 - (t2 >> 1);
571
573 }
574
576 {
577 int i;
578 int ct;
579 int left, right,
diff;
580 int deltas[16];
582
583 /* null chroma */
584 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
586
587 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
589
590 /* null luma */
591 for (i = 0; i < 16; i++)
592 deltas[i] = 0;
593
594 ct = ctx->
D[0] + ctx->
D[1] + ctx->
D[2] + ctx->
D[3];
595
596 if (bx > 0)
597 left = last[-1] - ct;
598 else
599 left = 0;
600
601 right = last[3];
602 diff = right - left;
603 last[0] = left + (diff >> 2);
604 last[1] = left + (diff >> 1);
605 last[2] = right - (diff >> 2);
606 last[3] = right;
607 {
608 int tp = left;
609
610 ctx->
D[0] = (tp + (ct >> 2)) - left;
612 ctx->
D[1] = (tp + (ct >> 1)) - left;
614 ctx->
D[2] = ((tp + ct) - (ct >> 2)) - left;
616 ctx->
D[3] = (tp + ct) - left;
617 }
619 }
620
622 {
623 int i, j;
625
626 /* update chroma */
627 for (j = 0; j < 2; j++) {
628 for (i = 0; i < 2; i++){
631 }
632 U += Ustride;
V += Vstride;
633 Uo += oUstride; Vo += oVstride;
634 }
639
640 /* update deltas */
641 ctx->
D[0] = Yo[3] - last[3];
642 ctx->
D[1] = Yo[3 + oYstride] - Yo[3];
643 ctx->
D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
644 ctx->
D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
645
646 for (j = 0; j < 4; j++) {
647 for (i = 0; i < 4; i++) {
649 last[i] = Yo[i];
650 }
652 Yo += oYstride;
653 }
654 }
655
657 {
658 int i, j;
659 int d;
661
662 /* update chroma */
663 for (j = 0; j < 2; j++) {
664 for (i = 0; i < 2; i++) {
667 }
670 Uo += oUstride;
671 Vo += oVstride;
672 }
677
678 /* update deltas */
679 ctx->
D[0] = Yo[3] - last[3];
680 ctx->
D[1] = Yo[3 + oYstride] - Yo[3];
681 ctx->
D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
682 ctx->
D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
683
684 for (j = 0; j < 4; j++) {
685 d = last[3];
686 for (i = 0; i < 4; i++) {
689 }
690 ctx->
D[j] = last[3] - d;
692 Yo += oYstride;
693 }
694 }
695
697 {
698 int i, j;
699 int mx, my;
701
704 mx = av_clip(mx, -(bx * 4 + 4), ctx->
avctx->
width - bx * 4);
705 my = av_clip(my, -(by * 4 + 4), ctx->
avctx->
height - by * 4);
706
709 return;
710 }
711
712 Yo += my * oYstride + mx;
713 Uo += (my >> 1) * oUstride + (mx >> 1);
714 Vo += (my >> 1) * oVstride + (mx >> 1);
715
716 /* copy chroma */
717 for (j = 0; j < 2; j++) {
718 for (i = 0; i < 2; i++) {
721 }
724 Uo += oUstride;
725 Vo += oVstride;
726 }
731
732 /* copy luma */
733 for (j = 0; j < 4; j++) {
734 for (i = 0; i < 4; i++) {
736 }
738 Yo += oYstride;
739 }
740 /* calculate deltas */
742 ctx->
D[0] =
Y[3] - last[3];
743 ctx->
D[1] =
Y[3 + Ystride] -
Y[3];
744 ctx->
D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
745 ctx->
D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
746 for (i = 0; i < 4; i++)
747 last[i] = Y[i + Ystride * 3];
748 }
749
751 {
752 int i, j;
755 int keyframe = 1;
758
761
765 }
766
767 memset(ctx->
last, 0, 4 * bw *
sizeof(
int));
768 memset(ctx->
clast, 0, 4 * bw *
sizeof(
int));
769
770 for (j = 0; j < bh; j++) {
771 memset(ctx->
D, 0, 4 *
sizeof(
int));
772 memset(ctx->
CD, 0, 4 *
sizeof(
int));
773 for (i = 0; i < bw; i++) {
775 switch(type) {
778 break;
781 break;
784 break;
787 break;
790 keyframe = 0;
791 break;
794 keyframe = 0;
795 break;
798 keyframe = 0;
799 break;
800 default:
802 }
803 }
804 }
805
806 /* copy data from our buffer to AVFrame */
807 Y = (ctx->
cur?ctx->
Y2:ctx->
Y1);
808 U = (ctx->
cur?ctx->
U2:ctx->
U1);
809 V = (ctx->
cur?ctx->
V2:ctx->
V1);
811 for (j = 0; j < h; j++) {
812 for (i = 0; i < w; i++) {
813 int y = Y[i],
u = U[i >> 1],
v = V[i >> 1];
814 dst[3*i+0] = av_clip_uint8(y + v);
815 dst[3*i+1] = av_clip_uint8(y);
816 dst[3*i+2] = av_clip_uint8(y + u);
817 }
818
819 /* horizontal edge extension */
820 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
821 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
822
823 /* vertical edge extension */
824 if (j == 0) {
829 } else if (j == h - 1) {
834 }
835
837 if (j & 1) {
838 /* horizontal edge extension */
839 U[-2] = U[-1] = U[0];
840 V[-2] = V[-1] = V[0];
841 U[cw + 1] = U[cw] = U[cw - 1];
842 V[cw + 1] = V[cw] = V[cw - 1];
843
844 /* vertical edge extension */
845 if (j == 1) {
850 } else if (j == h - 1) {
855 }
856
859 }
861 }
862
863 return keyframe;
864 }
865
868 };
869
870 #define TM2_HEADER_SIZE 40
871
873 void *
data,
int *got_frame,
875 {
878 int buf_size = avpkt->
size & ~3;
882
887 }
888
891
893 buf_size >> 2);
894
897 }
898
900 if (offset >= buf_size) {
903 }
904
906 buf_size - offset);
907 if (t < 0) {
910 return t;
911 }
912 offset += t;
913 }
917 else
919
921 *got_frame = 1;
923
924 return (ret < 0) ? ret : buf_size;
925 }
926
928 {
931
935 }
936
939
943
945
948
952 }
953
954 w += 8;
955 h += 8;
959 w = (w + 1) >> 1;
960 h = (h + 1) >> 1;
980 }
987
988 return 0;
989 }
990
992 {
994 int i;
995
1007 }
1010
1012
1013 return 0;
1014 }
1015
1017 .
name =
"truemotion2",
1026 };