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