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
32
33 #define TM2_ESCAPE 0x80000000
35
36 /* Huffman-coded streams of different types of blocks */
46 };
47
48 /* Block types */
57 };
58
62
65
68
69 /* TM2 streams */
74 /* for blocks decoding */
79
80 /* data for current and previous frame */
86
87 /**
88 * Huffman codes for each of streams
89 */
91 VLC vlc;
///< table for FFmpeg bitstream reader
93 int *
recode;
///< table for converting from code indexes to values
96
97 /**
98 * structure for gathering Huffman codes information
99 */
104 int nodes;
///< total number of nodes in tree
105 int num;
///< current number filled
111
113 {
119 }
120
122 if (length == 0) {
123 length = 1;
124 }
128 }
130 huff->
bits[huff->
num] = prefix;
133 return 0;
134 } else { /* non-terminal node */
135 if ((ret =
tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
137 if ((ret =
tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
139 }
140 return 0;
141 }
142
144 {
147
153
154 /* check for correct codes parameters */
160 }
161 if ((huff.
nodes <= 0) || (huff.
nodes > 0x10000)) {
163 "nodes: %i\n", huff.
nodes);
165 }
166 /* one-node tree */
169
170 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
175
177
182 }
183
184 /* convert codes to vlc_table */
185 if (res >= 0) {
186 int i;
187
189 huff.
lens,
sizeof(
int),
sizeof(
int),
190 huff.
bits,
sizeof(uint32_t),
sizeof(uint32_t), 0);
191 if (res < 0)
193 else {
197 for (i = 0; i < code->
length; i++)
199 }
200 }
201 /* free allocated memory */
205
207 }
208
210 {
214 }
215
217 {
220 if(val<0)
221 return -1;
223 }
224
225 #define TM2_OLD_HEADER_MAGIC 0x00000100
226 #define TM2_NEW_HEADER_MAGIC 0x00000101
227
229 {
231
232 switch (magic) {
235 return 0;
237 return 0;
238 default:
241 }
242 }
243
245 {
248
251
252 if ((d < 1) || (d >
TM2_DELTAS) || (mb < 1) || (mb > 32)) {
255 }
256
257 for (i = 0; i < d; i++) {
259 if (v & (1 << (mb - 1)))
260 ctx->
deltas[stream_id][i] = v - (1 <<
mb);
261 else
263 }
265 ctx->
deltas[stream_id][i] = 0;
266
267 return 0;
268 }
269
271 {
273 int skip = 0;
277
278 if (buf_size < 4) {
281 }
282
283 /* get stream length in dwords */
285 len = bytestream2_get_be32(&gb);
286 skip = len * 4 + 4;
287
288 if (len == 0)
289 return 4;
290
291 if (len >= INT_MAX/4-1 || len < 0 || skip > buf_size) {
294 }
295
296 toks = bytestream2_get_be32(&gb);
297 if (toks & 1) {
298 len = bytestream2_get_be32(&gb);
300 len = bytestream2_get_be32(&gb);
301 }
302 if (len > 0) {
304 if (skip <= pos)
310 }
311 }
312 /* skip unused fields */
313 len = bytestream2_get_be32(&gb);
314 if (len ==
TM2_ESCAPE) {
/* some unknown length - could be escaped too */
316 } else {
318 }
319
321 if (skip <= pos)
327
328 toks >>= 1;
329 /* check if we have sane number of tokens */
330 if ((toks < 0) || (toks > 0xFFFFFF)) {
334 }
337 len = bytestream2_get_be32(&gb);
338 if (len > 0) {
340 if (skip <= pos)
343 for (i = 0; i < toks; i++) {
347 }
349 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >=
TM2_DELTAS || ctx->
tokens[stream_id][i]<0) {
351 ctx->
tokens[stream_id][i], stream_id, i);
353 }
354 }
355 } else {
356 for (i = 0; i < toks; i++) {
358 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >=
TM2_DELTAS) {
360 ctx->
tokens[stream_id][i], stream_id, i);
362 }
363 }
364 }
366
367 return skip;
368 }
369
371 {
374 return 0;
375 }
379 return 0;
380 }
382 }
384 }
385
386 /* blocks decoding routines */
387
388 /* common Y, U, V pointers initialisation */
389 #define TM2_INIT_POINTERS() \
390 int *last, *clast; \
391 int *Y, *U, *V;\
392 int Ystride, Ustride, Vstride;\
393 \
394 Ystride = ctx->y_stride;\
395 Vstride = ctx->uv_stride;\
396 Ustride = ctx->uv_stride;\
397 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
398 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
399 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
400 last = ctx->last + bx * 4;\
401 clast = ctx->clast + bx * 4;
402
403 #define TM2_INIT_POINTERS_2() \
404 int *Yo, *Uo, *Vo;\
405 int oYstride, oUstride, oVstride;\
406 \
407 TM2_INIT_POINTERS();\
408 oYstride = Ystride;\
409 oVstride = Vstride;\
410 oUstride = Ustride;\
411 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
412 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
413 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
414
415 /* recalculate last and delta values for next blocks */
416 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
417 CD[0] = CHR[1] - last[1];\
418 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
419 last[0] = (int)CHR[stride + 0];\
420 last[1] = (int)CHR[stride + 1];}
421
422 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
424 {
425 int ct, d;
426 int i, j;
427
428 for (j = 0; j < 4; j++){
430 for (i = 0; i < 4; i++){
431 d = deltas[i + j * 4];
432 ct += d;
433 last[i] += ct;
434 Y[i] = av_clip_uint8(last[i]);
435 }
438 }
439 }
440
442 {
443 int i, j;
444 for (j = 0; j < 2; j++) {
445 for (i = 0; i < 2; i++) {
446 CD[j] += deltas[i + j * 2];
447 last[i] += CD[j];
448 data[i] = last[i];
449 }
451 }
452 }
453
455 {
457 int l;
458 int prev;
459
460 if (bx > 0)
461 prev = clast[-3];
462 else
463 prev = 0;
464 t = (CD[0] + CD[1]) >> 1;
465 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
466 CD[1] = CD[0] + CD[1] -
t;
468 clast[0] = l;
469
471 }
472
474 {
475 int i;
476 int deltas[16];
478
479 /* hi-res chroma */
480 for (i = 0; i < 4; i++) {
483 }
486
487 /* hi-res luma */
488 for (i = 0; i < 16; i++)
490
492 }
493
495 {
496 int i;
497 int deltas[16];
499
500 /* low-res chroma */
502 deltas[1] = deltas[2] = deltas[3] = 0;
504
506 deltas[1] = deltas[2] = deltas[3] = 0;
508
509 /* hi-res luma */
510 for (i = 0; i < 16; i++)
512
514 }
515
517 {
518 int i;
520 int deltas[16];
522
523 /* low-res chroma */
525 deltas[1] = deltas[2] = deltas[3] = 0;
527
529 deltas[1] = deltas[2] = deltas[3] = 0;
531
532 /* low-res luma */
533 for (i = 0; i < 16; i++)
534 deltas[i] = 0;
535
540
541 if (bx > 0)
542 last[0] = (last[-1] - ctx->
D[0] - ctx->
D[1] - ctx->
D[2] - ctx->
D[3] + last[1]) >> 1;
543 else
544 last[0] = (last[1] - ctx->
D[0] - ctx->
D[1] - ctx->
D[2] - ctx->
D[3])>> 1;
545 last[2] = (last[1] + last[3]) >> 1;
546
547 t1 = ctx->
D[0] + ctx->
D[1];
549 ctx->
D[1] = t1 - (t1 >> 1);
550 t2 = ctx->
D[2] + ctx->
D[3];
552 ctx->
D[3] = t2 - (t2 >> 1);
553
555 }
556
558 {
559 int i;
560 int ct;
561 int left, right,
diff;
562 int deltas[16];
564
565 /* null chroma */
566 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
568
569 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
571
572 /* null luma */
573 for (i = 0; i < 16; i++)
574 deltas[i] = 0;
575
576 ct = ctx->
D[0] + ctx->
D[1] + ctx->
D[2] + ctx->
D[3];
577
578 if (bx > 0)
579 left = last[-1] - ct;
580 else
581 left = 0;
582
583 right = last[3];
584 diff = right - left;
585 last[0] = left + (diff >> 2);
586 last[1] = left + (diff >> 1);
587 last[2] = right - (diff >> 2);
588 last[3] = right;
589 {
590 int tp = left;
591
592 ctx->
D[0] = (tp + (ct >> 2)) - left;
594 ctx->
D[1] = (tp + (ct >> 1)) - left;
596 ctx->
D[2] = ((tp + ct) - (ct >> 2)) - left;
598 ctx->
D[3] = (tp + ct) - left;
599 }
601 }
602
604 {
605 int i, j;
607
608 /* update chroma */
609 for (j = 0; j < 2; j++) {
610 for (i = 0; i < 2; i++){
613 }
614 U += Ustride;
V += Vstride;
615 Uo += oUstride; Vo += oVstride;
616 }
621
622 /* update deltas */
623 ctx->
D[0] = Yo[3] - last[3];
624 ctx->
D[1] = Yo[3 + oYstride] - Yo[3];
625 ctx->
D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
626 ctx->
D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
627
628 for (j = 0; j < 4; j++) {
629 for (i = 0; i < 4; i++) {
631 last[i] = Yo[i];
632 }
634 Yo += oYstride;
635 }
636 }
637
639 {
640 int i, j;
641 int d;
643
644 /* update chroma */
645 for (j = 0; j < 2; j++) {
646 for (i = 0; i < 2; i++) {
649 }
652 Uo += oUstride;
653 Vo += oVstride;
654 }
659
660 /* update deltas */
661 ctx->
D[0] = Yo[3] - last[3];
662 ctx->
D[1] = Yo[3 + oYstride] - Yo[3];
663 ctx->
D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
664 ctx->
D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
665
666 for (j = 0; j < 4; j++) {
667 d = last[3];
668 for (i = 0; i < 4; i++) {
671 }
672 ctx->
D[j] = last[3] - d;
674 Yo += oYstride;
675 }
676 }
677
679 {
680 int i, j;
681 int mx, my;
683
686 mx = av_clip(mx, -(bx * 4 + 4), ctx->
avctx->
width - bx * 4);
687 my = av_clip(my, -(by * 4 + 4), ctx->
avctx->
height - by * 4);
688
691 return;
692 }
693
694 Yo += my * oYstride + mx;
695 Uo += (my >> 1) * oUstride + (mx >> 1);
696 Vo += (my >> 1) * oVstride + (mx >> 1);
697
698 /* copy chroma */
699 for (j = 0; j < 2; j++) {
700 for (i = 0; i < 2; i++) {
703 }
706 Uo += oUstride;
707 Vo += oVstride;
708 }
713
714 /* copy luma */
715 for (j = 0; j < 4; j++) {
716 for (i = 0; i < 4; i++) {
718 }
720 Yo += oYstride;
721 }
722 /* calculate deltas */
724 ctx->
D[0] =
Y[3] - last[3];
725 ctx->
D[1] =
Y[3 + Ystride] -
Y[3];
726 ctx->
D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
727 ctx->
D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
728 for (i = 0; i < 4; i++)
729 last[i] = Y[i + Ystride * 3];
730 }
731
733 {
734 int i, j;
737 int keyframe = 1;
740
743
747 }
748
749 memset(ctx->
last, 0, 4 * bw *
sizeof(
int));
750 memset(ctx->
clast, 0, 4 * bw *
sizeof(
int));
751
752 for (j = 0; j < bh; j++) {
753 memset(ctx->
D, 0, 4 *
sizeof(
int));
754 memset(ctx->
CD, 0, 4 *
sizeof(
int));
755 for (i = 0; i < bw; i++) {
757 switch(type) {
760 break;
763 break;
766 break;
769 break;
772 keyframe = 0;
773 break;
776 keyframe = 0;
777 break;
780 keyframe = 0;
781 break;
782 default:
784 }
785 }
786 }
787
788 /* copy data from our buffer to AVFrame */
789 Y = (ctx->
cur?ctx->
Y2:ctx->
Y1);
790 U = (ctx->
cur?ctx->
U2:ctx->
U1);
791 V = (ctx->
cur?ctx->
V2:ctx->
V1);
793 for (j = 0; j < h; j++) {
794 for (i = 0; i < w; i++) {
795 int y = Y[i],
u = U[i >> 1],
v = V[i >> 1];
796 dst[3*i+0] = av_clip_uint8(y + v);
797 dst[3*i+1] = av_clip_uint8(y);
798 dst[3*i+2] = av_clip_uint8(y + u);
799 }
800
801 /* horizontal edge extension */
802 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
803 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
804
805 /* vertical edge extension */
806 if (j == 0) {
811 } else if (j == h - 1) {
816 }
817
819 if (j & 1) {
820 /* horizontal edge extension */
821 U[-2] = U[-1] = U[0];
822 V[-2] = V[-1] = V[0];
823 U[cw + 1] = U[cw] = U[cw - 1];
824 V[cw + 1] = V[cw] = V[cw - 1];
825
826 /* vertical edge extension */
827 if (j == 1) {
832 } else if (j == h - 1) {
837 }
838
841 }
843 }
844
845 return keyframe;
846 }
847
850 };
851
852 #define TM2_HEADER_SIZE 40
853
855 void *
data,
int *got_frame,
857 {
860 int buf_size = avpkt->
size & ~3;
864
869 }
870
873
875
878 }
879
881 if (offset >= buf_size) {
884 }
885
887 buf_size - offset);
888 if (t < 0) {
892 }
894 }
898 else
900
902 *got_frame = 1;
904
905 return (ret < 0) ? ret : buf_size;
906 }
907
909 {
912
916 }
917
921
923
926
930 }
931
932 w += 8;
933 h += 8;
937 w = (w + 1) >> 1;
938 h = (h + 1) >> 1;
957 }
964
965 return 0;
966 }
967
969 {
972 int i;
973
985 }
988
990
991 return 0;
992 }
993
995 .
name =
"truemotion2",
1004 };