idnits 2.17.1
draft-ietf-cellar-ffv1-v4-19.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:
----------------------------------------------------------------------------
== There are 2 instances of lines with non-ascii characters in the document.
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 (31 May 2022) is 694 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: '41' on line 1150
** Downref: Normative reference to an Informational RFC: RFC 4732
== Outdated reference: A later version (-21) exists of
draft-ietf-cellar-matroska-10
Summary: 1 error (**), 0 flaws (~~), 3 warnings (==), 2 comments (--).
Run idnits with the --verbose option for more detailed information about
the items above.
--------------------------------------------------------------------------------
2 cellar M. Niedermayer
3 Internet-Draft
4 Intended status: Standards Track D. Rice
5 Expires: 2 December 2022
6 J. Martinez
7 31 May 2022
9 FFV1 Video Coding Format Version 4
10 draft-ietf-cellar-ffv1-v4-19
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 2 December 2022.
37 Copyright Notice
39 Copyright (c) 2022 IETF Trust and the persons identified as the
40 document authors. All rights reserved.
42 This document is subject to BCP 78 and the IETF Trust's Legal
43 Provisions Relating to IETF Documents (https://trustee.ietf.org/
44 license-info) in effect on the date of publication of this document.
45 Please review these documents carefully, as they describe your rights
46 and restrictions with respect to this document. Code Components
47 extracted from this document must include Revised BSD License text as
48 described in Section 4.e of the Trust Legal Provisions and are
49 provided without warranty as described in the Revised BSD License.
51 Table of Contents
53 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 4
54 2. Notation and Conventions . . . . . . . . . . . . . . . . . . 4
55 2.1. Definitions . . . . . . . . . . . . . . . . . . . . . . . 4
56 2.2. Conventions . . . . . . . . . . . . . . . . . . . . . . . 5
57 2.2.1. Pseudocode . . . . . . . . . . . . . . . . . . . . . 5
58 2.2.2. Arithmetic Operators . . . . . . . . . . . . . . . . 6
59 2.2.3. Assignment Operators . . . . . . . . . . . . . . . . 6
60 2.2.4. Comparison Operators . . . . . . . . . . . . . . . . 7
61 2.2.5. Mathematical Functions . . . . . . . . . . . . . . . 7
62 2.2.6. Order of Operation Precedence . . . . . . . . . . . . 8
63 2.2.7. Range . . . . . . . . . . . . . . . . . . . . . . . . 8
64 2.2.8. NumBytes . . . . . . . . . . . . . . . . . . . . . . 8
65 2.2.9. Bitstream Functions . . . . . . . . . . . . . . . . . 8
66 3. Sample Coding . . . . . . . . . . . . . . . . . . . . . . . . 9
67 3.1. Border . . . . . . . . . . . . . . . . . . . . . . . . . 9
68 3.2. Samples . . . . . . . . . . . . . . . . . . . . . . . . . 10
69 3.3. Median Predictor . . . . . . . . . . . . . . . . . . . . 10
70 3.3.1. Exception . . . . . . . . . . . . . . . . . . . . . . 11
71 3.4. Quantization Table Sets . . . . . . . . . . . . . . . . . 11
72 3.5. Context . . . . . . . . . . . . . . . . . . . . . . . . . 12
73 3.6. Quantization Table Set Indexes . . . . . . . . . . . . . 12
74 3.7. Color Spaces . . . . . . . . . . . . . . . . . . . . . . 12
75 3.7.1. YCbCr . . . . . . . . . . . . . . . . . . . . . . . . 13
76 3.7.2. RGB . . . . . . . . . . . . . . . . . . . . . . . . . 13
77 3.8. Coding of the Sample Difference . . . . . . . . . . . . . 15
78 3.8.1. Range Coding Mode . . . . . . . . . . . . . . . . . . 15
79 3.8.2. Golomb Rice Mode . . . . . . . . . . . . . . . . . . 23
80 4. Bitstream . . . . . . . . . . . . . . . . . . . . . . . . . . 29
81 4.1. Quantization Table Set . . . . . . . . . . . . . . . . . 30
82 4.1.1. quant_tables . . . . . . . . . . . . . . . . . . . . 31
83 4.1.2. context_count . . . . . . . . . . . . . . . . . . . . 31
84 4.2. Parameters . . . . . . . . . . . . . . . . . . . . . . . 31
85 4.2.1. version . . . . . . . . . . . . . . . . . . . . . . . 33
86 4.2.2. micro_version . . . . . . . . . . . . . . . . . . . . 33
87 4.2.3. coder_type . . . . . . . . . . . . . . . . . . . . . 34
88 4.2.4. state_transition_delta . . . . . . . . . . . . . . . 35
89 4.2.5. colorspace_type . . . . . . . . . . . . . . . . . . . 35
90 4.2.6. chroma_planes . . . . . . . . . . . . . . . . . . . . 36
91 4.2.7. bits_per_raw_sample . . . . . . . . . . . . . . . . . 36
92 4.2.8. log2_h_chroma_subsample . . . . . . . . . . . . . . . 37
93 4.2.9. log2_v_chroma_subsample . . . . . . . . . . . . . . . 37
94 4.2.10. extra_plane . . . . . . . . . . . . . . . . . . . . . 37
95 4.2.11. num_h_slices . . . . . . . . . . . . . . . . . . . . 37
96 4.2.12. num_v_slices . . . . . . . . . . . . . . . . . . . . 38
97 4.2.13. quant_table_set_count . . . . . . . . . . . . . . . . 38
98 4.2.14. states_coded . . . . . . . . . . . . . . . . . . . . 38
99 4.2.15. initial_state_delta . . . . . . . . . . . . . . . . . 38
100 4.2.16. ec . . . . . . . . . . . . . . . . . . . . . . . . . 39
101 4.2.17. intra . . . . . . . . . . . . . . . . . . . . . . . . 39
102 4.3. Configuration Record . . . . . . . . . . . . . . . . . . 39
103 4.3.1. reserved_for_future_use . . . . . . . . . . . . . . . 40
104 4.3.2. configuration_record_crc_parity . . . . . . . . . . . 40
105 4.3.3. Mapping FFV1 into Containers . . . . . . . . . . . . 40
106 4.4. Frame . . . . . . . . . . . . . . . . . . . . . . . . . . 41
107 4.5. Slice . . . . . . . . . . . . . . . . . . . . . . . . . . 42
108 4.6. Slice Header . . . . . . . . . . . . . . . . . . . . . . 43
109 4.6.1. slice_x . . . . . . . . . . . . . . . . . . . . . . . 44
110 4.6.2. slice_y . . . . . . . . . . . . . . . . . . . . . . . 44
111 4.6.3. slice_width . . . . . . . . . . . . . . . . . . . . . 44
112 4.6.4. slice_height . . . . . . . . . . . . . . . . . . . . 44
113 4.6.5. quant_table_set_index_count . . . . . . . . . . . . . 45
114 4.6.6. quant_table_set_index . . . . . . . . . . . . . . . . 45
115 4.6.7. picture_structure . . . . . . . . . . . . . . . . . . 45
116 4.6.8. sar_num . . . . . . . . . . . . . . . . . . . . . . . 45
117 4.6.9. sar_den . . . . . . . . . . . . . . . . . . . . . . . 46
118 4.6.10. reset_contexts . . . . . . . . . . . . . . . . . . . 46
119 4.6.11. slice_coding_mode . . . . . . . . . . . . . . . . . . 46
120 4.7. Slice Content . . . . . . . . . . . . . . . . . . . . . . 46
121 4.7.1. primary_color_count . . . . . . . . . . . . . . . . . 47
122 4.7.2. plane_pixel_height . . . . . . . . . . . . . . . . . 47
123 4.7.3. slice_pixel_height . . . . . . . . . . . . . . . . . 47
124 4.7.4. slice_pixel_y . . . . . . . . . . . . . . . . . . . . 47
125 4.8. Line . . . . . . . . . . . . . . . . . . . . . . . . . . 48
126 4.8.1. plane_pixel_width . . . . . . . . . . . . . . . . . . 48
127 4.8.2. slice_pixel_width . . . . . . . . . . . . . . . . . . 48
128 4.8.3. slice_pixel_x . . . . . . . . . . . . . . . . . . . . 48
129 4.8.4. sample_difference . . . . . . . . . . . . . . . . . . 49
130 4.9. Slice Footer . . . . . . . . . . . . . . . . . . . . . . 49
131 4.9.1. slice_size . . . . . . . . . . . . . . . . . . . . . 49
132 4.9.2. error_status . . . . . . . . . . . . . . . . . . . . 49
133 4.9.3. slice_crc_parity . . . . . . . . . . . . . . . . . . 50
134 5. Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 50
135 6. Security Considerations . . . . . . . . . . . . . . . . . . . 51
136 7. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 51
137 7.1. Media Type Definition . . . . . . . . . . . . . . . . . . 51
139 8. Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . 52
140 9. Normative References . . . . . . . . . . . . . . . . . . . . 53
141 10. Informative References . . . . . . . . . . . . . . . . . . . 53
142 Appendix A. Multithreaded Decoder Implementation Suggestions . . 55
143 Appendix B. Future Handling of Some Streams Created by
144 Nonconforming Encoders . . . . . . . . . . . . . . . . . 55
145 Appendix C. FFV1 Implementations . . . . . . . . . . . . . . . . 56
146 C.1. FFmpeg FFV1 Codec . . . . . . . . . . . . . . . . . . . . 56
147 C.2. FFV1 Decoder in Go . . . . . . . . . . . . . . . . . . . 56
148 C.3. MediaConch . . . . . . . . . . . . . . . . . . . . . . . 56
149 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 57
151 1. Introduction
153 This document describes FFV1, a lossless video encoding format. The
154 design of FFV1 considers the storage of image characteristics, data
155 fixity, and the optimized use of encoding time and storage
156 requirements. FFV1 is designed to support a wide range of lossless
157 video applications such as long-term audiovisual preservation,
158 scientific imaging, screen recording, and other video encoding
159 scenarios that seek to avoid the generational loss of lossy video
160 encodings.
162 This document defines a version 4 of FFV1. Prior versions of FFV1
163 are defined within [I-D.ietf-cellar-ffv1].
165 This document assumes familiarity with mathematical and coding
166 concepts such as Range encoding [Range-Encoding] and YCbCr color
167 spaces [YCbCr].
169 This specification describes the valid bitstream and how to decode
170 it. Nonconformant bitstreams and the nonconformant handling of
171 bitstreams are outside this specification. A decoder can perform any
172 action that it deems appropriate for an invalid bitstream: reject the
173 bitstream, attempt to perform error concealment, or re-download or
174 use a redundant copy of the invalid part.
176 2. Notation and Conventions
178 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
179 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
180 "OPTIONAL" in this document are to be interpreted as described in BCP
181 14 [RFC2119] [RFC8174] when, and only when, they appear in all
182 capitals, as shown here.
184 2.1. Definitions
186 FFV1: The chosen name of this video encoding format, which is the
187 short version of "FF Video 1". The letters "FF" come from
188 "FFmpeg", which is the name of the reference decoder whose first
189 letters originally meant "Fast Forward".
190 Container: A format that encapsulates Frames (see Section 4.4) and
191 (when required) a Configuration Record into a bitstream.
192 Sample: The smallest addressable representation of a color component
193 or a luma component in a Frame. Examples of Sample are Luma (Y),
194 Blue-difference Chroma (Cb), Red-difference Chroma (Cr),
195 Transparency, Red, Green, and Blue.
196 Symbol: A value stored in the bitstream, which is defined and
197 decoded through one of the methods described in Table 4.
198 Line: A discrete component of a static image composed of Samples
199 that represent a specific quantification of Samples of that image.
200 Plane: A discrete component of a static image composed of Lines that
201 represent a specific quantification of Lines of that image.
202 Pixel: The smallest addressable representation of a color in a
203 Frame. It is composed of one or more Samples.
204 MSB: Most Significant Bit, the bit that can cause the largest change
205 in magnitude of the symbol.
206 VLC: Variable Length Code, a code that maps source symbols to a
207 variable number of bits.
208 RGB: A reference to the method of storing the value of a pixel by
209 using three numeric values that represent Red, Green, and Blue.
210 YCbCr: A reference to the method of storing the value of a pixel by
211 using three numeric values that represent the luma of the pixel
212 (Y) and the chroma of the pixel (Cb and Cr). The term YCbCr is
213 used for historical reasons and currently references any color
214 space relying on one luma Sample and two chroma Samples, e.g.,
215 YCbCr (luma, blue-difference chroma, red-difference chroma),
216 YCgCo, or ICtCp (intensity, blue-yellow, red-green).
217 TBA: To Be Announced. Used in reference to the development of
218 future iterations of the FFV1 specification.
220 2.2. Conventions
222 2.2.1. Pseudocode
224 The FFV1 bitstream is described in this document using pseudocode.
225 Note that the pseudocode is used to illustrate the structure of FFV1
226 and is not intended to specify any particular implementation. The
227 pseudocode used is based upon the C programming language
228 [ISO.9899.2018] and uses its if/else, while, and for keywords as well
229 as functions defined within this document.
231 In some instances, pseudocode is presented in a two-column format
232 such as shown in Figure 1. In this form, the type column provides a
233 symbol as defined in Table 4 that defines the storage of the data
234 referenced in that same line of pseudocode.
236 pseudocode | type
237 --------------------------------------------------------------|-----
238 ExamplePseudoCode( ) { |
239 value | ur
240 } |
242 Figure 1: A depiction of type-labeled pseudocode used within this
243 document.
245 2.2.2. Arithmetic Operators
247 Note: the operators and the order of precedence are the same as used
248 in the C programming language [ISO.9899.2018], with the exception of
249 >> (removal of implementation-defined behavior) and ^ (power instead
250 of XOR) operators, which are redefined within this section.
252 a + b means a plus b.
254 a - b means a minus b.
256 -a means negation of a.
258 a * b means a multiplied by b.
260 a / b means a divided by b.
262 a ^ b means a raised to the b-th power.
264 a & b means bitwise "and" of a and b.
266 a | b means bitwise "or" of a and b.
268 a >> b means arithmetic right shift of the two's complement integer
269 representation of a by b binary digits. This is equivalent to
270 dividing a by 2, b times, with rounding toward negative infinity.
272 a << b means arithmetic left shift of the two's complement integer
273 representation of a by b binary digits.
275 2.2.3. Assignment Operators
277 a = b means a is assigned b.
279 a++ is equivalent to a is assigned a + 1.
281 a-- is equivalent to a is assigned a - 1.
283 a += b is equivalent to a is assigned a + b.
285 a -= b is equivalent to a is assigned a - b.
287 a *= b is equivalent to a is assigned a * b.
289 2.2.4. Comparison Operators
291 a > b is true when a is greater than b.
293 a >= b is true when a is greater than or equal to b.
295 a < b is true when a is less than b.
297 a <= b is true when a is less than or equal b.
299 a == b is true when a is equal to b.
301 a != b is true when a is not equal to b.
303 a && b is true when both a is true and b is true.
305 a || b is true when either a is true or b is true.
307 !a is true when a is not true.
309 a ? b : c if a is true, then b, otherwise c.
311 2.2.5. Mathematical Functions
313 floor(a) means the largest integer less than or equal to a.
315 ceil(a) means the smallest integer greater than or equal to a.
317 sign(a) extracts the sign of a number, i.e., if a < 0 then -1, else
318 if a > 0 then 1, else 0.
320 abs(a) means the absolute value of a, i.e., abs(a) = sign(a) * a.
322 log2(a) means the base-two logarithm of a.
324 min(a,b) means the smaller of two values a and b.
326 max(a,b) means the larger of two values a and b.
328 median(a,b,c) means the numerical middle value in a data set of a, b,
329 and c, i.e., a+b+c-min(a,b,c)-max(a,b,c).
331 a ==> b means a implies b.
333 a <==> b means a ==> b , b ==> a.
335 a_b means the b-th value of a sequence of a.
337 a_(b,c) means the 'b,c'-th value of a sequence of a.
339 2.2.6. Order of Operation Precedence
341 When order of precedence is not indicated explicitly by use of
342 parentheses, operations are evaluated in the following order (from
343 top to bottom, operations of same precedence being evaluated from
344 left to right). This order of operations is based on the order of
345 operations used in Standard C.
347 a++, a--
348 !a, -a
349 a ^ b
350 a * b, a / b
351 a + b, a - b
352 a << b, a >> b
353 a < b, a <= b, a > b, a >= b
354 a == b, a != b
355 a & b
356 a | b
357 a && b
358 a || b
359 a ? b : c
360 a = b, a += b, a -= b, a *= b
362 2.2.7. Range
364 a...b means any value from a to b, inclusive.
366 2.2.8. NumBytes
368 NumBytes is a nonnegative integer that expresses the size in 8-bit
369 octets of a particular FFV1 Configuration Record or Frame. FFV1
370 relies on its container to store the NumBytes values; see
371 Section 4.3.3.
373 2.2.9. Bitstream Functions
374 2.2.9.1. remaining_bits_in_bitstream
376 remaining_bits_in_bitstream( NumBytes ) means the count of remaining
377 bits after the pointer in that Configuration Record or Frame. It is
378 computed from the NumBytes value multiplied by 8 minus the count of
379 bits of that Configuration Record or Frame already read by the
380 bitstream parser.
382 2.2.9.2. remaining_symbols_in_syntax
384 remaining_symbols_in_syntax( ) is true as long as the range coder has
385 not consumed all the given input bytes.
387 2.2.9.3. byte_aligned
389 byte_aligned( ) is true if remaining_bits_in_bitstream( NumBytes ) is
390 a multiple of 8, otherwise false.
392 2.2.9.4. get_bits
394 get_bits( i ) is the action to read the next i bits in the bitstream,
395 from most significant bit to least significant bit, and to return the
396 corresponding value. The pointer is increased by i.
398 3. Sample Coding
400 For each Slice (as described in Section 4.5) of a Frame, the Planes,
401 Lines, and Samples are coded in an order determined by the color
402 space (see Section 3.7). Each Sample is predicted by the median
403 predictor as described in Section 3.3 from other Samples within the
404 same Plane, and the difference is stored using the method described
405 in Section 3.8.
407 3.1. Border
409 A border is assumed for each coded Slice for the purpose of the
410 median predictor and context according to the following rules:
412 * One column of Samples to the left of the coded Slice is assumed as
413 identical to the Samples of the leftmost column of the coded Slice
414 shifted down by one row. The value of the topmost Sample of the
415 column of Samples to the left of the coded Slice is assumed to be
416 0.
417 * One column of Samples to the right of the coded Slice is assumed
418 as identical to the Samples of the rightmost column of the coded
419 Slice.
420 * An additional column of Samples to the left of the coded Slice and
421 two rows of Samples above the coded Slice are assumed to be 0.
423 Figure 2 depicts a Slice of nine Samples a,b,c,d,e,f,g,h,i in a
424 three-by-three arrangement along with its assumed border.
426 +---+---+---+---+---+---+---+---+
427 | 0 | 0 | | 0 | 0 | 0 | | 0 |
428 +---+---+---+---+---+---+---+---+
429 | 0 | 0 | | 0 | 0 | 0 | | 0 |
430 +---+---+---+---+---+---+---+---+
431 | | | | | | | | |
432 +---+---+---+---+---+---+---+---+
433 | 0 | 0 | | a | b | c | | c |
434 +---+---+---+---+---+---+---+---+
435 | 0 | a | | d | e | f | | f |
436 +---+---+---+---+---+---+---+---+
437 | 0 | d | | g | h | i | | i |
438 +---+---+---+---+---+---+---+---+
440 Figure 2: A depiction of FFV1's assumed border for a set of
441 example Samples.
443 3.2. Samples
445 Relative to any Sample X, six other relatively positioned Samples
446 from the coded Samples and presumed border are identified according
447 to the labels used in Figure 3. The labels for these relatively
448 positioned Samples are used within the median predictor and context.
450 +---+---+---+---+
451 | | | T | |
452 +---+---+---+---+
453 | |tl | t |tr |
454 +---+---+---+---+
455 | L | l | X | |
456 +---+---+---+---+
458 Figure 3: A depiction of how relatively positioned Samples are
459 referenced within this document.
461 The labels for these relative Samples are made of the first letters
462 of the words Top, Left, and Right.
464 3.3. Median Predictor
466 The prediction for any Sample value at position X may be computed
467 based upon the relative neighboring values of l, t, and tl via this
468 equation:
470 median(l, t, l + t - tl)
471 Note that this prediction template is also used in [ISO.14495-1.1999]
472 and [HuffYUV].
474 3.3.1. Exception
476 If colorspace_type == 0 && bits_per_raw_sample == 16 && ( coder_type
477 == 1 || coder_type == 2 ) (see Section 4.2.5, Section 4.2.7, and
478 Section 4.2.3), the following median predictor MUST be used:
480 median(left16s, top16s, left16s + top16s - diag16s)
482 where:
484 left16s = l >= 32768 ? ( l - 65536 ) : l
485 top16s = t >= 32768 ? ( t - 65536 ) : t
486 diag16s = tl >= 32768 ? ( tl - 65536 ) : tl
488 Background: a two's complement 16-bit signed integer was used for
489 storing Sample values in all known implementations of FFV1 bitstream
490 (see Appendix C). So in some circumstances, the most significant bit
491 was wrongly interpreted (used as a sign bit instead of the 16th bit
492 of an unsigned integer). Note that when the issue was discovered,
493 the only impacted configuration of all known implementations was the
494 16-bit YCbCr with no pixel transformation and with the range coder
495 coder type, as the other potentially impacted configurations (e.g.,
496 the 15/16-bit JPEG 2000 Reversible Color Transform (RCT)
497 [ISO.15444-1.2019] with range coder or the 16-bit content with the
498 Golomb Rice coder type) were not implemented. Meanwhile, the 16-bit
499 JPEG 2000 RCT with range coder was deployed without this issue in one
500 implementation and validated by one conformance checker. It is
501 expected (to be confirmed) that this exception for the median
502 predictor will be removed in the next version of the FFV1 bitstream.
504 3.4. Quantization Table Sets
506 Quantization Tables are used on Sample Differences (see Section 3.8),
507 so Quantized Sample Differences are stored in the bitstream.
509 The FFV1 bitstream contains one or more Quantization Table Sets.
510 Each Quantization Table Set contains exactly five Quantization Tables
511 with each Quantization Table corresponding to one of the five
512 Quantized Sample Differences. For each Quantization Table, both the
513 number of quantization steps and their distribution are stored in the
514 FFV1 bitstream; each Quantization Table has exactly 256 entries, and
515 the eight least significant bits of the Quantized Sample Difference
516 are used as an index:
518 Q_(j)[k] = quant_tables[i][j][k&255]
519 Figure 4: Description of the mapping from sample differences to the
520 corresponding Quantized Sample Differences.
522 In this formula, i is the Quantization Table Set index, j is the
523 Quantized Table index, and k is the Quantized Sample Difference (see
524 Section 4.1.1).
526 3.5. Context
528 Relative to any Sample X, the Quantized Sample Differences L-l, l-tl,
529 tl-t, T-t, and t-tr are used as context:
531 context = Q_(0)[l - tl] +
532 Q_(1)[tl - t] +
533 Q_(2)[t - tr] +
534 Q_(3)[L - l] +
535 Q_(4)[T - t]
537 Figure 5: Description of the computing of the Context.
539 If context >= 0 then context is used, and the difference between the
540 Sample and its predicted value is encoded as is; else -context is
541 used, and the difference between the Sample and its predicted value
542 is encoded with a flipped sign.
544 3.6. Quantization Table Set Indexes
546 For each Plane of each Slice, a Quantization Table Set is selected
547 from an index:
549 * For Y Plane, quant_table_set_index[ 0 ] index is used.
550 * For Cb and Cr Planes, quant_table_set_index[ 1 ] index is used.
551 * For extra Plane, quant_table_set_index[ (version <= 3 ||
552 chroma_planes) ? 2 : 1 ] index is used.
554 Background: in the first implementations of the FFV1 bitstream, the
555 index for Cb and Cr Planes was stored even if it was not used
556 (chroma_planes set to 0), this index is kept for version <= 3 in
557 order to keep compatibility with FFV1 bitstreams in the wild.
559 3.7. Color Spaces
561 FFV1 supports several color spaces. The count of allowed coded
562 Planes and the meaning of the extra Plane are determined by the
563 selected color space.
565 The FFV1 bitstream interleaves data in an order determined by the
566 color space. In YCbCr for each Plane, each Line is coded from top to
567 bottom, and for each Line, each Sample is coded from left to right.
568 In JPEG 2000 RCT for each Line from top to bottom, each Plane is
569 coded, and for each Plane, each Sample is encoded from left to right.
571 3.7.1. YCbCr
573 This color space allows one to four Planes.
575 The Cb and Cr Planes are optional, but if they are used, then they
576 MUST be used together. Omitting the Cb and Cr Planes codes the
577 frames in gray scale without color data.
579 An optional transparency Plane can be used to code transparency data.
581 An FFV1 Frame using YCbCr MUST use one of the following arrangements:
583 * Y
584 * Y, Transparency
585 * Y, Cb, Cr
586 * Y, Cb, Cr, Transparency
588 The Y Plane MUST be coded first. If the Cb and Cr Planes are used,
589 then they MUST be coded after the Y Plane. If a transparency Plane
590 is used, then it MUST be coded last.
592 3.7.2. RGB
594 This color space allows three or four Planes.
596 An optional transparency Plane can be used to code transparency data.
598 JPEG 2000 RCT is a Reversible Color Transform that codes RGB (Red,
599 Green, Blue) Planes losslessly in a modified YCbCr color space
600 [ISO.15444-1.2019]. Reversible pixel transformations between YCbCr
601 and RGB use the following formulae:
603 Cb = b - g
604 Cr = r - g
605 Y = g + (Cb + Cr) >> 2
607 Figure 6: Description of the transformation of pixels from RGB
608 color space to coded, modified YCbCr color space.
610 g = Y - (Cb + Cr) >> 2
611 r = Cr + g
612 b = Cb + g
613 Figure 7: Description of the transformation of pixels from coded,
614 modified YCbCr color space to RGB color space.
616 Cb and Cr are positively offset by 1 << bits_per_raw_sample after the
617 conversion from RGB to the modified YCbCr, and they are negatively
618 offset by the same value before the conversion from the modified
619 YCbCr to RGB in order to have only nonnegative values after the
620 conversion.
622 When FFV1 uses the JPEG 2000 RCT, the horizontal Lines are
623 interleaved to improve caching efficiency since it is most likely
624 that the JPEG 2000 RCT will immediately be converted to RGB during
625 decoding. The interleaved coding order is also Y, then Cb, then Cr,
626 and then, if used, transparency.
628 As an example, a Frame that is two pixels wide and two pixels high
629 could comprise the following structure:
631 +------------------------+------------------------+
632 | Pixel(1,1) | Pixel(2,1) |
633 | Y(1,1) Cb(1,1) Cr(1,1) | Y(2,1) Cb(2,1) Cr(2,1) |
634 +------------------------+------------------------+
635 | Pixel(1,2) | Pixel(2,2) |
636 | Y(1,2) Cb(1,2) Cr(1,2) | Y(2,2) Cb(2,2) Cr(2,2) |
637 +------------------------+------------------------+
639 In JPEG 2000 RCT, the coding order is left to right and then top to
640 bottom, with values interleaved by Lines and stored in this order:
642 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)
643 Cb(2,2) Cr(1,2) Cr(2,2)
645 3.7.2.1. RGB Exception
647 If bits_per_raw_sample is between 9 and 15 inclusive and extra_plane
648 is 0, the following formulae for reversible conversions between YCbCr
649 and RGB MUST be used instead of the ones above:
651 Cb = g - b
652 Cr = r - b
653 Y = b + (Cb + Cr) >> 2
655 Figure 8: Description of the transformation of pixels from RGB
656 color space to coded, modified YCbCr color space (in case of
657 exception).
659 b = Y - (Cb + Cr) >> 2
660 r = Cr + b
661 g = Cb + b
663 Figure 9: Description of the transformation of pixels from coded,
664 modified YCbCr color space to RGB color space (in case of
665 exception).
667 Background: At the time of this writing, in all known implementations
668 of the FFV1 bitstream, when bits_per_raw_sample was between 9 and 15
669 inclusive and extra_plane was 0, Green Blue Red (GBR) Planes were
670 used as Blue Green Red (BGR) Planes during both encoding and
671 decoding. Meanwhile, 16-bit JPEG 2000 RCT was implemented without
672 this issue in one implementation and validated by one conformance
673 checker. Methods to address this exception for the transform are
674 under consideration for the next version of the FFV1 bitstream.
676 3.8. Coding of the Sample Difference
678 Instead of coding the n+1 bits of the Sample Difference with Huffman
679 or Range coding (or n+2 bits, in the case of JPEG 2000 RCT), only the
680 n (or n+1, in the case of JPEG 2000 RCT) least significant bits are
681 used, since this is sufficient to recover the original Sample. In
682 Figure 10, the term bits represents bits_per_raw_sample + 1 for JPEG
683 2000 RCT or bits_per_raw_sample otherwise:
685 coder_input = ((sample_difference + 2 ^ (bits - 1)) &
686 (2 ^ bits - 1)) - 2 ^ (bits - 1)
688 Figure 10: Description of the coding of the Sample Difference in
689 the bitstream.
691 3.8.1. Range Coding Mode
693 Early experimental versions of FFV1 used the Context-Adaptive Binary
694 Arithmetic Coding (CABAC) coder from H.264 as defined in
695 [ISO.14496-10.2020], but due to the uncertain patent/royalty
696 situation, as well as its slightly worse performance, CABAC was
697 replaced by a range coder based on an algorithm defined by G. Nigel
698 N. Martin in 1979 [Range-Encoding].
700 3.8.1.1. Range Binary Values
702 To encode binary digits efficiently, a range coder is used. A range
703 coder encodes a series of binary symbols by using a probability
704 estimation within each context. The sizes of each of the two
705 subranges are proportional to their estimated probability. The
706 Quantization Table is used to choose the context used from the
707 surrounding image sample values for the case of coding the Sample
708 Differences. The coding of integers is done by coding multiple
709 binary values. The range decoder will read bytes until it can
710 determine into which subrange the input falls to return the next
711 binary symbol.
713 To describe Range coding for FFV1, the following values are used:
715 C_i the i-th context.
716 B_i the i-th byte of the bytestream.
717 R_i the Range at the i-th symbol.
718 r_i the boundary between two subranges of R_i: a subrange of r_i
719 values and a subrange R_i - r_i values.
720 L_i the Low value of the Range at the i-th symbol.
721 l_i a temporary variable to carry over or adjust the Low value of
722 the Range between range coding operations.
723 t_i a temporary variable to transmit subranges between range coding
724 operations.
725 b_i the i-th range-coded binary value.
726 S_(0, i) the i-th initial state.
727 j_n the length of the bytestream encoding n binary symbols.
729 The following range coder state variables are initialized to the
730 following values. The Range is initialized to a value of 65,280
731 (expressed in base 16 as 0xFF00) as depicted in Figure 11. The Low
732 is initialized according to the value of the first two bytes as
733 depicted in Figure 12. j_i tracks the length of the bytestream
734 encoding while incrementing from an initial value of j_0 to a final
735 value of j_n. j_0 is initialized to 2 as depicted in Figure 13.
737 R_(0) = 65280
739 Figure 11: The initial value for the Range.
741 L_(0) = 2 ^ 8 * B_(0) + B_(1)
743 Figure 12: The initial value for Low is set according to the
744 first two bytes of the bytestream.
746 j_(0) = 2
747 Figure 13: The initial value for j, the length of the bytestream
748 encoding.
750 The following equations define how the range coder variables evolve
751 as it reads or writes symbols.
753 r_(i) = floor( ( R_(i) * S_(i, C_(i)) ) / 2 ^ 8 )
755 Figure 14: This formula shows the positioning of range split
756 based on the state.
758 b_(i) = 0 <==>
759 L_(i) < R_(i) - r_(i) ==>
760 S_(i + 1, C_(i)) = zero_state_(S_(i, C_(i))) AND
761 l_(i) = L_(i) AND
762 t_(i) = R_(i) - r_(i)
764 b_(i) = 1 <==>
765 L_(i) >= R_(i) - r_(i) ==>
766 S_(i + 1, C_(i)) = one_state_(S_(i, C_(i))) AND
767 l_(i) = L_(i) - R_(i) + r_(i) AND
768 t_(i) = r_(i)
770 Figure 15: This formula shows the linking of the decoded symbol
771 (represented as b_i), the updated state (represented as
772 S_(i+1,C_(i))), and the updated range (represented as a range
773 from l_i to t_i).
775 C_(i) != k ==> S_(i + 1, k) = S_(i, k)
777 Figure 16: If the value of k is unequal to the i-th value of
778 context, in other words, if the state is unchanged from the last
779 symbol coding, then the value of the state is carried over to the
780 next symbol coding.
782 t_(i) < 2 ^ 8 ==>
783 R_(i + 1) = 2 ^ 8 * t_(i) AND
784 L_(i + 1) = 2 ^ 8 * l_(i) + B_(j_(i)) AND
785 j_(i + 1) = j_(i) + 1
787 t_(i) >= 2 ^ 8 ==>
788 R_(i + 1) = t_(i) AND
789 L_(i + 1) = l_(i) AND
790 j_(i + 1) = j_(i)
792 Figure 17: This formula shows the linking of the range coder with
793 the reading or writing of the bytestream.
795 range = 0xFF00;
796 end = 0;
797 low = get_bits(16);
798 if (low >= range) {
799 low = range;
800 end = 1;
801 }
803 Figure 18: A pseudocode description of the initialization of
804 range coder variables in Range binary mode.
806 refill() {
807 if (range < 256) {
808 range = range * 256;
809 low = low * 256;
810 if (!end) {
811 c.low += get_bits(8);
812 if (remaining_bits_in_bitstream( NumBytes ) == 0) {
813 end = 1;
814 }
815 }
816 }
817 }
819 Figure 19: A pseudocode description of refilling the binary value
820 buffer of the range coder.
822 get_rac(state) {
823 rangeoff = (range * state) / 256;
824 range -= rangeoff;
825 if (low < range) {
826 state = zero_state[state];
827 refill();
828 return 0;
829 } else {
830 low -= range;
831 state = one_state[state];
832 range = rangeoff;
833 refill();
834 return 1;
835 }
836 }
838 Figure 20: A pseudocode description of the read of a binary value
839 in Range binary mode.
841 3.8.1.1.1. Termination
843 The range coder can be used in three modes:
845 * In Open mode when decoding, every symbol the reader attempts to
846 read is available. In this mode, arbitrary data can have been
847 appended without affecting the range coder output. This mode is
848 not used in FFV1.
850 * In Closed mode, the length in bytes of the bytestream is provided
851 to the range decoder. Bytes beyond the length are read as 0 by
852 the range decoder. This is generally one byte shorter than the
853 Open mode.
855 * In Sentinel mode, the exact length in bytes is not known, and thus
856 the range decoder MAY read into the data that follows the range-
857 coded bytestream by one byte. In Sentinel mode, the end of the
858 range-coded bytestream is a binary symbol with state 129, which
859 value SHALL be discarded. After reading this symbol, the range
860 decoder will have read one byte beyond the end of the range-coded
861 bytestream. This way the byte position of the end can be
862 determined. Bytestreams written in Sentinel mode can be read in
863 Closed mode if the length can be determined. In this case, the
864 last (sentinel) symbol will be read uncorrupted and be of value 0.
866 The above describes the range decoding. Encoding is defined as any
867 process that produces a decodable bytestream.
869 There are three places where range coder termination is needed in
870 FFV1. The first is in the Configuration Record, which in this case
871 the size of the range coded bytestream is known and handled as Closed
872 mode. The second is the switch from the Slice Header, which is range
873 coded to Golomb-coded Slices as Sentinel mode. The third is the end
874 of range-coded Slices, which need to terminate before the CRC at
875 their end. This can be handled as Sentinel mode or as Closed mode if
876 the CRC position has been determined.
878 3.8.1.2. Range Non Binary Values
880 To encode scalar integers, it would be possible to encode each bit
881 separately and use the past bits as context. However, that would
882 mean 255 contexts per 8-bit symbol, which is not only a waste of
883 memory but also requires more past data to reach a reasonably good
884 estimate of the probabilities. Alternatively, it would also be
885 possible to assume a Laplacian distribution and only dealing with its
886 variance and mean (as in Huffman coding). However, for maximum
887 flexibility and simplicity, the chosen method uses a single symbol to
888 encode if a number is 0, and if the number is nonzero, it encodes the
889 number using its exponent, mantissa, and sign. The exact contexts
890 used are best described by Figure 21.
892 int get_symbol(RangeCoder *c, uint8_t *state, int is_signed) {
893 if (get_rac(c, state + 0) {
894 return 0;
895 }
897 int e = 0;
898 while (get_rac(c, state + 1 + min(e, 9)) { //1..10
899 e++;
900 }
902 int a = 1;
903 for (int i = e - 1; i >= 0; i--) {
904 a = a * 2 + get_rac(c, state + 22 + min(i, 9)); // 22..31
905 }
907 if (!is_signed) {
908 return a;
909 }
911 if (get_rac(c, state + 11 + min(e, 10))) { //11..21
912 return -a;
913 } else {
914 return a;
915 }
916 }
918 Figure 21: A pseudocode description of the contexts of Range
919 nonbinary values.
921 get_symbol is used for the read out of sample_difference indicated in
922 Figure 10.
924 get_rac returns a boolean, computed from the bytestream as described
925 by the formula found in Figure 14 and by the pseudocode found in
926 Figure 20.
928 3.8.1.3. Initial Values for the Context Model
930 When the keyframe value (see Section 4.4) value is 1, all range coder
931 state variables are set to their initial state.
933 3.8.1.4. State Transition Table
935 In this model, a state transition table is used, indicating to which
936 state the decoder will move to, based on the current state and the
937 value extracted from Figure 20.
939 one_state_(i) =
940 default_state_transition_(i) + state_transition_delta_(i)
942 Figure 22: Description of the coding of the state transition
943 table for a get_rac readout value of 0.
945 zero_state_(i) = 256 - one_state_(256-i)
947 Figure 23: Description of the coding of the state transition
948 table for a get_rac readout value of 1.
950 3.8.1.5. default_state_transition
952 By default, the following state transition table is used:
954 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27,
956 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42,
958 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57,
960 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
962 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
964 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99,100,101,102,103,
966 104,105,106,107,108,109,110,111,112,113,114,114,115,116,117,118,
968 119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,133,
970 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,
972 150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164,
974 165,166,167,168,169,170,171,171,172,173,174,175,176,177,178,179,
976 180,181,182,183,184,185,186,187,188,189,190,190,191,192,194,194,
978 195,196,197,198,199,200,201,202,202,204,205,206,207,208,209,209,
980 210,211,212,213,215,215,216,217,218,219,220,220,222,223,224,225,
982 226,227,227,229,229,230,231,232,234,234,235,236,237,238,239,240,
984 241,242,243,244,245,246,247,248,248, 0, 0, 0, 0, 0, 0, 0,
986 Figure 24: Default state transition table for Range coding.
988 3.8.1.6. Alternative State Transition Table
990 The alternative state transition table has been built using iterative
991 minimization of frame sizes and generally performs better than the
992 default. To use it, the coder_type (see Section 4.2.3) MUST be set
993 to 2, and the difference to the default MUST be stored in the
994 Parameters, see Section 4.2. At the time of this writing, the
995 reference implementation of FFV1 in FFmpeg uses Figure 25 by default
996 when Range coding is used.
998 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
1000 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
1002 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
1004 53, 74, 55, 57, 58, 58, 74, 60,101, 61, 62, 84, 66, 66, 68, 69,
1006 87, 82, 71, 97, 73, 73, 82, 75,111, 77, 94, 78, 87, 81, 83, 97,
1008 85, 83, 94, 86, 99, 89, 90, 99,111, 92, 93,134, 95, 98,105, 98,
1010 105,110,102,108,102,118,103,106,106,113,109,112,114,112,116,125,
1012 115,116,117,117,126,119,125,121,121,123,145,124,126,131,127,129,
1014 165,130,132,138,133,135,145,136,137,139,146,141,143,142,144,148,
1016 147,155,151,149,151,150,152,157,153,154,156,168,158,162,161,160,
1018 172,163,169,164,166,184,167,170,177,174,171,173,182,176,180,178,
1020 175,189,179,181,186,183,192,185,200,187,191,188,190,197,193,196,
1022 197,194,195,196,198,202,199,201,210,203,207,204,205,206,208,214,
1024 209,211,221,212,213,215,224,216,217,218,219,220,222,228,223,225,
1026 226,224,227,229,240,230,231,232,233,234,235,236,238,239,237,242,
1028 241,243,242,244,245,246,247,248,249,250,251,252,252,253,254,255,
1030 Figure 25: Alternative state transition table for Range coding.
1032 3.8.2. Golomb Rice Mode
1034 The end of the bitstream of the Frame is padded with zeroes until the
1035 bitstream contains a multiple of eight bits.
1037 3.8.2.1. Signed Golomb Rice Codes
1039 This coding mode uses Golomb Rice codes. The VLC is split into two
1040 parts: the prefix and suffix. The prefix stores the most significant
1041 bits or indicates if the symbol is too large to be stored (this is
1042 known as the ESC case. The suffix either stores the k least
1043 significant bits or stores the whole number in the ESC case.
1045 int get_ur_golomb(k) {
1046 for (prefix = 0; prefix < 12; prefix++) {
1047 if (get_bits(1)) {
1048 return get_bits(k) + (prefix << k);
1049 }
1050 }
1051 return get_bits(bits) + 11;
1052 }
1054 Figure 26: A pseudocode description of the read of an unsigned
1055 integer in Golomb Rice mode.
1057 int get_sr_golomb(k) {
1058 v = get_ur_golomb(k);
1059 if (v & 1) return - (v >> 1) - 1;
1060 else return (v >> 1);
1061 }
1063 Figure 27: A pseudocode description of the read of a signed
1064 integer in Golomb Rice mode.
1066 3.8.2.1.1. Prefix
1068 +================+=======+
1069 | bits | value |
1070 +================+=======+
1071 | 1 | 0 |
1072 +----------------+-------+
1073 | 01 | 1 |
1074 +----------------+-------+
1075 | ... | ... |
1076 +----------------+-------+
1077 | 0000 0000 01 | 9 |
1078 +----------------+-------+
1079 | 0000 0000 001 | 10 |
1080 +----------------+-------+
1081 | 0000 0000 0001 | 11 |
1082 +----------------+-------+
1083 | 0000 0000 0000 | ESC |
1084 +----------------+-------+
1086 Table 1: Description
1087 of the coding of the
1088 prefix of signed
1089 Golomb Rice codes.
1091 ESC is an ESCape symbol to indicate that the symbol to be stored is
1092 too large for normal storage and that an alternate storage method is
1093 used.
1095 3.8.2.1.2. Suffix
1097 +=========+========================================+
1098 +=========+========================================+
1099 | non ESC | the k least significant bits MSB first |
1100 +---------+----------------------------------------+
1101 | ESC | the value - 11, in MSB first order |
1102 +---------+----------------------------------------+
1104 Table 2: Description of the coding of the suffix
1105 of signed Golomb Rice codes.
1107 ESC MUST NOT be used if the value can be coded as non-ESC.
1109 3.8.2.1.3. Examples
1111 Table 3 shows practical examples of how signed Golomb Rice codes are
1112 decoded based on the series of bits extracted from the bitstream as
1113 described by the method above:
1115 +=====+=======================+=======+
1116 | k | bits | value |
1117 +=====+=======================+=======+
1118 | 0 | 1 | 0 |
1119 +-----+-----------------------+-------+
1120 | 0 | 001 | 2 |
1121 +-----+-----------------------+-------+
1122 | 2 | 1 00 | 0 |
1123 +-----+-----------------------+-------+
1124 | 2 | 1 10 | 2 |
1125 +-----+-----------------------+-------+
1126 | 2 | 01 01 | 5 |
1127 +-----+-----------------------+-------+
1128 | any | 000000000000 10000000 | 139 |
1129 +-----+-----------------------+-------+
1131 Table 3: Examples of decoded,
1132 signed Golomb Rice codes.
1134 3.8.2.2. Run Mode
1136 Run mode is entered when the context is 0 and left as soon as a
1137 nonzero difference is found. The Sample Difference is identical to
1138 the predicted one. The run and the first different Sample Difference
1139 are coded are coded as defined in Section 3.8.2.4.1.
1141 3.8.2.2.1. Run Length Coding
1143 The run value is encoded in two parts. The prefix part stores the
1144 more significant part of the run as well as adjusting the run_index
1145 that determines the number of bits in the less significant part of
1146 the run. The second part of the value stores the less significant
1147 part of the run as it is. The run_index is reset to zero for each
1148 Plane and Slice.
1150 log2_run[41] = {
1151 0, 0, 0, 0, 1, 1, 1, 1,
1152 2, 2, 2, 2, 3, 3, 3, 3,
1153 4, 4, 5, 5, 6, 6, 7, 7,
1154 8, 9,10,11,12,13,14,15,
1155 16,17,18,19,20,21,22,23,
1156 24,
1157 };
1159 if (run_count == 0 && run_mode == 1) {
1160 if (get_bits(1)) {
1161 run_count = 1 << log2_run[run_index];
1162 if (x + run_count <= w) {
1163 run_index++;
1164 }
1165 } else {
1166 if (log2_run[run_index]) {
1167 run_count = get_bits(log2_run[run_index]);
1168 } else {
1169 run_count = 0;
1170 }
1171 if (run_index) {
1172 run_index--;
1173 }
1174 run_mode = 2;
1175 }
1176 }
1178 The log2_run array is also used within [ISO.14495-1.1999].
1180 3.8.2.3. Sign Extension
1182 sign_extend is the function of increasing the number of bits of an
1183 input binary number in two's complement signed number representation
1184 while preserving the input number's sign (positive/negative) and
1185 value, in order to fit in the output bit width. It MAY be computed
1186 with the following:
1188 sign_extend(input_number, input_bits) {
1189 negative_bias = 1 << (input_bits - 1);
1190 bits_mask = negative_bias - 1;
1191 output_number = input_number & bits_mask; // Remove negative bit
1192 is_negative = input_number & negative_bias; // Test negative bit
1193 if (is_negative)
1194 output_number -= negative_bias;
1195 return output_number
1196 }
1198 3.8.2.4. Scalar Mode
1200 Each difference is coded with the per context mean prediction removed
1201 and a per context value for k.
1203 get_vlc_symbol(state) {
1204 i = state->count;
1205 k = 0;
1206 while (i < state->error_sum) {
1207 k++;
1208 i += i;
1209 }
1211 v = get_sr_golomb(k);
1213 if (2 * state->drift < -state->count) {
1214 v = -1 - v;
1215 }
1217 ret = sign_extend(v + state->bias, bits);
1219 state->error_sum += abs(v);
1220 state->drift += v;
1222 if (state->count == 128) {
1223 state->count >>= 1;
1224 state->drift >>= 1;
1225 state->error_sum >>= 1;
1226 }
1227 state->count++;
1228 if (state->drift <= -state->count) {
1229 state->bias = max(state->bias - 1, -128);
1231 state->drift = max(state->drift + state->count,
1232 -state->count + 1);
1233 } else if (state->drift > 0) {
1234 state->bias = min(state->bias + 1, 127);
1236 state->drift = min(state->drift - state->count, 0);
1237 }
1239 return ret;
1240 }
1242 3.8.2.4.1. Golomb Rice Sample Difference Coding
1244 Level coding is identical to the normal difference coding with the
1245 exception that the 0 value is removed as it cannot occur:
1247 diff = get_vlc_symbol(context_state);
1248 if (diff >= 0) {
1249 diff++;
1250 }
1252 Note that this is different from JPEG-LS (lossless JPEG), which
1253 doesn't use prediction in run mode and uses a different encoding and
1254 context model for the last difference. On a small set of test
1255 Samples, the use of prediction slightly improved the compression
1256 rate.
1258 3.8.2.5. Initial Values for the VLC Context State
1260 When keyframe (see Section 4.4) value is 1, all VLC coder state
1261 variables are set to their initial state.
1263 drift = 0;
1264 error_sum = 4;
1265 bias = 0;
1266 count = 1;
1268 4. Bitstream
1270 An FFV1 bitstream is composed of a series of one or more Frames and
1271 (when required) a Configuration Record.
1273 Within the following subsections, pseudocode as described in
1274 Section 2.2.1, is used to explain the structure of each FFV1
1275 bitstream component. Table 4 lists symbols used to annotate that
1276 pseudocode in order to define the storage of the data referenced in
1277 that line of pseudocode.
1279 +========+==================================================+
1280 | symbol | definition |
1281 +========+==================================================+
1282 | u(n) | Unsigned, big-endian integer symbol using n bits |
1283 +--------+--------------------------------------------------+
1284 | br | Boolean (1-bit) symbol that is range coded with |
1285 | | the method described in Section 3.8.1.1 |
1286 +--------+--------------------------------------------------+
1287 | ur | Unsigned scalar symbol that is range coded with |
1288 | | the method described in Section 3.8.1.2 |
1289 +--------+--------------------------------------------------+
1290 | sr | Signed scalar symbol that is range coded with |
1291 | | the method described in Section 3.8.1.2 |
1292 +--------+--------------------------------------------------+
1293 | sd | Sample difference symbol that is coded with the |
1294 | | method described in Section 3.8 |
1295 +--------+--------------------------------------------------+
1297 Table 4: Definition of pseudocode symbols for this document.
1299 The following MUST be provided by external means during the
1300 initialization of the decoder:
1302 frame_pixel_width is defined as Frame width in pixels.
1304 frame_pixel_height is defined as Frame height in pixels.
1306 Default values at the decoder initialization phase:
1308 ConfigurationRecordIsPresent is set to 0.
1310 4.1. Quantization Table Set
1312 The Quantization Table Sets store a sequence of values that are equal
1313 to one less than the count of equal concurrent entries for each set
1314 of equal concurrent entries within the first half of the table
1315 (represented as len - 1 in the pseudocode below) using the
1316 method described in Section 3.8.1.2. The second half doesn’
1317 (U+2019)t need to be stored as it is identical to the first with
1318 flipped sign. scale and len_count[ i ][ j ] are temporary values used
1319 for the computing of context_count[ i ] and are not used outside
1320 Quantization Table Set pseudocode.
1322 Example:
1324 Table: 0 0 1 1 1 1 2 2 -2 -2 -2 -1 -1 -1 -1 0
1326 Stored values: 1, 3, 1
1328 QuantizationTableSet has its own initial states, all set to 128.
1330 pseudocode | type
1331 --------------------------------------------------------------|-----
1332 QuantizationTableSet( i ) { |
1333 scale = 1 |
1334 for (j = 0; j < MAX_CONTEXT_INPUTS; j++) { |
1335 QuantizationTable( i, j, scale ) |
1336 scale *= 2 * len_count[ i ][ j ] - 1 |
1337 } |
1338 context_count[ i ] = ceil( scale / 2 ) |
1339 } |
1341 MAX_CONTEXT_INPUTS is 5.
1343 pseudocode | type
1344 --------------------------------------------------------------|-----
1345 QuantizationTable(i, j, scale) { |
1346 v = 0 |
1347 for (k = 0; k < 128;) { |
1348 len - 1 | ur
1349 for (n = 0; n < len; n++) { |
1350 quant_tables[ i ][ j ][ k ] = scale * v |
1351 k++ |
1352 } |
1353 v++ |
1354 } |
1355 for (k = 1; k < 128; k++) { |
1356 quant_tables[ i ][ j ][ 256 - k ] = \ |
1357 -quant_tables[ i ][ j ][ k ] |
1358 } |
1359 quant_tables[ i ][ j ][ 128 ] = \ |
1360 -quant_tables[ i ][ j ][ 127 ] |
1361 len_count[ i ][ j ] = v |
1362 } |
1364 4.1.1. quant_tables
1366 quant_tables[ i ][ j ][ k ] indicates the Quantization Table value of
1367 the Quantized Sample Difference k of the Quantization Table j of the
1368 Quantization Table Set i.
1370 4.1.2. context_count
1372 context_count[ i ] indicates the count of contexts for Quantization
1373 Table Set i. context_count[ i ] MUST be less than or equal to 32768.
1375 4.2. Parameters
1377 The Parameters section contains significant characteristics about the
1378 decoding configuration used for all instances of Frame (in FFV1
1379 version 0 and 1) or the whole FFV1 bitstream (other versions),
1380 including the stream version, color configuration, and quantization
1381 tables. Figure 28 describes the contents of the bitstream.
1383 Parameters has its own initial states, all set to 128.
1385 pseudocode | type
1386 --------------------------------------------------------------|-----
1387 Parameters( ) { |
1388 version | ur
1389 if (version >= 3) { |
1390 micro_version | ur
1391 } |
1392 coder_type | ur
1393 if (coder_type > 1) { |
1394 for (i = 1; i < 256; i++) { |
1395 state_transition_delta[ i ] | sr
1396 } |
1397 } |
1398 colorspace_type | ur
1399 if (version >= 1) { |
1400 bits_per_raw_sample | ur
1401 } |
1402 chroma_planes | br
1403 log2_h_chroma_subsample | ur
1404 log2_v_chroma_subsample | ur
1405 extra_plane | br
1406 if (version >= 3) { |
1407 num_h_slices - 1 | ur
1408 num_v_slices - 1 | ur
1409 quant_table_set_count | ur
1410 } |
1411 for (i = 0; i < quant_table_set_count; i++) { |
1412 QuantizationTableSet( i ) |
1413 } |
1414 if (version >= 3) { |
1415 for (i = 0; i < quant_table_set_count; i++) { |
1416 states_coded | br
1417 if (states_coded) { |
1418 for (j = 0; j < context_count[ i ]; j++) { |
1419 for (k = 0; k < CONTEXT_SIZE; k++) { |
1420 initial_state_delta[ i ][ j ][ k ] | sr
1421 } |
1422 } |
1423 } |
1424 } |
1425 ec | ur
1426 intra | ur
1427 } |
1428 } |
1430 Figure 28: A pseudocode description of the bitstream contents.
1432 CONTEXT_SIZE is 32.
1434 4.2.1. version
1436 version specifies the version of the FFV1 bitstream.
1438 Each version is incompatible with other versions: decoders SHOULD
1439 reject FFV1 bitstreams due to an unknown version.
1441 Decoders SHOULD reject FFV1 bitstreams with version <= 1 &&
1442 ConfigurationRecordIsPresent == 1.
1444 Decoders SHOULD reject FFV1 bitstreams with version >= 3 &&
1445 ConfigurationRecordIsPresent == 0.
1447 +=======+=========================+
1448 | value | version |
1449 +=======+=========================+
1450 | 0 | FFV1 version 0 |
1451 +-------+-------------------------+
1452 | 1 | FFV1 version 1 |
1453 +-------+-------------------------+
1454 | 2 | reserved* |
1455 +-------+-------------------------+
1456 | 3 | FFV1 version 3 |
1457 +-------+-------------------------+
1458 | 4 | FFV1 version 4 |
1459 +-------+-------------------------+
1460 | Other | reserved for future use |
1461 +-------+-------------------------+
1463 Table 5: The definitions for
1464 version values.
1466 * Version 2 was experimental and this document does not describe it.
1468 4.2.2. micro_version
1470 micro_version specifies the micro-version of the FFV1 bitstream.
1472 After a version is considered stable (a micro-version value is
1473 assigned to be the first stable variant of a specific version), each
1474 new micro-version after this first stable variant is compatible with
1475 the previous micro-version: decoders SHOULD NOT reject FFV1
1476 bitstreams due to an unknown micro-version equal or above the micro-
1477 version considered as stable.
1479 Meaning of micro_version for version 3:
1481 +=======+=========================+
1482 | value | micro_version |
1483 +=======+=========================+
1484 | 0...3 | reserved* |
1485 +-------+-------------------------+
1486 | 4 | first stable variant |
1487 +-------+-------------------------+
1488 | Other | reserved for future use |
1489 +-------+-------------------------+
1491 Table 6: The definitions for
1492 micro_version values for FFV1
1493 version 3.
1495 * Development versions may be incompatible with the stable variants.
1497 Meaning of micro_version for version 4 (note: at the time of writing
1498 of this specification, version 4 is not considered stable so the
1499 first stable micro_version value is to be announced in the future):
1501 +=========+=========================+
1502 | value | micro_version |
1503 +=========+=========================+
1504 | 0...TBA | reserved* |
1505 +---------+-------------------------+
1506 | TBA | first stable variant |
1507 +---------+-------------------------+
1508 | Other | reserved for future use |
1509 +---------+-------------------------+
1511 Table 7: The definitions for
1512 micro_version values for FFV1
1513 version 4.
1515 * Development versions which may be incompatible with the stable
1516 variants.
1518 4.2.3. coder_type
1520 coder_type specifies the coder used.
1522 +=======+=================================================+
1523 | value | coder used |
1524 +=======+=================================================+
1525 | 0 | Golomb Rice |
1526 +-------+-------------------------------------------------+
1527 | 1 | Range coder with default state transition table |
1528 +-------+-------------------------------------------------+
1529 | 2 | Range coder with custom state transition table |
1530 +-------+-------------------------------------------------+
1531 | Other | reserved for future use |
1532 +-------+-------------------------------------------------+
1534 Table 8: The definitions for coder_type values.
1536 Restrictions:
1538 If coder_type is 0, then bits_per_raw_sample SHOULD NOT be > 8.
1540 Background: At the time of this writing, there is no known
1541 implementation of FFV1 bitstream supporting the Golomb Rice algorithm
1542 with bits_per_raw_sample greater than eight, and range coder is
1543 preferred.
1545 4.2.4. state_transition_delta
1547 state_transition_delta specifies the range coder custom state
1548 transition table.
1550 If state_transition_delta is not present in the FFV1 bitstream, all
1551 range coder custom state transition table elements are assumed to be
1552 0.
1554 4.2.5. colorspace_type
1556 colorspace_type specifies the color space encoded, the pixel
1557 transformation used by the encoder, the extra Plane content, as well
1558 as interleave method.
1560 +=======+==============+================+==============+============+
1561 | value | color space | pixel | extra Plane | interleave |
1562 | | encoded | transformation | content | method |
1563 +=======+==============+================+==============+============+
1564 | 0 | YCbCr | None | Transparency | Plane then |
1565 | | | | | Line |
1566 +-------+--------------+----------------+--------------+------------+
1567 | 1 | RGB | JPEG 2000 RCT | Transparency | Line then |
1568 | | | | | Plane |
1569 +-------+--------------+----------------+--------------+------------+
1570 | Other | reserved | reserved for | reserved for | reserved |
1571 | | for future | future use | future use | for future |
1572 | | use | | | use |
1573 +-------+--------------+----------------+--------------+------------+
1575 Table 9: The definitions for colorspace_type values.
1577 FFV1 bitstreams with colorspace_type == 1 && (chroma_planes != 1 ||
1578 log2_h_chroma_subsample != 0 || log2_v_chroma_subsample != 0) are not
1579 part of this specification.
1581 4.2.6. chroma_planes
1583 chroma_planes indicates if chroma (color) Planes are present.
1585 +=======+===============================+
1586 | value | presence |
1587 +=======+===============================+
1588 | 0 | chroma Planes are not present |
1589 +-------+-------------------------------+
1590 | 1 | chroma Planes are present |
1591 +-------+-------------------------------+
1593 Table 10: The definitions for
1594 chroma_planes values.
1596 4.2.7. bits_per_raw_sample
1598 bits_per_raw_sample indicates the number of bits for each Sample.
1599 Inferred to be 8 if not present.
1601 +=======+=================================+
1602 | value | bits for each sample |
1603 +=======+=================================+
1604 | 0 | reserved* |
1605 +-------+---------------------------------+
1606 | Other | the actual bits for each Sample |
1607 +-------+---------------------------------+
1609 Table 11: The definitions for
1610 bits_per_raw_sample values.
1612 * Encoders MUST NOT store bits_per_raw_sample = 0. Decoders SHOULD
1613 accept and interpret bits_per_raw_sample = 0 as 8.
1615 4.2.8. log2_h_chroma_subsample
1617 log2_h_chroma_subsample indicates the subsample factor, stored in
1618 powers to which the number 2 is raised, between luma and chroma width
1619 (chroma_width = 2 ^ -log2_h_chroma_subsample * luma_width).
1621 4.2.9. log2_v_chroma_subsample
1623 log2_v_chroma_subsample indicates the subsample factor, stored in
1624 powers to which the number 2 is raised, between luma and chroma
1625 height (chroma_height = 2 ^ -log2_v_chroma_subsample * luma_height).
1627 4.2.10. extra_plane
1629 extra_plane indicates if an extra Plane is present.
1631 +=======+============================+
1632 | value | presence |
1633 +=======+============================+
1634 | 0 | extra Plane is not present |
1635 +-------+----------------------------+
1636 | 1 | extra Plane is present |
1637 +-------+----------------------------+
1639 Table 12: The definitions for
1640 extra_plane values.
1642 4.2.11. num_h_slices
1644 num_h_slices indicates the number of horizontal elements of the Slice
1645 raster.
1647 Inferred to be 1 if not present.
1649 4.2.12. num_v_slices
1651 num_v_slices indicates the number of vertical elements of the Slice
1652 raster.
1654 Inferred to be 1 if not present.
1656 4.2.13. quant_table_set_count
1658 quant_table_set_count indicates the number of Quantization
1659 Table Sets. quant_table_set_count MUST be less than or equal to 8.
1661 Inferred to be 1 if not present.
1663 MUST NOT be 0.
1665 4.2.14. states_coded
1667 states_coded indicates if the respective Quantization Table Set has
1668 the initial states coded.
1670 Inferred to be 0 if not present.
1672 +=======+================================+
1673 | value | initial states |
1674 +=======+================================+
1675 | 0 | initial states are not present |
1676 | | and are assumed to be all 128 |
1677 +-------+--------------------------------+
1678 | 1 | initial states are present |
1679 +-------+--------------------------------+
1681 Table 13: The definitions for
1682 states_coded values.
1684 4.2.15. initial_state_delta
1686 initial_state_delta[ i ][ j ][ k ] indicates the initial range coder
1687 state, and it is encoded using k as context index for the range coder
1688 and the following pseudocode:
1690 pred = j ? initial_states[ i ][j - 1][ k ] : 128
1692 Figure 29: Predictor value for the coding of initial_state_delta[
1693 i ][ j ][ k ].
1695 initial_state[ i ][ j ][ k ] =
1696 ( pred + initial_state_delta[ i ][ j ][ k ] ) & 255
1698 Figure 30: Description of the coding of initial_state_delta[ i ][
1699 j ][ k ].
1701 4.2.16. ec
1703 ec indicates the error detection/correction type.
1705 +=======+=============================================+
1706 | value | error detection/correction type |
1707 +=======+=============================================+
1708 | 0 | 32-bit CRC in ConfigurationRecord |
1709 +-------+---------------------------------------------+
1710 | 1 | 32-bit CRC in Slice and ConfigurationRecord |
1711 +-------+---------------------------------------------+
1712 | Other | reserved for future use |
1713 +-------+---------------------------------------------+
1715 Table 14: The definitions for ec values.
1717 4.2.17. intra
1719 intra indicates the constraint on keyframe in each instance of Frame.
1721 Inferred to be 0 if not present.
1723 +=======+=====================================================+
1724 | value | relationship |
1725 +=======+=====================================================+
1726 | 0 | keyframe can be 0 or 1 (non keyframes or keyframes) |
1727 +-------+-----------------------------------------------------+
1728 | 1 | keyframeMUST be 1 (keyframes only) |
1729 +-------+-----------------------------------------------------+
1730 | Other | reserved for future use |
1731 +-------+-----------------------------------------------------+
1733 Table 15: The definitions for intra values.
1735 4.3. Configuration Record
1737 In the case of a FFV1 bitstream with version >= 3, a Configuration
1738 Record is stored in the underlying container as described in
1739 Section 4.3.3. It contains the Parameters used for all instances of
1740 Frame. The size of the Configuration Record, NumBytes, is supplied
1741 by the underlying container.
1743 pseudocode | type
1744 -----------------------------------------------------------|-----
1745 ConfigurationRecord( NumBytes ) { |
1746 ConfigurationRecordIsPresent = 1 |
1747 Parameters( ) |
1748 while (remaining_symbols_in_syntax(NumBytes - 4)) { |
1749 reserved_for_future_use | br/ur/sr
1750 } |
1751 configuration_record_crc_parity | u(32)
1752 } |
1754 4.3.1. reserved_for_future_use
1756 reserved_for_future_use is a placeholder for future updates of this
1757 specification.
1759 Encoders conforming to this version of this specification SHALL NOT
1760 write reserved_for_future_use.
1762 Decoders conforming to this version of this specification SHALL
1763 ignore reserved_for_future_use.
1765 4.3.2. configuration_record_crc_parity
1767 configuration_record_crc_parity is 32 bits that are chosen so that
1768 the Configuration Record as a whole has a CRC remainder of zero.
1770 This is equivalent to storing the CRC remainder in the 32-bit parity.
1772 The CRC generator polynomial used is described in Section 4.9.3.
1774 4.3.3. Mapping FFV1 into Containers
1776 This Configuration Record can be placed in any file format that
1777 supports Configuration Records, fitting as much as possible with how
1778 the file format stores Configuration Records. The Configuration
1779 Record storage place and NumBytes are currently defined and supported
1780 for the following formats:
1782 4.3.3.1. Audio Video Interleave (AVI) File Format
1784 The Configuration Record extends the stream format chunk ("AVI ",
1785 "hdlr", "strl", "strf") with the ConfigurationRecord bitstream.
1787 See [AVI] for more information about chunks.
1789 NumBytes is defined as the size, in bytes, of the "strf" chunk
1790 indicated in the chunk header minus the size of the stream format
1791 structure.
1793 4.3.3.2. ISO Base Media File Format
1795 The Configuration Record extends the sample description box ("moov",
1796 "trak", "mdia", "minf", "stbl", "stsd") with a "glbl" box that
1797 contains the ConfigurationRecord bitstream. See [ISO.14496-12.2020]
1798 for more information about boxes.
1800 NumBytes is defined as the size, in bytes, of the "glbl" box
1801 indicated in the box header minus the size of the box header.
1803 4.3.3.3. NUT File Format
1805 The codec_specific_data element (in stream_header packet) contains
1806 the ConfigurationRecord bitstream. See [NUT] for more information
1807 about elements.
1809 NumBytes is defined as the size, in bytes, of the codec_specific_data
1810 element as indicated in the "length" field of codec_specific_data.
1812 4.3.3.4. Matroska File Format
1814 FFV1 SHOULD use V_FFV1 as the Matroska Codec ID. For FFV1 versions 2
1815 or less, the Matroska CodecPrivate Element SHOULD NOT be used. For
1816 FFV1 versions 3 or greater, the Matroska CodecPrivate Element MUST
1817 contain the FFV1 Configuration Record structure and no other data.
1818 See [I-D.ietf-cellar-matroska] for more information about elements.
1820 NumBytes is defined as the Element Data Size of the CodecPrivate
1821 Element.
1823 4.4. Frame
1825 A Frame is an encoded representation of a complete static image. The
1826 whole Frame is provided by the underlaying container.
1828 A Frame consists of the keyframe field, Parameters (if version <= 1),
1829 and a sequence of independent Slices. The pseudocode below describes
1830 the contents of a Frame.
1832 The keyframe field has its own initial state, set to 128.
1834 pseudocode | type
1835 --------------------------------------------------------------|-----
1836 Frame( NumBytes ) { |
1837 keyframe | br
1838 if (keyframe && !ConfigurationRecordIsPresent { |
1839 Parameters( ) |
1840 } |
1841 while (remaining_bits_in_bitstream( NumBytes )) { |
1842 Slice( ) |
1843 } |
1844 } |
1846 The following is an architecture overview of Slices in a Frame:
1848 +---------------------------------------------------------------+
1849 | first Slice header |
1850 +---------------------------------------------------------------+
1851 | first Slice content |
1852 +---------------------------------------------------------------+
1853 | first Slice footer |
1854 +---------------------------------------------------------------+
1855 | ------------------------------------------------------------- |
1856 +---------------------------------------------------------------+
1857 | second Slice header |
1858 +---------------------------------------------------------------+
1859 | second Slice content |
1860 +---------------------------------------------------------------+
1861 | second Slice footer |
1862 +---------------------------------------------------------------+
1863 | ------------------------------------------------------------- |
1864 +---------------------------------------------------------------+
1865 | ... |
1866 +---------------------------------------------------------------+
1867 | ------------------------------------------------------------- |
1868 +---------------------------------------------------------------+
1869 | last Slice header |
1870 +---------------------------------------------------------------+
1871 | last Slice content |
1872 +---------------------------------------------------------------+
1873 | last Slice footer |
1874 +---------------------------------------------------------------+
1876 4.5. Slice
1878 A Slice is an independent, spatial subsection of a Frame that is
1879 encoded separately from another region of the same Frame. The use of
1880 more than one Slice per Frame provides opportunities for taking
1881 advantage of multithreaded encoding and decoding.
1883 A Slice consists of a Slice Header (when relevant), a Slice Content,
1884 and a Slice Footer (when relevant). The pseudocode below describes
1885 the contents of a Slice.
1887 pseudocode | type
1888 --------------------------------------------------------------|-----
1889 Slice( ) { |
1890 if (version >= 3) { |
1891 SliceHeader( ) |
1892 } |
1893 SliceContent( ) |
1894 if (coder_type == 0) { |
1895 while (!byte_aligned()) { |
1896 padding | u(1)
1897 } |
1898 } |
1899 if (version <= 1) { |
1900 while (remaining_bits_in_bitstream( NumBytes ) != 0) {|
1901 reserved | u(1)
1902 } |
1903 } |
1904 if (version >= 3) { |
1905 SliceFooter( ) |
1906 } |
1907 } |
1909 padding specifies a bit without any significance and used only for
1910 byte alignment. padding MUST be 0.
1912 reserved specifies a bit without any significance in this
1913 specification but may have a significance in a later revision of this
1914 specification.
1916 Encoders SHOULD NOT fill reserved.
1918 Decoders SHOULD ignore reserved.
1920 4.6. Slice Header
1922 A Slice Header provides information about the decoding configuration
1923 of the Slice, such as its spatial position, size, and aspect ratio.
1924 The pseudocode below describes the contents of the Slice Header.
1926 Slice Header has its own initial states, all set to 128.
1928 pseudocode | type
1929 --------------------------------------------------------------|-----
1930 SliceHeader( ) { |
1931 slice_x | ur
1932 slice_y | ur
1933 slice_width - 1 | ur
1934 slice_height - 1 | ur
1935 for (i = 0; i < quant_table_set_index_count; i++) { |
1936 quant_table_set_index[ i ] | ur
1937 } |
1938 picture_structure | ur
1939 sar_num | ur
1940 sar_den | ur
1941 if (version >= 4) { |
1942 reset_contexts | br
1943 slice_coding_mode | ur
1944 } |
1945 } |
1947 4.6.1. slice_x
1949 slice_x indicates the x position on the Slice raster formed by
1950 num_h_slices.
1952 Inferred to be 0 if not present.
1954 4.6.2. slice_y
1956 slice_y indicates the y position on the Slice raster formed by
1957 num_v_slices.
1959 Inferred to be 0 if not present.
1961 4.6.3. slice_width
1963 slice_width indicates the width on the Slice raster formed by
1964 num_h_slices.
1966 Inferred to be 1 if not present.
1968 4.6.4. slice_height
1970 slice_height indicates the height on the Slice raster formed by
1971 num_v_slices.
1973 Inferred to be 1 if not present.
1975 4.6.5. quant_table_set_index_count
1977 quant_table_set_index_count is defined as the following:
1979 1 + ( ( chroma_planes || version <= 3 ) ? 1 : 0 )
1980 + ( extra_plane ? 1 : 0 )
1982 4.6.6. quant_table_set_index
1984 quant_table_set_index indicates the Quantization Table Set index to
1985 select the Quantization Table Set and the initial states for the
1986 Slice Content.
1988 Inferred to be 0 if not present.
1990 4.6.7. picture_structure
1992 picture_structure specifies the temporal and spatial relationship of
1993 each Line of the Frame.
1995 Inferred to be 0 if not present.
1997 +=======+=========================+
1998 | value | picture structure used |
1999 +=======+=========================+
2000 | 0 | unknown |
2001 +-------+-------------------------+
2002 | 1 | top field first |
2003 +-------+-------------------------+
2004 | 2 | bottom field first |
2005 +-------+-------------------------+
2006 | 3 | progressive |
2007 +-------+-------------------------+
2008 | Other | reserved for future use |
2009 +-------+-------------------------+
2011 Table 16: The definitions for
2012 picture_structure values.
2014 4.6.8. sar_num
2016 sar_num specifies the Sample aspect ratio numerator.
2018 Inferred to be 0 if not present.
2020 A value of 0 means that aspect ratio is unknown.
2022 Encoders MUST write 0 if the Sample aspect ratio is unknown.
2024 If sar_den is 0, decoders SHOULD ignore the encoded value and
2025 consider that sar_num is 0.
2027 4.6.9. sar_den
2029 sar_den specifies the Sample aspect ratio denominator.
2031 Inferred to be 0 if not present.
2033 A value of 0 means that aspect ratio is unknown.
2035 Encoders MUST write 0 if the Sample aspect ratio is unknown.
2037 If sar_num is 0, decoders SHOULD ignore the encoded value and
2038 consider that sar_den is 0.
2040 4.6.10. reset_contexts
2042 reset_contexts indicates if Slice contexts MUST be reset.
2044 Inferred to be 0 if not present.
2046 4.6.11. slice_coding_mode
2048 slice_coding_mode indicates the Slice coding mode.
2050 Inferred to be 0 if not present.
2052 +=======+=============================+
2053 | value | Slice coding mode |
2054 +=======+=============================+
2055 | 0 | Range Coding or Golomb Rice |
2056 +-------+-----------------------------+
2057 | 1 | raw PCM |
2058 +-------+-----------------------------+
2059 | Other | reserved for future use |
2060 +-------+-----------------------------+
2062 Table 17: The definitions for
2063 slice_coding_mode values.
2065 4.7. Slice Content
2067 A Slice Content contains all Line elements part of the Slice.
2069 Depending on the configuration, Line elements are ordered by Plane
2070 then by row (YCbCr) or by row then by Plane (RGB).
2072 pseudocode | type
2073 --------------------------------------------------------------|-----
2074 SliceContent( ) { |
2075 if (colorspace_type == 0) { |
2076 for (p = 0; p < primary_color_count; p++) { |
2077 for (y = 0; y < plane_pixel_height[ p ]; y++) { |
2078 Line( p, y ) |
2079 } |
2080 } |
2081 } else if (colorspace_type == 1) { |
2082 for (y = 0; y < slice_pixel_height; y++) { |
2083 for (p = 0; p < primary_color_count; p++) { |
2084 Line( p, y ) |
2085 } |
2086 } |
2087 } |
2088 } |
2090 4.7.1. primary_color_count
2092 primary_color_count is defined as the following:
2094 1 + ( chroma_planes ? 2 : 0 ) + ( extra_plane ? 1 : 0 )
2096 4.7.2. plane_pixel_height
2098 plane_pixel_height[ p ] is the height in pixels of Plane p of the
2099 Slice. It is defined as the following:
2101 chroma_planes == 1 && (p == 1 || p == 2)
2102 ? ceil(slice_pixel_height / (1 << log2_v_chroma_subsample))
2103 : slice_pixel_height
2105 4.7.3. slice_pixel_height
2107 slice_pixel_height is the height in pixels of the Slice. It is
2108 defined as the following:
2110 floor(
2111 ( slice_y + slice_height )
2112 * slice_pixel_height
2113 / num_v_slices
2114 ) - slice_pixel_y.
2116 4.7.4. slice_pixel_y
2118 slice_pixel_y is the Slice vertical position in pixels. It is
2119 defined as the following:
2121 floor( slice_y * frame_pixel_height / num_v_slices )
2123 4.8. Line
2125 A Line is a list of the Sample Differences (relative to the
2126 predictor) of primary color components. The pseudocode below
2127 describes the contents of the Line.
2129 pseudocode | type
2130 --------------------------------------------------------------|-----
2131 Line( p, y ) { |
2132 if (colorspace_type == 0) { |
2133 for (x = 0; x < plane_pixel_width[ p ]; x++) { |
2134 sample_difference[ p ][ y ][ x ] | sd
2135 } |
2136 } else if (colorspace_type == 1) { |
2137 for (x = 0; x < slice_pixel_width; x++) { |
2138 sample_difference[ p ][ y ][ x ] | sd
2139 } |
2140 } |
2141 } |
2143 4.8.1. plane_pixel_width
2145 plane_pixel_width[ p ] is the width in pixels of Plane p of the
2146 Slice. It is defined as the following:
2148 chroma_planes == 1 && (p == 1 || p == 2)
2149 ? ceil( slice_pixel_width / (1 << log2_h_chroma_subsample) )
2150 : slice_pixel_width.
2152 4.8.2. slice_pixel_width
2154 slice_pixel_width is the width in pixels of the Slice. It is defined
2155 as the following:
2157 floor(
2158 ( slice_x + slice_width )
2159 * slice_pixel_width
2160 / num_h_slices
2161 ) - slice_pixel_x
2163 4.8.3. slice_pixel_x
2165 slice_pixel_x is the Slice horizontal position in pixels. It is
2166 defined as the following:
2168 floor( slice_x * frame_pixel_width / num_h_slices )
2170 4.8.4. sample_difference
2172 sample_difference[ p ][ y ][ x ] is the Sample Difference for Sample
2173 at Plane p, y position y, and x position x. The Sample value is
2174 computed based on median predictor and context described in
2175 Section 3.2.
2177 4.9. Slice Footer
2179 A Slice Footer provides information about Slice size and (optionally)
2180 parity. The pseudocode below describes the contents of the Slice
2181 Footer.
2183 Note: Slice Footer is always byte aligned.
2185 pseudocode | type
2186 --------------------------------------------------------------|-----
2187 SliceFooter( ) { |
2188 slice_size | u(24)
2189 if (ec) { |
2190 error_status | u(8)
2191 slice_crc_parity | u(32)
2192 } |
2193 } |
2195 4.9.1. slice_size
2197 slice_size indicates the size of the Slice in bytes.
2199 Note: this allows finding the start of Slices before previous Slices
2200 have been fully decoded and allows parallel decoding as well as error
2201 resilience.
2203 4.9.2. error_status
2205 error_status specifies the error status.
2207 +=======+======================================+
2208 | value | error status |
2209 +=======+======================================+
2210 | 0 | no error |
2211 +-------+--------------------------------------+
2212 | 1 | Slice contains a correctable error |
2213 +-------+--------------------------------------+
2214 | 2 | Slice contains a uncorrectable error |
2215 +-------+--------------------------------------+
2216 | Other | reserved for future use |
2217 +-------+--------------------------------------+
2219 Table 18: The definitions for error_status
2220 values.
2222 4.9.3. slice_crc_parity
2224 slice_crc_parity is 32 bits that are chosen so that the Slice as a
2225 whole has a CRC remainder of 0.
2227 This is equivalent to storing the CRC remainder in the 32-bit parity.
2229 The CRC generator polynomial used is the standard IEEE CRC polynomial
2230 (0x104C11DB7) with initial value 0, without pre-inversion, and
2231 without post-inversion.
2233 5. Restrictions
2235 To ensure that fast multithreaded decoding is possible, starting with
2236 version 3 and if frame_pixel_width * frame_pixel_height is more than
2237 101376, slice_width * slice_height MUST be less or equal to
2238 num_h_slices * num_v_slices / 4. Note: 101376 is the frame size in
2239 pixels of a 352x288 frame also known as CIF (Common Intermediate
2240 Format) frame size format.
2242 For each Frame, each position in the Slice raster MUST be filled by
2243 one and only one Slice of the Frame (no missing Slice position and no
2244 Slice overlapping).
2246 For each Frame with a keyframe value of 0, each Slice MUST have the
2247 same value of slice_x, slice_y, slice_width, and slice_height as a
2248 Slice in the previous Frame, except if reset_contexts is 1.
2250 6. Security Considerations
2252 Like any other codec (such as [RFC6716]), FFV1 should not be used
2253 with insecure ciphers or cipher modes that are vulnerable to known
2254 plaintext attacks. Some of the header bits as well as the padding
2255 are easily predictable.
2257 Implementations of the FFV1 codec need to take appropriate security
2258 considerations into account. Those related to denial of service are
2259 outlined in Section 2.1 of [RFC4732]. It is extremely important for
2260 the decoder to be robust against malicious payloads. Malicious
2261 payloads MUST NOT cause the decoder to overrun its allocated memory
2262 or to take an excessive amount of resources to decode. An overrun in
2263 allocated memory could lead to arbitrary code execution by an
2264 attacker. The same applies to the encoder, even though problems in
2265 encoders are typically rarer. Malicious video streams MUST NOT cause
2266 the encoder to misbehave because this would allow an attacker to
2267 attack transcoding gateways. A frequent security problem in image
2268 and video codecs is failure to check for integer overflows. An
2269 example is allocating frame_pixel_width * frame_pixel_height in pixel
2270 count computations without considering that the multiplication result
2271 may have overflowed the range of the arithmetic type. The range
2272 coder could, if implemented naively, read one byte over the end. The
2273 implementation MUST ensure that no read outside allocated and
2274 initialized memory occurs.
2276 None of the content carried in FFV1 is intended to be executable.
2278 7. IANA Considerations
2280 IANA has registered the following values.
2282 7.1. Media Type Definition
2284 This registration is done using the template defined in [RFC6838] and
2285 following [RFC4855].
2287 Type name: video
2289 Subtype name: FFV1
2291 Required parameters: None.
2293 Optional parameters: These parameters are used to signal the
2294 capabilities of a receiver implementation. These parameters MUST
2295 NOT be used for any other purpose.
2297 version: The version of the FFV1 encoding as defined by
2298 Section 4.2.1.
2299 micro_version: The micro_version of the FFV1 encoding as defined
2300 by Section 4.2.2.
2301 coder_type: The coder_type of the FFV1 encoding as defined by
2302 Section 4.2.3.
2303 colorspace_type: The colorspace_type of the FFV1 encoding as
2304 defined by Section 4.2.5.
2305 bits_per_raw_sample: The bits_per_raw_sample of the FFV1 encoding
2306 as defined by Section 4.2.7.
2307 max_slices: The value of max_slices is an integer indicating the
2308 maximum count of Slices within a Frame of the FFV1 encoding.
2310 Encoding considerations: This media type is defined for
2311 encapsulation in several audiovisual container formats and
2312 contains binary data; see Section 4.3.3. This media type is
2313 framed binary data; see Section 4.8 of [RFC6838].
2315 Security considerations: See Section 6 of this document.
2317 Interoperability considerations: None.
2319 Published specification: RFC XXXX.
2321 [RFC Editor: Upon publication as an RFC, please replace "XXXX" with
2322 the number assigned to this document and remove this note.]
2324 Applications that use this media type: Any application that requires
2325 the transport of lossless video can use this media type. Some
2326 examples are, but not limited to, screen recording, scientific
2327 imaging, and digital video preservation.
2328 Fragment identifier considerations: N/A.
2329 Additional information: None.
2330 Person & email address to contact for further information: Michael N
2331 iedermayer (michael@niedermayer.cc
2332 (mailto:michael@niedermayer.cc))
2333 Intended usage: COMMON
2334 Restrictions on usage: None.
2335 Author: Dave Rice (dave@dericed.com (mailto:dave@dericed.com))
2336 Change controller: IETF CELLAR Working Group delegated from the
2337 IESG.
2339 8. Changelog
2341 See https://github.com/FFmpeg/FFV1/commits/master
2342 (https://github.com/FFmpeg/FFV1/commits/master)
2344 [RFC Editor: Please remove this Changelog section prior to
2345 publication.]
2347 9. Normative References
2349 [ISO.9899.2018]
2350 International Organization for Standardization,
2351 "Information technology - Programming languages - C", ISO/
2352 IEC 9899:2018, June 2018.
2354 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
2355 Requirement Levels", BCP 14, RFC 2119,
2356 DOI 10.17487/RFC2119, March 1997,
2357 .
2359 [RFC4732] Handley, M., Ed., Rescorla, E., Ed., and IAB, "Internet
2360 Denial-of-Service Considerations", RFC 4732,
2361 DOI 10.17487/RFC4732, December 2006,
2362 .
2364 [RFC4855] Casner, S., "Media Type Registration of RTP Payload
2365 Formats", RFC 4855, DOI 10.17487/RFC4855, February 2007,
2366 .
2368 [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type
2369 Specifications and Registration Procedures", BCP 13,
2370 RFC 6838, DOI 10.17487/RFC6838, January 2013,
2371 .
2373 [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC
2374 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174,
2375 May 2017, .
2377 10. Informative References
2379 [AddressSanitizer]
2380 Clang Project, "AddressSanitizer", Clang 12 documentation,
2381 .
2383 [AVI] Microsoft, "AVI RIFF File Reference",
2384 .
2387 [FFV1GO] Buitenhuis, D., "FFV1 Decoder in Go", 2019,
2388 .
2390 [HuffYUV] Rudiak-Gould, B., "HuffYUV revisited", December 2003,
2391 .
2394 [I-D.ietf-cellar-ffv1]
2395 Niedermayer, M., Rice, D., and J. Martinez, "FFV1 Video
2396 Coding Format Version 0, 1, and 3", Work in Progress,
2397 Internet-Draft, draft-ietf-cellar-ffv1-20, 23 February
2398 2021, .
2401 [I-D.ietf-cellar-matroska]
2402 Lhomme, S., Bunkus, M., and D. Rice, "Matroska Media
2403 Container Format Specifications", Work in Progress,
2404 Internet-Draft, draft-ietf-cellar-matroska-10, 30 April
2405 2022, .
2408 [ISO.14495-1.1999]
2409 International Organization for Standardization,
2410 "Information technology -- Lossless and near-lossless
2411 compression of continuous-tone still images: Baseline",
2412 ISO/IEC 14495-1:1999, December 1999.
2414 [ISO.14496-10.2020]
2415 International Organization for Standardization,
2416 "Information technology -- Coding of audio-visual objects
2417 -- Part 10: Advanced Video Coding", ISO/IEC 14496-10:2020,
2418 December 2020.
2420 [ISO.14496-12.2020]
2421 International Organization for Standardization,
2422 "Information technology -- Coding of audio-visual objects
2423 -- Part 12: ISO base media file format", ISO/IEC
2424 14496-12:2020, December 2020.
2426 [ISO.15444-1.2019]
2427 International Organization for Standardization,
2428 "Information technology -- JPEG 2000 image coding system:
2429 Core coding system", ISO/IEC 15444-1:2019, October 2019.
2431 [MediaConch]
2432 MediaArea.net, "MediaConch", 2018,
2433 .
2435 [NUT] Niedermayer, M., "NUT Open Container Format", December
2436 2013, .
2438 [Range-Encoding]
2439 Martin, G. N. N., "Range encoding: an algorithm for
2440 removing redundancy from a digitised message", Proceedings
2441 of the Conference on Video and Data Recording, Institution
2442 of Electronic and Radio Engineers, Hampshire, England,
2443 July 1979.
2445 [REFIMPL] Niedermayer, M., "The reference FFV1 implementation / the
2446 FFV1 codec in FFmpeg",
2447 .
2449 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the
2450 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716,
2451 September 2012, .
2453 [Valgrind] Valgrind Developers, "Valgrind website",
2454 .
2456 [YCbCr] Wikipedia, "YCbCr", 25 May 2021,
2457 .
2460 Appendix A. Multithreaded Decoder Implementation Suggestions
2462 This appendix is informative.
2464 The FFV1 bitstream is parsable in two ways: in sequential order as
2465 described in this document or with the pre-analysis of the footer of
2466 each Slice. Each Slice footer contains a slice_size field so the
2467 boundary of each Slice is computable without having to parse the
2468 Slice content. That allows multithreading as well as independence of
2469 Slice content (a bitstream error in a Slice header or Slice content
2470 has no impact on the decoding of the other Slices).
2472 After having checked the keyframe field, a decoder SHOULD parse
2473 slice_size fields, from slice_size of the last Slice at the end of
2474 the Frame up to slice_size of the first Slice at the beginning of the
2475 Frame before parsing Slices, in order to have Slice boundaries. A
2476 decoder MAY fall back on sequential order e.g., in case of a
2477 corrupted Frame (e.g., frame size unknown or slice_size of Slices not
2478 coherent) or if there is no possibility of seeking into the stream.
2480 Appendix B. Future Handling of Some Streams Created by Nonconforming
2481 Encoders
2483 This appendix is informative.
2485 Some bitstreams were found with 40 extra bits corresponding to
2486 error_status and slice_crc_parity in the reserved bits of Slice. Any
2487 revision of this specification should avoid adding 40 bits of content
2488 after SliceContent if version == 0 or version == 1, otherwise a
2489 decoder conforming to the revised specification could not distinguish
2490 between a revised bitstream and such buggy bitstream in the wild.
2492 Appendix C. FFV1 Implementations
2494 This appendix provides references to a few notable implementations of
2495 FFV1.
2497 C.1. FFmpeg FFV1 Codec
2499 This reference implementation [REFIMPL] contains no known buffer
2500 overflow or cases where a specially crafted packet or video segment
2501 could cause a significant increase in CPU load.
2503 The reference implementation [REFIMPL] was validated in the following
2504 conditions:
2506 * Sending the decoder valid packets generated by the reference
2507 encoder and verifying that the decoder's output matches the
2508 encoder's input.
2509 * Sending the decoder packets generated by the reference encoder and
2510 then subjected to random corruption.
2511 * Sending the decoder random packets that are not FFV1.
2513 In all of the conditions above, the decoder and encoder was run
2514 inside the Valgrind memory debugger [Valgrind] as well as the Clang
2515 AddressSanitizer [AddressSanitizer], which tracks reads and writes to
2516 invalid memory regions as well as the use of uninitialized memory.
2517 There were no errors reported on any of the tested conditions.
2519 C.2. FFV1 Decoder in Go
2521 An FFV1 decoder [FFV1GO] was written in Go by Derek Buitenhuis during
2522 the work to develop this document.
2524 C.3. MediaConch
2526 The developers of the MediaConch project [MediaConch] created an
2527 independent FFV1 decoder as part of that project to validate FFV1
2528 bitstreams. This work led to the discovery of three conflicts
2529 between existing FFV1 implementations and draft versions of this
2530 document. These issues are addressed by Section 3.3.1,
2531 Section 3.7.2.1, and Appendix B.
2533 Authors' Addresses
2535 Michael Niedermayer
2536 Email: michael@niedermayer.cc
2538 Dave Rice
2539 Email: dave@dericed.com
2541 Jérôme Martinez
2542 Email: jerome@mediaarea.net