Revision 7bc9090a libavcodec/mpeg12.c

View differences:

libavcodec/mpeg12.c
29 29

  
30 30
#include "mpeg12data.h"
31 31

  
32
#if 1
33
#define PRINT_QP(a, b) {}
34
#else
35
#define PRINT_QP(a, b) printf(a, b)
36
#endif
37 32

  
38 33
/* Start codes. */
39 34
#define SEQ_END_CODE		0x000001b7
......
833 828
                 &mbPatTable[0][1], 2, 1,
834 829
                 &mbPatTable[0][0], 2, 1);
835 830
        
836
        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32, 
831
        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7, 
837 832
                 &table_mb_ptype[0][1], 2, 1,
838 833
                 &table_mb_ptype[0][0], 2, 1);
839
        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32, 
834
        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11, 
840 835
                 &table_mb_btype[0][1], 2, 1,
841 836
                 &table_mb_btype[0][0], 2, 1);
842 837
        init_rl(&rl_mpeg1);
......
887 882
    assert(s->mb_skiped==0);
888 883

  
889 884
    if (s->mb_skip_run-- != 0) {
885
        if(s->pict_type == I_TYPE){
886
            fprintf(stderr, "skiped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
887
            return -1;
888
        }
889
    
890 890
        /* skip mb */
891 891
        s->mb_intra = 0;
892 892
        for(i=0;i<6;i++)
......
899 899
            s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
900 900
            s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
901 901
            s->mb_skiped = 1;
902
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
902 903
        } else {
903 904
            /* if B type, reuse previous vectors and directions */
904 905
            s->mv[0][0][0] = s->last_mv[0][0][0];
......
906 907
            s->mv[1][0][0] = s->last_mv[1][0][0];
907 908
            s->mv[1][0][1] = s->last_mv[1][0][1];
908 909

  
910
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= 
911
                s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1] | MB_TYPE_SKIP;
912
//            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
913

  
909 914
            if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0) 
910 915
                s->mb_skiped = 1;
911 916
        }
......
919 924
        if (get_bits1(&s->gb) == 0) {
920 925
            if (get_bits1(&s->gb) == 0)
921 926
                return -1;
922
            mb_type = MB_QUANT | MB_INTRA;
927
            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
923 928
        } else {
924
            mb_type = MB_INTRA;
929
            mb_type = MB_TYPE_INTRA;
925 930
        }
926 931
        break;
927 932
    case P_TYPE:
......
930 935
            fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
931 936
            return -1;
932 937
        }
938
        mb_type = ptype2mb_type[ mb_type ];
933 939
        break;
934 940
    case B_TYPE:
935 941
        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
......
937 943
            fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
938 944
            return -1;
939 945
        }
946
        mb_type = btype2mb_type[ mb_type ];
940 947
        break;
941 948
    }
942 949
    dprintf("mb_type=%x\n", mb_type);
943
    motion_type = 0; /* avoid warning */
944
    if (mb_type & (MB_FOR|MB_BACK)) {
945
        /* get additionnal motion vector type */
946
        if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) 
947
            motion_type = MT_FRAME;
948
        else
949
            motion_type = get_bits(&s->gb, 2);
950
    }
951
    /* compute dct type */
952
    if (s->picture_structure == PICT_FRAME && 
953
        !s->frame_pred_frame_dct &&
954
        (mb_type & (MB_PAT | MB_INTRA))) {
955
        s->interlaced_dct = get_bits1(&s->gb);
956
#ifdef DEBUG
957
        if (s->interlaced_dct)
958
            printf("interlaced_dct\n");
959
#endif
960
    } else {
961
        s->interlaced_dct = 0; /* frame based */
962
    }
950
//    motion_type = 0; /* avoid warning */
951
    if (IS_INTRA(mb_type)) {
952
        /* compute dct type */
953
        if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
954
            !s->frame_pred_frame_dct) {
955
            s->interlaced_dct = get_bits1(&s->gb);
956
        }
