📄 vlc.c
字号:
/*
***********************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2001, International Telecommunications Union, Geneva
*
* DISCLAIMER OF WARRANTY
*
* These software programs are available to the user without any
* license fee or royalty on an "as is" basis. The ITU disclaims
* any and all warranties, whether express, implied, or
* statutory, including any implied warranties of merchantability
* or of fitness for a particular purpose. In no event shall the
* contributor or the ITU be liable for any incidental, punitive, or
* consequential damages of any kind whatsoever arising from the
* use of these programs.
*
* This disclaimer of warranty extends to the user of these programs
* and user's customers, employees, agents, transferees, successors,
* and assigns.
*
* The ITU does not represent or warrant that the programs furnished
* hereunder are free of infringement of any third-party patents.
* Commercial implementations of ITU-T Recommendations, including
* shareware, may be subject to royalty fees to patent holders.
* Information regarding the ITU-T patent policy is available from
* the ITU Web site at http://www.itu.int.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.
************************************************************************
*/
/*!
***************************************************************************
* \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) // to 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 bitstream
* the Bitstream 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;
#if TRACE
strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
#endif
assert (part->bitstream->streamBuffer != NULL);
return writeSyntaxElement_UVLC (sym, part);
}
/*!
*************************************************************************************
* \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 bitstream
* the Bitstream 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;
#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 bitstream
* the Bitstream 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;
#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 tracestring
* the string for the trace file
* \param value
* the value to be coded
* \param bitstream
* the Bitstream 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;
#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 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 ue(v) syntax elements
* \param ue
* value to be mapped
* \param info
* returns mapped value
* \param len
* returns mapped value length
************************************************************************
*/
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:
* lenght 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:
* lenght 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 coefficiets
* \par Output:
* lenght and info
* \note
* see ITU document for bit assignment
************************************************************************
*/
//move here by zdd
const int NTAB_levrun_linfo_c2x2[2][2]=
{
{1,5},
{3,0}
};
const int LEVRUN_levrun_linfo_c2x2[4]=
{
2,1,0,0
};
//end
void levrun_linfo_c2x2(int level,int run,int *len,int *info)
{
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_levrun_linfo_c2x2[run])
{
n=NTAB_levrun_linfo_c2x2[levabs-1][run]+1;
}
else
{
n=(levabs-LEVRUN_levrun_linfo_c2x2[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
************************************************************************
*/
//move here by zdd
const byte LEVRUN_levrun_linfo_inter[16]=
{
4,2,2,1,1,1,1,1,1,1,0,0,0,0,0,0
};
const byte NTAB_levrun_linfo_inter[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},
};
//end
void levrun_linfo_inter(int level,int run,int *len,int *info)
{
int levabs,i,n,sign,nn;
if (level == 0) // check for EOB
{
*len=1;
return;
}
if (level <= 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -