📄 vlc.c
字号:
/*!
***************************************************************************
* \file vlc.c
*
* \brief
* (CA)VLC coding functions
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Inge Lille-Lang鴜 <inge.lille-langoy@telenor.com>
* - Detlev Marpe <marpe@hhi.de>
* - Stephan Wenger <stewe@cs.tu-berlin.de>
***************************************************************************
*/
#include "contributors.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "elements.h"
#include "vlc.h"
#if TRACE
#define SYMTRACESTRING(s) strncpy(sym.tracestring,s,TRACESTRING_SIZE)
#else
#define SYMTRACESTRING(s) // do nothing
#endif
/*!
*************************************************************************************
* \brief
* ue_v, writes an ue(v) syntax element, returns the length in bits
*
* \param tracestring
* the string for the trace file
* \param value
* the value to be coded
* \param part
* the Data Partition the value should be coded into
*
* \return
* Number of bits used by the coded syntax element
*
* \ note
* This function writes always the bit buffer for the progressive scan flag, and
* should not be used (or should be modified appropriately) for the interlace crap
* When used in the context of the Parameter Sets, this is obviously not a
* problem.
*
*************************************************************************************
*/
int ue_v (char *tracestring, int value, DataPartition *part)
{
SyntaxElement symbol, *sym=&symbol;
sym->type = SE_HEADER;
sym->mapping = ue_linfo; // Mapping rule: unsigned integer
sym->value1 = value;
sym->value2 = 0;
#if TRACE
strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
#endif
assert (part->bitstream->streamBuffer != NULL);
return writeSyntaxElement_UVLC (sym, part);
}
/*!
*************************************************************************************
* \brief
* se_v, writes an se(v) syntax element, returns the length in bits
*
* \param tracestring
* the string for the trace file
* \param value
* the value to be coded
* \param part
* the Data Partition the value should be coded into
*
* \return
* Number of bits used by the coded syntax element
*
* \ note
* This function writes always the bit buffer for the progressive scan flag, and
* should not be used (or should be modified appropriately) for the interlace crap
* When used in the context of the Parameter Sets, this is obviously not a
* problem.
*
*************************************************************************************
*/
int se_v (char *tracestring, int value, DataPartition *part)
{
SyntaxElement symbol, *sym=&symbol;
sym->type = SE_HEADER;
sym->mapping = se_linfo; // Mapping rule: signed integer
sym->value1 = value;
sym->value2 = 0;
#if TRACE
strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
#endif
assert (part->bitstream->streamBuffer != NULL);
return writeSyntaxElement_UVLC (sym, part);
}
/*!
*************************************************************************************
* \brief
* u_1, writes a flag (u(1) syntax element, returns the length in bits,
* always 1
*
* \param tracestring
* the string for the trace file
* \param value
* the value to be coded
* \param part
* the Data Partition the value should be coded into
*
* \return
* Number of bits used by the coded syntax element (always 1)
*
* \ note
* This function writes always the bit buffer for the progressive scan flag, and
* should not be used (or should be modified appropriately) for the interlace crap
* When used in the context of the Parameter Sets, this is obviously not a
* problem.
*
*************************************************************************************
*/
int u_1 (char *tracestring, int value, DataPartition *part)
{
SyntaxElement symbol, *sym=&symbol;
sym->bitpattern = value;
sym->len = 1;
sym->type = SE_HEADER;
sym->value1 = value;
sym->value2 = 0;
#if TRACE
strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
#endif
assert (part->bitstream->streamBuffer != NULL);
return writeSyntaxElement_fixed(sym, part);
}
/*!
*************************************************************************************
* \brief
* u_v, writes a a n bit fixed length syntax element, returns the length in bits,
*
* \param n
* length in bits
* \param tracestring
* the string for the trace file
* \param value
* the value to be coded
* \param part
* the Data Partition the value should be coded into
*
* \return
* Number of bits used by the coded syntax element
*
* \ note
* This function writes always the bit buffer for the progressive scan flag, and
* should not be used (or should be modified appropriately) for the interlace crap
* When used in the context of the Parameter Sets, this is obviously not a
* problem.
*
*************************************************************************************
*/
int u_v (int n, char *tracestring, int value, DataPartition *part)
{
SyntaxElement symbol, *sym=&symbol;
sym->bitpattern = value;
sym->len = n;
sym->type = SE_HEADER;
sym->value1 = value;
sym->value2 = 0;
#if TRACE
strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
#endif
assert (part->bitstream->streamBuffer != NULL);
return writeSyntaxElement_fixed(sym, part);
}
/*!
************************************************************************
* \brief
* mapping for ue(v) syntax elements
* \param ue
* value to be mapped
* \param dummy
* dummy parameter
* \param info
* returns mapped value
* \param len
* returns mapped value length
************************************************************************
*/
void ue_linfo(int ue, int dummy, int *len,int *info)
{
int i,nn;
nn=(ue+1)/2;
for (i=0; i < 16 && nn != 0; i++)
{
nn /= 2;
}
*len= 2*i + 1;
*info=ue+1-(int)pow(2,i);
}
/*!
************************************************************************
* \brief
* mapping for se(v) syntax elements
* \param se
* value to be mapped
* \param dummy
* dummy parameter
* \param len
* returns mapped value length
* \param info
* returns mapped value
************************************************************************
*/
void se_linfo(int se, int dummy, int *len,int *info)
{
int i,n,sign,nn;
sign=0;
if (se <= 0)
{
sign=1;
}
n=abs(se) << 1;
/*
n+1 is the number in the code table. Based on this we find length and info
*/
nn=n/2;
for (i=0; i < 16 && nn != 0; i++)
{
nn /= 2;
}
*len=i*2 + 1;
*info=n - (int)pow(2,i) + sign;
}
/*!
************************************************************************
* \par Input:
* Number in the code table
* \par Output:
* length and info
************************************************************************
*/
void cbp_linfo_intra(int cbp, int dummy, int *len,int *info)
{
extern const int NCBP[48][2];
ue_linfo(NCBP[cbp][0], dummy, len, info);
}
/*!
************************************************************************
* \par Input:
* Number in the code table
* \par Output:
* length and info
************************************************************************
*/
void cbp_linfo_inter(int cbp, int dummy, int *len,int *info)
{
extern const int NCBP[48][2];
ue_linfo(NCBP[cbp][1], dummy, len, info);
}
/*!
************************************************************************
* \brief
* 2x2 transform of chroma DC
* \par Input:
* level and run for coefficients
* \par Output:
* length and info
* \note
* see ITU document for bit assignment
************************************************************************
*/
void levrun_linfo_c2x2(int level,int run,int *len,int *info)
{
const int NTAB[2][2]=
{
{1,5},
{3,0}
};
const int LEVRUN[4]=
{
2,1,0,0
};
int levabs,i,n,sign,nn;
if (level == 0) // check if the coefficient sign EOB (level=0)
{
*len=1;
return;
}
sign=0;
if (level <= 0)
{
sign=1;
}
levabs=abs(level);
if (levabs <= LEVRUN[run])
{
n=NTAB[levabs-1][run]+1;
}
else
{
n=(levabs-LEVRUN[run])*8 + run*2;
}
nn=n/2;
for (i=0; i < 16 && nn != 0; i++)
{
nn /= 2;
}
*len= 2*i + 1;
*info=n-(int)pow(2,i)+sign;
}
/*!
************************************************************************
* \brief
* Single scan coefficients
* \par Input:
* level and run for coefficiets
* \par Output:
* lenght and info
* \note
* see ITU document for bit assignment
************************************************************************
*/
void levrun_linfo_inter(int level,int run,int *len,int *info)
{
const byte LEVRUN[16]=
{
4,2,2,1,1,1,1,1,1,1,0,0,0,0,0,0
};
const byte NTAB[4][10]=
{
{ 1, 3, 5, 9,11,13,21,23,25,27},
{ 7,17,19, 0, 0, 0, 0, 0, 0, 0},
{15, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{29, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};
int levabs,i,n,sign,nn;
if (level == 0) // check for EOB
{
*len=1;
return;
}
if (level <= 0)
sign=1;
else
sign=0;
levabs=abs(level);
if (levabs <= LEVRUN[run])
{
n=NTAB[levabs-1][run]+1;
}
else
{
n=(levabs-LEVRUN[run])*32 + run*2;
}
nn=n/2;
for (i=0; i < 16 && nn != 0; i++)
{
nn /= 2;
}
*len= 2*i + 1;
*info=n-(int)pow(2,i)+sign;
}
/*!
************************************************************************
* \brief
* Double scan coefficients
* \par Input:
* level and run for coefficiets
* \par Output:
* lenght and info
* \note
* see ITU document for bit assignment
************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -