Skip to content

Commit

Permalink
mkclean: add support for Zstd compression via new --compalgo option
Browse files Browse the repository at this point in the history
  • Loading branch information
robUx4 committed Dec 30, 2024
1 parent 0460ed1 commit 9906093
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions mkclean/mkclean.c
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,7 @@ int main(int argc, const char *argv[])
mkv_timestamp_t PrevTimestamp;
bool_t CuesChanged;
bool_t KeepCues = 0, Remux = 0, CuesCreated = 0, Optimize = 0, OptimizeVideo = 1, UnOptimize = 0, ClustersNeedRead = 0, Regression = 0;
MatroskaTrackEncodingCompAlgo CompressionAlgo = MATROSKA_TRACK_ENCODING_COMP_ZLIB;
int InputPathIndex = 1;
int64_t TimestampScale = 0, OldTimestampScale;
size_t MaxTrackNum = 0;
Expand Down Expand Up @@ -1521,6 +1522,26 @@ int main(int argc, const char *argv[])
else if (tcsisame_ascii(Path,T("--unsafe"))) { Unsafe = 1; InputPathIndex = i+1; }
else if (tcsisame_ascii(Path,T("--optimize"))) { Optimize = 1; OptimizeVideo = 1; InputPathIndex = i+1; }
else if (tcsisame_ascii(Path,T("--optimize_nv"))) { Optimize = 1; OptimizeVideo = 0; InputPathIndex = i+1; }
else if (tcsisame_ascii(Path,T("--compalgo")) && i+1<argc-1)
{
#if defined(TARGET_WIN) && defined(UNICODE)
Node_FromWcs(&p,Path,TSIZEOF(Path),argv[++i]);
#else
Node_FromStr(&p,Path,TSIZEOF(Path),argv[++i]);
#endif
if (tcsisame_ascii(Path,T("0")))
CompressionAlgo = MATROSKA_TRACK_ENCODING_COMP_ZLIB;
else if (tcsisame_ascii(Path,T("4")))
CompressionAlgo = MATROSKA_TRACK_ENCODING_COMP_ZSTD;
else
{
TextPrintf(StdErr,T("Unknown compression algorithm %s (0: zlib, 4: Zstd)\r\n"),Path);
Path[0] = 0;
Result = -8;
goto exit;
}
InputPathIndex = i+1;
}
else if (tcsisame_ascii(Path,T("--regression"))) { Regression = 1; InputPathIndex = i+1; }
else if (tcsisame_ascii(Path,T("--no-optimize"))) { UnOptimize = 1; InputPathIndex = i+1; }
else if (tcsisame_ascii(Path,T("--quiet"))) { Quiet = 1; InputPathIndex = i+1; }
Expand Down Expand Up @@ -1548,6 +1569,7 @@ int main(int argc, const char *argv[])
TextWrite(StdErr,T(" --live the output file resembles a live stream\r\n"));
TextWrite(StdErr,T(" --timecodescale <v> force the global TimestampScale to <v> (1000000 is a good value)\r\n"));
TextWrite(StdErr,T(" --unsafe don't output elements that are used for file recovery (saves more space)\r\n"));
TextWrite(StdErr,T(" --compalgo <v> force compression algorithm of all tracks\r\n"));
TextWrite(StdErr,T(" --optimize use all possible optimization for the output file\r\n"));
TextWrite(StdErr,T(" --optimize_nv use all possible optimization for the output file, except video tracks\r\n"));
TextWrite(StdErr,T(" --no-optimize disable some optimization for the output file\r\n"));
Expand Down Expand Up @@ -2706,13 +2728,24 @@ int main(int argc, const char *argv[])
if (CodecPrivate!=NULL)
{
size_t ExtraCompHeaderBytes = (encoding == MATROSKA_TRACK_ENCODING_COMP_NONE) ? 13 : 3; // extra bytes needed to add the comp header to the track
if (CompressionAlgo != MATROSKA_TRACK_ENCODING_COMP_ZLIB)
ExtraCompHeaderBytes += 1; // extra for the non default algo
size_t CompressedSize = (size_t)EBML_ElementDataSize((ebml_element*)CodecPrivate, 1);
size_t origCompressedSize = CompressedSize;
uint8_t *Compressed = malloc(CompressedSize);
if (CompressFrameZLib(EBML_BinaryGetData(CodecPrivate), origCompressedSize, &Compressed, &CompressedSize)==ERR_NONE
&& (CompressedSize + ExtraCompHeaderBytes) < origCompressedSize)
uint8_t *NewCompressed = Compressed;
int comp_err;
if (CompressionAlgo == MATROSKA_TRACK_ENCODING_COMP_ZSTD)
{
comp_err = CompressFrameZstd(EBML_BinaryGetData(CodecPrivate), origCompressedSize, &NewCompressed, &CompressedSize);
}
else
{
encoding = MATROSKA_TRACK_ENCODING_COMP_ZLIB;
comp_err = CompressFrameZLib(EBML_BinaryGetData(CodecPrivate), origCompressedSize, &NewCompressed, &CompressedSize);
}
if (comp_err==ERR_NONE && (CompressedSize + ExtraCompHeaderBytes) < origCompressedSize)
{
encoding = CompressionAlgo;
compress_scope |= MATROSKA_CONTENTENCODINGSCOPE_PRIVATE;
}
free(Compressed);
Expand All @@ -2727,6 +2760,12 @@ int main(int argc, const char *argv[])
if (MATROSKA_TrackSetCompressionAlgo((matroska_trackentry*)RLevel1, compress_scope,DstProfile, MATROSKA_TRACK_ENCODING_COMP_ZLIB))
ClustersNeedRead = 1;
break;
#if defined(CONFIG_ZSTD)
case MATROSKA_TRACK_ENCODING_COMP_ZSTD:
if (MATROSKA_TrackSetCompressionAlgo((matroska_trackentry*)RLevel1, compress_scope,DstProfile, MATROSKA_TRACK_ENCODING_COMP_ZSTD))
ClustersNeedRead = 1;
break;
#endif
case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
if (!HeaderData || MATROSKA_TrackSetCompressionHeader((matroska_trackentry*)RLevel1, ARRAYBEGIN(*HeaderData,uint8_t), ARRAYCOUNT(*HeaderData,uint8_t), DstProfile))
ClustersNeedRead = 1;
Expand Down

0 comments on commit 9906093

Please sign in to comment.