Skip to content

Commit 9906093

Browse files
committed
mkclean: add support for Zstd compression via new --compalgo option
1 parent 0460ed1 commit 9906093

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

mkclean/mkclean.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,7 @@ int main(int argc, const char *argv[])
14151415
mkv_timestamp_t PrevTimestamp;
14161416
bool_t CuesChanged;
14171417
bool_t KeepCues = 0, Remux = 0, CuesCreated = 0, Optimize = 0, OptimizeVideo = 1, UnOptimize = 0, ClustersNeedRead = 0, Regression = 0;
1418+
MatroskaTrackEncodingCompAlgo CompressionAlgo = MATROSKA_TRACK_ENCODING_COMP_ZLIB;
14181419
int InputPathIndex = 1;
14191420
int64_t TimestampScale = 0, OldTimestampScale;
14201421
size_t MaxTrackNum = 0;
@@ -1521,6 +1522,26 @@ int main(int argc, const char *argv[])
15211522
else if (tcsisame_ascii(Path,T("--unsafe"))) { Unsafe = 1; InputPathIndex = i+1; }
15221523
else if (tcsisame_ascii(Path,T("--optimize"))) { Optimize = 1; OptimizeVideo = 1; InputPathIndex = i+1; }
15231524
else if (tcsisame_ascii(Path,T("--optimize_nv"))) { Optimize = 1; OptimizeVideo = 0; InputPathIndex = i+1; }
1525+
else if (tcsisame_ascii(Path,T("--compalgo")) && i+1<argc-1)
1526+
{
1527+
#if defined(TARGET_WIN) && defined(UNICODE)
1528+
Node_FromWcs(&p,Path,TSIZEOF(Path),argv[++i]);
1529+
#else
1530+
Node_FromStr(&p,Path,TSIZEOF(Path),argv[++i]);
1531+
#endif
1532+
if (tcsisame_ascii(Path,T("0")))
1533+
CompressionAlgo = MATROSKA_TRACK_ENCODING_COMP_ZLIB;
1534+
else if (tcsisame_ascii(Path,T("4")))
1535+
CompressionAlgo = MATROSKA_TRACK_ENCODING_COMP_ZSTD;
1536+
else
1537+
{
1538+
TextPrintf(StdErr,T("Unknown compression algorithm %s (0: zlib, 4: Zstd)\r\n"),Path);
1539+
Path[0] = 0;
1540+
Result = -8;
1541+
goto exit;
1542+
}
1543+
InputPathIndex = i+1;
1544+
}
15241545
else if (tcsisame_ascii(Path,T("--regression"))) { Regression = 1; InputPathIndex = i+1; }
15251546
else if (tcsisame_ascii(Path,T("--no-optimize"))) { UnOptimize = 1; InputPathIndex = i+1; }
15261547
else if (tcsisame_ascii(Path,T("--quiet"))) { Quiet = 1; InputPathIndex = i+1; }
@@ -1548,6 +1569,7 @@ int main(int argc, const char *argv[])
15481569
TextWrite(StdErr,T(" --live the output file resembles a live stream\r\n"));
15491570
TextWrite(StdErr,T(" --timecodescale <v> force the global TimestampScale to <v> (1000000 is a good value)\r\n"));
15501571
TextWrite(StdErr,T(" --unsafe don't output elements that are used for file recovery (saves more space)\r\n"));
1572+
TextWrite(StdErr,T(" --compalgo <v> force compression algorithm of all tracks\r\n"));
15511573
TextWrite(StdErr,T(" --optimize use all possible optimization for the output file\r\n"));
15521574
TextWrite(StdErr,T(" --optimize_nv use all possible optimization for the output file, except video tracks\r\n"));
15531575
TextWrite(StdErr,T(" --no-optimize disable some optimization for the output file\r\n"));
@@ -2706,13 +2728,24 @@ int main(int argc, const char *argv[])
27062728
if (CodecPrivate!=NULL)
27072729
{
27082730
size_t ExtraCompHeaderBytes = (encoding == MATROSKA_TRACK_ENCODING_COMP_NONE) ? 13 : 3; // extra bytes needed to add the comp header to the track
2731+
if (CompressionAlgo != MATROSKA_TRACK_ENCODING_COMP_ZLIB)
2732+
ExtraCompHeaderBytes += 1; // extra for the non default algo
27092733
size_t CompressedSize = (size_t)EBML_ElementDataSize((ebml_element*)CodecPrivate, 1);
27102734
size_t origCompressedSize = CompressedSize;
27112735
uint8_t *Compressed = malloc(CompressedSize);
2712-
if (CompressFrameZLib(EBML_BinaryGetData(CodecPrivate), origCompressedSize, &Compressed, &CompressedSize)==ERR_NONE
2713-
&& (CompressedSize + ExtraCompHeaderBytes) < origCompressedSize)
2736+
uint8_t *NewCompressed = Compressed;
2737+
int comp_err;
2738+
if (CompressionAlgo == MATROSKA_TRACK_ENCODING_COMP_ZSTD)
2739+
{
2740+
comp_err = CompressFrameZstd(EBML_BinaryGetData(CodecPrivate), origCompressedSize, &NewCompressed, &CompressedSize);
2741+
}
2742+
else
27142743
{
2715-
encoding = MATROSKA_TRACK_ENCODING_COMP_ZLIB;
2744+
comp_err = CompressFrameZLib(EBML_BinaryGetData(CodecPrivate), origCompressedSize, &NewCompressed, &CompressedSize);
2745+
}
2746+
if (comp_err==ERR_NONE && (CompressedSize + ExtraCompHeaderBytes) < origCompressedSize)
2747+
{
2748+
encoding = CompressionAlgo;
27162749
compress_scope |= MATROSKA_CONTENTENCODINGSCOPE_PRIVATE;
27172750
}
27182751
free(Compressed);
@@ -2727,6 +2760,12 @@ int main(int argc, const char *argv[])
27272760
if (MATROSKA_TrackSetCompressionAlgo((matroska_trackentry*)RLevel1, compress_scope,DstProfile, MATROSKA_TRACK_ENCODING_COMP_ZLIB))
27282761
ClustersNeedRead = 1;
27292762
break;
2763+
#if defined(CONFIG_ZSTD)
2764+
case MATROSKA_TRACK_ENCODING_COMP_ZSTD:
2765+
if (MATROSKA_TrackSetCompressionAlgo((matroska_trackentry*)RLevel1, compress_scope,DstProfile, MATROSKA_TRACK_ENCODING_COMP_ZSTD))
2766+
ClustersNeedRead = 1;
2767+
break;
2768+
#endif
27302769
case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
27312770
if (!HeaderData || MATROSKA_TrackSetCompressionHeader((matroska_trackentry*)RLevel1, ARRAYBEGIN(*HeaderData,uint8_t), ARRAYCOUNT(*HeaderData,uint8_t), DstProfile))
27322771
ClustersNeedRead = 1;

0 commit comments

Comments
 (0)