1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       lzma_encoder_presets.c
4207753Smm/// \brief      Encoder presets
5312518Sdelphij/// \note       xz needs this even when only decoding is enabled.
6207753Smm//
7207753Smm//  Author:     Lasse Collin
8207753Smm//
9207753Smm//  This file has been put into the public domain.
10207753Smm//  You can do whatever you want with this file.
11207753Smm//
12207753Smm///////////////////////////////////////////////////////////////////////////////
13207753Smm
14207753Smm#include "common.h"
15207753Smm
16207753Smm
17207753Smmextern LZMA_API(lzma_bool)
18207753Smmlzma_lzma_preset(lzma_options_lzma *options, uint32_t preset)
19207753Smm{
20207753Smm	const uint32_t level = preset & LZMA_PRESET_LEVEL_MASK;
21207753Smm	const uint32_t flags = preset & ~LZMA_PRESET_LEVEL_MASK;
22207753Smm	const uint32_t supported_flags = LZMA_PRESET_EXTREME;
23207753Smm
24207753Smm	if (level > 9 || (flags & ~supported_flags))
25207753Smm		return true;
26207753Smm
27207753Smm	options->preset_dict = NULL;
28207753Smm	options->preset_dict_size = 0;
29207753Smm
30207753Smm	options->lc = LZMA_LC_DEFAULT;
31207753Smm	options->lp = LZMA_LP_DEFAULT;
32207753Smm	options->pb = LZMA_PB_DEFAULT;
33207753Smm
34274261Sdelphij	static const uint8_t dict_pow2[]
35274261Sdelphij			= { 18, 20, 21, 22, 22, 23, 23, 24, 25, 26 };
36274261Sdelphij	options->dict_size = UINT32_C(1) << dict_pow2[level];
37207753Smm
38213700Smm	if (level <= 3) {
39213700Smm		options->mode = LZMA_MODE_FAST;
40213700Smm		options->mf = level == 0 ? LZMA_MF_HC3 : LZMA_MF_HC4;
41213700Smm		options->nice_len = level <= 1 ? 128 : 273;
42274261Sdelphij		static const uint8_t depths[] = { 4, 8, 24, 48 };
43274261Sdelphij		options->depth = depths[level];
44213700Smm	} else {
45213700Smm		options->mode = LZMA_MODE_NORMAL;
46213700Smm		options->mf = LZMA_MF_BT4;
47213700Smm		options->nice_len = level == 4 ? 16 : level == 5 ? 32 : 64;
48213700Smm		options->depth = 0;
49213700Smm	}
50207753Smm
51207753Smm	if (flags & LZMA_PRESET_EXTREME) {
52207753Smm		options->mode = LZMA_MODE_NORMAL;
53207753Smm		options->mf = LZMA_MF_BT4;
54213700Smm		if (level == 3 || level == 5) {
55213700Smm			options->nice_len = 192;
56213700Smm			options->depth = 0;
57213700Smm		} else {
58213700Smm			options->nice_len = 273;
59213700Smm			options->depth = 512;
60213700Smm		}
61207753Smm	}
62207753Smm
63207753Smm	return false;
64207753Smm}
65