963 957

  
964
    if (mb_type & MB_QUANT) {
965
        s->qscale = get_qscale(s);
966
    }
967
    if (mb_type & MB_INTRA) {
958
        if (IS_QUANT(mb_type))
959
            s->qscale = get_qscale(s);
960
        
968 961
        if (s->concealment_motion_vectors) {
969 962
            /* just parse them */
970 963
            if (s->picture_structure != PICT_FRAME) 
971 964
                skip_bits1(&s->gb); /* field select */
972 965
            mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
973 966
            mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
967
            skip_bits1(&s->gb); /* marker */
974 968
        }
975 969
        s->mb_intra = 1;
976
        cbp = 0x3f;
977 970
        memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
971

  
972
        if (s->mpeg2) {
973
            for(i=0;i<6;i++) {
974
                if (mpeg2_decode_block_intra(s, block[i], i) < 0)
975
                    return -1;
976
            }
977
        } else {
978
            for(i=0;i<6;i++) {
979
                if (mpeg1_decode_block_intra(s, block[i], i) < 0)
980
                    return -1;
981
            }
982
        }
978 983
    } else {
979
        s->mb_intra = 0;
980
        cbp = 0;
981
    }
982
    /* special case of implicit zero motion vector */
983
    if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
984
        s->mv_dir = MV_DIR_FORWARD;
985
        s->mv_type = MV_TYPE_16X16;
986
        s->last_mv[0][0][0] = 0;
987
        s->last_mv[0][0][1] = 0;
988
        s->last_mv[0][1][0] = 0;
989
        s->last_mv[0][1][1] = 0;
990
        s->mv[0][0][0] = 0;
991
        s->mv[0][0][1] = 0;
