📄 macroblock.c
字号:
// restore the bitstream
currStream->bits_to_go = currStream->stored_bits_to_go;
currStream->byte_pos = currStream->stored_byte_pos;
currStream->byte_buf = currStream->stored_byte_buf;
skip = TRUE;
}
//! Check if the last coded macroblock fits into the size of the slice
//! But only if this is not the first macroblock of this slice
if (!new_slice)
{
if(slice_too_big(rlc_bits))
{
*recode_macroblock = TRUE;
*end_of_slice = TRUE;
}
else if(!img->cod_counter)
skip = FALSE;
}
// maximum number of MBs
// check if current slice group is finished
if ((*recode_macroblock == FALSE) && (img->current_mb_nr == FmoGetLastCodedMBOfSliceGroup (FmoMB2SliceGroup (img->current_mb_nr))))
{
*end_of_slice = TRUE;
if(!img->cod_counter)
skip = FALSE;
}
//! (first MB OR first MB in a slice) AND bigger that maximum size of slice
if (new_slice && slice_too_big(rlc_bits))
{
*end_of_slice = TRUE;
if(!img->cod_counter)
skip = FALSE;
}
if (!*recode_macroblock)
currSlice->num_mb++;
break;
case CALL_BACK:
if (img->current_mb_nr > 0 && !new_slice)
{
if (currSlice->slice_too_big(rlc_bits))
{
*recode_macroblock = TRUE;
*end_of_slice = TRUE;
}
}
if ( (*recode_macroblock == FALSE) && (img->current_mb_nr == FmoGetLastCodedMBOfSliceGroup (FmoMB2SliceGroup (img->current_mb_nr))))
*end_of_slice = TRUE;
break;
default:
snprintf(errortext, ET_SIZE, "Slice Mode %d not supported", input->slice_mode);
error(errortext, 600);
}
if (*recode_macroblock == TRUE)
{
// Restore everything
for (i=0; i<currSlice->max_part_nr; i++)
{
dataPart = &(currSlice->partArr[i]);
currStream = dataPart->bitstream;
currStream->bits_to_go = currStream->stored_bits_to_go;
currStream->byte_pos = currStream->stored_byte_pos;
currStream->byte_buf = currStream->stored_byte_buf;
stats->bit_slice = stats->stored_bit_slice;
if (input->symbol_mode == CABAC)
{
dataPart->ee_cabac = dataPart->ee_recode;
}
}
}
if (input->symbol_mode == UVLC)
{
// Skip MBs at the end of this slice
dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
if(*end_of_slice == TRUE && skip == TRUE)
{
// only for Slice Mode 2 or 3
// If we still have to write the skip, let's do it!
if(img->cod_counter && *recode_macroblock == TRUE) // MB that did not fit in this slice
{
// If recoding is true and we have had skip,
// we have to reduce the counter in case of recoding
img->cod_counter--;
if(img->cod_counter)
{
se.value1 = img->cod_counter;
se.value2 = 0;
se.type = SE_MBTYPE;
#if TRACE
snprintf(se.tracestring, TRACESTRING_SIZE, "Final MB runlength = %3d",img->cod_counter);
#endif
writeSE_UVLC(&se, dataPart);
rlc_bits=se.len;
currMB->bitcounter[BITS_MB_MODE]+=rlc_bits;
img->cod_counter = 0;
}
}
else //! MB that did not fit in this slice anymore is not a Skip MB
{
currStream = dataPart->bitstream;
// update the bitstream
currStream->bits_to_go = currStream->bits_to_go_skip;
currStream->byte_pos = currStream->byte_pos_skip;
currStream->byte_buf = currStream->byte_buf_skip;
// update the statistics
img->cod_counter = 0;
skip = FALSE;
}
}
// Skip MBs at the end of this slice for Slice Mode 0 or 1
if(*end_of_slice == TRUE && img->cod_counter && !use_bitstream_backing)
{
se.value1 = img->cod_counter;
se.value2 = 0;
se.type = SE_MBTYPE;
TRACE_SE (se.tracestring, "mb_skip_run");
writeSE_UVLC(&se, dataPart);
rlc_bits=se.len;
currMB->bitcounter[BITS_MB_MODE]+=rlc_bits;
img->cod_counter = 0;
}
}
}
/*!
*****************************************************************************
*
* \brief
* For Slice Mode 2: Checks if one partition of one slice exceeds the
* allowed size
*
* \return
* FALSE if all Partitions of this slice are smaller than the allowed size
* TRUE is at least one Partition exceeds the limit
*
* \par Side effects
* none
*
* \date
* 4 November 2001
*
* \author
* Tobias Oelbaum drehvial@gmx.net
*****************************************************************************/
int slice_too_big(int rlc_bits)
{
Slice *currSlice = img->currentSlice;
DataPartition *dataPart;
Bitstream *currStream;
EncodingEnvironmentPtr eep;
int i;
int size_in_bytes;
//! UVLC
if (input->symbol_mode == UVLC)
{
for (i=0; i<currSlice->max_part_nr; i++)
{
dataPart = &(currSlice->partArr[i]);
currStream = dataPart->bitstream;
size_in_bytes = currStream->byte_pos /*- currStream->tmp_byte_pos*/;
if (currStream->bits_to_go < 8)
size_in_bytes++;
if (currStream->bits_to_go < rlc_bits)
size_in_bytes++;
if(size_in_bytes > input->slice_argument)
return TRUE;
}
}
//! CABAC
if (input->symbol_mode ==CABAC)
{
for (i=0; i<currSlice->max_part_nr; i++)
{
dataPart= &(currSlice->partArr[i]);
eep = &(dataPart->ee_cabac);
if( arienco_bits_written(eep) > (input->slice_argument*8))
return TRUE;
}
}
return FALSE;
}
/*!
************************************************************************
* \brief
* Predict one component of a 4x4 Luma block
************************************************************************
*/
void OneComponentLumaPrediction4x4 ( imgpel* mpred, //!< array of prediction values (row by row)
int pic_pix_x, //!< absolute horizontal coordinate of 4x4 block
int pic_pix_y, //!< absolute vertical coordinate of 4x4 block
short* mv, //!< motion vector
short ref, //!< reference frame
StorablePicture **list //!< reference picture list
)
{
int j;
imgpel *ref_line;
width_pad = list[ref]->size_x_pad;
height_pad = list[ref]->size_y_pad;
if( IS_INDEPENDENT(input) )
{
ref_line = UMVLine4X (list[ref]->p_imgY_sub[img->colour_plane_id], pic_pix_y + mv[1], pic_pix_x + mv[0]);
}
else
{
ref_line = UMVLine4X (list[ref]->imgY_sub, pic_pix_y + mv[1], pic_pix_x + mv[0]);
}
for (j = 0; j < BLOCK_SIZE; j++)
{
memcpy(mpred, ref_line, BLOCK_SIZE * sizeof(imgpel));
ref_line += img_padded_size_x;
mpred += BLOCK_SIZE;
}
}
/*!
************************************************************************
* \brief
* Predict one 4x4 Luma block
************************************************************************
*/
void LumaPrediction4x4 ( int block_x, //!< relative horizontal block coordinate of 4x4 block
int block_y, //!< relative vertical block coordinate of 4x4 block
int p_dir, //!< prediction direction (0=list0, 1=list1, 2=bipred)
int l0_mode, //!< list0 prediction mode (1-7, 0=DIRECT if l1_mode=0)
int l1_mode, //!< list1 prediction mode (1-7, 0=DIRECT if l0_mode=0)
short l0_ref_idx, //!< reference frame for list0 prediction (-1: Intra4x4 pred. with l0_mode)
short l1_ref_idx //!< reference frame for list1 prediction
)
{
static imgpel l0_pred[16];
static imgpel l1_pred[16];
int i, j;
int block_x4 = block_x + 4;
int block_y4 = block_y + 4;
int pic_opix_x = ((img->opix_x + block_x) << 2) + IMG_PAD_SIZE_TIMES4;
int pic_opix_y = ((img->opix_y + block_y) << 2) + IMG_PAD_SIZE_TIMES4;
int bx = block_x >> 2;
int by = block_y >> 2;
imgpel* l0pred = l0_pred;
imgpel* l1pred = l1_pred;
Macroblock* currMB = &img->mb_data[img->current_mb_nr];
int apply_weights = ( (active_pps->weighted_pred_flag && (img->type== P_SLICE || img->type == SP_SLICE)) ||
(active_pps->weighted_bipred_idc && (img->type== B_SLICE)));
short**** mv_array = img->all_mv[by][bx];
if (currMB->bi_pred_me && l0_ref_idx == 0 && l1_ref_idx == 0 && p_dir == 2 && l0_mode==1 && l1_mode==1)
{
mv_array = currMB->bi_pred_me == 1? img->bipred_mv1[by][bx] : img->bipred_mv2[by][bx];
}
switch (p_dir)
{
case 0:
OneComponentLumaPrediction4x4 (l0_pred, pic_opix_x, pic_opix_y, mv_array[LIST_0][l0_ref_idx][l0_mode], l0_ref_idx, listX[0+currMB->list_offset]);
break;
case 1:
OneComponentLumaPrediction4x4 (l1_pred, pic_opix_x, pic_opix_y, mv_array[LIST_1][l1_ref_idx][l1_mode], l1_ref_idx, listX[1+currMB->list_offset]);
break;
case 2:
OneComponentLumaPrediction4x4 (l0_pred, pic_opix_x, pic_opix_y, mv_array[LIST_0][l0_ref_idx][l0_mode], l0_ref_idx, listX[0+currMB->list_offset]);
OneComponentLumaPrediction4x4 (l1_pred, pic_opix_x, pic_opix_y, mv_array[LIST_1][l1_ref_idx][l1_mode], l1_ref_idx, listX[1+currMB->list_offset]);
break;
default:
break;
}
if (apply_weights)
{
if (p_dir==2)
{
int wbp0 = wbp_weight[0][l0_ref_idx][l1_ref_idx][0];
int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][0];
int offset = (wp_offset[0][l0_ref_idx][0] + wp_offset[1][l1_ref_idx][0] + 1)>>1;
int wp_round = 2*wp_luma_round;
int weight_denom = luma_log_weight_denom + 1;
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[j][i] = iClip1( img->max_imgpel_value,
((wbp0 * *l0pred++ + wbp1 * *l1pred++ + wp_round) >> (weight_denom)) + offset);
}
else if (p_dir==0)
{
int wp = wp_weight[0][l0_ref_idx][0];
int offset = wp_offset[0][l0_ref_idx][0];
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[j][i] = iClip1( img->max_imgpel_value,
((wp * *l0pred++ + wp_luma_round) >> luma_log_weight_denom) + offset);
}
else // p_dir==1
{
int wp = wp_weight[1][l1_ref_idx][0];
int offset = wp_offset[1][l1_ref_idx][0];
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[j][i] = iClip1( img->max_imgpel_value,
((wp * *l1pred++ + wp_luma_round) >> luma_log_weight_denom) + offset );
}
}
else
{
if (p_dir==2)
{
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[j][i] = (*l0pred++ + *l1pred++ + 1) >> 1;
}
else if (p_dir==0)
{
for (j=block_y; j<block_y4; j++)
{
memcpy(&(img->mpr[j][block_x]), l0pred, BLOCK_SIZE * sizeof(imgpel));
l0pred += BLOCK_SIZE;
}
}
else // p_dir==1
{
for (j=block_y; j<block_y4; j++)
{
memcpy(&(img->mpr[j][block_x]), l1pred, BLOCK_SIZE * sizeof(imgpel));
l1pred += BLOCK_SIZE;
}
}
}
}
/*!
************************************************************************
* \brief
* Predict one 4x4 Luma block
************************************************************************
*/
void LumaPrediction4x4Bi ( int block_x, //!< relative horizontal block coordinate of 4x4 block
int block_y, //!< relative vertical block coordinate of 4x4 block
int l0_mode, //!< list0 prediction mode (1-7, 0=DIRECT if l1_mode=0)
int l1_mode, //!< list1 prediction mode (1-7, 0=DIRECT if l0_mode=0)
short l0_ref_idx, //!< reference frame for list0 prediction (-1: Intra4x4 pred. with l0_mode)
short l1_ref_idx, //!< reference frame for list1 prediction
int list //!< current list for prediction.
)
{
static imgpel l0_pred[16];
static imgpel l1_pred[16];
int i, j;
int block_x4 = block_x+4;
int block_y4 = block_y+4;
int pic_opix_x = ((img->opix_x + block_x) << 2) + IMG_PAD_SIZE_TIMES4;
int pic_opix_y = ((img->opix_y + block_y) << 2) + IMG_PAD_SIZE_TIMES4;
int bx = block_x >> 2;
int by = block_y >> 2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -