Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 1 | // Copyright 2013 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 | // API for Brotli compression |
| 16 | |
| 17 | #ifndef BROTLI_ENC_ENCODE_H_ |
| 18 | #define BROTLI_ENC_ENCODE_H_ |
| 19 | |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string> |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 23 | #include <vector> |
| 24 | #include "./hash.h" |
| 25 | #include "./ringbuffer.h" |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 26 | |
| 27 | namespace brotli { |
| 28 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 29 | class BrotliCompressor { |
| 30 | public: |
| 31 | BrotliCompressor(); |
| 32 | ~BrotliCompressor(); |
| 33 | |
| 34 | // Writes the stream header into the internal output buffer. |
| 35 | void WriteStreamHeader(); |
| 36 | |
| 37 | // Encodes the data in input_buffer as a meta-block and writes it to |
| 38 | // encoded_buffer and sets *encoded_size to the number of bytes that was |
| 39 | // written. |
| 40 | void WriteMetaBlock(const size_t input_size, |
| 41 | const uint8_t* input_buffer, |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 42 | const bool is_last, |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 43 | size_t* encoded_size, |
| 44 | uint8_t* encoded_buffer); |
| 45 | |
| 46 | // Writes a zero-length meta-block with end-of-input bit set to the |
| 47 | // internal output buffer and copies the output buffer to encoded_buffer and |
| 48 | // sets *encoded_size to the number of bytes written. |
| 49 | void FinishStream(size_t* encoded_size, uint8_t* encoded_buffer); |
| 50 | |
| 51 | |
| 52 | private: |
Zoltan Szabadka | 2733d6c | 2014-02-17 14:25:36 +0100 | [diff] [blame^] | 53 | // Initializes the hasher with the hashes of dictionary words. |
| 54 | void StoreDictionaryWordHashes(); |
| 55 | |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 56 | int window_bits_; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 57 | Hasher* hasher_; |
| 58 | int dist_ringbuffer_[4]; |
| 59 | size_t dist_ringbuffer_idx_; |
| 60 | size_t input_pos_; |
| 61 | RingBuffer ringbuffer_; |
| 62 | std::vector<float> literal_cost_; |
| 63 | int storage_ix_; |
| 64 | uint8_t* storage_; |
| 65 | }; |
| 66 | |
Zoltan Szabadka | 79e99af | 2013-10-23 13:06:13 +0200 | [diff] [blame] | 67 | // Compresses the data in input_buffer into encoded_buffer, and sets |
| 68 | // *encoded_size to the compressed length. |
| 69 | // Returns 0 if there was an error and 1 otherwise. |
| 70 | int BrotliCompressBuffer(size_t input_size, |
| 71 | const uint8_t* input_buffer, |
| 72 | size_t* encoded_size, |
| 73 | uint8_t* encoded_buffer); |
| 74 | |
| 75 | |
| 76 | } // namespace brotli |
| 77 | |
| 78 | #endif // BROTLI_ENC_ENCODE_H_ |