?? vlc.c
字號:
/*!
************************************************************************
* \file vlc.c
*
* \brief
* VLC support 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>
* - Gabi Blaettermann
************************************************************************
*/
//#include "contributors.h"
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include "global.h"
#include "vlc.h"
#include "elements.h"
// A little trick to avoid those horrible #if TRACE all over the source code
#if TRACE
#define SYMTRACESTRING(s) strncpy(symbol.tracestring,s,TRACESTRING_SIZE)
#else
#define SYMTRACESTRING(s) // do nothing
#endif
extern void tracebits(const char *trace_str, int len, int info,int value1);
int UsedBits; // for internal statistics, is adjusted by se_v, ue_v, u_1
// Note that all NA values are filled with 0
//! for the linfo_levrun_inter routine
const byte NTAB1[4][8][2] =
{
{{1,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{1,1},{1,2},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{2,0},{1,3},{1,4},{1,5},{0,0},{0,0},{0,0},{0,0}},
{{3,0},{2,1},{2,2},{1,6},{1,7},{1,8},{1,9},{4,0}},
};
const byte LEVRUN1[16]=
{
4,2,2,1,1,1,1,1,1,1,0,0,0,0,0,0,
};
const byte NTAB2[4][8][2] =
{
{{1,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{1,1},{2,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{1,2},{3,0},{4,0},{5,0},{0,0},{0,0},{0,0},{0,0}},
{{1,3},{1,4},{2,1},{3,1},{6,0},{7,0},{8,0},{9,0}},
};
//! for the linfo_levrun__c2x2 routine
const byte LEVRUN3[4] =
{
2,1,0,0
};
const byte NTAB3[2][2][2] =
{
{{1,0},{0,0}},
{{2,0},{1,1}},
};
/*!
*************************************************************************************
* \brief
* ue_v, reads an ue(v) syntax element, the length in bits is stored in
* the global UsedBits variable
*
* \param tracestring
* the string for the trace file
*
* \param bitstream
* the stream to be read from
*
* \return
* the value of the coded syntax element
*
*************************************************************************************
*/
int ue_v (char *tracestring, Bitstream *bitstream)
{
SyntaxElement symbol;
assert (bitstream->streamBuffer != NULL);
symbol.type = SE_HEADER;
symbol.mapping = linfo_ue; // Mapping rule
SYMTRACESTRING(tracestring);
readSyntaxElement_VLC (&symbol, bitstream);
UsedBits+=symbol.len;
return symbol.value1;
}
/*!
*************************************************************************************
* \brief
* ue_v, reads an se(v) syntax element, the length in bits is stored in
* the global UsedBits variable
*
* \param tracestring
* the string for the trace file
*
* \param bitstream
* the stream to be read from
*
* \return
* the value of the coded syntax element
*
*************************************************************************************
*/
int se_v (char *tracestring, Bitstream *bitstream)
{
SyntaxElement symbol;
assert (bitstream->streamBuffer != NULL);
symbol.type = SE_HEADER;
symbol.mapping = linfo_se; // Mapping rule: signed integer
SYMTRACESTRING(tracestring);
readSyntaxElement_VLC (&symbol, bitstream);
UsedBits+=symbol.len;
return symbol.value1;
}
/*!
*************************************************************************************
* \brief
* ue_v, reads an u(v) syntax element, the length in bits is stored in
* the global UsedBits variable
*
* \param LenInBits
* length of the syntax element
*
* \param tracestring
* the string for the trace file
*
* \param bitstream
* the stream to be read from
*
* \return
* the value of the coded syntax element
*
*************************************************************************************
*/
int u_v (int LenInBits, char*tracestring, Bitstream *bitstream)
{
SyntaxElement symbol;
symbol.inf = 0;
assert (bitstream->streamBuffer != NULL);
symbol.type = SE_HEADER;
symbol.mapping = linfo_ue; // Mapping rule
symbol.len = LenInBits;
SYMTRACESTRING(tracestring);
readSyntaxElement_FLC (&symbol, bitstream);
UsedBits+=symbol.len;
return symbol.inf;
}
/*!
*************************************************************************************
* \brief
* i_v, reads an i(v) syntax element, the length in bits is stored in
* the global UsedBits variable
*
* \param LenInBits
* length of the syntax element
*
* \param tracestring
* the string for the trace file
*
* \param bitstream
* the stream to be read from
*
* \return
* the value of the coded syntax element
*
*************************************************************************************
*/
int i_v (int LenInBits, char*tracestring, Bitstream *bitstream)
{
SyntaxElement symbol;
symbol.inf = 0;
assert (bitstream->streamBuffer != NULL);
symbol.type = SE_HEADER;
symbol.mapping = linfo_ue; // Mapping rule
symbol.len = LenInBits;
SYMTRACESTRING(tracestring);
readSyntaxElement_FLC (&symbol, bitstream);
UsedBits+=symbol.len;
// can be negative
symbol.inf = -( symbol.inf & (1 << (LenInBits - 1)) ) | symbol.inf;
return symbol.inf;
}
/*!
*************************************************************************************
* \brief
* ue_v, reads an u(1) syntax element, the length in bits is stored in
* the global UsedBits variable
*
* \param tracestring
* the string for the trace file
*
* \param bitstream
* the stream to be read from
*
* \return
* the value of the coded syntax element
*
*************************************************************************************
*/
Boolean u_1 (char *tracestring, Bitstream *bitstream)
{
return (Boolean) u_v (1, tracestring, bitstream);
}
/*!
************************************************************************
* \brief
* mapping rule for ue(v) syntax elements
* \par Input:
* lenght and info
* \par Output:
* number in the code table
************************************************************************
*/
void linfo_ue(int len, int info, int *value1, int *dummy)
{
assert ((len >> 1) < 32);
*value1 = (1 << (len >> 1)) + info - 1;
}
/*!
************************************************************************
* \brief
* mapping rule for se(v) syntax elements
* \par Input:
* lenght and info
* \par Output:
* signed mvd
************************************************************************
*/
void linfo_se(int len, int info, int *value1, int *dummy)
{
int n;
assert ((len >> 1) < 32);
n = (1 << (len >> 1)) + info - 1;
*value1 = (n + 1) >> 1;
if((n & 0x01) == 0) // lsb is signed bit
*value1 = -*value1;
}
/*!
************************************************************************
* \par Input:
* length and info
* \par Output:
* cbp (intra)
************************************************************************
*/
//void linfo_cbp_intra(int len,int info,int *cbp, int *dummy)
//{
// extern const byte NCBP[2][48][2];
// int cbp_idx;
//
// linfo_ue(len, info, &cbp_idx, dummy);
// *cbp = NCBP[active_sps->chroma_format_idc ? 1 : 0][cbp_idx][0];
//}
/*!
************************************************************************
* \par Input:
* length and info
* \par Output:
* cbp (inter)
************************************************************************
*/
//void linfo_cbp_inter(int len,int info,int *cbp, int *dummy)
//{
// extern const byte NCBP[2][48][2];
// int cbp_idx;
//
// linfo_ue(len, info, &cbp_idx, dummy);
// *cbp = NCBP[active_sps->chroma_format_idc ? 1 : 0][cbp_idx][1];
//}
/*!
************************************************************************
* \par Input:
* length and info
* \par Output:
* level, run
************************************************************************
*/
//void linfo_levrun_inter(int len, int info, int *level, int *irun)
//{
// int l2;
// int inf;
// assert (((len>>1)-5)<32);
// if (len<=9)
// {
// l2 = imax(0,(len >> 1)-1);
// inf = info >> 1;
// *level = NTAB1[l2][inf][0];
// *irun = NTAB1[l2][inf][1];
// if ((info & 0x01) == 1)
// *level = -*level; // make sign
// }
// else // if len > 9, skip using the array
// {
// *irun = (info & 0x1e) >> 1;
// *level = LEVRUN1[*irun] + (info >> 5) + ( 1 << ((len >> 1) - 5));
// if ((info & 0x01) == 1)
// *level = -*level;
// }
// if (len == 1) // EOB
// *level = 0;
//}
/*!
************************************************************************
* \par Input:
* length and info
* \par Output:
* level, run
************************************************************************
*/
//void linfo_levrun_c2x2(int len, int info, int *level, int *irun)
//{
// int l2;
// int inf;
//
// if (len<=5)
// {
// l2 = imax(0, (len >> 1) - 1);
// inf = info >> 1;
// *level = NTAB3[l2][inf][0];
// *irun = NTAB3[l2][inf][1];
// if ((info & 0x01) == 1)
// *level = -*level; // make sign
// }
// else // if len > 5, skip using the array
// {
// *irun = (info & 0x06) >> 1;
// *level = LEVRUN3[*irun] + (info >> 3) + (1 << ((len >> 1) - 3));
// if ((info & 0x01) == 1)
// *level = -*level;
// }
// if (len == 1) // EOB
// *level = 0;
//}
/*!
************************************************************************
* \brief
* read next UVLC codeword from UVLC-partition and
* map it to the corresponding syntax element
************************************************************************
*/
int readSyntaxElement_VLC(SyntaxElement *sym, Bitstream *currStream)
{
int frame_bitoffset = currStream->frame_bitoffset;
int BitstreamLengthInBytes = currStream->bitstream_length;
byte *buf = currStream->streamBuffer;
sym->len = GetVLCSymbol (buf, frame_bitoffset, &(sym->inf), BitstreamLengthInBytes);
if (sym->len == -1)
return -1;
currStream->frame_bitoffset += sym->len;
sym->mapping(sym->len,sym->inf,&(sym->value1),&(sym->value2));
#if TRACE
tracebits(sym->tracestring, sym->len, sym->inf, sym->value1);
#endif
return 1;
}
/*!
************************************************************************
* \brief
* read next UVLC codeword from UVLC-partition and
* map it to the corresponding syntax element
************************************************************************
*/
//int readSyntaxElement_UVLC(SyntaxElement *sym, struct img_par *img, struct datapartition *dP)
//{
// return (readSyntaxElement_VLC(sym, dP->bitstream));
//}
/*!
************************************************************************
* \brief
* read next VLC codeword for 4x4 Intra Prediction Mode and
* map it to the corresponding Intra Prediction Direction
************************************************************************
*/
//int readSyntaxElement_Intra4x4PredictionMode(SyntaxElement *sym, struct img_par *img,struct datapartition *dP)
//{
// Bitstream *currStream = dP->bitstream;
// int frame_bitoffset = currStream->frame_bitoffset;
// int BitstreamLengthInBytes = currStream->bitstream_length;
// byte *buf = currStream->streamBuffer;
//
// sym->len = GetVLCSymbol_IntraMode (buf, frame_bitoffset, &(sym->inf), BitstreamLengthInBytes);
//
// if (sym->len == -1)
// return -1;
//
// currStream->frame_bitoffset += sym->len;
// sym->value1 = sym->len == 1 ? -1 : sym->inf;
//
//#if TRACE
// tracebits2(sym->tracestring, sym->len, sym->value1);
//#endif
//
// return 1;
//}
//int GetVLCSymbol_IntraMode (byte buffer[],int totbitoffset,int *info, int bytecount)
//{
//
// register int inf;
// long byteoffset = (totbitoffset >> 3); // byte from start of buffer
// int bitoffset = (7 - (totbitoffset & 0x07)); // bit from start of byte
// byte *cur_byte = &(buffer[byteoffset]);
// int ctr_bit = (*cur_byte & (0x01 << bitoffset)); // control bit for current bit posision
// int bitcounter = 1;
// int len = 0;
//
// //First bit
// if (ctr_bit)
// {
// *info = 0;
// return bitcounter;
// }
// else
// len = 3;
//
// if (byteoffset + ((len + 7) >> 3) > bytecount)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -