Mercurial > libavcodec.hg
comparison msmpeg4.c @ 457:583dcee270d2 libavcodec
msmpeg4v1 decoding
author | michaelni |
---|---|
date | Sun, 02 Jun 2002 12:22:30 +0000 |
parents | 000aeeac27a2 |
children | 1e23eae32087 |
comparison
equal
deleted
inserted
replaced
456:c006ee838856 | 457:583dcee270d2 |
---|---|
14 * | 14 * |
15 * You should have received a copy of the GNU Lesser General Public | 15 * You should have received a copy of the GNU Lesser General Public |
16 * License along with this library; if not, write to the Free Software | 16 * License along with this library; if not, write to the Free Software |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 * | 18 * |
19 * msmpeg4v2 stuff by Michael Niedermayer <michaelni@gmx.at> | 19 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at> |
20 */ | 20 */ |
21 #include "avcodec.h" | 21 #include "avcodec.h" |
22 #include "dsputil.h" | 22 #include "dsputil.h" |
23 #include "mpegvideo.h" | 23 #include "mpegvideo.h" |
24 | 24 |
157 put_bits(pb, 1, 1); | 157 put_bits(pb, 1, 1); |
158 put_bits(pb, 1, (n >= 2)); | 158 put_bits(pb, 1, (n >= 2)); |
159 } | 159 } |
160 } | 160 } |
161 | 161 |
162 /* write MSMPEG4 V3 compatible frame header */ | 162 /* write MSMPEG4 compatible frame header */ |
163 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) | 163 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) |
164 { | 164 { |
165 int i; | 165 int i; |
166 | 166 |
167 align_put_bits(&s->pb); | 167 align_put_bits(&s->pb); |
169 put_bits(&s->pb, 2, s->pict_type - 1); | 169 put_bits(&s->pb, 2, s->pict_type - 1); |
170 | 170 |
171 put_bits(&s->pb, 5, s->qscale); | 171 put_bits(&s->pb, 5, s->qscale); |
172 | 172 |
173 s->rl_table_index = 2; | 173 s->rl_table_index = 2; |
174 if(s->msmpeg4_version==2) | 174 if(s->msmpeg4_version<=2) |
175 s->rl_chroma_table_index = 2; /* only for I frame */ | 175 s->rl_chroma_table_index = 2; /* only for I frame */ |
176 else | 176 else |
177 s->rl_chroma_table_index = 1; /* only for I frame */ | 177 s->rl_chroma_table_index = 1; /* only for I frame */ |
178 | 178 |
179 s->dc_table_index = 1; | 179 s->dc_table_index = 1; |
181 s->use_skip_mb_code = 1; /* only if P frame */ | 181 s->use_skip_mb_code = 1; /* only if P frame */ |
182 | 182 |
183 if (s->pict_type == I_TYPE) { | 183 if (s->pict_type == I_TYPE) { |
184 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */ | 184 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */ |
185 | 185 |
186 if(s->msmpeg4_version!=2){ | 186 if(s->msmpeg4_version>2){ |
187 code012(&s->pb, s->rl_chroma_table_index); | 187 code012(&s->pb, s->rl_chroma_table_index); |
188 code012(&s->pb, s->rl_table_index); | 188 code012(&s->pb, s->rl_table_index); |
189 | 189 |
190 put_bits(&s->pb, 1, s->dc_table_index); | 190 put_bits(&s->pb, 1, s->dc_table_index); |
191 } | 191 } |
192 s->no_rounding = 1; | 192 s->no_rounding = 1; |
193 } else { | 193 } else { |
194 put_bits(&s->pb, 1, s->use_skip_mb_code); | 194 put_bits(&s->pb, 1, s->use_skip_mb_code); |
195 | 195 |
196 s->rl_chroma_table_index = s->rl_table_index; | 196 s->rl_chroma_table_index = s->rl_table_index; |
197 if(s->msmpeg4_version!=2){ | 197 if(s->msmpeg4_version>2){ |
198 code012(&s->pb, s->rl_table_index); | 198 code012(&s->pb, s->rl_table_index); |
199 | 199 |
200 put_bits(&s->pb, 1, s->dc_table_index); | 200 put_bits(&s->pb, 1, s->dc_table_index); |
201 | 201 |
202 put_bits(&s->pb, 1, s->mv_table_index); | 202 put_bits(&s->pb, 1, s->mv_table_index); |
226 #endif | 226 #endif |
227 } | 227 } |
228 | 228 |
229 void msmpeg4_encode_ext_header(MpegEncContext * s) | 229 void msmpeg4_encode_ext_header(MpegEncContext * s) |
230 { | 230 { |
231 s->flipflop_rounding=1; | |
232 s->bitrate= 910; // FIXME | |
233 | |
234 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29 | 231 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29 |
235 | 232 |
236 put_bits(&s->pb, 11, s->bitrate); | 233 put_bits(&s->pb, 11, MIN(s->bit_rate, 2047)); |
237 | 234 |
238 put_bits(&s->pb, 1, s->flipflop_rounding); | 235 if(s->msmpeg4_version<3) |
236 s->flipflop_rounding=0; | |
237 else{ | |
238 s->flipflop_rounding=1; | |
239 put_bits(&s->pb, 1, s->flipflop_rounding); | |
240 } | |
239 } | 241 } |
240 | 242 |
241 /* predict coded block */ | 243 /* predict coded block */ |
242 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr) | 244 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr) |
243 { | 245 { |
326 return; | 328 return; |
327 } | 329 } |
328 if (s->use_skip_mb_code) | 330 if (s->use_skip_mb_code) |
329 put_bits(&s->pb, 1, 0); /* mb coded */ | 331 put_bits(&s->pb, 1, 0); /* mb coded */ |
330 | 332 |
331 if(s->msmpeg4_version==2){ | 333 if(s->msmpeg4_version<=2){ |
332 put_bits(&s->pb, | 334 put_bits(&s->pb, |
333 v2_mb_type[cbp&3][1], | 335 v2_mb_type[cbp&3][1], |
334 v2_mb_type[cbp&3][0]); | 336 v2_mb_type[cbp&3][0]); |
335 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C; | 337 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C; |
336 else coded_cbp= cbp; | 338 else coded_cbp= cbp; |
371 #if 0 | 373 #if 0 |
372 if (coded_cbp) | 374 if (coded_cbp) |
373 printf("cbp=%x %x\n", cbp, coded_cbp); | 375 printf("cbp=%x %x\n", cbp, coded_cbp); |
374 #endif | 376 #endif |
375 | 377 |
376 if(s->msmpeg4_version==2){ | 378 if(s->msmpeg4_version<=2){ |
377 if (s->pict_type == I_TYPE) { | 379 if (s->pict_type == I_TYPE) { |
378 put_bits(&s->pb, | 380 put_bits(&s->pb, |
379 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]); | 381 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]); |
380 } else { | 382 } else { |
381 if (s->use_skip_mb_code) | 383 if (s->use_skip_mb_code) |
423 s->y_dc_scale = s->qscale + 8; | 425 s->y_dc_scale = s->qscale + 8; |
424 s->c_dc_scale = (s->qscale + 13)>>1; | 426 s->c_dc_scale = (s->qscale + 13)>>1; |
425 } | 427 } |
426 } | 428 } |
427 | 429 |
430 static int msmpeg4v1_pred_dc(MpegEncContext * s, int n, | |
431 INT32 **dc_val_ptr) | |
432 { | |
433 int i; | |
434 | |
435 if (n < 4) { | |
436 i= 0; | |
437 } else { | |
438 i= n-3; | |
439 } | |
440 | |
441 *dc_val_ptr= &s->last_dc[i]; | |
442 return s->last_dc[i]; | |
443 } | |
444 | |
428 /* dir = 0: left, dir = 1: top prediction */ | 445 /* dir = 0: left, dir = 1: top prediction */ |
429 static int msmpeg4_pred_dc(MpegEncContext * s, int n, | 446 static int msmpeg4_pred_dc(MpegEncContext * s, int n, |
430 INT16 **dc_val_ptr, int *dir_ptr) | 447 INT16 **dc_val_ptr, int *dir_ptr) |
431 { | 448 { |
432 int a, b, c, wrap, pred, scale; | 449 int a, b, c, wrap, pred, scale; |
436 if (n < 4) { | 453 if (n < 4) { |
437 scale = s->y_dc_scale; | 454 scale = s->y_dc_scale; |
438 } else { | 455 } else { |
439 scale = s->c_dc_scale; | 456 scale = s->c_dc_scale; |
440 } | 457 } |
458 | |
441 wrap = s->block_wrap[n]; | 459 wrap = s->block_wrap[n]; |
442 dc_val= s->dc_val[0] + s->block_index[n]; | 460 dc_val= s->dc_val[0] + s->block_index[n]; |
443 | 461 |
444 /* B C | 462 /* B C |
445 * A X | 463 * A X |
505 | 523 |
506 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) | 524 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) |
507 { | 525 { |
508 int sign, code; | 526 int sign, code; |
509 int pred; | 527 int pred; |
510 INT16 *dc_val; | 528 |
511 | 529 if(s->msmpeg4_version==1){ |
512 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | 530 INT32 *dc_val; |
513 | 531 pred = msmpeg4v1_pred_dc(s, n, &dc_val); |
514 /* update predictor */ | 532 |
515 if (n < 4) { | 533 /* update predictor */ |
516 *dc_val = level * s->y_dc_scale; | 534 *dc_val= level; |
517 } else { | 535 }else{ |
518 *dc_val = level * s->c_dc_scale; | 536 INT16 *dc_val; |
537 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
538 | |
539 /* update predictor */ | |
540 if (n < 4) { | |
541 *dc_val = level * s->y_dc_scale; | |
542 } else { | |
543 *dc_val = level * s->c_dc_scale; | |
544 } | |
519 } | 545 } |
520 | 546 |
521 /* do the prediction */ | 547 /* do the prediction */ |
522 level -= pred; | 548 level -= pred; |
523 | 549 |
524 if(s->msmpeg4_version==2){ | 550 if(s->msmpeg4_version<=2){ |
525 if (n < 4) { | 551 if (n < 4) { |
526 put_bits(&s->pb, | 552 put_bits(&s->pb, |
527 v2_dc_lum_table[level+256][1], | 553 v2_dc_lum_table[level+256][1], |
528 v2_dc_lum_table[level+256][0]); | 554 v2_dc_lum_table[level+256][0]); |
529 }else{ | 555 }else{ |
586 run_diff = 0; | 612 run_diff = 0; |
587 set_stat(ST_INTRA_AC); | 613 set_stat(ST_INTRA_AC); |
588 } else { | 614 } else { |
589 i = 0; | 615 i = 0; |
590 rl = &rl_table[3 + s->rl_table_index]; | 616 rl = &rl_table[3 + s->rl_table_index]; |
591 if(s->msmpeg4_version==2) | 617 if(s->msmpeg4_version<=2) |
592 run_diff = 0; | 618 run_diff = 0; |
593 else | 619 else |
594 run_diff = 1; | 620 run_diff = 1; |
595 set_stat(ST_INTER_AC); | 621 set_stat(ST_INTER_AC); |
596 } | 622 } |
666 static VLC v2_dc_chroma_vlc; | 692 static VLC v2_dc_chroma_vlc; |
667 static VLC cbpy_vlc; | 693 static VLC cbpy_vlc; |
668 static VLC v2_intra_cbpc_vlc; | 694 static VLC v2_intra_cbpc_vlc; |
669 static VLC v2_mb_type_vlc; | 695 static VLC v2_mb_type_vlc; |
670 static VLC v2_mv_vlc; | 696 static VLC v2_mv_vlc; |
697 static VLC v1_intra_cbpc_vlc; | |
698 static VLC v1_inter_cbpc_vlc; | |
671 | 699 |
672 /* this table is practically identical to the one from h263 except that its inverted */ | 700 /* this table is practically identical to the one from h263 except that its inverted */ |
673 static void init_h263_dc_for_msmpeg4() | 701 static void init_h263_dc_for_msmpeg4() |
674 { | 702 { |
675 static int inited=0; | 703 static int inited=0; |
784 &table_mb_non_intra[0][1], 8, 4, | 812 &table_mb_non_intra[0][1], 8, 4, |
785 &table_mb_non_intra[0][0], 8, 4); | 813 &table_mb_non_intra[0][0], 8, 4); |
786 init_vlc(&mb_intra_vlc, 9, 64, | 814 init_vlc(&mb_intra_vlc, 9, 64, |
787 &table_mb_intra[0][1], 4, 2, | 815 &table_mb_intra[0][1], 4, 2, |
788 &table_mb_intra[0][0], 4, 2); | 816 &table_mb_intra[0][0], 4, 2); |
817 | |
818 init_vlc(&v1_intra_cbpc_vlc, 6, 8, | |
819 intra_MCBPC_bits, 1, 1, | |
820 intra_MCBPC_code, 1, 1); | |
821 init_vlc(&v1_inter_cbpc_vlc, 6, 25, | |
822 inter_MCBPC_bits, 1, 1, | |
823 inter_MCBPC_code, 1, 1); | |
824 | |
789 return 0; | 825 return 0; |
790 } | 826 } |
791 | 827 |
792 static int decode012(GetBitContext *gb) | 828 static int decode012(GetBitContext *gb) |
793 { | 829 { |
811 // get_bits1(&s->gb); | 847 // get_bits1(&s->gb); |
812 printf("END\n"); | 848 printf("END\n"); |
813 return -1; | 849 return -1; |
814 } | 850 } |
815 #endif | 851 #endif |
852 | |
853 if(s->msmpeg4_version==1){ | |
854 int start_code, num; | |
855 start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16); | |
856 if(start_code!=0x00000100){ | |
857 fprintf(stderr, "invalid startcode\n"); | |
858 return -1; | |
859 } | |
860 | |
861 num= get_bits(&s->gb, 5); // frame number */ | |
862 } | |
863 | |
816 s->pict_type = get_bits(&s->gb, 2) + 1; | 864 s->pict_type = get_bits(&s->gb, 2) + 1; |
817 if (s->pict_type != I_TYPE && | 865 if (s->pict_type != I_TYPE && |
818 s->pict_type != P_TYPE) | 866 s->pict_type != P_TYPE){ |
867 fprintf(stderr, "invalid picture type\n"); | |
819 return -1; | 868 return -1; |
869 } | |
820 | 870 |
821 s->qscale = get_bits(&s->gb, 5); | 871 s->qscale = get_bits(&s->gb, 5); |
822 | 872 |
823 if (s->pict_type == I_TYPE) { | 873 if (s->pict_type == I_TYPE) { |
824 code = get_bits(&s->gb, 5); | 874 code = get_bits(&s->gb, 5); |
825 /* 0x17: one slice, 0x18: two slices */ | 875 if(s->msmpeg4_version==1){ |
826 if (code < 0x17) | 876 if(code==0 || code>s->mb_height){ |
827 return -1; | 877 fprintf(stderr, "invalid slice height %d\n", code); |
828 s->slice_height = s->mb_height / (code - 0x16); | 878 return -1; |
879 } | |
880 | |
881 s->slice_height = code; | |
882 }else{ | |
883 /* 0x17: one slice, 0x18: two slices, ... */ | |
884 if (code < 0x17) | |
885 return -1; | |
886 | |
887 s->slice_height = s->mb_height / (code - 0x16); | |
888 } | |
829 | 889 |
830 switch(s->msmpeg4_version){ | 890 switch(s->msmpeg4_version){ |
891 case 1: | |
831 case 2: | 892 case 2: |
832 s->rl_chroma_table_index = 2; | 893 s->rl_chroma_table_index = 2; |
833 s->rl_table_index = 2; | 894 s->rl_table_index = 2; |
834 | 895 |
835 s->dc_table_index = 0; //not used | 896 s->dc_table_index = 0; //not used |
860 s->qscale, | 921 s->qscale, |
861 s->rl_chroma_table_index, | 922 s->rl_chroma_table_index, |
862 s->rl_table_index, | 923 s->rl_table_index, |
863 s->dc_table_index);*/ | 924 s->dc_table_index);*/ |
864 } else { | 925 } else { |
865 s->use_skip_mb_code = get_bits1(&s->gb); | |
866 | 926 |
867 if(s->msmpeg4_version==2){ | 927 switch(s->msmpeg4_version){ |
928 case 1: | |
929 case 2: | |
930 if(s->msmpeg4_version==1) | |
931 s->use_skip_mb_code = 1; | |
932 else | |
933 s->use_skip_mb_code = get_bits1(&s->gb); | |
868 s->rl_table_index = 2; | 934 s->rl_table_index = 2; |
869 s->rl_chroma_table_index = s->rl_table_index; | 935 s->rl_chroma_table_index = s->rl_table_index; |
870 | |
871 s->dc_table_index = 0; //not used | 936 s->dc_table_index = 0; //not used |
872 | |
873 s->mv_table_index = 0; | 937 s->mv_table_index = 0; |
874 }else{ | 938 break; |
939 case 3: | |
940 s->use_skip_mb_code = get_bits1(&s->gb); | |
875 s->rl_table_index = decode012(&s->gb); | 941 s->rl_table_index = decode012(&s->gb); |
876 s->rl_chroma_table_index = s->rl_table_index; | 942 s->rl_chroma_table_index = s->rl_table_index; |
877 | 943 |
878 s->dc_table_index = get_bits1(&s->gb); | 944 s->dc_table_index = get_bits1(&s->gb); |
879 | 945 |
880 s->mv_table_index = get_bits1(&s->gb); | 946 s->mv_table_index = get_bits1(&s->gb); |
947 break; | |
881 } | 948 } |
882 /* printf(" %d %d %d %d %d \n", | 949 /* printf(" %d %d %d %d %d \n", |
883 s->use_skip_mb_code, | 950 s->use_skip_mb_code, |
884 s->rl_table_index, | 951 s->rl_table_index, |
885 s->rl_chroma_table_index, | 952 s->rl_chroma_table_index, |
912 return 0; | 979 return 0; |
913 } | 980 } |
914 | 981 |
915 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) | 982 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
916 { | 983 { |
984 int left= buf_size*8 - get_bits_count(&s->gb); | |
985 int length= s->msmpeg4_version>=3 ? 17 : 16; | |
917 /* the alt_bitstream reader could read over the end so we need to check it */ | 986 /* the alt_bitstream reader could read over the end so we need to check it */ |
918 if(get_bits_count(&s->gb) + 16 < buf_size*8) | 987 if(left>=length && left<length+8) |
919 { | 988 { |
920 int fps; | 989 int fps; |
921 | 990 |
922 fps= get_bits(&s->gb, 5); | 991 fps= get_bits(&s->gb, 5); |
923 s->bitrate= get_bits(&s->gb, 11); | 992 s->bit_rate= get_bits(&s->gb, 11); |
924 s->flipflop_rounding= get_bits1(&s->gb); | 993 if(s->msmpeg4_version>=3) |
925 | 994 s->flipflop_rounding= get_bits1(&s->gb); |
926 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding); | 995 else |
996 s->flipflop_rounding= 0; | |
997 | |
998 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate, s->flipflop_rounding); | |
999 } | |
1000 else if(left<length+8) | |
1001 { | |
1002 s->flipflop_rounding= 0; | |
1003 printf("ext header missing, %d left\n", left); | |
927 } | 1004 } |
928 else | 1005 else |
929 { | 1006 { |
930 s->flipflop_rounding= 0; | 1007 fprintf(stderr, "I frame too long, ignoring ext header\n"); |
931 s->bitrate= 0; | |
932 } | 1008 } |
933 | 1009 |
934 return 0; | 1010 return 0; |
935 } | 1011 } |
936 | 1012 |
978 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) | 1054 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) |
979 { | 1055 { |
980 int code, val, sign, shift; | 1056 int code, val, sign, shift; |
981 | 1057 |
982 code = get_vlc(&s->gb, &v2_mv_vlc); | 1058 code = get_vlc(&s->gb, &v2_mv_vlc); |
1059 // printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred); | |
983 if (code < 0) | 1060 if (code < 0) |
984 return 0xffff; | 1061 return 0xffff; |
985 | 1062 |
986 if (code == 0) | 1063 if (code == 0) |
987 return pred; | 1064 return pred; |
991 if (shift > 0) | 1068 if (shift > 0) |
992 val |= get_bits(&s->gb, shift); | 1069 val |= get_bits(&s->gb, shift); |
993 val++; | 1070 val++; |
994 if (sign) | 1071 if (sign) |
995 val = -val; | 1072 val = -val; |
1073 | |
996 val += pred; | 1074 val += pred; |
997 | |
998 if (val <= -64) | 1075 if (val <= -64) |
999 val += 64; | 1076 val += 64; |
1000 else if (val >= 64) | 1077 else if (val >= 64) |
1001 val -= 64; | 1078 val -= 64; |
1002 | 1079 |
1003 return val; | 1080 return val; |
1004 } | 1081 } |
1005 | 1082 |
1006 | 1083 |
1007 int msmpeg4v2_decode_mb(MpegEncContext *s, | 1084 static int msmpeg4v12_decode_mb(MpegEncContext *s, |
1008 DCTELEM block[6][64]) | 1085 DCTELEM block[6][64]) |
1009 { | 1086 { |
1010 int cbp, code, i; | 1087 int cbp, code, i; |
1011 if (s->pict_type == P_TYPE) { | 1088 if (s->pict_type == P_TYPE) { |
1012 if (s->use_skip_mb_code) { | 1089 if (s->use_skip_mb_code) { |
1022 s->mb_skiped = 1; | 1099 s->mb_skiped = 1; |
1023 return 0; | 1100 return 0; |
1024 } | 1101 } |
1025 } | 1102 } |
1026 | 1103 |
1027 code = get_vlc(&s->gb, &v2_mb_type_vlc); | 1104 if(s->msmpeg4_version==2) |
1105 code = get_vlc(&s->gb, &v2_mb_type_vlc); | |
1106 else | |
1107 code = get_vlc(&s->gb, &v1_inter_cbpc_vlc); | |
1108 if(code<0 || code>7){ | |
1109 fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y); | |
1110 return -1; | |
1111 } | |
1112 | |
1028 s->mb_intra = code >>2; | 1113 s->mb_intra = code >>2; |
1029 | 1114 |
1030 cbp = code & 0x3; | 1115 cbp = code & 0x3; |
1031 } else { | 1116 } else { |
1032 s->mb_intra = 1; | 1117 s->mb_intra = 1; |
1033 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc); | 1118 if(s->msmpeg4_version==2) |
1119 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc); | |
1120 else | |
1121 cbp= get_vlc(&s->gb, &v1_intra_cbpc_vlc); | |
1122 if(cbp<0 || cbp>3){ | |
1123 fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); | |
1124 return -1; | |
1125 } | |
1034 } | 1126 } |
1035 | 1127 |
1036 if (!s->mb_intra) { | 1128 if (!s->mb_intra) { |
1037 int mx, my; | 1129 int mx, my, cbpy; |
1038 | 1130 |
1039 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | 1131 cbpy= get_vlc(&s->gb, &cbpy_vlc); |
1040 if((cbp&3) != 3) cbp^= 0x3C; | 1132 if(cbpy<0){ |
1133 fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); | |
1134 return -1; | |
1135 } | |
1136 | |
1137 cbp|= cbpy<<2; | |
1138 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C; | |
1041 | 1139 |
1042 h263_pred_motion(s, 0, &mx, &my); | 1140 h263_pred_motion(s, 0, &mx, &my); |
1043 mx= msmpeg4v2_decode_motion(s, mx, 1); | 1141 mx= msmpeg4v2_decode_motion(s, mx, 1); |
1044 my= msmpeg4v2_decode_motion(s, my, 1); | 1142 my= msmpeg4v2_decode_motion(s, my, 1); |
1045 | 1143 |
1046 s->mv_dir = MV_DIR_FORWARD; | 1144 s->mv_dir = MV_DIR_FORWARD; |
1047 s->mv_type = MV_TYPE_16X16; | 1145 s->mv_type = MV_TYPE_16X16; |
1048 s->mv[0][0][0] = mx; | 1146 s->mv[0][0][0] = mx; |
1049 s->mv[0][0][1] = my; | 1147 s->mv[0][0][1] = my; |
1050 } else { | 1148 } else { |
1051 s->ac_pred = get_bits1(&s->gb); | 1149 if(s->msmpeg4_version==2){ |
1052 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | 1150 s->ac_pred = get_bits1(&s->gb); |
1151 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors | |
1152 } else{ | |
1153 s->ac_pred = 0; | |
1154 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors | |
1155 if(s->pict_type==P_TYPE) cbp^=0x3C; | |
1156 } | |
1053 } | 1157 } |
1054 | 1158 |
1055 for (i = 0; i < 6; i++) { | 1159 for (i = 0; i < 6; i++) { |
1056 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | 1160 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) |
1057 { | 1161 { |
1058 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | 1162 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); |
1059 return -1; | 1163 return -1; |
1060 } | 1164 } |
1061 } | 1165 } |
1062 return 0; | 1166 return 0; |
1063 } | 1167 } |
1096 } else { | 1200 } else { |
1097 s->first_slice_line = 0; | 1201 s->first_slice_line = 0; |
1098 } | 1202 } |
1099 } | 1203 } |
1100 | 1204 |
1101 if(s->msmpeg4_version==2) return msmpeg4v2_decode_mb(s, block); //FIXME merge if possible | 1205 if(s->msmpeg4_version<=2) return msmpeg4v12_decode_mb(s, block); //FIXME export function & call from outside perhaps |
1102 | 1206 |
1103 if (s->pict_type == P_TYPE) { | 1207 if (s->pict_type == P_TYPE) { |
1104 set_stat(ST_INTER_MB); | 1208 set_stat(ST_INTER_MB); |
1105 if (s->use_skip_mb_code) { | 1209 if (s->use_skip_mb_code) { |
1106 if (get_bits1(&s->gb)) { | 1210 if (get_bits1(&s->gb)) { |
1159 } | 1263 } |
1160 | 1264 |
1161 for (i = 0; i < 6; i++) { | 1265 for (i = 0; i < 6; i++) { |
1162 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | 1266 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) |
1163 { | 1267 { |
1164 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | 1268 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); |
1165 // return -1; | 1269 return -1; |
1166 } | 1270 } |
1167 } | 1271 } |
1272 | |
1168 return 0; | 1273 return 0; |
1169 } | 1274 } |
1170 | 1275 |
1171 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | 1276 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, |
1172 int n, int coded) | 1277 int n, int coded) |
1182 qadd=0; | 1287 qadd=0; |
1183 | 1288 |
1184 /* DC coef */ | 1289 /* DC coef */ |
1185 set_stat(ST_DC); | 1290 set_stat(ST_DC); |
1186 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | 1291 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); |
1187 if (level < 0) | 1292 if (level < 0){ |
1293 fprintf(stderr, "dc overflow-\n"); | |
1188 return -1; | 1294 return -1; |
1189 block[0] = level; | 1295 } |
1190 if (n < 4) { | 1296 if (n < 4) { |
1191 rl = &rl_table[s->rl_table_index]; | 1297 rl = &rl_table[s->rl_table_index]; |
1298 if(level > 256*s->y_dc_scale){ | |
1299 fprintf(stderr, "dc overflow+\n"); | |
1300 return -1; | |
1301 } | |
1192 } else { | 1302 } else { |
1193 rl = &rl_table[3 + s->rl_chroma_table_index]; | 1303 rl = &rl_table[3 + s->rl_chroma_table_index]; |
1194 } | 1304 if(level > 256*s->c_dc_scale){ |
1305 fprintf(stderr, "dc overflow+\n"); | |
1306 return -1; | |
1307 } | |
1308 } | |
1309 block[0] = level; | |
1195 | 1310 |
1196 run_diff = 0; | 1311 run_diff = 0; |
1197 i = 1; | 1312 i = 1; |
1198 if (!coded) { | 1313 if (!coded) { |
1199 goto not_coded; | 1314 goto not_coded; |
1230 code = get_vlc(&s->gb, &rl->vlc); | 1345 code = get_vlc(&s->gb, &rl->vlc); |
1231 if (code < 0) | 1346 if (code < 0) |
1232 return -1; | 1347 return -1; |
1233 if (code == rl->n) { | 1348 if (code == rl->n) { |
1234 /* escape */ | 1349 /* escape */ |
1235 if (get_bits1(&s->gb) == 0) { | 1350 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) { |
1236 if (get_bits1(&s->gb) == 0) { | 1351 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) { |
1237 /* third escape */ | 1352 /* third escape */ |
1238 last = get_bits1(&s->gb); | 1353 last = get_bits1(&s->gb); |
1239 run = get_bits(&s->gb, 6); | 1354 run = get_bits(&s->gb, 6); |
1240 level = get_bits(&s->gb, 8); | 1355 level = get_bits(&s->gb, 8); |
1241 level = (level << 24) >> 24; /* sign extend */ | 1356 level = (level << 24) >> 24; /* sign extend */ |
1357 #if 0 // waste of time / this will detect very few errors | |
1358 { | |
1359 const int abs_level= ABS(level); | |
1360 const int run1= run - rl->max_run[last][abs_level] - run_diff; | |
1361 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){ | |
1362 if(abs_level <= rl->max_level[last][run]){ | |
1363 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n"); | |
1364 return DECODING_AC_LOST; | |
1365 } | |
1366 if(abs_level <= rl->max_level[last][run]*2){ | |
1367 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n"); | |
1368 return DECODING_AC_LOST; | |
1369 } | |
1370 if(abs_level <= rl->max_level[last][run1] && 0){ | |
1371 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n"); | |
1372 return DECODING_AC_LOST; | |
1373 } | |
1374 } | |
1375 } | |
1376 #endif | |
1242 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; | 1377 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
1243 if (level>0) level= level * qmul + qadd; | 1378 if (level>0) level= level * qmul + qadd; |
1244 else level= level * qmul - qadd; | 1379 else level= level * qmul - qadd; |
1380 #if 0 // waste of time too :( | |
1381 if(level>2048 || level<-2048){ | |
1382 fprintf(stderr, "|level| overflow in 3. esc\n"); | |
1383 return DECODING_AC_LOST; | |
1384 } | |
1385 #endif | |
1245 } else { | 1386 } else { |
1246 /* second escape */ | 1387 /* second escape */ |
1247 code = get_vlc(&s->gb, &rl->vlc); | 1388 code = get_vlc(&s->gb, &rl->vlc); |
1248 if (code < 0 || code >= rl->n) | 1389 if (code < 0 || code >= rl->n) |
1249 return -1; | 1390 return -1; |
1276 level = -level; | 1417 level = -level; |
1277 } | 1418 } |
1278 i += run; | 1419 i += run; |
1279 if (i >= 64) | 1420 if (i >= 64) |
1280 return -1; | 1421 return -1; |
1281 //printf("RL:%d %d %d ", run, level, last); | 1422 |
1282 j = scan_table[i]; | 1423 j = scan_table[i]; |
1283 block[j] = level; | 1424 block[j] = level; |
1284 i++; | 1425 i++; |
1285 if (last) | 1426 if (last) |
1286 break; | 1427 break; |
1298 } | 1439 } |
1299 | 1440 |
1300 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | 1441 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) |
1301 { | 1442 { |
1302 int level, pred; | 1443 int level, pred; |
1303 INT16 *dc_val; | 1444 |
1304 | 1445 if(s->msmpeg4_version<=2){ |
1305 if(s->msmpeg4_version==2){ | |
1306 if (n < 4) { | 1446 if (n < 4) { |
1307 level = get_vlc(&s->gb, &v2_dc_lum_vlc); | 1447 level = get_vlc(&s->gb, &v2_dc_lum_vlc); |
1308 } else { | 1448 } else { |
1309 level = get_vlc(&s->gb, &v2_dc_chroma_vlc); | 1449 level = get_vlc(&s->gb, &v2_dc_chroma_vlc); |
1310 } | 1450 } |
1315 if (n < 4) { | 1455 if (n < 4) { |
1316 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); | 1456 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); |
1317 } else { | 1457 } else { |
1318 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); | 1458 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); |
1319 } | 1459 } |
1320 if (level < 0) | 1460 if (level < 0){ |
1461 fprintf(stderr, "illegal dc vlc\n"); | |
1321 return -1; | 1462 return -1; |
1463 } | |
1322 | 1464 |
1323 if (level == DC_MAX) { | 1465 if (level == DC_MAX) { |
1324 level = get_bits(&s->gb, 8); | 1466 level = get_bits(&s->gb, 8); |
1325 if (get_bits1(&s->gb)) | 1467 if (get_bits1(&s->gb)) |
1326 level = -level; | 1468 level = -level; |
1328 if (get_bits1(&s->gb)) | 1470 if (get_bits1(&s->gb)) |
1329 level = -level; | 1471 level = -level; |
1330 } | 1472 } |
1331 } | 1473 } |
1332 | 1474 |
1333 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | 1475 if(s->msmpeg4_version==1){ |
1334 level += pred; | 1476 INT32 *dc_val; |
1335 | 1477 pred = msmpeg4v1_pred_dc(s, n, &dc_val); |
1336 /* update predictor */ | 1478 level += pred; |
1337 if (n < 4) { | 1479 |
1338 *dc_val = level * s->y_dc_scale; | 1480 /* update predictor */ |
1339 } else { | 1481 *dc_val= level; |
1340 *dc_val = level * s->c_dc_scale; | 1482 }else{ |
1483 INT16 *dc_val; | |
1484 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
1485 level += pred; | |
1486 | |
1487 /* update predictor */ | |
1488 if (n < 4) { | |
1489 *dc_val = level * s->y_dc_scale; | |
1490 } else { | |
1491 *dc_val = level * s->c_dc_scale; | |
1492 } | |
1341 } | 1493 } |
1342 | 1494 |
1343 return level; | 1495 return level; |
1344 } | 1496 } |
1345 | 1497 |