o3dgcDynamicVectorEncoder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "o3dgcDVEncodeParams.h"
  20. #include "o3dgcDynamicVectorEncoder.h"
  21. #include "o3dgcArithmeticCodec.h"
  22. #include "o3dgcBinaryStream.h"
  23. //#define DEBUG_VERBOSE
  24. namespace o3dgc
  25. {
  26. #ifdef DEBUG_VERBOSE
  27. FILE * g_fileDebugDVEnc = NULL;
  28. #endif //DEBUG_VERBOSE
  29. inline O3DGCErrorCode Update(long * const data, const long size)
  30. {
  31. assert(size > 1);
  32. const long size1 = size - 1;
  33. long p = 2;
  34. data[0] += data[1] >> 1;
  35. while(p < size1)
  36. {
  37. data[p] += (data[p-1] + data[p+1] + 2) >> 2;
  38. p += 2;
  39. }
  40. if ( p == size1)
  41. {
  42. data[p] += data[p-1]>>1;
  43. }
  44. return O3DGC_OK;
  45. }
  46. inline O3DGCErrorCode Predict(long * const data, const long size)
  47. {
  48. assert(size > 1);
  49. const long size1 = size - 1;
  50. long p = 1;
  51. while(p < size1)
  52. {
  53. data[p] -= (data[p-1] + data[p+1] + 1) >> 1;
  54. p += 2;
  55. }
  56. if ( p == size1)
  57. {
  58. data[p] -= data[p-1];
  59. }
  60. return O3DGC_OK;
  61. }
  62. inline O3DGCErrorCode Split(long * const data, const long size)
  63. {
  64. assert(size > 1);
  65. long a = 1;
  66. long b = size-1;
  67. while (a < b)
  68. {
  69. for (long i = a; i < b; i += 2)
  70. {
  71. swap(data[i], data[i+1]);
  72. }
  73. ++a;
  74. --b;
  75. }
  76. return O3DGC_OK;
  77. }
  78. inline O3DGCErrorCode Transform(long * const data, const unsigned long size)
  79. {
  80. unsigned long n = size;
  81. while(n > 1)
  82. {
  83. Predict(data, n);
  84. Update (data, n);
  85. Split(data, n);
  86. n = (n >> 1) + (n & 1);
  87. }
  88. return O3DGC_OK;
  89. }
  90. DynamicVectorEncoder::DynamicVectorEncoder(void)
  91. {
  92. m_maxNumVectors = 0;
  93. m_numVectors = 0;
  94. m_dimVectors = 0;
  95. m_quantVectors = 0;
  96. m_sizeBufferAC = 0;
  97. m_bufferAC = 0;
  98. m_posSize = 0;
  99. m_streamType = O3DGC_STREAM_TYPE_UNKOWN;
  100. }
  101. DynamicVectorEncoder::~DynamicVectorEncoder()
  102. {
  103. delete [] m_quantVectors;
  104. delete [] m_bufferAC;
  105. }
  106. O3DGCErrorCode DynamicVectorEncoder::Encode(const DVEncodeParams & params,
  107. const DynamicVector & dynamicVector,
  108. BinaryStream & bstream)
  109. {
  110. assert(params.GetQuantBits() > 0);
  111. assert(dynamicVector.GetNVector() > 0);
  112. assert(dynamicVector.GetDimVector() > 0);
  113. assert(dynamicVector.GetStride() >= dynamicVector.GetDimVector());
  114. assert(dynamicVector.GetVectors() && dynamicVector.GetMin() && dynamicVector.GetMax());
  115. assert(m_streamType != O3DGC_STREAM_TYPE_UNKOWN);
  116. // Encode header
  117. unsigned long start = bstream.GetSize();
  118. EncodeHeader(params, dynamicVector, bstream);
  119. // Encode payload
  120. EncodePayload(params, dynamicVector, bstream);
  121. bstream.WriteUInt32(m_posSize, bstream.GetSize() - start, m_streamType);
  122. return O3DGC_OK;
  123. }
  124. O3DGCErrorCode DynamicVectorEncoder::EncodeHeader(const DVEncodeParams & params,
  125. const DynamicVector & dynamicVector,
  126. BinaryStream & bstream)
  127. {
  128. m_streamType = params.GetStreamType();
  129. bstream.WriteUInt32(O3DGC_DV_START_CODE, m_streamType);
  130. m_posSize = bstream.GetSize();
  131. bstream.WriteUInt32(0, m_streamType); // to be filled later
  132. bstream.WriteUChar((unsigned char) params.GetEncodeMode(), m_streamType);
  133. bstream.WriteUInt32(dynamicVector.GetNVector() , m_streamType);
  134. if (dynamicVector.GetNVector() > 0)
  135. {
  136. bstream.WriteUInt32(dynamicVector.GetDimVector(), m_streamType);
  137. bstream.WriteUChar ((unsigned char) params.GetQuantBits(), m_streamType);
  138. }
  139. return O3DGC_OK;
  140. }
  141. O3DGCErrorCode DynamicVectorEncoder::EncodeAC(unsigned long num,
  142. unsigned long dim,
  143. unsigned long M,
  144. unsigned long & encodedBytes)
  145. {
  146. Arithmetic_Codec ace;
  147. Static_Bit_Model bModel0;
  148. Adaptive_Bit_Model bModel1;
  149. Adaptive_Data_Model mModelValues(M+2);
  150. const unsigned int NMAX = num * dim * 8 + 100;
  151. if ( m_sizeBufferAC < NMAX )
  152. {
  153. delete [] m_bufferAC;
  154. m_sizeBufferAC = NMAX;
  155. m_bufferAC = new unsigned char [m_sizeBufferAC];
  156. }
  157. ace.set_buffer(NMAX, m_bufferAC);
  158. ace.start_encoder();
  159. ace.ExpGolombEncode(0, 0, bModel0, bModel1);
  160. ace.ExpGolombEncode(M, 0, bModel0, bModel1);
  161. for(unsigned long v = 0; v < num; ++v)
  162. {
  163. for(unsigned long d = 0; d < dim; ++d)
  164. {
  165. EncodeIntACEGC(m_quantVectors[d * num + v], ace, mModelValues, bModel0, bModel1, M);
  166. }
  167. }
  168. encodedBytes = ace.stop_encoder();
  169. return O3DGC_OK;
  170. }
  171. O3DGCErrorCode DynamicVectorEncoder::EncodePayload(const DVEncodeParams & params,
  172. const DynamicVector & dynamicVector,
  173. BinaryStream & bstream)
  174. {
  175. #ifdef DEBUG_VERBOSE
  176. g_fileDebugDVEnc = fopen("dv_enc.txt", "w");
  177. #endif //DEBUG_VERBOSE
  178. unsigned long start = bstream.GetSize();
  179. const unsigned long dim = dynamicVector.GetDimVector();
  180. const unsigned long num = dynamicVector.GetNVector();
  181. bstream.WriteUInt32(0, m_streamType);
  182. for(unsigned long j=0 ; j<dynamicVector.GetDimVector() ; ++j)
  183. {
  184. bstream.WriteFloat32((float) dynamicVector.GetMin(j), m_streamType);
  185. bstream.WriteFloat32((float) dynamicVector.GetMax(j), m_streamType);
  186. }
  187. Quantize(dynamicVector.GetVectors(),
  188. num,
  189. dim,
  190. dynamicVector.GetStride(),
  191. dynamicVector.GetMin(),
  192. dynamicVector.GetMax(),
  193. params.GetQuantBits());
  194. for(unsigned long d = 0; d < dim; ++d)
  195. {
  196. Transform(m_quantVectors + d * num, num);
  197. }
  198. #ifdef DEBUG_VERBOSE
  199. printf("IntArray (%i, %i)\n", num, dim);
  200. fprintf(g_fileDebugDVEnc, "IntArray (%i, %i)\n", num, dim);
  201. for(unsigned long v = 0; v < num; ++v)
  202. {
  203. for(unsigned long d = 0; d < dim; ++d)
  204. {
  205. printf("%i\t %i \t %i\n", d * num + v, m_quantVectors[d * num + v], IntToUInt(m_quantVectors[d * num + v]));
  206. fprintf(g_fileDebugDVEnc, "%i\t %i \t %i\n", d * num + v, m_quantVectors[d * num + v], IntToUInt(m_quantVectors[d * num + v]));
  207. }
  208. }
  209. #endif //DEBUG_VERBOSE
  210. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  211. {
  212. for(unsigned long v = 0; v < num; ++v)
  213. {
  214. for(unsigned long d = 0; d < dim; ++d)
  215. {
  216. bstream.WriteIntASCII(m_quantVectors[d * num + v]);
  217. }
  218. }
  219. }
  220. else
  221. {
  222. unsigned long encodedBytes = 0;
  223. unsigned long bestEncodedBytes = O3DGC_MAX_ULONG;
  224. unsigned long M = 1;
  225. unsigned long bestM = 1;
  226. while (M < 1024)
  227. {
  228. EncodeAC(num, dim, M, encodedBytes);
  229. if (encodedBytes > bestEncodedBytes)
  230. {
  231. break;
  232. }
  233. bestM = M;
  234. bestEncodedBytes = encodedBytes;
  235. M *= 2;
  236. }
  237. EncodeAC(num, dim, bestM, encodedBytes);
  238. for(unsigned long i = 0; i < encodedBytes; ++i)
  239. {
  240. bstream.WriteUChar8Bin(m_bufferAC[i]);
  241. }
  242. }
  243. bstream.WriteUInt32(start, bstream.GetSize() - start, m_streamType);
  244. #ifdef DEBUG_VERBOSE
  245. fclose(g_fileDebugDVEnc);
  246. #endif //DEBUG_VERBOSE
  247. return O3DGC_OK;
  248. }
  249. O3DGCErrorCode DynamicVectorEncoder::Quantize(const Real * const floatArray,
  250. unsigned long numFloatArray,
  251. unsigned long dimFloatArray,
  252. unsigned long stride,
  253. const Real * const minFloatArray,
  254. const Real * const maxFloatArray,
  255. unsigned long nQBits)
  256. {
  257. const unsigned long size = numFloatArray * dimFloatArray;
  258. Real r;
  259. if (m_maxNumVectors < size)
  260. {
  261. delete [] m_quantVectors;
  262. m_maxNumVectors = size;
  263. m_quantVectors = new long [m_maxNumVectors];
  264. }
  265. Real delta;
  266. for(unsigned long d = 0; d < dimFloatArray; ++d)
  267. {
  268. r = maxFloatArray[d] - minFloatArray[d];
  269. if (r > 0.0f)
  270. {
  271. delta = (float)((1 << nQBits) - 1) / r;
  272. }
  273. else
  274. {
  275. delta = 1.0f;
  276. }
  277. for(unsigned long v = 0; v < numFloatArray; ++v)
  278. {
  279. m_quantVectors[v + d * numFloatArray] = (long)((floatArray[v * stride + d]-minFloatArray[d]) * delta + 0.5f);
  280. }
  281. }
  282. return O3DGC_OK;
  283. }
  284. }