idnits 2.17.1 draft-ietf-cellar-ffv1-v4-09.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 (28 January 2020) is 1550 days in the past. Is this intentional? Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) ** Downref: Normative reference to an Informational RFC: RFC 4732 == Outdated reference: A later version (-20) exists of draft-ietf-cellar-ffv1-11 ** Downref: Normative reference to an Informational draft: draft-ietf-cellar-ffv1 (ref. 'I-D.ietf-cellar-ffv1') Summary: 2 errors (**), 0 flaws (~~), 2 warnings (==), 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: Standards Track D. Rice 5 Expires: 31 July 2020 6 J. Martinez 7 28 January 2020 9 FFV1 Video Coding Format Version 4 10 draft-ietf-cellar-ffv1-v4-09 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 31 July 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 . . . . . . . . . . . . . . . . . . 4 55 2.1. Definitions . . . . . . . . . . . . . . . . . . . . . . . 4 56 2.2. Conventions . . . . . . . . . . . . . . . . . . . . . . . 5 57 2.2.1. Pseudo-code . . . . . . . . . . . . . . . . . . . . . 5 58 2.2.2. Arithmetic Operators . . . . . . . . . . . . . . . . 5 59 2.2.3. Assignment Operators . . . . . . . . . . . . . . . . 6 60 2.2.4. Comparison Operators . . . . . . . . . . . . . . . . 6 61 2.2.5. Mathematical Functions . . . . . . . . . . . . . . . 7 62 2.2.6. Order of Operation Precedence . . . . . . . . . . . . 7 63 2.2.7. Range . . . . . . . . . . . . . . . . . . . . . . . . 8 64 2.2.8. NumBytes . . . . . . . . . . . . . . . . . . . . . . 8 65 2.2.9. Bitstream Functions . . . . . . . . . . . . . . . . . 8 66 3. Sample Coding . . . . . . . . . . . . . . . . . . . . . . . . 8 67 3.1. Border . . . . . . . . . . . . . . . . . . . . . . . . . 9 68 3.2. Samples . . . . . . . . . . . . . . . . . . . . . . . . . 9 69 3.3. Median Predictor . . . . . . . . . . . . . . . . . . . . 10 70 3.4. Context . . . . . . . . . . . . . . . . . . . . . . . . . 11 71 3.5. Quantization Table Sets . . . . . . . . . . . . . . . . . 11 72 3.6. Quantization Table Set Indexes . . . . . . . . . . . . . 11 73 3.7. Color spaces . . . . . . . . . . . . . . . . . . . . . . 12 74 3.7.1. YCbCr . . . . . . . . . . . . . . . . . . . . . . . . 12 75 3.7.2. RGB . . . . . . . . . . . . . . . . . . . . . . . . . 13 76 3.8. Coding of the Sample Difference . . . . . . . . . . . . . 14 77 3.8.1. Range Coding Mode . . . . . . . . . . . . . . . . . . 14 78 3.8.2. Golomb Rice Mode . . . . . . . . . . . . . . . . . . 19 79 4. Bitstream . . . . . . . . . . . . . . . . . . . . . . . . . . 24 80 4.1. Parameters . . . . . . . . . . . . . . . . . . . . . . . 25 81 4.1.1. version . . . . . . . . . . . . . . . . . . . . . . . 27 82 4.1.2. micro_version . . . . . . . . . . . . . . . . . . . . 27 83 4.1.3. coder_type . . . . . . . . . . . . . . . . . . . . . 28 84 4.1.4. state_transition_delta . . . . . . . . . . . . . . . 29 85 4.1.5. colorspace_type . . . . . . . . . . . . . . . . . . . 29 86 4.1.6. chroma_planes . . . . . . . . . . . . . . . . . . . . 30 87 4.1.7. bits_per_raw_sample . . . . . . . . . . . . . . . . . 30 88 4.1.8. log2_h_chroma_subsample . . . . . . . . . . . . . . . 30 89 4.1.9. log2_v_chroma_subsample . . . . . . . . . . . . . . . 31 90 4.1.10. extra_plane . . . . . . . . . . . . . . . . . . . . . 31 91 4.1.11. num_h_slices . . . . . . . . . . . . . . . . . . . . 31 92 4.1.12. num_v_slices . . . . . . . . . . . . . . . . . . . . 31 93 4.1.13. quant_table_set_count . . . . . . . . . . . . . . . . 31 94 4.1.14. states_coded . . . . . . . . . . . . . . . . . . . . 31 95 4.1.15. initial_state_delta . . . . . . . . . . . . . . . . . 32 96 4.1.16. ec . . . . . . . . . . . . . . . . . . . . . . . . . 32 97 4.1.17. intra . . . . . . . . . . . . . . . . . . . . . . . . 32 98 4.2. Configuration Record . . . . . . . . . . . . . . . . . . 33 99 4.2.1. reserved_for_future_use . . . . . . . . . . . . . . . 33 100 4.2.2. configuration_record_crc_parity . . . . . . . . . . . 33 101 4.2.3. Mapping FFV1 into Containers . . . . . . . . . . . . 34 102 4.3. Frame . . . . . . . . . . . . . . . . . . . . . . . . . . 35 103 4.4. Slice . . . . . . . . . . . . . . . . . . . . . . . . . . 36 104 4.5. Slice Header . . . . . . . . . . . . . . . . . . . . . . 37 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.5.10. reset_contexts . . . . . . . . . . . . . . . . . . . 40 115 4.5.11. slice_coding_mode . . . . . . . . . . . . . . . . . . 40 116 4.6. Slice Content . . . . . . . . . . . . . . . . . . . . . . 40 117 4.6.1. primary_color_count . . . . . . . . . . . . . . . . . 41 118 4.6.2. plane_pixel_height . . . . . . . . . . . . . . . . . 41 119 4.6.3. slice_pixel_height . . . . . . . . . . . . . . . . . 41 120 4.6.4. slice_pixel_y . . . . . . . . . . . . . . . . . . . . 41 121 4.7. Line . . . . . . . . . . . . . . . . . . . . . . . . . . 42 122 4.7.1. plane_pixel_width . . . . . . . . . . . . . . . . . . 42 123 4.7.2. slice_pixel_width . . . . . . . . . . . . . . . . . . 42 124 4.7.3. slice_pixel_x . . . . . . . . . . . . . . . . . . . . 42 125 4.7.4. sample_difference . . . . . . . . . . . . . . . . . . 43 126 4.8. Slice Footer . . . . . . . . . . . . . . . . . . . . . . 43 127 4.8.1. slice_size . . . . . . . . . . . . . . . . . . . . . 43 128 4.8.2. error_status . . . . . . . . . . . . . . . . . . . . 43 129 4.8.3. slice_crc_parity . . . . . . . . . . . . . . . . . . 44 130 4.9. Quantization Table Set . . . . . . . . . . . . . . . . . 44 131 4.9.1. quant_tables . . . . . . . . . . . . . . . . . . . . 45 132 4.9.2. context_count . . . . . . . . . . . . . . . . . . . . 45 133 5. Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 46 134 6. Security Considerations . . . . . . . . . . . . . . . . . . . 46 135 7. Media Type Definition . . . . . . . . . . . . . . . . . . . . 47 136 8. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 48 137 9. Appendixes . . . . . . . . . . . . . . . . . . . . . . . . . 49 138 9.1. Decoder implementation suggestions . . . . . . . . . . . 49 139 9.1.1. Multi-threading Support and Independence of Slices . 49 140 10. Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . 49 141 11. Normative References . . . . . . . . . . . . . . . . . . . . 49 142 12. Informative References . . . . . . . . . . . . . . . . . . . 50 143 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 51 145 1. Introduction 147 This document describes FFV1, a lossless video encoding format. The 148 design of FFV1 considers the storage of image characteristics, data 149 fixity, and the optimized use of encoding time and storage 150 requirements. FFV1 is designed to support a wide range of lossless 151 video applications such as long-term audiovisual preservation, 152 scientific imaging, screen recording, and other video encoding 153 scenarios that seek to avoid the generational loss of lossy video 154 encodings. 156 This document defines a version 4 of FFV1. Prior versions of FFV1 157 are defined within [I-D.ietf-cellar-ffv1]. 159 This document assumes familiarity with mathematical and coding 160 concepts such as Range coding [range-coding] and YCbCr color spaces 161 [YCbCr]. 163 2. Notation and Conventions 165 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 166 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and 167 "OPTIONAL" in this document are to be interpreted as described in BCP 168 14 [RFC2119] [RFC8174] when, and only when, they appear in all 169 capitals, as shown here. 171 2.1. Definitions 173 "Container": Format that encapsulates "Frames" (see Section 4.3) and 174 (when required) a "Configuration Record" into a bitstream. 176 "Sample": The smallest addressable representation of a color 177 component or a luma component in a "Frame". Examples of "Sample" are 178 Luma, Blue Chrominance, Red Chrominance, Transparency, Red, Green, 179 and Blue. 181 "Plane": A discrete component of a static image comprised of 182 "Samples" that represent a specific quantification of "Samples" of 183 that image. 185 "Pixel": The smallest addressable representation of a color in a 186 "Frame". It is composed of 1 or more "Samples". 188 "ESC": An ESCape symbol to indicate that the symbol to be stored is 189 too large for normal storage and that an alternate storage method is 190 used. 192 "MSB": Most Significant Bit, the bit that can cause the largest 193 change in magnitude of the symbol. 195 "RCT": Reversible Color Transform, a near linear, exactly reversible 196 integer transform that converts between RGB and YCbCr representations 197 of a "Pixel". 199 "VLC": Variable Length Code, a code that maps source symbols to a 200 variable number of bits. 202 "RGB": A reference to the method of storing the value of a "Pixel" by 203 using three numeric values that represent Red, Green, and Blue. 205 "YCbCr": A reference to the method of storing the value of a "Pixel" 206 by using three numeric values that represent the luma of the "Pixel" 207 (Y) and the chrominance of the "Pixel" (Cb and Cr). YCbCr word is 208 used for historical reasons and currently references any color space 209 relying on 1 luma "Sample" and 2 chrominance "Samples", e.g. YCbCr, 210 YCgCo or ICtCp. The exact meaning of the three numeric values is 211 unspecified. 213 "TBA": To Be Announced. Used in reference to the development of 214 future iterations of the FFV1 specification. 216 2.2. Conventions 218 2.2.1. Pseudo-code 220 The FFV1 bitstream is described in this document using pseudo-code. 221 Note that the pseudo-code is used for clarity in order to illustrate 222 the structure of FFV1 and not intended to specify any particular 223 implementation. The pseudo-code used is based upon the C programming 224 language [ISO.9899.1990] and uses its "if/else", "while" and "for" 225 keywords as well as functions defined within this document. 227 2.2.2. Arithmetic Operators 229 Note: the operators and the order of precedence are the same as used 230 in the C programming language [ISO.9899.2018]. 232 "a + b" means a plus b. 234 "a - b" means a minus b. 236 "-a" means negation of a. 238 "a * b" means a multiplied by b. 240 "a / b" means a divided by b. 242 "a ^ b" means a raised to the b-th power. 244 "a & b" means bit-wise "and" of a and b. 246 "a | b" means bit-wise "or" of a and b. 248 "a >> b" means arithmetic right shift of two's complement integer 249 representation of a by b binary digits. 251 "a << b" means arithmetic left shift of two's complement integer 252 representation of a by b binary digits. 254 2.2.3. Assignment Operators 256 "a = b" means a is assigned b. 258 "a++" is equivalent to a is assigned a + 1. 260 "a--" is equivalent to a is assigned a - 1. 262 "a += b" is equivalent to a is assigned a + b. 264 "a -= b" is equivalent to a is assigned a - b. 266 "a *= b" is equivalent to a is assigned a * b. 268 2.2.4. Comparison Operators 270 "a > b" means a is greater than b. 272 "a >= b" means a is greater than or equal to b. 274 "a < b" means a is less than b. 276 "a <= b" means a is less than or equal b. 278 "a == b" means a is equal to b. 280 "a != b" means a is not equal to b. 282 "a && b" means Boolean logical "and" of a and b. 284 "a || b" means Boolean logical "or" of a and b. 286 "!a" means Boolean logical "not" of a. 288 "a ? b : c" if a is true, then b, otherwise c. 290 2.2.5. Mathematical Functions 292 floor(a) the largest integer less than or equal to a 294 ceil(a) the smallest integer greater than or equal to a 296 sign(a) extracts the sign of a number, i.e. if a < 0 then -1, else if 297 a > 0 then 1, else 0 299 abs(a) the absolute value of a, i.e. abs(a) = sign(a)*a 301 log2(a) the base-two logarithm of a 303 min(a,b) the smallest of two values a and b 305 max(a,b) the largest of two values a and b 307 median(a,b,c) the numerical middle value in a data set of a, b, and 308 c, i.e. a+b+c-min(a,b,c)-max(a,b,c) 310 a_(b) the b-th value of a sequence of a 312 a~b,c. the 'b,c'-th value of a sequence of a 314 2.2.6. Order of Operation Precedence 316 When order of precedence is not indicated explicitly by use of 317 parentheses, operations are evaluated in the following order (from 318 top to bottom, operations of same precedence being evaluated from 319 left to right). This order of operations is based on the order of 320 operations used in Standard C. 322 a++, a-- 323 !a, -a 324 a ^ b 325 a * b, a / b, a % b 326 a + b, a - b 327 a << b, a >> b 328 a < b, a <= b, a > b, a >= b 329 a == b, a != b 330 a & b 331 a | b 332 a && b 333 a || b 334 a ? b : c 335 a = b, a += b, a -= b, a *= b 337 2.2.7. Range 339 "a...b" means any value starting from a to b, inclusive. 341 2.2.8. NumBytes 343 "NumBytes" is a non-negative integer that expresses the size in 8-bit 344 octets of a particular FFV1 "Configuration Record" or "Frame". FFV1 345 relies on its "Container" to store the "NumBytes" values; see 346 Section 4.2.3. 348 2.2.9. Bitstream Functions 350 2.2.9.1. remaining_bits_in_bitstream 352 "remaining_bits_in_bitstream( )" means the count of remaining bits 353 after the pointer in that "Configuration Record" or "Frame". It is 354 computed from the "NumBytes" value multiplied by 8 minus the count of 355 bits of that "Configuration Record" or "Frame" already read by the 356 bitstream parser. 358 2.2.9.2. remaining_symbols_in_syntax 360 "remaining_symbols_in_syntax( )" is true as long as the RangeCoder 361 has not consumed all the given input bytes. 363 2.2.9.3. byte_aligned 365 "byte_aligned( )" is true if "remaining_bits_in_bitstream( NumBytes 366 )" is a multiple of 8, otherwise false. 368 2.2.9.4. get_bits 370 "get_bits( i )" is the action to read the next "i" bits in the 371 bitstream, from most significant bit to least significant bit, and to 372 return the corresponding value. The pointer is increased by "i". 374 3. Sample Coding 376 For each "Slice" (as described in Section 4.4) of a "Frame", the 377 "Planes", "Lines", and "Samples" are coded in an order determined by 378 the "Color Space" (see Section 3.7). Each "Sample" is predicted by 379 the median predictor as described in Section 3.3 from other "Samples" 380 within the same "Plane" and the difference is stored using the method 381 described in Section 3.8. 383 3.1. Border 385 A border is assumed for each coded "Slice" for the purpose of the 386 median predictor and context according to the following rules: 388 * one column of "Samples" to the left of the coded slice is assumed 389 as identical to the "Samples" of the leftmost column of the coded 390 slice shifted down by one row. The value of the topmost "Sample" 391 of the column of "Samples" to the left of the coded slice is 392 assumed to be "0" 394 * one column of "Samples" to the right of the coded slice is assumed 395 as identical to the "Samples" of the rightmost column of the coded 396 slice 398 * an additional column of "Samples" to the left of the coded slice 399 and two rows of "Samples" above the coded slice are assumed to be 400 "0" 402 Figure 1 depicts a slice of 9 "Samples" "a,b,c,d,e,f,g,h,i" in a 3x3 403 arrangement along with its assumed border. 405 +---+---+---+---+---+---+---+---+ 406 | 0 | 0 | | 0 | 0 | 0 | | 0 | 407 +---+---+---+---+---+---+---+---+ 408 | 0 | 0 | | 0 | 0 | 0 | | 0 | 409 +---+---+---+---+---+---+---+---+ 410 | | | | | | | | | 411 +---+---+---+---+---+---+---+---+ 412 | 0 | 0 | | a | b | c | | c | 413 +---+---+---+---+---+---+---+---+ 414 | 0 | a | | d | e | f | | f | 415 +---+---+---+---+---+---+---+---+ 416 | 0 | d | | g | h | i | | i | 417 +---+---+---+---+---+---+---+---+ 419 Figure 1: A depiction of FFV1's assumed border for a set example 420 Samples. 422 3.2. Samples 424 Relative to any "Sample" "X", six other relatively positioned 425 "Samples" from the coded "Samples" and presumed border are identified 426 according to the labels used in Figure 2. The labels for these 427 relatively positioned "Samples" are used within the median predictor 428 and context. 430 +---+---+---+---+ 431 | | | T | | 432 +---+---+---+---+ 433 | |tl | t |tr | 434 +---+---+---+---+ 435 | L | l | X | | 436 +---+---+---+---+ 438 Figure 2: A depiction of how relatively positions Samples are 439 references within this document. 441 The labels for these relative "Samples" are made of the first letters 442 of the words Top, Left and Right. 444 3.3. Median Predictor 446 The prediction for any "Sample" value at position "X" may be computed 447 based upon the relative neighboring values of "l", "t", and "tl" via 448 this equation: 450 "median(l, t, l + t - tl)". 452 Note, this prediction template is also used in [ISO.14495-1.1999] and 453 [HuffYUV]. 455 Exception for the median predictor: if "colorspace_type == 0 && 456 bits_per_raw_sample == 16 && ( coder_type == 1 || coder_type == 2 )", 457 the following median predictor MUST be used: 459 "median(left16s, top16s, left16s + top16s - diag16s)" 461 where: 463 left16s = l >= 32768 ? ( l - 65536 ) : l 464 top16s = t >= 32768 ? ( t - 65536 ) : t 465 diag16s = tl >= 32768 ? ( tl - 65536 ) : tl 467 Background: a two's complement signed 16-bit signed integer was used 468 for storing "Sample" values in all known implementations of FFV1 469 bitstream. So in some circumstances, the most significant bit was 470 wrongly interpreted (used as a sign bit instead of the 16th bit of an 471 unsigned integer). Note that when the issue is discovered, the only 472 configuration of all known implementations being impacted is 16-bit 473 YCbCr with no Pixel transformation with Range Coder coder, as other 474 potentially impacted configurations (e.g. 15/16-bit JPEG2000-RCT with 475 Range Coder coder, or 16-bit content with Golomb Rice coder) were 476 implemented nowhere [ISO.15444-1.2016]. In the meanwhile, 16-bit 477 JPEG2000-RCT with Range Coder coder was implemented without this 478 issue in one implementation and validated by one conformance checker. 479 It is expected (to be confirmed) to remove this exception for the 480 median predictor in the next version of the FFV1 bitstream. 482 3.4. Context 484 Relative to any "Sample" "X", the Quantized Sample Differences "L-l", 485 "l-tl", "tl-t", "T-t", and "t-tr" are used as context: 487 context = Q_{0}[l - tl] + 488 Q_{1}[tl - t] + 489 Q_{2}[t - tr] + 490 Q_{3}[L - l] + 491 Q_{4}[T - t] 493 Figure 3 495 If "context >= 0" then "context" is used and the difference between 496 the "Sample" and its predicted value is encoded as is, else 497 "-context" is used and the difference between the "Sample" and its 498 predicted value is encoded with a flipped sign. 500 3.5. Quantization Table Sets 502 The FFV1 bitstream contains 1 or more Quantization Table Sets. Each 503 Quantization Table Set contains exactly 5 Quantization Tables with 504 each Quantization Table corresponding to 1 of the 5 Quantized Sample 505 Differences. For each Quantization Table, both the number of 506 quantization steps and their distribution are stored in the FFV1 507 bitstream; each Quantization Table has exactly 256 entries, and the 8 508 least significant bits of the Quantized Sample Difference are used as 509 index: 511 Q_{j}[k] = quant_tables[i][j][k&255] 513 Figure 4 515 In this formula, "i" is the Quantization Table Set index, "j" is the 516 Quantized Table index, "k" the Quantized Sample Difference. 518 3.6. Quantization Table Set Indexes 520 For each "Plane" of each slice, a Quantization Table Set is selected 521 from an index: 523 * For Y "Plane", "quant_table_set_index[ 0 ]" index is used 525 * For Cb and Cr "Planes", "quant_table_set_index[ 1 ]" index is used 526 * For extra "Plane", "quant_table_set_index[ (version <= 3 || 527 chroma_planes) ? 2 : 1 ]" index is used 529 Background: in first implementations of FFV1 bitstream, the index for 530 Cb and Cr "Planes" was stored even if it is not used (chroma_planes 531 set to 0), this index is kept for version <= 3 in order to keep 532 compatibility with FFV1 bitstreams in the wild. 534 3.7. Color spaces 536 FFV1 supports several color spaces. The count of allowed coded 537 planes and the meaning of the extra "Plane" are determined by the 538 selected color space. 540 The FFV1 bitstream interleaves data in an order determined by the 541 color space. In YCbCr for each "Plane", each "Line" is coded from 542 top to bottom and for each "Line", each "Sample" is coded from left 543 to right. In JPEG2000-RCT for each "Line" from top to bottom, each 544 "Plane" is coded and for each "Plane", each "Sample" is encoded from 545 left to right. 547 3.7.1. YCbCr 549 This color space allows 1 to 4 "Planes". 551 The Cb and Cr "Planes" are optional, but if used then MUST be used 552 together. Omitting the Cb and Cr "Planes" codes the frames in 553 grayscale without color data. 555 An optional transparency "Plane" can be used to code transparency 556 data. 558 An FFV1 "Frame" using YCbCr MUST use one of the following 559 arrangements: 561 * Y 563 * Y, Transparency 565 * Y, Cb, Cr 567 * Y, Cb, Cr, Transparency 569 The Y "Plane" MUST be coded first. If the Cb and Cr "Planes" are 570 used then they MUST be coded after the Y "Plane". If a transparency 571 "Plane" is used, then it MUST be coded last. 573 3.7.2. RGB 575 This color space allows 3 or 4 "Planes". 577 An optional transparency "Plane" can be used to code transparency 578 data. 580 JPEG2000-RCT is a Reversible Color Transform that codes RGB (red, 581 green, blue) "Planes" losslessly in a modified YCbCr color space 582 [ISO.15444-1.2016]. Reversible Pixel transformations between YCbCr 583 and RGB use the following formulae. 585 Cb=b-g 586 Cr=r-g 587 Y=g+(Cb+Cr)>>2 588 g=Y-(Cb+Cr)>>2 589 r=Cr+g 590 b=Cb+g 592 Figure 5 594 Exception for the JPEG2000-RCT conversion: if bits_per_raw_sample is 595 between 9 and 15 inclusive and extra_plane is 0, the following 596 formulae for reversible conversions between YCbCr and RGB MUST be 597 used instead of the ones above: 599 Cb=g-b 600 Cr=r-b 601 Y=b+(Cb+Cr)>>2 602 b=Y-(Cb+Cr)>>2 603 r=Cr+b 604 g=Cb+b 606 Figure 6 608 Background: At the time of this writing, in all known implementations 609 of FFV1 bitstream, when bits_per_raw_sample was between 9 and 15 610 inclusive and extra_plane is 0, GBR "Planes" were used as BGR 611 "Planes" during both encoding and decoding. In the meanwhile, 16-bit 612 JPEG2000-RCT was implemented without this issue in one implementation 613 and validated by one conformance checker. Methods to address this 614 exception for the transform are under consideration for the next 615 version of the FFV1 bitstream. 617 When FFV1 uses the JPEG2000-RCT, the horizontal "Lines" are 618 interleaved to improve caching efficiency since it is most likely 619 that the JPEG2000-RCT will immediately be converted to RGB during 620 decoding. The interleaved coding order is also Y, then Cb, then Cr, 621 and then if used transparency. 623 As an example, a "Frame" that is two "Pixels" wide and two "Pixels" 624 high, could comprise the following structure: 626 +------------------------+------------------------+ 627 | Pixel(1,1) | Pixel(2,1) | 628 | Y(1,1) Cb(1,1) Cr(1,1) | Y(2,1) Cb(2,1) Cr(2,1) | 629 +------------------------+------------------------+ 630 | Pixel(1,2) | Pixel(2,2) | 631 | Y(1,2) Cb(1,2) Cr(1,2) | Y(2,2) Cb(2,2) Cr(2,2) | 632 +------------------------+------------------------+ 634 In JPEG2000-RCT, the coding order would be left to right and then top 635 to bottom, with values interleaved by "Lines" and stored in this 636 order: 638 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) 639 Cb(2,2) Cr(1,2) Cr(2,2) 641 3.8. Coding of the Sample Difference 643 Instead of coding the n+1 bits of the Sample Difference with Huffman 644 or Range coding (or n+2 bits, in the case of JPEG2000-RCT), only the 645 n (or n+1, in the case of JPEG2000-RCT) least significant bits are 646 used, since this is sufficient to recover the original "Sample". In 647 the equation below, the term "bits" represents bits_per_raw_sample+1 648 for JPEG2000-RCT or bits_per_raw_sample otherwise: 650 coder_input = 651 [(sample_difference + 2^(bits-1)) & (2^bits - 1)] - 2^(bits-1) 653 Figure 7 655 3.8.1. Range Coding Mode 657 Early experimental versions of FFV1 used the CABAC Arithmetic coder 658 from H.264 as defined in [ISO.14496-10.2014] but due to the uncertain 659 patent/royalty situation, as well as its slightly worse performance, 660 CABAC was replaced by a Range coder based on an algorithm defined by 661 G. Nigel and N. Martin in 1979 [range-coding]. 663 3.8.1.1. Range Binary Values 665 To encode binary digits efficiently a Range coder is used. "C~i~" is 666 the i-th Context. "B~i~" is the i-th byte of the bytestream. "b~i~" 667 is the i-th Range coded binary value, "S~0,i~" is the i-th initial 668 state. The length of the bytestream encoding n binary symbols is 669 "j~n~" bytes. 671 r_{i} = floor( ( R_{i} * S_{i,C_{i}} ) / 2^8 ) 673 Figure 8 675 S_{i+1,C_{i}} = zero_state_{S_{i,C_{i}}} AND 676 l_i = L_i AND 677 t_i = R_i - r_i <== 678 b_i = 0 <==> 679 L_i < R_i - r_i 681 S_{i+1,C_{i}} = one_state_{S_{i,C_{i}}} AND 682 l_i = L_i - R_i + r_i AND 683 t_i = r_i <== 684 b_i = 1 <==> 685 L_i >= R_i - r_i 687 Figure 9 689 S_{i+1,k} = S_{i,k} <== C_i != k 691 Figure 10 693 R_{i+1} = 2^8 * t_{i} AND 694 L_{i+1} = 2^8 * l_{i} + B_{j_{i}} AND 695 j_{i+1} = j_{i} + 1 <== 696 t_{i} < 2^8 698 R_{i+1} = t_{i} AND 699 L_{i+1} = l_{i} AND 700 j_{i+1} = j_{i} <== 701 t_{i} >= 2^8 703 Figure 11 705 R_{0} = 65280 707 Figure 12 709 L_{0} = 2^8 * B_{0} + B_{1} 710 Figure 13 712 j_{0} = 2 714 Figure 14 716 3.8.1.1.1. Termination 718 The range coder can be used in 3 modes. 720 * In "Open mode" when decoding, every symbol the reader attempts to 721 read is available. In this mode arbitrary data can have been 722 appended without affecting the range coder output. This mode is 723 not used in FFV1. 725 * In "Closed mode" the length in bytes of the bytestream is provided 726 to the range decoder. Bytes beyond the length are read as 0 by 727 the range decoder. This is generally 1 byte shorter than the open 728 mode. 730 * In "Sentinel mode" the exact length in bytes is not known and thus 731 the range decoder MAY read into the data that follows the range 732 coded bytestream by one byte. In "Sentinel mode", the end of the 733 range coded bytestream is a binary symbol with state 129, which 734 value SHALL be discarded. After reading this symbol, the range 735 decoder will have read one byte beyond the end of the range coded 736 bytestream. This way the byte position of the end can be 737 determined. Bytestreams written in "Sentinel mode" can be read in 738 "Closed mode" if the length can be determined, in this case the 739 last (sentinel) symbol will be read non-corrupted and be of value 740 0. 742 Above describes the range decoding, encoding is defined as any 743 process which produces a decodable bytestream. 745 There are 3 places where range coder termination is needed in FFV1. 746 First is in the "Configuration Record", in this case the size of the 747 range coded bytestream is known and handled as "Closed mode". Second 748 is the switch from the "Slice Header" which is range coded to Golomb 749 coded slices as "Sentinel mode". Third is the end of range coded 750 Slices which need to terminate before the CRC at their end. This can 751 be handled as "Sentinel mode" or as "Closed mode" if the CRC position 752 has been determined. 754 3.8.1.2. Range Non Binary Values 756 To encode scalar integers, it would be possible to encode each bit 757 separately and use the past bits as context. However that would mean 758 255 contexts per 8-bit symbol that is not only a waste of memory but 759 also requires more past data to reach a reasonably good estimate of 760 the probabilities. Alternatively assuming a Laplacian distribution 761 and only dealing with its variance and mean (as in Huffman coding) 762 would also be possible, however, for maximum flexibility and 763 simplicity, the chosen method uses a single symbol to encode if a 764 number is 0, and if not, encodes the number using its exponent, 765 mantissa and sign. The exact contexts used are best described by 766 Figure 15, followed by some comments. 768 pseudo-code | type 769 --------------------------------------------------------------|----- 770 void put_symbol(RangeCoder *c, uint8_t *state, int v, int \ | 771 is_signed) { | 772 int i; | 773 put_rac(c, state+0, !v); | 774 if (v) { | 775 int a= abs(v); | 776 int e= log2(a); | 777 | 778 for (i = 0; i < e; i++) { | 779 put_rac(c, state+1+min(i,9), 1); //1..10 | 780 } | 781 | 782 put_rac(c, state+1+min(i,9), 0); | 783 for (i = e-1; i >= 0; i--) { | 784 put_rac(c, state+22+min(i,9), (a>>i)&1); //22..31 | 785 } | 786 | 787 if (is_signed) { | 788 put_rac(c, state+11 + min(e, 10), v < 0); //11..21| 789 } | 790 } | 791 } | 793 Figure 15: A pseudo-code description of the contexts of Range Non 794 Binary Values. 796 3.8.1.3. Initial Values for the Context Model 798 At keyframes all Range coder state variables are set to their initial 799 state. 801 3.8.1.4. State Transition Table 803 one_state_{i} = 804 default_state_transition_{i} + state_transition_delta_{i} 806 Figure 16 808 zero_state_{i} = 256 - one_state_{256-i} 810 Figure 17 812 3.8.1.5. default_state_transition 814 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 816 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 818 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 820 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 822 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 824 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99,100,101,102,103, 826 104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118, 828 119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,133, 830 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149, 832 150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164, 834 165,166,167,168,169,170,171,171,172,173,174,175,176,177,178,179, 836 180,181,182,183,184,185,186,187,188,189,190,190,191,192,194,194, 838 195,196,197,198,199,200,201,202,202,204,205,206,207,208,209,209, 840 210,211,212,213,215,215,216,217,218,219,220,220,222,223,224,225, 842 226,227,227,229,229,230,231,232,234,234,235,236,237,238,239,240, 844 241,242,243,244,245,246,247,248,248, 0, 0, 0, 0, 0, 0, 0, 846 3.8.1.6. Alternative State Transition Table 848 The alternative state transition table has been built using iterative 849 minimization of frame sizes and generally performs better than the 850 default. To use it, the coder_type (see Section 4.1.3) MUST be set 851 to 2 and the difference to the default MUST be stored in the 852 "Parameters", see Section 4.1. The reference implementation of FFV1 853 in FFmpeg uses Figure 18 by default at the time of this writing when 854 Range coding is used. 856 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49, 858 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39, 860 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52, 862 53, 74, 55, 57, 58, 58, 74, 60,101, 61, 62, 84, 66, 66, 68, 69, 864 87, 82, 71, 97, 73, 73, 82, 75,111, 77, 94, 78, 87, 81, 83, 97, 866 85, 83, 94, 86, 99, 89, 90, 99,111, 92, 93,134, 95, 98,105, 98, 868 105,110,102,108,102,118,103,106,106,113,109,112,114,112,116,125, 870 115,116,117,117,126,119,125,121,121,123,145,124,126,131,127,129, 872 165,130,132,138,133,135,145,136,137,139,146,141,143,142,144,148, 874 147,155,151,149,151,150,152,157,153,154,156,168,158,162,161,160, 876 172,163,169,164,166,184,167,170,177,174,171,173,182,176,180,178, 878 175,189,179,181,186,183,192,185,200,187,191,188,190,197,193,196, 880 197,194,195,196,198,202,199,201,210,203,207,204,205,206,208,214, 882 209,211,221,212,213,215,224,216,217,218,219,220,222,228,223,225, 884 226,224,227,229,240,230,231,232,233,234,235,236,238,239,237,242, 886 241,243,242,244,245,246,247,248,249,250,251,252,252,253,254,255, 888 Figure 18: Alternative state transition table for Range coding. 890 3.8.2. Golomb Rice Mode 892 The end of the bitstream of the "Frame" is filled with 0-bits until 893 that the bitstream contains a multiple of 8 bits. 895 3.8.2.1. Signed Golomb Rice Codes 897 This coding mode uses Golomb Rice codes. The VLC is split into 2 898 parts, the prefix stores the most significant bits and the suffix 899 stores the k least significant bits or stores the whole number in the 900 ESC case. 902 pseudo-code | type 903 --------------------------------------------------------------|----- 904 int get_ur_golomb(k) { | 905 for (prefix = 0; prefix < 12; prefix++) { | 906 if (get_bits(1)) { | 907 return get_bits(k) + (prefix << k) | 908 } | 909 } | 910 return get_bits(bits) + 11 | 911 } | 912 | 913 int get_sr_golomb(k) { | 914 v = get_ur_golomb(k); | 915 if (v & 1) return - (v >> 1) - 1; | 916 else return (v >> 1); | 917 } 919 3.8.2.1.1. Prefix 921 +----------------+-------+ 922 | bits | value | 923 +================+=======+ 924 | 1 | 0 | 925 +----------------+-------+ 926 | 01 | 1 | 927 +----------------+-------+ 928 | ... | ... | 929 +----------------+-------+ 930 | 0000 0000 0001 | 11 | 931 +----------------+-------+ 932 | 0000 0000 0000 | ESC | 933 +----------------+-------+ 935 Table 1 937 3.8.2.1.2. Suffix 939 +---------+--------------------------------------------------+ 940 +=========+==================================================+ 941 | non ESC | the k least significant bits MSB first | 942 +---------+--------------------------------------------------+ 943 | ESC | the value - 11, in MSB first order, ESC may only | 944 | | be used if the value cannot be coded as non ESC | 945 +---------+--------------------------------------------------+ 947 Table 2 949 3.8.2.1.3. Examples 951 +-----+-------------------------+-------+ 952 | k | bits | value | 953 +=====+=========================+=======+ 954 | 0 | "1" | 0 | 955 +-----+-------------------------+-------+ 956 | 0 | "001" | 2 | 957 +-----+-------------------------+-------+ 958 | 2 | "1 00" | 0 | 959 +-----+-------------------------+-------+ 960 | 2 | "1 10" | 2 | 961 +-----+-------------------------+-------+ 962 | 2 | "01 01" | 5 | 963 +-----+-------------------------+-------+ 964 | any | "000000000000 10000000" | 139 | 965 +-----+-------------------------+-------+ 967 Table 3 969 3.8.2.2. Run Mode 971 Run mode is entered when the context is 0 and left as soon as a non-0 972 difference is found. The level is identical to the predicted one. 973 The run and the first different level are coded. 975 3.8.2.2.1. Run Length Coding 977 The run value is encoded in 2 parts, the prefix part stores the more 978 significant part of the run as well as adjusting the run_index that 979 determines the number of bits in the less significant part of the 980 run. The 2nd part of the value stores the less significant part of 981 the run as it is. The run_index is reset for each "Plane" and slice 982 to 0. 984 pseudo-code | type 985 --------------------------------------------------------------|----- 986 log2_run[41]={ | 987 0, 0, 0, 0, 1, 1, 1, 1, | 988 2, 2, 2, 2, 3, 3, 3, 3, | 989 4, 4, 5, 5, 6, 6, 7, 7, | 990 8, 9,10,11,12,13,14,15, | 991 16,17,18,19,20,21,22,23, | 992 24, | 993 }; | 994 | 995 if (run_count == 0 && run_mode == 1) { | 996 if (get_bits(1)) { | 997 run_count = 1 << log2_run[run_index]; | 998 if (x + run_count <= w) { | 999 run_index++; | 1000 } | 1001 } else { | 1002 if (log2_run[run_index]) { | 1003 run_count = get_bits(log2_run[run_index]); | 1004 } else { | 1005 run_count = 0; | 1006 } | 1007 if (run_index) { | 1008 run_index--; | 1009 } | 1010 run_mode = 2; | 1011 } | 1012 } | 1014 The log2_run function is also used within [ISO.14495-1.1999]. 1016 3.8.2.2.2. Level Coding 1018 Level coding is identical to the normal difference coding with the 1019 exception that the 0 value is removed as it cannot occur: 1021 diff = get_vlc_symbol(context_state); 1022 if (diff >= 0) { 1023 diff++; 1024 } 1026 Note, this is different from JPEG-LS, which doesn't use prediction in 1027 run mode and uses a different encoding and context model for the last 1028 difference On a small set of test "Samples" the use of prediction 1029 slightly improved the compression rate. 1031 3.8.2.3. Scalar Mode 1033 Each difference is coded with the per context mean prediction removed 1034 and a per context value for k. 1036 get_vlc_symbol(state) { 1037 i = state->count; 1038 k = 0; 1039 while (i < state->error_sum) { 1040 k++; 1041 i += i; 1042 } 1044 v = get_sr_golomb(k); 1046 if (2 * state->drift < -state->count) { 1047 v = -1 - v; 1048 } 1050 ret = sign_extend(v + state->bias, bits); 1052 state->error_sum += abs(v); 1053 state->drift += v; 1055 if (state->count == 128) { 1056 state->count >>= 1; 1057 state->drift >>= 1; 1058 state->error_sum >>= 1; 1059 } 1060 state->count++; 1061 if (state->drift <= -state->count) { 1062 state->bias = max(state->bias - 1, -128); 1064 state->drift = max(state->drift + state->count, 1065 -state->count + 1); 1066 } else if (state->drift > 0) { 1067 state->bias = min(state->bias + 1, 127); 1069 state->drift = min(state->drift - state->count, 0); 1070 } 1072 return ret; 1073 } 1075 3.8.2.4. Initial Values for the VLC context state 1077 At keyframes all coder state variables are set to their initial 1078 state. 1080 drift = 0; 1081 error_sum = 4; 1082 bias = 0; 1083 count = 1; 1085 4. Bitstream 1087 An FFV1 bitstream is composed of a series of 1 or more "Frames" and 1088 (when required) a "Configuration Record". 1090 Within the following sub-sections, pseudo-code is used to explain the 1091 structure of each FFV1 bitstream component, as described in 1092 Section 2.2.1. Table 4 lists symbols used to annotate that pseudo- 1093 code in order to define the storage of the data referenced in that 1094 line of pseudo-code. 1096 +--------+----------------------------------------------+ 1097 | Symbol | Definition | 1098 +========+==============================================+ 1099 | u(n) | unsigned big endian integer using n bits | 1100 +--------+----------------------------------------------+ 1101 | sg | Golomb Rice coded signed scalar symbol coded | 1102 | | with the method described in Section 3.8.2 | 1103 +--------+----------------------------------------------+ 1104 | br | Range coded Boolean (1-bit) symbol with the | 1105 | | method described in Section 3.8.1.1 | 1106 +--------+----------------------------------------------+ 1107 | ur | Range coded unsigned scalar symbol coded | 1108 | | with the method described in Section 3.8.1.2 | 1109 +--------+----------------------------------------------+ 1110 | sr | Range coded signed scalar symbol coded with | 1111 | | the method described in Section 3.8.1.2 | 1112 +--------+----------------------------------------------+ 1114 Table 4: Definition of pseudo-code symbols for this 1115 document. 1117 The same context that is initialized to 128 is used for all fields in 1118 the header. 1120 The following MUST be provided by external means during 1121 initialization of the decoder: 1123 "frame_pixel_width" is defined as "Frame" width in "Pixels". 1125 "frame_pixel_height" is defined as "Frame" height in "Pixels". 1127 Default values at the decoder initialization phase: 1129 "ConfigurationRecordIsPresent" is set to 0. 1131 4.1. Parameters 1133 The "Parameters" section contains significant characteristics about 1134 the decoding configuration used for all instances of "Frame" (in FFV1 1135 version 0 and 1) or the whole FFV1 bitstream (other versions), 1136 including the stream version, color configuration, and quantization 1137 tables. Figure 19 describes the contents of the bitstream. 1139 pseudo-code | type 1140 --------------------------------------------------------------|----- 1141 Parameters( ) { | 1142 version | ur 1143 if (version >= 3) { | 1144 micro_version | ur 1145 } | 1146 coder_type | ur 1147 if (coder_type > 1) { | 1148 for (i = 1; i < 256; i++) { | 1149 state_transition_delta[ i ] | sr 1150 } | 1151 } | 1152 colorspace_type | ur 1153 if (version >= 1) { | 1154 bits_per_raw_sample | ur 1155 } | 1156 chroma_planes | br 1157 log2_h_chroma_subsample | ur 1158 log2_v_chroma_subsample | ur 1159 extra_plane | br 1160 if (version >= 3) { | 1161 num_h_slices - 1 | ur 1162 num_v_slices - 1 | ur 1163 quant_table_set_count | ur 1164 } | 1165 for (i = 0; i < quant_table_set_count; i++) { | 1166 QuantizationTableSet( i ) | 1167 } | 1168 if (version >= 3) { | 1169 for (i = 0; i < quant_table_set_count; i++) { | 1170 states_coded | br 1171 if (states_coded) { | 1172 for (j = 0; j < context_count[ i ]; j++) { | 1173 for (k = 0; k < CONTEXT_SIZE; k++) { | 1174 initial_state_delta[ i ][ j ][ k ] | sr 1175 } | 1176 } | 1177 } | 1178 } | 1179 ec | ur 1180 intra | ur 1181 } | 1182 } | 1184 Figure 19: A pseudo-code description of the bitstream contents. 1186 CONTEXT_SIZE is 32. 1188 4.1.1. version 1190 "version" specifies the version of the FFV1 bitstream. 1192 Each version is incompatible with other versions: decoders SHOULD 1193 reject a file due to an unknown version. 1195 Decoders SHOULD reject a file with version <= 1 && 1196 ConfigurationRecordIsPresent == 1. 1198 Decoders SHOULD reject a file with version >= 3 && 1199 ConfigurationRecordIsPresent == 0. 1201 +-------+-------------------------+ 1202 | value | version | 1203 +=======+=========================+ 1204 | 0 | FFV1 version 0 | 1205 +-------+-------------------------+ 1206 | 1 | FFV1 version 1 | 1207 +-------+-------------------------+ 1208 | 2 | reserved* | 1209 +-------+-------------------------+ 1210 | 3 | FFV1 version 3 | 1211 +-------+-------------------------+ 1212 | 4 | FFV1 version 4 | 1213 +-------+-------------------------+ 1214 | Other | reserved for future use | 1215 +-------+-------------------------+ 1217 Table 5 1219 * Version 2 was never enabled in the encoder thus version 2 files 1220 SHOULD NOT exist, and this document does not describe them to keep 1221 the text simpler. 1223 4.1.2. micro_version 1225 "micro_version" specifies the micro-version of the FFV1 bitstream. 1227 After a version is considered stable (a micro-version value is 1228 assigned to be the first stable variant of a specific version), each 1229 new micro-version after this first stable variant is compatible with 1230 the previous micro-version: decoders SHOULD NOT reject a file due to 1231 an unknown micro-version equal or above the micro-version considered 1232 as stable. 1234 Meaning of micro_version for version 3: 1236 +-------+-------------------------+ 1237 | value | micro_version | 1238 +=======+=========================+ 1239 | 0...3 | reserved* | 1240 +-------+-------------------------+ 1241 | 4 | first stable variant | 1242 +-------+-------------------------+ 1243 | Other | reserved for future use | 1244 +-------+-------------------------+ 1246 Table 6: The definitions for 1247 micro_version values. 1249 * development versions may be incompatible with the stable variants. 1251 Meaning of micro_version for version 4 (note: at the time of writing 1252 of this specification, version 4 is not considered stable so the 1253 first stable version value is to be announced in the future): 1255 +---------+-------------------------+ 1256 | value | micro_version | 1257 +=========+=========================+ 1258 | 0...TBA | reserved* | 1259 +---------+-------------------------+ 1260 | TBA | first stable variant | 1261 +---------+-------------------------+ 1262 | Other | reserved for future use | 1263 +---------+-------------------------+ 1265 Table 7: The definitions for 1266 micro_version values for FFV1 1267 version 4. 1269 * development versions which may be incompatible with the stable 1270 variants. 1272 4.1.3. coder_type 1274 "coder_type" specifies the coder used. 1276 +-------+-------------------------------------------------+ 1277 | value | coder used | 1278 +=======+=================================================+ 1279 | 0 | Golomb Rice | 1280 +-------+-------------------------------------------------+ 1281 | 1 | Range Coder with default state transition table | 1282 +-------+-------------------------------------------------+ 1283 | 2 | Range Coder with custom state transition table | 1284 +-------+-------------------------------------------------+ 1285 | Other | reserved for future use | 1286 +-------+-------------------------------------------------+ 1288 Table 8 1290 4.1.4. state_transition_delta 1292 "state_transition_delta" specifies the Range coder custom state 1293 transition table. 1295 If state_transition_delta is not present in the FFV1 bitstream, all 1296 Range coder custom state transition table elements are assumed to be 1297 0. 1299 4.1.5. colorspace_type 1301 "colorspace_type" specifies the color space encoded, the pixel 1302 transformation used by the encoder, the extra plane content, as well 1303 as interleave method. 1305 +-------+-------------+----------------+--------------+-------------+ 1306 | value | color space | pixel | extra plane | interleave | 1307 | | encoded | transformation | content | method | 1308 +=======+=============+================+==============+=============+ 1309 | 0 | YCbCr | None | Transparency | "Plane" | 1310 | | | | | then | 1311 | | | | | "Line" | 1312 +-------+-------------+----------------+--------------+-------------+ 1313 | 1 | RGB | JPEG2000-RCT | Transparency | "Line" | 1314 | | | | | then | 1315 | | | | | "Plane" | 1316 +-------+-------------+----------------+--------------+-------------+ 1317 | Other | reserved | reserved for | reserved for | reserved | 1318 | | for future | future use | future use | for future | 1319 | | use | | | use | 1320 +-------+-------------+----------------+--------------+-------------+ 1322 Table 9 1324 Restrictions: 1326 If "colorspace_type" is 1, then "chroma_planes" MUST be 1, 1327 "log2_h_chroma_subsample" MUST be 0, and "log2_v_chroma_subsample" 1328 MUST be 0. 1330 4.1.6. chroma_planes 1332 "chroma_planes" indicates if chroma (color) "Planes" are present. 1334 +-------+---------------------------------+ 1335 | value | presence | 1336 +=======+=================================+ 1337 | 0 | chroma "Planes" are not present | 1338 +-------+---------------------------------+ 1339 | 1 | chroma "Planes" are present | 1340 +-------+---------------------------------+ 1342 Table 10 1344 4.1.7. bits_per_raw_sample 1346 "bits_per_raw_sample" indicates the number of bits for each "Sample". 1347 Inferred to be 8 if not present. 1349 +-------+-----------------------------------+ 1350 | value | bits for each sample | 1351 +=======+===================================+ 1352 | 0 | reserved* | 1353 +-------+-----------------------------------+ 1354 | Other | the actual bits for each "Sample" | 1355 +-------+-----------------------------------+ 1357 Table 11 1359 * Encoders MUST NOT store bits_per_raw_sample = 0 Decoders SHOULD 1360 accept and interpret bits_per_raw_sample = 0 as 8. 1362 4.1.8. log2_h_chroma_subsample 1364 "log2_h_chroma_subsample" indicates the subsample factor, stored in 1365 powers to which the number 2 must be raised, between luma and chroma 1366 width ("chroma_width = 2^-log2_h_chroma_subsample^ * luma_width"). 1368 4.1.9. log2_v_chroma_subsample 1370 "log2_v_chroma_subsample" indicates the subsample factor, stored in 1371 powers to which the number 2 must be raised, between luma and chroma 1372 height ("chroma_height=2^-log2_v_chroma_subsample^ * luma_height"). 1374 4.1.10. extra_plane 1376 "extra_plane" indicates if an extra "Plane" is present. 1378 +-------+------------------------------+ 1379 | value | presence | 1380 +=======+==============================+ 1381 | 0 | extra "Plane" is not present | 1382 +-------+------------------------------+ 1383 | 1 | extra "Plane" is present | 1384 +-------+------------------------------+ 1386 Table 12 1388 4.1.11. num_h_slices 1390 "num_h_slices" indicates the number of horizontal elements of the 1391 slice raster. 1393 Inferred to be 1 if not present. 1395 4.1.12. num_v_slices 1397 "num_v_slices" indicates the number of vertical elements of the slice 1398 raster. 1400 Inferred to be 1 if not present. 1402 4.1.13. quant_table_set_count 1404 "quant_table_set_count" indicates the number of Quantization 1405 Table Sets. "quant_table_set_count" MUST be less than or equal to 8. 1407 Inferred to be 1 if not present. 1409 MUST NOT be 0. 1411 4.1.14. states_coded 1413 "states_coded" indicates if the respective Quantization Table Set has 1414 the initial states coded. 1416 Inferred to be 0 if not present. 1418 +-------+--------------------------------+ 1419 | value | initial states | 1420 +=======+================================+ 1421 | 0 | initial states are not present | 1422 | | and are assumed to be all 128 | 1423 +-------+--------------------------------+ 1424 | 1 | initial states are present | 1425 +-------+--------------------------------+ 1427 Table 13 1429 4.1.15. initial_state_delta 1431 "initial_state_delta[ i ][ j ][ k ]" indicates the initial Range 1432 coder state, it is encoded using "k" as context index and 1434 pred = j ? initial_states[ i ][j - 1][ k ] 1436 Figure 20 1438 initial_state[ i ][ j ][ k ] = 1439 ( pred + initial_state_delta[ i ][ j ][ k ] ) & 255 1441 Figure 21 1443 4.1.16. ec 1445 "ec" indicates the error detection/correction type. 1447 +-------+--------------------------------------------+ 1448 | value | error detection/correction type | 1449 +=======+============================================+ 1450 | 0 | 32-bit CRC on the global header | 1451 +-------+--------------------------------------------+ 1452 | 1 | 32-bit CRC per slice and the global header | 1453 +-------+--------------------------------------------+ 1454 | Other | reserved for future use | 1455 +-------+--------------------------------------------+ 1457 Table 14 1459 4.1.17. intra 1461 "intra" indicates the relationship between the instances of "Frame". 1463 Inferred to be 0 if not present. 1465 +-------+-------------------------------------+ 1466 | value | relationship | 1467 +=======+=====================================+ 1468 | 0 | Frames are independent or dependent | 1469 | | (keyframes and non keyframes) | 1470 +-------+-------------------------------------+ 1471 | 1 | Frames are independent (keyframes | 1472 | | only) | 1473 +-------+-------------------------------------+ 1474 | Other | reserved for future use | 1475 +-------+-------------------------------------+ 1477 Table 15 1479 4.2. Configuration Record 1481 In the case of a FFV1 bitstream with "version >= 3", a "Configuration 1482 Record" is stored in the underlying "Container", at the track header 1483 level. It contains the "Parameters" used for all instances of 1484 "Frame". The size of the "Configuration Record", "NumBytes", is 1485 supplied by the underlying "Container". 1487 pseudo-code | type 1488 -----------------------------------------------------------|----- 1489 ConfigurationRecord( NumBytes ) { | 1490 ConfigurationRecordIsPresent = 1 | 1491 Parameters( ) | 1492 while (remaining_symbols_in_syntax(NumBytes - 4)) { | 1493 reserved_for_future_use | br/ur/sr 1494 } | 1495 configuration_record_crc_parity | u(32) 1496 } | 1498 4.2.1. reserved_for_future_use 1500 "reserved_for_future_use" has semantics that are reserved for future 1501 use. 1503 Encoders conforming to this version of this specification SHALL NOT 1504 write this value. 1506 Decoders conforming to this version of this specification SHALL 1507 ignore its value. 1509 4.2.2. configuration_record_crc_parity 1511 "configuration_record_crc_parity" 32 bits that are chosen so that the 1512 "Configuration Record" as a whole has a crc remainder of 0. 1514 This is equivalent to storing the crc remainder in the 32-bit parity. 1516 The CRC generator polynomial used is the standard IEEE CRC polynomial 1517 (0x104C11DB7) with initial value 0. 1519 4.2.3. Mapping FFV1 into Containers 1521 This "Configuration Record" can be placed in any file format 1522 supporting "Configuration Records", fitting as much as possible with 1523 how the file format uses to store "Configuration Records". The 1524 "Configuration Record" storage place and "NumBytes" are currently 1525 defined and supported by this version of this specification for the 1526 following formats: 1528 4.2.3.1. AVI File Format 1530 The "Configuration Record" extends the stream format chunk ("AVI ", 1531 "hdlr", "strl", "strf") with the ConfigurationRecord bitstream. 1533 See [AVI] for more information about chunks. 1535 "NumBytes" is defined as the size, in bytes, of the strf chunk 1536 indicated in the chunk header minus the size of the stream format 1537 structure. 1539 4.2.3.2. ISO Base Media File Format 1541 The "Configuration Record" extends the sample description box 1542 ("moov", "trak", "mdia", "minf", "stbl", "stsd") with a "glbl" box 1543 that contains the ConfigurationRecord bitstream. See 1544 [ISO.14496-12.2015] for more information about boxes. 1546 "NumBytes" is defined as the size, in bytes, of the "glbl" box 1547 indicated in the box header minus the size of the box header. 1549 4.2.3.3. NUT File Format 1551 The codec_specific_data element (in "stream_header" packet) contains 1552 the ConfigurationRecord bitstream. See [NUT] for more information 1553 about elements. 1555 "NumBytes" is defined as the size, in bytes, of the 1556 codec_specific_data element as indicated in the "length" field of 1557 codec_specific_data 1559 4.2.3.4. Matroska File Format 1561 FFV1 SHOULD use "V_FFV1" as the Matroska "Codec ID". For FFV1 1562 versions 2 or less, the Matroska "CodecPrivate" Element SHOULD NOT be 1563 used. For FFV1 versions 3 or greater, the Matroska "CodecPrivate" 1564 Element MUST contain the FFV1 "Configuration Record" structure and no 1565 other data. See [Matroska] for more information about elements. 1567 "NumBytes" is defined as the "Element Data Size" of the 1568 "CodecPrivate" Element. 1570 4.3. Frame 1572 A "Frame" is an encoded representation of a complete static image. 1573 The whole "Frame" is provided by the underlaying container. 1575 A "Frame" consists of the keyframe field, "Parameters" (if version 1576 <=1), and a sequence of independent slices. The pseudo-code below 1577 describes the contents of a "Frame". 1579 pseudo-code | type 1580 --------------------------------------------------------------|----- 1581 Frame( NumBytes ) { | 1582 keyframe | br 1583 if (keyframe && !ConfigurationRecordIsPresent { | 1584 Parameters( ) | 1585 } | 1586 while (remaining_bits_in_bitstream( NumBytes )) { | 1587 Slice( ) | 1588 } | 1589 } | 1591 Architecture overview of slices in a "Frame": 1593 +-----------------------------------------------------------------+ 1594 +=================================================================+ 1595 | first slice header | 1596 +-----------------------------------------------------------------+ 1597 | first slice content | 1598 +-----------------------------------------------------------------+ 1599 | first slice footer | 1600 +-----------------------------------------------------------------+ 1601 | --------------------------------------------------------------- | 1602 +-----------------------------------------------------------------+ 1603 | second slice header | 1604 +-----------------------------------------------------------------+ 1605 | second slice content | 1606 +-----------------------------------------------------------------+ 1607 | second slice footer | 1608 +-----------------------------------------------------------------+ 1609 | --------------------------------------------------------------- | 1610 +-----------------------------------------------------------------+ 1611 | ... | 1612 +-----------------------------------------------------------------+ 1613 | --------------------------------------------------------------- | 1614 +-----------------------------------------------------------------+ 1615 | last slice header | 1616 +-----------------------------------------------------------------+ 1617 | last slice content | 1618 +-----------------------------------------------------------------+ 1619 | last slice footer | 1620 +-----------------------------------------------------------------+ 1622 Table 16 1624 4.4. Slice 1626 A "Slice" is an independent spatial sub-section of a "Frame" that is 1627 encoded separately from an other region of the same "Frame". The use 1628 of more than one "Slice" per "Frame" can be useful for taking 1629 advantage of the opportunities of multithreaded encoding and 1630 decoding. 1632 A "Slice" consists of a "Slice Header" (when relevant), a "Slice 1633 Content", and a "Slice Footer" (when relevant). The pseudo-code 1634 below describes the contents of a "Slice". 1636 pseudo-code | type 1637 --------------------------------------------------------------|----- 1638 Slice( ) { | 1639 if (version >= 3) { | 1640 SliceHeader( ) | 1641 } | 1642 SliceContent( ) | 1643 if (coder_type == 0) { | 1644 while (!byte_aligned()) { | 1645 padding | u(1) 1646 } | 1647 } | 1648 if (version <= 1) { | 1649 while (remaining_bits_in_bitstream( NumBytes ) != 0) {| 1650 reserved | u(1) 1651 } | 1652 } | 1653 if (version >= 3) { | 1654 SliceFooter( ) | 1655 } | 1656 } | 1658 "padding" specifies a bit without any significance and used only for 1659 byte alignment. MUST be 0. 1661 "reserved" specifies a bit without any significance in this revision 1662 of the specification and may have a significance in a later revision 1663 of this specification. 1665 Encoders SHOULD NOT fill these bits. 1667 Decoders SHOULD ignore these bits. 1669 Note in case these bits are used in a later revision of this 1670 specification: any revision of this specification SHOULD care about 1671 avoiding to add 40 bits of content after "SliceContent" for version 0 1672 and 1 of the bitstream. Background: Due to some non-conforming 1673 encoders, some bitstreams were found with 40 extra bits corresponding 1674 to "error_status" and "slice_crc_parity". As a result, a decoder 1675 conforming to the revised specification could not distinguish between 1676 a revised bitstream and a buggy bitstream. 1678 4.5. Slice Header 1680 A "Slice Header" provides information about the decoding 1681 configuration of the "Slice", such as its spatial position, size, and 1682 aspect ratio. The pseudo-code below describes the contents of the 1683 "Slice Header". 1685 pseudo-code | type 1686 --------------------------------------------------------------|----- 1687 SliceHeader( ) { | 1688 slice_x | ur 1689 slice_y | ur 1690 slice_width - 1 | ur 1691 slice_height - 1 | ur 1692 for (i = 0; i < quant_table_set_index_count; i++) { | 1693 quant_table_set_index[ i ] | ur 1694 } | 1695 picture_structure | ur 1696 sar_num | ur 1697 sar_den | ur 1698 if (version >= 4) { | 1699 reset_contexts | br 1700 slice_coding_mode | ur 1701 } | 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 "1 + ( ( chroma_planes || 1735 version <= 3 ) ? 1 : 0 ) + ( extra_plane ? 1 : 0 )". 1737 4.5.6. quant_table_set_index 1739 "quant_table_set_index" indicates the Quantization Table Set index to 1740 select the Quantization Table Set and the initial states for the 1741 slice. 1743 Inferred to be 0 if not present. 1745 4.5.7. picture_structure 1747 "picture_structure" specifies the temporal and spatial relationship 1748 of each "Line" of the "Frame". 1750 Inferred to be 0 if not present. 1752 +-------+-------------------------+ 1753 | value | picture structure used | 1754 +=======+=========================+ 1755 | 0 | unknown | 1756 +-------+-------------------------+ 1757 | 1 | top field first | 1758 +-------+-------------------------+ 1759 | 2 | bottom field first | 1760 +-------+-------------------------+ 1761 | 3 | progressive | 1762 +-------+-------------------------+ 1763 | Other | reserved for future use | 1764 +-------+-------------------------+ 1766 Table 17 1768 4.5.8. sar_num 1770 "sar_num" specifies the "Sample" aspect ratio numerator. 1772 Inferred to be 0 if not present. 1774 A value of 0 means that aspect ratio is unknown. 1776 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1778 If "sar_den" is 0, decoders SHOULD ignore the encoded value and 1779 consider that "sar_num" is 0. 1781 4.5.9. sar_den 1783 "sar_den" specifies the "Sample" aspect ratio denominator. 1785 Inferred to be 0 if not present. 1787 A value of 0 means that aspect ratio is unknown. 1789 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1791 If "sar_num" is 0, decoders SHOULD ignore the encoded value and 1792 consider that "sar_den" is 0. 1794 4.5.10. reset_contexts 1796 "reset_contexts" indicates if slice contexts must be reset. 1798 Inferred to be 0 if not present. 1800 4.5.11. slice_coding_mode 1802 "slice_coding_mode" indicates the slice coding mode. 1804 Inferred to be 0 if not present. 1806 +-------+-----------------------------+ 1807 | value | slice coding mode | 1808 +=======+=============================+ 1809 | 0 | Range Coding or Golomb Rice | 1810 +-------+-----------------------------+ 1811 | 1 | raw PCM | 1812 +-------+-----------------------------+ 1813 | Other | reserved for future use | 1814 +-------+-----------------------------+ 1816 Table 18 1818 4.6. Slice Content 1820 A "Slice Content" contains all "Line" elements part of the "Slice". 1822 Depending on the configuration, "Line" elements are ordered by 1823 "Plane" then by row (YCbCr) or by row then by "Plane" (RGB). 1825 pseudo-code | type 1826 --------------------------------------------------------------|----- 1827 SliceContent( ) { | 1828 if (colorspace_type == 0) { | 1829 for (p = 0; p < primary_color_count; p++) { | 1830 for (y = 0; y < plane_pixel_height[ p ]; y++) { | 1831 Line( p, y ) | 1832 } | 1833 } | 1834 } else if (colorspace_type == 1) { | 1835 for (y = 0; y < slice_pixel_height; y++) { | 1836 for (p = 0; p < primary_color_count; p++) { | 1837 Line( p, y ) | 1838 } | 1839 } | 1840 } | 1841 } | 1843 4.6.1. primary_color_count 1845 "primary_color_count" is defined as "1 + ( chroma_planes ? 2 : 0 ) + 1846 ( extra_plane ? 1 : 0 )". 1848 4.6.2. plane_pixel_height 1850 "plane_pixel_height[ p ]" is the height in pixels of plane p of the 1851 slice. 1853 "plane_pixel_height[ 0 ]" and "plane_pixel_height[ 1 + ( 1854 chroma_planes ? 2 : 0 ) ]" value is "slice_pixel_height". 1856 If "chroma_planes" is set to 1, "plane_pixel_height[ 1 ]" and 1857 "plane_pixel_height[ 2 ]" value is "ceil( slice_pixel_height / (1 << 1858 log2_v_chroma_subsample) )". 1860 4.6.3. slice_pixel_height 1862 "slice_pixel_height" is the height in pixels of the slice. 1864 Its value is "floor( ( slice_y + slice_height ) * slice_pixel_height 1865 / num_v_slices ) - slice_pixel_y". 1867 4.6.4. slice_pixel_y 1869 "slice_pixel_y" is the slice vertical position in pixels. 1871 Its value is "floor( slice_y * frame_pixel_height / num_v_slices )". 1873 4.7. Line 1875 A "Line" is a list of the sample differences (relative to the 1876 predictor) of primary color components. The pseudo-code below 1877 describes the contents of the "Line". 1879 pseudo-code | type 1880 --------------------------------------------------------------|----- 1881 Line( p, y ) { | 1882 if (colorspace_type == 0) { | 1883 for (x = 0; x < plane_pixel_width[ p ]; x++) { | 1884 sample_difference[ p ][ y ][ x ] | 1885 } | 1886 } else if (colorspace_type == 1) { | 1887 for (x = 0; x < slice_pixel_width; x++) { | 1888 sample_difference[ p ][ y ][ x ] | 1889 } | 1890 } | 1891 } | 1893 4.7.1. plane_pixel_width 1895 "plane_pixel_width[ p ]" is the width in "Pixels" of "Plane" p of the 1896 slice. 1898 "plane_pixel_width[ 0 ]" and "plane_pixel_width[ 1 + ( chroma_planes 1899 ? 2 : 0 ) ]" value is "slice_pixel_width". 1901 If "chroma_planes" is set to 1, "plane_pixel_width[ 1 ]" and 1902 "plane_pixel_width[ 2 ]" value is "ceil( slice_pixel_width / (1 << 1903 log2_h_chroma_subsample) )". 1905 4.7.2. slice_pixel_width 1907 "slice_pixel_width" is the width in "Pixels" of the slice. 1909 Its value is "floor( ( slice_x + slice_width ) * slice_pixel_width / 1910 num_h_slices ) - slice_pixel_x". 1912 4.7.3. slice_pixel_x 1914 "slice_pixel_x" is the slice horizontal position in "Pixels". 1916 Its value is "floor( slice_x * frame_pixel_width / num_h_slices )". 1918 4.7.4. sample_difference 1920 "sample_difference[ p ][ y ][ x ]" is the sample difference for 1921 "Sample" at "Plane" "p", y position "y", and x position "x". The 1922 "Sample" value is computed based on median predictor and context 1923 described in Section 3.2. 1925 4.8. Slice Footer 1927 A "Slice Footer" provides information about slice size and 1928 (optionally) parity. The pseudo-code below describes the contents of 1929 the "Slice Footer". 1931 Note: "Slice Footer" is always byte aligned. 1933 pseudo-code | type 1934 --------------------------------------------------------------|----- 1935 SliceFooter( ) { | 1936 slice_size | u(24) 1937 if (ec) { | 1938 error_status | u(8) 1939 slice_crc_parity | u(32) 1940 } | 1941 } | 1943 4.8.1. slice_size 1945 "slice_size" indicates the size of the slice in bytes. 1947 Note: this allows finding the start of slices before previous slices 1948 have been fully decoded, and allows parallel decoding as well as 1949 error resilience. 1951 4.8.2. error_status 1953 "error_status" specifies the error status. 1955 +-------+--------------------------------------+ 1956 | value | error status | 1957 +=======+======================================+ 1958 | 0 | no error | 1959 +-------+--------------------------------------+ 1960 | 1 | slice contains a correctable error | 1961 +-------+--------------------------------------+ 1962 | 2 | slice contains a uncorrectable error | 1963 +-------+--------------------------------------+ 1964 | Other | reserved for future use | 1965 +-------+--------------------------------------+ 1967 Table 19 1969 4.8.3. slice_crc_parity 1971 "slice_crc_parity" 32 bits that are chosen so that the slice as a 1972 whole has a crc remainder of 0. 1974 This is equivalent to storing the crc remainder in the 32-bit parity. 1976 The CRC generator polynomial used is the standard IEEE CRC polynomial 1977 (0x104C11DB7), with initial value 0, without pre-inversion and 1978 without post-inversion. 1980 4.9. Quantization Table Set 1982 The Quantization Table Sets are stored by storing the number of equal 1983 entries -1 of the first half of the table (represented as "len - 1" 1984 in the pseudo-code below) using the method described in 1985 Section 3.8.1.2. The second half doesn't need to be stored as it is 1986 identical to the first with flipped sign. "scale" and "len_count[ i 1987 ][ j ]" are temporary values used for the computing of 1988 "context_count[ i ]" and are not used outside Quantization Table Set 1989 pseudo-code. 1991 Example: 1993 Table: 0 0 1 1 1 1 2 2 -2 -2 -2 -1 -1 -1 -1 0 1995 Stored values: 1, 3, 1 1996 pseudo-code | type 1997 --------------------------------------------------------------|----- 1998 QuantizationTableSet( i ) { | 1999 scale = 1 | 2000 for (j = 0; j < MAX_CONTEXT_INPUTS; j++) { | 2001 QuantizationTable( i, j, scale ) | 2002 scale *= 2 * len_count[ i ][ j ] - 1 | 2003 } | 2004 context_count[ i ] = ceil( scale / 2 ) | 2005 } | 2007 MAX_CONTEXT_INPUTS is 5. 2009 pseudo-code | type 2010 --------------------------------------------------------------|----- 2011 QuantizationTable(i, j, scale) { | 2012 v = 0 | 2013 for (k = 0; k < 128;) { | 2014 len - 1 | ur 2015 for (a = 0; a < len; a++) { | 2016 quant_tables[ i ][ j ][ k ] = scale * v | 2017 k++ | 2018 } | 2019 v++ | 2020 } | 2021 for (k = 1; k < 128; k++) { | 2022 quant_tables[ i ][ j ][ 256 - k ] = \ | 2023 -quant_tables[ i ][ j ][ k ] | 2024 } | 2025 quant_tables[ i ][ j ][ 128 ] = \ | 2026 -quant_tables[ i ][ j ][ 127 ] | 2027 len_count[ i ][ j ] = v | 2028 } | 2030 4.9.1. quant_tables 2032 "quant_tables[ i ][ j ][ k ]" indicates the quantification table 2033 value of the Quantized Sample Difference "k" of the Quantization 2034 Table "j" of the Set Quantization Table Set "i". 2036 4.9.2. context_count 2038 "context_count[ i ]" indicates the count of contexts for Quantization 2039 Table Set "i". "context_count[ i ]" MUST be less than or equal to 2040 32768. 2042 5. Restrictions 2044 To ensure that fast multithreaded decoding is possible, starting with 2045 version 3 and if "frame_pixel_width * frame_pixel_height" is more 2046 than 101376, "slice_width * slice_height" MUST be less or equal to 2047 "num_h_slices * num_v_slices / 4". Note: 101376 is the frame size in 2048 "Pixels" of a 352x288 frame also known as CIF ("Common Intermediate 2049 Format") frame size format. 2051 For each "Frame", each position in the slice raster MUST be filled by 2052 one and only one slice of the "Frame" (no missing slice position, no 2053 slice overlapping). 2055 For each "Frame" with keyframe value of 0, each slice MUST have the 2056 same value of "slice_x, slice_y, slice_width, slice_height" as a 2057 slice in the previous "Frame", except if "reset_contexts" is 1. 2059 6. Security Considerations 2061 Like any other codec, (such as [RFC6716]), FFV1 should not be used 2062 with insecure ciphers or cipher-modes that are vulnerable to known 2063 plaintext attacks. Some of the header bits as well as the padding 2064 are easily predictable. 2066 Implementations of the FFV1 codec need to take appropriate security 2067 considerations into account, as outlined in [RFC4732]. It is 2068 extremely important for the decoder to be robust against malicious 2069 payloads. Malicious payloads must not cause the decoder to overrun 2070 its allocated memory or to take an excessive amount of resources to 2071 decode. The same applies to the encoder, even though problems in 2072 encoders are typically rarer. Malicious video streams must not cause 2073 the encoder to misbehave because this would allow an attacker to 2074 attack transcoding gateways. A frequent security problem in image 2075 and video codecs is also to not check for integer overflows in 2076 "Pixel" count computations, that is to allocate width * height 2077 without considering that the multiplication result may have 2078 overflowed the arithmetic types range. The range coder could, if 2079 implemented naively, read one byte over the end. The implementation 2080 must ensure that no read outside allocated and initialized memory 2081 occurs. 2083 The reference implementation [REFIMPL] contains no known buffer 2084 overflow or cases where a specially crafted packet or video segment 2085 could cause a significant increase in CPU load. 2087 The reference implementation [REFIMPL] was validated in the following 2088 conditions: 2090 * Sending the decoder valid packets generated by the reference 2091 encoder and verifying that the decoder's output matches the 2092 encoder's input. 2094 * Sending the decoder packets generated by the reference encoder and 2095 then subjected to random corruption. 2097 * Sending the decoder random packets that are not FFV1. 2099 In all of the conditions above, the decoder and encoder was run 2100 inside the [VALGRIND] memory debugger as well as clangs address 2101 sanitizer [Address-Sanitizer], which track reads and writes to 2102 invalid memory regions as well as the use of uninitialized memory. 2103 There were no errors reported on any of the tested conditions. 2105 7. Media Type Definition 2107 This registration is done using the template defined in [RFC6838] and 2108 following [RFC4855]. 2110 Type name: video 2112 Subtype name: FFV1 2114 Required parameters: None. 2116 Optional parameters: 2118 This parameter is used to signal the capabilities of a receiver 2119 implementation. This parameter MUST NOT be used for any other 2120 purpose. 2122 version: The version of the FFV1 encoding as defined by 2123 Section 4.1.1. 2125 micro_version: The micro_version of the FFV1 encoding as defined by 2126 Section 4.1.2. 2128 coder_type: The coder_type of the FFV1 encoding as defined by 2129 Section 4.1.3. 2131 colorspace_type: The colorspace_type of the FFV1 encoding as defined 2132 by Section 4.1.5. 2134 bits_per_raw_sample: The bits_per_raw_sample of the FFV1 encoding as 2135 defined by Section 4.1.7. 2137 max-slices: The value of max-slices is an integer indicating the 2138 maximum count of slices with a frames of the FFV1 encoding. 2140 Encoding considerations: 2142 This media type is defined for encapsulation in several audiovisual 2143 container formats and contains binary data; see Section 4.2.3. This 2144 media type is framed binary data Section 4.8 of [RFC6838]. 2146 Security considerations: 2148 See Section 6 of this document. 2150 Interoperability considerations: None. 2152 Published specification: 2154 [I-D.ietf-cellar-ffv1] and RFC XXXX. 2156 [RFC Editor: Upon publication as an RFC, please replace "XXXX" with 2157 the number assigned to this document and remove this note.] 2159 Applications which use this media type: 2161 Any application that requires the transport of lossless video can use 2162 this media type. Some examples are, but not limited to screen 2163 recording, scientific imaging, and digital video preservation. 2165 Fragment identifier considerations: N/A. 2167 Additional information: None. 2169 Person & email address to contact for further information: Michael 2170 Niedermayer michael@niedermayer.cc (mailto:michael@niedermayer.cc) 2172 Intended usage: COMMON 2174 Restrictions on usage: None. 2176 Author: Dave Rice dave@dericed.com (mailto:dave@dericed.com) 2178 Change controller: IETF cellar working group delegated from the IESG. 2180 8. IANA Considerations 2182 The IANA is requested to register the following values: 2184 * Media type registration as described in Section 7. 2186 9. Appendixes 2188 9.1. Decoder implementation suggestions 2190 9.1.1. Multi-threading Support and Independence of Slices 2192 The FFV1 bitstream is parsable in two ways: in sequential order as 2193 described in this document or with the pre-analysis of the footer of 2194 each slice. Each slice footer contains a slice_size field so the 2195 boundary of each slice is computable without having to parse the 2196 slice content. That allows multi-threading as well as independence 2197 of slice content (a bitstream error in a slice header or slice 2198 content has no impact on the decoding of the other slices). 2200 After having checked keyframe field, a decoder SHOULD parse 2201 slice_size fields, from slice_size of the last slice at the end of 2202 the "Frame" up to slice_size of the first slice at the beginning of 2203 the "Frame", before parsing slices, in order to have slices 2204 boundaries. A decoder MAY fallback on sequential order e.g. in case 2205 of a corrupted "Frame" (frame size unknown, slice_size of slices not 2206 coherent...) or if there is no possibility of seeking into the 2207 stream. 2209 10. Changelog 2211 See https://github.com/FFmpeg/FFV1/commits/master 2212 (https://github.com/FFmpeg/FFV1/commits/master) 2214 11. Normative References 2216 [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 2217 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, 2218 May 2017, . 2220 [Matroska] IETF, "Matroska", 2019, . 2223 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 2224 Requirement Levels", BCP 14, RFC 2119, 2225 DOI 10.17487/RFC2119, March 1997, 2226 . 2228 [RFC4732] Handley, M., Ed., Rescorla, E., Ed., and IAB, "Internet 2229 Denial-of-Service Considerations", RFC 4732, 2230 DOI 10.17487/RFC4732, December 2006, 2231 . 2233 [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type 2234 Specifications and Registration Procedures", BCP 13, 2235 RFC 6838, DOI 10.17487/RFC6838, January 2013, 2236 . 2238 [ISO.9899.2018] 2239 International Organization for Standardization, 2240 "Programming languages - C", 2018. 2242 [ISO.15444-1.2016] 2243 International Organization for Standardization, 2244 "Information technology -- JPEG 2000 image coding system: 2245 Core coding system", October 2016. 2247 [ISO.9899.1990] 2248 International Organization for Standardization, 2249 "Programming languages - C", 1990. 2251 [RFC4855] Casner, S., "Media Type Registration of RTP Payload 2252 Formats", RFC 4855, DOI 10.17487/RFC4855, February 2007, 2253 . 2255 [I-D.ietf-cellar-ffv1] 2256 Niedermayer, M., Rice, D., and J. Martinez, "FFV1 Video 2257 Coding Format Version 0, 1, and 3", Work in Progress, 2258 Internet-Draft, draft-ietf-cellar-ffv1-11, 23 October 2259 2019, 2260 . 2262 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 2263 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716, 2264 September 2012, . 2266 12. Informative References 2268 [ISO.14495-1.1999] 2269 International Organization for Standardization, 2270 "Information technology -- Lossless and near-lossless 2271 compression of continuous-tone still images: Baseline", 2272 December 1999. 2274 [AVI] Microsoft, "AVI RIFF File Reference", undated, 2275 . 2278 [ISO.14496-12.2015] 2279 International Organization for Standardization, 2280 "Information technology -- Coding of audio-visual objects 2281 -- Part 12: ISO base media file format", December 2015. 2283 [REFIMPL] Niedermayer, M., "The reference FFV1 implementation / the 2284 FFV1 codec in FFmpeg", undated, . 2286 [YCbCr] Wikipedia, "YCbCr", undated, 2287 . 2289 [HuffYUV] Rudiak-Gould, B., "HuffYUV", December 2003, 2290 . 2293 [VALGRIND] Valgrind Developers, "Valgrind website", undated, 2294 . 2296 [Address-Sanitizer] 2297 The Clang Team, "ASAN AddressSanitizer website", undated, 2298 . 2300 [NUT] Niedermayer, M., "NUT Open Container Format", December 2301 2013, . 2303 [range-coding] 2304 Nigel, G. and N. Martin, "Range encoding: an algorithm for 2305 removing redundancy from a digitised message.", July 1979. 2307 [ISO.14496-10.2014] 2308 International Organization for Standardization, 2309 "Information technology -- Coding of audio-visual objects 2310 -- Part 10: Advanced Video Coding", September 2014. 2312 Authors' Addresses 2314 Michael Niedermayer 2316 Email: michael@niedermayer.cc 2318 Dave Rice 2320 Email: dave@dericed.com 2322 Jerome Martinez 2324 Email: jerome@mediaarea.net