1 /*
2 * MSMPEG4 backend for encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
35
37 #define V2_INTRA_CBPC_VLC_BITS 3
38 #define V2_MB_TYPE_VLC_BITS 7
40 #define V2_MV_VLC_BITS 9
41 #define TEX_VLC_BITS 9
42
43 #define DEFAULT_INTER_INDEX 3
44
47 {
48 int i;
49
50 if (n < 4) {
51 i= 0;
52 } else {
53 i= n-3;
54 }
55
58 }
59
60 /****************************************/
61 /* decoding stuff */
62
70
71 /* This is identical to h263 except that its range is multiplied by 2. */
73 {
75
77 av_dlog(s,
"MV code %d at %d %d pred: %d\n", code, s->
mb_x,s->
mb_y, pred);
78 if (code < 0)
79 return 0xffff;
80
81 if (code == 0)
84 shift = f_code - 1;
85 val = code;
86 if (shift) {
87 val = (val - 1) << shift;
89 val++;
90 }
91 if (sign)
93
95 if (val <= -64)
96 val += 64;
97 else if (val >= 64)
98 val -= 64;
99
101 }
102
104 {
105 int cbp, code, i;
107
111 /* skip mb */
113 for(i=0;i<6;i++)
121 return 0;
122 }
123 }
124
127 else
129 if(code<0 || code>7){
131 return -1;
132 }
133
135
136 cbp = code & 0x3;
137 } else {
141 else
143 if(cbp<0 || cbp>3){
145 return -1;
146 }
147 }
148
150 int mx, my, cbpy;
151
153 if(cbpy<0){
155 return -1;
156 }
157
158 cbp|= cbpy<<2;
160
164
170 } else {
174 } else{
178 }
180 }
181
183 for (i = 0; i < 6; i++) {
185 {
187 return -1;
188 }
189 }
190 return 0;
191 }
192
194 {
195 int cbp, code, i;
198
202 /* skip mb */
204 for(i=0;i<6;i++)
212
213 return 0;
214 }
215 }
216
218 if (code < 0)
219 return -1;
220 //s->mb_intra = (code & 0x40) ? 0 : 1;
222
223 cbp = code & 0x3f;
224 } else {
227 if (code < 0)
228 return -1;
229 /* predict coded block pattern */
230 cbp = 0;
231 for(i=0;i<6;i++) {
232 int val = ((code >> (5 - i)) & 1);
233 if (i < 4) {
237 }
238 cbp |= val << (5 - i);
239 }
240 }
241
243 int mx, my;
247 }
250 return -1;
256 } else {
258 ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
266 }
270 }
271 }
272
274 for (i = 0; i < 6; i++) {
276 {
278 return -1;
279 }
280 }
281
282 return 0;
283 }
284
285 /* init all vlc decoding tables */
287 {
289 static volatile int done = 0;
292
295
297 return -1;
298
300
301 if (!done) {
304 }
311
320
333
340
350
363
367
371 done = 1;
372 }
373
375 case 1:
376 case 2:
378 break;
379 case 3:
380 case 4:
382 break;
383 case 5:
384 if (CONFIG_WMV2_DECODER)
386 case 6:
387 //FIXME + TODO VC1 decode mb
388 break;
389 }
390
392
393 return 0;
394 }
395
397 {
398 int code;
399
402 if(start_code!=0x00000100){
404 return -1;
405 }
406
408 }
409
414 return -1;
415 }
416 #if 0
417 {
418 static int had_i=0;
420 if(!had_i) return -1;
421 }
422 #endif
426 return -1;
427 }
428
434 return -1;
435 }
436
438 }else{
439 /* 0x17: one slice, 0x18: two slices, ... */
440 if (code < 0x17){
442 return -1;
443 }
444
446 }
447
449 case 1:
450 case 2:
453
455 break;
456 case 3:
459
461 break;
462 case 4:
464
467
471 }
472
475 break;
476 }
486 } else {
488 case 1:
489 case 2:
492 else
498 break;
499 case 3:
503
505
507 break;
508 case 4:
510
513
517 }
518
520
523 break;
524 }
525
535
538 }else{
540 }
541 }
544
547
548 return 0;
549 }
550
552 {
555 /* the alt_bitstream reader could read over the end so we need to check it */
556 if(left>=length && left<length+8)
557 {
562 else
564 }
565 else if(left<length+8)
566 {
570 }
571 else
572 {
574 }
575
576 return 0;
577 }
578
580 {
582
584 if (n < 4) {
586 } else {
588 }
589 if (level < 0) {
591 *dir_ptr = 0;
592 return -1;
593 }
594 level-=256;
595 }else{ //FIXME optimize use unified tables & index
596 if (n < 4) {
598 } else {
600 }
601 if (level < 0){
603 *dir_ptr = 0;
604 return -1;
605 }
606
611 } else if (level != 0) {
614 }
615 }
616
621
622 /* update predictor */
624 }else{
625 int16_t *dc_val;
628
629 /* update predictor */
630 if (n < 4) {
632 } else {
634 }
635 }
636
638 }
639
640 //#define ERROR_DETAILS
642 int n,
int coded,
const uint8_t *scan_table)
643 {
648 int qmul, qadd;
649
651 qmul=1;
652 qadd=0;
653
654 /* DC coef */
656
657 if (level < 0){
660 }
661 if (n < 4) {
666 }
667 } else {
672 }
673 }
675
677 i = 0;
678 if (!coded) {
679 goto not_coded;
680 }
682 if (dc_pred_dir == 0)
684 else
686 } else {
688 }
690 } else {
692 qadd = (s->
qscale - 1) | 1;
693 i = -1;
695
697 run_diff = 0;
698 else
699 run_diff = 1;
700
701 if (!coded) {
703 return 0;
704 }
705 if(!scan_table)
708 }
709 {
711 for(;;) {
714 if (level==0) {
715 int cache;
717 /* escape */
720 /* third escape */
728 }else{
729 int sign;
732 int ll;
737 if(ll==0){
739 }
740 }else{
741 ll=2;
743 ll++;
745 }
747 }
748
752 }
755
758
761 if(sign) level= -
level;
762 }
763
764 #if 0 // waste of time / this will detect very few errors
765 {
766 const int abs_level=
FFABS(level);
767 const int run1= run - rl->
max_run[last][abs_level] - run_diff;
769 if(abs_level <= rl->max_level[last][run]){
771 return DECODING_AC_LOST;
772 }
773 if(abs_level <= rl->max_level[last][run]*2){
775 return DECODING_AC_LOST;
776 }
777 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
779 return DECODING_AC_LOST;
780 }
781 }
782 }
783 #endif
784 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
785 if (level>0) level= level * qmul + qadd;
786 else level= level * qmul - qadd;
787 #if 0 // waste of time too :(
788 if(level>2048 || level<-2048){
790 return DECODING_AC_LOST;
791 }
792 #endif
793 i+= run + 1;
794 if(last) i+=192;
795 #ifdef ERROR_DETAILS
796 if(run==66)
798 else if((i>62 && i<192) || i>192+63)
800 #endif
801 } else {
802 /* second escape */
805 i+= run + rl->
max_run[run>>7][level/qmul] + run_diff;
//FIXME opt indexing
808 #ifdef ERROR_DETAILS
809 if(run==66)
811 else if((i>62 && i<192) || i>192+63)
813 #endif
814 }
815 } else {
816 /* first escape */
820 level = level + rl->
max_level[run>>7][(run-1)&63] * qmul;
//FIXME opt indexing
823 #ifdef ERROR_DETAILS
824 if(run==66)
826 else if((i>62 && i<192) || i>192+63)
828 #endif
829 }
830 } else {
834 #ifdef ERROR_DETAILS
835 if(run==66)
837 else if((i>62 && i<192) || i>192+63)
839 #endif
840 }
841 if (i > 62){
842 i-= 192;
843 if(i&(~63)){
847 i = 63;
848 break;
849 }else{
851 return -1;
852 }
853 }
854
855 block[scan_table[i]] =
level;
856 break;
857 }
858
859 block[scan_table[i]] =
level;
860 }
862 }
863 not_coded:
867 i = 63; /* XXX: not optimal */
868 }
869 }
872
873 return 0;
874 }
875
877 int *mx_ptr, int *my_ptr)
878 {
880 int code, mx, my;
881
883
885 if (code < 0){
887 return -1;
888 }
892 } else {
895 }
896
897 mx += *mx_ptr - 32;
898 my += *my_ptr - 32;
899 /* WARNING : they do not do exactly modulo encoding */
900 if (mx <= -64)
901 mx += 64;
902 else if (mx >= 64)
903 mx -= 64;
904
905 if (my <= -64)
906 my += 64;
907 else if (my >= 64)
908 my -= 64;
909 *mx_ptr = mx;
910 *my_ptr = my;
911 return 0;
912 }
913
924 .max_lowres = 3,
928 },
929 };
930
941 .max_lowres = 3,
945 },
946 };
947
958 .max_lowres = 3,
962 },
963 };
964
975 .max_lowres = 3,
979 },
980 };