idnits 2.17.1 draft-ietf-cellar-ffv1-v4-16.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- No issues found here. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (1 December 2020) is 1240 days in the past. Is this intentional? Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) -- Looks like a reference, but probably isn't: '41' on line 1121 ** Downref: Normative reference to an Informational RFC: RFC 4732 == Outdated reference: A later version (-20) exists of draft-ietf-cellar-ffv1-18 Summary: 1 error (**), 0 flaws (~~), 2 warnings (==), 2 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 cellar M. Niedermayer 3 Internet-Draft 4 Intended status: Standards Track D. Rice 5 Expires: 4 June 2021 6 J. Martinez 7 1 December 2020 9 FFV1 Video Coding Format Version 4 10 draft-ietf-cellar-ffv1-v4-16 12 Abstract 14 This document defines FFV1, a lossless intra-frame video encoding 15 format. FFV1 is designed to efficiently compress video data in a 16 variety of pixel formats. Compared to uncompressed video, FFV1 17 offers storage compression, frame fixity, and self-description, which 18 makes FFV1 useful as a preservation or intermediate video format. 20 Status of This Memo 22 This Internet-Draft is submitted in full conformance with the 23 provisions of BCP 78 and BCP 79. 25 Internet-Drafts are working documents of the Internet Engineering 26 Task Force (IETF). Note that other groups may also distribute 27 working documents as Internet-Drafts. The list of current Internet- 28 Drafts is at https://datatracker.ietf.org/drafts/current/. 30 Internet-Drafts are draft documents valid for a maximum of six months 31 and may be updated, replaced, or obsoleted by other documents at any 32 time. It is inappropriate to use Internet-Drafts as reference 33 material or to cite them other than as "work in progress." 35 This Internet-Draft will expire on 4 June 2021. 37 Copyright Notice 39 Copyright (c) 2020 IETF Trust and the persons identified as the 40 document authors. All rights reserved. 42 This document is subject to BCP 78 and the IETF Trust's Legal 43 Provisions Relating to IETF Documents (https://trustee.ietf.org/ 44 license-info) in effect on the date of publication of this document. 45 Please review these documents carefully, as they describe your rights 46 and restrictions with respect to this document. Code Components 47 extracted from this document must include Simplified BSD License text 48 as described in Section 4.e of the Trust Legal Provisions and are 49 provided without warranty as described in the Simplified BSD License. 51 Table of Contents 53 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 4 54 2. Notation and Conventions . . . . . . . . . . . . . . . . . . 4 55 2.1. Definitions . . . . . . . . . . . . . . . . . . . . . . . 5 56 2.2. Conventions . . . . . . . . . . . . . . . . . . . . . . . 5 57 2.2.1. Pseudo-code . . . . . . . . . . . . . . . . . . . . . 6 58 2.2.2. Arithmetic Operators . . . . . . . . . . . . . . . . 6 59 2.2.3. Assignment Operators . . . . . . . . . . . . . . . . 7 60 2.2.4. Comparison Operators . . . . . . . . . . . . . . . . 7 61 2.2.5. Mathematical Functions . . . . . . . . . . . . . . . 7 62 2.2.6. Order of Operation Precedence . . . . . . . . . . . . 8 63 2.2.7. Range . . . . . . . . . . . . . . . . . . . . . . . . 9 64 2.2.8. NumBytes . . . . . . . . . . . . . . . . . . . . . . 9 65 2.2.9. Bitstream Functions . . . . . . . . . . . . . . . . . 9 66 3. Sample Coding . . . . . . . . . . . . . . . . . . . . . . . . 9 67 3.1. Border . . . . . . . . . . . . . . . . . . . . . . . . . 10 68 3.2. Samples . . . . . . . . . . . . . . . . . . . . . . . . . 10 69 3.3. Median Predictor . . . . . . . . . . . . . . . . . . . . 11 70 3.3.1. Exception . . . . . . . . . . . . . . . . . . . . . . 11 71 3.4. Quantization Table Sets . . . . . . . . . . . . . . . . . 12 72 3.5. Context . . . . . . . . . . . . . . . . . . . . . . . . . 12 73 3.6. Quantization Table Set Indexes . . . . . . . . . . . . . 13 74 3.7. Color spaces . . . . . . . . . . . . . . . . . . . . . . 13 75 3.7.1. YCbCr . . . . . . . . . . . . . . . . . . . . . . . . 13 76 3.7.2. RGB . . . . . . . . . . . . . . . . . . . . . . . . . 14 77 3.8. Coding of the Sample Difference . . . . . . . . . . . . . 16 78 3.8.1. Range Coding Mode . . . . . . . . . . . . . . . . . . 16 79 3.8.2. Golomb Rice Mode . . . . . . . . . . . . . . . . . . 22 80 4. Bitstream . . . . . . . . . . . . . . . . . . . . . . . . . . 28 81 4.1. Quantization Table Set . . . . . . . . . . . . . . . . . 29 82 4.1.1. quant_tables . . . . . . . . . . . . . . . . . . . . 30 83 4.1.2. context_count . . . . . . . . . . . . . . . . . . . . 31 84 4.2. Parameters . . . . . . . . . . . . . . . . . . . . . . . 31 85 4.2.1. version . . . . . . . . . . . . . . . . . . . . . . . 33 86 4.2.2. micro_version . . . . . . . . . . . . . . . . . . . . 33 87 4.2.3. coder_type . . . . . . . . . . . . . . . . . . . . . 34 88 4.2.4. state_transition_delta . . . . . . . . . . . . . . . 35 89 4.2.5. colorspace_type . . . . . . . . . . . . . . . . . . . 35 90 4.2.6. chroma_planes . . . . . . . . . . . . . . . . . . . . 36 91 4.2.7. bits_per_raw_sample . . . . . . . . . . . . . . . . . 36 92 4.2.8. log2_h_chroma_subsample . . . . . . . . . . . . . . . 37 93 4.2.9. log2_v_chroma_subsample . . . . . . . . . . . . . . . 37 94 4.2.10. extra_plane . . . . . . . . . . . . . . . . . . . . . 37 95 4.2.11. num_h_slices . . . . . . . . . . . . . . . . . . . . 37 96 4.2.12. num_v_slices . . . . . . . . . . . . . . . . . . . . 38 97 4.2.13. quant_table_set_count . . . . . . . . . . . . . . . . 38 98 4.2.14. states_coded . . . . . . . . . . . . . . . . . . . . 38 99 4.2.15. initial_state_delta . . . . . . . . . . . . . . . . . 38 100 4.2.16. ec . . . . . . . . . . . . . . . . . . . . . . . . . 39 101 4.2.17. intra . . . . . . . . . . . . . . . . . . . . . . . . 39 102 4.3. Configuration Record . . . . . . . . . . . . . . . . . . 39 103 4.3.1. reserved_for_future_use . . . . . . . . . . . . . . . 40 104 4.3.2. configuration_record_crc_parity . . . . . . . . . . . 40 105 4.3.3. Mapping FFV1 into Containers . . . . . . . . . . . . 40 106 4.4. Frame . . . . . . . . . . . . . . . . . . . . . . . . . . 41 107 4.5. Slice . . . . . . . . . . . . . . . . . . . . . . . . . . 43 108 4.6. Slice Header . . . . . . . . . . . . . . . . . . . . . . 44 109 4.6.1. slice_x . . . . . . . . . . . . . . . . . . . . . . . 44 110 4.6.2. slice_y . . . . . . . . . . . . . . . . . . . . . . . 44 111 4.6.3. slice_width . . . . . . . . . . . . . . . . . . . . . 44 112 4.6.4. slice_height . . . . . . . . . . . . . . . . . . . . 45 113 4.6.5. quant_table_set_index_count . . . . . . . . . . . . . 45 114 4.6.6. quant_table_set_index . . . . . . . . . . . . . . . . 45 115 4.6.7. picture_structure . . . . . . . . . . . . . . . . . . 45 116 4.6.8. sar_num . . . . . . . . . . . . . . . . . . . . . . . 46 117 4.6.9. sar_den . . . . . . . . . . . . . . . . . . . . . . . 46 118 4.6.10. reset_contexts . . . . . . . . . . . . . . . . . . . 46 119 4.6.11. slice_coding_mode . . . . . . . . . . . . . . . . . . 46 120 4.7. Slice Content . . . . . . . . . . . . . . . . . . . . . . 47 121 4.7.1. primary_color_count . . . . . . . . . . . . . . . . . 47 122 4.7.2. plane_pixel_height . . . . . . . . . . . . . . . . . 47 123 4.7.3. slice_pixel_height . . . . . . . . . . . . . . . . . 48 124 4.7.4. slice_pixel_y . . . . . . . . . . . . . . . . . . . . 48 125 4.8. Line . . . . . . . . . . . . . . . . . . . . . . . . . . 48 126 4.8.1. plane_pixel_width . . . . . . . . . . . . . . . . . . 48 127 4.8.2. slice_pixel_width . . . . . . . . . . . . . . . . . . 49 128 4.8.3. slice_pixel_x . . . . . . . . . . . . . . . . . . . . 49 129 4.8.4. sample_difference . . . . . . . . . . . . . . . . . . 49 130 4.9. Slice Footer . . . . . . . . . . . . . . . . . . . . . . 49 131 4.9.1. slice_size . . . . . . . . . . . . . . . . . . . . . 50 132 4.9.2. error_status . . . . . . . . . . . . . . . . . . . . 50 133 4.9.3. slice_crc_parity . . . . . . . . . . . . . . . . . . 50 134 5. Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 50 135 6. Security Considerations . . . . . . . . . . . . . . . . . . . 51 136 7. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 51 137 7.1. Media Type Definition . . . . . . . . . . . . . . . . . . 51 139 8. Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . 53 140 9. Normative References . . . . . . . . . . . . . . . . . . . . 53 141 10. Informative References . . . . . . . . . . . . . . . . . . . 54 142 Appendix A. Multi-theaded decoder implementation suggestions . . 55 143 Appendix B. Future handling of some streams created by non 144 conforming encoders . . . . . . . . . . . . . . . . . . . 56 145 Appendix C. FFV1 Implementations . . . . . . . . . . . . . . . . 56 146 C.1. FFmpeg FFV1 Codec . . . . . . . . . . . . . . . . . . . . 56 147 C.2. FFV1 Decoder in Go . . . . . . . . . . . . . . . . . . . 56 148 C.3. MediaConch . . . . . . . . . . . . . . . . . . . . . . . 57 149 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 57 151 1. Introduction 153 This document describes FFV1, a lossless video encoding format. The 154 design of FFV1 considers the storage of image characteristics, data 155 fixity, and the optimized use of encoding time and storage 156 requirements. FFV1 is designed to support a wide range of lossless 157 video applications such as long-term audiovisual preservation, 158 scientific imaging, screen recording, and other video encoding 159 scenarios that seek to avoid the generational loss of lossy video 160 encodings. 162 This document defines a version 4 of FFV1. Prior versions of FFV1 163 are defined within [I-D.ietf-cellar-ffv1]. 165 This document assumes familiarity with mathematical and coding 166 concepts such as Range coding [range-coding] and YCbCr color spaces 167 [YCbCr]. 169 This specification describes the valid bitstream and how to decode 170 such valid bitstream. Bitstreams not conforming to this 171 specification or how they are handled is outside this specification. 172 A decoder could reject every invalid bitstream or attempt to perform 173 error concealment or re-download or use a redundant copy of the 174 invalid part or any other action it deems appropriate. 176 2. Notation and Conventions 178 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 179 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and 180 "OPTIONAL" in this document are to be interpreted as described in BCP 181 14 [RFC2119] [RFC8174] when, and only when, they appear in all 182 capitals, as shown here. 184 2.1. Definitions 186 "FFV1": choosen name of this video encoding format, short version of 187 "FF Video 1", the letters "FF" coming from "FFmpeg", the name of the 188 reference decoder, whose the first letters originaly means "Fast 189 Forward". 191 "Container": Format that encapsulates Frames (see Section 4.4) and 192 (when required) a "Configuration Record" into a bitstream. 194 "Sample": The smallest addressable representation of a color 195 component or a luma component in a Frame. Examples of Sample are 196 Luma (Y), Blue-difference Chroma (Cb), Red-difference Chroma (Cr), 197 Transparency, Red, Green, and Blue. 199 "Symbol": A value stored in the bitstream, which is defined and 200 decoded through one of the methods described in Table 4. 202 "Line": A discrete component of a static image composed of Samples 203 that represent a specific quantification of Samples of that image. 205 "Plane": A discrete component of a static image composed of Lines 206 that represent a specific quantification of Lines of that image. 208 "Pixel": The smallest addressable representation of a color in a 209 Frame. It is composed of one or more Samples. 211 "MSB": Most Significant Bit, the bit that can cause the largest 212 change in magnitude of the Symbol. 214 "VLC": Variable Length Code, a code that maps source symbols to a 215 variable number of bits. 217 "RGB": A reference to the method of storing the value of a Pixel by 218 using three numeric values that represent Red, Green, and Blue. 220 "YCbCr": A reference to the method of storing the value of a Pixel by 221 using three numeric values that represent the luma of the Pixel (Y) 222 and the chroma of the Pixel (Cb and Cr). YCbCr word is used for 223 historical reasons and currently references any color space relying 224 on 1 luma Sample and 2 chroma Samples, e.g. YCbCr, YCgCo or ICtCp. 225 The exact meaning of the three numeric values is unspecified. 227 "TBA": To Be Announced. Used in reference to the development of 228 future iterations of the FFV1 specification. 230 2.2. Conventions 231 2.2.1. Pseudo-code 233 The FFV1 bitstream is described in this document using pseudo-code. 234 Note that the pseudo-code is used for clarity in order to illustrate 235 the structure of FFV1 and not intended to specify any particular 236 implementation. The pseudo-code used is based upon the C programming 237 language [ISO.9899.2018] and uses its "if/else", "while" and "for" 238 keywords as well as functions defined within this document. 240 In some instances, pseudo-code is presented in a two-column format 241 such as shown in Figure 1. In this form the "type" column provides a 242 Symbol as defined in Table 4 that defines the storage of the data 243 referenced in that same line of pseudo-code. 245 pseudo-code | type 246 --------------------------------------------------------------|----- 247 ExamplePseudoCode( ) { | 248 value | ur 249 } | 251 Figure 1: A depiction of type-labelled pseudo-code used within 252 this document. 254 2.2.2. Arithmetic Operators 256 Note: the operators and the order of precedence are the same as used 257 in the C programming language [ISO.9899.2018], with the exception of 258 ">>" (removal of implementation defined behavior) and "^" (power 259 instead of XOR) operators which are re-defined within this section. 261 "a + b" means a plus b. 263 "a - b" means a minus b. 265 "-a" means negation of a. 267 "a * b" means a multiplied by b. 269 "a / b" means a divided by b. 271 "a ^ b" means a raised to the b-th power. 273 "a & b" means bit-wise "and" of a and b. 275 "a | b" means bit-wise "or" of a and b. 277 "a >> b" means arithmetic right shift of two's complement integer 278 representation of a by b binary digits. This is equivalent to 279 dividing a by 2, b times, with rounding toward negative infinity. 281 "a << b" means arithmetic left shift of two's complement integer 282 representation of a by b binary digits. 284 2.2.3. Assignment Operators 286 "a = b" means a is assigned b. 288 "a++" is equivalent to a is assigned a + 1. 290 "a--" is equivalent to a is assigned a - 1. 292 "a += b" is equivalent to a is assigned a + b. 294 "a -= b" is equivalent to a is assigned a - b. 296 "a *= b" is equivalent to a is assigned a * b. 298 2.2.4. Comparison Operators 300 "a > b" is true when a is greater than b. 302 "a >= b" is true when a is greater than or equal to b. 304 "a < b" is true when a is less than b. 306 "a <= b" is true when a is less than or equal b. 308 "a == b" is true when a is equal to b. 310 "a != b" is true when a is not equal to b. 312 "a && b" is true when both a is true and b is true. 314 "a || b" is true when either a is true or b is true. 316 "!a" is true when a is not true. 318 "a ? b : c" if a is true, then b, otherwise c. 320 2.2.5. Mathematical Functions 322 "floor(a)" means the largest integer less than or equal to a. 324 "ceil(a)" means the smallest integer greater than or equal to a. 326 "sign(a)" extracts the sign of a number, i.e. if a < 0 then -1, else 327 if a > 0 then 1, else 0. 329 "abs(a)" means the absolute value of a, i.e. "abs(a)" = "sign(a) * 330 a". 332 "log2(a)" means the base-two logarithm of a. 334 "min(a,b)" means the smaller of two values a and b. 336 "max(a,b)" means the larger of two values a and b. 338 "median(a,b,c)" means the numerical middle value in a data set of a, 339 b, and c, i.e. a+b+c-min(a,b,c)-max(a,b,c). 341 "A <== B" means B implies A. 343 "A <==> B" means A <== B , B <== A. 345 a_(b) means the b-th value of a sequence of a 347 a_(b,c) means the 'b,c'-th value of a sequence of a 349 2.2.6. Order of Operation Precedence 351 When order of precedence is not indicated explicitly by use of 352 parentheses, operations are evaluated in the following order (from 353 top to bottom, operations of same precedence being evaluated from 354 left to right). This order of operations is based on the order of 355 operations used in Standard C. 357 a++, a-- 358 !a, -a 359 a ^ b 360 a * b, a / b 361 a + b, a - b 362 a << b, a >> b 363 a < b, a <= b, a > b, a >= b 364 a == b, a != b 365 a & b 366 a | b 367 a && b 368 a || b 369 a ? b : c 370 a = b, a += b, a -= b, a *= b 372 2.2.7. Range 374 "a...b" means any value from a to b, inclusive. 376 2.2.8. NumBytes 378 "NumBytes" is a non-negative integer that expresses the size in 8-bit 379 octets of a particular FFV1 "Configuration Record" or "Frame". FFV1 380 relies on its Container to store the "NumBytes" values; see 381 Section 4.3.3. 383 2.2.9. Bitstream Functions 385 2.2.9.1. remaining_bits_in_bitstream 387 "remaining_bits_in_bitstream( NumBytes )" means the count of 388 remaining bits after the pointer in that "Configuration Record" or 389 "Frame". It is computed from the "NumBytes" value multiplied by 8 390 minus the count of bits of that "Configuration Record" or "Frame" 391 already read by the bitstream parser. 393 2.2.9.2. remaining_symbols_in_syntax 395 "remaining_symbols_in_syntax( )" is true as long as the RangeCoder 396 has not consumed all the given input bytes. 398 2.2.9.3. byte_aligned 400 "byte_aligned( )" is true if "remaining_bits_in_bitstream( NumBytes 401 )" is a multiple of 8, otherwise false. 403 2.2.9.4. get_bits 405 "get_bits( i )" is the action to read the next "i" bits in the 406 bitstream, from most significant bit to least significant bit, and to 407 return the corresponding value. The pointer is increased by "i". 409 3. Sample Coding 411 For each "Slice" (as described in Section 4.5) of a Frame, the 412 Planes, Lines, and Samples are coded in an order determined by the 413 color space (see Section 3.7). Each Sample is predicted by the 414 median predictor as described in Section 3.3 from other Samples 415 within the same Plane and the difference is stored using the method 416 described in Section 3.8. 418 3.1. Border 420 A border is assumed for each coded "Slice" for the purpose of the 421 median predictor and context according to the following rules: 423 * one column of Samples to the left of the coded slice is assumed as 424 identical to the Samples of the leftmost column of the coded slice 425 shifted down by one row. The value of the topmost Sample of the 426 column of Samples to the left of the coded slice is assumed to be 427 "0" 429 * one column of Samples to the right of the coded slice is assumed 430 as identical to the Samples of the rightmost column of the coded 431 slice 433 * an additional column of Samples to the left of the coded slice and 434 two rows of Samples above the coded slice are assumed to be "0" 436 Figure 2 depicts a slice of 9 Samples "a,b,c,d,e,f,g,h,i" in a 3x3 437 arrangement along with its assumed border. 439 +---+---+---+---+---+---+---+---+ 440 | 0 | 0 | | 0 | 0 | 0 | | 0 | 441 +---+---+---+---+---+---+---+---+ 442 | 0 | 0 | | 0 | 0 | 0 | | 0 | 443 +---+---+---+---+---+---+---+---+ 444 | | | | | | | | | 445 +---+---+---+---+---+---+---+---+ 446 | 0 | 0 | | a | b | c | | c | 447 +---+---+---+---+---+---+---+---+ 448 | 0 | a | | d | e | f | | f | 449 +---+---+---+---+---+---+---+---+ 450 | 0 | d | | g | h | i | | i | 451 +---+---+---+---+---+---+---+---+ 453 Figure 2: A depiction of FFV1's assumed border for a set example 454 Samples. 456 3.2. Samples 458 Relative to any Sample "X", six other relatively positioned Samples 459 from the coded Samples and presumed border are identified according 460 to the labels used in Figure 3. The labels for these relatively 461 positioned Samples are used within the median predictor and context. 463 +---+---+---+---+ 464 | | | T | | 465 +---+---+---+---+ 466 | |tl | t |tr | 467 +---+---+---+---+ 468 | L | l | X | | 469 +---+---+---+---+ 471 Figure 3: A depiction of how relatively positioned Samples are 472 referenced within this document. 474 The labels for these relative Samples are made of the first letters 475 of the words Top, Left and Right. 477 3.3. Median Predictor 479 The prediction for any Sample value at position "X" may be computed 480 based upon the relative neighboring values of "l", "t", and "tl" via 481 this equation: 483 median(l, t, l + t - tl) 485 Note, this prediction template is also used in [ISO.14495-1.1999] and 486 [HuffYUV]. 488 3.3.1. Exception 490 If "colorspace_type == 0 && bits_per_raw_sample == 16 && ( coder_type 491 == 1 || coder_type == 2 )" (see Section 4.2.5, Section 4.2.7 and 492 Section 4.2.3), the following median predictor MUST be used: 494 median(left16s, top16s, left16s + top16s - diag16s) 496 where: 498 left16s = l >= 32768 ? ( l - 65536 ) : l 499 top16s = t >= 32768 ? ( t - 65536 ) : t 500 diag16s = tl >= 32768 ? ( tl - 65536 ) : tl 502 Background: a two's complement 16-bit signed integer was used for 503 storing Sample values in all known implementations of FFV1 bitstream 504 (see Appendix C). So in some circumstances, the most significant bit 505 was wrongly interpreted (used as a sign bit instead of the 16th bit 506 of an unsigned integer). Note that when the issue was discovered, 507 the only configuration of all known implementations being impacted is 508 16-bit YCbCr with no Pixel transformation with Range Coder coder, as 509 other potentially impacted configurations (e.g. 15/16-bit 510 JPEG2000-RCT with Range Coder coder, or 16-bit content with Golomb 511 Rice coder) were implemented nowhere [ISO.15444-1.2016]. In the 512 meanwhile, 16-bit JPEG2000-RCT with Range Coder coder was implemented 513 without this issue in one implementation and validated by one 514 conformance checker. It is expected (to be confirmed) to remove this 515 exception for the median predictor in the next version of the FFV1 516 bitstream. 518 3.4. Quantization Table Sets 520 Quantization Tables are used on Sample Differences (see Section 3.8), 521 so Quantized Sample Differences are stored in the bitstream. 523 The FFV1 bitstream contains one or more Quantization Table Sets. 524 Each Quantization Table Set contains exactly 5 Quantization Tables 525 with each Quantization Table corresponding to one of the five 526 Quantized Sample Differences. For each Quantization Table, both the 527 number of quantization steps and their distribution are stored in the 528 FFV1 bitstream; each Quantization Table has exactly 256 entries, and 529 the 8 least significant bits of the Quantized Sample Difference are 530 used as index: 532 Q_(j)[k] = quant_tables[i][j][k&255] 534 Figure 4 536 In this formula, "i" is the Quantization Table Set index, "j" is the 537 Quantized Table index, "k" the Quantized Sample Difference. 539 3.5. Context 541 Relative to any Sample "X", the Quantized Sample Differences "L-l", 542 "l-tl", "tl-t", "T-t", and "t-tr" are used as context: 544 context = Q_(0)[l - tl] + 545 Q_(1)[tl - t] + 546 Q_(2)[t - tr] + 547 Q_(3)[L - l] + 548 Q_(4)[T - t] 550 Figure 5 552 If "context >= 0" then "context" is used and the difference between 553 the Sample and its predicted value is encoded as is, else "-context" 554 is used and the difference between the Sample and its predicted value 555 is encoded with a flipped sign. 557 3.6. Quantization Table Set Indexes 559 For each Plane of each slice, a Quantization Table Set is selected 560 from an index: 562 * For Y Plane, "quant_table_set_index[ 0 ]" index is used 564 * For Cb and Cr Planes, "quant_table_set_index[ 1 ]" index is used 566 * For extra Plane, "quant_table_set_index[ (version <= 3 || 567 chroma_planes) ? 2 : 1 ]" index is used 569 Background: in first implementations of FFV1 bitstream, the index for 570 Cb and Cr Planes was stored even if it is not used (chroma_planes set 571 to 0), this index is kept for "version" <= 3 in order to keep 572 compatibility with FFV1 bitstreams in the wild. 574 3.7. Color spaces 576 FFV1 supports several color spaces. The count of allowed coded 577 planes and the meaning of the extra Plane are determined by the 578 selected color space. 580 The FFV1 bitstream interleaves data in an order determined by the 581 color space. In YCbCr for each Plane, each Line is coded from top to 582 bottom and for each Line, each Sample is coded from left to right. 583 In JPEG2000-RCT for each Line from top to bottom, each Plane is coded 584 and for each Plane, each Sample is encoded from left to right. 586 3.7.1. YCbCr 588 This color space allows 1 to 4 Planes. 590 The Cb and Cr Planes are optional, but if used then MUST be used 591 together. Omitting the Cb and Cr Planes codes the frames in 592 grayscale without color data. 594 An optional transparency Plane can be used to code transparency data. 596 An FFV1 Frame using YCbCr MUST use one of the following arrangements: 598 * Y 600 * Y, Transparency 602 * Y, Cb, Cr 604 * Y, Cb, Cr, Transparency 605 The Y Plane MUST be coded first. If the Cb and Cr Planes are used 606 then they MUST be coded after the Y Plane. If a transparency Plane 607 is used, then it MUST be coded last. 609 3.7.2. RGB 611 This color space allows 3 or 4 Planes. 613 An optional transparency Plane can be used to code transparency data. 615 JPEG2000-RCT is a Reversible Color Transform that codes RGB (red, 616 green, blue) Planes losslessly in a modified YCbCr color space 617 [ISO.15444-1.2016]. Reversible Pixel transformations between YCbCr 618 and RGB use the following formulae. 620 Cb = b - g 621 Cr = r - g 622 Y = g + (Cb + Cr) >> 2 624 Figure 6: Description of the transformation of pixels from RGB 625 color space to coded modified YCbCr color space. 627 g = Y - (Cb + Cr) >> 2 628 r = Cr + g 629 b = Cb + g 631 Figure 7: Description of the transformation of pixels from coded 632 modified YCbCr color space to RGB color space. 634 Cb and Cr are positively offset by "1 << bits_per_raw_sample" after 635 the conversion from RGB to the modified YCbCr and are negatively 636 offseted by the same value before the conversion from the modified 637 YCbCr to RGB, in order to have only non-negative values after the 638 conversion. 640 When FFV1 uses the JPEG2000-RCT, the horizontal Lines are interleaved 641 to improve caching efficiency since it is most likely that the 642 JPEG2000-RCT will immediately be converted to RGB during decoding. 643 The interleaved coding order is also Y, then Cb, then Cr, and then, 644 if used, transparency. 646 As an example, a Frame that is two Pixels wide and two Pixels high, 647 could comprise the following structure: 649 +------------------------+------------------------+ 650 | Pixel(1,1) | Pixel(2,1) | 651 | Y(1,1) Cb(1,1) Cr(1,1) | Y(2,1) Cb(2,1) Cr(2,1) | 652 +------------------------+------------------------+ 653 | Pixel(1,2) | Pixel(2,2) | 654 | Y(1,2) Cb(1,2) Cr(1,2) | Y(2,2) Cb(2,2) Cr(2,2) | 655 +------------------------+------------------------+ 657 In JPEG2000-RCT, the coding order would be left to right and then top 658 to bottom, with values interleaved by Lines and stored in this order: 660 Y(1,1) Y(2,1) Cb(1,1) Cb(2,1) Cr(1,1) Cr(2,1) Y(1,2) Y(2,2) Cb(1,2) 661 Cb(2,2) Cr(1,2) Cr(2,2) 663 3.7.2.1. Exception 665 If "bits_per_raw_sample" is between 9 and 15 inclusive and 666 "extra_plane" is 0, the following formulae for reversible conversions 667 between YCbCr and RGB MUST be used instead of the ones above: 669 Cb = g - b 670 Cr = r - b 671 Y = b + (Cb + Cr) >> 2 673 Figure 8: Description of the transformation of pixels from RGB 674 color space to coded modified YCbCr color space (in case of 675 exception). 677 b = Y - (Cb + Cr) >> 2 678 r = Cr + b 679 g = Cb + b 681 Figure 9: Description of the transformation of pixels from coded 682 modified YCbCr color space to RGB color space (in case of 683 exception). 685 Background: At the time of this writing, in all known implementations 686 of FFV1 bitstream, when "bits_per_raw_sample" was between 9 and 15 687 inclusive and "extra_plane" is 0, GBR Planes were used as BGR Planes 688 during both encoding and decoding. In the meanwhile, 16-bit 689 JPEG2000-RCT was implemented without this issue in one implementation 690 and validated by one conformance checker. Methods to address this 691 exception for the transform are under consideration for the next 692 version of the FFV1 bitstream. 694 3.8. Coding of the Sample Difference 696 Instead of coding the n+1 bits of the Sample Difference with Huffman 697 or Range coding (or n+2 bits, in the case of JPEG2000-RCT), only the 698 n (or n+1, in the case of JPEG2000-RCT) least significant bits are 699 used, since this is sufficient to recover the original Sample. In 700 the equation below, the term "bits" represents "bits_per_raw_sample + 701 1" for JPEG2000-RCT or "bits_per_raw_sample" otherwise: 703 coder_input = ((sample_difference + 2 ^ (bits - 1)) & 704 (2 ^ bits - 1)) - 2 ^ (bits - 1) 706 Figure 10: Description of the coding of the Sample Difference in 707 the bitstream. 709 3.8.1. Range Coding Mode 711 Early experimental versions of FFV1 used the CABAC Arithmetic coder 712 from H.264 as defined in [ISO.14496-10.2014] but due to the uncertain 713 patent/royalty situation, as well as its slightly worse performance, 714 CABAC was replaced by a Range coder based on an algorithm defined by 715 G. Nigel N. Martin in 1979 [range-coding]. 717 3.8.1.1. Range Binary Values 719 To encode binary digits efficiently a Range coder is used. C_(i) is 720 the i-th Context. B_(i) is the i-th byte of the bytestream. b_(i) is 721 the i-th Range coded binary value, S_(0, i) is the i-th initial 722 state. The length of the bytestream encoding n binary symbols is 723 j_(n) bytes. 725 r_(i) = floor( ( R_(i) * S_(i, C_(i)) ) / 2 ^ 8 ) 727 Figure 11: A formula of the read of a binary value in Range 728 Binary mode. 730 S_(i + 1, C_(i)) = zero_state_(S_(i, C_(i))) AND 731 l_(i) = L_(i) AND 732 t_(i) = R_(i) - r_(i) <== 733 b_(i) = 0 <==> 734 L_(i) < R_(i) - r_(i) 736 S_(i + 1, C_(i)) = one_state_(S_(i, C_(i))) AND 737 l_(i) = L_(i) - R_(i) + r_(i) AND 738 t_(i) = r_(i) <== 739 b_(i) = 1 <==> 740 L_(i) >= R_(i) - r_(i) 741 Figure 12 743 S_(i + 1, k) = S_(i, k) <== C_(i) != k 745 Figure 13: The "i+1,k"-th State is equal to the "i,k"-th State if 746 the value of "k" is unequal to the i-th value of Context. 748 R_(i + 1) = 2 ^ 8 * t_(i) AND 749 L_(i + 1) = 2 ^ 8 * l_(i) + B_(j_(i)) AND 750 j_(i + 1) = j_(i) + 1 <== 751 t_(i) < 2 ^ 8 753 R_(i + 1) = t_(i) AND 754 L_(i + 1) = l_(i) AND 755 j_(i + 1) = j_(i) <== 756 t_(i) >= 2 ^ 8 758 Figure 14: The "i+1"-th values for "Range", "Low", and the length 759 of the bytestream encoding are conditionally set depending on the 760 "i-th" value of "t". 762 R_(0) = 65280 764 Figure 15: The initial value for "Range". 766 L_(0) = 2 ^ 8 * B_(0) + B_(1) 768 Figure 16: The initial value for "Low" is set according to the 769 first two bytes of the bytestream. 771 j_(0) = 2 773 Figure 17: The initial value for "j", the length of the 774 bytestream encoding. 776 range = 0xFF00; 777 end = 0; 778 low = get_bits(16); 779 if (low >= range) { 780 low = range; 781 end = 1; 782 } 784 Figure 18: A pseudo-code description of the initial states in 785 Range Binary mode. 787 refill() { 788 if (range < 256) { 789 range = range * 256; 790 low = low * 256; 791 if (!end) { 792 c.low += get_bits(8); 793 if (remaining_bits_in_bitstream( NumBytes ) == 0) { 794 end = 1; 795 } 796 } 797 } 798 } 800 Figure 19: A pseudo-code description of refilling the Range 801 Binary Value coder buffer. 803 get_rac(state) { 804 rangeoff = (range * state) / 256; 805 range -= rangeoff; 806 if (low < range) { 807 state = zero_state[state]; 808 refill(); 809 return 0; 810 } else { 811 low -= range; 812 state = one_state[state]; 813 range = rangeoff; 814 refill(); 815 return 1; 816 } 817 } 819 Figure 20: A pseudo-code description of the read of a binary 820 value in Range Binary mode. 822 3.8.1.1.1. Termination 824 The range coder can be used in three modes. 826 * In "Open mode" when decoding, every Symbol the reader attempts to 827 read is available. In this mode arbitrary data can have been 828 appended without affecting the range coder output. This mode is 829 not used in FFV1. 831 * In "Closed mode" the length in bytes of the bytestream is provided 832 to the range decoder. Bytes beyond the length are read as 0 by 833 the range decoder. This is generally one byte shorter than the 834 open mode. 836 * In "Sentinel mode" the exact length in bytes is not known and thus 837 the range decoder MAY read into the data that follows the range 838 coded bytestream by one byte. In "Sentinel mode", the end of the 839 range coded bytestream is a binary Symbol with state 129, which 840 value SHALL be discarded. After reading this Symbol, the range 841 decoder will have read one byte beyond the end of the range coded 842 bytestream. This way the byte position of the end can be 843 determined. Bytestreams written in "Sentinel mode" can be read in 844 "Closed mode" if the length can be determined, in this case the 845 last (sentinel) Symbol will be read non-corrupted and be of value 846 0. 848 Above describes the range decoding. Encoding is defined as any 849 process which produces a decodable bytestream. 851 There are three places where range coder termination is needed in 852 FFV1. First is in the "Configuration Record", in this case the size 853 of the range coded bytestream is known and handled as "Closed mode". 854 Second is the switch from the "Slice Header" which is range coded to 855 Golomb coded slices as "Sentinel mode". Third is the end of range 856 coded Slices which need to terminate before the CRC at their end. 857 This can be handled as "Sentinel mode" or as "Closed mode" if the CRC 858 position has been determined. 860 3.8.1.2. Range Non Binary Values 862 To encode scalar integers, it would be possible to encode each bit 863 separately and use the past bits as context. However that would mean 864 255 contexts per 8-bit Symbol that is not only a waste of memory but 865 also requires more past data to reach a reasonably good estimate of 866 the probabilities. Alternatively assuming a Laplacian distribution 867 and only dealing with its variance and mean (as in Huffman coding) 868 would also be possible, however, for maximum flexibility and 869 simplicity, the chosen method uses a single Symbol to encode if a 870 number is 0, and if not, encodes the number using its exponent, 871 mantissa and sign. The exact contexts used are best described by 872 Figure 21. 874 int get_symbol(RangeCoder *c, uint8_t *state, int is_signed) { 875 if (get_rac(c, state + 0) { 876 return 0; 877 } 879 int e = 0; 880 while (get_rac(c, state + 1 + min(e, 9)) { //1..10 881 e++; 882 } 884 int a = 1; 885 for (int i = e - 1; i >= 0; i--) { 886 a = a * 2 + get_rac(c, state + 22 + min(i, 9)); // 22..31 887 } 889 if (!is_signed) { 890 return a; 891 } 893 if (get_rac(c, state + 11 + min(e, 10))) { //11..21 894 return -a; 895 } else { 896 return a; 897 } 898 } 900 Figure 21: A pseudo-code description of the contexts of Range Non 901 Binary Values. 903 "get_symbol" is used for the read out of "sample_difference" 904 indicated in Figure 10. 906 "get_rac" returns a boolean, computed from the bytestream as 907 described in Figure 11 as a formula and in Figure 20 as pseudo-code. 909 3.8.1.3. Initial Values for the Context Model 911 When "keyframe" (see Section 4.4) value is 1, all Range coder state 912 variables are set to their initial state. 914 3.8.1.4. State Transition Table 916 In this mode a State Transition Table is used, indicating in which 917 state the decoder will move to, based on the current state and the 918 value extracted from Figure 20. 920 one_state_(i) = 921 default_state_transition_(i) + state_transition_delta_(i) 922 Figure 22 924 zero_state_(i) = 256 - one_state_(256-i) 926 Figure 23 928 3.8.1.5. default_state_transition 930 By default, the following State Transition Table is used: 932 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 934 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 936 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 938 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 940 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 942 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99,100,101,102,103, 944 104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118, 946 119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,133, 948 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149, 950 150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164, 952 165,166,167,168,169,170,171,171,172,173,174,175,176,177,178,179, 954 180,181,182,183,184,185,186,187,188,189,190,190,191,192,194,194, 956 195,196,197,198,199,200,201,202,202,204,205,206,207,208,209,209, 958 210,211,212,213,215,215,216,217,218,219,220,220,222,223,224,225, 960 226,227,227,229,229,230,231,232,234,234,235,236,237,238,239,240, 962 241,242,243,244,245,246,247,248,248, 0, 0, 0, 0, 0, 0, 0, 964 3.8.1.6. Alternative State Transition Table 966 The alternative state transition table has been built using iterative 967 minimization of frame sizes and generally performs better than the 968 default. To use it, the "coder_type" (see Section 4.2.3) MUST be set 969 to 2 and the difference to the default MUST be stored in the 970 "Parameters", see Section 4.2. The reference implementation of FFV1 971 in FFmpeg uses Figure 24 by default at the time of this writing when 972 Range coding is used. 974 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49, 976 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39, 978 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52, 980 53, 74, 55, 57, 58, 58, 74, 60,101, 61, 62, 84, 66, 66, 68, 69, 982 87, 82, 71, 97, 73, 73, 82, 75,111, 77, 94, 78, 87, 81, 83, 97, 984 85, 83, 94, 86, 99, 89, 90, 99,111, 92, 93,134, 95, 98,105, 98, 986 105,110,102,108,102,118,103,106,106,113,109,112,114,112,116,125, 988 115,116,117,117,126,119,125,121,121,123,145,124,126,131,127,129, 990 165,130,132,138,133,135,145,136,137,139,146,141,143,142,144,148, 992 147,155,151,149,151,150,152,157,153,154,156,168,158,162,161,160, 994 172,163,169,164,166,184,167,170,177,174,171,173,182,176,180,178, 996 175,189,179,181,186,183,192,185,200,187,191,188,190,197,193,196, 998 197,194,195,196,198,202,199,201,210,203,207,204,205,206,208,214, 1000 209,211,221,212,213,215,224,216,217,218,219,220,222,228,223,225, 1002 226,224,227,229,240,230,231,232,233,234,235,236,238,239,237,242, 1004 241,243,242,244,245,246,247,248,249,250,251,252,252,253,254,255, 1006 Figure 24: Alternative state transition table for Range coding. 1008 3.8.2. Golomb Rice Mode 1010 The end of the bitstream of the Frame is padded with 0-bits until the 1011 bitstream contains a multiple of 8 bits. 1013 3.8.2.1. Signed Golomb Rice Codes 1015 This coding mode uses Golomb Rice codes. The VLC is split into two 1016 parts. The prefix stores the most significant bits and the suffix 1017 stores the k least significant bits or stores the whole number in the 1018 ESC case. 1020 int get_ur_golomb(k) { 1021 for (prefix = 0; prefix < 12; prefix++) { 1022 if (get_bits(1)) { 1023 return get_bits(k) + (prefix << k); 1024 } 1025 } 1026 return get_bits(bits) + 11; 1027 } 1029 Figure 25: A pseudo-code description of the read of an unsigned 1030 integer in Golomb Rice mode. 1032 int get_sr_golomb(k) { 1033 v = get_ur_golomb(k); 1034 if (v & 1) return - (v >> 1) - 1; 1035 else return (v >> 1); 1036 } 1038 Figure 26: A pseudo-code description of the read of a signed 1039 integer in Golomb Rice mode. 1041 3.8.2.1.1. Prefix 1043 +================+=======+ 1044 | bits | value | 1045 +================+=======+ 1046 | 1 | 0 | 1047 +----------------+-------+ 1048 | 01 | 1 | 1049 +----------------+-------+ 1050 | ... | ... | 1051 +----------------+-------+ 1052 | 0000 0000 01 | 9 | 1053 +----------------+-------+ 1054 | 0000 0000 001 | 10 | 1055 +----------------+-------+ 1056 | 0000 0000 0001 | 11 | 1057 +----------------+-------+ 1058 | 0000 0000 0000 | ESC | 1059 +----------------+-------+ 1061 Table 1 1063 "ESC" is an ESCape Symbol to indicate that the Symbol to be stored is 1064 too large for normal storage and that an alternate storage method is 1065 used. 1067 3.8.2.1.2. Suffix 1069 +=========+========================================+ 1070 +=========+========================================+ 1071 | non ESC | the k least significant bits MSB first | 1072 +---------+----------------------------------------+ 1073 | ESC | the value - 11, in MSB first order | 1074 +---------+----------------------------------------+ 1076 Table 2 1078 ESC MUST NOT be used if the value can be coded as non ESC. 1080 3.8.2.1.3. Examples 1082 Table 3 shows practical examples of how Signed Golomb Rice Codes are 1083 decoded based on the series of bits extracted from the bitstream as 1084 described by the method above: 1086 +=====+=======================+=======+ 1087 | k | bits | value | 1088 +=====+=======================+=======+ 1089 | 0 | 1 | 0 | 1090 +-----+-----------------------+-------+ 1091 | 0 | 001 | 2 | 1092 +-----+-----------------------+-------+ 1093 | 2 | 1 00 | 0 | 1094 +-----+-----------------------+-------+ 1095 | 2 | 1 10 | 2 | 1096 +-----+-----------------------+-------+ 1097 | 2 | 01 01 | 5 | 1098 +-----+-----------------------+-------+ 1099 | any | 000000000000 10000000 | 139 | 1100 +-----+-----------------------+-------+ 1102 Table 3: Examples of decoded Signed 1103 Golomb Rice Codes. 1105 3.8.2.2. Run Mode 1107 Run mode is entered when the context is 0 and left as soon as a non-0 1108 difference is found. The sample difference is identical to the 1109 predicted one. The run and the first different sample difference are 1110 coded as defined in Section 3.8.2.4.1. 1112 3.8.2.2.1. Run Length Coding 1114 The run value is encoded in two parts. The prefix part stores the 1115 more significant part of the run as well as adjusting the "run_index" 1116 that determines the number of bits in the less significant part of 1117 the run. The second part of the value stores the less significant 1118 part of the run as it is. The "run_index" is reset for each Plane 1119 and slice to 0. 1121 log2_run[41] = { 1122 0, 0, 0, 0, 1, 1, 1, 1, 1123 2, 2, 2, 2, 3, 3, 3, 3, 1124 4, 4, 5, 5, 6, 6, 7, 7, 1125 8, 9,10,11,12,13,14,15, 1126 16,17,18,19,20,21,22,23, 1127 24, 1128 }; 1130 if (run_count == 0 && run_mode == 1) { 1131 if (get_bits(1)) { 1132 run_count = 1 << log2_run[run_index]; 1133 if (x + run_count <= w) { 1134 run_index++; 1135 } 1136 } else { 1137 if (log2_run[run_index]) { 1138 run_count = get_bits(log2_run[run_index]); 1139 } else { 1140 run_count = 0; 1141 } 1142 if (run_index) { 1143 run_index--; 1144 } 1145 run_mode = 2; 1146 } 1147 } 1149 The "log2_run" array is also used within [ISO.14495-1.1999]. 1151 3.8.2.3. Sign extension 1153 "sign_extend" is the function of increasing the number of bits of an 1154 input binary number in twos complement signed number representation 1155 while preserving the input number's sign (positive/negative) and 1156 value, in order to fit in the output bit width. It MAY be computed 1157 with: 1159 sign_extend(input_number, input_bits) { 1160 negative_bias = 1 << (input_bits - 1); 1161 bits_mask = negative_bias - 1; 1162 output_number = input_number & bits_mask; // Remove negative bit 1163 is_negative = input_number & negative_bias; // Test negative bit 1164 if (is_negative) 1165 output_number -= negative_bias; 1166 return output_number 1167 } 1169 3.8.2.4. Scalar Mode 1171 Each difference is coded with the per context mean prediction removed 1172 and a per context value for k. 1174 get_vlc_symbol(state) { 1175 i = state->count; 1176 k = 0; 1177 while (i < state->error_sum) { 1178 k++; 1179 i += i; 1180 } 1182 v = get_sr_golomb(k); 1184 if (2 * state->drift < -state->count) { 1185 v = -1 - v; 1186 } 1188 ret = sign_extend(v + state->bias, bits); 1190 state->error_sum += abs(v); 1191 state->drift += v; 1193 if (state->count == 128) { 1194 state->count >>= 1; 1195 state->drift >>= 1; 1196 state->error_sum >>= 1; 1197 } 1198 state->count++; 1199 if (state->drift <= -state->count) { 1200 state->bias = max(state->bias - 1, -128); 1202 state->drift = max(state->drift + state->count, 1203 -state->count + 1); 1204 } else if (state->drift > 0) { 1205 state->bias = min(state->bias + 1, 127); 1207 state->drift = min(state->drift - state->count, 0); 1208 } 1210 return ret; 1211 } 1213 3.8.2.4.1. Golomb Rice Sample Difference Coding 1215 Level coding is identical to the normal difference coding with the 1216 exception that the 0 value is removed as it cannot occur: 1218 diff = get_vlc_symbol(context_state); 1219 if (diff >= 0) { 1220 diff++; 1221 } 1223 Note, this is different from JPEG-LS, which doesn't use prediction in 1224 run mode and uses a different encoding and context model for the last 1225 difference. On a small set of test Samples the use of prediction 1226 slightly improved the compression rate. 1228 3.8.2.5. Initial Values for the VLC context state 1230 When "keyframe" (see Section 4.4) value is 1, all coder state 1231 variables are set to their initial state. 1233 drift = 0; 1234 error_sum = 4; 1235 bias = 0; 1236 count = 1; 1238 4. Bitstream 1240 An FFV1 bitstream is composed of a series of one or more Frames and 1241 (when required) a "Configuration Record". 1243 Within the following sub-sections, pseudo-code is used, as described 1244 in Section 2.2.1, to explain the structure of each FFV1 bitstream 1245 component. Table 4 lists symbols used to annotate that pseudo-code 1246 in order to define the storage of the data referenced in that line of 1247 pseudo-code. 1249 +========+=================================================+ 1250 | Symbol | Definition | 1251 +========+=================================================+ 1252 | u(n) | unsigned big endian integer Symbol using n bits | 1253 +--------+-------------------------------------------------+ 1254 | sg | Golomb Rice coded signed scalar Symbol coded | 1255 | | with the method described in Section 3.8.2 | 1256 +--------+-------------------------------------------------+ 1257 | br | Range coded Boolean (1-bit) Symbol with the | 1258 | | method described in Section 3.8.1.1 | 1259 +--------+-------------------------------------------------+ 1260 | ur | Range coded unsigned scalar Symbol coded with | 1261 | | the method described in Section 3.8.1.2 | 1262 +--------+-------------------------------------------------+ 1263 | sr | Range coded signed scalar Symbol coded with the | 1264 | | method described in Section 3.8.1.2 | 1265 +--------+-------------------------------------------------+ 1266 | sd | Sample difference Symbol coded with the method | 1267 | | described in Section 3.8 | 1268 +--------+-------------------------------------------------+ 1270 Table 4: Definition of pseudo-code symbols for this 1271 document. 1273 The following MUST be provided by external means during 1274 initialization of the decoder: 1276 "frame_pixel_width" is defined as Frame width in Pixels. 1278 "frame_pixel_height" is defined as Frame height in Pixels. 1280 Default values at the decoder initialization phase: 1282 "ConfigurationRecordIsPresent" is set to 0. 1284 4.1. Quantization Table Set 1286 The Quantization Table Sets are stored by storing the number of equal 1287 entries -1 of the first half of the table (represented as "len - 1" 1288 in the pseudo-code below) using the method described in 1289 Section 3.8.1.2. The second half doesn't need to be stored as it is 1290 identical to the first with flipped sign. "scale" and "len_count[ i 1291 ][ j ]" are temporary values used for the computing of 1292 "context_count[ i ]" and are not used outside Quantization Table Set 1293 pseudo-code. 1295 Example: 1297 Table: 0 0 1 1 1 1 2 2 -2 -2 -2 -1 -1 -1 -1 0 1299 Stored values: 1, 3, 1 1301 "QuantizationTableSet" has its own initial states, all set to 128. 1303 pseudo-code | type 1304 --------------------------------------------------------------|----- 1305 QuantizationTableSet( i ) { | 1306 scale = 1 | 1307 for (j = 0; j < MAX_CONTEXT_INPUTS; j++) { | 1308 QuantizationTable( i, j, scale ) | 1309 scale *= 2 * len_count[ i ][ j ] - 1 | 1310 } | 1311 context_count[ i ] = ceil( scale / 2 ) | 1312 } | 1314 "MAX_CONTEXT_INPUTS" is 5. 1316 pseudo-code | type 1317 --------------------------------------------------------------|----- 1318 QuantizationTable(i, j, scale) { | 1319 v = 0 | 1320 for (k = 0; k < 128;) { | 1321 len - 1 | ur 1322 for (n = 0; n < len; n++) { | 1323 quant_tables[ i ][ j ][ k ] = scale * v | 1324 k++ | 1325 } | 1326 v++ | 1327 } | 1328 for (k = 1; k < 128; k++) { | 1329 quant_tables[ i ][ j ][ 256 - k ] = \ | 1330 -quant_tables[ i ][ j ][ k ] | 1331 } | 1332 quant_tables[ i ][ j ][ 128 ] = \ | 1333 -quant_tables[ i ][ j ][ 127 ] | 1334 len_count[ i ][ j ] = v | 1335 } | 1337 4.1.1. quant_tables 1339 "quant_tables[ i ][ j ][ k ]" indicates the quantification table 1340 value of the Quantized Sample Difference "k" of the Quantization 1341 Table "j" of the Set Quantization Table Set "i". 1343 4.1.2. context_count 1345 "context_count[ i ]" indicates the count of contexts for Quantization 1346 Table Set "i". "context_count[ i ]" MUST be less than or equal to 1347 32768. 1349 4.2. Parameters 1351 The "Parameters" section contains significant characteristics about 1352 the decoding configuration used for all instances of Frame (in FFV1 1353 version 0 and 1) or the whole FFV1 bitstream (other versions), 1354 including the stream version, color configuration, and quantization 1355 tables. Figure 27 describes the contents of the bitstream. 1357 "Parameters" has its own initial states, all set to 128. 1359 pseudo-code | type 1360 --------------------------------------------------------------|----- 1361 Parameters( ) { | 1362 version | ur 1363 if (version >= 3) { | 1364 micro_version | ur 1365 } | 1366 coder_type | ur 1367 if (coder_type > 1) { | 1368 for (i = 1; i < 256; i++) { | 1369 state_transition_delta[ i ] | sr 1370 } | 1371 } | 1372 colorspace_type | ur 1373 if (version >= 1) { | 1374 bits_per_raw_sample | ur 1375 } | 1376 chroma_planes | br 1377 log2_h_chroma_subsample | ur 1378 log2_v_chroma_subsample | ur 1379 extra_plane | br 1380 if (version >= 3) { | 1381 num_h_slices - 1 | ur 1382 num_v_slices - 1 | ur 1383 quant_table_set_count | ur 1384 } | 1385 for (i = 0; i < quant_table_set_count; i++) { | 1386 QuantizationTableSet( i ) | 1387 } | 1388 if (version >= 3) { | 1389 for (i = 0; i < quant_table_set_count; i++) { | 1390 states_coded | br 1391 if (states_coded) { | 1392 for (j = 0; j < context_count[ i ]; j++) { | 1393 for (k = 0; k < CONTEXT_SIZE; k++) { | 1394 initial_state_delta[ i ][ j ][ k ] | sr 1395 } | 1396 } | 1397 } | 1398 } | 1399 ec | ur 1400 intra | ur 1401 } | 1402 } | 1404 Figure 27: A pseudo-code description of the bitstream contents. 1406 CONTEXT_SIZE is 32. 1408 4.2.1. version 1410 "version" specifies the version of the FFV1 bitstream. 1412 Each version is incompatible with other versions: decoders SHOULD 1413 reject FFV1 bitstreams due to an unknown version. 1415 Decoders SHOULD reject FFV1 bitstreams with version <= 1 && 1416 ConfigurationRecordIsPresent == 1. 1418 Decoders SHOULD reject FFV1 bitstreams with version >= 3 && 1419 ConfigurationRecordIsPresent == 0. 1421 +=======+=========================+ 1422 | value | version | 1423 +=======+=========================+ 1424 | 0 | FFV1 version 0 | 1425 +-------+-------------------------+ 1426 | 1 | FFV1 version 1 | 1427 +-------+-------------------------+ 1428 | 2 | reserved* | 1429 +-------+-------------------------+ 1430 | 3 | FFV1 version 3 | 1431 +-------+-------------------------+ 1432 | 4 | FFV1 version 4 | 1433 +-------+-------------------------+ 1434 | Other | reserved for future use | 1435 +-------+-------------------------+ 1437 Table 5 1439 * Version 2 was experimental and this document does not describe it. 1441 4.2.2. micro_version 1443 "micro_version" specifies the micro-version of the FFV1 bitstream. 1445 After a version is considered stable (a micro-version value is 1446 assigned to be the first stable variant of a specific version), each 1447 new micro-version after this first stable variant is compatible with 1448 the previous micro-version: decoders SHOULD NOT reject FFV1 1449 bitstreams due to an unknown micro-version equal or above the micro- 1450 version considered as stable. 1452 Meaning of "micro_version" for "version" 3: 1454 +=======+=========================+ 1455 | value | micro_version | 1456 +=======+=========================+ 1457 | 0...3 | reserved* | 1458 +-------+-------------------------+ 1459 | 4 | first stable variant | 1460 +-------+-------------------------+ 1461 | Other | reserved for future use | 1462 +-------+-------------------------+ 1464 Table 6: The definitions for 1465 "micro_version" values for FFV1 1466 version 3. 1468 * development versions may be incompatible with the stable variants. 1470 Meaning of "micro_version" for "version" 4 (note: at the time of 1471 writing of this specification, version 4 is not considered stable so 1472 the first stable "micro_version" value is to be announced in the 1473 future): 1475 +=========+=========================+ 1476 | value | micro_version | 1477 +=========+=========================+ 1478 | 0...TBA | reserved* | 1479 +---------+-------------------------+ 1480 | TBA | first stable variant | 1481 +---------+-------------------------+ 1482 | Other | reserved for future use | 1483 +---------+-------------------------+ 1485 Table 7: The definitions for 1486 "micro_version" values for FFV1 1487 version 4. 1489 * development versions which may be incompatible with the stable 1490 variants. 1492 4.2.3. coder_type 1494 "coder_type" specifies the coder used. 1496 +=======+=================================================+ 1497 | value | coder used | 1498 +=======+=================================================+ 1499 | 0 | Golomb Rice | 1500 +-------+-------------------------------------------------+ 1501 | 1 | Range Coder with default state transition table | 1502 +-------+-------------------------------------------------+ 1503 | 2 | Range Coder with custom state transition table | 1504 +-------+-------------------------------------------------+ 1505 | Other | reserved for future use | 1506 +-------+-------------------------------------------------+ 1508 Table 8 1510 Restrictions: 1512 If "coder_type" is 0, then "bits_per_raw_sample" SHOULD NOT be > 8. 1514 Background: At the time of this writing, there is no known 1515 implementation of FFV1 bitstream supporting Golomb Rice algorithm 1516 with "bits_per_raw_sample" greater than 8, and Range Coder is 1517 prefered. 1519 4.2.4. state_transition_delta 1521 "state_transition_delta" specifies the Range coder custom state 1522 transition table. 1524 If "state_transition_delta" is not present in the FFV1 bitstream, all 1525 Range coder custom state transition table elements are assumed to be 1526 0. 1528 4.2.5. colorspace_type 1530 "colorspace_type" specifies the color space encoded, the pixel 1531 transformation used by the encoder, the extra plane content, as well 1532 as interleave method. 1534 +=======+==============+================+==============+============+ 1535 | value | color space | pixel | extra plane | interleave | 1536 | | encoded | transformation | content | method | 1537 +=======+==============+================+==============+============+ 1538 | 0 | YCbCr | None | Transparency | Plane then | 1539 | | | | | Line | 1540 +-------+--------------+----------------+--------------+------------+ 1541 | 1 | RGB | JPEG2000-RCT | Transparency | Line then | 1542 | | | | | Plane | 1543 +-------+--------------+----------------+--------------+------------+ 1544 | Other | reserved | reserved for | reserved for | reserved | 1545 | | for future | future use | future use | for future | 1546 | | use | | | use | 1547 +-------+--------------+----------------+--------------+------------+ 1549 Table 9 1551 FFV1 bitstreams with "colorspace_type" == 1 && ("chroma_planes" != 1552 1 || "log2_h_chroma_subsample" != 0 || "log2_v_chroma_subsample" != 1553 0) are not part of this specification. 1555 4.2.6. chroma_planes 1557 "chroma_planes" indicates if chroma (color) Planes are present. 1559 +=======+===============================+ 1560 | value | presence | 1561 +=======+===============================+ 1562 | 0 | chroma Planes are not present | 1563 +-------+-------------------------------+ 1564 | 1 | chroma Planes are present | 1565 +-------+-------------------------------+ 1567 Table 10 1569 4.2.7. bits_per_raw_sample 1571 "bits_per_raw_sample" indicates the number of bits for each Sample. 1572 Inferred to be 8 if not present. 1574 +=======+=================================+ 1575 | value | bits for each sample | 1576 +=======+=================================+ 1577 | 0 | reserved* | 1578 +-------+---------------------------------+ 1579 | Other | the actual bits for each Sample | 1580 +-------+---------------------------------+ 1582 Table 11 1584 * Encoders MUST NOT store "bits_per_raw_sample" = 0. Decoders SHOULD 1585 accept and interpret "bits_per_raw_sample" = 0 as 8. 1587 4.2.8. log2_h_chroma_subsample 1589 "log2_h_chroma_subsample" indicates the subsample factor, stored in 1590 powers to which the number 2 is raised, between luma and chroma width 1591 ("chroma_width = 2 ^ -log2_h_chroma_subsample * luma_width"). 1593 4.2.9. log2_v_chroma_subsample 1595 "log2_v_chroma_subsample" indicates the subsample factor, stored in 1596 powers to which the number 2 is raised, between luma and chroma 1597 height ("chroma_height = 2 ^ -log2_v_chroma_subsample * 1598 luma_height"). 1600 4.2.10. extra_plane 1602 "extra_plane" indicates if an extra Plane is present. 1604 +=======+============================+ 1605 | value | presence | 1606 +=======+============================+ 1607 | 0 | extra Plane is not present | 1608 +-------+----------------------------+ 1609 | 1 | extra Plane is present | 1610 +-------+----------------------------+ 1612 Table 12 1614 4.2.11. num_h_slices 1616 "num_h_slices" indicates the number of horizontal elements of the 1617 slice raster. 1619 Inferred to be 1 if not present. 1621 4.2.12. num_v_slices 1623 "num_v_slices" indicates the number of vertical elements of the slice 1624 raster. 1626 Inferred to be 1 if not present. 1628 4.2.13. quant_table_set_count 1630 "quant_table_set_count" indicates the number of Quantization 1631 Table Sets. "quant_table_set_count" MUST be less than or equal to 8. 1633 Inferred to be 1 if not present. 1635 MUST NOT be 0. 1637 4.2.14. states_coded 1639 "states_coded" indicates if the respective Quantization Table Set has 1640 the initial states coded. 1642 Inferred to be 0 if not present. 1644 +=======+================================+ 1645 | value | initial states | 1646 +=======+================================+ 1647 | 0 | initial states are not present | 1648 | | and are assumed to be all 128 | 1649 +-------+--------------------------------+ 1650 | 1 | initial states are present | 1651 +-------+--------------------------------+ 1653 Table 13 1655 4.2.15. initial_state_delta 1657 "initial_state_delta[ i ][ j ][ k ]" indicates the initial Range 1658 coder state, it is encoded using "k" as context index and 1660 pred = j ? initial_states[ i ][j - 1][ k ] : 128 1662 Figure 28 1664 initial_state[ i ][ j ][ k ] = 1665 ( pred + initial_state_delta[ i ][ j ][ k ] ) & 255 1667 Figure 29 1669 4.2.16. ec 1671 "ec" indicates the error detection/correction type. 1673 +=======+=================================================+ 1674 | value | error detection/correction type | 1675 +=======+=================================================+ 1676 | 0 | 32-bit CRC in "ConfigurationRecord" | 1677 +-------+-------------------------------------------------+ 1678 | 1 | 32-bit CRC in "Slice" and "ConfigurationRecord" | 1679 +-------+-------------------------------------------------+ 1680 | Other | reserved for future use | 1681 +-------+-------------------------------------------------+ 1683 Table 14 1685 4.2.17. intra 1687 "intra" indicates the constraint on "keyframe" in each instance of 1688 Frame. 1690 Inferred to be 0 if not present. 1692 +=======+=======================================================+ 1693 | value | relationship | 1694 +=======+=======================================================+ 1695 | 0 | "keyframe" can be 0 or 1 (non keyframes or keyframes) | 1696 +-------+-------------------------------------------------------+ 1697 | 1 | "keyframe" MUST be 1 (keyframes only) | 1698 +-------+-------------------------------------------------------+ 1699 | Other | reserved for future use | 1700 +-------+-------------------------------------------------------+ 1702 Table 15 1704 4.3. Configuration Record 1706 In the case of a FFV1 bitstream with "version >= 3", a "Configuration 1707 Record" is stored in the underlying Container as described in 1708 Section 4.3.3. It contains the "Parameters" used for all instances 1709 of Frame. The size of the "Configuration Record", "NumBytes", is 1710 supplied by the underlying Container. 1712 pseudo-code | type 1713 -----------------------------------------------------------|----- 1714 ConfigurationRecord( NumBytes ) { | 1715 ConfigurationRecordIsPresent = 1 | 1716 Parameters( ) | 1717 while (remaining_symbols_in_syntax(NumBytes - 4)) { | 1718 reserved_for_future_use | br/ur/sr 1719 } | 1720 configuration_record_crc_parity | u(32) 1721 } | 1723 4.3.1. reserved_for_future_use 1725 "reserved_for_future_use" is a placeholder for future updates of this 1726 specification. 1728 Encoders conforming to this version of this specification SHALL NOT 1729 write "reserved_for_future_use". 1731 Decoders conforming to this version of this specification SHALL 1732 ignore "reserved_for_future_use". 1734 4.3.2. configuration_record_crc_parity 1736 "configuration_record_crc_parity" 32 bits that are chosen so that the 1737 "Configuration Record" as a whole has a CRC remainder of 0. 1739 This is equivalent to storing the CRC remainder in the 32-bit parity. 1741 The CRC generator polynomial used is described in Section 4.9.3. 1743 4.3.3. Mapping FFV1 into Containers 1745 This "Configuration Record" can be placed in any file format 1746 supporting "Configuration Records", fitting as much as possible with 1747 how the file format uses to store "Configuration Records". The 1748 "Configuration Record" storage place and "NumBytes" are currently 1749 defined and supported by this version of this specification for the 1750 following formats: 1752 4.3.3.1. AVI File Format 1754 The "Configuration Record" extends the stream format chunk ("AVI ", 1755 "hdlr", "strl", "strf") with the ConfigurationRecord bitstream. 1757 See [AVI] for more information about chunks. 1759 "NumBytes" is defined as the size, in bytes, of the strf chunk 1760 indicated in the chunk header minus the size of the stream format 1761 structure. 1763 4.3.3.2. ISO Base Media File Format 1765 The "Configuration Record" extends the sample description box 1766 ("moov", "trak", "mdia", "minf", "stbl", "stsd") with a "glbl" box 1767 that contains the ConfigurationRecord bitstream. See 1768 [ISO.14496-12.2015] for more information about boxes. 1770 "NumBytes" is defined as the size, in bytes, of the "glbl" box 1771 indicated in the box header minus the size of the box header. 1773 4.3.3.3. NUT File Format 1775 The "codec_specific_data" element (in "stream_header" packet) 1776 contains the ConfigurationRecord bitstream. See [NUT] for more 1777 information about elements. 1779 "NumBytes" is defined as the size, in bytes, of the 1780 "codec_specific_data" element as indicated in the "length" field of 1781 "codec_specific_data". 1783 4.3.3.4. Matroska File Format 1785 FFV1 SHOULD use "V_FFV1" as the Matroska "Codec ID". For FFV1 1786 versions 2 or less, the Matroska "CodecPrivate" Element SHOULD NOT be 1787 used. For FFV1 versions 3 or greater, the Matroska "CodecPrivate" 1788 Element MUST contain the FFV1 "Configuration Record" structure and no 1789 other data. See [Matroska] for more information about elements. 1791 "NumBytes" is defined as the "Element Data Size" of the 1792 "CodecPrivate" Element. 1794 4.4. Frame 1796 A Frame is an encoded representation of a complete static image. The 1797 whole Frame is provided by the underlaying container. 1799 A Frame consists of the "keyframe" field, "Parameters" (if "version" 1800 <= 1), and a sequence of independent slices. The pseudo-code below 1801 describes the contents of a Frame. 1803 "keyframe" field has its own initial state, set to 128. 1805 pseudo-code | type 1806 --------------------------------------------------------------|----- 1807 Frame( NumBytes ) { | 1808 keyframe | br 1809 if (keyframe && !ConfigurationRecordIsPresent { | 1810 Parameters( ) | 1811 } | 1812 while (remaining_bits_in_bitstream( NumBytes )) { | 1813 Slice( ) | 1814 } | 1815 } | 1817 Architecture overview of slices in a Frame: 1819 +=================================================================+ 1820 +=================================================================+ 1821 | first slice header | 1822 +-----------------------------------------------------------------+ 1823 | first slice content | 1824 +-----------------------------------------------------------------+ 1825 | first slice footer | 1826 +-----------------------------------------------------------------+ 1827 | --------------------------------------------------------------- | 1828 +-----------------------------------------------------------------+ 1829 | second slice header | 1830 +-----------------------------------------------------------------+ 1831 | second slice content | 1832 +-----------------------------------------------------------------+ 1833 | second slice footer | 1834 +-----------------------------------------------------------------+ 1835 | --------------------------------------------------------------- | 1836 +-----------------------------------------------------------------+ 1837 | ... | 1838 +-----------------------------------------------------------------+ 1839 | --------------------------------------------------------------- | 1840 +-----------------------------------------------------------------+ 1841 | last slice header | 1842 +-----------------------------------------------------------------+ 1843 | last slice content | 1844 +-----------------------------------------------------------------+ 1845 | last slice footer | 1846 +-----------------------------------------------------------------+ 1848 Table 16 1850 4.5. Slice 1852 A "Slice" is an independent spatial sub-section of a Frame that is 1853 encoded separately from another region of the same Frame. The use of 1854 more than one "Slice" per Frame can be useful for taking advantage of 1855 the opportunities of multithreaded encoding and decoding. 1857 A "Slice" consists of a "Slice Header" (when relevant), a "Slice 1858 Content", and a "Slice Footer" (when relevant). The pseudo-code 1859 below describes the contents of a "Slice". 1861 pseudo-code | type 1862 --------------------------------------------------------------|----- 1863 Slice( ) { | 1864 if (version >= 3) { | 1865 SliceHeader( ) | 1866 } | 1867 SliceContent( ) | 1868 if (coder_type == 0) { | 1869 while (!byte_aligned()) { | 1870 padding | u(1) 1871 } | 1872 } | 1873 if (version <= 1) { | 1874 while (remaining_bits_in_bitstream( NumBytes ) != 0) {| 1875 reserved | u(1) 1876 } | 1877 } | 1878 if (version >= 3) { | 1879 SliceFooter( ) | 1880 } | 1881 } | 1883 "padding" specifies a bit without any significance and used only for 1884 byte alignment. MUST be 0. 1886 "reserved" specifies a bit without any significance in this revision 1887 of the specification and may have a significance in a later revision 1888 of this specification. 1890 Encoders SHOULD NOT fill "reserved". 1892 Decoders SHOULD ignore "reserved". 1894 4.6. Slice Header 1896 A "Slice Header" provides information about the decoding 1897 configuration of the "Slice", such as its spatial position, size, and 1898 aspect ratio. The pseudo-code below describes the contents of the 1899 "Slice Header". 1901 "Slice Header" has its own initial states, all set to 128. 1903 pseudo-code | type 1904 --------------------------------------------------------------|----- 1905 SliceHeader( ) { | 1906 slice_x | ur 1907 slice_y | ur 1908 slice_width - 1 | ur 1909 slice_height - 1 | ur 1910 for (i = 0; i < quant_table_set_index_count; i++) { | 1911 quant_table_set_index[ i ] | ur 1912 } | 1913 picture_structure | ur 1914 sar_num | ur 1915 sar_den | ur 1916 if (version >= 4) { | 1917 reset_contexts | br 1918 slice_coding_mode | ur 1919 } | 1920 } | 1922 4.6.1. slice_x 1924 "slice_x" indicates the x position on the slice raster formed by 1925 num_h_slices. 1927 Inferred to be 0 if not present. 1929 4.6.2. slice_y 1931 "slice_y" indicates the y position on the slice raster formed by 1932 num_v_slices. 1934 Inferred to be 0 if not present. 1936 4.6.3. slice_width 1938 "slice_width" indicates the width on the slice raster formed by 1939 num_h_slices. 1941 Inferred to be 1 if not present. 1943 4.6.4. slice_height 1945 "slice_height" indicates the height on the slice raster formed by 1946 num_v_slices. 1948 Inferred to be 1 if not present. 1950 4.6.5. quant_table_set_index_count 1952 "quant_table_set_index_count" is defined as: 1954 1 + ( ( chroma_planes || version <= 3 ) ? 1 : 0 ) 1955 + ( extra_plane ? 1 : 0 ) 1957 4.6.6. quant_table_set_index 1959 "quant_table_set_index" indicates the Quantization Table Set index to 1960 select the Quantization Table Set and the initial states for the 1961 "Slice Content". 1963 Inferred to be 0 if not present. 1965 4.6.7. picture_structure 1967 "picture_structure" specifies the temporal and spatial relationship 1968 of each Line of the Frame. 1970 Inferred to be 0 if not present. 1972 +=======+=========================+ 1973 | value | picture structure used | 1974 +=======+=========================+ 1975 | 0 | unknown | 1976 +-------+-------------------------+ 1977 | 1 | top field first | 1978 +-------+-------------------------+ 1979 | 2 | bottom field first | 1980 +-------+-------------------------+ 1981 | 3 | progressive | 1982 +-------+-------------------------+ 1983 | Other | reserved for future use | 1984 +-------+-------------------------+ 1986 Table 17 1988 4.6.8. sar_num 1990 "sar_num" specifies the Sample aspect ratio numerator. 1992 Inferred to be 0 if not present. 1994 A value of 0 means that aspect ratio is unknown. 1996 Encoders MUST write 0 if Sample aspect ratio is unknown. 1998 If "sar_den" is 0, decoders SHOULD ignore the encoded value and 1999 consider that "sar_num" is 0. 2001 4.6.9. sar_den 2003 "sar_den" specifies the Sample aspect ratio denominator. 2005 Inferred to be 0 if not present. 2007 A value of 0 means that aspect ratio is unknown. 2009 Encoders MUST write 0 if Sample aspect ratio is unknown. 2011 If "sar_num" is 0, decoders SHOULD ignore the encoded value and 2012 consider that "sar_den" is 0. 2014 4.6.10. reset_contexts 2016 "reset_contexts" indicates if slice contexts MUST be reset. 2018 Inferred to be 0 if not present. 2020 4.6.11. slice_coding_mode 2022 "slice_coding_mode" indicates the slice coding mode. 2024 Inferred to be 0 if not present. 2026 +=======+=============================+ 2027 | value | slice coding mode | 2028 +=======+=============================+ 2029 | 0 | Range Coding or Golomb Rice | 2030 +-------+-----------------------------+ 2031 | 1 | raw PCM | 2032 +-------+-----------------------------+ 2033 | Other | reserved for future use | 2034 +-------+-----------------------------+ 2036 Table 18 2038 4.7. Slice Content 2040 A "Slice Content" contains all Line elements part of the "Slice". 2042 Depending on the configuration, Line elements are ordered by Plane 2043 then by row (YCbCr) or by row then by Plane (RGB). 2045 pseudo-code | type 2046 --------------------------------------------------------------|----- 2047 SliceContent( ) { | 2048 if (colorspace_type == 0) { | 2049 for (p = 0; p < primary_color_count; p++) { | 2050 for (y = 0; y < plane_pixel_height[ p ]; y++) { | 2051 Line( p, y ) | 2052 } | 2053 } | 2054 } else if (colorspace_type == 1) { | 2055 for (y = 0; y < slice_pixel_height; y++) { | 2056 for (p = 0; p < primary_color_count; p++) { | 2057 Line( p, y ) | 2058 } | 2059 } | 2060 } | 2061 } | 2063 4.7.1. primary_color_count 2065 "primary_color_count" is defined as: 2067 1 + ( chroma_planes ? 2 : 0 ) + ( extra_plane ? 1 : 0 ) 2069 4.7.2. plane_pixel_height 2071 "plane_pixel_height[ p ]" is the height in Pixels of Plane p of the 2072 "Slice". It is defined as: 2074 chroma_planes == 1 && (p == 1 || p == 2) 2075 ? ceil(slice_pixel_height / (1 << log2_v_chroma_subsample)) 2076 : slice_pixel_height 2078 4.7.3. slice_pixel_height 2080 "slice_pixel_height" is the height in pixels of the slice. It is 2081 defined as: 2083 floor( 2084 ( slice_y + slice_height ) 2085 * slice_pixel_height 2086 / num_v_slices 2087 ) - slice_pixel_y. 2089 4.7.4. slice_pixel_y 2091 "slice_pixel_y" is the slice vertical position in pixels. It is 2092 defined as: 2094 floor( slice_y * frame_pixel_height / num_v_slices ) 2096 4.8. Line 2098 A Line is a list of the sample differences (relative to the 2099 predictor) of primary color components. The pseudo-code below 2100 describes the contents of the Line. 2102 pseudo-code | type 2103 --------------------------------------------------------------|----- 2104 Line( p, y ) { | 2105 if (colorspace_type == 0) { | 2106 for (x = 0; x < plane_pixel_width[ p ]; x++) { | 2107 sample_difference[ p ][ y ][ x ] | sd 2108 } | 2109 } else if (colorspace_type == 1) { | 2110 for (x = 0; x < slice_pixel_width; x++) { | 2111 sample_difference[ p ][ y ][ x ] | sd 2112 } | 2113 } | 2114 } | 2116 4.8.1. plane_pixel_width 2118 "plane_pixel_width[ p ]" is the width in Pixels of Plane p of the 2119 "Slice". It is defined as: 2121 chroma\_planes == 1 && (p == 1 || p == 2) 2122 ? ceil( slice_pixel_width / (1 << log2_h_chroma_subsample) ) 2123 : slice_pixel_width. 2125 4.8.2. slice_pixel_width 2127 "slice_pixel_width" is the width in Pixels of the slice. It is 2128 defined as: 2130 floor( 2131 ( slice_x + slice_width ) 2132 * slice_pixel_width 2133 / num_h_slices 2134 ) - slice_pixel_x 2136 4.8.3. slice_pixel_x 2138 "slice_pixel_x" is the slice horizontal position in Pixels. It is 2139 defined as: 2141 floor( slice_x * frame_pixel_width / num_h_slices ) 2143 4.8.4. sample_difference 2145 "sample_difference[ p ][ y ][ x ]" is the sample difference for 2146 Sample at Plane "p", y position "y", and x position "x". The Sample 2147 value is computed based on median predictor and context described in 2148 Section 3.2. 2150 4.9. Slice Footer 2152 A "Slice Footer" provides information about slice size and 2153 (optionally) parity. The pseudo-code below describes the contents of 2154 the "Slice Footer". 2156 Note: "Slice Footer" is always byte aligned. 2158 pseudo-code | type 2159 --------------------------------------------------------------|----- 2160 SliceFooter( ) { | 2161 slice_size | u(24) 2162 if (ec) { | 2163 error_status | u(8) 2164 slice_crc_parity | u(32) 2165 } | 2166 } | 2168 4.9.1. slice_size 2170 "slice_size" indicates the size of the slice in bytes. 2172 Note: this allows finding the start of slices before previous slices 2173 have been fully decoded, and allows parallel decoding as well as 2174 error resilience. 2176 4.9.2. error_status 2178 "error_status" specifies the error status. 2180 +=======+======================================+ 2181 | value | error status | 2182 +=======+======================================+ 2183 | 0 | no error | 2184 +-------+--------------------------------------+ 2185 | 1 | slice contains a correctable error | 2186 +-------+--------------------------------------+ 2187 | 2 | slice contains a uncorrectable error | 2188 +-------+--------------------------------------+ 2189 | Other | reserved for future use | 2190 +-------+--------------------------------------+ 2192 Table 19 2194 4.9.3. slice_crc_parity 2196 "slice_crc_parity" 32 bits that are chosen so that the slice as a 2197 whole has a crc remainder of 0. 2199 This is equivalent to storing the crc remainder in the 32-bit parity. 2201 The CRC generator polynomial used is the standard IEEE CRC polynomial 2202 (0x104C11DB7), with initial value 0, without pre-inversion and 2203 without post-inversion. 2205 5. Restrictions 2207 To ensure that fast multithreaded decoding is possible, starting with 2208 version 3 and if "frame_pixel_width * frame_pixel_height" is more 2209 than 101376, "slice_width * slice_height" MUST be less or equal to 2210 "num_h_slices * num_v_slices / 4". Note: 101376 is the frame size in 2211 Pixels of a 352x288 frame also known as CIF ("Common Intermediate 2212 Format") frame size format. 2214 For each Frame, each position in the slice raster MUST be filled by 2215 one and only one slice of the Frame (no missing slice position, no 2216 slice overlapping). 2218 For each Frame with "keyframe" value of 0, each slice MUST have the 2219 same value of "slice_x", "slice_y", "slice_width", "slice_height" as 2220 a slice in the previous Frame, except if "reset_contexts" is 1. 2222 6. Security Considerations 2224 Like any other codec, (such as [RFC6716]), FFV1 should not be used 2225 with insecure ciphers or cipher-modes that are vulnerable to known 2226 plaintext attacks. Some of the header bits as well as the padding 2227 are easily predictable. 2229 Implementations of the FFV1 codec need to take appropriate security 2230 considerations into account. Those related to denial of service are 2231 outlined in Section 2.1 of [RFC4732]. It is extremely important for 2232 the decoder to be robust against malicious payloads. Malicious 2233 payloads MUST NOT cause the decoder to overrun its allocated memory 2234 or to take an excessive amount of resources to decode. An overrun in 2235 allocated memory could lead to arbitrary code execution by an 2236 attacker. The same applies to the encoder, even though problems in 2237 encoders are typically rarer. Malicious video streams MUST NOT cause 2238 the encoder to misbehave because this would allow an attacker to 2239 attack transcoding gateways. A frequent security problem in image 2240 and video codecs is failure to check for integer overflows. An 2241 example is allocating "frame_pixel_width * frame_pixel_height" in 2242 Pixel count computations without considering that the multiplication 2243 result may have overflowed the arithmetic types range. The range 2244 coder could, if implemented naively, read one byte over the end. The 2245 implementation MUST ensure that no read outside allocated and 2246 initialized memory occurs. 2248 None of the content carried in FFV1 is intended to be executable. 2250 7. IANA Considerations 2252 The IANA is requested to register the following values: 2254 7.1. Media Type Definition 2256 This registration is done using the template defined in [RFC6838] and 2257 following [RFC4855]. 2259 Type name: video 2261 Subtype name: FFV1 2262 Required parameters: None. 2264 Optional parameters: These parameters are used to signal the 2265 capabilities of a receiver implementation. These parameters MUST NOT 2266 be used for any other purpose. 2268 * "version": The "version" of the FFV1 encoding as defined by 2269 Section 4.2.1. 2271 * "micro_version": The "micro_version" of the FFV1 encoding as 2272 defined by Section 4.2.2. 2274 * "coder_type": The "coder_type" of the FFV1 encoding as defined by 2275 Section 4.2.3. 2277 * "colorspace_type": The "colorspace_type" of the FFV1 encoding as 2278 defined by Section 4.2.5. 2280 * "bits_per_raw_sample": The "bits_per_raw_sample" of the FFV1 2281 encoding as defined by Section 4.2.7. 2283 * "max_slices": The value of "max_slices" is an integer indicating 2284 the maximum count of slices with a frames of the FFV1 encoding. 2286 Encoding considerations: This media type is defined for encapsulation 2287 in several audiovisual container formats and contains binary data; 2288 see Section 4.3.3. This media type is framed binary data; see 2289 Section 4.8 of [RFC6838]. 2291 Security considerations: See Section 6 of this document. 2293 Interoperability considerations: None. 2295 Published specification: RFC XXXX. 2297 [RFC Editor: Upon publication as an RFC, please replace "XXXX" with 2298 the number assigned to this document and remove this note.] 2300 Applications which use this media type: Any application that requires 2301 the transport of lossless video can use this media type. Some 2302 examples are, but not limited to screen recording, scientific 2303 imaging, and digital video preservation. 2305 Fragment identifier considerations: N/A. 2307 Additional information: None. 2309 Person & email address to contact for further information: Michael 2310 Niedermayer michael@niedermayer.cc (mailto:michael@niedermayer.cc) 2312 Intended usage: COMMON 2314 Restrictions on usage: None. 2316 Author: Dave Rice dave@dericed.com (mailto:dave@dericed.com) 2318 Change controller: IETF cellar working group delegated from the IESG. 2320 8. Changelog 2322 See https://github.com/FFmpeg/FFV1/commits/master 2323 (https://github.com/FFmpeg/FFV1/commits/master) 2325 [RFC Editor: Please remove this Changelog section prior to 2326 publication.] 2328 9. Normative References 2330 [ISO.15444-1.2016] 2331 International Organization for Standardization, 2332 "Information technology -- JPEG 2000 image coding system: 2333 Core coding system", October 2016. 2335 [ISO.9899.2018] 2336 International Organization for Standardization, 2337 "Programming languages - C", ISO Standard 9899, 2018. 2339 [Matroska] IETF, "Matroska", 2019, . 2342 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 2343 Requirement Levels", BCP 14, RFC 2119, 2344 DOI 10.17487/RFC2119, March 1997, 2345 . 2347 [RFC4732] Handley, M., Ed., Rescorla, E., Ed., and IAB, "Internet 2348 Denial-of-Service Considerations", RFC 4732, 2349 DOI 10.17487/RFC4732, December 2006, 2350 . 2352 [RFC4855] Casner, S., "Media Type Registration of RTP Payload 2353 Formats", RFC 4855, DOI 10.17487/RFC4855, February 2007, 2354 . 2356 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 2357 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716, 2358 September 2012, . 2360 [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type 2361 Specifications and Registration Procedures", BCP 13, 2362 RFC 6838, DOI 10.17487/RFC6838, January 2013, 2363 . 2365 [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 2366 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, 2367 May 2017, . 2369 10. Informative References 2371 [Address-Sanitizer] 2372 The Clang Team, "ASAN AddressSanitizer website", undated, 2373 . 2375 [AVI] Microsoft, "AVI RIFF File Reference", undated, 2376 . 2379 [FFV1GO] Buitenhuis, D., "FFV1 Decoder in Go", 2019, 2380 . 2382 [HuffYUV] Rudiak-Gould, B., "HuffYUV", December 2003, 2383 . 2386 [I-D.ietf-cellar-ffv1] 2387 Niedermayer, M., Rice, D., and J. Martinez, "FFV1 Video 2388 Coding Format Version 0, 1, and 3", Work in Progress, 2389 Internet-Draft, draft-ietf-cellar-ffv1-18, 7 October 2020, 2390 . 2392 [ISO.14495-1.1999] 2393 International Organization for Standardization, 2394 "Information technology -- Lossless and near-lossless 2395 compression of continuous-tone still images: Baseline", 2396 December 1999. 2398 [ISO.14496-10.2014] 2399 International Organization for Standardization, 2400 "Information technology -- Coding of audio-visual objects 2401 -- Part 10: Advanced Video Coding", September 2014. 2403 [ISO.14496-12.2015] 2404 International Organization for Standardization, 2405 "Information technology -- Coding of audio-visual objects 2406 -- Part 12: ISO base media file format", December 2015. 2408 [MediaConch] 2409 MediaArea.net, "MediaConch", 2018, 2410 . 2412 [NUT] Niedermayer, M., "NUT Open Container Format", December 2413 2013, . 2415 [range-coding] 2416 Martin, G. N. N., "Range encoding: an algorithm for 2417 removing redundancy from a digitised message", Proceedings 2418 of the Conference on Video and Data Recording. Institution 2419 of Electronic and Radio Engineers, Hampshire, England, 2420 July 1979. 2422 [REFIMPL] Niedermayer, M., "The reference FFV1 implementation / the 2423 FFV1 codec in FFmpeg", undated, . 2425 [VALGRIND] Valgrind Developers, "Valgrind website", undated, 2426 . 2428 [YCbCr] Wikipedia, "YCbCr", undated, 2429 . 2431 Appendix A. Multi-theaded decoder implementation suggestions 2433 This appendix is informative. 2435 The FFV1 bitstream is parsable in two ways: in sequential order as 2436 described in this document or with the pre-analysis of the footer of 2437 each slice. Each slice footer contains a "slice_size" field so the 2438 boundary of each slice is computable without having to parse the 2439 slice content. That allows multi-threading as well as independence 2440 of slice content (a bitstream error in a slice header or slice 2441 content has no impact on the decoding of the other slices). 2443 After having checked "keyframe" field, a decoder SHOULD parse 2444 "slice_size" fields, from "slice_size" of the last slice at the end 2445 of the "Frame" up to "slice_size" of the first slice at the beginning 2446 of the "Frame", before parsing slices, in order to have slices 2447 boundaries. A decoder MAY fallback on sequential order e.g. in case 2448 of a corrupted "Frame" (frame size unknown, "slice_size" of slices 2449 not coherent...) or if there is no possibility of seeking into the 2450 stream. 2452 Appendix B. Future handling of some streams created by non conforming 2453 encoders 2455 This appendix is informative. 2457 Some bitstreams were found with 40 extra bits corresponding to 2458 "error_status" and "slice_crc_parity" in the "reserved" bits of 2459 "Slice()". Any revision of this specification SHOULD care about 2460 avoiding to add 40 bits of content after "SliceContent" if "version" 2461 == 0 or "version" == 1. Else a decoder conforming to the revised 2462 specification could not distinguish between a revised bitstream and 2463 such buggy bitstream in the wild. 2465 Appendix C. FFV1 Implementations 2467 This appendix provides references to a few notable implementations of 2468 FFV1. 2470 C.1. FFmpeg FFV1 Codec 2472 This reference implementation [REFIMPL] contains no known buffer 2473 overflow or cases where a specially crafted packet or video segment 2474 could cause a significant increase in CPU load. 2476 The reference implementation [REFIMPL] was validated in the following 2477 conditions: 2479 * Sending the decoder valid packets generated by the reference 2480 encoder and verifying that the decoder's output matches the 2481 encoder's input. 2483 * Sending the decoder packets generated by the reference encoder and 2484 then subjected to random corruption. 2486 * Sending the decoder random packets that are not FFV1. 2488 In all of the conditions above, the decoder and encoder was run 2489 inside the [VALGRIND] memory debugger as well as clangs address 2490 sanitizer [Address-Sanitizer], which track reads and writes to 2491 invalid memory regions as well as the use of uninitialized memory. 2492 There were no errors reported on any of the tested conditions. 2494 C.2. FFV1 Decoder in Go 2496 An FFV1 decoder was [FFV1GO] written in Go by Derek Buitenhuis during 2497 the work to development this document. 2499 C.3. MediaConch 2501 The developers of the MediaConch project [MediaConch] created an 2502 independent FFV1 decoder as part of that project to validate FFV1 2503 bitstreams. This work led to the discovery of three conflicts 2504 between existing FFV1 implementations and this document without the 2505 added exceptions. 2507 Authors' Addresses 2509 Michael Niedermayer 2511 Email: michael@niedermayer.cc 2513 Dave Rice 2515 Email: dave@dericed.com 2517 Jerome Martinez 2519 Email: jerome@mediaarea.net