992
    } else if (mb_type & (MB_FOR | MB_BACK)) {
993
        /* motion vectors */
994
        s->mv_dir = 0;
995
        for(i=0;i<2;i++) {
996
            if (mb_type & (MB_FOR >> i)) {
997
                s->mv_dir |= (MV_DIR_FORWARD >> i);
998
                dprintf("motion_type=%d\n", motion_type);
999
                switch(motion_type) {
1000
                case MT_FRAME: /* or MT_16X8 */
1001
                    if (s->picture_structure == PICT_FRAME) {
1002
                        /* MT_FRAME */
1003
                        s->mv_type = MV_TYPE_16X16;
1004
                        for(k=0;k<2;k++) {
1005
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], 
1006
                                                     s->last_mv[i][0][k]);
1007
                            s->last_mv[i][0][k] = val;
1008
                            s->last_mv[i][1][k] = val;
984
        if (mb_type & MB_TYPE_ZERO_MV){
985
            assert(mb_type & MB_TYPE_PAT);
986

  
987
            /* compute dct type */
988
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
989
                !s->frame_pred_frame_dct) {
990
                s->interlaced_dct = get_bits1(&s->gb);
991
            }
992

  
993
            if (IS_QUANT(mb_type))
994
                s->qscale = get_qscale(s);
995

  
996
            s->mv_dir = MV_DIR_FORWARD;
997
            s->mv_type = MV_TYPE_16X16;
998
            s->last_mv[0][0][0] = 0;
999
            s->last_mv[0][0][1] = 0;
1000
            s->last_mv[0][1][0] = 0;
1001
            s->last_mv[0][1][1] = 0;
1002
            s->mv[0][0][0] = 0;
1003
            s->mv[0][0][1] = 0;
1004
        }else{
1005
            assert(mb_type & MB_TYPE_L0L1);
1006
//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
1007
            /* get additionnal motion vector type */
1008
            if (s->frame_pred_frame_dct) 
1009
                motion_type = MT_FRAME;
1010
            else{
1011
                motion_type = get_bits(&s->gb, 2);
1012
            }
1013

  
1014
            /* compute dct type */
1015
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1016
                !s->frame_pred_frame_dct && IS_PAT(mb_type)) {
1017
                s->interlaced_dct = get_bits1(&s->gb);
1018
            }
1019

  
1020
            if (IS_QUANT(mb_type))
1021
                s->qscale = get_qscale(s);
1022

  
1023
            /* motion vectors */
1024
            s->mv_dir = 0;
1025
            for(i=0;i<2;i++) {
1026
                if (USES_LIST(mb_type, i)) {
1027
                    s->mv_dir |= (MV_DIR_FORWARD >> i);
1028
                    dprintf("motion_type=%d\n", motion_type);
1029
                    switch(motion_type) {
1030
                    case MT_FRAME: /* or MT_16X8 */
1031
                        if (s->picture_structure == PICT_FRAME) {
1032
                            /* MT_FRAME */
1033
                            mb_type |= MB_TYPE_16x16; 
1034
                            s->mv_type = MV_TYPE_16X16;
1035
                            s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] = 
1036
                                mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
1037
                            s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] = 
1038
                                mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
1009 1039
                            /* full_pel: only for mpeg1 */
1010
                            if (s->full_pel[i])
1011
                                val = val << 1;
1012
                            s->mv[i][0][k] = val;
1013
                            dprintf("mv%d: %d\n", k, val);
1040
                            if (s->full_pel[i]){
1041
                                s->mv[i][0][0] <<= 1;
1042
                                s->mv[i][0][1] <<= 1;
1043
                            }
1044
                        } else {
1045
                            /* MT_16X8 */
1046
                            mb_type |= MB_TYPE_16x8; 
1047
                            s->mv_type = MV_TYPE_16X8;
1048
                            for(j=0;j<2;j++) {
1049
                                s->field_select[i][j] = get_bits1(&s->gb);
1050
                                for(k=0;k<2;k++) {
1051
                                    val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1052
                                                             s->last_mv[i][j][k]);
1053
                                    s->last_mv[i][j][k] = val;
1054
                                    s->mv[i][j][k] = val;
1055
                                }
1056
                            }
1014 1057
                        }
1015
                    } else {
1016
                        /* MT_16X8 */
1017
                        s->mv_type = MV_TYPE_16X8;
1018
                        for(j=0;j<2;j++) {
1019
                            s->field_select[i][j] = get_bits1(&s->gb);
1058
                        break;
1059
                    case MT_FIELD:
1060
                        s->mv_type = MV_TYPE_FIELD;
1061
                        if (s->picture_structure == PICT_FRAME) {
1062
                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; 
1063
                            for(j=0;j<2;j++) {
1064
                                s->field_select[i][j] = get_bits1(&s->gb);
1065
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1066
                                                         s->last_mv[i][j][0]);
1067
                                s->last_mv[i][j][0] = val;
1068
                                s->mv[i][j][0] = val;
1069
                                dprintf("fmx=%d\n", val);
1070
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1071
                                                         s->last_mv[i][j][1] >> 1);
1072
                                s->last_mv[i][j][1] = val << 1;
1073
                                s->mv[i][j][1] = val;
1074
                                dprintf("fmy=%d\n", val);
1075
                            }
1076
                        } else {
1077
                            mb_type |= MB_TYPE_16x16; 
1078
                            s->field_select[i][0] = get_bits1(&s->gb);
1020 1079
                            for(k=0;k<2;k++) {
1021 1080
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1022
                                                         s->last_mv[i][j][k]);
1023
                                s->last_mv[i][j][k] = val;
1024
                                s->mv[i][j][k] = val;
1081
                                                         s->last_mv[i][0][k]);
1082
                                s->last_mv[i][0][k] = val;
1083
                                s->last_mv[i][1][k] = val;
1084
                                s->mv[i][0][k] = val;
1025 1085
                            }
1026 1086
                        }
1027
                    }
1028
                    break;
1029
                case MT_FIELD:
1030
                    s->mv_type = MV_TYPE_FIELD;
