1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Easy Stream encoder initialization
6 // Copyright (C) 2008 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "stream_encoder.h"
24 lzma_next_coder stream_encoder;
26 /// We need to keep the filters array available in case
27 /// LZMA_FULL_FLUSH is used.
28 lzma_filter filters[5];
33 easy_set_filters(lzma_filter *filters, uint32_t level)
38 // TODO FIXME Use Subblock or LZMA2 with no compression.
41 #ifdef HAVE_ENCODER_LZMA2
42 } else if (level <= 9) {
43 filters[0].id = LZMA_FILTER_LZMA2;
44 filters[0].options = (void *)(&lzma_preset_lzma[level - 1]);
45 filters[1].id = LZMA_VLI_UNKNOWN;
57 easy_encode(lzma_coder *coder, lzma_allocator *allocator,
58 const uint8_t *restrict in, size_t *restrict in_pos,
59 size_t in_size, uint8_t *restrict out,
60 size_t *restrict out_pos, size_t out_size, lzma_action action)
62 return coder->stream_encoder.code(
63 coder->stream_encoder.coder, allocator,
64 in, in_pos, in_size, out, out_pos, out_size, action);
69 easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
71 lzma_next_end(&coder->stream_encoder, allocator);
72 lzma_free(coder, allocator);
78 easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
79 lzma_easy_level level)
81 lzma_next_coder_init(easy_encoder_init, next, allocator);
83 if (next->coder == NULL) {
84 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
85 if (next->coder == NULL)
86 return LZMA_MEM_ERROR;
88 next->code = &easy_encode;
89 next->end = &easy_encoder_end;
91 next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
94 if (easy_set_filters(next->coder->filters, level))
95 return LZMA_OPTIONS_ERROR;
97 return lzma_stream_encoder_init(&next->coder->stream_encoder,
98 allocator, next->coder->filters, LZMA_CHECK_CRC32);
102 extern LZMA_API lzma_ret
103 lzma_easy_encoder(lzma_stream *strm, lzma_easy_level level)
105 lzma_next_strm_init(easy_encoder_init, strm, level);
107 strm->internal->supported_actions[LZMA_RUN] = true;
108 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
109 strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
110 strm->internal->supported_actions[LZMA_FINISH] = true;
116 extern LZMA_API uint64_t
117 lzma_easy_memory_usage(lzma_easy_level level)
119 lzma_filter filters[5];
120 if (easy_set_filters(filters, level))
123 return lzma_memusage_encoder(filters);