1417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka/* Copyright 2015 Google Inc. All Rights Reserved.
2417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka
3417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka   Distributed under MIT license.
4417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka*/
6417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka
7352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov/* Function for fast encoding of an input fragment, independently from the input
8352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov   history. This function uses two-pass processing: in the first pass we save
9352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov   the found backward matches and literal bytes into a buffer, and in the
10352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov   second pass we emit them into the bit stream using prefix codes built based
11352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov   on the actual command and literal byte histograms. */
12417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka
13417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka#ifndef BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_
14417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka#define BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_
15417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka
1681480011581d1bb40e2ed26566a95d060f2767b3Eugene Kliuchnikov#include <brotli/types.h>
17b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#include "./memory.h"
18b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#include "./port.h"
19417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka
20b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#if defined(__cplusplus) || defined(c_plusplus)
21b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikovextern "C" {
22b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#endif
23417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka
24417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadkastatic const size_t kCompressFragmentTwoPassBlockSize = 1 << 17;
25417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka
26352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov/* Compresses "input" string to the "*storage" buffer as one or more complete
27352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov   meta-blocks, and updates the "*storage_ix" bit position.
28352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov
29352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov   If "is_last" is 1, emits an additional empty last meta-block.
30352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov
31352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov   REQUIRES: "input_size" is greater than zero, or "is_last" is 1.
32cdca91b6f59dd7632985667d2cd585ab68937b48Eugene Kliuchnikov   REQUIRES: "input_size" is less or equal to maximal metablock size (1 << 24).
33352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov   REQUIRES: "command_buf" and "literal_buf" point to at least
34352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov              kCompressFragmentTwoPassBlockSize long arrays.
35352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov   REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero.
360a63f99db9eeb8a15dc3941b96802141b1993912Eugene Kliuchnikov   REQUIRES: "table_size" is a power of two
370a63f99db9eeb8a15dc3941b96802141b1993912Eugene Kliuchnikov   OUTPUT: maximal copy distance <= |input_size|
3811df843cf019e0a18f5efce660693a30f487fb06Eugene Kliuchnikov   OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */
39b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene KliuchnikovBROTLI_INTERNAL void BrotliCompressFragmentTwoPass(MemoryManager* m,
40b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov                                                   const uint8_t* input,
41b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov                                                   size_t input_size,
422048189048f130b0e8fb60307277379743dc5a2dEugene Kliuchnikov                                                   BROTLI_BOOL is_last,
43b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov                                                   uint32_t* command_buf,
44b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov                                                   uint8_t* literal_buf,
45b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov                                                   int* table,
46b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov                                                   size_t table_size,
47b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov                                                   size_t* storage_ix,
48b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov                                                   uint8_t* storage);
49b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov
50b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#if defined(__cplusplus) || defined(c_plusplus)
51b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov}  /* extern "C" */
52b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#endif
53417107b3ddb04cfd4ddfa7592fed0eb886186281Zoltan Szabadka
54352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov#endif  /* BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_ */
55