1031
                    if (s->picture_structure == PICT_FRAME) {
1032
                        for(j=0;j<2;j++) {
1033
                            s->field_select[i][j] = get_bits1(&s->gb);
1034
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1035
                                                     s->last_mv[i][j][0]);
1036
                            s->last_mv[i][j][0] = val;
1037
                            s->mv[i][j][0] = val;
1038
                            dprintf("fmx=%d\n", val);
1039
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1040
                                                     s->last_mv[i][j][1] >> 1);
1041
                            s->last_mv[i][j][1] = val << 1;
1042
                            s->mv[i][j][1] = val;
1043
                            dprintf("fmy=%d\n", val);
1044
                        }
1045
                    } else {
1046
                        s->field_select[i][0] = get_bits1(&s->gb);
1047
                        for(k=0;k<2;k++) {
1048
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1049
                                                     s->last_mv[i][0][k]);
1050
                            s->last_mv[i][0][k] = val;
1051
                            s->last_mv[i][1][k] = val;
1052
                            s->mv[i][0][k] = val;
1053
                        }
1054
                    }
1055
                    break;
1056
                case MT_DMV:
1057
                    {
1058
                        int dmx, dmy, mx, my, m;
1059

  
1060
                        mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], 
1061
                                                s->last_mv[i][0][0]);
1062
                        s->last_mv[i][0][0] = mx;
1063
                        s->last_mv[i][1][0] = mx;
1064
                        dmx = get_dmv(s);
1065
                        my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], 
1066
                                                s->last_mv[i][0][1] >> 1);
1067
                        dmy = get_dmv(s);
1068
                        s->mv_type = MV_TYPE_DMV;
1069
                        /* XXX: totally broken */
1070
                        if (s->picture_structure == PICT_FRAME) {
1071
                            s->last_mv[i][0][1] = my << 1;
1072
                            s->last_mv[i][1][1] = my << 1;
1073

  
1074
                            m = s->top_field_first ? 1 : 3;
1075
                            /* top -> top pred */
1076
                            s->mv[i][0][0] = mx; 
1077
                            s->mv[i][0][1] = my << 1;
1078
                            s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1079
                            s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1080
                            m = 4 - m;
1081
                            s->mv[i][2][0] = mx;
1082
                            s->mv[i][2][1] = my << 1;
1083
                            s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1084
                            s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1085
                        } else {
1086
                            s->last_mv[i][0][1] = my;
1087
                            s->last_mv[i][1][1] = my;
1088
                            s->mv[i][0][0] = mx;
1089
                            s->mv[i][0][1] = my;
1090
                            s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
1091
                            s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 
1092
                                /* + 2 * cur_field */;
1087
                        break;
1088
                    case MT_DMV:
1089
                        {
1090
                            int dmx, dmy, mx, my, m;
1091

  
1092
                            mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], 
1093
                                                    s->last_mv[i][0][0]);
1094
                            s->last_mv[i][0][0] = mx;
1095
                            s->last_mv[i][1][0] = mx;
1096
                            dmx = get_dmv(s);
1097
                            my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], 
1098
                                                    s->last_mv[i][0][1] >> 1);
1099
                            dmy = get_dmv(s);
1100
                            s->mv_type = MV_TYPE_DMV;
1101
                            /* XXX: totally broken */
1102
                            if (s->picture_structure == PICT_FRAME) {
1103
                                mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED; 
1104

  
1105
                                s->last_mv[i][0][1] = my << 1;
1106
                                s->last_mv[i][1][1] = my << 1;
1107

  
1108
                                m = s->top_field_first ? 1 : 3;
1109
                                /* top -> top pred */
1110
                                s->mv[i][0][0] = mx; 
1111
                                s->mv[i][0][1] = my << 1;
1112
                                s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1113
                                s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1114
                                m = 4 - m;
1115
                                s->mv[i][2][0] = mx;
1116
                                s->mv[i][2][1] = my << 1;
1117
                                s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1118
                                s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1119
                            } else {
1120
                                mb_type |= MB_TYPE_16x16;
1121

  
1122
                                s->last_mv[i][0][1] = my;
1123
                                s->last_mv[i][1][1] = my;
1124
                                s->mv[i][0][0] = mx;
1125
                                s->mv[i][0][1] = my;
1126
                                s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
1127
                                s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 
1128
                                    /* + 2 * cur_field */;
1129
                            }
