/* Assigns a block id from the range [0, num_histograms) to each data element indata[0..length)andfillsinblock_id[0..length)withtheassignedvalues.
Returns the number of blocks, i.e. one plus the number of block switches. */ static size_t FN(FindBlocks)(const DataType* data, const size_t length, constdouble block_switch_bitcost, const size_t num_histograms, const HistogramType* histograms, double* insert_cost, double* cost,
uint8_t* switch_signal,
uint8_t* block_id) { const size_t alphabet_size = FN(HistogramDataSize)(); const size_t bitmap_len = (num_histograms + 7) >> 3;
size_t num_blocks = 1;
size_t byte_ix;
size_t i;
size_t j;
BROTLI_DCHECK(num_histograms <= 256);
/* Trivial case: single histogram -> single block type. */ if (num_histograms <= 1) { for (i = 0; i < length; ++i) {
block_id[i] = 0;
} return1;
}
/* Fill bitcost for each symbol of all histograms. *Non-existingsymbolcost:2+log2(total_count).
* Regular symbol cost: -log2(symbol_count / total_count). */
memset(insert_cost, 0, sizeof(insert_cost[0]) * alphabet_size * num_histograms); for (i = 0; i < num_histograms; ++i) {
insert_cost[i] = FastLog2((uint32_t)histograms[i].total_count_);
} for (i = alphabet_size; i != 0;) { /* Reverse order to use the 0-th row as a temporary storage. */
--i; for (j = 0; j < num_histograms; ++j) {
insert_cost[i * num_histograms + j] =
insert_cost[j] - BitCost(histograms[j].data_[i]);
}
}
/* After each iteration of this loop, cost[k] will contain the difference betweentheminimumcostofarrivingatthecurrentbytepositionusing entropycodek,andtheminimumcostofarrivingatthecurrentbyte position.Thisdifferenceiscappedattheblockswitchcost,andifit reachesblockswitchcost,itmeansthatwhenwetracebackfromthelast
position, we need to switch here. */
memset(cost, 0, sizeof(cost[0]) * num_histograms);
memset(switch_signal, 0, sizeof(switch_signal[0]) * length * bitmap_len); for (byte_ix = 0; byte_ix < length; ++byte_ix) {
size_t ix = byte_ix * bitmap_len;
size_t symbol = data[byte_ix];
size_t insert_cost_ix = symbol * num_histograms; double min_cost = 1e99; double block_switch_cost = block_switch_bitcost; staticconst size_t prologue_length = 2000; staticconstdouble multiplier = 0.07 / 2000;
size_t k; for (k = 0; k < num_histograms; ++k) { /* We are coding the symbol with entropy code k. */
cost[k] += insert_cost[insert_cost_ix + k]; if (cost[k] < min_cost) {
min_cost = cost[k];
block_id[byte_ix] = (uint8_t)k;
}
} /* More blocks for the beginning. */ if (byte_ix < prologue_length) {
block_switch_cost *= 0.77 + multiplier * (double)byte_ix;
} for (k = 0; k < num_histograms; ++k) {
cost[k] -= min_cost; if (cost[k] >= block_switch_cost) { const uint8_t mask = (uint8_t)(1u << (k & 7));
cost[k] = block_switch_cost;
BROTLI_DCHECK((k >> 3) < bitmap_len);
switch_signal[ix + (k >> 3)] |= mask;
}
}
}
byte_ix = length - 1;
{ /* Trace back from the last position and switch at the marked places. */
size_t ix = byte_ix * bitmap_len;
uint8_t cur_id = block_id[byte_ix]; while (byte_ix > 0) { const uint8_t mask = (uint8_t)(1u << (cur_id & 7));
BROTLI_DCHECK(((size_t)cur_id >> 3) < bitmap_len);
--byte_ix;
ix -= bitmap_len; if (switch_signal[ix + (cur_id >> 3)] & mask) { if (cur_id != block_id[byte_ix]) {
cur_id = block_id[byte_ix];
++num_blocks;
}
}
block_id[byte_ix] = cur_id;
}
} return num_blocks;
}
static size_t FN(RemapBlockIds)(uint8_t* block_ids, const size_t length,
uint16_t* new_id, const size_t num_histograms) { staticconst uint16_t kInvalidId = 256;
uint16_t next_id = 0;
size_t i; for (i = 0; i < num_histograms; ++i) {
new_id[i] = kInvalidId;
} for (i = 0; i < length; ++i) {
BROTLI_DCHECK(block_ids[i] < num_histograms); if (new_id[block_ids[i]] == kInvalidId) {
new_id[block_ids[i]] = next_id++;
}
} for (i = 0; i < length; ++i) {
block_ids[i] = (uint8_t)new_id[block_ids[i]];
BROTLI_DCHECK(block_ids[i] < num_histograms);
}
BROTLI_DCHECK(next_id <= num_histograms); return next_id;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.