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[LZMA_FILTERS_MAX + 1];
36 easy_set_filters(lzma_coder *coder, uint32_t preset)
38 if (lzma_lzma_preset(&coder->opt_lzma, preset))
41 coder->filters[0].id = LZMA_FILTER_LZMA2;
42 coder->filters[0].options = &coder->opt_lzma;
43 coder->filters[1].id = LZMA_VLI_UNKNOWN;
50 easy_encode(lzma_coder *coder, lzma_allocator *allocator,
51 const uint8_t *restrict in, size_t *restrict in_pos,
52 size_t in_size, uint8_t *restrict out,
53 size_t *restrict out_pos, size_t out_size, lzma_action action)
55 return coder->stream_encoder.code(
56 coder->stream_encoder.coder, allocator,
57 in, in_pos, in_size, out, out_pos, out_size, action);
62 easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
64 lzma_next_end(&coder->stream_encoder, allocator);
65 lzma_free(coder, allocator);
71 easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
72 uint32_t preset, lzma_check check)
74 lzma_next_coder_init(easy_encoder_init, next, allocator);
76 if (next->coder == NULL) {
77 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
78 if (next->coder == NULL)
79 return LZMA_MEM_ERROR;
81 next->code = &easy_encode;
82 next->end = &easy_encoder_end;
84 next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
87 if (easy_set_filters(next->coder, preset))
88 return LZMA_OPTIONS_ERROR;
90 return lzma_stream_encoder_init(&next->coder->stream_encoder,
91 allocator, next->coder->filters, check);
95 extern LZMA_API(lzma_ret)
96 lzma_easy_encoder(lzma_stream *strm, uint32_t preset, lzma_check check)
98 lzma_next_strm_init(easy_encoder_init, strm, preset, check);
100 strm->internal->supported_actions[LZMA_RUN] = true;
101 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
102 strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
103 strm->internal->supported_actions[LZMA_FINISH] = true;
109 extern LZMA_API(uint64_t)
110 lzma_easy_encoder_memusage(uint32_t preset)
113 if (easy_set_filters(&coder, preset))
116 return lzma_raw_encoder_memusage(coder.filters);
120 extern LZMA_API(uint64_t)
121 lzma_easy_decoder_memusage(uint32_t preset)
124 if (easy_set_filters(&coder, preset))
127 return lzma_raw_decoder_memusage(coder.filters);