Added bitstream functions

This commit is contained in:
Marko Viitanen 2012-05-30 15:37:42 +03:00
parent ef9784c107
commit bbe03211e1
4 changed files with 152 additions and 0 deletions

View file

@ -52,6 +52,7 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<CompileAs>CompileAsC</CompileAs>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@ -67,6 +68,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<CompileAs>CompileAsC</CompileAs>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@ -76,10 +78,12 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\bitstream.c" />
<ClCompile Include="..\..\src\config.c" /> <ClCompile Include="..\..\src\config.c" />
<ClCompile Include="..\..\src\encmain.c" /> <ClCompile Include="..\..\src\encmain.c" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\src\bitstream.h" />
<ClInclude Include="..\..\src\config.h" /> <ClInclude Include="..\..\src\config.h" />
<ClInclude Include="..\..\src\global.h" /> <ClInclude Include="..\..\src\global.h" />
</ItemGroup> </ItemGroup>

View file

@ -21,6 +21,9 @@
<ClCompile Include="..\..\src\config.c"> <ClCompile Include="..\..\src\config.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\bitstream.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\src\global.h"> <ClInclude Include="..\..\src\global.h">
@ -29,5 +32,8 @@
<ClInclude Include="..\..\src\config.h"> <ClInclude Include="..\..\src\config.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\bitstream.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

121
src/bitstream.c Normal file
View file

@ -0,0 +1,121 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include "global.h"
#include "bitstream.h"
void printf_bitstream(char *msg, ...)
{
va_list fmtargs;
char buffer[1024];
va_start(fmtargs,msg);
vsnprintf(buffer,sizeof(buffer)-1,msg,fmtargs);
va_end(fmtargs);
printf("%s",buffer);
}
/*
* Clear bitstream
*/
void bitstream_init(bitstream* stream)
{
stream->cur_byte=0;
stream->cur_bit=0;
memset(stream->data, 0, sizeof(uint32_t)*32);
}
/*
* Put bits to bitstream
* Input:
* stream = pointer bitstream to put the data
* data = pointer to actual data
* bits = number of bits to write
*/
void bitstream_put(bitstream* stream, uint32_t data, uint8_t bits)
{
uint8_t i=0;
uint32_t bitsleft=32-stream->cur_bit;
printf_bitstream("put: ");
for(i=0;i<bits;i++)
{
printf("%i",(data&(1<<(bits-i-1)))?1:0);
}
printf_bitstream("\n");
//printf_bitstream(" count: %i\n",bits);
//Theres space for all the bits
if(bits<=bitsleft)
{
stream->data[stream->cur_byte] |= (data<<((bitsleft-bits)));
stream->cur_bit+=bits;
bits=0;
}
//No space for everything, store the bits we can and continue later
else
{
stream->data[stream->cur_byte] |= (data>>(bits-bitsleft));
stream->cur_bit=32;
bits-=bitsleft;
}
//Check if the buffer is full
if(stream->cur_bit==32)
{
stream->cur_byte++;
bitsleft=32;
if(stream->cur_byte==32)
{
//Flush data out
bitstream_flush(stream);
}
}
//..still some writing to do
if(bits!=0)
{
stream->data[stream->cur_byte] |= (data<<(bitsleft-bits));
stream->cur_bit+=bits;
}
}
/*
* Align the bitstream
*/
void bitstream_align(bitstream* stream)
{
if(stream->cur_byte==32)
{
//Stream flushed, zero out the values
bitstream_init(stream);
}
else
{
stream->cur_byte++;
}
}
void bitstream_flush(bitstream* stream)
{
/*
* SAVE DATA TO OUTPUT
*/
//Stream flushed, zero out the values
bitstream_init(stream);
}

21
src/bitstream.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef _BITSTREAM_H
#define _BITSTREAM_H
typedef struct
{
uint32_t data[32];
uint8_t cur_byte;
uint8_t cur_bit;
} bitstream;
void bitstream_init(bitstream* stream);
void bitstream_put(bitstream* stream, uint32_t* data, uint8_t bits);
void bitstream_align(bitstream* stream);
void bitstream_flush(bitstream* stream);
#endif