idnits 2.17.1 draft-ietf-cellar-ffv1-v4-05.txt: -(653): Line appears to be too long, but this could be caused by non-ascii characters in UTF-8 encoding 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: ---------------------------------------------------------------------------- == There are 6 instances of lines with non-ascii characters in the document. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** There are 14 instances of too long lines in the document, the longest one being 2491 characters in excess of 72. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (August 13, 2019) is 1718 days in the past. Is this intentional? Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) -- Looks like a reference, but probably isn't: '1' on line 641 -- Looks like a reference, but probably isn't: '2' on line 641 == Outdated reference: A later version (-20) exists of draft-ietf-cellar-ffv1-07 ** Downref: Normative reference to an Informational draft: draft-ietf-cellar-ffv1 (ref. 'I-D.ietf-cellar-ffv1') ** Downref: Normative reference to an Informational RFC: RFC 4732 Summary: 3 errors (**), 0 flaws (~~), 3 warnings (==), 3 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 cellar M. Niedermayer 3 Internet-Draft D. Rice 4 Intended status: Standards Track J. Martinez 5 Expires: February 14, 2020 August 13, 2019 7 FFV1 Video Coding Format Version 4 8 draft-ietf-cellar-ffv1-v4-05 10 Abstract 12 This document defines FFV1, a lossless intra-frame video encoding 13 format. FFV1 is designed to efficiently compress video data in a 14 variety of pixel formats. Compared to uncompressed video, FFV1 15 offers storage compression, frame fixity, and self-description, which 16 makes FFV1 useful as a preservation or intermediate video format. 18 Status of This Memo 20 This Internet-Draft is submitted in full conformance with the 21 provisions of BCP 78 and BCP 79. 23 Internet-Drafts are working documents of the Internet Engineering 24 Task Force (IETF). Note that other groups may also distribute 25 working documents as Internet-Drafts. The list of current Internet- 26 Drafts is at https://datatracker.ietf.org/drafts/current/. 28 Internet-Drafts are draft documents valid for a maximum of six months 29 and may be updated, replaced, or obsoleted by other documents at any 30 time. It is inappropriate to use Internet-Drafts as reference 31 material or to cite them other than as "work in progress." 33 This Internet-Draft will expire on February 14, 2020. 35 Copyright Notice 37 Copyright (c) 2019 IETF Trust and the persons identified as the 38 document authors. All rights reserved. 40 This document is subject to BCP 78 and the IETF Trust's Legal 41 Provisions Relating to IETF Documents (https://trustee.ietf.org/ 42 license-info) in effect on the date of publication of this document. 43 Please review these documents carefully, as they describe your rights 44 and restrictions with respect to this document. Code Components 45 extracted from this document must include Simplified BSD License text 46 as described in Section 4.e of the Trust Legal Provisions and are 47 provided without warranty as described in the Simplified BSD License. 49 Table of Contents 51 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 4 52 2. Notation and Conventions . . . . . . . . . . . . . . . . . . 4 53 2.1. Definitions . . . . . . . . . . . . . . . . . . . . . . . 4 54 2.2. Conventions . . . . . . . . . . . . . . . . . . . . . . . 5 55 2.2.1. Pseudo-code . . . . . . . . . . . . . . . . . . . . . 5 56 2.2.2. Arithmetic Operators . . . . . . . . . . . . . . . . 5 57 2.2.3. Assignment Operators . . . . . . . . . . . . . . . . 6 58 2.2.4. Comparison Operators . . . . . . . . . . . . . . . . 6 59 2.2.5. Mathematical Functions . . . . . . . . . . . . . . . 7 60 2.2.6. Order of Operation Precedence . . . . . . . . . . . . 7 61 2.2.7. Range . . . . . . . . . . . . . . . . . . . . . . . . 8 62 2.2.8. NumBytes . . . . . . . . . . . . . . . . . . . . . . 8 63 2.2.9. Bitstream Functions . . . . . . . . . . . . . . . . . 8 64 3. Sample Coding . . . . . . . . . . . . . . . . . . . . . . . . 9 65 3.1. Border . . . . . . . . . . . . . . . . . . . . . . . . . 9 66 3.2. Samples . . . . . . . . . . . . . . . . . . . . . . . . . 10 67 3.3. Median Predictor . . . . . . . . . . . . . . . . . . . . 10 68 3.4. Context . . . . . . . . . . . . . . . . . . . . . . . . . 11 69 3.5. Quantization Table Sets . . . . . . . . . . . . . . . . . 12 70 3.6. Quantization Table Set Indexes . . . . . . . . . . . . . 12 71 3.7. Color spaces . . . . . . . . . . . . . . . . . . . . . . 12 72 3.7.1. YCbCr . . . . . . . . . . . . . . . . . . . . . . . . 13 73 3.7.2. RGB . . . . . . . . . . . . . . . . . . . . . . . . . 13 74 3.8. Coding of the Sample Difference . . . . . . . . . . . . . 15 75 3.8.1. Range Coding Mode . . . . . . . . . . . . . . . . . . 15 76 3.8.2. Golomb Rice Mode . . . . . . . . . . . . . . . . . . 19 77 4. Bitstream . . . . . . . . . . . . . . . . . . . . . . . . . . 22 78 4.1. Parameters . . . . . . . . . . . . . . . . . . . . . . . 23 79 4.1.1. version . . . . . . . . . . . . . . . . . . . . . . . 24 80 4.1.2. micro_version . . . . . . . . . . . . . . . . . . . . 24 81 4.1.3. coder_type . . . . . . . . . . . . . . . . . . . . . 25 82 4.1.4. state_transition_delta . . . . . . . . . . . . . . . 26 83 4.1.5. colorspace_type . . . . . . . . . . . . . . . . . . . 26 84 4.1.6. chroma_planes . . . . . . . . . . . . . . . . . . . . 27 85 4.1.7. bits_per_raw_sample . . . . . . . . . . . . . . . . . 27 86 4.1.8. log2_h_chroma_subsample . . . . . . . . . . . . . . . 27 87 4.1.9. log2_v_chroma_subsample . . . . . . . . . . . . . . . 28 88 4.1.10. extra_plane . . . . . . . . . . . . . . . . . . . . . 28 89 4.1.11. num_h_slices . . . . . . . . . . . . . . . . . . . . 28 90 4.1.12. num_v_slices . . . . . . . . . . . . . . . . . . . . 28 91 4.1.13. quant_table_set_count . . . . . . . . . . . . . . . . 28 92 4.1.14. states_coded . . . . . . . . . . . . . . . . . . . . 28 93 4.1.15. initial_state_delta . . . . . . . . . . . . . . . . . 29 94 4.1.16. ec . . . . . . . . . . . . . . . . . . . . . . . . . 29 95 4.1.17. intra . . . . . . . . . . . . . . . . . . . . . . . . 29 96 4.2. Configuration Record . . . . . . . . . . . . . . . . . . 30 97 4.2.1. reserved_for_future_use . . . . . . . . . . . . . . . 30 98 4.2.2. configuration_record_crc_parity . . . . . . . . . . . 30 99 4.2.3. Mapping FFV1 into Containers . . . . . . . . . . . . 31 100 4.3. Frame . . . . . . . . . . . . . . . . . . . . . . . . . . 32 101 4.4. Slice . . . . . . . . . . . . . . . . . . . . . . . . . . 33 102 4.5. Slice Header . . . . . . . . . . . . . . . . . . . . . . 33 103 4.5.1. slice_x . . . . . . . . . . . . . . . . . . . . . . . 33 104 4.5.2. slice_y . . . . . . . . . . . . . . . . . . . . . . . 34 105 4.5.3. slice_width . . . . . . . . . . . . . . . . . . . . . 34 106 4.5.4. slice_height . . . . . . . . . . . . . . . . . . . . 34 107 4.5.5. quant_table_set_index_count . . . . . . . . . . . . . 34 108 4.5.6. quant_table_set_index . . . . . . . . . . . . . . . . 34 109 4.5.7. picture_structure . . . . . . . . . . . . . . . . . . 34 110 4.5.8. sar_num . . . . . . . . . . . . . . . . . . . . . . . 35 111 4.5.9. sar_den . . . . . . . . . . . . . . . . . . . . . . . 35 112 4.5.10. reset_contexts . . . . . . . . . . . . . . . . . . . 35 113 4.5.11. slice_coding_mode . . . . . . . . . . . . . . . . . . 36 114 4.6. Slice Content . . . . . . . . . . . . . . . . . . . . . . 36 115 4.6.1. primary_color_count . . . . . . . . . . . . . . . . . 36 116 4.6.2. plane_pixel_height . . . . . . . . . . . . . . . . . 36 117 4.6.3. slice_pixel_height . . . . . . . . . . . . . . . . . 36 118 4.6.4. slice_pixel_y . . . . . . . . . . . . . . . . . . . . 37 119 4.7. Line . . . . . . . . . . . . . . . . . . . . . . . . . . 37 120 4.7.1. plane_pixel_width . . . . . . . . . . . . . . . . . . 37 121 4.7.2. slice_pixel_width . . . . . . . . . . . . . . . . . . 37 122 4.7.3. slice_pixel_x . . . . . . . . . . . . . . . . . . . . 37 123 4.7.4. sample_difference . . . . . . . . . . . . . . . . . . 37 124 4.8. Slice Footer . . . . . . . . . . . . . . . . . . . . . . 38 125 4.8.1. slice_size . . . . . . . . . . . . . . . . . . . . . 38 126 4.8.2. error_status . . . . . . . . . . . . . . . . . . . . 38 127 4.8.3. slice_crc_parity . . . . . . . . . . . . . . . . . . 38 128 4.9. Quantization Table Set . . . . . . . . . . . . . . . . . 39 129 4.9.1. quant_tables . . . . . . . . . . . . . . . . . . . . 39 130 4.9.2. context_count . . . . . . . . . . . . . . . . . . . . 39 131 5. Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 39 132 6. Security Considerations . . . . . . . . . . . . . . . . . . . 40 133 7. Media Type Definition . . . . . . . . . . . . . . . . . . . . 41 134 8. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 42 135 9. Appendixes . . . . . . . . . . . . . . . . . . . . . . . . . 42 136 9.1. Decoder implementation suggestions . . . . . . . . . . . 42 137 9.1.1. Multi-threading Support and Independence of 138 Slices . . . . . . . . . . . . . . . . . . . . . . . 43 139 10. Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . 43 140 11. Normative References . . . . . . . . . . . . . . . . . . . . 43 141 12. Informative References . . . . . . . . . . . . . . . . . . . 44 142 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 45 144 1. Introduction 146 This document describes FFV1, a lossless video encoding format. The 147 design of FFV1 considers the storage of image characteristics, data 148 fixity, and the optimized use of encoding time and storage 149 requirements. FFV1 is designed to support a wide range of lossless 150 video applications such as long-term audiovisual preservation, 151 scientific imaging, screen recording, and other video encoding 152 scenarios that seek to avoid the generational loss of lossy video 153 encodings. 155 This document defines a version 4 of FFV1. Prior versions of FFV1 156 are defined within [I-D.ietf-cellar-ffv1]. 158 The latest version of this document is available at 159 https://raw.github.com/FFmpeg/FFV1/master/ffv1.md 160 (https://raw.github.com/FFmpeg/FFV1/master/ffv1.md) 162 This document assumes familiarity with mathematical and coding 163 concepts such as Range coding [range-coding] and YCbCr color spaces 164 [YCbCr]. 166 2. Notation and Conventions 168 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 169 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this 170 document are to be interpreted as described in [RFC2119]. 172 2.1. Definitions 174 "Container": Format that encapsulates "Frames" (see the section on 175 Frames (#frame)) and (when required) a "Configuration Record" into a 176 bitstream. 178 "Sample": The smallest addressable representation of a color 179 component or a luma component in a "Frame". Examples of "Sample" are 180 Luma, Blue Chrominance, Red Chrominance, Transparency, Red, Green, 181 and Blue. 183 "Plane": A discrete component of a static image comprised of 184 "Samples" that represent a specific quantification of "Samples" of 185 that image. 187 "Pixel": The smallest addressable representation of a color in a 188 "Frame". It is composed of 1 or more "Samples". 190 "ESC": An ESCape symbol to indicate that the symbol to be stored is 191 too large for normal storage and that an alternate storage method is 192 used. 194 "MSB": Most Significant Bit, the bit that can cause the largest 195 change in magnitude of the symbol. 197 "RCT": Reversible Color Transform, a near linear, exactly reversible 198 integer transform that converts between RGB and YCbCr representations 199 of a "Pixel". 201 "VLC": Variable Length Code, a code that maps source symbols to a 202 variable number of bits. 204 "RGB": A reference to the method of storing the value of a "Pixel" by 205 using three numeric values that represent Red, Green, and Blue. 207 "YCbCr": A reference to the method of storing the value of a "Pixel" 208 by using three numeric values that represent the luma of the "Pixel" 209 (Y) and the chrominance of the "Pixel" (Cb and Cr). YCbCr word is 210 used for historical reasons and currently references any color space 211 relying on 1 luma "Sample" and 2 chrominance "Samples", e.g. YCbCr, 212 YCgCo or ICtCp. The exact meaning of the three numeric values is 213 unspecified. 215 "TBA": To Be Announced. Used in reference to the development of 216 future iterations of the FFV1 specification. 218 2.2. Conventions 220 2.2.1. Pseudo-code 222 The FFV1 bitstream is described in this document using pseudo-code. 223 Note that the pseudo-code is used for clarity in order to illustrate 224 the structure of FFV1 and not intended to specify any particular 225 implementation. The pseudo-code used is based upon the C programming 226 language [ISO.9899.1990] and uses its "if/else", "while" and "for" 227 functions as well as functions defined within this document. 229 2.2.2. Arithmetic Operators 231 Note: the operators and the order of precedence are the same as used 232 in the C programming language [ISO.9899.1990]. 234 "a + b" means a plus b. 236 "a - b" means a minus b. 238 "-a" means negation of a. 240 "a * b" means a multiplied by b. 242 "a / b" means a divided by b. 244 "a ^ b" means a raised to the b-th power. 246 "a & b" means bit-wise "and" of a and b. 248 "a | b" means bit-wise "or" of a and b. 250 "a >> b" means arithmetic right shift of two's complement integer 251 representation of a by b binary digits. 253 "a << b" means arithmetic left shift of two's complement integer 254 representation of a by b binary digits. 256 2.2.3. Assignment Operators 258 "a = b" means a is assigned b. 260 "a++" is equivalent to a is assigned a + 1. 262 "a--" is equivalent to a is assigned a - 1. 264 "a += b" is equivalent to a is assigned a + b. 266 "a -= b" is equivalent to a is assigned a - b. 268 "a *= b" is equivalent to a is assigned a * b. 270 2.2.4. Comparison Operators 272 "a > b" means a is greater than b. 274 "a >= b" means a is greater than or equal to b. 276 "a < b" means a is less than b. 278 "a <= b" means a is less than or equal b. 280 "a == b" means a is equal to b. 282 "a != b" means a is not equal to b. 284 "a && b" means Boolean logical "and" of a and b. 286 "a || b" means Boolean logical "or" of a and b. 288 "!a" means Boolean logical "not" of a. 290 "a ? b : c" if a is true, then b, otherwise c. 292 2.2.5. Mathematical Functions 294 floor(a) the largest integer less than or equal to a 296 ceil(a) the smallest integer greater than or equal to a 298 sign(a) extracts the sign of a number, i.e. if a < 0 then -1, else if 299 a > 0 then 1, else 0 301 abs(a) the absolute value of a, i.e. abs(a) = sign(a)*a 303 log2(a) the base-two logarithm of a 305 min(a,b) the smallest of two values a and b 307 max(a,b) the largest of two values a and b 309 median(a,b,c) the numerical middle value in a data set of a, b, and 310 c, i.e. a+b+c-min(a,b,c)-max(a,b,c) 312 a_{b} the b-th value of a sequence of a 314 a_{b,c} the 'b,c'-th value of a sequence of a 316 2.2.6. Order of Operation Precedence 318 When order of precedence is not indicated explicitly by use of 319 parentheses, operations are evaluated in the following order (from 320 top to bottom, operations of same precedence being evaluated from 321 left to right). This order of operations is based on the order of 322 operations used in Standard C. 324 a++, a-- 325 !a, -a 326 a ^ b 327 a * b, a / b, a % b 328 a + b, a - b 329 a << b, a >> b 330 a < b, a <= b, a > b, a >= b 331 a == b, a != b 332 a & b 333 a | b 334 a && b 335 a || b 336 a ? b : c 337 a = b, a += b, a -= b, a *= b 339 2.2.7. Range 341 "a...b" means any value starting from a to b, inclusive. 343 2.2.8. NumBytes 345 "NumBytes" is a non-negative integer that expresses the size in 8-bit 346 octets of a particular FFV1 "Configuration Record" or "Frame". FFV1 347 relies on its "Container" to store the "NumBytes" values, see the 348 section on the Mapping FFV1 into Containers (#mapping-ffv1-into- 349 containers). 351 2.2.9. Bitstream Functions 353 2.2.9.1. remaining_bits_in_bitstream 355 "remaining_bits_in_bitstream( )" means the count of remaining bits 356 after the pointer in that "Configuration Record" or "Frame". It is 357 computed from the "NumBytes" value multiplied by 8 minus the count of 358 bits of that "Configuration Record" or "Frame" already read by the 359 bitstream parser. 361 2.2.9.2. remaining_symbols_in_syntax 363 "remaining_symbols_in_syntax( )" is true as long as the RangeCoder 364 has not consumed all the given input bytes. 366 2.2.9.3. byte_aligned 368 "byte_aligned( )" is true if "remaining_bits_in_bitstream( NumBytes 369 )" is a multiple of 8, otherwise false. 371 2.2.9.4. get_bits 373 "get_bits( i )" is the action to read the next "i" bits in the 374 bitstream, from most significant bit to least significant bit, and to 375 return the corresponding value. The pointer is increased by "i". 377 3. Sample Coding 379 For each "Slice" (as described in the section on Slices (#slice)) of 380 a "Frame", the "Planes", "Lines", and "Samples" are coded in an order 381 determined by the "Color Space" (see the section on Color Space 382 (#color-spaces)). Each "Sample" is predicted by the median predictor 383 as described in the section of the Median Predictor (#median- 384 predictor) from other "Samples" within the same "Plane" and the 385 difference is stored using the method described in Coding of the 386 Sample Difference (#coding-of-the-sample-difference). 388 3.1. Border 390 A border is assumed for each coded "Slice" for the purpose of the 391 median predictor and context according to the following rules: 393 * one column of "Samples" to the left of the coded slice is assumed 394 as identical to the "Samples" of the leftmost column of the coded 395 slice shifted down by one row. The value of the topmost "Sample" 396 of the column of "Samples" to the left of the coded slice is 397 assumed to be "0" 399 * one column of "Samples" to the right of the coded slice is assumed 400 as identical to the "Samples" of the rightmost column of the coded 401 slice 403 * an additional column of "Samples" to the left of the coded slice 404 and two rows of "Samples" above the coded slice are assumed to be 405 "0" 407 The following table depicts a slice of 9 "Samples" 408 "a,b,c,d,e,f,g,h,i" in a 3x3 arrangement along with its assumed 409 border. 411 +---+---+---+---+---+---+---+---+ 412 | 0 | 0 | | 0 | 0 | 0 | | 0 | 413 +---+---+---+---+---+---+---+---+ 414 | 0 | 0 | | 0 | 0 | 0 | | 0 | 415 +---+---+---+---+---+---+---+---+ 416 | | | | | | | | | 417 +---+---+---+---+---+---+---+---+ 418 | 0 | 0 | | a | b | c | | c | 419 +---+---+---+---+---+---+---+---+ 420 | 0 | a | | d | e | f | | f | 421 +---+---+---+---+---+---+---+---+ 422 | 0 | d | | g | h | i | | i | 423 +---+---+---+---+---+---+---+---+ 425 3.2. Samples 427 Relative to any "Sample" "X", six other relatively positioned 428 "Samples" from the coded "Samples" and presumed border are identified 429 according to the labels used in the following diagram. The labels 430 for these relatively positioned "Samples" are used within the median 431 predictor and context. 433 +---+---+---+---+ 434 | | | T | | 435 +---+---+---+---+ 436 | |tl | t |tr | 437 +---+---+---+---+ 438 | L | l | X | | 439 +---+---+---+---+ 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 If "context >= 0" then "context" is used and the difference between 494 the "Sample" and its predicted value is encoded as is, else 495 "-context" is used and the difference between the "Sample" and its 496 predicted value is encoded with a flipped sign. 498 3.5. Quantization Table Sets 500 The FFV1 bitstream contains 1 or more Quantization Table Sets. Each 501 Quantization Table Set contains exactly 5 Quantization Tables with 502 each Quantization Table corresponding to 1 of the 5 Quantized Sample 503 Differences. For each Quantization Table, both the number of 504 quantization steps and their distribution are stored in the FFV1 505 bitstream; each Quantization Table has exactly 256 entries, and the 8 506 least significant bits of the Quantized Sample Difference are used as 507 index: 509 Q_{j}[k] = quant_tables[i][j][k&255] 511 In this formula, "i" is the Quantization Table Set index, "j" is the 512 Quantized Table index, "k" the Quantized Sample Difference. 514 3.6. Quantization Table Set Indexes 516 For each "Plane" of each slice, a Quantization Table Set is selected 517 from an index: 519 * For Y "Plane", "quant_table_set_index[ 0 ]" index is used 521 * For Cb and Cr "Planes", "quant_table_set_index[ 1 ]" index is used 523 * For extra "Plane", "quant_table_set_index[ (version <= 3 || 524 chroma_planes) ? 2 : 1 ]" index is used 526 Background: in first implementations of FFV1 bitstream, the index for 527 Cb and Cr "Planes" was stored even if it is not used (chroma_planes 528 set to 0), this index is kept for version <= 3 in order to keep 529 compatibility with FFV1 bitstreams in the wild. 531 3.7. Color spaces 533 FFV1 supports several color spaces. The count of allowed coded 534 planes and the meaning of the extra "Plane" are determined by the 535 selected color space. 537 The FFV1 bitstream interleaves data in an order determined by the 538 color space. In YCbCr for each "Plane", each "Line" is coded from 539 top to bottom and for each "Line", each "Sample" is coded from left 540 to right. In JPEG2000-RCT for each "Line" from top to bottom, each 541 "Plane" is coded and for each "Plane", each "Sample" is encoded from 542 left to right. 544 3.7.1. YCbCr 546 This color space allows 1 to 4 "Planes". 548 The Cb and Cr "Planes" are optional, but if used then MUST be used 549 together. Omitting the Cb and Cr "Planes" codes the frames in 550 grayscale without color data. 552 An optional transparency "Plane" can be used to code transparency 553 data. 555 An FFV1 "Frame" using YCbCr MUST use one of the following 556 arrangements: 558 * Y 560 * Y, Transparency 562 * Y, Cb, Cr 564 * Y, Cb, Cr, Transparency 566 The Y "Plane" MUST be coded first. If the Cb and Cr "Planes" are 567 used then they MUST be coded after the Y "Plane". If a transparency 568 "Plane" is used, then it MUST be coded last. 570 3.7.2. RGB 572 This color space allows 3 or 4 "Planes". 574 An optional transparency "Plane" can be used to code transparency 575 data. 577 JPEG2000-RCT is a Reversible Color Transform that codes RGB (red, 578 green, blue) "Planes" losslessly in a modified YCbCr color space 579 [ISO.15444-1.2016]. Reversible Pixel transformations between YCbCr 580 and RGB use the following formulae. 582 Cb=b-g 584 Cr=r-g 586 Y=g+(Cb+Cr)>>2 588 g=Y-(Cb+Cr)>>2 590 r=Cr+g 591 b=Cb+g 593 Exception for the JPEG2000-RCT conversion: if bits_per_raw_sample is 594 between 9 and 15 inclusive and extra_plane is 0, the following 595 formulae for reversible conversions between YCbCr and RGB MUST be 596 used instead of the ones above: 598 Cb=g-b 600 Cr=r-b 602 Y=b+(Cb+Cr)>>2 604 b=Y-(Cb+Cr)>>2 606 r=Cr+b 608 g=Cb+b 610 Background: At the time of this writing, in all known implementations 611 of FFV1 bitstream, when bits_per_raw_sample was between 9 and 15 612 inclusive and extra_plane is 0, GBR "Planes" were used as BGR 613 "Planes" during both encoding and decoding. In the meanwhile, 16-bit 614 JPEG2000-RCT was implemented without this issue in one implementation 615 and validated by one conformance checker. Methods to address this 616 exception for the transform are under consideration for the next 617 version of the FFV1 bitstream. 619 When FFV1 uses the JPEG2000-RCT, the horizontal "Lines" are 620 interleaved to improve caching efficiency since it is most likely 621 that the JPEG2000-RCT will immediately be converted to RGB during 622 decoding. The interleaved coding order is also Y, then Cb, then Cr, 623 and then if used transparency. 625 As an example, a "Frame" that is two "Pixels" wide and two "Pixels" 626 high, could be comprised of the following structure: 628 +------------------------+------------------------+ 629 | Pixel[1,1] | Pixel[2,1] | 630 | Y[1,1] Cb[1,1] Cr[1,1] | Y[2,1] Cb[2,1] Cr[2,1] | 631 +------------------------+------------------------+ 632 | Pixel[1,2] | Pixel[2,2] | 633 | Y[1,2] Cb[1,2] Cr[1,2] | Y[2,2] Cb[2,2] Cr[2,2] | 634 +------------------------+------------------------+ 636 In JPEG2000-RCT, the coding order would be left to right and then top 637 to bottom, with values interleaved by "Lines" and stored in this 638 order: 640 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] 641 Cb[2,2] Cr[1,2] Cr[2,2] 643 3.8. Coding of the Sample Difference 645 Instead of coding the n+1 bits of the Sample Difference with Huffman 646 or Range coding (or n+2 bits, in the case of JPEG2000-RCT), only the 647 n (or n+1, in the case of JPEG2000-RCT) least significant bits are 648 used, since this is sufficient to recover the original "Sample". In 649 the equation below, the term "bits" represents bits_per_raw_sample+1 650 for JPEG2000-RCT or bits_per_raw_sample otherwise: 652 coder_input = 653 [(sample_difference + 2^(bits−1)) & (2^bits − 1)] − 2^(bits−1) 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}" 666 is the i-th Context. "B_{i}" is the i-th byte of the bytestream. 667 "b_{i}" is the i-th Range coded binary value, "S_{0,i}" is the i-th 668 initial state. The length of the bytestream encoding n binary 669 symbols is "j_{n}" bytes. 671 r_{i} = floor( ( R_{i} * S_{i,C_{i}} ) / 2^8 ) 673 S_{i+1,C_{i}} = zero_state_{S_{i,C_{i}}} XOR 674 l_i = L_i XOR 675 t_i = R_i - r_i <== 676 b_i = 0 <==> 677 L_i < R_i - r_i 679 S_{i+1,C_{i}} = one_state_{S_{i,C_{i}}} XOR 680 l_i = L_i - R_i + r_i XOR 681 t_i = r_i <== 682 b_i = 1 <==> 683 L_i >= R_i - r_i 685 S_{i+1,k} = S_{i,k} <== C_i != k 686 R_{i+1} = 2^8 * t_{i} XOR 687 L_{i+1} = 2^8 * l_{i} + B_{j_{i}} XOR 688 j_{i+1} = j_{i} + 1 <== 689 t_{i} < 2^8 691 R_{i+1} = t_{i} XOR 692 L_{i+1} = l_{i} XOR 693 j_{i+1} = j_{i} <== 694 t_{i} >= 2^8 696 R_{0} = 65280 698 L_{0} = 2^8 * B_{0} + B_{1} 700 j_{0} = 2 702 3.8.1.1.1. Termination 704 The range coder can be used in 3 modes. 706 * In "Open mode" when decoding, every symbol the reader attempts to 707 read is available. In this mode arbitrary data can have been 708 appended without affecting the range coder output. This mode is 709 not used in FFV1. 711 * In "Closed mode" the length in bytes of the bytestream is provided 712 to the range decoder. Bytes beyond the length are read as 0 by 713 the range decoder. This is generally 1 byte shorter than the open 714 mode. 716 * In "Sentinel mode" the exact length in bytes is not known and thus 717 the range decoder MAY read into the data that follows the range 718 coded bytestream by one byte. In "Sentinel mode", the end of the 719 range coded bytestream is a binary symbol with state 129, which 720 value SHALL be discarded. After reading this symbol, the range 721 decoder will have read one byte beyond the end of the range coded 722 bytestream. This way the byte position of the end can be 723 determined. Bytestreams written in "Sentinel mode" can be read in 724 "Closed mode" if the length can be determined, in this case the 725 last (sentinel) symbol will be read non-corrupted and be of value 726 0. 728 Above describes the range decoding, encoding is defined as any 729 process which produces a decodable bytestream. 731 There are 3 places where range coder termination is needed in FFV1. 732 First is in the "Configuration Record", in this case the size of the 733 range coded bytestream is known and handled as "Closed mode". Second 734 is the switch from the "Slice Header" which is range coded to Golomb 735 coded slices as "Sentinel mode". Third is the end of range coded 736 Slices which need to terminate before the CRC at their end. This can 737 be handled as "Sentinel mode" or as "Closed mode" if the CRC position 738 has been determined. 740 3.8.1.2. Range Non Binary Values 742 To encode scalar integers, it would be possible to encode each bit 743 separately and use the past bits as context. However that would mean 744 255 contexts per 8-bit symbol that is not only a waste of memory but 745 also requires more past data to reach a reasonably good estimate of 746 the probabilities. Alternatively assuming a Laplacian distribution 747 and only dealing with its variance and mean (as in Huffman coding) 748 would also be possible, however, for maximum flexibility and 749 simplicity, the chosen method uses a single symbol to encode if a 750 number is 0, and if not, encodes the number using its exponent, 751 mantissa and sign. The exact contexts used are best described by the 752 following code, followed by some comments. 754 pseudo-code | type --------------------------------------------------------------|----- void put_symbol(RangeCoder *c, uint8_t *state, int v, int \ | is_signed) { | int i; | put_rac(c, state+0, !v); | if (v) { | int a= abs(v); | int e= log2(a); | | for (i = 0; i < e; i++) { | put_rac(c, state+1+min(i,9), 1); //1..10 | } | | put_rac(c, state+1+min(i,9), 0); | for (i = e-1; i >= 0; i--) { | put_rac(c, state+22+min(i,9), (a>>i)&1); //22..31 | } | | if (is_signed) { | put_rac(c, state+11 + min(e, 10), v < 0); //11..21| } | } | } | 756 3.8.1.3. Initial Values for the Context Model 758 At keyframes all Range coder state variables are set to their initial 759 state. 761 3.8.1.4. State Transition Table 763 one_state_{i} = 764 default_state_transition_{i} + state_transition_delta_{i} 766 zero_state_{i} = 256 - one_state_{256-i} 768 3.8.1.5. default_state_transition 769 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 771 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 773 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 775 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 777 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 779 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99,100,101,102,103, 781 104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118, 783 119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,133, 785 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149, 787 150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164, 789 165,166,167,168,169,170,171,171,172,173,174,175,176,177,178,179, 791 180,181,182,183,184,185,186,187,188,189,190,190,191,192,194,194, 793 195,196,197,198,199,200,201,202,202,204,205,206,207,208,209,209, 795 210,211,212,213,215,215,216,217,218,219,220,220,222,223,224,225, 797 226,227,227,229,229,230,231,232,234,234,235,236,237,238,239,240, 799 241,242,243,244,245,246,247,248,248, 0, 0, 0, 0, 0, 0, 0, 801 3.8.1.6. Alternative State Transition Table 803 The alternative state transition table has been built using iterative 804 minimization of frame sizes and generally performs better than the 805 default. To use it, the coder_type (see the section on coder_type 806 (#codertype)) MUST be set to 2 and the difference to the default MUST 807 be stored in the "Parameters", see the section on Parameters 808 (#parameters). The reference implementation of FFV1 in FFmpeg uses 809 this table by default at the time of this writing when Range coding 810 is used. 812 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49, 814 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39, 816 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52, 818 53, 74, 55, 57, 58, 58, 74, 60,101, 61, 62, 84, 66, 66, 68, 69, 820 87, 82, 71, 97, 73, 73, 82, 75,111, 77, 94, 78, 87, 81, 83, 97, 822 85, 83, 94, 86, 99, 89, 90, 99,111, 92, 93,134, 95, 98,105, 98, 824 105,110,102,108,102,118,103,106,106,113,109,112,114,112,116,125, 826 115,116,117,117,126,119,125,121,121,123,145,124,126,131,127,129, 828 165,130,132,138,133,135,145,136,137,139,146,141,143,142,144,148, 830 147,155,151,149,151,150,152,157,153,154,156,168,158,162,161,160, 832 172,163,169,164,166,184,167,170,177,174,171,173,182,176,180,178, 834 175,189,179,181,186,183,192,185,200,187,191,188,190,197,193,196, 836 197,194,195,196,198,202,199,201,210,203,207,204,205,206,208,214, 838 209,211,221,212,213,215,224,216,217,218,219,220,222,228,223,225, 840 226,224,227,229,240,230,231,232,233,234,235,236,238,239,237,242, 842 241,243,242,244,245,246,247,248,249,250,251,252,252,253,254,255, 844 3.8.2. Golomb Rice Mode 846 The end of the bitstream of the "Frame" is filled with 0-bits until 847 that the bitstream contains a multiple of 8 bits. 849 3.8.2.1. Signed Golomb Rice Codes 851 This coding mode uses Golomb Rice codes. The VLC is split into 2 852 parts, the prefix stores the most significant bits and the suffix 853 stores the k least significant bits or stores the whole number in the 854 ESC case. 856 pseudo-code | type --------------------------------------------------------------|----- int get_ur_golomb(k) { | for (prefix = 0; prefix < 12; prefix++) { | if (get_bits(1)) { | return get_bits(k) + (prefix << k) | } | } | return get_bits(bits) + 11 | } | | int get_sr_golomb(k) { | v = get_ur_golomb(k); | if (v & 1) return - (v >> 1) - 1; | else return (v >> 1); | } 858 3.8.2.1.1. Prefix 860 +----------------+-------+ 861 | bits | value | 862 +================+=======+ 863 | 1 | 0 | 864 +----------------+-------+ 865 | 01 | 1 | 866 +----------------+-------+ 867 | ... | ... | 868 +----------------+-------+ 869 | 0000 0000 0001 | 11 | 870 +----------------+-------+ 871 | 0000 0000 0000 | ESC | 872 +----------------+-------+ 874 Table 1 876 3.8.2.1.2. Suffix 878 +---------+--------------------------------------------------+ 879 +=========+==================================================+ 880 | non ESC | the k least significant bits MSB first | 881 +---------+--------------------------------------------------+ 882 | ESC | the value - 11, in MSB first order, ESC may only | 883 | | be used if the value cannot be coded as non ESC | 884 +---------+--------------------------------------------------+ 886 Table 2 888 3.8.2.1.3. Examples 890 +-----+-------------------------+-------+ 891 | k | bits | value | 892 +=====+=========================+=======+ 893 | 0 | "1" | 0 | 894 +-----+-------------------------+-------+ 895 | 0 | "001" | 2 | 896 +-----+-------------------------+-------+ 897 | 2 | "1 00" | 0 | 898 +-----+-------------------------+-------+ 899 | 2 | "1 10" | 2 | 900 +-----+-------------------------+-------+ 901 | 2 | "01 01" | 5 | 902 +-----+-------------------------+-------+ 903 | any | "000000000000 10000000" | 139 | 904 +-----+-------------------------+-------+ 906 Table 3 908 3.8.2.2. Run Mode 910 Run mode is entered when the context is 0 and left as soon as a non-0 911 difference is found. The level is identical to the predicted one. 912 The run and the first different level are coded. 914 3.8.2.2.1. Run Length Coding 916 The run value is encoded in 2 parts, the prefix part stores the more 917 significant part of the run as well as adjusting the run_index that 918 determines the number of bits in the less significant part of the 919 run. The 2nd part of the value stores the less significant part of 920 the run as it is. The run_index is reset for each "Plane" and slice 921 to 0. 923 pseudo-code | type --------------------------------------------------------------|----- log2_run[41]={ | 0, 0, 0, 0, 1, 1, 1, 1, | 2, 2, 2, 2, 3, 3, 3, 3, | 4, 4, 5, 5, 6, 6, 7, 7, | 8, 9,10,11,12,13,14,15, | 16,17,18,19,20,21,22,23, | 24, | }; | | if (run_count == 0 && run_mode == 1) { | if (get_bits(1)) { | run_count = 1 << log2_run[run_index]; | if (x + run_count <= w) { | run_index++; | } | } else { | if (log2_run[run_index]) { | run_count = get_bits(log2_run[run_index]); | } else { | run_count = 0; | } | if (run_index) { | run_index--; | } | run_mode = 2; | } | } | 925 The log2_run function is also used within [ISO.14495-1.1999]. 927 3.8.2.2.2. Level Coding 929 Level coding is identical to the normal difference coding with the 930 exception that the 0 value is removed as it cannot occur: 932 diff = get_vlc_symbol(context_state); if (diff >= 0) { diff++; } 934 Note, this is different from JPEG-LS, which doesn't use prediction in 935 run mode and uses a different encoding and context model for the last 936 difference On a small set of test "Samples" the use of prediction 937 slightly improved the compression rate. 939 3.8.2.3. Scalar Mode 941 Each difference is coded with the per context mean prediction removed 942 and a per context value for k. 944 get_vlc_symbol(state) { i = state->count; k = 0; while (i < state->error_sum) { k++; i += i; } v = get_sr_golomb(k); if (2 * state->drift < -state->count) { v = -1 - v; } ret = sign_extend(v + state->bias, bits); state->error_sum += abs(v); state->drift += v; if (state->count == 128) { state->count >>= 1; state->drift >>= 1; state->error_sum >>= 1; } state->count++; if (state->drift <= -state->count) { state->bias = max(state->bias - 1, -128); state->drift = max(state->drift + state->count, -state->count + 1); } else if (state->drift > 0) { state->bias = min(state->bias + 1, 127); state->drift = min(state->drift - state->count, 0); } return ret; } 946 3.8.2.4. Initial Values for the VLC context state 948 At keyframes all coder state variables are set to their initial 949 state. 951 drift = 0; error_sum = 4; bias = 0; count = 1; 953 4. Bitstream 955 An FFV1 bitstream is composed of a series of 1 or more "Frames" and 956 (when required) a "Configuration Record". 958 Within the following sub-sections, pseudo-code is used to explain the 959 structure of each FFV1 bitstream component, as described in the 960 section on Pseudo-Code (#pseudocode). The following table lists 961 symbols used to annotate that pseudo-code in order to define the 962 storage of the data referenced in that line of pseudo-code. 964 +--------+-------------------------------------------+ 965 | Symbol | Definition | 966 +========+===========================================+ 967 | u(n) | unsigned big endian integer using n bits | 968 +--------+-------------------------------------------+ 969 | sg | Golomb Rice coded signed scalar symbol | 970 | | coded with the method described in Signed | 971 | | Golomb Rice Codes (#golomb-rice-mode) | 972 +--------+-------------------------------------------+ 973 | br | Range coded Boolean (1-bit) symbol with | 974 | | the method described in Range binary | 975 | | values (#range-binary-values) | 976 +--------+-------------------------------------------+ 977 | ur | Range coded unsigned scalar symbol coded | 978 | | with the method described in Range non | 979 | | binary values (#range-non-binary-values) | 980 +--------+-------------------------------------------+ 981 | sr | Range coded signed scalar symbol coded | 982 | | with the method described in Range non | 983 | | binary values (#range-non-binary-values) | 984 +--------+-------------------------------------------+ 986 Table 4 988 The same context that is initialized to 128 is used for all fields in 989 the header. 991 The following MUST be provided by external means during 992 initialization of the decoder: 994 "frame_pixel_width" is defined as "Frame" width in "Pixels". 996 "frame_pixel_height" is defined as "Frame" height in "Pixels". 998 Default values at the decoder initialization phase: 1000 "ConfigurationRecordIsPresent" is set to 0. 1002 4.1. Parameters 1004 The "Parameters" section contains significant characteristics about 1005 the decoding configuration used for all instances of "Frame" (in FFV1 1006 version 0 and 1) or the whole FFV1 bitstream (other versions), 1007 including the stream version, color configuration, and quantization 1008 tables. The pseudo-code below describes the contents of the 1009 bitstream. 1011 pseudo-code | type --------------------------------------------------------------|----- Parameters( ) { | version | ur if (version >= 3) { | micro_version | ur } | coder_type | ur if (coder_type > 1) { | for (i = 1; i < 256; i++) { | state_transition_delta[ i ] | sr } | } | colorspace_type | ur if (version >= 1) { | bits_per_raw_sample | ur } | chroma_planes | br log2_h_chroma_subsample | ur log2_v_chroma_subsample | ur extra_plane | br if (version >= 3) { | num_h_slices - 1 | ur num_v_slices - 1 | ur quant_table_set_count | ur } | for (i = 0; i < quant_table_set_count; i++) { | QuantizationTableSet( i ) | } | if (version >= 3) { | for (i = 0; i < quant_table_set_count; i++) { | states_coded | br if (states_coded) { | for (j = 0; j < context_count[ i ]; j++) { | for (k = 0; k < CONTEXT_SIZE; k++) { | initial_state_delta[ i ][ j ][ k ] | sr } | } | } | } | ec | ur intra | ur } | } | 1013 4.1.1. version 1015 "version" specifies the version of the FFV1 bitstream. 1017 Each version is incompatible with other versions: decoders SHOULD 1018 reject a file due to an unknown version. 1020 Decoders SHOULD reject a file with version <= 1 && 1021 ConfigurationRecordIsPresent == 1. 1023 Decoders SHOULD reject a file with version >= 3 && 1024 ConfigurationRecordIsPresent == 0. 1026 +-------+-------------------------+ 1027 | value | version | 1028 +=======+=========================+ 1029 | 0 | FFV1 version 0 | 1030 +-------+-------------------------+ 1031 | 1 | FFV1 version 1 | 1032 +-------+-------------------------+ 1033 | 2 | reserved* | 1034 +-------+-------------------------+ 1035 | 3 | FFV1 version 3 | 1036 +-------+-------------------------+ 1037 | 4 | FFV1 version 4 | 1038 +-------+-------------------------+ 1039 | Other | reserved for future use | 1040 +-------+-------------------------+ 1042 Table 5 1044 * Version 2 was never enabled in the encoder thus version 2 files 1045 SHOULD NOT exist, and this document does not describe them to keep 1046 the text simpler. 1048 4.1.2. micro_version 1050 "micro_version" specifies the micro-version of the FFV1 bitstream. 1052 After a version is considered stable (a micro-version value is 1053 assigned to be the first stable variant of a specific version), each 1054 new micro-version after this first stable variant is compatible with 1055 the previous micro-version: decoders SHOULD NOT reject a file due to 1056 an unknown micro-version equal or above the micro-version considered 1057 as stable. 1059 Meaning of micro_version for version 3: 1061 +-------+-------------------------+ 1062 | value | micro_version | 1063 +=======+=========================+ 1064 | 0...3 | reserved* | 1065 +-------+-------------------------+ 1066 | 4 | first stable variant | 1067 +-------+-------------------------+ 1068 | Other | reserved for future use | 1069 +-------+-------------------------+ 1071 Table 6 1073 * development versions may be incompatible with the stable variants. 1075 Meaning of micro_version for version 4 (note: at the time of writing 1076 of this specification, version 4 is not considered stable so the 1077 first stable version value is to be announced in the future): 1079 +---------+-------------------------+ 1080 | value | micro_version | 1081 +=========+=========================+ 1082 | 0...TBA | reserved* | 1083 +---------+-------------------------+ 1084 | TBA | first stable variant | 1085 +---------+-------------------------+ 1086 | Other | reserved for future use | 1087 +---------+-------------------------+ 1089 Table 7 1091 * development versions which may be incompatible with the stable 1092 variants. 1094 4.1.3. coder_type 1096 "coder_type" specifies the coder used. 1098 +-------+-------------------------------------------------+ 1099 | value | coder used | 1100 +=======+=================================================+ 1101 | 0 | Golomb Rice | 1102 +-------+-------------------------------------------------+ 1103 | 1 | Range Coder with default state transition table | 1104 +-------+-------------------------------------------------+ 1105 | 2 | Range Coder with custom state transition table | 1106 +-------+-------------------------------------------------+ 1107 | Other | reserved for future use | 1108 +-------+-------------------------------------------------+ 1110 Table 8 1112 4.1.4. state_transition_delta 1114 "state_transition_delta" specifies the Range coder custom state 1115 transition table. 1117 If state_transition_delta is not present in the FFV1 bitstream, all 1118 Range coder custom state transition table elements are assumed to be 1119 0. 1121 4.1.5. colorspace_type 1123 "colorspace_type" specifies the color space encoded, the pixel 1124 transformation used by the encoder, the extra plane content, as well 1125 as interleave method. 1127 +-------+-------------+----------------+--------------+-------------+ 1128 | value | color space | pixel | extra plane | interleave | 1129 | | encoded | transformation | content | method | 1130 +=======+=============+================+==============+=============+ 1131 | 0 | YCbCr | None | Transparency | "Plane" | 1132 | | | | | then | 1133 | | | | | "Line" | 1134 +-------+-------------+----------------+--------------+-------------+ 1135 | 1 | RGB | JPEG2000-RCT | Transparency | "Line" | 1136 | | | | | then | 1137 | | | | | "Plane" | 1138 +-------+-------------+----------------+--------------+-------------+ 1139 | Other | reserved | reserved for | reserved for | reserved | 1140 | | for future | future use | future use | for future | 1141 | | use | | | use | 1142 +-------+-------------+----------------+--------------+-------------+ 1144 Table 9 1146 Restrictions: 1148 If "colorspace_type" is 1, then "chroma_planes" MUST be 1, 1149 "log2_h_chroma_subsample" MUST be 0, and "log2_v_chroma_subsample" 1150 MUST be 0. 1152 4.1.6. chroma_planes 1154 "chroma_planes" indicates if chroma (color) "Planes" are present. 1156 +-------+---------------------------------+ 1157 | value | presence | 1158 +=======+=================================+ 1159 | 0 | chroma "Planes" are not present | 1160 +-------+---------------------------------+ 1161 | 1 | chroma "Planes" are present | 1162 +-------+---------------------------------+ 1164 Table 10 1166 4.1.7. bits_per_raw_sample 1168 "bits_per_raw_sample" indicates the number of bits for each "Sample". 1169 Inferred to be 8 if not present. 1171 +-------+-----------------------------------+ 1172 | value | bits for each sample | 1173 +=======+===================================+ 1174 | 0 | reserved* | 1175 +-------+-----------------------------------+ 1176 | Other | the actual bits for each "Sample" | 1177 +-------+-----------------------------------+ 1179 Table 11 1181 * Encoders MUST NOT store bits_per_raw_sample = 0 Decoders SHOULD 1182 accept and interpret bits_per_raw_sample = 0 as 8. 1184 4.1.8. log2_h_chroma_subsample 1186 "log2_h_chroma_subsample" indicates the subsample factor, stored in 1187 powers to which the number 2 must be raised, between luma and chroma 1188 width ("chroma_width = 2^(-log2_h_chroma_subsample) * luma_width"). 1190 4.1.9. log2_v_chroma_subsample 1192 "log2_v_chroma_subsample" indicates the subsample factor, stored in 1193 powers to which the number 2 must be raised, between luma and chroma 1194 height ("chroma_height=2^(-log2_v_chroma_subsample) * luma_height"). 1196 4.1.10. extra_plane 1198 "extra_plane" indicates if an extra "Plane" is present. 1200 +-------+------------------------------+ 1201 | value | presence | 1202 +=======+==============================+ 1203 | 0 | extra "Plane" is not present | 1204 +-------+------------------------------+ 1205 | 1 | extra "Plane" is present | 1206 +-------+------------------------------+ 1208 Table 12 1210 4.1.11. num_h_slices 1212 "num_h_slices" indicates the number of horizontal elements of the 1213 slice raster. 1215 Inferred to be 1 if not present. 1217 4.1.12. num_v_slices 1219 "num_v_slices" indicates the number of vertical elements of the slice 1220 raster. 1222 Inferred to be 1 if not present. 1224 4.1.13. quant_table_set_count 1226 "quant_table_set_count" indicates the number of Quantization 1227 Table Sets. 1229 Inferred to be 1 if not present. 1231 MUST NOT be 0. 1233 4.1.14. states_coded 1235 "states_coded" indicates if the respective Quantization Table Set has 1236 the initial states coded. 1238 Inferred to be 0 if not present. 1240 +-------+--------------------------------+ 1241 | value | initial states | 1242 +=======+================================+ 1243 | 0 | initial states are not present | 1244 | | and are assumed to be all 128 | 1245 +-------+--------------------------------+ 1246 | 1 | initial states are present | 1247 +-------+--------------------------------+ 1249 Table 13 1251 4.1.15. initial_state_delta 1253 "initial_state_delta[ i ][ j ][ k ]" indicates the initial Range 1254 coder state, it is encoded using "k" as context index and 1256 pred = j ? initial_states[ i ][j - 1][ k ] : 128 1258 initial_state[ i ][ j ][ k ] = 1259 ( pred + initial_state_delta[ i ][ j ][ k ] ) & 255 1261 4.1.16. ec 1263 "ec" indicates the error detection/correction type. 1265 +-------+--------------------------------------------+ 1266 | value | error detection/correction type | 1267 +=======+============================================+ 1268 | 0 | 32-bit CRC on the global header | 1269 +-------+--------------------------------------------+ 1270 | 1 | 32-bit CRC per slice and the global header | 1271 +-------+--------------------------------------------+ 1272 | Other | reserved for future use | 1273 +-------+--------------------------------------------+ 1275 Table 14 1277 4.1.17. intra 1279 "intra" indicates the relationship between the instances of "Frame". 1281 Inferred to be 0 if not present. 1283 +-------+-------------------------------------+ 1284 | value | relationship | 1285 +=======+=====================================+ 1286 | 0 | Frames are independent or dependent | 1287 | | (keyframes and non keyframes) | 1288 +-------+-------------------------------------+ 1289 | 1 | Frames are independent (keyframes | 1290 | | only) | 1291 +-------+-------------------------------------+ 1292 | Other | reserved for future use | 1293 +-------+-------------------------------------+ 1295 Table 15 1297 4.2. Configuration Record 1299 In the case of a FFV1 bitstream with "version >= 3", a "Configuration 1300 Record" is stored in the underlying "Container", at the track header 1301 level. It contains the "Parameters" used for all instances of 1302 "Frame". The size of the "Configuration Record", "NumBytes", is 1303 supplied by the underlying "Container". 1305 pseudo-code | type --------------------------------------------------------------|----- ConfigurationRecord( NumBytes ) { | ConfigurationRecordIsPresent = 1 | Parameters( ) | while (remaining_symbols_in_syntax(NumBytes - 4)) { | reserved_for_future_use | br/ur/sr } | configuration_record_crc_parity | u(32) } | 1307 4.2.1. reserved_for_future_use 1309 "reserved_for_future_use" has semantics that are reserved for future 1310 use. 1312 Encoders conforming to this version of this specification SHALL NOT 1313 write this value. 1315 Decoders conforming to this version of this specification SHALL 1316 ignore its value. 1318 4.2.2. configuration_record_crc_parity 1320 "configuration_record_crc_parity" 32 bits that are chosen so that the 1321 "Configuration Record" as a whole has a crc remainder of 0. 1323 This is equivalent to storing the crc remainder in the 32-bit parity. 1325 The CRC generator polynomial used is the standard IEEE CRC polynomial 1326 (0x104C11DB7) with initial value 0. 1328 4.2.3. Mapping FFV1 into Containers 1330 This "Configuration Record" can be placed in any file format 1331 supporting "Configuration Records", fitting as much as possible with 1332 how the file format uses to store "Configuration Records". The 1333 "Configuration Record" storage place and "NumBytes" are currently 1334 defined and supported by this version of this specification for the 1335 following formats: 1337 4.2.3.1. AVI File Format 1339 The "Configuration Record" extends the stream format chunk ("AVI ", 1340 "hdlr", "strl", "strf") with the ConfigurationRecord bitstream. 1342 See [AVI] for more information about chunks. 1344 "NumBytes" is defined as the size, in bytes, of the strf chunk 1345 indicated in the chunk header minus the size of the stream format 1346 structure. 1348 4.2.3.2. ISO Base Media File Format 1350 The "Configuration Record" extends the sample description box 1351 ("moov", "trak", "mdia", "minf", "stbl", "stsd") with a "glbl" box 1352 that contains the ConfigurationRecord bitstream. See 1353 [ISO.14496-12.2015] for more information about boxes. 1355 "NumBytes" is defined as the size, in bytes, of the "glbl" box 1356 indicated in the box header minus the size of the box header. 1358 4.2.3.3. NUT File Format 1360 The codec_specific_data element (in "stream_header" packet) contains 1361 the ConfigurationRecord bitstream. See [NUT] for more information 1362 about elements. 1364 "NumBytes" is defined as the size, in bytes, of the 1365 codec_specific_data element as indicated in the "length" field of 1366 codec_specific_data 1368 4.2.3.4. Matroska File Format 1370 FFV1 SHOULD use "V_FFV1" as the Matroska "Codec ID". For FFV1 1371 versions 2 or less, the Matroska "CodecPrivate" Element SHOULD NOT be 1372 used. For FFV1 versions 3 or greater, the Matroska "CodecPrivate" 1373 Element MUST contain the FFV1 "Configuration Record" structure and no 1374 other data. See [Matroska] for more information about elements. 1376 "NumBytes" is defined as the "Element Data Size" of the 1377 "CodecPrivate" Element. 1379 4.3. Frame 1381 A "Frame" is an encoded representation of a complete static image. 1382 The whole "Frame" is provided by the underlaying container. 1384 A "Frame" consists of the keyframe field, "Parameters" (if version 1385 <=1), and a sequence of independent slices. The pseudo-code below 1386 describes the contents of a "Frame". 1388 pseudo-code | type --------------------------------------------------------------|----- Frame( NumBytes ) { | keyframe | br if (keyframe && !ConfigurationRecordIsPresent { | Parameters( ) | } | while (remaining_bits_in_bitstream( NumBytes )) { | Slice( ) | } | } | 1390 Architecture overview of slices in a "Frame": 1392 +-----------------------------------------------------------------+ 1393 +=================================================================+ 1394 | first slice header | 1395 +-----------------------------------------------------------------+ 1396 | first slice content | 1397 +-----------------------------------------------------------------+ 1398 | first slice footer | 1399 +-----------------------------------------------------------------+ 1400 | --------------------------------------------------------------- | 1401 +-----------------------------------------------------------------+ 1402 | second slice header | 1403 +-----------------------------------------------------------------+ 1404 | second slice content | 1405 +-----------------------------------------------------------------+ 1406 | second slice footer | 1407 +-----------------------------------------------------------------+ 1408 | --------------------------------------------------------------- | 1409 +-----------------------------------------------------------------+ 1410 | ... | 1411 +-----------------------------------------------------------------+ 1412 | --------------------------------------------------------------- | 1413 +-----------------------------------------------------------------+ 1414 | last slice header | 1415 +-----------------------------------------------------------------+ 1416 | last slice content | 1417 +-----------------------------------------------------------------+ 1418 | last slice footer | 1419 +-----------------------------------------------------------------+ 1421 Table 16 1423 4.4. Slice 1425 A "Slice" is an independent spatial sub-section of a "Frame" that is 1426 encoded separately from an other region of the same "Frame". The use 1427 of more than one "Slice" per "Frame" can be useful for taking 1428 advantage of the opportunities of multithreaded encoding and 1429 decoding. 1431 A "Slice" consists of a "Slice Header" (when relevant), a "Slice 1432 Content", and a "Slice Footer" (when relevant). The pseudo-code 1433 below describes the contents of a "Slice". 1435 pseudo-code | type --------------------------------------------------------------|----- Slice( ) { | if (version >= 3) { | SliceHeader( ) | } | SliceContent( ) | if (coder_type == 0) { | while (!byte_aligned()) { | padding | u(1) } | } | if (version <= 1) { | while (remaining_bits_in_bitstream( NumBytes ) != 0) {| reserved | u(1) } | } | if (version >= 3) { | SliceFooter( ) | } | } | 1437 "padding" specifies a bit without any significance and used only for 1438 byte alignment. MUST be 0. 1440 "reserved" specifies a bit without any significance in this revision 1441 of the specification and may have a significance in a later revision 1442 of this specification. 1444 Encoders SHOULD NOT fill these bits. 1446 Decoders SHOULD ignore these bits. 1448 Note in case these bits are used in a later revision of this 1449 specification: any revision of this specification SHOULD care about 1450 avoiding to add 40 bits of content after "SliceContent" for version 0 1451 and 1 of the bitstream. Background: due to some non conforming 1452 encoders, some bitstreams where found with 40 extra bits 1453 corresponding to "error_status" and "slice_crc_parity", a decoder 1454 conforming to the revised specification could not do the difference 1455 between a revised bitstream and a buggy bitstream. 1457 4.5. Slice Header 1459 A "Slice Header" provides information about the decoding 1460 configuration of the "Slice", such as its spatial position, size, and 1461 aspect ratio. The pseudo-code below describes the contents of the 1462 "Slice Header". 1464 pseudo-code | type --------------------------------------------------------------|----- SliceHeader( ) { | slice_x | ur slice_y | ur slice_width - 1 | ur slice_height - 1 | ur for (i = 0; i < quant_table_set_index_count; i++) { | quant_table_set_index[ i ] | ur } | picture_structure | ur sar_num | ur sar_den | ur if (version >= 4) { | reset_contexts | br slice_coding_mode | ur } | } | 1466 4.5.1. slice_x 1468 "slice_x" indicates the x position on the slice raster formed by 1469 num_h_slices. 1471 Inferred to be 0 if not present. 1473 4.5.2. slice_y 1475 "slice_y" indicates the y position on the slice raster formed by 1476 num_v_slices. 1478 Inferred to be 0 if not present. 1480 4.5.3. slice_width 1482 "slice_width" indicates the width on the slice raster formed by 1483 num_h_slices. 1485 Inferred to be 1 if not present. 1487 4.5.4. slice_height 1489 "slice_height" indicates the height on the slice raster formed by 1490 num_v_slices. 1492 Inferred to be 1 if not present. 1494 4.5.5. quant_table_set_index_count 1496 "quant_table_set_index_count" is defined as "1 + ( ( chroma_planes || 1497 version <= 3 ) ? 1 : 0 ) + ( extra_plane ? 1 : 0 )". 1499 4.5.6. quant_table_set_index 1501 "quant_table_set_index" indicates the Quantization Table Set index to 1502 select the Quantization Table Set and the initial states for the 1503 slice. 1505 Inferred to be 0 if not present. 1507 4.5.7. picture_structure 1509 "picture_structure" specifies the temporal and spatial relationship 1510 of each "Line" of the "Frame". 1512 Inferred to be 0 if not present. 1514 +-------+-------------------------+ 1515 | value | picture structure used | 1516 +=======+=========================+ 1517 | 0 | unknown | 1518 +-------+-------------------------+ 1519 | 1 | top field first | 1520 +-------+-------------------------+ 1521 | 2 | bottom field first | 1522 +-------+-------------------------+ 1523 | 3 | progressive | 1524 +-------+-------------------------+ 1525 | Other | reserved for future use | 1526 +-------+-------------------------+ 1528 Table 17 1530 4.5.8. sar_num 1532 "sar_num" specifies the "Sample" aspect ratio numerator. 1534 Inferred to be 0 if not present. 1536 A value of 0 means that aspect ratio is unknown. 1538 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1540 If "sar_den" is 0, decoders SHOULD ignore the encoded value and 1541 consider that "sar_num" is 0. 1543 4.5.9. sar_den 1545 "sar_den" specifies the "Sample" aspect ratio denominator. 1547 Inferred to be 0 if not present. 1549 A value of 0 means that aspect ratio is unknown. 1551 Encoders MUST write 0 if "Sample" aspect ratio is unknown. 1553 If "sar_num" is 0, decoders SHOULD ignore the encoded value and 1554 consider that "sar_den" is 0. 1556 4.5.10. reset_contexts 1558 "reset_contexts" indicates if slice contexts must be reset. 1560 Inferred to be 0 if not present. 1562 4.5.11. slice_coding_mode 1564 "slice_coding_mode" indicates the slice coding mode. 1566 Inferred to be 0 if not present. 1568 +-------+-----------------------------+ 1569 | value | slice coding mode | 1570 +=======+=============================+ 1571 | 0 | Range Coding or Golomb Rice | 1572 +-------+-----------------------------+ 1573 | 1 | raw PCM | 1574 +-------+-----------------------------+ 1575 | Other | reserved for future use | 1576 +-------+-----------------------------+ 1578 Table 18 1580 4.6. Slice Content 1582 A "Slice Content" contains all "Line" elements part of the "Slice". 1584 Depending on the configuration, "Line" elements are ordered by 1585 "Plane" then by row (YCbCr) or by row then by "Plane" (RGB). 1587 pseudo-code | type --------------------------------------------------------------|----- SliceContent( ) { | if (colorspace_type == 0) { | for (p = 0; p < primary_color_count; p++) { | for (y = 0; y < plane_pixel_height[ p ]; y++) { | Line( p, y ) | } | } | } else if (colorspace_type == 1) { | for (y = 0; y < slice_pixel_height; y++) { | for (p = 0; p < primary_color_count; p++) { | Line( p, y ) | } | } | } | } | 1589 4.6.1. primary_color_count 1591 "primary_color_count" is defined as "1 + ( chroma_planes ? 2 : 0 ) + 1592 ( extra_plane ? 1 : 0 )". 1594 4.6.2. plane_pixel_height 1596 "plane_pixel_height[ p ]" is the height in pixels of plane p of the 1597 slice. 1599 "plane_pixel_height[ 0 ]" and "plane_pixel_height[ 1 + ( 1600 chroma_planes ? 2 : 0 ) ]" value is "slice_pixel_height". 1602 If "chroma_planes" is set to 1, "plane_pixel_height[ 1 ]" and 1603 "plane_pixel_height[ 2 ]" value is "ceil(slice_pixel_height / 1604 log2_v_chroma_subsample)". 1606 4.6.3. slice_pixel_height 1608 "slice_pixel_height" is the height in pixels of the slice. 1610 Its value is "floor(( slice_y + slice_height ) * slice_pixel_height / 1611 num_v_slices) - slice_pixel_y". 1613 4.6.4. slice_pixel_y 1615 "slice_pixel_y" is the slice vertical position in pixels. 1617 Its value is "floor(slice_y * frame_pixel_height / num_v_slices)". 1619 4.7. Line 1621 A "Line" is a list of the sample differences (relative to the 1622 predictor) of primary color components. The pseudo-code below 1623 describes the contents of the "Line". 1625 pseudo-code | type --------------------------------------------------------------|----- Line( p, y ) { | if (colorspace_type == 0) { | for (x = 0; x < plane_pixel_width[ p ]; x++) { | sample_difference[ p ][ y ][ x ] | } | } else if (colorspace_type == 1) { | for (x = 0; x < slice_pixel_width; x++) { | sample_difference[ p ][ y ][ x ] | } | } | } | 1627 4.7.1. plane_pixel_width 1629 "plane_pixel_width[ p ]" is the width in "Pixels" of "Plane" p of the 1630 slice. 1632 "plane_pixel_width[ 0 ]" and "plane_pixel_width[ 1 + ( chroma_planes 1633 ? 2 : 0 ) ]" value is "slice_pixel_width". 1635 If "chroma_planes" is set to 1, "plane_pixel_width[ 1 ]" and 1636 "plane_pixel_width[ 2 ]" value is "ceil(slice_pixel_width / (1 << 1637 log2_h_chroma_subsample))". 1639 4.7.2. slice_pixel_width 1641 "slice_pixel_width" is the width in "Pixels" of the slice. 1643 Its value is "floor(( slice_x + slice_width ) * slice_pixel_width / 1644 num_h_slices) - slice_pixel_x". 1646 4.7.3. slice_pixel_x 1648 "slice_pixel_x" is the slice horizontal position in "Pixels". 1650 Its value is "floor(slice_x * frame_pixel_width / num_h_slices)". 1652 4.7.4. sample_difference 1654 "sample_difference[ p ][ y ][ x ]" is the sample difference for 1655 "Sample" at "Plane" "p", y position "y", and x position "x". The 1656 "Sample" value is computed based on median predictor and context 1657 described in the section on Samples (#samples). 1659 4.8. Slice Footer 1661 A "Slice Footer" provides information about slice size and 1662 (optionally) parity. The pseudo-code below describes the contents of 1663 the "Slice Footer". 1665 Note: "Slice Footer" is always byte aligned. 1667 pseudo-code | type --------------------------------------------------------------|----- SliceFooter( ) { | slice_size | u(24) if (ec) { | error_status | u(8) slice_crc_parity | u(32) } | } | 1669 4.8.1. slice_size 1671 "slice_size" indicates the size of the slice in bytes. 1673 Note: this allows finding the start of slices before previous slices 1674 have been fully decoded, and allows parallel decoding as well as 1675 error resilience. 1677 4.8.2. error_status 1679 "error_status" specifies the error status. 1681 +-------+--------------------------------------+ 1682 | value | error status | 1683 +=======+======================================+ 1684 | 0 | no error | 1685 +-------+--------------------------------------+ 1686 | 1 | slice contains a correctable error | 1687 +-------+--------------------------------------+ 1688 | 2 | slice contains a uncorrectable error | 1689 +-------+--------------------------------------+ 1690 | Other | reserved for future use | 1691 +-------+--------------------------------------+ 1693 Table 19 1695 4.8.3. slice_crc_parity 1697 "slice_crc_parity" 32 bits that are chosen so that the slice as a 1698 whole has a crc remainder of 0. 1700 This is equivalent to storing the crc remainder in the 32-bit parity. 1702 The CRC generator polynomial used is the standard IEEE CRC polynomial 1703 (0x104C11DB7) with initial value 0. 1705 4.9. Quantization Table Set 1707 The Quantization Table Sets are stored by storing the number of equal 1708 entries -1 of the first half of the table (represented as "len - 1" 1709 in the pseudo-code below) using the method described in Range Non 1710 Binary Values (#range-non-binary-values). The second half doesn't 1711 need to be stored as it is identical to the first with flipped sign. 1712 "scale" and "len_count[ i ][ j ]" are temporary values used for the 1713 computing of "context_count[ i ]" and are not used outside 1714 Quantization Table Set pseudo-code. 1716 Example: 1718 Table: 0 0 1 1 1 1 2 2 -2 -2 -2 -1 -1 -1 -1 0 1720 Stored values: 1, 3, 1 1722 pseudo-code | type --------------------------------------------------------------|----- QuantizationTableSet( i ) { | scale = 1 | for (j = 0; j < MAX_CONTEXT_INPUTS; j++) { | QuantizationTable( i, j, scale ) | scale *= 2 * len_count[ i ][ j ] - 1 | } | context_count[ i ] = ceil( scale / 2 ) | } | 1724 MAX_CONTEXT_INPUTS is 5. 1726 pseudo-code | type --------------------------------------------------------------|----- QuantizationTable(i, j, scale) { | v = 0 | for (k = 0; k < 128;) { | len - 1 | ur for (a = 0; a < len; a++) { | quant_tables[ i ][ j ][ k ] = scale * v | k++ | } | v++ | } | for (k = 1; k < 128; k++) { | quant_tables[ i ][ j ][ 256 - k ] = \ | -quant_tables[ i ][ j ][ k ] | } | quant_tables[ i ][ j ][ 128 ] = \ | -quant_tables[ i ][ j ][ 127 ] | len_count[ i ][ j ] = v | } | 1728 4.9.1. quant_tables 1730 "quant_tables[ i ][ j ][ k ]" indicates the quantification table 1731 value of the Quantized Sample Difference "k" of the Quantization 1732 Table "j" of the Set Quantization Table Set "i". 1734 4.9.2. context_count 1736 "context_count[ i ]" indicates the count of contexts for Quantization 1737 Table Set "i". 1739 5. Restrictions 1741 To ensure that fast multithreaded decoding is possible, starting with 1742 version 3 and if "frame_pixel_width * frame_pixel_height" is more 1743 than 101376, "slice_width * slice_height" MUST be less or equal to 1744 "num_h_slices * num_v_slices / 4". Note: 101376 is the frame size in 1745 "Pixels" of a 352x288 frame also known as CIF ("Common Intermediate 1746 Format") frame size format. 1748 For each "Frame", each position in the slice raster MUST be filled by 1749 one and only one slice of the "Frame" (no missing slice position, no 1750 slice overlapping). 1752 For each "Frame" with keyframe value of 0, each slice MUST have the 1753 same value of "slice_x, slice_y, slice_width, slice_height" as a 1754 slice in the previous "Frame", except if "reset_contexts" is 1. 1756 6. Security Considerations 1758 Like any other codec, (such as [RFC6716]), FFV1 should not be used 1759 with insecure ciphers or cipher-modes that are vulnerable to known 1760 plaintext attacks. Some of the header bits as well as the padding 1761 are easily predictable. 1763 Implementations of the FFV1 codec need to take appropriate security 1764 considerations into account, as outlined in [RFC4732]. It is 1765 extremely important for the decoder to be robust against malicious 1766 payloads. Malicious payloads must not cause the decoder to overrun 1767 its allocated memory or to take an excessive amount of resources to 1768 decode. The same applies to the encoder, even though problems in 1769 encoders are typically rarer. Malicious video streams must not cause 1770 the encoder to misbehave because this would allow an attacker to 1771 attack transcoding gateways. A frequent security problem in image 1772 and video codecs is also to not check for integer overflows in 1773 "Pixel" count computations, that is to allocate width * height 1774 without considering that the multiplication result may have 1775 overflowed the arithmetic types range. The range coder could, if 1776 implemented naively, read one byte over the end. The implementation 1777 must ensure that no read outside allocated and initialized memory 1778 occurs. 1780 The reference implementation [REFIMPL] contains no known buffer 1781 overflow or cases where a specially crafted packet or video segment 1782 could cause a significant increase in CPU load. 1784 The reference implementation [REFIMPL] was validated in the following 1785 conditions: 1787 * Sending the decoder valid packets generated by the reference 1788 encoder and verifying that the decoder's output matches the 1789 encoder's input. 1791 * Sending the decoder packets generated by the reference encoder and 1792 then subjected to random corruption. 1794 * Sending the decoder random packets that are not FFV1. 1796 In all of the conditions above, the decoder and encoder was run 1797 inside the [VALGRIND] memory debugger as well as clangs address 1798 sanitizer [Address-Sanitizer], which track reads and writes to 1799 invalid memory regions as well as the use of uninitialized memory. 1800 There were no errors reported on any of the tested conditions. 1802 7. Media Type Definition 1804 This registration is done using the template defined in [RFC6838] and 1805 following [RFC4855]. 1807 Type name: video 1809 Subtype name: FFV1 1811 Required parameters: None. 1813 Optional parameters: 1815 This parameter is used to signal the capabilities of a receiver 1816 implementation. This parameter MUST NOT be used for any other 1817 purpose. 1819 version: The version of the FFV1 encoding as defined by the section 1820 on version (#version). 1822 micro_version: The micro_version of the FFV1 encoding as defined by 1823 the section on micro_version (#micro-version). 1825 coder_type: The coder_type of the FFV1 encoding as defined by the 1826 section on coder_type (#coder-type). 1828 colorspace_type: The colorspace_type of the FFV1 encoding as defined 1829 by the section on colorspace_type (#colorspace-type). 1831 bits_per_raw_sample: The bits_per_raw_sample of the FFV1 encoding as 1832 defined by the section on bits_per_raw_sample (#bits-per-raw-sample). 1834 max-slices: The value of max-slices is an integer indicating the 1835 maximum count of slices with a frames of the FFV1 encoding. 1837 Encoding considerations: 1839 This media type is defined for encapsulation in several audiovisual 1840 container formats and contains binary data; see the section on 1841 "Mapping FFV1 into Containers" (#mapping-ffv1-into-containers). This 1842 media type is framed binary data Section 4.8 of [RFC6838]. 1844 Security considerations: 1846 See the "Security Considerations" section (#security-considerations) 1847 of this document. 1849 Interoperability considerations: None. 1851 Published specification: 1853 [I-D.ietf-cellar-ffv1] and RFC XXXX. 1855 [RFC Editor: Upon publication as an RFC, please replace "XXXX" with 1856 the number assigned to this document and remove this note.] 1858 Applications which use this media type: 1860 Any application that requires the transport of lossless video can use 1861 this media type. Some examples are, but not limited to screen 1862 recording, scientific imaging, and digital video preservation. 1864 Fragment identifier considerations: N/A. 1866 Additional information: None. 1868 Person & email address to contact for further information: Michael 1869 Niedermayer michael@niedermayer.cc (mailto:michael@niedermayer.cc) 1871 Intended usage: COMMON 1873 Restrictions on usage: None. 1875 Author: Dave Rice dave@dericed.com (mailto:dave@dericed.com) 1877 Change controller: IETF cellar working group delegated from the IESG. 1879 8. IANA Considerations 1881 The IANA is requested to register the following values: 1883 * Media type registration as described in Media Type Definition 1884 (#media-type-definition). 1886 9. Appendixes 1888 9.1. Decoder implementation suggestions 1889 9.1.1. Multi-threading Support and Independence of Slices 1891 The FFV1 bitstream is parsable in two ways: in sequential order as 1892 described in this document or with the pre-analysis of the footer of 1893 each slice. Each slice footer contains a slice_size field so the 1894 boundary of each slice is computable without having to parse the 1895 slice content. That allows multi-threading as well as independence 1896 of slice content (a bitstream error in a slice header or slice 1897 content has no impact on the decoding of the other slices). 1899 After having checked keyframe field, a decoder SHOULD parse 1900 slice_size fields, from slice_size of the last slice at the end of 1901 the "Frame" up to slice_size of the first slice at the beginning of 1902 the "Frame", before parsing slices, in order to have slices 1903 boundaries. A decoder MAY fallback on sequential order e.g. in case 1904 of a corrupted "Frame" (frame size unknown, slice_size of slices not 1905 coherent...) or if there is no possibility of seeking into the 1906 stream. 1908 10. Changelog 1910 See https://github.com/FFmpeg/FFV1/commits/master 1911 (https://github.com/FFmpeg/FFV1/commits/master) 1913 11. Normative References 1915 [I-D.ietf-cellar-ffv1] 1916 Niedermayer, M., Rice, D., and J. Martinez, "FFV1 Video 1917 Coding Format Version 0, 1, and 3", draft-ietf-cellar- 1918 ffv1-07 (work in progress), February 6, 2019, 1919 . 1922 [ISO.15444-1.2016] 1923 International Organization for Standardization, 1924 "Information technology -- JPEG 2000 image coding system: 1925 Core coding system", October 2016. 1927 [ISO.9899.1990] 1928 International Organization for Standardization, 1929 "Programming languages - C", 1990. 1931 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1932 Requirement Levels", BCP 14, RFC 2119, 1933 DOI 10.17487/RFC2119, March 1997, 1934 . 1936 [RFC4732] Handley, M., Ed., Rescorla, E., Ed., and IAB, "Internet 1937 Denial-of-Service Considerations", RFC 4732, 1938 DOI 10.17487/RFC4732, December 2006, 1939 . 1941 [RFC4855] Casner, S., "Media Type Registration of RTP Payload 1942 Formats", RFC 4855, DOI 10.17487/RFC4855, February 2007, 1943 . 1945 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 1946 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716, 1947 September 2012, . 1949 [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type 1950 Specifications and Registration Procedures", BCP 13, 1951 RFC 6838, DOI 10.17487/RFC6838, January 2013, 1952 . 1954 12. Informative References 1956 [Address-Sanitizer] 1957 The Clang Team, "ASAN AddressSanitizer website", August 1958 2019, . 1960 [AVI] Microsoft, "AVI RIFF File Reference", August 2019, 1961 . 1964 [HuffYUV] Rudiak-Gould, B., "HuffYUV", December 2003, 1965 . 1968 [ISO.14495-1.1999] 1969 International Organization for Standardization, 1970 "Information technology -- Lossless and near-lossless 1971 compression of continuous-tone still images: Baseline", 1972 December 1999. 1974 [ISO.14496-10.2014] 1975 International Organization for Standardization, 1976 "Information technology -- Coding of audio-visual objects 1977 -- Part 10: Advanced Video Coding", September 2014. 1979 [ISO.14496-12.2015] 1980 International Organization for Standardization, 1981 "Information technology -- Coding of audio-visual objects 1982 -- Part 12: ISO base media file format", December 2015. 1984 [Matroska] IETF, "Matroska", 2016, 1985 . 1988 [NUT] Niedermayer, M., "NUT Open Container Format", December 1989 2013, . 1991 [range-coding] 1992 Nigel, G. and N. Martin, "Range encoding: an algorithm for 1993 removing redundancy from a digitised message.", July 1979. 1995 [REFIMPL] Niedermayer, M., "The reference FFV1 implementation / the 1996 FFV1 codec in FFmpeg", August 2019, . 1998 [VALGRIND] Valgrind Developers, "Valgrind website", August 2019, 1999 . 2001 [YCbCr] Wikipedia, "YCbCr", August 2019, 2002 . 2004 Authors' Addresses 2006 Michael Niedermayer 2008 Email: michael@niedermayer.cc 2010 Dave Rice 2012 Email: dave@dericed.com 2014 Jerome Martinez 2016 Email: jerome@mediaarea.net