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

📄 cabac.c

📁 Mobile IP VCEG的信道模拟程序
💻 C
📖 第 1 页 / 共 3 页
字号:
*
***************************************************************************/
void readCBPFromBuffer_CABAC(SyntaxElement *se,
							 struct inp_par *inp,
							 struct img_par *img,		
							 DecodingEnvironmentPtr dep_dp)
{
	TextureInfoContexts *ctx = img->currentSlice->tex_ctx;
	Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	
	int mb_x, mb_y;
	int a, b;
	int curr_cbp_ctx, curr_cbp_idx;
	int cbp = 0;
	int cbp_bit;
    int mask;
  
	if ( se->type == SE_CBP_INTRA )
		curr_cbp_idx = 0;
	else
		curr_cbp_idx = 1; 
						
	/*  coding of luma part (bit by bit) */
	for (mb_y=0; mb_y < 4; mb_y += 2) 
	{
		for (mb_x=0; mb_x < 4; mb_x += 2)
		{       
					
			if (mb_y == 0)
			{
				if (currMB->mb_available[0][1] == NULL)
					b = 0;
				else 
					b = (( ((currMB->mb_available[0][1])->cbp & (1<<(2+mb_x/2))) == 0) ? 1 : 0);				
			}
			else
				b = ( ((cbp & (1<<(mb_x/2))) == 0) ? 1: 0);
					
			if (mb_x == 0)
			{
				if (currMB->mb_available[1][0] == NULL)
					a = 0;
				else 
					a = (( ((currMB->mb_available[1][0])->cbp & (1<<(mb_y+1))) == 0) ? 1 : 0);								
			}
			else
				a = ( ((cbp & (1<<mb_y)) == 0) ? 1: 0);

			curr_cbp_ctx = a+2*b;	
			mask = (1<<(mb_y+mb_x/2));
            cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[curr_cbp_idx][0] + curr_cbp_ctx );
            if (cbp_bit) cbp += mask;	
		}
	}
		
	/* coding of chroma part */
	b = 0;
	if (currMB->mb_available[0][1] != NULL)
		b = ((currMB->mb_available[0][1])->cbp > 15) ? 1 : 0;
			
	a = 0;
	if (currMB->mb_available[1][0] != NULL)
		a = ((currMB->mb_available[1][0])->cbp > 15) ? 1 : 0;
						
	curr_cbp_ctx = a+2*b;					 	
    cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[curr_cbp_idx][1] + curr_cbp_ctx );

    if (cbp_bit) /* set the chroma bits */
    {		
	    b = 0;
	    if (currMB->mb_available[0][1] != NULL)
		    if ((currMB->mb_available[0][1])->cbp > 15)
			    b = (( ((currMB->mb_available[0][1])->cbp >> 4) == 2) ? 1 : 0);

	    a = 0;
	    if (currMB->mb_available[1][0] != NULL)
		    if ((currMB->mb_available[1][0])->cbp > 15)
			    a = (( ((currMB->mb_available[1][0])->cbp >> 4) == 2) ? 1 : 0);
				
	    curr_cbp_ctx = a+2*b;
        cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[curr_cbp_idx][2] + curr_cbp_ctx );
        cbp += (cbp_bit == 1) ? 32 : 16;
    }
		
    se->value1 = cbp;

#if TRACE
    fprintf(p_trace, "@%d      %s\t\t\t%d",symbolCount++, se->tracestring, se->value1);
    fflush(p_trace);
