11cb0ef41Sopenharmony_ci/* NOLINT(build/header_guard) */
21cb0ef41Sopenharmony_ci/* Copyright 2013 Google Inc. All Rights Reserved.
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci   Distributed under MIT license.
51cb0ef41Sopenharmony_ci   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
61cb0ef41Sopenharmony_ci*/
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci/* template parameters: EXPORT_FN, FN */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cistatic BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
111cb0ef41Sopenharmony_ci    size_t num_bytes, size_t position,
121cb0ef41Sopenharmony_ci    const uint8_t* ringbuffer, size_t ringbuffer_mask,
131cb0ef41Sopenharmony_ci    ContextLut literal_context_lut, const BrotliEncoderParams* params,
141cb0ef41Sopenharmony_ci    Hasher* hasher, int* dist_cache, size_t* last_insert_len,
151cb0ef41Sopenharmony_ci    Command* commands, size_t* num_commands, size_t* num_literals) {
161cb0ef41Sopenharmony_ci  HASHER()* privat = &hasher->privat.FN(_);
171cb0ef41Sopenharmony_ci  /* Set maximum distance, see section 9.1. of the spec. */
181cb0ef41Sopenharmony_ci  const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin);
191cb0ef41Sopenharmony_ci  const size_t position_offset = params->stream_offset;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  const Command* const orig_commands = commands;
221cb0ef41Sopenharmony_ci  size_t insert_length = *last_insert_len;
231cb0ef41Sopenharmony_ci  const size_t pos_end = position + num_bytes;
241cb0ef41Sopenharmony_ci  const size_t store_end = num_bytes >= FN(StoreLookahead)() ?
251cb0ef41Sopenharmony_ci      position + num_bytes - FN(StoreLookahead)() + 1 : position;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  /* For speed up heuristics for random data. */
281cb0ef41Sopenharmony_ci  const size_t random_heuristics_window_size =
291cb0ef41Sopenharmony_ci      LiteralSpreeLengthForSparseSearch(params);
301cb0ef41Sopenharmony_ci  size_t apply_random_heuristics = position + random_heuristics_window_size;
311cb0ef41Sopenharmony_ci  const size_t gap = 0;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  /* Minimum score to accept a backward reference. */
341cb0ef41Sopenharmony_ci  const score_t kMinScore = BROTLI_SCORE_BASE + 100;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  BROTLI_UNUSED(literal_context_lut);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  FN(PrepareDistanceCache)(privat, dist_cache);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  while (position + FN(HashTypeLength)() < pos_end) {
411cb0ef41Sopenharmony_ci    size_t max_length = pos_end - position;
421cb0ef41Sopenharmony_ci    size_t max_distance = BROTLI_MIN(size_t, position, max_backward_limit);
431cb0ef41Sopenharmony_ci    size_t dictionary_start = BROTLI_MIN(size_t,
441cb0ef41Sopenharmony_ci        position + position_offset, max_backward_limit);
451cb0ef41Sopenharmony_ci    HasherSearchResult sr;
461cb0ef41Sopenharmony_ci    sr.len = 0;
471cb0ef41Sopenharmony_ci    sr.len_code_delta = 0;
481cb0ef41Sopenharmony_ci    sr.distance = 0;
491cb0ef41Sopenharmony_ci    sr.score = kMinScore;
501cb0ef41Sopenharmony_ci    FN(FindLongestMatch)(privat, &params->dictionary,
511cb0ef41Sopenharmony_ci        ringbuffer, ringbuffer_mask, dist_cache, position, max_length,
521cb0ef41Sopenharmony_ci        max_distance, dictionary_start + gap, params->dist.max_distance, &sr);
531cb0ef41Sopenharmony_ci    if (sr.score > kMinScore) {
541cb0ef41Sopenharmony_ci      /* Found a match. Let's look for something even better ahead. */
551cb0ef41Sopenharmony_ci      int delayed_backward_references_in_row = 0;
561cb0ef41Sopenharmony_ci      --max_length;
571cb0ef41Sopenharmony_ci      for (;; --max_length) {
581cb0ef41Sopenharmony_ci        const score_t cost_diff_lazy = 175;
591cb0ef41Sopenharmony_ci        HasherSearchResult sr2;
601cb0ef41Sopenharmony_ci        sr2.len = params->quality < MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH ?
611cb0ef41Sopenharmony_ci            BROTLI_MIN(size_t, sr.len - 1, max_length) : 0;
621cb0ef41Sopenharmony_ci        sr2.len_code_delta = 0;
631cb0ef41Sopenharmony_ci        sr2.distance = 0;
641cb0ef41Sopenharmony_ci        sr2.score = kMinScore;
651cb0ef41Sopenharmony_ci        max_distance = BROTLI_MIN(size_t, position + 1, max_backward_limit);
661cb0ef41Sopenharmony_ci        dictionary_start = BROTLI_MIN(size_t,
671cb0ef41Sopenharmony_ci            position + 1 + position_offset, max_backward_limit);
681cb0ef41Sopenharmony_ci        FN(FindLongestMatch)(privat,
691cb0ef41Sopenharmony_ci            &params->dictionary,
701cb0ef41Sopenharmony_ci            ringbuffer, ringbuffer_mask, dist_cache, position + 1, max_length,
711cb0ef41Sopenharmony_ci            max_distance, dictionary_start + gap, params->dist.max_distance,
721cb0ef41Sopenharmony_ci            &sr2);
731cb0ef41Sopenharmony_ci        if (sr2.score >= sr.score + cost_diff_lazy) {
741cb0ef41Sopenharmony_ci          /* Ok, let's just write one byte for now and start a match from the
751cb0ef41Sopenharmony_ci             next byte. */
761cb0ef41Sopenharmony_ci          ++position;
771cb0ef41Sopenharmony_ci          ++insert_length;
781cb0ef41Sopenharmony_ci          sr = sr2;
791cb0ef41Sopenharmony_ci          if (++delayed_backward_references_in_row < 4 &&
801cb0ef41Sopenharmony_ci              position + FN(HashTypeLength)() < pos_end) {
811cb0ef41Sopenharmony_ci            continue;
821cb0ef41Sopenharmony_ci          }
831cb0ef41Sopenharmony_ci        }
841cb0ef41Sopenharmony_ci        break;
851cb0ef41Sopenharmony_ci      }
861cb0ef41Sopenharmony_ci      apply_random_heuristics =
871cb0ef41Sopenharmony_ci          position + 2 * sr.len + random_heuristics_window_size;
881cb0ef41Sopenharmony_ci      dictionary_start = BROTLI_MIN(size_t,
891cb0ef41Sopenharmony_ci          position + position_offset, max_backward_limit);
901cb0ef41Sopenharmony_ci      {
911cb0ef41Sopenharmony_ci        /* The first 16 codes are special short-codes,
921cb0ef41Sopenharmony_ci           and the minimum offset is 1. */
931cb0ef41Sopenharmony_ci        size_t distance_code = ComputeDistanceCode(
941cb0ef41Sopenharmony_ci            sr.distance, dictionary_start + gap, dist_cache);
951cb0ef41Sopenharmony_ci        if ((sr.distance <= (dictionary_start + gap)) && distance_code > 0) {
961cb0ef41Sopenharmony_ci          dist_cache[3] = dist_cache[2];
971cb0ef41Sopenharmony_ci          dist_cache[2] = dist_cache[1];
981cb0ef41Sopenharmony_ci          dist_cache[1] = dist_cache[0];
991cb0ef41Sopenharmony_ci          dist_cache[0] = (int)sr.distance;
1001cb0ef41Sopenharmony_ci          FN(PrepareDistanceCache)(privat, dist_cache);
1011cb0ef41Sopenharmony_ci        }
1021cb0ef41Sopenharmony_ci        InitCommand(commands++, &params->dist, insert_length,
1031cb0ef41Sopenharmony_ci            sr.len, sr.len_code_delta, distance_code);
1041cb0ef41Sopenharmony_ci      }
1051cb0ef41Sopenharmony_ci      *num_literals += insert_length;
1061cb0ef41Sopenharmony_ci      insert_length = 0;
1071cb0ef41Sopenharmony_ci      /* Put the hash keys into the table, if there are enough bytes left.
1081cb0ef41Sopenharmony_ci         Depending on the hasher implementation, it can push all positions
1091cb0ef41Sopenharmony_ci         in the given range or only a subset of them.
1101cb0ef41Sopenharmony_ci         Avoid hash poisoning with RLE data. */
1111cb0ef41Sopenharmony_ci      {
1121cb0ef41Sopenharmony_ci        size_t range_start = position + 2;
1131cb0ef41Sopenharmony_ci        size_t range_end = BROTLI_MIN(size_t, position + sr.len, store_end);
1141cb0ef41Sopenharmony_ci        if (sr.distance < (sr.len >> 2)) {
1151cb0ef41Sopenharmony_ci          range_start = BROTLI_MIN(size_t, range_end, BROTLI_MAX(size_t,
1161cb0ef41Sopenharmony_ci              range_start, position + sr.len - (sr.distance << 2)));
1171cb0ef41Sopenharmony_ci        }
1181cb0ef41Sopenharmony_ci        FN(StoreRange)(privat, ringbuffer, ringbuffer_mask, range_start,
1191cb0ef41Sopenharmony_ci                       range_end);
1201cb0ef41Sopenharmony_ci      }
1211cb0ef41Sopenharmony_ci      position += sr.len;
1221cb0ef41Sopenharmony_ci    } else {
1231cb0ef41Sopenharmony_ci      ++insert_length;
1241cb0ef41Sopenharmony_ci      ++position;
1251cb0ef41Sopenharmony_ci      /* If we have not seen matches for a long time, we can skip some
1261cb0ef41Sopenharmony_ci         match lookups. Unsuccessful match lookups are very very expensive
1271cb0ef41Sopenharmony_ci         and this kind of a heuristic speeds up compression quite
1281cb0ef41Sopenharmony_ci         a lot. */
1291cb0ef41Sopenharmony_ci      if (position > apply_random_heuristics) {
1301cb0ef41Sopenharmony_ci        /* Going through uncompressible data, jump. */
1311cb0ef41Sopenharmony_ci        if (position >
1321cb0ef41Sopenharmony_ci            apply_random_heuristics + 4 * random_heuristics_window_size) {
1331cb0ef41Sopenharmony_ci          /* It is quite a long time since we saw a copy, so we assume
1341cb0ef41Sopenharmony_ci             that this data is not compressible, and store hashes less
1351cb0ef41Sopenharmony_ci             often. Hashes of non compressible data are less likely to
1361cb0ef41Sopenharmony_ci             turn out to be useful in the future, too, so we store less of
1371cb0ef41Sopenharmony_ci             them to not to flood out the hash table of good compressible
1381cb0ef41Sopenharmony_ci             data. */
1391cb0ef41Sopenharmony_ci          const size_t kMargin =
1401cb0ef41Sopenharmony_ci              BROTLI_MAX(size_t, FN(StoreLookahead)() - 1, 4);
1411cb0ef41Sopenharmony_ci          size_t pos_jump =
1421cb0ef41Sopenharmony_ci              BROTLI_MIN(size_t, position + 16, pos_end - kMargin);
1431cb0ef41Sopenharmony_ci          for (; position < pos_jump; position += 4) {
1441cb0ef41Sopenharmony_ci            FN(Store)(privat, ringbuffer, ringbuffer_mask, position);
1451cb0ef41Sopenharmony_ci            insert_length += 4;
1461cb0ef41Sopenharmony_ci          }
1471cb0ef41Sopenharmony_ci        } else {
1481cb0ef41Sopenharmony_ci          const size_t kMargin =
1491cb0ef41Sopenharmony_ci              BROTLI_MAX(size_t, FN(StoreLookahead)() - 1, 2);
1501cb0ef41Sopenharmony_ci          size_t pos_jump =
1511cb0ef41Sopenharmony_ci              BROTLI_MIN(size_t, position + 8, pos_end - kMargin);
1521cb0ef41Sopenharmony_ci          for (; position < pos_jump; position += 2) {
1531cb0ef41Sopenharmony_ci            FN(Store)(privat, ringbuffer, ringbuffer_mask, position);
1541cb0ef41Sopenharmony_ci            insert_length += 2;
1551cb0ef41Sopenharmony_ci          }
1561cb0ef41Sopenharmony_ci        }
1571cb0ef41Sopenharmony_ci      }
1581cb0ef41Sopenharmony_ci    }
1591cb0ef41Sopenharmony_ci  }
1601cb0ef41Sopenharmony_ci  insert_length += pos_end - position;
1611cb0ef41Sopenharmony_ci  *last_insert_len = insert_length;
1621cb0ef41Sopenharmony_ci  *num_commands += (size_t)(commands - orig_commands);
1631cb0ef41Sopenharmony_ci}
164