1093 1130
                        }
1131
                        break;
1094 1132
                    }
1095
                    break;
1096 1133
                }
1097 1134
            }
1098 1135
        }
1099
    }
1100

  
1101
    if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
1102
        skip_bits1(&s->gb); /* marker */
1103
    }
1104
    
1105
    if (mb_type & MB_PAT) {
1106
        cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1107
        if (cbp < 0){
1108
            fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1109
            return -1;
1110
        }
1111
        cbp++;
1112
    }
1113
    dprintf("cbp=%x\n", cbp);
1136
        
1137
        s->mb_intra = 0;
1114 1138

  
1115
    if (s->mpeg2) {
1116
        if (s->mb_intra) {
1117
            for(i=0;i<6;i++) {
1118
                if (mpeg2_decode_block_intra(s, block[i], i) < 0)
1119
                    return -1;
1139
        if (IS_PAT(mb_type)) {
1140
            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1141
            if (cbp < 0){
1142
                fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1143
                return -1;
1120 1144
            }
1121
        } else {
1122
            for(i=0;i<6;i++) {
1123
                if (cbp & 32) {
1124
                    if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
1125
                        return -1;
1126
                } else {
1127
                    s->block_last_index[i] = -1;
1145
            cbp++;
1146

  
1147
            if (s->mpeg2) {
1148
                for(i=0;i<6;i++) {
1149
                    if (cbp & 32) {
1150
                        if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
1151
                            return -1;
1152
                    } else {
1153
                        s->block_last_index[i] = -1;
1154
                    }
1155
                    cbp+=cbp;
1128 1156
                }
1129
                cbp+=cbp;
1130
            }
1131
        }
1132
    } else {
1133
        if (s->mb_intra) {
1134
            for(i=0;i<6;i++) {
1135
                if (mpeg1_decode_block_intra(s, block[i], i) < 0)
1136
                    return -1;
1137
            }
1138
        }else{
1139
            for(i=0;i<6;i++) {
1140
                if (cbp & 32) {
1141
                    if (mpeg1_decode_block_inter(s, block[i], i) < 0)
1142
                        return -1;
1143
                } else {
1144
                    s->block_last_index[i] = -1;
1157
            } else {
1158
                for(i=0;i<6;i++) {
1159
                    if (cbp & 32) {
1160
                        if (mpeg1_decode_block_inter(s, block[i], i) < 0)
1161
                            return -1;
1162
                    } else {
1163
                        s->block_last_index[i] = -1;
1164
                    }
1165
                    cbp+=cbp;
1145 1166
                }
1146
                cbp+=cbp;
1147 1167
            }
1168
        }else{
1169
            for(i=0;i<6;i++)
1170
                s->block_last_index[i] = -1;
1148 1171
        }
1149 1172
    }
1173

  
1174
    s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
1175

  
1150 1176
    return 0;
1151 1177
}
1152 1178

  
......
1156 1182
    int code, sign, val, m, l, shift;
1157 1183

  
1158 1184
    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
1159
    if (code < 0) {
1160
        return 0xffff;
1161
    }
1162 1185
    if (code == 0) {
1163 1186
        return pred;
1164 1187
    }
1188
    if (code < 0) {
1189
        return 0xffff;
1190
    }
1191

  
1165 1192
    sign = get_bits1(&s->gb);
1166 1193
    shift = fcode - 1;
1167 1194
    val = (code - 1) << shift;
......
1173 1200
    val += pred;
1174 1201
    
1175 1202
    /* modulo decoding */
1176
    l = (1 << shift) * 16;
1203
    l = 1 << (shift+4);
1177 1204
    m = 2 * l;
1178 1205
    if (val < -l) {
1179 1206
        val += m;
......
1713 1740
        s->first_field=0;
1714 1741
    else{
1715 1742
        s->first_field ^= 1;
1716
        memset(s->mbskip_table, 0, s->mb_width*s->mb_height);
1743
        memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
1717 1744
    }
1718 1745
    
1719 1746
    if(s->alternate_scan){
......
1766 1793
    }
1767 1794
}
1768 1795

  
1796
#define DECODE_SLICE_MB_ADDR_ERROR -3 //we faild decoding the mb_x/y info
1769 1797
#define DECODE_SLICE_FATAL_ERROR -2
1770 1798
#define DECODE_SLICE_ERROR -1
1771 1799
#define DECODE_SLICE_OK 0
......
1791 1819
    start_code = (start_code - 1) & 0xff;
1792 1820
    if (start_code >= s->mb_height){
1793 1821
        fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height);
1794
        return DECODE_SLICE_ERROR;
1822
        return DECODE_SLICE_MB_ADDR_ERROR;
1795 1823
    }
1796 1824
    
1797 1825
    ff_mpeg1_clean_buffers(s);
1826
    s->interlaced_dct = 0;
1798 1827
        
1799 1828
    /* start frame decoding */
1800 1829
    if (s->first_slice) {
1801 1830
      if(s->first_field || s->picture_structure==PICT_FRAME){
1802 1831
        if(MPV_frame_start(s, avctx) < 0)
1803 1832
            return DECODE_SLICE_FATAL_ERROR;
1833

  
1834
        ff_er_frame_start(s);
1835

  
1804 1836
        /* first check if we must repeat the frame */
1805 1837
        s->current_picture.repeat_pict = 0;
1806 1838

  
......
1839 1871
    init_get_bits(&s->gb, buf, buf_size*8);
1840 1872

  
1841 1873
    s->qscale = get_qscale(s);
1874
    if(s->qscale == 0){
1875
        fprintf(stderr, "qscale == 0\n");
1876
        return DECODE_SLICE_MB_ADDR_ERROR;
1877
    }
1878
    
1842 1879
    /* extra slice info */
1843 1880
    while (get_bits1(&s->gb) != 0) {
1844 1881
        skip_bits(&s->gb, 8);
1845 1882
    }
1846

  
1883
    
1847 1884
    s->mb_x=0;
1885

  
1848 1886
    for(;;) {
1849 1887
        int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1850
        if (code < 0)
1851
            return -1; /* error = end of slice, but empty slice is bad or?*/
1888
        if (code < 0){
1889
            fprintf(stderr, "first mb_incr damaged\n");
1890
            return DECODE_SLICE_MB_ADDR_ERROR;
1891
        }
1852 1892
        if (code >= 33) {
1853 1893
            if (code == 33) {
1854 1894
                s->mb_x += 33;
......
1860 1900
        }
1861 1901
    }
1862 1902
    
1863
    s->mb_y = start_code;
1903
    s->resync_mb_x= s->mb_x;
1904
    s->resync_mb_y= s->mb_y = start_code;
1864 1905
    s->mb_skip_run= 0;
1865 1906

  
1866 1907
    for(;;) {
1867 1908
	s->dsp.clear_blocks(s->block[0]);
1868
        
1909

  
1869 1910
        ret = mpeg_decode_mb(s, s->block);
1911

  
1870 1912
        dprintf("ret=%d\n", ret);
1871 1913
        if (ret < 0)
1872 1914
            return -1;
1915
            
1916
        if(s->motion_val && s->pict_type != B_TYPE){ //note motion_val is normally NULL unless we want to extract the MVs
1917
            const int wrap = s->block_wrap[0];
1918
            const int xy = s->mb_x*2 + 1 + (s->mb_y*2 +1)*wrap;
1919
            int motion_x, motion_y;
1920

  
1921
            if (s->mb_intra || s->mv_type == MV_TYPE_16X16) {
1922
                motion_x = s->mv[0][0][0];
1923
                motion_y = s->mv[0][0][1];
1924
            } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
1925
                int i;
1926
                motion_x = s->mv[0][0][0] + s->mv[0][1][0];
1927
                motion_y = s->mv[0][0][1] + s->mv[0][1][1];
1928
                motion_x = (motion_x>>1) | (motion_x&1);
1929
            }
1930
            s->motion_val[xy][0] = motion_x;
1931
            s->motion_val[xy][1] = motion_y;
1932
            s->motion_val[xy + 1][0] = motion_x;
1933
            s->motion_val[xy + 1][1] = motion_y;
1934
            s->motion_val[xy + wrap][0] = motion_x;
1935
            s->motion_val[xy + wrap][1] = motion_y;
1936
            s->motion_val[xy + 1 + wrap][0] = motion_x;
1937
            s->motion_val[xy + 1 + wrap][1] = motion_y;
1938
        }
1873 1939
        
1874 1940
        MPV_decode_mb(s, s->block);
1875 1941

  
......
1884 1950

  
1885 1951
            s->mb_x = 0;
1886 1952
            s->mb_y++;
1887
            PRINT_QP("%s", "\n");
1888 1953
        }
1889
        PRINT_QP("%2d", s->qscale);
1890 1954

  
1891 1955
        /* skip mb handling */
1892 1956
        if (s->mb_skip_run == -1) {
......
1894 1958
            s->mb_skip_run = 0;
1895 1959
            for(;;) {
1896 1960
                int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1897
                if (code < 0)
1961
                if (code < 0){
1962
                    align_get_bits(&s->gb);
1963
                    if(s->mb_skip_run != 0 || show_bits(&s->gb, 24) != 1){
1964
                        fprintf(stderr, "slice end missmatch\n");
1965
                        return -1;
1966
                    }
1898 1967
                    goto eos; /* error = end of slice */
1968
                }
1899 1969
                if (code >= 33) {
1900 1970
                    if (code == 33) {
1901 1971
                        s->mb_skip_run += 33;
......
1913 1983
        }
1914 1984
    }
1915 1985
eos: //end of slice
1916
    
1986
//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1987
    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
1988

  
1917 1989
    emms_c();
1990
    
1918 1991
//intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number);
1919 1992
    /* end of slice reached */
1920 1993
    if (s->mb_y<<field_pic == s->mb_height && !s->first_field) {
......
1922 1995

  
1923 1996
        if(s->mpeg2)
1924 1997
            s->qscale >>=1;
1998
        ff_er_frame_end(s);
1925 1999

  
1926 2000
        MPV_frame_end(s);
1927 2001

  
1928 2002
        if (s->pict_type == B_TYPE || s->low_delay) {
1929 2003
            *pict= *(AVFrame*)&s->current_picture;
2004
            ff_print_debug_info(s, s->current_picture_ptr);
1930 2005
        } else {
1931 2006
            s->picture_number++;
1932 2007
            /* latency of 1 frame for I and P frames */
......
1935 2010
                return DECODE_SLICE_OK;
1936 2011
            } else {
1937 2012
                *pict= *(AVFrame*)&s->last_picture;
2013
                 ff_print_debug_info(s, s->last_picture_ptr);
1938 2014
            }
1939 2015
        }
1940 2016
        return DECODE_SLICE_EOP;
......
2194 2270
                        if (ret == DECODE_SLICE_EOP) {
2195 2271
                            *data_size = sizeof(AVPicture);
2196 2272
                            goto the_end;
2197
                        }else if(ret<0){
2273
                        }else if(ret < 0){
2274
                            if(ret == DECODE_SLICE_ERROR)
2275
                                ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
2276
                                
2198 2277
                            fprintf(stderr,"Error while decoding slice\n");
2199 2278
			    if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
2200 2279
                        }

Also available in: Unified diff