126fa459cSmrg/* Copyright 2014 Google Inc. All Rights Reserved.
226fa459cSmrg
326fa459cSmrg   Distributed under MIT license.
426fa459cSmrg   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
526fa459cSmrg*/
626fa459cSmrg
726fa459cSmrg/* Functions to convert brotli-related data structures into the
826fa459cSmrg   brotli bit stream. The functions here operate under
926fa459cSmrg   assumption that there is enough space in the storage, i.e., there are
1026fa459cSmrg   no out-of-range checks anywhere.
1126fa459cSmrg
1226fa459cSmrg   These functions do bit addressing into a byte array. The byte array
1326fa459cSmrg   is called "storage" and the index to the bit is called storage_ix
1426fa459cSmrg   in function arguments. */
1526fa459cSmrg
1626fa459cSmrg#ifndef BROTLI_ENC_BROTLI_BIT_STREAM_H_
1726fa459cSmrg#define BROTLI_ENC_BROTLI_BIT_STREAM_H_
1826fa459cSmrg
1926fa459cSmrg#include "../common/context.h"
2026fa459cSmrg#include "../common/platform.h"
2126fa459cSmrg#include <brotli/types.h>
2226fa459cSmrg#include "./command.h"
2326fa459cSmrg#include "./entropy_encode.h"
2426fa459cSmrg#include "./memory.h"
2526fa459cSmrg#include "./metablock.h"
2626fa459cSmrg
2726fa459cSmrg#if defined(__cplusplus) || defined(c_plusplus)
2826fa459cSmrgextern "C" {
2926fa459cSmrg#endif
3026fa459cSmrg
3126fa459cSmrg/* All Store functions here will use a storage_ix, which is always the bit
3226fa459cSmrg   position for the current storage. */
3326fa459cSmrg
3426fa459cSmrgBROTLI_INTERNAL void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num,
3526fa459cSmrg    HuffmanTree* tree, size_t* storage_ix, uint8_t* storage);
3626fa459cSmrg
3726fa459cSmrgBROTLI_INTERNAL void BrotliBuildAndStoreHuffmanTreeFast(
3826fa459cSmrg    MemoryManager* m, const uint32_t* histogram, const size_t histogram_total,
3926fa459cSmrg    const size_t max_bits, uint8_t* depth, uint16_t* bits, size_t* storage_ix,
4026fa459cSmrg    uint8_t* storage);
4126fa459cSmrg
4226fa459cSmrg/* REQUIRES: length > 0 */
4326fa459cSmrg/* REQUIRES: length <= (1 << 24) */
4426fa459cSmrgBROTLI_INTERNAL void BrotliStoreMetaBlock(MemoryManager* m,
4526fa459cSmrg    const uint8_t* input, size_t start_pos, size_t length, size_t mask,
4626fa459cSmrg    uint8_t prev_byte, uint8_t prev_byte2, BROTLI_BOOL is_last,
4726fa459cSmrg    const BrotliEncoderParams* params, ContextType literal_context_mode,
4826fa459cSmrg    const Command* commands, size_t n_commands, const MetaBlockSplit* mb,
4926fa459cSmrg    size_t* storage_ix, uint8_t* storage);
5026fa459cSmrg
5126fa459cSmrg/* Stores the meta-block without doing any block splitting, just collects
5226fa459cSmrg   one histogram per block category and uses that for entropy coding.
5326fa459cSmrg   REQUIRES: length > 0
5426fa459cSmrg   REQUIRES: length <= (1 << 24) */
5526fa459cSmrgBROTLI_INTERNAL void BrotliStoreMetaBlockTrivial(MemoryManager* m,
5626fa459cSmrg    const uint8_t* input, size_t start_pos, size_t length, size_t mask,
5726fa459cSmrg    BROTLI_BOOL is_last, const BrotliEncoderParams* params,
5826fa459cSmrg    const Command* commands, size_t n_commands,
5926fa459cSmrg    size_t* storage_ix, uint8_t* storage);
6026fa459cSmrg
6126fa459cSmrg/* Same as above, but uses static prefix codes for histograms with a only a few
6226fa459cSmrg   symbols, and uses static code length prefix codes for all other histograms.
6326fa459cSmrg   REQUIRES: length > 0
6426fa459cSmrg   REQUIRES: length <= (1 << 24) */
6526fa459cSmrgBROTLI_INTERNAL void BrotliStoreMetaBlockFast(MemoryManager* m,
6626fa459cSmrg    const uint8_t* input, size_t start_pos, size_t length, size_t mask,
6726fa459cSmrg    BROTLI_BOOL is_last, const BrotliEncoderParams* params,
6826fa459cSmrg    const Command* commands, size_t n_commands,
6926fa459cSmrg    size_t* storage_ix, uint8_t* storage);
7026fa459cSmrg
7126fa459cSmrg/* This is for storing uncompressed blocks (simple raw storage of
7226fa459cSmrg   bytes-as-bytes).
7326fa459cSmrg   REQUIRES: length > 0
7426fa459cSmrg   REQUIRES: length <= (1 << 24) */
7526fa459cSmrgBROTLI_INTERNAL void BrotliStoreUncompressedMetaBlock(
7626fa459cSmrg    BROTLI_BOOL is_final_block, const uint8_t* BROTLI_RESTRICT input,
7726fa459cSmrg    size_t position, size_t mask, size_t len,
7826fa459cSmrg    size_t* BROTLI_RESTRICT storage_ix, uint8_t* BROTLI_RESTRICT storage);
7926fa459cSmrg
8026fa459cSmrg#if defined(__cplusplus) || defined(c_plusplus)
8126fa459cSmrg}  /* extern "C" */
8226fa459cSmrg#endif
8326fa459cSmrg
8426fa459cSmrg#endif  /* BROTLI_ENC_BROTLI_BIT_STREAM_H_ */
85