1/* Copyright 2013 Google Inc. All Rights Reserved.
2
3   Distributed under MIT license.
4   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5*/
6
7/* Function to find backward reference copies. */
8
9#ifndef BROTLI_ENC_BACKWARD_REFERENCES_HQ_H_
10#define BROTLI_ENC_BACKWARD_REFERENCES_HQ_H_
11
12#include "../common/constants.h"
13#include "../common/context.h"
14#include "../common/dictionary.h"
15#include "../common/platform.h"
16#include <brotli/types.h>
17#include "./command.h"
18#include "./hash.h"
19#include "./memory.h"
20#include "./quality.h"
21
22#if defined(__cplusplus) || defined(c_plusplus)
23extern "C" {
24#endif
25
26BROTLI_INTERNAL void BrotliCreateZopfliBackwardReferences(MemoryManager* m,
27    size_t num_bytes,
28    size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
29    ContextLut literal_context_lut, const BrotliEncoderParams* params,
30    Hasher* hasher, int* dist_cache, size_t* last_insert_len,
31    Command* commands, size_t* num_commands, size_t* num_literals);
32
33BROTLI_INTERNAL void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m,
34    size_t num_bytes,
35    size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
36    ContextLut literal_context_lut, const BrotliEncoderParams* params,
37    Hasher* hasher, int* dist_cache, size_t* last_insert_len,
38    Command* commands, size_t* num_commands, size_t* num_literals);
39
40typedef struct ZopfliNode {
41  /* Best length to get up to this byte (not including this byte itself)
42     highest 7 bit is used to reconstruct the length code. */
43  uint32_t length;
44  /* Distance associated with the length. */
45  uint32_t distance;
46  /* Number of literal inserts before this copy; highest 5 bits contain
47     distance short code + 1 (or zero if no short code). */
48  uint32_t dcode_insert_length;
49
50  /* This union holds information used by dynamic-programming. During forward
51     pass |cost| it used to store the goal function. When node is processed its
52     |cost| is invalidated in favor of |shortcut|. On path back-tracing pass
53     |next| is assigned the offset to next node on the path. */
54  union {
55    /* Smallest cost to get to this byte from the beginning, as found so far. */
56    float cost;
57    /* Offset to the next node on the path. Equals to command_length() of the
58       next node on the path. For last node equals to BROTLI_UINT32_MAX */
59    uint32_t next;
60    /* Node position that provides next distance for distance cache. */
61    uint32_t shortcut;
62  } u;
63} ZopfliNode;
64
65BROTLI_INTERNAL void BrotliInitZopfliNodes(ZopfliNode* array, size_t length);
66
67/* Computes the shortest path of commands from position to at most
68   position + num_bytes.
69
70   On return, path->size() is the number of commands found and path[i] is the
71   length of the i-th command (copy length plus insert length).
72   Note that the sum of the lengths of all commands can be less than num_bytes.
73
74   On return, the nodes[0..num_bytes] array will have the following
75   "ZopfliNode array invariant":
76   For each i in [1..num_bytes], if nodes[i].cost < kInfinity, then
77     (1) nodes[i].copy_length() >= 2
78     (2) nodes[i].command_length() <= i and
79     (3) nodes[i - nodes[i].command_length()].cost < kInfinity */
80BROTLI_INTERNAL size_t BrotliZopfliComputeShortestPath(
81    MemoryManager* m, size_t num_bytes,
82    size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
83    ContextLut literal_context_lut, const BrotliEncoderParams* params,
84    const int* dist_cache, Hasher* hasher, ZopfliNode* nodes);
85
86BROTLI_INTERNAL void BrotliZopfliCreateCommands(
87    const size_t num_bytes, const size_t block_start, const ZopfliNode* nodes,
88    int* dist_cache, size_t* last_insert_len, const BrotliEncoderParams* params,
89    Command* commands, size_t* num_literals);
90
91#if defined(__cplusplus) || defined(c_plusplus)
92}  /* extern "C" */
93#endif
94
95#endif  /* BROTLI_ENC_BACKWARD_REFERENCES_HQ_H_ */
96