#endif
}
/****************************************************************************
*
* Function:        readRunLevelFromBuffer_CABAC()
*
* Purpose:         This function is used to arithmetically decode level and 
*                  run of a given MB. 
*
*
***************************************************************************/
void readRunLevelFromBuffer_CABAC(  SyntaxElement *se,
								    struct inp_par *inp,
									struct img_par *img,		
									DecodingEnvironmentPtr dep_dp)
{
    int level;
	int run=0;
	const int curr_ctx_idx = se->context;
	int curr_level_ctx;
	int sign_of_level;
	int max_run;
	//    static int c=0;

	TextureInfoContexts *ctx = img->currentSlice->tex_ctx;
	//	Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	
	level = unary_level_decode(dep_dp,ctx->level_context[curr_ctx_idx]);
											
	if (level!=0)
	{
		curr_level_ctx = 3;														
        sign_of_level = biari_decode_symbol(dep_dp, ctx->level_context[curr_ctx_idx] + curr_level_ctx );
        if (sign_of_level) level = (-1)*level;
        //if (curr_ctx_idx != 0 && curr_ctx_idx != 3) // not double scan and not DC-chroma
        /* GB DC_chroma => ctx == 6 || ctx == 5 */
        if (curr_ctx_idx != 0 && curr_ctx_idx != 6 && curr_ctx_idx != 5) // not double scan and not DC-chroma
	        run = unary_bin_decode(dep_dp,ctx->run_context[curr_ctx_idx],1);
		else
		{
			max_run =  (curr_ctx_idx == 0) ? 7 : 3;  // if double scan max_run = 7; if DC-chroma max_run = 3;
            run = unary_bin_max_decode(dep_dp,ctx->run_context[curr_ctx_idx],1,max_run);
		}
	}
    se->value1 = level;
    se->value2 = run;


#if TRACE
    fprintf(p_trace, "@%d%s\t\t\t%d ",symbolCount++, se->tracestring, se->value1);
    fflush(p_trace);
#endif

}

/************************************************************************
*
*  Routine      readSyntaxElement_CABAC
*
*  Description: arithmetic decoding 
*
************************************************************************/
int	readSyntaxElement_CABAC(SyntaxElement *se, struct img_par *img, struct inp_par *inp, DataPartition *this_dataPart)
{
	int curr_len;
	DecodingEnvironmentPtr dep_dp = &(this_dataPart->de_cabac);

    curr_len = arideco_bits_read(dep_dp);

	/* perform the actual decoding by calling the appropriate method */
	se->reading(se, inp, img, dep_dp);

	return (se->len = (arideco_bits_read(dep_dp) - curr_len));
}

/************************************************************************
*
*  Routine      readSliceCABAC
*
*  Description: get slice and header  
*
************************************************************************/

int readSliceCABAC(struct img_par *img, struct inp_par *inp)
{
	Slice *currSlice = img->currentSlice;
	Bitstream *currStream = currSlice->partArr[0].bitstream;
	unsigned char *code_buffer = currStream->streamBuffer;
	int *read_len = &(currStream->read_len);
	DecodingEnvironmentPtr dep = &((currSlice->partArr[0]).de_cabac);
	int current_header;
	int BitstreamLengthInBytes;
	int info;
	int BitsUsedByHeader = 0, ByteStartPosition;
	
	currStream->frame_bitoffset =0;
	
	memset (code_buffer, 0xff, MAX_CODED_FRAME_SIZE);		// this prevents a buffer full with zeros
	BitstreamLengthInBytes = currStream->bitstram_length = GetOneSliceIntoSourceBitBuffer(code_buffer);
	
	// Here we are ready to interpret the picture and slice headers.  Since 
	// PictureHeader() and SliceHeader() get their data out of the UVLC's len/info
	// array, we need to convert the start of our slice to such a format.
	
	
	if (BitstreamLengthInBytes < 4) 
		return EOS;
	
	// Now we have the bits between the current startcode (inclusive) and the 
	// next start code in code_buffer.  Now decode the start codes and the headers
	if (31 != GetVLCSymbol (code_buffer, 0, &info, BitstreamLengthInBytes)) { 
		printf ("readSliceCABAC: Panic, expected start code symbol, found wrong len\n");
		exit (-1);
	}
	currStream->frame_bitoffset +=31;
	if (info==0) {
		current_header = SOP;
		BitsUsedByHeader+=PictureHeader(img, inp);
	}
	else if (info==1) {
		current_header = SOS;
		BitsUsedByHeader+=SliceHeader(img, inp);
	}
	else {
		printf ("readSliceCABAC: Panic, expected start code info 0 or 1, found %d\n", info);
		exit (-1);
	}
	
	ByteStartPosition = currStream->frame_bitoffset/8;
	if ((currStream->frame_bitoffset)%8 != 0)
		ByteStartPosition++;
	arideco_start_decoding(dep, code_buffer, ByteStartPosition, read_len);
	
	
	currSlice->picture_id = img->tr; 
	return current_header;
	
}

