Mercurial > libavcodec.hg
comparison h263.c @ 664:00a882f626bd libavcodec
interlaced mpeg4 b frame decoding
author | michaelni |
---|---|
date | Fri, 13 Sep 2002 09:45:32 +0000 |
parents | 5a158f0d1981 |
children | f5b8b5dd50d7 |
comparison
equal
deleted
inserted
replaced
663:76fef3b11680 | 664:00a882f626bd |
---|---|
1329 s->time= picture_number*(INT64)FRAME_RATE_BASE*s->time_increment_resolution/s->frame_rate; | 1329 s->time= picture_number*(INT64)FRAME_RATE_BASE*s->time_increment_resolution/s->frame_rate; |
1330 time_div= s->time/s->time_increment_resolution; | 1330 time_div= s->time/s->time_increment_resolution; |
1331 time_mod= s->time%s->time_increment_resolution; | 1331 time_mod= s->time%s->time_increment_resolution; |
1332 | 1332 |
1333 if(s->pict_type==B_TYPE){ | 1333 if(s->pict_type==B_TYPE){ |
1334 s->bp_time= s->last_non_b_time - s->time; | 1334 s->pb_time= s->pp_time - (s->last_non_b_time - s->time); |
1335 }else{ | 1335 }else{ |
1336 s->last_time_base= s->time_base; | 1336 s->last_time_base= s->time_base; |
1337 s->time_base= time_div; | 1337 s->time_base= time_div; |
1338 s->pp_time= s->time - s->last_non_b_time; | 1338 s->pp_time= s->time - s->last_non_b_time; |
1339 s->last_non_b_time= s->time; | 1339 s->last_non_b_time= s->time; |
2826 else if (s->qscale > 31) | 2826 else if (s->qscale > 31) |
2827 s->qscale = 31; | 2827 s->qscale = 31; |
2828 h263_dc_scale(s); | 2828 h263_dc_scale(s); |
2829 } | 2829 } |
2830 } | 2830 } |
2831 // decode_interlaced_info(s, cbp, mb_type); | 2831 s->mv_type= MV_TYPE_16X16; //might be changed to 8X8 or FIELD later |
2832 | |
2833 if(!s->progressive_sequence){ | |
2834 if(cbp) | |
2835 s->interlaced_dct= get_bits1(&s->gb); | |
2836 | |
2837 if(mb_type!=MB_TYPE_B_DIRECT){ | |
2838 if(get_bits1(&s->gb)){ | |
2839 s->mv_type= MV_TYPE_FIELD; | |
2840 | |
2841 if(mb_type!=MB_TYPE_B_BACKW){ | |
2842 s->field_select[0][0]= get_bits1(&s->gb); //FIXME move down | |
2843 s->field_select[0][1]= get_bits1(&s->gb); | |
2844 } | |
2845 if(mb_type!=MB_TYPE_B_FORW){ | |
2846 s->field_select[1][0]= get_bits1(&s->gb); | |
2847 s->field_select[1][1]= get_bits1(&s->gb); | |
2848 } | |
2849 } | |
2850 } | |
2851 } | |
2832 }else{ | 2852 }else{ |
2833 mb_type=4; //like 0 but no vectors coded | 2853 s->mv_type= MV_TYPE_16X16; //might be changed to 8X8 later |
2854 mb_type=4; //like MB_TYPE_B_DIRECT but no vectors coded | |
2834 cbp=0; | 2855 cbp=0; |
2835 } | 2856 } |
2836 s->mv_type = MV_TYPE_16X16; // we'll switch to 8x8 only if the last P frame had 8x8 for this MB and mb_type=0 here | 2857 |
2837 mx=my=0; //for case 4, we could put this to the mb_type=4 but than gcc compains about uninitalized mx/my | 2858 mx=my=0; |
2838 switch(mb_type) | 2859 if(mb_type==MB_TYPE_B_DIRECT){ |
2839 { | |
2840 case 0: /* direct */ | |
2841 mx = h263_decode_motion(s, 0, 1); | 2860 mx = h263_decode_motion(s, 0, 1); |
2842 my = h263_decode_motion(s, 0, 1); | 2861 my = h263_decode_motion(s, 0, 1); |
2843 case 4: /* direct with mx=my=0 */ | 2862 } |
2863 | |
2864 if(s->mv_type==MV_TYPE_16X16){ | |
2865 if(mb_type==MB_TYPE_B_FORW || mb_type==MB_TYPE_B_BIDIR){ | |
2866 s->mv_dir = MV_DIR_FORWARD; | |
2867 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code); | |
2868 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code); | |
2869 s->last_mv[0][1][0]= s->last_mv[0][0][0]= s->mv[0][0][0] = mx; | |
2870 s->last_mv[0][1][1]= s->last_mv[0][0][1]= s->mv[0][0][1] = my; | |
2871 }else | |
2872 s->mv_dir = 0; | |
2873 | |
2874 if(mb_type==MB_TYPE_B_BACKW || mb_type==MB_TYPE_B_BIDIR){ | |
2875 s->mv_dir |= MV_DIR_BACKWARD; | |
2876 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code); | |
2877 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code); | |
2878 s->last_mv[1][1][0]= s->last_mv[1][0][0]= s->mv[1][0][0] = mx; | |
2879 s->last_mv[1][1][1]= s->last_mv[1][0][1]= s->mv[1][0][1] = my; | |
2880 } | |
2881 if(mb_type!=4 && mb_type!=MB_TYPE_B_DIRECT) | |
2882 PRINT_MB_TYPE(mb_type==MB_TYPE_B_FORW ? "F" : (mb_type==MB_TYPE_B_BACKW ? "B" : "T")); | |
2883 }else{ | |
2884 /* MV_TYPE_FIELD */ | |
2885 if(mb_type==MB_TYPE_B_FORW || mb_type==MB_TYPE_B_BIDIR){ | |
2886 s->mv_dir = MV_DIR_FORWARD; | |
2887 for(i=0; i<2; i++){ | |
2888 mx = h263_decode_motion(s, s->last_mv[0][i][0] , s->f_code); | |
2889 my = h263_decode_motion(s, s->last_mv[0][i][1]/2, s->f_code); | |
2890 s->last_mv[0][i][0]= s->mv[0][i][0] = mx; | |
2891 s->last_mv[0][i][1]= (s->mv[0][i][1] = my)*2; | |
2892 } | |
2893 }else | |
2894 s->mv_dir = 0; | |
2895 | |
2896 if(mb_type==MB_TYPE_B_BACKW || mb_type==MB_TYPE_B_BIDIR){ | |
2897 s->mv_dir |= MV_DIR_BACKWARD; | |
2898 for(i=0; i<2; i++){ | |
2899 mx = h263_decode_motion(s, s->last_mv[1][i][0] , s->b_code); | |
2900 my = h263_decode_motion(s, s->last_mv[1][i][1]/2, s->b_code); | |
2901 s->last_mv[1][i][0]= s->mv[1][i][0] = mx; | |
2902 s->last_mv[1][i][1]= (s->mv[1][i][1] = my)*2; | |
2903 } | |
2904 } | |
2905 if(mb_type!=4 && mb_type!=MB_TYPE_B_DIRECT) | |
2906 PRINT_MB_TYPE(mb_type==MB_TYPE_B_FORW ? "f" : (mb_type==MB_TYPE_B_BACKW ? "b" : "t")); | |
2907 } | |
2908 | |
2909 if(mb_type==4 || mb_type==MB_TYPE_B_DIRECT){ | |
2910 int mb_index= s->mb_x + s->mb_y*s->mb_width; | |
2911 int i; | |
2912 | |
2844 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | 2913 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; |
2845 xy= s->block_index[0]; | 2914 xy= s->block_index[0]; |
2846 time_pp= s->pp_time; | 2915 time_pp= s->pp_time; |
2847 time_pb= time_pp - s->bp_time; | 2916 time_pb= s->pb_time; |
2848 //if(time_pp>3000 )printf("%d %d ", time_pp, time_pb); | 2917 |
2849 //FIXME avoid divides | 2918 //FIXME avoid divides |
2850 s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx; | 2919 switch(s->co_located_type_table[mb_index]){ |
2851 s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my; | 2920 case 0: |
2852 s->mv[1][0][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0] | 2921 s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx; |
2853 : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp; | 2922 s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my; |
2854 s->mv[1][0][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1] | 2923 s->mv[1][0][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0] |
2855 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp; | 2924 : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp; |
2856 if(s->non_b_mv4_table[xy]){ | 2925 s->mv[1][0][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1] |
2857 int i; | 2926 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp; |
2927 PRINT_MB_TYPE(mb_type==4 ? "D" : "S"); | |
2928 break; | |
2929 case CO_LOCATED_TYPE_4MV: | |
2858 s->mv_type = MV_TYPE_8X8; | 2930 s->mv_type = MV_TYPE_8X8; |
2859 for(i=1; i<4; i++){ | 2931 for(i=0; i<4; i++){ |
2860 xy= s->block_index[i]; | 2932 xy= s->block_index[i]; |
2861 s->mv[0][i][0] = s->motion_val[xy][0]*time_pb/time_pp + mx; | 2933 s->mv[0][i][0] = s->motion_val[xy][0]*time_pb/time_pp + mx; |
2862 s->mv[0][i][1] = s->motion_val[xy][1]*time_pb/time_pp + my; | 2934 s->mv[0][i][1] = s->motion_val[xy][1]*time_pb/time_pp + my; |
2863 s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->motion_val[xy][0] | 2935 s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->motion_val[xy][0] |
2864 : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp; | 2936 : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp; |
2865 s->mv[1][i][1] = my ? s->mv[0][i][1] - s->motion_val[xy][1] | 2937 s->mv[1][i][1] = my ? s->mv[0][i][1] - s->motion_val[xy][1] |
2866 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp; | 2938 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp; |
2867 } | 2939 } |
2868 PRINT_MB_TYPE("4"); | 2940 PRINT_MB_TYPE("4"); |
2869 }else{ | 2941 break; |
2870 PRINT_MB_TYPE(mb_type==4 ? "D" : "S"); | 2942 case CO_LOCATED_TYPE_FIELDMV: |
2871 } | 2943 s->mv_type = MV_TYPE_FIELD; |
2872 /* s->mv[0][0][0] = | 2944 for(i=0; i<2; i++){ |
2873 s->mv[0][0][1] = | 2945 if(s->top_field_first){ |
2874 s->mv[1][0][0] = | 2946 time_pp= s->pp_field_time - s->field_select_table[mb_index][i] + i; |
2875 s->mv[1][0][1] = 1000;*/ | 2947 time_pb= s->pb_field_time - s->field_select_table[mb_index][i] + i; |
2876 break; | 2948 }else{ |
2877 //FIXME additional MVs for interlaced stuff | 2949 time_pp= s->pp_field_time + s->field_select_table[mb_index][i] - i; |
2878 case 1: | 2950 time_pb= s->pb_field_time + s->field_select_table[mb_index][i] - i; |
2879 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | 2951 } |
2880 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code); | 2952 s->mv[0][i][0] = s->field_mv_table[mb_index][i][0]*time_pb/time_pp + mx; |
2881 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code); | 2953 s->mv[0][i][1] = s->field_mv_table[mb_index][i][1]*time_pb/time_pp + my; |
2882 s->last_mv[0][0][0]= s->mv[0][0][0] = mx; | 2954 s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->field_mv_table[mb_index][i][0] |
2883 s->last_mv[0][0][1]= s->mv[0][0][1] = my; | 2955 : s->field_mv_table[mb_index][i][0]*(time_pb - time_pp)/time_pp; |
2884 | 2956 s->mv[1][i][1] = my ? s->mv[0][i][1] - s->field_mv_table[mb_index][i][1] |
2885 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code); | 2957 : s->field_mv_table[mb_index][i][1]*(time_pb - time_pp)/time_pp; |
2886 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code); | 2958 } |
2887 s->last_mv[1][0][0]= s->mv[1][0][0] = mx; | 2959 PRINT_MB_TYPE("="); |
2888 s->last_mv[1][0][1]= s->mv[1][0][1] = my; | 2960 break; |
2889 PRINT_MB_TYPE("i"); | 2961 } |
2890 break; | 2962 } |
2891 case 2: | 2963 |
2892 s->mv_dir = MV_DIR_BACKWARD; | 2964 if(mb_type<0 || mb_type>4){ |
2893 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code); | |
2894 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code); | |
2895 s->last_mv[1][0][0]= s->mv[1][0][0] = mx; | |
2896 s->last_mv[1][0][1]= s->mv[1][0][1] = my; | |
2897 PRINT_MB_TYPE("B"); | |
2898 break; | |
2899 case 3: | |
2900 s->mv_dir = MV_DIR_FORWARD; | |
2901 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code); | |
2902 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code); | |
2903 s->last_mv[0][0][0]= s->mv[0][0][0] = mx; | |
2904 s->last_mv[0][0][1]= s->mv[0][0][1] = my; | |
2905 PRINT_MB_TYPE("F"); | |
2906 break; | |
2907 default: | |
2908 printf("illegal MB_type\n"); | 2965 printf("illegal MB_type\n"); |
2909 return -1; | 2966 return -1; |
2910 } | 2967 } |
2911 } else { /* I-Frame */ | 2968 } else { /* I-Frame */ |
2912 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1); | 2969 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1); |
3791 } | 3848 } |
3792 | 3849 |
3793 skip_bits1(&s->gb); /* marker */ | 3850 skip_bits1(&s->gb); /* marker */ |
3794 | 3851 |
3795 s->time_increment_resolution = get_bits(&s->gb, 16); | 3852 s->time_increment_resolution = get_bits(&s->gb, 16); |
3853 | |
3796 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1; | 3854 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1; |
3797 if (s->time_increment_bits < 1) | 3855 if (s->time_increment_bits < 1) |
3798 s->time_increment_bits = 1; | 3856 s->time_increment_bits = 1; |
3799 skip_bits1(&s->gb); /* marker */ | 3857 skip_bits1(&s->gb); /* marker */ |
3800 | 3858 |
4003 //printf(" type:%d incr:%d increment:%d\n", s->pict_type, time_incr, time_increment); | 4061 //printf(" type:%d incr:%d increment:%d\n", s->pict_type, time_incr, time_increment); |
4004 if(s->pict_type!=B_TYPE){ | 4062 if(s->pict_type!=B_TYPE){ |
4005 s->last_time_base= s->time_base; | 4063 s->last_time_base= s->time_base; |
4006 s->time_base+= time_incr; | 4064 s->time_base+= time_incr; |
4007 s->time= s->time_base*s->time_increment_resolution + time_increment; | 4065 s->time= s->time_base*s->time_increment_resolution + time_increment; |
4066 if(s->time < s->last_non_b_time && s->workaround_bugs==3){ | |
4067 fprintf(stderr, "header is not mpeg4 compatible, broken encoder, trying to workaround\n"); | |
4068 s->time_base++; | |
4069 s->time+= s->time_increment_resolution; | |
4070 } | |
4008 s->pp_time= s->time - s->last_non_b_time; | 4071 s->pp_time= s->time - s->last_non_b_time; |
4009 s->last_non_b_time= s->time; | 4072 s->last_non_b_time= s->time; |
4010 }else{ | 4073 }else{ |
4011 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment; | 4074 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment; |
4012 s->bp_time= s->last_non_b_time - s->time; | 4075 s->pb_time= s->pp_time - (s->last_non_b_time - s->time); |
4013 if(s->pp_time <=s->bp_time){ | 4076 if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){ |
4014 // printf("messed up order, seeking?, skiping current b frame\n"); | 4077 // printf("messed up order, seeking?, skiping current b frame\n"); |
4015 return FRAME_SKIPED; | 4078 return FRAME_SKIPED; |
4016 } | 4079 } |
4080 | |
4081 if(s->t_frame==0) s->t_frame= s->time - s->last_time_base; | |
4082 if(s->t_frame==0) s->t_frame=1; // 1/0 protection | |
4083 //printf("%Ld %Ld %d %d\n", s->last_non_b_time, s->time, s->pp_time, s->t_frame); fflush(stdout); | |
4084 s->pp_field_time= ( ROUNDED_DIV(s->last_non_b_time, s->t_frame) | |
4085 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2; | |
4086 s->pb_field_time= ( ROUNDED_DIV(s->time, s->t_frame) | |
4087 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2; | |
4017 } | 4088 } |
4018 | 4089 |
4019 s->avctx->pts= s->time*1000LL*1000LL / s->time_increment_resolution; | 4090 s->avctx->pts= s->time*1000LL*1000LL / s->time_increment_resolution; |
4020 | 4091 |
4021 if(check_marker(&s->gb, "before vop_coded")==0 && s->picture_number==0){ | 4092 if(check_marker(&s->gb, "before vop_coded")==0 && s->picture_number==0){ |
4026 printf("my guess is %d bits ;)\n",s->time_increment_bits); | 4097 printf("my guess is %d bits ;)\n",s->time_increment_bits); |
4027 } | 4098 } |
4028 /* vop coded */ | 4099 /* vop coded */ |
4029 if (get_bits1(&s->gb) != 1) | 4100 if (get_bits1(&s->gb) != 1) |
4030 goto redo; | 4101 goto redo; |
4031 //printf("time %d %d %d || %d %d %d\n", s->time_increment_bits, s->time_increment, s->time_base, | 4102 //printf("time %d %d %d || %Ld %Ld %Ld\n", s->time_increment_bits, s->time_increment_resolution, s->time_base, |
4032 //s->time, s->last_non_b_time[0], s->last_non_b_time[1]); | 4103 //s->time, s->last_non_b_time, s->last_non_b_time - s->pp_time); |
4033 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == P_TYPE | 4104 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == P_TYPE |
4034 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) { | 4105 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) { |
4035 /* rounding type for motion estimation */ | 4106 /* rounding type for motion estimation */ |
4036 s->no_rounding = get_bits1(&s->gb); | 4107 s->no_rounding = get_bits1(&s->gb); |
4037 } else { | 4108 } else { |
4099 | 4170 |
4100 if (s->pict_type == B_TYPE) { | 4171 if (s->pict_type == B_TYPE) { |
4101 s->b_code = get_bits(&s->gb, 3); | 4172 s->b_code = get_bits(&s->gb, 3); |
4102 }else | 4173 }else |
4103 s->b_code=1; | 4174 s->b_code=1; |
4104 | 4175 #if 0 |
4105 //printf("qpuant:%d fcode:%d bcode:%d type:%d size:%d pro:%d alt:%d\n", | 4176 printf("qp:%d fc:%d bc:%d type:%s size:%d pro:%d alt:%d qpel:%d part:%d resync:%d\n", |
4106 // s->qscale, s->f_code, s->b_code, s->pict_type, s->gb.size,s->progressive_sequence, s->alternate_scan); | 4177 s->qscale, s->f_code, s->b_code, |
4178 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")), | |
4179 s->gb.size,s->progressive_sequence, s->alternate_scan, s->quarter_sample, s->data_partitioning, s->resync_marker); | |
4180 #endif | |
4107 if(!s->scalability){ | 4181 if(!s->scalability){ |
4108 if (s->shape!=RECT_SHAPE && s->pict_type!=I_TYPE) { | 4182 if (s->shape!=RECT_SHAPE && s->pict_type!=I_TYPE) { |
4109 skip_bits1(&s->gb); // vop shape coding type | 4183 skip_bits1(&s->gb); // vop shape coding type |
4110 } | 4184 } |
4111 }else{ | 4185 }else{ |