blob: 61e79f52bd6877e5dfaa894c7c3adef12980dda0 [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
Zoltan Szabadka534654d2015-03-27 14:20:35 +010031#include "./metablock.h"
32
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +020033namespace brotli {
34
35// All Store functions here will use a storage_ix, which is always the bit
36// position for the current storage.
37
38// Stores a number between 0 and 255.
39void StoreVarLenUint8(int n, int* storage_ix, uint8_t* storage);
40
41// Stores the compressed meta-block header.
Zoltan Szabadkaca8c2892014-10-28 12:09:18 +010042bool StoreCompressedMetaBlockHeader(bool final_block,
Zoltan Szabadka497814e2015-03-24 10:18:06 +010043 size_t length,
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +020044 int* storage_ix,
45 uint8_t* storage);
46
47// Stores the uncompressed meta-block header.
Zoltan Szabadka497814e2015-03-24 10:18:06 +010048bool StoreUncompressedMetaBlockHeader(size_t length,
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +020049 int* storage_ix,
50 uint8_t* storage);
51
52// Stores a context map where the histogram type is always the block type.
53void StoreTrivialContextMap(int num_types,
54 int context_bits,
55 int* storage_ix,
56 uint8_t* storage);
57
Zoltan Szabadka0428f2d2014-10-28 13:47:21 +010058void StoreHuffmanTreeOfHuffmanTreeToBitMask(
59 const int num_codes,
60 const uint8_t *code_length_bitdepth,
61 int *storage_ix,
62 uint8_t *storage);
63
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +020064// Builds a Huffman tree from histogram[0:length] into depth[0:length] and
65// bits[0:length] and stores the encoded tree to the bit stream.
66void BuildAndStoreHuffmanTree(const int *histogram,
67 const int length,
68 const int quality,
69 uint8_t* depth,
70 uint16_t* bits,
71 int* storage_ix,
72 uint8_t* storage);
73
Zoltan Szabadka0428f2d2014-10-28 13:47:21 +010074// Encodes the given context map to the bit stream. The number of different
75// histogram ids is given by num_clusters.
76void EncodeContextMap(const std::vector<int>& context_map,
77 int num_clusters,
78 int* storage_ix, uint8_t* storage);
79
Zoltan Szabadka467e6ee2014-10-28 11:53:52 +010080// Data structure that stores everything that is needed to encode each block
81// switch command.
82struct BlockSplitCode {
83 std::vector<int> type_code;
84 std::vector<int> length_prefix;
85 std::vector<int> length_nextra;
86 std::vector<int> length_extra;
87 std::vector<uint8_t> type_depths;
88 std::vector<uint16_t> type_bits;
89 std::vector<uint8_t> length_depths;
90 std::vector<uint16_t> length_bits;
91};
92
93// Builds a BlockSplitCode data structure from the block split given by the
94// vector of block types and block lengths and stores it to the bit stream.
95void BuildAndStoreBlockSplitCode(const std::vector<int>& types,
96 const std::vector<int>& lengths,
97 const int num_types,
98 const int quality,
99 BlockSplitCode* code,
100 int* storage_ix,
101 uint8_t* storage);
102
103// Stores the block switch command with index block_ix to the bit stream.
104void StoreBlockSwitch(const BlockSplitCode& code,
105 const int block_ix,
106 int* storage_ix,
107 uint8_t* storage);
108
Zoltan Szabadka534654d2015-03-27 14:20:35 +0100109bool StoreMetaBlock(const uint8_t* input,
110 size_t start_pos,
111 size_t length,
112 size_t mask,
113 bool final_block,
114 int quality,
115 int num_direct_distance_codes,
116 int distance_postfix_bits,
117 int literal_context_mode,
118 const brotli::Command *commands,
119 size_t n_commands,
120 const MetaBlockSplit& mb,
121 int *storage_ix,
122 uint8_t *storage);
123
124// This is for storing uncompressed blocks (simple raw storage of
125// bytes-as-bytes).
126bool StoreUncompressedMetaBlock(bool final_block,
127 const uint8_t* input,
128 size_t position, size_t mask,
129 size_t len,
130 int* storage_ix,
131 uint8_t* storage);
132
Zoltan Szabadkad6d9fc62014-10-15 14:01:36 +0200133} // namespace brotli
134
135#endif // BROTLI_ENC_BROTLI_BIT_STREAM_H_