/************************************************************************
 * Module:		unary_bin_max_decode()
 *
 *
 * Purpose:   decoding of unary binarization using one or 2 distinct 
 *            models for the first and all remaining bins; no terminating
 *            "0" for max_symbol
 *
 *
 ***********************************************************************
 */
unsigned int unary_bin_max_decode(  DecodingEnvironmentPtr dep_dp,
								    BiContextTypePtr ctx,
								    int ctx_offset,
									unsigned int max_symbol)
{ 
	unsigned int l;
	unsigned int symbol;
	BiContextTypePtr ictx;

	symbol = 	biari_decode_symbol(dep_dp, ctx );

	if (symbol==0)
		return 0;
	else 
	{
		symbol=0;
		ictx=ctx+ctx_offset;
		do
		{
			l=biari_decode_symbol(dep_dp, ictx);
			symbol++;
		}
		while( (l!=0) && (symbol<max_symbol-1) );
		if ((l!=0) && (symbol==max_symbol-1))
			symbol++;
		return symbol;
	}  

}
/************************************************************************
 * Module:		unary_level_decode()
 *
 *
 * Purpose:   decoding of unary binarization of the absolute value 
 *            of a level using 3 distinct models by separating the first,
 *			  the second and all remaining bins
 *
 *
 ***********************************************************************
 */
unsigned int unary_level_decode(DecodingEnvironmentPtr dep_dp,
								BiContextTypePtr ctx)
{ 
	unsigned int l;
	unsigned int symbol;
	int bin=1;
	BiContextTypePtr ictx=ctx;

	symbol = biari_decode_symbol(dep_dp, ictx );

	if (symbol==0) 
		return 0;
	else 
	{
		symbol=0;
		ictx++;
		do
		{
			l=biari_decode_symbol(dep_dp, ictx  );
			if ((++bin)==2) ictx++;
			symbol++;
		}
		while (l!=0);	
		return symbol;
	}  
}
/************************************************************************
 * Module:		unary_bin_decode()
 *
 *
 * Purpose:   decoding of unary binarization using one or 2 distinct 
 *            models for the first and all remaining bins
 *
 *
 ***********************************************************************
 */
unsigned int unary_bin_decode(DecodingEnvironmentPtr dep_dp,
							  BiContextTypePtr ctx,
							  int ctx_offset)
{ 
	unsigned int l;
	unsigned int symbol;
	BiContextTypePtr ictx;

	symbol = biari_decode_symbol(dep_dp, ctx );

	if (symbol==0)
		return 0;
	else 
	{
		symbol=0;
		ictx=ctx+ctx_offset;
		do
		{
			l=biari_decode_symbol(dep_dp, ictx);
			symbol++;
		}
		while( l!=0 );
		return symbol;
	}  
}
/************************************************************************
 * Module:		unary_mv_decode()
 *
 *
 * Purpose:   decoding of unary binarization of the absolute value of a
 *            mv component using 4 distinct models by separating the first,
 *			  the second, intermediate and all remaining bins
 *
 *
 ***********************************************************************
 */
unsigned int unary_mv_decode(DecodingEnvironmentPtr dep_dp,
												BiContextTypePtr ctx,
												unsigned int max_bin)
{ 
	unsigned int l;
	unsigned int bin=1;
	unsigned int symbol;

	BiContextTypePtr ictx=ctx;

	symbol = biari_decode_symbol(dep_dp, ictx );

	if (symbol==0) 
		return 0;
	else 
	{		
		symbol=0;
		ictx++;
		do
		{
			l=biari_decode_symbol(dep_dp, ictx  );
			if ((++bin)==2) ictx++;
			if (bin==max_bin) ictx++;
			symbol++;
		}	
		while (l!=0);
		return symbol;
	}  
}

/************************************************************************
 * Module:		cabac_startcode_follows(struct img_par *img)
 *
 *
 * Purpose:   finding end of a slice in case this is not the end of a frame
 *			  
 *
 ************************************************************************/
int cabac_startcode_follows(struct img_par *img, struct inp_par *inp)
{
    Slice *currSlice = img->currentSlice;
	if (img->current_mb_nr == currSlice->last_mb_nr)
		return TRUE;
	return FALSE;
}

⌨️ 快捷键说明

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