doom3-bfg/neo/swf/SWF_Bitstream.cpp

454 lines
12 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "../idlib/precompiled.h"
#define NBM( x ) (int32)( ( 1LL << x ) - 1 )
int maskForNumBits[33] = { NBM( 0x00 ), NBM( 0x01 ), NBM( 0x02 ), NBM( 0x03 ),
NBM( 0x04 ), NBM( 0x05 ), NBM( 0x06 ), NBM( 0x07 ),
NBM( 0x08 ), NBM( 0x09 ), NBM( 0x0A ), NBM( 0x0B ),
NBM( 0x0C ), NBM( 0x0D ), NBM( 0x0E ), NBM( 0x0F ),
NBM( 0x10 ), NBM( 0x11 ), NBM( 0x12 ), NBM( 0x13 ),
NBM( 0x14 ), NBM( 0x15 ), NBM( 0x16 ), NBM( 0x17 ),
NBM( 0x18 ), NBM( 0x19 ), NBM( 0x1A ), NBM( 0x1B ),
NBM( 0x1C ), NBM( 0x1D ), NBM( 0x1E ), NBM( 0x1F ), -1
};
2012-11-26 18:58:24 +00:00
#define NBS( x ) (int32)( (-1) << ( x - 1 ) )
int signForNumBits[33] = { NBS( 0x01 ), NBS( 0x01 ), NBS( 0x02 ), NBS( 0x03 ),
NBS( 0x04 ), NBS( 0x05 ), NBS( 0x06 ), NBS( 0x07 ),
NBS( 0x08 ), NBS( 0x09 ), NBS( 0x0A ), NBS( 0x0B ),
NBS( 0x0C ), NBS( 0x0D ), NBS( 0x0E ), NBS( 0x0F ),
NBS( 0x10 ), NBS( 0x11 ), NBS( 0x12 ), NBS( 0x13 ),
NBS( 0x14 ), NBS( 0x15 ), NBS( 0x16 ), NBS( 0x17 ),
NBS( 0x18 ), NBS( 0x19 ), NBS( 0x1A ), NBS( 0x1B ),
NBS( 0x1C ), NBS( 0x1D ), NBS( 0x1E ), NBS( 0x1F ), NBS( 0x20 )
};
2012-11-26 18:58:24 +00:00
/*
========================
idSWFBitStream::idSWFBitStream
========================
*/
idSWFBitStream::idSWFBitStream()
{
2012-11-26 18:58:24 +00:00
free = false;
Free();
}
/*
========================
idSWFBitStream::operator=
========================
*/
idSWFBitStream& idSWFBitStream::operator=( idSWFBitStream& other )
{
2012-11-26 18:58:24 +00:00
Free();
free = other.free;
startp = other.startp;
readp = other.readp;
endp = other.endp;
currentBit = other.currentBit;
currentByte = other.currentByte;
if( other.free )
{
2012-11-26 18:58:24 +00:00
// this is actually quite dangerous, but we need to do this
// because these things are copied around inside idList
other.free = false;
}
return *this;
}
/*
========================
idSWFBitStream::Free
========================
*/
void idSWFBitStream::Free()
{
if( free )
{
Mem_Free( ( void* )startp );
2012-11-26 18:58:24 +00:00
}
free = false;
startp = NULL;
endp = NULL;
readp = NULL;
ResetBits();
}
/*
========================
idSWFBitStream::Load
========================
*/
void idSWFBitStream::Load( const byte* data, uint32 len, bool copy )
{
2012-11-26 18:58:24 +00:00
Free();
if( copy )
{
2012-11-26 18:58:24 +00:00
free = true;
startp = ( const byte* )Mem_Alloc( len, TAG_SWF );
memcpy( ( byte* )startp, data, len );
}
else
{
2012-11-26 18:58:24 +00:00
free = false;
startp = data;
}
endp = startp + len;
readp = startp;
2012-11-26 18:58:24 +00:00
ResetBits();
}
/*
========================
idSWFBitStream::ReadEncodedU32
========================
*/
uint32 idSWFBitStream::ReadEncodedU32()
{
2012-11-26 18:58:24 +00:00
uint32 result = 0;
for( int i = 0; i < 5; i++ )
{
2012-11-26 18:58:24 +00:00
byte b = ReadU8();
result |= ( b & 0x7F ) << ( 7 * i );
if( ( b & 0x80 ) == 0 )
{
2012-11-26 18:58:24 +00:00
return result;
}
}
return result;
}
/*
========================
idSWFBitStream::ReadData
========================
*/
const byte* idSWFBitStream::ReadData( int size )
{
2012-11-26 18:58:24 +00:00
assert( readp >= startp && readp <= endp );
ResetBits();
if( readp + size > endp )
{
2012-11-26 18:58:24 +00:00
// buffer overrun
assert( false );
readp = endp;
return startp;
}
const byte* buffer = readp;
2012-11-26 18:58:24 +00:00
readp += size;
assert( readp >= startp && readp <= endp );
return buffer;
}
/*
========================
idSWFBitStream::ReadInternalU
========================
*/
ID_FORCE_INLINE unsigned int idSWFBitStream::ReadInternalU( uint64& regCurrentBit, uint64& regCurrentByte, unsigned int numBits )
{
2012-11-26 18:58:24 +00:00
assert( numBits <= 32 );
2012-11-26 18:58:24 +00:00
// read bits with only one microcoded shift instruction (shift with variable) on the consoles
// this routine never reads more than 7 bits beyond the requested number of bits from the stream
// such that calling ResetBits() never discards more than 7 bits and aligns with the next byte
uint64 numExtraBytes = ( numBits - regCurrentBit + 7 ) >> 3;
regCurrentBit = regCurrentBit + ( numExtraBytes << 3 ) - numBits;
for( int i = 0; i < numExtraBytes; i++ )
{
2012-11-26 18:58:24 +00:00
regCurrentByte = ( regCurrentByte << 8 ) | readp[i];
}
readp += numExtraBytes;
return ( unsigned int )( ( regCurrentByte >> regCurrentBit ) & maskForNumBits[numBits] );
2012-11-26 18:58:24 +00:00
}
/*
========================
idSWFBitStream::ReadInternalS
========================
*/
ID_FORCE_INLINE int idSWFBitStream::ReadInternalS( uint64& regCurrentBit, uint64& regCurrentByte, unsigned int numBits )
{
int i = ( int )ReadInternalU( regCurrentBit, regCurrentByte, numBits );
2012-11-26 18:58:24 +00:00
// sign extend without microcoded shift instrunction (shift with variable) on the consoles
int s = signForNumBits[numBits];
return ( ( i + s ) ^ s );
}
/*
========================
idSWFBitStream::ReadU
========================
*/
unsigned int idSWFBitStream::ReadU( unsigned int numBits )
{
2012-11-26 18:58:24 +00:00
return ReadInternalU( currentBit, currentByte, numBits );
}
/*
========================
idSWFBitStream::ReadS
========================
*/
int idSWFBitStream::ReadS( unsigned int numBits )
{
2012-11-26 18:58:24 +00:00
return ReadInternalS( currentBit, currentByte, numBits );
}
/*
========================
idSWFBitStream::ReadRect
========================
*/
void idSWFBitStream::ReadRect( swfRect_t& rect )
{
2012-11-26 18:58:24 +00:00
uint64 regCurrentBit = 0;
uint64 regCurrentByte = 0;
2012-11-26 18:58:24 +00:00
int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 5 );
2012-11-26 18:58:24 +00:00
int tl_x = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
int br_x = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
int tl_y = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
int br_y = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
2012-11-26 18:58:24 +00:00
rect.tl.x = SWFTWIP( tl_x );
rect.br.x = SWFTWIP( br_x );
rect.tl.y = SWFTWIP( tl_y );
rect.br.y = SWFTWIP( br_y );
2012-11-26 18:58:24 +00:00
currentBit = regCurrentBit;
currentByte = regCurrentByte;
}
/*
========================
idSWFBitStream::ReadMatrix
========================
*/
void idSWFBitStream::ReadMatrix( swfMatrix_t& matrix )
{
2012-11-26 18:58:24 +00:00
uint64 regCurrentBit = 0;
uint64 regCurrentByte = 0;
2012-11-26 18:58:24 +00:00
unsigned int hasScale = ReadInternalU( regCurrentBit, regCurrentByte, 1 );
2012-11-26 18:58:24 +00:00
int xx;
int yy;
if( !hasScale )
{
2012-11-26 18:58:24 +00:00
xx = 65536;
yy = 65536;
}
else
{
2012-11-26 18:58:24 +00:00
int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 5 );
xx = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
yy = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
}
2012-11-26 18:58:24 +00:00
unsigned int hasRotate = ReadInternalU( regCurrentBit, regCurrentByte, 1 );
2012-11-26 18:58:24 +00:00
int yx;
int xy;
if( !hasRotate )
{
2012-11-26 18:58:24 +00:00
yx = 0;
xy = 0;
}
else
{
2012-11-26 18:58:24 +00:00
int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 5 );
yx = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
xy = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
}
2012-11-26 18:58:24 +00:00
int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 5 );
int tx = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
int ty = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
2012-11-26 18:58:24 +00:00
currentBit = regCurrentBit;
currentByte = regCurrentByte;
2012-11-26 18:58:24 +00:00
matrix.xx = SWFFIXED16( xx );
matrix.yy = SWFFIXED16( yy );
matrix.yx = SWFFIXED16( yx );
matrix.xy = SWFFIXED16( xy );
matrix.tx = SWFTWIP( tx );
matrix.ty = SWFTWIP( ty );
2012-11-26 18:58:24 +00:00
}
/*
========================
idSWFBitStream::ReadColorXFormRGBA
========================
*/
void idSWFBitStream::ReadColorXFormRGBA( swfColorXform_t& cxf )
{
2012-11-26 18:58:24 +00:00
uint64 regCurrentBit = 0;
uint64 regCurrentByte = 0;
2012-11-26 18:58:24 +00:00
unsigned int hasAddTerms = ReadInternalU( regCurrentBit, regCurrentByte, 1 );
unsigned int hasMulTerms = ReadInternalU( regCurrentBit, regCurrentByte, 1 );
int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 4 );
union
{
int i[4];
} m;
union
{
int i[4];
} a;
if( !hasMulTerms )
{
2012-11-26 18:58:24 +00:00
m.i[0] = 256;
m.i[1] = 256;
m.i[2] = 256;
m.i[3] = 256;
}
else
{
2012-11-26 18:58:24 +00:00
m.i[0] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
m.i[1] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
m.i[2] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
m.i[3] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
}
if( !hasAddTerms )
{
2012-11-26 18:58:24 +00:00
a.i[0] = 0;
a.i[1] = 0;
a.i[2] = 0;
a.i[3] = 0;
}
else
{
2012-11-26 18:58:24 +00:00
a.i[0] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
a.i[1] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
a.i[2] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
a.i[3] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
}
2012-11-26 18:58:24 +00:00
currentBit = regCurrentBit;
currentByte = regCurrentByte;
for( int i = 0; i < 4; i++ )
{
2012-11-26 18:58:24 +00:00
cxf.mul[i] = SWFFIXED8( m.i[i] );
cxf.add[i] = SWFFIXED8( a.i[i] );
}
}
/*
========================
idSWFBitStream::ReadString
========================
*/
const char* idSWFBitStream::ReadString()
{
return ( const char* )ReadData( idStr::Length( ( const char* )readp ) + 1 );
2012-11-26 18:58:24 +00:00
}
/*
========================
idSWFBitStream::ReadColorRGB
========================
*/
void idSWFBitStream::ReadColorRGB( swfColorRGB_t& color )
{
2012-11-26 18:58:24 +00:00
ResetBits();
color.r = *readp++;
color.g = *readp++;
color.b = *readp++;
}
/*
========================
idSWFBitStream::ReadColorRGBA
========================
*/
void idSWFBitStream::ReadColorRGBA( swfColorRGBA_t& color )
{
2012-11-26 18:58:24 +00:00
ResetBits();
color.r = *readp++;
color.g = *readp++;
color.b = *readp++;
color.a = *readp++;
}
/*
========================
idSWFBitStream::ReadGradient
========================
*/
void idSWFBitStream::ReadGradient( swfGradient_t& grad, bool rgba )
{
2012-11-26 18:58:24 +00:00
grad.numGradients = ReadU8() & 0xF; // the top 4 bits control spread and interpolation mode, but we ignore them
for( int i = 0; i < grad.numGradients; i++ )
{
2012-11-26 18:58:24 +00:00
grad.gradientRecords[i].startRatio = ReadU8();
if( rgba )
{
2012-11-26 18:58:24 +00:00
ReadColorRGBA( grad.gradientRecords[i].startColor );
}
else
{
2012-11-26 18:58:24 +00:00
ReadColorRGB( grad.gradientRecords[i].startColor );
}
grad.gradientRecords[i].endRatio = grad.gradientRecords[i].startRatio;
grad.gradientRecords[i].endColor = grad.gradientRecords[i].startColor;
}
}
/*
========================
idSWFBitStream::ReadMorphGradient
========================
*/
void idSWFBitStream::ReadMorphGradient( swfGradient_t& grad )
{
2012-11-26 18:58:24 +00:00
grad.numGradients = ReadU8() & 0xF; // the top 4 bits control spread and interpolation mode, but we ignore them
for( int i = 0; i < grad.numGradients; i++ )
{
2012-11-26 18:58:24 +00:00
grad.gradientRecords[i].startRatio = ReadU8();
ReadColorRGBA( grad.gradientRecords[i].startColor );
grad.gradientRecords[i].endRatio = ReadU8();
ReadColorRGBA( grad.gradientRecords[i].endColor );
}
}