Content
ffmpeg-tool supports several video lossless codecs (e.g. for medical imaging applications), implemented from de-facto standards, one of such codecs is FFV1.
Note: the full version of ffmpeg binary is here.
According to Lossless Video Codec comparisons, most of lossless codecs are too complex, most balanced codec is FFV1 and HuffYUV:
Another comparison reveals that FFV1 is comparable to lossless H264/AVC (CAVLC):
taken from the paper “COMPARISON OF LOSSLESS VIDEO CODECS FOR CROWD-BASED QUALITY ASSESSMENT ON TABLETS”, by Christian Keimel, Christopher Pangerl and Klaus Diepold
Note: H264 encoders (e.g. x264) and HEVC encoders (e.g. x265) supports lossless encoding (e.g. see here), but the input must be YCbCr and if you have RGB24 input you have to convert RGB24 into YCbCr, the conversion process adds rounding errors.
The process RGB → YCRCB →Lossless Encode → Decode to YCRCB → RGB is not exactly lossless and not exactly reversible.
The spec of FFV1 is here. FFV1 combines intra prediction with Entropy Coding, there are two entropy coding modes:
- Golomb-Rice variable run length code
- Arithmetic code
Typical encoding command:
ffmpeg -video_size 384x320 -i test.yuv -c:v ffv1 -context 1 -coder 0 -y test_lossless.mkv
where
-context 1
take greater context
-coder 0
Golomb-Rice VLC
To run FFV1 encoding in parallel it’s recommended to use '-slices'
(legal values of slices: 4, 6, 9, 12, 16, 24, 30)
ffmpeg -video_size 384x320 -i test.yuv -c:v ffv1 -context 1 -coder 0 -slices 4 -y test_lossless_slices4.mkv
With PowerShall’s Measure-Command i collected encoding times with ‘-slices 4’ and without:
- Without slicing encoding time is 1955 ms and with
'-slices 4'
the time is 748 ms - Penalty in bit-size of using
'-slices 4'
is about 0.2%
FFV1 compressed files in mkv-container can be viewed with VLC, Pot Player, ffplay and perhaps by others viewers.
The lossless video standard HuffYUV is supported by ffmpeg as ‘huffyuv’. HuffYUV was proposed by Ben Rudiak-Gould, it combines an intra-frame prediction with Huffman entropy coding of the residuals. There are three intra prediction modes: left, gradient and median.
ffmpeg -video_size 1920x1080 -i crowdrun1080p50fps.yuv -c:v huffyuv -pred 2 -y crowd_huffyuv.mkv
-pred 2
means median intra-prediction, '-pred 0
‘ means the left prediction.
x264 installed in ffmpeg supports the lossless encoding mode by setting ‘-crf 0
‘.
ffmpeg -video_size 1920x1080 -i crowdrun1080p50fps.yuv -c:v libx264 -crf 0 -vframes 100 -y crowd_ls.h264
Note: there is a subtle point to mention, the output h264 stream is in High 4:4:4 profile (I and P frames only), if the input yuv sequence is 4:2:0 and output sequence is 4:4:4 then the conversion from 4:2:0 to 4:4:4 is performed.
23+ years’ programming and theoretical experience in the computer science fields such as video compression, media streaming and artificial intelligence (co-author of several papers and patents).
the author is looking for new job, my resume