git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-support/gbcompress/zx0/zx0_shrink.h

      1 /*
      2  * shrink.h - compressor definitions
      3  *
      4  * Copyright (C) 2021 Emmanuel Marty
      5  *
      6  * This software is provided 'as-is', without any express or implied
      7  * warranty.  In no event will the authors be held liable for any damages
      8  * arising from the use of this software.
      9  *
     10  * Permission is granted to anyone to use this software for any purpose,
     11  * including commercial applications, and to alter it and redistribute it
     12  * freely, subject to the following restrictions:
     13  *
     14  * 1. The origin of this software must not be misrepresented; you must not
     15  *    claim that you wrote the original software. If you use this software
     16  *    in a product, an acknowledgment in the product documentation would be
     17  *    appreciated but is not required.
     18  * 2. Altered source versions must be plainly marked as such, and must not be
     19  *    misrepresented as being the original software.
     20  * 3. This notice may not be removed or altered from any source distribution.
     21  */
     22 
     23 /*
     24  * Uses the libdivsufsort library Copyright (c) 2003-2008 Yuta Mori
     25  *
     26  * Implements the ZX0 encoding designed by Einar Saukas. https://github.com/einar-saukas/ZX0
     27  * Also inspired by Charles Bloom's compression blog. http://cbloomrants.blogspot.com/
     28  *
     29  */
     30 
     31 #ifndef _SHRINK_H
     32 #define _SHRINK_H
     33 
     34 #include "libdivsufsort/divsufsort.h"
     35 
     36 #ifdef __cplusplus
     37 extern "C" {
     38 #endif
     39 
     40 #define LCP_BITS 18
     41 #define TAG_BITS 4
     42 #define LCP_MAX ((1U<<(LCP_BITS - TAG_BITS)) - 1)
     43 #define LCP_AND_TAG_MAX ((1U<<LCP_BITS) - 1)
     44 #define LCP_SHIFT (63-LCP_BITS)
     45 #define LCP_MASK (((1ULL<<LCP_BITS) - 1) << LCP_SHIFT)
     46 #define POS_MASK ((1ULL<<LCP_SHIFT) - 1)
     47 #define VISITED_FLAG 0x8000000000000000ULL
     48 #define EXCL_VISITED_MASK  0x7fffffffffffffffULL
     49 
     50 #define NINITIAL_ARRIVALS_PER_POSITION 40
     51 #define NMAX_ARRIVALS_PER_POSITION 109
     52 #define NMATCHES_PER_INDEX 78
     53 
     54 #define LEAVE_ALONE_MATCH_SIZE 340
     55 
     56 /** One match option */
     57 typedef struct _salvador_match {
     58    unsigned short length;
     59    unsigned short offset;
     60 } salvador_match;
     61 
     62 /** Forward arrival slot */
     63 typedef struct _salvador_arrival {
     64    int cost;
     65 
     66    unsigned int from_pos:17;
     67    int from_slot:11;
     68 
     69    unsigned int rep_offset;
     70 
     71    unsigned int rep_pos:17;
     72    unsigned int match_len:14;
     73 
     74    int num_literals;
     75    int score;
     76 } salvador_arrival;
     77 
     78 /** Visited match */
     79 typedef int salvador_visited;
     80 
     81 /** Compression statistics */
     82 typedef struct _salvador_stats {
     83    int num_literals;
     84    int num_normal_matches;
     85    int num_rep_matches;
     86    int num_eod;
     87 
     88    int safe_dist;
     89 
     90    int min_literals;
     91    int max_literals;
     92    int total_literals;
     93 
     94    int min_offset;
     95    int max_offset;
     96    long long total_offsets;
     97 
     98    int min_match_len;
     99    int max_match_len;
    100    int total_match_lens;
    101 
    102    int min_rle1_len;
    103    int max_rle1_len;
    104    int total_rle1_lens;
    105 
    106    int min_rle2_len;
    107    int max_rle2_len;
    108    int total_rle2_lens;
    109 
    110    int commands_divisor;
    111    int literals_divisor;
    112    int match_divisor;
    113    int rle1_divisor;
    114    int rle2_divisor;
    115 } salvador_stats;
    116 
    117 /** Compression context */
    118 typedef struct _salvador_compressor {
    119    divsufsort_ctx_t divsufsort_context;
    120    unsigned long long *intervals;
    121    unsigned long long *pos_data;
    122    unsigned long long *open_intervals;
    123    salvador_match *match;
    124    unsigned short *match_depth;
    125    salvador_match *best_match;
    126    salvador_arrival *arrival;
    127    int *first_offset_for_byte;
    128    int *next_offset_for_pos;
    129    int *offset_cache;
    130    int flags;
    131    int block_size;
    132    int max_offset;
    133    int max_arrivals_per_position;
    134    salvador_stats stats;
    135 } salvador_compressor;
    136 
    137 /**
    138  * Get maximum compressed size of input(source) data
    139  *
    140  * @param nInputSize input(source) size in bytes
    141  *
    142  * @return maximum compressed size
    143  */
    144 size_t salvador_get_max_compressed_size(const size_t nInputSize);
    145 
    146 /**
    147  * Compress memory
    148  *
    149  * @param pInputData pointer to input(source) data to compress
    150  * @param pOutBuffer buffer for compressed data
    151  * @param nInputSize input(source) size in bytes
    152  * @param nMaxOutBufferSize maximum capacity of compression buffer
    153  * @param nFlags compression flags (set to FLG_IS_INVERTED)
    154  * @param nMaxOffset maximum match offset to use (0 for default)
    155  * @param nDictionarySize size of dictionary in front of input data (0 for none)
    156  * @param progress progress function, called after compressing each block, or NULL for none
    157  * @param pStats pointer to compression stats that are filled if this function is successful, or NULL
    158  *
    159  * @return actual compressed size, or -1 for error
    160  */
    161 size_t salvador_compress(const unsigned char *pInputData, unsigned char *pOutBuffer, const size_t nInputSize, const size_t nMaxOutBufferSize,
    162    const unsigned int nFlags, const size_t nMaxOffset, const size_t nDictionarySize, void(*progress)(long long nOriginalSize, long long nCompressedSize), salvador_stats *pStats);
    163 
    164 #ifdef __cplusplus
    165 }
    166 #endif
    167 
    168 #endif /* _SHRINK_H */

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.