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