19# pragma warning(disable : 4127)
31#define FSE_STATIC_LINKING_ONLY
33#define HUF_STATIC_LINKING_ONLY
41#define HUF_isError ERR_isError
42#define HUF_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)
62#define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6
67 BYTE*
const oend = ostart + dstSize;
69 unsigned maxSymbolValue = HUF_TABLELOG_MAX;
75 unsigned count[HUF_TABLELOG_MAX+1];
79 if (wtSize <= 1)
return 0;
98 if (cSize == 0)
return 0;
102 return (
size_t)(
op-ostart);
115 const HUF_CElt* CTable,
unsigned maxSymbolValue,
unsigned huffLog)
117 BYTE bitsToWeight[HUF_TABLELOG_MAX + 1];
118 BYTE huffWeight[HUF_SYMBOLVALUE_MAX];
123 if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
return ERROR(maxSymbolValue_tooLarge);
127 for (
n=1;
n<huffLog+1;
n++)
128 bitsToWeight[
n] = (
BYTE)(huffLog + 1 -
n);
129 for (
n=0;
n<maxSymbolValue;
n++)
130 huffWeight[
n] = bitsToWeight[CTable[
n].nbBits];
134 if ((hSize>1) & (hSize < maxSymbolValue/2)) {
140 if (maxSymbolValue > (256-128))
return ERROR(GENERIC);
141 if (((maxSymbolValue+1)/2) + 1 > maxDstSize)
return ERROR(dstSize_tooSmall);
142 op[0] = (
BYTE)(128 + (maxSymbolValue-1));
143 huffWeight[maxSymbolValue] = 0;
144 for (
n=0;
n<maxSymbolValue;
n+=2)
145 op[(
n/2)+1] = (
BYTE)((huffWeight[
n] << 4) + huffWeight[
n+1]);
146 return ((maxSymbolValue+1)/2) + 1;
150size_t HUF_readCTable (HUF_CElt* CTable,
unsigned* maxSymbolValuePtr,
const void*
src,
size_t srcSize,
unsigned* hasZeroWeights)
152 BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1];
153 U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1];
161 if (tableLog > HUF_TABLELOG_MAX)
return ERROR(tableLog_tooLarge);
162 if (nbSymbols > *maxSymbolValuePtr+1)
return ERROR(maxSymbolValue_tooSmall);
165 {
U32 n, nextRankStart = 0;
166 for (
n=1;
n<=tableLog;
n++) {
168 nextRankStart += (rankVal[
n] << (
n-1));
174 {
U32 n;
for (
n=0;
n<nbSymbols;
n++) {
175 const U32 w = huffWeight[
n];
176 *hasZeroWeights |= (
w == 0);
177 CTable[
n].nbBits = (
BYTE)(tableLog + 1 -
w) & -(
w != 0);
181 {
U16 nbPerRank[HUF_TABLELOG_MAX+2] = {0};
182 U16 valPerRank[HUF_TABLELOG_MAX+2] = {0};
183 {
U32 n;
for (
n=0;
n<nbSymbols;
n++) nbPerRank[CTable[
n].nbBits]++; }
185 valPerRank[tableLog+1] = 0;
187 U32 n;
for (
n=tableLog;
n>0;
n--) {
193 {
U32 n;
for (
n=0;
n<nbSymbols;
n++) CTable[
n].
val = valPerRank[CTable[
n].nbBits]++; }
196 *maxSymbolValuePtr = nbSymbols - 1;
202 const HUF_CElt*
table = (
const HUF_CElt*)symbolTable;
203 assert(symbolValue <= HUF_SYMBOLVALUE_MAX);
204 return table[symbolValue].nbBits;
217 const U32 largestBits = huffNode[lastNonNull].
nbBits;
218 if (largestBits <= maxNbBits)
return largestBits;
222 const U32 baseCost = 1 << (largestBits - maxNbBits);
223 int n = (
int)lastNonNull;
225 while (huffNode[
n].nbBits > maxNbBits) {
226 totalCost += baseCost - (1 << (largestBits - huffNode[
n].
nbBits));
230 while (huffNode[
n].nbBits == maxNbBits)
n--;
233 totalCost >>= (largestBits - maxNbBits);
236 {
U32 const noSymbol = 0xF0F0F0F0;
237 U32 rankLast[HUF_TABLELOG_MAX+2];
240 memset(rankLast, 0xF0,
sizeof(rankLast));
241 {
U32 currentNbBits = maxNbBits;
244 if (huffNode[
pos].nbBits >= currentNbBits)
continue;
246 rankLast[maxNbBits-currentNbBits] = (
U32)
pos;
249 while (totalCost > 0) {
251 for ( ; nBitsToDecrease > 1; nBitsToDecrease--) {
252 U32 const highPos = rankLast[nBitsToDecrease];
253 U32 const lowPos = rankLast[nBitsToDecrease-1];
254 if (highPos == noSymbol)
continue;
255 if (lowPos == noSymbol)
break;
256 {
U32 const highTotal = huffNode[highPos].
count;
257 U32 const lowTotal = 2 * huffNode[lowPos].
count;
258 if (highTotal <= lowTotal)
break;
262 while ((nBitsToDecrease<=HUF_TABLELOG_MAX) && (rankLast[nBitsToDecrease] == noSymbol))
264 totalCost -= 1 << (nBitsToDecrease-1);
265 if (rankLast[nBitsToDecrease-1] == noSymbol)
266 rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease];
267 huffNode[rankLast[nBitsToDecrease]].
nbBits ++;
268 if (rankLast[nBitsToDecrease] == 0)
269 rankLast[nBitsToDecrease] = noSymbol;
271 rankLast[nBitsToDecrease]--;
272 if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits-nBitsToDecrease)
273 rankLast[nBitsToDecrease] = noSymbol;
276 while (totalCost < 0) {
277 if (rankLast[1] == noSymbol) {
278 while (huffNode[
n].nbBits == maxNbBits)
n--;
281 rankLast[1] = (
U32)(
n+1);
285 huffNode[ rankLast[1] + 1 ].
nbBits--;
300#define RANK_POSITION_TABLE_SIZE 32
312 for (
n=0;
n<=maxSymbolValue;
n++) {
314 rankPosition[
r].
base ++;
316 for (
n=30;
n>0;
n--) rankPosition[
n-1].
base += rankPosition[
n].
base;
318 for (
n=0;
n<=maxSymbolValue;
n++) {
323 huffNode[
pos] = huffNode[
pos-1];
336#define STARTNODE (HUF_SYMBOLVALUE_MAX+1)
342 nodeElt*
const huffNode = huffNode0+1;
349 if (((
size_t)workSpace & 3) != 0)
return ERROR(GENERIC);
351 return ERROR(workSpace_tooSmall);
352 if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT;
353 if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
354 return ERROR(maxSymbolValue_tooLarge);
361 nonNullRank = (
int)maxSymbolValue;
362 while(huffNode[nonNullRank].
count == 0) nonNullRank--;
363 lowS = nonNullRank; nodeRoot = nodeNb + lowS - 1; lowN = nodeNb;
364 huffNode[nodeNb].
count = huffNode[lowS].
count + huffNode[lowS-1].
count;
367 for (
n=nodeNb;
n<=nodeRoot;
n++) huffNode[
n].
count = (
U32)(1U<<30);
371 while (nodeNb <= nodeRoot) {
372 int const n1 = (huffNode[lowS].
count < huffNode[lowN].
count) ? lowS-- : lowN++;
373 int const n2 = (huffNode[lowS].
count < huffNode[lowN].
count) ? lowS-- : lowN++;
380 huffNode[nodeRoot].
nbBits = 0;
382 huffNode[
n].nbBits = huffNode[ huffNode[
n].
parent ].nbBits + 1;
383 for (
n=0;
n<=nonNullRank;
n++)
384 huffNode[
n].nbBits = huffNode[ huffNode[
n].
parent ].nbBits + 1;
390 {
U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0};
391 U16 valPerRank[HUF_TABLELOG_MAX+1] = {0};
392 int const alphabetSize = (
int)(maxSymbolValue + 1);
393 if (maxNbBits > HUF_TABLELOG_MAX)
return ERROR(GENERIC);
394 for (
n=0;
n<=nonNullRank;
n++)
395 nbPerRank[huffNode[
n].nbBits]++;
398 for (
n=(
int)maxNbBits;
n>0;
n--) {
403 for (
n=0;
n<alphabetSize;
n++)
404 tree[huffNode[
n].
byte].nbBits = huffNode[
n].nbBits;
405 for (
n=0;
n<alphabetSize;
n++)
426 for (
s = 0;
s <= (
int)maxSymbolValue; ++
s) {
427 nbBits += CTable[
s].nbBits *
count[
s];
435 for (
s = 0;
s <= (
int)maxSymbolValue; ++
s) {
436 bad |= (
count[
s] != 0) & (CTable[
s].nbBits == 0);
449#define HUF_FLUSHBITS(s) BIT_flushBits(s)
451#define HUF_FLUSHBITS_1(stream) \
452 if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*2+7) HUF_FLUSHBITS(stream)
454#define HUF_FLUSHBITS_2(stream) \
455 if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*4+7) HUF_FLUSHBITS(stream)
459 const void*
src,
size_t srcSize,
460 const HUF_CElt* CTable)
464 BYTE*
const oend = ostart + dstSize;
470 if (dstSize < 8)
return 0;
507HUF_compress1X_usingCTable_internal_bmi2(
void*
dst,
size_t dstSize,
509 const HUF_CElt* CTable)
515HUF_compress1X_usingCTable_internal_default(
void*
dst,
size_t dstSize,
516 const void*
src,
size_t srcSize,
517 const HUF_CElt* CTable)
524 const void*
src,
size_t srcSize,
525 const HUF_CElt* CTable,
const int bmi2)
528 return HUF_compress1X_usingCTable_internal_bmi2(
dst, dstSize,
src, srcSize, CTable);
530 return HUF_compress1X_usingCTable_internal_default(
dst, dstSize,
src, srcSize, CTable);
537 const void*
src,
size_t srcSize,
538 const HUF_CElt* CTable,
const int bmi2)
554 const void*
src,
size_t srcSize,
555 const HUF_CElt* CTable,
int bmi2)
557 size_t const segmentSize = (srcSize+3)/4;
559 const BYTE*
const iend =
ip + srcSize;
561 BYTE*
const oend = ostart + dstSize;
564 if (dstSize < 6 + 1 + 1 + 1 + 8)
return 0;
565 if (srcSize < 12)
return 0;
570 if (cSize==0)
return 0;
579 if (cSize==0)
return 0;
588 if (cSize==0)
return 0;
598 if (cSize==0)
return 0;
602 return (
size_t)(
op-ostart);
614 const void*
src,
size_t srcSize,
621 if (cSize==0) {
return 0; }
625 if ((
size_t)(
op-ostart) >= srcSize-1) {
return 0; }
626 return (
size_t)(
op-ostart);
630 unsigned count[HUF_SYMBOLVALUE_MAX + 1];
631 HUF_CElt CTable[HUF_SYMBOLVALUE_MAX + 1];
639 const void*
src,
size_t srcSize,
640 unsigned maxSymbolValue,
unsigned huffLog,
642 void* workSpace,
size_t wkspSize,
643 HUF_CElt* oldHufTable, HUF_repeat*
repeat,
int preferRepeat,
648 BYTE*
const oend = ostart + dstSize;
654 if (((
size_t)workSpace & 3) != 0)
return ERROR(GENERIC);
656 if (!srcSize)
return 0;
657 if (!dstSize)
return 0;
659 if (huffLog > HUF_TABLELOG_MAX)
return ERROR(tableLog_tooLarge);
660 if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
return ERROR(maxSymbolValue_tooLarge);
661 if (!maxSymbolValue) maxSymbolValue = HUF_SYMBOLVALUE_MAX;
662 if (!huffLog) huffLog = HUF_TABLELOG_DEFAULT;
665 if (preferRepeat &&
repeat && *
repeat == HUF_repeat_valid) {
668 nbStreams, oldHufTable, bmi2);
673 if (largest == srcSize) { *ostart = ((
const BYTE*)
src)[0];
return 1; }
674 if (largest <= (srcSize >> 7)+4)
return 0;
679 && *
repeat == HUF_repeat_check
681 *
repeat = HUF_repeat_none;
684 if (preferRepeat &&
repeat && *
repeat != HUF_repeat_none) {
687 nbStreams, oldHufTable, bmi2);
693 maxSymbolValue, huffLog,
694 &
table->buildCTable_wksp,
sizeof(
table->buildCTable_wksp));
696 huffLog = (
U32)maxBits;
699 sizeof(
table->CTable) - ((maxSymbolValue + 1) *
sizeof(HUF_CElt)));
708 if (oldSize <= hSize + newSize || hSize + 12 >= srcSize) {
711 nbStreams, oldHufTable, bmi2);
715 if (hSize + 12ul >= srcSize) {
return 0; }
723 nbStreams,
table->CTable, bmi2);
728 const void*
src,
size_t srcSize,
729 unsigned maxSymbolValue,
unsigned huffLog,
730 void* workSpace,
size_t wkspSize)
739 const void*
src,
size_t srcSize,
740 unsigned maxSymbolValue,
unsigned huffLog,
741 void* workSpace,
size_t wkspSize,
742 HUF_CElt* hufTable, HUF_repeat*
repeat,
int preferRepeat,
int bmi2)
746 workSpace, wkspSize, hufTable,
747 repeat, preferRepeat, bmi2);
751 const void*
src,
size_t srcSize,
752 unsigned maxSymbolValue,
unsigned huffLog)
762 const void*
src,
size_t srcSize,
763 unsigned maxSymbolValue,
unsigned huffLog,
764 void* workSpace,
size_t wkspSize)
776 const void*
src,
size_t srcSize,
777 unsigned maxSymbolValue,
unsigned huffLog,
778 void* workSpace,
size_t wkspSize,
779 HUF_CElt* hufTable, HUF_repeat*
repeat,
int preferRepeat,
int bmi2)
784 hufTable,
repeat, preferRepeat, bmi2);
788 const void*
src,
size_t srcSize,
789 unsigned maxSymbolValue,
unsigned huffLog)
_Tp _STLP_CALL norm(const complex< _Tp > &__z)
MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t *bitC)
MEM_STATIC unsigned BIT_highbit32(U32 val)
MEM_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *dstBuffer, size_t dstCapacity)
MEM_STATIC void BIT_addBitsFast(BIT_CStream_t *bitC, size_t value, unsigned nbBits)
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
#define TARGET_ATTRIBUTE(target)
MEM_STATIC void MEM_writeLE16(void *memPtr, U16 val)
size_t HUF_readStats(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize)
FSE_PUBLIC_API size_t FSE_writeNCount(void *buffer, size_t bufferSize, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
FSE_PUBLIC_API size_t FSE_normalizeCount(short *normalizedCounter, unsigned tableLog, const unsigned *count, size_t srcSize, unsigned maxSymbolValue)
FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
FSE_PUBLIC_API size_t FSE_compress_usingCTable(void *dst, size_t dstCapacity, const void *src, size_t srcSize, const FSE_CTable *ct)
size_t FSE_buildCTable_wksp(FSE_CTable *ct, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, size_t wkspSize)
unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus)
GLuint GLuint GLsizei count
GLdouble GLdouble GLdouble r
GLubyte GLubyte GLubyte GLubyte w
size_t HIST_count_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *source, size_t sourceSize, void *workSpace, size_t workSpaceSize)
unsigned HIST_count_simple(unsigned *count, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize)
#define HUF_BLOCKSIZE_MAX
#define HUF_WORKSPACE_SIZE
#define HUF_WORKSPACE_SIZE_U32
#define HUF_STATIC_ASSERT(c)
size_t HUF_compress1X(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog)
size_t HUF_compress4X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog, void *workSpace, size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, int preferRepeat, int bmi2)
size_t HUF_compressBound(size_t size)
size_t HUF_compress2(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog)
int HUF_validateCTable(const HUF_CElt *CTable, const unsigned *count, unsigned maxSymbolValue)
size_t HUF_compress4X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable)
static size_t HUF_compressWeights(void *dst, size_t dstSize, const void *weightTable, size_t wtSize)
static void HUF_sort(nodeElt *huffNode, const unsigned *count, U32 maxSymbolValue, rankPos *rankPosition)
unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
size_t HUF_buildCTable(HUF_CElt *tree, const unsigned *count, unsigned maxSymbolValue, unsigned maxNbBits)
FORCE_INLINE_TEMPLATE void HUF_encodeSymbol(BIT_CStream_t *bitCPtr, U32 symbol, const HUF_CElt *CTable)
size_t HUF_writeCTable(void *dst, size_t maxDstSize, const HUF_CElt *CTable, unsigned maxSymbolValue, unsigned huffLog)
static size_t HUF_compress1X_usingCTable_internal(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable, const int bmi2)
static size_t HUF_compress4X_usingCTable_internal(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable, int bmi2)
size_t HUF_compress1X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable)
#define HUF_FLUSHBITS_2(stream)
#define HUF_FLUSHBITS_1(stream)
U32 HUF_getNbBits(const void *symbolTable, U32 symbolValue)
size_t HUF_compress4X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog, void *workSpace, size_t wkspSize)
size_t HUF_estimateCompressedSize(const HUF_CElt *CTable, const unsigned *count, unsigned maxSymbolValue)
size_t HUF_compress(void *dst, size_t maxDstSize, const void *src, size_t srcSize)
#define RANK_POSITION_TABLE_SIZE
size_t HUF_compress1X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog, void *workSpace, size_t wkspSize)
static U32 HUF_setMaxHeight(nodeElt *huffNode, U32 lastNonNull, U32 maxNbBits)
size_t HUF_buildCTable_wksp(HUF_CElt *tree, const unsigned *count, U32 maxSymbolValue, U32 maxNbBits, void *workSpace, size_t wkspSize)
FORCE_INLINE_TEMPLATE size_t HUF_compress1X_usingCTable_internal_body(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable)
#define MAX_FSE_TABLELOG_FOR_HUFF_HEADER
size_t HUF_readCTable(HUF_CElt *CTable, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize, unsigned *hasZeroWeights)
static size_t HUF_compressCTable_internal(BYTE *const ostart, BYTE *op, BYTE *const oend, const void *src, size_t srcSize, HUF_nbStreams_e nbStreams, const HUF_CElt *CTable, const int bmi2)
size_t HUF_compress1X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog, void *workSpace, size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, int preferRepeat, int bmi2)
nodeElt huffNodeTable[HUF_CTABLE_WORKSPACE_SIZE_U32]
static size_t HUF_compress_internal(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog, HUF_nbStreams_e nbStreams, void *workSpace, size_t wkspSize, HUF_CElt *oldHufTable, HUF_repeat *repeat, int preferRepeat, const int bmi2)
#define memcpy(s1, s2, n)
struct task_struct * current
rankPos rankPosition[RANK_POSITION_TABLE_SIZE]
huffNodeTable huffNodeTbl
HUF_buildCTable_wksp_tables buildCTable_wksp
#define FORCE_INLINE_TEMPLATE