idnits 2.17.1 draft-ietf-cellar-ffv1-v4-14.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 (21 August 2020) is 1344 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 1032 ** Downref: Normative reference to an Informational RFC: RFC 4732 == Outdated reference: A later version (-20) exists of draft-ietf-cellar-ffv1-16 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: 22 February 2021 6 J. Martinez 7 21 August 2020 9 FFV1 Video Coding Format Version 4 10 draft-ietf-cellar-ffv1-v4-14 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 22 February 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 . . . . . . . . . . . . . . . . . . . . . . . 4 56 2.2. Conventions . . . . . . . . . . . . . . . . . . . . . . . 5 57 2.2.1. Pseudo-code . . . . . . . . . . . . . . . . . . . . . 5 58 2.2.2. Arithmetic Operators . . . . . . . . . . . . . . . . 6 59 2.2.3. Assignment Operators . . . . . . . . . . . . . . . . 6 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 . . . . . . . . . . . . . . . . . . . . . . . . 8 64 2.2.8. NumBytes . . . . . . . . . . . . . . . . . . . . . . 8 65 2.2.9. Bitstream Functions . . . . . . . . . . . . . . . . . 9 66 3. Sample Coding . . . . . . . . . . . . . . . . . . . . . . . . 9 67 3.1. Border . . . . . . . . . . . . . . . . . . . . . . . . . 9 68 3.2. Samples . . . . . . . . . . . . . . . . . . . . . . . . . 10 69 3.3. Median Predictor . . . . . . . . . . . . . . . . . . . . 11 70 3.4. Quantization Table Sets . . . . . . . . . . . . . . . . . 12 71 3.5. Context . . . . . . . . . . . . . . . . . . . . . . . . . 12 72 3.6. Quantization Table Set Indexes . . . . . . . . . . . . . 12 73 3.7. Color spaces . . . . . . . . . . . . . . . . . . . . . . 13 74 3.7.1. YCbCr . . . . . . . . . . . . . . . . . . . . . . . . 13 75 3.7.2. RGB . . . . . . . . . . . . . . . . . . . . . . . . . 13 76 3.8. Coding of the Sample Difference . . . . . . . . . . . . . 15 77 3.8.1. Range Coding Mode . . . . . . . . . . . . . . . . . . 15 78 3.8.2. Golomb Rice Mode . . . . . . . . . . . . . . . . . . 21 79 4. Bitstream . . . . . . . . . . . . . . . . . . . . . . . . . . 26 80 4.1. Quantization Table Set . . . . . . . . . . . . . . . . . 27 81 4.1.1. quant_tables . . . . . . . . . . . . . . . . . . . . 28 82 4.1.2. context_count . . . . . . . . . . . . . . . . . . . . 29 83 4.2. Parameters . . . . . . . . . . . . . . . . . . . . . . . 29 84 4.2.1. version . . . . . . . . . . . . . . . . . . . . . . . 31 85 4.2.2. micro_version . . . . . . . . . . . . . . . . . . . . 31 86 4.2.3. coder_type . . . . . . . . . . . . . . . . . . . . . 32 87 4.2.4. state_transition_delta . . . . . . . . . . . . . . . 33 88 4.2.5. colorspace_type . . . . . . . . . . . . . . . . . . . 33 89 4.2.6. chroma_planes . . . . . . . . . . . . . . . . . . . . 34 90 4.2.7. bits_per_raw_sample . . . . . . . . . . . . . . . . . 34 91 4.2.8. log2_h_chroma_subsample . . . . . . . . . . . . . . . 35 92 4.2.9. log2_v_chroma_subsample . . . . . . . . . . . . . . . 35 93 4.2.10. extra_plane . . . . . . . . . . . . . . . . . . . . . 35 94 4.2.11. num_h_slices . . . . . . . . . . . . . . . . . . . . 35 95 4.2.12. num_v_slices . . . . . . . . . . . . . . . . . . . . 36 96 4.2.13. quant_table_set_count . . . . . . . . . . . . . . . . 36 97 4.2.14. states_coded . . . . . . . . . . . . . . . . . . . . 36 98 4.2.15. initial_state_delta . . . . . . . . . . . . . . . . . 36 99 4.2.16. ec . . . . . . . . . . . . . . . . . . . . . . . . . 37 100 4.2.17. intra . . . . . . . . . . . . . . . . . . . . . . . . 37 101 4.3. Configuration Record . . . . . . . . . . . . . . . . . . 37 102 4.3.1. reserved_for_future_use . . . . . . . . . . . . . . . 38 103 4.3.2. configuration_record_crc_parity . . . . . . . . . . . 38 104 4.3.3. Mapping FFV1 into Containers . . . . . . . . . . . . 38 105 4.4. Frame . . . . . . . . . . . . . . . . . . . . . . . . . . 39 106 4.5. Slice . . . . . . . . . . . . . . . . . . . . . . . . . . 41 107 4.6. Slice Header . . . . . . . . . . . . . . . . . . . . . . 42 108 4.6.1. slice_x . . . . . . . . . . . . . . . . . . . . . . . 42 109 4.6.2. slice_y . . . . . . . . . . . . . . . . . . . . . . . 42 110 4.6.3. slice_width . . . . . . . . . . . . . . . . . . . . . 42 111 4.6.4. slice_height . . . . . . . . . . . . . . . . . . . . 43 112 4.6.5. quant_table_set_index_count . . . . . . . . . . . . . 43 113 4.6.6. quant_table_set_index . . . . . . . . . . . . . . . . 43 114 4.6.7. picture_structure . . . . . . . . . . . . . . . . . . 43 115 4.6.8. sar_num . . . . . . . . . . . . . . . . . . . . . . . 44 116 4.6.9. sar_den . . . . . . . . . . . . . . . . . . . . . . . 44 117 4.6.10. reset_contexts . . . . . . . . . . . . . . . . . . . 44 118 4.6.11. slice_coding_mode . . . . . . . . . . . . . . . . . . 44 119 4.7. Slice Content . . . . . . . . . . . . . . . . . . . . . . 45 120 4.7.1. primary_color_count . . . . . . . . . . . . . . . . . 45 121 4.7.2. plane_pixel_height . . . . . . . . . . . . . . . . . 45 122 4.7.3. slice_pixel_height . . . . . . . . . . . . . . . . . 46 123 4.7.4. slice_pixel_y . . . . . . . . . . . . . . . . . . . . 46 124 4.8. Line . . . . . . . . . . . . . . . . . . . . . . . . . . 46 125 4.8.1. plane_pixel_width . . . . . . . . . . . . . . . . . . 46 126 4.8.2. slice_pixel_width . . . . . . . . . . . . . . . . . . 47 127 4.8.3. slice_pixel_x . . . . . . . . . . . . . . . . . . . . 47 128 4.8.4. sample_difference . . . . . . . . . . . . . . . . . . 47 129 4.9. Slice Footer . . . . . . . . . . . . . . . . . . . . . . 47 130 4.9.1. slice_size . . . . . . . . . . . . . . . . . . . . . 48 131 4.9.2. error_status . . . . . . . . . . . . . . . . . . . . 48 132 4.9.3. slice_crc_parity . . . . . . . . . . . . . . . . . . 48 133 5. Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 48 134 6. Security Considerations . . . . . . . . . . . . . . . . . . . 49 135 7. Media Type Definition . . . . . . . . . . . . . . . . . . . . 50 136 8. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 51 137 9. Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . 51 138 10. Normative References . . . . . . . . . . . . . . . . . . . . 51 139 11. Informative References . . . . . . . . . . . . . . . . . . . 52 140 Appendix A. Multi-theaded decoder implementation suggestions . . 54 141 Appendix B. Future handling of some streams created by non 142 conforming encoders . . . . . . . . . . . . . . . . . . . 54 143 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 54 145 1. Introduction 147 This document describes FFV1, a lossless video encoding format. The 148 design of FFV1 considers the storage of image characteristics, data 149 fixity, and the optimized use of encoding time and storage 150 requirements. FFV1 is designed to support a wide range of lossless 151 video applications such as long-term audiovisual preservation, 152 scientific imaging, screen recording, and other video encoding 153 scenarios that seek to avoid the generational loss of lossy video 154 encodings. 156 This document defines a version 4 of FFV1. Prior versions of FFV1 157 are defined within [I-D.ietf-cellar-ffv1]. 159 This document assumes familiarity with mathematical and coding 160 concepts such as Range coding [range-coding] and YCbCr color spaces 161 [YCbCr]. 163 This specification describes the valid bitstream and how to decode 164 such valid bitstream. Bitstreams not conforming to this 165 specification or how they are handled is outside this specification. 166 A decoder could reject every invalid bitstream or attempt to perform 167 error concealment or re-download or use a redundant copy of the 168 invalid part or any other action it deems appropriate. 170 2. Notation and Conventions 172 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 173 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and 174 "OPTIONAL" in this document are to be interpreted as described in BCP 175 14 [RFC2119] [RFC8174] when, and only when, they appear in all 176 capitals, as shown here. 178 2.1. Definitions 180 "Container": Format that encapsulates "Frames" (see Section 4.4) and 181 (when required) a "Configuration Record" into a bitstream. 183 "Sample": The smallest addressable representation of a color 184 component or a luma component in a "Frame". Examples of "Sample" are 185 Luma (Y), Blue-difference Chroma (Cb), Red-difference Chroma (Cr), 186 Transparency, Red, Green, and Blue. 188 "Plane": A discrete component of a static image comprised of 189 "Samples" that represent a specific quantification of "Samples" of 190 that image. 192 "Pixel": The smallest addressable representation of a color in a 193 "Frame". It is composed of one or more "Samples". 195 "ESC": An ESCape symbol to indicate that the symbol to be stored is 196 too large for normal storage and that an alternate storage method is 197 used. 199 "MSB": Most Significant Bit, the bit that can cause the largest 200 change in magnitude of the symbol. 202 "VLC": Variable Length Code, a code that maps source symbols to a 203 variable number of bits. 205 "RGB": A reference to the method of storing the value of a "Pixel" by 206 using three numeric values that represent Red, Green, and Blue. 208 "YCbCr": A reference to the method of storing the value of a "Pixel" 209 by using three numeric values that represent the luma of the "Pixel" 210 (Y) and the chroma of the "Pixel" (Cb and Cr). YCbCr word is used 211 for historical reasons and currently references any color space 212 relying on 1 luma "Sample" and 2 chroma "Samples", e.g. YCbCr, YCgCo 213 or ICtCp. The exact meaning of the three numeric values is 214 unspecified. 216 "TBA": To Be Announced. Used in reference to the development of 217 future iterations of the FFV1 specification. 219 2.2. Conventions 221 2.2.1. Pseudo-code 223 The FFV1 bitstream is described in this document using pseudo-code. 224 Note that the pseudo-code is used for clarity in order to illustrate 225 the structure of FFV1 and not intended to specify any particular 226 implementation. The pseudo-code used is based upon the C programming 227 language [ISO.9899.1990] and uses its "if/else", "while" and "for" 228 keywords as well as functions defined within this document. 230 In some instances, pseudo-code is presented in a two-column format 231 such as shown in Figure 1. In this form the "type" column provides a 232 symbol as defined in Table 4 that defines the storage of the data 233 referenced in that same line of pseudo-code. 235 pseudo-code | type 236 --------------------------------------------------------------|----- 237 ExamplePseudoCode( ) { | 238 value | ur 239 } | 241 Figure 1: A depiction of type-labelled pseudo-code used within 242 this document. 244 2.2.2. Arithmetic Operators 246 Note: the operators and the order of precedence are the same as used 247 in the C programming language [ISO.9899.2018], with the exception of 248 ">>" (removal of implementation defined behavior) and "^" (power 249 instead of XOR) operators which are re-defined within this section. 251 "a + b" means a plus b. 253 "a - b" means a minus b. 255 "-a" means negation of a. 257 "a * b" means a multiplied by b. 259 "a / b" means a divided by b. 261 "a ^ b" means a raised to the b-th power. 263 "a & b" means bit-wise "and" of a and b. 265 "a | b" means bit-wise "or" of a and b. 267 "a >> b" means arithmetic right shift of two's complement integer 268 representation of a by b binary digits. This is equivalent to 269 dividing a by 2, b times, with rounding toward negative infinity. 271 "a << b" means arithmetic left shift of two's complement integer 272 representation of a by b binary digits. 274 2.2.3. Assignment Operators 276 "a = b" means a is assigned b. 278 "a++" is equivalent to a is assigned a + 1. 280 "a--" is equivalent to a is assigned a - 1. 282 "a += b" is equivalent to a is assigned a + b. 284 "a -= b" is equivalent to a is assigned a - b. 286 "a *= b" is equivalent to a is assigned a * b. 288 2.2.4. Comparison Operators 290 "a > b" means a is greater than b. 292 "a >= b" means a is greater than or equal to b. 294 "a < b" means a is less than b. 296 "a <= b" means a is less than or equal b. 298 "a == b" means a is equal to b. 300 "a != b" means a is not equal to b. 302 "a && b" means Boolean logical "and" of a and b. 304 "a || b" means Boolean logical "or" of a and b. 306 "!a" means Boolean logical "not" of a. 308 "a ? b : c" if a is true, then b, otherwise c. 310 2.2.5. Mathematical Functions 312 "floor(a)" means the largest integer less than or equal to a. 314 "ceil(a)" means the smallest integer greater than or equal to a. 316 "sign(a)" extracts the sign of a number, i.e. if a < 0 then -1, else 317 if a > 0 then 1, else 0. 319 "abs(a)" means the absolute value of a, i.e. "abs(a)" = "sign(a) * 320 a". 322 "log2(a)" means the base-two logarithm of a. 324 "min(a,b)" means the smallest of two values a and b. 326 "max(a,b)" means the largest of two values a and b. 328 "median(a,b,c)" means the numerical middle value in a data set of a, 329 b, and c, i.e. a+b+c-min(a,b,c)-max(a,b,c). 331 "A <== B" means B implies A. 333 "A <==> B" means A <== B , B <== A. 335 a_(b) means the b-th value of a sequence of a 337 a_(b,c) means the 'b,c'-th value of a sequence of a 339 2.2.6. Order of Operation Precedence 341 When order of precedence is not indicated explicitly by use of 342 parentheses, operations are evaluated in the following order (from 343 top to bottom, operations of same precedence being evaluated from 344 left to right). This order of operations is based on the order of 345 operations used in Standard C. 347 a++, a-- 348 !a, -a 349 a ^ b 350 a * b, a / b, a % b 351 a + b, a - b 352 a << b, a >> b 353 a < b, a <= b, a > b, a >= b 354 a == b, a != b 355 a & b 356 a | b 357 a && b 358 a || b 359 a ? b : c 360 a = b, a += b, a -= b, a *= b 362 2.2.7. Range 364 "a...b" means any value starting from a to b, inclusive. 366 2.2.8. NumBytes 368 "NumBytes" is a non-negative integer that expresses the size in 8-bit 369 octets of a particular FFV1 "Configuration Record" or "Frame". FFV1 370 relies on its "Container" to store the "NumBytes" values; see 371 Section 4.3.3. 373 2.2.9. Bitstream Functions 375 2.2.9.1. remaining_bits_in_bitstream 377 "remaining_bits_in_bitstream( )" means the count of remaining bits 378 after the pointer in that "Configuration Record" or "Frame". It is 379 computed from the "NumBytes" value multiplied by 8 minus the count of 380 bits of that "Configuration Record" or "Frame" already read by the 381 bitstream parser. 383 2.2.9.2. remaining_symbols_in_syntax 385 "remaining_symbols_in_syntax( )" is true as long as the RangeCoder 386 has not consumed all the given input bytes. 388 2.2.9.3. byte_aligned 390 "byte_aligned( )" is true if "remaining_bits_in_bitstream( NumBytes 391 )" is a multiple of 8, otherwise false. 393 2.2.9.4. get_bits 395 "get_bits( i )" is the action to read the next "i" bits in the 396 bitstream, from most significant bit to least significant bit, and to 397 return the corresponding value. The pointer is increased by "i". 399 3. Sample Coding 401 For each "Slice" (as described in Section 4.5) of a "Frame", the 402 "Planes", "Lines", and "Samples" are coded in an order determined by 403 the "Color Space" (see Section 3.7). Each "Sample" is predicted by 404 the median predictor as described in Section 3.3 from other "Samples" 405 within the same "Plane" and the difference is stored using the method 406 described in Section 3.8. 408 3.1. Border 410 A border is assumed for each coded "Slice" for the purpose of the 411 median predictor and context according to the following rules: 413 * one column of "Samples" to the left of the coded slice is assumed 414 as identical to the "Samples" of the leftmost column of the coded 415 slice shifted down by one row. The value of the topmost "Sample" 416 of the column of "Samples" to the left of the coded slice is 417 assumed to be "0" 419 * one column of "Samples" to the right of the coded slice is assumed 420 as identical to the "Samples" of the rightmost column of the coded 421 slice 423 * an additional column of "Samples" to the left of the coded slice 424 and two rows of "Samples" above the coded slice are assumed to be 425 "0" 427 Figure 2 depicts a slice of 9 "Samples" "a,b,c,d,e,f,g,h,i" in a 3x3 428 arrangement along with its assumed border. 430 +---+---+---+---+---+---+---+---+ 431 | 0 | 0 | | 0 | 0 | 0 | | 0 | 432 +---+---+---+---+---+---+---+---+ 433 | 0 | 0 | | 0 | 0 | 0 | | 0 | 434 +---+---+---+---+---+---+---+---+ 435 | | | | | | | | | 436 +---+---+---+---+---+---+---+---+ 437 | 0 | 0 | | a | b | c | | c | 438 +---+---+---+---+---+---+---+---+ 439 | 0 | a | | d | e | f | | f | 440 +---+---+---+---+---+---+---+---+ 441 | 0 | d | | g | h | i | | i | 442 +---+---+---+---+---+---+---+---+ 444 Figure 2: A depiction of FFV1's assumed border for a set example 445 Samples. 447 3.2. Samples 449 Relative to any "Sample" "X", six other relatively positioned 450 "Samples" from the coded "Samples" and presumed border are identified 451 according to the labels used in Figure 3. The labels for these 452 relatively positioned "Samples" are used within the median predictor 453 and context. 455 +---+---+---+---+ 456 | | | T | | 457 +---+---+---+---+ 458 | |tl | t |tr | 459 +---+---+---+---+ 460 | L | l | X | | 461 +---+---+---+---+ 463 Figure 3: A depiction of how relatively positions Samples are 464 references within this document. 466 The labels for these relative "Samples" are made of the first letters 467 of the words Top, Left and Right. 469 3.3. Median Predictor 471 The prediction for any "Sample" value at position "X" may be computed 472 based upon the relative neighboring values of "l", "t", and "tl" via 473 this equation: 475 median(l, t, l + t - tl) 477 Note, this prediction template is also used in [ISO.14495-1.1999] and 478 [HuffYUV]. 480 Exception for the median predictor: if "colorspace_type == 0 && 481 bits_per_raw_sample == 16 && ( coder_type == 1 || coder_type == 2 )", 482 the following median predictor MUST be used: 484 median(left16s, top16s, left16s + top16s - diag16s) 486 where: 488 left16s = l >= 32768 ? ( l - 65536 ) : l 489 top16s = t >= 32768 ? ( t - 65536 ) : t 490 diag16s = tl >= 32768 ? ( tl - 65536 ) : tl 492 Background: a two's complement signed 16-bit signed integer was used 493 for storing "Sample" values in all known implementations of FFV1 494 bitstream. So in some circumstances, the most significant bit was 495 wrongly interpreted (used as a sign bit instead of the 16th bit of an 496 unsigned integer). Note that when the issue was discovered, the only 497 configuration of all known implementations being impacted is 16-bit 498 YCbCr with no Pixel transformation with Range Coder coder, as other 499 potentially impacted configurations (e.g. 15/16-bit JPEG2000-RCT with 500 Range Coder coder, or 16-bit content with Golomb Rice coder) were 501 implemented nowhere [ISO.15444-1.2016]. In the meanwhile, 16-bit 502 JPEG2000-RCT with Range Coder coder was implemented without this 503 issue in one implementation and validated by one conformance checker. 504 It is expected (to be confirmed) to remove this exception for the 505 median predictor in the next version of the FFV1 bitstream. 507 3.4. Quantization Table Sets 509 The FFV1 bitstream contains one or more Quantization Table Sets. 510 Each Quantization Table Set contains exactly 5 Quantization Tables 511 with each Quantization Table corresponding to one of the five 512 Quantized Sample Differences. For each Quantization Table, both the 513 number of quantization steps and their distribution are stored in the 514 FFV1 bitstream; each Quantization Table has exactly 256 entries, and 515 the 8 least significant bits of the Quantized Sample Difference are 516 used as index: 518 Q_(j)[k] = quant_tables[i][j][k&255] 520 Figure 4 522 In this formula, "i" is the Quantization Table Set index, "j" is the 523 Quantized Table index, "k" the Quantized Sample Difference. 525 3.5. Context 527 Relative to any "Sample" "X", the Quantized Sample Differences "L-l", 528 "l-tl", "tl-t", "T-t", and "t-tr" are used as context: 530 context = Q_(0)[l - tl] + 531 Q_(1)[tl - t] + 532 Q_(2)[t - tr] + 533 Q_(3)[L - l] + 534 Q_(4)[T - t] 536 Figure 5 538 If "context >= 0" then "context" is used and the difference between 539 the "Sample" and its predicted value is encoded as is, else 540 "-context" is used and the difference between the "Sample" and its 541 predicted value is encoded with a flipped sign. 543 3.6. Quantization Table Set Indexes 545 For each "Plane" of each slice, a Quantization Table Set is selected 546 from an index: 548 * For Y "Plane", "quant_table_set_index[ 0 ]" index is used 550 * For Cb and Cr "Planes", "quant_table_set_index[ 1 ]" index is used 552 * For extra "Plane", "quant_table_set_index[ (version <= 3 || 553 chroma_planes) ? 2 : 1 ]" index is used 555 Background: in first implementations of FFV1 bitstream, the index for 556 Cb and Cr "Planes" was stored even if it is not used (chroma_planes 557 set to 0), this index is kept for "version" <= 3 in order to keep 558 compatibility with FFV1 bitstreams in the wild. 560 3.7. Color spaces 562 FFV1 supports several color spaces. The count of allowed coded 563 planes and the meaning of the extra "Plane" are determined by the 564 selected color space. 566 The FFV1 bitstream interleaves data in an order determined by the 567 color space. In YCbCr for each "Plane", each "Line" is coded from 568 top to bottom and for each "Line", each "Sample" is coded from left 569 to right. In JPEG2000-RCT for each "Line" from top to bottom, each 570 "Plane" is coded and for each "Plane", each "Sample" is encoded from 571 left to right. 573 3.7.1. YCbCr 575 This color space allows 1 to 4 "Planes". 577 The Cb and Cr "Planes" are optional, but if used then MUST be used 578 together. Omitting the Cb and Cr "Planes" codes the frames in 579 grayscale without color data. 581 An optional transparency "Plane" can be used to code transparency 582 data. 584 An FFV1 "Frame" using YCbCr MUST use one of the following 585 arrangements: 587 * Y 589 * Y, Transparency 591 * Y, Cb, Cr 593 * Y, Cb, Cr, Transparency 595 The Y "Plane" MUST be coded first. If the Cb and Cr "Planes" are 596 used then they MUST be coded after the Y "Plane". If a transparency 597 "Plane" is used, then it MUST be coded last. 599 3.7.2. RGB 601 This color space allows 3 or 4 "Planes". 603 An optional transparency "Plane" can be used to code transparency 604 data. 606 JPEG2000-RCT is a Reversible Color Transform that codes RGB (red, 607 green, blue) "Planes" losslessly in a modified YCbCr color space 608 [ISO.15444-1.2016]. Reversible Pixel transformations between YCbCr 609 and RGB use the following formulae. 611 Cb = b - g 612 Cr = r - g 613 Y = g + (Cb + Cr) >> 2 614 g = Y - (Cb + Cr) >> 2 615 r = Cr + g 616 b = Cb + g 618 Figure 6 620 Exception for the JPEG2000-RCT conversion: if "bits_per_raw_sample" 621 is between 9 and 15 inclusive and "extra_plane" is 0, the following 622 formulae for reversible conversions between YCbCr and RGB MUST be 623 used instead of the ones above: 625 Cb = g - b 626 Cr = r - b 627 Y = b +(Cb + Cr) >> 2 628 b = Y -(Cb + Cr) >> 2 629 r = Cr + b 630 g = Cb + b 632 Figure 7 634 Background: At the time of this writing, in all known implementations 635 of FFV1 bitstream, when "bits_per_raw_sample" was between 9 and 15 636 inclusive and "extra_plane" is 0, GBR "Planes" were used as BGR 637 "Planes" during both encoding and decoding. In the meanwhile, 16-bit 638 JPEG2000-RCT was implemented without this issue in one implementation 639 and validated by one conformance checker. Methods to address this 640 exception for the transform are under consideration for the next 641 version of the FFV1 bitstream. 643 Cb and Cr are positively offset by "1 << bits_per_raw_sample" after 644 the conversion from RGB to the modified YCbCr and are negatively 645 offseted by the same value before the conversion from the modified 646 YCbCr to RGB, in order to have only non-negative values after the 647 conversion. 649 When FFV1 uses the JPEG2000-RCT, the horizontal "Lines" are 650 interleaved to improve caching efficiency since it is most likely 651 that the JPEG2000-RCT will immediately be converted to RGB during 652 decoding. The interleaved coding order is also Y, then Cb, then Cr, 653 and then if used transparency. 655 As an example, a "Frame" that is two "Pixels" wide and two "Pixels" 656 high, could comprise the following structure: 658 +------------------------+------------------------+ 659 | Pixel(1,1) | Pixel(2,1) | 660 | Y(1,1) Cb(1,1) Cr(1,1) | Y(2,1) Cb(2,1) Cr(2,1) | 661 +------------------------+------------------------+ 662 | Pixel(1,2) | Pixel(2,2) | 663 | Y(1,2) Cb(1,2) Cr(1,2) | Y(2,2) Cb(2,2) Cr(2,2) | 664 +------------------------+------------------------+ 666 In JPEG2000-RCT, the coding order would be left to right and then top 667 to bottom, with values interleaved by "Lines" and stored in this 668 order: 670 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) 671 Cb(2,2) Cr(1,2) Cr(2,2) 673 3.8. Coding of the Sample Difference 675 Instead of coding the n+1 bits of the Sample Difference with Huffman 676 or Range coding (or n+2 bits, in the case of JPEG2000-RCT), only the 677 n (or n+1, in the case of JPEG2000-RCT) least significant bits are 678 used, since this is sufficient to recover the original "Sample". In 679 the equation below, the term "bits" represents "bits_per_raw_sample + 680 1" for JPEG2000-RCT or "bits_per_raw_sample" otherwise: 682 coder_input = [(sample_difference + 2 ^ (bits - 1)) & 683 (2 ^ bits - 1)] - 2 ^ (bits - 1) 685 Figure 8: Description of the coding of the Sample Difference in 686 the bitstream. 688 3.8.1. Range Coding Mode 690 Early experimental versions of FFV1 used the CABAC Arithmetic coder 691 from H.264 as defined in [ISO.14496-10.2014] but due to the uncertain 692 patent/royalty situation, as well as its slightly worse performance, 693 CABAC was replaced by a Range coder based on an algorithm defined by 694 G. Nigel and N. Martin in 1979 [range-coding]. 696 3.8.1.1. Range Binary Values 698 To encode binary digits efficiently a Range coder is used. C_(i) is 699 the i-th Context. B_(i) is the i-th byte of the bytestream. b_(i) is 700 the i-th Range coded binary value, S_(0, i) is the i-th initial 701 state. The length of the bytestream encoding n binary symbols is 702 j_(n) bytes. 704 r_(i) = floor( ( R_(i) * S_(i, C_(i)) ) / 2 ^ 8 ) 706 Figure 9 708 S_(i + 1, C_(i)) = zero_state_(S_(i, C_(i))) AND 709 l_(i) = L_(i) AND 710 t_(i) = R_(i) - r_(i) <== 711 b_(i) = 0 <==> 712 L_(i) < R_(i) - r_(i) 714 S_(i + 1, C_(i)) = one_state_(S_(i, C_(i))) AND 715 l_(i) = L_(i) - R_(i) + r_(i) AND 716 t_(i) = r_(i) <== 717 b_(i) = 1 <==> 718 L_(i) >= R_(i) - r_(i) 720 Figure 10 722 S_(i + 1, k) = S_(i, k) <== C_(i) != k 724 Figure 11 726 R_(i + 1) = 2 ^ 8 * t_(i) AND 727 L_(i + 1) = 2 ^ 8 * l_(i) + B_(j_(i)) AND 728 j_(i + 1) = j_(i) + 1 <== 729 t_(i) < 2 ^ 8 731 R_(i + 1) = t_(i) AND 732 L_(i + 1) = l_(i) AND 733 j_(i + 1) = j_(i) <== 734 t_(i) >= 2 ^ 8 736 Figure 12 738 R_(0) = 65280 740 Figure 13 742 L_(0) = 2 ^ 8 * B_(0) + B_(1) 743 Figure 14 745 j_(0) = 2 747 Figure 15 749 3.8.1.1.1. Termination 751 The range coder can be used in three modes. 753 * In "Open mode" when decoding, every symbol the reader attempts to 754 read is available. In this mode arbitrary data can have been 755 appended without affecting the range coder output. This mode is 756 not used in FFV1. 758 * In "Closed mode" the length in bytes of the bytestream is provided 759 to the range decoder. Bytes beyond the length are read as 0 by 760 the range decoder. This is generally one byte shorter than the 761 open mode. 763 * In "Sentinel mode" the exact length in bytes is not known and thus 764 the range decoder MAY read into the data that follows the range 765 coded bytestream by one byte. In "Sentinel mode", the end of the 766 range coded bytestream is a binary symbol with state 129, which 767 value SHALL be discarded. After reading this symbol, the range 768 decoder will have read one byte beyond the end of the range coded 769 bytestream. This way the byte position of the end can be 770 determined. Bytestreams written in "Sentinel mode" can be read in 771 "Closed mode" if the length can be determined, in this case the 772 last (sentinel) symbol will be read non-corrupted and be of value 773 0. 775 Above describes the range decoding. Encoding is defined as any 776 process which produces a decodable bytestream. 778 There are three places where range coder termination is needed in 779 FFV1. First is in the "Configuration Record", in this case the size 780 of the range coded bytestream is known and handled as "Closed mode". 781 Second is the switch from the "Slice Header" which is range coded to 782 Golomb coded slices as "Sentinel mode". Third is the end of range 783 coded Slices which need to terminate before the CRC at their end. 784 This can be handled as "Sentinel mode" or as "Closed mode" if the CRC 785 position has been determined. 787 3.8.1.2. Range Non Binary Values 789 To encode scalar integers, it would be possible to encode each bit 790 separately and use the past bits as context. However that would mean 791 255 contexts per 8-bit symbol that is not only a waste of memory but 792 also requires more past data to reach a reasonably good estimate of 793 the probabilities. Alternatively assuming a Laplacian distribution 794 and only dealing with its variance and mean (as in Huffman coding) 795 would also be possible, however, for maximum flexibility and 796 simplicity, the chosen method uses a single symbol to encode if a 797 number is 0, and if not, encodes the number using its exponent, 798 mantissa and sign. The exact contexts used are best described by 799 Figure 16. 801 int get_symbol(RangeCoder *c, uint8_t *state, int is_signed) { 802 if (get_rac(c, state + 0) { 803 return 0; 804 } 806 int e = 0; 807 while (get_rac(c, state + 1 + min(e, 9)) { //1..10 808 e++; 809 } 811 int a = 1; 812 for (int i = e - 1; i >= 0; i--) { 813 a = a * 2 + get_rac(c, state + 22 + min(i, 9)); // 22..31 814 } 816 if (!is_signed) { 817 return a; 818 } 820 if (get_rac(c, state + 11 + min(e, 10))) { //11..21 821 return -a; 822 } else { 823 return a; 824 } 825 } 827 Figure 16: A pseudo-code description of the contexts of Range Non 828 Binary Values. 830 "get_symbol" is used for the read out of "sample_difference" 831 indicated in Figure 8. 833 "get_rac" returns a boolean, computed from the bytestream as 834 described in Section 3.8.1.1. 836 3.8.1.3. Initial Values for the Context Model 838 At keyframes all Range coder state variables are set to their initial 839 state. 841 3.8.1.4. State Transition Table 843 one_state_(i) = 844 default_state_transition_(i) + state_transition_delta_(i) 846 Figure 17 848 zero_state_(i) = 256 - one_state_(256-i) 850 Figure 18 852 3.8.1.5. default_state_transition 853 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 855 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 857 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 859 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 861 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 863 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99,100,101,102,103, 865 104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118, 867 119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,133, 869 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149, 871 150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164, 873 165,166,167,168,169,170,171,171,172,173,174,175,176,177,178,179, 875 180,181,182,183,184,185,186,187,188,189,190,190,191,192,194,194, 877 195,196,197,198,199,200,201,202,202,204,205,206,207,208,209,209, 879 210,211,212,213,215,215,216,217,218,219,220,220,222,223,224,225, 881 226,227,227,229,229,230,231,232,234,234,235,236,237,238,239,240, 883 241,242,243,244,245,246,247,248,248, 0, 0, 0, 0, 0, 0, 0, 885 3.8.1.6. Alternative State Transition Table 887 The alternative state transition table has been built using iterative 888 minimization of frame sizes and generally performs better than the 889 default. To use it, the "coder_type" (see Section 4.2.3) MUST be set 890 to 2 and the difference to the default MUST be stored in the 891 "Parameters", see Section 4.2. The reference implementation of FFV1 892 in FFmpeg uses Figure 19 by default at the time of this writing when 893 Range coding is used. 895 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49, 897 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39, 899 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52, 901 53, 74, 55, 57, 58, 58, 74, 60,101, 61, 62, 84, 66, 66, 68, 69, 903 87, 82, 71, 97, 73, 73, 82, 75,111, 77, 94, 78, 87, 81, 83, 97, 905 85, 83, 94, 86, 99, 89, 90, 99,111, 92, 93,134, 95, 98,105, 98, 907 105,110,102,108,102,118,103,106,106,113,109,112,114,112,116,125, 909 115,116,117,117,126,119,125,121,121,123,145,124,126,131,127,129, 911 165,130,132,138,133,135,145,136,137,139,146,141,143,142,144,148, 913 147,155,151,149,151,150,152,157,153,154,156,168,158,162,161,160, 915 172,163,169,164,166,184,167,170,177,174,171,173,182,176,180,178, 917 175,189,179,181,186,183,192,185,200,187,191,188,190,197,193,196, 919 197,194,195,196,198,202,199,201,210,203,207,204,205,206,208,214, 921 209,211,221,212,213,215,224,216,217,218,219,220,222,228,223,225, 923 226,224,227,229,240,230,231,232,233,234,235,236,238,239,237,242, 925 241,243,242,244,245,246,247,248,249,250,251,252,252,253,254,255, 927 Figure 19: Alternative state transition table for Range coding. 929 3.8.2. Golomb Rice Mode 931 The end of the bitstream of the "Frame" is filled with 0-bits until 932 that the bitstream contains a multiple of 8 bits. 934 3.8.2.1. Signed Golomb Rice Codes 936 This coding mode uses Golomb Rice codes. The VLC is split into two 937 parts. The prefix stores the most significant bits and the suffix 938 stores the k least significant bits or stores the whole number in the 939 ESC case. 941 int get_ur_golomb(k) { 942 for (prefix = 0; prefix < 12; prefix++) { 943 if (get_bits(1)) { 944 return get_bits(k) + (prefix << k); 945 } 946 } 947 return get_bits(bits) + 11; 948 } 950 Figure 20: A pseudo-code description of the read of an unsigned 951 integer in Golomb Rice mode. 953 int get_sr_golomb(k) { 954 v = get_ur_golomb(k); 955 if (v & 1) return - (v >> 1) - 1; 956 else return (v >> 1); 957 } 959 Figure 21: A pseudo-code description of the read of a signed 960 integer in Golomb Rice mode. 962 3.8.2.1.1. Prefix 964 +================+=======+ 965 | bits | value | 966 +================+=======+ 967 | 1 | 0 | 968 +----------------+-------+ 969 | 01 | 1 | 970 +----------------+-------+ 971 | ... | ... | 972 +----------------+-------+ 973 | 0000 0000 01 | 9 | 974 +----------------+-------+ 975 | 0000 0000 001 | 10 | 976 +----------------+-------+ 977 | 0000 0000 0001 | 11 | 978 +----------------+-------+ 979 | 0000 0000 0000 | ESC | 980 +----------------+-------+ 982 Table 1 984 3.8.2.1.2. Suffix 986 +=========+========================================+ 987 +=========+========================================+ 988 | non ESC | the k least significant bits MSB first | 989 +---------+----------------------------------------+ 990 | ESC | the value - 11, in MSB first order | 991 +---------+----------------------------------------+ 993 Table 2 995 "ESC" MUST NOT be used if the value can be coded as "non ESC". 997 3.8.2.1.3. Examples 999 +=====+=======================+=======+ 1000 | k | bits | value | 1001 +=====+=======================+=======+ 1002 | 0 | 1 | 0 | 1003 +-----+-----------------------+-------+ 1004 | 0 | 001 | 2 | 1005 +-----+-----------------------+-------+ 1006 | 2 | 1 00 | 0 | 1007 +-----+-----------------------+-------+ 1008 | 2 | 1 10 | 2 | 1009 +-----+-----------------------+-------+ 1010 | 2 | 01 01 | 5 | 1011 +-----+-----------------------+-------+ 1012 | any | 000000000000 10000000 | 139 | 1013 +-----+-----------------------+-------+ 1015 Table 3 1017 3.8.2.2. Run Mode 1019 Run mode is entered when the context is 0 and left as soon as a non-0 1020 difference is found. The level is identical to the predicted one. 1021 The run and the first different level are coded. 1023 3.8.2.2.1. Run Length Coding 1025 The run value is encoded in two parts. The prefix part stores the 1026 more significant part of the run as well as adjusting the "run_index" 1027 that determines the number of bits in the less significant part of 1028 the run. The second part of the value stores the less significant 1029 part of the run as it is. The "run_index" is reset for each "Plane" 1030 and slice to 0. 1032 log2_run[41] = { 1033 0, 0, 0, 0, 1, 1, 1, 1, 1034 2, 2, 2, 2, 3, 3, 3, 3, 1035 4, 4, 5, 5, 6, 6, 7, 7, 1036 8, 9,10,11,12,13,14,15, 1037 16,17,18,19,20,21,22,23, 1038 24, 1039 }; 1041 if (run_count == 0 && run_mode == 1) { 1042 if (get_bits(1)) { 1043 run_count = 1 << log2_run[run_index]; 1044 if (x + run_count <= w) { 1045 run_index++; 1046 } 1047 } else { 1048 if (log2_run[run_index]) { 1049 run_count = get_bits(log2_run[run_index]); 1050 } else { 1051 run_count = 0; 1052 } 1053 if (run_index) { 1054 run_index--; 1055 } 1056 run_mode = 2; 1057 } 1058 } 1060 The "log2_run" array is also used within [ISO.14495-1.1999]. 1062 3.8.2.3. Sign extension 1064 "sign_extend" is the function of increasing the number of bits of an 1065 input binary number in twos complement signed number representation 1066 while preserving the input number's sign (positive/negative) and 1067 value, in order to fit in the output bit width. It MAY be computed 1068 with: 1070 sign_extend(input_number, input_bits) { 1071 negative_bias = 1 << (input_bits - 1); 1072 bits_mask = negative_bias - 1; 1073 output_number = input_number & bits_mask; // Remove negative bit 1074 is_negative = input_number & negative_bias; // Test negative bit 1075 if (is_negative) 1076 output_number -= negative_bias; 1077 return output_number 1078 } 1080 3.8.2.4. Scalar Mode 1082 Each difference is coded with the per context mean prediction removed 1083 and a per context value for k. 1085 get_vlc_symbol(state) { 1086 i = state->count; 1087 k = 0; 1088 while (i < state->error_sum) { 1089 k++; 1090 i += i; 1091 } 1093 v = get_sr_golomb(k); 1095 if (2 * state->drift < -state->count) { 1096 v = -1 - v; 1097 } 1099 ret = sign_extend(v + state->bias, bits); 1101 state->error_sum += abs(v); 1102 state->drift += v; 1104 if (state->count == 128) { 1105 state->count >>= 1; 1106 state->drift >>= 1; 1107 state->error_sum >>= 1; 1108 } 1109 state->count++; 1110 if (state->drift <= -state->count) { 1111 state->bias = max(state->bias - 1, -128); 1113 state->drift = max(state->drift + state->count, 1114 -state->count + 1); 1115 } else if (state->drift > 0) { 1116 state->bias = min(state->bias + 1, 127); 1118 state->drift = min(state->drift - state->count, 0); 1119 } 1121 return ret; 1122 } 1124 3.8.2.4.1. Level Coding 1126 Level coding is identical to the normal difference coding with the 1127 exception that the 0 value is removed as it cannot occur: 1129 diff = get_vlc_symbol(context_state); 1130 if (diff >= 0) { 1131 diff++; 1132 } 1134 Note, this is different from JPEG-LS, which doesn't use prediction in 1135 run mode and uses a different encoding and context model for the last 1136 difference. On a small set of test "Samples" the use of prediction 1137 slightly improved the compression rate. 1139 3.8.2.5. Initial Values for the VLC context state 1141 At keyframes all coder state variables are set to their initial 1142 state. 1144 drift = 0; 1145 error_sum = 4; 1146 bias = 0; 1147 count = 1; 1149 4. Bitstream 1151 An FFV1 bitstream is composed of a series of one or more "Frames" and 1152 (when required) a "Configuration Record". 1154 Within the following sub-sections, pseudo-code is used to explain the 1155 structure of each FFV1 bitstream component, as described in 1156 Section 2.2.1. Table 4 lists symbols used to annotate that pseudo- 1157 code in order to define the storage of the data referenced in that 1158 line of pseudo-code. 1160 +========+==============================================+ 1161 | Symbol | Definition | 1162 +========+==============================================+ 1163 | u(n) | unsigned big endian integer using n bits | 1164 +--------+----------------------------------------------+ 1165 | sg | Golomb Rice coded signed scalar symbol coded | 1166 | | with the method described in Section 3.8.2 | 1167 +--------+----------------------------------------------+ 1168 | br | Range coded Boolean (1-bit) symbol with the | 1169 | | method described in Section 3.8.1.1 | 1170 +--------+----------------------------------------------+ 1171 | ur | Range coded unsigned scalar symbol coded | 1172 | | with the method described in Section 3.8.1.2 | 1173 +--------+----------------------------------------------+ 1174 | sr | Range coded signed scalar symbol coded with | 1175 | | the method described in Section 3.8.1.2 | 1176 +--------+----------------------------------------------+ 1177 | sd | Sample difference coded with the method | 1178 | | described in Section 3.8 | 1179 +--------+----------------------------------------------+ 1181 Table 4: Definition of pseudo-code symbols for this 1182 document. 1184 The following MUST be provided by external means during 1185 initialization of the decoder: 1187 "frame_pixel_width" is defined as "Frame" width in "Pixels". 1189 "frame_pixel_height" is defined as "Frame" height in "Pixels". 1191 Default values at the decoder initialization phase: 1193 "ConfigurationRecordIsPresent" is set to 0. 1195 4.1. Quantization Table Set 1197 The Quantization Table Sets are stored by storing the number of equal 1198 entries -1 of the first half of the table (represented as "len - 1" 1199 in the pseudo-code below) using the method described in 1200 Section 3.8.1.2. The second half doesn't need to be stored as it is 1201 identical to the first with flipped sign. "scale" and "len_count[ i 1202 ][ j ]" are temporary values used for the computing of 1203 "context_count[ i ]" and are not used outside Quantization Table Set 1204 pseudo-code. 1206 Example: 1208 Table: 0 0 1 1 1 1 2 2 -2 -2 -2 -1 -1 -1 -1 0 1210 Stored values: 1, 3, 1 1212 "QuantizationTableSet" has its own initial states, all set to 128. 1214 pseudo-code | type 1215 --------------------------------------------------------------|----- 1216 QuantizationTableSet( i ) { | 1217 scale = 1 | 1218 for (j = 0; j < MAX_CONTEXT_INPUTS; j++) { | 1219 QuantizationTable( i, j, scale ) | 1220 scale *= 2 * len_count[ i ][ j ] - 1 | 1221 } | 1222 context_count[ i ] = ceil( scale / 2 ) | 1223 } | 1225 "MAX_CONTEXT_INPUTS" is 5. 1227 pseudo-code | type 1228 --------------------------------------------------------------|----- 1229 QuantizationTable(i, j, scale) { | 1230 v = 0 | 1231 for (k = 0; k < 128;) { | 1232 len - 1 | ur 1233 for (n = 0; n < len; n++) { | 1234 quant_tables[ i ][ j ][ k ] = scale * v | 1235 k++ | 1236 } | 1237 v++ | 1238 } | 1239 for (k = 1; k < 128; k++) { | 1240 quant_tables[ i ][ j ][ 256 - k ] = \ | 1241 -quant_tables[ i ][ j ][ k ] | 1242 } | 1243 quant_tables[ i ][ j ][ 128 ] = \ | 1244 -quant_tables[ i ][ j ][ 127 ] | 1245 len_count[ i ][ j ] = v | 1246 } | 1248 4.1.1. quant_tables 1250 "quant_tables[ i ][ j ][ k ]" indicates the quantification table 1251 value of the Quantized Sample Difference "k" of the Quantization 1252 Table "j" of the Set Quantization Table Set "i". 1254 4.1.2. context_count 1256 "context_count[ i ]" indicates the count of contexts for Quantization 1257 Table Set "i". "context_count[ i ]" MUST be less than or equal to 1258 32768. 1260 4.2. Parameters 1262 The "Parameters" section contains significant characteristics about 1263 the decoding configuration used for all instances of "Frame" (in FFV1 1264 version 0 and 1) or the whole FFV1 bitstream (other versions), 1265 including the stream version, color configuration, and quantization 1266 tables. Figure 22 describes the contents of the bitstream. 1268 "Parameters" has its own initial states, all set to 128. 1270 pseudo-code | type 1271 --------------------------------------------------------------|----- 1272 Parameters( ) { | 1273 version | ur 1274 if (version >= 3) { | 1275 micro_version | ur 1276 } | 1277 coder_type | ur 1278 if (coder_type > 1) { | 1279 for (i = 1; i < 256; i++) { | 1280 state_transition_delta[ i ] | sr 1281 } | 1282 } | 1283 colorspace_type | ur 1284 if (version >= 1) { | 1285 bits_per_raw_sample | ur 1286 } | 1287 chroma_planes | br 1288 log2_h_chroma_subsample | ur 1289 log2_v_chroma_subsample | ur 1290 extra_plane | br 1291 if (version >= 3) { | 1292 num_h_slices - 1 | ur 1293 num_v_slices - 1 | ur 1294 quant_table_set_count | ur 1295 } | 1296 for (i = 0; i < quant_table_set_count; i++) { | 1297 QuantizationTableSet( i ) | 1298 } | 1299 if (version >= 3) { | 1300 for (i = 0; i < quant_table_set_count; i++) { | 1301 states_coded | br 1302 if (states_coded) { | 1303 for (j = 0; j < context_count[ i ]; j++) { | 1304 for (k = 0; k < CONTEXT_SIZE; k++) { | 1305 initial_state_delta[ i ][ j ][ k ] | sr 1306 } | 1307 } | 1308 } | 1309 } | 1310 ec | ur 1311 intra | ur 1312 } | 1313 } | 1315 Figure 22: A pseudo-code description of the bitstream contents. 1317 CONTEXT_SIZE is 32. 1319 4.2.1. version 1321 "version" specifies the version of the FFV1 bitstream. 1323 Each version is incompatible with other versions: decoders SHOULD 1324 reject FFV1 bitstreams due to an unknown version. 1326 Decoders SHOULD reject FFV1 bitstreams with version <= 1 && 1327 ConfigurationRecordIsPresent == 1. 1329 Decoders SHOULD reject FFV1 bitstreams with version >= 3 && 1330 ConfigurationRecordIsPresent == 0. 1332 +=======+=========================+ 1333 | value | version | 1334 +=======+=========================+ 1335 | 0 | FFV1 version 0 | 1336 +-------+-------------------------+ 1337 | 1 | FFV1 version 1 | 1338 +-------+-------------------------+ 1339 | 2 | reserved* | 1340 +-------+-------------------------+ 1341 | 3 | FFV1 version 3 | 1342 +-------+-------------------------+ 1343 | 4 | FFV1 version 4 | 1344 +-------+-------------------------+ 1345 | Other | reserved for future use | 1346 +-------+-------------------------+ 1348 Table 5 1350 * Version 2 was experimental and this document does not describe it. 1352 4.2.2. micro_version 1354 "micro_version" specifies the micro-version of the FFV1 bitstream. 1356 After a version is considered stable (a micro-version value is 1357 assigned to be the first stable variant of a specific version), each 1358 new micro-version after this first stable variant is compatible with 1359 the previous micro-version: decoders SHOULD NOT reject FFV1 1360 bitstreams due to an unknown micro-version equal or above the micro- 1361 version considered as stable. 1363 Meaning of "micro_version" for "version" 3: 1365 +=======+=========================+ 1366 | value | micro_version | 1367 +=======+=========================+ 1368 | 0...3 | reserved* | 1369 +-------+-------------------------+ 1370 | 4 | first stable variant | 1371 +-------+-------------------------+ 1372 | Other | reserved for future use | 1373 +-------+-------------------------+ 1375 Table 6: The definitions for 1376 "micro_version" values for FFV1 1377 version 3. 1379 * development versions may be incompatible with the stable variants. 1381 Meaning of "micro_version" for "version" 4 (note: at the time of 1382 writing of this specification, version 4 is not considered stable so 1383 the first stable "micro_version" value is to be announced in the 1384 future): 1386 +=========+=========================+ 1387 | value | micro_version | 1388 +=========+=========================+ 1389 | 0...TBA | reserved* | 1390 +---------+-------------------------+ 1391 | TBA | first stable variant | 1392 +---------+-------------------------+ 1393 | Other | reserved for future use | 1394 +---------+-------------------------+ 1396 Table 7: The definitions for 1397 "micro_version" values for FFV1 1398 version 4. 1400 * development versions which may be incompatible with the stable 1401 variants. 1403 4.2.3. coder_type 1405 "coder_type" specifies the coder used. 1407 +=======+=================================================+ 1408 | value | coder used | 1409 +=======+=================================================+ 1410 | 0 | Golomb Rice | 1411 +-------+-------------------------------------------------+ 1412 | 1 | Range Coder with default state transition table | 1413 +-------+-------------------------------------------------+ 1414 | 2 | Range Coder with custom state transition table | 1415 +-------+-------------------------------------------------+ 1416 | Other | reserved for future use | 1417 +-------+-------------------------------------------------+ 1419 Table 8 1421 Restrictions: 1423 If "coder_type" is 0, then "bits_per_raw_sample" SHOULD NOT be > 8. 1425 Background: At the time of this writing, there is no known 1426 implementation of FFV1 bitstream supporting Golomb Rice algorithm 1427 with "bits_per_raw_sample" greater than 8, and Range Coder is 1428 prefered. 1430 4.2.4. state_transition_delta 1432 "state_transition_delta" specifies the Range coder custom state 1433 transition table. 1435 If "state_transition_delta" is not present in the FFV1 bitstream, all 1436 Range coder custom state transition table elements are assumed to be 1437 0. 1439 4.2.5. colorspace_type 1441 "colorspace_type" specifies the color space encoded, the pixel 1442 transformation used by the encoder, the extra plane content, as well 1443 as interleave method. 1445 +=======+=============+================+==============+=============+ 1446 | value | color space | pixel | extra plane | interleave | 1447 | | encoded | transformation | content | method | 1448 +=======+=============+================+==============+=============+ 1449 | 0 | YCbCr | None | Transparency | "Plane" | 1450 | | | | | then | 1451 | | | | | "Line" | 1452 +-------+-------------+----------------+--------------+-------------+ 1453 | 1 | RGB | JPEG2000-RCT | Transparency | "Line" | 1454 | | | | | then | 1455 | | | | | "Plane" | 1456 +-------+-------------+----------------+--------------+-------------+ 1457 | Other | reserved | reserved for | reserved for | reserved | 1458 | | for future | future use | future use | for future | 1459 | | use | | | use | 1460 +-------+-------------+----------------+--------------+-------------+ 1462 Table 9 1464 FFV1 bitstreams with "colorspace_type" == 1 && ("chroma_planes" != 1465 1 || "log2_h_chroma_subsample" != 0 || "log2_v_chroma_subsample" != 1466 0) are not part of this specification. 1468 4.2.6. chroma_planes 1470 "chroma_planes" indicates if chroma (color) "Planes" are present. 1472 +=======+=================================+ 1473 | value | presence | 1474 +=======+=================================+ 1475 | 0 | chroma "Planes" are not present | 1476 +-------+---------------------------------+ 1477 | 1 | chroma "Planes" are present | 1478 +-------+---------------------------------+ 1480 Table 10 1482 4.2.7. bits_per_raw_sample 1484 "bits_per_raw_sample" indicates the number of bits for each "Sample". 1485 Inferred to be 8 if not present. 1487 +=======+===================================+ 1488 | value | bits for each sample | 1489 +=======+===================================+ 1490 | 0 | reserved* | 1491 +-------+-----------------------------------+ 1492 | Other | the actual bits for each "Sample" | 1493 +-------+-----------------------------------+ 1495 Table 11 1497 * Encoders MUST NOT store "bits_per_raw_sample" = 0. Decoders SHOULD 1498 accept and interpret "bits_per_raw_sample" = 0 as 8. 1500 4.2.8. log2_h_chroma_subsample 1502 "log2_h_chroma_subsample" indicates the subsample factor, stored in 1503 powers to which the number 2 must be raised, between luma and chroma 1504 width ("chroma_width = 2 ^ -log2_h_chroma_subsample * luma_width"). 1506 4.2.9. log2_v_chroma_subsample 1508 "log2_v_chroma_subsample" indicates the subsample factor, stored in 1509 powers to which the number 2 must be raised, between luma and chroma 1510 height ("chroma_height = 2 ^ -log2_v_chroma_subsample * 1511 luma_height"). 1513 4.2.10. extra_plane 1515 "extra_plane" indicates if an extra "Plane" is present. 1517 +=======+==============================+ 1518 | value | presence | 1519 +=======+==============================+ 1520 | 0 | extra "Plane" is not present | 1521 +-------+------------------------------+ 1522 | 1 | extra "Plane" is present | 1523 +-------+------------------------------+ 1525 Table 12 1527 4.2.11. num_h_slices 1529 "num_h_slices" indicates the number of horizontal elements of the 1530 slice raster. 1532 Inferred to be 1 if not present. 1534 4.2.12. num_v_slices 1536 "num_v_slices" indicates the number of vertical elements of the slice 1537 raster. 1539 Inferred to be 1 if not present. 1541 4.2.13. quant_table_set_count 1543 "quant_table_set_count" indicates the number of Quantization 1544 Table Sets. "quant_table_set_count" MUST be less than or equal to 8. 1546 Inferred to be 1 if not present. 1548 MUST NOT be 0. 1550 4.2.14. states_coded 1552 "states_coded" indicates if the respective Quantization Table Set has 1553 the initial states coded. 1555 Inferred to be 0 if not present. 1557 +=======+================================+ 1558 | value | initial states | 1559 +=======+================================+ 1560 | 0 | initial states are not present | 1561 | | and are assumed to be all 128 | 1562 +-------+--------------------------------+ 1563 | 1 | initial states are present | 1564 +-------+--------------------------------+ 1566 Table 13 1568 4.2.15. initial_state_delta 1570 "initial_state_delta[ i ][ j ][ k ]" indicates the initial Range 1571 coder state, it is encoded using "k" as context index and 1573 pred = j ? initial_states[ i ][j - 1][ k ] : 128 1575 Figure 23 1577 initial_state[ i ][ j ][ k ] = 1578 ( pred + initial_state_delta[ i ][ j ][ k ] ) & 255 1580 Figure 24 1582 4.2.16. ec 1584 "ec" indicates the error detection/correction type. 1586 +=======+=================================================+ 1587 | value | error detection/correction type | 1588 +=======+=================================================+ 1589 | 0 | 32-bit CRC in "ConfigurationRecord" | 1590 +-------+-------------------------------------------------+ 1591 | 1 | 32-bit CRC in "Slice" and "ConfigurationRecord" | 1592 +-------+-------------------------------------------------+ 1593 | Other | reserved for future use | 1594 +-------+-------------------------------------------------+ 1596 Table 14 1598 4.2.17. intra 1600 "intra" indicates the constraint on "keyframe" in each instance of 1601 "Frame". 1603 Inferred to be 0 if not present. 1605 +=======+=======================================================+ 1606 | value | relationship | 1607 +=======+=======================================================+ 1608 | 0 | "keyframe" can be 0 or 1 (non keyframes or keyframes) | 1609 +-------+-------------------------------------------------------+ 1610 | 1 | "keyframe" MUST be 1 (keyframes only) | 1611 +-------+-------------------------------------------------------+ 1612 | Other | reserved for future use | 1613 +-------+-------------------------------------------------------+ 1615 Table 15 1617 4.3. Configuration Record 1619 In the case of a FFV1 bitstream with "version >= 3", a "Configuration 1620 Record" is stored in the underlying "Container" as described in 1621 Section 4.3.3. It contains the "Parameters" used for all instances 1622 of "Frame". The size of the "Configuration Record", "NumBytes", is 1623 supplied by the underlying "Container". 1625 pseudo-code | type 1626 -----------------------------------------------------------|----- 1627 ConfigurationRecord( NumBytes ) { | 1628 ConfigurationRecordIsPresent = 1 | 1629 Parameters( ) | 1630 while (remaining_symbols_in_syntax(NumBytes - 4)) { | 1631 reserved_for_future_use | br/ur/sr 1632 } | 1633 configuration_record_crc_parity | u(32) 1634 } | 1636 4.3.1. reserved_for_future_use 1638 "reserved_for_future_use" has semantics that are reserved for future 1639 use. 1641 Encoders conforming to this version of this specification SHALL NOT 1642 write this value. 1644 Decoders conforming to this version of this specification SHALL 1645 ignore its value. 1647 4.3.2. configuration_record_crc_parity 1649 "configuration_record_crc_parity" 32 bits that are chosen so that the 1650 "Configuration Record" as a whole has a CRC remainder of 0. 1652 This is equivalent to storing the CRC remainder in the 32-bit parity. 1654 The CRC generator polynomial used is described in Section 4.9.3. 1656 4.3.3. Mapping FFV1 into Containers 1658 This "Configuration Record" can be placed in any file format 1659 supporting "Configuration Records", fitting as much as possible with 1660 how the file format uses to store "Configuration Records". The 1661 "Configuration Record" storage place and "NumBytes" are currently 1662 defined and supported by this version of this specification for the 1663 following formats: 1665 4.3.3.1. AVI File Format 1667 The "Configuration Record" extends the stream format chunk ("AVI ", 1668 "hdlr", "strl", "strf") with the ConfigurationRecord bitstream. 1670 See [AVI] for more information about chunks. 1672 "NumBytes" is defined as the size, in bytes, of the strf chunk 1673 indicated in the chunk header minus the size of the stream format 1674 structure. 1676 4.3.3.2. ISO Base Media File Format 1678 The "Configuration Record" extends the sample description box 1679 ("moov", "trak", "mdia", "minf", "stbl", "stsd") with a "glbl" box 1680 that contains the ConfigurationRecord bitstream. See 1681 [ISO.14496-12.2015] for more information about boxes. 1683 "NumBytes" is defined as the size, in bytes, of the "glbl" box 1684 indicated in the box header minus the size of the box header. 1686 4.3.3.3. NUT File Format 1688 The "codec_specific_data" element (in "stream_header" packet) 1689 contains the ConfigurationRecord bitstream. See [NUT] for more 1690 information about elements. 1692 "NumBytes" is defined as the size, in bytes, of the 1693 "codec_specific_data" element as indicated in the "length" field of 1694 "codec_specific_data". 1696 4.3.3.4. Matroska File Format 1698 FFV1 SHOULD use "V_FFV1" as the Matroska "Codec ID". For FFV1 1699 versions 2 or less, the Matroska "CodecPrivate" Element SHOULD NOT be 1700 used. For FFV1 versions 3 or greater, the Matroska "CodecPrivate" 1701 Element MUST contain the FFV1 "Configuration Record" structure and no 1702 other data. See [Matroska] for more information about elements. 1704 "NumBytes" is defined as the "Element Data Size" of the 1705 "CodecPrivate" Element. 1707 4.4. Frame 1709 A "Frame" is an encoded representation of a complete static image. 1710 The whole "Frame" is provided by the underlaying container. 1712 A "Frame" consists of the "keyframe" field, "Parameters" (if 1713 "version" <= 1), and a sequence of independent slices. The pseudo- 1714 code below describes the contents of a "Frame". 1716 "keyframe" field has its own initial state, set to 128. 1718 pseudo-code | type 1719 --------------------------------------------------------------|----- 1720 Frame( NumBytes ) { | 1721 keyframe | br 1722 if (keyframe && !ConfigurationRecordIsPresent { | 1723 Parameters( ) | 1724 } | 1725 while (remaining_bits_in_bitstream( NumBytes )) { | 1726 Slice( ) | 1727 } | 1728 } | 1730 Architecture overview of slices in a "Frame": 1732 +=================================================================+ 1733 +=================================================================+ 1734 | first slice header | 1735 +-----------------------------------------------------------------+ 1736 | first slice content | 1737 +-----------------------------------------------------------------+ 1738 | first slice footer | 1739 +-----------------------------------------------------------------+ 1740 | --------------------------------------------------------------- | 1741 +-----------------------------------------------------------------+ 1742 | second slice header | 1743 +-----------------------------------------------------------------+ 1744 | second slice content | 1745 +-----------------------------------------------------------------+ 1746 | second slice footer | 1747 +-----------------------------------------------------------------+ 1748 | --------------------------------------------------------------- | 1749 +-----------------------------------------------------------------+ 1750 | ... | 1751 +-----------------------------------------------------------------+ 1752 | --------------------------------------------------------------- | 1753 +-----------------------------------------------------------------+ 1754 | last slice header | 1755 +-----------------------------------------------------------------+ 1756 | last slice content | 1757 +-----------------------------------------------------------------+ 1758 | last slice footer | 1759 +-----------------------------------------------------------------+ 1761 Table 16 1763 4.5. Slice 1765 A "Slice" is an independent spatial sub-section of a "Frame" that is 1766 encoded separately from another region of the same "Frame". The use 1767 of more than one "Slice" per "Frame" can be useful for taking 1768 advantage of the opportunities of multithreaded encoding and 1769 decoding. 1771 A "Slice" consists of a "Slice Header" (when relevant), a "Slice 1772 Content", and a "Slice Footer" (when relevant). The pseudo-code 1773 below describes the contents of a "Slice". 1775 pseudo-code | type 1776 --------------------------------------------------------------|----- 1777 Slice( ) { | 1778 if (version >= 3) { | 1779 SliceHeader( ) | 1780 } | 1781 SliceContent( ) | 1782 if (coder_type == 0) { | 1783 while (!byte_aligned()) { | 1784 padding | u(1) 1785 } | 1786 } | 1787 if (version <= 1) { | 1788 while (remaining_bits_in_bitstream( NumBytes ) != 0) {| 1789 reserved | u(1) 1790 } | 1791 } | 1792 if (version >= 3) { | 1793 SliceFooter( ) | 1794 } | 1795 } | 1797 "padding" specifies a bit without any significance and used only for 1798 byte alignment. MUST be 0. 1800 "reserved" specifies a bit without any significance in this revision 1801 of the specification and may have a significance in a later revision 1802 of this specification. 1804 Encoders SHOULD NOT fill these bits. 1806 Decoders SHOULD ignore these bits. 1808 4.6. Slice Header 1810 A "Slice Header" provides information about the decoding 1811 configuration of the "Slice", such as its spatial position, size, and 1812 aspect ratio. The pseudo-code below describes the contents of the 1813 "Slice Header". 1815 "Slice Header" has its own initial states, all set to 128. 1817 pseudo-code | type 1818 --------------------------------------------------------------|----- 1819 SliceHeader( ) { | 1820 slice_x | ur 1821 slice_y | ur 1822 slice_width - 1 | ur 1823 slice_height - 1 | ur 1824 for (i = 0; i < quant_table_set_index_count; i++) { | 1825 quant_table_set_index[ i ] | ur 1826 } | 1827 picture_structure | ur 1828 sar_num | ur 1829 sar_den | ur 1830 if (version >= 4) { | 1831 reset_contexts | br 1832 slice_coding_mode | ur 1833 } | 1834 } | 1836 4.6.1. slice_x 1838 "slice_x" indicates the x position on the slice raster formed by 1839 num_h_slices. 1841 Inferred to be 0 if not present. 1843 4.6.2. slice_y 1845 "slice_y" indicates the y position on the slice raster formed by 1846 num_v_slices. 1848 Inferred to be 0 if not present. 1850 4.6.3. slice_width 1852 "slice_width" indicates the width on the slice raster formed by 1853 num_h_slices. 1855 Inferred to be 1 if not present. 1857 4.6.4. slice_height 1859 "slice_height" indicates the height on the slice raster formed by 1860 num_v_slices. 1862 Inferred to be 1 if not present. 1864 4.6.5. quant_table_set_index_count 1866 "quant_table_set_index_count" is defined as: 1868 1 + ( ( chroma_planes || version <= 3 ) ? 1 : 0 ) 1869 + ( extra_plane ? 1 : 0 ) 1871 4.6.6. quant_table_set_index 1873 "quant_table_set_index" indicates the Quantization Table Set index to 1874 select the Quantization Table Set and the initial states for the 1875 "Slice Content". 1877 Inferred to be 0 if not present. 1879 4.6.7. picture_structure 1881 "picture_structure" specifies the temporal and spatial relationship 1882 of each "Line" of the "Frame". 1884 Inferred to be 0 if not present. 1886 +=======+=========================+ 1887 | value | picture structure used | 1888 +=======+=========================+ 1889 | 0 | unknown | 1890 +-------+-------------------------+ 1891 | 1 | top field first | 1892 +-------+-------------------------+ 1893 | 2 | bottom field first | 1894 +-------+-------------------------+ 1895 | 3 | progressive | 1896 +-------+-------------------------+ 1897 | Other | reserved for future use | 1898 +-------+-------------------------+ 1900 Table 17 1902 4.6.8. sar_num 1904 "sar_num" specifies the "Sample" aspect ratio numerator. 1906 Inferred to be 0 if not present. 1908 A value of 0 means that aspect ratio is unknown. 1910 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1912 If "sar_den" is 0, decoders SHOULD ignore the encoded value and 1913 consider that "sar_num" is 0. 1915 4.6.9. sar_den 1917 "sar_den" specifies the "Sample" aspect ratio denominator. 1919 Inferred to be 0 if not present. 1921 A value of 0 means that aspect ratio is unknown. 1923 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1925 If "sar_num" is 0, decoders SHOULD ignore the encoded value and 1926 consider that "sar_den" is 0. 1928 4.6.10. reset_contexts 1930 "reset_contexts" indicates if slice contexts must be reset. 1932 Inferred to be 0 if not present. 1934 4.6.11. slice_coding_mode 1936 "slice_coding_mode" indicates the slice coding mode. 1938 Inferred to be 0 if not present. 1940 +=======+=============================+ 1941 | value | slice coding mode | 1942 +=======+=============================+ 1943 | 0 | Range Coding or Golomb Rice | 1944 +-------+-----------------------------+ 1945 | 1 | raw PCM | 1946 +-------+-----------------------------+ 1947 | Other | reserved for future use | 1948 +-------+-----------------------------+ 1950 Table 18 1952 4.7. Slice Content 1954 A "Slice Content" contains all "Line" elements part of the "Slice". 1956 Depending on the configuration, "Line" elements are ordered by 1957 "Plane" then by row (YCbCr) or by row then by "Plane" (RGB). 1959 pseudo-code | type 1960 --------------------------------------------------------------|----- 1961 SliceContent( ) { | 1962 if (colorspace_type == 0) { | 1963 for (p = 0; p < primary_color_count; p++) { | 1964 for (y = 0; y < plane_pixel_height[ p ]; y++) { | 1965 Line( p, y ) | 1966 } | 1967 } | 1968 } else if (colorspace_type == 1) { | 1969 for (y = 0; y < slice_pixel_height; y++) { | 1970 for (p = 0; p < primary_color_count; p++) { | 1971 Line( p, y ) | 1972 } | 1973 } | 1974 } | 1975 } | 1977 4.7.1. primary_color_count 1979 "primary_color_count" is defined as: 1981 1 + ( chroma_planes ? 2 : 0 ) + ( extra_plane ? 1 : 0 ) 1983 4.7.2. plane_pixel_height 1985 "plane_pixel_height[ p ]" is the height in "Pixels" of "Plane" p of 1986 the "Slice". It is defined as: 1988 chroma_planes == 1 && (p == 1 || p == 2) 1989 ? ceil(slice_pixel_height / (1 << log2_v_chroma_subsample)) 1990 : slice_pixel_height 1992 4.7.3. slice_pixel_height 1994 "slice_pixel_height" is the height in pixels of the slice. It is 1995 defined as: 1997 floor( 1998 ( slice_y + slice_height ) 1999 * slice_pixel_height 2000 / num_v_slices 2001 ) - slice_pixel_y. 2003 4.7.4. slice_pixel_y 2005 "slice_pixel_y" is the slice vertical position in pixels. It is 2006 defined as: 2008 floor( slice_y * frame_pixel_height / num_v_slices ) 2010 4.8. Line 2012 A "Line" is a list of the sample differences (relative to the 2013 predictor) of primary color components. The pseudo-code below 2014 describes the contents of the "Line". 2016 pseudo-code | type 2017 --------------------------------------------------------------|----- 2018 Line( p, y ) { | 2019 if (colorspace_type == 0) { | 2020 for (x = 0; x < plane_pixel_width[ p ]; x++) { | 2021 sample_difference[ p ][ y ][ x ] | sd 2022 } | 2023 } else if (colorspace_type == 1) { | 2024 for (x = 0; x < slice_pixel_width; x++) { | 2025 sample_difference[ p ][ y ][ x ] | sd 2026 } | 2027 } | 2028 } | 2030 4.8.1. plane_pixel_width 2032 "plane_pixel_width[ p ]" is the width in "Pixels" of "Plane" p of the 2033 "Slice". It is defined as: 2035 chroma\_planes == 1 && (p == 1 || p == 2) 2036 ? ceil( slice_pixel_width / (1 << log2_h_chroma_subsample) ) 2037 : slice_pixel_width. 2039 4.8.2. slice_pixel_width 2041 "slice_pixel_width" is the width in "Pixels" of the slice. It is 2042 defined as: 2044 floor( 2045 ( slice_x + slice_width ) 2046 * slice_pixel_width 2047 / num_h_slices 2048 ) - slice_pixel_x 2050 4.8.3. slice_pixel_x 2052 "slice_pixel_x" is the slice horizontal position in "Pixels". It is 2053 defined as: 2055 floor( slice_x * frame_pixel_width / num_h_slices ) 2057 4.8.4. sample_difference 2059 "sample_difference[ p ][ y ][ x ]" is the sample difference for 2060 "Sample" at "Plane" "p", y position "y", and x position "x". The 2061 "Sample" value is computed based on median predictor and context 2062 described in Section 3.2. 2064 4.9. Slice Footer 2066 A "Slice Footer" provides information about slice size and 2067 (optionally) parity. The pseudo-code below describes the contents of 2068 the "Slice Footer". 2070 Note: "Slice Footer" is always byte aligned. 2072 pseudo-code | type 2073 --------------------------------------------------------------|----- 2074 SliceFooter( ) { | 2075 slice_size | u(24) 2076 if (ec) { | 2077 error_status | u(8) 2078 slice_crc_parity | u(32) 2079 } | 2080 } | 2082 4.9.1. slice_size 2084 "slice_size" indicates the size of the slice in bytes. 2086 Note: this allows finding the start of slices before previous slices 2087 have been fully decoded, and allows parallel decoding as well as 2088 error resilience. 2090 4.9.2. error_status 2092 "error_status" specifies the error status. 2094 +=======+======================================+ 2095 | value | error status | 2096 +=======+======================================+ 2097 | 0 | no error | 2098 +-------+--------------------------------------+ 2099 | 1 | slice contains a correctable error | 2100 +-------+--------------------------------------+ 2101 | 2 | slice contains a uncorrectable error | 2102 +-------+--------------------------------------+ 2103 | Other | reserved for future use | 2104 +-------+--------------------------------------+ 2106 Table 19 2108 4.9.3. slice_crc_parity 2110 "slice_crc_parity" 32 bits that are chosen so that the slice as a 2111 whole has a crc remainder of 0. 2113 This is equivalent to storing the crc remainder in the 32-bit parity. 2115 The CRC generator polynomial used is the standard IEEE CRC polynomial 2116 (0x104C11DB7), with initial value 0, without pre-inversion and 2117 without post-inversion. 2119 5. Restrictions 2121 To ensure that fast multithreaded decoding is possible, starting with 2122 version 3 and if "frame_pixel_width * frame_pixel_height" is more 2123 than 101376, "slice_width * slice_height" MUST be less or equal to 2124 "num_h_slices * num_v_slices / 4". Note: 101376 is the frame size in 2125 "Pixels" of a 352x288 frame also known as CIF ("Common Intermediate 2126 Format") frame size format. 2128 For each "Frame", each position in the slice raster MUST be filled by 2129 one and only one slice of the "Frame" (no missing slice position, no 2130 slice overlapping). 2132 For each "Frame" with "keyframe" value of 0, each slice MUST have the 2133 same value of "slice_x", "slice_y", "slice_width", "slice_height" as 2134 a slice in the previous "Frame", except if "reset_contexts" is 1. 2136 6. Security Considerations 2138 Like any other codec, (such as [RFC6716]), FFV1 should not be used 2139 with insecure ciphers or cipher-modes that are vulnerable to known 2140 plaintext attacks. Some of the header bits as well as the padding 2141 are easily predictable. 2143 Implementations of the FFV1 codec need to take appropriate security 2144 considerations into account, as outlined in [RFC4732]. It is 2145 extremely important for the decoder to be robust against malicious 2146 payloads. Malicious payloads must not cause the decoder to overrun 2147 its allocated memory or to take an excessive amount of resources to 2148 decode. The same applies to the encoder, even though problems in 2149 encoders are typically rarer. Malicious video streams must not cause 2150 the encoder to misbehave because this would allow an attacker to 2151 attack transcoding gateways. A frequent security problem in image 2152 and video codecs is also to not check for integer overflows, for 2153 example to allocate "frame_pixel_width * frame_pixel_height" in 2154 "Pixel" count computations without considering that the 2155 multiplication result may have overflowed the arithmetic types range. 2156 The range coder could, if implemented naively, read one byte over the 2157 end. The implementation must ensure that no read outside allocated 2158 and initialized memory occurs. 2160 None of the content carried in FFV1 is intended to be executable. 2162 The reference implementation [REFIMPL] contains no known buffer 2163 overflow or cases where a specially crafted packet or video segment 2164 could cause a significant increase in CPU load. 2166 The reference implementation [REFIMPL] was validated in the following 2167 conditions: 2169 * Sending the decoder valid packets generated by the reference 2170 encoder and verifying that the decoder's output matches the 2171 encoder's input. 2173 * Sending the decoder packets generated by the reference encoder and 2174 then subjected to random corruption. 2176 * Sending the decoder random packets that are not FFV1. 2178 In all of the conditions above, the decoder and encoder was run 2179 inside the [VALGRIND] memory debugger as well as clangs address 2180 sanitizer [Address-Sanitizer], which track reads and writes to 2181 invalid memory regions as well as the use of uninitialized memory. 2182 There were no errors reported on any of the tested conditions. 2184 7. Media Type Definition 2186 This registration is done using the template defined in [RFC6838] and 2187 following [RFC4855]. 2189 Type name: video 2191 Subtype name: FFV1 2193 Required parameters: None. 2195 Optional parameters: These parameters are used to signal the 2196 capabilities of a receiver implementation. These parameters MUST NOT 2197 be used for any other purpose. 2199 * "version": The "version" of the FFV1 encoding as defined by 2200 Section 4.2.1. 2202 * "micro_version": The "micro_version" of the FFV1 encoding as 2203 defined by Section 4.2.2. 2205 * "coder_type": The "coder_type" of the FFV1 encoding as defined by 2206 Section 4.2.3. 2208 * "colorspace_type": The "colorspace_type" of the FFV1 encoding as 2209 defined by Section 4.2.5. 2211 * "bits_per_raw_sample": The "bits_per_raw_sample" of the FFV1 2212 encoding as defined by Section 4.2.7. 2214 * "max_slices": The value of "max_slices" is an integer indicating 2215 the maximum count of slices with a frames of the FFV1 encoding. 2217 Encoding considerations: This media type is defined for encapsulation 2218 in several audiovisual container formats and contains binary data; 2219 see Section 4.3.3. This media type is framed binary data; see 2220 Section 4.8 of [RFC6838]. 2222 Security considerations: See Section 6 of this document. 2224 Interoperability considerations: None. 2226 Published specification: RFC XXXX. 2228 [RFC Editor: Upon publication as an RFC, please replace "XXXX" with 2229 the number assigned to this document and remove this note.] 2231 Applications which use this media type: Any application that requires 2232 the transport of lossless video can use this media type. Some 2233 examples are, but not limited to screen recording, scientific 2234 imaging, and digital video preservation. 2236 Fragment identifier considerations: N/A. 2238 Additional information: None. 2240 Person & email address to contact for further information: Michael 2241 Niedermayer michael@niedermayer.cc (mailto:michael@niedermayer.cc) 2243 Intended usage: COMMON 2245 Restrictions on usage: None. 2247 Author: Dave Rice dave@dericed.com (mailto:dave@dericed.com) 2249 Change controller: IETF cellar working group delegated from the IESG. 2251 8. IANA Considerations 2253 The IANA is requested to register the following values: 2255 * Media type registration as described in Section 7. 2257 9. Changelog 2259 See https://github.com/FFmpeg/FFV1/commits/master 2260 (https://github.com/FFmpeg/FFV1/commits/master) 2262 [RFC Editor: Please remove this Changelog section prior to 2263 publication.] 2265 10. Normative References 2267 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 2268 Requirement Levels", BCP 14, RFC 2119, 2269 DOI 10.17487/RFC2119, March 1997, 2270 . 2272 [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 2273 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, 2274 May 2017, . 2276 [RFC4732] Handley, M., Ed., Rescorla, E., Ed., and IAB, "Internet 2277 Denial-of-Service Considerations", RFC 4732, 2278 DOI 10.17487/RFC4732, December 2006, 2279 . 2281 [ISO.9899.2018] 2282 International Organization for Standardization, 2283 "Programming languages - C", ISO Standard 9899, 2018. 2285 [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type 2286 Specifications and Registration Procedures", BCP 13, 2287 RFC 6838, DOI 10.17487/RFC6838, January 2013, 2288 . 2290 [ISO.9899.1990] 2291 International Organization for Standardization, 2292 "Programming languages - C", ISO Standard 9899, 1990. 2294 [ISO.15444-1.2016] 2295 International Organization for Standardization, 2296 "Information technology -- JPEG 2000 image coding system: 2297 Core coding system", October 2016. 2299 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 2300 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716, 2301 September 2012, . 2303 [RFC4855] Casner, S., "Media Type Registration of RTP Payload 2304 Formats", RFC 4855, DOI 10.17487/RFC4855, February 2007, 2305 . 2307 [Matroska] IETF, "Matroska", 2019, . 2310 11. Informative References 2312 [I-D.ietf-cellar-ffv1] 2313 Niedermayer, M., Rice, D., and J. Martinez, "FFV1 Video 2314 Coding Format Version 0, 1, and 3", Work in Progress, 2315 Internet-Draft, draft-ietf-cellar-ffv1-16, 2 July 2020, 2316 . 2318 [ISO.14495-1.1999] 2319 International Organization for Standardization, 2320 "Information technology -- Lossless and near-lossless 2321 compression of continuous-tone still images: Baseline", 2322 December 1999. 2324 [NUT] Niedermayer, M., "NUT Open Container Format", December 2325 2013, . 2327 [YCbCr] Wikipedia, "YCbCr", undated, 2328 . 2330 [ISO.14496-10.2014] 2331 International Organization for Standardization, 2332 "Information technology -- Coding of audio-visual objects 2333 -- Part 10: Advanced Video Coding", September 2014. 2335 [VALGRIND] Valgrind Developers, "Valgrind website", undated, 2336 . 2338 [REFIMPL] Niedermayer, M., "The reference FFV1 implementation / the 2339 FFV1 codec in FFmpeg", undated, . 2341 [HuffYUV] Rudiak-Gould, B., "HuffYUV", December 2003, 2342 . 2345 [ISO.14496-12.2015] 2346 International Organization for Standardization, 2347 "Information technology -- Coding of audio-visual objects 2348 -- Part 12: ISO base media file format", December 2015. 2350 [range-coding] 2351 Nigel, G. and N. Martin, "Range encoding: an algorithm for 2352 removing redundancy from a digitised message.", 2353 Proceedings of the Conference on Video and Data 2354 Recording. Institution of Electronic and Radio Engineers, 2355 Hampshire, England, July 1979. 2357 [AVI] Microsoft, "AVI RIFF File Reference", undated, 2358 . 2361 [Address-Sanitizer] 2362 The Clang Team, "ASAN AddressSanitizer website", undated, 2363 . 2365 Appendix A. Multi-theaded decoder implementation suggestions 2367 This appendix is informative. 2369 The FFV1 bitstream is parsable in two ways: in sequential order as 2370 described in this document or with the pre-analysis of the footer of 2371 each slice. Each slice footer contains a "slice_size" field so the 2372 boundary of each slice is computable without having to parse the 2373 slice content. That allows multi-threading as well as independence 2374 of slice content (a bitstream error in a slice header or slice 2375 content has no impact on the decoding of the other slices). 2377 After having checked "keyframe" field, a decoder SHOULD parse 2378 "slice_size" fields, from "slice_size" of the last slice at the end 2379 of the "Frame" up to "slice_size" of the first slice at the beginning 2380 of the "Frame", before parsing slices, in order to have slices 2381 boundaries. A decoder MAY fallback on sequential order e.g. in case 2382 of a corrupted "Frame" (frame size unknown, "slice_size" of slices 2383 not coherent...) or if there is no possibility of seeking into the 2384 stream. 2386 Appendix B. Future handling of some streams created by non conforming 2387 encoders 2389 This appendix is informative. 2391 Some bitstreams were found with 40 extra bits corresponding to 2392 "error_status" and "slice_crc_parity" in the "reserved" bits of 2393 "Slice()". Any revision of this specification SHOULD care about 2394 avoiding to add 40 bits of content after "SliceContent" if "version" 2395 == 0 or "version" == 1. Else a decoder conforming to the revised 2396 specification could not distinguish between a revised bitstream and 2397 such buggy bitstream in the wild. 2399 Authors' Addresses 2401 Michael Niedermayer 2403 Email: michael@niedermayer.cc 2405 Dave Rice 2407 Email: dave@dericed.com 2409 Jerome Martinez 2410 Email: jerome@mediaarea.net