xref: /freebsd/contrib/xz/src/liblzma/lzma/lzma_encoder_presets.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lzma_encoder_presets.c
4 /// \brief      Encoder presets
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
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 	options->dict_size = UINT32_C(1) << (uint8_t []){
34 			18, 20, 21, 22, 22, 23, 23, 24, 25, 26 }[level];
35 
36 	if (level <= 3) {
37 		options->mode = LZMA_MODE_FAST;
38 		options->mf = level == 0 ? LZMA_MF_HC3 : LZMA_MF_HC4;
39 		options->nice_len = level <= 1 ? 128 : 273;
40 		options->depth = (uint8_t []){ 4, 8, 24, 48 }[level];
41 	} else {
42 		options->mode = LZMA_MODE_NORMAL;
43 		options->mf = LZMA_MF_BT4;
44 		options->nice_len = level == 4 ? 16 : level == 5 ? 32 : 64;
45 		options->depth = 0;
46 	}
47 
48 	if (flags & LZMA_PRESET_EXTREME) {
49 		options->mode = LZMA_MODE_NORMAL;
50 		options->mf = LZMA_MF_BT4;
51 		if (level == 3 || level == 5) {
52 			options->nice_len = 192;
53 			options->depth = 0;
54 		} else {
55 			options->nice_len = 273;
56 			options->depth = 512;
57 		}
58 	}
59 
60 	return false;
61 }
62