idnits 2.17.1 draft-ietf-cellar-ffv1-15.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 (23 June 2020) is 1395 days in the past. Is this intentional? Checking references for intended status: Informational ---------------------------------------------------------------------------- -- Looks like a reference, but probably isn't: '41' on line 1024 Summary: 0 errors (**), 0 flaws (~~), 1 warning (==), 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: Informational D. Rice 5 Expires: 25 December 2020 6 J. Martinez 7 23 June 2020 9 FFV1 Video Coding Format Version 0, 1, and 3 10 draft-ietf-cellar-ffv1-15 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 25 December 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. Quantization Table Set . . . . . . . . . . . . . . . . . 26 81 4.1.1. quant_tables . . . . . . . . . . . . . . . . . . . . 27 82 4.1.2. context_count . . . . . . . . . . . . . . . . . . . . 28 83 4.2. Parameters . . . . . . . . . . . . . . . . . . . . . . . 28 84 4.2.1. version . . . . . . . . . . . . . . . . . . . . . . . 30 85 4.2.2. micro_version . . . . . . . . . . . . . . . . . . . . 30 86 4.2.3. coder_type . . . . . . . . . . . . . . . . . . . . . 31 87 4.2.4. state_transition_delta . . . . . . . . . . . . . . . 31 88 4.2.5. colorspace_type . . . . . . . . . . . . . . . . . . . 32 89 4.2.6. chroma_planes . . . . . . . . . . . . . . . . . . . . 32 90 4.2.7. bits_per_raw_sample . . . . . . . . . . . . . . . . . 33 91 4.2.8. log2_h_chroma_subsample . . . . . . . . . . . . . . . 33 92 4.2.9. log2_v_chroma_subsample . . . . . . . . . . . . . . . 33 93 4.2.10. extra_plane . . . . . . . . . . . . . . . . . . . . . 33 94 4.2.11. num_h_slices . . . . . . . . . . . . . . . . . . . . 34 95 4.2.12. num_v_slices . . . . . . . . . . . . . . . . . . . . 34 96 4.2.13. quant_table_set_count . . . . . . . . . . . . . . . . 34 97 4.2.14. states_coded . . . . . . . . . . . . . . . . . . . . 34 98 4.2.15. initial_state_delta . . . . . . . . . . . . . . . . . 34 99 4.2.16. ec . . . . . . . . . . . . . . . . . . . . . . . . . 35 100 4.2.17. intra . . . . . . . . . . . . . . . . . . . . . . . . 35 101 4.3. Configuration Record . . . . . . . . . . . . . . . . . . 36 102 4.3.1. reserved_for_future_use . . . . . . . . . . . . . . . 36 103 4.3.2. configuration_record_crc_parity . . . . . . . . . . . 36 104 4.3.3. Mapping FFV1 into Containers . . . . . . . . . . . . 36 105 4.4. Frame . . . . . . . . . . . . . . . . . . . . . . . . . . 37 106 4.5. Slice . . . . . . . . . . . . . . . . . . . . . . . . . . 39 107 4.6. Slice Header . . . . . . . . . . . . . . . . . . . . . . 40 108 4.6.1. slice_x . . . . . . . . . . . . . . . . . . . . . . . 41 109 4.6.2. slice_y . . . . . . . . . . . . . . . . . . . . . . . 41 110 4.6.3. slice_width . . . . . . . . . . . . . . . . . . . . . 41 111 4.6.4. slice_height . . . . . . . . . . . . . . . . . . . . 41 112 4.6.5. quant_table_set_index_count . . . . . . . . . . . . . 41 113 4.6.6. quant_table_set_index . . . . . . . . . . . . . . . . 42 114 4.6.7. picture_structure . . . . . . . . . . . . . . . . . . 42 115 4.6.8. sar_num . . . . . . . . . . . . . . . . . . . . . . . 42 116 4.6.9. sar_den . . . . . . . . . . . . . . . . . . . . . . . 43 117 4.7. Slice Content . . . . . . . . . . . . . . . . . . . . . . 43 118 4.7.1. primary_color_count . . . . . . . . . . . . . . . . . 43 119 4.7.2. plane_pixel_height . . . . . . . . . . . . . . . . . 43 120 4.7.3. slice_pixel_height . . . . . . . . . . . . . . . . . 44 121 4.7.4. slice_pixel_y . . . . . . . . . . . . . . . . . . . . 44 122 4.8. Line . . . . . . . . . . . . . . . . . . . . . . . . . . 44 123 4.8.1. plane_pixel_width . . . . . . . . . . . . . . . . . . 44 124 4.8.2. slice_pixel_width . . . . . . . . . . . . . . . . . . 45 125 4.8.3. slice_pixel_x . . . . . . . . . . . . . . . . . . . . 45 126 4.8.4. sample_difference . . . . . . . . . . . . . . . . . . 45 127 4.9. Slice Footer . . . . . . . . . . . . . . . . . . . . . . 45 128 4.9.1. slice_size . . . . . . . . . . . . . . . . . . . . . 45 129 4.9.2. error_status . . . . . . . . . . . . . . . . . . . . 46 130 4.9.3. slice_crc_parity . . . . . . . . . . . . . . . . . . 46 131 5. Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 46 132 6. Security Considerations . . . . . . . . . . . . . . . . . . . 47 133 7. Media Type Definition . . . . . . . . . . . . . . . . . . . . 48 134 8. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 49 135 9. Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . 49 136 10. Normative References . . . . . . . . . . . . . . . . . . . . 49 137 11. Informative References . . . . . . . . . . . . . . . . . . . 50 138 Appendix A. Multi-theaded decoder implementation suggestions . . 52 139 Appendix B. Future handling of some streams created by non 140 conforming encoders . . . . . . . . . . . . . . . . . . . 52 141 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 52 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.4) 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.3.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.5) 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 = [(sample_difference + 2 ^ (bits - 1)) & 675 (2 ^ bits - 1)] - 2 ^ (bits - 1) 677 Figure 7: Description of the coding of the Sample Difference in 678 the bitstream. 680 3.8.1. Range Coding Mode 682 Early experimental versions of FFV1 used the CABAC Arithmetic coder 683 from H.264 as defined in [ISO.14496-10.2014] but due to the uncertain 684 patent/royalty situation, as well as its slightly worse performance, 685 CABAC was replaced by a Range coder based on an algorithm defined by 686 G. Nigel and N. Martin in 1979 [range-coding]. 688 3.8.1.1. Range Binary Values 690 To encode binary digits efficiently a Range coder is used. "C(i)" is 691 the i-th Context. "B(i)" is the i-th byte of the bytestream. "b(i)" 692 is the i-th Range coded binary value, "S(0,i)" is the i-th initial 693 state. The length of the bytestream encoding n binary symbols is 694 "j(n)" bytes. 696 r_{i} = floor( ( R_{i} * S_{i,C_{i}} ) / 2 ^ 8 ) 698 Figure 8 700 S_{i+1,C_{i}} = zero_state_{S_{i,C_{i}}} AND 701 l_i = L_i AND 702 t_i = R_i - r_i <== 703 b_i = 0 <==> 704 L_i < R_i - r_i 706 S_{i+1,C_{i}} = one_state_{S_{i,C_{i}}} AND 707 l_i = L_i - R_i + r_i AND 708 t_i = r_i <== 709 b_i = 1 <==> 710 L_i >= R_i - r_i 712 Figure 9 714 S_{i+1,k} = S_{i,k} <== C_i != k 716 Figure 10 718 R_{i+1} = 2 ^ 8 * t_{i} AND 719 L_{i+1} = 2 ^ 8 * l_{i} + B_{j_{i}} AND 720 j_{i+1} = j_{i} + 1 <== 721 t_{i} < 2 ^ 8 723 R_{i+1} = t_{i} AND 724 L_{i+1} = l_{i} AND 725 j_{i+1} = j_{i} <== 726 t_{i} >= 2 ^ 8 728 Figure 11 730 R_{0} = 65280 732 Figure 12 734 L_{0} = 2 ^ 8 * B_{0} + B_{1} 736 Figure 13 738 j_{0} = 2 740 Figure 14 742 3.8.1.1.1. Termination 744 The range coder can be used in three modes. 746 * In "Open mode" when decoding, every symbol the reader attempts to 747 read is available. In this mode arbitrary data can have been 748 appended without affecting the range coder output. This mode is 749 not used in FFV1. 751 * In "Closed mode" the length in bytes of the bytestream is provided 752 to the range decoder. Bytes beyond the length are read as 0 by 753 the range decoder. This is generally one byte shorter than the 754 open mode. 756 * In "Sentinel mode" the exact length in bytes is not known and thus 757 the range decoder MAY read into the data that follows the range 758 coded bytestream by one byte. In "Sentinel mode", the end of the 759 range coded bytestream is a binary symbol with state 129, which 760 value SHALL be discarded. After reading this symbol, the range 761 decoder will have read one byte beyond the end of the range coded 762 bytestream. This way the byte position of the end can be 763 determined. Bytestreams written in "Sentinel mode" can be read in 764 "Closed mode" if the length can be determined, in this case the 765 last (sentinel) symbol will be read non-corrupted and be of value 766 0. 768 Above describes the range decoding. Encoding is defined as any 769 process which produces a decodable bytestream. 771 There are three places where range coder termination is needed in 772 FFV1. First is in the "Configuration Record", in this case the size 773 of the range coded bytestream is known and handled as "Closed mode". 774 Second is the switch from the "Slice Header" which is range coded to 775 Golomb coded slices as "Sentinel mode". Third is the end of range 776 coded Slices which need to terminate before the CRC at their end. 777 This can be handled as "Sentinel mode" or as "Closed mode" if the CRC 778 position has been determined. 780 3.8.1.2. Range Non Binary Values 782 To encode scalar integers, it would be possible to encode each bit 783 separately and use the past bits as context. However that would mean 784 255 contexts per 8-bit symbol that is not only a waste of memory but 785 also requires more past data to reach a reasonably good estimate of 786 the probabilities. Alternatively assuming a Laplacian distribution 787 and only dealing with its variance and mean (as in Huffman coding) 788 would also be possible, however, for maximum flexibility and 789 simplicity, the chosen method uses a single symbol to encode if a 790 number is 0, and if not, encodes the number using its exponent, 791 mantissa and sign. The exact contexts used are best described by 792 Figure 15. 794 int get_symbol(RangeCoder *c, uint8_t *state, int is_signed) { 795 if (get_rac(c, state + 0) { 796 return 0; 797 } 799 int e = 0; 800 while (get_rac(c, state + 1 + min(e, 9)) { //1..10 801 e++; 802 } 804 int a = 1; 805 for (int i = e - 1; i >= 0; i--) { 806 a = a * 2 + get_rac(c, state + 22 + min(i, 9)); // 22..31 807 } 809 if (!is_signed) { 810 return a; 811 } 813 if (get_rac(c, state + 11 + min(e, 10))) { //11..21 814 return -a; 815 } else { 816 return a; 817 } 818 } 820 Figure 15: A pseudo-code description of the contexts of Range Non 821 Binary Values. 823 "get_symbol" is used for the read out of "sample_difference" 824 indicated in Figure 7. 826 "get_rac" is the process described in Section 3.8.1.1. 828 3.8.1.3. Initial Values for the Context Model 830 At keyframes all Range coder state variables are set to their initial 831 state. 833 3.8.1.4. State Transition Table 835 one_state_{i} = 836 default_state_transition_{i} + state_transition_delta_{i} 838 Figure 16 840 zero_state_{i} = 256 - one_state_{256-i} 841 Figure 17 843 3.8.1.5. default_state_transition 845 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 847 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 849 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 851 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 853 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 855 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99,100,101,102,103, 857 104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118, 859 119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,133, 861 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149, 863 150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164, 865 165,166,167,168,169,170,171,171,172,173,174,175,176,177,178,179, 867 180,181,182,183,184,185,186,187,188,189,190,190,191,192,194,194, 869 195,196,197,198,199,200,201,202,202,204,205,206,207,208,209,209, 871 210,211,212,213,215,215,216,217,218,219,220,220,222,223,224,225, 873 226,227,227,229,229,230,231,232,234,234,235,236,237,238,239,240, 875 241,242,243,244,245,246,247,248,248, 0, 0, 0, 0, 0, 0, 0, 877 3.8.1.6. Alternative State Transition Table 879 The alternative state transition table has been built using iterative 880 minimization of frame sizes and generally performs better than the 881 default. To use it, the "coder_type" (see Section 4.2.3) MUST be set 882 to 2 and the difference to the default MUST be stored in the 883 "Parameters", see Section 4.2. The reference implementation of FFV1 884 in FFmpeg uses Figure 18 by default at the time of this writing when 885 Range coding is used. 887 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49, 889 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39, 891 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52, 893 53, 74, 55, 57, 58, 58, 74, 60,101, 61, 62, 84, 66, 66, 68, 69, 895 87, 82, 71, 97, 73, 73, 82, 75,111, 77, 94, 78, 87, 81, 83, 97, 897 85, 83, 94, 86, 99, 89, 90, 99,111, 92, 93,134, 95, 98,105, 98, 899 105,110,102,108,102,118,103,106,106,113,109,112,114,112,116,125, 901 115,116,117,117,126,119,125,121,121,123,145,124,126,131,127,129, 903 165,130,132,138,133,135,145,136,137,139,146,141,143,142,144,148, 905 147,155,151,149,151,150,152,157,153,154,156,168,158,162,161,160, 907 172,163,169,164,166,184,167,170,177,174,171,173,182,176,180,178, 909 175,189,179,181,186,183,192,185,200,187,191,188,190,197,193,196, 911 197,194,195,196,198,202,199,201,210,203,207,204,205,206,208,214, 913 209,211,221,212,213,215,224,216,217,218,219,220,222,228,223,225, 915 226,224,227,229,240,230,231,232,233,234,235,236,238,239,237,242, 917 241,243,242,244,245,246,247,248,249,250,251,252,252,253,254,255, 919 Figure 18: Alternative state transition table for Range coding. 921 3.8.2. Golomb Rice Mode 923 The end of the bitstream of the "Frame" is filled with 0-bits until 924 that the bitstream contains a multiple of 8 bits. 926 3.8.2.1. Signed Golomb Rice Codes 928 This coding mode uses Golomb Rice codes. The VLC is split into two 929 parts. The prefix stores the most significant bits and the suffix 930 stores the k least significant bits or stores the whole number in the 931 ESC case. 933 int get_ur_golomb(k) { 934 for (prefix = 0; prefix < 12; prefix++) { 935 if (get_bits(1)) { 936 return get_bits(k) + (prefix << k); 937 } 938 } 939 return get_bits(bits) + 11; 940 } 942 Figure 19: A pseudo-code description of the read of an unsigned 943 integer in Golomb Rice mode. 945 int get_sr_golomb(k) { 946 v = get_ur_golomb(k); 947 if (v & 1) return - (v >> 1) - 1; 948 else return (v >> 1); 949 } 951 Figure 20: A pseudo-code description of the read of a signed 952 integer in Golomb Rice mode. 954 3.8.2.1.1. Prefix 956 +----------------+-------+ 957 | bits | value | 958 +================+=======+ 959 | 1 | 0 | 960 +----------------+-------+ 961 | 01 | 1 | 962 +----------------+-------+ 963 | ... | ... | 964 +----------------+-------+ 965 | 0000 0000 01 | 9 | 966 +----------------+-------+ 967 | 0000 0000 001 | 10 | 968 +----------------+-------+ 969 | 0000 0000 0001 | 11 | 970 +----------------+-------+ 971 | 0000 0000 0000 | ESC | 972 +----------------+-------+ 974 Table 1 976 3.8.2.1.2. Suffix 978 +---------+----------------------------------------+ 979 +=========+========================================+ 980 | non ESC | the k least significant bits MSB first | 981 +---------+----------------------------------------+ 982 | ESC | the value - 11, in MSB first order | 983 +---------+----------------------------------------+ 985 Table 2 987 "ESC" MUST NOT be used if the value can be coded as "non ESC". 989 3.8.2.1.3. Examples 991 +-----+-------------------------+-------+ 992 | k | bits | value | 993 +=====+=========================+=======+ 994 | 0 | "1" | 0 | 995 +-----+-------------------------+-------+ 996 | 0 | "001" | 2 | 997 +-----+-------------------------+-------+ 998 | 2 | "1 00" | 0 | 999 +-----+-------------------------+-------+ 1000 | 2 | "1 10" | 2 | 1001 +-----+-------------------------+-------+ 1002 | 2 | "01 01" | 5 | 1003 +-----+-------------------------+-------+ 1004 | any | "000000000000 10000000" | 139 | 1005 +-----+-------------------------+-------+ 1007 Table 3 1009 3.8.2.2. Run Mode 1011 Run mode is entered when the context is 0 and left as soon as a non-0 1012 difference is found. The level is identical to the predicted one. 1013 The run and the first different level are coded. 1015 3.8.2.2.1. Run Length Coding 1017 The run value is encoded in two parts. The prefix part stores the 1018 more significant part of the run as well as adjusting the "run_index" 1019 that determines the number of bits in the less significant part of 1020 the run. The second part of the value stores the less significant 1021 part of the run as it is. The "run_index" is reset for each "Plane" 1022 and slice to 0. 1024 log2_run[41] = { 1025 0, 0, 0, 0, 1, 1, 1, 1, 1026 2, 2, 2, 2, 3, 3, 3, 3, 1027 4, 4, 5, 5, 6, 6, 7, 7, 1028 8, 9,10,11,12,13,14,15, 1029 16,17,18,19,20,21,22,23, 1030 24, 1031 }; 1033 if (run_count == 0 && run_mode == 1) { 1034 if (get_bits(1)) { 1035 run_count = 1 << log2_run[run_index]; 1036 if (x + run_count <= w) { 1037 run_index++; 1038 } 1039 } else { 1040 if (log2_run[run_index]) { 1041 run_count = get_bits(log2_run[run_index]); 1042 } else { 1043 run_count = 0; 1044 } 1045 if (run_index) { 1046 run_index--; 1047 } 1048 run_mode = 2; 1049 } 1050 } 1052 The "log2_run" array is also used within [ISO.14495-1.1999]. 1054 3.8.2.3. Sign extension 1056 "sign_extend" is the function of increasing the number of bits of an 1057 input binary number in twos complement signed number representation 1058 while preserving the input number's sign (positive/negative) and 1059 value, in order to fit in the output bit width. It MAY be computed 1060 with: 1062 sign_extend(input_number, input_bits) { 1063 negative_bias = 1 << (input_bits - 1); 1064 bits_mask = negative_bias - 1; 1065 output_number = input_number & bits_mask; // Remove negative bit 1066 is_negative = input_number & negative_bias; // Test negative bit 1067 if (is_negative) 1068 output_number -= negative_bias; 1069 return output_number 1070 } 1072 3.8.2.4. Scalar Mode 1074 Each difference is coded with the per context mean prediction removed 1075 and a per context value for k. 1077 get_vlc_symbol(state) { 1078 i = state->count; 1079 k = 0; 1080 while (i < state->error_sum) { 1081 k++; 1082 i += i; 1083 } 1085 v = get_sr_golomb(k); 1087 if (2 * state->drift < -state->count) { 1088 v = -1 - v; 1089 } 1091 ret = sign_extend(v + state->bias, bits); 1093 state->error_sum += abs(v); 1094 state->drift += v; 1096 if (state->count == 128) { 1097 state->count >>= 1; 1098 state->drift >>= 1; 1099 state->error_sum >>= 1; 1100 } 1101 state->count++; 1102 if (state->drift <= -state->count) { 1103 state->bias = max(state->bias - 1, -128); 1105 state->drift = max(state->drift + state->count, 1106 -state->count + 1); 1107 } else if (state->drift > 0) { 1108 state->bias = min(state->bias + 1, 127); 1110 state->drift = min(state->drift - state->count, 0); 1111 } 1113 return ret; 1114 } 1116 3.8.2.4.1. Level Coding 1118 Level coding is identical to the normal difference coding with the 1119 exception that the 0 value is removed as it cannot occur: 1121 diff = get_vlc_symbol(context_state); 1122 if (diff >= 0) { 1123 diff++; 1124 } 1126 Note, this is different from JPEG-LS, which doesn't use prediction in 1127 run mode and uses a different encoding and context model for the last 1128 difference. On a small set of test "Samples" the use of prediction 1129 slightly improved the compression rate. 1131 3.8.2.5. Initial Values for the VLC context state 1133 At keyframes all coder state variables are set to their initial 1134 state. 1136 drift = 0; 1137 error_sum = 4; 1138 bias = 0; 1139 count = 1; 1141 4. Bitstream 1143 An FFV1 bitstream is composed of a series of one or more "Frames" and 1144 (when required) a "Configuration Record". 1146 Within the following sub-sections, pseudo-code is used to explain the 1147 structure of each FFV1 bitstream component, as described in 1148 Section 2.2.1. Table 4 lists symbols used to annotate that pseudo- 1149 code in order to define the storage of the data referenced in that 1150 line of pseudo-code. 1152 +--------+----------------------------------------------+ 1153 | Symbol | Definition | 1154 +========+==============================================+ 1155 | u(n) | unsigned big endian integer using n bits | 1156 +--------+----------------------------------------------+ 1157 | sg | Golomb Rice coded signed scalar symbol coded | 1158 | | with the method described in Section 3.8.2 | 1159 +--------+----------------------------------------------+ 1160 | br | Range coded Boolean (1-bit) symbol with the | 1161 | | method described in Section 3.8.1.1 | 1162 +--------+----------------------------------------------+ 1163 | ur | Range coded unsigned scalar symbol coded | 1164 | | with the method described in Section 3.8.1.2 | 1165 +--------+----------------------------------------------+ 1166 | sr | Range coded signed scalar symbol coded with | 1167 | | the method described in Section 3.8.1.2 | 1168 +--------+----------------------------------------------+ 1169 | sd | Sample difference coded with the method | 1170 | | described in Section 3.8 | 1171 +--------+----------------------------------------------+ 1173 Table 4: Definition of pseudo-code symbols for this 1174 document. 1176 The following MUST be provided by external means during 1177 initialization of the decoder: 1179 "frame_pixel_width" is defined as "Frame" width in "Pixels". 1181 "frame_pixel_height" is defined as "Frame" height in "Pixels". 1183 Default values at the decoder initialization phase: 1185 "ConfigurationRecordIsPresent" is set to 0. 1187 4.1. Quantization Table Set 1189 The Quantization Table Sets are stored by storing the number of equal 1190 entries -1 of the first half of the table (represented as "len - 1" 1191 in the pseudo-code below) using the method described in 1192 Section 3.8.1.2. The second half doesn't need to be stored as it is 1193 identical to the first with flipped sign. "scale" and "len_count[ i 1194 ][ j ]" are temporary values used for the computing of 1195 "context_count[ i ]" and are not used outside Quantization Table Set 1196 pseudo-code. 1198 Example: 1200 Table: 0 0 1 1 1 1 2 2 -2 -2 -2 -1 -1 -1 -1 0 1202 Stored values: 1, 3, 1 1204 "QuantizationTableSet" has its own initial states, all set to 128. 1206 pseudo-code | type 1207 --------------------------------------------------------------|----- 1208 QuantizationTableSet( i ) { | 1209 scale = 1 | 1210 for (j = 0; j < MAX_CONTEXT_INPUTS; j++) { | 1211 QuantizationTable( i, j, scale ) | 1212 scale *= 2 * len_count[ i ][ j ] - 1 | 1213 } | 1214 context_count[ i ] = ceil( scale / 2 ) | 1215 } | 1217 "MAX_CONTEXT_INPUTS" is 5. 1219 pseudo-code | type 1220 --------------------------------------------------------------|----- 1221 QuantizationTable(i, j, scale) { | 1222 v = 0 | 1223 for (k = 0; k < 128;) { | 1224 len - 1 | ur 1225 for (n = 0; n < len; n++) { | 1226 quant_tables[ i ][ j ][ k ] = scale * v | 1227 k++ | 1228 } | 1229 v++ | 1230 } | 1231 for (k = 1; k < 128; k++) { | 1232 quant_tables[ i ][ j ][ 256 - k ] = \ | 1233 -quant_tables[ i ][ j ][ k ] | 1234 } | 1235 quant_tables[ i ][ j ][ 128 ] = \ | 1236 -quant_tables[ i ][ j ][ 127 ] | 1237 len_count[ i ][ j ] = v | 1238 } | 1240 4.1.1. quant_tables 1242 "quant_tables[ i ][ j ][ k ]" indicates the quantification table 1243 value of the Quantized Sample Difference "k" of the Quantization 1244 Table "j" of the Set Quantization Table Set "i". 1246 4.1.2. context_count 1248 "context_count[ i ]" indicates the count of contexts for Quantization 1249 Table Set "i". "context_count[ i ]" MUST be less than or equal to 1250 32768. 1252 4.2. Parameters 1254 The "Parameters" section contains significant characteristics about 1255 the decoding configuration used for all instances of "Frame" (in FFV1 1256 version 0 and 1) or the whole FFV1 bitstream (other versions), 1257 including the stream version, color configuration, and quantization 1258 tables. Figure 21 describes the contents of the bitstream. 1260 "Parameters" has its own initial states, all set to 128. 1262 pseudo-code | type 1263 --------------------------------------------------------------|----- 1264 Parameters( ) { | 1265 version | ur 1266 if (version >= 3) { | 1267 micro_version | ur 1268 } | 1269 coder_type | ur 1270 if (coder_type > 1) { | 1271 for (i = 1; i < 256; i++) { | 1272 state_transition_delta[ i ] | sr 1273 } | 1274 } | 1275 colorspace_type | ur 1276 if (version >= 1) { | 1277 bits_per_raw_sample | ur 1278 } | 1279 chroma_planes | br 1280 log2_h_chroma_subsample | ur 1281 log2_v_chroma_subsample | ur 1282 extra_plane | br 1283 if (version >= 3) { | 1284 num_h_slices - 1 | ur 1285 num_v_slices - 1 | ur 1286 quant_table_set_count | ur 1287 } | 1288 for (i = 0; i < quant_table_set_count; i++) { | 1289 QuantizationTableSet( i ) | 1290 } | 1291 if (version >= 3) { | 1292 for (i = 0; i < quant_table_set_count; i++) { | 1293 states_coded | br 1294 if (states_coded) { | 1295 for (j = 0; j < context_count[ i ]; j++) { | 1296 for (k = 0; k < CONTEXT_SIZE; k++) { | 1297 initial_state_delta[ i ][ j ][ k ] | sr 1298 } | 1299 } | 1300 } | 1301 } | 1302 ec | ur 1303 intra | ur 1304 } | 1305 } | 1307 Figure 21: A pseudo-code description of the bitstream contents. 1309 CONTEXT_SIZE is 32. 1311 4.2.1. version 1313 "version" specifies the version of the FFV1 bitstream. 1315 Each version is incompatible with other versions: decoders SHOULD 1316 reject FFV1 bitstreams due to an unknown version. 1318 Decoders SHOULD reject FFV1 bitstreams with version <= 1 && 1319 ConfigurationRecordIsPresent == 1. 1321 Decoders SHOULD reject FFV1 bitstreams with version >= 3 && 1322 ConfigurationRecordIsPresent == 0. 1324 +-------+-------------------------+ 1325 | value | version | 1326 +=======+=========================+ 1327 | 0 | FFV1 version 0 | 1328 +-------+-------------------------+ 1329 | 1 | FFV1 version 1 | 1330 +-------+-------------------------+ 1331 | 2 | reserved* | 1332 +-------+-------------------------+ 1333 | 3 | FFV1 version 3 | 1334 +-------+-------------------------+ 1335 | Other | reserved for future use | 1336 +-------+-------------------------+ 1338 Table 5 1340 * Version 2 was experimental and this document does not describe it. 1342 4.2.2. micro_version 1344 "micro_version" specifies the micro-version of the FFV1 bitstream. 1346 After a version is considered stable (a micro-version value is 1347 assigned to be the first stable variant of a specific version), each 1348 new micro-version after this first stable variant is compatible with 1349 the previous micro-version: decoders SHOULD NOT reject FFV1 1350 bitstreams due to an unknown micro-version equal or above the micro- 1351 version considered as stable. 1353 Meaning of "micro_version" for "version" 3: 1355 +-------+-------------------------+ 1356 | value | micro_version | 1357 +=======+=========================+ 1358 | 0...3 | reserved* | 1359 +-------+-------------------------+ 1360 | 4 | first stable variant | 1361 +-------+-------------------------+ 1362 | Other | reserved for future use | 1363 +-------+-------------------------+ 1365 Table 6: The definitions for 1366 "micro_version" values for FFV1 1367 version 3. 1369 * development versions may be incompatible with the stable variants. 1371 4.2.3. coder_type 1373 "coder_type" specifies the coder used. 1375 +-------+-------------------------------------------------+ 1376 | value | coder used | 1377 +=======+=================================================+ 1378 | 0 | Golomb Rice | 1379 +-------+-------------------------------------------------+ 1380 | 1 | Range Coder with default state transition table | 1381 +-------+-------------------------------------------------+ 1382 | 2 | Range Coder with custom state transition table | 1383 +-------+-------------------------------------------------+ 1384 | Other | reserved for future use | 1385 +-------+-------------------------------------------------+ 1387 Table 7 1389 Restrictions: 1391 If "coder_type" is 0, then "bits_per_raw_sample" SHOULD NOT be > 8. 1393 Background: At the time of this writing, there is no known 1394 implementation of FFV1 bitstream supporting Golomb Rice algorithm 1395 with "bits_per_raw_sample" greater than 8, and Range Coder is 1396 prefered. 1398 4.2.4. state_transition_delta 1400 "state_transition_delta" specifies the Range coder custom state 1401 transition table. 1403 If "state_transition_delta" is not present in the FFV1 bitstream, all 1404 Range coder custom state transition table elements are assumed to be 1405 0. 1407 4.2.5. colorspace_type 1409 "colorspace_type" specifies the color space encoded, the pixel 1410 transformation used by the encoder, the extra plane content, as well 1411 as interleave method. 1413 +-------+-------------+----------------+--------------+-------------+ 1414 | value | color space | pixel | extra plane | interleave | 1415 | | encoded | transformation | content | method | 1416 +=======+=============+================+==============+=============+ 1417 | 0 | YCbCr | None | Transparency | "Plane" | 1418 | | | | | then | 1419 | | | | | "Line" | 1420 +-------+-------------+----------------+--------------+-------------+ 1421 | 1 | RGB | JPEG2000-RCT | Transparency | "Line" | 1422 | | | | | then | 1423 | | | | | "Plane" | 1424 +-------+-------------+----------------+--------------+-------------+ 1425 | Other | reserved | reserved for | reserved for | reserved | 1426 | | for future | future use | future use | for future | 1427 | | use | | | use | 1428 +-------+-------------+----------------+--------------+-------------+ 1430 Table 8 1432 FFV1 bitstreams with "colorspace_type" == 1 && ("chroma_planes" != 1433 1 || "log2_h_chroma_subsample" != 0 || "log2_v_chroma_subsample" != 1434 0) are not part of this specification. 1436 4.2.6. chroma_planes 1438 "chroma_planes" indicates if chroma (color) "Planes" are present. 1440 +-------+---------------------------------+ 1441 | value | presence | 1442 +=======+=================================+ 1443 | 0 | chroma "Planes" are not present | 1444 +-------+---------------------------------+ 1445 | 1 | chroma "Planes" are present | 1446 +-------+---------------------------------+ 1448 Table 9 1450 4.2.7. bits_per_raw_sample 1452 "bits_per_raw_sample" indicates the number of bits for each "Sample". 1453 Inferred to be 8 if not present. 1455 +-------+-----------------------------------+ 1456 | value | bits for each sample | 1457 +=======+===================================+ 1458 | 0 | reserved* | 1459 +-------+-----------------------------------+ 1460 | Other | the actual bits for each "Sample" | 1461 +-------+-----------------------------------+ 1463 Table 10 1465 * Encoders MUST NOT store "bits_per_raw_sample" = 0. Decoders SHOULD 1466 accept and interpret "bits_per_raw_sample" = 0 as 8. 1468 4.2.8. log2_h_chroma_subsample 1470 "log2_h_chroma_subsample" indicates the subsample factor, stored in 1471 powers to which the number 2 must be raised, between luma and chroma 1472 width ("chroma_width = 2 ^ -log2_h_chroma_subsample * luma_width"). 1474 4.2.9. log2_v_chroma_subsample 1476 "log2_v_chroma_subsample" indicates the subsample factor, stored in 1477 powers to which the number 2 must be raised, between luma and chroma 1478 height ("chroma_height = 2 ^ -log2_v_chroma_subsample * 1479 luma_height"). 1481 4.2.10. extra_plane 1483 "extra_plane" indicates if an extra "Plane" is present. 1485 +-------+------------------------------+ 1486 | value | presence | 1487 +=======+==============================+ 1488 | 0 | extra "Plane" is not present | 1489 +-------+------------------------------+ 1490 | 1 | extra "Plane" is present | 1491 +-------+------------------------------+ 1493 Table 11 1495 4.2.11. num_h_slices 1497 "num_h_slices" indicates the number of horizontal elements of the 1498 slice raster. 1500 Inferred to be 1 if not present. 1502 4.2.12. num_v_slices 1504 "num_v_slices" indicates the number of vertical elements of the slice 1505 raster. 1507 Inferred to be 1 if not present. 1509 4.2.13. quant_table_set_count 1511 "quant_table_set_count" indicates the number of Quantization 1512 Table Sets. "quant_table_set_count" MUST be less than or equal to 8. 1514 Inferred to be 1 if not present. 1516 MUST NOT be 0. 1518 4.2.14. states_coded 1520 "states_coded" indicates if the respective Quantization Table Set has 1521 the initial states coded. 1523 Inferred to be 0 if not present. 1525 +-------+--------------------------------+ 1526 | value | initial states | 1527 +=======+================================+ 1528 | 0 | initial states are not present | 1529 | | and are assumed to be all 128 | 1530 +-------+--------------------------------+ 1531 | 1 | initial states are present | 1532 +-------+--------------------------------+ 1534 Table 12 1536 4.2.15. initial_state_delta 1538 "initial_state_delta[ i ][ j ][ k ]" indicates the initial Range 1539 coder state, it is encoded using "k" as context index and 1541 pred = j ? initial_states[ i ][j - 1][ k ] : 128 1542 Figure 22 1544 initial_state[ i ][ j ][ k ] = 1545 ( pred + initial_state_delta[ i ][ j ][ k ] ) & 255 1547 Figure 23 1549 4.2.16. ec 1551 "ec" indicates the error detection/correction type. 1553 +-------+-------------------------------------------------+ 1554 | value | error detection/correction type | 1555 +=======+=================================================+ 1556 | 0 | 32-bit CRC in "ConfigurationRecord" | 1557 +-------+-------------------------------------------------+ 1558 | 1 | 32-bit CRC in "Slice" and "ConfigurationRecord" | 1559 +-------+-------------------------------------------------+ 1560 | Other | reserved for future use | 1561 +-------+-------------------------------------------------+ 1563 Table 13 1565 4.2.17. intra 1567 "intra" indicates the constraint on "keyframe" in each instance of 1568 "Frame". 1570 Inferred to be 0 if not present. 1572 +-------+-------------------------------------------------------+ 1573 | value | relationship | 1574 +=======+=======================================================+ 1575 | 0 | "keyframe" can be 0 or 1 (non keyframes or keyframes) | 1576 +-------+-------------------------------------------------------+ 1577 | 1 | "keyframe" MUST be 1 (keyframes only) | 1578 +-------+-------------------------------------------------------+ 1579 | Other | reserved for future use | 1580 +-------+-------------------------------------------------------+ 1582 Table 14 1584 4.3. Configuration Record 1586 In the case of a FFV1 bitstream with "version >= 3", a "Configuration 1587 Record" is stored in the underlying "Container" as described in 1588 Section 4.3.3. It contains the "Parameters" used for all instances 1589 of "Frame". The size of the "Configuration Record", "NumBytes", is 1590 supplied by the underlying "Container". 1592 pseudo-code | type 1593 -----------------------------------------------------------|----- 1594 ConfigurationRecord( NumBytes ) { | 1595 ConfigurationRecordIsPresent = 1 | 1596 Parameters( ) | 1597 while (remaining_symbols_in_syntax(NumBytes - 4)) { | 1598 reserved_for_future_use | br/ur/sr 1599 } | 1600 configuration_record_crc_parity | u(32) 1601 } | 1603 4.3.1. reserved_for_future_use 1605 "reserved_for_future_use" has semantics that are reserved for future 1606 use. 1608 Encoders conforming to this version of this specification SHALL NOT 1609 write this value. 1611 Decoders conforming to this version of this specification SHALL 1612 ignore its value. 1614 4.3.2. configuration_record_crc_parity 1616 "configuration_record_crc_parity" 32 bits that are chosen so that the 1617 "Configuration Record" as a whole has a CRC remainder of 0. 1619 This is equivalent to storing the CRC remainder in the 32-bit parity. 1621 The CRC generator polynomial used is described in Section 4.9.3. 1623 4.3.3. Mapping FFV1 into Containers 1625 This "Configuration Record" can be placed in any file format 1626 supporting "Configuration Records", fitting as much as possible with 1627 how the file format uses to store "Configuration Records". The 1628 "Configuration Record" storage place and "NumBytes" are currently 1629 defined and supported by this version of this specification for the 1630 following formats: 1632 4.3.3.1. AVI File Format 1634 The "Configuration Record" extends the stream format chunk ("AVI ", 1635 "hdlr", "strl", "strf") with the ConfigurationRecord bitstream. 1637 See [AVI] for more information about chunks. 1639 "NumBytes" is defined as the size, in bytes, of the strf chunk 1640 indicated in the chunk header minus the size of the stream format 1641 structure. 1643 4.3.3.2. ISO Base Media File Format 1645 The "Configuration Record" extends the sample description box 1646 ("moov", "trak", "mdia", "minf", "stbl", "stsd") with a "glbl" box 1647 that contains the ConfigurationRecord bitstream. See 1648 [ISO.14496-12.2015] for more information about boxes. 1650 "NumBytes" is defined as the size, in bytes, of the "glbl" box 1651 indicated in the box header minus the size of the box header. 1653 4.3.3.3. NUT File Format 1655 The "codec_specific_data" element (in "stream_header" packet) 1656 contains the ConfigurationRecord bitstream. See [NUT] for more 1657 information about elements. 1659 "NumBytes" is defined as the size, in bytes, of the 1660 "codec_specific_data" element as indicated in the "length" field of 1661 "codec_specific_data". 1663 4.3.3.4. Matroska File Format 1665 FFV1 SHOULD use "V_FFV1" as the Matroska "Codec ID". For FFV1 1666 versions 2 or less, the Matroska "CodecPrivate" Element SHOULD NOT be 1667 used. For FFV1 versions 3 or greater, the Matroska "CodecPrivate" 1668 Element MUST contain the FFV1 "Configuration Record" structure and no 1669 other data. See [Matroska] for more information about elements. 1671 "NumBytes" is defined as the "Element Data Size" of the 1672 "CodecPrivate" Element. 1674 4.4. Frame 1676 A "Frame" is an encoded representation of a complete static image. 1677 The whole "Frame" is provided by the underlaying container. 1679 A "Frame" consists of the "keyframe" field, "Parameters" (if 1680 "version" <= 1), and a sequence of independent slices. The pseudo- 1681 code below describes the contents of a "Frame". 1683 "keyframe" field has its own initial state, set to 128. 1685 pseudo-code | type 1686 --------------------------------------------------------------|----- 1687 Frame( NumBytes ) { | 1688 keyframe | br 1689 if (keyframe && !ConfigurationRecordIsPresent { | 1690 Parameters( ) | 1691 } | 1692 while (remaining_bits_in_bitstream( NumBytes )) { | 1693 Slice( ) | 1694 } | 1695 } | 1697 Architecture overview of slices in a "Frame": 1699 +-----------------------------------------------------------------+ 1700 +=================================================================+ 1701 | first slice header | 1702 +-----------------------------------------------------------------+ 1703 | first slice content | 1704 +-----------------------------------------------------------------+ 1705 | first slice footer | 1706 +-----------------------------------------------------------------+ 1707 | --------------------------------------------------------------- | 1708 +-----------------------------------------------------------------+ 1709 | second slice header | 1710 +-----------------------------------------------------------------+ 1711 | second slice content | 1712 +-----------------------------------------------------------------+ 1713 | second slice footer | 1714 +-----------------------------------------------------------------+ 1715 | --------------------------------------------------------------- | 1716 +-----------------------------------------------------------------+ 1717 | ... | 1718 +-----------------------------------------------------------------+ 1719 | --------------------------------------------------------------- | 1720 +-----------------------------------------------------------------+ 1721 | last slice header | 1722 +-----------------------------------------------------------------+ 1723 | last slice content | 1724 +-----------------------------------------------------------------+ 1725 | last slice footer | 1726 +-----------------------------------------------------------------+ 1728 Table 15 1730 4.5. Slice 1732 A "Slice" is an independent spatial sub-section of a "Frame" that is 1733 encoded separately from another region of the same "Frame". The use 1734 of more than one "Slice" per "Frame" can be useful for taking 1735 advantage of the opportunities of multithreaded encoding and 1736 decoding. 1738 A "Slice" consists of a "Slice Header" (when relevant), a "Slice 1739 Content", and a "Slice Footer" (when relevant). The pseudo-code 1740 below describes the contents of a "Slice". 1742 pseudo-code | type 1743 --------------------------------------------------------------|----- 1744 Slice( ) { | 1745 if (version >= 3) { | 1746 SliceHeader( ) | 1747 } | 1748 SliceContent( ) | 1749 if (coder_type == 0) { | 1750 while (!byte_aligned()) { | 1751 padding | u(1) 1752 } | 1753 } | 1754 if (version <= 1) { | 1755 while (remaining_bits_in_bitstream( NumBytes ) != 0) {| 1756 reserved | u(1) 1757 } | 1758 } | 1759 if (version >= 3) { | 1760 SliceFooter( ) | 1761 } | 1762 } | 1764 "padding" specifies a bit without any significance and used only for 1765 byte alignment. MUST be 0. 1767 "reserved" specifies a bit without any significance in this revision 1768 of the specification and may have a significance in a later revision 1769 of this specification. 1771 Encoders SHOULD NOT fill these bits. 1773 Decoders SHOULD ignore these bits. 1775 4.6. Slice Header 1777 A "Slice Header" provides information about the decoding 1778 configuration of the "Slice", such as its spatial position, size, and 1779 aspect ratio. The pseudo-code below describes the contents of the 1780 "Slice Header". 1782 "Slice Header" has its own initial states, all set to 128. 1784 pseudo-code | type 1785 --------------------------------------------------------------|----- 1786 SliceHeader( ) { | 1787 slice_x | ur 1788 slice_y | ur 1789 slice_width - 1 | ur 1790 slice_height - 1 | ur 1791 for (i = 0; i < quant_table_set_index_count; i++) { | 1792 quant_table_set_index[ i ] | ur 1793 } | 1794 picture_structure | ur 1795 sar_num | ur 1796 sar_den | ur 1797 } | 1799 4.6.1. slice_x 1801 "slice_x" indicates the x position on the slice raster formed by 1802 num_h_slices. 1804 Inferred to be 0 if not present. 1806 4.6.2. slice_y 1808 "slice_y" indicates the y position on the slice raster formed by 1809 num_v_slices. 1811 Inferred to be 0 if not present. 1813 4.6.3. slice_width 1815 "slice_width" indicates the width on the slice raster formed by 1816 num_h_slices. 1818 Inferred to be 1 if not present. 1820 4.6.4. slice_height 1822 "slice_height" indicates the height on the slice raster formed by 1823 num_v_slices. 1825 Inferred to be 1 if not present. 1827 4.6.5. quant_table_set_index_count 1829 "quant_table_set_index_count" is defined as: 1831 1 + ( ( chroma_planes || version <= 3 ) ? 1 : 0 ) + ( extra_plane ? 1 1832 : 0 ) 1834 4.6.6. quant_table_set_index 1836 "quant_table_set_index" indicates the Quantization Table Set index to 1837 select the Quantization Table Set and the initial states for the 1838 "Slice Content". 1840 Inferred to be 0 if not present. 1842 4.6.7. picture_structure 1844 "picture_structure" specifies the temporal and spatial relationship 1845 of each "Line" of the "Frame". 1847 Inferred to be 0 if not present. 1849 +-------+-------------------------+ 1850 | value | picture structure used | 1851 +=======+=========================+ 1852 | 0 | unknown | 1853 +-------+-------------------------+ 1854 | 1 | top field first | 1855 +-------+-------------------------+ 1856 | 2 | bottom field first | 1857 +-------+-------------------------+ 1858 | 3 | progressive | 1859 +-------+-------------------------+ 1860 | Other | reserved for future use | 1861 +-------+-------------------------+ 1863 Table 16 1865 4.6.8. sar_num 1867 "sar_num" specifies the "Sample" aspect ratio numerator. 1869 Inferred to be 0 if not present. 1871 A value of 0 means that aspect ratio is unknown. 1873 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1875 If "sar_den" is 0, decoders SHOULD ignore the encoded value and 1876 consider that "sar_num" is 0. 1878 4.6.9. sar_den 1880 "sar_den" specifies the "Sample" aspect ratio denominator. 1882 Inferred to be 0 if not present. 1884 A value of 0 means that aspect ratio is unknown. 1886 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1888 If "sar_num" is 0, decoders SHOULD ignore the encoded value and 1889 consider that "sar_den" is 0. 1891 4.7. Slice Content 1893 A "Slice Content" contains all "Line" elements part of the "Slice". 1895 Depending on the configuration, "Line" elements are ordered by 1896 "Plane" then by row (YCbCr) or by row then by "Plane" (RGB). 1898 pseudo-code | type 1899 --------------------------------------------------------------|----- 1900 SliceContent( ) { | 1901 if (colorspace_type == 0) { | 1902 for (p = 0; p < primary_color_count; p++) { | 1903 for (y = 0; y < plane_pixel_height[ p ]; y++) { | 1904 Line( p, y ) | 1905 } | 1906 } | 1907 } else if (colorspace_type == 1) { | 1908 for (y = 0; y < slice_pixel_height; y++) { | 1909 for (p = 0; p < primary_color_count; p++) { | 1910 Line( p, y ) | 1911 } | 1912 } | 1913 } | 1914 } | 1916 4.7.1. primary_color_count 1918 "primary_color_count" is defined as: 1920 1 + ( chroma_planes ? 2 : 0 ) + ( extra_plane ? 1 : 0 ) 1922 4.7.2. plane_pixel_height 1924 "plane_pixel_height[ p ]" is the height in "Pixels" of "Plane" p of 1925 the "Slice". It is defined as: 1927 (chroma_planes == 1 && (p == 1 || p == 2)) ? ceil(slice_pixel_height 1928 / (1 << log2_v_chroma_subsample)) : slice_pixel_height 1930 4.7.3. slice_pixel_height 1932 "slice_pixel_height" is the height in pixels of the slice. It is 1933 defined as: 1935 floor( ( slice_y + slice_height ) * slice_pixel_height / num_v_slices 1936 ) - slice_pixel_y. 1938 4.7.4. slice_pixel_y 1940 "slice_pixel_y" is the slice vertical position in pixels. It is 1941 defined as: 1943 floor( slice_y * frame_pixel_height / num_v_slices ) 1945 4.8. Line 1947 A "Line" is a list of the sample differences (relative to the 1948 predictor) of primary color components. The pseudo-code below 1949 describes the contents of the "Line". 1951 pseudo-code | type 1952 --------------------------------------------------------------|----- 1953 Line( p, y ) { | 1954 if (colorspace_type == 0) { | 1955 for (x = 0; x < plane_pixel_width[ p ]; x++) { | 1956 sample_difference[ p ][ y ][ x ] | sd 1957 } | 1958 } else if (colorspace_type == 1) { | 1959 for (x = 0; x < slice_pixel_width; x++) { | 1960 sample_difference[ p ][ y ][ x ] | sd 1961 } | 1962 } | 1963 } | 1965 4.8.1. plane_pixel_width 1967 "plane_pixel_width[ p ]" is the width in "Pixels" of "Plane" p of the 1968 "Slice". It is defined as: 1970 (chroma_planes == 1 && (p == 1 || p == 2)) ? ceil( slice_pixel_width 1971 / (1 << log2_h_chroma_subsample) ) : slice_pixel_width. 1973 4.8.2. slice_pixel_width 1975 "slice_pixel_width" is the width in "Pixels" of the slice. It is 1976 defined as: 1978 floor( ( slice_x + slice_width ) * slice_pixel_width / num_h_slices ) 1979 - slice_pixel_x 1981 4.8.3. slice_pixel_x 1983 "slice_pixel_x" is the slice horizontal position in "Pixels". It is 1984 defined as: 1986 floor( slice_x * frame_pixel_width / num_h_slices ) 1988 4.8.4. sample_difference 1990 "sample_difference[ p ][ y ][ x ]" is the sample difference for 1991 "Sample" at "Plane" "p", y position "y", and x position "x". The 1992 "Sample" value is computed based on median predictor and context 1993 described in Section 3.2. 1995 4.9. Slice Footer 1997 A "Slice Footer" provides information about slice size and 1998 (optionally) parity. The pseudo-code below describes the contents of 1999 the "Slice Footer". 2001 Note: "Slice Footer" is always byte aligned. 2003 pseudo-code | type 2004 --------------------------------------------------------------|----- 2005 SliceFooter( ) { | 2006 slice_size | u(24) 2007 if (ec) { | 2008 error_status | u(8) 2009 slice_crc_parity | u(32) 2010 } | 2011 } | 2013 4.9.1. slice_size 2015 "slice_size" indicates the size of the slice in bytes. 2017 Note: this allows finding the start of slices before previous slices 2018 have been fully decoded, and allows parallel decoding as well as 2019 error resilience. 2021 4.9.2. error_status 2023 "error_status" specifies the error status. 2025 +-------+--------------------------------------+ 2026 | value | error status | 2027 +=======+======================================+ 2028 | 0 | no error | 2029 +-------+--------------------------------------+ 2030 | 1 | slice contains a correctable error | 2031 +-------+--------------------------------------+ 2032 | 2 | slice contains a uncorrectable error | 2033 +-------+--------------------------------------+ 2034 | Other | reserved for future use | 2035 +-------+--------------------------------------+ 2037 Table 17 2039 4.9.3. slice_crc_parity 2041 "slice_crc_parity" 32 bits that are chosen so that the slice as a 2042 whole has a crc remainder of 0. 2044 This is equivalent to storing the crc remainder in the 32-bit parity. 2046 The CRC generator polynomial used is the standard IEEE CRC polynomial 2047 (0x104C11DB7), with initial value 0, without pre-inversion and 2048 without post-inversion. 2050 5. Restrictions 2052 To ensure that fast multithreaded decoding is possible, starting with 2053 version 3 and if "frame_pixel_width * frame_pixel_height" is more 2054 than 101376, "slice_width * slice_height" MUST be less or equal to 2055 "num_h_slices * num_v_slices / 4". Note: 101376 is the frame size in 2056 "Pixels" of a 352x288 frame also known as CIF ("Common Intermediate 2057 Format") frame size format. 2059 For each "Frame", each position in the slice raster MUST be filled by 2060 one and only one slice of the "Frame" (no missing slice position, no 2061 slice overlapping). 2063 For each "Frame" with "keyframe" value of 0, each slice MUST have the 2064 same value of "slice_x", "slice_y", "slice_width", "slice_height" as 2065 a slice in the previous "Frame". 2067 6. Security Considerations 2069 Like any other codec, (such as [RFC6716]), FFV1 should not be used 2070 with insecure ciphers or cipher-modes that are vulnerable to known 2071 plaintext attacks. Some of the header bits as well as the padding 2072 are easily predictable. 2074 Implementations of the FFV1 codec need to take appropriate security 2075 considerations into account, as outlined in [RFC4732]. It is 2076 extremely important for the decoder to be robust against malicious 2077 payloads. Malicious payloads must not cause the decoder to overrun 2078 its allocated memory or to take an excessive amount of resources to 2079 decode. The same applies to the encoder, even though problems in 2080 encoders are typically rarer. Malicious video streams must not cause 2081 the encoder to misbehave because this would allow an attacker to 2082 attack transcoding gateways. A frequent security problem in image 2083 and video codecs is also to not check for integer overflows in 2084 "Pixel" count computations, that is to allocate width * height 2085 without considering that the multiplication result may have 2086 overflowed the arithmetic types range. The range coder could, if 2087 implemented naively, read one byte over the end. The implementation 2088 must ensure that no read outside allocated and initialized memory 2089 occurs. 2091 The reference implementation [REFIMPL] contains no known buffer 2092 overflow or cases where a specially crafted packet or video segment 2093 could cause a significant increase in CPU load. 2095 The reference implementation [REFIMPL] was validated in the following 2096 conditions: 2098 * Sending the decoder valid packets generated by the reference 2099 encoder and verifying that the decoder's output matches the 2100 encoder's input. 2102 * Sending the decoder packets generated by the reference encoder and 2103 then subjected to random corruption. 2105 * Sending the decoder random packets that are not FFV1. 2107 In all of the conditions above, the decoder and encoder was run 2108 inside the [VALGRIND] memory debugger as well as clangs address 2109 sanitizer [Address-Sanitizer], which track reads and writes to 2110 invalid memory regions as well as the use of uninitialized memory. 2111 There were no errors reported on any of the tested conditions. 2113 7. Media Type Definition 2115 This registration is done using the template defined in [RFC6838] and 2116 following [RFC4855]. 2118 Type name: video 2120 Subtype name: FFV1 2122 Required parameters: None. 2124 Optional parameters: These parameters are used to signal the 2125 capabilities of a receiver implementation. These parameters MUST NOT 2126 be used for any other purpose. 2128 * "version": The "version" of the FFV1 encoding as defined by 2129 Section 4.2.1. 2131 * "micro_version": The "micro_version" of the FFV1 encoding as 2132 defined by Section 4.2.2. 2134 * "coder_type": The "coder_type" of the FFV1 encoding as defined by 2135 Section 4.2.3. 2137 * "colorspace_type": The "colorspace_type" of the FFV1 encoding as 2138 defined by Section 4.2.5. 2140 * "bits_per_raw_sample": The "bits_per_raw_sample" of the FFV1 2141 encoding as defined by Section 4.2.7. 2143 * "max_slices": The value of "max_slices" is an integer indicating 2144 the maximum count of slices with a frames of the FFV1 encoding. 2146 Encoding considerations: This media type is defined for encapsulation 2147 in several audiovisual container formats and contains binary data; 2148 see Section 4.3.3. This media type is framed binary data; see 2149 Section 4.8 of [RFC6838]. 2151 Security considerations: See Section 6 of this document. 2153 Interoperability considerations: None. 2155 Published specification: RFC XXXX. 2157 [RFC Editor: Upon publication as an RFC, please replace "XXXX" with 2158 the number assigned to this document and remove this note.] 2159 Applications which use this media type: Any application that requires 2160 the transport of lossless video can use this media type. Some 2161 examples are, but not limited to screen recording, scientific 2162 imaging, and digital video preservation. 2164 Fragment identifier considerations: N/A. 2166 Additional information: None. 2168 Person & email address to contact for further information: Michael 2169 Niedermayer michael@niedermayer.cc (mailto:michael@niedermayer.cc) 2171 Intended usage: COMMON 2173 Restrictions on usage: None. 2175 Author: Dave Rice dave@dericed.com (mailto:dave@dericed.com) 2177 Change controller: IETF cellar working group delegated from the IESG. 2179 8. IANA Considerations 2181 The IANA is requested to register the following values: 2183 * Media type registration as described in Section 7. 2185 9. Changelog 2187 See https://github.com/FFmpeg/FFV1/commits/master 2188 (https://github.com/FFmpeg/FFV1/commits/master) 2190 [RFC Editor: Please remove this Changelog section prior to 2191 publication.] 2193 10. Normative References 2195 [RFC4732] Handley, M., Ed., Rescorla, E., Ed., and IAB, "Internet 2196 Denial-of-Service Considerations", RFC 4732, 2197 DOI 10.17487/RFC4732, December 2006, 2198 . 2200 [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type 2201 Specifications and Registration Procedures", BCP 13, 2202 RFC 6838, DOI 10.17487/RFC6838, January 2013, 2203 . 2205 [RFC4855] Casner, S., "Media Type Registration of RTP Payload 2206 Formats", RFC 4855, DOI 10.17487/RFC4855, February 2007, 2207 . 2209 [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 2210 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, 2211 May 2017, . 2213 [ISO.9899.1990] 2214 International Organization for Standardization, 2215 "Programming languages - C", 1990. 2217 [ISO.9899.2018] 2218 International Organization for Standardization, 2219 "Programming languages - C", 2018. 2221 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 2222 Requirement Levels", BCP 14, RFC 2119, 2223 DOI 10.17487/RFC2119, March 1997, 2224 . 2226 [Matroska] IETF, "Matroska", 2019, . 2229 [ISO.15444-1.2016] 2230 International Organization for Standardization, 2231 "Information technology -- JPEG 2000 image coding system: 2232 Core coding system", October 2016. 2234 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 2235 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716, 2236 September 2012, . 2238 11. Informative References 2240 [REFIMPL] Niedermayer, M., "The reference FFV1 implementation / the 2241 FFV1 codec in FFmpeg", undated, . 2243 [range-coding] 2244 Nigel, G. and N. Martin, "Range encoding: an algorithm for 2245 removing redundancy from a digitised message.", July 1979. 2247 [NUT] Niedermayer, M., "NUT Open Container Format", December 2248 2013, . 2250 [VALGRIND] Valgrind Developers, "Valgrind website", undated, 2251 . 2253 [ISO.14496-12.2015] 2254 International Organization for Standardization, 2255 "Information technology -- Coding of audio-visual objects 2256 -- Part 12: ISO base media file format", December 2015. 2258 [AVI] Microsoft, "AVI RIFF File Reference", undated, 2259 . 2262 [HuffYUV] Rudiak-Gould, B., "HuffYUV", December 2003, 2263 . 2266 [FFV1_V0] Niedermayer, M., "Commit to mark FFV1 version 0 as non- 2267 experimental", April 2006, . 2271 [ISO.14496-10.2014] 2272 International Organization for Standardization, 2273 "Information technology -- Coding of audio-visual objects 2274 -- Part 10: Advanced Video Coding", September 2014. 2276 [FFV1_V1] Niedermayer, M., "Commit to release FFV1 version 1", April 2277 2009, . 2280 [YCbCr] Wikipedia, "YCbCr", undated, 2281 . 2283 [ISO.14495-1.1999] 2284 International Organization for Standardization, 2285 "Information technology -- Lossless and near-lossless 2286 compression of continuous-tone still images: Baseline", 2287 December 1999. 2289 [Address-Sanitizer] 2290 The Clang Team, "ASAN AddressSanitizer website", undated, 2291 . 2293 [FFV1_V3] Niedermayer, M., "Commit to mark FFV1 version 3 as non- 2294 experimental", August 2013, . 2298 Appendix A. Multi-theaded decoder implementation suggestions 2300 This appendix is informative. 2302 The FFV1 bitstream is parsable in two ways: in sequential order as 2303 described in this document or with the pre-analysis of the footer of 2304 each slice. Each slice footer contains a "slice_size" field so the 2305 boundary of each slice is computable without having to parse the 2306 slice content. That allows multi-threading as well as independence 2307 of slice content (a bitstream error in a slice header or slice 2308 content has no impact on the decoding of the other slices). 2310 After having checked "keyframe" field, a decoder SHOULD parse 2311 "slice_size" fields, from "slice_size" of the last slice at the end 2312 of the "Frame" up to "slice_size" of the first slice at the beginning 2313 of the "Frame", before parsing slices, in order to have slices 2314 boundaries. A decoder MAY fallback on sequential order e.g. in case 2315 of a corrupted "Frame" (frame size unknown, "slice_size" of slices 2316 not coherent...) or if there is no possibility of seeking into the 2317 stream. 2319 Appendix B. Future handling of some streams created by non conforming 2320 encoders 2322 This appendix is informative. 2324 Some bitstreams were found with 40 extra bits corresponding to 2325 "error_status" and "slice_crc_parity" in the "reserved" bits of 2326 "Slice()". Any revision of this specification SHOULD care about 2327 avoiding to add 40 bits of content after "SliceContent" if "version" 2328 == 0 or "version" == 1. Else a decoder conforming to the revised 2329 specification could not distinguish between a revised bitstream and 2330 such buggy bitstream in the wild. 2332 Authors' Addresses 2334 Michael Niedermayer 2336 Email: michael@niedermayer.cc 2338 Dave Rice 2340 Email: dave@dericed.com 2342 Jerome Martinez 2343 Email: jerome@mediaarea.net