update to zstd 1.1.0

- also reverted multithreading encoder (it is in testing)
This commit is contained in:
Tino Reichardt
2016-09-29 20:32:33 +02:00
parent 0e6c1206d2
commit 46d329631a
19 changed files with 13359 additions and 255 deletions

View File

@@ -4,61 +4,29 @@
#include "StdAfx.h"
#include "ZstdEncoder.h"
#include <stdio.h>
#ifndef EXTRACT_ONLY
struct MyStream {
ISequentialInStream *inStream;
ISequentialOutStream *outStream;
ICompressProgressInfo *progress;
UInt64 *processedIn;
UInt64 *processedOut;
};
int MyRead(void *arg, ZSTDMT_Buffer * in)
{
struct MyStream *x = (struct MyStream*)arg;
size_t size = static_cast < size_t > (in->size);
//_props._level = static_cast < Byte > (prop.ulVal);
HRESULT res = ReadStream(x->inStream, in->buf, &size);
if (res != 0)
return -1;
*x->processedIn += size;
x->progress->SetRatioInfo(x->processedIn, x->processedOut);
in->size = static_cast < int > (size);
return S_OK;
}
int MyWrite(void *arg, ZSTDMT_Buffer * out)
{
struct MyStream *x = (struct MyStream*)arg;
HRESULT res = WriteStream(x->outStream, out->buf, out->size);
if (res != 0)
return -1;
*x->processedOut += out->size;
x->progress->SetRatioInfo(x->processedIn, x->processedOut);
return S_OK;
}
namespace NCompress {
namespace NZSTD {
CEncoder::CEncoder():
_cstream(NULL),
_buffIn(NULL),
_buffOut(NULL),
_buffInSize(0),
_buffOutSize(0),
_processedIn(0),
_processedOut(0),
_inputSize(0),
_numThreads(1)
_processedOut(0)
{
_props.clear();
}
CEncoder::~CEncoder()
{
if (_cstream)
ZSTD_freeCStream(_cstream);
MyFree(_buffIn);
MyFree(_buffOut);
}
STDMETHODIMP CEncoder::SetCoderProperties(const PROPID * propIDs, const PROPVARIANT * coderProps, UInt32 numProps)
@@ -69,7 +37,6 @@ STDMETHODIMP CEncoder::SetCoderProperties(const PROPID * propIDs, const PROPVARI
{
const PROPVARIANT & prop = coderProps[i];
PROPID propID = propIDs[i];
UInt32 v = (UInt32)prop.ulVal;
switch (propID)
{
case NCoderPropID::kLevel:
@@ -85,11 +52,6 @@ STDMETHODIMP CEncoder::SetCoderProperties(const PROPID * propIDs, const PROPVARI
break;
}
case NCoderPropID::kNumThreads:
{
SetNumberOfThreads(v);
break;
}
default:
{
break;
@@ -112,67 +74,73 @@ STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UInt64 * /* inSize */ ,
const UInt64 * /* outSize */ , ICompressProgressInfo *progress)
{
ZSTDMT_RdWr_t rdwr;
int ret;
size_t result;
struct MyStream Rd;
Rd.progress = progress;
Rd.inStream = inStream;
Rd.processedIn = &_processedIn;
Rd.processedOut = &_processedOut;
/* init only once in beginning */
if (!_cstream) {
struct MyStream Wr;
Wr.progress = progress;
Wr.outStream = outStream;
Wr.processedIn = &_processedIn;
Wr.processedOut = &_processedOut;
/* allocate stream */
_cstream = ZSTD_createCStream();
if (!_cstream)
return E_OUTOFMEMORY;
/* 1) setup read/write functions */
rdwr.fn_read = ::MyRead;
rdwr.fn_write = ::MyWrite;
rdwr.arg_read = (void *)&Rd;
rdwr.arg_write = (void *)&Wr;
/* allocate buffers */
_buffInSize = ZSTD_CStreamInSize();
_buffIn = MyAlloc(_buffInSize);
if (!_buffIn)
return E_OUTOFMEMORY;
/* 2) create compression context */
ZSTDMT_CCtx *ctx = ZSTDMT_createCCtx(_numThreads, _props._level, _inputSize);
if (!ctx)
return S_FALSE;
// perror_exit("Allocating ctx failed!");
_buffOutSize = ZSTD_CStreamOutSize();
_buffOut = MyAlloc(_buffOutSize);
if (!_buffOut)
return E_OUTOFMEMORY;
}
/* 3) compress */
ret = ZSTDMT_CompressCCtx(ctx, &rdwr);
if (ret == -1)
return S_FALSE;
// perror_exit("ZSTDMT_CompressCCtx() failed!");
/* init or re-init stream */
result = ZSTD_initCStream(_cstream, _props._level);
if (ZSTD_isError(result))
return S_FALSE;
/* 4) free resources */
ZSTDMT_freeCCtx(ctx);
return S_OK;
UInt32 read, toRead = static_cast < UInt32 > (_buffInSize);
for(;;) {
/* read input */
RINOK(inStream->Read(_buffIn, toRead, &read));
size_t InSize = static_cast < size_t > (read);
_processedIn += InSize;
if (InSize == 0) {
/* @eof */
ZSTD_outBuffer output = { _buffOut, _buffOutSize, 0 };
result = ZSTD_endStream(_cstream, &output);
if (ZSTD_isError(result))
return S_FALSE;
if (output.pos) {
/* write last compressed bytes and update progress */
RINOK(WriteStream(outStream, _buffOut, output.pos));
_processedOut += output.pos;
RINOK(progress->SetRatioInfo(&_processedIn, &_processedOut));
}
return S_OK;
}
/* compress input */
ZSTD_inBuffer input = { _buffIn, InSize, 0 };
while (input.pos < input.size) {
ZSTD_outBuffer output = { _buffOut, _buffOutSize, 0 };
result = ZSTD_compressStream(_cstream, &output , &input);
if (ZSTD_isError(result))
return S_FALSE;
/* write compressed stream and update progress */
RINOK(WriteStream(outStream, _buffOut, output.pos));
_processedOut += output.pos;
RINOK(progress->SetRatioInfo(&_processedIn, &_processedOut));
}
}
}
STDMETHODIMP CEncoder::SetNumberOfThreads(UInt32 numThreads)
{
const UInt32 kNumThreadsMax = 128;
if (numThreads < 1) numThreads = 1;
if (numThreads > kNumThreadsMax) numThreads = kNumThreadsMax;
_numThreads = numThreads;
return S_OK;
}
HRESULT CEncoder::ErrorOut(size_t code)
{
const char *strError = ZSTD_getErrorName(code);
size_t strErrorLen = strlen(strError) + 1;
wchar_t *wstrError = (wchar_t *)MyAlloc(sizeof(wchar_t) * strErrorLen);
if (!wstrError)
return E_FAIL;
mbstowcs(wstrError, strError, strErrorLen - 1);
MessageBoxW(0, wstrError, L"7-Zip ZStandard", MB_ICONERROR | MB_OK);
MyFree(wstrError);
return E_FAIL;
}
}}
#endif

View File

@@ -4,7 +4,6 @@
#define ZSTD_STATIC_LINKING_ONLY
#include "../../../C/Alloc.h"
#include "../../../C/ZStd/zstd.h"
#include "../../../C/ZStd/zstdmt.h"
#include "../../Common/Common.h"
#include "../../Common/MyCom.h"
@@ -40,22 +39,21 @@ class CEncoder:
{
CProps _props;
ZSTD_CStream *_cstream;
void *_buffIn;
void *_buffOut;
size_t _buffInSize;
size_t _buffOutSize;
UInt64 _processedIn;
UInt64 _processedOut;
UInt32 _inputSize;
UInt32 _numThreads;
int MyRead(void *arg, ZSTDMT_Buffer * in);
int MyWrite(void *arg, ZSTDMT_Buffer * out);
HRESULT CEncoder::ErrorOut(size_t code);
HRESULT CreateCompressor();
public:
MY_UNKNOWN_IMP2 (ICompressSetCoderProperties, ICompressWriteCoderProperties)
STDMETHOD (Code) (ISequentialInStream *inStream, ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
STDMETHOD (SetCoderProperties) (const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
STDMETHOD (WriteCoderProperties) (ISequentialOutStream *outStream);
STDMETHODIMP CEncoder::SetNumberOfThreads(UInt32 numThreads);
CEncoder();
virtual ~CEncoder();