idnits 2.17.1 draft-ietf-cellar-ffv1-03.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 (May 31, 2018) is 2154 days in the past. Is this intentional? Checking references for intended status: Informational ---------------------------------------------------------------------------- -- Looks like a reference, but probably isn't: '1' on line 615 -- Looks like a reference, but probably isn't: '2' on line 615 == Outdated reference: A later version (-20) exists of draft-ietf-cellar-ffv1-02 ** Obsolete normative reference: RFC 4288 (Obsoleted by RFC 6838) Summary: 1 error (**), 0 flaws (~~), 2 warnings (==), 3 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 cellar M. Niedermayer 3 Internet-Draft 4 Intended status: Informational D. Rice 5 Expires: December 2, 2018 6 J. Martinez 7 May 31, 2018 9 FFV1 Video Coding Format Version 0, 1, and 3 10 draft-ietf-cellar-ffv1-03 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 December 2, 2018. 37 Copyright Notice 39 Copyright (c) 2018 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 44 (https://trustee.ietf.org/license-info) in effect on the date of 45 publication of this document. Please review these documents 46 carefully, as they describe your rights and restrictions with respect 47 to this document. Code Components extracted from this document must 48 include Simplified BSD License text as described in Section 4.e of 49 the Trust Legal Provisions and are provided without warranty as 50 described in the Simplified BSD License. 52 Table of Contents 54 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 4 55 2. Notation and Conventions . . . . . . . . . . . . . . . . . . 4 56 2.1. Definitions . . . . . . . . . . . . . . . . . . . . . . . 4 57 2.2. Conventions . . . . . . . . . . . . . . . . . . . . . . . 5 58 2.2.1. Arithmetic operators . . . . . . . . . . . . . . . . 5 59 2.2.2. Assignment operators . . . . . . . . . . . . . . . . 6 60 2.2.3. Comparison operators . . . . . . . . . . . . . . . . 6 61 2.2.4. Mathematical functions . . . . . . . . . . . . . . . 7 62 2.2.5. Order of operation precedence . . . . . . . . . . . . 7 63 2.2.6. Pseudo-code . . . . . . . . . . . . . . . . . . . . . 8 64 2.2.7. Range . . . . . . . . . . . . . . . . . . . . . . . . 8 65 2.2.8. NumBytes . . . . . . . . . . . . . . . . . . . . . . 8 66 2.2.9. Bitstream functions . . . . . . . . . . . . . . . . . 8 67 3. General Description . . . . . . . . . . . . . . . . . . . . . 8 68 3.1. Border . . . . . . . . . . . . . . . . . . . . . . . . . 9 69 3.2. Samples . . . . . . . . . . . . . . . . . . . . . . . . . 9 70 3.3. Median predictor . . . . . . . . . . . . . . . . . . . . 10 71 3.4. Context . . . . . . . . . . . . . . . . . . . . . . . . . 10 72 3.5. Quantization Table Sets . . . . . . . . . . . . . . . . . 11 73 3.6. Quantization Table Set indexes . . . . . . . . . . . . . 11 74 3.7. Color spaces . . . . . . . . . . . . . . . . . . . . . . 12 75 3.7.1. YCbCr . . . . . . . . . . . . . . . . . . . . . . . . 12 76 3.7.2. RGB . . . . . . . . . . . . . . . . . . . . . . . . . 12 77 3.8. Coding of the Sample Difference . . . . . . . . . . . . . 14 78 3.8.1. Range coding mode . . . . . . . . . . . . . . . . . . 14 79 3.8.2. Golomb Rice mode . . . . . . . . . . . . . . . . . . 17 80 4. Bitstream . . . . . . . . . . . . . . . . . . . . . . . . . . 19 81 4.1. Configuration Record . . . . . . . . . . . . . . . . . . 20 82 4.1.1. reserved_for_future_use . . . . . . . . . . . . . . . 21 83 4.1.2. configuration_record_crc_parity . . . . . . . . . . . 21 84 4.1.3. Mapping FFV1 into Containers . . . . . . . . . . . . 21 85 4.2. Frame . . . . . . . . . . . . . . . . . . . . . . . . . . 22 86 4.3. Slice . . . . . . . . . . . . . . . . . . . . . . . . . . 22 87 4.4. Slice Header . . . . . . . . . . . . . . . . . . . . . . 23 88 4.4.1. slice_x . . . . . . . . . . . . . . . . . . . . . . . 23 89 4.4.2. slice_y . . . . . . . . . . . . . . . . . . . . . . . 23 90 4.4.3. slice_width . . . . . . . . . . . . . . . . . . . . . 24 91 4.4.4. slice_height . . . . . . . . . . . . . . . . . . . . 24 92 4.4.5. quant_table_set_index_count . . . . . . . . . . . . . 24 93 4.4.6. quant_table_set_index . . . . . . . . . . . . . . . . 24 94 4.4.7. picture_structure . . . . . . . . . . . . . . . . . . 24 95 4.4.8. sar_num . . . . . . . . . . . . . . . . . . . . . . . 24 96 4.4.9. sar_den . . . . . . . . . . . . . . . . . . . . . . . 25 98 4.5. Slice Content . . . . . . . . . . . . . . . . . . . . . . 25 99 4.5.1. primary_color_count . . . . . . . . . . . . . . . . . 25 100 4.5.2. plane_pixel_height . . . . . . . . . . . . . . . . . 25 101 4.5.3. slice_pixel_height . . . . . . . . . . . . . . . . . 25 102 4.5.4. slice_pixel_y . . . . . . . . . . . . . . . . . . . . 25 103 4.6. Line . . . . . . . . . . . . . . . . . . . . . . . . . . 26 104 4.6.1. plane_pixel_width . . . . . . . . . . . . . . . . . . 26 105 4.6.2. slice_pixel_width . . . . . . . . . . . . . . . . . . 26 106 4.6.3. slice_pixel_x . . . . . . . . . . . . . . . . . . . . 26 107 4.6.4. sample_difference . . . . . . . . . . . . . . . . . . 26 108 4.7. Slice Footer . . . . . . . . . . . . . . . . . . . . . . 26 109 4.7.1. slice_size . . . . . . . . . . . . . . . . . . . . . 27 110 4.7.2. error_status . . . . . . . . . . . . . . . . . . . . 27 111 4.7.3. slice_crc_parity . . . . . . . . . . . . . . . . . . 27 112 4.8. Parameters . . . . . . . . . . . . . . . . . . . . . . . 27 113 4.8.1. version . . . . . . . . . . . . . . . . . . . . . . . 28 114 4.8.2. micro_version . . . . . . . . . . . . . . . . . . . . 29 115 4.8.3. coder_type . . . . . . . . . . . . . . . . . . . . . 29 116 4.8.4. state_transition_delta . . . . . . . . . . . . . . . 30 117 4.8.5. colorspace_type . . . . . . . . . . . . . . . . . . . 30 118 4.8.6. chroma_planes . . . . . . . . . . . . . . . . . . . . 30 119 4.8.7. bits_per_raw_sample . . . . . . . . . . . . . . . . . 30 120 4.8.8. log2_h_chroma_subsample . . . . . . . . . . . . . . . 31 121 4.8.9. log2_v_chroma_subsample . . . . . . . . . . . . . . . 31 122 4.8.10. alpha_plane . . . . . . . . . . . . . . . . . . . . . 31 123 4.8.11. num_h_slices . . . . . . . . . . . . . . . . . . . . 31 124 4.8.12. num_v_slices . . . . . . . . . . . . . . . . . . . . 31 125 4.8.13. quant_table_set_count . . . . . . . . . . . . . . . . 32 126 4.8.14. states_coded . . . . . . . . . . . . . . . . . . . . 32 127 4.8.15. initial_state_delta . . . . . . . . . . . . . . . . . 32 128 4.8.16. ec . . . . . . . . . . . . . . . . . . . . . . . . . 32 129 4.8.17. intra . . . . . . . . . . . . . . . . . . . . . . . . 32 130 4.9. Quantization Table Set . . . . . . . . . . . . . . . . . 33 131 4.9.1. quant_tables . . . . . . . . . . . . . . . . . . . . 34 132 4.9.2. context_count . . . . . . . . . . . . . . . . . . . . 34 133 5. Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 34 134 6. Security Considerations . . . . . . . . . . . . . . . . . . . 35 135 7. Media Type Definition . . . . . . . . . . . . . . . . . . . . 35 136 8. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 37 137 9. Appendixes . . . . . . . . . . . . . . . . . . . . . . . . . 37 138 9.1. Decoder implementation suggestions . . . . . . . . . . . 37 139 9.1.1. Multi-threading support and independence of slices . 37 140 10. Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . 38 141 11. ToDo . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 142 12. References . . . . . . . . . . . . . . . . . . . . . . . . . 38 143 12.1. Normative References . . . . . . . . . . . . . . . . . . 38 144 12.2. Informative References . . . . . . . . . . . . . . . . . 39 145 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 40 147 1. Introduction 149 This document describes FFV1, a lossless video encoding format. The 150 design of FFV1 considers the storage of image characteristics, data 151 fixity, and the optimized use of encoding time and storage 152 requirements. FFV1 is designed to support a wide range of lossless 153 video applications such as long-term audiovisual preservation, 154 scientific imaging, screen recording, and other video encoding 155 scenarios that seek to avoid the generational loss of lossy video 156 encodings. 158 This document defines a version 0, 1, and 3 of FFV1. The 159 distinctions of the versions are provided throughout the document, 160 but in summary: 162 o Version 0 of FFV1 was the original implementation of FFV1 and has 163 been in non-experimental use since April 14, 2006 [FFV1_V0]. 165 o Version 1 of FFV1 adds support of more video bit depths and has 166 been in use since April 24, 2009 [FFV1_V1]. 168 o Version 2 of FFV1 only existed in experimental form and is not 169 described by this document. 171 o Version 3 of FFV1 adds several features such as increased 172 description of the characteristics of the encoding images and 173 embedded CRC data to support fixity verification of the encoding. 174 Version 3 has been in non-experimental use since August 17, 2013 175 [FFV1_V3]. 177 The latest version of this document is available at 178 180 This document assumes familiarity with mathematical and coding 181 concepts such as Range coding [range-coding] and YCbCr color spaces 182 [YCbCr]. 184 2. Notation and Conventions 186 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 187 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this 188 document are to be interpreted as described in [RFC2119]. 190 2.1. Definitions 192 "Container": Format that encapsulates "Frames" and (when required) a 193 "Configuration Record" into a bitstream. 195 "Sample": The smallest addressable representation of a color 196 component or a luma component in a "Frame". Examples of sample are 197 Luma, Blue Chrominance, Red Chrominance, Alpha, Red, Green, and Blue. 199 "Pixel": The smallest addressable representation of a color in a 200 "Frame". It is composed of 1 or more samples. 202 "ESC": An ESCape symbol to indicate that the symbol to be stored is 203 too large for normal storage and that an alternate storage method. 205 "MSB": Most Significant Bit, the bit that can cause the largest 206 change in magnitude of the symbol. 208 "RCT": Reversible Color Transform, a near linear, exactly reversible 209 integer transform that converts between RGB and YCbCr representations 210 of a Pixel. 212 "VLC": Variable Length Code, a code that maps source symbols to a 213 variable number of bits. 215 "RGB": A reference to the method of storing the value of a Pixel by 216 using three numeric values that represent Red, Green, and Blue. 218 "YCbCr": A reference to the method of storing the value of a Pixel by 219 using three numeric values that represent the luma of the Pixel (Y) 220 and the chrominance of the Pixel (Cb and Cr). YCbCr word is used for 221 historical reasons and currently references any color space relying 222 on 1 luma sample and 2 chrominance samples e.g. YCbCr, YCgCo or 223 ICtCp. Exact meaning of the three numeric values is unspecified. 225 "TBA": To Be Announced. Used in reference to the development of 226 future iterations of the FFV1 specification. 228 2.2. Conventions 230 Note: the operators and the order of precedence are the same as used 231 in the C programming language [ISO.9899.1990]. 233 2.2.1. Arithmetic operators 235 "a + b" means a plus b. 237 "a - b" means a minus b. 239 "-a" means negation of a. 241 "a * b" means a multiplied by b. 243 "a / b" means a divided by b. 245 "a & b" means bit-wise "and" of a and b. 247 "a | b" means bit-wise "or" of a and b. 249 "a >> b" means arithmetic right shift of two's complement integer 250 representation of a by b binary digits. 252 "a << b" means arithmetic left shift of two's complement integer 253 representation of a by b binary digits. 255 2.2.2. Assignment operators 257 "a = b" means a is assigned b. 259 "a++" is equivalent to a is assigned a + 1. 261 "a--" is equivalent to a is assigned a - 1. 263 "a += b" is equivalent to a is assigned a + b. 265 "a -= b" is equivalent to a is assigned a - b. 267 "a *= b" is equivalent to a is assigned a * b. 269 2.2.3. Comparison operators 271 "a > b" means a is greater than b. 273 "a >= b" means a is greater than or equal to b. 275 "a < b" means a is less than b. 277 "a <= b" means a is less than or equal b. 279 "a == b" means a is equal to b. 281 "a != b" means a is not equal to b. 283 "a && b" means Boolean logical "and" of a and b. 285 "a || b" means Boolean logical "or" of a and b. 287 "!a" means Boolean logical "not" of a. 289 "a ? b : c" if a is true, then b, otherwise c. 291 2.2.4. Mathematical functions 293 floor(a) the largest integer less than or equal to a 295 ceil(a) the largest integer less than or equal to a 297 sign(a) extracts the sign of a number, i.e. if a < 0 then -1, else if 298 a > 0 then 1, else 0 300 abs(a) the absolute value of a, i.e. abs(a) = sign(a)*a 302 log2(a) the base-two logarithm of a 304 min(a,b) the smallest of two values a and b 306 max(a,b) the largest of two values a and b 308 median(a,b,c) the numerical middle value in a data set of a, b, and 309 c, i.e. a+b+c-min(a,b,c)-max(a,b,c) 311 a_{b} the b-th value of a sequence of a 313 a_{b,c} the 'b,c'-th value of a sequence of a 315 2.2.5. Order of operation precedence 317 When order of precedence is not indicated explicitly by use of 318 parentheses, operations are evaluated in the following order (from 319 top to bottom, operations of same precedence being evaluated from 320 left to right). This order of operations is based on the order of 321 operations used in Standard C. 323 a++, a-- 324 !a, -a 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.6. Pseudo-code 339 The FFV1 bitstream is described in this document using pseudo-code. 340 Note that the pseudo-code is used for clarity in order to illustrate 341 the structure of FFV1 and not intended to specify any particular 342 implementation. The pseudo-code used is based upon the C programming 343 language [ISO.9899.1990] as uses its "if/else", "while" and "for" 344 functions as well as functions defined within this document. 346 2.2.7. Range 348 "a...b" means any value starting from a to b, inclusive. 350 2.2.8. NumBytes 352 "NumBytes" is a non-negative integer that expresses the size in 8-bit 353 octets of particular FFV1 "Configuration Record" or "Frame". FFV1 354 relies on its "Container" to store the "NumBytes" values, see 355 Section 4.1.3. 357 2.2.9. Bitstream functions 359 2.2.9.1. remaining_bits_in_bitstream 361 "remaining_bits_in_bitstream( )" means the count of remaining bits 362 after the pointer in that "Configuration Record" or "Frame". It is 363 computed from the "NumBytes" value multiplied by 8 minus the count of 364 bits of that "Configuration Record" or "Frame" already read by the 365 bitstream parser. 367 2.2.9.2. byte_aligned 369 "byte_aligned( )" is true if "remaining_bits_in_bitstream( NumBytes 370 )" is a multiple of 8, otherwise false. 372 2.2.9.3. get_bits 374 "get_bits( i )" is the action to read the next "i" bits in the 375 bitstream, from most significant bit to least significant bit, and to 376 return the corresponding value. The pointer is increased by "i". 378 3. General Description 380 Samples within a plane are coded in raster scan order (left->right, 381 top->bottom). Each sample is predicted by the median predictor from 382 samples in the same plane and the difference is stored see 383 Section 3.8. 385 3.1. Border 387 A border is assumed for each coded slice for the purpose of the 388 predictor and context according to the following rules: 390 o one column of samples to the left of the coded slice is assumed as 391 identical to the samples of the leftmost column of the coded slice 392 shifted down by one row. The value of the topmost sample of the 393 column of samples to the left of the coded slice is assumed to be 394 "0" 396 o one column of samples to the right of the coded slice is assumed 397 as identical to the samples of the rightmost column of the coded 398 slice 400 o an additional column of samples to the left of the coded slice and 401 two rows of samples above the coded slice are assumed to be "0" 403 The following table depicts a slice of samples "a,b,c,d,e,f,g,h,i" 404 along with its assumed border. 406 +---+---+---+---+---+---+---+---+ 407 | 0 | 0 | | 0 | 0 | 0 | | 0 | 408 +---+---+---+---+---+---+---+---+ 409 | 0 | 0 | | 0 | 0 | 0 | | 0 | 410 +---+---+---+---+---+---+---+---+ 411 | | | | | | | | | 412 +---+---+---+---+---+---+---+---+ 413 | 0 | 0 | | a | b | c | | c | 414 +---+---+---+---+---+---+---+---+ 415 | 0 | a | | d | e | f | | f | 416 +---+---+---+---+---+---+---+---+ 417 | 0 | d | | g | h | i | | i | 418 +---+---+---+---+---+---+---+---+ 420 3.2. Samples 422 Positions used for context and median predictor are: 424 +---+---+---+---+ 425 | | | T | | 426 +---+---+---+---+ 427 | |tl | t |tr | 428 +---+---+---+---+ 429 | L | l | X | | 430 +---+---+---+---+ 432 "X" is the current processed Sample. The identifiers are made of the 433 first letters of the words Top, Left and Right. 435 3.3. Median predictor 437 The prediction for any sample value at position "X" may be computed 438 based upon the relative neighboring values of "l", "t", and "tl" via 439 this equation: 441 "median(l, t, l + t - tl)". 443 Note, this prediction template is also used in [ISO.14495-1.1999] and 444 [HuffYUV]. 446 Exception for the media predictor: if "colorspace_type == 0 && 447 bits_per_raw_sample == 16 && ( coder_type == 1 || coder_type == 2 )", 448 the following media predictor MUST be used: 450 "median(left16s, top16s, left16s + top16s - diag16s)" 452 where: 454 left16s = l >= 32768 ? ( l - 65536 ) : l 455 top16s = t >= 32768 ? ( t - 65536 ) : t 456 diag16s = tl >= 32768 ? ( tl - 65536 ) : tl 458 Background: a two's complement signed 16-bit signed integer was used 459 for storing sample values in all known implementations of FFV1 460 bitstream. So in some circumstances, the most significant bit was 461 wrongly interpreted (used as a sign bit instead of the 16th bit of an 462 unsigned integer). Note that when the issue is discovered, the only 463 configuration of all known implementations being impacted is 16-bit 464 YCbCr with no Pixel transformation with Range Coder coder, as other 465 potentially impacted configurations (e.g. 15/16-bit JPEG2000-RCT with 466 Range Coder coder, or 16-bit content with Golomb Rice coder) were 467 implemented nowhere. In the meanwhile, 16-bit JPEG2000-RCT with 468 Range Coder coder was implemented without this issue in one 469 implementation and validated by one conformance checker. It is 470 expected (to be confirmed) to remove this exception for the media 471 predictor in the next version of the FFV1 bitstream. 473 3.4. Context 475 Relative to any sample "X", the Quantized Sample Differences "L-l", 476 "l-tl", "tl-t", "T-t", and "t-tr" are used as context: 478 context = Q_{0}[l - tl] + 479 Q_{1}[tl - t] + 480 Q_{2}[t - tr] + 481 Q_{3}[L - l] + 482 Q_{4}[T - t] 484 If "context >= 0" then "context" is used and the difference between 485 the sample and its predicted value is encoded as is, else "-context" 486 is used and the difference between the sample and its predicted value 487 is encoded with a flipped sign. 489 3.5. Quantization Table Sets 491 The FFV1 bitstream contains 1 or more Quantization Table Sets. Each 492 Quantization Table Set contains exactly 5 Quantization Tables, each 493 Quantization Table corresponding to 1 of the 5 Quantized Sample 494 Differences. For each Quantization Table, both the number of 495 quantization steps and their distribution are stored in the FFV1 496 bitstream; each Quantization Table has exactly 256 entries, and the 8 497 least significant bits of the Quantized Sample Difference are used as 498 index: 500 Q_{j}[k] = quant_tables[i][j][k&255] 502 In this formula, "i" is the Quantization Table Set index, "j" is the 503 Quantized Table index, "k" the Quantized Sample Difference. 505 3.6. Quantization Table Set indexes 507 For each plane of each slice, a Quantization Table Set is selected 508 from an index: 510 o For Y plane, "quant_table_set_index [ 0 ]" index is used 512 o For Cb and Cr planes, "quant_table_set_index [ 1 ]" index is used 514 o For Alpha plane, "quant_table_set_index [ (version <= 3 || 515 chroma_planes) ? 2 : 1 ]" index is used 517 Background: in first implementations of FFV1 bitstream, the index for 518 Cb and Cr planes was stored even if it is not used (chroma_planes set 519 to 0), this index is kept for version <= 3 in order to keep 520 compatibility with FFV1 bitstreams in the wild. 522 3.7. Color spaces 524 FFV1 supports two color spaces: YCbCr and RGB. Both color spaces 525 allow an optional Alpha plane that can be used to code transparency 526 data. 528 3.7.1. YCbCr 530 In YCbCr color space, the Cb and Cr planes are optional, but if used 531 then MUST be used together. Omitting the Cb and Cr planes codes the 532 frames in grayscale without color data. An FFV1 "Frame" using YCbCr 533 MUST use one of the following arrangements: 535 o Y 537 o Y, Alpha 539 o Y, Cb, Cr 541 o Y, Cb, Cr, Alpha 543 The Y plane MUST be coded first. If the Cb and Cr planes are used 544 then they MUST be coded after the Y plane. If an Alpha 545 (transparency) plane is used, then it MUST be coded last. 547 3.7.2. RGB 549 JPEG2000-RCT is a Reversible Color Transform that codes RGB (red, 550 green, blue) planes losslessly in a modified YCbCr color space. 551 Reversible Pixel transformations between YCbCr and RGB use the 552 following formulae. 554 Cb=b-g 556 Cr=r-g 558 Y=g+(Cb+Cr)>>2 560 g=Y-(Cb+Cr)>>2 562 r=Cr+g 564 b=Cb+g 566 Exception for the JPEG2000-RCT conversion: if bits_per_raw_sample is 567 between 9 and 15 inclusive and alpha_plane is 0, the following 568 formulae for reversible conversions between YCbCr and RGB MUST be 569 used instead of the ones above: 571 Cb=g-b 573 Cr=r-b 575 Y=b+(Cb+Cr)>>2 577 b=Y-(Cb+Cr)>>2 579 r=Cr+b 581 g=Cb+b 583 Background: At the time of this writing, in all known implementations 584 of FFV1 bitstream, when bits_per_raw_sample was between 9 and 15 585 inclusive and alpha_plane is 0, GBR planes were used as BGR planes 586 during both encoding and decoding. In the meanwhile, 16-bit 587 JPEG2000-RCT was implemented without this issue in one implementation 588 and validated by one conformance checker. Methods to address this 589 exception for the transform are under consideration for the next 590 version of the FFV1 bitstream. 592 [ISO.15444-1.2016] 594 When FFV1 uses the JPEG2000-RCT, the horizontal lines are interleaved 595 to improve caching efficiency since it is most likely that the RCT 596 will immediately be converted to RGB during decoding. The 597 interleaved coding order is also Y, then Cb, then Cr, and then if 598 used Alpha. 600 As an example, a "Frame" that is two pixels wide and two pixels high, 601 could be comprised of the following structure: 603 +------------------------+------------------------+ 604 | Pixel[1,1] | Pixel[2,1] | 605 | Y[1,1] Cb[1,1] Cr[1,1] | Y[2,1] Cb[2,1] Cr[2,1] | 606 +------------------------+------------------------+ 607 | Pixel[1,2] | Pixel[2,2] | 608 | Y[1,2] Cb[1,2] Cr[1,2] | Y[2,2] Cb[2,2] Cr[2,2] | 609 +------------------------+------------------------+ 611 In JPEG2000-RCT, the coding order would be left to right and then top 612 to bottom, with values interleaved by lines and stored in this order: 614 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] 615 Cb[2,2] Cr[1,2] Cr[2,2] 617 3.8. Coding of the Sample Difference 619 Instead of coding the n+1 bits of the Sample Difference with Huffman 620 or Range coding (or n+2 bits, in the case of RCT), only the n (or 621 n+1) least significant bits are used, since this is sufficient to 622 recover the original sample. In the equation below, the term "bits" 623 represents bits_per_raw_sample+1 for RCT or bits_per_raw_sample 624 otherwise: 626 coder_input = 627 [(sample_difference + 2^(bits-1)) & (2^bits - 1)] - 2^(bits-1) 629 3.8.1. Range coding mode 631 Early experimental versions of FFV1 used the CABAC Arithmetic coder 632 from H.264 as defined in [ISO.14496-10.2014] but due to the uncertain 633 patent/royalty situation, as well as its slightly worse performance, 634 CABAC was replaced by a Range coder based on an algorithm defined by 635 _G. Nigel_ and _N. Martin_ in 1979 [range-coding]. 637 3.8.1.1. Range binary values 639 To encode binary digits efficiently a Range coder is used. "C_{i}" 640 is the i-th Context. "B_{i}" is the i-th byte of the bytestream. 641 "b_{i}" is the i-th Range coded binary value, "S_{0,i}" is the i-th 642 initial state, which is 128. The length of the bytestream encoding n 643 binary symbols is "j_{n}" bytes. 645 r_{i} = floor( ( R_{i} * S_{i,C_{i}} ) / 2^8 ) 647 S_{i+1,C_{i}} = zero_state_{S_{i,C_{i}}} XOR 648 l_i = L_i XOR 649 t_i = R_i - r_i <== 650 b_i = 0 <==> 651 L_i < R_i - r_i 653 S_{i+1,C_{i}} = one_state_{S_{i,C_{i}}} XOR 654 l_i = L_i - R_i + r_i XOR 655 t_i = r_i <== 656 b_i = 1 <==> 657 L_i >= R_i - r_i 659 S_{i+1,k} = S_{i,k} <== C_i != k 661 R_{i+1} = 2^8 * t_{i} XOR 662 L_{i+1} = 2^8 * l_{i} + B_{j_{i}} XOR 663 j_{i+1} = j_{i} + 1 <== 664 t_{i} < 2^8 665 R_{i+1} = t_{i} XOR 666 L_{i+1} = l_{i} XOR 667 j_{i+1} = j_{i} <== 668 t_{i} >= 2^8 670 R_{0} = 65280 672 L_{0} = 2^8 * B_{0} + B_{1} 674 j_{0} = 2 676 3.8.1.2. Range non binary values 678 To encode scalar integers, it would be possible to encode each bit 679 separately and use the past bits as context. However that would mean 680 255 contexts per 8-bit symbol that is not only a waste of memory but 681 also requires more past data to reach a reasonably good estimate of 682 the probabilities. Alternatively assuming a Laplacian distribution 683 and only dealing with its variance and mean (as in Huffman coding) 684 would also be possible, however, for maximum flexibility and 685 simplicity, the chosen method uses a single symbol to encode if a 686 number is 0 and if not encodes the number using its exponent, 687 mantissa and sign. The exact contexts used are best described by the 688 following code, followed by some comments. 690 pseudo-code | type 691 --------------------------------------------------------------|----- 692 void put_symbol(RangeCoder *c, uint8_t *state, int v, int \ | 693 is_signed) { | 694 int i; | 695 put_rac(c, state+0, !v); | 696 if (v) { | 697 int a= abs(v); | 698 int e= log2(a); | 699 | 700 for (i=0; i=0; i--) | 705 put_rac(c, state+22+min(i,9), (a>>i)&1); //22..31 | 706 | 707 if (is_signed) | 708 put_rac(c, state+11 + min(e, 10), v < 0); //11..21| 709 } | 710 } | 712 3.8.1.3. Initial values for the context model 714 At keyframes all Range coder state variables are set to their initial 715 state. 717 3.8.1.4. State transition table 719 one_state_{i} = 720 default_state_transition_{i} + state_transition_delta_{i} 722 zero_state_{i} = 256 - one_state_{256-i} 724 3.8.1.5. default_state_transition 726 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 728 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 730 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 732 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 734 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 736 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99,100,101,102,103, 738 104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118, 740 119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,133, 742 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149, 744 150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164, 746 165,166,167,168,169,170,171,171,172,173,174,175,176,177,178,179, 748 180,181,182,183,184,185,186,187,188,189,190,190,191,192,194,194, 750 195,196,197,198,199,200,201,202,202,204,205,206,207,208,209,209, 752 210,211,212,213,215,215,216,217,218,219,220,220,222,223,224,225, 754 226,227,227,229,229,230,231,232,234,234,235,236,237,238,239,240, 756 241,242,243,244,245,246,247,248,248, 0, 0, 0, 0, 0, 0, 0, 758 3.8.1.6. alternative state transition table 760 The alternative state transition table has been built using iterative 761 minimization of frame sizes and generally performs better than the 762 default. To use it, the coder_type MUST be set to 2 and the 763 difference to the default MUST be stored in the parameters. The 764 reference implementation of FFV1 in FFmpeg uses this table by default 765 at the time of this writing when Range coding is used. 767 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49, 769 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39, 771 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52, 773 53, 74, 55, 57, 58, 58, 74, 60,101, 61, 62, 84, 66, 66, 68, 69, 775 87, 82, 71, 97, 73, 73, 82, 75,111, 77, 94, 78, 87, 81, 83, 97, 777 85, 83, 94, 86, 99, 89, 90, 99,111, 92, 93,134, 95, 98,105, 98, 779 105,110,102,108,102,118,103,106,106,113,109,112,114,112,116,125, 781 115,116,117,117,126,119,125,121,121,123,145,124,126,131,127,129, 783 165,130,132,138,133,135,145,136,137,139,146,141,143,142,144,148, 785 147,155,151,149,151,150,152,157,153,154,156,168,158,162,161,160, 787 172,163,169,164,166,184,167,170,177,174,171,173,182,176,180,178, 789 175,189,179,181,186,183,192,185,200,187,191,188,190,197,193,196, 791 197,194,195,196,198,202,199,201,210,203,207,204,205,206,208,214, 793 209,211,221,212,213,215,224,216,217,218,219,220,222,228,223,225, 795 226,224,227,229,240,230,231,232,233,234,235,236,238,239,237,242, 797 241,243,242,244,245,246,247,248,249,250,251,252,252,253,254,255, 799 3.8.2. Golomb Rice mode 801 This coding mode uses Golomb Rice codes. The VLC is split into 2 802 parts, the prefix stores the most significant bits and the suffix 803 stores the k least significant bits or stores the whole number in the 804 ESC case. The end of the bitstream of the "Frame" is filled with 805 0-bits until that the bitstream contains a multiple of 8 bits. 807 3.8.2.1. Prefix 809 +----------------+-------+ 810 | bits | value | 811 +----------------+-------+ 812 | 1 | 0 | 813 | 01 | 1 | 814 | ... | ... | 815 | 0000 0000 0001 | 11 | 816 | 0000 0000 0000 | ESC | 817 +----------------+-------+ 819 3.8.2.2. Suffix 821 +-------+-----------------------------------------------------------+ 822 | non | the k least significant bits MSB first | 823 | ESC | | 824 | ESC | the value - 11, in MSB first order, ESC may only be used | 825 | | if the value cannot be coded as non ESC | 826 +-------+-----------------------------------------------------------+ 828 3.8.2.3. Examples 830 +-----+-------------------------+-------+ 831 | k | bits | value | 832 +-----+-------------------------+-------+ 833 | 0 | "1" | 0 | 834 | 0 | "001" | 2 | 835 | 2 | "1 00" | 0 | 836 | 2 | "1 10" | 2 | 837 | 2 | "01 01" | 5 | 838 | any | "000000000000 10000000" | 139 | 839 +-----+-------------------------+-------+ 841 3.8.2.4. Run mode 843 Run mode is entered when the context is 0 and left as soon as a non-0 844 difference is found. The level is identical to the predicted one. 845 The run and the first different level are coded. 847 3.8.2.5. Run length coding 849 The run value is encoded in 2 parts, the prefix part stores the more 850 significant part of the run as well as adjusting the run_index that 851 determines the number of bits in the less significant part of the 852 run. The 2nd part of the value stores the less significant part of 853 the run as it is. The run_index is reset for each plane and slice to 854 0. 856 pseudo-code | type 857 --------------------------------------------------------------|----- 858 log2_run[41]={ | 859 0, 0, 0, 0, 1, 1, 1, 1, | 860 2, 2, 2, 2, 3, 3, 3, 3, | 861 4, 4, 5, 5, 6, 6, 7, 7, | 862 8, 9,10,11,12,13,14,15, | 863 16,17,18,19,20,21,22,23, | 864 24, | 865 }; | 866 | 867 if (run_count == 0 && run_mode == 1) { | 868 if (get_bits(1)) { | 869 run_count = 1 << log2_run[run_index]; | 870 if (x + run_count <= w) | 871 run_index++; | 872 } else { | 873 if (log2_run[run_index]) | 874 run_count = get_bits(log2_run[run_index]); | 875 else | 876 run_count = 0; | 877 if (run_index) | 878 run_index--; | 879 run_mode = 2; | 880 } | 881 } | 883 The log2_run function is also used within [ISO.14495-1.1999]. 885 3.8.2.6. Level coding 887 Level coding is identical to the normal difference coding with the 888 exception that the 0 value is removed as it cannot occur: 890 if (diff>0) diff--; 891 encode(diff); 893 Note, this is different from JPEG-LS, which doesn't use prediction in 894 run mode and uses a different encoding and context model for the last 895 difference On a small set of test samples the use of prediction 896 slightly improved the compression rate. 898 4. Bitstream 899 +--------+----------------------------------------------------------+ 900 | Symbol | Definition | 901 +--------+----------------------------------------------------------+ 902 | u(n) | unsigned big endian integer using n bits | 903 | sg | Golomb Rice coded signed scalar symbol coded with the | 904 | | method described in Section 3.8.2 | 905 | br | Range coded Boolean (1-bit) symbol with the method | 906 | | described in Section 3.8.1.1 | 907 | ur | Range coded unsigned scalar symbol coded with the method | 908 | | described in Section 3.8.1.2 | 909 | sr | Range coded signed scalar symbol coded with the method | 910 | | described in Section 3.8.1.2 | 911 +--------+----------------------------------------------------------+ 913 The same context that is initialized to 128 is used for all fields in 914 the header. 916 The following MUST be provided by external means during 917 initialization of the decoder: 919 "frame_pixel_width" is defined as "Frame" width in pixels. 921 "frame_pixel_height" is defined as "Frame" height in pixels. 923 Default values at the decoder initialization phase: 925 "ConfigurationRecordIsPresent" is set to 0. 927 4.1. Configuration Record 929 In the case of a FFV1 bitstream with "version >= 3", a "Configuration 930 Record" is stored in the underlying "Container", at the track header 931 level. It contains the parameters used for all instances of "Frame". 932 The size of the "Configuration Record", "NumBytes", is supplied by 933 the underlying "Container". 935 pseudo-code | type 936 --------------------------------------------------------------|----- 937 ConfigurationRecord( NumBytes ) { | 938 ConfigurationRecordIsPresent = 1 | 939 Parameters( ) | 940 while( remaining_bits_in_bitstream( NumBytes ) > 32 ) | 941 reserved_for_future_use | u(1) 942 configuration_record_crc_parity | u(32) 943 } | 945 4.1.1. reserved_for_future_use 947 "reserved_for_future_use" has semantics that are reserved for future 948 use. 949 Encoders conforming to this version of this specification SHALL NOT 950 write this value. 951 Decoders conforming to this version of this specification SHALL 952 ignore its value. 954 4.1.2. configuration_record_crc_parity 956 "configuration_record_crc_parity" 32 bits that are chosen so that the 957 "Configuration Record" as a whole has a crc remainder of 0. 958 This is equivalent to storing the crc remainder in the 32-bit parity. 959 The CRC generator polynomial used is the standard IEEE CRC polynomial 960 (0x104C11DB7) with initial value 0. 962 4.1.3. Mapping FFV1 into Containers 964 This "Configuration Record" can be placed in any file format 965 supporting "Configuration Records", fitting as much as possible with 966 how the file format uses to store "Configuration Records". The 967 "Configuration Record" storage place and "NumBytes" are currently 968 defined and supported by this version of this specification for the 969 following formats: 971 4.1.3.1. AVI File Format 973 The "Configuration Record" extends the stream format chunk ("AVI ", 974 "hdlr", "strl", "strf") with the ConfigurationRecord bitstream. 975 See [AVI] for more information about chunks. 977 "NumBytes" is defined as the size, in bytes, of the strf chunk 978 indicated in the chunk header minus the size of the stream format 979 structure. 981 4.1.3.2. ISO Base Media File Format 983 The "Configuration Record" extends the sample description box 984 ("moov", "trak", "mdia", "minf", "stbl", "stsd") with a "glbl" box 985 that contains the ConfigurationRecord bitstream. See 986 [ISO.14496-12.2015] for more information about boxes. 988 "NumBytes" is defined as the size, in bytes, of the "glbl" box 989 indicated in the box header minus the size of the box header. 991 4.1.3.3. NUT File Format 993 The codec_specific_data element (in "stream_header" packet) contains 994 the ConfigurationRecord bitstream. See [NUT] for more information 995 about elements. 997 "NumBytes" is defined as the size, in bytes, of the 998 codec_specific_data element as indicated in the "length" field of 999 codec_specific_data 1001 4.1.3.4. Matroska File Format 1003 FFV1 SHOULD use "V_FFV1" as the Matroska "Codec ID". For FFV1 1004 versions 2 or less, the Matroska "CodecPrivate" Element SHOULD NOT be 1005 used. For FFV1 versions 3 or greater, the Matroska "CodecPrivate" 1006 Element MUST contain the FFV1 "Configuration Record" structure and no 1007 other data. See [Matroska] for more information about elements. 1009 "NumBytes" is defined as the "Element Data Size" of the 1010 "CodecPrivate" Element. 1012 4.2. Frame 1014 A "Frame" consists of the keyframe field, parameters (if version 1015 <=1), and a sequence of independent slices. 1017 pseudo-code | type 1018 --------------------------------------------------------------|----- 1019 Frame( NumBytes ) { | 1020 keyframe | br 1021 if (keyframe && !ConfigurationRecordIsPresent | 1022 Parameters( ) | 1023 while ( remaining_bits_in_bitstream( NumBytes ) ) | 1024 Slice( ) | 1025 } | 1027 4.3. Slice 1028 pseudo-code | type 1029 --------------------------------------------------------------|----- 1030 Slice( ) { | 1031 if (version >= 3) | 1032 SliceHeader( ) | 1033 SliceContent( ) | 1034 if (coder_type == 0) | 1035 while (!byte_aligned()) | 1036 padding | u(1) 1037 if (version >= 3) | 1038 SliceFooter( ) | 1039 } | 1041 "padding" specifies a bit without any significance and used only for 1042 byte alignment. MUST be 0. 1044 4.4. Slice Header 1046 pseudo-code | type 1047 --------------------------------------------------------------|----- 1048 SliceHeader( ) { | 1049 slice_x | ur 1050 slice_y | ur 1051 slice_width - 1 | ur 1052 slice_height - 1 | ur 1053 for( i = 0; i < quant_table_set_index_count; i++ ) | 1054 quant_table_set_index [ i ] | ur 1055 picture_structure | ur 1056 sar_num | ur 1057 sar_den | ur 1058 } | 1060 4.4.1. slice_x 1062 "slice_x" indicates the x position on the slice raster formed by 1063 num_h_slices. 1064 Inferred to be 0 if not present. 1066 4.4.2. slice_y 1068 "slice_y" indicates the y position on the slice raster formed by 1069 num_v_slices. 1070 Inferred to be 0 if not present. 1072 4.4.3. slice_width 1074 "slice_width" indicates the width on the slice raster formed by 1075 num_h_slices. 1076 Inferred to be 1 if not present. 1078 4.4.4. slice_height 1080 "slice_height" indicates the height on the slice raster formed by 1081 num_v_slices. 1082 Inferred to be 1 if not present. 1084 4.4.5. quant_table_set_index_count 1086 "quant_table_set_index_count" is defined as "1 + ( ( chroma_planes || 1087 version \<= 3 ) ? 1 : 0 ) + ( alpha_plane ? 1 : 0 )". 1089 4.4.6. quant_table_set_index 1091 "quant_table_set_index" indicates the Quantization Table Set index to 1092 select the Quantization Table Set and the initial states for the 1093 slice. 1094 Inferred to be 0 if not present. 1096 4.4.7. picture_structure 1098 "picture_structure" specifies the temporal and spatial relationship 1099 of each line of the "Frame". 1100 Inferred to be 0 if not present. 1102 +-------+-------------------------+ 1103 | value | picture structure used | 1104 +-------+-------------------------+ 1105 | 0 | unknown | 1106 | 1 | top field first | 1107 | 2 | bottom field first | 1108 | 3 | progressive | 1109 | Other | reserved for future use | 1110 +-------+-------------------------+ 1112 4.4.8. sar_num 1114 "sar_num" specifies the sample aspect ratio numerator. 1115 Inferred to be 0 if not present. 1116 MUST be 0 if sample aspect ratio is unknown. 1118 4.4.9. sar_den 1120 "sar_den" specifies the sample aspect ratio numerator. 1121 Inferred to be 0 if not present. 1122 MUST be 0 if sample aspect ratio is unknown. 1124 4.5. Slice Content 1126 pseudo-code | type 1127 --------------------------------------------------------------|----- 1128 SliceContent( ) { | 1129 if (colorspace_type == 0) { | 1130 for( p = 0; p < primary_color_count; p++ ) | 1131 for( y = 0; y < plane_pixel_height[ p ]; y++ ) | 1132 Line( p, y ) | 1133 } else if (colorspace_type == 1) { | 1134 for( y = 0; y < slice_pixel_height; y++ ) | 1135 for( p = 0; p < primary_color_count; p++ ) | 1136 Line( p, y ) | 1137 } | 1138 } | 1140 4.5.1. primary_color_count 1142 "primary_color_count" is defined as 1 + ( chroma_planes ? 2 : 0 ) + ( 1143 alpha_plane ? 1 : 0 ). 1145 4.5.2. plane_pixel_height 1147 "plane_pixel_height[ p ]" is the height in pixels of plane p of the 1148 slice. 1149 "plane_pixel_height[ 0 ]" and "plane_pixel_height[ 1 + ( 1150 chroma_planes ? 2 : 0 ) ]" value is "slice_pixel_height". 1151 If "chroma_planes" is set to 1, "plane_pixel_height[ 1 ]" and 1152 "plane_pixel_height[ 2 ]" value is "ceil(slice_pixel_height / 1153 log2_v_chroma_subsample)". 1155 4.5.3. slice_pixel_height 1157 "slice_pixel_height" is the height in pixels of the slice. 1158 Its value is "floor(( slice_y + slice_height ) * slice_pixel_height / 1159 num_v_slices) - slice_pixel_y". 1161 4.5.4. slice_pixel_y 1163 "slice_pixel_y" is the slice vertical position in pixels. 1164 Its value is "floor(slice_y * frame_pixel_height / num_v_slices)". 1166 4.6. Line 1168 pseudo-code | type 1169 --------------------------------------------------------------|----- 1170 Line( p, y ) { | 1171 if (colorspace_type == 0) { | 1172 for( x = 0; x < plane_pixel_width[ p ]; x++ ) | 1173 sample_difference[ p ][ y ][ x ] | 1174 } else if (colorspace_type == 1) { | 1175 for( x = 0; x < slice_pixel_width; x++ ) | 1176 sample_difference[ p ][ y ][ x ] | 1177 } | 1178 } | 1180 4.6.1. plane_pixel_width 1182 "plane_pixel_width[ p ]" is the width in pixels of plane p of the 1183 slice. 1184 "plane_pixel_width[ 0 ]" and "plane_pixel_width[ 1 + ( chroma_planes 1185 ? 2 : 0 ) ]" value is "slice_pixel_width". 1186 If "chroma_planes" is set to 1, "plane_pixel_width[ 1 ]" and 1187 "plane_pixel_width[ 2 ]" value is "ceil(slice_pixel_width / (1 << 1188 log2_h_chroma_subsample))". 1190 4.6.2. slice_pixel_width 1192 "slice_pixel_width" is the width in pixels of the slice. 1193 Its value is "floor(( slice_x + slice_width ) * slice_pixel_width / 1194 num_h_slices) - slice_pixel_x". 1196 4.6.3. slice_pixel_x 1198 "slice_pixel_x" is the slice horizontal position in pixels. 1199 Its value is "floor(slice_x * frame_pixel_width / num_h_slices)". 1201 4.6.4. sample_difference 1203 "sample_difference[ p ][ y ][ x ]" is the sample difference for 1204 sample at plane "p", y position "y", and x position "x". The sample 1205 value is computed based on prediction and context described in 1206 Section 3.2. 1208 4.7. Slice Footer 1210 Note: slice footer is always byte aligned. 1212 pseudo-code | type 1213 --------------------------------------------------------------|----- 1214 SliceFooter( ) { | 1215 slice_size | u(24) 1216 if (ec) { | 1217 error_status | u(8) 1218 slice_crc_parity | u(32) 1219 } | 1220 } | 1222 4.7.1. slice_size 1224 "slice_size" indicates the size of the slice in bytes. 1225 Note: this allows finding the start of slices before previous slices 1226 have been fully decoded. And allows this way parallel decoding as 1227 well as error resilience. 1229 4.7.2. error_status 1231 "error_status" specifies the error status. 1233 +-------+--------------------------------------+ 1234 | value | error status | 1235 +-------+--------------------------------------+ 1236 | 0 | no error | 1237 | 1 | slice contains a correctable error | 1238 | 2 | slice contains a uncorrectable error | 1239 | Other | reserved for future use | 1240 +-------+--------------------------------------+ 1242 4.7.3. slice_crc_parity 1244 "slice_crc_parity" 32 bits that are chosen so that the slice as a 1245 whole has a crc remainder of 0. 1246 This is equivalent to storing the crc remainder in the 32-bit parity. 1247 The CRC generator polynomial used is the standard IEEE CRC polynomial 1248 (0x104C11DB7) with initial value 0. 1250 4.8. Parameters 1251 pseudo-code | type 1252 --------------------------------------------------------------|----- 1253 Parameters( ) { | 1254 version | ur 1255 if (version >= 3) | 1256 micro_version | ur 1257 coder_type | ur 1258 if (coder_type > 1) | 1259 for (i = 1; i < 256; i++) | 1260 state_transition_delta[ i ] | sr 1261 colorspace_type | ur 1262 if (version >= 1) | 1263 bits_per_raw_sample | ur 1264 chroma_planes | br 1265 log2_h_chroma_subsample | ur 1266 log2_v_chroma_subsample | ur 1267 alpha_plane | br 1268 if (version >= 3) { | 1269 num_h_slices - 1 | ur 1270 num_v_slices - 1 | ur 1271 quant_table_set_count | ur 1272 } | 1273 for( i = 0; i < quant_table_set_count; i++ ) | 1274 QuantizationTableSet( i ) | 1275 if (version >= 3) { | 1276 for( i = 0; i < quant_table_set_count; i++ ) { | 1277 states_coded | br 1278 if (states_coded) | 1279 for( j = 0; j < context_count[ i ]; j++ ) | 1280 for( k = 0; k < CONTEXT_SIZE; k++ ) | 1281 initial_state_delta[ i ][ j ][ k ] | sr 1282 } | 1283 ec | ur 1284 intra | ur 1285 } | 1286 } | 1288 4.8.1. version 1290 "version" specifies the version of the FFV1 bitstream. 1291 Each version is incompatible with others versions: decoders SHOULD 1292 reject a file due to unknown version. 1293 Decoders SHOULD reject a file with version <= 1 && 1294 ConfigurationRecordIsPresent == 1. 1295 Decoders SHOULD reject a file with version >= 3 && 1296 ConfigurationRecordIsPresent == 0. 1298 +-------+-------------------------+ 1299 | value | version | 1300 +-------+-------------------------+ 1301 | 0 | FFV1 version 0 | 1302 | 1 | FFV1 version 1 | 1303 | 2 | reserved* | 1304 | 3 | FFV1 version 3 | 1305 | Other | reserved for future use | 1306 +-------+-------------------------+ 1308 * Version 2 was never enabled in the encoder thus version 2 files 1309 SHOULD NOT exist, and this document does not describe them to keep 1310 the text simpler. 1312 4.8.2. micro_version 1314 "micro_version" specifies the micro-version of the FFV1 bitstream. 1315 After a version is considered stable (a micro-version value is 1316 assigned to be the first stable variant of a specific version), each 1317 new micro-version after this first stable variant is compatible with 1318 the previous micro-version: decoders SHOULD NOT reject a file due to 1319 an unknown micro-version equal or above the micro-version considered 1320 as stable. 1322 Meaning of micro_version for version 3: 1324 +-------+-------------------------+ 1325 | value | micro_version | 1326 +-------+-------------------------+ 1327 | 0...3 | reserved* | 1328 | 4 | first stable variant | 1329 | Other | reserved for future use | 1330 +-------+-------------------------+ 1332 * development versions may be incompatible with the stable variants. 1334 4.8.3. coder_type 1336 "coder_type" specifies the coder used. 1338 +-------+-------------------------------------------------+ 1339 | value | coder used | 1340 +-------+-------------------------------------------------+ 1341 | 0 | Golomb Rice | 1342 | 1 | Range Coder with default state transition table | 1343 | 2 | Range Coder with custom state transition table | 1344 | Other | reserved for future use | 1345 +-------+-------------------------------------------------+ 1347 4.8.4. state_transition_delta 1349 "state_transition_delta" specifies the Range coder custom state 1350 transition table. 1351 If state_transition_delta is not present in the FFV1 bitstream, all 1352 Range coder custom state transition table elements are assumed to be 1353 0. 1355 4.8.5. colorspace_type 1357 "colorspace_type" specifies color space losslessly encoded, Pixel 1358 transformation used by the encoder, as well as interleave method. 1360 +-------+---------------------+------------------+------------------+ 1361 | value | color space | transformation | interleave | 1362 | | losslessly encoded | | method | 1363 +-------+---------------------+------------------+------------------+ 1364 | 0 | YCbCr | No Pixel | plane then line | 1365 | | | transformation | | 1366 | 1 | RGB | JPEG2000-RCT | line then plane | 1367 | Other | reserved for future | reserved for | reserved for | 1368 | | use | future use | future use | 1369 +-------+---------------------+------------------+------------------+ 1371 Restrictions: 1372 If "colorspace_type" is 1, then "chroma_planes" MUST be 1, 1373 "log2_h_chroma_subsample" MUST be 0, and "log2_v_chroma_subsample" 1374 MUST be 0. 1376 4.8.6. chroma_planes 1378 "chroma_planes" indicates if chroma (color) planes are present. 1380 +-------+-------------------------------+ 1381 | value | presence | 1382 +-------+-------------------------------+ 1383 | 0 | chroma planes are not present | 1384 | 1 | chroma planes are present | 1385 +-------+-------------------------------+ 1387 4.8.7. bits_per_raw_sample 1389 "bits_per_raw_sample" indicates the number of bits for each sample. 1390 Inferred to be 8 if not present. 1392 +-------+---------------------------------+ 1393 | value | bits for each sample | 1394 +-------+---------------------------------+ 1395 | 0 | reserved* | 1396 | Other | the actual bits for each sample | 1397 +-------+---------------------------------+ 1399 * Encoders MUST NOT store bits_per_raw_sample = 0 Decoders SHOULD 1400 accept and interpret bits_per_raw_sample = 0 as 8. 1402 4.8.8. log2_h_chroma_subsample 1404 "log2_h_chroma_subsample" indicates the subsample factor, stored in 1405 powers to which the number 2 must be raised, between luma and chroma 1406 width ("chroma_width = 2^(-log2_h_chroma_subsample) * luma_width"). 1408 4.8.9. log2_v_chroma_subsample 1410 "log2_v_chroma_subsample" indicates the subsample factor, stored in 1411 powers to which the number 2 must be raised, between luma and chroma 1412 height ("chroma_height=2^(-log2_v_chroma_subsample) * luma_height"). 1414 4.8.10. alpha_plane 1416 "alpha_plane" indicates if a transparency plane is present. 1418 +-------+-----------------------------------+ 1419 | value | presence | 1420 +-------+-----------------------------------+ 1421 | 0 | transparency plane is not present | 1422 | 1 | transparency plane is present | 1423 +-------+-----------------------------------+ 1425 4.8.11. num_h_slices 1427 "num_h_slices" indicates the number of horizontal elements of the 1428 slice raster. 1429 Inferred to be 1 if not present. 1431 4.8.12. num_v_slices 1433 "num_v_slices" indicates the number of vertical elements of the slice 1434 raster. 1435 Inferred to be 1 if not present. 1437 4.8.13. quant_table_set_count 1439 "quant_table_set_count" indicates the number of Quantization 1440 Table Sets. 1441 Inferred to be 1 if not present. 1442 MUST NOT be 0. 1444 4.8.14. states_coded 1446 "states_coded" indicates if the respective Quantization Table Set has 1447 the initial states coded. 1448 Inferred to be 0 if not present. 1450 +-------+-----------------------------------------------------------+ 1451 | value | initial states | 1452 +-------+-----------------------------------------------------------+ 1453 | 0 | initial states are not present and are assumed to be all | 1454 | | 128 | 1455 | 1 | initial states are present | 1456 +-------+-----------------------------------------------------------+ 1458 4.8.15. initial_state_delta 1460 "initial_state_delta[ i ][ j ][ k ]" indicates the initial Range 1461 coder state, it is encoded using "k" as context index and 1463 pred = j ? initial_states[ i ][j - 1][ k ] : 128 1465 initial_state[ i ][ j ][ k ] = 1466 ( pred + initial_state_delta[ i ][ j ][ k ] ) & 255 1468 4.8.16. ec 1470 "ec" indicates the error detection/correction type. 1472 +-------+--------------------------------------------+ 1473 | value | error detection/correction type | 1474 +-------+--------------------------------------------+ 1475 | 0 | 32-bit CRC on the global header | 1476 | 1 | 32-bit CRC per slice and the global header | 1477 | Other | reserved for future use | 1478 +-------+--------------------------------------------+ 1480 4.8.17. intra 1482 "intra" indicates the relationship between the instances of "Frame". 1483 Inferred to be 0 if not present. 1485 +-------+-----------------------------------------------------------+ 1486 | value | relationship | 1487 +-------+-----------------------------------------------------------+ 1488 | 0 | Frames are independent or dependent (keyframes and non | 1489 | | keyframes) | 1490 | 1 | Frames are independent (keyframes only) | 1491 | Other | reserved for future use | 1492 +-------+-----------------------------------------------------------+ 1494 4.9. Quantization Table Set 1496 The Quantization Table Sets are stored by storing the number of equal 1497 entries -1 of the first half of the table (represented as "len - 1" 1498 in the pseudo-code below) using the method described in 1499 Section 3.8.1.2. The second half doesn't need to be stored as it is 1500 identical to the first with flipped sign. "scale" and "len_count[ i 1501 ][ j ]" are temporary values used for the computing of 1502 "context_count[ i ]" and are not used outside Quantization Table Set 1503 pseudo-code. 1505 example: 1507 Table: 0 0 1 1 1 1 2 2 -2 -2 -2 -1 -1 -1 -1 0 1509 Stored values: 1, 3, 1 1511 pseudo-code | type 1512 --------------------------------------------------------------|----- 1513 QuantizationTableSet( i ) { | 1514 scale = 1 | 1515 for( j = 0; j < MAX_CONTEXT_INPUTS; j++ ) { | 1516 QuantizationTable( i, j, scale ) | 1517 scale *= 2 * len_count[ i ][ j ] - 1 | 1518 } | 1519 context_count[ i ] = ceil ( scale / 2 ) | 1520 } | 1522 MAX_CONTEXT_INPUTS is 5. 1524 pseudo-code | type 1525 --------------------------------------------------------------|----- 1526 QuantizationTable(i, j, scale) { | 1527 v = 0 | 1528 for( k = 0; k < 128; ) { | 1529 len - 1 | ur 1530 for( a = 0; a < len; a++ ) { | 1531 quant_tables[ i ][ j ][ k ] = scale* v | 1532 k++ | 1533 } | 1534 v++ | 1535 } | 1536 for( k = 1; k < 128; k++ ) { | 1537 quant_tables[ i ][ j ][ 256 - k ] = \ | 1538 -quant_tables[ i ][ j ][ k ] | 1539 } | 1540 quant_tables[ i ][ j ][ 128 ] = \ | 1541 -quant_tables[ i ][ j ][ 127 ] | 1542 len_count[ i ][ j ] = v | 1543 } | 1545 4.9.1. quant_tables 1547 "quant_tables[ i ][ j ][ k ]" indicates the quantification table 1548 value of the Quantized Sample Difference "k" of the Quantization 1549 Table "j" of the Set Quantization Table Set "i". 1551 4.9.2. context_count 1553 "context_count[ i ]" indicates the count of contexts for Quantization 1554 Table Set "i". 1556 5. Restrictions 1558 To ensure that fast multithreaded decoding is possible, starting 1559 version 3 and if frame_pixel_width * frame_pixel_height is more than 1560 101376, slice_width * slice_height MUST be less or equal to 1561 num_h_slices * num_v_slices / 4. Note: 101376 is the frame size in 1562 pixels of a 352x288 frame also known as CIF ("Common Intermediate 1563 Format") frame size format. 1565 For each "Frame", each position in the slice raster MUST be filled by 1566 one and only one slice of the "Frame" (no missing slice position, no 1567 slice overlapping). 1569 For each "Frame" with keyframe value of 0, each slice MUST have the 1570 same value of slice_x, slice_y, slice_width, slice_height as a slice 1571 in the previous "Frame". 1573 6. Security Considerations 1575 Like any other codec, (such as [RFC6716]), FFV1 should not be used 1576 with insecure ciphers or cipher-modes that are vulnerable to known 1577 plaintext attacks. Some of the header bits as well as the padding 1578 are easily predictable. 1580 Implementations of the FFV1 codec need to take appropriate security 1581 considerations into account, as outlined in [RFC4732]. It is 1582 extremely important for the decoder to be robust against malicious 1583 payloads. Malicious payloads must not cause the decoder to overrun 1584 its allocated memory or to take an excessive amount of resources to 1585 decode. Although problems in encoders are typically rarer, the same 1586 applies to the encoder. Malicious video streams must not cause the 1587 encoder to misbehave because this would allow an attacker to attack 1588 transcoding gateways. A frequent security problem in image and video 1589 codecs is also to not check for integer overflows in Pixel count 1590 computations, that is to allocate width * height without considering 1591 that the multiplication result may have overflowed the arithmetic 1592 types range. 1594 The reference implementation [REFIMPL] contains no known buffer 1595 overflow or cases where a specially crafted packet or video segment 1596 could cause a significant increase in CPU load. 1598 The reference implementation [REFIMPL] was validated in the following 1599 conditions: 1601 o Sending the decoder valid packets generated by the reference 1602 encoder and verifying that the decoder's output matches the 1603 encoder's input. 1605 o Sending the decoder packets generated by the reference encoder and 1606 then subjected to random corruption. 1608 o Sending the decoder random packets that are not FFV1. 1610 In all of the conditions above, the decoder and encoder was run 1611 inside the [VALGRIND] memory debugger as well as clangs address 1612 sanitizer [Address-Sanitizer], which track reads and writes to 1613 invalid memory regions as well as the use of uninitialized memory. 1614 There were no errors reported on any of the tested conditions. 1616 7. Media Type Definition 1618 This registration is done using the template defined in [RFC6838] and 1619 following [RFC4855]. 1621 Type name: video 1623 Subtype name: FFV1 1625 Required parameters: None. 1627 Optional parameters: 1629 This parameter is used to signal the capabilities of a receiver 1630 implementation. This parameter MUST NOT be used for any other 1631 purpose. 1633 version: The version of the FFV1 encoding as defined by 1634 Section 4.8.1. 1636 micro_version: The micro_version of the FFV1 encoding as defined by 1637 Section 4.8.2. 1639 coder_type: The coder_type of the FFV1 encoding as defined by 1640 Section 4.8.3. 1642 colorspace_type: The colorspace_type of the FFV1 encoding as defined 1643 by Section 4.8.5. 1645 bits_per_raw_sample: The version of the FFV1 encoding as defined by 1646 Section 4.8.7. 1648 max-slices: The value of max-slices is an integer indicating the 1649 maximum count of slices with a frames of the FFV1 encoding. 1651 Encoding considerations: 1653 This media type is defined for encapsulation in several audiovisual 1654 container formats and contains binary data; see Section 4.1.3. This 1655 media type is framed binary data Section 4.8 of [RFC4288]. 1657 Security considerations: 1659 See Section 6 of this document. 1661 Interoperability considerations: None. 1663 Published specification: 1665 [I-D.ietf-cellar-ffv1] and RFC XXXX. 1667 [RFC Editor: Upon publication as an RFC, please replace "XXXX" with 1668 the number assigned to this document and remove this note.] 1669 Applications which use this media type: 1671 Any application that requires the transport of lossless video can use 1672 this media type. Some examples are, but not limited to screen 1673 recording, scientific imaging, and digital video preservation. 1675 Fragment identifier considerations: N/A. 1677 Additional information: None. 1679 Person & email address to contact for further information: Michael 1680 Niedermayer 1682 Intended usage: COMMON 1684 Restrictions on usage: None. 1686 Author: Dave Rice 1688 Change controller: IETF cellar working group delegated from the IESG. 1690 8. IANA Considerations 1692 The IANA is requested to register the following values: 1694 o Media type registration as described in Section 7. 1696 9. Appendixes 1698 9.1. Decoder implementation suggestions 1700 9.1.1. Multi-threading support and independence of slices 1702 The FFV1 bitstream is parsable in two ways: in sequential order as 1703 described in this document or with the pre-analysis of the footer of 1704 each slice. Each slice footer contains a slice_size field so the 1705 boundary of each slice is computable without having to parse the 1706 slice content. That allows multi-threading as well as independence 1707 of slice content (a bitstream error in a slice header or slice 1708 content has no impact on the decoding of the other slices). 1710 After having checked keyframe field, a decoder SHOULD parse 1711 slice_size fields, from slice_size of the last slice at the end of 1712 the "Frame" up to slice_size of the first slice at the beginning of 1713 the "Frame", before parsing slices, in order to have slices 1714 boundaries. A decoder MAY fallback on sequential order e.g. in case 1715 of a corrupted "Frame" (frame size unknown, slice_size of slices not 1716 coherent...) or if there is no possibility of seek into the stream. 1718 Architecture overview of slices in a "Frame": 1720 +-----------------------------------------------------------------+ 1721 | first slice header | 1722 | first slice content | 1723 | first slice footer | 1724 | --------------------------------------------------------------- | 1725 | second slice header | 1726 | second slice content | 1727 | second slice footer | 1728 | --------------------------------------------------------------- | 1729 | ... | 1730 | --------------------------------------------------------------- | 1731 | last slice header | 1732 | last slice content | 1733 | last slice footer | 1734 +-----------------------------------------------------------------+ 1736 10. Changelog 1738 See 1740 11. ToDo 1742 o mean,k estimation for the Golomb Rice codes 1744 12. References 1746 12.1. Normative References 1748 [I-D.ietf-cellar-ffv1] 1749 Niedermayer, M., Rice, D., and J. Martinez, "FFV1 Video 1750 Coding Format Version 0, 1, and 3", draft-ietf-cellar- 1751 ffv1-02 (work in progress), April 2018. 1753 [ISO.15444-1.2016] 1754 International Organization for Standardization, 1755 "Information technology -- JPEG 2000 image coding system: 1756 Core coding system", October 2016. 1758 [ISO.9899.1990] 1759 International Organization for Standardization, 1760 "Programming languages - C", ISO Standard 9899, 1990. 1762 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1763 Requirement Levels", BCP 14, RFC 2119, 1764 DOI 10.17487/RFC2119, March 1997, 1765 . 1767 [RFC4288] Freed, N. and J. Klensin, "Media Type Specifications and 1768 Registration Procedures", RFC 4288, DOI 10.17487/RFC4288, 1769 December 2005, . 1771 [RFC4855] Casner, S., "Media Type Registration of RTP Payload 1772 Formats", RFC 4855, DOI 10.17487/RFC4855, February 2007, 1773 . 1775 [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type 1776 Specifications and Registration Procedures", BCP 13, 1777 RFC 6838, DOI 10.17487/RFC6838, January 2013, 1778 . 1780 12.2. Informative References 1782 [Address-Sanitizer] 1783 The Clang Team, "ASAN AddressSanitizer website", undated, 1784 . 1786 [AVI] Microsoft, "AVI RIFF File Reference", undated, 1787 . 1790 [FFV1_V0] Niedermayer, M., "Commit to mark FFV1 version 0 as non- 1791 experimental", April 2006, . 1795 [FFV1_V1] Niedermayer, M., "Commit to release FFV1 version 1", April 1796 2009, . 1799 [FFV1_V3] Niedermayer, M., "Commit to mark FFV1 version 3 as non- 1800 experimental", August 2013, . 1804 [HuffYUV] Rudiak-Gould, B., "HuffYUV", December 2003, 1805 . 1808 [ISO.14495-1.1999] 1809 International Organization for Standardization, 1810 "Information technology -- Lossless and near-lossless 1811 compression of continuous-tone still images: Baseline", 1812 December 1999. 1814 [ISO.14496-10.2014] 1815 International Organization for Standardization, 1816 "Information technology -- Coding of audio-visual objects 1817 -- Part 10: Advanced Video Coding", September 2014. 1819 [ISO.14496-12.2015] 1820 International Organization for Standardization, 1821 "Information technology -- Coding of audio-visual objects 1822 -- Part 12: ISO base media file format", December 2015. 1824 [Matroska] 1825 IETF, "Matroska", 2016, . 1828 [NUT] Niedermayer, M., "NUT Open Container Format", December 1829 2013, . 1831 [range-coding] 1832 Nigel, G. and N. Martin, "Range encoding: an algorithm for 1833 removing redundancy from a digitised message.", Proc. 1834 Institution of Electronic and Radio Engineers 1835 International Conference on Video and Data Recording , 1836 July 1979. 1838 [REFIMPL] Niedermayer, M., "The reference FFV1 implementation / the 1839 FFV1 codec in FFmpeg", undated, . 1841 [RFC4732] Handley, M., Ed., Rescorla, E., Ed., and IAB, "Internet 1842 Denial-of-Service Considerations", RFC 4732, 1843 DOI 10.17487/RFC4732, December 2006, 1844 . 1846 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 1847 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716, 1848 September 2012, . 1850 [VALGRIND] 1851 Valgrind Developers, "Valgrind website", undated, 1852 . 1854 [YCbCr] Wikipedia, "YCbCr", undated, 1855 . 1857 Authors' Addresses 1859 Michael Niedermayer 1861 Email: michael@niedermayer.cc 1862 Dave Rice 1864 Email: dave@dericed.com 1866 Jerome Martinez 1868 Email: jerome@mediaarea.net