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;
27 lzma_options_lzma opt_lzma;
29 /// We need to keep the filters array available in case
30 /// LZMA_FULL_FLUSH is used.
31 lzma_filter filters[5];
36 easy_set_filters(lzma_coder *coder, uint32_t level)
41 // TODO FIXME Use Subblock or LZMA2 with no compression.
44 #ifdef HAVE_ENCODER_LZMA2
45 } else if (level <= 9) {
46 error = lzma_lzma_preset(&coder->opt_lzma, level - 1);
47 coder->filters[0].id = LZMA_FILTER_LZMA2;
48 coder->filters[0].options = &coder->opt_lzma;
49 coder->filters[1].id = LZMA_VLI_UNKNOWN;
61 easy_encode(lzma_coder *coder, lzma_allocator *allocator,
62 const uint8_t *restrict in, size_t *restrict in_pos,
63 size_t in_size, uint8_t *restrict out,
64 size_t *restrict out_pos, size_t out_size, lzma_action action)
66 return coder->stream_encoder.code(
67 coder->stream_encoder.coder, allocator,
68 in, in_pos, in_size, out, out_pos, out_size, action);
73 easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
75 lzma_next_end(&coder->stream_encoder, allocator);
76 lzma_free(coder, allocator);
82 easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
83 lzma_easy_level level)
85 lzma_next_coder_init(easy_encoder_init, next, allocator);
87 if (next->coder == NULL) {
88 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
89 if (next->coder == NULL)
90 return LZMA_MEM_ERROR;
92 next->code = &easy_encode;
93 next->end = &easy_encoder_end;
95 next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
98 if (easy_set_filters(next->coder, level))
99 return LZMA_OPTIONS_ERROR;
101 return lzma_stream_encoder_init(&next->coder->stream_encoder,
102 allocator, next->coder->filters, LZMA_CHECK_CRC32);
106 extern LZMA_API lzma_ret
107 lzma_easy_encoder(lzma_stream *strm, lzma_easy_level level)
109 lzma_next_strm_init(easy_encoder_init, strm, level);
111 strm->internal->supported_actions[LZMA_RUN] = true;
112 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
113 strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
114 strm->internal->supported_actions[LZMA_FINISH] = true;
120 extern LZMA_API uint64_t
121 lzma_easy_memory_usage(lzma_easy_level level)
124 if (easy_set_filters(&coder, level))
127 return lzma_memusage_encoder(coder.filters);