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