2015-11-27 10:27:11 +00:00
|
|
|
/* Copyright 2014 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2014-10-15 12:01:36 +00:00
|
|
|
// Functions to convert brotli-related data structures into the
|
|
|
|
// brotli bit stream. The functions here operate under
|
|
|
|
// assumption that there is enough space in the storage, i.e., there are
|
|
|
|
// no out-of-range checks anywhere.
|
|
|
|
//
|
|
|
|
// These functions do bit addressing into a byte array. The byte array
|
|
|
|
// is called "storage" and the index to the bit is called storage_ix
|
|
|
|
// in function arguments.
|
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_BROTLI_BIT_STREAM_H_
|
|
|
|
#define BROTLI_ENC_BROTLI_BIT_STREAM_H_
|
|
|
|
|
2014-10-28 10:53:52 +00:00
|
|
|
#include <vector>
|
2014-10-15 12:01:36 +00:00
|
|
|
|
2015-03-27 13:20:35 +00:00
|
|
|
#include "./metablock.h"
|
2015-10-01 10:08:14 +00:00
|
|
|
#include "./types.h"
|
2015-03-27 13:20:35 +00:00
|
|
|
|
2014-10-15 12:01:36 +00:00
|
|
|
namespace brotli {
|
|
|
|
|
|
|
|
// All Store functions here will use a storage_ix, which is always the bit
|
|
|
|
// position for the current storage.
|
|
|
|
|
|
|
|
// Stores a number between 0 and 255.
|
2016-01-07 15:27:49 +00:00
|
|
|
void StoreVarLenUint8(size_t n, size_t* storage_ix, uint8_t* storage);
|
2014-10-15 12:01:36 +00:00
|
|
|
|
|
|
|
// Stores the compressed meta-block header.
|
2016-01-07 15:27:49 +00:00
|
|
|
// REQUIRES: length > 0
|
|
|
|
// REQUIRES: length <= (1 << 24)
|
|
|
|
void StoreCompressedMetaBlockHeader(bool final_block,
|
2015-03-24 09:18:06 +00:00
|
|
|
size_t length,
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t* storage_ix,
|
2014-10-15 12:01:36 +00:00
|
|
|
uint8_t* storage);
|
|
|
|
|
|
|
|
// Stores the uncompressed meta-block header.
|
2016-01-07 15:27:49 +00:00
|
|
|
// REQUIRES: length > 0
|
|
|
|
// REQUIRES: length <= (1 << 24)
|
|
|
|
void StoreUncompressedMetaBlockHeader(size_t length,
|
|
|
|
size_t* storage_ix,
|
2014-10-15 12:01:36 +00:00
|
|
|
uint8_t* storage);
|
|
|
|
|
|
|
|
// Stores a context map where the histogram type is always the block type.
|
2016-01-07 15:27:49 +00:00
|
|
|
void StoreTrivialContextMap(size_t num_types,
|
|
|
|
size_t context_bits,
|
|
|
|
size_t* storage_ix,
|
2014-10-15 12:01:36 +00:00
|
|
|
uint8_t* storage);
|
|
|
|
|
2014-10-28 12:47:21 +00:00
|
|
|
void StoreHuffmanTreeOfHuffmanTreeToBitMask(
|
|
|
|
const int num_codes,
|
|
|
|
const uint8_t *code_length_bitdepth,
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t *storage_ix,
|
2014-10-28 12:47:21 +00:00
|
|
|
uint8_t *storage);
|
|
|
|
|
2016-01-07 15:27:49 +00:00
|
|
|
void StoreHuffmanTree(const uint8_t* depths, size_t num,
|
|
|
|
size_t *storage_ix, uint8_t *storage);
|
|
|
|
|
2014-10-15 12:01:36 +00:00
|
|
|
// Builds a Huffman tree from histogram[0:length] into depth[0:length] and
|
|
|
|
// bits[0:length] and stores the encoded tree to the bit stream.
|
2016-01-07 15:27:49 +00:00
|
|
|
void BuildAndStoreHuffmanTree(const uint32_t *histogram,
|
|
|
|
const size_t length,
|
2014-10-15 12:01:36 +00:00
|
|
|
uint8_t* depth,
|
|
|
|
uint16_t* bits,
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t* storage_ix,
|
2014-10-15 12:01:36 +00:00
|
|
|
uint8_t* storage);
|
|
|
|
|
2016-01-08 09:10:22 +00:00
|
|
|
void BuildAndStoreHuffmanTreeFast(const uint32_t *histogram,
|
|
|
|
const size_t histogram_total,
|
|
|
|
const size_t max_bits,
|
|
|
|
uint8_t* depth,
|
|
|
|
uint16_t* bits,
|
|
|
|
size_t* storage_ix,
|
|
|
|
uint8_t* storage);
|
|
|
|
|
2014-10-28 12:47:21 +00:00
|
|
|
// Encodes the given context map to the bit stream. The number of different
|
|
|
|
// histogram ids is given by num_clusters.
|
2016-01-07 15:27:49 +00:00
|
|
|
void EncodeContextMap(const std::vector<uint32_t>& context_map,
|
|
|
|
size_t num_clusters,
|
|
|
|
size_t* storage_ix, uint8_t* storage);
|
2014-10-28 12:47:21 +00:00
|
|
|
|
2014-10-28 10:53:52 +00:00
|
|
|
// Data structure that stores everything that is needed to encode each block
|
|
|
|
// switch command.
|
|
|
|
struct BlockSplitCode {
|
2016-01-07 15:27:49 +00:00
|
|
|
std::vector<uint32_t> type_code;
|
|
|
|
std::vector<uint32_t> length_prefix;
|
|
|
|
std::vector<uint32_t> length_nextra;
|
|
|
|
std::vector<uint32_t> length_extra;
|
2014-10-28 10:53:52 +00:00
|
|
|
std::vector<uint8_t> type_depths;
|
|
|
|
std::vector<uint16_t> type_bits;
|
|
|
|
std::vector<uint8_t> length_depths;
|
|
|
|
std::vector<uint16_t> length_bits;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Builds a BlockSplitCode data structure from the block split given by the
|
|
|
|
// vector of block types and block lengths and stores it to the bit stream.
|
2016-01-07 15:27:49 +00:00
|
|
|
void BuildAndStoreBlockSplitCode(const std::vector<uint8_t>& types,
|
|
|
|
const std::vector<uint32_t>& lengths,
|
|
|
|
const size_t num_types,
|
2014-10-28 10:53:52 +00:00
|
|
|
BlockSplitCode* code,
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t* storage_ix,
|
2014-10-28 10:53:52 +00:00
|
|
|
uint8_t* storage);
|
|
|
|
|
|
|
|
// Stores the block switch command with index block_ix to the bit stream.
|
|
|
|
void StoreBlockSwitch(const BlockSplitCode& code,
|
2016-01-07 15:27:49 +00:00
|
|
|
const size_t block_ix,
|
|
|
|
size_t* storage_ix,
|
2014-10-28 10:53:52 +00:00
|
|
|
uint8_t* storage);
|
|
|
|
|
2016-01-07 15:27:49 +00:00
|
|
|
// REQUIRES: length > 0
|
|
|
|
// REQUIRES: length <= (1 << 24)
|
|
|
|
void StoreMetaBlock(const uint8_t* input,
|
2015-03-27 13:20:35 +00:00
|
|
|
size_t start_pos,
|
|
|
|
size_t length,
|
|
|
|
size_t mask,
|
2015-04-23 14:20:29 +00:00
|
|
|
uint8_t prev_byte,
|
|
|
|
uint8_t prev_byte2,
|
2015-03-27 13:20:35 +00:00
|
|
|
bool final_block,
|
2016-01-07 15:27:49 +00:00
|
|
|
uint32_t num_direct_distance_codes,
|
|
|
|
uint32_t distance_postfix_bits,
|
|
|
|
ContextType literal_context_mode,
|
2015-03-27 13:20:35 +00:00
|
|
|
const brotli::Command *commands,
|
|
|
|
size_t n_commands,
|
|
|
|
const MetaBlockSplit& mb,
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t *storage_ix,
|
2015-03-27 13:20:35 +00:00
|
|
|
uint8_t *storage);
|
|
|
|
|
2015-04-28 08:12:47 +00:00
|
|
|
// Stores the meta-block without doing any block splitting, just collects
|
|
|
|
// one histogram per block category and uses that for entropy coding.
|
2016-01-07 15:27:49 +00:00
|
|
|
// REQUIRES: length > 0
|
|
|
|
// REQUIRES: length <= (1 << 24)
|
|
|
|
void StoreMetaBlockTrivial(const uint8_t* input,
|
2015-04-28 08:12:47 +00:00
|
|
|
size_t start_pos,
|
|
|
|
size_t length,
|
|
|
|
size_t mask,
|
|
|
|
bool is_last,
|
|
|
|
const brotli::Command *commands,
|
|
|
|
size_t n_commands,
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t *storage_ix,
|
2015-04-28 08:12:47 +00:00
|
|
|
uint8_t *storage);
|
|
|
|
|
2016-01-08 09:10:22 +00:00
|
|
|
// Same as above, but uses static prefix codes for histograms with a only a few
|
|
|
|
// symbols, and uses static code length prefix codes for all other histograms.
|
|
|
|
// REQUIRES: length > 0
|
|
|
|
// REQUIRES: length <= (1 << 24)
|
|
|
|
void StoreMetaBlockFast(const uint8_t* input,
|
|
|
|
size_t start_pos,
|
|
|
|
size_t length,
|
|
|
|
size_t mask,
|
|
|
|
bool is_last,
|
|
|
|
const brotli::Command *commands,
|
|
|
|
size_t n_commands,
|
|
|
|
size_t *storage_ix,
|
|
|
|
uint8_t *storage);
|
|
|
|
|
2015-03-27 13:20:35 +00:00
|
|
|
// This is for storing uncompressed blocks (simple raw storage of
|
|
|
|
// bytes-as-bytes).
|
2016-01-07 15:27:49 +00:00
|
|
|
// REQUIRES: length > 0
|
|
|
|
// REQUIRES: length <= (1 << 24)
|
|
|
|
void StoreUncompressedMetaBlock(bool final_block,
|
2015-03-27 13:20:35 +00:00
|
|
|
const uint8_t* input,
|
|
|
|
size_t position, size_t mask,
|
|
|
|
size_t len,
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t* storage_ix,
|
2015-03-27 13:20:35 +00:00
|
|
|
uint8_t* storage);
|
|
|
|
|
2015-04-23 13:43:37 +00:00
|
|
|
// Stores an empty metadata meta-block and syncs to a byte boundary.
|
2016-01-07 15:27:49 +00:00
|
|
|
void StoreSyncMetaBlock(size_t* storage_ix, uint8_t* storage);
|
2015-04-23 13:43:37 +00:00
|
|
|
|
2014-10-15 12:01:36 +00:00
|
|
|
} // namespace brotli
|
|
|
|
|
|
|
|
#endif // BROTLI_ENC_BROTLI_BIT_STREAM_H_
|