⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 video_mpeg2.c

📁 基于linux的DVD播放器程序
💻 C
📖 第 1 页 / 共 3 页
字号:
      else	tab = &DCTtab1[(code >> 6) - 8];  // 14    else if(code>=256)      tab = &DCTtab2[(code >> 4) - 16];    else if(code>=128)      tab = &DCTtab3[(code >> 3) - 16];    else if(code>=64)      tab = &DCTtab4[(code >> 2) - 16];    else if(code>=32)      tab = &DCTtab5[(code >> 1) - 16];    else if(code>=16)      tab = &DCTtab6[code - 16];    else {      fprintf(stderr,	      "(vlc) invalid huffman code 0x%x in vlc_get_block_coeff()\n",	      code);      //exit_program(1);      tab = &DCTtabnext[4]; // end_of_block     }        if(tab->run == 64 /*VLC_END_OF_BLOCK*/) { // end_of_block       /*dropbits(tab->len); // pic.coding_ext.intra_vlc_format ? 4 : 2 bits */      left -= tab->len;      break;    }     else {      unsigned int i, f, run, val, sgn;            if(tab->run == 65) { /* escape */	//    dropbits(tab->len); // always 6 bits.	//    run = GETBITS(6, "(get_dct escape - run )");	//    val = GETBITS(12, "(get_dct escape - level )");	/* val = GETBITS(6 + 6 + 12, "(get_dct escape - run & level )"); */	val = (word >> (24-(6+12+6))); left -= (6+12+6);	run = (val >> 12) & 0x3f;	val = val & 0xfff;#if 0	if ((val & 2047) == 0) {	  fprintf(stderr,"invalid escape in vlc_get_block_coeff()\n");	  exit_program(1);	}#endif	sgn = (val >= 2048);	if(val >= 2048)                // !!!! ?sgn? 	  val = 4096 - val;#ifdef DEBUG	DPRINTF(4, "coeff run: %d, level: %d (esc)\n", run, sgn?-val:val);#endif            } else {	//    dropbits(tab->len);	run = tab->run;	val = tab->level; 	/* sgn = 0x1 & GETBITS(tab->len + 1, "(get_dct sign )"); //sign bit */	sgn = 0x1 & (word >> (24-(tab->len + 1))); left -= (tab->len + 1);#ifdef DEBUG	DPRINTF(4,"coeff run: %d, level: %d\n",tab->run,(sgn?-1:1)*tab->level);#endif      }            n += run;            /* inverse quantisation */      i = inverse_scan[pic.coding_ext.alternate_scan][n];      f = (val 	   * mb.quantiser_scale	   * seq.header.intra_inverse_quantiser_matrix[i])/16;      #if 0	            if(!sgn) {	if(f > 2047)	  f = 2047;      }      else {	if(f > 2048)	  f = 2048;      }#endif      inverse_quantisation_sum += f; // The last bit is the same in f and -f.      // mb.QFS[i] = sgn ? -f : f;      mb.QFS[i] = (f ^ -sgn) + sgn;            n++;    }        /* Works like:       dropbits(..);       word = nextbits(24);    */    if(left <= 24) {      if(offset < buf_size) {	uint32_t new_word = FROM_BE_32(buf[offset++]);	cur_word = (cur_word << 32) | new_word;	left += 32;      }      else {	bits_left = left;	offs = offset;	read_buf();	left = bits_left;	offset = offs;      }    }    word = (cur_word << (64-left)) >> 40;  }  //cpc_count_usr_events(0);    mb.QFS[63] ^= inverse_quantisation_sum & 1;    // Clean-up  bits_left = left;  offs = offset;  dropbits(0);    DPRINTF(4, "nr of coeffs: %d\n", n);}/* 6.2.6 Block */staticvoid block_non_intra(unsigned int b){  unsigned int n = 0;  int inverse_quantisation_sum = 1;    /* Make a local 'getbits' implementation. */  unsigned int left = bits_left;  unsigned int offset = offs;  uint64_t lcur_word = cur_word;  uint32_t word = (lcur_word << (64-left)) >> 40; //nextbits(24);    DPRINTF(3, "pattern_code(%d) set\n", b);    //cpc_count_usr_events(1);       { /* Reset all coefficients to 0. */    int m;    for(m=0; m<16; m++)      *(((uint64_t *)mb.QFS) + m) = 0;  }    while(1) {    //      get_dct_non_intra(&runlevel, "dct_dc_subsequent");    const DCTtab *tab;    /*    const unsigned int code = nextbits(16);*/    const unsigned int code = (word >> (24-16));        if(code >= 16384)      if(n == 0)	tab = &DCTtabfirst[(code >> 12) - 4];      else {	tab = &DCTtabnext[(code >> 12) - 4];	/* EOB only here */      }    else if(code >= 1024) {      tab = &DCTtab0[(code >> 8) - 4];      /* Escape only here */    }    else if(code >= 512)      tab = &DCTtab1[(code >> 6) - 8];    else if(code >= 256)      tab = &DCTtab2[(code >> 4) - 16];    else if(code >= 128)      tab = &DCTtab3[(code >> 3) - 16];    else if(code >= 64)      tab = &DCTtab4[(code >> 2) - 16];    else if(code >= 32)      tab = &DCTtab5[(code >> 1) - 16];    else if(code >= 16)      tab = &DCTtab6[code - 16];    else {      fprintf(stderr, "(vlc) invalid huffman code 0x%x " 	      "in vlc_get_block_coeff()\n", code);      //exit_program(1);      break; // end_of_block     }           if(tab->run == 64 /*VLC_END_OF_BLOCK*/) { // end_of_block       //      dropbits( 2 ); // tab->len, end of block always = 2bits      //left -= 2; see the code after the loop      break;    }     else {      unsigned int i, f, run, val, sgn;      if(tab->run == 65) { /* escape */	//	  dropbits(tab->len); escape always = 6 bits	//	  run = GETBITS(6, "(get_dct escape - run )");	//	  val = GETBITS(12, "(get_dct escape - level )");	/*	val = GETBITS(6 + 12 + 6, "(escape run + val)" );*/	val = (word >> (24-(6+12+6))); left -= (6+12+6);	run = (val >> 12) & 0x3f;	val &= 0xfff;#if 0	if((val & 2047) == 0) {	  fprintf(stderr,"invalid escape in vlc_get_block_coeff()\n");	  exit_program(1);	}#endif		sgn = (val >= 2048);	if(val >= 2048)                // !!!! ?sgn? 	  val =  4096 - val;// - 4096;#ifdef DEBUG	DPRINTF(4, "coeff run: %d, level: %d (esc)\n", run, sgn?-val:val);#endif      }      else {	//	  dropbits(tab->len);	run = tab->run;	val = tab->level; 	/*sgn = 0x1 & GETBITS(tab->len + 1, "(get_dct sign )"); //sign bit*/	sgn = 0x1 & (word >> (24-(tab->len + 1))); left -= (tab->len + 1);#ifdef DEBUG	DPRINTF(4,"coeff run: %d, level: %d\n",tab->run,(sgn?-1:1)*tab->level);#endif      }            n += run;            /* inverse quantisation */      i = inverse_scan[pic.coding_ext.alternate_scan][n];      // flytta ut &inverse_scan[pic.coding_ext.alternate_scan] ??      // flytta ut mb.quantiser_scale ??      f = ( ((val*2)+1)	    * mb.quantiser_scale	    * seq.header.non_intra_inverse_quantiser_matrix[i])/32;#if 0      if(!sgn) {	if(f > 2047)	  f = 2047;      }      else {	if(f > 2048)	  f = 2048;      }#endif            inverse_quantisation_sum += f; // The last bit is the same in f and -f.      // mb.QFS[i] = sgn ? -f : f;      mb.QFS[i] = (f ^ -sgn) + sgn;            n++;          }        /* Works like:       dropbits(..);       word = nextbits(24);    */    if(left <= 24) {      if(offset < buf_size) {	uint32_t new_word = FROM_BE_32(buf[offset++]);	lcur_word = (lcur_word << 32) | new_word;	left += 32;      }      else {	bits_left = left;	offs = offset;	cur_word = lcur_word;	read_buf();	left = bits_left;	offset = offs;	lcur_word = cur_word;      }    }    word = (lcur_word << (64-left)) >> 40;  }    //cpc_count_usr_events(0);  mb.QFS[63] ^= inverse_quantisation_sum & 1;    // Clean-up  bits_left = left;  offs = offset;  cur_word = lcur_word;  dropbits(2); // eob-token ++    DPRINTF(4, "nr of coeffs: %d\n", n);}#endif/* 6.2.5.3 Coded block pattern */staticvoid coded_block_pattern(void){  //  uint16_t coded_block_pattern_420;  uint8_t cbp = 0;    DPRINTF(3, "coded_block_pattern\n");  cbp = get_vlc(table_b9, "cbp (b9)");    if((cbp == 0) && (seq.ext.chroma_format == 0x1)) {    fprintf(stderr, "** cbp = 0, shall not be used with 4:2:0 chrominance\n");    //exit_program(1);  }  mb.cbp = cbp;    DPRINTF(4, "cpb = %u\n", mb.cbp);#if 0  if(seq.ext.chroma_format == 0x02) {    mb.coded_block_pattern_1 = GETBITS(2, "coded_block_pattern_1");  }   if(seq.ext.chroma_format == 0x03) {    mb.coded_block_pattern_2 = GETBITS(6, "coded_block_pattern_2");  }#endif} /* 6.2.5.2.1 Motion vector */staticvoid motion_vector(unsigned int r, unsigned int s){  unsigned int t;    DPRINTF(3, "motion_vector(%d, %d)\n", r, s);  for(t = 0; t < 2; t++) {       int delta;    int prediction;    int vector;        unsigned int r_size = pic.coding_ext.f_code[s][t] - 1;        { // Read and compute the motion vector delta      int motion_code = get_vlc(table_b10, "motion_code[r][s][0] (b10)");            if((pic.coding_ext.f_code[s][t] != 1) && (motion_code != 0)) {	int motion_residual = GETBITS(r_size, "motion_residual[r][s][0]");		delta = ((abs(motion_code) - 1) << r_size) + motion_residual + 1;	if(motion_code < 0)	  delta = -delta;      }      else {	delta = motion_code;      }          }        if(mb.dmv == 1)      mb.dmvector[t] = get_vlc(table_b11, "dmvector[0] (b11)");      // Get the predictor    prediction = pic.PMV[r][s][t];    if((t==1) && (mb.mv_format == MV_FORMAT_FIELD) &&       (pic.coding_ext.picture_structure == PIC_STRUCT_FRAME_PICTURE))      prediction = prediction >> 1; /* DIV */        { // Compute the resulting motion vector      int f = 1 << r_size;      int high = (16 * f) - 1;      int low = ((-16) * f);      int range = (32 * f);            vector = prediction + delta;      if(vector < low)	vector = vector + range;      if(vector > high)	vector = vector - range;    }        // Update predictors    if((t==1) && (mb.mv_format == MV_FORMAT_FIELD) &&       (pic.coding_ext.picture_structure == PIC_STRUCT_FRAME_PICTURE))      pic.PMV[r][s][t] = vector * 2;    else      pic.PMV[r][s][t] = vector;    #if MPEG1    // Scale the vector so that it is always measured in half pels    if(pic.header.full_pel_vector[s])      mb.vector[r][s][t] = vector << 1;    else#endif      mb.vector[r][s][t] = vector;  }}/* 6.2.5.2 Motion vectors */staticvoid motion_vectors(unsigned int s){  DPRINTF(3, "motion_vectors(%u)\n", s);  if(pic.coding_ext.picture_structure == PIC_STRUCT_FRAME_PICTURE) {        if((mb.modes.macroblock_type & MACROBLOCK_INTRA) &&       pic.coding_ext.concealment_motion_vectors) {      mb.prediction_type = PRED_TYPE_FRAME_BASED;      mb.motion_vector_count = 1;      mb.mv_format = MV_FORMAT_FRAME;      mb.dmv = 0;    } else {      /* Table 6-17 Meaning of frame_motion_type */      switch(mb.modes.frame_motion_type) {      case 0x0:	// Should never happen, value is checked when read in.      case 0x1:	mb.prediction_type = PRED_TYPE_FIELD_BASED;	mb.motion_vector_count = 2;	mb.mv_format = MV_FORMAT_FIELD;	mb.dmv = 0;	break;      case 0x2:	/* spatial_temporal_weight_class always 0 for now */	mb.prediction_type = PRED_TYPE_FRAME_BASED;	mb.motion_vector_count = 1;	mb.mv_format = MV_FORMAT_FRAME;	mb.dmv = 0;	break;      case 0x3:	mb.prediction_type = PRED_TYPE_DUAL_PRIME;	mb.motion_vector_count = 1;	mb.mv_format = MV_FORMAT_FIELD;	mb.dmv = 1;	break;      default:	fprintf(stderr, "*** impossible prediction type\n");	exit_program(1);	break;      }    }      } else {    /* Table 6-18 Meaning of field_motion_type */    switch(mb.modes.field_motion_type) {    case 0x0:      // Should never happen, value is checked when read in.    case 0x1:      mb.prediction_type = PRED_TYPE_FIELD_BASED;      mb.motion_vector_count = 1;      mb.mv_format = MV_FORMAT_FIELD;      mb.dmv = 0;      break;    case 0x2:      mb.prediction_type = PRED_TYPE_16x8_MC;      mb.motion_vector_count = 2;      mb.mv_format = MV_FORMAT_FIELD;      mb.dmv = 0;      break;    case 0x3:      mb.prediction_type = PRED_TYPE_DUAL_PRIME;      mb.motion_vector_count = 1;      mb.mv_format = MV_FORMAT_FIELD;      mb.dmv = 1;      break;    default:      fprintf(stderr, "*** impossible prediction type\n");      exit_program(1);      break;    }  }  if(mb.motion_vector_count == 1) {    if((mb.mv_format == MV_FORMAT_FIELD) && (mb.dmv != 1)) {      mb.motion_vertical_field_select[0][s] 	= GETBITS(1, "motion_vertical_field_select[0][s]");    }    motion_vector(0, s);  } else {    mb.motion_vertical_field_select[0][s]       = GETBITS(1, "motion_vertical_field_select[0][s]");    motion_vector(0, s);    mb.motion_vertical_field_select[1][s]       = GETBITS(1, "motion_vertical_field_select[1][s]");    motion_vector(1, s);  }}/* 6.2.5.1 Macroblock modes */staticint macroblock_modes(void){  DPRINTF(3, "macroblock_modes\n");  if(pic.header.picture_coding_type == PIC_CODING_TYPE_I) {    mb.modes.macroblock_type = get_vlc(table_b2, "macroblock_type (b2)");  } else if(pic.header.picture_coding_type == PIC_CODING_TYPE_P) {    mb.modes.macroblock_type = get_vlc(table_b3, "macroblock_type (b3)");  } else if(pic.header.picture_coding_type == PIC_CODING_TYPE_B) {    mb.modes.macroblock_type = get_vlc(table_b4, "macroblock_type (b4)");  } else {    fprintf(stderr, "*** Unsupported picture type %02x\n", 	    pic.header.picture_coding_type);    exit_program(1);    // should not be tested / handled here  }  if(mb.modes.macroblock_type == VLC_FAIL) {    fprintf(stderr, "*** invalid macroblock type\n");    return -1;  }    DPRINTF(5, "spatial_temporal_weight_code_flag: %01x\n", 	  !!(mb.modes.macroblock_type & SPATIAL_TEMPORAL_WEIGHT_CODE_FLAG));  if((mb.modes.macroblock_type & SPATIAL_TEMPORAL_WEIGHT_CODE_FLAG) &&     (1 /*spatial_temporal_weight_code_table_index != 0*/)) {    mb.modes.spatial_temporal_weight_code 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -