Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 1 | // 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 Szabadka | 467e6ee | 2014-10-28 11:53:52 +0100 | [diff] [blame] | 29 | #include <vector> |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 30 | |
Zoltan Szabadka | 534654d | 2015-03-27 14:20:35 +0100 | [diff] [blame] | 31 | #include "./metablock.h" |
| 32 | |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 33 | namespace 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. |
| 39 | void StoreVarLenUint8(int n, int* storage_ix, uint8_t* storage); |
| 40 | |
| 41 | // Stores the compressed meta-block header. |
Zoltan Szabadka | ca8c289 | 2014-10-28 12:09:18 +0100 | [diff] [blame] | 42 | bool StoreCompressedMetaBlockHeader(bool final_block, |
Zoltan Szabadka | 497814e | 2015-03-24 10:18:06 +0100 | [diff] [blame] | 43 | size_t length, |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 44 | int* storage_ix, |
| 45 | uint8_t* storage); |
| 46 | |
| 47 | // Stores the uncompressed meta-block header. |
Zoltan Szabadka | 497814e | 2015-03-24 10:18:06 +0100 | [diff] [blame] | 48 | bool StoreUncompressedMetaBlockHeader(size_t length, |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 49 | int* storage_ix, |
| 50 | uint8_t* storage); |
| 51 | |
| 52 | // Stores a context map where the histogram type is always the block type. |
| 53 | void StoreTrivialContextMap(int num_types, |
| 54 | int context_bits, |
| 55 | int* storage_ix, |
| 56 | uint8_t* storage); |
| 57 | |
Zoltan Szabadka | 0428f2d | 2014-10-28 13:47:21 +0100 | [diff] [blame] | 58 | void StoreHuffmanTreeOfHuffmanTreeToBitMask( |
| 59 | const int num_codes, |
| 60 | const uint8_t *code_length_bitdepth, |
| 61 | int *storage_ix, |
| 62 | uint8_t *storage); |
| 63 | |
Zoltan Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 64 | // 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. |
| 66 | void 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 Szabadka | 0428f2d | 2014-10-28 13:47:21 +0100 | [diff] [blame] | 74 | // Encodes the given context map to the bit stream. The number of different |
| 75 | // histogram ids is given by num_clusters. |
| 76 | void EncodeContextMap(const std::vector<int>& context_map, |
| 77 | int num_clusters, |
| 78 | int* storage_ix, uint8_t* storage); |
| 79 | |
Zoltan Szabadka | 467e6ee | 2014-10-28 11:53:52 +0100 | [diff] [blame] | 80 | // Data structure that stores everything that is needed to encode each block |
| 81 | // switch command. |
| 82 | struct 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. |
| 95 | void 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. |
| 104 | void StoreBlockSwitch(const BlockSplitCode& code, |
| 105 | const int block_ix, |
| 106 | int* storage_ix, |
| 107 | uint8_t* storage); |
| 108 | |
Zoltan Szabadka | 534654d | 2015-03-27 14:20:35 +0100 | [diff] [blame] | 109 | bool 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). |
| 126 | bool 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 Szabadka | d6d9fc6 | 2014-10-15 14:01:36 +0200 | [diff] [blame] | 133 | } // namespace brotli |
| 134 | |
| 135 | #endif // BROTLI_ENC_BROTLI_BIT_STREAM_H_ |