📄 mpg_getbits.c
字号:
/* getbits.c, bit level routines */
//#include <stdio.h>
//#include <stdlib.h>
#include "mpg_config.h"
#include "mpg_global.h"
/* initialize buffer, call once before first getbits or showbits */
void Initialize_Buffer(layer_data *laydat)
{
laydat->Incnt = 0;
laydat->Rdptr = laydat->Rdbfr + 2048;
laydat->Rdmax = laydat->Rdptr;
#ifdef VERIFY
/* only the verifier uses this particular bit counter
* Bitcnt keeps track of the current parser position with respect
* to the video elementary stream being decoded, regardless
* of whether or not it is wrapped within a systems layer stream
*/
laydat->Bitcnt = 0;
#endif
laydat->Bfr = 0;
if (laydat != ld2)
Flush_Buffer(laydat, 0); /* fills valid data into bfr */
}
void Fill_Buffer(layer_data *laydat)
{
int Buffer_Level;
Buffer_Level = fread(laydat->Rdbfr, 1, 2048, laydat->Infile);
laydat->Rdptr = laydat->Rdbfr;
if (System_Stream_Flag)
laydat->Rdmax -= 2048;
/* end of the bitstream file */
if (Buffer_Level < 2048)
{
/* just to be safe */
if (Buffer_Level < 0)
Buffer_Level = 0;
/* pad until the next to the next 32-bit word boundary */
while (Buffer_Level & 3)
laydat->Rdbfr[Buffer_Level++] = 0;
/* pad the buffer with sequence end codes */
while (Buffer_Level < 2048)
{
laydat->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE >> 24;
laydat->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE >> 16;
laydat->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE >> 8;
laydat->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE & 0xff;
}
}
}
/* MPEG-1 system layer demultiplexer */
int Get_Byte(layer_data *laydat)
{
int readSize;
while (laydat->Rdptr >= laydat->Rdbfr + 2048)
{
readSize = fread(laydat->Rdbfr, 1, 2048, laydat->Infile);
if (readSize <= 0)
{
//fprintf(stderr,"Reach file end!\n");
//exit(1);
if ((readSize = fread(laydat->Rdbfr, 1, 2048, laydat->Infile)) <= 0)
Fault_Flag = 1;
}
laydat->Rdptr -= 2048;
laydat->Rdmax -= 2048;
}
return *laydat->Rdptr++;
}
/* extract a 16-bit word from the bitstream buffer */
int Get_Word(layer_data *laydat)
{
int Val;
Val = Get_Byte(laydat);
return (Val << 8) | Get_Byte(laydat);
}
/* return next n bits (right adjusted) without advancing */
unsigned int Show_Bits(layer_data *laydat, int N)
{
return laydat->Bfr >> (32 - N);
}
/* return next bit (could be made faster than Get_Bits(1)) */
unsigned int Get_Bits1(layer_data *laydat)
{
return Get_Bits(laydat, 1);
}
/* advance by n bits */
void Flush_Buffer(layer_data *laydat , int N)
{
int Incnt;
laydat->Bfr <<= N;
Incnt = laydat->Incnt -= N;
if (Incnt <= 24)
{
if (System_Stream_Flag && (laydat->Rdptr >= laydat->Rdmax - 4))
{
do
{
if (laydat->Rdptr >= laydat->Rdmax)
if (laydat == ld)
Next_Packet(0);
else
Next_PacketAudio();
laydat->Bfr |= Get_Byte(laydat) << (24 - Incnt);
Incnt += 8;
}
while (Incnt <= 24);
}
else if (laydat->Rdptr < laydat->Rdbfr + 2044)
{
do
{
laydat->Bfr |= *laydat->Rdptr++ << (24 - Incnt);
Incnt += 8;
}
while (Incnt <= 24);
}
else
{
do
{
if (laydat->Rdptr >= laydat->Rdbfr + 2048)
Fill_Buffer(laydat);
laydat->Bfr |= *laydat->Rdptr++ << (24 - Incnt);
Incnt += 8;
}
while (Incnt <= 24);
}
laydat->Incnt = Incnt;
}
#ifdef VERIFY
laydat->Bitcnt += N;
#endif /* VERIFY */
}
/* return next n bits (right adjusted) */
unsigned int Get_Bits(layer_data *laydat, int N)
{
unsigned int Val;
Val = Show_Bits(laydat, N);
Flush_Buffer(laydat, N);
return Val;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -