char sub_mb_P_cabac ( int *dct_allowed, CABACContext *c, unsigned char *state, SLICE *slice, RESIDU *Current_residu, short mv_cache_l0[][2], short *ref_cache_l0, short mvl0_cache[][2], short *refl0_cache) { int mbPartIdx ; int subMbPartIdx ; char RefIdxL0[4] = {0, 0, 0, 0}; //Recovery of the sub-macroblock type for ( mbPartIdx = 0 ; mbPartIdx < 4 ; mbPartIdx++ ) { Current_residu -> SubMbType [mbPartIdx] = decode_cabac_p_mb_sub_type(c, state); } #ifdef ERROR_DETECTION //Error detection if(ErrorsCheckSubMbType( Current_residu -> SubMbType, MaxPSubMbType)){ //should be set to zero because the macroblock will be decoded. refl0_cache [12] = refl0_cache [14] = refl0_cache [28] = refl0_cache [30] = 0; return 1; } #endif //Recovery of the image reference of each sub-macroblock if (slice -> num_RefIdxL0_active_minus1 > 0) { if(GetCabacPSubRefIdx(c, state, RefIdxL0, ref_cache_l0, RefIdxL0, slice -> num_RefIdxL0_active_minus1)){ refl0_cache [12] = refl0_cache [14] = refl0_cache [28] = refl0_cache [30] = 0; return 1; } } else { ref_cache_l0[12] = ref_cache_l0[14] = ref_cache_l0[28] = ref_cache_l0[30] = 0; } //Recovery of the motion vector of each sub-macroblock for ( mbPartIdx = 0 ; mbPartIdx < 4 ; mbPartIdx++ ) { short MvdL0[2]; int sub_mb_type = Current_residu -> SubMbType [mbPartIdx]; for ( subMbPartIdx = 0 ; subMbPartIdx < sub_num_p_part[ sub_mb_type] ; subMbPartIdx++ ) { int index = SCAN8(( mbPartIdx << 2) + (sub_mb_type == 1 ? 2 * subMbPartIdx: subMbPartIdx)); //Decode cabac ReadCabacSubMotionVector(c, state, mv_cache_l0, MvdL0, mbPartIdx, subMbPartIdx, sub_mb_type, index, slice -> slice_type); //Compute Mv refill_ref(2 - (sub_mb_type > 1), 2 - (sub_mb_type & 1), RefIdxL0[mbPartIdx], &refl0_cache[index]); motion_vector(mvl0_cache, MvdL0, refl0_cache, &mvl0_cache[index][0], RefIdxL0[mbPartIdx], index, 2 / ( 1 + ( sub_mb_type > 1))); } refill_motion_vector(0, sub_mb_type, &mvl0_cache[SCAN8(( mbPartIdx << 2))]); } for ( mbPartIdx = 0; mbPartIdx < 4 && dct_allowed; mbPartIdx ++){ if ( !(sub_num_p_part[Current_residu -> SubMbType[mbPartIdx]] == 1) ){ *dct_allowed = 0; } } return 0; }
int main(int argc, char* argv[]) { FILE *fd; fd = fopen("predictor_error.txt", "w+"); FILE *fd1,*fd2; fd1 = fopen("predictor1_error.txt", "w+"); fd2 = fopen("predictor2_error.txt", "w+"); parameters params; codingvars vars; image_data* pre_im_data = NULL;//n-1 frame image image_data* im_data = NULL;//n frame image // parsing command line parameters and/or JPEG-LS header params = coding_parameters(argc, argv); // encoding process //loading previous image data pre_im_data = load_image(params.input_file0); printf("%s**\n", params.input_file0); // loading image data im_data = load_image(params.input_file); printf("%s**\n", params.input_file); // bitstream initialization init_bitstream(params.output_file, 'w'); params.MAXVAL = im_data->maxval; write_header(params, im_data); // setting parameters if(params.specified_T == false) { /* C.2.4.1.1 Default threshold values */ if(params.MAXVAL>=128) { vars.FACTOR = floor((float64)(min(params.MAXVAL,4095)+128)/256); params.T1 = CLAMP(vars.FACTOR*(vars.BASIC_T1-2)+2+3*params.NEAR,params.NEAR+1,params.MAXVAL); params.T2 = CLAMP(vars.FACTOR*(vars.BASIC_T2-3)+3+5*params.NEAR,params.T1,params.MAXVAL); params.T3 = CLAMP(vars.FACTOR*(vars.BASIC_T3-4)+4+7*params.NEAR,params.T2,params.MAXVAL); } else { vars.FACTOR = floor( 256.0/(params.MAXVAL + 1) ); params.T1 = CLAMP(max(2,floor((float64)vars.BASIC_T1/vars.FACTOR)+3*params.NEAR),params.NEAR+1,params.MAXVAL); params.T2 = CLAMP(max(2,floor((float64)vars.BASIC_T2/vars.FACTOR)+5*params.NEAR),params.T1,params.MAXVAL); params.T3 = CLAMP(max(2,floor((float64)vars.BASIC_T3/vars.FACTOR)+7*params.NEAR),params.T2,params.MAXVAL); } } if(params.verbose) { fprintf(stdout, "Encoding %s...\n\n\twidth\t\t%d\n\theight\t\t%d\n\tcomponents\t%d\n", params.input_file, im_data->width, im_data->height, im_data->n_comp); fprintf(stdout, "\tMAXVAL\t\t%d\n\tNEAR\t\t%d\n\tT1\t\t%d\n\tT2\t\t%d\n\tT3\t\t%d\n\tRESET\t\t%d\n", params.MAXVAL, params.NEAR, params.T1, params.T2, params.T3, params.RESET); } /* A.2 Initializations and conventions */ init_codingvars(&vars, params); //for(vars.comp=0; vars.comp<im_data->n_comp; vars.comp++) for(vars.comp=0; vars.comp<1; vars.comp++) for(vars.row=0; vars.row<im_data->height; vars.row++) for(vars.col=0; vars.col<im_data->width; vars.col++) { /* A.3 Context determination */ context_determination(&vars, params, im_data); //printf("%s\n", "HERE IT IS!"); motion_vector(&vars, params, pre_im_data, im_data); if((vars.row == 100) && (vars.col >= 200) && (vars.col < 220)) printf("motion_vector is (%d,%d) and Dif_min is %d.\n", vars.m0,vars.n0,vars.temp); prev_context(&vars, params, pre_im_data); // regular mode /* A.4 Prediction*/ predict_sample_value(&vars, params); predictor1(&vars, params); predictor2(&vars, params); encode_prediction_error(&vars, params, im_data); fprintf(fd, "%d\n", vars.Errval_temp3); encode_prediction_error_intra(&vars, params); fprintf(fd1, "%d\n", vars.Errval_temp1); fprintf(fd2, "%d\n", vars.Errval_temp2); /* A.6 Update variables */ update_codingvars(&vars, params); } end_bitstream(); if(params.decoding_flag == true) write_image("lena_decoded.ppm", im_data); else print_bpp(im_data); return 0; }