idnits 2.17.1 draft-ietf-cellar-ffv1-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 : ---------------------------------------------------------------------------- ** There is 1 instance of too long lines in the document, the longest one being 5 characters in excess of 72. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (26 May 2020) is 1430 days in the past. Is this intentional? Checking references for intended status: Informational ---------------------------------------------------------------------------- No issues found here. Summary: 1 error (**), 0 flaws (~~), 1 warning (==), 1 comment (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 cellar M. Niedermayer 3 Internet-Draft 4 Intended status: Informational D. Rice 5 Expires: 27 November 2020 6 J. Martinez 7 26 May 2020 9 FFV1 Video Coding Format Version 0, 1, and 3 10 draft-ietf-cellar-ffv1-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 27 November 2020. 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 . . . . . . . . . . . . . . . . . . 5 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 . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . 8 66 3. Sample Coding . . . . . . . . . . . . . . . . . . . . . . . . 9 67 3.1. Border . . . . . . . . . . . . . . . . . . . . . . . . . 9 68 3.2. Samples . . . . . . . . . . . . . . . . . . . . . . . . . 10 69 3.3. Median Predictor . . . . . . . . . . . . . . . . . . . . 10 70 3.4. Context . . . . . . . . . . . . . . . . . . . . . . . . . 11 71 3.5. Quantization Table Sets . . . . . . . . . . . . . . . . . 12 72 3.6. Quantization Table Set Indexes . . . . . . . . . . . . . 12 73 3.7. Color spaces . . . . . . . . . . . . . . . . . . . . . . 12 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 . . . . . . . . . . . . . . . . . . 20 79 4. Bitstream . . . . . . . . . . . . . . . . . . . . . . . . . . 25 80 4.1. Parameters . . . . . . . . . . . . . . . . . . . . . . . 26 81 4.1.1. version . . . . . . . . . . . . . . . . . . . . . . . 28 82 4.1.2. micro_version . . . . . . . . . . . . . . . . . . . . 28 83 4.1.3. coder_type . . . . . . . . . . . . . . . . . . . . . 29 84 4.1.4. state_transition_delta . . . . . . . . . . . . . . . 29 85 4.1.5. colorspace_type . . . . . . . . . . . . . . . . . . . 30 86 4.1.6. chroma_planes . . . . . . . . . . . . . . . . . . . . 30 87 4.1.7. bits_per_raw_sample . . . . . . . . . . . . . . . . . 31 88 4.1.8. log2_h_chroma_subsample . . . . . . . . . . . . . . . 31 89 4.1.9. log2_v_chroma_subsample . . . . . . . . . . . . . . . 31 90 4.1.10. extra_plane . . . . . . . . . . . . . . . . . . . . . 31 91 4.1.11. num_h_slices . . . . . . . . . . . . . . . . . . . . 32 92 4.1.12. num_v_slices . . . . . . . . . . . . . . . . . . . . 32 93 4.1.13. quant_table_set_count . . . . . . . . . . . . . . . . 32 94 4.1.14. states_coded . . . . . . . . . . . . . . . . . . . . 32 95 4.1.15. initial_state_delta . . . . . . . . . . . . . . . . . 32 96 4.1.16. ec . . . . . . . . . . . . . . . . . . . . . . . . . 33 97 4.1.17. intra . . . . . . . . . . . . . . . . . . . . . . . . 33 98 4.2. Configuration Record . . . . . . . . . . . . . . . . . . 34 99 4.2.1. reserved_for_future_use . . . . . . . . . . . . . . . 34 100 4.2.2. configuration_record_crc_parity . . . . . . . . . . . 34 101 4.2.3. Mapping FFV1 into Containers . . . . . . . . . . . . 34 102 4.3. Frame . . . . . . . . . . . . . . . . . . . . . . . . . . 35 103 4.4. Slice . . . . . . . . . . . . . . . . . . . . . . . . . . 37 104 4.5. Slice Header . . . . . . . . . . . . . . . . . . . . . . 38 105 4.5.1. slice_x . . . . . . . . . . . . . . . . . . . . . . . 38 106 4.5.2. slice_y . . . . . . . . . . . . . . . . . . . . . . . 38 107 4.5.3. slice_width . . . . . . . . . . . . . . . . . . . . . 38 108 4.5.4. slice_height . . . . . . . . . . . . . . . . . . . . 38 109 4.5.5. quant_table_set_index_count . . . . . . . . . . . . . 39 110 4.5.6. quant_table_set_index . . . . . . . . . . . . . . . . 39 111 4.5.7. picture_structure . . . . . . . . . . . . . . . . . . 39 112 4.5.8. sar_num . . . . . . . . . . . . . . . . . . . . . . . 39 113 4.5.9. sar_den . . . . . . . . . . . . . . . . . . . . . . . 40 114 4.6. Slice Content . . . . . . . . . . . . . . . . . . . . . . 40 115 4.6.1. primary_color_count . . . . . . . . . . . . . . . . . 40 116 4.6.2. plane_pixel_height . . . . . . . . . . . . . . . . . 41 117 4.6.3. slice_pixel_height . . . . . . . . . . . . . . . . . 41 118 4.6.4. slice_pixel_y . . . . . . . . . . . . . . . . . . . . 41 119 4.7. Line . . . . . . . . . . . . . . . . . . . . . . . . . . 41 120 4.7.1. plane_pixel_width . . . . . . . . . . . . . . . . . . 41 121 4.7.2. slice_pixel_width . . . . . . . . . . . . . . . . . . 42 122 4.7.3. slice_pixel_x . . . . . . . . . . . . . . . . . . . . 42 123 4.7.4. sample_difference . . . . . . . . . . . . . . . . . . 42 124 4.8. Slice Footer . . . . . . . . . . . . . . . . . . . . . . 42 125 4.8.1. slice_size . . . . . . . . . . . . . . . . . . . . . 42 126 4.8.2. error_status . . . . . . . . . . . . . . . . . . . . 43 127 4.8.3. slice_crc_parity . . . . . . . . . . . . . . . . . . 43 128 4.9. Quantization Table Set . . . . . . . . . . . . . . . . . 43 129 4.9.1. quant_tables . . . . . . . . . . . . . . . . . . . . 44 130 4.9.2. context_count . . . . . . . . . . . . . . . . . . . . 44 131 5. Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 45 132 6. Security Considerations . . . . . . . . . . . . . . . . . . . 45 133 7. Media Type Definition . . . . . . . . . . . . . . . . . . . . 46 134 8. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 47 135 9. Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . 48 136 10. Normative References . . . . . . . . . . . . . . . . . . . . 48 137 11. Informative References . . . . . . . . . . . . . . . . . . . 49 138 Appendix A. Multi-theaded decoder implementation suggestions . . 50 139 Appendix B. Future handling of some streams created by non 140 conforming encoders . . . . . . . . . . . . . . . . . . . 50 141 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 51 143 1. Introduction 145 This document describes FFV1, a lossless video encoding format. The 146 design of FFV1 considers the storage of image characteristics, data 147 fixity, and the optimized use of encoding time and storage 148 requirements. FFV1 is designed to support a wide range of lossless 149 video applications such as long-term audiovisual preservation, 150 scientific imaging, screen recording, and other video encoding 151 scenarios that seek to avoid the generational loss of lossy video 152 encodings. 154 This document defines version 0, 1 and 3 of FFV1. The distinctions 155 of the versions are provided throughout the document, but in summary: 157 * Version 0 of FFV1 was the original implementation of FFV1 and has 158 been in non-experimental use since April 14, 2006 [FFV1_V0]. 160 * Version 1 of FFV1 adds support of more video bit depths and has 161 been in use since April 24, 2009 [FFV1_V1]. 163 * Version 2 of FFV1 only existed in experimental form and is not 164 described by this document, but is available as a LyX file at 165 https://github.com/FFmpeg/FFV1/ 166 blob/8ad772b6d61c3dd8b0171979a2cd9f11924d5532/ffv1.lyx 167 (https://github.com/FFmpeg/FFV1/ 168 blob/8ad772b6d61c3dd8b0171979a2cd9f11924d5532/ffv1.lyx). 170 * Version 3 of FFV1 adds several features such as increased 171 description of the characteristics of the encoding images and 172 embedded CRC data to support fixity verification of the encoding. 173 Version 3 has been in non-experimental use since August 17, 2013 174 [FFV1_V3]. 176 This document assumes familiarity with mathematical and coding 177 concepts such as Range coding [range-coding] and YCbCr color spaces 178 [YCbCr]. 180 This specification describes the valid bitstream and how to decode 181 such valid bitstream. Bitstreams not conforming to this 182 specification or how they are handled is outside this specification. 183 A decoder could reject every invalid bitstream or attempt to perform 184 error concealment or re-download or use a redundant copy of the 185 invalid part or any other action it deems appropriate. 187 2. Notation and Conventions 189 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 190 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and 191 "OPTIONAL" in this document are to be interpreted as described in BCP 192 14 [RFC2119] [RFC8174] when, and only when, they appear in all 193 capitals, as shown here. 195 2.1. Definitions 197 "Container": Format that encapsulates "Frames" (see Section 4.3) and 198 (when required) a "Configuration Record" into a bitstream. 200 "Sample": The smallest addressable representation of a color 201 component or a luma component in a "Frame". Examples of "Sample" are 202 Luma, Blue Chrominance, Red Chrominance, Transparency, Red, Green, 203 and Blue. 205 "Plane": A discrete component of a static image comprised of 206 "Samples" that represent a specific quantification of "Samples" of 207 that image. 209 "Pixel": The smallest addressable representation of a color in a 210 "Frame". It is composed of one or more "Samples". 212 "ESC": An ESCape symbol to indicate that the symbol to be stored is 213 too large for normal storage and that an alternate storage method is 214 used. 216 "MSB": Most Significant Bit, the bit that can cause the largest 217 change in magnitude of the symbol. 219 "VLC": Variable Length Code, a code that maps source symbols to a 220 variable number of bits. 222 "RGB": A reference to the method of storing the value of a "Pixel" by 223 using three numeric values that represent Red, Green, and Blue. 225 "YCbCr": A reference to the method of storing the value of a "Pixel" 226 by using three numeric values that represent the luma of the "Pixel" 227 (Y) and the chrominance of the "Pixel" (Cb and Cr). YCbCr word is 228 used for historical reasons and currently references any color space 229 relying on 1 luma "Sample" and 2 chrominance "Samples", e.g. YCbCr, 230 YCgCo or ICtCp. The exact meaning of the three numeric values is 231 unspecified. 233 2.2. Conventions 234 2.2.1. Pseudo-code 236 The FFV1 bitstream is described in this document using pseudo-code. 237 Note that the pseudo-code is used for clarity in order to illustrate 238 the structure of FFV1 and not intended to specify any particular 239 implementation. The pseudo-code used is based upon the C programming 240 language [ISO.9899.1990] and uses its "if/else", "while" and "for" 241 keywords as well as functions defined within this document. 243 2.2.2. Arithmetic Operators 245 Note: the operators and the order of precedence are the same as used 246 in the C programming language [ISO.9899.2018], with the exception of 247 ">>" (removal of implementation defined behavior) and "^" (power 248 instead of XOR) operators which are re-defined within this section. 250 "a + b" means a plus b. 252 "a - b" means a minus b. 254 "-a" means negation of a. 256 "a * b" means a multiplied by b. 258 "a / b" means a divided by b. 260 "a ^ b" means a raised to the b-th power. 262 "a & b" means bit-wise "and" of a and b. 264 "a | b" means bit-wise "or" of a and b. 266 "a >> b" means arithmetic right shift of two's complement integer 267 representation of a by b binary digits. This is equivalent to 268 dividing a by 2, b times, with rounding toward negative infinity. 270 "a << b" means arithmetic left shift of two's complement integer 271 representation of a by b binary digits. 273 2.2.3. Assignment Operators 275 "a = b" means a is assigned b. 277 "a++" is equivalent to a is assigned a + 1. 279 "a--" is equivalent to a is assigned a - 1. 281 "a += b" is equivalent to a is assigned a + b. 283 "a -= b" is equivalent to a is assigned a - b. 285 "a *= b" is equivalent to a is assigned a * b. 287 2.2.4. Comparison Operators 289 "a > b" means a is greater than b. 291 "a >= b" means a is greater than or equal to b. 293 "a < b" means a is less than b. 295 "a <= b" means a is less than or equal b. 297 "a == b" means a is equal to b. 299 "a != b" means a is not equal to b. 301 "a && b" means Boolean logical "and" of a and b. 303 "a || b" means Boolean logical "or" of a and b. 305 "!a" means Boolean logical "not" of a. 307 "a ? b : c" if a is true, then b, otherwise c. 309 2.2.5. Mathematical Functions 311 "floor(a)" means the largest integer less than or equal to a. 313 "ceil(a)" means the smallest integer greater than or equal to a. 315 "sign(a)" extracts the sign of a number, i.e. if a < 0 then -1, else 316 if a > 0 then 1, else 0. 318 "abs(a)" means the absolute value of a, i.e. "abs(a)" = "sign(a) * 319 a". 321 "log2(a)" means the base-two logarithm of a. 323 "min(a,b)" means the smallest of two values a and b. 325 "max(a,b)" means the largest of two values a and b. 327 "median(a,b,c)" means the numerical middle value in a data set of a, 328 b, and c, i.e. a+b+c-min(a,b,c)-max(a,b,c). 330 "A <== B" means B implies A. 332 "A <==> B" means A <== B , B <== A. 334 2.2.6. Order of Operation Precedence 336 When order of precedence is not indicated explicitly by use of 337 parentheses, operations are evaluated in the following order (from 338 top to bottom, operations of same precedence being evaluated from 339 left to right). This order of operations is based on the order of 340 operations used in Standard C. 342 a++, a-- 343 !a, -a 344 a ^ b 345 a * b, a / b, a % b 346 a + b, a - b 347 a << b, a >> b 348 a < b, a <= b, a > b, a >= b 349 a == b, a != b 350 a & b 351 a | b 352 a && b 353 a || b 354 a ? b : c 355 a = b, a += b, a -= b, a *= b 357 2.2.7. Range 359 "a...b" means any value starting from a to b, inclusive. 361 2.2.8. NumBytes 363 "NumBytes" is a non-negative integer that expresses the size in 8-bit 364 octets of a particular FFV1 "Configuration Record" or "Frame". FFV1 365 relies on its "Container" to store the "NumBytes" values; see 366 Section 4.2.3. 368 2.2.9. Bitstream Functions 370 2.2.9.1. remaining_bits_in_bitstream 372 "remaining_bits_in_bitstream( )" means the count of remaining bits 373 after the pointer in that "Configuration Record" or "Frame". It is 374 computed from the "NumBytes" value multiplied by 8 minus the count of 375 bits of that "Configuration Record" or "Frame" already read by the 376 bitstream parser. 378 2.2.9.2. remaining_symbols_in_syntax 380 "remaining_symbols_in_syntax( )" is true as long as the RangeCoder 381 has not consumed all the given input bytes. 383 2.2.9.3. byte_aligned 385 "byte_aligned( )" is true if "remaining_bits_in_bitstream( NumBytes 386 )" is a multiple of 8, otherwise false. 388 2.2.9.4. get_bits 390 "get_bits( i )" is the action to read the next "i" bits in the 391 bitstream, from most significant bit to least significant bit, and to 392 return the corresponding value. The pointer is increased by "i". 394 3. Sample Coding 396 For each "Slice" (as described in Section 4.4) of a "Frame", the 397 "Planes", "Lines", and "Samples" are coded in an order determined by 398 the "Color Space" (see Section 3.7). Each "Sample" is predicted by 399 the median predictor as described in Section 3.3 from other "Samples" 400 within the same "Plane" and the difference is stored using the method 401 described in Section 3.8. 403 3.1. Border 405 A border is assumed for each coded "Slice" for the purpose of the 406 median predictor and context according to the following rules: 408 * one column of "Samples" to the left of the coded slice is assumed 409 as identical to the "Samples" of the leftmost column of the coded 410 slice shifted down by one row. The value of the topmost "Sample" 411 of the column of "Samples" to the left of the coded slice is 412 assumed to be "0" 414 * one column of "Samples" to the right of the coded slice is assumed 415 as identical to the "Samples" of the rightmost column of the coded 416 slice 418 * an additional column of "Samples" to the left of the coded slice 419 and two rows of "Samples" above the coded slice are assumed to be 420 "0" 422 Figure 1 depicts a slice of 9 "Samples" "a,b,c,d,e,f,g,h,i" in a 3x3 423 arrangement along with its assumed border. 425 +---+---+---+---+---+---+---+---+ 426 | 0 | 0 | | 0 | 0 | 0 | | 0 | 427 +---+---+---+---+---+---+---+---+ 428 | 0 | 0 | | 0 | 0 | 0 | | 0 | 429 +---+---+---+---+---+---+---+---+ 430 | | | | | | | | | 431 +---+---+---+---+---+---+---+---+ 432 | 0 | 0 | | a | b | c | | c | 433 +---+---+---+---+---+---+---+---+ 434 | 0 | a | | d | e | f | | f | 435 +---+---+---+---+---+---+---+---+ 436 | 0 | d | | g | h | i | | i | 437 +---+---+---+---+---+---+---+---+ 439 Figure 1: A depiction of FFV1's assumed border for a set example 440 Samples. 442 3.2. Samples 444 Relative to any "Sample" "X", six other relatively positioned 445 "Samples" from the coded "Samples" and presumed border are identified 446 according to the labels used in Figure 2. The labels for these 447 relatively positioned "Samples" are used within the median predictor 448 and context. 450 +---+---+---+---+ 451 | | | T | | 452 +---+---+---+---+ 453 | |tl | t |tr | 454 +---+---+---+---+ 455 | L | l | X | | 456 +---+---+---+---+ 458 Figure 2: A depiction of how relatively positions Samples are 459 references within this document. 461 The labels for these relative "Samples" are made of the first letters 462 of the words Top, Left and Right. 464 3.3. Median Predictor 466 The prediction for any "Sample" value at position "X" may be computed 467 based upon the relative neighboring values of "l", "t", and "tl" via 468 this equation: 470 median(l, t, l + t - tl) 471 Note, this prediction template is also used in [ISO.14495-1.1999] and 472 [HuffYUV]. 474 Exception for the median predictor: if "colorspace_type == 0 && 475 bits_per_raw_sample == 16 && ( coder_type == 1 || coder_type == 2 )", 476 the following median predictor MUST be used: 478 median(left16s, top16s, left16s + top16s - diag16s) 480 where: 482 left16s = l >= 32768 ? ( l - 65536 ) : l top16s = t >= 32768 ? ( t - 483 65536 ) : t diag16s = tl >= 32768 ? ( tl - 65536 ) : tl 485 Background: a two's complement signed 16-bit signed integer was used 486 for storing "Sample" values in all known implementations of FFV1 487 bitstream. So in some circumstances, the most significant bit was 488 wrongly interpreted (used as a sign bit instead of the 16th bit of an 489 unsigned integer). Note that when the issue was discovered, the only 490 configuration of all known implementations being impacted is 16-bit 491 YCbCr with no Pixel transformation with Range Coder coder, as other 492 potentially impacted configurations (e.g. 15/16-bit JPEG2000-RCT with 493 Range Coder coder, or 16-bit content with Golomb Rice coder) were 494 implemented nowhere [ISO.15444-1.2016]. In the meanwhile, 16-bit 495 JPEG2000-RCT with Range Coder coder was implemented without this 496 issue in one implementation and validated by one conformance checker. 497 It is expected (to be confirmed) to remove this exception for the 498 median predictor in the next version of the FFV1 bitstream. 500 3.4. Context 502 Relative to any "Sample" "X", the Quantized Sample Differences "L-l", 503 "l-tl", "tl-t", "T-t", and "t-tr" are used as context: 505 context = Q_{0}[l - tl] + 506 Q_{1}[tl - t] + 507 Q_{2}[t - tr] + 508 Q_{3}[L - l] + 509 Q_{4}[T - t] 511 Figure 3 513 If "context >= 0" then "context" is used and the difference between 514 the "Sample" and its predicted value is encoded as is, else 515 "-context" is used and the difference between the "Sample" and its 516 predicted value is encoded with a flipped sign. 518 3.5. Quantization Table Sets 520 The FFV1 bitstream contains one or more Quantization Table Sets. 521 Each Quantization Table Set contains exactly 5 Quantization Tables 522 with each Quantization Table corresponding to one of the five 523 Quantized Sample Differences. For each Quantization Table, both the 524 number of quantization steps and their distribution are stored in the 525 FFV1 bitstream; each Quantization Table has exactly 256 entries, and 526 the 8 least significant bits of the Quantized Sample Difference are 527 used as index: 529 Q_{j}[k] = quant_tables[i][j][k&255] 531 Figure 4 533 In this formula, "i" is the Quantization Table Set index, "j" is the 534 Quantized Table index, "k" the Quantized Sample Difference. 536 3.6. Quantization Table Set Indexes 538 For each "Plane" of each slice, a Quantization Table Set is selected 539 from an index: 541 * For Y "Plane", "quant_table_set_index[ 0 ]" index is used 543 * For Cb and Cr "Planes", "quant_table_set_index[ 1 ]" index is used 545 * For extra "Plane", "quant_table_set_index[ (version <= 3 || 546 chroma_planes) ? 2 : 1 ]" index is used 548 Background: in first implementations of FFV1 bitstream, the index for 549 Cb and Cr "Planes" was stored even if it is not used (chroma_planes 550 set to 0), this index is kept for "version" <= 3 in order to keep 551 compatibility with FFV1 bitstreams in the wild. 553 3.7. Color spaces 555 FFV1 supports several color spaces. The count of allowed coded 556 planes and the meaning of the extra "Plane" are determined by the 557 selected color space. 559 The FFV1 bitstream interleaves data in an order determined by the 560 color space. In YCbCr for each "Plane", each "Line" is coded from 561 top to bottom and for each "Line", each "Sample" is coded from left 562 to right. In JPEG2000-RCT for each "Line" from top to bottom, each 563 "Plane" is coded and for each "Plane", each "Sample" is encoded from 564 left to right. 566 3.7.1. YCbCr 568 This color space allows 1 to 4 "Planes". 570 The Cb and Cr "Planes" are optional, but if used then MUST be used 571 together. Omitting the Cb and Cr "Planes" codes the frames in 572 grayscale without color data. 574 An optional transparency "Plane" can be used to code transparency 575 data. 577 An FFV1 "Frame" using YCbCr MUST use one of the following 578 arrangements: 580 * Y 582 * Y, Transparency 584 * Y, Cb, Cr 586 * Y, Cb, Cr, Transparency 588 The Y "Plane" MUST be coded first. If the Cb and Cr "Planes" are 589 used then they MUST be coded after the Y "Plane". If a transparency 590 "Plane" is used, then it MUST be coded last. 592 3.7.2. RGB 594 This color space allows 3 or 4 "Planes". 596 An optional transparency "Plane" can be used to code transparency 597 data. 599 JPEG2000-RCT is a Reversible Color Transform that codes RGB (red, 600 green, blue) "Planes" losslessly in a modified YCbCr color space 601 [ISO.15444-1.2016]. Reversible Pixel transformations between YCbCr 602 and RGB use the following formulae. 604 Cb = b - g 605 Cr = r - g 606 Y = g + (Cb + Cr) >> 2 607 g = Y - (Cb + Cr) >> 2 608 r = Cr + g 609 b = Cb + g 611 Figure 5 613 Exception for the JPEG2000-RCT conversion: if "bits_per_raw_sample" 614 is between 9 and 15 inclusive and "extra_plane" is 0, the following 615 formulae for reversible conversions between YCbCr and RGB MUST be 616 used instead of the ones above: 618 Cb = g - b 619 Cr = r - b 620 Y = b +(Cb + Cr) >> 2 621 b = Y -(Cb + Cr) >> 2 622 r = Cr + b 623 g = Cb + b 625 Figure 6 627 Background: At the time of this writing, in all known implementations 628 of FFV1 bitstream, when "bits_per_raw_sample" was between 9 and 15 629 inclusive and "extra_plane" is 0, GBR "Planes" were used as BGR 630 "Planes" during both encoding and decoding. In the meanwhile, 16-bit 631 JPEG2000-RCT was implemented without this issue in one implementation 632 and validated by one conformance checker. Methods to address this 633 exception for the transform are under consideration for the next 634 version of the FFV1 bitstream. 636 Cb and Cr are positively offset by "1 << bits_per_raw_sample" after 637 the conversion from RGB to the modified YCbCr and are negatively 638 offseted by the same value before the conversion from the modified 639 YCbCr to RGB, in order to have only non-negative values after the 640 conversion. 642 When FFV1 uses the JPEG2000-RCT, the horizontal "Lines" are 643 interleaved to improve caching efficiency since it is most likely 644 that the JPEG2000-RCT will immediately be converted to RGB during 645 decoding. The interleaved coding order is also Y, then Cb, then Cr, 646 and then if used transparency. 648 As an example, a "Frame" that is two "Pixels" wide and two "Pixels" 649 high, could comprise the following structure: 651 +------------------------+------------------------+ 652 | Pixel(1,1) | Pixel(2,1) | 653 | Y(1,1) Cb(1,1) Cr(1,1) | Y(2,1) Cb(2,1) Cr(2,1) | 654 +------------------------+------------------------+ 655 | Pixel(1,2) | Pixel(2,2) | 656 | Y(1,2) Cb(1,2) Cr(1,2) | Y(2,2) Cb(2,2) Cr(2,2) | 657 +------------------------+------------------------+ 658 In JPEG2000-RCT, the coding order would be left to right and then top 659 to bottom, with values interleaved by "Lines" and stored in this 660 order: 662 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) 663 Cb(2,2) Cr(1,2) Cr(2,2) 665 3.8. Coding of the Sample Difference 667 Instead of coding the n+1 bits of the Sample Difference with Huffman 668 or Range coding (or n+2 bits, in the case of JPEG2000-RCT), only the 669 n (or n+1, in the case of JPEG2000-RCT) least significant bits are 670 used, since this is sufficient to recover the original "Sample". In 671 the equation below, the term "bits" represents "bits_per_raw_sample + 672 1" for JPEG2000-RCT or "bits_per_raw_sample" otherwise: 674 coder_input = 675 [(sample_difference + 2 ^ (bits-1)) & (2 ^ bits - 1)] - 2 ^ (bits - 1) 677 Figure 7 679 3.8.1. Range Coding Mode 681 Early experimental versions of FFV1 used the CABAC Arithmetic coder 682 from H.264 as defined in [ISO.14496-10.2014] but due to the uncertain 683 patent/royalty situation, as well as its slightly worse performance, 684 CABAC was replaced by a Range coder based on an algorithm defined by 685 G. Nigel and N. Martin in 1979 [range-coding]. 687 3.8.1.1. Range Binary Values 689 To encode binary digits efficiently a Range coder is used. "C(i)" is 690 the i-th Context. "B(i)" is the i-th byte of the bytestream. "b(i)" 691 is the i-th Range coded binary value, "S(0,i)" is the i-th initial 692 state. The length of the bytestream encoding n binary symbols is 693 "j(n)" bytes. 695 r_{i} = floor( ( R_{i} * S_{i,C_{i}} ) / 2 ^ 8 ) 697 Figure 8 699 S_{i+1,C_{i}} = zero_state_{S_{i,C_{i}}} AND 700 l_i = L_i AND 701 t_i = R_i - r_i <== 702 b_i = 0 <==> 703 L_i < R_i - r_i 705 S_{i+1,C_{i}} = one_state_{S_{i,C_{i}}} AND 706 l_i = L_i - R_i + r_i AND 707 t_i = r_i <== 708 b_i = 1 <==> 709 L_i >= R_i - r_i 711 Figure 9 713 S_{i+1,k} = S_{i,k} <== C_i != k 715 Figure 10 717 R_{i+1} = 2 ^ 8 * t_{i} AND 718 L_{i+1} = 2 ^ 8 * l_{i} + B_{j_{i}} AND 719 j_{i+1} = j_{i} + 1 <== 720 t_{i} < 2 ^ 8 722 R_{i+1} = t_{i} AND 723 L_{i+1} = l_{i} AND 724 j_{i+1} = j_{i} <== 725 t_{i} >= 2 ^ 8 727 Figure 11 729 R_{0} = 65280 731 Figure 12 733 L_{0} = 2 ^ 8 * B_{0} + B_{1} 735 Figure 13 737 j_{0} = 2 739 Figure 14 741 3.8.1.1.1. Termination 743 The range coder can be used in three modes. 745 * In "Open mode" when decoding, every symbol the reader attempts to 746 read is available. In this mode arbitrary data can have been 747 appended without affecting the range coder output. This mode is 748 not used in FFV1. 750 * In "Closed mode" the length in bytes of the bytestream is provided 751 to the range decoder. Bytes beyond the length are read as 0 by 752 the range decoder. This is generally one byte shorter than the 753 open mode. 755 * In "Sentinel mode" the exact length in bytes is not known and thus 756 the range decoder MAY read into the data that follows the range 757 coded bytestream by one byte. In "Sentinel mode", the end of the 758 range coded bytestream is a binary symbol with state 129, which 759 value SHALL be discarded. After reading this symbol, the range 760 decoder will have read one byte beyond the end of the range coded 761 bytestream. This way the byte position of the end can be 762 determined. Bytestreams written in "Sentinel mode" can be read in 763 "Closed mode" if the length can be determined, in this case the 764 last (sentinel) symbol will be read non-corrupted and be of value 765 0. 767 Above describes the range decoding. Encoding is defined as any 768 process which produces a decodable bytestream. 770 There are three places where range coder termination is needed in 771 FFV1. First is in the "Configuration Record", in this case the size 772 of the range coded bytestream is known and handled as "Closed mode". 773 Second is the switch from the "Slice Header" which is range coded to 774 Golomb coded slices as "Sentinel mode". Third is the end of range 775 coded Slices which need to terminate before the CRC at their end. 776 This can be handled as "Sentinel mode" or as "Closed mode" if the CRC 777 position has been determined. 779 3.8.1.2. Range Non Binary Values 781 To encode scalar integers, it would be possible to encode each bit 782 separately and use the past bits as context. However that would mean 783 255 contexts per 8-bit symbol that is not only a waste of memory but 784 also requires more past data to reach a reasonably good estimate of 785 the probabilities. Alternatively assuming a Laplacian distribution 786 and only dealing with its variance and mean (as in Huffman coding) 787 would also be possible, however, for maximum flexibility and 788 simplicity, the chosen method uses a single symbol to encode if a 789 number is 0, and if not, encodes the number using its exponent, 790 mantissa and sign. The exact contexts used are best described by 791 Figure 15, followed by some comments. 793 pseudo-code | type 794 --------------------------------------------------------------|----- 795 void put_symbol(RangeCoder *c, uint8_t *state, int v, int \ | 796 is_signed) { | 797 int i; | 798 put_rac(c, state+0, !v); | 799 if (v) { | 800 int a= abs(v); | 801 int e= log2(a); | 802 | 803 for (i = 0; i < e; i++) { | 804 put_rac(c, state+1+min(i,9), 1); //1..10 | 805 } | 806 | 807 put_rac(c, state+1+min(i,9), 0); | 808 for (i = e-1; i >= 0; i--) { | 809 put_rac(c, state+22+min(i,9), (a>>i)&1); //22..31 | 810 } | 811 | 812 if (is_signed) { | 813 put_rac(c, state+11 + min(e, 10), v < 0); //11..21| 814 } | 815 } | 816 } | 818 Figure 15: A pseudo-code description of the contexts of Range Non 819 Binary Values. 821 3.8.1.3. Initial Values for the Context Model 823 At keyframes all Range coder state variables are set to their initial 824 state. 826 3.8.1.4. State Transition Table 828 one_state_{i} = 829 default_state_transition_{i} + state_transition_delta_{i} 831 Figure 16 833 zero_state_{i} = 256 - one_state_{256-i} 835 Figure 17 837 3.8.1.5. default_state_transition 838 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 840 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 842 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 844 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 846 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 848 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99,100,101,102,103, 850 104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118, 852 119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,133, 854 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149, 856 150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164, 858 165,166,167,168,169,170,171,171,172,173,174,175,176,177,178,179, 860 180,181,182,183,184,185,186,187,188,189,190,190,191,192,194,194, 862 195,196,197,198,199,200,201,202,202,204,205,206,207,208,209,209, 864 210,211,212,213,215,215,216,217,218,219,220,220,222,223,224,225, 866 226,227,227,229,229,230,231,232,234,234,235,236,237,238,239,240, 868 241,242,243,244,245,246,247,248,248, 0, 0, 0, 0, 0, 0, 0, 870 3.8.1.6. Alternative State Transition Table 872 The alternative state transition table has been built using iterative 873 minimization of frame sizes and generally performs better than the 874 default. To use it, the "coder_type" (see Section 4.1.3) MUST be set 875 to 2 and the difference to the default MUST be stored in the 876 "Parameters", see Section 4.1. The reference implementation of FFV1 877 in FFmpeg uses Figure 18 by default at the time of this writing when 878 Range coding is used. 880 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49, 882 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39, 884 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52, 886 53, 74, 55, 57, 58, 58, 74, 60,101, 61, 62, 84, 66, 66, 68, 69, 888 87, 82, 71, 97, 73, 73, 82, 75,111, 77, 94, 78, 87, 81, 83, 97, 890 85, 83, 94, 86, 99, 89, 90, 99,111, 92, 93,134, 95, 98,105, 98, 892 105,110,102,108,102,118,103,106,106,113,109,112,114,112,116,125, 894 115,116,117,117,126,119,125,121,121,123,145,124,126,131,127,129, 896 165,130,132,138,133,135,145,136,137,139,146,141,143,142,144,148, 898 147,155,151,149,151,150,152,157,153,154,156,168,158,162,161,160, 900 172,163,169,164,166,184,167,170,177,174,171,173,182,176,180,178, 902 175,189,179,181,186,183,192,185,200,187,191,188,190,197,193,196, 904 197,194,195,196,198,202,199,201,210,203,207,204,205,206,208,214, 906 209,211,221,212,213,215,224,216,217,218,219,220,222,228,223,225, 908 226,224,227,229,240,230,231,232,233,234,235,236,238,239,237,242, 910 241,243,242,244,245,246,247,248,249,250,251,252,252,253,254,255, 912 Figure 18: Alternative state transition table for Range coding. 914 3.8.2. Golomb Rice Mode 916 The end of the bitstream of the "Frame" is filled with 0-bits until 917 that the bitstream contains a multiple of 8 bits. 919 3.8.2.1. Signed Golomb Rice Codes 921 This coding mode uses Golomb Rice codes. The VLC is split into two 922 parts. The prefix stores the most significant bits and the suffix 923 stores the k least significant bits or stores the whole number in the 924 ESC case. 926 pseudo-code | type 927 --------------------------------------------------------------|----- 928 int get_ur_golomb(k) { | 929 for (prefix = 0; prefix < 12; prefix++) { | 930 if (get_bits(1)) { | 931 return get_bits(k) + (prefix << k) | 932 } | 933 } | 934 return get_bits(bits) + 11 | 935 } | 936 | 937 int get_sr_golomb(k) { | 938 v = get_ur_golomb(k); | 939 if (v & 1) return - (v >> 1) - 1; | 940 else return (v >> 1); | 941 } 943 3.8.2.1.1. Prefix 945 +----------------+-------+ 946 | bits | value | 947 +================+=======+ 948 | 1 | 0 | 949 +----------------+-------+ 950 | 01 | 1 | 951 +----------------+-------+ 952 | ... | ... | 953 +----------------+-------+ 954 | 0000 0000 01 | 9 | 955 +----------------+-------+ 956 | 0000 0000 001 | 10 | 957 +----------------+-------+ 958 | 0000 0000 0001 | 11 | 959 +----------------+-------+ 960 | 0000 0000 0000 | ESC | 961 +----------------+-------+ 963 Table 1 965 3.8.2.1.2. Suffix 967 +---------+----------------------------------------+ 968 +=========+========================================+ 969 | non ESC | the k least significant bits MSB first | 970 +---------+----------------------------------------+ 971 | ESC | the value - 11, in MSB first order | 972 +---------+----------------------------------------+ 974 Table 2 976 "ESC" MUST NOT be used if the value can be coded as "non ESC". 978 3.8.2.1.3. Examples 980 +-----+-------------------------+-------+ 981 | k | bits | value | 982 +=====+=========================+=======+ 983 | 0 | "1" | 0 | 984 +-----+-------------------------+-------+ 985 | 0 | "001" | 2 | 986 +-----+-------------------------+-------+ 987 | 2 | "1 00" | 0 | 988 +-----+-------------------------+-------+ 989 | 2 | "1 10" | 2 | 990 +-----+-------------------------+-------+ 991 | 2 | "01 01" | 5 | 992 +-----+-------------------------+-------+ 993 | any | "000000000000 10000000" | 139 | 994 +-----+-------------------------+-------+ 996 Table 3 998 3.8.2.2. Run Mode 1000 Run mode is entered when the context is 0 and left as soon as a non-0 1001 difference is found. The level is identical to the predicted one. 1002 The run and the first different level are coded. 1004 3.8.2.2.1. Run Length Coding 1006 The run value is encoded in two parts. The prefix part stores the 1007 more significant part of the run as well as adjusting the "run_index" 1008 that determines the number of bits in the less significant part of 1009 the run. The second part of the value stores the less significant 1010 part of the run as it is. The run_index is reset for each "Plane" 1011 and slice to 0. 1013 pseudo-code | type 1014 --------------------------------------------------------------|----- 1015 log2_run[41]={ | 1016 0, 0, 0, 0, 1, 1, 1, 1, | 1017 2, 2, 2, 2, 3, 3, 3, 3, | 1018 4, 4, 5, 5, 6, 6, 7, 7, | 1019 8, 9,10,11,12,13,14,15, | 1020 16,17,18,19,20,21,22,23, | 1021 24, | 1022 }; | 1023 | 1024 if (run_count == 0 && run_mode == 1) { | 1025 if (get_bits(1)) { | 1026 run_count = 1 << log2_run[run_index]; | 1027 if (x + run_count <= w) { | 1028 run_index++; | 1029 } | 1030 } else { | 1031 if (log2_run[run_index]) { | 1032 run_count = get_bits(log2_run[run_index]); | 1033 } else { | 1034 run_count = 0; | 1035 } | 1036 if (run_index) { | 1037 run_index--; | 1038 } | 1039 run_mode = 2; | 1040 } | 1041 } | 1043 The "log2_run" array is also used within [ISO.14495-1.1999]. 1045 3.8.2.3. Scalar Mode 1047 Each difference is coded with the per context mean prediction removed 1048 and a per context value for k. 1050 get_vlc_symbol(state) { 1051 i = state->count; 1052 k = 0; 1053 while (i < state->error_sum) { 1054 k++; 1055 i += i; 1056 } 1058 v = get_sr_golomb(k); 1060 if (2 * state->drift < -state->count) { 1061 v = -1 - v; 1062 } 1064 ret = sign_extend(v + state->bias, bits); 1066 state->error_sum += abs(v); 1067 state->drift += v; 1069 if (state->count == 128) { 1070 state->count >>= 1; 1071 state->drift >>= 1; 1072 state->error_sum >>= 1; 1073 } 1074 state->count++; 1075 if (state->drift <= -state->count) { 1076 state->bias = max(state->bias - 1, -128); 1078 state->drift = max(state->drift + state->count, 1079 -state->count + 1); 1080 } else if (state->drift > 0) { 1081 state->bias = min(state->bias + 1, 127); 1083 state->drift = min(state->drift - state->count, 0); 1084 } 1086 return ret; 1087 } 1089 3.8.2.3.1. Level Coding 1091 Level coding is identical to the normal difference coding with the 1092 exception that the 0 value is removed as it cannot occur: 1094 diff = get_vlc_symbol(context_state); 1095 if (diff >= 0) { 1096 diff++; 1097 } 1099 Note, this is different from JPEG-LS, which doesn't use prediction in 1100 run mode and uses a different encoding and context model for the last 1101 difference. On a small set of test "Samples" the use of prediction 1102 slightly improved the compression rate. 1104 3.8.2.4. Initial Values for the VLC context state 1106 At keyframes all coder state variables are set to their initial 1107 state. 1109 drift = 0; 1110 error_sum = 4; 1111 bias = 0; 1112 count = 1; 1114 4. Bitstream 1116 An FFV1 bitstream is composed of a series of one or more "Frames" and 1117 (when required) a "Configuration Record". 1119 Within the following sub-sections, pseudo-code is used to explain the 1120 structure of each FFV1 bitstream component, as described in 1121 Section 2.2.1. Table 4 lists symbols used to annotate that pseudo- 1122 code in order to define the storage of the data referenced in that 1123 line of pseudo-code. 1125 +--------+----------------------------------------------+ 1126 | Symbol | Definition | 1127 +========+==============================================+ 1128 | u(n) | unsigned big endian integer using n bits | 1129 +--------+----------------------------------------------+ 1130 | sg | Golomb Rice coded signed scalar symbol coded | 1131 | | with the method described in Section 3.8.2 | 1132 +--------+----------------------------------------------+ 1133 | br | Range coded Boolean (1-bit) symbol with the | 1134 | | method described in Section 3.8.1.1 | 1135 +--------+----------------------------------------------+ 1136 | ur | Range coded unsigned scalar symbol coded | 1137 | | with the method described in Section 3.8.1.2 | 1138 +--------+----------------------------------------------+ 1139 | sr | Range coded signed scalar symbol coded with | 1140 | | the method described in Section 3.8.1.2 | 1141 +--------+----------------------------------------------+ 1142 | sd | Sample difference coded with the method | 1143 | | described in Section 3.8 | 1144 +--------+----------------------------------------------+ 1146 Table 4: Definition of pseudo-code symbols for this 1147 document. 1149 The same context that is initialized to 128 is used for all fields in 1150 the header. 1152 The following MUST be provided by external means during 1153 initialization of the decoder: 1155 "frame_pixel_width" is defined as "Frame" width in "Pixels". 1157 "frame_pixel_height" is defined as "Frame" height in "Pixels". 1159 Default values at the decoder initialization phase: 1161 "ConfigurationRecordIsPresent" is set to 0. 1163 4.1. Parameters 1165 The "Parameters" section contains significant characteristics about 1166 the decoding configuration used for all instances of "Frame" (in FFV1 1167 version 0 and 1) or the whole FFV1 bitstream (other versions), 1168 including the stream version, color configuration, and quantization 1169 tables. Figure 19 describes the contents of the bitstream. 1171 pseudo-code | type 1172 --------------------------------------------------------------|----- 1173 Parameters( ) { | 1174 version | ur 1175 if (version >= 3) { | 1176 micro_version | ur 1177 } | 1178 coder_type | ur 1179 if (coder_type > 1) { | 1180 for (i = 1; i < 256; i++) { | 1181 state_transition_delta[ i ] | sr 1182 } | 1183 } | 1184 colorspace_type | ur 1185 if (version >= 1) { | 1186 bits_per_raw_sample | ur 1187 } | 1188 chroma_planes | br 1189 log2_h_chroma_subsample | ur 1190 log2_v_chroma_subsample | ur 1191 extra_plane | br 1192 if (version >= 3) { | 1193 num_h_slices - 1 | ur 1194 num_v_slices - 1 | ur 1195 quant_table_set_count | ur 1196 } | 1197 for (i = 0; i < quant_table_set_count; i++) { | 1198 QuantizationTableSet( i ) | 1199 } | 1200 if (version >= 3) { | 1201 for (i = 0; i < quant_table_set_count; i++) { | 1202 states_coded | br 1203 if (states_coded) { | 1204 for (j = 0; j < context_count[ i ]; j++) { | 1205 for (k = 0; k < CONTEXT_SIZE; k++) { | 1206 initial_state_delta[ i ][ j ][ k ] | sr 1207 } | 1208 } | 1209 } | 1210 } | 1211 ec | ur 1212 intra | ur 1213 } | 1214 } | 1216 Figure 19: A pseudo-code description of the bitstream contents. 1218 CONTEXT_SIZE is 32. 1220 4.1.1. version 1222 "version" specifies the version of the FFV1 bitstream. 1224 Each version is incompatible with other versions: decoders SHOULD 1225 reject FFV1 bitstreams due to an unknown version. 1227 Decoders SHOULD reject FFV1 bitstreams with version <= 1 && 1228 ConfigurationRecordIsPresent == 1. 1230 Decoders SHOULD reject FFV1 bitstreams with version >= 3 && 1231 ConfigurationRecordIsPresent == 0. 1233 +-------+-------------------------+ 1234 | value | version | 1235 +=======+=========================+ 1236 | 0 | FFV1 version 0 | 1237 +-------+-------------------------+ 1238 | 1 | FFV1 version 1 | 1239 +-------+-------------------------+ 1240 | 2 | reserved* | 1241 +-------+-------------------------+ 1242 | 3 | FFV1 version 3 | 1243 +-------+-------------------------+ 1244 | Other | reserved for future use | 1245 +-------+-------------------------+ 1247 Table 5 1249 * Version 2 was experimental and this document does not describe it. 1251 4.1.2. micro_version 1253 "micro_version" specifies the micro-version of the FFV1 bitstream. 1255 After a version is considered stable (a micro-version value is 1256 assigned to be the first stable variant of a specific version), each 1257 new micro-version after this first stable variant is compatible with 1258 the previous micro-version: decoders SHOULD NOT reject FFV1 1259 bitstreams due to an unknown micro-version equal or above the micro- 1260 version considered as stable. 1262 Meaning of "micro_version" for "version" 3: 1264 +-------+-------------------------+ 1265 | value | micro_version | 1266 +=======+=========================+ 1267 | 0...3 | reserved* | 1268 +-------+-------------------------+ 1269 | 4 | first stable variant | 1270 +-------+-------------------------+ 1271 | Other | reserved for future use | 1272 +-------+-------------------------+ 1274 Table 6: The definitions for 1275 "micro_version" values for FFV1 1276 version 3. 1278 * development versions may be incompatible with the stable variants. 1280 4.1.3. coder_type 1282 "coder_type" specifies the coder used. 1284 +-------+-------------------------------------------------+ 1285 | value | coder used | 1286 +=======+=================================================+ 1287 | 0 | Golomb Rice | 1288 +-------+-------------------------------------------------+ 1289 | 1 | Range Coder with default state transition table | 1290 +-------+-------------------------------------------------+ 1291 | 2 | Range Coder with custom state transition table | 1292 +-------+-------------------------------------------------+ 1293 | Other | reserved for future use | 1294 +-------+-------------------------------------------------+ 1296 Table 7 1298 Restrictions: 1300 If "coder_type" is 0, then "bits_per_raw_sample" SHOULD NOT be > 8. 1302 Background: At the time of this writing, there is no known 1303 implementation of FFV1 bitstream supporting Golomb Rice algorithm 1304 with "bits_per_raw_sample" greater than 8, and Range Coder is 1305 prefered. 1307 4.1.4. state_transition_delta 1309 "state_transition_delta" specifies the Range coder custom state 1310 transition table. 1312 If "state_transition_delta" is not present in the FFV1 bitstream, all 1313 Range coder custom state transition table elements are assumed to be 1314 0. 1316 4.1.5. colorspace_type 1318 "colorspace_type" specifies the color space encoded, the pixel 1319 transformation used by the encoder, the extra plane content, as well 1320 as interleave method. 1322 +-------+-------------+----------------+--------------+-------------+ 1323 | value | color space | pixel | extra plane | interleave | 1324 | | encoded | transformation | content | method | 1325 +=======+=============+================+==============+=============+ 1326 | 0 | YCbCr | None | Transparency | "Plane" | 1327 | | | | | then | 1328 | | | | | "Line" | 1329 +-------+-------------+----------------+--------------+-------------+ 1330 | 1 | RGB | JPEG2000-RCT | Transparency | "Line" | 1331 | | | | | then | 1332 | | | | | "Plane" | 1333 +-------+-------------+----------------+--------------+-------------+ 1334 | Other | reserved | reserved for | reserved for | reserved | 1335 | | for future | future use | future use | for future | 1336 | | use | | | use | 1337 +-------+-------------+----------------+--------------+-------------+ 1339 Table 8 1341 FFV1 bitstreams with "colorspace_type" == 1 && ("chroma_planes" != 1342 1 || "log2_h_chroma_subsample" != 0 || "log2_v_chroma_subsample" != 1343 0) are not part of this specification. 1345 4.1.6. chroma_planes 1347 "chroma_planes" indicates if chroma (color) "Planes" are present. 1349 +-------+---------------------------------+ 1350 | value | presence | 1351 +=======+=================================+ 1352 | 0 | chroma "Planes" are not present | 1353 +-------+---------------------------------+ 1354 | 1 | chroma "Planes" are present | 1355 +-------+---------------------------------+ 1357 Table 9 1359 4.1.7. bits_per_raw_sample 1361 "bits_per_raw_sample" indicates the number of bits for each "Sample". 1362 Inferred to be 8 if not present. 1364 +-------+-----------------------------------+ 1365 | value | bits for each sample | 1366 +=======+===================================+ 1367 | 0 | reserved* | 1368 +-------+-----------------------------------+ 1369 | Other | the actual bits for each "Sample" | 1370 +-------+-----------------------------------+ 1372 Table 10 1374 * Encoders MUST NOT store "bits_per_raw_sample" = 0. Decoders SHOULD 1375 accept and interpret "bits_per_raw_sample" = 0 as 8. 1377 4.1.8. log2_h_chroma_subsample 1379 "log2_h_chroma_subsample" indicates the subsample factor, stored in 1380 powers to which the number 2 must be raised, between luma and chroma 1381 width ("chroma_width = 2 ^ -log2_h_chroma_subsample * luma_width"). 1383 4.1.9. log2_v_chroma_subsample 1385 "log2_v_chroma_subsample" indicates the subsample factor, stored in 1386 powers to which the number 2 must be raised, between luma and chroma 1387 height ("chroma_height = 2 ^ -log2_v_chroma_subsample * 1388 luma_height"). 1390 4.1.10. extra_plane 1392 "extra_plane" indicates if an extra "Plane" is present. 1394 +-------+------------------------------+ 1395 | value | presence | 1396 +=======+==============================+ 1397 | 0 | extra "Plane" is not present | 1398 +-------+------------------------------+ 1399 | 1 | extra "Plane" is present | 1400 +-------+------------------------------+ 1402 Table 11 1404 4.1.11. num_h_slices 1406 "num_h_slices" indicates the number of horizontal elements of the 1407 slice raster. 1409 Inferred to be 1 if not present. 1411 4.1.12. num_v_slices 1413 "num_v_slices" indicates the number of vertical elements of the slice 1414 raster. 1416 Inferred to be 1 if not present. 1418 4.1.13. quant_table_set_count 1420 "quant_table_set_count" indicates the number of Quantization 1421 Table Sets. "quant_table_set_count" MUST be less than or equal to 8. 1423 Inferred to be 1 if not present. 1425 MUST NOT be 0. 1427 4.1.14. states_coded 1429 "states_coded" indicates if the respective Quantization Table Set has 1430 the initial states coded. 1432 Inferred to be 0 if not present. 1434 +-------+--------------------------------+ 1435 | value | initial states | 1436 +=======+================================+ 1437 | 0 | initial states are not present | 1438 | | and are assumed to be all 128 | 1439 +-------+--------------------------------+ 1440 | 1 | initial states are present | 1441 +-------+--------------------------------+ 1443 Table 12 1445 4.1.15. initial_state_delta 1447 "initial_state_delta[ i ][ j ][ k ]" indicates the initial Range 1448 coder state, it is encoded using "k" as context index and 1450 pred = j ? initial_states[ i ][j - 1][ k ] : 128 1451 Figure 20 1453 initial_state[ i ][ j ][ k ] = 1454 ( pred + initial_state_delta[ i ][ j ][ k ] ) & 255 1456 Figure 21 1458 4.1.16. ec 1460 "ec" indicates the error detection/correction type. 1462 +-------+--------------------------------------------+ 1463 | value | error detection/correction type | 1464 +=======+============================================+ 1465 | 0 | 32-bit CRC on the global header | 1466 +-------+--------------------------------------------+ 1467 | 1 | 32-bit CRC per slice and the global header | 1468 +-------+--------------------------------------------+ 1469 | Other | reserved for future use | 1470 +-------+--------------------------------------------+ 1472 Table 13 1474 4.1.17. intra 1476 "intra" indicates the constraint on "keyframe" in each instance of 1477 "Frame". 1479 Inferred to be 0 if not present. 1481 +-------+-------------------------------------------------------+ 1482 | value | relationship | 1483 +=======+=======================================================+ 1484 | 0 | "keyframe" can be 0 or 1 (non keyframes or keyframes) | 1485 +-------+-------------------------------------------------------+ 1486 | 1 | "keyframe" MUST be 1 (keyframes only) | 1487 +-------+-------------------------------------------------------+ 1488 | Other | reserved for future use | 1489 +-------+-------------------------------------------------------+ 1491 Table 14 1493 4.2. Configuration Record 1495 In the case of a FFV1 bitstream with "version >= 3", a "Configuration 1496 Record" is stored in the underlying "Container", at the track header 1497 level. It contains the "Parameters" used for all instances of 1498 "Frame". The size of the "Configuration Record", "NumBytes", is 1499 supplied by the underlying "Container". 1501 pseudo-code | type 1502 -----------------------------------------------------------|----- 1503 ConfigurationRecord( NumBytes ) { | 1504 ConfigurationRecordIsPresent = 1 | 1505 Parameters( ) | 1506 while (remaining_symbols_in_syntax(NumBytes - 4)) { | 1507 reserved_for_future_use | br/ur/sr 1508 } | 1509 configuration_record_crc_parity | u(32) 1510 } | 1512 4.2.1. reserved_for_future_use 1514 "reserved_for_future_use" has semantics that are reserved for future 1515 use. 1517 Encoders conforming to this version of this specification SHALL NOT 1518 write this value. 1520 Decoders conforming to this version of this specification SHALL 1521 ignore its value. 1523 4.2.2. configuration_record_crc_parity 1525 "configuration_record_crc_parity" 32 bits that are chosen so that the 1526 "Configuration Record" as a whole has a CRC remainder of 0. 1528 This is equivalent to storing the CRC remainder in the 32-bit parity. 1530 The CRC generator polynomial used is described in Section 4.8.3. 1532 4.2.3. Mapping FFV1 into Containers 1534 This "Configuration Record" can be placed in any file format 1535 supporting "Configuration Records", fitting as much as possible with 1536 how the file format uses to store "Configuration Records". The 1537 "Configuration Record" storage place and "NumBytes" are currently 1538 defined and supported by this version of this specification for the 1539 following formats: 1541 4.2.3.1. AVI File Format 1543 The "Configuration Record" extends the stream format chunk ("AVI ", 1544 "hdlr", "strl", "strf") with the ConfigurationRecord bitstream. 1546 See [AVI] for more information about chunks. 1548 "NumBytes" is defined as the size, in bytes, of the strf chunk 1549 indicated in the chunk header minus the size of the stream format 1550 structure. 1552 4.2.3.2. ISO Base Media File Format 1554 The "Configuration Record" extends the sample description box 1555 ("moov", "trak", "mdia", "minf", "stbl", "stsd") with a "glbl" box 1556 that contains the ConfigurationRecord bitstream. See 1557 [ISO.14496-12.2015] for more information about boxes. 1559 "NumBytes" is defined as the size, in bytes, of the "glbl" box 1560 indicated in the box header minus the size of the box header. 1562 4.2.3.3. NUT File Format 1564 The "codec_specific_data" element (in "stream_header" packet) 1565 contains the ConfigurationRecord bitstream. See [NUT] for more 1566 information about elements. 1568 "NumBytes" is defined as the size, in bytes, of the 1569 "codec_specific_data" element as indicated in the "length" field of 1570 "codec_specific_data". 1572 4.2.3.4. Matroska File Format 1574 FFV1 SHOULD use "V_FFV1" as the Matroska "Codec ID". For FFV1 1575 versions 2 or less, the Matroska "CodecPrivate" Element SHOULD NOT be 1576 used. For FFV1 versions 3 or greater, the Matroska "CodecPrivate" 1577 Element MUST contain the FFV1 "Configuration Record" structure and no 1578 other data. See [Matroska] for more information about elements. 1580 "NumBytes" is defined as the "Element Data Size" of the 1581 "CodecPrivate" Element. 1583 4.3. Frame 1585 A "Frame" is an encoded representation of a complete static image. 1586 The whole "Frame" is provided by the underlaying container. 1588 A "Frame" consists of the "keyframe" field, "Parameters" (if 1589 "version" <= 1), and a sequence of independent slices. The pseudo- 1590 code below describes the contents of a "Frame". 1592 pseudo-code | type 1593 --------------------------------------------------------------|----- 1594 Frame( NumBytes ) { | 1595 keyframe | br 1596 if (keyframe && !ConfigurationRecordIsPresent { | 1597 Parameters( ) | 1598 } | 1599 while (remaining_bits_in_bitstream( NumBytes )) { | 1600 Slice( ) | 1601 } | 1602 } | 1604 Architecture overview of slices in a "Frame": 1606 +-----------------------------------------------------------------+ 1607 +=================================================================+ 1608 | first slice header | 1609 +-----------------------------------------------------------------+ 1610 | first slice content | 1611 +-----------------------------------------------------------------+ 1612 | first slice footer | 1613 +-----------------------------------------------------------------+ 1614 | --------------------------------------------------------------- | 1615 +-----------------------------------------------------------------+ 1616 | second slice header | 1617 +-----------------------------------------------------------------+ 1618 | second slice content | 1619 +-----------------------------------------------------------------+ 1620 | second slice footer | 1621 +-----------------------------------------------------------------+ 1622 | --------------------------------------------------------------- | 1623 +-----------------------------------------------------------------+ 1624 | ... | 1625 +-----------------------------------------------------------------+ 1626 | --------------------------------------------------------------- | 1627 +-----------------------------------------------------------------+ 1628 | last slice header | 1629 +-----------------------------------------------------------------+ 1630 | last slice content | 1631 +-----------------------------------------------------------------+ 1632 | last slice footer | 1633 +-----------------------------------------------------------------+ 1635 Table 15 1637 4.4. Slice 1639 A "Slice" is an independent spatial sub-section of a "Frame" that is 1640 encoded separately from another region of the same "Frame". The use 1641 of more than one "Slice" per "Frame" can be useful for taking 1642 advantage of the opportunities of multithreaded encoding and 1643 decoding. 1645 A "Slice" consists of a "Slice Header" (when relevant), a "Slice 1646 Content", and a "Slice Footer" (when relevant). The pseudo-code 1647 below describes the contents of a "Slice". 1649 pseudo-code | type 1650 --------------------------------------------------------------|----- 1651 Slice( ) { | 1652 if (version >= 3) { | 1653 SliceHeader( ) | 1654 } | 1655 SliceContent( ) | 1656 if (coder_type == 0) { | 1657 while (!byte_aligned()) { | 1658 padding | u(1) 1659 } | 1660 } | 1661 if (version <= 1) { | 1662 while (remaining_bits_in_bitstream( NumBytes ) != 0) {| 1663 reserved | u(1) 1664 } | 1665 } | 1666 if (version >= 3) { | 1667 SliceFooter( ) | 1668 } | 1669 } | 1671 "padding" specifies a bit without any significance and used only for 1672 byte alignment. MUST be 0. 1674 "reserved" specifies a bit without any significance in this revision 1675 of the specification and may have a significance in a later revision 1676 of this specification. 1678 Encoders SHOULD NOT fill these bits. 1680 Decoders SHOULD ignore these bits. 1682 4.5. Slice Header 1684 A "Slice Header" provides information about the decoding 1685 configuration of the "Slice", such as its spatial position, size, and 1686 aspect ratio. The pseudo-code below describes the contents of the 1687 "Slice Header". 1689 pseudo-code | type 1690 --------------------------------------------------------------|----- 1691 SliceHeader( ) { | 1692 slice_x | ur 1693 slice_y | ur 1694 slice_width - 1 | ur 1695 slice_height - 1 | ur 1696 for (i = 0; i < quant_table_set_index_count; i++) { | 1697 quant_table_set_index[ i ] | ur 1698 } | 1699 picture_structure | ur 1700 sar_num | ur 1701 sar_den | ur 1702 } | 1704 4.5.1. slice_x 1706 "slice_x" indicates the x position on the slice raster formed by 1707 num_h_slices. 1709 Inferred to be 0 if not present. 1711 4.5.2. slice_y 1713 "slice_y" indicates the y position on the slice raster formed by 1714 num_v_slices. 1716 Inferred to be 0 if not present. 1718 4.5.3. slice_width 1720 "slice_width" indicates the width on the slice raster formed by 1721 num_h_slices. 1723 Inferred to be 1 if not present. 1725 4.5.4. slice_height 1727 "slice_height" indicates the height on the slice raster formed by 1728 num_v_slices. 1730 Inferred to be 1 if not present. 1732 4.5.5. quant_table_set_index_count 1734 "quant_table_set_index_count" is defined as: 1736 1 + ( ( chroma_planes || version <= 3 ) ? 1 : 0 ) + ( extra_plane ? 1 1737 : 0 ) 1739 4.5.6. quant_table_set_index 1741 "quant_table_set_index" indicates the Quantization Table Set index to 1742 select the Quantization Table Set and the initial states for the 1743 slice. 1745 Inferred to be 0 if not present. 1747 4.5.7. picture_structure 1749 "picture_structure" specifies the temporal and spatial relationship 1750 of each "Line" of the "Frame". 1752 Inferred to be 0 if not present. 1754 +-------+-------------------------+ 1755 | value | picture structure used | 1756 +=======+=========================+ 1757 | 0 | unknown | 1758 +-------+-------------------------+ 1759 | 1 | top field first | 1760 +-------+-------------------------+ 1761 | 2 | bottom field first | 1762 +-------+-------------------------+ 1763 | 3 | progressive | 1764 +-------+-------------------------+ 1765 | Other | reserved for future use | 1766 +-------+-------------------------+ 1768 Table 16 1770 4.5.8. sar_num 1772 "sar_num" specifies the "Sample" aspect ratio numerator. 1774 Inferred to be 0 if not present. 1776 A value of 0 means that aspect ratio is unknown. 1778 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1780 If "sar_den" is 0, decoders SHOULD ignore the encoded value and 1781 consider that "sar_num" is 0. 1783 4.5.9. sar_den 1785 "sar_den" specifies the "Sample" aspect ratio denominator. 1787 Inferred to be 0 if not present. 1789 A value of 0 means that aspect ratio is unknown. 1791 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1793 If "sar_num" is 0, decoders SHOULD ignore the encoded value and 1794 consider that "sar_den" is 0. 1796 4.6. Slice Content 1798 A "Slice Content" contains all "Line" elements part of the "Slice". 1800 Depending on the configuration, "Line" elements are ordered by 1801 "Plane" then by row (YCbCr) or by row then by "Plane" (RGB). 1803 pseudo-code | type 1804 --------------------------------------------------------------|----- 1805 SliceContent( ) { | 1806 if (colorspace_type == 0) { | 1807 for (p = 0; p < primary_color_count; p++) { | 1808 for (y = 0; y < plane_pixel_height[ p ]; y++) { | 1809 Line( p, y ) | 1810 } | 1811 } | 1812 } else if (colorspace_type == 1) { | 1813 for (y = 0; y < slice_pixel_height; y++) { | 1814 for (p = 0; p < primary_color_count; p++) { | 1815 Line( p, y ) | 1816 } | 1817 } | 1818 } | 1819 } | 1821 4.6.1. primary_color_count 1823 "primary_color_count" is defined as: 1825 1 + ( chroma_planes ? 2 : 0 ) + ( extra_plane ? 1 : 0 ) 1827 4.6.2. plane_pixel_height 1829 "plane_pixel_height[ p ]" is the height in "Pixels" of "Plane" p of 1830 the "Slice". It is defined as: 1832 (chroma_planes == 1 && (p == 1 || p == 2)) ? ceil(slice_pixel_height 1833 / (1 << log2_v_chroma_subsample)) : slice_pixel_height 1835 4.6.3. slice_pixel_height 1837 "slice_pixel_height" is the height in pixels of the slice. It is 1838 defined as: 1840 floor( ( slice_y + slice_height ) * slice_pixel_height / num_v_slices 1841 ) - slice_pixel_y. 1843 4.6.4. slice_pixel_y 1845 "slice_pixel_y" is the slice vertical position in pixels. It is 1846 defined as: 1848 floor( slice_y * frame_pixel_height / num_v_slices ) 1850 4.7. Line 1852 A "Line" is a list of the sample differences (relative to the 1853 predictor) of primary color components. The pseudo-code below 1854 describes the contents of the "Line". 1856 pseudo-code | type 1857 --------------------------------------------------------------|----- 1858 Line( p, y ) { | 1859 if (colorspace_type == 0) { | 1860 for (x = 0; x < plane_pixel_width[ p ]; x++) { | 1861 sample_difference[ p ][ y ][ x ] | sd 1862 } | 1863 } else if (colorspace_type == 1) { | 1864 for (x = 0; x < slice_pixel_width; x++) { | 1865 sample_difference[ p ][ y ][ x ] | sd 1866 } | 1867 } | 1868 } | 1870 4.7.1. plane_pixel_width 1872 "plane_pixel_width[ p ]" is the width in "Pixels" of "Plane" p of the 1873 "Slice". It is defined as: 1875 (chroma_planes == 1 && (p == 1 || p == 2)) ? ceil( slice_pixel_width 1876 / (1 << log2_h_chroma_subsample) ) : slice_pixel_width. 1878 4.7.2. slice_pixel_width 1880 "slice_pixel_width" is the width in "Pixels" of the slice. It is 1881 defined as: 1883 floor( ( slice_x + slice_width ) * slice_pixel_width / num_h_slices ) 1884 - slice_pixel_x 1886 4.7.3. slice_pixel_x 1888 "slice_pixel_x" is the slice horizontal position in "Pixels". It is 1889 defined as: 1891 floor( slice_x * frame_pixel_width / num_h_slices ) 1893 4.7.4. sample_difference 1895 "sample_difference[ p ][ y ][ x ]" is the sample difference for 1896 "Sample" at "Plane" "p", y position "y", and x position "x". The 1897 "Sample" value is computed based on median predictor and context 1898 described in Section 3.2. 1900 4.8. Slice Footer 1902 A "Slice Footer" provides information about slice size and 1903 (optionally) parity. The pseudo-code below describes the contents of 1904 the "Slice Footer". 1906 Note: "Slice Footer" is always byte aligned. 1908 pseudo-code | type 1909 --------------------------------------------------------------|----- 1910 SliceFooter( ) { | 1911 slice_size | u(24) 1912 if (ec) { | 1913 error_status | u(8) 1914 slice_crc_parity | u(32) 1915 } | 1916 } | 1918 4.8.1. slice_size 1920 "slice_size" indicates the size of the slice in bytes. 1922 Note: this allows finding the start of slices before previous slices 1923 have been fully decoded, and allows parallel decoding as well as 1924 error resilience. 1926 4.8.2. error_status 1928 "error_status" specifies the error status. 1930 +-------+--------------------------------------+ 1931 | value | error status | 1932 +=======+======================================+ 1933 | 0 | no error | 1934 +-------+--------------------------------------+ 1935 | 1 | slice contains a correctable error | 1936 +-------+--------------------------------------+ 1937 | 2 | slice contains a uncorrectable error | 1938 +-------+--------------------------------------+ 1939 | Other | reserved for future use | 1940 +-------+--------------------------------------+ 1942 Table 17 1944 4.8.3. slice_crc_parity 1946 "slice_crc_parity" 32 bits that are chosen so that the slice as a 1947 whole has a crc remainder of 0. 1949 This is equivalent to storing the crc remainder in the 32-bit parity. 1951 The CRC generator polynomial used is the standard IEEE CRC polynomial 1952 (0x104C11DB7), with initial value 0, without pre-inversion and 1953 without post-inversion. 1955 4.9. Quantization Table Set 1957 The Quantization Table Sets are stored by storing the number of equal 1958 entries -1 of the first half of the table (represented as "len - 1" 1959 in the pseudo-code below) using the method described in 1960 Section 3.8.1.2. The second half doesn't need to be stored as it is 1961 identical to the first with flipped sign. "scale" and "len_count[ i 1962 ][ j ]" are temporary values used for the computing of 1963 "context_count[ i ]" and are not used outside Quantization Table Set 1964 pseudo-code. 1966 Example: 1968 Table: 0 0 1 1 1 1 2 2 -2 -2 -2 -1 -1 -1 -1 0 1969 Stored values: 1, 3, 1 1971 pseudo-code | type 1972 --------------------------------------------------------------|----- 1973 QuantizationTableSet( i ) { | 1974 scale = 1 | 1975 for (j = 0; j < MAX_CONTEXT_INPUTS; j++) { | 1976 QuantizationTable( i, j, scale ) | 1977 scale *= 2 * len_count[ i ][ j ] - 1 | 1978 } | 1979 context_count[ i ] = ceil( scale / 2 ) | 1980 } | 1982 "MAX_CONTEXT_INPUTS" is 5. 1984 pseudo-code | type 1985 --------------------------------------------------------------|----- 1986 QuantizationTable(i, j, scale) { | 1987 v = 0 | 1988 for (k = 0; k < 128;) { | 1989 len - 1 | ur 1990 for (a = 0; a < len; a++) { | 1991 quant_tables[ i ][ j ][ k ] = scale * v | 1992 k++ | 1993 } | 1994 v++ | 1995 } | 1996 for (k = 1; k < 128; k++) { | 1997 quant_tables[ i ][ j ][ 256 - k ] = \ | 1998 -quant_tables[ i ][ j ][ k ] | 1999 } | 2000 quant_tables[ i ][ j ][ 128 ] = \ | 2001 -quant_tables[ i ][ j ][ 127 ] | 2002 len_count[ i ][ j ] = v | 2003 } | 2005 4.9.1. quant_tables 2007 "quant_tables[ i ][ j ][ k ]" indicates the quantification table 2008 value of the Quantized Sample Difference "k" of the Quantization 2009 Table "j" of the Set Quantization Table Set "i". 2011 4.9.2. context_count 2013 "context_count[ i ]" indicates the count of contexts for Quantization 2014 Table Set "i". "context_count[ i ]" MUST be less than or equal to 2015 32768. 2017 5. Restrictions 2019 To ensure that fast multithreaded decoding is possible, starting with 2020 version 3 and if "frame_pixel_width * frame_pixel_height" is more 2021 than 101376, "slice_width * slice_height" MUST be less or equal to 2022 "num_h_slices * num_v_slices / 4". Note: 101376 is the frame size in 2023 "Pixels" of a 352x288 frame also known as CIF ("Common Intermediate 2024 Format") frame size format. 2026 For each "Frame", each position in the slice raster MUST be filled by 2027 one and only one slice of the "Frame" (no missing slice position, no 2028 slice overlapping). 2030 For each "Frame" with "keyframe" value of 0, each slice MUST have the 2031 same value of "slice_x", "slice_y", "slice_width", "slice_height" as 2032 a slice in the previous "Frame". 2034 6. Security Considerations 2036 Like any other codec, (such as [RFC6716]), FFV1 should not be used 2037 with insecure ciphers or cipher-modes that are vulnerable to known 2038 plaintext attacks. Some of the header bits as well as the padding 2039 are easily predictable. 2041 Implementations of the FFV1 codec need to take appropriate security 2042 considerations into account, as outlined in [RFC4732]. It is 2043 extremely important for the decoder to be robust against malicious 2044 payloads. Malicious payloads must not cause the decoder to overrun 2045 its allocated memory or to take an excessive amount of resources to 2046 decode. The same applies to the encoder, even though problems in 2047 encoders are typically rarer. Malicious video streams must not cause 2048 the encoder to misbehave because this would allow an attacker to 2049 attack transcoding gateways. A frequent security problem in image 2050 and video codecs is also to not check for integer overflows in 2051 "Pixel" count computations, that is to allocate width * height 2052 without considering that the multiplication result may have 2053 overflowed the arithmetic types range. The range coder could, if 2054 implemented naively, read one byte over the end. The implementation 2055 must ensure that no read outside allocated and initialized memory 2056 occurs. 2058 The reference implementation [REFIMPL] contains no known buffer 2059 overflow or cases where a specially crafted packet or video segment 2060 could cause a significant increase in CPU load. 2062 The reference implementation [REFIMPL] was validated in the following 2063 conditions: 2065 * Sending the decoder valid packets generated by the reference 2066 encoder and verifying that the decoder's output matches the 2067 encoder's input. 2069 * Sending the decoder packets generated by the reference encoder and 2070 then subjected to random corruption. 2072 * Sending the decoder random packets that are not FFV1. 2074 In all of the conditions above, the decoder and encoder was run 2075 inside the [VALGRIND] memory debugger as well as clangs address 2076 sanitizer [Address-Sanitizer], which track reads and writes to 2077 invalid memory regions as well as the use of uninitialized memory. 2078 There were no errors reported on any of the tested conditions. 2080 7. Media Type Definition 2082 This section completes the media type registration template defined 2083 in [RFC6838] and following [RFC4855]. 2085 Type name: video 2087 Subtype name: FFV1 2089 Required parameters: None. 2091 Optional parameters: 2093 This parameter is used to signal the capabilities of a receiver 2094 implementation. This parameter MUST NOT be used for any other 2095 purpose. 2097 "version": The "version" of the FFV1 encoding as defined by 2098 Section 4.1.1. 2100 "micro_version": The "micro_version" of the FFV1 encoding as defined 2101 by Section 4.1.2. 2103 "coder_type": The "coder_type" of the FFV1 encoding as defined by 2104 Section 4.1.3. 2106 "colorspace_type": The "colorspace_type" of the FFV1 encoding as 2107 defined by Section 4.1.5. 2109 "bits_per_raw_sample": The "bits_per_raw_sample" of the FFV1 encoding 2110 as defined by Section 4.1.7. 2112 "max_slices": The value of "max_slices" is an integer indicating the 2113 maximum count of slices with a frames of the FFV1 encoding. 2115 Encoding considerations: 2117 This media type is defined for encapsulation in several audiovisual 2118 container formats and contains binary data; see Section 4.2.3. This 2119 media type is framed binary data; see Section 4.8 of [RFC6838]. 2121 Security considerations: 2123 See Section 6 of this document. 2125 Interoperability considerations: None. 2127 Published specification: 2129 RFC XXXX. 2131 [RFC Editor: Upon publication as an RFC, please replace "XXXX" with 2132 the number assigned to this document and remove this note.] 2134 Applications which use this media type: 2136 Any application that requires the transport of lossless video can use 2137 this media type. Some examples are, but not limited to screen 2138 recording, scientific imaging, and digital video preservation. 2140 Fragment identifier considerations: N/A. 2142 Additional information: None. 2144 Person & email address to contact for further information: Michael 2145 Niedermayer michael@niedermayer.cc (mailto:michael@niedermayer.cc) 2147 Intended usage: COMMON 2149 Restrictions on usage: None. 2151 Author: Dave Rice dave@dericed.com (mailto:dave@dericed.com) 2153 Change controller: IETF cellar working group delegated from the IESG. 2155 8. IANA Considerations 2157 The IANA is requested to register the following values: 2159 * Media type registration as described in Section 7. 2161 9. Changelog 2163 See https://github.com/FFmpeg/FFV1/commits/master 2164 (https://github.com/FFmpeg/FFV1/commits/master) 2166 [RFC Editor: Please remove this Changelog section prior to 2167 publication.] 2169 10. Normative References 2171 [ISO.15444-1.2016] 2172 International Organization for Standardization, 2173 "Information technology -- JPEG 2000 image coding system: 2174 Core coding system", October 2016. 2176 [Matroska] IETF, "Matroska", 2019, . 2179 [RFC4732] Handley, M., Ed., Rescorla, E., Ed., and IAB, "Internet 2180 Denial-of-Service Considerations", RFC 4732, 2181 DOI 10.17487/RFC4732, December 2006, 2182 . 2184 [RFC4855] Casner, S., "Media Type Registration of RTP Payload 2185 Formats", RFC 4855, DOI 10.17487/RFC4855, February 2007, 2186 . 2188 [ISO.9899.2018] 2189 International Organization for Standardization, 2190 "Programming languages - C", 2018. 2192 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 2193 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716, 2194 September 2012, . 2196 [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 2197 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, 2198 May 2017, . 2200 [ISO.9899.1990] 2201 International Organization for Standardization, 2202 "Programming languages - C", 1990. 2204 [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type 2205 Specifications and Registration Procedures", BCP 13, 2206 RFC 6838, DOI 10.17487/RFC6838, January 2013, 2207 . 2209 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 2210 Requirement Levels", BCP 14, RFC 2119, 2211 DOI 10.17487/RFC2119, March 1997, 2212 . 2214 11. Informative References 2216 [YCbCr] Wikipedia, "YCbCr", undated, 2217 . 2219 [REFIMPL] Niedermayer, M., "The reference FFV1 implementation / the 2220 FFV1 codec in FFmpeg", undated, . 2222 [Address-Sanitizer] 2223 The Clang Team, "ASAN AddressSanitizer website", undated, 2224 . 2226 [VALGRIND] Valgrind Developers, "Valgrind website", undated, 2227 . 2229 [ISO.14495-1.1999] 2230 International Organization for Standardization, 2231 "Information technology -- Lossless and near-lossless 2232 compression of continuous-tone still images: Baseline", 2233 December 1999. 2235 [ISO.14496-10.2014] 2236 International Organization for Standardization, 2237 "Information technology -- Coding of audio-visual objects 2238 -- Part 10: Advanced Video Coding", September 2014. 2240 [ISO.14496-12.2015] 2241 International Organization for Standardization, 2242 "Information technology -- Coding of audio-visual objects 2243 -- Part 12: ISO base media file format", December 2015. 2245 [HuffYUV] Rudiak-Gould, B., "HuffYUV", December 2003, 2246 . 2249 [AVI] Microsoft, "AVI RIFF File Reference", undated, 2250 . 2253 [NUT] Niedermayer, M., "NUT Open Container Format", December 2254 2013, . 2256 [FFV1_V3] Niedermayer, M., "Commit to mark FFV1 version 3 as non- 2257 experimental", August 2013, . 2261 [range-coding] 2262 Nigel, G. and N. Martin, "Range encoding: an algorithm for 2263 removing redundancy from a digitised message.", July 1979. 2265 [FFV1_V0] Niedermayer, M., "Commit to mark FFV1 version 0 as non- 2266 experimental", April 2006, . 2270 [FFV1_V1] Niedermayer, M., "Commit to release FFV1 version 1", April 2271 2009, . 2274 Appendix A. Multi-theaded decoder implementation suggestions 2276 This appendix is informative. 2278 The FFV1 bitstream is parsable in two ways: in sequential order as 2279 described in this document or with the pre-analysis of the footer of 2280 each slice. Each slice footer contains a "slice_size" field so the 2281 boundary of each slice is computable without having to parse the 2282 slice content. That allows multi-threading as well as independence 2283 of slice content (a bitstream error in a slice header or slice 2284 content has no impact on the decoding of the other slices). 2286 After having checked "keyframe" field, a decoder SHOULD parse 2287 "slice_size" fields, from "slice_size" of the last slice at the end 2288 of the "Frame" up to "slice_size" of the first slice at the beginning 2289 of the "Frame", before parsing slices, in order to have slices 2290 boundaries. A decoder MAY fallback on sequential order e.g. in case 2291 of a corrupted "Frame" (frame size unknown, "slice_size" of slices 2292 not coherent...) or if there is no possibility of seeking into the 2293 stream. 2295 Appendix B. Future handling of some streams created by non conforming 2296 encoders 2298 This appendix is informative. 2300 Some bitstreams were found with 40 extra bits corresponding to 2301 "error_status" and "slice_crc_parity" in the "reserved" bits of 2302 "Slice()". Any revision of this specification SHOULD care about 2303 avoiding to add 40 bits of content after "SliceContent" if "version" 2304 == 0 or "version" == 1. Else a decoder conforming to the revised 2305 specification could not distinguish between a revised bitstream and 2306 such buggy bitstream in the wild. 2308 Authors' Addresses 2310 Michael Niedermayer 2312 Email: michael@niedermayer.cc 2314 Dave Rice 2316 Email: dave@dericed.com 2318 Jerome Martinez 2320 Email: jerome@mediaarea.net