blob: 085f100430267d6b8389dda109a458ccdb5ce126 [file] [log] [blame]
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +02001// Copyright 2014 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Functions to convert brotli-related data structures into the
16// brotli bit stream. The functions here operate under
17// assumption that there is enough space in the storage, i.e., there are
18// no out-of-range checks anywhere.
19//
20// These functions do bit addressing into a byte array. The byte array
21// is called "storage" and the index to the bit is called storage_ix
22// in function arguments.
23
24#ifndef BROTLI_ENC_BROTLI_BIT_STREAM_H_
25#define BROTLI_ENC_BROTLI_BIT_STREAM_H_
26
27#include <stddef.h>
28#include <stdint.h>
Zoltan Szabadka467e6ee2014-10-28 11:53:52 +010029#include <vector>
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +020030
31namespace brotli {
32
33// All Store functions here will use a storage_ix, which is always the bit
34// position for the current storage.
35
36// Stores a number between 0 and 255.
37void StoreVarLenUint8(int n, int* storage_ix, uint8_t* storage);
38
39// Stores the compressed meta-block header.
40void StoreCompressedMetaBlockHeader(bool final_block,
41 int length,
42 int* storage_ix,
43 uint8_t* storage);
44
45// Stores the uncompressed meta-block header.
46void StoreUncompressedMetaBlockHeader(int length,
47 int* storage_ix,
48 uint8_t* storage);
49
50// Stores a context map where the histogram type is always the block type.
51void StoreTrivialContextMap(int num_types,
52 int context_bits,
53 int* storage_ix,
54 uint8_t* storage);
55
56// Builds a Huffman tree from histogram[0:length] into depth[0:length] and
57// bits[0:length] and stores the encoded tree to the bit stream.
58void BuildAndStoreHuffmanTree(const int *histogram,
59 const int length,
60 const int quality,
61 uint8_t* depth,
62 uint16_t* bits,
63 int* storage_ix,
64 uint8_t* storage);
65
Zoltan Szabadka467e6ee2014-10-28 11:53:52 +010066// Data structure that stores everything that is needed to encode each block
67// switch command.
68struct BlockSplitCode {
69 std::vector<int> type_code;
70 std::vector<int> length_prefix;
71 std::vector<int> length_nextra;
72 std::vector<int> length_extra;
73 std::vector<uint8_t> type_depths;
74 std::vector<uint16_t> type_bits;
75 std::vector<uint8_t> length_depths;
76 std::vector<uint16_t> length_bits;
77};
78
79// Builds a BlockSplitCode data structure from the block split given by the
80// vector of block types and block lengths and stores it to the bit stream.
81void BuildAndStoreBlockSplitCode(const std::vector<int>& types,
82 const std::vector<int>& lengths,
83 const int num_types,
84 const int quality,
85 BlockSplitCode* code,
86 int* storage_ix,
87 uint8_t* storage);
88
89// Stores the block switch command with index block_ix to the bit stream.
90void StoreBlockSwitch(const BlockSplitCode& code,
91 const int block_ix,
92 int* storage_ix,
93 uint8_t* storage);
94
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +020095} // namespace brotli
96
97#endif // BROTLI_ENC_BROTLI_BIT_STREAM_H_