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)
590 return -1;
591 level-=256;
592 }else{ //FIXME optimize use unified tables & index
593 if (n < 4) {
595 } else {
597 }
598 if (level < 0){
600 return -1;
601 }
602
607 } else if (level != 0) {
610 }
611 }
612
617
618 /* update predictor */
620 }else{
621 int16_t *dc_val;
624
625 /* update predictor */
626 if (n < 4) {
628 } else {
630 }
631 }
632
634 }
635
636 //#define ERROR_DETAILS
638 int n,
int coded,
const uint8_t *scan_table)
639 {
644 int qmul, qadd;
645
647 qmul=1;
648 qadd=0;
649
650 /* DC coef */
652
653 if (level < 0){
656 }
657 if (n < 4) {
662 }
663 } else {
668 }
669 }
671
673 i = 0;
674 if (!coded) {
675 goto not_coded;
676 }
678 if (dc_pred_dir == 0)
680 else
682 } else {
684 }
686 } else {
688 qadd = (s->
qscale - 1) | 1;
689 i = -1;
691
693 run_diff = 0;
694 else
695 run_diff = 1;
696
697 if (!coded) {
699 return 0;
700 }
701 if(!scan_table)
704 }
705 {
707 for(;;) {
710 if (level==0) {
711 int cache;
713 /* escape */
716 /* third escape */
724 }else{
725 int sign;
728 int ll;
733 if(ll==0){
735 }
736 }else{
737 ll=2;
739 ll++;
741 }
743 }
744
748 }
751
754
757 if(sign) level= -
level;
758 }
759
760 #if 0 // waste of time / this will detect very few errors
761 {
762 const int abs_level=
FFABS(level);
763 const int run1= run - rl->
max_run[last][abs_level] - run_diff;
765 if(abs_level <= rl->max_level[last][run]){
767 return DECODING_AC_LOST;
768 }
769 if(abs_level <= rl->max_level[last][run]*2){
771 return DECODING_AC_LOST;
772 }
773 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
775 return DECODING_AC_LOST;
776 }
777 }
778 }
779 #endif
780 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
781 if (level>0) level= level * qmul + qadd;
782 else level= level * qmul - qadd;
783 #if 0 // waste of time too :(
784 if(level>2048 || level<-2048){
786 return DECODING_AC_LOST;
787 }
788 #endif
789 i+= run + 1;
790 if(last) i+=192;
791 #ifdef ERROR_DETAILS
792 if(run==66)
794 else if((i>62 && i<192) || i>192+63)
796 #endif
797 } else {
798 /* second escape */
801 i+= run + rl->
max_run[run>>7][level/qmul] + run_diff;
//FIXME opt indexing
804 #ifdef ERROR_DETAILS
805 if(run==66)
807 else if((i>62 && i<192) || i>192+63)
809 #endif
810 }
811 } else {
812 /* first escape */
816 level = level + rl->
max_level[run>>7][(run-1)&63] * qmul;
//FIXME opt indexing
819 #ifdef ERROR_DETAILS
820 if(run==66)
822 else if((i>62 && i<192) || i>192+63)
824 #endif
825 }
826 } else {
830 #ifdef ERROR_DETAILS
831 if(run==66)
833 else if((i>62 && i<192) || i>192+63)
835 #endif
836 }
837 if (i > 62){
838 i-= 192;
839 if(i&(~63)){
843 i = 63;
844 break;
845 }else{
847 return -1;
848 }
849 }
850
851 block[scan_table[i]] =
level;
852 break;
853 }
854
855 block[scan_table[i]] =
level;
856 }
858 }
859 not_coded:
863 i = 63; /* XXX: not optimal */
864 }
865 }
868
869 return 0;
870 }
871
873 int *mx_ptr, int *my_ptr)
874 {
876 int code, mx, my;
877
879
881 if (code < 0){
883 return -1;
884 }
888 } else {
891 }
892
893 mx += *mx_ptr - 32;
894 my += *my_ptr - 32;
895 /* WARNING : they do not do exactly modulo encoding */
896 if (mx <= -64)
897 mx += 64;
898 else if (mx >= 64)
899 mx -= 64;
900
901 if (my <= -64)
902 my += 64;
903 else if (my >= 64)
904 my -= 64;
905 *mx_ptr = mx;
906 *my_ptr = my;
907 return 0;
908 }
909
920 .max_lowres = 3,
922 };
923
934 .max_lowres = 3,
936 };
937
948 .max_lowres = 3,
950 };
951
962 .max_lowres = 3,
964 };