SWF_Bitstream.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #define NBM( x ) (int32)( ( 1LL << x ) - 1 )
  23. int maskForNumBits[33] = { NBM( 0x00 ), NBM( 0x01 ), NBM( 0x02 ), NBM( 0x03 ),
  24. NBM( 0x04 ), NBM( 0x05 ), NBM( 0x06 ), NBM( 0x07 ),
  25. NBM( 0x08 ), NBM( 0x09 ), NBM( 0x0A ), NBM( 0x0B ),
  26. NBM( 0x0C ), NBM( 0x0D ), NBM( 0x0E ), NBM( 0x0F ),
  27. NBM( 0x10 ), NBM( 0x11 ), NBM( 0x12 ), NBM( 0x13 ),
  28. NBM( 0x14 ), NBM( 0x15 ), NBM( 0x16 ), NBM( 0x17 ),
  29. NBM( 0x18 ), NBM( 0x19 ), NBM( 0x1A ), NBM( 0x1B ),
  30. NBM( 0x1C ), NBM( 0x1D ), NBM( 0x1E ), NBM( 0x1F ), -1 };
  31. #define NBS( x ) (int32)( (-1) << ( x - 1 ) )
  32. int signForNumBits[33] = { NBS( 0x01 ), NBS( 0x01 ), NBS( 0x02 ), NBS( 0x03 ),
  33. NBS( 0x04 ), NBS( 0x05 ), NBS( 0x06 ), NBS( 0x07 ),
  34. NBS( 0x08 ), NBS( 0x09 ), NBS( 0x0A ), NBS( 0x0B ),
  35. NBS( 0x0C ), NBS( 0x0D ), NBS( 0x0E ), NBS( 0x0F ),
  36. NBS( 0x10 ), NBS( 0x11 ), NBS( 0x12 ), NBS( 0x13 ),
  37. NBS( 0x14 ), NBS( 0x15 ), NBS( 0x16 ), NBS( 0x17 ),
  38. NBS( 0x18 ), NBS( 0x19 ), NBS( 0x1A ), NBS( 0x1B ),
  39. NBS( 0x1C ), NBS( 0x1D ), NBS( 0x1E ), NBS( 0x1F ), NBS( 0x20 ) };
  40. #define ID_FORCEINLINE __forceinline
  41. /*
  42. ========================
  43. idSWFBitStream::idSWFBitStream
  44. ========================
  45. */
  46. idSWFBitStream::idSWFBitStream() {
  47. free = false;
  48. Free();
  49. }
  50. /*
  51. ========================
  52. idSWFBitStream::operator=
  53. ========================
  54. */
  55. idSWFBitStream & idSWFBitStream::operator=( idSWFBitStream & other ) {
  56. Free();
  57. free = other.free;
  58. startp = other.startp;
  59. readp = other.readp;
  60. endp = other.endp;
  61. currentBit = other.currentBit;
  62. currentByte = other.currentByte;
  63. if ( other.free ) {
  64. // this is actually quite dangerous, but we need to do this
  65. // because these things are copied around inside idList
  66. other.free = false;
  67. }
  68. return *this;
  69. }
  70. /*
  71. ========================
  72. idSWFBitStream::Free
  73. ========================
  74. */
  75. void idSWFBitStream::Free() {
  76. if ( free ) {
  77. Mem_Free( (void *)startp );
  78. }
  79. free = false;
  80. startp = NULL;
  81. endp = NULL;
  82. readp = NULL;
  83. ResetBits();
  84. }
  85. /*
  86. ========================
  87. idSWFBitStream::Load
  88. ========================
  89. */
  90. void idSWFBitStream::Load( const byte * data, uint32 len, bool copy ) {
  91. Free();
  92. if ( copy ) {
  93. free = true;
  94. startp = (const byte *)Mem_Alloc( len, TAG_SWF );
  95. memcpy( (byte *)startp, data, len );
  96. } else {
  97. free = false;
  98. startp = data;
  99. }
  100. endp = startp + len;
  101. readp = startp;
  102. ResetBits();
  103. }
  104. /*
  105. ========================
  106. idSWFBitStream::ReadEncodedU32
  107. ========================
  108. */
  109. uint32 idSWFBitStream::ReadEncodedU32() {
  110. uint32 result = 0;
  111. for ( int i = 0; i < 5; i++ ) {
  112. byte b = ReadU8();
  113. result |= ( b & 0x7F ) << ( 7 * i );
  114. if ( ( b & 0x80 ) == 0 ) {
  115. return result;
  116. }
  117. }
  118. return result;
  119. }
  120. /*
  121. ========================
  122. idSWFBitStream::ReadData
  123. ========================
  124. */
  125. const byte * idSWFBitStream::ReadData( int size ) {
  126. assert( readp >= startp && readp <= endp );
  127. ResetBits();
  128. if ( readp + size > endp ) {
  129. // buffer overrun
  130. assert( false );
  131. readp = endp;
  132. return startp;
  133. }
  134. const byte * buffer = readp;
  135. readp += size;
  136. assert( readp >= startp && readp <= endp );
  137. return buffer;
  138. }
  139. /*
  140. ========================
  141. idSWFBitStream::ReadInternalU
  142. ========================
  143. */
  144. ID_FORCEINLINE unsigned int idSWFBitStream::ReadInternalU( uint64 & regCurrentBit, uint64 & regCurrentByte, unsigned int numBits ) {
  145. assert( numBits <= 32 );
  146. // read bits with only one microcoded shift instruction (shift with variable) on the consoles
  147. // this routine never reads more than 7 bits beyond the requested number of bits from the stream
  148. // such that calling ResetBits() never discards more than 7 bits and aligns with the next byte
  149. uint64 numExtraBytes = ( numBits - regCurrentBit + 7 ) >> 3;
  150. regCurrentBit = regCurrentBit + ( numExtraBytes << 3 ) - numBits;
  151. for ( int i = 0; i < numExtraBytes; i++ ) {
  152. regCurrentByte = ( regCurrentByte << 8 ) | readp[i];
  153. }
  154. readp += numExtraBytes;
  155. return (unsigned int) ( ( regCurrentByte >> regCurrentBit ) & maskForNumBits[numBits] );
  156. }
  157. /*
  158. ========================
  159. idSWFBitStream::ReadInternalS
  160. ========================
  161. */
  162. ID_FORCEINLINE int idSWFBitStream::ReadInternalS( uint64 & regCurrentBit, uint64 & regCurrentByte, unsigned int numBits ) {
  163. int i = (int)ReadInternalU( regCurrentBit, regCurrentByte, numBits );
  164. // sign extend without microcoded shift instrunction (shift with variable) on the consoles
  165. int s = signForNumBits[numBits];
  166. return ( ( i + s ) ^ s );
  167. }
  168. /*
  169. ========================
  170. idSWFBitStream::ReadU
  171. ========================
  172. */
  173. unsigned int idSWFBitStream::ReadU( unsigned int numBits ) {
  174. return ReadInternalU( currentBit, currentByte, numBits );
  175. }
  176. /*
  177. ========================
  178. idSWFBitStream::ReadS
  179. ========================
  180. */
  181. int idSWFBitStream::ReadS( unsigned int numBits ) {
  182. return ReadInternalS( currentBit, currentByte, numBits );
  183. }
  184. /*
  185. ========================
  186. idSWFBitStream::ReadRect
  187. ========================
  188. */
  189. void idSWFBitStream::ReadRect( swfRect_t & rect ) {
  190. uint64 regCurrentBit = 0;
  191. uint64 regCurrentByte = 0;
  192. int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 5 );
  193. int tl_x = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  194. int br_x = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  195. int tl_y = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  196. int br_y = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  197. rect.tl.x = SWFTWIP( tl_x );
  198. rect.br.x = SWFTWIP( br_x );
  199. rect.tl.y = SWFTWIP( tl_y );
  200. rect.br.y = SWFTWIP( br_y );
  201. currentBit = regCurrentBit;
  202. currentByte = regCurrentByte;
  203. }
  204. /*
  205. ========================
  206. idSWFBitStream::ReadMatrix
  207. ========================
  208. */
  209. void idSWFBitStream::ReadMatrix( swfMatrix_t & matrix ) {
  210. uint64 regCurrentBit = 0;
  211. uint64 regCurrentByte = 0;
  212. unsigned int hasScale = ReadInternalU( regCurrentBit, regCurrentByte, 1 );
  213. int xx;
  214. int yy;
  215. if ( !hasScale ) {
  216. xx = 65536;
  217. yy = 65536;
  218. } else {
  219. int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 5 );
  220. xx = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  221. yy = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  222. }
  223. unsigned int hasRotate = ReadInternalU( regCurrentBit, regCurrentByte, 1 );
  224. int yx;
  225. int xy;
  226. if ( !hasRotate ) {
  227. yx = 0;
  228. xy = 0;
  229. } else {
  230. int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 5 );
  231. yx = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  232. xy = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  233. }
  234. int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 5 );
  235. int tx = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  236. int ty = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  237. currentBit = regCurrentBit;
  238. currentByte = regCurrentByte;
  239. matrix.xx = SWFFIXED16( xx );
  240. matrix.yy = SWFFIXED16( yy );
  241. matrix.yx = SWFFIXED16( yx );
  242. matrix.xy = SWFFIXED16( xy );
  243. matrix.tx = SWFTWIP( tx );
  244. matrix.ty = SWFTWIP( ty );
  245. }
  246. /*
  247. ========================
  248. idSWFBitStream::ReadColorXFormRGBA
  249. ========================
  250. */
  251. void idSWFBitStream::ReadColorXFormRGBA( swfColorXform_t & cxf ) {
  252. uint64 regCurrentBit = 0;
  253. uint64 regCurrentByte = 0;
  254. unsigned int hasAddTerms = ReadInternalU( regCurrentBit, regCurrentByte, 1 );
  255. unsigned int hasMulTerms = ReadInternalU( regCurrentBit, regCurrentByte, 1 );
  256. int nBits = ReadInternalU( regCurrentBit, regCurrentByte, 4 );
  257. union { int i[4]; } m;
  258. union { int i[4]; } a;
  259. if ( !hasMulTerms ) {
  260. m.i[0] = 256;
  261. m.i[1] = 256;
  262. m.i[2] = 256;
  263. m.i[3] = 256;
  264. } else {
  265. m.i[0] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  266. m.i[1] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  267. m.i[2] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  268. m.i[3] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  269. }
  270. if ( !hasAddTerms ) {
  271. a.i[0] = 0;
  272. a.i[1] = 0;
  273. a.i[2] = 0;
  274. a.i[3] = 0;
  275. } else {
  276. a.i[0] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  277. a.i[1] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  278. a.i[2] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  279. a.i[3] = ReadInternalS( regCurrentBit, regCurrentByte, nBits );
  280. }
  281. currentBit = regCurrentBit;
  282. currentByte = regCurrentByte;
  283. for ( int i = 0; i < 4; i++ ) {
  284. cxf.mul[i] = SWFFIXED8( m.i[i] );
  285. cxf.add[i] = SWFFIXED8( a.i[i] );
  286. }
  287. }
  288. /*
  289. ========================
  290. idSWFBitStream::ReadString
  291. ========================
  292. */
  293. const char * idSWFBitStream::ReadString() {
  294. return (const char *)ReadData( idStr::Length( (const char *)readp ) + 1 );
  295. }
  296. /*
  297. ========================
  298. idSWFBitStream::ReadColorRGB
  299. ========================
  300. */
  301. void idSWFBitStream::ReadColorRGB( swfColorRGB_t & color ) {
  302. ResetBits();
  303. color.r = *readp++;
  304. color.g = *readp++;
  305. color.b = *readp++;
  306. }
  307. /*
  308. ========================
  309. idSWFBitStream::ReadColorRGBA
  310. ========================
  311. */
  312. void idSWFBitStream::ReadColorRGBA( swfColorRGBA_t & color ) {
  313. ResetBits();
  314. color.r = *readp++;
  315. color.g = *readp++;
  316. color.b = *readp++;
  317. color.a = *readp++;
  318. }
  319. /*
  320. ========================
  321. idSWFBitStream::ReadGradient
  322. ========================
  323. */
  324. void idSWFBitStream::ReadGradient( swfGradient_t & grad, bool rgba ) {
  325. grad.numGradients = ReadU8() & 0xF; // the top 4 bits control spread and interpolation mode, but we ignore them
  326. for ( int i = 0; i < grad.numGradients; i++ ) {
  327. grad.gradientRecords[i].startRatio = ReadU8();
  328. if ( rgba ) {
  329. ReadColorRGBA( grad.gradientRecords[i].startColor );
  330. } else {
  331. ReadColorRGB( grad.gradientRecords[i].startColor );
  332. }
  333. grad.gradientRecords[i].endRatio = grad.gradientRecords[i].startRatio;
  334. grad.gradientRecords[i].endColor = grad.gradientRecords[i].startColor;
  335. }
  336. }
  337. /*
  338. ========================
  339. idSWFBitStream::ReadMorphGradient
  340. ========================
  341. */
  342. void idSWFBitStream::ReadMorphGradient( swfGradient_t & grad ) {
  343. grad.numGradients = ReadU8() & 0xF; // the top 4 bits control spread and interpolation mode, but we ignore them
  344. for ( int i = 0; i < grad.numGradients; i++ ) {
  345. grad.gradientRecords[i].startRatio = ReadU8();
  346. ReadColorRGBA( grad.gradientRecords[i].startColor );
  347. grad.gradientRecords[i].endRatio = ReadU8();
  348. ReadColorRGBA( grad.gradientRecords[i].endColor );
  349. }
  350. }