FFV1 Video Codec Specification by Michael Niedermayer Table of Contents 1 Introduction The FFV1 video codec is a simple and efficient lossless intra-frame only codec. The latest version of this document is available at https://raw.github.com/FFmpeg/FFV1/master/ffv1.lyx This document assumes familiarity with mathematical and coding concepts such as Range coding and YCbCr colorspaces. 2 Terms and Definitions ESC Escape symbol to indicate that the symbol to be stored is too large for normal storage and a different method is used to store it. MSB Most significant bit, the bit that can cause the largest change in magnitude of the symbol RCT Reversible Color Transform, a near linear, exactly reversible integer transform that converts between RGB and YCbCr representations of a sample. VLC Variable length code 3 General Description Each frame is split in 1 to 4 planes (Y, Cb, Cr, Alpha). In the case of the normal YCbCr colorspace the Y plane is coded first followed by the Cb and Cr planes, if an Alpha/transparency plane exists, it is coded last. In the case of the JPEG2000-RCT colorspace the lines are interleaved to improve caching efficiency since it is most likely that the RCT will immediately be converted to RGB during decoding; the interleaved coding order is also Y,Cb,Cr,Alpha. Samples within a plane are coded in raster scan order (left->right, top->bottom). Each sample is predicted by the median predictor from samples in the same plane and the difference is stored see[sub:Coding-of-the-sample-difference]. 3.1 Border For the purpose of the predictior and context, samples above the coded slice are assumed to be 0; samples to the right of the coded slice are identical to the closest left sample; samples to the left of the coded slice are identical to the top right sample (if there is one), otherwise 0. +----+---++----+----+---++---+ | 0 | 0 || 0 | 0 | 0 || 0 | +----+---++----+----+---++---+ | 0 | 0 || 0 | 0 | 0 || 0 | +----+---++----+----+---++---+ +----+---++----+----+---++---+ | 0 | 0 || a | b | c || c | +----+---++----+----+---++---+ | 0 | a || d | | e || e | +----+---++----+----+---++---+ | 0 | d || f | g | h || h | +----+---++----+----+---++---+ 3.2 Median predictor median(left, top, left + top - diag) left, top, diag are the left, top and left-top samples Note, this is also used in JPEG-LS and HuffYuv[JPEGLS, HuffYuv]. 3.3 Context +----+-----+----+----+ | | | T | | +----+-----+----+----+ | | tl | t | tr | +----+-----+---------- | L | l | X +----+----- The quantized sample differences L-l, l-tl, tl-t, t-T, t-tr are used as context: context=Q_{0}[l-tl]+\left|Q_{0}\right|(Q_{1}[tl-t]+\left|Q_{1}\right|(Q_{2}[t-tr]+\left|Q_{2}\right|(Q_{3}[L-l]+\left|Q_{3}\right|Q_{4}[T-t]))) If the context is smaller than 0 then -context is used and the difference between the sample and its predicted value is encoded with a flipped sign. 3.4 Quantization There are 5 quantization tables for the 5 sample differences, both the number of quantization steps and their distribution are stored in the bitstream. Each quantization table has exactly 256 entries, and the 8 least significant bits of the sample difference are used as index: Q_{i}[a-b]=Table_{i}[(a-b)\&255] 3.5 Colorspace 3.5.1 JPEG2000-RCT Cb=b-g Cr=r-g Y=g+(Cb+Cr)>>2 g=Y-(Cb+Cr)>>2 r=Cr+g b=Cb+g [JPEG2000] 3.6 Coding of the sample difference Instead of coding the n+1 bits of the sample difference with Huffman-, or Range coding (or n+2 bits, in the case of RCT), only the n (or n+1) least significant bits are used, since this is sufficient to recover the original sample. In the equation below, the term “bits” represents bits_per_raw_sample+1 for RCT or bits_per_raw_sample otherwise: coder\_input=\left[\left(sample\_difference+2^{bits-1}\right)\&\left(2^{bits}-1\right)\right]-2^{bits-1} 3.6.1 Range coding mode Early experimental versions of FFV1 used the CABAC Arithmetic coder from H.264[H264], but due to the uncertain patent/royality situation, as well as its slightly worse performance, CABAC was replaced by a range coder based on an algorithm defined by G. Nigel N. Martin in 1979 [RangeCoder]. Binary values To encode binary digits efficiently a range coder is used. C_{i} is the i-th Context. B_{i} is the i-th byte of the bytestream. b_{i} is the i-th range coded binary value, S_{0,i} is the i-th initial state, which is 128. The length of the bytestream encoding n binary symbols is j_{n} bytes. r_{i}=\left\lfloor \frac{R_{i}S_{i,C_{i}}}{2^{8}}\right\rfloor \begin{array}{ccccccccc} S_{i+1,C_{i}}=zero\_state_{S_{i,C_{i}}} & \wedge & l{}_{i}=L_{i} & \wedge & t_{i}=R_{i}-r_{i} & \Longleftarrow & b_{i}=0 & \Longleftrightarrow & L_{i}To encode scalar integers it would be possible to encode each bit separately and use the past bits as context. However that would mean 255 contexts per 8-bit symbol which is not only a waste of memory but also requires more past data to reach a reasonably good estimate of the probabilities. Alternatively assuming a Laplacian distribution and only dealing with its variance and mean (as in Huffman coding) would also be possible, however, for maximum flexibility and simplicity, the chosen method uses a single symbol to encode if a number is 0 and if not encodes the number using its exponent, mantissa and sign. The exact contexts used are best described by the following code, followed by some comments. void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed) { int i; put_rac(c, state+0, !v); if (v) { int a= ABS(v); int e= log2(a); for (i=0; i=0; i--) put_rac(c, state+22+MIN(i,9), (a>>i)&1); //22..31 if (is_signed) put_rac(c, state+11 + MIN(e, 10), v < 0); //11..21 } } Initial values for the context model At keyframes all range coder state variables are set to their initial state. State transition table one\_state_{i}=default\_state\_transition_{i}+state\_transition\_delta_{i} zero\_state_{i}=256-one\_state_{256-i} default_state_transition 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99,100,101,102,103, 104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118, 119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,133, 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149, 150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164, 165,166,167,168,169,170,171,171,172,173,174,175,176,177,178,179, 180,181,182,183,184,185,186,187,188,189,190,190,191,192,194,194, 195,196,197,198,199,200,201,202,202,204,205,206,207,208,209,209, 210,211,212,213,215,215,216,217,218,219,220,220,222,223,224,225, 226,227,227,229,229,230,231,232,234,234,235,236,237,238,239,240, 241,242,243,244,245,246,247,248,248, 0, 0, 0, 0, 0, 0, 0, alternative state transition table The alternative state transition table has been build using iterative minimization of frame sizes and generally performs better than the default. To use it, the coder_type has to be set to 2 and the difference to the default has to be stored in the header. The reference implemenation of FFV1 in FFmpeg uses this table by default at the time of this writing when Range coding is used. 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49, 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39, 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52, 53, 74, 55, 57, 58, 58, 74, 60,101, 61, 62, 84, 66, 66, 68, 69, 87, 82, 71, 97, 73, 73, 82, 75,111, 77, 94, 78, 87, 81, 83, 97, 85, 83, 94, 86, 99, 89, 90, 99,111, 92, 93,134, 95, 98,105, 98, 105,110,102,108,102,118,103,106,106,113,109,112,114,112,116,125, 115,116,117,117,126,119,125,121,121,123,145,124,126,131,127,129, 165,130,132,138,133,135,145,136,137,139,146,141,143,142,144,148, 147,155,151,149,151,150,152,157,153,154,156,168,158,162,161,160, 172,163,169,164,166,184,167,170,177,174,171,173,182,176,180,178, 175,189,179,181,186,183,192,185,200,187,191,188,190,197,193,196, 197,194,195,196,198,202,199,201,210,203,207,204,205,206,208,214, 209,211,221,212,213,215,224,216,217,218,219,220,222,228,223,225, 226,224,227,229,240,230,231,232,233,234,235,236,238,239,237,242, 241,243,242,244,245,246,247,248,249,250,251,252,252,253,254,255, 3.6.2 Huffman coding mode This coding mode uses golomb rice codes. The VLC code is split into 2 parts, the prefix stores the most significant bits, the suffix stores the k least significant bits or stores the whole number in the ESC case. The end of the bitstream (of the frame) is filled with 0-bits so that the bitstream contains a multiple of 8 bits. Prefix +-----------------+-------+ | bits | value | +-----------------+-------+ +-----------------+-------+ | 1 | 0 | +-----------------+-------+ | 01 | 1 | +-----------------+-------+ | ... | ... | +-----------------+-------+ | 0000 0000 0001 | 11 | +-----------------+-------+ | 0000 0000 0000 | ESC | +-----------------+-------+ Suffix non ESC the k least significant bits MSB first ESC the value - 11, in MSB first order, ESC may only be used if the value cannot be coded as non ESC Examples +------+------------------------+-------+ | k | bits | value | +------+------------------------+-------+ +------+------------------------+-------+ | 0 | 1 | 0 | +------+------------------------+-------+ | 0 | 001 | 2 | +------+------------------------+-------+ | 2 | 1 00 | 0 | +------+------------------------+-------+ | 2 | 1 10 | 2 | +------+------------------------+-------+ | 2 | 01 01 | 5 | +------+------------------------+-------+ | any | 000000000000 10000000 | 139 | +------+------------------------+-------+ Run mode Run mode is entered when the context is 0, and left as soon as a non-0 difference is found, the level is identical to the predicted one, the run and the first different level is coded. Run length coding The run value is encoded in 2 parts, the prefix part stores the more significant part of the run as well as adjusting the run_index which determines the number of bits in the less significant part of the run. The 2nd part of the value stores the less significant part of the run as it is. The run_index is reset for each plane and slice to 0. log2_run[41]={[JPEGLS]. 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9,10,11,12,13,14,15, 16,17,18,19,20,21,22,23, 24, }; if (run_count == 0 && run_mode == 1) { if (get_bits1()) { run_count = 1 << log2_run[run_index]; if (x + run_count <= w) run_index++; } else { if (log2_run[run_index]) run_count = get_bits(log2_run[run_index]); else run_count = 0; if (run_index) run_index--; run_mode = 2; } } Level coding Level coding is identical to the normal difference coding with the exception that the 0 value is removed as it cannot occur: if(diff>0) diff--; encode(diff); Note, this is different from JPEG-LS, which doesn't use prediction in run mode and uses a different encoding and context model for the last difference On a small set of test samples the use of prediction slightly improved the compression rate. 4 Bitstream b Range Coded 1-bit symbol v unsigned scalar symbol coded with the method described in [sub:Non-binary-values] s signed scalar symbol coded with the method described in [sub:Non-binary-values] 24BE 24bit big endian integer 32BE 32bit big endian integer The same context which is initialized to 128 is used for all fields in the header. 4.1 Frame +-----------------------------------+------+ | Frame { | type | +-----------------------------------+------+ | keyframe | b | +-----------------------------------+------+ | if (keyframe) { | | +-----------------------------------+------+ | if(version<2) | | +-----------------------------------+------+ | FrameHeader01 | | +-----------------------------------+------+ | } | | +-----------------------------------+------+ | for(i=0; i2) { | | +---------------------------------------+------+ | slice_x | v | +---------------------------------------+------+ | slice_y | v | +---------------------------------------+------+ | slice_width-1 | v | +---------------------------------------+------+ | slice_height-1 | v | +---------------------------------------+------+ | for(j=0; j2) | | +---------------------------------------+------+ | slice_size | 24BE | +---------------------------------------+------+ | if(ec){ | | +---------------------------------------+------+ | error_status | 8BE | +---------------------------------------+------+ | crc_parity | 32BE | +---------------------------------------+------+ | } | | +---------------------------------------+------+ | } | | +---------------------------------------+------+ slice_size Size of the slice in bytes, this allows finding the start of slices before previous slices have been fully decoded. And allows this way parallel decoding as well as error resilience. error_status 0(no error), 1(slice contained a correctable error), 2(slice contains a uncorrectable error) plane_count prior to version 4: without transparency: 2 else 3. version 4 and later: gray: 1, gray+alpha or color:2, color+alpha:3 4.3 Header 4.3.1 Version 0 and 1 +----------------------------------------+------+ | FrameHeader01 { | type | +----------------------------------------+------+ | version | v | +----------------------------------------+------+ | coder_type | v | +----------------------------------------+------+ | if(coder_type>1) | | +----------------------------------------+------+ | for(i=1; i<256; i++) | | +----------------------------------------+------+ | state_transition_delta[i] | s | +----------------------------------------+------+ | colorspace_type | v | +----------------------------------------+------+ | if(version>0) | | +----------------------------------------+------+ | bits_per_raw_sample | v | +----------------------------------------+------+ | chroma_planes | b | +----------------------------------------+------+ | log2(h_chroma_subsample) | v | +----------------------------------------+------+ | log2(v_chroma_subsample) | v | +----------------------------------------+------+ | alpha_plane | b | +----------------------------------------+------+ | QuantizationTables | | +----------------------------------------+------+ | } | | +----------------------------------------+------+ 4.3.2 Version 3 Version 2 and later files use a global header and a per frame header. +---------------------------------------------------+------+ | GlobalHeader { | type | +---------------------------------------------------+------+ | version | v | +---------------------------------------------------+------+ | micro_version | v | +---------------------------------------------------+------+ | coder_type | v | +---------------------------------------------------+------+ | if(coder_type>1) | | +---------------------------------------------------+------+ | for(i=1; i<256; i++) | | +---------------------------------------------------+------+ | state_transition_delta[i] | s | +---------------------------------------------------+------+ | colorspace_type | v | +---------------------------------------------------+------+ | bits_per_raw_sample | v | +---------------------------------------------------+------+ | chroma_planes | b | +---------------------------------------------------+------+ | log2(h_chroma_subsample) | v | +---------------------------------------------------+------+ | log2(v_chroma_subsample) | v | +---------------------------------------------------+------+ | alpha_plane | b | +---------------------------------------------------+------+ | num_h_slices-1 | v | +---------------------------------------------------+------+ | num_v_slices-1 | v | +---------------------------------------------------+------+ | quant_table_count | v | +---------------------------------------------------+------+ | for(i=0; i This text can be used under the GNU Free Documentation License or GNU General Public License. See http://www.gnu.org/licenses/fdl.txt .