xref: /freebsd/contrib/xz/src/liblzma/lzma/lzma_encoder_presets.c (revision 3b35e7ee8de9b0260149a2b77e87a2b9c7a36244)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       lzma_encoder_presets.c
6 /// \brief      Encoder presets
7 /// \note       xz needs this even when only decoding is enabled.
8 //
9 //  Author:     Lasse Collin
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "common.h"
14 
15 
16 extern LZMA_API(lzma_bool)
17 lzma_lzma_preset(lzma_options_lzma *options, uint32_t preset)
18 {
19 	const uint32_t level = preset & LZMA_PRESET_LEVEL_MASK;
20 	const uint32_t flags = preset & ~LZMA_PRESET_LEVEL_MASK;
21 	const uint32_t supported_flags = LZMA_PRESET_EXTREME;
22 
23 	if (level > 9 || (flags & ~supported_flags))
24 		return true;
25 
26 	options->preset_dict = NULL;
27 	options->preset_dict_size = 0;
28 
29 	options->lc = LZMA_LC_DEFAULT;
30 	options->lp = LZMA_LP_DEFAULT;
31 	options->pb = LZMA_PB_DEFAULT;
32 
33 	static const uint8_t dict_pow2[]
34 			= { 18, 20, 21, 22, 22, 23, 23, 24, 25, 26 };
35 	options->dict_size = UINT32_C(1) << dict_pow2[level];
36 
37 	if (level <= 3) {
38 		options->mode = LZMA_MODE_FAST;
39 		options->mf = level == 0 ? LZMA_MF_HC3 : LZMA_MF_HC4;
40 		options->nice_len = level <= 1 ? 128 : 273;
41 		static const uint8_t depths[] = { 4, 8, 24, 48 };
42 		options->depth = depths[level];
43 	} else {
44 		options->mode = LZMA_MODE_NORMAL;
45 		options->mf = LZMA_MF_BT4;
46 		options->nice_len = level == 4 ? 16 : level == 5 ? 32 : 64;
47 		options->depth = 0;
48 	}
49 
50 	if (flags & LZMA_PRESET_EXTREME) {
51 		options->mode = LZMA_MODE_NORMAL;
52 		options->mf = LZMA_MF_BT4;
53 		if (level == 3 || level == 5) {
54 			options->nice_len = 192;
55 			options->depth = 0;
56 		} else {
57 			options->nice_len = 273;
58 			options->depth = 512;
59 		}
60 	}
61 
62 	return false;
63 }
64