idnits 2.17.00 (12 Aug 2021) /tmp/idnits17936/draft-bankoski-vp8-bitstream-01.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** The document seems to lack a Security Considerations section. ** The document seems to lack an IANA Considerations section. (See Section 2.2 of https://www.ietf.org/id-info/checklist for how to handle the case when there are no actions for IANA.) ** The document seems to lack separate sections for Informative/Normative References. All references will be assumed normative when checking for downward references. ** The document seems to lack a both a reference to RFC 2119 and the recommended RFC 2119 boilerplate, even if it appears to use RFC 2119 keywords. RFC 2119 keyword, line 11710: '... member \ref MUST be properly init...' RFC 2119 keyword, line 11711: '... but \ref MAY be clobbered by t...' RFC 2119 keyword, line 11712: '... algorithm. This parameter \ref MAY be...' RFC 2119 keyword, line 11730: '... member \ref MUST be properly init...' RFC 2119 keyword, line 11731: '... but \ref MAY be clobbered by t...' (11 more instances...) Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year == Line 635 has weird spacing: '...typedef sign...' == Line 644 has weird spacing: '...def int int32...' == Line 6224 has weird spacing: '...ned int split...' == Line 6665 has weird spacing: '...decoder bool;...' == Line 7100 has weird spacing: '...tioning parti...' == (34 more instances...) -- The document date (March 11, 2011) is 4088 days in the past. Is this intentional? -- Found something which looks like a code comment -- if you have code sections in the document, please surround them with '' and '' lines. Checking references for intended status: Informational ---------------------------------------------------------------------------- -- Looks like a reference, but probably isn't: '3' on line 9982 == Missing Reference: 'A' is mentioned on line 2048, but not defined == Missing Reference: 'L' is mentioned on line 2048, but not defined -- Looks like a reference, but probably isn't: '8' on line 11108 == Missing Reference: '-1' is mentioned on line 9565, but not defined -- Looks like a reference, but probably isn't: '0' on line 12035 -- Looks like a reference, but probably isn't: '1' on line 12024 -- Looks like a reference, but probably isn't: '4' on line 9955 -- Looks like a reference, but probably isn't: '7' on line 9232 -- Looks like a reference, but probably isn't: '9' on line 8691 -- Looks like a reference, but probably isn't: '2' on line 11585 -- Looks like a reference, but probably isn't: '5' on line 9602 -- Looks like a reference, but probably isn't: '6' on line 9232 -- Looks like a reference, but probably isn't: '16' on line 10864 -- Looks like a reference, but probably isn't: '12' on line 10818 == Missing Reference: 'MVPcount' is mentioned on line 4893, but not defined == Missing Reference: 'MVPsign' is mentioned on line 4942, but not defined == Missing Reference: '-4096' is mentioned on line 5073, but not defined -- Looks like a reference, but probably isn't: '4095' on line 5073 -- Looks like a reference, but probably isn't: '128' on line 6335 == Missing Reference: 'SIGN' is mentioned on line 8172, but not defined == Missing Reference: 'NEAREST' is mentioned on line 8487, but not defined == Missing Reference: 'NEAR' is mentioned on line 8491, but not defined == Missing Reference: 'BEST' is mentioned on line 8506, but not defined -- Looks like a reference, but probably isn't: '15' on line 8508 -- Looks like a reference, but probably isn't: '10' on line 8691 -- Looks like a reference, but probably isn't: '18' on line 8834 -- Looks like a reference, but probably isn't: '14' on line 8846 == Missing Reference: '-stride' is mentioned on line 9088, but not defined == Missing Reference: '-2' is mentioned on line 9564, but not defined -- Looks like a reference, but probably isn't: '25' on line 10825 -- Looks like a reference, but probably isn't: '80' on line 12051 Summary: 4 errors (**), 0 flaws (~~), 19 warnings (==), 22 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 Network Working Group J. Bankoski 3 Internet-Draft P. Wilkins 4 Intended status: Informational Y. Xu 5 Expires: September 12, 2011 Google, Inc. 6 March 11, 2011 8 VP8 Data Format and Decoding Guide 9 draft-bankoski-vp8-bitstream-01 11 Abstract 13 This document describes the VP8 compressed video data format, 14 together with a discussion of the decoding procedure for the format. 16 Status of this Memo 18 This Internet-Draft is submitted in full conformance with the 19 provisions of BCP 78 and BCP 79. 21 Internet-Drafts are working documents of the Internet Engineering 22 Task Force (IETF). Note that other groups may also distribute 23 working documents as Internet-Drafts. The list of current Internet- 24 Drafts is at http://datatracker.ietf.org/drafts/current/. 26 Internet-Drafts are draft documents valid for a maximum of six months 27 and may be updated, replaced, or obsoleted by other documents at any 28 time. It is inappropriate to use Internet-Drafts as reference 29 material or to cite them other than as "work in progress." 31 This Internet-Draft will expire on September 12, 2011. 33 Copyright Notice 35 Copyright (c) 2011 IETF Trust and the persons identified as the 36 document authors. All rights reserved. 38 This document is subject to BCP 78 and the IETF Trust's Legal 39 Provisions Relating to IETF Documents 40 (http://trustee.ietf.org/license-info) in effect on the date of 41 publication of this document. Please review these documents 42 carefully, as they describe your rights and restrictions with respect 43 to this document. 45 Table of Contents 47 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 5 48 2. Format Overview . . . . . . . . . . . . . . . . . . . . . . . 7 49 3. Compressed Frame Types . . . . . . . . . . . . . . . . . . . 9 50 4. Overview of Compressed Data Format . . . . . . . . . . . . . 10 51 5. Overview of the Decoding Process . . . . . . . . . . . . . . 12 52 6. Description of Algorithms . . . . . . . . . . . . . . . . . . 17 53 7. Boolean Entropy Decoder . . . . . . . . . . . . . . . . . . . 20 54 7.1. Underlying Theory of Coding . . . . . . . . . . . . . . 21 55 7.2. Practical Algorithm Description . . . . . . . . . . . . 22 56 7.3. Actual Implementation . . . . . . . . . . . . . . . . . 24 57 8. Compressed Data Components . . . . . . . . . . . . . . . . . 29 58 8.1. Tree Coding Implementation . . . . . . . . . . . . . . . 31 59 8.2. Tree Coding Example . . . . . . . . . . . . . . . . . . 32 60 9. Frame Header . . . . . . . . . . . . . . . . . . . . . . . . 35 61 9.1. Uncompressed Data Chunk . . . . . . . . . . . . . . . . 35 62 9.2. Color Space and Pixel Type (Key Frames-only) . . . . . . 38 63 9.3. Segment-based Adjustments . . . . . . . . . . . . . . . 38 64 9.4. Loop Filter Type and Levels . . . . . . . . . . . . . . 39 65 9.5. Token Partition and Partition Data Offsets . . . . . . . 40 66 9.6. Dequantization Indices . . . . . . . . . . . . . . . . . 41 67 9.7. Refresh Golden Frame and AltRef Frame . . . . . . . . . 42 68 9.8. Refresh Last Frame Buffer . . . . . . . . . . . . . . . 43 69 9.9. DCT Coefficient Probability Update . . . . . . . . . . . 43 70 9.10. Remaining Frame Header Data (non-Key Frame) . . . . . . 44 71 9.11. Remaining Frame Header Data (Key Frame) . . . . . . . . 45 72 10. Segment-based Feature Adjustments . . . . . . . . . . . . . . 46 73 11. Key Frame Macroblock Prediction Records . . . . . . . . . . . 47 74 11.1. mb_skip_coeff . . . . . . . . . . . . . . . . . . . . . 47 75 11.2. Luma Modes . . . . . . . . . . . . . . . . . . . . . . . 47 76 11.3. Subblock Mode Contexts . . . . . . . . . . . . . . . . . 50 77 11.4. Chroma Modes . . . . . . . . . . . . . . . . . . . . . . 51 78 11.5. Subblock Mode Probability Table . . . . . . . . . . . . 52 79 12. Intraframe Prediction . . . . . . . . . . . . . . . . . . . . 56 80 12.1. mb_skip_coeff . . . . . . . . . . . . . . . . . . . . . 56 81 12.2. Chroma Prediction . . . . . . . . . . . . . . . . . . . 57 82 12.3. Luma Prediction . . . . . . . . . . . . . . . . . . . . 59 83 13. DCT Coefficient Decoding . . . . . . . . . . . . . . . . . . 66 84 13.1. MB Without non-Zero Coefficient Values . . . . . . . . . 66 85 13.2. Coding of Individual Coefficient Values . . . . . . . . 67 86 13.3. Token Probabilities . . . . . . . . . . . . . . . . . . 69 87 13.4. Token Probability Updates . . . . . . . . . . . . . . . 73 88 13.5. Default Token Probability Table . . . . . . . . . . . . 78 89 14. DCT and WHT Inversion and Macroblock Reconstruction . . . . . 83 90 14.1. Dequantization . . . . . . . . . . . . . . . . . . . . . 83 91 14.2. Inverse Transforms . . . . . . . . . . . . . . . . . . . 84 92 14.3. Implementation of the WHT Inversion . . . . . . . . . . 85 93 14.4. Implementation of the DCT Inversion . . . . . . . . . . 87 94 14.5. Summation of Predictor and Residue . . . . . . . . . . . 90 95 15. Loop Filter . . . . . . . . . . . . . . . . . . . . . . . . . 91 96 15.1. Filter Geometry and Overall Procedure . . . . . . . . . 92 97 15.2. Simple Filter . . . . . . . . . . . . . . . . . . . . . 94 98 15.3. Normal Filter . . . . . . . . . . . . . . . . . . . . . 98 99 15.4. Calculation of Control Parameters . . . . . . . . . . . 103 100 16. Interframe Macroblock Prediction Records . . . . . . . . . . 105 101 16.1. Intra-Predicted Macroblocks . . . . . . . . . . . . . . 105 102 16.2. Inter-Predicted Macroblocks . . . . . . . . . . . . . . 106 103 16.3. Mode and Motion Vector Contexts . . . . . . . . . . . . 107 104 16.4. Split Prediction . . . . . . . . . . . . . . . . . . . . 113 105 17. Motion Vector Decoding . . . . . . . . . . . . . . . . . . . 117 106 17.1. Coding of Each Component . . . . . . . . . . . . . . . . 117 107 17.2. Probability Updates . . . . . . . . . . . . . . . . . . 119 108 18. Interframe Prediction . . . . . . . . . . . . . . . . . . . . 122 109 18.1. Bounds on and Adjustment of Motion Vectors . . . . . . . 122 110 18.2. Prediction Subblocks . . . . . . . . . . . . . . . . . . 123 111 18.3. Sub-pixel Interpolation . . . . . . . . . . . . . . . . 124 112 18.4. Filter Properties . . . . . . . . . . . . . . . . . . . 127 113 19. Annex A: Bitstream Syntax . . . . . . . . . . . . . . . . . . 130 114 19.1. Uncompressed Data Chunk . . . . . . . . . . . . . . . . 130 115 19.2. Frame Header . . . . . . . . . . . . . . . . . . . . . . 132 116 19.3. Macroblock Data . . . . . . . . . . . . . . . . . . . . 144 117 20. Patent License . . . . . . . . . . . . . . . . . . . . . . . 148 118 21. Attachment One: Reference Decoder Source Code . . . . . . . . 149 119 21.1. bit_ops.h . . . . . . . . . . . . . . . . . . . . . . . 149 120 21.2. bool_decoder.h . . . . . . . . . . . . . . . . . . . . . 149 121 21.3. dequant_data.h . . . . . . . . . . . . . . . . . . . . . 153 122 21.4. dixie.c . . . . . . . . . . . . . . . . . . . . . . . . 153 123 21.5. dixie.h . . . . . . . . . . . . . . . . . . . . . . . . 165 124 21.6. dixie_loopfilter.c . . . . . . . . . . . . . . . . . . . 172 125 21.7. dixie_loopfilter.h . . . . . . . . . . . . . . . . . . . 184 126 21.8. idct_add.c . . . . . . . . . . . . . . . . . . . . . . . 184 127 21.9. idct_add.h . . . . . . . . . . . . . . . . . . . . . . . 188 128 21.10. mem.h . . . . . . . . . . . . . . . . . . . . . . . . . 188 129 21.11. modemv.c . . . . . . . . . . . . . . . . . . . . . . . . 190 130 21.12. modemv.h . . . . . . . . . . . . . . . . . . . . . . . . 205 131 21.13. modemv_data.h . . . . . . . . . . . . . . . . . . . . . 205 132 21.14. predict.c . . . . . . . . . . . . . . . . . . . . . . . 210 133 21.15. predict.h . . . . . . . . . . . . . . . . . . . . . . . 240 134 21.16. tokens.c . . . . . . . . . . . . . . . . . . . . . . . . 240 135 21.17. tokens.h . . . . . . . . . . . . . . . . . . . . . . . . 250 136 21.18. vp8_prob_data.h . . . . . . . . . . . . . . . . . . . . 259 137 21.19. vpx_codec_internal.h . . . . . . . . . . . . . . . . . . 268 138 21.20. vpx_decoder.h . . . . . . . . . . . . . . . . . . . . . 278 139 21.21. vpx_integer.h . . . . . . . . . . . . . . . . . . . . . 285 140 22. References . . . . . . . . . . . . . . . . . . . . . . . . . 288 141 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 289 143 1. Introduction 145 This document describes the VP8 compressed video data format, 146 together with a discussion of the decoding procedure for the format. 147 It is intended to be used in conjunction with and as a guide to the 148 reference decoder source code provided in Attachment One. If there 149 are any conflicts between this narrative and the reference source 150 code, the reference source code should be considered correct. The 151 bitstream is defined by the reference source code and not this 152 narrative. 154 Like many modern video compression schemes, VP8 is based on 155 decomposition of frames into square subblocks of pixels, prediction 156 of such subblocks using previously constructed blocks, and adjustment 157 of such predictions (as well as synthesis of unpredicted blocks) 158 using a discrete cosine transform (hereafter abbreviated as DCT). In 159 one special case, however, VP8 uses a "Walsh-Hadamard" transform 160 (hereafter abbreviated as WHT) instead of a DCT. 162 Roughly speaking, such systems reduce datarate by exploiting the 163 temporal and spatial coherence of most video signals. It is more 164 efficient to specify the location of a visually similar portion of a 165 prior frame than it is to specify pixel values. The frequency 166 segregation provided by the DCT and WHT facilitate the exploitation 167 of both spatial coherence in the original signal and the tolerance of 168 the human visual system to moderate losses of fidelity in the 169 reconstituted signal. 171 VP8 augments these basic concepts with, among other things, 172 sophisticated usage of contextual probabilities. The result is a 173 significant reduction in datarate at a given quality. 175 Unlike some similar schemes (the older MPEG formats, for example), 176 VP8 specifies exact values for reconstructed pixels. Specifically, 177 the specification for the DCT and WHT portions of the reconstruction 178 does not allow for any "drift" caused by truncation of fractions. 179 Rather, the algorithm is specified using fixed-precision integer 180 operations exclusively. This greatly facilitates the verification of 181 the correctness of a decoder implementation as well as avoiding 182 difficult-to-predict visual incongruities between such 183 implementations. 185 It should be remarked that, in a complete video playback system, the 186 displayed frames may or may not be identical to the reconstructed 187 frames. Many systems apply a final level of filtering (commonly 188 referred to as postprocessing) to the reconstructed frames prior to 189 viewing. Such postprocessing has no effect on the decoding and 190 reconstruction of subsequent frames (which are predicted using the 191 completely-specified reconstructed frames) and is beyond the scope of 192 this document. In practice, the nature and extent of this sort of 193 postprocessing is dependent on both the taste of the user and on the 194 computational facilities of the playback environment. 196 2. Format Overview 198 VP8 works exclusively with an 8-bit YUV 4:2:0 image format. In this 199 format, each 8-bit pixel in the two chroma planes (U and V) 200 corresponds positionally to a 2x2 block of 8-bit luma pixels in the Y 201 plane; coordinates of the upper left corner of the Y block are of 202 course exactly twice the coordinates of the corresponding chroma 203 pixels. When we refer to pixels or pixel distances without 204 specifying a plane, we are implicitly referring to the Y plane or to 205 the complete image, both of which have the same (full) resolution. 207 As is usually the case, the pixels are simply a large array of bytes 208 stored in rows from top to bottom, each row being stored from left to 209 right. This "left to right" then "top to bottom" raster-scan order 210 is reflected in the layout of the compressed data as well. 212 Provision has been made in the VP8 bitstream header for the support 213 of a secondary YUV color format, in the form of a reserved bit. 215 Occasionally, at very low datarates, a compression system may decide 216 to reduce the resolution of the input signal to facilitate efficient 217 compression. The VP8 data format supports this via optional 218 upscaling of its internal reconstruction buffer prior to output (this 219 is completely distinct from the optional postprocessing discussed 220 earlier, which has nothing to do with decoding per se). This 221 upsampling restores the video frames to their original resolution. 222 In other words, the compression/decompression system can be viewed as 223 a "black box", where the input and output is always at a given 224 resolution. The compressor might decide to "cheat" and process the 225 signal at a lower resolution. In that case, the decompressor needs 226 the ability to restore the signal to its original resolution. 228 Internally, VP8 decomposes each output frame into an array of 229 macroblocks. A macroblock is a square array of pixels whose Y 230 dimensions are 16x16 and whose U and V dimensions are 8x8. 231 Macroblock-level data in a compressed frame occurs (and must be 232 processed) in a raster order similar to that of the pixels comprising 233 the frame. 235 Macroblocks are further decomposed into 4x4 subblocks. Every 236 macroblock has 16 Y subblocks, 4 U subblocks, and 4 V subblocks. Any 237 subblock-level data (and processing of such data) again occurs in 238 raster order, this time in raster order within the containing 239 macroblock. 241 As discussed in further detail below, data can be specified at the 242 levels of both macroblocks and their subblocks. 244 Pixels are always treated, at a minimum, at the level of subblocks, 245 which may be thought of as the "atoms" of the VP8 algorithm. In 246 particular, the 2x2 chroma blocks corresponding to 4x4 Y subblocks 247 are never treated explicitly in the data format or in the algorithm 248 specification. 250 The DCT and WHT always operate at a 4x4 resolution. The DCT is used 251 for the 16Y, 4U and 4V subblocks. The WHT is used (with some but not 252 all prediction modes) to encode a 4x4 array comprising the average 253 intensities of the 16 Y subblocks of a macroblock. These average 254 intensities are, up to a constant normalization factor, nothing more 255 that the zeroth DCT coefficients of the Y subblocks. This "higher- 256 level" WHT is a substitute for the explicit specification of those 257 coefficients, in exactly the same way as the DCT of a subblock 258 substitutes for the specification of the pixel values comprising the 259 subblock. We consider this 4x4 array as a second-order subblock 260 called Y2, and think of a macroblock as containing 24 "real" 261 subblocks and, sometimes, a 25th "virtual" subblock. This is dealt 262 with further in Chapter 13. 264 The frame layout used by the reference decoder may be found in the 265 file yv12config.h. 267 3. Compressed Frame Types 269 There are only two types of frames in VP8. 271 Intraframes (also called key frames and, in MPEG terminology, 272 I-frames) are decoded without reference to any other frame in a 273 sequence, that is, the decompressor reconstructs such frames 274 beginning from its "default" state. Key frames provide random access 275 (or seeking) points in a video stream. 277 Interframes (also called prediction frames and, in MPEG terminology, 278 P-frames) are encoded with reference to prior frames, specifically 279 all prior frames up to and including the most recent key frame. 280 Generally speaking, the correct decoding of an interframe depends on 281 the correct decoding of the most recent key frame and all ensuing 282 frames. Consequently, the decoding algorithm is not tolerant of 283 dropped frames: In an environment in which frames may be dropped or 284 corrupted, correct decoding will not be possible until a key frame is 285 correctly received. 287 In contrast to MPEG, there is no use of bidirectional prediction. No 288 frame is predicted using frames temporally subsequent to it; there is 289 no analog to an MPEG B-frame. 291 Secondly, VP8 augments these notions with that of alternate 292 prediction frames, called golden frames and altref frames 293 (alternative reference frames). Blocks in an interframe may be 294 predicted using blocks in the immediately previous frame as well as 295 the most recent golden frame or altref frame. Every key frame is 296 automatically golden and altref, and any interframe may optionally 297 replace the most recent golden or altref frame. 299 Golden frames and altref frames may also be used to partially 300 overcome the intolerance to dropped frames discussed above: If a 301 compressor is configured to code golden frames only with reference to 302 the prior golden frame (and key frame) then the "substream" of key 303 and golden frames may be decoded regardless of loss of other 304 interframes. Roughly speaking, the implementation requires (on the 305 compressor side) that golden frames subsume and recode any context 306 updates effected by the intervening interframes. A typical 307 application of this approach is video conferencing, in which 308 retransmission of a prior golden frame and/or a delay in playback 309 until receipt of the next golden frame is preferable to a larger 310 retransmit and/or delay until the next key frame. 312 4. Overview of Compressed Data Format 314 The input to a VP8 decoder is a sequence of compressed frames whose 315 order matches their order in time. Issues such as the duration of 316 frames, the corresponding audio, and synchronization are generally 317 provided by the playback environment and are irrelevant to the 318 decoding process itself, however, to aid in fast seeking a start code 319 is included in the header of each key frame. 321 The decoder is simply presented with a sequence of compressed frames 322 and produces a sequence of decompressed (reconstructed) YUV frames 323 corresponding to the input sequence. As stated in the introduction, 324 the exact pixel values in the reconstructed frame are part of VP8's 325 specification. This document specifies the layout of the compressed 326 frames and gives unambiguous algorithms for the correct production of 327 reconstructed frames. 329 The first frame presented to the decompressor is of course a key 330 frame. This may be followed by any number of interframes; the 331 correct reconstruction of each frame depends on all prior frames up 332 to the key frame. The next key frame restarts this process: The 333 decompressor resets to its default initial condition upon reception 334 of a key frame and the decoding of a key frame (and its ensuing 335 interframes) is completely independent of any prior decoding. 337 At the highest level, every compressed frame has three or more 338 pieces. It begins with an uncompressed data chunk comprising 10 339 bytes in the case of key frames and 3-bytes for inter frames. This 340 is followed by two or more blocks of compressed data (called 341 partitions). These compressed data partitions begin and end on byte 342 boundaries. 344 The first compressed partition has two subsections: 346 1. Header information that applies to the frame as a whole. 348 2. Per-macroblock information specifying how each macroblock is 349 predicted from the already-reconstructed data that is available 350 to the decompressor. 352 As stated above, the macroblock-level information occurs in raster- 353 scan order. 355 The rest of the partitions contain, for each block, the DCT/WHT 356 coefficients (quantized and logically compressed) of the residue 357 signal to be added to the predicted block values. It typically 358 accounts for roughly 70% of the overall datarate. VP8 supports 359 packing the compressed DCT/WHT coefficients' data from macroblock 360 rows into separate partitions. If there is more than one partition 361 for these coefficients, the sizes of the partitions -- except the 362 last partition -- in bytes are also present in the bitstream right 363 after the above first partition. Each of the sizes is a 3-byte data 364 item written in little endian format. These sizes provide the 365 decoder direct access to all DCT/WHT coefficient partitions, which 366 enables parallel processing of the coefficients in a decoder. 368 The separate partitioning of the prediction data and coefficient data 369 also allows flexibility in the implementation of a decompressor: An 370 implementation may decode and store the prediction information for 371 the whole frame and then decode, transform, and add the residue 372 signal to the entire frame, or it may simultaneously decode both 373 partitions, calculating prediction information and adding in the 374 residue signal for each block in order. The length field in the 375 frame tag, which allows decoding of the second partition to begin 376 before the first partition has been completely decoded, is necessary 377 for the second "block-at-a-time" decoder implementation. 379 All partitions are decoded using separate instances of the boolean 380 entropy decoder described in Chapter 7. Although some of the data 381 represented within the partitions is conceptually "flat" (a bit is 382 just a bit with no probabilistic expectation one way or the other), 383 because of the way such coders work, there is never a direct 384 correspondence between a "conceptual bit" and an actual physical bit 385 in the compressed data partitions. Only in the 3 or 10 byte 386 uncompressed chunk described above is there such a physical 387 correspondence. 389 A related matter, which is true for most lossless compression 390 formats, is that seeking within a partition is not supported. The 391 data must be decompressed and processed (or at least stored) in the 392 order in which it occurs in the partition. 394 While this document specifies the ordering of the partition data 395 correctly, the details and semantics of this data are discussed in a 396 more logical fashion to facilitate comprehension. For example, the 397 frame header contains updates to many probability tables used in 398 decoding per-macroblock data. The latter is often described before 399 the layouts of the probabilities and their updates, even though this 400 is the opposite of their order in the bitstream. 402 5. Overview of the Decoding Process 404 A VP8 decoder needs to maintain four YUV frame buffers whose 405 resolutions are at least equal to that of the encoded image. These 406 buffers hold the current frame being reconstructed, the immediately 407 previous reconstructed frame, the most recent golden frame, and the 408 most recent altref frame. 410 Most implementations will wish to "pad" these buffers with 411 "invisible" pixels that extend a moderate number of pixels beyond all 412 four edges of the visible image. This simplifies interframe 413 prediction by allowing all (or most) prediction blocks -- which are 414 not guaranteed to lie within the visible area of a prior frame -- to 415 address usable image data. 417 Regardless of the amount of padding chosen, the invisible rows above 418 (below) the image are filled with copies of the top (bottom) row of 419 the image; the invisible columns to the left (right) of the image are 420 filled with copies of the leftmost (rightmost) visible row; and the 421 four invisible corners are filled with copies of the corresponding 422 visible corner pixels. The use of these prediction buffers (and 423 suggested sizes for the halo) will be elaborated on in the discussion 424 of motion vectors, interframe prediction, and sub-pixel interpolation 425 later in this document. 427 As will be seen in the description of the frame header, the image 428 dimensions are specified (and can change) with every key frame. 429 These buffers (and any other data structures whose size depends on 430 the size of the image) should be allocated (or re-allocated) 431 immediately after the dimensions are decoded. 433 Leaving most of the details for later elaboration, the following is 434 an outline the decoding process. 436 First, the frame header (beginning of the first data partition) is 437 decoded. Altering or augmenting the maintained state of the decoder, 438 this provides the context in which the per-macroblock data can be 439 interpreted. 441 The macroblock data occurs (and must be processed) in raster-scan 442 order. This data comes in two or more parts. The first (prediction 443 or mode) part comes in the remainder of the first data partition. 444 The other parts comprise the data partition(s) for the DCT/WHT 445 coefficients of the residue signal. For each macroblock, the 446 prediction data must be processed before the residue. 448 Each macroblock is predicted using one (and only one) of four 449 possible frames. All macroblocks in a key frame, and all intra-coded 450 macroblocks in an interframe, are predicted using the already-decoded 451 macroblocks in the current frame. Macroblocks in an interframe may 452 also be predicted using the previous frame, the golden frame or the 453 altref frame. Such macroblocks are said to be inter-coded. 455 The purpose of prediction is to use already-constructed image data to 456 approximate the portion of the original image being reconstructed. 457 The effect of any of the prediction modes is then to write a 458 macroblock-sized prediction buffer containing this approximation. 460 Regardless of the prediction method, the residue DCT signal is 461 decoded, dequantized, reverse-transformed, and added to the 462 prediction buffer to produce the (almost final) reconstruction value 463 of the macroblock, which is stored in the correct position of the 464 current frame buffer. 466 The residue signal consists of 24 (sixteen Y, four U, and four V) 4x4 467 quantized and losslessly-compressed DCT transforms approximating the 468 difference between the original macroblock in the uncompressed source 469 and the prediction buffer. For most prediction modes, the zeroth 470 coefficients of the sixteen Y subblocks are expressed via a 25th WHT 471 of the second-order virtual Y2 subblock discussed above. 473 Intra-prediction exploits the spatial coherence of frames. The 16x16 474 luma (Y) and 8x8 chroma (UV) components are predicted independently 475 of each other using one of four simple means of pixel propagation, 476 starting from the already-reconstructed (16-pixel long luma, 8-pixel 477 long chroma) row above and column to the left of the current 478 macroblock. The four methods are: 480 1. Copying the row from above throughout the prediction buffer. 482 2. Copying the column from left throughout the prediction buffer. 484 3. Copying the average value of the row and column throughout the 485 prediction buffer. 487 4. Extrapolation from the row and column using the (fixed) second 488 difference (horizontal and vertical) from the upper left corner. 490 Additionally, the sixteen Y subblocks may be predicted independently 491 of each other using one of ten different modes, four of which are 4x4 492 analogs of those described above, augmented with six "diagonal" 493 prediction methods. There are two types of predictions, one intra 494 and one prediction (among all the modes), for which the residue 495 signal does not use the Y2 block to encode the DC portion of the 496 sixteen 4x4 Y subblock DCTs. This "independent Y subblock" mode has 497 no effect on the 8x8 chroma prediction. 499 Inter-prediction exploits the temporal coherence between nearby 500 frames. Except for the choice of the prediction frame itself, there 501 is no difference between inter-prediction based on the previous frame 502 and that based on the golden frame or altref frame. 504 Inter-prediction is conceptually very simple. While, for reasons of 505 efficiency, there are several methods of encoding the relationship 506 between the current macroblock and corresponding sections of the 507 prediction frame, ultimately each of the sixteen Y subblocks is 508 related to a 4x4 subblock of the prediction frame, whose position in 509 that frame differs from the current subblock position by a (usually 510 small) displacement. These two-dimensional displacements are called 511 motion vectors. 513 The motion vectors used by VP8 have quarter-pixel precision. 514 Prediction of a subblock using a motion vector that happens to have 515 integer (whole number) components is very easy: the 4x4 block of 516 pixels from the displaced block in the previous, golden, or altref 517 frame are simply copied into the correct position of the current 518 macroblock's prediction buffer. 520 Fractional displacements are conceptually and implementationally more 521 complex. They require the inference (or synthesis) of sample values 522 that, strictly speaking, do not exist. This is one of the most basic 523 problems in signal processing and readers conversant with that 524 subject will see that the approach taken by VP8 provides a good 525 balance of robustness, accuracy, and efficiency. 527 Leaving the details for the implementation discussion below, the 528 pixel interpolation is calculated by applying a kernel filter (using 529 reasonable-precision integer math) three pixels on either side, both 530 horizontally and vertically, of the pixel to be synthesized. The 531 resulting 4x4 block of synthetic pixels is then copied into position 532 exactly as in the case of integer displacements. 534 Each of the eight chroma subblocks is handled similarly. Their 535 motion vectors are never specified explicitly; instead, the motion 536 vector for each chroma subblock is calculated by averaging the 537 vectors of the four Y subblocks that occupy the same area of the 538 frame. Since chroma pixels have twice the diameter (and four times 539 the area) of luma pixels, the calculated chroma motion vectors have 540 1/8 pixel resolution, but the procedure for copying or generating 541 pixels for each subblock is essentially identical to that done in the 542 luma plane. 544 After all the macroblocks have been generated (predicted and 545 corrected with the DCT/WHT residue), a filtering step (the loop 546 filter) is applied to the entire frame. The purpose of the loop 547 filter is to reduce blocking artifacts at the boundaries between 548 macroblocks and between subblocks of the macroblocks. The term loop 549 filter is used because this filter is part of the "coding loop," that 550 is, it affects the reconstructed frame buffers that are used to 551 predict ensuing frames. This is distinguished from the 552 postprocessing filters discussed earlier which affect only the viewed 553 video and do not "feed into" subsequent frames. 555 Next, if signaled in the data, the current frame may replace the 556 golden frame prediction buffer and/or the altref frame buffer. 558 The halos of the frame buffers are next filled as specified above. 559 Finally, at least as far as decoding is concerned, the (references 560 to) the "current" and "last" frame buffers should be exchanged in 561 preparation for the next frame. 563 Various processes may be required (or desired) before viewing the 564 generated frame. As discussed in the frame dimension information 565 below, truncation and/or upscaling of the frame may be required. 566 Some playback systems may require a different frame format (RGB, 567 YUY2, etc.). Finally, as mentioned in the introduction, further 568 postprocessing or filtering of the image prior to viewing may be 569 desired. Since the primary purpose of this document is a decoding 570 specification, the postprocessing is not specified in this document. 572 While the basic ideas of prediction and correction used by VP8 are 573 straightforward, many of the details are quite complex. The 574 management of probabilities is particularly elaborate. Not only do 575 the various modes of intra-prediction and motion vector specification 576 have associated probabilities but they, together with the coding of 577 DCT coefficients and motion vectors, often base these probabilities 578 on a variety of contextual information (calculated from what has been 579 decoded so far), as well as on explicit modification via the frame 580 header. 582 The "top-level" of decoding and frame reconstruction is implemented 583 in the reference decoder files onyxd_if.c and decodframe.c. 585 This concludes our summary of decoding and reconstruction; we 586 continue by discussing the individual aspects in more depth. 588 A reasonable "divide and conquer" approach to implementation of a 589 decoder is to begin by decoding streams composed exclusively of key 590 frames. After that works reliably, interframe handling can be added 591 more easily than if complete functionality were attempted 592 immediately. In accordance with this, we first discuss components 593 needed to decode key frames (most of which are also used in the 594 decoding of interframes) and conclude with topics exclusive to 595 interframes. 597 6. Description of Algorithms 599 As the intent of this document, together with the reference decoder 600 source code, is to specify a platform-independent procedure for the 601 decoding and reconstruction of a VP8 video stream, many (small) 602 algorithms must be described exactly. 604 Due to its near-universality, terseness, ability to easily describe 605 calculation at specific precisions, and the fact that On2's reference 606 VP8 decoder is written in C, these algorithm fragments are written 607 using the C programming language, augmented with a few simple 608 definitions below. 610 The standard (and best) reference for C is [Kernighan]. 612 Many code fragments will be presented in this document. Some will be 613 nearly identical to corresponding sections of the reference decoder; 614 others will differ. Roughly speaking, there are three reasons for 615 such differences: 617 1. For reasons of efficiency, the reference decoder version may be 618 less obvious. 620 2. The reference decoder often uses large data structures to 621 maintain context that need not be described or used here. 623 3. The authors of this document felt that a different expression of 624 the same algorithm might facilitate exposition. 626 Regardless of the chosen presentation, the calculation effected by 627 any of the algorithms described here is identical to that effected by 628 the corresponding portion of the reference decoder. 630 All VP8 decoding algorithms use integer math. To facilitate 631 specification of arithmetic precision, we define the following types. 633 ---- Begin code block -------------------------------------- 635 typedef signed char int8; /* signed int exactly 8 bits wide */ 636 typedef unsigned char uint8; /* unsigned "" */ 638 typedef short int16; /* signed int exactly 16 bits wide */ 639 typedef unsigned int16 uint16; /* unsigned "" */ 641 /* int32 is a signed integer type at least 32 bits wide */ 643 typedef long int32; /* guaranteed to work on all systems */ 644 typedef int int32; /* will be more efficient on some systems */ 646 typedef unsigned int32 uint32; 648 /* unsigned integer type, at least 16 bits wide, whose exact size 649 is most convenient to whatever processor we are using */ 651 typedef unsigned int uint; 653 /* While pixels themselves are 8-bit unsigned integers, 654 pixel arithmetic often occurs at 16- or 32-bit precision and 655 the results need to be "saturated" or clamped to an 8-bit 656 range. */ 658 typedef uint8 Pixel; 660 Pixel clamp255( int32 v) { return v < 0? 0 : (v < 255? v : 255);} 662 /* As is elaborated in the discussion of the bool_decoder below, 663 VP8 represents probabilities as unsigned 8-bit numbers. */ 665 typedef uint8 Prob; 667 ---- End code block ---------------------------------------- 669 We occasionally need to discuss mathematical functions involving 670 honest-to-goodness "infinite precision" real numbers. The DCT is 671 first described via the cosine function cos; the ratio of the lengths 672 of the circumference and diameter of a circle is denoted pi; at one 673 point, we take a (base 1/2) logarithm denoted log; and pow( x, y) 674 denotes x raised to the power y. If x = 2 and y is a small non- 675 negative integer, pow( 2, y) may be expressed in C as 1 << y. 677 Finally, we sometimes need to divide signed integers by powers of 678 two, that is, we occasionally right-shift signed numbers. The 679 behavior of such shifts (i.e., the propagation of the sign bit) is, 680 perhaps surprisingly, not defined by the C language itself and is 681 left up to individual compilers. Because of the utility of this 682 frequently needed operation, it is at least arguable that it should 683 be defined by the language (to naturally propagate the sign bit) and, 684 at a minimum, should be correctly implemented by any reasonable 685 compiler. In the interest of strict portability, we attempt to call 686 attention to these shifts when they arise. 688 7. Boolean Entropy Decoder 690 As discussed in the overview above, essentially the entire VP8 data 691 stream is encoded using a boolean entropy coder. 693 An understanding of the bool_decoder is critical to the 694 implementation of a VP8 decompressor, so we discuss in detail. It is 695 easier to comprehend the bool_decoder in conjunction with the 696 bool_encoder used by the compressor to write the compressed data 697 partitions. 699 The bool_encoder encodes (and the bool_decoder decodes) one bool 700 (zero-or-one boolean value) at a time. Its purpose is to losslessly 701 compress a sequence of bools for which the probability of their being 702 zero or one can be well-estimated (via constant or previously-coded 703 information) at the time they are written, using identical 704 corresponding probabilities at the time they are read. 706 As the reader is probably aware, if a bool is much more likely to be 707 zero than one (for instance), it can, on average, be faithfully 708 encoded using much less than one bit per value. The bool_encoder 709 exploits this. 711 In the 1940s, [Shannon] proved that there is a lower bound for the 712 average datarate of a faithful encoding of a sequence of bools (whose 713 probability distributions are known and are independent of each 714 other) and also that there are encoding algorithms that approximate 715 this lower bound as closely as one wishes. 717 If we encode a sequence of bools whose probability of being zero is p 718 (and whose probability of being 1 is 1-p), the lowest possible 719 datarate per value is 721 plog(p) + (1-p)log(1-p); 723 taking the logarithms to the base 1/2 expresses the datarate in bits/ 724 value. 726 We give two simple examples. At one extreme, if p=1/2, then log(p) = 727 log(1-p) = 1 and the lowest possible datarate per bool is 1/2 + 1/2 = 728 1, that is, we cannot do any better than simply literally writing out 729 bits. At another extreme, if p is very small, say p=1/1024, then 730 log(p)=10, log(1-p) is roughly .0014, and the lowest possible 731 datarate is approximately 10/1024 + .0014, roughly 1/100 of a bit per 732 bool. 734 Because most of the bools in the VP8 datastream have zero- 735 probabilities nowhere near 1/2, the compression provided by the 736 bool_encoder is critical to the performance of VP8. 738 The bool coder used by VP8 is a variant of an arithmetic coder. An 739 excellent discussion of arithmetic coding (and other lossless 740 compression techniques) can be found in [Bell]. 742 7.1. Underlying Theory of Coding 744 The basic idea used by the bool coder is to consider the entire data 745 stream (either of the partitions in our case) as the binary expansion 746 of a single number x with 0 <= x < 1. The bits (or bytes) in x are 747 of course written from high to low order and if b[j] (B[j]) is the 748 j^(th) bit (byte) in the partition, the value x is simply the sum 749 (starting with j = 1) of pow(2, -j) * b[j] or pow(256, -j) * B[j]. 751 Before the first bool is coded, all values of x are possible. 753 The coding of each bool restricts the possible values of x in 754 proportion to the probability of what is coded. If p1 is the 755 probability of the first bool being zero and a zero is coded, the 756 range of possible x is restricted to 0 <= x < p1. If a one is coded, 757 the range becomes p1 <= x < 1. 759 The coding continues by repeating the same idea. At every stage, 760 there is an interval a <= x < b of possible values of x. If p is the 761 probability of a zero being coded at this stage and a zero is coded, 762 the interval becomes a <= x < a + (p(b-a)). If a one is coded, the 763 possible x are restricted to a + (p(b-a)) <= x < b. 765 Assuming only finitely many values are to be coded, after the encoder 766 has received the last bool, it can write as its output any value x 767 that lies in the final interval. VP8 simply writes the left endpoint 768 of the final interval. Consequently, the output it would make if 769 encoding were to stop at any time either increases or stays the same 770 as each bool is encoded. 772 Decoding parallels encoding. The decoder is presented with the 773 number x, which has only the initial restriction 0 <= x < 1. To 774 decode the first bool, the decoder is given the first probability p1. 775 If x < p1, a zero is decoded; if x >= p1, a one is decoded. In 776 either case, the new restriction on x, that is, the interval of 777 possible x, is remembered. 779 Decoding continues in exactly the same way: If a <= x < b is the 780 current interval and we are to decode a bool with zero-probability p, 781 we return a zero if a <= x < a + (p(b-a)) and a one if a + (p(b-a)) 782 <= x < b. In either case, the new restriction is remembered in 783 preparation for decoding the next bool. 785 The process outlined above uses real numbers of infinite precision to 786 express the probabilities and ranges. It is true that, if one could 787 actualize this process and coded a large number of bools whose 788 supplied probabilities matched their value distributions, the 789 datarate achieved would approach the theoretical minimum as the 790 number of bools encoded increased. 792 Unfortunately, computers operate at finite precision and an 793 approximation to the theoretically perfect process described above is 794 necessary. Such approximation increases the datarate but, at quite 795 moderate precision and for a wide variety of data sets, this increase 796 is negligible. 798 The only conceptual limitations are, first, that coder probabilities 799 must be expressed at finite precision and, second, that the decoder 800 be able to detect each individual modification to the value interval 801 via examination of a fixed amount of input. As a practical matter, 802 many of the implementation details stem from the fact that the coder 803 can function using only a small "window" to incrementally read or 804 write the arbitrarily precise number x. 806 7.2. Practical Algorithm Description 808 VP8's bool coder works with 8-bit probabilities p. The range of such 809 p is 0 <= p <= 255; the actual probability represented by p is p/256. 810 Also, the coder is designed so that decoding of a bool requires no 811 more than an 8-bit comparison and so that the state of both the 812 encoder and decoder can be easily represented using a small number of 813 unsigned 16-bit integers. 815 The details are most easily understood if we first describe the 816 algorithm using bit-at-a-time input and output. Aside from the 817 ability to maintain a position in this bitstream and write/read bits, 818 the encoder also needs the ability to add 1 to the bits already 819 output; after writing n bits, adding 1 to the existing output is the 820 same thing as adding pow( 2, -n) to x. 822 Together with the bit position, the encoder must maintain two 823 unsigned 8-bit numbers which we call bottom and range. Writing w for 824 the n bits already written and S = pow( 2, - n - 8) for the scale of 825 the current bit position one byte out, we have the following 826 constraint on all future values v of w (including the final value v = 827 x): 829 w + ( S * bottom ) <= v < w + ( S * ( bottom + range ) ) 831 Thus, appending bottom to the already-written bits w gives the left 832 endpoint of the interval of possible values, appending bottom + range 833 gives the right endpoint, range itself (scaled to the current output 834 position) is the length of the interval. 836 So that our probabilistic encodings are reasonably accurate, we do 837 not let range vary by more than a factor of two: It stays within the 838 bounds 128 <= range <= 255. 840 The process for encoding a boolean value val whose probability of 841 being zero is prob / 256 -- and whose probability of being one is ( 842 256 - prob ) / 256 -- with 1 <= prob <= 255 is as follows. 844 Using an unsigned 16-bit multiply followed by an unsigned right 845 shift, we calculate an unsigned 8-bit split value: 847 split = 1 + (((range - 1) * probability)]] >> 8) 849 split is approximately ( prob / 256 ) * range and lies within the 850 bounds 1 <= split <= range - 1. These bounds ensure the correctness 851 of the decoding procedure described below. 853 If the incoming boolean val to be encoded is false, we leave the left 854 interval endpoint bottom alone and reduce range, replacing it by 855 split. If the incoming val is true, we move up the left endpoint to 856 bottom + split, propagating any carry to the already-written value w 857 (this is where we need the ability to add 1 to w), and reduce range 858 to range - split. 860 Regardless of the value encoded, range has been reduced and now has 861 the bounds 1 <= range <= 254. If range < 128, the encoder doubles it 862 and shifts the high-order bit out of bottom to the output as it also 863 doubles bottom, repeating this process one bit at a time until 128 <= 864 range <= 255. Once this is completed, the encoder is ready to accept 865 another bool, maintaining the constraints described above. 867 After encoding the last bool, the partition may be completed by 868 appending bottom to the bitstream. 870 The decoder mimics the state of the encoder. It maintains, together 871 with an input bit position, two unsigned 8-bit numbers, a range 872 identical to that maintained by the encoder and a value. Decoding 873 one bool at a time, the decoder (in effect) tracks the same left 874 interval endpoint as does the encoder and subtracts it from the 875 remaining input. Appending the unread portion of the bitstream to 876 the 8-bit value gives the difference between the actual value encoded 877 and the known left endpoint. 879 The decoder is initialized by setting range = 255 and reading the 880 first 16 input bits into value. The decoder maintains range and 881 calculates split in exactly the same way as does the encoder. 883 To decode a bool, it compares value to split; if value < split, the 884 bool is zero, and range is replaced with split. If value >= split, 885 the bool is one, range is replaced with range - split, and value is 886 replaced with value - split. 888 Again, range is doubled one bit at a time until it is at least 128. 889 The value is doubled in parallel, shifting a new input bit into the 890 bottom each time. 892 Writing Value for value together with the unread input bits and Range 893 for range extended indefinitely on the right by zeros, the condition 894 Value < Range is maintained at all times by the decoder. In 895 particular, the bits shifted out of value as it is doubled are always 896 zero. 898 7.3. Actual Implementation 900 The C code below gives complete implementations of the encoder and 901 decoder described above. While they are logically identical to the 902 "bit-at-a-time" versions, they internally buffer a couple of extra 903 bytes of the bitstream. This allows I/O to be done (more 904 practically) a byte at a time and drastically reduces the number of 905 carries the encoder has to propagate into the already-written data. 907 Another (logically equivalent) implementation may be found in the 908 reference decoder files dboolhuff.h and dboolhuff.c. 910 ---- Begin code block -------------------------------------- 912 /* Encoder first */ 914 typedef struct { 915 uint8 *output; /* ptr to next byte to be written */ 916 uint32 range; /* 128 <= range <= 255 */ 917 uint32 bottom; /* minimum value of remaining output */ 918 int bit_count; /* # of shifts before an output byte 919 is available */ 920 } bool_encoder; 922 /* Must set initial state of encoder before writing any bools. */ 924 void init_bool_encoder( bool_encoder *e, uint8 *start_partition) 925 { 926 e->output = start_partition; 927 e->range = 255; 928 e->bottom = 0; 929 e->bit_count = 24; 930 } 932 /* Encoding very rarely produces a carry that must be propagated 933 to the already-written output. The arithmetic guarantees that 934 the propagation will never go beyond the beginning of the 935 output. Put another way, the encoded value x is always less 936 than one. */ 938 void add_one_to_output( uint8 *q) 939 { 940 while( *--q == 255) 941 *q = 0; 942 ++*q; 943 } 945 /* Main function writes a bool_value whose probability of being 946 zero is (expected to be) prob/256. */ 948 void write_bool( bool_encoder *e, Prob prob, int bool_value) 949 { 950 /* split is approximately (range * prob) / 256 and, 951 crucially, is strictly bigger than zero and strictly 952 smaller than range */ 954 uint32 split = 1 + ( ((e->range - 1) * prob) >> 8); 956 if( bool_value) { 957 e->bottom += split; /* move up bottom of interval */ 958 e->range -= split; /* with corresponding decrease in range */ 959 } else 960 e->range = split; /* decrease range, leaving bottom alone */ 962 while( e->range < 128) 963 { 964 e->range <<= 1; 966 if( e->bottom & (1 << 31)) /* detect carry */ 967 add_one_to_output( e->output); 969 e->bottom <<= 1; /* before shifting bottom */ 971 if( !--e->bit_count) { /* write out high byte of bottom ... */ 973 *e->output++ = (uint8) (e->bottom >> 24); 975 e->bottom &= (1 << 24) - 1; /* ... keeping low 3 bytes */ 976 e->bit_count = 8; /* 8 shifts until next output */ 977 } 978 } 979 } 981 /* Call this function (exactly once) after encoding the last 982 bool value for the partition being written */ 984 void flush_bool_encoder( bool_encoder *e) 985 { 986 int c = e->bit_count; 987 uint32 v = e->bottom; 989 if( v & (1 << (32 - c))) /* propagate (unlikely) carry */ 990 add_one_to_output( e->output); 991 v <<= c & 7; /* before shifting remaining output */ 992 c >>= 3; /* to top of internal buffer */ 993 while( --c >= 0) 994 v <<= 8; 995 c = 4; 996 while( --c >= 0) { /* write remaining data, possibly padded */ 997 *e->output++ = (uint8) (v >> 24); 998 v <<= 8; 999 } 1000 } 1002 /* Decoder state exactly parallels that of the encoder. 1003 "value", together with the remaining input, equals the 1004 complete encoded number x less the left endpoint of the 1005 current coding interval. */ 1007 typedef struct { 1008 uint8 *input; /* pointer to next compressed data byte */ 1009 uint32 range; /* always identical to encoder's range */ 1010 uint32 value; /* contains at least 8 significant bits */ 1011 int bit_count; /* # of bits shifted out of 1012 value, at most 7 */ 1013 } bool_decoder; 1015 /* Call this function before reading any bools from the 1016 partition.*/ 1018 void init_bool_decoder( bool_decoder *d, uint8 *start_partition) 1019 { 1020 { 1021 int i = 0; 1022 d->value = 0; /* value = first 2 input bytes */ 1023 while( ++i <= 2) 1024 d->value = (d->value << 8) | *start_partition++; 1025 } 1027 d->input = start_partition; /* ptr to next byte to be read */ 1028 d->range = 255; /* initial range is full */ 1029 d->bit_count = 0; /* have not yet shifted out any bits */ 1030 } 1032 /* Main function reads a bool encoded at probability prob/256, 1033 which of course must agree with the probability used when the 1034 bool was written. */ 1036 int read_bool( bool_decoder *d, Prob prob) 1037 { 1038 /* range and split are identical to the corresponding values 1039 used by the encoder when this bool was written */ 1041 uint32 split = 1 + ( ((d->range - 1) * prob) >> 8); 1042 uint32 SPLIT = split << 8; 1043 int retval; /* will be 0 or 1 */ 1045 if( d->value >= SPLIT) { /* encoded a one */ 1046 retval = 1; 1047 d->range -= split; /* reduce range */ 1048 d->value -= SPLIT; /* subtract off left endpoint of interval */ 1049 } else { /* encoded a zero */ 1050 retval = 0; 1051 d->range = split; /* reduce range, no change in left endpoint */ 1052 } 1054 while( d->range < 128) { /* shift out irrelevant value bits */ 1055 d->value <<= 1; 1056 d->range <<= 1; 1057 if( ++d->bit_count == 8) { /* shift in new bits 8 at a time */ 1058 d->bit_count = 0; 1059 d->value |= *d->input++; 1060 } 1061 } 1062 return retval; 1063 } 1065 /* Convenience function reads a "literal", that is, a "num_bits" 1066 wide unsigned value whose bits come high- to low-order, with 1067 each bit encoded at probability 128 (i.e., 1/2). */ 1069 uint32 read_literal( bool_decoder *d, int num_bits) 1070 { 1071 uint32 v = 0; 1072 while( num_bits--) 1073 v = (v << 1) + read_bool( d, 128); 1074 return v; 1075 } 1077 /* Variant reads a signed number */ 1079 int32 read_signed_literal( bool_decoder *d, int num_bits) 1080 { 1081 int32 v = 0; 1082 if( !num_bits) 1083 return 0; 1084 if( read_bool( d, 128)) 1085 v = -1; 1086 while( --num_bits) 1087 v = (v << 1) + read_bool( d, 128); 1088 return v; 1089 } 1091 ---- End code block ---------------------------------------- 1093 8. Compressed Data Components 1095 At the lowest level, VP8's compressed data is simply a sequence of 1096 probabilistically-encoded bools. Most of this data is composed of 1097 (slightly) larger semantic units fashioned from bools, which we 1098 describe here. 1100 We sometimes use these descriptions in C expressions within data 1101 format specifications. In this context, they refer to the return 1102 value of a call to an appropriate bool_decoder d, reading (as always) 1103 from its current reference point. 1105 +--------------+-------+--------------------------------------------+ 1106 | Call | Alt. | Return | 1107 +--------------+-------+--------------------------------------------+ 1108 | Bool(p) | B(p) | Bool with probability p/256 of being 0. | 1109 | | | Return value of read_bool(d, p). | 1110 | | | | 1111 | Flag | F | A one-bit flag (same thing as a B(128) or | 1112 | | | an L(1)). Abbreviated F. Return value of | 1113 | | | read_bool(d, 128). | 1114 | | | | 1115 | Lit(n) | L(n) | Unsigned n-bit number encoded as n flags | 1116 | | | (a "literal"). Abbreviated L(n). The bits | 1117 | | | are read from high to low order. Return | 1118 | | | value of read_literal(d, n). | 1119 | | | | 1120 | SignedLit(n) | | Signed n-bit number encoded similarly to | 1121 | | | an L(n). Return value of | 1122 | | | read_signed_literal(d, n). These are rare. | 1123 | | | | 1124 | P(8) | | An 8-bit probability. No different from an | 1125 | | | L(8), but we sometimes use this notation | 1126 | | | to emphasize that a probability is being | 1127 | | | coded. | 1128 | | | | 1129 | P(7) | | A 7-bit specification of an 8-bit | 1130 | | | probability. Coded as an L(7) number x; | 1131 | | | the resulting 8-bit probability is x ? x | 1132 | | | << 1 : 1. | 1133 | | | | 1134 | F? X | | A flag which, if true, is followed by a | 1135 | | | piece of data X. | 1136 | | | | 1137 | F? X:Y | | A flag which, if true, is followed by X | 1138 | | | and, if false, is followed by Y. Also used | 1139 | | | to express a value where Y is an implicit | 1140 | | | default (not encoded in the data stream), | 1141 | | | as in F? P(8):255, which expresses an | 1142 | | | optional probability: if the flag is true, | 1143 | | | the probability is specified as an 8-bit | 1144 | | | literal, while if the flag is false, the | 1145 | | | probability defaults to 255. | 1146 | | | | 1147 | B(p)? X | B(p)? | Variants of the above using a boolean | 1148 | | X:Y | indicator whose probability is not | 1149 | | | necessarily 128. | 1150 | | | | 1151 | X | | Multi-component field, the specifics of | 1152 | | | which will be given at a more appropriate | 1153 | | | point in the discussion. | 1154 | | | | 1155 | T | | Tree-encoded value from small alphabet. | 1156 +--------------+-------+--------------------------------------------+ 1158 The last type requires elaboration. We often wish to encode 1159 something whose value is restricted to a small number of 1160 possibilities (the alphabet). 1162 This is done by representing the alphabet as the leaves of a small 1163 binary tree. The (non-leaf) nodes of the tree have associated 1164 probabilities p and correspond to calls to read_bool(d, p). We think 1165 of a zero as choosing the left branch below the node and a one as 1166 choosing the right branch. 1168 Thus every value (leaf) whose tree depth is x is decoded after 1169 exactly x calls to read_bool. 1171 A tree representing an encoding of an alphabet of n possible values 1172 always contains n-1 non-leaf nodes, regardless of its shape (this is 1173 easily seen by induction on n). 1175 There are many ways that a given alphabet can be so represented. The 1176 choice of tree has little impact on datarate but does affect decoder 1177 performance. The trees used by VP8 are chosen to (on average) 1178 minimize the number of calls to read_bool. This amounts to shaping 1179 the tree so that more probable values have smaller tree depth than do 1180 less probable values. 1182 Readers familiar with Huffman coding will notice that, given an 1183 alphabet together with probabilities for each value, the associated 1184 Huffman tree minimizes the expected number of calls to read_bool. 1186 Such readers will also realize that the coding method described here 1187 never results in higher datarates than does the Huffman method and, 1188 indeed, often results in much lower datarates. Huffman coding is, in 1189 fact, nothing more than a special case of this method in which each 1190 node probability is fixed at 128 (i.e., 1/2). 1192 8.1. Tree Coding Implementation 1194 We give a suggested implementation of a tree data structure followed 1195 by a couple of actual examples of its usage by VP8. 1197 It is most convenient to represent the values using small positive 1198 integers, typically an enum counting up from zero. The largest 1199 alphabet (used to code DCT coefficients, described in Chapter 13 that 1200 is tree-coded by VP8 has only 12 values. The tree for this alphabet 1201 adds 11 interior nodes and so has a total of 23 positions. Thus, an 1202 8-bit number easily accommodates both a tree position and a return 1203 value. 1205 A tree may then be compactly represented as an array of (pairs of) 1206 8-bit integers. Each (even) array index corresponds to an interior 1207 node of the tree;, the zeroth index of course corresponds to the root 1208 of the tree. The array entries come in pairs corresponding to the 1209 left (0) and right (1) branches of the subtree below the interior 1210 node. We use the convention that a positive (even) branch entry is 1211 the index of a deeper interior node, while a nonpositive entry v 1212 corresponds to a leaf whose value is -v. 1214 The node probabilities associated to a tree-coded value are stored in 1215 an array whose indices are half the indices of the corresponding tree 1216 positions. The length of the probability array is one less than the 1217 size of the alphabet. 1219 Here is C code implementing the foregoing. The advantages of our 1220 data structure should be noted. Aside from the smallness of the 1221 structure itself, the tree-directed reading algorithm is essentially 1222 a single line of code. 1224 ---- Begin code block -------------------------------------- 1226 /* A tree specification is simply an array of 8-bit integers. */ 1228 typedef int8 tree_index; 1229 typedef const tree_index Tree[]; 1231 /* Read and return a tree-coded value at the current decoder 1232 position. */ 1234 int treed_read( 1235 bool_decoder * const d, /* bool_decoder always returns a 0 or 1 */ 1236 Tree t, /* tree specification */ 1237 const Prob p[] /* corresponding interior node probabilities */ 1238 ) { 1239 register tree_index i = 0; /* begin at root */ 1241 /* Descend tree until leaf is reached */ 1243 while( ( i = t[ i + read_bool( d, p[i>>1]) ] ) > 0) {} 1245 return -i; /* return value is negation of nonpositive index */ 1246 } 1248 ---- End code block ---------------------------------------- 1250 Tree-based decoding is implemented in the reference decoder file 1251 tree_reader.h. 1253 8.2. Tree Coding Example 1255 As a multi-part example, without getting too far into the semantics 1256 of macroblock decoding (which is of course taken up below), we look 1257 at the "mode" coding for intra-predicted macroblocks. 1259 It so happens that, because of a difference in statistics, the Y (or 1260 luma) mode encoding uses two different trees: one for key frames and 1261 another for interframes. This is the only instance in VP8 of the 1262 same dataset being coded by different trees under different 1263 circumstances. The UV (or chroma) modes are a proper subset of the Y 1264 modes and, as such, have their own decoding tree. 1266 ---- Begin code block -------------------------------------- 1268 typedef enum 1269 { 1270 DC_PRED, /* predict DC using row above and column to the left */ 1271 V_PRED, /* predict rows using row above */ 1272 H_PRED, /* predict columns using column to the left */ 1273 TM_PRED, /* propagate second differences a la "true motion" */ 1275 B_PRED, /* each Y subblock is independently predicted */ 1277 num_uv_modes = B_PRED, /* first four modes apply to chroma */ 1278 num_ymodes /* all modes apply to luma */ 1279 } 1280 intra_mbmode; 1282 /* The aforementioned trees together with the implied codings as 1283 comments. 1284 Actual (i.e., positive) indices are always even. 1285 Value (i.e., nonpositive) indices are arbitrary. */ 1287 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 1288 { 1289 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 1290 4, 6, /* "1" subtree has 2 descendant subtrees */ 1291 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 1292 H_PRED = "101" */ 1293 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 1294 B_PRED = "111" */ 1295 }; 1297 const tree_index kf_ymode_tree [2 * (num_ymodes - 1)] = 1298 { 1299 -B_PRED, 2, /* root: B_PRED = "0", "1" subtree */ 1300 4, 6, /* "1" subtree has 2 descendant subtrees */ 1301 -DC_PRED, -V_PRED, /* "10" subtree: DC_PRED = "100", 1302 V_PRED = "101" */ 1303 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1304 TM_PRED = "111" */ 1305 }; 1307 const tree_index uv_mode_tree [2 * (num_uv_modes - 1)] = 1308 { 1309 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 1310 -V_PRED, 4, /* "1" subtree: V_PRED = "10", 1311 "11" subtree */ 1312 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1313 TM_PRED = "111" */ 1314 }; 1316 /* Given a bool_decoder d, a Y mode might be decoded as follows.*/ 1318 const Prob pretend_its_huffman [num_ymodes - 1] = 1319 { 128, 128, 128, 128}; 1321 Ymode = (intra_mbmode) treed_read( d, ymode_tree, 1322 pretend_its_huffman); 1324 ---- End code block ---------------------------------------- 1326 Since it greatly facilitates re-use of reference code and since there 1327 is no real reason to do otherwise, it is strongly suggested that any 1328 decoder implementation use exactly the same enumeration values and 1329 probability table layouts as described in this document (and in the 1330 reference code) for all tree-coded data in VP8. 1332 9. Frame Header 1334 The uncompressed data chunk at the start of each frame and the first 1335 part of the first data partition contains information pertaining to 1336 the frame as a whole. We list the fields in the order of occurrence, 1337 giving details for some of the fields. Other details are postponed 1338 until a more logical point in our overall description. Most of the 1339 header decoding occurs in the reference decoder file decodeframe.c. 1341 9.1. Uncompressed Data Chunk 1343 The uncompressed data chunk comprises a common (for key frames and 1344 interframes) 3-byte frame tag that contains four fields, as follows: 1346 1. A 1-bit frame type (0 for key frames, 1 for interframes). 1348 2. A 3-bit version number (0 - 3 are defined as four different 1349 profiles with different decoding complexity; other values may be 1350 defined for future variants of the VP8 data format). 1352 3. A 1-bit show_frame flag (0 when current frame is not for display, 1353 1 when current frame is for display). 1355 4. A 19-bit field containing the size of the first data partition in 1356 bytes. 1358 Version number enables or disables certain features in the bitstream, 1359 as follows: 1361 +---------+-------------------------+-------------+ 1362 | Version | Reconstruction filter | Loop filter | 1363 +---------+-------------------------+-------------+ 1364 | 0 | Bicubic | Normal | 1365 | | | | 1366 | 1 | Bilinear | Simple | 1367 | | | | 1368 | 2 | Bilinear | None | 1369 | | | | 1370 | 3 | None | None | 1371 | | | | 1372 | Other | Reserved for future use | | 1373 +---------+-------------------------+-------------+ 1375 The reference software also adjusts the loop filter based on version 1376 number, as per the table above. Version number 1 implies a "simple" 1377 loop filter and version numbers 2 and 3 imply no loop filter. 1378 However, the "simple" filter setting in this context has no effect 1379 whatsoever on the decoding process, and the "no loop filter" setting 1380 only forces the reference encoder to set filter level equal to 0. 1381 Neither affect the decoding process. In decoding, the only loop 1382 filter settings that matter are those in the frame header. 1384 For key frames the frame tag is followed by a further 7 bytes of 1385 uncompressed data, as follows: 1387 ---- Begin code block -------------------------------------- 1389 Start code byte 0 0x9d 1390 Start code byte 1 0x01 1391 Start code byte 2 0x2a 1393 16 bits : (2 bits Horizontal Scale << 14) | Width (14 bits) 1394 16 bits : (2 bits Vertical Scale << 14) | Height (14 bits) 1396 ---- End code block ---------------------------------------- 1398 The following source code segment illustrates validation of the start 1399 code and reading the width, height and scale factors for a key frame. 1401 ---- Begin code block -------------------------------------- 1403 unsigned char *c = pbi->Source+3; 1405 // vet via sync code 1406 if(c[0]!=0x9d||c[1]!=0x01||c[2]!=0x2a) 1407 return -1; 1409 ---- End code block ---------------------------------------- 1411 where pbi->source points to the beginning of the frame. 1413 The following code reads the image dimension from the bitstream: 1415 ---- Begin code block -------------------------------------- 1417 pc->Width = swap2(*(unsigned short*)(c+3))&0x3fff; 1418 pc->horiz_scale = swap2(*(unsigned short*)(c+3))>>14; 1419 pc->Height = swap2(*(unsigned short*)(c+5))&0x3fff; 1420 pc->vert_scale = swap2(*(unsigned short*)(c+5))>>14; 1422 ---- End code block ---------------------------------------- 1424 where swap2 macro takes care of the endian on different platform: 1426 ---- Begin code block -------------------------------------- 1428 #if defined(__ppc__) || defined(__ppc64__) 1429 # define swap2(d) \ 1430 ((d&0x000000ff)<<8) | \ 1431 ((d&0x0000ff00)>>8) 1432 #else 1433 # define swap2(d) d 1434 #endif 1436 ---- End code block ---------------------------------------- 1438 While each frame is encoded as a raster scan of 16x16 macroblocks, 1439 the frame dimensions are not necessarily evenly divisible by 16. In 1440 this case, write ew = 16 - (width & 15) and eh = 16 - (height & 15) 1441 for the excess width and height, respectively. Although they are 1442 encoded, the last ew columns and eh rows are not actually part of the 1443 image and should be discarded before final output. However, these 1444 "excess pixels" should be maintained in the internal reconstruction 1445 buffer used to predict ensuing frames. 1447 The scaling specifications for each dimension are encoded as follows. 1449 +-------+--------------------------------------+ 1450 | Value | Scaling | 1451 +-------+--------------------------------------+ 1452 | 0 | No upscaling (the most common case). | 1453 | | | 1454 | 1 | Upscale by 5/4. | 1455 | | | 1456 | 2 | Upscale by 5/3. | 1457 | | | 1458 | 3 | Upscale by 2. | 1459 +-------+--------------------------------------+ 1461 Upscaling does not affect the reconstruction buffer, which should be 1462 maintained at the encoded resolution. Any reasonable method of 1463 upsampling (including any that may be supported by video hardware in 1464 the playback environment) may be used. Since scaling has no effect 1465 on decoding, we do not discuss it any further. 1467 As discussed in Chapter 5, allocation (or re-allocation) of data 1468 structures (such as the reconstruction buffer) whose size depends on 1469 dimension will be triggered here. 1471 9.2. Color Space and Pixel Type (Key Frames-only) 1473 +-------+------------------------------------------+ 1474 | Field | Value | 1475 +-------+------------------------------------------+ 1476 | L(1) | 1-bit color space type specification | 1477 | | | 1478 | L(1) | 1-bit pixel value clamping specification | 1479 +-------+------------------------------------------+ 1481 The color space type bit is encoded as the following: 1483 o 0 - YUV color space similar to the YCrCb color space defined in 1484 [ITU-R_BT.601] 1486 o 1 - Reserved for future use 1488 The pixel value clamping type bit is encoded as the following: 1490 o 0 - Decoders are required to clamp the reconstructed pixel values 1491 to between 0 and 255 (inclusive). 1493 o 1 - Reconstructed pixel values are guaranteed to be between 0 and 1494 255, no clamping is necessary. 1496 Information in this subsection does not appear in interframes. 1498 9.3. Segment-based Adjustments 1500 This subsection contains probability and value information for 1501 implementing segment adaptive adjustments to default decoder 1502 behaviors. The data in this section is used in the decoding of the 1503 ensuing per-segment information and applies to the entire frame. 1504 When segment adaptive adjustments are enabled, each macroblock will 1505 be assigned a segment ID. Macroblocks with the same segment ID 1506 belong to same segment, and have the same adaptive adjustments over 1507 default baseline values for the frame. The adjustments can be 1508 quantization level or loop filter strength. 1510 The context for decoding this feature at macroblock level is provided 1511 by a subsection in the frame header, which contains:: 1513 1. A segmentation_enabled Flag which if 1 (0), enables (disables) 1514 the feature for this frame. The remaining fields occur if the 1515 feature is enabled. 1517 2. L(1) indicates if the segment map is updated for the current 1518 frame (update_mb_segmentaton_map) 1520 3. L(1) indicates if the segment feature data items are updated for 1521 the current frame 1523 4. If flag in 3 is 1, the following fields occur: 1525 1. L(1) the mode of segment feature data, can be absolute value 1526 mode or delta value mode, later mode, feature data is the 1527 difference against current frame defaults. 1529 2. Segment feature data items are decoded segment by each 1530 segment for each segment feature. For every data item, a one 1531 bit flag indicating if the item is 0 or a non-zero value to 1532 be decoded. If there is non-zero value, the value is decoded 1533 as a magnitude L(n) followed by a one bit sign (L(1), 0 for 1534 positive and 1 for negative). The length n can be looked up 1535 from a pre-defined length table for all feature data. 1537 5. If flag in 2 is 1, the probabilities of the decoding tree for 1538 segment map are decoded from the bitstream. Each probability is 1539 decoded with one bit flag indicating if the probability is the 1540 default value of 255 (flag is 0), or the probability is an 8-bit 1541 value, L(8), from the bitstream. 1543 The layout and semantics supporting this feature at the macroblock 1544 level will be described in Chapter 10. 1546 9.4. Loop Filter Type and Levels 1548 VP8 supports two types of loop filter, having different computational 1549 complexity. The following bits occur in the header to support the 1550 selection of the baseline type, strength and sharpness behavior of 1551 the loop filter used for the current frame. 1553 +-------+-------------------+ 1554 | Index | Description | 1555 +-------+-------------------+ 1556 | L(1) | filter_type | 1557 | | | 1558 | L(6) | loop_filter_level | 1559 | | | 1560 | L(3) | sharpness_level | 1561 +-------+-------------------+ 1563 The meaning of these numbers will be further explained in Chapter 15. 1565 VP8 has a feature in the bitstream that enables adjustment of the 1566 loop filter level based on a macroblock's prediction mode and 1567 reference frame. The per-macroblock adjustment is done through delta 1568 values against default loop filter level for the current frame. This 1569 subsection contains flag and value information for implementing per- 1570 macroblock loop filter level adjustment to default decoder behaviors. 1571 The data in this section is used in the decoding of the ensuing per- 1572 macroblock information and applies to the entire frame. 1574 L(1) is a one-bit flag indicating if macroblock loop filter 1575 adjustment is on for the current frame. 0 means such feature is not 1576 supported in the current frame and 1 means this feature is enabled 1577 for the current frame. 1579 Whether the adjustment is based on reference frame or encoding mode, 1580 the adjustment of loop filter level is done via a delta value against 1581 a baseline loop filter value. The delta values are updated for the 1582 current frame if an L(1) bit, mode_ref_lf_delta_update, takes the 1583 value 1. There are two groups of delta values, one group of delta 1584 values are for reference frame-based adjustments, the other group is 1585 for mode-based adjustments. The number of delta values in the two 1586 groups is MAX_REF_LF_DELTAS and MAX_MODE_LF_DELTAS, respectively. 1587 For every value within the two groups, there is one bit L(1) to 1588 indicate if the particular value is updated. When one is updated 1589 (1), it is transmitted as a six-bit magnitude L(6) followed by a one- 1590 bit sign flag (L(1), 0 for positive and 1 for negative). 1592 9.5. Token Partition and Partition Data Offsets 1594 VP8 allows DCT coefficients to be packed into multiple partitions 1595 besides the first partition with header and per-macroblock prediction 1596 information, so the decoder can perform parallel decoding in an 1597 efficient manner. There are two bits L(2) used to indicate the 1598 number of coefficient data partitions within a compressed frame. The 1599 two bits are defined in the following table: 1601 +-------+-------+----------------------+ 1602 | Bit 1 | Bit 0 | Number of Partitions | 1603 +-------+-------+----------------------+ 1604 | 0 | 0 | 1 | 1605 | | | | 1606 | 0 | 1 | 2 | 1607 | | | | 1608 | 1 | 0 | 4 | 1609 | | | | 1610 | 1 | 1 | 8 | 1611 +-------+-------+----------------------+ 1613 When the number of partitions is greater than one, offsets are 1614 embedded in the bitstream to provide the decoder direct access to 1615 token partitions. Each offset is written in 3 bytes (24 bits). 1617 Since the offset to the first partition is always 0, only the offsets 1618 for partitions other than the first partition are encoded in the 1619 bitstream. The partitioned data are consecutive in the bitstream, so 1620 offsets can also be used to calculate the data size of each 1621 partition. The following pseudo code illustrates how the size/offset 1622 is defined by the three bytes in the bitstream. 1624 ---- Begin code block -------------------------------------- 1626 Offset/size = (uint32)(byte0) + ((uint32)(byte1)<<8) 1627 + ((uint32)(byte2)<<16); 1629 ---- End code block ---------------------------------------- 1631 9.6. Dequantization Indices 1633 All residue signals are specified via a quantized 4x4 DCT applied to 1634 the Y, U, V, or Y2 subblocks of a macroblock. As detailed in Chapter 1635 14, before inverting the transform, each decoded coefficient is 1636 multiplied by one of six dequantization factors, the choice of which 1637 depends on the plane (Y, chroma = U or V, Y2) and coefficient 1638 position (DC = coefficient 0, AC = coefficients 1-15). The six 1639 values are specified using 7-bit indices into six corresponding fixed 1640 tables (the tables are given in Chapter 14). 1642 The first 7-bit index gives the dequantization table index for Y 1643 plane AC coefficients, called yac_qi. It is always coded and acts as 1644 a baseline for the other 5 quantization indices, each of which is 1645 represented by a delta from this baseline index. Following is pseudo 1646 code for reading the indices: 1648 ---- Begin code block -------------------------------------- 1650 yac_qi = L(7); /* Y ac index always specified */ 1651 ydc_delta = F? delta(): 0; /* Y dc delta specified if 1652 flag is true */ 1654 y2dc_delta = F? delta(): 0; /* Y2 dc delta specified if 1655 flag is true */ 1656 y2ac_delta = F? delta(): 0; /* Y2 ac delta specified if 1657 flag is true */ 1659 uvdc_delta = F? delta(): 0; /* chroma dc delta specified 1660 if flag is true */ 1661 uvac_delta = F? delta(): 0; /* chroma ac delta specified 1662 if flag is true */ 1664 ---- End code block ---------------------------------------- 1665 Where delta() is the process to read 5 bits from the bitstream to 1666 determine a signed delta value: 1668 +-------+--------------------------------------------------+ 1669 | Index | Description | 1670 +-------+--------------------------------------------------+ 1671 | L(4) | Magnitude of delta | 1672 | | | 1673 | L(1) | Sign of delta, 0 for positive and 1 for negative | 1674 +-------+--------------------------------------------------+ 1676 9.7. Refresh Golden Frame and AltRef Frame 1678 For key frames, both golden frame and altref frame are refreshed/ 1679 replaced by the current reconstructed frame, by default. For non-key 1680 frames, VP8 uses two bits to indicate whether the two frame buffers 1681 are refreshed, using the reconstructed current frame: 1683 +-------+----------------------------------------------------------+ 1684 | Index | Description | 1685 +-------+----------------------------------------------------------+ 1686 | L(1) | Whether golden frame is refreshed (0 for no, 1 for yes). | 1687 | | | 1688 | L(1) | Whether altref frame is refreshed (0 for no, 1 for yes). | 1689 +-------+----------------------------------------------------------+ 1691 When the flag for golden frame is 0, VP8 uses 2 more bits in the 1692 bitstream to indicate whether the buffer (and which buffer) is copied 1693 to the golden frame, or if no buffer is copied: 1695 +-------+------------------------------------------+ 1696 | Index | Description | 1697 +-------+------------------------------------------+ 1698 | L(2) | Buffer copy flag for golden frame buffer | 1699 +-------+------------------------------------------+ 1701 Where: 1703 o 0 means no buffer is copied to golden frame 1705 o 1 means last_frame is copied to golden frame 1707 o 2 means alt_ref_frame is copied to golden frame 1709 Similarly, when the flag for altref is 0, VP8 uses 2 bits in the 1710 bitstream to indicate which buffer is copied to alt_ref_frame. 1712 +-------+------------------------------------------+ 1713 | Index | Description | 1714 +-------+------------------------------------------+ 1715 | L(2) | Buffer copy flag for altref frame buffer | 1716 +-------+------------------------------------------+ 1718 Where: 1720 o 0 means no buffer is copied to altref frame 1722 o 1 means last_frame is copied to altref frame 1724 o 2 means golden_frame is copied to altref frame 1726 Two bits are transmitted for ref_frame_sign_bias for golden_frame and 1727 alt_ref_frame respectively. 1729 +-------+---------------------------------+ 1730 | Index | Description | 1731 +-------+---------------------------------+ 1732 | L(1) | Sign bias flag for golden frame | 1733 | | | 1734 | L(1) | Sign bias flag for altref frame | 1735 +-------+---------------------------------+ 1737 These values are used to control the sign of the motion vectors when 1738 a golden frame or an altref frame is used as the reference frame for 1739 a macroblock. 1741 9.8. Refresh Last Frame Buffer 1743 VP8 uses one bit, L(1), to indicate if the last frame reference 1744 buffer is refreshed using the constructed current frame. On key 1745 frame this bit is overridden, and the last frame buffer is always 1746 refreshed. 1748 9.9. DCT Coefficient Probability Update 1750 Contains a partial update of the probability tables used to decode 1751 DCT coefficients. These tables are maintained across interframes but 1752 are of course replaced with their defaults at the beginning of every 1753 key frame. 1755 The layout and semantics of this field will be taken up in Chapter 1756 13. 1758 9.10. Remaining Frame Header Data (non-Key Frame) 1760 +-------+-----------------------------------------------------------+ 1761 | Index | Description | 1762 +-------+-----------------------------------------------------------+ 1763 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1764 | | if skipping of macroblocks with no non-zero coefficients | 1765 | | is enabled. If it is set to 0 then prob_skip_false is not | 1766 | | read and mb_skip_coeff is forced to 0 for all macroblocks | 1767 | | (see Sections 11.1 and 12.1). | 1768 | | | 1769 | L(8) | prob_skip_false = probability used for decoding a | 1770 | | macroblock level flag, which indicates if a macroblock | 1771 | | has any non-zero coefficients. Only read if | 1772 | | mb_no_coeff_skip is 1. | 1773 | | | 1774 | L(8) | prob_intra = probability that a macroblock is "intra" | 1775 | | predicted, that is, predicted from the already-encoded | 1776 | | portions of the current frame as opposed to "inter" | 1777 | | predicted, that is, predicted from the contents of a | 1778 | | prior frame. | 1779 | | | 1780 | L(8) | prob_last = probability that an inter-predicted | 1781 | | macroblock is predicted from the immediately previous | 1782 | | frame, as opposed to the most recent golden frame or | 1783 | | altref frame.. | 1784 | | | 1785 | L(8) | prob_gf = probability that an inter-predicted macroblock | 1786 | | is predicted from the most recent golden frame, as | 1787 | | opposed to the altref frame | 1788 | | | 1789 | F | If true, followed by four L(8)s updating the | 1790 | | probabilities for the different types of intra-prediction | 1791 | | for the Y plane. These probabilities correspond to the | 1792 | | four interior nodes of the decoding tree for intra Y | 1793 | | modes in an interframe, that is, the even positions in | 1794 | | the ymode_tree array given above. | 1795 | | | 1796 | F | If true, followed by three L(8)s updating the | 1797 | | probabilities for the different types of intra-prediction | 1798 | | for the chroma planes. These probabilities correspond to | 1799 | | the even positions in the uv_mode_tree array given above. | 1800 | | | 1801 | X | Motion vector probability update. The details will be | 1802 | | given after the discussion of motion vector decoding. | 1803 +-------+-----------------------------------------------------------+ 1805 Decoding of this portion (only) of the frame header is handled in the 1806 reference decoder file decodemv.c. 1808 9.11. Remaining Frame Header Data (Key Frame) 1810 +-------+-----------------------------------------------------------+ 1811 | Index | Description | 1812 +-------+-----------------------------------------------------------+ 1813 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1814 | | if skipping of macroblocks with no non-zero coefficients | 1815 | | is enabled. If it is set to 0 then prob_skip_false is not | 1816 | | read and mb_skip_coeff is forced to 0 for all macroblocks | 1817 | | (see Sections 11.1 and 12.1). | 1818 | | | 1819 | L(8) | prob_skip_false = Probability used for decoding a | 1820 | | macroblock level flag, which indicates if a macroblock | 1821 | | has any non-zero coefficients. Only read if | 1822 | | mb_no_coeff_skip is 1. | 1823 +-------+-----------------------------------------------------------+ 1825 Decoding of this portion of the frame header is handled in the 1826 reference decoder file demode.c. 1828 This completes the layout of the frame header. The remainder of the 1829 first data partition consists of macroblock-level prediction data. 1831 After the frame header is processed, all probabilities needed to 1832 decode the prediction and residue data are known and will not change 1833 until the next frame. 1835 10. Segment-based Feature Adjustments 1837 Every macroblock may optionally override some of the default 1838 behaviors of the decoder. Specifically, VP8 uses segment based 1839 adjustments to support changing quantizer level and loop filter level 1840 for a macroblock. When the segment-based adjustment feature is 1841 enabled for a frame, each macroblock within the frame is coded with a 1842 segment_id. This effectively segments all the macroblocks in the 1843 current frame into a number of different segments. Macroblocks 1844 within the same segment behave exactly the same for quantizer and 1845 loop filter level adjustments. 1847 If both the segmentation_enabled and update_mb_segmentation_map flags 1848 in subsection B of the frame header take a value of 1, the prediction 1849 data for each (intra- or inter-coded) macroblock begins with a 1850 specification of segment_id for the current macroblock. It is 1851 decoded using this simple tree ... 1853 ---- Begin code block -------------------------------------- 1855 const tree_index mb_segment_tree [2 * (4-1)] = 1856 { 1857 2, 4, /* root: "0", "1" subtrees */ 1858 -0, -1, /* "00" = 0th value, "01" = 1st value */ 1859 -2, -3 /* "10" = 2nd value, "11" = 3rd value */ 1860 } 1862 ---- End code block ---------------------------------------- 1864 ... combined with a 3-entry probability table 1865 mb_segment_tree_probs[3]. The macroblock's segment_id is used later 1866 in the decoding process to look into the segment_feature_data table 1867 and determine how the quantizer and loop filter levels are adjusted. 1869 The decoding of segment_id, together with the parsing of intra- 1870 prediction modes (which is taken up next), is implemented in the 1871 reference decoder file demode.c. 1873 11. Key Frame Macroblock Prediction Records 1875 After the features described above, the macroblock prediction record 1876 next specifies the prediction mode used for the macroblock. 1878 11.1. mb_skip_coeff 1880 The single bool flag is decoded using prob_skip_false if and only if 1881 mb_no_coeff_skip is set to 1 (see sections 9.10 and 9.11). If 1882 mb_no_coeff_skip is set to 0 then this value defaults to 0. 1884 11.2. Luma Modes 1886 First comes the luma specification of type intra_mbmode, coded using 1887 the kf_ymode_tree, as described in Chapter 8 and repeated here for 1888 convenience: 1890 ---- Begin code block -------------------------------------- 1892 typedef enum 1893 { 1894 DC_PRED, /* predict DC using row above and column to the left */ 1895 V_PRED, /* predict rows using row above */ 1896 H_PRED, /* predict columns using column to the left */ 1897 TM_PRED, /* propagate second differences a la "true motion" */ 1899 B_PRED, /* each Y subblock is independently predicted */ 1901 num_uv_modes = B_PRED, /* first four modes apply to chroma */ 1902 num_ymodes /* all modes apply to luma */ 1903 } 1904 intra_mbmode; 1906 const tree_index kf_ymode_tree [2 * (num_ymodes - 1)] = 1907 { 1908 -B_PRED, 2, /* root: B_PRED = "0", "1" subtree */ 1909 4, 6, /* "1" subtree has 2 descendant subtrees */ 1910 -DC_PRED, -V_PRED, /* "10" subtree: DC_PRED = "100", 1911 V_PRED = "101" */ 1912 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1913 TM_PRED = "111" */ 1914 }; 1916 ---- End code block ---------------------------------------- 1918 For key frames, the Y mode is decoded using a fixed probability array 1919 as follows: 1921 ---- Begin code block -------------------------------------- 1923 const Prob kf_ymode_prob [num_ymodes - 1] = { 145, 156, 163, 128}; 1924 Ymode = (intra_mbmode) treed_read( d, kf_ymode_tree, kf_ymode_prob); 1926 ---- End code block ---------------------------------------- 1928 d is of course the bool_decoder being used to read the first data 1929 partition. 1931 If the Ymode is B_PRED, it is followed by a (tree-coded) mode for 1932 each of the 16 Y subblocks. The 10 subblock modes and their coding 1933 tree as follows: 1935 ---- Begin code block -------------------------------------- 1937 typedef enum 1938 { 1939 B_DC_PRED, /* predict DC using row above and column 1940 to the left */ 1941 B_TM_PRED, /* propagate second differences a la 1942 "true motion" */ 1944 B_VE_PRED, /* predict rows using row above */ 1945 B_HE_PRED, /* predict columns using column to the left */ 1947 B_LD_PRED, /* southwest (left and down) 45 degree diagonal 1948 prediction */ 1949 B_RD_PRED, /* southeast (right and down) "" */ 1951 B_VR_PRED, /* SSE (vertical right) diagonal prediction */ 1952 B_VL_PRED, /* SSW (vertical left) "" */ 1953 B_HD_PRED, /* ESE (horizontal down) "" */ 1954 B_HU_PRED, /* ENE (horizontal up) "" */ 1956 num_intra_bmodes 1957 } 1958 intra_bmode; 1960 /* Coding tree for the above, with implied codings as comments */ 1962 const tree_index bmode_tree [2 * (num_intra_bmodes - 1)] = 1963 { 1964 -B_DC_PRED, 2, /* B_DC_PRED = "0" */ 1965 -B_TM_PRED, 4, /* B_TM_PRED = "10" */ 1966 -B_VE_PRED, 6, /* B_VE_PRED = "110" */ 1967 8, 12, 1968 -B_HE_PRED, 10, /* B_HE_PRED = "11100" */ 1969 -B_RD_PRED, -B_VR_PRED, /* B_RD_PRED = "111010", 1970 B_VR_PRED = "111011" */ 1971 -B_LD_PRED, 14, /* B_LD_PRED = "111110" */ 1972 -B_VL_PRED, 16 /* B_VL_PRED = "1111110" */ 1973 -B_HD_PRED, -B_HU_PRED /* HD = "11111110", 1974 HU = "11111111" */ 1975 }; 1977 ---- End code block ---------------------------------------- 1979 The first four modes are smaller versions of the similarly-named 1980 16x16 modes above, albeit with slightly different numbering. The 1981 last six "diagonal" modes are unique to luma subblocks. 1983 11.3. Subblock Mode Contexts 1985 The coding of subblock modes in key frames uses the modes already 1986 coded for the subblocks to the left of and above the subblock to 1987 select a probability array for decoding the current subblock mode. 1988 This is our first instance of contextual prediction, and there are 1989 several caveats associated with it: 1991 1. The adjacency relationships between subblocks are based on the 1992 normal default raster placement of the subblocks. 1994 2. The adjacent subblocks need not lie in the current macroblock. 1995 The subblocks to the left of the left-edge subblocks 0, 4, 8, and 1996 12 are the right-edge subblocks 3, 7, 11, and 15, respectively, 1997 of the (already coded) macroblock immediately to the left. 1998 Similarly, the subblocks above the top-edge subblocks 0, 1, 2, 1999 and 3 are the bottom-edge subblocks 12, 13, 14, and 15 of the 2000 already-coded macroblock immediately above us. 2002 3. For macroblocks on the top row or left edge of the image, some of 2003 the predictors will be non-existent. Such predictors are taken 2004 to have had the value B_DC_PRED which, perhaps conveniently, 2005 takes the value 0 in the enumeration above. A simple management 2006 scheme for these contexts might maintain a row of above 2007 predictors and four left predictors. Before decoding the frame, 2008 the entire row is initialized to B_DC_PRED; before decoding each 2009 row of macroblocks, the four left predictors are also set to 2010 B_DC_PRED. After decoding a macroblock, the bottom four subblock 2011 modes are copied into the row predictor (at the current position, 2012 which then advances to be above the next macroblock) and the 2013 right four subblock modes are copied into the left predictor. 2015 4. Many macroblocks will of course be coded using a 16x16 luma 2016 prediction mode. For the purpose of predicting ensuing subblock 2017 modes (only), such macroblocks derive a subblock mode, constant 2018 throughout the macroblock, from the 16x16 luma mode as follows: 2019 DC_PRED uses B_DC_PRED, V_PRED uses B_VE_PRED, H_PRED uses 2020 B_HE_PRED, and TM_PRED uses B_TM_PRED. 2022 5. Although we discuss interframe modes later, we remark here that, 2023 while interframes do use all the intra-coding modes described 2024 here and below, the subblock modes in an interframe are coded 2025 using a single constant probability array that does not depend on 2026 any context. 2028 The dependence of subblock mode probability on the nearby subblock 2029 mode context is most easily handled using a three-dimensional 2030 constant array: 2032 ---- Begin code block -------------------------------------- 2034 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2035 [num_intra_bmodes-1]; 2037 ---- End code block ---------------------------------------- 2039 The outer two dimensions of this array are indexed by the already- 2040 coded subblock modes above and to the left of the current block, 2041 respectively. The inner dimension is a typical tree probability list 2042 whose indices correspond to the even indices of the bmode_tree above. 2043 The mode for the j^(th) luma subblock is then 2045 ---- Begin code block -------------------------------------- 2047 Bmode = (intra_bmode) treed_read( d, bmode_tree, kf_bmode_prob 2048 [A] [L]); 2050 ---- End code block ---------------------------------------- 2052 where the 4x4 Y subblock index j varies from 0 to 15 in raster order 2053 and A and L are the modes used above and to-the-left of the j^(th) 2054 subblock. 2056 The contents of the kf_bmode_prob array are given at the end of this 2057 chapter. 2059 11.4. Chroma Modes 2061 After the Y mode (and optional subblock mode) specification comes the 2062 chroma mode. The chroma modes are a subset of the Y modes and are 2063 coded using the uv_mode_tree described in Chapter 8, again repeated 2064 here for convenience: 2066 ---- Begin code block -------------------------------------- 2068 const tree_index uv_mode_tree [2 * (num_uv_modes - 1)] = 2069 { 2070 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 2071 -V_PRED, 4, /* "1" subtree: V_PRED = "10", 2072 "11" subtree */ 2073 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 2074 TM_PRED = "111" */ 2075 }; 2077 ---- End code block ---------------------------------------- 2079 As for the Y modes (in a key frame), the chroma modes are coded using 2080 a fixed, contextless probability table: 2082 ---- Begin code block -------------------------------------- 2084 const Prob kf_uv_mode_prob [num_uv_modes - 1] = { 142, 114, 183}; 2085 uv_mode = (intra_mbmode) treed_read( d, uv_mode_tree, 2086 kf_uv_mode_prob); 2088 ---- End code block ---------------------------------------- 2090 This completes the description of macroblock prediction coding for 2091 key frames. As will be discussed in Chapter 16, the coding of intra 2092 modes within interframes is similar, but not identical, to that 2093 described here (and in the reference code) for prediction modes and, 2094 indeed, for all tree-coded data in VP8. 2096 11.5. Subblock Mode Probability Table 2098 Finally, here is the fixed probability table used to decode subblock 2099 modes in key frames. 2101 ---- Begin code block -------------------------------------- 2103 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2104 [num_intra_bmodes-1] = 2105 { 2106 { 2107 { 231, 120, 48, 89, 115, 113, 120, 152, 112}, 2108 { 152, 179, 64, 126, 170, 118, 46, 70, 95}, 2109 { 175, 69, 143, 80, 85, 82, 72, 155, 103}, 2110 { 56, 58, 10, 171, 218, 189, 17, 13, 152}, 2111 { 144, 71, 10, 38, 171, 213, 144, 34, 26}, 2112 { 114, 26, 17, 163, 44, 195, 21, 10, 173}, 2113 { 121, 24, 80, 195, 26, 62, 44, 64, 85}, 2114 { 170, 46, 55, 19, 136, 160, 33, 206, 71}, 2115 { 63, 20, 8, 114, 114, 208, 12, 9, 226}, 2116 { 81, 40, 11, 96, 182, 84, 29, 16, 36} 2117 }, 2118 { 2119 { 134, 183, 89, 137, 98, 101, 106, 165, 148}, 2120 { 72, 187, 100, 130, 157, 111, 32, 75, 80}, 2121 { 66, 102, 167, 99, 74, 62, 40, 234, 128}, 2122 { 41, 53, 9, 178, 241, 141, 26, 8, 107}, 2123 { 104, 79, 12, 27, 217, 255, 87, 17, 7}, 2124 { 74, 43, 26, 146, 73, 166, 49, 23, 157}, 2125 { 65, 38, 105, 160, 51, 52, 31, 115, 128}, 2126 { 87, 68, 71, 44, 114, 51, 15, 186, 23}, 2127 { 47, 41, 14, 110, 182, 183, 21, 17, 194}, 2128 { 66, 45, 25, 102, 197, 189, 23, 18, 22} 2129 }, 2130 { 2131 { 88, 88, 147, 150, 42, 46, 45, 196, 205}, 2132 { 43, 97, 183, 117, 85, 38, 35, 179, 61}, 2133 { 39, 53, 200, 87, 26, 21, 43, 232, 171}, 2134 { 56, 34, 51, 104, 114, 102, 29, 93, 77}, 2135 { 107, 54, 32, 26, 51, 1, 81, 43, 31}, 2136 { 39, 28, 85, 171, 58, 165, 90, 98, 64}, 2137 { 34, 22, 116, 206, 23, 34, 43, 166, 73}, 2138 { 68, 25, 106, 22, 64, 171, 36, 225, 114}, 2139 { 34, 19, 21, 102, 132, 188, 16, 76, 124}, 2140 { 62, 18, 78, 95, 85, 57, 50, 48, 51} 2141 }, 2142 { 2143 { 193, 101, 35, 159, 215, 111, 89, 46, 111}, 2144 { 60, 148, 31, 172, 219, 228, 21, 18, 111}, 2145 { 112, 113, 77, 85, 179, 255, 38, 120, 114}, 2146 { 40, 42, 1, 196, 245, 209, 10, 25, 109}, 2147 { 100, 80, 8, 43, 154, 1, 51, 26, 71}, 2148 { 88, 43, 29, 140, 166, 213, 37, 43, 154}, 2149 { 61, 63, 30, 155, 67, 45, 68, 1, 209}, 2150 { 142, 78, 78, 16, 255, 128, 34, 197, 171}, 2151 { 41, 40, 5, 102, 211, 183, 4, 1, 221}, 2152 { 51, 50, 17, 168, 209, 192, 23, 25, 82} 2153 }, 2154 { 2155 { 125, 98, 42, 88, 104, 85, 117, 175, 82}, 2156 { 95, 84, 53, 89, 128, 100, 113, 101, 45}, 2157 { 75, 79, 123, 47, 51, 128, 81, 171, 1}, 2158 { 57, 17, 5, 71, 102, 57, 53, 41, 49}, 2159 { 115, 21, 2, 10, 102, 255, 166, 23, 6}, 2160 { 38, 33, 13, 121, 57, 73, 26, 1, 85}, 2161 { 41, 10, 67, 138, 77, 110, 90, 47, 114}, 2162 { 101, 29, 16, 10, 85, 128, 101, 196, 26}, 2163 { 57, 18, 10, 102, 102, 213, 34, 20, 43}, 2164 { 117, 20, 15, 36, 163, 128, 68, 1, 26} 2165 }, 2166 { 2167 { 138, 31, 36, 171, 27, 166, 38, 44, 229}, 2168 { 67, 87, 58, 169, 82, 115, 26, 59, 179}, 2169 { 63, 59, 90, 180, 59, 166, 93, 73, 154}, 2170 { 40, 40, 21, 116, 143, 209, 34, 39, 175}, 2171 { 57, 46, 22, 24, 128, 1, 54, 17, 37}, 2172 { 47, 15, 16, 183, 34, 223, 49, 45, 183}, 2173 { 46, 17, 33, 183, 6, 98, 15, 32, 183}, 2174 { 65, 32, 73, 115, 28, 128, 23, 128, 205}, 2175 { 40, 3, 9, 115, 51, 192, 18, 6, 223}, 2176 { 87, 37, 9, 115, 59, 77, 64, 21, 47} 2177 }, 2178 { 2179 { 104, 55, 44, 218, 9, 54, 53, 130, 226}, 2180 { 64, 90, 70, 205, 40, 41, 23, 26, 57}, 2181 { 54, 57, 112, 184, 5, 41, 38, 166, 213}, 2182 { 30, 34, 26, 133, 152, 116, 10, 32, 134}, 2183 { 75, 32, 12, 51, 192, 255, 160, 43, 51}, 2184 { 39, 19, 53, 221, 26, 114, 32, 73, 255}, 2185 { 31, 9, 65, 234, 2, 15, 1, 118, 73}, 2186 { 88, 31, 35, 67, 102, 85, 55, 186, 85}, 2187 { 56, 21, 23, 111, 59, 205, 45, 37, 192}, 2188 { 55, 38, 70, 124, 73, 102, 1, 34, 98} 2189 }, 2190 { 2191 { 102, 61, 71, 37, 34, 53, 31, 243, 192}, 2192 { 69, 60, 71, 38, 73, 119, 28, 222, 37}, 2193 { 68, 45, 128, 34, 1, 47, 11, 245, 171}, 2194 { 62, 17, 19, 70, 146, 85, 55, 62, 70}, 2195 { 75, 15, 9, 9, 64, 255, 184, 119, 16}, 2196 { 37, 43, 37, 154, 100, 163, 85, 160, 1}, 2197 { 63, 9, 92, 136, 28, 64, 32, 201, 85}, 2198 { 86, 6, 28, 5, 64, 255, 25, 248, 1}, 2199 { 56, 8, 17, 132, 137, 255, 55, 116, 128}, 2200 { 58, 15, 20, 82, 135, 57, 26, 121, 40} 2201 }, 2202 { 2203 { 164, 50, 31, 137, 154, 133, 25, 35, 218}, 2204 { 51, 103, 44, 131, 131, 123, 31, 6, 158}, 2205 { 86, 40, 64, 135, 148, 224, 45, 183, 128}, 2206 { 22, 26, 17, 131, 240, 154, 14, 1, 209}, 2207 { 83, 12, 13, 54, 192, 255, 68, 47, 28}, 2208 { 45, 16, 21, 91, 64, 222, 7, 1, 197}, 2209 { 56, 21, 39, 155, 60, 138, 23, 102, 213}, 2210 { 85, 26, 85, 85, 128, 128, 32, 146, 171}, 2211 { 18, 11, 7, 63, 144, 171, 4, 4, 246}, 2212 { 35, 27, 10, 146, 174, 171, 12, 26, 128} 2213 }, 2214 { 2215 { 190, 80, 35, 99, 180, 80, 126, 54, 45}, 2216 { 85, 126, 47, 87, 176, 51, 41, 20, 32}, 2217 { 101, 75, 128, 139, 118, 146, 116, 128, 85}, 2218 { 56, 41, 15, 176, 236, 85, 37, 9, 62}, 2219 { 146, 36, 19, 30, 171, 255, 97, 27, 20}, 2220 { 71, 30, 17, 119, 118, 255, 17, 18, 138}, 2221 { 101, 38, 60, 138, 55, 70, 43, 26, 142}, 2222 { 138, 45, 61, 62, 219, 1, 81, 188, 64}, 2223 { 32, 41, 20, 117, 151, 142, 20, 21, 163}, 2224 { 112, 19, 12, 61, 195, 128, 48, 4, 24} 2225 } 2226 }; 2228 ---- End code block ---------------------------------------- 2230 12. Intraframe Prediction 2232 Intraframe prediction uses already-coded macroblocks within the 2233 current frame to approximate the contents of the current macroblock. 2234 It applies to intra-coded macroblocks in an interframe and to all 2235 macroblocks in a key frame. 2237 Relative to the current macroblock "M", the already-coded macroblocks 2238 include all macroblocks above M together with the macroblocks on the 2239 same row as, and to the left of, M, though at most four of these 2240 macroblocks are actually used: the block "A" directly above M, the 2241 blocks immediately to the left and right of A, and the block 2242 immediately to the left of M. 2244 Each of the prediction modes (i.e., means of extrapolation from 2245 already-calculated values) uses fairly simple arithmetic on pixel 2246 values whose positions, relative to the current position, are defined 2247 by the mode. 2249 The chroma (U and V) and luma (Y) predictions are independent of each 2250 other. 2252 The relative addressing of pixels applied to macroblocks on the upper 2253 row or left column of the frame will sometimes cause pixels outside 2254 the visible frame to be referenced. Usually such out-of-bounds 2255 pixels have an assumed value of 129 for pixels to the left of the 2256 leftmost column of the visible frame and 127 for pixels above the top 2257 row of the visible frame (including the special case of the pixel 2258 above and to the left of the top-left pixel in the visible frame). 2259 Exceptions to this (associated to certain modes) will be noted below. 2261 The already-coded macroblocks referenced by intra-prediction have 2262 been "reconstructed", that is, have been predicted and residue- 2263 adjusted (as described in Chapter 14), but have not been loop- 2264 filtered. While it does process the edges between individual 2265 macroblocks and individual subblocks, loop filtering (described in 2266 Chapter 15) is applied to the frame as a whole, after all of the 2267 macroblocks have been reconstructed. 2269 12.1. mb_skip_coeff 2271 The single bool flag is decoded using prob_skip_false if and only if 2272 mb_no_coeff_skip is set to 1 (see Sections 9.10 and 9.11). If 2273 mb_no_coeff_skip is set to 0 then this value defaults to 0. 2275 12.2. Chroma Prediction 2277 The chroma prediction is a little simpler than the luma prediction, 2278 so we treat it first. Each of the chroma modes treats U and V 2279 identically, that is, the U and V prediction values are calculated in 2280 parallel, using the same relative addressing and arithmetic in each 2281 of the two planes. 2283 The modes extrapolate prediction values using the 8-pixel row "A" 2284 lying immediately above the block (that is, the bottom chroma row of 2285 the macroblock immediately above the current macroblock) and the 2286 8-pixel column "L" immediately to the left of the block (that is, the 2287 rightmost chroma column of the macroblock immediately to the left of 2288 the current macroblock). 2290 Vertical prediction (chroma mode V_PRED) simply fills each 8-pixel 2291 row of the 8x8 chroma block with a copy of the "above" row (A). If 2292 the current macroblock lies on the top row of the frame, all 8 of the 2293 pixel values in A are assigned the value 127. 2295 Similarly, horizontal prediction (H_PRED) fills each 8-pixel column 2296 of the 8x8 chroma block with a copy of the "left" column (L). If the 2297 current macroblock is in the left column of the frame, all 8 pixel 2298 values in L are assigned the value 129. 2300 DC prediction (DC_PRED) fills the 8x8 chroma block with a single 2301 value. In the generic case of a macroblock lying below the top row 2302 and right of the leftmost column of the frame, this value is the 2303 average of the 16 (genuinely visible) pixels in the (union of the) 2304 above row A and left column L. 2306 Otherwise, if the current macroblock lies on the top row of the 2307 frame, the average of the 8 pixels in L is used; if it lies in the 2308 left column of the frame, the average of the 8 pixels in A is used. 2309 Note that the averages used in these exceptional cases are not the 2310 same as those that would be arrived at by using the out-of-bounds A 2311 and L values defined for V_PRED and H_PRED. In the case of the 2312 leftmost macroblock on the top row of the frame the 8x8 block is 2313 simply filled with the constant value 128. 2315 For DC_PRED, apart from the exceptional case of the top left 2316 macroblock, we are averaging either 16 or 8 pixel values to get a 2317 single prediction value that fills the 8x8 block. The rounding is 2318 done as follows: 2320 ---- Begin code block -------------------------------------- 2322 int sum; /* sum of 8 or 16 pixels at (at least) 16-bit precision */ 2323 int shf; /* base 2 logarithm of the number of pixels (3 or 4) */ 2325 Pixel DCvalue = (sum + (1 << (shf-1))) >> shf; 2327 ---- End code block ---------------------------------------- 2329 Because the summands are all valid pixels, no "clamp" is necessary in 2330 the calculation of DCvalue. 2332 The remaining "True Motion" (TM_PRED) chroma mode gets its name from 2333 an older technique of video compression used by On2 Technologies, to 2334 which it bears some relation. In addition to the row "A" and column 2335 "L", TM_PRED uses the pixel "P" above and to the left of the chroma 2336 block. 2338 The following figure gives an example of how TM_PRED works: 2340 ---- Begin code block -------------------------------------- 2342 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2343 | P | A0 | A1 | A2 | A3 | A4 | A5 | A6 | A7 | 2344 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2345 | L0 | X00 | X01 | X02 | X03 | X04 | X05 | X06 | X07 | 2346 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2347 | L1 | X10 | X11 | X12 | X13 | X14 | X15 | X16 | X17 | 2348 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2349 | L2 | X20 | X21 | X22 | X23 | X24 | X25 | X26 | X27 | 2350 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2351 | L3 | X30 | X31 | X32 | X33 | X34 | X35 | X36 | X37 | 2352 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2353 | L4 | X40 | X41 | X42 | X43 | X44 | X45 | X46 | X47 | 2354 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2355 | L5 | X50 | X51 | X52 | X53 | X54 | X55 | X56 | X57 | 2356 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2357 | L6 | X60 | X61 | X62 | X63 | X64 | X65 | X66 | X67 | 2358 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2359 | L7 | X70 | X71 | X72 | X73 | X74 | X75 | X76 | X77 | 2360 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2362 ---- End code block ---------------------------------------- 2364 Where P, As and Ls represent reconstructed pixel values from 2365 previously coded blocks, and X00 through X77 represent predicted 2366 values for the current block. TM_PRED uses the following equation to 2367 calculate X_ij: 2369 X_ij = L_i + A_j - P (i, j=0, 1, 2, 3) 2371 The exact algorithm is as follows: 2373 ---- Begin code block -------------------------------------- 2375 void TMpred( 2376 Pixel b[8][8], /* chroma (U or V) prediction block */ 2377 const Pixel A[8], /* row of already-constructed pixels 2378 above block */ 2379 const Pixel L[8], /* column of "" just to the left of 2380 block */ 2381 const Pixel P /* pixel just to the left of A and 2382 above L*/ 2383 ) { 2384 int r = 0; /* row */ 2385 do { 2386 int c = 0; /* column */ 2387 do { 2388 b[r][c] = clamp255( L[r]+ A[c] - P); 2389 } while( ++c < 8); 2390 } while( ++r < 8); 2391 } 2393 ---- End code block ---------------------------------------- 2395 Note that the process could equivalently be described as propagating 2396 the vertical differences between pixels in L (starting from P), using 2397 the pixels from A to start each column. 2399 An implementation of chroma intra-prediction may be found in the 2400 reference decoder file reconintra.c. 2402 Unlike DC_PRED, for macroblocks on the top row or left edge TM_PRED 2403 does use the out-of-bounds values of 127 and 129 (respectively) 2404 defined for V_PRED and H_PRED. 2406 12.3. Luma Prediction 2408 The prediction processes for the first four 16x16 luma modes 2409 (DC_PRED, V_PRED, H_PRED, and TM_PRED) are essentially identical to 2410 the corresponding chroma prediction processes described above, the 2411 only difference being that we are predicting a single 16x16 luma 2412 block instead of two 8x8 chroma blocks. 2414 Thus, the row "A" and column "L" here contain 16 pixels, the DC 2415 prediction is calculated using 16 or 32 pixels (and shf is 4 or 5), 2416 and we of course fill the entire prediction buffer, that is, 16 rows 2417 (or columns) containing 16 pixels each. The reference implementation 2418 of 16x16 luma prediction is also in reconintra.c. 2420 In the remaining luma mode (B_PRED), each 4x4 Y subblock is 2421 independently predicted using one of ten modes (listed, along with 2422 their encodings, in Chapter 11). 2424 Also, unlike the full-macroblock modes already described, some of the 2425 subblock modes use prediction pixels above and to the right of the 2426 current subblock. In detail, each 4x4 subblock "B" is predicted 2427 using (at most) the 4-pixel column "L" immediately to the left of B 2428 and the 8-pixel row "A" immediately above B, consisting of the 4 2429 pixels above B followed by the 4 adjacent pixels above and to the 2430 right of B, together with the single pixel "P" immediately to the 2431 left of A (and immediately above L). 2433 For the purpose of subblock intra-prediction, the pixels immediately 2434 to the left and right of a pixel in a subblock are the same as the 2435 pixels immediately to the left and right of the corresponding pixel 2436 in the frame buffer "F". Vertical offsets behave similarly: The 2437 above row A lies immediately above B in F, and the adjacent pixels in 2438 the left column L are separated by a single row in F. 2440 Because entire macroblocks (as opposed to their constituent 2441 subblocks) are reconstructed in raster-scan order, for subblocks 2442 lying along the right edge (and not along the top row) of the current 2443 macroblock, the four "extra" prediction pixels in A above and to the 2444 right of B have not yet actually been constructed. 2446 Subblocks 7, 11, and 15 are affected. All three of these subblocks 2447 use the same extra pixels as does subblock 3 (at the upper right 2448 corner of the macroblock), namely the 4 pixels immediately above and 2449 to the right of subblock 3. Writing (R,C) for a frame buffer 2450 position offset from the upper left corner of the current macroblock 2451 by R rows and C columns, the extra pixels for all the right-edge 2452 subblocks (3, 7, 11, and 15) are at positions (-1,16), (-1,17), 2453 (-1,18), and (-1,19). For the right-most macroblock in each 2454 macroblock row except the top row, the extra pixels shall use the 2455 same value as the pixel at position (-1, 15), which is the right-most 2456 visible pixel on the line immediately above the macroblock row. For 2457 the top macroblock row, all the extra pixels assume a value of 127. 2459 The details of the prediction modes are most easily described in 2460 code. 2462 ---- Begin code block -------------------------------------- 2464 /* Result pixels are often averages of two or three predictor 2465 pixels. The following subroutines are used to calculate 2466 these averages. Because the arguments are valid pixels, no 2467 clamping is necessary. An actual implementation would 2468 probably use inline functions or macros. */ 2470 /* Compute weighted average centered at y w/adjacent x, z */ 2472 Pixel avg3( Pixel x, Pixel y, Pixel z) { 2473 return (x + y + y + z + 2) >> 2;} 2475 /* Weighted average of 3 adjacent pixels centered at p */ 2477 Pixel avg3p( const Pixel *p) { return avg3( p[-1], p[0], p[1]);} 2479 /* Simple average of x and y */ 2481 Pixel avg2( Pixel x, Pixel y) { return (x + y + 1) >> 1;} 2483 /* Average of p[0] and p[1] may be considered to be a synthetic 2484 pixel lying between the two, that is, one half-step past p. */ 2486 Pixel avg2p( const Pixel *p) { return avg2( p[0], p[1]);} 2488 /* Main function. Out-of-frame pixels in A or L should be set 2489 to 128. */ 2491 void subblock_intra_predict( 2492 Pixel B[4][4], /* Y subblock prediction buffer */ 2493 const Pixel *A, /* A[0]...A[7] = above row, A[-1] = P */ 2494 const Pixel *L, /* L[0]...L[3] = left column, L[-1] = P */ 2495 intra_bmode mode /* enum is in section 11.1 above */ 2496 ) { 2497 Pixel E[9]; /* 9 already-constructed edge pixels */ 2498 E[0] = L[3]; E[1] = L[2]; E[2] = L[1]; E[3] = L[0]; 2499 E[4] = A[-1]; /* == L[-1] == P */ 2500 E[5] = A[0]; E[6] = A[1]; E[7] = A[2]; E[8] = A[3]; 2502 switch( mode) { 2503 /* First four modes are similar to corresponding 2504 full-block modes. */ 2506 case B_DC_PRED: 2507 { 2508 int v = 4; /* DC sum/avg, 4 is rounding adjustment */ 2509 int i = 0; do { v += A[i] + L[i];} while( ++i < 4); 2510 v >>= 3; /* averaging 8 pixels */ 2511 i = 0; do { /* fill prediction buffer with constant DC 2512 value */ 2514 int j = 0; do { B[i][j] = v;} while( ++j < 4); 2515 } while( ++i < 4); 2516 break; 2517 } 2519 case B_TM_PRED: /* just like 16x16 TM_PRED */ 2520 { 2521 int r = 0; do { 2522 int c = 0; do { 2523 B[r][c] = clamp255( L[r] + A[c] - A[-1]); 2524 } while( ++c < 4); 2525 } while( ++r < 4); 2526 break; 2527 } 2529 case B_VE_PRED: /* like 16x16 V_PRED except using averages */ 2530 { 2531 int c = 0; do { /* all 4 rows = smoothed top row */ 2532 B[0][c] = B[1][c] = B[2][c] = B[3][c] = avg3p( A + c); 2533 } while( ++c < 4); 2534 break; 2535 } 2537 case B_HE_PRED: /* like 16x16 H_PRED except using averages */ 2538 { 2539 /* Bottom row is exceptional because L[4] does not exist */ 2540 int v = avg3( L[2], L[3], L[3]); 2541 int r = 3; while( 1) { /* all 4 columns = smoothed left 2542 column */ 2543 B[r][0] = B[r][1] = B[r][2] = B[r][3] = v; 2544 if( --r < 0) 2545 break; 2546 v = avg3p( L + r); /* upper 3 rows use average of 2547 3 pixels */ 2548 } 2549 break; 2550 } 2552 /* The remaining six "diagonal" modes subdivide the 2553 prediction buffer into diagonal lines. All the pixels 2554 on each line are assigned the same value; this value is 2555 (a smoothed or synthetic version of) an 2556 already-constructed predictor value lying on the same 2557 line. For clarity, in the comments, we express the 2558 positions of these predictor pixels relative to the 2559 upper left corner of the destination array B. 2561 These modes are unique to subblock prediction and have 2562 no full-block analogues. The first two use lines at 2563 +|- 45 degrees from horizontal (or, equivalently, 2564 vertical), that is, lines whose slopes are +|- 1. */ 2566 case B_LD_PRED: /* southwest (left and down) step = 2567 (-1, 1) or (1,-1) */ 2568 /* avg3p( A + j) is the "smoothed" pixel at (-1,j) */ 2569 B[0][0] = avg3p( A + 1); 2570 B[0][1] = B[1][0] = avg3p( A + 2); 2571 B[0][2] = B[1][1] = B[2][0] = avg3p( A + 3); 2572 B[0][3] = B[1][2] = B[2][1] = B[3][0] = avg3p( A + 4); 2573 B[1][3] = B[2][2] = B[3][1] = avg3p( A + 5); 2574 B[2][3] = B[3][2] = avg3p( A + 6); 2575 B[3][3] = avg3( A[6], A[7], A[7]); /* A[8] does not exist */ 2576 break; 2578 case B_RD_PRED: /* southeast (right and down) step = 2579 (1,1) or (-1,-1) */ 2580 B[3][0] = avg3p( E + 1); /* predictor is from (2, -1) */ 2581 B[3][1] = B[2][0] = avg3p( E + 2); /* (1, -1) */ 2582 B[3][2] = B[2][1] = B[1][0] = avg3p( E + 3); /* (0, -1) */ 2583 B[3][3] = B[2][2] = B[1][1] = B[0][0] = 2584 avg3p( E + 4); /* (-1, -1) */ 2585 B[2][3] = B[1][2] = B[0][1] = avg3p( E + 5); /* (-1, 0) */ 2586 B[1][3] = B[0][2] = avg3p( E + 6); /* (-1, 1) */ 2587 B[0][3] = avg3p( E + 7); /* (-1, 2) */ 2588 break; 2590 /* The remaining 4 diagonal modes use lines whose slopes are 2591 +|- 2 and +|- 1/2. The angles of these lines are roughly 2592 +|- 27 degrees from horizontal or vertical. 2594 Unlike the 45 degree diagonals, here we often need to 2595 "synthesize" predictor pixels midway between two actual 2596 predictors using avg2p(p), which we think of as returning 2597 the pixel "at" p[1/2]. */ 2599 case B_VR_PRED: /* SSE (vertical right) step = 2600 (2,1) or (-2,-1) */ 2601 B[3][0] = avg3p( E + 2); /* predictor is from (1, -1) */ 2602 B[2][0] = avg3p( E + 3); /* (0, -1) */ 2603 B[3][1] = B[1][0] = avg3p( E + 4); /* (-1, -1) */ 2604 B[2][1] = B[0][0] = avg2p( E + 4); /* (-1, -1/2) */ 2605 B[3][2] = B[1][1] = avg3p( E + 5); /* (-1, 0) */ 2606 B[2][2] = B[0][1] = avg2p( E + 5); /* (-1, 1/2) */ 2607 B[3][3] = B[1][2] = avg3p( E + 6); /* (-1, 1) */ 2608 B[2][3] = B[0][2] = avg2p( E + 6); /* (-1, 3/2) */ 2609 B[1][3] = avg3p( E + 7); /* (-1, 2) */ 2610 B[0][3] = avg2p( E + 7); /* (-1, 5/2) */ 2611 break; 2613 case B_VL_PRED: /* SSW (vertical left) step = 2614 (2,-1) or (-2,1) */ 2615 B[0][0] = avg2p( A); /* predictor is from (-1, 1/2) */ 2616 B[1][0] = avg3p( A + 1); /* (-1, 1) */ 2617 B[2][0] = B[0][1] = avg2p( A + 1); /* (-1, 3/2) */ 2618 B[1][1] = B[3][0] = avg3p( A + 2); /* (-1, 2) */ 2619 B[2][1] = B[0][2] = avg2p( A + 2); /* (-1, 5/2) */ 2620 B[3][1] = B[1][2] = avg3p( A + 3); /* (-1, 3) */ 2621 B[2][2] = B[0][3] = avg2p( A + 3); /* (-1, 7/2) */ 2622 B[3][2] = B[1][3] = avg3p( A + 4); /* (-1, 4) */ 2623 /* Last two values do not strictly follow the pattern. */ 2624 B[2][3] = avg3p( A + 5); /* (-1, 5) [avg2p( A + 4) = 2625 (-1,9/2)] */ 2626 B[3][3] = avg3p( A + 6); /* (-1, 6) [avg3p( A + 5) = 2627 (-1,5)] */ 2628 break; 2630 case B_HD_PRED: /* ESE (horizontal down) step = 2631 (1,2) or (-1,-2) */ 2632 B[3][0] = avg2p( E); /* predictor is from (5/2, -1) */ 2633 B[3][1] = avg3p( E + 1); /* (2, -1) */ 2634 B[2][0] = B[3][2] = svg2p( E + 1); /* ( 3/2, -1) */ 2635 B[2][1] = B[3][3] = avg3p( E + 2); /* ( 1, -1) */ 2636 B[2][2] = B[1][0] = avg2p( E + 2); /* ( 1/2, -1) */ 2637 B[2][3] = B[1][1] = avg3p( E + 3); /* ( 0, -1) */ 2638 B[1][2] = B[0][0] = avg2p( E + 3); /* (-1/2, -1) */ 2639 B[1][3] = B[0][1] = avg3p( E + 4); /* ( -1, -1) */ 2640 B[0][2] = avg3p( E + 5); /* (-1, 0) */ 2641 B[0][3] = avg3p( E + 6); /* (-1, 1) */ 2642 break; 2644 case B_HU_PRED: /* ENE (horizontal up) step = (1,-2) 2645 or (-1,2) */ 2646 B[0][0] = avg2p( L); /* predictor is from ( 1/2, -1) */ 2647 B[0][1] = avg3p( L + 1); /* ( 1, -1) */ 2648 B[0][2] = B[1][0] = avg2p( L + 1); /* (3/2, -1) */ 2649 B[0][3] = B[1][1] = avg3p( L + 2); /* ( 2, -1) */ 2650 B[1][2] = B[2][0] = avg2p( L + 2); /* (5/2, -1) */ 2651 B[1][3] = B[2][1] = avg3( L[2], L[3], L[3]); /* ( 3, -1) */ 2652 /* Not possible to follow pattern for much of the bottom 2653 row because no (nearby) already-constructed pixels lie 2654 on the diagonals in question. */ 2655 B[2][2] = B[2][3] = B[3][0] = B[3][1] = B[3][2] = B[3][3] 2656 = L[3]; 2657 } 2659 } 2661 ---- End code block ---------------------------------------- 2663 The reference decoder implementation of subblock intra-prediction may 2664 be found in reconintra4x4.c. 2666 13. DCT Coefficient Decoding 2668 The second data partition consists of an encoding of the quantized 2669 DCT (and WHT) coefficients of the residue signal. As discussed in 2670 the format overview (Chapter 2), for each macroblock, the residue is 2671 added to the (intra- or inter-generated) prediction buffer to produce 2672 the final (except for loop-filtering) reconstructed macroblock. 2674 VP8 works exclusively with 4x4 DCTs and WHTs, applied to the 24 (or 2675 25 with the Y2 subblock) 4x4 subblocks of a macroblock. The ordering 2676 of macroblocks within any of the "residue" partitions in general 2677 follows the same raster-scan as used in the first "prediction" 2678 partition. 2680 For all intra- and inter-prediction modes apart from B_PRED (intra: 2681 whose Y subblocks are independently predicted) and SPLIT_MV (inter) 2682 each macroblock's residue record begins with the Y2 component of the 2683 residue, coded using a WHT. B_PRED and SPLIT_MV coded macroblocks 2684 omit this WHT, instead specifying the 0th DCT coefficient of each of 2685 the 16 Y subblocks as part of its DCT. 2687 After the optional Y2 block, the residue record continues with 16 2688 DCTs for the Y subblocks, followed by 4 DCTs for the U subblocks, 2689 ending with 4 DCTs for the V subblocks. The subblocks occur in the 2690 usual order. 2692 The DCTs and WHT are tree-coded using a 12-element alphabet whose 2693 members we call tokens. Except for the end of block token (which 2694 sets the remaining subblock coefficients to zero and is followed by 2695 the next block), each token (sometimes augmented with data 2696 immediately following the token) specifies the value of the single 2697 coefficient at the current (implicit) position and is followed by a 2698 token applying to the next (implicit) position. 2700 For all the Y and chroma subblocks, the ordering of the coefficients 2701 follows a so-called zig-zag order. DCTs begin at coefficient 1 if Y2 2702 is present, and begin at coefficient 0 if Y2 is absent. The WHT for 2703 a Y2 subblock always begins at coefficient 0. 2705 13.1. MB Without non-Zero Coefficient Values 2707 If the flag within macroblock mode info indicates that a macroblock 2708 does not have any non-zero coefficients, the decoding process of DCT 2709 coefficients is skipped for the macroblock. 2711 13.2. Coding of Individual Coefficient Values 2713 The coding of coefficient tokens is the same for the DCT and WHT and 2714 for the remainder of this chapter DCT should be taken to mean either 2715 DCT or WHT. 2717 All tokens (except end-of-block) specify either a single unsigned 2718 value or a range of unsigned values (immediately) followed by a 2719 simple probabilistic encoding of the offset of the value from the 2720 base of that range. 2722 Non-zero values (of either type) are then followed by a flag 2723 indicating the sign of the coded value (negative if 1, positive if 2724 0). 2726 Here are the tokens and decoding tree. 2728 ---- Begin code block -------------------------------------- 2730 typedef enum 2731 { 2732 DCT_0, /* value 0 */ 2733 DCT_1, /* 1 */ 2734 DCT_2, /* 2 */ 2735 DCT_3, /* 3 */ 2736 DCT_4, /* 4 */ 2737 dct_cat1, /* range 5 - 6 (size 2) */ 2738 dct_cat2, /* 7 - 10 (4) */ 2739 dct_cat3, /* 11 - 18 (8) */ 2740 dct_cat4, /* 19 - 34 (16) */ 2741 dct_cat5, /* 35 - 66 (32) */ 2742 dct_cat6, /* 67 - 2048 (1982) */ 2743 dct_eob, /* end of block */ 2745 num_dct_tokens /* 12 */ 2746 } 2747 dct_token; 2749 const tree_index coef_tree [2 * (num_dct_tokens - 1)] = 2750 { 2751 -dct_eob, 2, /* eob = "0" */ 2752 -DCT_0, 4, /* 0 = "10" */ 2753 -DCT_1, 6, /* 1 = "110" */ 2754 8, 12, 2755 -DCT_2, 10, /* 2 = "11100" */ 2756 -DCT_3, -DCT_4, /* 3 = "111010", 4 = "111011" */ 2757 14, 16, 2758 -dct_cat1, -dct_cat2, /* cat1 = "111100", 2759 cat2 = "111101" */ 2760 18, 20, 2761 -dct_cat3, -dct_cat4, /* cat3 = "1111100", 2762 cat4 = "1111101" */ 2763 -dct_cat5, -dct_cat6 /* cat4 = "1111110", 2764 cat4 = "1111111" */ 2765 }; 2767 ---- End code block ---------------------------------------- 2769 While in general all DCT coefficients are decoded using the same 2770 tree, decoding of certain DCT coefficients may skip the first branch, 2771 whose preceding coefficient is a DCT_0. This makes use of the fact 2772 that in any block last non zero coefficient before the end of the 2773 block is not 0, therefore no dct_eob follows a DCT_0 coefficient in 2774 any block. 2776 The tokens dct_cat1 ... dct_cat6 specify ranges of unsigned values, 2777 the value within the range being formed by adding an unsigned offset 2778 (whose width is 1, 2, 3, 4, 5, or 11 bits, respectively) to the base 2779 of the range, using the following algorithm and fixed probability 2780 tables. 2782 ---- Begin code block -------------------------------------- 2784 uint DCTextra( bool_decoder *d, const Prob *p) 2785 { 2786 uint v = 0; 2787 do { v += v + read_bool( d, *p);} while( *++p); 2788 return v; 2789 } 2791 const Prob Pcat1[] = { 159, 0}; 2792 const Prob Pcat2[] = { 165, 145, 0}; 2793 const Prob Pcat3[] = { 173, 148, 140, 0}; 2794 const Prob Pcat4[] = { 176, 155, 140, 135, 0}; 2795 const Prob Pcat5[] = { 180, 157, 141, 134, 130, 0}; 2796 const Prob Pcat6[] = 2797 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0}; 2799 ---- End code block ---------------------------------------- 2801 If v, the unsigned value decoded using the coefficient tree, possibly 2802 augmented by the process above, is non-zero, its sign is set by 2803 simply reading a flag: 2805 ---- Begin code block -------------------------------------- 2807 if( read_bool( d, 128)) 2808 v = -v; 2810 ---- End code block ---------------------------------------- 2812 13.3. Token Probabilities 2814 The probability specification for the token tree (unlike that for the 2815 "extra bits" described above) is rather involved. It uses three 2816 pieces of context to index a large probability table, the contents of 2817 which may be incrementally modified in the frame header. The full 2818 (non-constant) probability table is laid out as follows. 2820 ---- Begin code block -------------------------------------- 2822 Prob coef_probs [4] [8] [3] [num_dct_tokens-1]; 2823 ---- End code block ---------------------------------------- 2825 Working from the outside in, the outermost dimension is indexed by 2826 the type of plane being decoded: 2828 o 0 - Y beginning at coefficient 1 (i.e., Y after Y2) 2830 o 1 - Y2 2832 o 2 - U or V 2834 o 3 - Y beginning at coefficient 0 (i.e., Y in the absence of Y2). 2836 The next dimension is selected by the position of the coefficient 2837 being decoded. That position c steps by ones up to 15, starting from 2838 zero for block types 1, 2, or 3 and starting from one for block type 2839 0. The second array index is then 2841 ---- Begin code block -------------------------------------- 2843 coef_bands [c] 2845 ---- End code block ---------------------------------------- 2847 where 2849 ---- Begin code block -------------------------------------- 2851 const int coef_bands [16] = { 2852 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7 2853 }; 2855 ---- End code block ---------------------------------------- 2857 is a fixed mapping of position to "band". 2859 The third dimension is the trickiest. Roughly speaking, it measures 2860 the "local complexity" or extent to which nearby coefficients are 2861 non-zero. 2863 For the first coefficient (DC, unless the block type is 0), we 2864 consider the (already encoded) blocks within the same plane (Y2, Y, 2865 U, or V) above and to the left of the current block. The context 2866 index is then the number (0, 1 or 2) of these blocks that had at 2867 least one non-zero coefficient in their residue record. 2869 Beyond the first coefficient, the context index is determined by the 2870 absolute value of the most recently decoded coefficient (necessarily 2871 within the current block) and is 0 if the last coefficient was a 2872 zero, 1 if it was plus or minus one, and 2 if its absolute value 2873 exceeded one. 2875 Note that the intuitive meaning of this measure changes as 2876 coefficients are decoded. For example, prior to the first token, a 2877 zero means that the neighbors are empty, suggesting that the current 2878 block may also be empty. After the first token, because an end-of- 2879 block token must have at least one non-zero value before it, a zero 2880 means that we just decoded a zero and hence guarantees that a non- 2881 zero coefficient will appear later in this block. However, this 2882 shift in meaning is perfectly okay because the complete context 2883 depends also on the coefficient band (and since band 0 is occupied 2884 exclusively by position 0). 2886 As with other contexts used by VP8, the "neighboring block" context 2887 described here needs a special definition for subblocks lying along 2888 the top row or left edge of the frame. These "non-existent" 2889 predictors above and to the left of the image are simply taken to be 2890 empty -- that is, taken to contain no non-zero coefficients. 2892 The residue decoding of each macroblock then requires, in each of two 2893 directions (above and to the left), an aggregate coefficient 2894 predictor consisting of a single Y2 predictor, two predictors for 2895 each of U and V, and four predictors for Y. In accordance with the 2896 scan-ordering of macroblocks, a decoder needs to maintain a single 2897 "left" aggregate predictor and a row of "above" aggregate predictors. 2899 Before decoding any residue, these maintained predictors may simply 2900 be cleared, in compliance with the definition of "non-existent" 2901 prediction. After each block is decoded, the two predictors 2902 referenced by the block are replaced with the (empty or non-empty) 2903 state of the block, in preparation for the later decoding of the 2904 blocks below and to the right of the block just decoded. 2906 The fourth, and final, dimension of the token probability array is of 2907 course indexed by (half) the position in the token tree structure, as 2908 are all tree probability arrays. 2910 The below pseudo-code illustrates the decoding process. Note that 2911 criteria, functions, etc. delimited with ** are either dependent on 2912 decoder architecture or are elaborated on elsewhere in this document. 2914 ---- Begin code block -------------------------------------- 2916 int block[16] = { 0 }; /* current 4x4 block coeffs */ 2917 int firstCoeff = 0; 2918 int plane; 2919 int ctx2; 2920 int ctx3 = 0; /* the 3rd context referred to in above description */ 2921 Prob *probTable; 2922 int token; 2923 int sign; 2924 int absValue; 2925 int extraBits; 2926 bool prevCoeffWasZero = false; 2927 bool currentBlockHasCoeffs = false; 2928 /* base coeff abs values per each category, elem #0 is 2929 DCT_VAL_CATEGORY1, * #1 is DCT_VAL_CATEGORY2 etc */ 2930 int categoryBase[6] = { 5, 7, 11, 19, 35, 67 }; 2932 /* Determine plane to use */ 2933 if( **current_block_is_Y2_block** ) plane = 0; 2934 else if ( **current_block_is_chroma** ) plane = 2; 2935 else if ( **current_macroblock_has_Y2** ) plane = 1; 2936 else plane = 3; 2938 /* For luma blocks of an "Y2 macroblock" we skip coeff index #0 */ 2939 if( plane == 1 ) 2940 firstCoeff++; 2942 /* Determine whether neighbour 4x4 blocks have coeffiecients. 2943 This is dependant of the plane we are currently decoding; 2944 i.e. we check only coefficients from same plane as current 2945 block. */ 2946 if( **left_neighbor_block_has_coefficients(plane)** ) 2947 ctx3++; 2948 if( **above_neighbor_block_has_coefficients(plane)** ) 2949 ctx3++; 2951 for( i = firstCoeff ; i < 16 ; ++i ) 2952 { 2953 ctx2 = coef_bands[i]; 2954 probTable = coef_probs[plane][ctx2][ctx3]; 2956 /* skip first code (dct_eob) if previous token was DCT_0 */ 2957 if( prevCoeffWasZero ) 2958 token = treed_read ( d, **coef_tree_without_eob**, 2959 probTable ); 2960 else 2961 token = treed_read ( d, coef_tree, probTable ); 2963 if( token == dct_eob ) 2964 break; 2966 if( token != DCT_0 ) 2967 { 2968 currentBlockHasCoeffs = true; 2969 if( **token_has_extra_bits(token)** ) 2970 { 2971 extraBits = DCTextra( token ); 2972 absValue = 2973 categoryBase[**token_to_cat_index(token)**] + 2974 extraBits; 2975 } 2976 else 2977 { 2978 absValue = **token_to_abs_value(token)**; 2979 } 2981 sign = read_bool(d, 128); 2982 block[i] = sign ? -absValue : absValue; 2983 } 2984 else 2985 { 2986 absValue = 0; 2987 } 2989 /* Set contexts and stuff for next coeff */ 2990 if( absValue == 0 ) ctx3 = 0; 2991 else if ( absValue == 1 ) ctx3 = 1; 2992 else ctx3 = 2; 2993 prevCoeffWasZero = true; 2994 } 2996 /* Store current block status to decoder internals */ 2997 **block_has_coefficients[currentMb][currentBlock]** = 2998 currentBlockHasCoeffs; 3000 ---- End code block ---------------------------------------- 3002 While we have in fact completely described the coefficient decoding 3003 procedure, the reader will probably find it helpful to consult the 3004 reference implementation, which can be found in the file 3005 detokenize.c. 3007 13.4. Token Probability Updates 3009 As mentioned above, the token-decoding probabilities may change from 3010 frame to frame. After detection of a key frame, they are of course 3011 set to their defaults shown in Section 13.5; this must occur before 3012 decoding the remainder of the header, as both key frames and 3013 interframes may adjust these probabilities. 3015 The layout and semantics of the coefficient probability update record 3016 (Section I of the frame header) are straightforward. For each 3017 position in the coef_probs array there occurs a fixed-probability 3018 bool indicating whether or not the corresponding probability should 3019 be updated. If the bool is true, there follows a P(8) replacing that 3020 probability. Note that updates are cumulative, that is, a 3021 probability updated on one frame is in effect for all ensuing frames 3022 until the next key frame, or until the probability is explicitly 3023 updated by another frame. 3025 The algorithm to effect the foregoing is simple: 3027 ---- Begin code block -------------------------------------- 3029 int i = 0; do { 3030 int j = 0; do { 3031 int k = 0; do { 3032 int t = 0; do { 3034 if( read_bool( d, coef_update_probs [i] [j] [k] [t])) 3035 coef_probs [i] [j] [k] [t] = read_literal( d, 8); 3037 } while( ++t < num_dct_tokens - 1); 3038 } while( ++k < 3); 3039 } while( ++j < 8); 3040 } while( ++i < 4); 3042 ---- End code block ---------------------------------------- 3044 The (constant) update probabilities are as follows (they may also be 3045 found in the reference decoder file coef_update_probs.c). 3047 ---- Begin code block -------------------------------------- 3049 const Prob coef_update_probs [4] [8] [3] [num_dct_tokens-1] = 3050 { 3051 { 3052 { 3053 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3054 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3055 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3056 }, 3057 { 3058 { 176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3059 { 223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3060 { 249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255} 3061 }, 3062 { 3063 { 255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3064 { 234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3065 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3066 }, 3067 { 3068 { 255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3069 { 239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3070 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3071 }, 3072 { 3073 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3074 { 251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3075 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3076 }, 3077 { 3078 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3079 { 251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3080 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3081 }, 3082 { 3083 { 255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255}, 3084 { 250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255}, 3085 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3086 }, 3087 { 3088 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3089 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3090 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3091 } 3092 }, 3093 { 3094 { 3095 { 217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3096 { 225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255}, 3097 { 234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255} 3098 }, 3099 { 3100 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3101 { 223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3102 { 238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3103 }, 3104 { 3105 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3106 { 249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3107 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3108 }, 3109 { 3110 { 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3111 { 247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3112 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3113 }, 3114 { 3115 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3116 { 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3117 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3118 }, 3119 { 3120 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3121 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3122 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3123 }, 3124 { 3125 { 255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3126 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3127 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3128 }, 3129 { 3130 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3131 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3132 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3133 } 3134 }, 3135 { 3136 { 3137 { 186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255}, 3138 { 234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255}, 3139 { 251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255} 3140 }, 3141 { 3142 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3143 { 236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3144 { 251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255} 3145 }, 3146 { 3147 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3148 { 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3149 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3150 }, 3151 { 3152 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3153 { 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3154 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3155 }, 3156 { 3157 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3158 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3159 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3160 }, 3161 { 3162 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3163 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3164 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3165 }, 3166 { 3167 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3168 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3169 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3170 }, 3171 { 3172 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3173 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3174 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3175 } 3176 }, 3177 { 3178 { 3179 { 248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3180 { 250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255}, 3181 { 248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255} 3182 }, 3183 { 3184 { 255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3185 { 246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3186 { 252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255} 3187 }, 3188 { 3189 { 255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3190 { 248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3191 { 253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3192 }, 3193 { 3194 { 255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3195 { 245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3196 { 253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3197 }, 3198 { 3199 { 255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3200 { 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3201 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3202 }, 3203 { 3204 { 255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3205 { 249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3206 { 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3208 }, 3209 { 3210 { 255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3211 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3212 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3213 }, 3214 { 3215 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3216 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3217 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3218 } 3219 } 3220 }; 3222 ---- End code block ---------------------------------------- 3224 13.5. Default Token Probability Table 3226 The default token probabilities are as follows. 3228 ---- Begin code block -------------------------------------- 3230 const Prob default_coef_probs [4] [8] [3] [num_dct_tokens - 1] = 3231 { 3232 { 3233 { 3234 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3235 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3236 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3237 }, 3238 { 3239 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 3240 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 3241 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 3242 }, 3243 { 3244 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 3245 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 3246 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 3247 }, 3248 { 3249 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 3250 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 3251 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 3252 }, 3253 { 3254 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 3255 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 3256 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 3257 }, 3258 { 3259 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 3260 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 3261 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 3262 }, 3263 { 3264 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 3265 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 3266 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 3267 }, 3268 { 3269 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3270 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3271 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3272 } 3273 }, 3274 { 3275 { 3276 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 3277 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 3278 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 3279 }, 3280 { 3281 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 3282 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 3283 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 3284 }, 3285 { 3286 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 3287 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 3288 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 3289 }, 3290 { 3291 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 3292 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 3293 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 3294 }, 3295 { 3296 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 3297 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 3298 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 3299 }, 3300 { 3301 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 3302 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 3303 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 3305 }, 3306 { 3307 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 3308 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 3309 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 3310 }, 3311 { 3312 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 3313 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3314 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 3315 } 3316 }, 3317 { 3318 { 3319 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 3320 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 3321 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 3322 }, 3323 { 3324 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 3325 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 3326 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 3327 }, 3328 { 3329 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 3330 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 3331 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 3332 }, 3333 { 3334 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 3335 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 3336 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 3337 }, 3338 { 3339 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3340 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 3341 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3342 }, 3343 { 3344 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3345 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3346 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3347 }, 3348 { 3349 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 3350 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 3351 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3352 }, 3353 { 3354 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3355 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3356 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3357 } 3358 }, 3359 { 3360 { 3361 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 3362 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 3363 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 3364 }, 3365 { 3366 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 3367 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 3368 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 3369 }, 3370 { 3371 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 3372 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 3373 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 3374 }, 3375 { 3376 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 3377 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 3378 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 3379 }, 3380 { 3381 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 3382 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 3383 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 3384 }, 3385 { 3386 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 3387 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 3388 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 3389 }, 3390 { 3391 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 3392 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 3393 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 3394 }, 3395 { 3396 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3397 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3398 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3399 } 3400 } 3402 }; 3404 ---- End code block ---------------------------------------- 3406 14. DCT and WHT Inversion and Macroblock Reconstruction 3408 14.1. Dequantization 3410 After decoding the DCTs/WHTs as described above, each (quantized) 3411 coefficient in each subblock is multiplied by one of six 3412 dequantization factors, the choice of factor depending on the plane 3413 (Y2, Y, or chroma) and position (DC = coefficient zero, AC = any 3414 other coefficient). If the current macroblock has overridden the 3415 quantization level (as described in Chapter 10) then the six factors 3416 are looked up from two dequantization tables with appropriate scaling 3417 and clamping using the single index supplied by the override. 3418 Otherwise, the frame-level dequantization factors (as described in 3419 Section 9.6) are used. In either case, the multiplies are computed 3420 and stored using 16-bit signed integers. 3422 The two dequantization tables, which may also be found in the 3423 reference decoder file quant_common.c, are as follows. 3425 ---- Begin code block -------------------------------------- 3427 static const int dc_qlookup[QINDEX_RANGE] = 3428 { 3429 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 3430 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 3431 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 3432 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 3433 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 3434 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 3435 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 3436 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 3437 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, 126, 128, 130, 3438 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, 3439 }; 3441 static const int ac_qlookup[QINDEX_RANGE] = 3442 { 3443 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 3444 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3445 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 3446 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 3447 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 3448 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 3449 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 3450 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 3451 181, 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, 3452 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, 3453 }; 3455 ---- End code block ---------------------------------------- 3457 Lookup values from the above two tables are directly used the DC and 3458 AC coefficients in Y1 respectively. For Y2 and chroma, values from 3459 above tables undergo either a scaling process or clamping processing 3460 before the multiplies. Details to these scaling and clamping can be 3461 found related lookup functions in quant_common.c. 3463 14.2. Inverse Transforms 3465 If the Y2 residue block exists (i.e., the macroblock luma mode is not 3466 SPLITMV or B_PRED), it is inverted first (using the inverse WHT) and 3467 the element of the result at row i, column j is used as the 0th 3468 coefficient of the Y subblock at position (i, j), that is, the Y 3469 subblock whose index is (i * 4) + j. As discussed in Chapter 13, if 3470 the luma mode is B_PRED or SPLITMV, the 0th Y coefficients are part 3471 of the residue signal for the subblocks themselves. 3473 In either case, the inverse transforms for the sixteen Y subblocks 3474 and eight chroma subblocks are computed next. All 24 of these 3475 inversions are independent of each other; their results may (at least 3476 conceptually) be stored in 24 separate 4x4 arrays. 3478 As is done by the reference decoder, an implementation may wish to 3479 represent the prediction and residue buffers as macroblock-sized 3480 arrays (that is, a 16x16 Y buffer and two 8x8 chroma buffers). 3481 Regarding the inverse DCT implementation given below, this requires a 3482 simple adjustment to the address calculation for the resulting 3483 residue pixels. 3485 14.3. Implementation of the WHT Inversion 3487 As previously discussed (see Chapters 2 and 13), for macroblocks 3488 encoded using prediction modes other than B_PRED and SPLITMV, the DC 3489 values derived from the DCT transform on the 16 Y blocks are 3490 collected to construct a 25th block of a macroblock (16 Y, 4 U, 4 V 3491 constitute the 24 blocks). This 25th block is transformed using a 3492 Walsh-Hadamard transform (WHT). 3494 The inputs to the inverse WHT (that is, the dequantized 3495 coefficients), the intermediate "horizontally detransformed" signal, 3496 and the completely detransformed residue signal are all stored as 3497 arrays of 16-bit signed integers. 3499 Following the tradition of specifying bitstream format using the 3500 decoding process, we specify the inverse WHT in the decoding process 3501 using the following C style source code: 3503 ---- Begin code block -------------------------------------- 3505 void vp8_short_inv_walsh4x4_c(short *input, short *output) 3506 { 3507 int i; 3508 int a1, b1, c1, d1; 3509 int a2, b2, c2, d2; 3510 short *ip = input; 3511 short *op = output; 3512 int temp1, temp2; 3514 for(i=0;i<4;i++) 3515 { 3516 a1 = ip[0] + ip[12]; 3517 b1 = ip[4] + ip[8]; 3518 c1 = ip[4] - ip[8]; 3519 d1 = ip[0] - ip[12]; 3520 op[0] = a1 + b1; 3521 op[4] = c1 + d1; 3522 op[8] = a1 - b1; 3523 op[12]= d1 - c1; 3524 ip++; 3525 op++; 3526 } 3527 ip = output; 3528 op = output; 3529 for(i=0;i<4;i++) 3530 { 3531 a1 = ip[0] + ip[3]; 3532 b1 = ip[1] + ip[2]; 3533 c1 = ip[1] - ip[2]; 3534 d1 = ip[0] - ip[3]; 3536 a2 = a1 + b1; 3537 b2 = c1 + d1; 3538 c2 = a1 - b1; 3539 d2 = d1 - c1; 3541 op[0] = (a2+3)>>3; 3542 op[1] = (b2+3)>>3; 3543 op[2] = (c2+3)>>3; 3544 op[3] = (d2+3)>>3; 3546 ip+=4; 3547 op+=4; 3548 } 3549 } 3551 ---- End code block ---------------------------------------- 3553 In the case that there is only one non-zero DC value in input, the 3554 inverse transform can be simplified to the following: 3556 ---- Begin code block -------------------------------------- 3558 void vp8_short_inv_walsh4x4_1_c(short *input, short *output) 3559 { 3560 int i; 3561 int a1; 3562 short *op=output; 3564 a1 = ((input[0] + 3)>>3); 3566 for(i=0;i<4;i++) 3567 { 3568 op[0] = a1; 3569 op[1] = a1; 3570 op[2] = a1; 3571 op[3] = a1; 3572 op+=4; 3573 } 3574 } 3576 ---- End code block ---------------------------------------- 3578 It should be noted, a conforming decoder should implement the inverse 3579 transform using exactly the same rounding to achieve bit-wise 3580 matching output to the output of the process specified by the above 3581 "C" source code. 3583 The reference decoder WHT inversion may be found in the files 3584 invtrans.c and idctllm.c. 3586 14.4. Implementation of the DCT Inversion 3588 All of the DCT inversions are computed in exactly the same way. In 3589 principle, VP8 uses a classical 2D inverse discrete cosine transform, 3590 implemented as two passes of 1-D inverse DCT. The 1-D inverse DCT 3591 was calculated using a similar algorithm to what was described in 3592 [Loeffler]. However, the paper only provided the 8-point and 16- 3593 point version of the algorithms, which was adapted by On2 to perform 3594 the 4-point 1-D DCT. 3596 Accurate calculation of 1-D DCT of the above algorithm requires 3597 infinite precision. VP8 of course can use only a finite-precision 3598 approximation. Also, the inverse DCT used by VP8 takes care of 3599 normalization of the standard unitary transform, that is, every 3600 dequantized coefficient has roughly double the size of the 3601 corresponding unitary coefficient. However, at all but the highest 3602 datarates, the discrepancy between transmitted and ideal coefficients 3603 is due almost entirely to (lossy) compression and not to errors 3604 induced by finite-precision arithmetic. 3606 The inputs to the inverse DCT (that is, the dequantized 3607 coefficients), the intermediate "horizontally detransformed" signal, 3608 and the completely detransformed residue signal are all stored as 3609 arrays of 16-bit signed integers. The details of the computation are 3610 as follows. 3612 It should also be noted that this implementation makes use of 16-bit 3613 fixed point version of two multiplication constants: 3615 sqrt(2) * cos (pi/8) 3617 sqrt(2) * sin (pi/8) 3619 Because the first constant is bigger than 1, to maintain the same 16- 3620 bit fixed point precision as the second one, we make use of the fact 3621 that 3623 x * a = x + x*(a-1) 3625 therefore 3627 x * sqrt(2) * cos (pi/8) = x + x * ( sqrt(2) * cos(pi/8)-1) 3629 ---- Begin code block -------------------------------------- 3631 /* IDCT implementation */ 3632 static const int cospi8sqrt2minus1=20091; 3633 static const int sinpi8sqrt2 =35468; 3634 void short_idct4x4llm_c(short *input, short *output, int pitch) 3635 { 3636 int i; 3637 int a1, b1, c1, d1; 3639 short *ip=input; 3640 short *op=output; 3641 int temp1, temp2; 3642 int shortpitch = pitch>>1; 3644 for(i=0;i<4;i++) 3645 { 3646 a1 = ip[0]+ip[8]; 3647 b1 = ip[0]-ip[8]; 3649 temp1 = (ip[4] * sinpi8sqrt2)>>16; 3650 temp2 = ip[12]+((ip[12] * cospi8sqrt2minus1)>>16); 3651 c1 = temp1 - temp2; 3653 temp1 = ip[4] + ((ip[4] * cospi8sqrt2minus1)>>16); 3654 temp2 = (ip[12] * sinpi8sqrt2)>>16; 3655 d1 = temp1 + temp2; 3657 op[shortpitch*0] = a1+d1; 3658 op[shortpitch*3] = a1-d1; 3659 op[shortpitch*1] = b1+c1; 3660 op[shortpitch*2] = b1-c1; 3662 ip++; 3663 op++; 3664 } 3665 ip = output; 3666 op = output; 3667 for(i=0;i<4;i++) 3668 { 3669 a1 = ip[0]+ip[2]; 3670 b1 = ip[0]-ip[2]; 3672 temp1 = (ip[1] * sinpi8sqrt2)>>16; 3673 temp2 = ip[3]+((ip[3] * cospi8sqrt2minus1)>>16); 3674 c1 = temp1 - temp2; 3676 temp1 = ip[1] + ((ip[1] * cospi8sqrt2minus1)>>16); 3677 temp2 = (ip[3] * sinpi8sqrt2)>>16; 3678 d1 = temp1 + temp2; 3680 op[0] = (a1+d1+4)>>3; 3681 op[3] = (a1-d1+4)>>3; 3682 op[1] = (b1+c1+4)>>3; 3683 op[2] = (b1-c1+4)>>3; 3685 ip+=shortpitch; 3686 op+=shortpitch; 3687 } 3688 } 3690 ---- End code block ---------------------------------------- 3692 The reference decoder DCT inversion may be found in the files 3693 invtrans.c and idctllm.c. 3695 14.5. Summation of Predictor and Residue 3697 Finally, the prediction and residue signals are summed to form the 3698 reconstructed macroblock, which, except for loop filtering (taken up 3699 next), completes the decoding process. 3701 The summing procedure is fairly straightforward, having only a couple 3702 of details. The prediction and residue buffers are both arrays of 3703 16-bit signed integers. Each individual (Y, U, and V pixel) result 3704 is calculated first as a 32-bit sum of the prediction and residue, 3705 and is then saturated to 8-bit unsigned range (using, say, the 3706 clamp255 function defined above) before being stored as an 8-bit 3707 unsigned pixel value. 3709 VP8 also supports a mode where the encoding of a bitstream guarantees 3710 all reconstructed pixel values between 0 and 255, compliant 3711 bitstreams of such requirements have the clamp_type bit in the frame 3712 header set to 1. In such case, the clamp255 is no longer required. 3714 The summation process is the same, regardless of the (intra or inter) 3715 mode of prediction in effect for the macroblock. The reference 3716 decoder implementation of reconstruction may be found in the file 3717 recon.c. 3719 15. Loop Filter 3721 Loop filtering is the last stage of frame reconstruction and the 3722 next-to-last stage of the decoding process. The loop filter is 3723 applied to the entire frame after the summation of predictor and 3724 residue described in Chapter 14. 3726 The purpose of the loop filter is to eliminate (or at least reduce) 3727 visually objectionable artifacts associated with the semi- 3728 independence of the coding of macroblocks and their constituent 3729 subblocks. 3731 As was discussed in Chapter 5, the loop filter is "integral" to 3732 decoding, in that the results of loop filtering are used in the 3733 prediction of subsequent frames. Consequently, a functional decoder 3734 implementation must perform loop filtering exactly as described here. 3735 This is in distinction to any postprocessing that may be applied only 3736 to the image immediately before display; such postprocessing is 3737 entirely at the option of the implementor (and/or user) and has no 3738 effect on decoding per se. 3740 The baseline frame level parameters controlling the loop filter are 3741 defined in the frame header (Chapter 9.4) along with a mechanism for 3742 adjustment based on a macroblock's prediction mode and/or reference 3743 frame. The first is a flag selecting the type of filter (normal or 3744 simple), the other two are numbers (loop_filter_level and 3745 sharpness_level) that adjust the strength or sensitivity of the 3746 filter. As described in Chapters 9.3 and 10, loop_filter_level may 3747 be also overridden on a per-macroblock basis using segmentation. 3749 Loop filtering is one of the more computationally-intensive aspects 3750 of VP8 decoding. This is the reason for the existence of the 3751 optional less-demanding simple filter type. Also, the loop filter is 3752 completely disabled if the loop_filter_level in the frame header is 3753 zero; macroblock-level overrides are ignored in this case. (It is of 3754 course possible for a compressor to encode a frame in which only a 3755 few macroblocks are loop filtered: The global loop_filter_level must 3756 be non-zero and each macroblock can select one of four levels, most 3757 of which could be zero.) 3759 To facilitate efficient implementation, the VP8 decoding algorithms 3760 generally, and the loop filter especially, were designed with SIMD 3761 ("Single Instruction Multiple Datum" or "integer vector") processors 3762 in mind. The reference decoder implementation of loop filtering 3763 (found in loopfilter.c) is, in effect, a portable SIMD specification 3764 of the loop filtering algorithms intended to simplify a realization 3765 on an actual SIMD processor. 3767 Unfortunately, the approach taken there does not lead to maximal 3768 efficency (restricted to the C language, that is) and, as far as a 3769 pure algorithm specification is concerned, is in places obscure. For 3770 example, various aspects of filtering are conditioned on absolute 3771 differences lying below certain thresholds. An ordinary C 3772 implementation would simply discriminate amongst these behaviors 3773 using if statements. The reference decoder instead effects this by 3774 "masking arithmetic", that is, using "and" operations to 3775 (conditionally) zero-out values to be added or subtracted to pixels. 3776 Furthermore, the structure holding the various threshold values is 3777 artificially parallelized. While this mimics closely the approach 3778 taken in vector-processor machine language, it is not how one usually 3779 programs in C. 3781 In this document, we take a different approach and present the 3782 algorithms in a more straightforward, idiomatic, and terse C style. 3783 Together with the reference version, we hope to provide the "best of 3784 both worlds", that is, a pure algorithm specification here and a 3785 strong suggestion as to an optimal actual implementation in 3786 loopfilter.c. 3788 We begin by discussing the aspects of loop filtering that are 3789 independent of the controlling parameters and type of filter chosen. 3791 15.1. Filter Geometry and Overall Procedure 3793 The Y, U, and V planes are processed independently and, except for 3794 the values of certain control parameters (derived from the 3795 loop_filter_level and sharpness_level), identically. 3797 The loop filter acts on the edges between adjacent macroblocks and on 3798 the edges between adjacent subblocks of a macroblock. All such edges 3799 are horizontal or vertical. For each pixel position on an edge, a 3800 small number (two or three) of pixels adjacent to either side of the 3801 position are examined and possibly modified. The displacements of 3802 these pixels are at a right angle to the edge orientation, that is, 3803 for a horizontal edge, we treat the pixels immediately above and 3804 below the edge position, for a vertical edge, we treat the pixels 3805 immediately to the left and right of the edge. 3807 We call this collection of pixels associated to an edge position a 3808 segment; the length of a segment is 2, 4, 6, or 8. Excepting that 3809 the normal filter uses slightly different algorithms for, and that 3810 either filter may apply different control parameters to, the edges 3811 between macroblocks and those between subblocks, the treatment of 3812 edges is quite uniform: All segments straddling an edge are treated 3813 identically, there is no distinction between the treatment of 3814 horizontal and vertical edges, whether between macroblocks or between 3815 subblocks. 3817 As a consequence, adjacent subblock edges within a macroblock may be 3818 concatenated and processed in their entirety. There is a single 3819 8-pixel long vertical edge horizontally centered in each of the U and 3820 V blocks (the concatenation of upper and lower 4-pixel edges between 3821 chroma subblocks), and three 16-pixel long vertical edges at 3822 horizontal positions 1/4, 1/2, and 3/4 the width of the luma 3823 macroblock, each representing the concatenation of four 4-pixel sub- 3824 edges between pairs of Y subblocks. 3826 The macroblocks comprising the frame are processed in the usual 3827 raster-scan order. Each macroblock is "responsible for" the inter- 3828 macroblock edges immediately above and left of it (but not the edges 3829 below and right of it), as well as the edges between its subblocks. 3831 For each macroblock M, there are four filtering steps, which are, 3832 (almost) in order: 3834 1. If M is not on the leftmost column of macroblocks, filter across 3835 the left (vertical) inter-macroblock edge of M. 3837 2. Filter across the vertical subblock edges within M. 3839 3. If M is not on the topmost row of macroblocks, filter across the 3840 top (horizontal) inter-macroblock edge of M. 3842 4. Filter across the horizontal subblock edges within M. 3844 We write MY, MU, and MV for the planar constituents of M, that is, 3845 the 16x16 luma block, 8x8 U block, and 8x8 V block comprising M. 3847 In step 1, for each of the three blocks MY, MU, and MV, we filter 3848 each of the (16 luma or 8 chroma) segments straddling the column 3849 separating the block from the block immediately to the left of it, 3850 using the inter-macroblock filter and controls associated to the 3851 loop_filter_level and sharpness_level. 3853 In step 4, we filter across the (three luma and one each for U and V) 3854 vertical subblock edges described above, this time using the inter- 3855 subblock filter and controls. 3857 Step 2 and 4 are skipped for macroblocks that satisfy both of the 3858 following two conditions: 3860 1. Macroblock coding mode is neither B_PRED nor SPLTMV; and 3861 2. There is no DCT coefficient coded for the whole macroblock. 3863 For these macroblocks, loop filtering for edges between subblocks 3864 internal to a macroblock is effectively skipped. This skip strategy 3865 significantly reduces VP8 loop-filtering complexity. 3867 Edges between macroblocks and those between subblocks are treated 3868 with different control parameters (and, in the case of the normal 3869 filter, with different algorithms); luma and chroma edges are also 3870 treated with different control parameters. Except for pixel 3871 addressing, there is no distinction between the treatment of vertical 3872 and horizontal edges. Luma edges are always 16 pixels long, chroma 3873 edges are always 8 pixels long, and the segments straddling an edge 3874 are treated identically; this of course facilitates vector 3875 processing. 3877 Because many pixels belong to segments straddling two or more edges, 3878 and so will be filtered more than once, the order in which edges are 3879 processed given above must be respected by any implementation. 3880 Within a single edge, however, the segments straddling that edge are 3881 disjoint and the order in which these segments are processed is 3882 immaterial. 3884 Before taking up the filtering algorithms themselves, we should 3885 emphasize a point already made: Even though the pixel segments 3886 associated to a macroblock are antecedent to the macroblock (that is, 3887 lie within the macroblock or in already-constructed macroblocks), a 3888 macroblock must not be filtered immediately after its 3889 "reconstruction" (described in Chapter 14). Rather, the loop filter 3890 applies after all the macroblocks have been "reconstructed" (i.e., 3891 had their predictor summed with their residue); correct decoding is 3892 predicated on the fact that already-constructed portions of the 3893 current frame referenced via intra-prediction (described in Chapter 3894 12) are not yet filtered. 3896 15.2. Simple Filter 3898 Having described the overall procedure of, and pixels affected by, 3899 the loop filter, we turn our attention to the treatment of individual 3900 segments straddling edges. We begin by describing the simple filter, 3901 which, as the reader might guess, is somewhat simpler than the normal 3902 filter. 3904 Note that the simple filter only applies to luma edges. Chroma edges 3905 are left unfiltered. 3907 Roughly speaking, the idea of loop filtering is, within limits, to 3908 reduce the difference between pixels straddling an edge. Differences 3909 in excess of a threshold (associated to the loop_filter_level) are 3910 assumed to be "natural" and are unmodified; differences below the 3911 threshold are assumed to be artifacts of quantization and the 3912 (partially) separate coding of blocks, and are reduced via the 3913 procedures described below. While the loop_filter_level is in 3914 principle arbitrary, the levels chosen by a VP8 compressor tend to be 3915 correlated to quantization levels. 3917 Most of the filtering arithmetic is done using 8-bit signed operands 3918 (having a range -128 to +127, inclusive), supplemented by 16-bit 3919 temporaries holding results of multiplies. 3921 Sums and other temporaries need to be "clamped" to a valid signed 3922 8-bit range: 3924 ---- Begin code block -------------------------------------- 3926 int8 c( int v) 3927 { 3928 return (int8) (v < -128 ? -128 : (v < 128 ? v : 127)); 3929 } 3931 ---- End code block ---------------------------------------- 3933 Since pixel values themselves are unsigned 8-bit numbers, we need to 3934 convert between signed and unsigned values: 3936 ---- Begin code block -------------------------------------- 3938 /* Convert pixel value (0 <= v <= 255) to an 8-bit signed 3939 number. */ 3940 int8 u2s( Pixel v) { return (int8) (v - 128);} 3942 /* Clamp, then convert signed number back to pixel value. */ 3943 Pixel s2u( int v) { return (Pixel) ( c(v) + 128);} 3945 ---- End code block ---------------------------------------- 3947 Filtering is often predicated on absolute-value thresholds. The 3948 following function is the equivalent of the standard library function 3949 abs, whose prototype is found in the standard header file stdlib.h. 3950 For us, the argument v is always the difference between two pixels 3951 and lies in the range -255 <= v <= +255. 3953 ---- Begin code block -------------------------------------- 3955 int abs( int v) { return v < 0? -v : v;} 3956 ---- End code block ---------------------------------------- 3958 An actual implementation would of course use inline functions or 3959 macros to accomplish these trivial procedures (which are used by both 3960 the normal and simple loop filters). An optimal implementation would 3961 probably express them in machine language, perhaps using SIMD vector 3962 instructions. On many SIMD processors, the saturation accomplished 3963 by the above clamping function is often folded into the arithmetic 3964 instructions themselves, obviating the explicit step taken here. 3966 To simplify the specification of relative pixel positions, we use the 3967 word before to mean "immediately above" (for a vertical segment 3968 straddling a horizontal edge) or "immediately to the left of" (for a 3969 horizontal segment straddling a vertical edge) and the word after to 3970 mean "immediately below" or "immediately to the right of". 3972 Given an edge, a segment, and a limit value, the simple loop filter 3973 computes a value based on the four pixels that straddle the edge (two 3974 either side). If that value is below a supplied limit, then, very 3975 roughly speaking, the two pixel values are brought closer to each 3976 other, "shaving off" something like a quarter of the difference. The 3977 same procedure is used for all segments straddling any type of edge, 3978 regardless of the nature (inter-macroblock, inter-subblock, luma, or 3979 chroma) of the edge; only the limit value depends on the edge-type. 3981 The exact procedure (for a single segment) is as follows; the 3982 subroutine common_adjust is used by both the simple filter presented 3983 here and the normal filters discussed in Section 15.3. 3985 ---- Begin code block -------------------------------------- 3987 int8 common_adjust( 3988 int use_outer_taps, /* filter is 2 or 4 taps wide */ 3989 const Pixel *P1, /* pixel before P0 */ 3990 Pixel *P0, /* pixel before edge */ 3991 Pixel *Q0, /* pixel after edge */ 3992 const Pixel *Q1 /* pixel after Q0 */ 3993 ) { 3994 cint8 p1 = u2s( *P1); /* retrieve and convert all 4 pixels */ 3995 cint8 p0 = u2s( *P0); 3996 cint8 q0 = u2s( *Q0); 3997 cint8 q1 = u2s( *Q1); 3999 /* Disregarding clamping, when "use_outer_taps" is false, 4000 "a" is 3*(q0-p0). Since we are about to divide "a" by 4001 8, in this case we end up multiplying the edge 4002 difference by 5/8. 4004 When "use_outer_taps" is true (as for the simple filter), 4005 "a" is p1 - 3*p0 + 3*q0 - q1, which can be thought of as 4006 a refinement of 2*(q0 - p0) and the adjustment is 4007 something like (q0 - p0)/4. */ 4009 int8 a = c( ( use_outer_taps? c(p1 - q1) : 0 ) + 3*(q0 - p0) ); 4011 /* b is used to balance the rounding of a/8 in the case where 4012 the "fractional" part "f" of a/8 is exactly 1/2. */ 4014 cint8 b = (c(a + 3)) >> 3; 4016 /* Divide a by 8, rounding up when f >= 1/2. 4017 Although not strictly part of the "C" language, 4018 the right-shift is assumed to propagate the sign bit. */ 4020 a = c( a + 4) >> 3; 4022 /* Subtract "a" from q0, "bringing it closer" to p0. */ 4024 *Q0 = s2u( q0 - a); 4026 /* Add "a" (with adjustment "b") to p0, "bringing it closer" 4027 to q0. 4029 The clamp of "a+b", while present in the reference decoder, 4030 is superfluous; we have -16 <= a <= 15 at this point. */ 4032 *P0 = s2u( p0 + b); 4034 return a; 4035 } 4037 ---- End code block ---------------------------------------- 4038 ---- Begin code block -------------------------------------- 4040 void simple_segment( 4041 uint8 edge_limit, /* do nothing if edge difference 4042 exceeds limit */ 4043 const Pixel *P1, /* pixel before P0 */ 4044 Pixel *P0, /* pixel before edge */ 4045 Pixel *Q0, /* pixel after edge */ 4046 const Pixel *Q1 /* pixel after Q0 */ 4047 ) { 4048 if( (abs(*P0 - *Q0)*2 + abs(*P1-*Q1)/2) <= edge_limit)) 4049 common_adjust( 1, P1, P0, Q0, Q1); /* use outer taps */ 4050 } 4052 ---- End code block ---------------------------------------- 4054 We make a couple of remarks about the rounding procedure above. When 4055 b is zero (that is, when the "fractional part" of a is not 1/2 ), we 4056 are (except for clamping) adding the same number to p0 as we are 4057 subtracting from q0. This preserves the average value of p0 and q0 4058 but the resulting difference between p0 and q0 is always even; in 4059 particular, the smallest non-zero gradation +-1 is not possible here. 4061 When b is one, the value we add to p0 (again except for clamping) is 4062 one less than the value we are subtracting from q0. In this case, 4063 the resulting difference is always odd (and the small gradation +-1 4064 is possible) but the average value is reduced by 1/2, yielding, for 4065 instance, a very slight darkening in the luma plane. (In the very 4066 unlikely event of appreciable darkening after a large number of 4067 interframes, a compressor would of course eventually compensate for 4068 this in the selection of predictor and/or residue.) 4070 The derivation of the edge_limit value used above, which depends on 4071 the loop_filter_level and sharpness_level, as well as the type of 4072 edge being processed, will be taken up after we describe the normal 4073 loop filtering algorithm below. 4075 15.3. Normal Filter 4077 The normal loop filter is a refinement of the simple loop filter; all 4078 of the general discussion above applies here as well. In particular, 4079 the functions c, u2s, s2u, abs, and common_adjust are used by both 4080 the normal and simple filters. 4082 As mentioned above, the normal algorithms for inter-macroblock and 4083 inter-subblock edges differ. Nonetheless, they have a great deal in 4084 common: They use similar threshold algorithms to disable the filter 4085 and to detect high internal edge variance (which influences the 4086 filtering algorithm). Both algorithms also use, at least 4087 conditionally, the simple filter adjustment procedure described 4088 above. 4090 The common thresholding algorithms are as follows. 4092 ---- Begin code block -------------------------------------- 4094 /* All functions take (among other things) a segment (of length 4095 at most 4 + 4 = 8) symmetrically straddling an edge. 4097 The pixel values (or pointers) are always given in order, 4098 from the "beforemost" to the "aftermost". So, for a 4099 horizontal edge (written "|"), an 8-pixel segment would be 4100 ordered p3 p2 p1 p0 | q0 q1 q2 q3. */ 4102 /* Filtering is disabled if the difference between any two 4103 adjacent "interior" pixels in the 8-pixel segment exceeds 4104 the relevant threshold (I). A more complex thresholding 4105 calculation is done for the group of four pixels that 4106 straddle the edge, in line with the calculation in 4107 simple_segment() above. */ 4109 int filter_yes( 4110 uint8 I, /* limit on interior differences */ 4111 uint8 E, /* limit at the edge */ 4113 cint8 p3, cint8 p2, cint8 p1, cint8 p0, /* pixels before 4114 edge */ 4115 cint8 q0, cint8 q1, cint8 q2, cint8 q3 /* pixels after 4116 edge */ 4117 ) { 4118 return (abs(p0 - q0)*2 + abs(p1-q1)/2) <= E 4119 && abs(p3 - p2) <= I && abs(p2 - p1) <= I && 4120 abs(p1 - p0) <= I 4121 && abs(q3 - q2) <= I && abs(q2 - q1) <= I && 4122 abs(q1 - q0) <= I; 4123 } 4125 ---- End code block ---------------------------------------- 4126 ---- Begin code block -------------------------------------- 4128 /* Filtering is altered if (at least) one of the differences 4129 on either side of the edge exceeds a threshold (we have 4130 "high edge variance"). */ 4132 int hev( 4133 uint8 threshold, 4134 cint8 p1, cint8 p0, /* pixels before edge */ 4135 cint8 q0, cint8 q1 /* pixels after edge */ 4136 ) { 4137 return abs(p1 - p0) > threshold || abs(q1 - q0) > threshold; 4138 } 4140 ---- End code block ---------------------------------------- 4142 The subblock filter is a variant of the simple filter. In fact, if 4143 we have high edge variance, the adjustment is exactly as for the 4144 simple filter. Otherwise, the simple adjustment (without outer taps) 4145 is applied and the two pixels one step in from the edge pixels are 4146 adjusted by roughly half the amount by which the two edge pixels are 4147 adjusted; since the edge adjustment here is essentially 3/8 the edge 4148 difference, the inner adjustment is approximately 3/16 the edge 4149 difference. 4151 ---- Begin code block -------------------------------------- 4153 void subblock_filter( 4154 uint8 hev_threshold, /* detect high edge variance */ 4155 uint8 interior_limit, /* possibly disable filter */ 4156 uint8 edge_limit, 4157 cint8 *P3, cint8 *P2, int8 *P1, int8 *P0, /* pixels before 4158 edge */ 4159 int8 *Q0, int8 *Q1, cint8 *Q2, cint8 *Q3 /* pixels after 4160 edge */ 4161 ) { 4162 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4163 p0 = u2s(*P0); 4164 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4165 q3 = u2s(*Q3); 4167 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4168 p0, p1, p2, p3)) 4169 { 4170 const int hv = hev( hev_threshold, p1, p0, q0, q1); 4172 cint8 a = ( common_adjust( hv, P1, P0, Q0, Q1) + 1) >> 1; 4174 if( !hv) { 4175 *Q1 = s2u( q1 - a); 4176 *P1 = s2u( p1 + a); 4177 } 4178 } 4179 } 4181 ---- End code block ---------------------------------------- 4183 The inter-macroblock filter has potentially wider scope. If the edge 4184 variance is high, it performs the simple adjustment (using the outer 4185 taps, just like the simple filter and the corresponding case of the 4186 normal subblock filter). If the edge variance is low, we begin with 4187 the same basic filter calculation and apply multiples of it to pixel 4188 pairs symmetric about the edge; the magnitude of adjustment decays as 4189 we move away from the edge and six of the pixels in the segment are 4190 affected. 4192 ---- Begin code block -------------------------------------- 4194 void MBfilter( 4195 uint8 hev_threshold, /* detect high edge variance */ 4196 uint8 interior_limit, /* possibly disable filter */ 4197 uint8 edge_limit, 4198 cint8 *P3, int8 *P2, int8 *P1, int8 *P0, /* pixels before 4199 edge */ 4200 int8 *Q0, int8 *Q1, int8 *Q2, cint8 *Q3 /* pixels after 4201 edge */ 4202 ) { 4203 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4204 p0 = u2s(*P0); 4205 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4206 q3 = u2s(*Q3); 4208 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4209 p0, p1, p2, p3)) 4210 { 4211 if( !hev( hev_threshold, p1, p0, q0, q1)) 4212 { 4213 /* Same as the initial calculation in "common_adjust", 4214 w is something like twice the edge difference */ 4216 const int8 w = c( c(p1 - q1) + 3*(q0 - p0) ); 4218 /* 9/64 is approximately 9/63 = 1/7 and 1<<7 = 128 = 4219 2*64. So this a, used to adjust the pixels adjacent 4220 to the edge, is something like 3/7 the edge 4221 difference. */ 4223 int8 a = c( (27*w + 63) >> 7); 4225 *Q0 = s2u( q0 - a); *P0 = s2u( p0 + a); 4227 /* Next two are adjusted by 2/7 the edge difference */ 4229 a = c( (18*w + 63) >> 7); 4231 *Q1 = s2u( q1 - a); *P1 = s2u( p1 + a); 4233 /* Last two are adjusted by 1/7 the edge difference */ 4235 a = c( (9*w + 63) >> 7); 4237 *Q2 = s2u( q2 - a); *P2 = s2u( p2 + a); 4239 } else /* if hev, do simple filter */ 4240 common_adjust( 1, P1, P0, Q0, Q1); /* using outer 4241 taps */ 4242 } 4244 } 4246 ---- End code block ---------------------------------------- 4248 15.4. Calculation of Control Parameters 4250 We conclude the discussion of loop filtering by showing how the 4251 thresholds supplied to the procedures above are derived from the two 4252 control parameters sharpness_level (an unsigned 3-bit number having 4253 maximum value 7) and loop_filter_level (an unsigned 6-bit number 4254 having maximum value 63). 4256 While the sharpness_level is constant over the frame, individual 4257 macroblocks may override the loop_filter_level with one of four 4258 possibilities supplied in the frame header (as described in Chapter 4259 10). 4261 Both the simple and normal filters disable filtering if a value 4262 derived from the four pixels that straddle the edge (2 either side) 4263 exceeds a threshold / limit value. 4265 ---- Begin code block -------------------------------------- 4267 /* Luma and Chroma use the same inter-macroblock edge limit */ 4268 uint8 mbedge_limit = ((loop_filter_level + 2) * 2) + 4269 interior_limit; 4271 /* Luma and Chroma use the same inter-subblock edge limit */ 4272 uint8 sub_bedge_limit = (loop_filter_level * 2) + interior_limit; 4274 ---- End code block ---------------------------------------- 4276 The remaining thresholds are used only by the normal filters. The 4277 filter-disabling interior difference limit is the same for all edges 4278 (luma, chroma, inter-subblock, inter-macroblock) and is given by the 4279 following. 4281 ---- Begin code block -------------------------------------- 4283 uint8 interior_limit = loop_filter_level; 4285 if( sharpness_level) 4286 { 4287 interior_limit >>= sharpness_level > 4 ? 2 : 1; 4288 if( interior_limit > 9 - sharpness_level) 4289 interior_limit = 9 - sharpness_level; 4290 } 4291 if( !interior_limit) 4292 interior_limit = 1; 4294 ---- End code block ---------------------------------------- 4295 Finally, we give the derivation of the high edge-variance threshold, 4296 which is also the same for all edge types. 4298 ---- Begin code block -------------------------------------- 4300 uint8 hev_threshold = 0; 4302 if( we_are_decoding_akey_frame) /* current frame is a key frame */ 4303 { 4304 if( loop_filter_level >= 40) 4305 hev_threshold = 2; 4306 else if( loop_filter_level >= 15) 4307 hev_threshold = 1; 4308 } 4309 else /* current frame is an interframe */ 4310 { 4311 if( loop_filter_level >= 40) 4312 hev_threshold = 3; 4313 else if( loop_filter_level >= 20) 4314 hev_threshold = 2; 4315 else if( loop_filter_level >= 15) 4316 hev_threshold = 1; 4317 } 4319 ---- End code block ---------------------------------------- 4321 16. Interframe Macroblock Prediction Records 4323 We describe the layout and semantics of the prediction records for 4324 macroblocks in an interframe. 4326 After the feature specification (which is described in Chapter 10 and 4327 is identical for intraframes and interframes), there comes a Bool( 4328 prob_intra), which indicates inter-prediction (i.e., prediction from 4329 prior frames) when true and intra-prediction (i.e., prediction from 4330 already-coded portions of the current frame) when false. The zero- 4331 probability prob_intra is set by field J of the frame header. 4333 16.1. Intra-Predicted Macroblocks 4335 For intra-prediction, the layout of the prediction data is 4336 essentially the same as the layout for key frames, although the 4337 contexts used by the decoding process are slightly different. 4339 As discussed in Chapter 8, the "outer" Y mode here uses a different 4340 tree from that used in key frames, repeated here for convenience. 4342 ---- Begin code block -------------------------------------- 4344 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 4345 { 4346 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 4347 4, 6, /* "1" subtree has 2 descendant subtrees */ 4348 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 4349 H_PRED = "101" */ 4350 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 4351 B_PRED = "111" */ 4352 }; 4354 ---- End code block ---------------------------------------- 4356 The probability table used to decode this tree is variable. As 4357 described in Section 9, it (along with the similarly-treated UV 4358 table) can be updated by field J of the frame header. Similar to the 4359 coefficient-decoding probabilities, such updates are cumulative and 4360 affect all ensuing frames until the next key frame or explicit 4361 update. The default probabilities for the Y and UV tables are 4363 ---- Begin code block -------------------------------------- 4365 Prob ymode_prob [num_ymodes - 1] = { 112, 86, 140, 37}; 4366 Prob uv_mode_prob [num_uv_modes - 1] = { 162, 101, 204}; 4368 ---- End code block ---------------------------------------- 4369 These defaults must be restored after detection of a key frame. 4371 Just as for key frames, if the Y mode is B_PRED, there next comes an 4372 encoding of the intra_bpred mode used by each of the sixteen Y 4373 subblocks. These encodings use the same tree as does that for key 4374 frames but, in place of the contexts used in key frames, use the 4375 single fixed probability table 4377 ---- Begin code block -------------------------------------- 4379 const Prob bmode_prob [num_intra_bmodes - 1] = { 4380 120, 90, 79, 133, 87, 85, 80, 111, 151 4381 }; 4383 ---- End code block ---------------------------------------- 4385 Last comes the chroma mode, again coded using the same tree as that 4386 for key frames, this time using the dynamic uv_mode_prob table 4387 described above. 4389 The calculation of the intra-prediction buffer is identical to that 4390 described for key frames in Chapter 12. 4392 16.2. Inter-Predicted Macroblocks 4394 Otherwise (when the above bool is true), we are using inter- 4395 prediction (which of course only happens for interframes), to which 4396 we now restrict our attention. 4398 The next datum is then another bool, B( prob_last), selecting the 4399 reference frame. If 0, the reference frame is previous frame (last 4400 frame); if 1, another bool (prob_gf) selects the reference frame 4401 between golden frame (0) or altref frame (1). The probabilities 4402 prob_last and prob_gf are set in field J of the frame header. 4404 Together with setting the reference frame, the purpose of inter-mode 4405 decoding is to set a motion vector for each of the sixteen Y 4406 subblocks of the current macroblock. This then defines the 4407 calculation of the inter-prediction buffer (detailed in Chapter 18). 4408 While the net effect of inter-mode decoding is straightforward, the 4409 implementation is somewhat complex; the (lossless) compression 4410 achieved by this method justifies the complexity. 4412 After the reference frame selector comes the mode (or motion vector 4413 reference) applied to the macroblock as a whole, coded using the 4414 following enumeration and tree. Setting mv_nearest = num_ymodes is a 4415 convenience that allows a single variable to unambiguously hold an 4416 inter- or intraprediction mode. 4418 ---- Begin code block -------------------------------------- 4420 typedef enum 4421 { 4422 mv_nearest = num_ymodes, /* use "nearest" motion vector 4423 for entire MB */ 4424 mv_near, /* use "next nearest" "" */ 4425 mv_zero, /* use zero "" */ 4426 mv_new, /* use explicit offset from 4427 implicit "" */ 4428 mv_split, /* use multiple motion vectors */ 4430 num_mv_refs = mv_split + 1 - mv_nearest 4431 } 4432 mv_ref; 4434 const tree_index mv_ref_tree [2 * (num_mv_refs - 1)] = 4435 { 4436 -mv_zero, 2, /* zero = "0" */ 4437 -mv_nearest, 4, /* nearest = "10" */ 4438 -mv_near, 6, /* near = "110" */ 4439 -mv_new, -mv_split /* new = "1110", split = "1111" */ 4440 }; 4442 ---- End code block ---------------------------------------- 4444 16.3. Mode and Motion Vector Contexts 4446 The probability table used to decode the mv_ref, along with three 4447 reference motion vectors used by the selected mode, is calculated via 4448 a survey of the already-decoded motion vectors in (up to) 3 nearby 4449 macroblocks. 4451 The algorithm generates a sorted list of distinct motion vectors 4452 adjacent to the search site. The best_mv is the vector with the 4453 highest score. The nearest_mv is the non-zero vector with the 4454 highest score. The near_mv is the non-zero vector with the next 4455 highest score. The number of motion vectors coded using the SPLITMV 4456 mode is scored using the same weighting and is returned with the 4457 scores of the best, nearest, and near vectors. 4459 The three adjacent macroblocks above, left, and above-left are 4460 considered in order. If the macroblock is intra-coded, no action is 4461 taken. Otherwise, the motion vector is compared to other previously 4462 found motion vectors to determine if it has been seen before, and if 4463 so contributes its weight to that vector, otherwise enters a new 4464 vector in the list. The above and left vectors have twice the weight 4465 of the above-left vector. 4467 As is the case with many contexts used by VP8, it is possible for 4468 macroblocks near the top or left edges of the image to reference 4469 blocks that are outside the visible image. VP8 provides a border of 4470 1 macroblock filled with 0x0 motion vectors left of the left edge, 4471 and a border filled with 0,0 motion vectors of 1 macroblocks above 4472 the top edge. 4474 Much of the process is more easily described in C than in English. 4475 The reference code for this can be found in findnearmv.c. The 4476 calculation of reference vectors, probability table, and, finally, 4477 the inter-prediction mode itself is implemented as follows. 4479 ---- Begin code block -------------------------------------- 4481 typedef union 4482 { 4483 unsigned int as_int; 4484 MV as_mv; 4485 } int_mv; /* facilitates rapid equality tests */ 4487 static void mv_bias(MODE_INFO *x,int refframe, int_mv *mvp, 4488 int * ref_frame_sign_bias ) 4489 { 4490 MV xmv; 4491 xmv = x->mbmi.mv.as_mv; 4492 if ( ref_frame_sign_bias[x->mbmi.ref_frame] != 4493 ref_frame_sign_bias[refframe] ) 4494 { 4495 xmv.row*=-1; 4496 xmv.col*=-1; 4497 } 4498 mvp->as_mv = xmv; 4499 } 4501 ---- End code block ---------------------------------------- 4502 ---- Begin code block -------------------------------------- 4504 void vp8_clamp_mv(MV *mv, const MACROBLOCKD *xd) 4505 { 4506 if ( mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN) ) 4507 mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN; 4508 else if ( mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN ) 4509 mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN; 4511 if ( mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN) ) 4512 mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN; 4513 else if ( mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN ) 4514 mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN; 4515 } 4517 ---- End code block ---------------------------------------- 4519 In the function vp8_find_near_mvs(), the vectors "nearest" and "near" 4520 are used by the corresponding modes. 4522 The vector best_mv is used as a base for explicitly-coded motion 4523 vectors. 4525 The first three entries in the return value cnt are (in order) 4526 weighted census values for "zero", "nearest", and "near" vectors. 4527 The final value indicates the extent to which SPLIT_MV was used by 4528 the neighboring macroblocks. The largest possible "weight" value in 4529 each case is 5. 4531 ---- Begin code block -------------------------------------- 4533 void vp8_find_near_mvs 4534 ( 4535 MACROBLOCKD *xd, 4536 const MODE_INFO *here, 4537 MV *nearest, 4538 MV *near, 4539 MV *best_mv, 4540 int cnt[4], 4541 int refframe, 4542 int * ref_frame_sign_bias 4543 ) 4544 { 4545 const MODE_INFO *above = here - xd->mode_info_stride; 4546 const MODE_INFO *left = here - 1; 4547 const MODE_INFO *aboveleft = above - 1; 4548 int_mv near_mvs[4]; 4549 int_mv *mv = near_mvs; 4550 int *cntx = cnt; 4551 enum {CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV}; 4553 /* Zero accumulators */ 4554 mv[0].as_int = mv[1].as_int = mv[2].as_int = 0; 4555 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 4557 /* Process above */ 4558 if(above->mbmi.ref_frame != INTRA_FRAME) { 4559 if(above->mbmi.mv.as_int) { 4560 (++mv)->as_int = above->mbmi.mv.as_int; 4561 mv_bias(above, refframe, mv, ref_frame_sign_bias); 4562 ++cntx; 4563 } 4564 *cntx += 2; 4565 } 4567 /* Process left */ 4568 if(left->mbmi.ref_frame != INTRA_FRAME) { 4569 if(left->mbmi.mv.as_int) { 4570 int_mv this_mv; 4572 this_mv.as_int = left->mbmi.mv.as_int; 4573 mv_bias(left, refframe, &this_mv, ref_frame_sign_bias); 4575 if(this_mv.as_int != mv->as_int) { 4576 (++mv)->as_int = this_mv.as_int; 4577 ++cntx; 4578 } 4579 *cntx += 2; 4580 } else 4581 cnt[CNT_ZERO] += 2; 4582 } 4584 /* Process above left */ 4585 if(aboveleft->mbmi.ref_frame != INTRA_FRAME) { 4586 if(aboveleft->mbmi.mv.as_int) { 4587 int_mv this_mv; 4589 this_mv.as_int = aboveleft->mbmi.mv.as_int; 4590 mv_bias(aboveleft, refframe, &this_mv, 4591 ref_frame_sign_bias); 4593 if(this_mv.as_int != mv->as_int) { 4594 (++mv)->as_int = this_mv.as_int; 4595 ++cntx; 4596 } 4597 *cntx += 1; 4599 } else 4600 cnt[CNT_ZERO] += 1; 4601 } 4603 /* If we have three distinct MV's ... */ 4604 if(cnt[CNT_SPLITMV]) { 4605 /* See if above-left MV can be merged with NEAREST */ 4606 if(mv->as_int == near_mvs[CNT_NEAREST].as_int) 4607 cnt[CNT_NEAREST] += 1; 4608 } 4610 cnt[CNT_SPLITMV] = ((above->mbmi.mode == SPLITMV) 4611 + (left->mbmi.mode == SPLITMV)) * 2 4612 + (aboveleft->mbmi.mode == SPLITMV); 4614 /* Swap near and nearest if necessary */ 4615 if(cnt[CNT_NEAR] > cnt[CNT_NEAREST]) { 4616 int tmp; 4617 tmp = cnt[CNT_NEAREST]; 4618 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 4619 cnt[CNT_NEAR] = tmp; 4620 tmp = near_mvs[CNT_NEAREST].as_int; 4621 near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int; 4622 near_mvs[CNT_NEAR].as_int = tmp; 4623 } 4625 /* Use near_mvs[0] to store the "best" MV */ 4626 if(cnt[CNT_NEAREST] >= cnt[CNT_ZERO]) 4627 near_mvs[CNT_ZERO] = near_mvs[CNT_NEAREST]; 4629 /* Set up return values */ 4630 *best_mv = near_mvs[0].as_mv; 4631 *nearest = near_mvs[CNT_NEAREST].as_mv; 4632 *near = near_mvs[CNT_NEAR].as_mv; 4634 vp8_clamp_mv(nearest, xd); 4635 vp8_clamp_mv(near, xd); 4636 vp8_clamp_mv(best_mv, xd); //TODO: move this up before 4637 the copy 4638 } 4640 ---- End code block ---------------------------------------- 4642 The mv_ref probability table (mv_ref_p) is then derived from the 4643 census as follows. 4645 ---- Begin code block -------------------------------------- 4647 const int vp8_mode_contexts[6][4] = 4648 { 4649 { 7, 1, 1, 143, }, 4650 { 14, 18, 14, 107, }, 4651 { 135, 64, 57, 68, }, 4652 { 60, 56, 128, 65, }, 4653 { 159, 134, 128, 34, }, 4654 { 234, 188, 128, 28, }, 4655 } 4657 ---- End code block ---------------------------------------- 4659 ---- Begin code block -------------------------------------- 4661 vp8_prob *vp8_mv_ref_probs(vp8_prob mv_ref_p[VP8_MVREFS-1], 4662 int cnt[4]) 4663 { 4664 mv_ref_p[0] = vp8_mode_contexts [cnt[0]] [0]; 4665 mv_ref_p[1] = vp8_mode_contexts [cnt[1]] [1]; 4666 mv_ref_p[2] = vp8_mode_contexts [cnt[2]] [2]; 4667 mv_ref_p[3] = vp8_mode_contexts [cnt[3]] [3]; 4668 return p; 4669 } 4671 ---- End code block ---------------------------------------- 4673 Once mv_ref_p is established, the mv_ref is decoded as usual. 4675 ---- Begin code block -------------------------------------- 4677 mvr = (mv_ref) treed_read( d, mv_ref_tree, mv_ref_p); 4679 ---- End code block ---------------------------------------- 4681 For the first four inter-coding modes, the same motion vector is used 4682 for all the Y subblocks. The first three modes use an implicit 4683 motion vector. 4685 +------------+------------------------------------------------------+ 4686 | Mode | Instruction | 4687 +------------+------------------------------------------------------+ 4688 | mv_nearest | Use the nearest vector returned by | 4689 | | vp8_find_near_mvs. | 4690 | | | 4691 | mv_near | Use the near vector returned by vp8_find_near_mvs. | 4692 | | | 4693 | mv_zero | Use a zero vector, that is, predict the current | 4694 | | macroblock from the corresponding macroblock in the | 4695 | | prediction frame. | 4696 | | | 4697 | NEWMV | This mode is followed by an explicitly-coded motion | 4698 | | vector (the format of which is described in the next | 4699 | | chapter) that is added (component-wise) to the | 4700 | | best_mv reference vector returned by find_near_mvs | 4701 | | and applied to all 16 subblocks. | 4702 +------------+------------------------------------------------------+ 4704 16.4. Split Prediction 4706 The remaining mode (SPLITMV) causes multiple vectors to be applied to 4707 the Y subblocks. It is immediately followed by a partition 4708 specification that determines how many vectors will be specified and 4709 how they will be assigned to the subblocks. The possible partitions, 4710 with indicated subdivisions and coding tree, are as follows. 4712 ---- Begin code block -------------------------------------- 4714 typedef enum 4715 { 4716 mv_top_bottom, /* two pieces {0...7} and {8...15} */ 4717 mv_left_right, /* {0,1,4,5,8,9,12,13} and 4718 {2,3,6,7,10,11,14,15} */ 4719 mv_quarters, /* {0,1,4,5}, {2,3,6,7}, {8,9,12,13}, 4720 {10,11,14,15} */ 4721 MV_16, /* every subblock gets its own vector 4722 {0} ... {15} */ 4724 mv_num_partitions 4725 } 4726 MVpartition; 4728 const tree_index mvpartition_tree [2 * (mvnum_partition - 1)] = 4729 { 4730 -MV_16, 2, /* MV_16 = "0" */ 4731 -mv_quarters, 4, /* mv_quarters = "10" */ 4732 -mv_top_bottom, -mv_left_right /* top_bottom = "110", 4733 left_right = "111" */ 4734 }; 4736 ---- End code block ---------------------------------------- 4738 The partition is decoded using a fixed, constant probability table: 4740 ---- Begin code block -------------------------------------- 4742 const Prob mvpartition_probs [mvnum_partition - 1] = 4743 { 110, 111, 150}; 4744 part = (MVpartition) treed_read( d, mvpartition_tree, 4745 mvpartition_probs); 4747 ---- End code block ---------------------------------------- 4749 After the partition come two (for mv_top_bottom or mv_left_right), 4750 four (for mv_quarters), or sixteen (for MV_16) subblock inter- 4751 prediction modes. These modes occur in the order indicated by the 4752 partition layouts (given as comments to the MVpartition enum) and are 4753 coded as follows. (As was done for the macroblock-level modes, we 4754 offset the mode enumeration so that a single variable may 4755 unambiguously hold either an intra- or inter-subblock mode.) 4757 Prior to decoding each subblock, a decoding tree context is chosen as 4758 illustrated in the code snippet below. The context is based on the 4759 immediate left and above subblock neighbors, and whether they are 4760 equal, are zero, or a combination of those. 4762 ---- Begin code block -------------------------------------- 4764 typedef enum 4765 { 4766 LEFT4x4 = num_intra_bmodes, /* use already-coded MV to 4767 my left */ 4768 ABOVE4x4, /* use already-coded MV above me */ 4769 ZERO4x4, /* use zero MV */ 4770 NEW4x4, /* explicit offset from "best" */ 4772 num_sub_mv_ref 4773 }; 4774 sub_mv_ref; 4776 const tree_index sub_mv_ref_tree [2 * (num_sub_mv_ref - 1)] = 4777 { 4778 -LEFT4X4, 2, /* LEFT = "0" */ 4779 -ABOVE4X4, 4, /* ABOVE = "10" */ 4780 -ZERO4X4, -NEW4X4 /* ZERO = "110", NEW = "111" */ 4781 }; 4783 /* Choose correct decoding tree context 4784 * Function parameters are left subblock neighbor MV and above 4785 * subblock neighbor MV */ 4786 int vp8_mvCont(MV *l, MV*a) 4787 { 4788 int lez = (l->row == 0 && l->col == 0); /* left neighbour 4789 is zero */ 4790 int aez = (a->row == 0 && a->col == 0); /* above neighbour 4791 is zero */ 4792 int lea = (l->row == a->row && l->col == a->col); /* left 4793 neighbour equals above neighbour */ 4795 if(lea && lez) 4796 return SUBMVREF_LEFT_ABOVE_ZED; /* =4 */ 4798 if(lea) 4799 return SUBMVREF_LEFT_ABOVE_SAME; /* =3 */ 4801 if(aez) 4802 return SUBMVREF_ABOVE_ZED; /* =2 */ 4804 if(lez) 4805 return SUBMVREF_LEFT_ZED; /* =1*/ 4807 return SUBMVREF_NORMAL; /* =0 */ 4809 } 4811 /* Constant probabilities and decoding procedure. */ 4813 const Prob sub_mv_ref_prob [5][num_sub_mv_ref - 1] = { 4814 { 147,136,18 }, 4815 { 106,145,1 }, 4816 { 179,121,1 }, 4817 { 223,1 ,34 }, 4818 { 208,1 ,1 } }; 4820 sub_ref = (sub_mv_ref) treed_read( d, sub_mv_ref_tree, 4821 sub_mv_ref_prob[context]); 4823 ---- End code block ---------------------------------------- 4825 The first two sub-prediction modes simply copy the already-coded 4826 motion vectors used by the blocks above and to-the-left of the 4827 subblock at the upper left corner of the current subset (i.e., 4828 collection of subblocks being predicted). These prediction blocks 4829 need not lie in the current macroblock and, if the current subset 4830 lies at the top or left edges of the frame, need not lie in the 4831 frame. In this latter case, their motion vectors are taken to be 4832 zero, as are subblock motion vectors within an intra-predicted 4833 macroblock. Also, to ensure the correctness of prediction within 4834 this macroblock, all subblocks lying in an already-decoded subset of 4835 the current macroblock must have their motion vectors set. 4837 ZERO4x4 uses a zero motion vector and predicts the current subset 4838 using the corresponding subset from the prediction frame. 4840 NEW4x4 is exactly like NEWMV except applied only to the current 4841 subset. It is followed by a 2-dimensional motion vector offset 4842 (described in the next chapter) that is added to the best vector 4843 returned by the earlier call to find_near_mvs to form the motion 4844 vector in effect for the subset. 4846 Parsing of both inter-prediction modes and motion vectors (described 4847 next) can be found in the reference decoder file decodemv.c. 4849 17. Motion Vector Decoding 4851 As discussed above, motion vectors appear in two places in the VP8 4852 datastream: applied to whole macroblocks in NEWMV mode and applied to 4853 subsets of macroblocks in NEW4x4 mode. The format of the vectors is 4854 identical in both cases. 4856 Each vector has two pieces: A vertical component (row) followed by a 4857 horizontal component (column). The row and column use separate 4858 coding probabilities but are otherwise represented identically. 4860 17.1. Coding of Each Component 4862 Each component is a signed integer V representing a vertical or 4863 horizontal luma displacement of V quarter-pixels (and a chroma 4864 displacement of V eighth-pixels). The absolute value of V, if non- 4865 zero, is followed by a boolean sign. V may take any value between 4866 -1023 and +1023, inclusive. 4868 The absolute value A is coded in one of two different ways according 4869 to its size. For 0 <= A <= 7, A is tree-coded, and for 8 <= A <= 4870 1023, the bits in the binary expansion of A are coded using 4871 independent boolean probabilities. The coding of A begins with a 4872 bool specifying which range is in effect. 4874 Decoding a motion vector component then requires a 19-position 4875 probability table, whose offsets, along with the procedure used to 4876 decode components, are as follows: 4878 ---- Begin code block -------------------------------------- 4880 typedef enum 4881 { 4882 mvpis_short, /* short (<= 7) vs long (>= 8) */ 4883 MVPsign, /* sign for non-zero */ 4884 MVPshort, /* 8 short values = 7-position tree */ 4886 MVPbits = MVPshort + 7, /* 8 long value bits 4887 w/independent probs */ 4889 MVPcount = MVPbits + 10 /* 19 probabilities in total */ 4890 } 4891 MVPindices; 4893 typedef Prob MV_CONTEXT [MVPcount]; /* Decoding spec for 4894 a single component */ 4896 /* Tree used for small absolute values (has expected 4897 correspondence). */ 4899 const tree_index small_mvtree [2 * (8 - 1)] = 4900 { 4901 2, 8, /* "0" subtree, "1" subtree */ 4902 4, 6, /* "00" subtree", "01" subtree */ 4903 -0, -1, /* 0 = "000", 1 = "001" */ 4904 -2, -3, /* 2 = "010", 3 = "011" */ 4905 10, 12, /* "10" subtree, "11" subtree */ 4906 -4, -5, /* 4 = "100", 5 = "101" */ 4907 -6, -7 /* 6 = "110", 7 = "111" */ 4908 }; 4910 /* Read MV component at current decoder position, using 4911 supplied probs. */ 4913 int read_mvcomponent( bool_decoder *d, const MV_CONTEXT *mvc) 4914 { 4915 const Prob * const p = (const Prob *) mvc; 4916 int A = 0; 4918 if( read_bool( d, p [mvpis_short])) /* 8 <= A <= 1023 */ 4919 { 4920 /* Read bits 0, 1, 2 */ 4922 int i = 0; 4923 do { A += read_bool( d, p [MVPbits + i]) << i;} 4924 while( ++i < 3); 4926 /* Read bits 9, 8, 7, 6, 5, 4 */ 4928 i = 9; 4929 do { A += read_bool( d, p [MVPbits + i]) << i;} 4930 while( --i > 3); 4932 /* We know that A >= 8 because it is coded long, 4933 so if A <= 15, bit 3 is one and is not 4934 explicitly coded. */ 4936 if( !(A & 0xfff0) || read_bool( d, p [MVPbits + 3])) 4937 A += 8; 4938 } 4939 else /* 0 <= A <= 7 */ 4940 A = treed_read( d, small_mvtree, p + MVPshort); 4942 return A && read_bool( r, p [MVPsign]) ? -A : A; 4943 } 4944 ---- End code block ---------------------------------------- 4946 17.2. Probability Updates 4948 The decoder should maintain an array of two MV_CONTEXTs for decoding 4949 row and column components, respectively. These MV_CONTEXTs should be 4950 set to their defaults every key frame. Each individual probability 4951 may be updated every interframe (by field J of the frame header) 4952 using a constant table of update probabilities. Each optional update 4953 is of the form B? P(7), that is, a bool followed by a 7-bit 4954 probability specification if true. 4956 As with other dynamic probabilities used by VP8, the updates remain 4957 in effect until the next key frame or until replaced via another 4958 update. 4960 In detail, the probabilities should then be managed as follows. 4962 ---- Begin code block -------------------------------------- 4964 /* Never-changing table of update probabilities for each 4965 individual probability used in decoding motion vectors. */ 4967 const MV_CONTEXT vp8_mv_update_probs[2] = 4968 { 4969 { 4970 237, 4971 246, 4972 253, 253, 254, 254, 254, 254, 254, 4973 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 4974 }, 4975 { 4976 231, 4977 243, 4978 245, 253, 254, 254, 254, 254, 254, 4979 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 4980 } 4981 }; 4983 /* Default MV decoding probabilities. */ 4985 const MV_CONTEXT default_mv_context[2] = 4986 { 4987 { // row 4988 162, // is short 4989 128, // sign 4990 225, 146, 172, 147, 214, 39, 156, // short tree 4991 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 // long bits 4993 }, 4995 { // same for column 4996 164, // is short 4997 128, 4998 204, 170, 119, 235, 140, 230, 228, 4999 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 // long bits 5001 } 5002 }; 5004 /* Current MV decoding probabilities, set to above defaults 5005 every key frame. */ 5007 MV_CONTEXT mvc [2]; /* always row, then column */ 5009 /* Procedure for decoding a complete motion vector. */ 5011 typedef struct { int16 row, col;} MV; /* as in previous chapter */ 5013 MV read_mv( bool_decoder *d) 5014 { 5015 MV v; 5016 v.row = (int16) read_mvcomponent( d, mvc); 5017 v.col = (int16) read_mvcomponent( d, mvc + 1); 5018 return v; 5019 } 5021 /* Procedure for updating MV decoding probabilities, called 5022 every interframe with "d" at the appropriate position in 5023 the frame header. */ 5025 void update_mvcontexts( bool_decoder *d) 5026 { 5027 int i = 0; 5028 do { /* component = row, then column */ 5029 const Prob *up = mv_update_probs[i]; /* update probs 5030 for component */ 5031 Prob *p = mvc[i]; /* start decode tbl "" */ 5032 Prob * const pstop = p + MVPcount; /* end decode tbl "" */ 5033 do { 5034 if( read_bool( d, *up++)) /* update this position */ 5035 { 5036 const Prob x = read_literal( d, 7); 5038 *p = x? x<<1 : 1; 5039 } 5040 } while( ++p < pstop); /* next position */ 5042 } while( ++i < 2); /* next component */ 5043 } 5045 ---- End code block ---------------------------------------- 5047 This completes the description of the motion-vector decoding 5048 procedure and, with it, the procedure for decoding interframe 5049 macroblock prediction records. 5051 18. Interframe Prediction 5053 Given an inter-prediction specification for the current macroblock, 5054 that is, a reference frame together with a motion vector for each of 5055 the sixteen Y subblocks, we describe the calculation of the 5056 prediction buffer for the macroblock. Frame reconstruction is then 5057 completed via the previously-described processes of residue summation 5058 (Section 14) and loop filtering (Section 15). 5060 The management of inter-predicted subblocks may be found in the 5061 reference decoder file reconinter.c; sub-pixel interpolation is 5062 implemented in filter_c.c. 5064 18.1. Bounds on and Adjustment of Motion Vectors 5066 Since each motion vector is differentially encoded from a neighboring 5067 block or macroblock and the only clamp is to ensure that the 5068 referenced motion vector represents a valid location inside a 5069 reference frame buffer, it is technically possible within the VP8 5070 format for a block or macroblock to have arbitrarily large motion 5071 vectors, up to the size of the input image plus the extended border 5072 areas. For practical reasons, VP8 imposes a motion vector size range 5073 limit of [-4096, 4095] full pixels, regardless of image size (VP8 5074 defines 14 raw bits for width and height; 16383x16383 is the maximum 5075 possible image size). Bitstream-compliant encoders and decoders 5076 shall enforce this limit. 5078 Because the motion vectors applied to the chroma subblocks have 1/8 5079 pixel resolution, the synthetic pixel calculation, outlined in 5080 Chapter 5 and detailed below, uses this resolution for the luma 5081 subblocks as well. In accordance, the stored luma motion vectors are 5082 all doubled, each component of each luma vector becoming an even 5083 integer in the range -2046 to +2046, inclusive. 5085 The vector applied to each chroma subblock is calculated by averaging 5086 the vectors for the 4 luma subblocks occupying the same visible area 5087 as the chroma subblock in the usual correspondence, that is, the 5088 vector for U and V block 0 is the average of the vectors for the Y 5089 subblocks { 0, 1, 4, 5}, chroma block 1 corresponds to Y blocks { 2, 5090 3, 6, 7}, chroma block 2 to Y blocks { 8, 9, 12, 13}, and chroma 5091 block 3 to Y blocks { 10, 11, 14, 15}. 5093 In detail, each of the two components of the vectors for each of the 5094 chroma subblocks is calculated from the corresponding luma vector 5095 components as follows: 5097 ---- Begin code block -------------------------------------- 5099 int avg( int c1, int c2, int c3, int c4) 5100 { 5101 int s = c1 + c2 + c3 + c4; 5103 /* The shift divides by 8 (not 4) because chroma pixels 5104 have half the diameter of luma pixels. The handling 5105 of negative motion vector components is slightly 5106 cumbersome because, strictly speaking, right shifts 5107 of negative numbers are not well-defined in C. */ 5109 return s >= 0 ? (s + 4) >> 3 : -( (-s + 4) >> 3); 5110 } 5112 ---- End code block ---------------------------------------- 5114 Furthermore, if the version number in the frame tag specifies only 5115 full-pel chroma motion vectors, then the fractional parts of both 5116 components of the vector are truncated to zero, as illustrated in the 5117 following pseudo-code (assuming 3 bits of fraction for both luma and 5118 chroma vectors): 5120 ---- Begin code block -------------------------------------- 5122 x = x & (~7); 5123 y = y & (~7); 5125 ---- End code block ---------------------------------------- 5127 Earlier in this document we described the vp8_clamp_mv() function to 5128 limit "nearest" and "near" motion vector predictors inside specified 5129 margins within the frame boundaries. Additional clamping is 5130 performed for NEW_MV macroblocks, for which the final motion vector 5131 is clamped again after combining the "best" predictor and the 5132 differential vector decoded from the stream. 5134 However, the secondary clamping is not performed for SPLIT_MV 5135 macroblocks, meaning that any subblock's motion vector within the 5136 SPLIT_MV macroblock may point outside the clamping zone. These non- 5137 clamped vectors are also used when determining the decoding tree 5138 context for subsequent subblocks' modes in the vp8_mvCont() function. 5140 18.2. Prediction Subblocks 5142 The prediction calculation for each subblock is then as follows. 5143 Temporarily disregarding the fractional part of the motion vector 5144 (that is, rounding "up" or "left" by right-shifting each component 3 5145 bits with sign propagation) and adding the origin (upper left 5146 position) of the (16x16 luma or 8x8 chroma) current macroblock gives 5147 us an origin in the Y, U, or V plane of the predictor frame (either 5148 the golden frame or previous frame). 5150 Considering that origin to be the upper left corner of a (luma or 5151 chroma) macroblock, we need to specify the relative positions of the 5152 pixels associated to that subblock, that is, any pixels that might be 5153 involved in the sub-pixel interpolation processes for the subblock. 5155 18.3. Sub-pixel Interpolation 5157 The sub-pixel interpolation is effected via two one-dimensional 5158 convolutions. These convolutions may be thought of as operating on a 5159 two-dimensional array of pixels whose origin is the subblock origin, 5160 that is the origin of the prediction macroblock described above plus 5161 the offset to the subblock. Because motion vectors are arbitrary, so 5162 are these "prediction subblock origins". 5164 The integer part of the motion vector is subsumed in the origin of 5165 the prediction subblock, the 16 (synthetic) pixels we need to 5166 construct are given by 16 offsets from the origin. The integer part 5167 of each of these offsets is the offset of the corresponding pixel 5168 from the subblock origin (using the vertical stride). To these 5169 integer parts is added a constant fractional part, which is simply 5170 the difference between the actual motion vector and its integer 5171 truncation used to calculate the origins of the prediction macroblock 5172 and subblock. Each component of this fractional part is an integer 5173 between 0 and 7, representing a forward displacement in eighths of a 5174 pixel. 5176 It is these fractional displacements that determine the filtering 5177 process. If they both happen to be zero (that is, we had a "whole 5178 pixel" motion vector), the prediction subblock is simply copied into 5179 the corresponding piece of the current macroblock's prediction 5180 buffer. As discussed in Chapter 14, the layout of the macroblock's 5181 prediction buffer can depend on the specifics of the reconstruction 5182 implementation chosen. Of course, the vertical displacement between 5183 lines of the prediction subblock is given by the stride, as are all 5184 vertical displacements used here. 5186 Otherwise, at least one of the fractional displacements is non-zero. 5187 We then synthesize the missing pixels via a horizontal, followed by a 5188 vertical, one-dimensional interpolation. 5190 The two interpolations are essentially identical. Each uses an (at 5191 most) six-tap filter (the choice of which of course depends on the 5192 one-dimensional offset). Thus, every calculated pixel references at 5193 most three pixels before (above or to-the-left of) it and at most 5194 three pixels after (below or to-the-right of) it. The horizontal 5195 interpolation must calculate two extra rows above and three extra 5196 rows below the 4x4 block, to provide enough samples for the vertical 5197 interpolation to proceed. 5199 Depending on the reconstruction filter type given in the field 5200 Version Number in the frame tag, either a bicubic or a bilinear tap 5201 set is used. 5203 The exact implementation of subsampling is as follows. 5205 ---- Begin code block -------------------------------------- 5207 /* Filter taps taken to 7-bit precision. 5208 Because DC is always passed, taps always sum to 128. */ 5210 const int BilinearFilters[8][6] = 5211 { 5212 { 0, 0, 128, 0, 0, 0 }, 5213 { 0, 0, 112, 16, 0, 0 }, 5214 { 0, 0, 96, 32, 0, 0 }, 5215 { 0, 0, 80, 48, 0, 0 }, 5216 { 0, 0, 64, 64, 0, 0 }, 5217 { 0, 0, 48, 80, 0, 0 }, 5218 { 0, 0, 32, 96, 0, 0 }, 5219 { 0, 0, 16, 112, 0, 0 } 5220 }; 5222 const int filters [8] [6] = { /* indexed by displacement */ 5223 { 0, 0, 128, 0, 0, 0 }, /* degenerate whole-pixel */ 5224 { 0, -6, 123, 12, -1, 0 }, /* 1/8 */ 5225 { 2, -11, 108, 36, -8, 1 }, /* 1/4 */ 5226 { 0, -9, 93, 50, -6, 0 }, /* 3/8 */ 5227 { 3, -16, 77, 77, -16, 3 }, /* 1/2 is symmetric */ 5228 { 0, -6, 50, 93, -9, 0 }, /* 5/8 = reverse of 3/8 */ 5229 { 1, -8, 36, 108, -11, 2 }, /* 3/4 = reverse of 1/4 */ 5230 { 0, -1, 12, 123, -6, 0 } /* 7/8 = reverse of 1/8 */ 5231 }; 5233 /* One-dimensional synthesis of a single sample. 5234 Filter is determined by fractional displacement */ 5236 Pixel interp( 5237 const int fil[6], /* filter to apply */ 5238 const Pixel *p, /* origin (rounded "before") in 5239 prediction area */ 5241 const int s /* size of one forward step "" */ 5242 ) { 5243 int32 a = 0; 5244 int i = 0; 5245 p -= s + s; /* move back two positions */ 5247 do { 5248 a += *p * fil[i]; 5249 p += s; 5250 } while( ++i < 6); 5252 return clamp255( (a + 64) >> 7); /* round to nearest 5253 8-bit value */ 5254 } 5256 /* First do horizontal interpolation, producing intermediate 5257 buffer. */ 5259 void Hinterp( 5260 Pixel temp[9][4], /* 9 rows of 4 (intermediate) 5261 destination values */ 5262 const Pixel *p, /* subblock origin in prediction 5263 frame */ 5264 int s, /* vertical stride to be used in 5265 prediction frame */ 5266 uint hfrac, /* 0 <= horizontal displacement <= 7 */ 5267 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5268 ) { 5269 const int * const fil = bicubic ? filters [hfrac] : 5270 BilinearFilters[hfrac]; 5272 int r = 0; do /* for each row */ 5273 { 5274 int c = 0; do /* for each destination sample */ 5275 { 5276 /* Pixel separation = one horizontal step = 1 */ 5278 temp[r][c] = interp( fil, p + c, 1); 5279 } 5280 while( ++c < 4); 5281 } 5282 while( p += s, ++r < 9); /* advance p to next row */ 5283 } 5285 /* Finish with vertical interpolation, producing final results. 5286 Input array "temp" is of course that computed above. */ 5288 void Vinterp( 5289 Pixel final[4][4], /* 4 rows of 4 (final) destination values */ 5290 const Pixel temp[9][4], 5291 uint vfrac, /* 0 <= vertical displacement <= 7 */ 5292 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5293 ) { 5294 const int * const fil = bicubic ? filters [vfrac] : 5295 BilinearFilters[vfrac]; 5297 int r = 0; do /* for each row */ 5298 { 5299 int c = 0; do /* for each destination sample */ 5300 { 5301 /* Pixel separation = one vertical step = width 5302 of array = 4 */ 5304 final[r][c] = interp( fil, temp[r] + c, 4); 5305 } 5306 while( ++c < 4); 5307 } 5308 while( ++r < 4); 5309 } 5311 ---- End code block ---------------------------------------- 5313 18.4. Filter Properties 5315 We discuss briefly the rationale behind the choice of filters. Our 5316 approach is necessarily cursory; a genuinely accurate discussion 5317 would require a couple of books. Readers unfamiliar with signal 5318 processing may or may not wish to skip this. 5320 All digital signals are of course sampled in some fashion. The case 5321 where the inter-sample spacing (say in time for audio samples, or 5322 space for pixels) is uniform, that is, the same at all positions, is 5323 particularly common and amenable to analysis. Many aspects of the 5324 treatment of such signals are best-understood in the frequency domain 5325 via Fourier Analysis, particularly those aspects of the signal that 5326 are not changed by shifts in position, especially when those 5327 positional shifts are not given by a whole number of samples. 5329 Non-integral translates of a sampled signal are a textbook example of 5330 the foregoing. In our case of non-integral motion vectors, we wish 5331 to say what the underlying image "really is" at these pixels we don't 5332 have values for but feel that it makes sense to talk about. The 5333 correctness of this feeling is predicated on the underlying signal 5334 being band-limited, that is, not containing any energy in spatial 5335 frequencies that cannot be faithfully rendered at the pixel 5336 resolution at our disposal. In one dimension, this range of "OK" 5337 frequencies is called the Nyquist band; in our two-dimensional case 5338 of integer-grid samples, this range might be termed a Nyquist 5339 rectangle. The finer the grid, the more we know about the image, and 5340 the wider the Nyquist rectangle. 5342 It turns out that, for such band-limited signals, there is indeed an 5343 exact mathematical formula to produce the correct sample value at an 5344 arbitrary point. Unfortunately, this calculation requires the 5345 consideration of every single sample in the image, as well as needing 5346 to operate at infinite precision. Also, strictly speaking, all band- 5347 limited signals have infinite spatial (or temporal) extent, so 5348 everything we are discussing is really some sort of approximation. 5350 It is true that the theoretically correct subsampling procedure, as 5351 well as any approximation thereof, is always given by a translation- 5352 invariant weighted sum (or filter) similar to that used by VP8. It 5353 is also true that the reconstruction error made by such a filter can 5354 be simply represented as a multiplier in the frequency domain, that 5355 is, such filters simply multiply the Fourier transform of any signal 5356 to which they are applied by a fixed function associated to the 5357 filter. This fixed function is usually called the frequency response 5358 (or transfer function); the ideal subsampling filter has a frequency 5359 response equal to one in the Nyquist rectangle and zero everywhere 5360 else. 5362 Another basic fact about approximations to "truly correct" 5363 subsampling is that, the wider the subrectangle (within the Nyquist 5364 rectangle) of spatial frequencies one wishes to "pass" (that is, 5365 correctly render) or, put more accurately, the closer one wishes to 5366 approximate the ideal transfer function, the more samples of the 5367 original signal must be considered by the subsampling, and the wider 5368 the calculation precision necessitated. 5370 The filters chosen by VP8 were chosen, within the constraints of 4 or 5371 6 taps and 7-bit precision, to do the best possible job of handling 5372 the low spatial frequencies near the zeroth DC frequency along with 5373 introducing no resonances (places where the absolute value of the 5374 frequency response exceeds one). 5376 The justification for the foregoing has two parts. First, resonances 5377 can produce extremely objectionable visible artifacts when, as often 5378 happens in actual compressed video streams, filters are applied 5379 repeatedly. Second, the vast majority of energy in real-world images 5380 lies near DC and not at the high-end. 5382 To get slightly more specific, the filters chosen by VP8 are the best 5383 resonance-free 4- or 6-tap filters possible, where "best" describes 5384 the frequency response near the origin: the response at 0 is required 5385 to be 1 and the graph of the response at 0 is as flat as possible. 5387 To provide an intuitively more obvious point of reference, the "best" 5388 2-tap filter is given by simple linear interpolation between the 5389 surrounding actual pixels. 5391 Finally, it should be noted that, because of the way motion vectors 5392 are calculated, the (shorter) 4-tap filters (used for odd fractional 5393 displacements) are applied in the chroma plane only. Human color 5394 perception is notoriously poor, especially where higher spatial 5395 frequencies are involved. The shorter filters are easier to 5396 understand mathematically, and the difference between them and a 5397 theoretically slightly better 6-tap filter is negligible where chroma 5398 is concerned. 5400 19. Annex A: Bitstream Syntax 5402 This annex presents the bitstream syntax in a tabular form. All the 5403 information elements have been introduced and explained in the 5404 previous chapters but are collected here for a quick reference. Each 5405 syntax element is shortly described after the tabular representation 5406 along with a reference to the corresponding paragraph in the main 5407 document. The meaning of each syntax element value is not repeated 5408 here. 5410 The top-level hierarchy of the bitstream is introduced in Section 4. 5412 Definition of syntax element coding types can be found in Section 8. 5413 The types used in the representation in this annex are: 5415 o f(n), n-bit value from stream (n successive bits, not boolean 5416 encoded) 5418 o L(n), n-bit number encoded as n booleans (with equal probability 5419 of being 0 or 1) 5421 o B(p), bool with probability p of being 0 5423 o T, tree-encoded value 5425 19.1. Uncompressed Data Chunk 5427 +----------------------+-------+ 5428 | Frame Tag | Type | 5429 +----------------------+-------+ 5430 | frame_tag | f(24) | 5431 | | | 5432 | if (key_frame) { | | 5433 | | | 5434 | start_code | f(24) | 5435 | | | 5436 | horizontal_size_code | f(16) | 5437 | | | 5438 | vertical_size_code | f(16) | 5439 | | | 5440 | } | | 5441 +----------------------+-------+ 5443 The 3-byte frame tag can be parsed as follows: 5445 ---- Begin code block -------------------------------------- 5447 unsigned char *c = pbi->Source; 5448 unsigned int tmp; 5450 tmp = (c[2] << 16) | (c[1] << 8) | c[0]; 5452 key_frame = tmp & 0x1; 5453 version = (tmp >> 1) & 0x7; 5454 show_frame = (tmp >> 4) & 0x1; 5455 first_part_size = (tmp >> 5) & 0x7FFFF; 5457 ---- End code block ---------------------------------------- 5459 Where: 5461 o key_frame indicates if the current frame is a key frame or not 5463 o version determines the bitstream version 5465 o show_frame indicates if the current frame is meant to be displayed 5466 or not 5468 o first_part_size determines the size of the first partition 5469 (control partition) 5471 The start_code is a constant 3-byte pattern having value 0x9d012a. 5472 The latter part of the uncompressed chunk (after the start_code) can 5473 be parsed as follows: 5475 ---- Begin code block -------------------------------------- 5477 unsigned char *c = pbi->Source + 6; 5478 unsigned int tmp; 5480 tmp = (c[1] << 8) | c[0]; 5482 width = tmp & 0x3FFF; 5483 horizontal_scale = tmp >> 14; 5485 tmp = (c[3] << 8) | c[2]; 5487 height = tmp & 0x3FFF; 5488 vertical_scale = tmp >> 14; 5490 ---- End code block ---------------------------------------- 5492 19.2. Frame Header 5494 +-------------------------------+------+ 5495 | Frame Header | Type | 5496 +-------------------------------+------+ 5497 | if (key_frame) { | | 5498 | | | 5499 | color_space | L(1) | 5500 | | | 5501 | clamping_type | L(1) | 5502 | | | 5503 | } | | 5504 | | | 5505 | segmentation_enabled | L(1) | 5506 | | | 5507 | if (segmentation_enabled) { | | 5508 | | | 5509 | update_segmentation() | | 5510 | | | 5511 | } | | 5512 | | | 5513 | filter_type | L(1) | 5514 | | | 5515 | loop_filter_level | L(6) | 5516 | | | 5517 | sharpness_level | L(3) | 5518 | | | 5519 | mb_lf_adjustments() | | 5520 | | | 5521 | log2_nbr_of_dct_partitions | L(2) | 5522 | | | 5523 | quant_indices() | | 5524 | | | 5525 | if (key_frame) { | | 5526 | | | 5527 | refresh_entropy_probs | L(1) | 5528 | | | 5529 | } else { | | 5530 | | | 5531 | refresh_golden_frame | L(1) | 5532 | | | 5533 | refresh_alternate_frame | L(1) | 5534 | | | 5535 | if (!refresh_golden_frame) | | 5536 | | | 5537 | copy_buffer_to_golden | L(2) | 5538 | | | 5539 | if (!refresh_alternate_frame) | | 5540 | copy_buffer_to_alternate | L(2) | 5541 | | | 5542 | sign_bias_golden | L(1) | 5543 | | | 5544 | sign_bias_alternate | L(1) | 5545 | | | 5546 | refresh_entropy_probs | L(1) | 5547 | | | 5548 | refresh_last | L(1) | 5549 | | | 5550 | } | | 5551 | | | 5552 | token_prob_update() | | 5553 | | | 5554 | mb_no_coeff_skip | L(1) | 5555 +-------------------------------+------+ 5557 +--------------------------------------+------+ 5558 | Frame Header | Type | 5559 +--------------------------------------+------+ 5560 | prob_skip_false | L(8) | 5561 | | | 5562 | if (!key_frame) { | | 5563 | | | 5564 | prob_intra | L(8) | 5565 | | | 5566 | prob_last | L(8) | 5567 | | | 5568 | prob_golden | L(8) | 5569 | | | 5570 | intra_16x16_prob_update_flag | L(1) | 5571 | | | 5572 | if (intra_16x16_prob_update_flag) { | | 5573 | | | 5574 | for (i = 0; i < 4; i++) | | 5575 | | | 5576 | intra_16x16_prob | L(8) | 5577 | | | 5578 | } | | 5579 | | | 5580 | intra_chroma prob_update_flag | L(1) | 5581 | | | 5582 | if (intra_chroma_prob_update_flag) { | | 5583 | | | 5584 | for (i = 0; i < 3; i++) | | 5585 | | | 5586 | intra_chroma_prob | L(8) | 5587 | | | 5588 | } | | 5589 | | | 5590 | mv_prob_update() | | 5591 | | | 5592 | } | | 5593 +--------------------------------------+------+ 5595 o color_space defines the YUV color space of the sequence 5596 (Section 9.2) 5598 o clamping_type specifies if the decoder is required to clamp the 5599 reconstructed pixel values (Section 9.2) 5601 o segmentation_enabled enables the segmentation feature for the 5602 current frame (Section 9.3) 5604 o filter_type determines whether the normal or the simple loop 5605 filter is used (Section 9.4, Section 15) 5607 o loop_filter_level controls the deblocking filter (Section 9.4, 5608 Section 15) 5610 o sharpness_level controls the deblocking filter (Section 9.4, 5611 Section 15) 5613 o log2_nbr_of_dct_partitions determines the number of separate 5614 partitions containing the DCT coefficients of the macroblocks 5615 (Section 9.5) 5617 o refresh_entropy_probs determines whether updated token 5618 probabilities are used only for this frame or until further update 5620 o refresh_golden_frame determines if the current decoded frame 5621 refreshes the golden frame (Section 9.7) 5623 o refresh_alternate_frame determines if the current decoded frame 5624 refreshes the alternate reference frame (Section 9.7) 5626 o copy_buffer_to_golden determines if the golden reference is 5627 replaced by another reference (Section 9.7) 5629 o copy_buffer_to_alternate determines if the alternate reference is 5630 replaced by another reference (Section 9.7) 5632 o sign_bias_golden controls the sign of motion vectors when the 5633 golden frame is referenced (Section 9.7) 5635 o sign_bias_alternate controls the sign of motion vectors when the 5636 alternate frame is referenced (Section 9.7) 5638 o refresh_last determines if the current decoded frame refreshes the 5639 last frame reference buffer (Section 9.8) 5641 o mb_no_coeff_skip enables or disables the skipping of macroblocks 5642 containing no non-zero coefficients (Section 9.10) 5644 o prob_skip_false the probability that the macroblock is not skipped 5645 (flag indicating skipped macroblock is false) (Section 9.10) 5647 o prob_intra the probability of an intra macroblock (Section 9.10) 5649 o prob_last the probability that the last reference frame is used 5650 for inter prediction (Section 9.10) 5652 o prob_golden the probability that the golden reference frame is 5653 used for inter prediction (Section 9.10) 5655 o intra_16x16_prob_update_flag indicates if the branch probabilies 5656 used in the decoding of luma intra prediction mode are updated 5657 (Section 9.10) 5659 o intra_16x16_prob the branch probabilities of the luma intra 5660 prediction mode decoding tree 5662 o intra_chroma_prob_update_flag indicates if the branch probabilies 5663 used in the decoding of chroma intra prediction mode are updated 5664 (Section 9.10) 5666 o intra_chroma_prob the branch probabilities of the chroma intra 5667 prediction mode decoding tree 5669 +------------------------------------+------+ 5670 | update_segmentation() | Type | 5671 +------------------------------------+------+ 5672 | update_mb_segmentation_map | L(1) | 5673 | | | 5674 | update_segment_feature_data | L(1) | 5675 | | | 5676 | if (update_segment_feature_data) { | | 5677 | | | 5678 | segment_feature_mode | L(1) | 5679 | | | 5680 | for (i = 0; i < 4; i++) { | | 5681 | | | 5682 | quantizer_update | L(1) | 5683 | | | 5684 | if (quantizer_update) { | | 5685 | | | 5686 | quantizer_update_value | L(7) | 5687 | | | 5688 | quantizer_update_sign | L(1) | 5689 | | | 5690 | } | | 5691 | | | 5692 | } | | 5693 | | | 5694 | for (i = 0; i < 4; i++) { | | 5695 | | | 5696 | loop_filter_update | L(1) | 5697 | | | 5698 | if (loop_filter_update) { | | 5699 | | | 5700 | lf_update_value | L(6) | 5701 | | | 5702 | lf_update_sign | L(1) | 5703 | | | 5704 | } | | 5705 | | | 5706 | } | | 5707 | | | 5708 | } | | 5709 | | | 5710 | if (update_mb_segmentation_map) { | | 5711 | | | 5712 | for (i = 0; i < 3; i++) { | | 5713 | | | 5714 | segment_prob_update | L(1) | 5715 | | | 5716 | if (segment_prob_update) { | | 5717 | | | 5718 | segment_prob | L(8) | 5719 | | | 5720 | } | | 5721 | | | 5722 | } | | 5723 | | | 5724 | } | | 5725 +------------------------------------+------+ 5727 o update_mb_segmentation_map determines if the MB segmentation map 5728 is updated in the current frame (Section 9.3) 5730 o update_segment_feature_data indicates if the segment feature data 5731 is updated in the current frame (Section 9.3) 5733 o segment_feature_mode indicates the feature data update mode, 0 for 5734 delta and 1 for the absolute value (Section 9.3) 5736 o quantizer_update indicates if the quantizer value is updated for 5737 the i^(th) segment (Section 9.3) 5739 o quantizer_update_value indicates the update value for the segment 5740 quantizer (Section 9.3) 5742 o quantizer_update_sign indicates the update sign for the segment 5743 quantizer (Section 9.3) 5745 o loop_filter_update indicates if the loop filter level value is 5746 updated for the i^(th) segment (Section 9.3) 5748 o lf_update_value indicates the update value for the loop filter 5749 level (Section 9.3) 5751 o lf_update_sign indicates the update sign for the loop filter level 5752 (Section 9.3) 5754 o segment_prob_update indicates if the branch probabilities used to 5755 decode the segment_id in the MB header are decoded from the stream 5756 or use the default value of 255 (Section 9.3) 5758 o segment_prob the branch probabilities of the segment_id decoding 5759 tree (Section 9.3) 5760 +------------------------------------+------+ 5761 | mb_lf_adjustments() | Type | 5762 +------------------------------------+------+ 5763 | loop_filter_adj_enable | L(1) | 5764 | | | 5765 | if (loop_filter_adj_enable) { | | 5766 | | | 5767 | mode_ref_lf_delta_update | L(1) | 5768 | | | 5769 | if (mode_ref_lf_delta_update) { | | 5770 | | | 5771 | for (i = 0; i < 4; i++) { | | 5772 | | | 5773 | ref_frame_delta_update_flag | L(1) | 5774 | | | 5775 | if (ref_frame_delta_update_flag) { | | 5776 | | | 5777 | delta_magnitude | L(6) | 5778 | | | 5779 | delta_sign | L(1) | 5780 | | | 5781 | } | | 5782 | | | 5783 | } | | 5784 | | | 5785 | for (i = 0; i < 4; i++) { | | 5786 | | | 5787 | mb_mode_delta_update_flag | L(1) | 5788 | | | 5789 | if (mb_mode_delta_update_flag) { | | 5790 | | | 5791 | delta_magnitude | L(6) | 5792 | | | 5793 | delta_sign | L(1) | 5794 | | | 5795 | } | | 5796 | | | 5797 | } | | 5798 | | | 5799 | } | | 5800 | | | 5801 | } | | 5802 +------------------------------------+------+ 5804 o loop_filter_adj_enable indicates if the MB-level loop filter 5805 adjustment (based on the used reference frame and coding mode) is 5806 on for the current frame (Section 9.4) 5808 o mode_ref_lf_delta_update indicates if the delta values used in 5809 adjustment are updated in the current frame (Section 9.4) 5811 o ref_frame_delta_update_flag indicates if the adjustment delta 5812 value corresponding to a certain used reference frame is updated 5813 (Section 9.4) 5815 o delta_magnitude is the absolute value of the delta value 5817 o delta_sign is the sign of the delta value 5819 o mb_mode_delta_update_flag indicates if the adjustment delta value 5820 corresponding to certain MB prediction mode is updated 5821 (Section 9.4) 5822 +----------------------------+------+ 5823 | quant_indices() | Type | 5824 +----------------------------+------+ 5825 | y_ac_qi | L(7) | 5826 | | | 5827 | y_dc_delta_present | L(1) | 5828 | | | 5829 | if (y_dc_delta_present) { | | 5830 | | | 5831 | y_dc_delta_magnitude | L(4) | 5832 | | | 5833 | y_dc_delta_sign | L(1) | 5834 | | | 5835 | } | | 5836 | | | 5837 | if (y2_dc_delta_present) { | | 5838 | | | 5839 | y2_dc_delta_magnitude | L(4) | 5840 | | | 5841 | y2_dc_delta_sign | L(1) | 5842 | | | 5843 | } | | 5844 | | | 5845 | if (y2_ac_delta_present) { | | 5846 | | | 5847 | y2_ac_delta_magnitude | L(4) | 5848 | | | 5849 | y2_ac_delta_sign | L(1) | 5850 | | | 5851 | } | | 5852 | | | 5853 | if (uv_dc_delta_present) { | | 5854 | | | 5855 | uv_dc_delta_magnitude | L(4) | 5856 | | | 5857 | uv_dc_delta_sign | L(1) | 5858 | | | 5859 | } | | 5860 | | | 5861 | if (uv_ac_delta_present) { | | 5862 | | | 5863 | uv_ac_delta_magnitude | L(4) | 5864 | | | 5865 | uv_ac_delta_sign | L(1) | 5866 | | | 5867 | } | | 5868 +----------------------------+------+ 5870 o y_ac_qi is the dequantization table index used for the luma AC 5871 coefficients (and other coefficient groups if no delta value is 5872 present) (Section 9.6) 5874 o y_dc_delta_present indicates if the stream contains a delta value 5875 that is added to the baseline index to obtain the luma DC 5876 coefficient dequantization index (Section 9.6) 5878 o y_dc_delta_magnitude the magnitude of the delta value 5879 (Section 9.6) 5881 o y_dc_delta_sign the sign of the delta value (Section 9.6) 5883 o y2_dc_delta_present indicates if the stream contains a delta value 5884 that is added to the baseline index to obtain the Y2 block DC 5885 coefficient dequantization index (Section 9.6) 5887 o y2_ac_delta_present indicates if the stream contains a delta value 5888 that is added to the baseline index to obtain the Y2 block AC 5889 coefficient dequantization index (Section 9.6) 5891 o uv_dc_delta_present indicates if the stream contains a delta value 5892 that is added to the baseline index to obtain the chroma DC 5893 coefficient dequantization index (Section 9.6) 5895 o uv_ac_delta_present indicates if the stream contains a delta value 5896 that is added to the baseline index to obtain the chroma AC 5897 coefficient dequantization index (Section 9.6) 5898 +-------------------------------+------+ 5899 | token_prob_update() | Type | 5900 +-------------------------------+------+ 5901 | for (i = 0; i < 4; i++) { | | 5902 | | | 5903 | for (j = 0; j < 8; j++) { | | 5904 | | | 5905 | for (k = 0; k < 3; k++) { | | 5906 | | | 5907 | for (l = 0; l < 11; l++) { | | 5908 | | | 5909 | coeff_prob_update_flag | L(1) | 5910 | | | 5911 | if (coeff_prob_update_flag) { | | 5912 | | | 5913 | coeff_prob | L(8) | 5914 | | | 5915 | } | | 5916 | | | 5917 | } | | 5918 | | | 5919 | } | | 5920 | | | 5921 | } | | 5922 | | | 5923 | } | | 5924 +-------------------------------+------+ 5926 o coeff_prob_update_flag indicates if the corresponding branch 5927 probability is updated in the current frame (Section 13.4) 5929 o coeff_prob is the new branch probability (Section 13.4) 5930 +----------------------------+------+ 5931 | mv_prob_update() | Type | 5932 +----------------------------+------+ 5933 | for (i = 0; i < 2; i++) { | | 5934 | | | 5935 | for (j = 0; j < 19; j++) { | | 5936 | | | 5937 | mv_prob_update_flag | L(1) | 5938 | | | 5939 | if (mv_prob_update_flag) { | | 5940 | | | 5941 | prob | L(7) | 5942 | | | 5943 | } | | 5944 | | | 5945 | } | | 5946 | | | 5947 | } | | 5948 +----------------------------+------+ 5950 o mv_prob_update_flag indicates if the corresponding MV decoding 5951 probability is updated in the current frame (Section 17.2) 5953 o prob is the updated probability (Section 17.2) 5955 19.3. Macroblock Data 5957 +---------------------+------+ 5958 | Macroblock Data | Type | 5959 +---------------------+------+ 5960 | macroblock_header() | | 5961 | | | 5962 | residual_data() | | 5963 +---------------------+------+ 5965 +--------------------------------+------+ 5966 | macroblock_header() | Type | 5967 +--------------------------------+------+ 5968 | if (segmentation_map_update) { | | 5969 | | | 5970 | segment_id | T | 5971 | | | 5972 | if (mb_no_coeff_skip) { | | 5973 | | | 5974 | mb_coeff_skip | B(p) | 5975 | | | 5976 | } | | 5977 | | | 5978 | if (!key_frame) { | | 5979 | | | 5980 | is_inter_mb | B(p) | 5981 | | | 5982 | if (is_inter_mb) { | | 5983 | | | 5984 | mb_ref_frame_sel1 | B(p) | 5985 | | | 5986 | if (mb_ref_frame_sel1) | | 5987 | | | 5988 | mb_ref_frame_sel2 | B(p) | 5989 | | | 5990 | mv_mode | T | 5991 | | | 5992 | if (mv_mode == SPLITMV) { | | 5993 | | | 5994 | mv_split_mode | T | 5995 | | | 5996 | for (i = 0; i < numMvs; i++) { | | 5997 | | | 5998 | sub_mv_mode | T | 5999 | | | 6000 | if (sub_mv_mode == NEWMV4x4) { | | 6001 | | | 6002 | read_mvcomponent() | | 6003 | | | 6004 | read_mvcomponent() | | 6005 | | | 6006 | } | | 6007 | | | 6008 | } | | 6009 | | | 6010 | } else if (mv_mode == NEWMV) { | | 6011 | | | 6012 | read_mvcomponent() | | 6013 | | | 6014 | read_mvcomponent() | | 6015 | | | 6016 | } | | 6017 | | | 6018 | } else { /* intra mb */ | | 6019 | | | 6020 | intra_y_mode | T | 6021 +--------------------------------+------+ 6022 +-------------------------------+------+ 6023 | macroblock_header() | Type | 6024 +-------------------------------+------+ 6025 | if (intra_y_mode == B_PRED) { | | 6026 | | | 6027 | for (i = 0; i < 16; i++) | | 6028 | | | 6029 | intra_b_mode | T | 6030 | | | 6031 | } | | 6032 | | | 6033 | intra_uv_mode | T | 6034 | | | 6035 | } | | 6036 +-------------------------------+------+ 6038 o segment_id indicates to which segment the macroblock belongs 6039 (Section 10) 6041 o mb_coeff_skip indicates if the macroblock contains any coded 6042 coefficients or not (Section 11.1) 6044 o is_inter_mb indicates if the macroblock is intra or inter coded 6045 (Section 16) 6047 o mb_ref_frame_sel1 selects the reference frame to be used; last 6048 frame (0), golden/alternate (1) (Section 16.2) 6050 o mb_ref_frame_sel2 selects whether the golden (0) or alternate 6051 reference frame (1) is used (Section 16.2) 6053 o mv_mode determines the macroblock motion vector mode 6054 (Section 16.2) 6056 o mv_split_mode gives macroblock partitioning specification and 6057 determines number of motion vectors used (numMvs)(Section 16.2) 6059 o sub_mv_mode determines the sub-macroblock motion vector mode for 6060 macroblocks coded using SPLITMV motion vector mode (Section 16.2) 6062 o intra_y_mode selects the luminance intra prediction mode 6063 (Section 16.1) 6065 o intra_b_mode selects the sub-macroblock luminance prediction mode 6066 for macroblocks coded using B_PRED mode (Section 16.1) 6068 o intra_uv_mode selects the chrominance intra prediction mode 6069 (Section 16.1) 6070 +----------------------------------------------+------+ 6071 | residual_data() | Type | 6072 +----------------------------------------------+------+ 6073 | if (!mb_coeff_skip) { | | 6074 | | | 6075 | if ( (is_inter_mb && mv_mode != SPLITMV) || | | 6076 | | | 6077 | (!is_inter_mb && intra_y_mode != B_PRED) ) { | | 6078 | | | 6079 | residual_block() /* Y2 */ | | 6080 | | | 6081 | } | | 6082 | | | 6083 | for (i = 0; i < 24; i++) | | 6084 | | | 6085 | residual_block() /* 16 Y, 4 U, 4 V */ | | 6086 | | | 6087 | } | | 6088 +----------------------------------------------+------+ 6090 +-------------------------------------+------+ 6091 | residual_block() | Type | 6092 +-------------------------------------+------+ 6093 | for (i = firstCoeff; i < 16; i++) { | | 6094 | | | 6095 | token | T | 6096 | | | 6097 | if (token == EOB) break; | | 6098 | | | 6099 | if (token_has_extra_bits) { | | 6100 | | | 6101 | extra_bits | L(n) | 6102 | | | 6103 | sign | L(1) | 6104 +-------------------------------------+------+ 6106 o firstCoeff is 1 for luma blocks of macroblocks containing Y2 6107 subblock, otherwise 0 6109 o token defines the value of the coefficient, the value range of the 6110 coefficient or the end of block (Section 13.2) 6112 o extra_bits determine the value of the coefficient within the value 6113 range defined by token (Section 13.2) 6115 o sign indicates the sign of the coefficient (Section 13.2) 6117 20. Patent License 6119 Google hereby grants to You a perpetual, worldwide, non-exclusive, 6120 no-charge, royalty-free, irrevocable (except as stated in this 6121 section) patent license to make, have made, use, offer to sell, sell, 6122 import, and otherwise implementations of this specification where 6123 such license applies only to those patent claims, both currently 6124 owned by Google and acquired in the future, licensable by Google that 6125 are necessarily infringed by implementation of this specification. 6126 If You or your agent or exclusive licensee institute or order or 6127 agree to the institution of patent litigation against any entity 6128 (including a cross-claim or counterclaim in a lawsuit) alleging that 6129 any implementation of this specification constitutes direct or 6130 contributory patent infringement, or inducement of patent 6131 infringement, then any rights granted to You under the License for 6132 this specification shall terminate as of the date such litigation is 6133 filed. 6135 21. Attachment One: Reference Decoder Source Code 6137 Note that the code in this attachment may exhibit bugs, and should be 6138 considered a draft until this document reaches RFC status. 6140 21.1. bit_ops.h 6142 ---- Begin code block -------------------------------------- 6144 /* 6145 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6146 * 6147 * Use of this source code is governed by a BSD-style license 6148 * that can be found in the LICENSE file in the root of the source 6149 * tree. An additional intellectual property rights grant can be 6150 * found in the file PATENTS. All contributing project authors may 6151 * be found in the AUTHORS file in the root of the source tree. 6152 */ 6153 #ifndef BIT_OPS_H 6154 #define BIT_OPS_H 6156 /* Evaluates to a mask with n bits set */ 6157 #define BITS_MASK(n) ((1<<(n))-1) 6159 /* Returns len bits, with the LSB at position bit */ 6160 #define BITS_GET(val, bit, len) (((val)>>(bit))&BITS_MASK(len)) 6162 #endif 6164 ---- End code block ---------------------------------------- 6166 21.2. bool_decoder.h 6168 ---- Begin code block -------------------------------------- 6170 /* 6171 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6172 * 6173 * Use of this source code is governed by a BSD-style license and 6174 * patent grant that can be found in the LICENSE file in the root of 6175 * the source tree. All contributing project authors may be found in 6176 * the AUTHORS file in the root of the source tree. 6177 */ 6179 #ifndef BOOL_DECODER_H 6180 #define BOOL_DECODER_H 6181 #include 6183 struct bool_decoder 6184 { 6185 const unsigned char *input; /* next compressed data byte */ 6186 size_t input_len; /* length of the input buffer */ 6187 unsigned int range; /* identical to encoder's 6188 * range */ 6189 unsigned int value; /* contains at least 8 6190 * significant bits */ 6191 int bit_count; /* # of bits shifted out of 6192 * value, max 7 */ 6193 }; 6195 static void 6196 init_bool_decoder(struct bool_decoder *d, 6197 const unsigned char *start_partition, 6198 size_t sz) 6199 { 6200 if (sz >= 2) 6201 { 6202 d->value = (start_partition[0] << 8) /* first 2 input 6203 * bytes */ 6204 | start_partition[1]; 6205 d->input = start_partition + 2; /* ptr to next byte */ 6206 d->input_len = sz - 2; 6207 } 6208 else 6209 { 6210 d->value = 0; 6211 d->input = NULL; 6212 d->input_len = 0; 6213 } 6215 d->range = 255; /* initial range is full */ 6216 d->bit_count = 0; /* have not yet shifted out any bits */ 6217 } 6219 static int bool_get(struct bool_decoder *d, int probability) 6220 { 6221 /* range and split are identical to the corresponding values 6222 used by the encoder when this bool was written */ 6224 unsigned int split = 1 + (((d->range - 1) * probability) >> 8); 6225 unsigned int SPLIT = split << 8; 6226 int retval; /* will be 0 or 1 */ 6227 if (d->value >= SPLIT) /* encoded a one */ 6228 { 6229 retval = 1; 6230 d->range -= split; /* reduce range */ 6231 d->value -= SPLIT; /* subtract off left endpoint of 6232 * interval */ 6233 } 6234 else /* encoded a zero */ 6235 { 6236 retval = 0; 6237 d->range = split; /* reduce range, no change in left 6238 * endpoint */ 6239 } 6241 while (d->range < 128) /* shift out irrelevant value bits */ 6242 { 6243 d->value <<= 1; 6244 d->range <<= 1; 6246 if (++d->bit_count == 8) /* shift in new bits 8 at a time */ 6247 { 6248 d->bit_count = 0; 6250 if (d->input_len) 6251 { 6252 d->value |= *d->input++; 6253 d->input_len--; 6254 } 6255 } 6256 } 6258 return retval; 6259 } 6261 static int bool_get_bit(struct bool_decoder *br) 6262 { 6263 return bool_get(br, 128); 6264 } 6266 static int bool_get_uint(struct bool_decoder *br, int bits) 6267 { 6268 int z = 0; 6269 int bit; 6271 for (bit = bits - 1; bit >= 0; bit--) 6272 { 6273 z |= (bool_get_bit(br) << bit); 6274 } 6276 return z; 6277 } 6279 static int bool_get_int(struct bool_decoder *br, int bits) 6280 { 6281 int z = 0; 6282 int bit; 6284 for (bit = bits - 1; bit >= 0; bit--) 6285 { 6286 z |= (bool_get_bit(br) << bit); 6287 } 6289 return bool_get_bit(br) ? -z : z; 6290 } 6292 static int bool_maybe_get_int(struct bool_decoder *br, int bits) 6293 { 6294 return bool_get_bit(br) ? bool_get_int(br, bits) : 0; 6295 } 6297 static int 6298 bool_read_tree(struct bool_decoder *bool, 6299 const int *t, 6300 const unsigned char *p) 6301 { 6302 int i = 0; 6304 while ((i = t[ i + bool_get(bool, p[i>>1])]) > 0) ; 6306 return -i; 6307 } 6308 #endif 6310 ---- End code block ---------------------------------------- 6312 21.3. dequant_data.h 6314 ---- Begin code block -------------------------------------- 6316 static const int dc_q_lookup[128] = 6317 { 6318 4, 5, 6, 7, 8, 9, 10, 10, 6319 11, 12, 13, 14, 15, 16, 17, 17, 6320 18, 19, 20, 20, 21, 21, 22, 22, 6321 23, 23, 24, 25, 25, 26, 27, 28, 6322 29, 30, 31, 32, 33, 34, 35, 36, 6323 37, 37, 38, 39, 40, 41, 42, 43, 6324 44, 45, 46, 46, 47, 48, 49, 50, 6325 51, 52, 53, 54, 55, 56, 57, 58, 6326 59, 60, 61, 62, 63, 64, 65, 66, 6327 67, 68, 69, 70, 71, 72, 73, 74, 6328 75, 76, 76, 77, 78, 79, 80, 81, 6329 82, 83, 84, 85, 86, 87, 88, 89, 6330 91, 93, 95, 96, 98, 100, 101, 102, 6331 104, 106, 108, 110, 112, 114, 116, 118, 6332 122, 124, 126, 128, 130, 132, 134, 136, 6333 138, 140, 143, 145, 148, 151, 154, 157 6334 }; 6335 static const int ac_q_lookup[128] = 6336 { 6337 4, 5, 6, 7, 8, 9, 10, 11, 6338 12, 13, 14, 15, 16, 17, 18, 19, 6339 20, 21, 22, 23, 24, 25, 26, 27, 6340 28, 29, 30, 31, 32, 33, 34, 35, 6341 36, 37, 38, 39, 40, 41, 42, 43, 6342 44, 45, 46, 47, 48, 49, 50, 51, 6343 52, 53, 54, 55, 56, 57, 58, 60, 6344 62, 64, 66, 68, 70, 72, 74, 76, 6345 78, 80, 82, 84, 86, 88, 90, 92, 6346 94, 96, 98, 100, 102, 104, 106, 108, 6347 110, 112, 114, 116, 119, 122, 125, 128, 6348 131, 134, 137, 140, 143, 146, 149, 152, 6349 155, 158, 161, 164, 167, 170, 173, 177, 6350 181, 185, 189, 193, 197, 201, 205, 209, 6351 213, 217, 221, 225, 229, 234, 239, 245, 6352 249, 254, 259, 264, 269, 274, 279, 284 6353 }; 6355 ---- End code block ---------------------------------------- 6357 21.4. dixie.c 6358 ---- Begin code block -------------------------------------- 6360 /* 6361 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6362 * 6363 * Use of this source code is governed by a BSD-style license 6364 * that can be found in the LICENSE file in the root of the source 6365 * tree. An additional intellectual property rights grant can be 6366 * found in the file PATENTS. All contributing project authors may 6367 * be found in the AUTHORS file in the root of the source tree. 6368 */ 6369 #include "vpx_codec_internal.h" 6370 #include "bit_ops.h" 6371 #include "dixie.h" 6372 #include "vp8_prob_data.h" 6373 #include "dequant_data.h" 6374 #include "modemv.h" 6375 #include "tokens.h" 6376 #include "predict.h" 6377 #include "dixie_loopfilter.h" 6378 #include 6379 #include 6381 enum 6382 { 6383 FRAME_HEADER_SZ = 3, 6384 KEYFRAME_HEADER_SZ = 7 6385 }; 6387 #define ARRAY_COPY(a,b) {\ 6388 assert(sizeof(a)==sizeof(b));memcpy(a,b,sizeof(a));} 6389 static void 6390 decode_entropy_header(struct vp8_decoder_ctx *ctx, 6391 struct bool_decoder *bool, 6392 struct vp8_entropy_hdr *hdr) 6393 { 6394 int i, j, k, l; 6396 /* Read coefficient probability updates */ 6397 for (i = 0; i < BLOCK_TYPES; i++) 6398 for (j = 0; j < COEF_BANDS; j++) 6399 for (k = 0; k < PREV_COEF_CONTEXTS; k++) 6400 for (l = 0; l < ENTROPY_NODES; l++) 6401 if (bool_get(bool, 6402 k_coeff_entropy_update_probs 6403 [i][j][k][l])) 6404 hdr->coeff_probs[i][j][k][l] = 6405 bool_get_uint(bool, 8); 6407 /* Read coefficient skip mode probability */ 6408 hdr->coeff_skip_enabled = bool_get_bit(bool); 6410 if (hdr->coeff_skip_enabled) 6411 hdr->coeff_skip_prob = bool_get_uint(bool, 8); 6413 /* Parse interframe probability updates */ 6414 if (!ctx->frame_hdr.is_keyframe) 6415 { 6416 hdr->prob_inter = bool_get_uint(bool, 8); 6417 hdr->prob_last = bool_get_uint(bool, 8); 6418 hdr->prob_gf = bool_get_uint(bool, 8); 6420 if (bool_get_bit(bool)) 6421 for (i = 0; i < 4; i++) 6422 hdr->y_mode_probs[i] = bool_get_uint(bool, 8); 6424 if (bool_get_bit(bool)) 6425 for (i = 0; i < 3; i++) 6426 hdr->uv_mode_probs[i] = bool_get_uint(bool, 8); 6428 for (i = 0; i < 2; i++) 6429 for (j = 0; j < MV_PROB_CNT; j++) 6430 if (bool_get(bool, k_mv_entropy_update_probs[i][j])) 6431 { 6432 int x = bool_get_uint(bool, 7); 6433 hdr->mv_probs[i][j] = x ? x << 1 : 1; 6434 } 6435 } 6436 } 6438 static void 6439 decode_reference_header(struct vp8_decoder_ctx *ctx, 6440 struct bool_decoder *bool, 6441 struct vp8_reference_hdr *hdr) 6442 { 6443 unsigned int key = ctx->frame_hdr.is_keyframe; 6445 hdr->refresh_gf = key ? 1 : bool_get_bit(bool); 6446 hdr->refresh_arf = key ? 1 : bool_get_bit(bool); 6447 hdr->copy_gf = key ? 0 : !hdr->refresh_gf 6448 ? bool_get_uint(bool, 2) : 0; 6449 hdr->copy_arf = key ? 0 : !hdr->refresh_arf 6450 ? bool_get_uint(bool, 2) : 0; 6451 hdr->sign_bias[GOLDEN_FRAME] = key ? 0 : bool_get_bit(bool); 6452 hdr->sign_bias[ALTREF_FRAME] = key ? 0 : bool_get_bit(bool); 6453 hdr->refresh_entropy = bool_get_bit(bool); 6454 hdr->refresh_last = key ? 1 : bool_get_bit(bool); 6455 } 6457 static void 6458 decode_quantizer_header(struct vp8_decoder_ctx *ctx, 6459 struct bool_decoder *bool, 6460 struct vp8_quant_hdr *hdr) 6461 { 6462 int update; 6463 int last_q = hdr->q_index; 6465 hdr->q_index = bool_get_uint(bool, 7); 6466 update = last_q != hdr->q_index; 6467 update |= (hdr->y1_dc_delta_q = bool_maybe_get_int(bool, 4)); 6468 update |= (hdr->y2_dc_delta_q = bool_maybe_get_int(bool, 4)); 6469 update |= (hdr->y2_ac_delta_q = bool_maybe_get_int(bool, 4)); 6470 update |= (hdr->uv_dc_delta_q = bool_maybe_get_int(bool, 4)); 6471 update |= (hdr->uv_ac_delta_q = bool_maybe_get_int(bool, 4)); 6472 hdr->delta_update = update; 6473 } 6475 static void 6476 decode_and_init_token_partitions(struct vp8_decoder_ctx *ctx, 6477 struct bool_decoder *bool, 6478 const unsigned char *data, 6479 unsigned int sz, 6480 struct vp8_token_hdr *hdr) 6481 { 6482 int i; 6484 hdr->partitions = 1 << bool_get_uint(bool, 2); 6486 if (sz < 3 *(hdr->partitions - 1)) 6487 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6488 "Truncated packet found parsing partition" 6489 " lengths."); 6491 sz -= 3 * (hdr->partitions - 1); 6493 for (i = 0; i < hdr->partitions; i++) 6494 { 6495 if (i < hdr->partitions - 1) 6496 { 6497 hdr->partition_sz[i] = (data[2] << 16) 6498 | (data[1] << 8) | data[0]; 6499 data += 3; 6500 } 6501 else 6502 hdr->partition_sz[i] = sz; 6504 if (sz < hdr->partition_sz[i]) 6505 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6506 "Truncated partition %d", i); 6508 sz -= hdr->partition_sz[i]; 6509 } 6511 for (i = 0; i < ctx->token_hdr.partitions; i++) 6512 { 6513 init_bool_decoder(&ctx->tokens[i].bool, data, 6514 ctx->token_hdr.partition_sz[i]); 6515 data += ctx->token_hdr.partition_sz[i]; 6516 } 6517 } 6519 static void 6520 decode_loopfilter_header(struct vp8_decoder_ctx *ctx, 6521 struct bool_decoder *bool, 6522 struct vp8_loopfilter_hdr *hdr) 6523 { 6524 if (ctx->frame_hdr.is_keyframe) 6525 memset(hdr, 0, sizeof(*hdr)); 6527 hdr->use_simple = bool_get_bit(bool); 6528 hdr->level = bool_get_uint(bool, 6); 6529 hdr->sharpness = bool_get_uint(bool, 3); 6530 hdr->delta_enabled = bool_get_bit(bool); 6532 if (hdr->delta_enabled && bool_get_bit(bool)) 6533 { 6534 int i; 6536 for (i = 0; i < BLOCK_CONTEXTS; i++) 6537 hdr->ref_delta[i] = bool_maybe_get_int(bool, 6); 6539 for (i = 0; i < BLOCK_CONTEXTS; i++) 6540 hdr->mode_delta[i] = bool_maybe_get_int(bool, 6); 6541 } 6542 } 6543 static void 6544 decode_segmentation_header(struct vp8_decoder_ctx *ctx, 6545 struct bool_decoder *bool, 6546 struct vp8_segment_hdr *hdr) 6547 { 6548 if (ctx->frame_hdr.is_keyframe) 6549 memset(hdr, 0, sizeof(*hdr)); 6551 hdr->enabled = bool_get_bit(bool); 6553 if (hdr->enabled) 6554 { 6555 int i; 6557 hdr->update_map = bool_get_bit(bool); 6558 hdr->update_data = bool_get_bit(bool); 6560 if (hdr->update_data) 6561 { 6562 hdr->abs = bool_get_bit(bool); 6564 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6565 hdr->quant_idx[i] = bool_maybe_get_int(bool, 7); 6567 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6568 hdr->lf_level[i] = bool_maybe_get_int(bool, 6); 6569 } 6571 if (hdr->update_map) 6572 { 6573 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) 6574 hdr->tree_probs[i] = bool_get_bit(bool) 6575 ? bool_get_uint(bool, 8) 6576 : 255; 6577 } 6578 } 6579 else 6580 { 6581 hdr->update_map = 0; 6582 hdr->update_data = 0; 6583 } 6584 } 6586 static void 6587 dequant_global_init(struct dequant_factors dqf[MAX_MB_SEGMENTS]) 6588 { 6589 int i; 6590 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6591 dqf[i].quant_idx = -1; 6592 } 6594 static int 6595 clamp_q(int q) 6596 { 6597 if (q < 0) return 0; 6598 else if (q > 127) return 127; 6600 return q; 6601 } 6603 static int 6604 dc_q(int q) 6605 { 6606 return dc_q_lookup[clamp_q(q)]; 6607 } 6609 static int 6610 ac_q(int q) 6611 { 6612 return ac_q_lookup[clamp_q(q)]; 6613 } 6615 static void 6616 dequant_init(struct dequant_factors factors[MAX_MB_SEGMENTS], 6617 const struct vp8_segment_hdr *seg, 6618 const struct vp8_quant_hdr *quant_hdr) 6619 { 6620 int i, q; 6621 struct dequant_factors *dqf = factors; 6623 for (i = 0; i < (seg->enabled ? MAX_MB_SEGMENTS : 1); i++) 6624 { 6625 q = quant_hdr->q_index; 6627 if (seg->enabled) 6628 q = (!seg->abs) ? q + seg->quant_idx[i] 6629 : seg->quant_idx[i]; 6631 if (dqf->quant_idx != q || quant_hdr->delta_update) 6632 { 6633 dqf->factor[TOKEN_BLOCK_Y1][0] = 6634 dc_q(q + quant_hdr->y1_dc_delta_q); 6635 dqf->factor[TOKEN_BLOCK_Y1][1] = 6636 ac_q(q); 6637 dqf->factor[TOKEN_BLOCK_UV][0] = 6638 dc_q(q + quant_hdr->uv_dc_delta_q); 6639 dqf->factor[TOKEN_BLOCK_UV][1] = 6640 ac_q(q + quant_hdr->uv_ac_delta_q); 6641 dqf->factor[TOKEN_BLOCK_Y2][0] = 6642 dc_q(q + quant_hdr->y2_dc_delta_q) * 2; 6643 dqf->factor[TOKEN_BLOCK_Y2][1] = 6644 ac_q(q + quant_hdr->y2_ac_delta_q) * 155 / 100; 6646 if (dqf->factor[TOKEN_BLOCK_Y2][1] < 8) 6647 dqf->factor[TOKEN_BLOCK_Y2][1] = 8; 6649 if (dqf->factor[TOKEN_BLOCK_UV][0] > 132) 6650 dqf->factor[TOKEN_BLOCK_UV][0] = 132; 6652 dqf->quant_idx = q; 6653 } 6655 dqf++; 6656 } 6657 } 6659 static void 6660 decode_frame(struct vp8_decoder_ctx *ctx, 6661 const unsigned char *data, 6662 unsigned int sz) 6663 { 6664 vpx_codec_err_t res; 6665 struct bool_decoder bool; 6666 int i, row, partition; 6668 ctx->saved_entropy_valid = 0; 6670 if ((res = vp8_parse_frame_header(data, sz, &ctx->frame_hdr))) 6671 vpx_internal_error(&ctx->error, res, 6672 "Failed to parse frame header"); 6674 if (ctx->frame_hdr.is_experimental) 6675 vpx_internal_error(&ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6676 "Experimental bitstreams not supported."); 6678 data += FRAME_HEADER_SZ; 6679 sz -= FRAME_HEADER_SZ; 6680 if (ctx->frame_hdr.is_keyframe) 6681 { 6682 data += KEYFRAME_HEADER_SZ; 6683 sz -= KEYFRAME_HEADER_SZ; 6684 ctx->mb_cols = (ctx->frame_hdr.kf.w + 15) / 16; 6685 ctx->mb_rows = (ctx->frame_hdr.kf.h + 15) / 16; 6686 } 6688 /* Start the bitreader for the header/entropy partition */ 6689 init_bool_decoder(&bool, data, ctx->frame_hdr.part0_sz); 6691 /* Skip the colorspace and clamping bits */ 6692 if (ctx->frame_hdr.is_keyframe) 6693 if (bool_get_uint(&bool, 2)) 6694 vpx_internal_error( 6695 &ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6696 "Reserved bits not supported."); 6698 decode_segmentation_header(ctx, &bool, &ctx->segment_hdr); 6699 decode_loopfilter_header(ctx, &bool, &ctx->loopfilter_hdr); 6700 decode_and_init_token_partitions(ctx, 6701 &bool, 6702 data + ctx->frame_hdr.part0_sz, 6703 sz - ctx->frame_hdr.part0_sz, 6704 &ctx->token_hdr); 6705 decode_quantizer_header(ctx, &bool, &ctx->quant_hdr); 6706 decode_reference_header(ctx, &bool, &ctx->reference_hdr); 6708 /* Set keyframe entropy defaults. These get updated on keyframes 6709 * regardless of the refresh_entropy setting. 6710 */ 6711 if (ctx->frame_hdr.is_keyframe) 6712 { 6713 ARRAY_COPY(ctx->entropy_hdr.coeff_probs, 6714 k_default_coeff_probs); 6715 ARRAY_COPY(ctx->entropy_hdr.mv_probs, 6716 k_default_mv_probs); 6717 ARRAY_COPY(ctx->entropy_hdr.y_mode_probs, 6718 k_default_y_mode_probs); 6719 ARRAY_COPY(ctx->entropy_hdr.uv_mode_probs, 6720 k_default_uv_mode_probs); 6721 } 6723 if (!ctx->reference_hdr.refresh_entropy) 6724 { 6725 ctx->saved_entropy = ctx->entropy_hdr; 6726 ctx->saved_entropy_valid = 1; 6727 } 6728 decode_entropy_header(ctx, &bool, &ctx->entropy_hdr); 6730 vp8_dixie_modemv_init(ctx); 6731 vp8_dixie_tokens_init(ctx); 6732 vp8_dixie_predict_init(ctx); 6733 dequant_init(ctx->dequant_factors, &ctx->segment_hdr, 6734 &ctx->quant_hdr); 6736 for (row = 0, partition = 0; row < ctx->mb_rows; row++) 6737 { 6738 vp8_dixie_modemv_process_row( 6739 ctx, &bool, row, 0, ctx->mb_cols); 6740 vp8_dixie_tokens_process_row(ctx, partition, row, 0, 6741 ctx->mb_cols); 6742 vp8_dixie_predict_process_row(ctx, row, 0, ctx->mb_cols); 6744 if (ctx->loopfilter_hdr.level && row) 6745 vp8_dixie_loopfilter_process_row(ctx, row - 1, 0, 6746 ctx->mb_cols); 6748 if (++partition == ctx->token_hdr.partitions) 6749 partition = 0; 6750 } 6752 if (ctx->loopfilter_hdr.level) 6753 vp8_dixie_loopfilter_process_row( 6754 ctx, row - 1, 0, ctx->mb_cols); 6756 ctx->frame_cnt++; 6758 if (!ctx->reference_hdr.refresh_entropy) 6759 { 6760 ctx->entropy_hdr = ctx->saved_entropy; 6761 ctx->saved_entropy_valid = 0; 6762 } 6764 /* Handle reference frame updates */ 6765 if (ctx->reference_hdr.copy_arf == 1) 6766 { 6767 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6768 ctx->ref_frames[ALTREF_FRAME] = 6769 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6770 } 6771 else if (ctx->reference_hdr.copy_arf == 2) 6772 { 6773 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6774 ctx->ref_frames[ALTREF_FRAME] = 6775 vp8_dixie_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6777 } 6779 if (ctx->reference_hdr.copy_gf == 1) 6780 { 6781 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6782 ctx->ref_frames[GOLDEN_FRAME] = 6783 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6784 } 6785 else if (ctx->reference_hdr.copy_gf == 2) 6786 { 6787 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6788 ctx->ref_frames[GOLDEN_FRAME] = 6789 vp8_dixie_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6790 } 6792 if (ctx->reference_hdr.refresh_gf) 6793 { 6794 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6795 ctx->ref_frames[GOLDEN_FRAME] = 6796 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6797 } 6799 if (ctx->reference_hdr.refresh_arf) 6800 { 6801 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6802 ctx->ref_frames[ALTREF_FRAME] = 6803 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6804 } 6806 if (ctx->reference_hdr.refresh_last) 6807 { 6808 vp8_dixie_release_ref_frame(ctx->ref_frames[LAST_FRAME]); 6809 ctx->ref_frames[LAST_FRAME] = 6810 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6811 } 6813 } 6815 void 6816 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx) 6817 { 6818 dequant_global_init(ctx->dequant_factors); 6819 } 6821 #define CHECK_FOR_UPDATE(lval,rval,update_flag) do {\ 6822 unsigned int old = lval; \ 6823 update_flag |= (old != (lval = rval)); \ 6824 } while(0) 6826 vpx_codec_err_t 6827 vp8_parse_frame_header(const unsigned char *data, 6828 unsigned int sz, 6829 struct vp8_frame_hdr *hdr) 6830 { 6831 unsigned long raw; 6833 if (sz < 10) 6834 return VPX_CODEC_CORRUPT_FRAME; 6836 /* The frame header is defined as a three byte little endian 6837 * value 6838 */ 6839 raw = data[0] | (data[1] << 8) | (data[2] << 16); 6840 hdr->is_keyframe = !BITS_GET(raw, 0, 1); 6841 hdr->version = BITS_GET(raw, 1, 2); 6842 hdr->is_experimental = BITS_GET(raw, 3, 1); 6843 hdr->is_shown = BITS_GET(raw, 4, 1); 6844 hdr->part0_sz = BITS_GET(raw, 5, 19); 6846 if (sz <= hdr->part0_sz + (hdr->is_keyframe ? 10 : 3)) 6847 return VPX_CODEC_CORRUPT_FRAME; 6849 hdr->frame_size_updated = 0; 6851 if (hdr->is_keyframe) 6852 { 6853 unsigned int update = 0; 6855 /* Keyframe header consists of a three byte sync code 6856 * followed by the width and height and associated scaling 6857 * factors. 6858 */ 6859 if (data[3] != 0x9d || data[4] != 0x01 || data[5] != 0x2a) 6860 return VPX_CODEC_UNSUP_BITSTREAM; 6862 raw = data[6] | (data[7] << 8) 6863 | (data[8] << 16) | (data[9] << 24); 6864 CHECK_FOR_UPDATE(hdr->kf.w, BITS_GET(raw, 0, 14), 6865 update); 6866 CHECK_FOR_UPDATE(hdr->kf.scale_w, BITS_GET(raw, 14, 2), 6867 update); 6868 CHECK_FOR_UPDATE(hdr->kf.h, BITS_GET(raw, 16, 14), 6869 update); 6870 CHECK_FOR_UPDATE(hdr->kf.scale_h, BITS_GET(raw, 30, 2), 6871 update); 6873 hdr->frame_size_updated = update; 6875 if (!hdr->kf.w || !hdr->kf.h) 6876 return VPX_CODEC_UNSUP_BITSTREAM; 6877 } 6879 return VPX_CODEC_OK; 6880 } 6882 vpx_codec_err_t 6883 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 6884 const unsigned char *data, 6885 unsigned int sz) 6886 { 6887 volatile struct vp8_decoder_ctx *ctx_ = ctx; 6889 ctx->error.error_code = VPX_CODEC_OK; 6890 ctx->error.has_detail = 0; 6892 if (!setjmp(ctx->error.jmp)) 6893 decode_frame(ctx, data, sz); 6895 return ctx_->error.error_code; 6896 } 6898 void 6899 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx) 6900 { 6901 vp8_dixie_predict_destroy(ctx); 6902 vp8_dixie_tokens_destroy(ctx); 6903 vp8_dixie_modemv_destroy(ctx); 6904 } 6906 ---- End code block ---------------------------------------- 6908 21.5. dixie.h 6910 ---- Begin code block -------------------------------------- 6912 /* 6913 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6914 * 6915 * Use of this source code is governed by a BSD-style license 6916 * that can be found in the LICENSE file in the root of the source 6917 * tree. An additional intellectual property rights grant can be 6918 * found in the file PATENTS. All contributing project authors may 6919 * be found in the AUTHORS file in the root of the source tree. 6920 */ 6921 #ifndef DIXIE_H 6922 #define DIXIE_H 6923 #include "vpx_codec_internal.h" 6924 #include "bool_decoder.h" 6926 struct vp8_frame_hdr 6927 { 6928 unsigned int is_keyframe; /* Frame is a keyframe */ 6929 unsigned int is_experimental; /* Frame is a keyframe */ 6930 unsigned int version; /* Bitstream version */ 6931 unsigned int is_shown; /* Frame is to be displayed. */ 6932 unsigned int part0_sz; /* Partition 0 length, in bytes */ 6934 struct vp8_kf_hdr 6935 { 6936 unsigned int w; /* Width */ 6937 unsigned int h; /* Height */ 6938 unsigned int scale_w; /* Scaling factor, Width */ 6939 unsigned int scale_h; /* Scaling factor, Height */ 6940 } kf; 6942 unsigned int frame_size_updated; /* Flag to indicate a resolution 6943 * update. 6944 */ 6945 }; 6947 enum 6948 { 6949 MB_FEATURE_TREE_PROBS = 3, 6950 MAX_MB_SEGMENTS = 4 6951 }; 6953 struct vp8_segment_hdr 6954 { 6955 unsigned int enabled; 6956 unsigned int update_data; 6957 unsigned int update_map; 6958 unsigned int abs; /* 0=deltas, 1=absolute values */ 6959 unsigned int tree_probs[MB_FEATURE_TREE_PROBS]; 6960 int lf_level[MAX_MB_SEGMENTS]; 6961 int quant_idx[MAX_MB_SEGMENTS]; 6963 }; 6965 enum 6966 { 6967 BLOCK_CONTEXTS = 4 6968 }; 6970 struct vp8_loopfilter_hdr 6971 { 6972 unsigned int use_simple; 6973 unsigned int level; 6974 unsigned int sharpness; 6975 unsigned int delta_enabled; 6976 int ref_delta[BLOCK_CONTEXTS]; 6977 int mode_delta[BLOCK_CONTEXTS]; 6978 }; 6980 enum 6981 { 6982 MAX_PARTITIONS = 8 6983 }; 6985 struct vp8_token_hdr 6986 { 6987 unsigned int partitions; 6988 unsigned int partition_sz[MAX_PARTITIONS]; 6989 }; 6991 struct vp8_quant_hdr 6992 { 6993 unsigned int q_index; 6994 int delta_update; 6995 int y1_dc_delta_q; 6996 int y2_dc_delta_q; 6997 int y2_ac_delta_q; 6998 int uv_dc_delta_q; 6999 int uv_ac_delta_q; 7000 }; 7002 struct vp8_reference_hdr 7003 { 7004 unsigned int refresh_last; 7005 unsigned int refresh_gf; 7006 unsigned int refresh_arf; 7007 unsigned int copy_gf; 7008 unsigned int copy_arf; 7009 unsigned int sign_bias[4]; 7010 unsigned int refresh_entropy; 7011 }; 7013 enum 7014 { 7015 BLOCK_TYPES = 4, 7016 PREV_COEF_CONTEXTS = 3, 7017 COEF_BANDS = 8, 7018 ENTROPY_NODES = 11, 7019 }; 7020 typedef unsigned char coeff_probs_table_t[BLOCK_TYPES][COEF_BANDS] 7021 [PREV_COEF_CONTEXTS] 7022 [ENTROPY_NODES]; 7024 enum 7025 { 7026 MV_PROB_CNT = 2 + 8 - 1 + 10 /* from entropymv.h */ 7027 }; 7028 typedef unsigned char mv_component_probs_t[MV_PROB_CNT]; 7030 struct vp8_entropy_hdr 7031 { 7032 coeff_probs_table_t coeff_probs; 7033 mv_component_probs_t mv_probs[2]; 7034 unsigned int coeff_skip_enabled; 7035 unsigned char coeff_skip_prob; 7036 unsigned char y_mode_probs[4]; 7037 unsigned char uv_mode_probs[3]; 7038 unsigned char prob_inter; 7039 unsigned char prob_last; 7040 unsigned char prob_gf; 7041 }; 7043 enum reference_frame 7044 { 7045 CURRENT_FRAME, 7046 LAST_FRAME, 7047 GOLDEN_FRAME, 7048 ALTREF_FRAME, 7049 NUM_REF_FRAMES 7051 }; 7053 enum prediction_mode 7054 { 7055 /* 16x16 intra modes */ 7056 DC_PRED, V_PRED, H_PRED, TM_PRED, B_PRED, 7058 /* 16x16 inter modes */ 7059 NEARESTMV, NEARMV, ZEROMV, NEWMV, SPLITMV, 7061 MB_MODE_COUNT, 7063 /* 4x4 intra modes */ 7064 B_DC_PRED = 0, B_TM_PRED, B_VE_PRED, B_HE_PRED, B_LD_PRED, 7065 B_RD_PRED, B_VR_PRED, B_VL_PRED, B_HD_PRED, B_HU_PRED, 7067 /* 4x4 inter modes */ 7068 LEFT4X4, ABOVE4X4, ZERO4X4, NEW4X4, 7070 B_MODE_COUNT 7071 }; 7073 enum splitmv_partitioning 7074 { 7075 SPLITMV_16X8, 7076 SPLITMV_8X16, 7077 SPLITMV_8X8, 7078 SPLITMV_4X4 7079 }; 7081 typedef short filter_t[6]; 7083 typedef union mv 7084 { 7085 struct 7086 { 7087 int16_t x, y; 7088 } d; 7089 uint32_t raw; 7090 } mv_t; 7092 struct mb_base_info 7093 { 7094 unsigned char y_mode : 4; 7095 unsigned char uv_mode : 4; 7096 unsigned char segment_id : 2; 7097 unsigned char ref_frame : 2; 7098 unsigned char skip_coeff : 1; 7099 unsigned char need_mc_border : 1; 7100 enum splitmv_partitioning partitioning : 2; 7101 union mv mv; 7102 unsigned int eob_mask; 7103 }; 7105 struct mb_info 7106 { 7107 struct mb_base_info base; 7108 union 7109 { 7110 union mv mvs[16]; 7111 enum prediction_mode modes[16]; 7112 } split; 7113 }; 7115 /* A "token entropy context" has 4 Y values, 2 U, 2 V, and 1 Y2 */ 7116 typedef int token_entropy_ctx_t[4 + 2 + 2 + 1]; 7118 struct token_decoder 7119 { 7120 struct bool_decoder bool; 7121 token_entropy_ctx_t left_token_entropy_ctx; 7122 short *coeffs; 7123 }; 7125 enum token_block_type 7126 { 7127 TOKEN_BLOCK_Y1, 7128 TOKEN_BLOCK_UV, 7129 TOKEN_BLOCK_Y2, 7130 TOKEN_BLOCK_TYPES, 7131 }; 7133 struct dequant_factors 7134 { 7135 int quant_idx; 7136 short factor[TOKEN_BLOCK_TYPES][2]; /* [ Y1, UV, Y2 ] 7137 * [ DC, AC ] */ 7138 }; 7139 struct ref_cnt_img 7140 { 7141 vpx_image_t img; 7142 unsigned int ref_cnt; 7143 }; 7145 struct vp8_decoder_ctx 7146 { 7147 struct vpx_internal_error_info error; 7148 unsigned int frame_cnt; 7150 struct vp8_frame_hdr frame_hdr; 7151 struct vp8_segment_hdr segment_hdr; 7152 struct vp8_loopfilter_hdr loopfilter_hdr; 7153 struct vp8_token_hdr token_hdr; 7154 struct vp8_quant_hdr quant_hdr; 7155 struct vp8_reference_hdr reference_hdr; 7156 struct vp8_entropy_hdr entropy_hdr; 7158 struct vp8_entropy_hdr saved_entropy; 7159 unsigned int saved_entropy_valid; 7161 unsigned int mb_rows; 7162 unsigned int mb_cols; 7163 struct mb_info *mb_info_storage; 7164 struct mb_info **mb_info_rows_storage; 7165 struct mb_info **mb_info_rows; 7167 token_entropy_ctx_t *above_token_entropy_ctx; 7168 struct token_decoder tokens[MAX_PARTITIONS]; 7169 struct dequant_factors dequant_factors[MAX_MB_SEGMENTS]; 7171 struct ref_cnt_img frame_strg[NUM_REF_FRAMES]; 7172 struct ref_cnt_img *ref_frames[NUM_REF_FRAMES]; 7173 ptrdiff_t ref_frame_offsets[4]; 7175 const filter_t *subpixel_filters; 7176 }; 7178 void 7179 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx); 7181 void 7182 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx); 7183 vpx_codec_err_t 7184 vp8_parse_frame_header(const unsigned char *data, 7185 unsigned int sz, 7186 struct vp8_frame_hdr *hdr); 7188 vpx_codec_err_t 7189 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 7190 const unsigned char *data, 7191 unsigned int sz); 7193 #define CLAMP_255(x) ((x)<0?0:((x)>255?255:(x))) 7195 #endif 7197 ---- End code block ---------------------------------------- 7199 21.6. dixie_loopfilter.c 7201 ---- Begin code block -------------------------------------- 7203 /* 7204 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7205 * 7206 * Use of this source code is governed by a BSD-style license 7207 * that can be found in the LICENSE file in the root of the source 7208 * tree. An additional intellectual property rights grant can be 7209 * found in the file PATENTS. All contributing project authors may 7210 * be found in the AUTHORS file in the root of the source tree. 7211 */ 7212 #include "dixie.h" 7213 #include "dixie_loopfilter.h" 7215 #define ABS(x) ((x) >= 0 ? (x) : -(x)) 7217 #define p3 pixels[-4*stride] 7218 #define p2 pixels[-3*stride] 7219 #define p1 pixels[-2*stride] 7220 #define p0 pixels[-1*stride] 7221 #define q0 pixels[ 0*stride] 7222 #define q1 pixels[ 1*stride] 7223 #define q2 pixels[ 2*stride] 7224 #define q3 pixels[ 3*stride] 7226 #define static 7227 static int 7228 saturate_int8(int x) 7229 { 7230 if (x < -128) 7231 return -128; 7233 if (x > 127) 7234 return 127; 7236 return x; 7237 } 7239 static int 7240 saturate_uint8(int x) 7241 { 7242 if (x < 0) 7243 return 0; 7245 if (x > 255) 7246 return 255; 7248 return x; 7249 } 7251 static int 7252 high_edge_variance(unsigned char *pixels, 7253 int stride, 7254 int hev_threshold) 7255 { 7256 return ABS(p1 - p0) > hev_threshold || 7257 ABS(q1 - q0) > hev_threshold; 7258 } 7260 static int 7261 simple_threshold(unsigned char *pixels, 7262 int stride, 7263 int filter_limit) 7264 { 7265 return (ABS(p0 - q0) * 2 + (ABS(p1 - q1) >> 1)) <= filter_limit; 7266 } 7268 static int 7269 normal_threshold(unsigned char *pixels, 7270 int stride, 7271 int edge_limit, 7272 int interior_limit) 7273 { 7274 int E = edge_limit; 7275 int I = interior_limit; 7277 return simple_threshold(pixels, stride, 2 * E + I) 7278 && ABS(p3 - p2) <= I && ABS(p2 - p1) <= I 7279 && ABS(p1 - p0) <= I && ABS(q3 - q2) <= I 7280 && ABS(q2 - q1) <= I && ABS(q1 - q0) <= I; 7281 } 7283 static void 7284 filter_common(unsigned char *pixels, 7285 int stride, 7286 int use_outer_taps) 7287 { 7288 int a, f1, f2; 7290 a = 3 * (q0 - p0); 7292 if (use_outer_taps) 7293 a += saturate_int8(p1 - q1); 7295 a = saturate_int8(a); 7297 f1 = ((a + 4 > 127) ? 127 : a + 4) >> 3; 7298 f2 = ((a + 3 > 127) ? 127 : a + 3) >> 3; 7300 p0 = saturate_uint8(p0 + f2); 7301 q0 = saturate_uint8(q0 - f1); 7303 if (!use_outer_taps) 7304 { 7305 /* This handles the case of subblock_filter() 7306 * (from the bitstream guide. 7307 */ 7308 a = (f1 + 1) >> 1; 7309 p1 = saturate_uint8(p1 + a); 7310 q1 = saturate_uint8(q1 - a); 7311 } 7312 } 7314 static void 7315 filter_mb_edge(unsigned char *pixels, 7316 int stride) 7317 { 7318 int w, a; 7320 w = saturate_int8(saturate_int8(p1 - q1) + 3 * (q0 - p0)); 7322 a = (27 * w + 63) >> 7; 7323 p0 = saturate_uint8(p0 + a); 7324 q0 = saturate_uint8(q0 - a); 7326 a = (18 * w + 63) >> 7; 7327 p1 = saturate_uint8(p1 + a); 7328 q1 = saturate_uint8(q1 - a); 7330 a = (9 * w + 63) >> 7; 7331 p2 = saturate_uint8(p2 + a); 7332 q2 = saturate_uint8(q2 - a); 7334 } 7336 static void 7337 filter_mb_v_edge(unsigned char *src, 7338 int stride, 7339 int edge_limit, 7340 int interior_limit, 7341 int hev_threshold, 7342 int size) 7343 { 7344 int i; 7346 for (i = 0; i < 8 * size; i++) 7347 { 7348 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7349 { 7350 if (high_edge_variance(src, 1, hev_threshold)) 7351 filter_common(src, 1, 1); 7352 else 7353 filter_mb_edge(src, 1); 7354 } 7356 src += stride; 7357 } 7358 } 7360 static void 7361 filter_subblock_v_edge(unsigned char *src, 7362 int stride, 7363 int edge_limit, 7364 int interior_limit, 7365 int hev_threshold, 7366 int size) 7367 { 7368 int i; 7370 for (i = 0; i < 8 * size; i++) 7371 { 7372 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7373 filter_common(src, 1, 7374 high_edge_variance(src, 1, hev_threshold)); 7376 src += stride; 7377 } 7378 } 7380 static void 7381 filter_mb_h_edge(unsigned char *src, 7382 int stride, 7383 int edge_limit, 7384 int interior_limit, 7385 int hev_threshold, 7386 int size) 7387 { 7388 int i; 7390 for (i = 0; i < 8 * size; i++) 7391 { 7392 if (normal_threshold(src, stride, edge_limit, interior_limit)) 7393 { 7394 if (high_edge_variance(src, stride, hev_threshold)) 7395 filter_common(src, stride, 1); 7396 else 7397 filter_mb_edge(src, stride); 7398 } 7400 src += 1; 7401 } 7402 } 7404 static void 7405 filter_subblock_h_edge(unsigned char *src, 7406 int stride, 7407 int edge_limit, 7408 int interior_limit, 7409 int hev_threshold, 7410 int size) 7411 { 7412 int i; 7414 for (i = 0; i < 8 * size; i++) 7415 { 7416 if (normal_threshold(src, stride, edge_limit, interior_limit)) 7417 filter_common(src, stride, 7418 high_edge_variance(src, stride, 7419 hev_threshold)); 7421 src += 1; 7422 } 7423 } 7425 static void 7426 filter_v_edge_simple(unsigned char *src, 7427 int stride, 7428 int filter_limit) 7429 { 7430 int i; 7432 for (i = 0; i < 16; i++) 7433 { 7434 if (simple_threshold(src, 1, filter_limit)) 7435 filter_common(src, 1, 1); 7437 src += stride; 7438 } 7439 } 7441 static void 7442 filter_h_edge_simple(unsigned char *src, 7443 int stride, 7444 int filter_limit) 7445 { 7446 int i; 7448 for (i = 0; i < 16; i++) 7449 { 7450 if (simple_threshold(src, stride, filter_limit)) 7451 filter_common(src, stride, 1); 7453 src += 1; 7454 } 7455 } 7456 static void 7457 calculate_filter_parameters(struct vp8_decoder_ctx *ctx, 7458 struct mb_info *mbi, 7459 int *edge_limit_, 7460 int *interior_limit_, 7461 int *hev_threshold_) 7462 { 7463 int filter_level, interior_limit, hev_threshold; 7465 /* Reference code/spec seems to conflate filter_level and 7466 * edge_limit 7467 */ 7469 filter_level = ctx->loopfilter_hdr.level; 7471 if (ctx->segment_hdr.enabled) 7472 { 7473 if (!ctx->segment_hdr.abs) 7474 filter_level += 7475 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7476 else 7477 filter_level = 7478 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7479 } 7481 if (ctx->loopfilter_hdr.delta_enabled) 7482 { 7483 filter_level += 7484 ctx->loopfilter_hdr.ref_delta[mbi->base.ref_frame]; 7486 if (mbi->base.ref_frame == CURRENT_FRAME) 7487 { 7488 if (mbi->base.y_mode == B_PRED) 7489 filter_level += ctx->loopfilter_hdr.mode_delta[0]; 7490 } 7491 else if (mbi->base.y_mode == ZEROMV) 7492 filter_level += ctx->loopfilter_hdr.mode_delta[1]; 7493 else if (mbi->base.y_mode == SPLITMV) 7494 filter_level += ctx->loopfilter_hdr.mode_delta[3]; 7495 else 7496 filter_level += ctx->loopfilter_hdr.mode_delta[2]; 7497 } 7499 if (filter_level > 63) 7500 filter_level = 63; 7501 else if (filter_level < 0) 7502 filter_level = 0; 7504 interior_limit = filter_level; 7506 if (ctx->loopfilter_hdr.sharpness) 7507 { 7508 interior_limit >>= ctx->loopfilter_hdr.sharpness > 4 ? 2 : 1; 7510 if (interior_limit > 9 - ctx->loopfilter_hdr.sharpness) 7511 interior_limit = 9 - ctx->loopfilter_hdr.sharpness; 7512 } 7514 if (interior_limit < 1) 7515 interior_limit = 1; 7517 hev_threshold = (filter_level >= 15); 7519 if (filter_level >= 40) 7520 hev_threshold++; 7522 if (filter_level >= 20 && !ctx->frame_hdr.is_keyframe) 7523 hev_threshold++; 7525 *edge_limit_ = filter_level; 7526 *interior_limit_ = interior_limit; 7527 *hev_threshold_ = hev_threshold; 7528 } 7530 static void 7531 filter_row_normal(struct vp8_decoder_ctx *ctx, 7532 unsigned int row, 7533 unsigned int start_col, 7534 unsigned int num_cols) 7535 { 7536 unsigned char *y, *u, *v; 7537 int stride, uv_stride; 7538 struct mb_info *mbi; 7539 unsigned int col; 7541 /* Adjust pointers based on row, start_col */ 7542 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7543 uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 7544 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7545 u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 7546 v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 7547 y += (stride * row + start_col) * 16; 7548 u += (uv_stride * row + start_col) * 8; 7549 v += (uv_stride * row + start_col) * 8; 7550 mbi = ctx->mb_info_rows[row] + start_col; 7551 for (col = start_col; col < start_col + num_cols; col++) 7552 { 7553 int edge_limit, interior_limit, hev_threshold; 7555 /* TODO: only need to recalculate every MB if segmentation is 7556 * enabled. 7557 */ 7558 calculate_filter_parameters(ctx, mbi, &edge_limit, 7559 &interior_limit, &hev_threshold); 7561 if (edge_limit) 7562 { 7563 if (col) 7564 { 7565 filter_mb_v_edge(y, stride, edge_limit + 2, 7566 interior_limit, hev_threshold, 2); 7567 filter_mb_v_edge(u, uv_stride, edge_limit + 2, 7568 interior_limit, hev_threshold, 1); 7569 filter_mb_v_edge(v, uv_stride, edge_limit + 2, 7570 interior_limit, hev_threshold, 1); 7571 } 7573 /* NOTE: This conditional is actually dependent on the 7574 * number of coefficients decoded, not the skip flag as 7575 * coded in the bitstream. The tokens task is expected to 7576 * set 31 if there is *any* non-zero data. 7577 */ 7578 if (mbi->base.eob_mask 7579 || mbi->base.y_mode == SPLITMV 7580 || mbi->base.y_mode == B_PRED) 7581 { 7582 filter_subblock_v_edge(y + 4, stride, edge_limit, 7583 interior_limit, hev_threshold, 7584 2); 7585 filter_subblock_v_edge(y + 8, stride, edge_limit, 7586 interior_limit, hev_threshold, 7587 2); 7588 filter_subblock_v_edge(y + 12, stride, edge_limit, 7589 interior_limit, hev_threshold, 7590 2); 7591 filter_subblock_v_edge(u + 4, uv_stride, edge_limit, 7592 interior_limit, hev_threshold, 7593 1); 7594 filter_subblock_v_edge(v + 4, uv_stride, edge_limit, 7595 interior_limit, hev_threshold, 7596 1); 7597 } 7598 if (row) 7599 { 7600 filter_mb_h_edge(y, stride, edge_limit + 2, 7601 interior_limit, hev_threshold, 2); 7602 filter_mb_h_edge(u, uv_stride, edge_limit + 2, 7603 interior_limit, hev_threshold, 1); 7604 filter_mb_h_edge(v, uv_stride, edge_limit + 2, 7605 interior_limit, hev_threshold, 1); 7606 } 7608 if (mbi->base.eob_mask 7609 || mbi->base.y_mode == SPLITMV 7610 || mbi->base.y_mode == B_PRED) 7611 { 7612 filter_subblock_h_edge(y + 4 * stride, stride, 7613 edge_limit, interior_limit, 7614 hev_threshold, 2); 7615 filter_subblock_h_edge(y + 8 * stride, stride, 7616 edge_limit, interior_limit, 7617 hev_threshold, 2); 7618 filter_subblock_h_edge(y + 12 * stride, stride, 7619 edge_limit, interior_limit, 7620 hev_threshold, 2); 7621 filter_subblock_h_edge(u + 4 * uv_stride, uv_stride, 7622 edge_limit, interior_limit, 7623 hev_threshold, 1); 7624 filter_subblock_h_edge(v + 4 * uv_stride, uv_stride, 7625 edge_limit, interior_limit, 7626 hev_threshold, 1); 7627 } 7628 } 7630 y += 16; 7631 u += 8; 7632 v += 8; 7633 mbi++; 7634 } 7635 } 7637 static void 7638 filter_row_simple(struct vp8_decoder_ctx *ctx, 7639 unsigned int row, 7640 unsigned int start_col, 7641 unsigned int num_cols) 7642 { 7643 unsigned char *y; 7644 int stride; 7645 struct mb_info *mbi; 7646 unsigned int col; 7648 /* Adjust pointers based on row, start_col */ 7649 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7650 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7651 y += (stride * row + start_col) * 16; 7652 mbi = ctx->mb_info_rows[row] + start_col; 7654 for (col = start_col; col < start_col + num_cols; col++) 7655 { 7656 int edge_limit, interior_limit, hev_threshold; 7658 /* TODO: only need to recalculate every MB if segmentation is 7659 * enabled. 7660 */ 7661 calculate_filter_parameters(ctx, mbi, &edge_limit, 7662 &interior_limit, &hev_threshold); 7664 if (edge_limit) 7665 { 7667 /* NOTE: This conditional is actually dependent on the 7668 * number of coefficients decoded, not the skip flag as 7669 * coded in the bitstream. The tokens task is expected to 7670 * set 31 if there is *any* non-zero data. 7671 */ 7672 int filter_subblocks = (mbi->base.eob_mask 7673 || mbi->base.y_mode == SPLITMV 7674 || mbi->base.y_mode == B_PRED); 7675 int mb_limit = (edge_limit + 2) * 2 + interior_limit; 7676 int b_limit = edge_limit * 2 + interior_limit; 7678 if (col) 7679 filter_v_edge_simple(y, stride, mb_limit); 7681 if (filter_subblocks) 7682 { 7683 filter_v_edge_simple(y + 4, stride, b_limit); 7684 filter_v_edge_simple(y + 8, stride, b_limit); 7685 filter_v_edge_simple(y + 12, stride, b_limit); 7686 } 7688 if (row) 7689 filter_h_edge_simple(y, stride, mb_limit); 7691 if (filter_subblocks) 7692 { 7693 filter_h_edge_simple(y + 4 * stride, stride, b_limit); 7694 filter_h_edge_simple(y + 8 * stride, stride, b_limit); 7695 filter_h_edge_simple(y + 12 * stride, stride, b_limit); 7696 } 7697 } 7699 y += 16; 7700 mbi++; 7701 } 7702 } 7704 void 7705 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7706 unsigned int row, 7707 unsigned int start_col, 7708 unsigned int num_cols) 7709 { 7710 if (ctx->loopfilter_hdr.use_simple) 7711 filter_row_simple(ctx, row, start_col, num_cols); 7712 else 7713 filter_row_normal(ctx, row, start_col, num_cols); 7714 } 7716 ---- End code block ---------------------------------------- 7718 21.7. dixie_loopfilter.h 7720 ---- Begin code block -------------------------------------- 7722 /* 7723 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7724 * 7725 * Use of this source code is governed by a BSD-style license 7726 * that can be found in the LICENSE file in the root of the source 7727 * tree. An additional intellectual property rights grant can be 7728 * found in the file PATENTS. All contributing project authors may 7729 * be found in the AUTHORS file in the root of the source tree. 7730 */ 7731 #ifndef DIXIE_LOOPFILTER_H 7732 #define DIXIE_LOOPFILTER_H 7734 void 7735 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7736 unsigned int row, 7737 unsigned int start_col, 7738 unsigned int num_cols); 7740 #endif 7742 ---- End code block ---------------------------------------- 7744 21.8. idct_add.c 7746 ---- Begin code block -------------------------------------- 7748 /* 7749 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7750 * 7751 * Use of this source code is governed by a BSD-style license 7752 * that can be found in the LICENSE file in the root of the source 7753 * tree. An additional intellectual property rights grant can be 7754 * found in the file PATENTS. All contributing project authors may 7755 * be found in the AUTHORS file in the root of the source tree. 7756 */ 7757 #include "dixie.h" 7758 #include "idct_add.h" 7759 #include 7761 void 7762 vp8_dixie_walsh(const short *input, short *output) 7763 { 7764 int i; 7765 int a1, b1, c1, d1; 7766 int a2, b2, c2, d2; 7767 const short *ip = input; 7768 short *op = output; 7770 for (i = 0; i < 4; i++) 7771 { 7772 a1 = ip[0] + ip[12]; 7773 b1 = ip[4] + ip[8]; 7774 c1 = ip[4] - ip[8]; 7775 d1 = ip[0] - ip[12]; 7777 op[0] = a1 + b1; 7778 op[4] = c1 + d1; 7779 op[8] = a1 - b1; 7780 op[12] = d1 - c1; 7781 ip++; 7782 op++; 7783 } 7785 ip = output; 7786 op = output; 7788 for (i = 0; i < 4; i++) 7789 { 7790 a1 = ip[0] + ip[3]; 7791 b1 = ip[1] + ip[2]; 7792 c1 = ip[1] - ip[2]; 7793 d1 = ip[0] - ip[3]; 7795 a2 = a1 + b1; 7796 b2 = c1 + d1; 7797 c2 = a1 - b1; 7798 d2 = d1 - c1; 7800 op[0] = (a2 + 3) >> 3; 7801 op[1] = (b2 + 3) >> 3; 7802 op[2] = (c2 + 3) >> 3; 7803 op[3] = (d2 + 3) >> 3; 7805 ip += 4; 7806 op += 4; 7807 } 7808 } 7810 #define cospi8sqrt2minus1 20091 7811 #define sinpi8sqrt2 35468 7812 #define rounding 0 7813 static void 7814 idct_columns(const short *input, short *output) 7815 { 7816 int i; 7817 int a1, b1, c1, d1; 7819 const short *ip = input; 7820 short *op = output; 7821 int temp1, temp2; 7822 int shortpitch = 4; 7824 for (i = 0; i < 4; i++) 7825 { 7826 a1 = ip[0] + ip[8]; 7827 b1 = ip[0] - ip[8]; 7829 temp1 = (ip[4] * sinpi8sqrt2 + rounding) >> 16; 7830 temp2 = ip[12] + 7831 ((ip[12] * cospi8sqrt2minus1 + rounding) >> 16); 7832 c1 = temp1 - temp2; 7834 temp1 = ip[4] + 7835 ((ip[4] * cospi8sqrt2minus1 + rounding) >> 16); 7836 temp2 = (ip[12] * sinpi8sqrt2 + rounding) >> 16; 7837 d1 = temp1 + temp2; 7839 op[shortpitch*0] = a1 + d1; 7840 op[shortpitch*3] = a1 - d1; 7842 op[shortpitch*1] = b1 + c1; 7843 op[shortpitch*2] = b1 - c1; 7845 ip++; 7846 op++; 7847 } 7848 } 7850 void 7851 vp8_dixie_idct_add(unsigned char *recon, 7852 const unsigned char *predict, 7853 int stride, 7854 const short *coeffs) 7855 { 7856 int i; 7857 int a1, b1, c1, d1, temp1, temp2; 7858 short tmp[16]; 7859 idct_columns(coeffs, tmp); 7860 coeffs = tmp; 7862 for (i = 0; i < 4; i++) 7863 { 7864 a1 = coeffs[0] + coeffs[2]; 7865 b1 = coeffs[0] - coeffs[2]; 7867 temp1 = (coeffs[1] * sinpi8sqrt2 + rounding) >> 16; 7868 temp2 = coeffs[3] + 7869 ((coeffs[3] * cospi8sqrt2minus1 + rounding) >> 16); 7870 c1 = temp1 - temp2; 7872 temp1 = coeffs[1] + 7873 ((coeffs[1] * cospi8sqrt2minus1 + rounding) >> 16); 7874 temp2 = (coeffs[3] * sinpi8sqrt2 + rounding) >> 16; 7875 d1 = temp1 + temp2; 7877 recon[0] = CLAMP_255(predict[0] + ((a1 + d1 + 4) >> 3)); 7878 recon[3] = CLAMP_255(predict[3] + ((a1 - d1 + 4) >> 3)); 7879 recon[1] = CLAMP_255(predict[1] + ((b1 + c1 + 4) >> 3)); 7880 recon[2] = CLAMP_255(predict[2] + ((b1 - c1 + 4) >> 3)); 7882 coeffs += 4; 7883 recon += stride; 7884 predict += stride; 7885 } 7886 } 7888 ---- End code block ---------------------------------------- 7890 21.9. idct_add.h 7892 ---- Begin code block -------------------------------------- 7894 /* 7895 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7896 * 7897 * Use of this source code is governed by a BSD-style license 7898 * that can be found in the LICENSE file in the root of the source 7899 * tree. An additional intellectual property rights grant can be 7900 * found in the file PATENTS. All contributing project authors may 7901 * be found in the AUTHORS file in the root of the source tree. 7902 */ 7903 #ifndef IDCT_ADD_H 7904 #define IDCT_ADD_H 7906 void 7907 vp8_dixie_idct_add_init(struct vp8_decoder_ctx *ctx); 7909 void 7910 vp8_dixie_idct_add(unsigned char *recon, 7911 const unsigned char *predict, 7912 int stride, 7913 const short *coeffs); 7915 void 7916 vp8_dixie_walsh(const short *in, short *out); 7918 void 7919 vp8_dixie_idct_add_process_row(struct vp8_decoder_ctx *ctx, 7920 short *coeffs, 7921 unsigned int row, 7922 unsigned int start_col, 7923 unsigned int num_cols); 7925 #endif 7927 ---- End code block ---------------------------------------- 7929 21.10. mem.h 7931 ---- Begin code block -------------------------------------- 7933 /* 7934 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7935 * 7936 * Use of this source code is governed by a BSD-style license 7937 * that can be found in the LICENSE file in the root of the source 7938 * tree. An additional intellectual property rights grant can be 7939 * found in the file PATENTS. All contributing project authors may 7940 * be found in the AUTHORS file in the root of the source tree. 7941 */ 7943 #ifndef VPX_PORTS_MEM_H 7944 #define VPX_PORTS_MEM_H 7945 #include "vpx_config.h" 7946 #include "vpx_integer.h" 7948 #if defined(__GNUC__) && __GNUC__ 7949 #define DECLARE_ALIGNED(n,typ,val) typ val __attribute__ \ 7950 ((aligned (n))) 7951 #elif defined(_MSC_VER) 7952 #define DECLARE_ALIGNED(n,typ,val) __declspec(align(n)) typ val 7953 #else 7954 #warning No alignment directives known for this compiler. 7955 #define DECLARE_ALIGNED(n,typ,val) typ val 7956 #endif 7957 #endif 7959 /* Declare an aligned array on the stack, for situations where the 7960 * stack pointer may not have the alignment we expect. Creates an 7961 * array with a modified name, then defines val to be a pointer, and 7962 * aligns that pointer within the array. 7963 */ 7964 #define DECLARE_ALIGNED_ARRAY(a,typ,val,n)\ 7965 typ val##_[(n)+(a)/sizeof(typ)+1];\ 7966 typ *val = (typ*)((((intptr_t)val##_)+(a)-1)&((intptr_t)-(a))) 7968 /* Indicates that the usage of the specified variable has been 7969 * audited to assure that it's safe to use uninitialized. Silences 7970 * 'may be used uninitialized' warnings on gcc. 7971 */ 7972 #if defined(__GNUC__) && __GNUC__ 7973 #define UNINITIALIZED_IS_SAFE(x) x=x 7974 #else 7975 #define UNINITIALIZED_IS_SAFE(x) x 7976 #endif 7977 ---- End code block ---------------------------------------- 7979 21.11. modemv.c 7981 ---- Begin code block -------------------------------------- 7983 /* 7984 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7985 * 7986 * Use of this source code is governed by a BSD-style license 7987 * that can be found in the LICENSE file in the root of the source 7988 * tree. An additional intellectual property rights grant can be 7989 * found in the file PATENTS. All contributing project authors may 7990 * be found in the AUTHORS file in the root of the source tree. 7991 */ 7992 #include "dixie.h" 7993 #include "modemv_data.h" 7994 #include 7995 #include 7997 struct mv_clamp_rect 7998 { 7999 int to_left, to_right, to_top, to_bottom; 8000 }; 8002 static union mv 8003 clamp_mv(union mv raw, const struct mv_clamp_rect *bounds) 8004 { 8005 union mv newmv; 8007 newmv.d.x = (raw.d.x < bounds->to_left) 8008 ? bounds->to_left : raw.d.x; 8009 newmv.d.x = (raw.d.x > bounds->to_right) 8010 ? bounds->to_right : newmv.d.x; 8011 newmv.d.y = (raw.d.y < bounds->to_top) 8012 ? bounds->to_top : raw.d.y; 8013 newmv.d.y = (raw.d.y > bounds->to_bottom) 8014 ? bounds->to_bottom : newmv.d.y; 8015 return newmv; 8016 } 8018 static int 8019 read_segment_id(struct bool_decoder *bool, 8020 struct vp8_segment_hdr *seg) 8021 { 8022 return bool_get(bool, seg->tree_probs[0]) 8023 ? 2 + bool_get(bool, seg->tree_probs[2]) 8024 : bool_get(bool, seg->tree_probs[1]); 8025 } 8027 static enum prediction_mode 8028 above_block_mode(const struct mb_info *this, 8029 const struct mb_info *above, 8030 unsigned int b) 8031 { 8032 if (b < 4) 8033 { 8034 switch (above->base.y_mode) 8035 { 8036 case DC_PRED: 8037 return B_DC_PRED; 8038 case V_PRED: 8039 return B_VE_PRED; 8040 case H_PRED: 8041 return B_HE_PRED; 8042 case TM_PRED: 8043 return B_TM_PRED; 8044 case B_PRED: 8045 return above->split.modes[b+12]; 8046 default: 8047 assert(0); 8048 } 8049 } 8051 return this->split.modes[b-4]; 8052 } 8054 static enum prediction_mode 8055 left_block_mode(const struct mb_info *this, 8056 const struct mb_info *left, 8057 unsigned int b) 8058 { 8059 if (!(b & 3)) 8060 { 8061 switch (left->base.y_mode) 8062 { 8063 case DC_PRED: 8064 return B_DC_PRED; 8065 case V_PRED: 8066 return B_VE_PRED; 8067 case H_PRED: 8069 return B_HE_PRED; 8070 case TM_PRED: 8071 return B_TM_PRED; 8072 case B_PRED: 8073 return left->split.modes[b+3]; 8074 default: 8075 assert(0); 8076 } 8077 } 8079 return this->split.modes[b-1]; 8080 } 8082 static void 8083 decode_kf_mb_mode(struct mb_info *this, 8084 struct mb_info *left, 8085 struct mb_info *above, 8086 struct bool_decoder *bool) 8087 { 8088 int y_mode, uv_mode; 8090 y_mode = bool_read_tree(bool, kf_y_mode_tree, kf_y_mode_probs); 8092 if (y_mode == B_PRED) 8093 { 8094 unsigned int i; 8096 for (i = 0; i < 16; i++) 8097 { 8098 enum prediction_mode a = above_block_mode(this, above, i); 8099 enum prediction_mode l = left_block_mode(this, left, i); 8100 enum prediction_mode b; 8102 b = bool_read_tree(bool, b_mode_tree, 8103 kf_b_mode_probs[a][l]); 8104 this->split.modes[i] = b; 8105 } 8106 } 8108 uv_mode = bool_read_tree(bool, uv_mode_tree, kf_uv_mode_probs); 8110 this->base.y_mode = y_mode; 8111 this->base.uv_mode = uv_mode; 8112 this->base.mv.raw = 0; 8113 this->base.ref_frame = 0; 8114 } 8115 static void 8116 decode_intra_mb_mode(struct mb_info *this, 8117 struct vp8_entropy_hdr *hdr, 8118 struct bool_decoder *bool) 8119 { 8120 /* Like decode_kf_mb_mode, but with probabilities transmitted in the 8121 * bitstream and no context on the above/left block mode. 8122 */ 8123 int y_mode, uv_mode; 8125 y_mode = bool_read_tree(bool, y_mode_tree, hdr->y_mode_probs); 8127 if (y_mode == B_PRED) 8128 { 8129 unsigned int i; 8131 for (i = 0; i < 16; i++) 8132 { 8133 enum prediction_mode b; 8135 b = bool_read_tree(bool, b_mode_tree, default_b_mode_probs); 8136 this->split.modes[i] = b; 8137 } 8138 } 8140 uv_mode = bool_read_tree(bool, uv_mode_tree, hdr->uv_mode_probs); 8142 this->base.y_mode = y_mode; 8143 this->base.uv_mode = uv_mode; 8144 this->base.mv.raw = 0; 8145 this->base.ref_frame = CURRENT_FRAME; 8146 } 8148 static int 8149 read_mv_component(struct bool_decoder *bool, 8150 const unsigned char mvc[MV_PROB_CNT]) 8151 { 8152 enum {IS_SHORT, SIGN, SHORT, BITS = SHORT + 8 - 1, LONG_WIDTH = 10}; 8153 int x = 0; 8155 if (bool_get(bool, mvc[IS_SHORT])) /* Large */ 8156 { 8157 int i = 0; 8159 for (i = 0; i < 3; i++) 8160 x += bool_get(bool, mvc[BITS + i]) << i; 8162 /* Skip bit 3, which is sometimes implicit */ 8163 for (i = LONG_WIDTH - 1; i > 3; i--) 8164 x += bool_get(bool, mvc[BITS + i]) << i; 8166 if (!(x & 0xFFF0) || bool_get(bool, mvc[BITS + 3])) 8167 x += 8; 8168 } 8169 else /* small */ 8170 x = bool_read_tree(bool, small_mv_tree, mvc + SHORT); 8172 if (x && bool_get(bool, mvc[SIGN])) 8173 x = -x; 8175 return x << 1; 8176 } 8178 static mv_t 8179 above_block_mv(const struct mb_info *this, 8180 const struct mb_info *above, 8181 unsigned int b) 8182 { 8183 if (b < 4) 8184 { 8185 if (above->base.y_mode == SPLITMV) 8186 return above->split.mvs[b+12]; 8188 return above->base.mv; 8189 } 8191 return this->split.mvs[b-4]; 8192 } 8194 static mv_t 8195 left_block_mv(const struct mb_info *this, 8196 const struct mb_info *left, 8197 unsigned int b) 8198 { 8199 if (!(b & 3)) 8200 { 8201 if (left->base.y_mode == SPLITMV) 8202 return left->split.mvs[b+3]; 8204 return left->base.mv; 8205 } 8207 return this->split.mvs[b-1]; 8209 } 8211 static enum prediction_mode 8212 submv_ref(struct bool_decoder *bool, union mv l, union mv a) 8213 { 8214 enum subblock_mv_ref 8215 { 8216 SUBMVREF_NORMAL, 8217 SUBMVREF_LEFT_ZED, 8218 SUBMVREF_ABOVE_ZED, 8219 SUBMVREF_LEFT_ABOVE_SAME, 8220 SUBMVREF_LEFT_ABOVE_ZED 8221 }; 8223 int lez = !(l.raw); 8224 int aez = !(a.raw); 8225 int lea = l.raw == a.raw; 8226 enum subblock_mv_ref ctx = SUBMVREF_NORMAL; 8228 if (lea && lez) 8229 ctx = SUBMVREF_LEFT_ABOVE_ZED; 8230 else if (lea) 8231 ctx = SUBMVREF_LEFT_ABOVE_SAME; 8232 else if (aez) 8233 ctx = SUBMVREF_ABOVE_ZED; 8234 else if (lez) 8235 ctx = SUBMVREF_LEFT_ZED; 8237 return bool_read_tree(bool, submv_ref_tree, submv_ref_probs2[ctx]); 8238 } 8240 static void 8241 read_mv(struct bool_decoder *bool, 8242 union mv *mv, 8243 mv_component_probs_t mvc[2]) 8244 { 8245 mv->d.y = read_mv_component(bool, mvc[0]); 8246 mv->d.x = read_mv_component(bool, mvc[1]); 8247 } 8249 static void 8250 mv_bias(const struct mb_info *mb, 8251 const unsigned int sign_bias[3], 8252 enum reference_frame ref_frame, 8253 union mv *mv) 8255 { 8256 if (sign_bias[mb->base.ref_frame] ^ sign_bias[ref_frame]) 8257 { 8258 mv->d.x *= -1; 8259 mv->d.y *= -1; 8260 } 8261 } 8263 enum near_mv_v 8264 { 8265 CNT_BEST = 0, 8266 CNT_ZEROZERO = 0, 8267 CNT_NEAREST, 8268 CNT_NEAR, 8269 CNT_SPLITMV 8270 }; 8272 static void 8273 find_near_mvs(const struct mb_info *this, 8274 const struct mb_info *left, 8275 const struct mb_info *above, 8276 const unsigned int sign_bias[3], 8277 union mv near_mvs[4], 8278 int cnt[4]) 8279 { 8280 const struct mb_info *aboveleft = above - 1; 8281 union mv *mv = near_mvs; 8282 int *cntx = cnt; 8284 /* Zero accumulators */ 8285 mv[0].raw = mv[1].raw = mv[2].raw = 0; 8286 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 8288 /* Process above */ 8289 if (above->base.ref_frame != CURRENT_FRAME) 8290 { 8291 if (above->base.mv.raw) 8292 { 8293 (++mv)->raw = above->base.mv.raw; 8294 mv_bias(above, sign_bias, this->base.ref_frame, mv); 8295 ++cntx; 8296 } 8298 *cntx += 2; 8299 } 8300 /* Process left */ 8301 if (left->base.ref_frame != CURRENT_FRAME) 8302 { 8303 if (left->base.mv.raw) 8304 { 8305 union mv this_mv; 8307 this_mv.raw = left->base.mv.raw; 8308 mv_bias(left, sign_bias, this->base.ref_frame, &this_mv); 8310 if (this_mv.raw != mv->raw) 8311 { 8312 (++mv)->raw = this_mv.raw; 8313 ++cntx; 8314 } 8316 *cntx += 2; 8317 } 8318 else 8319 cnt[CNT_ZEROZERO] += 2; 8320 } 8322 /* Process above left */ 8323 if (aboveleft->base.ref_frame != CURRENT_FRAME) 8324 { 8325 if (aboveleft->base.mv.raw) 8326 { 8327 union mv this_mv; 8329 this_mv.raw = aboveleft->base.mv.raw; 8330 mv_bias(aboveleft, sign_bias, this->base.ref_frame, 8331 &this_mv); 8333 if (this_mv.raw != mv->raw) 8334 { 8335 (++mv)->raw = this_mv.raw; 8336 ++cntx; 8337 } 8339 *cntx += 1; 8340 } 8341 else 8342 cnt[CNT_ZEROZERO] += 1; 8343 } 8345 /* If we have three distinct MV's ... */ 8346 if (cnt[CNT_SPLITMV]) 8347 { 8348 /* See if above-left MV can be merged with NEAREST */ 8349 if (mv->raw == near_mvs[CNT_NEAREST].raw) 8350 cnt[CNT_NEAREST] += 1; 8351 } 8353 cnt[CNT_SPLITMV] = ((above->base.y_mode == SPLITMV) 8354 + (left->base.y_mode == SPLITMV)) * 2 8355 + (aboveleft->base.y_mode == SPLITMV); 8357 /* Swap near and nearest if necessary */ 8358 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) 8359 { 8360 int tmp; 8361 tmp = cnt[CNT_NEAREST]; 8362 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 8363 cnt[CNT_NEAR] = tmp; 8364 tmp = near_mvs[CNT_NEAREST].raw; 8365 near_mvs[CNT_NEAREST].raw = near_mvs[CNT_NEAR].raw; 8366 near_mvs[CNT_NEAR].raw = tmp; 8367 } 8369 /* Use near_mvs[CNT_BEST] to store the "best" MV. Note that this 8370 * storage shares the same address as near_mvs[CNT_ZEROZERO]. 8371 */ 8372 if (cnt[CNT_NEAREST] >= cnt[CNT_BEST]) 8373 near_mvs[CNT_BEST] = near_mvs[CNT_NEAREST]; 8374 } 8376 static void 8377 decode_split_mv(struct mb_info *this, 8378 const struct mb_info *left, 8379 const struct mb_info *above, 8380 struct vp8_entropy_hdr *hdr, 8381 union mv *best_mv, 8382 struct bool_decoder *bool) 8383 { 8384 const int *partition; 8385 int j, k, mask, partition_id; 8387 partition_id = bool_read_tree(bool, split_mv_tree, split_mv_probs); 8388 partition = mv_partitions[partition_id]; 8389 this->base.partitioning = partition_id; 8391 for (j = 0, mask = 0; mask < 65535; j++) 8392 { 8393 union mv mv, left_mv, above_mv; 8394 enum prediction_mode subblock_mode; 8395 /* Find the first subblock in this partition. */ 8396 for (k = 0; j != partition[k]; k++); 8398 /* Decode the next MV */ 8399 left_mv = left_block_mv(this, left, k); 8400 above_mv = above_block_mv(this, above, k); 8401 subblock_mode = submv_ref(bool, left_mv, above_mv); 8403 switch (subblock_mode) 8404 { 8405 case LEFT4X4: 8406 mv = left_mv; 8407 break; 8408 case ABOVE4X4: 8409 mv = above_mv; 8410 break; 8411 case ZERO4X4: 8412 mv.raw = 0; 8413 break; 8414 case NEW4X4: 8415 read_mv(bool, &mv, hdr->mv_probs); 8416 mv.d.x += best_mv->d.x; 8417 mv.d.y += best_mv->d.y; 8418 break; 8419 default: 8420 assert(0); 8421 } 8423 /* Fill the MV's for this partition */ 8424 for (; k < 16; k++) 8425 if (j == partition[k]) 8426 { 8427 this->split.mvs[k] = mv; 8428 mask |= 1 << k; 8429 } 8430 } 8431 } 8433 static int 8434 need_mc_border(union mv mv, int l, int t, int b_w, int w, int h) 8435 { 8436 int b, r; 8438 /* Get distance to edge for top-left pixel */ 8439 l += (mv.d.x >> 3); 8440 t += (mv.d.y >> 3); 8441 /* Get distance to edge for bottom-right pixel */ 8442 r = w - (l + b_w); 8443 b = h - (t + b_w); 8445 return (l >> 1 < 2 || r >> 1 < 3 || t >> 1 < 2 || b >> 1 < 3); 8446 } 8448 static void 8449 decode_mvs(struct vp8_decoder_ctx *ctx, 8450 struct mb_info *this, 8451 const struct mb_info *left, 8452 const struct mb_info *above, 8453 const struct mv_clamp_rect *bounds, 8454 struct bool_decoder *bool) 8455 { 8456 struct vp8_entropy_hdr *hdr = &ctx->entropy_hdr; 8457 union mv near_mvs[4]; 8458 union mv clamped_best_mv; 8459 int mv_cnts[4]; 8460 unsigned char probs[4]; 8461 enum {BEST, NEAREST, NEAR}; 8462 int x, y, w, h, b; 8464 this->base.ref_frame = bool_get(bool, hdr->prob_last) 8465 ? 2 + bool_get(bool, hdr->prob_gf) 8466 : 1; 8468 find_near_mvs(this, this - 1, above, ctx->reference_hdr.sign_bias, 8469 near_mvs, mv_cnts); 8470 probs[0] = mv_counts_to_probs[mv_cnts[0]][0]; 8471 probs[1] = mv_counts_to_probs[mv_cnts[1]][1]; 8472 probs[2] = mv_counts_to_probs[mv_cnts[2]][2]; 8473 probs[3] = mv_counts_to_probs[mv_cnts[3]][3]; 8475 this->base.y_mode = bool_read_tree(bool, mv_ref_tree, probs); 8476 this->base.uv_mode = this->base.y_mode; 8478 this->base.need_mc_border = 0; 8479 x = (-bounds->to_left - 128) >> 3; 8480 y = (-bounds->to_top - 128) >> 3; 8481 w = ctx->mb_cols * 16; 8482 h = ctx->mb_rows * 16; 8484 switch (this->base.y_mode) 8485 { 8486 case NEARESTMV: 8487 this->base.mv = clamp_mv(near_mvs[NEAREST], bounds); 8488 break; 8490 case NEARMV: 8491 this->base.mv = clamp_mv(near_mvs[NEAR], bounds); 8492 break; 8493 case ZEROMV: 8494 this->base.mv.raw = 0; 8495 return; //skip need_mc_border check 8496 case NEWMV: 8497 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8498 read_mv(bool, &this->base.mv, hdr->mv_probs); 8499 this->base.mv.d.x += clamped_best_mv.d.x; 8500 this->base.mv.d.y += clamped_best_mv.d.y; 8501 break; 8502 case SPLITMV: 8503 { 8504 union mv chroma_mv[4] = {{{0}}}; 8506 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8507 decode_split_mv(this, left, above, hdr, &clamped_best_mv, bool); 8508 this->base.mv = this->split.mvs[15]; 8510 for (b = 0; b < 16; b++) 8511 { 8512 chroma_mv[(b>>1&1) + (b>>2&2)].d.x += 8513 this->split.mvs[b].d.x; 8514 chroma_mv[(b>>1&1) + (b>>2&2)].d.y += 8515 this->split.mvs[b].d.y; 8517 if (need_mc_border(this->split.mvs[b], 8518 x + (b & 3) * 4, y + (b & ~3), 4, w, h)) 8519 { 8520 this->base.need_mc_border = 1; 8521 break; 8522 } 8523 } 8525 for (b = 0; b < 4; b++) 8526 { 8527 chroma_mv[b].d.x += 4 + 8 * (chroma_mv[b].d.x >> 31); 8528 chroma_mv[b].d.y += 4 + 8 * (chroma_mv[b].d.y >> 31); 8529 chroma_mv[b].d.x /= 4; 8530 chroma_mv[b].d.y /= 4; 8532 //note we're passing in non-subsampled coordinates 8533 if (need_mc_border(chroma_mv[b], 8534 x + (b & 1) * 8, y + (b >> 1) * 8, 16, w, h)) 8535 { 8536 this->base.need_mc_border = 1; 8537 break; 8539 } 8540 } 8542 return; //skip need_mc_border check 8543 } 8544 default: 8545 assert(0); 8546 } 8548 if (need_mc_border(this->base.mv, x, y, 16, w, h)) 8549 this->base.need_mc_border = 1; 8550 } 8552 void 8553 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8554 struct bool_decoder *bool, 8555 int row, 8556 int start_col, 8557 int num_cols) 8558 { 8559 struct mb_info *above, *this; 8560 unsigned int col; 8561 struct mv_clamp_rect bounds; 8563 this = ctx->mb_info_rows[row] + start_col; 8564 above = ctx->mb_info_rows[row - 1] + start_col; 8566 /* Calculate the eighth-pel MV bounds using a 1 MB border. */ 8567 bounds.to_left = -((start_col + 1) << 7); 8568 bounds.to_right = (ctx->mb_cols - start_col) << 7; 8569 bounds.to_top = -((row + 1) << 7); 8570 bounds.to_bottom = (ctx->mb_rows - row) << 7; 8572 for (col = start_col; col < start_col + num_cols; col++) 8573 { 8574 if (ctx->segment_hdr.update_map) 8575 this->base.segment_id = read_segment_id(bool, 8576 &ctx->segment_hdr); 8578 if (ctx->entropy_hdr.coeff_skip_enabled) 8579 this->base.skip_coeff = bool_get(bool, 8580 ctx->entropy_hdr.coeff_skip_prob); 8582 if (ctx->frame_hdr.is_keyframe) 8583 { 8584 if (!ctx->segment_hdr.update_map) 8585 this->base.segment_id = 0; 8587 decode_kf_mb_mode(this, this - 1, above, bool); 8588 } 8589 else 8590 { 8591 if (bool_get(bool, ctx->entropy_hdr.prob_inter)) 8592 decode_mvs(ctx, this, this - 1, above, &bounds, bool); 8593 else 8594 decode_intra_mb_mode(this, &ctx->entropy_hdr, bool); 8596 bounds.to_left -= 16 << 3; 8597 bounds.to_right -= 16 << 3; 8598 } 8600 /* Advance to next mb */ 8601 this++; 8602 above++; 8603 } 8604 } 8606 void 8607 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx) 8608 { 8609 unsigned int mbi_w, mbi_h, i; 8610 struct mb_info *mbi; 8612 mbi_w = ctx->mb_cols + 1; /* For left border col */ 8613 mbi_h = ctx->mb_rows + 1; /* For above border row */ 8615 if (ctx->frame_hdr.frame_size_updated) 8616 { 8617 free(ctx->mb_info_storage); 8618 ctx->mb_info_storage = NULL; 8619 free(ctx->mb_info_rows_storage); 8620 ctx->mb_info_rows_storage = NULL; 8621 } 8623 if (!ctx->mb_info_storage) 8624 ctx->mb_info_storage = calloc(mbi_w * mbi_h, 8625 sizeof(*ctx->mb_info_storage)); 8627 if (!ctx->mb_info_rows_storage) 8628 ctx->mb_info_rows_storage = calloc(mbi_h, 8629 sizeof(*ctx->mb_info_rows_storage)); 8631 /* Set up row pointers */ 8632 mbi = ctx->mb_info_storage + 1; 8633 for (i = 0; i < mbi_h; i++) 8634 { 8635 ctx->mb_info_rows_storage[i] = mbi; 8636 mbi += mbi_w; 8637 } 8639 ctx->mb_info_rows = ctx->mb_info_rows_storage + 1; 8640 } 8642 void 8643 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx) 8644 { 8645 free(ctx->mb_info_storage); 8646 ctx->mb_info_storage = NULL; 8647 free(ctx->mb_info_rows_storage); 8648 ctx->mb_info_rows_storage = NULL; 8649 } 8651 ---- End code block ---------------------------------------- 8652 21.12. modemv.h 8654 ---- Begin code block -------------------------------------- 8656 /* 8657 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8658 * 8659 * Use of this source code is governed by a BSD-style license 8660 * that can be found in the LICENSE file in the root of the source 8661 * tree. An additional intellectual property rights grant can be 8662 * found in the file PATENTS. All contributing project authors may 8663 * be found in the AUTHORS file in the root of the source tree. 8664 */ 8665 #ifndef MODEMV_H 8666 #define MODEMV_H 8668 void 8669 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx); 8671 void 8672 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx); 8674 void 8675 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8676 struct bool_decoder *bool, 8677 int row, 8678 int start_col, 8679 int num_cols); 8681 #endif 8683 ---- End code block ---------------------------------------- 8685 21.13. modemv_data.h 8687 ---- Begin code block -------------------------------------- 8689 static const unsigned char kf_y_mode_probs[] = { 145, 156, 163, 128}; 8690 static const unsigned char kf_uv_mode_probs[] = { 142, 114, 183}; 8691 static const unsigned char kf_b_mode_probs[10][10][9] = 8692 { 8693 { /* above mode 0 */ 8694 { /* left mode 0 */ 231, 120, 48, 89, 115, 113, 120, 152, 112}, 8695 { /* left mode 1 */ 152, 179, 64, 126, 170, 118, 46, 70, 95}, 8696 { /* left mode 2 */ 175, 69, 143, 80, 85, 82, 72, 155, 103}, 8697 { /* left mode 3 */ 56, 58, 10, 171, 218, 189, 17, 13, 152}, 8698 { /* left mode 4 */ 144, 71, 10, 38, 171, 213, 144, 34, 26}, 8699 { /* left mode 5 */ 114, 26, 17, 163, 44, 195, 21, 10, 173}, 8700 { /* left mode 6 */ 121, 24, 80, 195, 26, 62, 44, 64, 85}, 8701 { /* left mode 7 */ 170, 46, 55, 19, 136, 160, 33, 206, 71}, 8702 { /* left mode 8 */ 63, 20, 8, 114, 114, 208, 12, 9, 226}, 8703 { /* left mode 9 */ 81, 40, 11, 96, 182, 84, 29, 16, 36} 8704 }, 8705 { /* above mode 1 */ 8706 { /* left mode 0 */ 134, 183, 89, 137, 98, 101, 106, 165, 148}, 8707 { /* left mode 1 */ 72, 187, 100, 130, 157, 111, 32, 75, 80}, 8708 { /* left mode 2 */ 66, 102, 167, 99, 74, 62, 40, 234, 128}, 8709 { /* left mode 3 */ 41, 53, 9, 178, 241, 141, 26, 8, 107}, 8710 { /* left mode 4 */ 104, 79, 12, 27, 217, 255, 87, 17, 7}, 8711 { /* left mode 5 */ 74, 43, 26, 146, 73, 166, 49, 23, 157}, 8712 { /* left mode 6 */ 65, 38, 105, 160, 51, 52, 31, 115, 128}, 8713 { /* left mode 7 */ 87, 68, 71, 44, 114, 51, 15, 186, 23}, 8714 { /* left mode 8 */ 47, 41, 14, 110, 182, 183, 21, 17, 194}, 8715 { /* left mode 9 */ 66, 45, 25, 102, 197, 189, 23, 18, 22} 8716 }, 8717 { /* above mode 2 */ 8718 { /* left mode 0 */ 88, 88, 147, 150, 42, 46, 45, 196, 205}, 8719 { /* left mode 1 */ 43, 97, 183, 117, 85, 38, 35, 179, 61}, 8720 { /* left mode 2 */ 39, 53, 200, 87, 26, 21, 43, 232, 171}, 8721 { /* left mode 3 */ 56, 34, 51, 104, 114, 102, 29, 93, 77}, 8722 { /* left mode 4 */ 107, 54, 32, 26, 51, 1, 81, 43, 31}, 8723 { /* left mode 5 */ 39, 28, 85, 171, 58, 165, 90, 98, 64}, 8724 { /* left mode 6 */ 34, 22, 116, 206, 23, 34, 43, 166, 73}, 8725 { /* left mode 7 */ 68, 25, 106, 22, 64, 171, 36, 225, 114}, 8726 { /* left mode 8 */ 34, 19, 21, 102, 132, 188, 16, 76, 124}, 8727 { /* left mode 9 */ 62, 18, 78, 95, 85, 57, 50, 48, 51} 8728 }, 8729 { /* above mode 3 */ 8730 { /* left mode 0 */ 193, 101, 35, 159, 215, 111, 89, 46, 111}, 8731 { /* left mode 1 */ 60, 148, 31, 172, 219, 228, 21, 18, 111}, 8732 { /* left mode 2 */ 112, 113, 77, 85, 179, 255, 38, 120, 114}, 8733 { /* left mode 3 */ 40, 42, 1, 196, 245, 209, 10, 25, 109}, 8734 { /* left mode 4 */ 100, 80, 8, 43, 154, 1, 51, 26, 71}, 8735 { /* left mode 5 */ 88, 43, 29, 140, 166, 213, 37, 43, 154}, 8736 { /* left mode 6 */ 61, 63, 30, 155, 67, 45, 68, 1, 209}, 8737 { /* left mode 7 */ 142, 78, 78, 16, 255, 128, 34, 197, 171}, 8738 { /* left mode 8 */ 41, 40, 5, 102, 211, 183, 4, 1, 221}, 8739 { /* left mode 9 */ 51, 50, 17, 168, 209, 192, 23, 25, 82} 8740 }, 8741 { /* above mode 4 */ 8742 { /* left mode 0 */ 125, 98, 42, 88, 104, 85, 117, 175, 82}, 8743 { /* left mode 1 */ 95, 84, 53, 89, 128, 100, 113, 101, 45}, 8744 { /* left mode 2 */ 75, 79, 123, 47, 51, 128, 81, 171, 1}, 8745 { /* left mode 3 */ 57, 17, 5, 71, 102, 57, 53, 41, 49}, 8746 { /* left mode 4 */ 115, 21, 2, 10, 102, 255, 166, 23, 6}, 8747 { /* left mode 5 */ 38, 33, 13, 121, 57, 73, 26, 1, 85}, 8748 { /* left mode 6 */ 41, 10, 67, 138, 77, 110, 90, 47, 114}, 8749 { /* left mode 7 */ 101, 29, 16, 10, 85, 128, 101, 196, 26}, 8750 { /* left mode 8 */ 57, 18, 10, 102, 102, 213, 34, 20, 43}, 8751 { /* left mode 9 */ 117, 20, 15, 36, 163, 128, 68, 1, 26} 8752 }, 8753 { /* above mode 5 */ 8754 { /* left mode 0 */ 138, 31, 36, 171, 27, 166, 38, 44, 229}, 8755 { /* left mode 1 */ 67, 87, 58, 169, 82, 115, 26, 59, 179}, 8756 { /* left mode 2 */ 63, 59, 90, 180, 59, 166, 93, 73, 154}, 8757 { /* left mode 3 */ 40, 40, 21, 116, 143, 209, 34, 39, 175}, 8758 { /* left mode 4 */ 57, 46, 22, 24, 128, 1, 54, 17, 37}, 8759 { /* left mode 5 */ 47, 15, 16, 183, 34, 223, 49, 45, 183}, 8760 { /* left mode 6 */ 46, 17, 33, 183, 6, 98, 15, 32, 183}, 8761 { /* left mode 7 */ 65, 32, 73, 115, 28, 128, 23, 128, 205}, 8762 { /* left mode 8 */ 40, 3, 9, 115, 51, 192, 18, 6, 223}, 8763 { /* left mode 9 */ 87, 37, 9, 115, 59, 77, 64, 21, 47} 8764 }, 8765 { /* above mode 6 */ 8766 { /* left mode 0 */ 104, 55, 44, 218, 9, 54, 53, 130, 226}, 8767 { /* left mode 1 */ 64, 90, 70, 205, 40, 41, 23, 26, 57}, 8768 { /* left mode 2 */ 54, 57, 112, 184, 5, 41, 38, 166, 213}, 8769 { /* left mode 3 */ 30, 34, 26, 133, 152, 116, 10, 32, 134}, 8770 { /* left mode 4 */ 75, 32, 12, 51, 192, 255, 160, 43, 51}, 8771 { /* left mode 5 */ 39, 19, 53, 221, 26, 114, 32, 73, 255}, 8772 { /* left mode 6 */ 31, 9, 65, 234, 2, 15, 1, 118, 73}, 8773 { /* left mode 7 */ 88, 31, 35, 67, 102, 85, 55, 186, 85}, 8774 { /* left mode 8 */ 56, 21, 23, 111, 59, 205, 45, 37, 192}, 8775 { /* left mode 9 */ 55, 38, 70, 124, 73, 102, 1, 34, 98} 8776 }, 8777 { /* above mode 7 */ 8778 { /* left mode 0 */ 102, 61, 71, 37, 34, 53, 31, 243, 192}, 8779 { /* left mode 1 */ 69, 60, 71, 38, 73, 119, 28, 222, 37}, 8780 { /* left mode 2 */ 68, 45, 128, 34, 1, 47, 11, 245, 171}, 8781 { /* left mode 3 */ 62, 17, 19, 70, 146, 85, 55, 62, 70}, 8782 { /* left mode 4 */ 75, 15, 9, 9, 64, 255, 184, 119, 16}, 8783 { /* left mode 5 */ 37, 43, 37, 154, 100, 163, 85, 160, 1}, 8784 { /* left mode 6 */ 63, 9, 92, 136, 28, 64, 32, 201, 85}, 8785 { /* left mode 7 */ 86, 6, 28, 5, 64, 255, 25, 248, 1}, 8786 { /* left mode 8 */ 56, 8, 17, 132, 137, 255, 55, 116, 128}, 8787 { /* left mode 9 */ 58, 15, 20, 82, 135, 57, 26, 121, 40} 8788 }, 8789 { /* above mode 8 */ 8790 { /* left mode 0 */ 164, 50, 31, 137, 154, 133, 25, 35, 218}, 8791 { /* left mode 1 */ 51, 103, 44, 131, 131, 123, 31, 6, 158}, 8792 { /* left mode 2 */ 86, 40, 64, 135, 148, 224, 45, 183, 128}, 8793 { /* left mode 3 */ 22, 26, 17, 131, 240, 154, 14, 1, 209}, 8794 { /* left mode 4 */ 83, 12, 13, 54, 192, 255, 68, 47, 28}, 8795 { /* left mode 5 */ 45, 16, 21, 91, 64, 222, 7, 1, 197}, 8796 { /* left mode 6 */ 56, 21, 39, 155, 60, 138, 23, 102, 213}, 8797 { /* left mode 7 */ 85, 26, 85, 85, 128, 128, 32, 146, 171}, 8798 { /* left mode 8 */ 18, 11, 7, 63, 144, 171, 4, 4, 246}, 8799 { /* left mode 9 */ 35, 27, 10, 146, 174, 171, 12, 26, 128} 8800 }, 8801 { /* above mode 9 */ 8802 { /* left mode 0 */ 190, 80, 35, 99, 180, 80, 126, 54, 45}, 8803 { /* left mode 1 */ 85, 126, 47, 87, 176, 51, 41, 20, 32}, 8804 { /* left mode 2 */ 101, 75, 128, 139, 118, 146, 116, 128, 85}, 8805 { /* left mode 3 */ 56, 41, 15, 176, 236, 85, 37, 9, 62}, 8806 { /* left mode 4 */ 146, 36, 19, 30, 171, 255, 97, 27, 20}, 8807 { /* left mode 5 */ 71, 30, 17, 119, 118, 255, 17, 18, 138}, 8808 { /* left mode 6 */ 101, 38, 60, 138, 55, 70, 43, 26, 142}, 8809 { /* left mode 7 */ 138, 45, 61, 62, 219, 1, 81, 188, 64}, 8810 { /* left mode 8 */ 32, 41, 20, 117, 151, 142, 20, 21, 163}, 8811 { /* left mode 9 */ 112, 19, 12, 61, 195, 128, 48, 4, 24} 8812 } 8813 }; 8814 static const int kf_y_mode_tree[] = 8815 { 8816 -B_PRED, 2, 8817 4, 6, 8818 -DC_PRED, -V_PRED, 8819 -H_PRED, -TM_PRED 8820 }; 8821 static const int y_mode_tree[] = 8822 { 8823 -DC_PRED, 2, 8824 4, 6, 8825 -V_PRED, -H_PRED, 8826 -TM_PRED, -B_PRED 8827 }; 8828 static const int uv_mode_tree[6] = 8829 { 8830 -DC_PRED, 2, 8831 -V_PRED, 4, 8832 -H_PRED, -TM_PRED 8833 }; 8834 static const int b_mode_tree[18] = 8835 { 8836 -B_DC_PRED, 2, /* 0 = DC_NODE */ 8837 -B_TM_PRED, 4, /* 1 = TM_NODE */ 8838 -B_VE_PRED, 6, /* 2 = VE_NODE */ 8839 8, 12, /* 3 = COM_NODE */ 8840 -B_HE_PRED, 10, /* 4 = HE_NODE */ 8841 -B_RD_PRED, -B_VR_PRED, /* 5 = RD_NODE */ 8842 -B_LD_PRED, 14, /* 6 = LD_NODE */ 8843 -B_VL_PRED, 16, /* 7 = VL_NODE */ 8844 -B_HD_PRED, -B_HU_PRED /* 8 = HD_NODE */ 8845 }; 8846 static const int small_mv_tree[14] = 8847 { 8848 2, 8, 8849 4, 6, 8850 -0, -1, 8851 -2, -3, 8852 10, 12, 8853 -4, -5, 8854 -6, -7 8855 }; 8856 static const int mv_ref_tree[8] = 8857 { 8858 -ZEROMV, 2, 8859 -NEARESTMV, 4, 8860 -NEARMV, 6, 8861 -NEWMV, -SPLITMV 8862 }; 8863 static const int submv_ref_tree[6] = 8864 { 8865 -LEFT4X4, 2, 8866 -ABOVE4X4, 4, 8867 -ZERO4X4, -NEW4X4 8868 }; 8869 static const int split_mv_tree[6] = 8870 { 8871 -3, 2, 8872 -2, 4, 8873 -0, -1 8874 }; 8875 static const unsigned char default_b_mode_probs[] = 8876 { 120, 90, 79, 133, 87, 85, 80, 111, 151}; 8877 static const unsigned char mv_counts_to_probs[6][4] = 8878 { 8879 { 7, 1, 1, 143 }, 8880 { 14, 18, 14, 107 }, 8881 { 135, 64, 57, 68 }, 8882 { 60, 56, 128, 65 }, 8883 { 159, 134, 128, 34 }, 8884 { 234, 188, 128, 28 } 8886 }; 8887 static const unsigned char split_mv_probs[3] = 8888 { 110, 111, 150}; 8889 static const unsigned char submv_ref_probs2[5][3] = 8890 { 8891 { 147, 136, 18 }, 8892 { 106, 145, 1 }, 8893 { 179, 121, 1 }, 8894 { 223, 1, 34 }, 8895 { 208, 1, 1 } 8896 }; 8898 const static int mv_partitions[4][16] = 8899 { 8900 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, 8901 {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1 }, 8902 {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3 }, 8903 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } 8904 }; 8906 ---- End code block ---------------------------------------- 8908 21.14. predict.c 8910 ---- Begin code block -------------------------------------- 8912 /* 8913 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8914 * 8915 * Use of this source code is governed by a BSD-style license 8916 * that can be found in the LICENSE file in the root of the source 8917 * tree. An additional intellectual property rights grant can be 8918 * found in the file PATENTS. All contributing project authors may 8919 * be found in the AUTHORS file in the root of the source tree. 8920 */ 8921 #include "dixie.h" 8922 #include "predict.h" 8923 #include "idct_add.h" 8924 #include "mem.h" 8925 #include 8926 #include 8928 enum 8929 { 8930 BORDER_PIXELS = 16, 8931 }; 8933 static const filter_t sixtap_filters[8] = 8934 { 8935 { 0, 0, 128, 0, 0, 0 }, 8936 { 0, -6, 123, 12, -1, 0 }, 8937 { 2, -11, 108, 36, -8, 1 }, 8938 { 0, -9, 93, 50, -6, 0 }, 8939 { 3, -16, 77, 77, -16, 3 }, 8940 { 0, -6, 50, 93, -9, 0 }, 8941 { 1, -8, 36, 108, -11, 2 }, 8942 { 0, -1, 12, 123, -6, 0 }, 8943 }; 8945 static const filter_t bilinear_filters[8] = 8946 { 8948 { 0, 0, 128, 0, 0, 0 }, 8949 { 0, 0, 112, 16, 0, 0 }, 8950 { 0, 0, 96, 32, 0, 0 }, 8951 { 0, 0, 80, 48, 0, 0 }, 8952 { 0, 0, 64, 64, 0, 0 }, 8953 { 0, 0, 48, 80, 0, 0 }, 8954 { 0, 0, 32, 96, 0, 0 }, 8955 { 0, 0, 16, 112, 0, 0 } 8956 }; 8958 static void 8959 predict_h_nxn(unsigned char *predict, 8960 int stride, 8961 int n) 8962 { 8963 unsigned char *left = predict - 1; 8964 int i, j; 8966 for (i = 0; i < n; i++) 8967 for (j = 0; j < n; j++) 8968 predict[i *stride + j] = left[i * stride]; 8969 } 8971 static void 8972 predict_v_nxn(unsigned char *predict, 8973 int stride, 8974 int n) 8975 { 8976 unsigned char *above = predict - stride; 8977 int i, j; 8979 for (i = 0; i < n; i++) 8980 for (j = 0; j < n; j++) 8981 predict[i *stride + j] = above[j]; 8982 } 8984 static void 8985 predict_tm_nxn(unsigned char *predict, 8986 int stride, 8987 int n) 8988 { 8989 /* Transposes the left column to the top row for later consumption 8990 * by the idct/recon stage 8991 */ 8992 unsigned char *left = predict - 1; 8993 unsigned char *above = predict - stride; 8994 unsigned char p = above[-1]; 8995 int i, j; 8997 for (j = 0; j < n; j++) 8998 { 8999 for (i = 0; i < n; i++) 9000 predict[i] = CLAMP_255(*left + above[i] - p); 9002 predict += stride; 9003 left += stride; 9004 } 9005 } 9007 static void 9008 predict_dc_nxn(unsigned char *predict, 9009 int stride, 9010 int n) 9011 { 9012 unsigned char *left = predict - 1; 9013 unsigned char *above = predict - stride; 9014 int i, j, dc = 0; 9016 for (i = 0; i < n; i++) 9017 { 9018 dc += *left + above[i]; 9019 left += stride; 9020 } 9022 switch (n) 9023 { 9024 case 16: 9025 dc = (dc + 16) >> 5; 9026 break; 9028 case 8: 9029 dc = (dc + 8) >> 4; 9030 break; 9031 case 4: 9032 dc = (dc + 4) >> 3; 9033 break; 9034 } 9036 for (i = 0; i < n; i++) 9037 for (j = 0; j < n; j++) 9038 predict[i *stride + j] = dc; 9039 } 9041 static void 9042 predict_ve_4x4(unsigned char *predict, 9043 int stride) 9044 { 9045 unsigned char *above = predict - stride; 9046 int i, j; 9048 predict[0] = (above[-1] + 2 * above[0] + above[1] + 2) >> 2; 9049 predict[1] = (above[ 0] + 2 * above[1] + above[2] + 2) >> 2; 9050 predict[2] = (above[ 1] + 2 * above[2] + above[3] + 2) >> 2; 9051 predict[3] = (above[ 2] + 2 * above[3] + above[4] + 2) >> 2; 9053 for (i = 1; i < 4; i++) 9054 for (j = 0; j < 4; j++) 9055 predict[i *stride + j] = predict[j]; 9056 } 9058 static void 9059 predict_he_4x4(unsigned char *predict, 9060 int stride) 9061 { 9062 unsigned char *left = predict - 1; 9064 predict[0] = 9065 predict[1] = 9066 predict[2] = 9067 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9068 predict += stride; 9069 left += stride; 9071 predict[0] = 9072 predict[1] = 9073 predict[2] = 9074 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9075 predict += stride; 9076 left += stride; 9078 predict[0] = 9079 predict[1] = 9080 predict[2] = 9081 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9082 predict += stride; 9083 left += stride; 9085 predict[0] = 9086 predict[1] = 9087 predict[2] = 9088 predict[3] = (left[-stride] + 2 * left[0] + left[0] + 2) >> 2; 9089 } 9091 static void 9092 predict_ld_4x4(unsigned char *predict, 9093 int stride) 9094 { 9095 unsigned char *above = predict - stride; 9096 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9098 predict[0] = pred0 = (above[0] + 2 * above[1] + above[2] + 2) >> 2; 9099 predict[1] = pred1 = (above[1] + 2 * above[2] + above[3] + 2) >> 2; 9100 predict[2] = pred2 = (above[2] + 2 * above[3] + above[4] + 2) >> 2; 9101 predict[3] = pred3 = (above[3] + 2 * above[4] + above[5] + 2) >> 2; 9102 predict += stride; 9104 predict[0] = pred1; 9105 predict[1] = pred2; 9106 predict[2] = pred3; 9107 predict[3] = pred4 = (above[4] + 2 * above[5] + above[6] + 2) >> 2; 9108 predict += stride; 9110 predict[0] = pred2; 9111 predict[1] = pred3; 9112 predict[2] = pred4; 9113 predict[3] = pred5 = (above[5] + 2 * above[6] + above[7] + 2) >> 2; 9114 predict += stride; 9116 predict[0] = pred3; 9117 predict[1] = pred4; 9118 predict[2] = pred5; 9119 predict[3] = pred6 = (above[6] + 2 * above[7] + above[7] + 2) >> 2; 9120 } 9121 static void 9122 predict_rd_4x4(unsigned char *predict, 9123 int stride) 9124 { 9125 unsigned char *left = predict - 1; 9126 unsigned char *above = predict - stride; 9127 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9129 predict[0] = pred0 = 9130 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9131 predict[1] = pred1 = 9132 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9133 predict[2] = pred2 = 9134 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9135 predict[3] = pred3 = 9136 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9137 predict += stride; 9139 predict[0] = pred4 = 9140 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9141 predict[1] = pred0; 9142 predict[2] = pred1; 9143 predict[3] = pred2; 9144 predict += stride; 9146 predict[0] = pred5 = 9147 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9148 predict[1] = pred4; 9149 predict[2] = pred0; 9150 predict[3] = pred1; 9151 predict += stride; 9153 predict[0] = pred6 = 9154 (left[stride*3] + 2 * left[stride*2] + left[stride] + 2) >> 2; 9155 predict[1] = pred5; 9156 predict[2] = pred4; 9157 predict[3] = pred0; 9158 } 9160 static void 9161 predict_vr_4x4(unsigned char *predict, 9162 int stride) 9163 { 9164 unsigned char *left = predict - 1; 9165 unsigned char *above = predict - stride; 9166 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9167 pred7, pred8, pred9; 9169 predict[0] = pred0 = (above[-1] + above[0] + 1) >> 1; 9170 predict[1] = pred1 = (above[ 0] + above[1] + 1) >> 1; 9171 predict[2] = pred2 = (above[ 1] + above[2] + 1) >> 1; 9172 predict[3] = pred3 = (above[ 2] + above[3] + 1) >> 1; 9173 predict += stride; 9175 predict[0] = pred4 = 9176 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9177 predict[1] = pred5 = 9178 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9179 predict[2] = pred6 = 9180 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9181 predict[3] = pred7 = 9182 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9183 predict += stride; 9185 predict[0] = pred8 = 9186 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9187 predict[1] = pred0; 9188 predict[2] = pred1; 9189 predict[3] = pred2; 9190 predict += stride; 9192 predict[0] = pred9 = 9193 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9194 predict[1] = pred4; 9195 predict[2] = pred5; 9196 predict[3] = pred6; 9197 } 9199 static void 9200 predict_vl_4x4(unsigned char *predict, 9201 int stride) 9202 { 9203 unsigned char *above = predict - stride; 9204 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9205 pred7, pred8, pred9; 9207 predict[0] = pred0 = (above[0] + above[1] + 1) >> 1; 9208 predict[1] = pred1 = (above[1] + above[2] + 1) >> 1; 9209 predict[2] = pred2 = (above[2] + above[3] + 1) >> 1; 9210 predict[3] = pred3 = (above[3] + above[4] + 1) >> 1; 9211 predict += stride; 9213 predict[0] = pred4 = 9214 (above[0] + 2 * above[1] + above[2] + 2) >> 2; 9215 predict[1] = pred5 = 9216 (above[1] + 2 * above[2] + above[3] + 2) >> 2; 9217 predict[2] = pred6 = 9218 (above[2] + 2 * above[3] + above[4] + 2) >> 2; 9219 predict[3] = pred7 = 9220 (above[3] + 2 * above[4] + above[5] + 2) >> 2; 9221 predict += stride; 9223 predict[0] = pred1; 9224 predict[1] = pred2; 9225 predict[2] = pred3; 9226 predict[3] = pred8 = (above[4] + 2 * above[5] + above[6] + 2) >> 2; 9227 predict += stride; 9229 predict[0] = pred5; 9230 predict[1] = pred6; 9231 predict[2] = pred7; 9232 predict[3] = pred9 = (above[5] + 2 * above[6] + above[7] + 2) >> 2; 9233 } 9235 static void 9236 predict_hd_4x4(unsigned char *predict, 9237 int stride) 9238 { 9239 unsigned char *left = predict - 1; 9240 unsigned char *above = predict - stride; 9241 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9242 pred7, pred8, pred9; 9244 predict[0] = pred0 = 9245 (left[ 0] + above[-1] + 1) >> 1; 9246 predict[1] = pred1 = 9247 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9248 predict[2] = pred2 = 9249 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9250 predict[3] = pred3 = 9251 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9252 predict += stride; 9254 predict[0] = pred4 = 9255 (left[stride] + left[0] + 1) >> 1; 9256 predict[1] = pred5 = 9257 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9258 predict[2] = pred0; 9259 predict[3] = pred1; 9260 predict += stride; 9262 predict[0] = pred6 = 9263 (left[stride*2] + left[stride] + 1) >> 1; 9264 predict[1] = pred7 = 9265 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9266 predict[2] = pred4; 9267 predict[3] = pred5; 9268 predict += stride; 9270 predict[0] = pred8 = 9271 (left[stride*3] + left[stride*2] + 1) >> 1; 9272 predict[1] = pred9 = 9273 (left[stride*3] + 2 * left[stride*2] + left[stride] + 2) >> 2; 9274 predict[2] = pred6; 9275 predict[3] = pred7; 9276 } 9278 static void 9279 predict_hu_4x4(unsigned char *predict, 9280 int stride) 9281 { 9282 unsigned char *left = predict - 1; 9283 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9285 predict[0] = pred0 = 9286 (left[stride*0] + left[stride*1] + 1) >> 1; 9287 predict[1] = pred1 = 9288 (left[stride*0] + 2 * left[stride*1] + left[stride*2] + 2) >> 2; 9289 predict[2] = pred2 = 9290 (left[stride*1] + left[stride*2] + 1) >> 1; 9291 predict[3] = pred3 = 9292 (left[stride*1] + 2 * left[stride*2] + left[stride*3] + 2) >> 2; 9293 predict += stride; 9295 predict[0] = pred2; 9296 predict[1] = pred3; 9297 predict[2] = pred4 = 9298 (left[stride*2] + left[stride*3] + 1) >> 1; 9299 predict[3] = pred5 = 9300 (left[stride*2] + 2 * left[stride*3] + left[stride*3] + 2) >> 2; 9301 predict += stride; 9303 predict[0] = pred4; 9304 predict[1] = pred5; 9305 predict[2] = pred6 = left[stride*3]; 9306 predict[3] = pred6; 9307 predict += stride; 9309 predict[0] = pred6; 9310 predict[1] = pred6; 9311 predict[2] = pred6; 9312 predict[3] = pred6; 9313 } 9315 static void 9316 predict_h_16x16(unsigned char *predict, int stride) 9317 { 9318 predict_h_nxn(predict, stride, 16); 9319 } 9321 static void 9322 predict_v_16x16(unsigned char *predict, int stride) 9323 { 9324 predict_v_nxn(predict, stride, 16); 9325 } 9327 static void 9328 predict_tm_16x16(unsigned char *predict, int stride) 9329 { 9330 predict_tm_nxn(predict, stride, 16); 9331 } 9333 static void 9334 predict_h_8x8(unsigned char *predict, int stride) 9335 { 9336 predict_h_nxn(predict, stride, 8); 9337 } 9339 static void 9340 predict_v_8x8(unsigned char *predict, int stride) 9341 { 9342 predict_v_nxn(predict, stride, 8); 9343 } 9345 static void 9346 predict_tm_8x8(unsigned char *predict, int stride) 9347 { 9348 predict_tm_nxn(predict, stride, 8); 9349 } 9350 static void 9351 predict_tm_4x4(unsigned char *predict, int stride) 9352 { 9353 predict_tm_nxn(predict, stride, 4); 9354 } 9356 static void 9357 copy_down(unsigned char *recon, 9358 int stride) 9359 { 9360 /* Copy the four pixels above-right of subblock 3 to 9361 * above-right of subblocks 7, 11, and 15 9362 */ 9363 uint32_t tmp, *copy = (void *)(recon + 16 - stride); 9365 stride = stride / sizeof(unsigned int); 9366 tmp = *copy; 9367 copy += stride * 4; 9368 *copy = tmp; 9369 copy += stride * 4; 9370 *copy = tmp; 9371 copy += stride * 4; 9372 *copy = tmp; 9373 } 9375 static void 9376 b_pred(unsigned char *predict, 9377 int stride, 9378 struct mb_info *mbi, 9379 short *coeffs) 9380 { 9381 int i; 9383 copy_down(predict, stride); 9385 for (i = 0; i < 16; i++) 9386 { 9387 unsigned char *b_predict = predict + (i & 3) * 4; 9389 switch (mbi->split.modes[i]) 9390 { 9391 case B_DC_PRED: 9392 predict_dc_nxn(b_predict, stride, 4); 9393 break; 9394 case B_TM_PRED: 9395 predict_tm_4x4(b_predict, stride); 9396 break; 9397 case B_VE_PRED: 9398 predict_ve_4x4(b_predict, stride); 9399 break; 9400 case B_HE_PRED: 9401 predict_he_4x4(b_predict, stride); 9402 break; 9403 case B_LD_PRED: 9404 predict_ld_4x4(b_predict, stride); 9405 break; 9406 case B_RD_PRED: 9407 predict_rd_4x4(b_predict, stride); 9408 break; 9409 case B_VR_PRED: 9410 predict_vr_4x4(b_predict, stride); 9411 break; 9412 case B_VL_PRED: 9413 predict_vl_4x4(b_predict, stride); 9414 break; 9415 case B_HD_PRED: 9416 predict_hd_4x4(b_predict, stride); 9417 break; 9418 case B_HU_PRED: 9419 predict_hu_4x4(b_predict, stride); 9420 break; 9421 default: 9422 assert(0); 9423 } 9425 vp8_dixie_idct_add(b_predict, b_predict, stride, coeffs); 9426 coeffs += 16; 9428 if ((i & 3) == 3) 9429 { 9430 predict += stride * 4; 9431 } 9432 } 9433 } 9435 static void 9436 fixup_dc_coeffs(struct mb_info *mbi, 9437 short *coeffs) 9438 { 9439 short y2[16]; 9440 int i; 9442 vp8_dixie_walsh(coeffs + 24 * 16, y2); 9443 for (i = 0; i < 16; i++) 9444 coeffs[i*16] = y2[i]; 9445 } 9447 static void 9448 predict_intra_luma(unsigned char *predict, 9449 int stride, 9450 struct mb_info *mbi, 9451 short *coeffs) 9452 { 9453 if (mbi->base.y_mode == B_PRED) 9454 b_pred(predict, stride, mbi, coeffs); 9455 else 9456 { 9457 int i; 9459 switch (mbi->base.y_mode) 9460 { 9461 case DC_PRED: 9462 predict_dc_nxn(predict, stride, 16); 9463 break; 9464 case V_PRED: 9465 predict_v_16x16(predict, stride); 9466 break; 9467 case H_PRED: 9468 predict_h_16x16(predict, stride); 9469 break; 9470 case TM_PRED: 9471 predict_tm_16x16(predict, stride); 9472 break; 9473 default: 9474 assert(0); 9475 } 9477 fixup_dc_coeffs(mbi, coeffs); 9479 for (i = 0; i < 16; i++) 9480 { 9481 vp8_dixie_idct_add(predict, predict, stride, coeffs); 9482 coeffs += 16; 9483 predict += 4; 9485 if ((i & 3) == 3) 9486 predict += stride * 4 - 16; 9487 } 9489 } 9491 } 9493 static void 9494 predict_intra_chroma(unsigned char *predict_u, 9495 unsigned char *predict_v, 9496 int stride, 9497 struct mb_info *mbi, 9498 short *coeffs) 9499 { 9500 int i; 9502 switch (mbi->base.uv_mode) 9503 { 9504 case DC_PRED: 9505 predict_dc_nxn(predict_u, stride, 8); 9506 predict_dc_nxn(predict_v, stride, 8); 9507 break; 9508 case V_PRED: 9509 predict_v_8x8(predict_u, stride); 9510 predict_v_8x8(predict_v, stride); 9511 break; 9512 case H_PRED: 9513 predict_h_8x8(predict_u, stride); 9514 predict_h_8x8(predict_v, stride); 9515 break; 9516 case TM_PRED: 9517 predict_tm_8x8(predict_u, stride); 9518 predict_tm_8x8(predict_v, stride); 9519 break; 9520 default: 9521 assert(0); 9522 } 9524 coeffs += 16 * 16; 9526 for (i = 16; i < 20; i++) 9527 { 9528 vp8_dixie_idct_add(predict_u, predict_u, stride, coeffs); 9529 coeffs += 16; 9530 predict_u += 4; 9532 if (i & 1) 9533 predict_u += stride * 4 - 8; 9534 } 9536 for (i = 20; i < 24; i++) 9537 { 9538 vp8_dixie_idct_add(predict_v, predict_v, stride, coeffs); 9539 coeffs += 16; 9540 predict_v += 4; 9542 if (i & 1) 9543 predict_v += stride * 4 - 8; 9544 } 9546 } 9548 static void 9549 sixtap_horiz(unsigned char *output, 9550 int output_stride, 9551 const unsigned char *reference, 9552 int reference_stride, 9553 int cols, 9554 int rows, 9555 const filter_t filter 9556 ) 9557 { 9558 int r, c, temp; 9560 for (r = 0; r < rows; r++) 9561 { 9562 for (c = 0; c < cols; c++) 9563 { 9564 temp = (reference[-2] * filter[0]) + 9565 (reference[-1] * filter[1]) + 9566 (reference[ 0] * filter[2]) + 9567 (reference[ 1] * filter[3]) + 9568 (reference[ 2] * filter[4]) + 9569 (reference[ 3] * filter[5]) + 9570 64; 9571 temp >>= 7; 9572 output[c] = CLAMP_255(temp); 9573 reference++; 9574 } 9576 reference += reference_stride - cols; 9577 output += output_stride; 9578 } 9579 } 9581 static void 9582 sixtap_vert(unsigned char *output, 9583 int output_stride, 9584 const unsigned char *reference, 9585 int reference_stride, 9586 int cols, 9587 int rows, 9588 const filter_t filter 9589 ) 9590 { 9591 int r, c, temp; 9593 for (r = 0; r < rows; r++) 9594 { 9595 for (c = 0; c < cols; c++) 9596 { 9597 temp = (reference[-2*reference_stride] * filter[0]) + 9598 (reference[-1*reference_stride] * filter[1]) + 9599 (reference[ 0*reference_stride] * filter[2]) + 9600 (reference[ 1*reference_stride] * filter[3]) + 9601 (reference[ 2*reference_stride] * filter[4]) + 9602 (reference[ 3*reference_stride] * filter[5]) + 9603 64; 9604 temp >>= 7; 9605 output[c] = CLAMP_255(temp); 9606 reference++; 9607 } 9609 reference += reference_stride - cols; 9610 output += output_stride; 9611 } 9612 } 9614 static void 9615 sixtap_2d(unsigned char *output, 9616 int output_stride, 9617 const unsigned char *reference, 9618 int reference_stride, 9619 int cols, 9620 int rows, 9621 int mx, 9622 int my, 9623 const filter_t filters[8] 9624 ) 9625 { 9626 DECLARE_ALIGNED(16, unsigned char, temp[16*(16+5)]); 9628 sixtap_horiz(temp, 16, 9629 reference - 2 * reference_stride, reference_stride, 9630 cols, rows + 5, filters[mx]); 9632 sixtap_vert(output, output_stride, 9633 temp + 2 * 16, 16, 9634 cols, rows, filters[my]); 9635 } 9637 struct img_index 9638 { 9639 unsigned char *y, *u, *v; 9640 int stride, uv_stride; 9641 }; 9643 static const unsigned char * 9644 filter_block(unsigned char *output, 9645 const unsigned char *reference, 9646 int stride, 9647 const union mv *mv, 9648 const filter_t filters[8]) 9649 { 9650 int mx, my; 9652 /* Handle 0,0 as a special case. TODO: does this make it any 9653 * faster? 9654 */ 9655 if (!mv->raw) 9656 return reference; 9658 mx = mv->d.x & 7; 9659 my = mv->d.y & 7; 9660 reference += ((mv->d.y >> 3) * stride) + (mv->d.x >> 3); 9662 if (mx | my) 9663 { 9664 sixtap_2d(output, stride, reference, stride, 4, 4, mx, my, 9665 filters); 9666 reference = output; 9667 } 9669 return reference; 9670 } 9672 static void 9673 recon_1_block(unsigned char *output, 9674 const unsigned char *reference, 9675 int stride, 9676 const union mv *mv, 9677 const filter_t filters[8], 9678 short *coeffs, 9679 struct mb_info *mbi, 9680 int b 9681 ) 9682 { 9683 const unsigned char *predict; 9685 predict = filter_block(output, reference, stride, mv, filters); 9686 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9687 } 9689 static mv_t 9690 calculate_chroma_splitmv(struct mb_info *mbi, 9691 int b, 9692 int full_pixel) 9693 { 9694 int temp; 9695 union mv mv; 9697 temp = mbi->split.mvs[b].d.x + 9698 mbi->split.mvs[b+1].d.x + 9699 mbi->split.mvs[b+4].d.x + 9700 mbi->split.mvs[b+5].d.x; 9702 if (temp < 0) 9703 temp -= 4; 9704 else 9705 temp += 4; 9707 mv.d.x = temp / 8; 9709 temp = mbi->split.mvs[b].d.y + 9710 mbi->split.mvs[b+1].d.y + 9711 mbi->split.mvs[b+4].d.y + 9712 mbi->split.mvs[b+5].d.y; 9714 if (temp < 0) 9715 temp -= 4; 9716 else 9717 temp += 4; 9719 mv.d.y = temp / 8; 9721 if (full_pixel) 9722 { 9723 mv.d.x &= ~7; 9724 mv.d.y &= ~7; 9725 } 9727 return mv; 9728 } 9730 /* Note: We rely on the reconstructed border having the same stride as 9731 * the reference buffer because the filter_block can't adjust the 9732 * stride with its return value, only the reference pointer. 9733 */ 9734 static void 9735 build_mc_border(unsigned char *dst, 9736 const unsigned char *src, 9737 int stride, 9738 int x, 9739 int y, 9740 int b_w, 9741 int b_h, 9742 int w, 9743 int h 9744 ) 9745 { 9746 const unsigned char *ref_row; 9748 /* Get a pointer to the start of the real data for this row */ 9749 ref_row = src - x - y * stride; 9751 if (y >= h) 9752 ref_row += (h - 1) * stride; 9753 else if (y > 0) 9754 ref_row += y * stride; 9756 do 9757 { 9758 int left, right = 0, copy; 9760 left = x < 0 ? -x : 0; 9762 if (left > b_w) 9763 left = b_w; 9765 if (x + b_w > w) 9766 right = x + b_w - w; 9768 if (right > b_w) 9769 right = b_w; 9771 copy = b_w - left - right; 9773 if (left) 9774 memset(dst, ref_row[0], left); 9776 if (copy) 9777 memcpy(dst + left, ref_row + x + left, copy); 9779 if (right) 9780 memset(dst + left + copy, ref_row[w-1], right); 9782 dst += stride; 9783 y++; 9785 if (y < h && y > 0) 9786 ref_row += stride; 9787 } 9788 while (--b_h); 9789 } 9791 static void 9792 recon_1_edge_block(unsigned char *output, 9793 unsigned char *emul_block, 9794 const unsigned char *reference, 9795 int stride, 9796 const union mv *mv, 9797 const filter_t filters[8], 9798 short *coeffs, 9799 struct mb_info *mbi, 9800 int x, 9801 int y, 9802 int w, 9803 int h, 9804 int start_b 9805 ) 9806 { 9807 const unsigned char *predict; 9808 int b = start_b; 9809 const int b_w = 4; 9810 const int b_h = 4; 9812 x += mv->d.x >> 3; 9813 y += mv->d.y >> 3; 9815 /* Need two pixels left/above, 3 right/below for 6-tap */ 9816 if (x < 2 || x + b_w - 1 + 3 >= w || y < 2 || y + b_h - 1 + 3 >= h) 9817 { 9818 reference += (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9819 build_mc_border(emul_block, 9820 reference - 2 - 2 * stride, stride, 9821 x - 2, y - 2, b_w + 5, b_h + 5, w, h); 9822 reference = emul_block + 2 * stride + 2; 9823 reference -= (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9824 } 9826 predict = filter_block(output, reference, stride, mv, filters); 9827 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9828 } 9830 static void 9831 predict_inter_emulated_edge(struct vp8_decoder_ctx *ctx, 9832 struct img_index *img, 9833 short *coeffs, 9834 struct mb_info *mbi, 9835 int mb_col, 9836 int mb_row) 9837 { 9838 /* TODO: move this into its own buffer. This only works because we 9839 * still have a border allocated. 9840 */ 9841 unsigned char *emul_block = ctx->frame_strg[0].img.img_data; 9842 unsigned char *reference; 9843 unsigned char *output; 9844 ptrdiff_t reference_offset; 9845 int w, h, x, y, b; 9846 union mv chroma_mv[4]; 9847 unsigned char *u = img->u, *v = img->v; 9848 int full_pixel = ctx->frame_hdr.version == 3; 9850 x = mb_col * 16; 9851 y = mb_row * 16; 9852 w = ctx->mb_cols * 16; 9853 h = ctx->mb_rows * 16; 9854 output = img->y; 9855 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9856 reference = output + reference_offset; 9858 if (mbi->base.y_mode != SPLITMV) 9859 { 9860 union mv uvmv; 9862 uvmv = mbi->base.mv; 9863 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9864 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9866 if (full_pixel) 9867 { 9868 uvmv.d.x &= ~7; 9869 uvmv.d.y &= ~7; 9870 } 9872 chroma_mv[0] = uvmv; 9873 chroma_mv[1] = uvmv; 9874 chroma_mv[2] = uvmv; 9875 chroma_mv[3] = uvmv; 9876 } 9877 else 9878 { 9879 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9880 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9881 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9882 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9883 } 9885 /* Luma */ 9886 for (b = 0; b < 16; b++) 9887 { 9888 union mv *ymv; 9890 if (mbi->base.y_mode != SPLITMV) 9891 ymv = &mbi->base.mv; 9892 else 9893 ymv = mbi->split.mvs + b; 9895 recon_1_edge_block(output, emul_block, reference, img->stride, 9896 ymv, ctx->subpixel_filters, 9897 coeffs, mbi, x, y, w, h, b); 9899 x += 4; 9900 output += 4; 9901 reference += 4; 9903 if ((b & 3) == 3) 9904 { 9905 x -= 16; 9906 y += 4; 9907 output += 4 * img->stride - 16; 9908 reference += 4 * img->stride - 16; 9909 } 9910 } 9911 x = mb_col * 16; 9912 y = mb_row * 16; 9914 /* Chroma */ 9915 x >>= 1; 9916 y >>= 1; 9917 w >>= 1; 9918 h >>= 1; 9920 for (b = 0; b < 4; b++) 9921 { 9922 recon_1_edge_block(u, emul_block, u + reference_offset, 9923 img->uv_stride, 9924 &chroma_mv[b], ctx->subpixel_filters, 9925 coeffs, mbi, x, y, w, h, b + 16); 9926 recon_1_edge_block(v, emul_block, v + reference_offset, 9927 img->uv_stride, 9928 &chroma_mv[b], ctx->subpixel_filters, 9929 coeffs, mbi, x, y, w, h, b + 20); 9930 u += 4; 9931 v += 4; 9932 x += 4; 9934 if (b & 1) 9935 { 9936 x -= 8; 9937 y += 4; 9938 u += 4 * img->uv_stride - 8; 9939 v += 4 * img->uv_stride - 8; 9940 } 9941 } 9943 } 9945 static void 9946 predict_inter(struct vp8_decoder_ctx *ctx, 9947 struct img_index *img, 9948 short *coeffs, 9949 struct mb_info *mbi) 9950 { 9951 unsigned char *y = img->y; 9952 unsigned char *u = img->u; 9953 unsigned char *v = img->v; 9954 ptrdiff_t reference_offset; 9955 union mv chroma_mv[4]; 9956 int full_pixel = ctx->frame_hdr.version == 3; 9957 int b; 9958 if (mbi->base.y_mode != SPLITMV) 9959 { 9960 union mv uvmv; 9962 uvmv = mbi->base.mv; 9963 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9964 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9966 if (full_pixel) 9967 { 9968 uvmv.d.x &= ~7; 9969 uvmv.d.y &= ~7; 9970 } 9972 chroma_mv[0] = 9973 chroma_mv[1] = 9974 chroma_mv[2] = 9975 chroma_mv[3] = uvmv; 9976 } 9977 else 9978 { 9979 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9980 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9981 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9982 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9983 } 9985 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9987 for (b = 0; b < 16; b++) 9988 { 9989 union mv *ymv; 9991 if (mbi->base.y_mode != SPLITMV) 9992 ymv = &mbi->base.mv; 9993 else 9994 ymv = mbi->split.mvs + b; 9996 recon_1_block(y, y + reference_offset, img->stride, 9997 ymv, ctx->subpixel_filters, coeffs, mbi, b); 9998 y += 4; 10000 if ((b & 3) == 3) 10001 y += 4 * img->stride - 16; 10002 } 10004 for (b = 0; b < 4; b++) 10005 { 10006 recon_1_block(u, u + reference_offset, 10007 img->uv_stride, &chroma_mv[b], 10008 ctx->subpixel_filters, coeffs, mbi, b + 16); 10009 recon_1_block(v, v + reference_offset, 10010 img->uv_stride, &chroma_mv[b], 10011 ctx->subpixel_filters, coeffs, mbi, b + 20); 10012 u += 4; 10013 v += 4; 10015 if (b & 1) 10016 { 10017 u += 4 * img->uv_stride - 8; 10018 v += 4 * img->uv_stride - 8; 10019 } 10020 } 10021 } 10023 void 10024 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg) 10025 { 10026 if (rcimg) 10027 { 10028 assert(rcimg->ref_cnt); 10029 rcimg->ref_cnt--; 10030 } 10031 } 10033 struct ref_cnt_img * 10034 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg) 10035 { 10036 rcimg->ref_cnt++; 10037 return rcimg; 10038 } 10040 struct ref_cnt_img * 10041 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames) 10042 { 10043 int i; 10045 for (i = 0; i < NUM_REF_FRAMES; i++) 10046 if (frames[i].ref_cnt == 0) 10047 { 10048 frames[i].ref_cnt = 1; 10049 return &frames[i]; 10051 } 10053 assert(0); 10054 return NULL; 10055 } 10057 static void 10058 fixup_left(unsigned char *predict, 10059 int width, 10060 int stride, 10061 unsigned int row, 10062 enum prediction_mode mode) 10063 { 10064 /* The left column of out-of-frame pixels is taken to be 129, 10065 * unless we're doing DC_PRED, in which case we duplicate the 10066 * above row, unless this is also row 0, in which case we use 10067 * 129. 10068 */ 10069 unsigned char *left = predict - 1; 10070 int i; 10072 if (mode == DC_PRED && row) 10073 { 10074 unsigned char *above = predict - stride; 10076 for (i = 0; i < width; i++) 10077 { 10078 *left = above[i]; 10079 left += stride; 10080 } 10081 } 10082 else 10083 { 10084 /* Need to re-set the above row, in case the above MB was 10085 * DC_PRED. 10086 */ 10087 left -= stride; 10089 for (i = -1; i < width; i++) 10090 { 10091 *left = 129; 10092 left += stride; 10093 } 10094 } 10095 } 10096 static void 10097 fixup_above(unsigned char *predict, 10098 int width, 10099 int stride, 10100 unsigned int col, 10101 enum prediction_mode mode) 10102 { 10103 /* The above row of out-of-frame pixels is taken to be 127, 10104 * unless we're doing DC_PRED, in which case we duplicate the 10105 * left col, unless this is also col 0, in which case we use 10106 * 127. 10107 */ 10108 unsigned char *above = predict - stride; 10109 int i; 10111 if (mode == DC_PRED && col) 10112 { 10113 unsigned char *left = predict - 1; 10115 for (i = 0; i < width; i++) 10116 { 10117 above[i] = *left; 10118 left += stride; 10119 } 10120 } 10121 else 10122 /* Need to re-set the left col, in case the last MB was 10123 * DC_PRED. 10124 */ 10125 memset(above - 1, 127, width + 1); 10127 memset(above + width, 127, 4); // for above-right subblock modes 10128 } 10130 void 10131 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx) 10132 { 10134 int i; 10135 unsigned char *this_frame_base; 10137 if (ctx->frame_hdr.frame_size_updated) 10138 { 10139 for (i = 0; i < NUM_REF_FRAMES; i++) 10140 { 10141 unsigned int w = ctx->mb_cols * 16 + BORDER_PIXELS * 2; 10142 unsigned int h = ctx->mb_rows * 16 + BORDER_PIXELS * 2; 10143 vpx_img_free(&ctx->frame_strg[i].img); 10144 ctx->frame_strg[i].ref_cnt = 0; 10145 ctx->ref_frames[i] = NULL; 10147 if (!vpx_img_alloc(&ctx->frame_strg[i].img, 10148 IMG_FMT_I420, w, h, 16)) 10149 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10150 "Failed to allocate %dx%d" 10151 " framebuffer", 10152 w, h); 10154 vpx_img_set_rect(&ctx->frame_strg[i].img, 10155 BORDER_PIXELS, BORDER_PIXELS, 10156 ctx->frame_hdr.kf.w, ctx->frame_hdr.kf.h); 10158 } 10160 if (ctx->frame_hdr.version) 10161 ctx->subpixel_filters = bilinear_filters; 10162 else 10163 ctx->subpixel_filters = sixtap_filters; 10164 } 10166 /* Find a free framebuffer to predict into */ 10167 if (ctx->ref_frames[CURRENT_FRAME]) 10168 vp8_dixie_release_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 10170 ctx->ref_frames[CURRENT_FRAME] = 10171 vp8_dixie_find_free_ref_frame(ctx->frame_strg); 10172 this_frame_base = ctx->ref_frames[CURRENT_FRAME]->img.img_data; 10174 /* Calculate offsets to the other reference frames */ 10175 for (i = 0; i < NUM_REF_FRAMES; i++) 10176 { 10177 struct ref_cnt_img *ref = ctx->ref_frames[i]; 10179 ctx->ref_frame_offsets[i] = 10180 ref ? ref->img.img_data - this_frame_base : 0; 10181 } 10183 /* TODO: No need to do this on every frame... */ 10184 } 10186 void 10187 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx) 10188 { 10189 int i; 10190 for (i = 0; i < NUM_REF_FRAMES; i++) 10191 { 10192 vpx_img_free(&ctx->frame_strg[i].img); 10193 ctx->frame_strg[i].ref_cnt = 0; 10194 ctx->ref_frames[i] = NULL; 10195 } 10196 } 10198 void 10199 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10200 unsigned int row, 10201 unsigned int start_col, 10202 unsigned int num_cols) 10203 { 10204 struct img_index img; 10205 struct mb_info *mbi; 10206 unsigned int col; 10207 short *coeffs; 10209 /* Adjust pointers based on row, start_col */ 10210 img.stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 10211 img.uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 10212 img.y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 10213 img.u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 10214 img.v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 10215 img.y += (img.stride * row + start_col) * 16; 10216 img.u += (img.uv_stride * row + start_col) * 8; 10217 img.v += (img.uv_stride * row + start_col) * 8; 10218 mbi = ctx->mb_info_rows[row] + start_col; 10219 coeffs = ctx->tokens[row & (ctx->token_hdr.partitions - 1)].coeffs 10220 + 25 * 16 * start_col; 10222 /* Fix up the out-of-frame pixels */ 10223 if (start_col == 0) 10224 { 10225 fixup_left(img.y, 16, img.stride, row, mbi->base.y_mode); 10226 fixup_left(img.u, 8, img.uv_stride, row, mbi->base.uv_mode); 10227 fixup_left(img.v, 8, img.uv_stride, row, mbi->base.uv_mode); 10229 if (row == 0) 10230 *(img.y - img.stride - 1) = 127; 10231 } 10233 for (col = start_col; col < start_col + num_cols; col++) 10234 { 10235 if (row == 0) 10236 { 10237 fixup_above(img.y, 16, img.stride, col, mbi->base.y_mode); 10238 fixup_above(img.u, 8, img.uv_stride, col, 10239 mbi->base.uv_mode); 10240 fixup_above(img.v, 8, img.uv_stride, col, 10241 mbi->base.uv_mode); 10242 } 10244 if (mbi->base.y_mode <= B_PRED) 10245 { 10246 predict_intra_luma(img.y, img.stride, mbi, coeffs); 10247 predict_intra_chroma(img.u, img.v, img.uv_stride, mbi, 10248 coeffs); 10249 } 10250 else 10251 { 10252 if (mbi->base.y_mode != SPLITMV) // && != BPRED 10253 fixup_dc_coeffs(mbi, coeffs); 10255 if (mbi->base.need_mc_border) 10256 predict_inter_emulated_edge(ctx, &img, coeffs, mbi, col, 10257 row); 10258 else 10259 predict_inter(ctx, &img, coeffs, mbi); 10260 } 10262 /* Advance to the next macroblock */ 10263 mbi++; 10264 img.y += 16; 10265 img.u += 8; 10266 img.v += 8; 10267 coeffs += 25 * 16; 10268 } 10270 if (col == ctx->mb_cols) 10271 { 10272 /* Extend the last row by four pixels for intra prediction. 10273 * This will be propagated later by copy_down. 10274 */ 10275 uint32_t *extend = (uint32_t *)(img.y + 15 * img.stride); 10276 uint32_t val = 0x01010101 * img.y[-1 + 15 * img.stride]; 10277 *extend = val; 10278 } 10279 } 10281 ---- End code block ---------------------------------------- 10282 21.15. predict.h 10284 ---- Begin code block -------------------------------------- 10286 /* 10287 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10288 * 10289 * Use of this source code is governed by a BSD-style license 10290 * that can be found in the LICENSE file in the root of the source 10291 * tree. An additional intellectual property rights grant can be 10292 * found in the file PATENTS. All contributing project authors may 10293 * be found in the AUTHORS file in the root of the source tree. 10294 */ 10295 #ifndef PREDICT_H 10296 #define PREDICT_H 10298 void 10299 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx); 10301 void 10302 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx); 10304 void 10305 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10306 unsigned int row, 10307 unsigned int start_col, 10308 unsigned int num_cols); 10310 void 10311 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg); 10313 struct ref_cnt_img * 10314 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg); 10316 struct ref_cnt_img * 10317 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames); 10319 #endif 10321 ---- End code block ---------------------------------------- 10323 21.16. tokens.c 10325 ---- Begin code block -------------------------------------- 10326 /* 10327 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10328 * 10329 * Use of this source code is governed by a BSD-style license 10330 * that can be found in the LICENSE file in the root of the source 10331 * tree. An additional intellectual property rights grant can be 10332 * found in the file PATENTS. All contributing project authors may 10333 * be found in the AUTHORS file in the root of the source tree. 10334 */ 10335 #include "vpx_codec_internal.h" 10336 #include "dixie.h" 10337 #include "tokens.h" 10338 #include 10339 #include 10340 #include 10342 enum 10343 { 10344 EOB_CONTEXT_NODE, 10345 ZERO_CONTEXT_NODE, 10346 ONE_CONTEXT_NODE, 10347 LOW_VAL_CONTEXT_NODE, 10348 TWO_CONTEXT_NODE, 10349 THREE_CONTEXT_NODE, 10350 HIGH_LOW_CONTEXT_NODE, 10351 CAT_ONE_CONTEXT_NODE, 10352 CAT_THREEFOUR_CONTEXT_NODE, 10353 CAT_THREE_CONTEXT_NODE, 10354 CAT_FIVE_CONTEXT_NODE 10355 }; 10356 enum 10357 { 10358 ZERO_TOKEN, 10359 ONE_TOKEN, 10360 TWO_TOKEN, 10361 THREE_TOKEN, 10362 FOUR_TOKEN, 10363 DCT_VAL_CATEGORY1, 10364 DCT_VAL_CATEGORY2, 10365 DCT_VAL_CATEGORY3, 10366 DCT_VAL_CATEGORY4, 10367 DCT_VAL_CATEGORY5, 10368 DCT_VAL_CATEGORY6, 10369 DCT_EOB_TOKEN, 10370 MAX_ENTROPY_TOKENS 10371 }; 10372 struct extrabits 10373 { 10374 short min_val; 10375 short length; 10376 unsigned char probs[12]; 10377 }; 10378 static const unsigned int left_context_index[25] = 10379 { 10380 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10381 4, 4, 5, 5, 6, 6, 7, 7, 8 10382 }; 10383 static const unsigned int above_context_index[25] = 10384 { 10385 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10386 4, 5, 4, 5, 6, 7, 6, 7, 8 10387 }; 10388 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10389 static const unsigned int bands_x[16] = 10390 { 10391 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10392 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10393 }; 10394 #undef X 10395 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10396 { 10397 { 0, -1, { 0, 0, 0, 0, 0, 0, 10398 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10399 { 1, 0, { 0, 0, 0, 0, 0, 0, 10400 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10401 { 2, 0, { 0, 0, 0, 0, 0, 0, 10402 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10403 { 3, 0, { 0, 0, 0, 0, 0, 0, 10404 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10405 { 4, 0, { 0, 0, 0, 0, 0, 0, 10406 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10407 { 5, 0, { 159, 0, 0, 0, 0, 0, 10408 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10409 { 7, 1, { 145, 165, 0, 0, 0, 0, 10410 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10411 { 11, 2, { 140, 148, 173, 0, 0, 0, 10412 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10413 { 19, 3, { 135, 140, 155, 176, 0, 0, 10414 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10415 { 35, 4, { 130, 134, 141, 157, 180, 0, 10416 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10417 { 67, 10, { 129, 130, 133, 140, 153, 177, 10418 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10419 { 0, -1, { 0, 0, 0, 0, 0, 0, 10420 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10422 }; 10423 static const unsigned int zigzag[16] = 10424 { 10425 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10426 }; 10428 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10429 v = (bool_get_bit(bool) ? -value_to_sign \ 10430 : value_to_sign) * dqf[!!c]; 10432 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10433 if (!bool_get(bool, probability)) goto branch; 10435 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10436 if (!bool_get(bool, probability)) \ 10437 { \ 10438 prob = type_probs; \ 10439 if(c<15) {\ 10440 ++c; \ 10441 prob += bands_x[c]; \ 10442 goto branch; \ 10443 }\ 10444 else \ 10445 goto BLOCK_FINISHED; /*for malformed input */\ 10446 } 10448 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10449 DECODE_AND_APPLYSIGN(val) \ 10450 prob = type_probs + (ENTROPY_NODES*2); \ 10451 if(c < 15){\ 10452 b_tokens[zigzag[c]] = v; \ 10453 ++c; \ 10454 goto DO_WHILE; }\ 10455 b_tokens[zigzag[15]] = v; \ 10456 goto BLOCK_FINISHED; 10458 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10459 val += bool_get(bool, extrabits[t].probs[bits_count]) << bits_count; 10461 static int 10462 decode_mb_tokens(struct bool_decoder *bool, 10463 token_entropy_ctx_t left, 10464 token_entropy_ctx_t above, 10465 short *tokens, 10466 enum prediction_mode mode, 10467 coeff_probs_table_t probs, 10468 short factor[TOKEN_BLOCK_TYPES][2]) 10469 { 10470 int i, stop, type; 10471 int c, t, v; 10472 int val, bits_count; 10473 int eob_mask; 10474 short *b_tokens; /* tokens for this block */ 10475 unsigned char *type_probs; /* probabilities for this block type */ 10476 unsigned char *prob; 10477 short *dqf; 10479 eob_mask = 0; 10481 if (mode != B_PRED && mode != SPLITMV) 10482 { 10483 i = 24; 10484 stop = 24; 10485 type = 1; 10486 b_tokens = tokens + 24 * 16; 10487 dqf = factor[TOKEN_BLOCK_Y2]; 10488 } 10489 else 10490 { 10491 i = 0; 10492 stop = 16; 10493 type = 3; 10494 b_tokens = tokens; 10495 dqf = factor[TOKEN_BLOCK_Y1]; 10496 } 10498 /* Save a pointer to the coefficient probs for the current type. 10499 * Need to repeat this whenever type changes. 10500 */ 10501 type_probs = probs[type][0][0]; 10503 BLOCK_LOOP: 10504 t = left[left_context_index[i]] + above[above_context_index[i]]; 10505 c = !type; /* all blocks start at 0 except type 0, which starts 10506 * at 1. */ 10508 prob = type_probs; 10509 prob += t * ENTROPY_NODES; 10511 DO_WHILE: 10512 prob += bands_x[c]; 10513 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], BLOCK_FINISHED); 10515 CHECK_0_: 10517 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10518 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10519 ONE_CONTEXT_NODE_0_); 10520 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10521 LOW_VAL_CONTEXT_NODE_0_); 10522 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10523 HIGH_LOW_CONTEXT_NODE_0_); 10524 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10525 CAT_THREEFOUR_CONTEXT_NODE_0_); 10526 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10527 CAT_FIVE_CONTEXT_NODE_0_); 10528 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10529 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10531 do 10532 { 10533 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count); 10534 bits_count -- ; 10535 } 10536 while (bits_count >= 0); 10538 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10540 CAT_FIVE_CONTEXT_NODE_0_: 10541 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10542 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10543 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10544 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10545 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10546 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10547 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10549 CAT_THREEFOUR_CONTEXT_NODE_0_: 10550 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10551 CAT_THREE_CONTEXT_NODE_0_); 10552 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10553 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10554 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10555 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10556 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10557 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10559 CAT_THREE_CONTEXT_NODE_0_: 10560 val = extrabits[DCT_VAL_CATEGORY3].min_val; 10561 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 10562 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 10563 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 10564 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10566 HIGH_LOW_CONTEXT_NODE_0_: 10567 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 10568 CAT_ONE_CONTEXT_NODE_0_); 10570 val = extrabits[DCT_VAL_CATEGORY2].min_val; 10571 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 10572 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 10573 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10575 CAT_ONE_CONTEXT_NODE_0_: 10576 val = extrabits[DCT_VAL_CATEGORY1].min_val; 10577 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 10578 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10580 LOW_VAL_CONTEXT_NODE_0_: 10581 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 10582 TWO_CONTEXT_NODE_0_); 10583 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 10584 THREE_CONTEXT_NODE_0_); 10585 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 10587 THREE_CONTEXT_NODE_0_: 10588 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 10590 TWO_CONTEXT_NODE_0_: 10591 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 10593 ONE_CONTEXT_NODE_0_: 10594 DECODE_AND_APPLYSIGN(1); 10595 prob = type_probs + ENTROPY_NODES; 10597 if (c < 15) 10598 { 10599 b_tokens[zigzag[c]] = v; 10600 ++c; 10601 goto DO_WHILE; 10602 } 10604 b_tokens[zigzag[15]] = v; 10605 BLOCK_FINISHED: 10606 eob_mask |= (c > 1) << i; 10607 t = (c != !type); // any nonzero data? 10608 eob_mask |= t << 31; 10610 left[left_context_index[i]] = above[above_context_index[i]] = t; 10611 b_tokens += 16; 10613 i++; 10614 if (i < stop) 10615 goto BLOCK_LOOP; 10617 if (i == 25) 10618 { 10619 type = 0; 10620 i = 0; 10621 stop = 16; 10622 type_probs = probs[type][0][0]; 10623 b_tokens = tokens; 10624 dqf = factor[TOKEN_BLOCK_Y1]; 10625 goto BLOCK_LOOP; 10626 } 10628 if (i == 16) 10629 { 10630 type = 2; 10631 type_probs = probs[type][0][0]; 10632 stop = 24; 10633 dqf = factor[TOKEN_BLOCK_UV]; 10634 goto BLOCK_LOOP; 10635 } 10637 return eob_mask; 10638 } 10640 static void 10641 reset_row_context(token_entropy_ctx_t *left) 10642 { 10643 memset(left, 0, sizeof(*left)); 10644 } 10646 static void 10647 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 10648 { 10649 memset(above, 0, cols * sizeof(*above)); 10650 } 10652 static void 10653 reset_mb_context(token_entropy_ctx_t *left, 10654 token_entropy_ctx_t *above, 10655 enum prediction_mode mode) 10656 { 10657 /* Reset the macroblock context on the left and right. We have to 10658 * preserve the context of the second order block if this mode 10659 * would not have updated it. 10660 */ 10661 memset(left, 0, sizeof((*left)[0]) * 8); 10662 memset(above, 0, sizeof((*above)[0]) * 8); 10664 if (mode != B_PRED && mode != SPLITMV) 10665 { 10666 (*left)[8] = 0; 10667 (*above)[8] = 0; 10668 } 10669 } 10671 void 10672 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 10673 unsigned int partition, 10674 unsigned int row, 10675 unsigned int start_col, 10676 unsigned int num_cols) 10677 { 10678 struct token_decoder *tokens = &ctx->tokens[partition]; 10679 short *coeffs = tokens->coeffs + 25 * 16 * start_col; 10680 unsigned int col; 10681 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 10682 + start_col; 10683 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 10684 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 10686 if (row == 0) 10687 reset_above_context(above, num_cols); 10689 if (start_col == 0) 10690 reset_row_context(left); 10692 for (col = start_col; col < start_col + num_cols; col++) 10693 { 10694 memset(coeffs, 0, 25 * 16 * sizeof(short)); 10696 if (mbi->base.skip_coeff) 10697 { 10698 reset_mb_context(left, above, mbi->base.y_mode); 10699 mbi->base.eob_mask = 0; 10700 } 10701 else 10702 { 10703 struct dequant_factors *dqf; 10705 dqf = ctx->dequant_factors + mbi->base.segment_id; 10706 mbi->base.eob_mask = 10707 decode_mb_tokens(&tokens->bool, 10708 *left, *above, 10709 coeffs, 10710 mbi->base.y_mode, 10711 ctx->entropy_hdr.coeff_probs, 10712 dqf->factor); 10713 } 10715 above++; 10716 mbi++; 10717 coeffs += 25 * 16; 10718 } 10719 } 10721 void 10722 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 10723 { 10724 unsigned int partitions = ctx->token_hdr.partitions; 10726 if (ctx->frame_hdr.frame_size_updated) 10727 { 10728 unsigned int i; 10729 unsigned int coeff_row_sz = 10730 ctx->mb_cols * 25 * 16 * sizeof(short); 10732 for (i = 0; i < partitions; i++) 10733 { 10734 free(ctx->tokens[i].coeffs); 10735 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 10737 if (!ctx->tokens[i].coeffs) 10738 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10739 NULL); 10740 } 10742 free(ctx->above_token_entropy_ctx); 10743 ctx->above_token_entropy_ctx = 10744 calloc(ctx->mb_cols, sizeof(*ctx->above_token_entropy_ctx)); 10746 if (!ctx->above_token_entropy_ctx) 10747 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, NULL); 10748 } 10749 } 10751 void 10752 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 10753 { 10754 int i; 10756 for (i = 0; i < MAX_PARTITIONS; i++) 10757 free(ctx->tokens[i].coeffs); 10759 free(ctx->above_token_entropy_ctx); 10760 } 10762 ---- End code block ---------------------------------------- 10764 21.17. tokens.h 10766 ---- Begin code block -------------------------------------- 10768 /* 10769 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10770 * 10771 * Use of this source code is governed by a BSD-style license 10772 * that can be found in the LICENSE file in the root of the source 10773 * tree. An additional intellectual property rights grant can be 10774 * found in the file PATENTS. All contributing project authors may 10775 * be found in the AUTHORS file in the root of the source tree. 10776 */ 10777 #include "vpx_codec_internal.h" 10778 #include "dixie.h" 10779 #include "tokens.h" 10780 #include 10781 #include 10782 #include 10784 enum 10785 { 10786 EOB_CONTEXT_NODE, 10787 ZERO_CONTEXT_NODE, 10788 ONE_CONTEXT_NODE, 10789 LOW_VAL_CONTEXT_NODE, 10790 TWO_CONTEXT_NODE, 10791 THREE_CONTEXT_NODE, 10792 HIGH_LOW_CONTEXT_NODE, 10793 CAT_ONE_CONTEXT_NODE, 10794 CAT_THREEFOUR_CONTEXT_NODE, 10795 CAT_THREE_CONTEXT_NODE, 10796 CAT_FIVE_CONTEXT_NODE 10797 }; 10798 enum 10799 { 10800 ZERO_TOKEN, 10801 ONE_TOKEN, 10802 TWO_TOKEN, 10803 THREE_TOKEN, 10804 FOUR_TOKEN, 10805 DCT_VAL_CATEGORY1, 10806 DCT_VAL_CATEGORY2, 10807 DCT_VAL_CATEGORY3, 10808 DCT_VAL_CATEGORY4, 10809 DCT_VAL_CATEGORY5, 10810 DCT_VAL_CATEGORY6, 10811 DCT_EOB_TOKEN, 10812 MAX_ENTROPY_TOKENS 10813 }; 10814 struct extrabits 10815 { 10816 short min_val; 10817 short length; 10818 unsigned char probs[12]; 10819 }; 10820 static const unsigned int left_context_index[25] = 10821 { 10822 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10823 4, 4, 5, 5, 6, 6, 7, 7, 8 10824 }; 10825 static const unsigned int above_context_index[25] = 10826 { 10827 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10828 4, 5, 4, 5, 6, 7, 6, 7, 8 10829 }; 10830 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10831 static const unsigned int bands_x[16] = 10832 { 10833 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10834 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10835 }; 10836 #undef X 10837 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10838 { 10839 { 0, -1, { 0, 0, 0, 0, 0, 0, 10840 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10841 { 1, 0, { 0, 0, 0, 0, 0, 0, 10842 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10843 { 2, 0, { 0, 0, 0, 0, 0, 0, 10844 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10845 { 3, 0, { 0, 0, 0, 0, 0, 0, 10846 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10847 { 4, 0, { 0, 0, 0, 0, 0, 0, 10848 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10849 { 5, 0, { 159, 0, 0, 0, 0, 0, 10850 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10851 { 7, 1, { 145, 165, 0, 0, 0, 0, 10852 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10853 { 11, 2, { 140, 148, 173, 0, 0, 0, 10854 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10855 { 19, 3, { 135, 140, 155, 176, 0, 0, 10856 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10857 { 35, 4, { 130, 134, 141, 157, 180, 0, 10858 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10859 { 67, 10, { 129, 130, 133, 140, 153, 177, 10860 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10861 { 0, -1, { 0, 0, 0, 0, 0, 0, 10862 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10863 }; 10864 static const unsigned int zigzag[16] = 10865 { 10866 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10867 }; 10869 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10870 v = (bool_get_bit(bool) ? -value_to_sign \ 10871 : value_to_sign) * dqf[!!c]; 10873 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10874 if (!bool_get(bool, probability)) goto branch; 10876 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10877 if (!bool_get(bool, probability)) \ 10878 { \ 10879 prob = type_probs; \ 10880 if(c<15) {\ 10881 ++c; \ 10882 prob += bands_x[c]; \ 10883 goto branch; \ 10884 }\ 10885 else \ 10886 goto BLOCK_FINISHED; /*for malformed input */\ 10887 } 10889 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10890 DECODE_AND_APPLYSIGN(val) \ 10891 prob = type_probs + (ENTROPY_NODES*2); \ 10892 if(c < 15){\ 10893 b_tokens[zigzag[c]] = v; \ 10894 ++c; \ 10895 goto DO_WHILE; }\ 10896 b_tokens[zigzag[15]] = v; \ 10897 goto BLOCK_FINISHED; 10899 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10900 val += bool_get(bool, extrabits[t].probs[bits_count]) << bits_count; 10902 static int 10903 decode_mb_tokens(struct bool_decoder *bool, 10904 token_entropy_ctx_t left, 10905 token_entropy_ctx_t above, 10906 short *tokens, 10907 enum prediction_mode mode, 10908 coeff_probs_table_t probs, 10909 short factor[TOKEN_BLOCK_TYPES][2]) 10910 { 10911 int i, stop, type; 10912 int c, t, v; 10913 int val, bits_count; 10914 int eob_mask; 10915 short *b_tokens; /* tokens for this block */ 10916 unsigned char *type_probs; /* probabilities for this block type */ 10917 unsigned char *prob; 10918 short *dqf; 10920 eob_mask = 0; 10922 if (mode != B_PRED && mode != SPLITMV) 10923 { 10924 i = 24; 10925 stop = 24; 10926 type = 1; 10927 b_tokens = tokens + 24 * 16; 10928 dqf = factor[TOKEN_BLOCK_Y2]; 10929 } 10930 else 10931 { 10932 i = 0; 10933 stop = 16; 10934 type = 3; 10935 b_tokens = tokens; 10936 dqf = factor[TOKEN_BLOCK_Y1]; 10937 } 10939 /* Save a pointer to the coefficient probs for the current type. 10941 * Need to repeat this whenever type changes. 10942 */ 10943 type_probs = probs[type][0][0]; 10945 BLOCK_LOOP: 10946 t = left[left_context_index[i]] + above[above_context_index[i]]; 10947 c = !type; /* all blocks start at 0 except type 0, which starts 10948 * at 1. */ 10950 prob = type_probs; 10951 prob += t * ENTROPY_NODES; 10953 DO_WHILE: 10954 prob += bands_x[c]; 10955 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], BLOCK_FINISHED); 10957 CHECK_0_: 10958 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10959 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10960 ONE_CONTEXT_NODE_0_); 10961 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10962 LOW_VAL_CONTEXT_NODE_0_); 10963 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10964 HIGH_LOW_CONTEXT_NODE_0_); 10965 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10966 CAT_THREEFOUR_CONTEXT_NODE_0_); 10967 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10968 CAT_FIVE_CONTEXT_NODE_0_); 10969 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10970 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10972 do 10973 { 10974 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count); 10975 bits_count -- ; 10976 } 10977 while (bits_count >= 0); 10979 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10981 CAT_FIVE_CONTEXT_NODE_0_: 10982 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10983 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10984 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10985 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10986 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10987 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10988 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10990 CAT_THREEFOUR_CONTEXT_NODE_0_: 10991 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10992 CAT_THREE_CONTEXT_NODE_0_); 10993 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10994 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10995 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10996 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10997 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10998 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 11000 CAT_THREE_CONTEXT_NODE_0_: 11001 val = extrabits[DCT_VAL_CATEGORY3].min_val; 11002 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 11003 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 11004 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 11005 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 11007 HIGH_LOW_CONTEXT_NODE_0_: 11008 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 11009 CAT_ONE_CONTEXT_NODE_0_); 11011 val = extrabits[DCT_VAL_CATEGORY2].min_val; 11012 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 11013 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 11014 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 11016 CAT_ONE_CONTEXT_NODE_0_: 11017 val = extrabits[DCT_VAL_CATEGORY1].min_val; 11018 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 11019 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 11021 LOW_VAL_CONTEXT_NODE_0_: 11022 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 11023 TWO_CONTEXT_NODE_0_); 11024 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 11025 THREE_CONTEXT_NODE_0_); 11026 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 11028 THREE_CONTEXT_NODE_0_: 11029 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 11031 TWO_CONTEXT_NODE_0_: 11032 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 11034 ONE_CONTEXT_NODE_0_: 11035 DECODE_AND_APPLYSIGN(1); 11036 prob = type_probs + ENTROPY_NODES; 11037 if (c < 15) 11038 { 11039 b_tokens[zigzag[c]] = v; 11040 ++c; 11041 goto DO_WHILE; 11042 } 11044 b_tokens[zigzag[15]] = v; 11045 BLOCK_FINISHED: 11046 eob_mask |= (c > 1) << i; 11047 t = (c != !type); // any nonzero data? 11048 eob_mask |= t << 31; 11050 left[left_context_index[i]] = above[above_context_index[i]] = t; 11051 b_tokens += 16; 11053 i++; 11055 if (i < stop) 11056 goto BLOCK_LOOP; 11058 if (i == 25) 11059 { 11060 type = 0; 11061 i = 0; 11062 stop = 16; 11063 type_probs = probs[type][0][0]; 11064 b_tokens = tokens; 11065 dqf = factor[TOKEN_BLOCK_Y1]; 11066 goto BLOCK_LOOP; 11067 } 11069 if (i == 16) 11070 { 11071 type = 2; 11072 type_probs = probs[type][0][0]; 11073 stop = 24; 11074 dqf = factor[TOKEN_BLOCK_UV]; 11075 goto BLOCK_LOOP; 11076 } 11078 return eob_mask; 11079 } 11081 static void 11082 reset_row_context(token_entropy_ctx_t *left) 11083 { 11084 memset(left, 0, sizeof(*left)); 11085 } 11087 static void 11088 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 11089 { 11090 memset(above, 0, cols * sizeof(*above)); 11091 } 11093 static void 11094 reset_mb_context(token_entropy_ctx_t *left, 11095 token_entropy_ctx_t *above, 11096 enum prediction_mode mode) 11097 { 11098 /* Reset the macroblock context on the left and right. We have to 11099 * preserve the context of the second order block if this mode 11100 * would not have updated it. 11101 */ 11102 memset(left, 0, sizeof((*left)[0]) * 8); 11103 memset(above, 0, sizeof((*above)[0]) * 8); 11105 if (mode != B_PRED && mode != SPLITMV) 11106 { 11107 (*left)[8] = 0; 11108 (*above)[8] = 0; 11109 } 11110 } 11112 void 11113 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 11114 unsigned int partition, 11115 unsigned int row, 11116 unsigned int start_col, 11117 unsigned int num_cols) 11118 { 11119 struct token_decoder *tokens = &ctx->tokens[partition]; 11120 short *coeffs = tokens->coeffs + 25 * 16 * start_col; 11121 unsigned int col; 11122 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 11123 + start_col; 11124 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 11125 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 11127 if (row == 0) 11128 reset_above_context(above, num_cols); 11130 if (start_col == 0) 11131 reset_row_context(left); 11133 for (col = start_col; col < start_col + num_cols; col++) 11134 { 11135 memset(coeffs, 0, 25 * 16 * sizeof(short)); 11137 if (mbi->base.skip_coeff) 11138 { 11139 reset_mb_context(left, above, mbi->base.y_mode); 11140 mbi->base.eob_mask = 0; 11141 } 11142 else 11143 { 11144 struct dequant_factors *dqf; 11146 dqf = ctx->dequant_factors + mbi->base.segment_id; 11147 mbi->base.eob_mask = 11148 decode_mb_tokens(&tokens->bool, 11149 *left, *above, 11150 coeffs, 11151 mbi->base.y_mode, 11152 ctx->entropy_hdr.coeff_probs, 11153 dqf->factor); 11154 } 11156 above++; 11157 mbi++; 11158 coeffs += 25 * 16; 11159 } 11160 } 11162 void 11163 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 11164 { 11165 unsigned int partitions = ctx->token_hdr.partitions; 11167 if (ctx->frame_hdr.frame_size_updated) 11168 { 11169 unsigned int i; 11170 unsigned int coeff_row_sz = 11171 ctx->mb_cols * 25 * 16 * sizeof(short); 11173 for (i = 0; i < partitions; i++) 11174 { 11175 free(ctx->tokens[i].coeffs); 11176 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 11177 if (!ctx->tokens[i].coeffs) 11178 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 11179 NULL); 11180 } 11182 free(ctx->above_token_entropy_ctx); 11183 ctx->above_token_entropy_ctx = 11184 calloc(ctx->mb_cols, sizeof(*ctx->above_token_entropy_ctx)); 11186 if (!ctx->above_token_entropy_ctx) 11187 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, NULL); 11188 } 11189 } 11191 void 11192 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 11193 { 11194 int i; 11196 for (i = 0; i < MAX_PARTITIONS; i++) 11197 free(ctx->tokens[i].coeffs); 11199 free(ctx->above_token_entropy_ctx); 11200 } 11202 ---- End code block ---------------------------------------- 11204 21.18. vp8_prob_data.h 11206 ---- Begin code block -------------------------------------- 11208 static const 11209 unsigned char k_coeff_entropy_update_probs[BLOCK_TYPES][COEF_BANDS] 11210 [PREV_COEF_CONTEXTS] 11211 [ENTROPY_NODES] = 11212 { 11213 { 11214 { 11215 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11216 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11217 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11218 }, 11219 { 11220 {176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11221 {223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11222 {249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11224 }, 11225 { 11226 {255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11227 {234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11228 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11229 }, 11230 { 11231 {255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11232 {239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11233 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11234 }, 11235 { 11236 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11237 {251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11238 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11239 }, 11240 { 11241 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11242 {251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11243 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11244 }, 11245 { 11246 {255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255, }, 11247 {250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255, }, 11248 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11249 }, 11250 { 11251 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11252 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11253 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11254 }, 11255 }, 11256 { 11257 { 11258 {217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11259 {225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255, }, 11260 {234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255, }, 11261 }, 11262 { 11263 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11264 {223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11265 {238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, }, 11266 }, 11267 { 11268 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11269 {249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11270 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11271 }, 11272 { 11273 {255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11274 {247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11275 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11276 }, 11277 { 11278 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11279 {252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11280 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11281 }, 11282 { 11283 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11284 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11285 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11286 }, 11287 { 11288 {255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11289 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11290 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11291 }, 11292 { 11293 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11294 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11295 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11296 }, 11297 }, 11298 { 11299 { 11300 {186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255, }, 11301 {234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255, }, 11302 {251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255, }, 11303 }, 11304 { 11305 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11306 {236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11307 {251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255, }, 11308 }, 11309 { 11310 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11311 {254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11312 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11313 }, 11314 { 11315 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11316 {254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11317 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11318 }, 11319 { 11320 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11321 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11322 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11323 }, 11324 { 11325 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11326 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11327 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11328 }, 11329 { 11330 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11331 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11332 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11333 }, 11334 { 11335 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11336 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11337 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11338 }, 11339 }, 11340 { 11341 { 11342 {248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11343 {250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255, }, 11344 {248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255, }, 11345 }, 11346 { 11347 {255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11348 {246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11349 {252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255, }, 11350 }, 11351 { 11352 {255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11353 {248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11354 {253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, }, 11355 }, 11356 { 11357 {255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11358 {245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11359 {253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11360 }, 11361 { 11362 {255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11363 {252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11364 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11365 }, 11366 { 11367 {255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11368 {249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11369 {255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11370 }, 11371 { 11372 {255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11373 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11374 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11375 }, 11376 { 11377 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11378 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11379 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11380 }, 11381 }, 11382 }; 11384 static const 11385 unsigned char k_default_y_mode_probs [] = 11386 { 112, 86, 140, 37}; 11388 static const 11389 unsigned char k_default_uv_mode_probs [] = 11390 { 162, 101, 204}; 11392 static const 11393 unsigned char k_default_coeff_probs [BLOCK_TYPES][COEF_BANDS] 11394 [PREV_COEF_CONTEXTS][ENTROPY_NODES] = 11395 { 11396 { /* block type 0 */ 11397 { /* coeff band 0 */ 11398 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11399 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11400 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11401 }, 11402 { /* coeff band 1 */ 11403 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 11404 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 11405 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 11406 }, 11407 { /* coeff band 2 */ 11408 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 11409 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 11410 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 11411 }, 11412 { /* coeff band 3 */ 11413 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 11414 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 11415 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 11416 }, 11417 { /* coeff band 4 */ 11418 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 11419 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 11420 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 11421 }, 11422 { /* coeff band 5 */ 11423 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 11424 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 11425 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 11426 }, 11427 { /* coeff band 6 */ 11428 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 11429 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 11430 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 11431 }, 11432 { /* coeff band 7 */ 11433 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11434 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11435 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11436 } 11437 }, 11438 { /* block type 1 */ 11439 { /* coeff band 0 */ 11440 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 11441 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 11442 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 11443 }, 11444 { /* coeff band 1 */ 11445 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 11446 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 11447 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 11448 }, 11449 { /* coeff band 2 */ 11450 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 11451 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 11452 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 11453 }, 11454 { /* coeff band 3 */ 11455 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 11456 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 11457 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 11458 }, 11459 { /* coeff band 4 */ 11460 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 11461 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 11462 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 11463 }, 11464 { /* coeff band 5 */ 11465 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 11466 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 11467 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 11468 }, 11469 { /* coeff band 6 */ 11470 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 11471 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 11472 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 11473 }, 11474 { /* coeff band 7 */ 11475 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 11476 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11477 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 11478 } 11479 }, 11480 { /* block type 2 */ 11481 { /* coeff band 0 */ 11482 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 11483 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 11484 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 11485 }, 11486 { /* coeff band 1 */ 11487 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 11488 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 11489 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 11490 }, 11491 { /* coeff band 2 */ 11492 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 11493 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 11494 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 11495 }, 11496 { /* coeff band 3 */ 11497 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 11498 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 11499 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 11500 }, 11501 { /* coeff band 4 */ 11502 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11503 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 11504 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11505 }, 11506 { /* coeff band 5 */ 11507 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11508 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11509 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11510 }, 11511 { /* coeff band 6 */ 11512 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 11513 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 11514 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11515 }, 11516 { /* coeff band 7 */ 11517 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11518 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11519 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11520 } 11521 }, 11522 { /* block type 3 */ 11523 { /* coeff band 0 */ 11524 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 11525 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 11526 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 11527 }, 11528 { /* coeff band 1 */ 11529 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 11530 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 11531 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 11532 }, 11533 { /* coeff band 2 */ 11534 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 11535 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 11536 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 11537 }, 11538 { /* coeff band 3 */ 11539 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 11540 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 11541 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 11542 }, 11543 { /* coeff band 4 */ 11544 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 11545 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 11546 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 11547 }, 11548 { /* coeff band 5 */ 11549 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 11550 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 11551 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 11552 }, 11553 { /* coeff band 6 */ 11554 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 11555 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 11556 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 11558 }, 11559 { /* coeff band 7 */ 11560 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11561 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11562 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11563 } 11564 } 11565 }; 11567 static const 11568 unsigned char k_mv_entropy_update_probs[2][MV_PROB_CNT] = 11569 { 11570 { 11571 237, 11572 246, 11573 253, 253, 254, 254, 254, 254, 254, 11574 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 11575 }, 11576 { 11577 231, 11578 243, 11579 245, 253, 254, 254, 254, 254, 254, 11580 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 11581 } 11582 }; 11584 static const 11585 unsigned char k_default_mv_probs[2][MV_PROB_CNT] = 11586 { 11587 { /* row */ 11588 162, /* is short */ 11589 128, /* sign */ 11590 225, 146, 172, 147, 214, 39, 156, /* short tree */ 11591 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 /* long bits */ 11592 }, 11593 { 11594 164, 11595 128, 11596 204, 170, 119, 235, 140, 230, 228, 11597 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 11599 } 11600 }; 11602 ---- End code block ---------------------------------------- 11603 21.19. vpx_codec_internal.h 11605 ---- Begin code block -------------------------------------- 11607 /* 11608 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 11609 * 11610 * Use of this source code is governed by a BSD-style license 11611 * that can be found in the LICENSE file in the root of the source 11612 * tree. An additional intellectual property rights grant can be 11613 * found in the file PATENTS. All contributing project authors may 11614 * be found in the AUTHORS file in the root of the source tree. 11615 */ 11617 /*!\file decoder_impl.h 11618 * \brief Describes the decoder algorithm interface for algorithm 11619 * implementations. 11620 * 11621 * This file defines the private structures and data types that are 11622 * only relevant to implementing an algorithm, as opposed to using 11623 * it. 11624 * 11625 * To create a decoder algorithm class, an interface structure is put 11626 * into the global namespace: 11627 *
11628	    *     my_codec.c:
11629	    *       vpx_codec_iface_t my_codec = {
11630	    *           "My Codec v1.0",
11631	    *           VPX_CODEC_ALG_ABI_VERSION,
11632	    *           ...
11633	    *       };
11634	    *     
11635 * 11636 * An application instantiates a specific decoder instance by using 11637 * vpx_codec_init() and a pointer to the algorithm's interface 11638 * structure: 11639 *
11640	    *     my_app.c:
11641	    *       extern vpx_codec_iface_t my_codec;
11642	    *       {
11643	    *           vpx_codec_ctx_t algo;
11644	    *           res = vpx_codec_init(&algo, &my_codec);
11645	    *       }
11646	    *     
11647 * 11648 * Once initialized, the instance is manged using other functions 11649 * from the vpx_codec_* family. 11651 */ 11652 #ifndef VPX_CODEC_INTERNAL_H 11653 #define VPX_CODEC_INTERNAL_H 11654 #include "vpx_decoder.h" 11655 #include 11657 /*!\brief Current ABI version number 11658 * 11659 * \internal 11660 * If this file is altered in any way that changes the ABI, this 11661 * value must be bumped. Examples include, but are not limited to, 11662 * changing types, removing or reassigning enums, 11663 * adding/removing/rearranging fields to structures 11664 */ 11665 #define VPX_CODEC_INTERNAL_ABI_VERSION (3) 11667 typedef struct vpx_codec_alg_priv vpx_codec_alg_priv_t; 11669 /*!\brief init function pointer prototype 11670 * 11671 * Performs algorithm-specific initialization of the decoder context. 11672 * This function is called by the generic vpx_codec_init() wrapper 11673 * function, so plugins implementing this interface may trust the 11674 * input parameters to be properly initialized. 11675 * 11676 * \param[in] ctx Pointer to this instance's context 11677 * \retval #VPX_CODEC_OK 11678 * The input stream was recognized and decoder initialized. 11679 * \retval #VPX_CODEC_MEM_ERROR 11680 * Memory operation failed. 11681 */ 11682 typedef vpx_codec_err_t (*vpx_codec_init_fn_t)(vpx_codec_ctx_t *ctx); 11684 /*!\brief destroy function pointer prototype 11685 * 11686 * Performs algorithm-specific destruction of the decoder context. 11687 * This function is called by the generic vpx_codec_destroy() wrapper 11688 * function, so plugins implementing this interface may trust the 11689 * input parameters to be properly initialized. 11690 * 11691 * \param[in] ctx Pointer to this instance's context 11692 * \retval #VPX_CODEC_OK 11693 * The input stream was recognized and decoder initialized. 11694 * \retval #VPX_CODEC_MEM_ERROR 11695 * Memory operation failed. 11696 */ 11697 typedef vpx_codec_err_t (*vpx_codec_destroy_fn_t)( 11698 vpx_codec_alg_priv_t *ctx); 11700 /*!\brief parse stream info function pointer prototype 11701 * 11702 * Performs high level parsing of the bitstream. This function is 11703 * called by the generic vpx_codec_parse_stream() wrapper function, 11704 * so plugins implementing this interface may trust the input 11705 * parameters to be properly initialized. 11706 * 11707 * \param[in] data Pointer to a block of data to parse 11708 * \param[in] data_sz Size of the data buffer 11709 * \param[in,out] si Pointer to stream info to update. The size 11710 * member \ref MUST be properly initialized, 11711 * but \ref MAY be clobbered by the 11712 * algorithm. This parameter \ref MAY be 11713 * NULL. 11714 * 11715 * \retval #VPX_CODEC_OK 11716 * Bitstream is parsable and stream information updated 11717 */ 11718 typedef vpx_codec_err_t (*vpx_codec_peek_si_fn_t)( 11719 const uint8_t *data, 11720 unsigned int data_sz, 11721 vpx_codec_stream_info_t *si); 11723 /*!\brief Return information about the current stream. 11724 * 11725 * Returns information about the stream that has been parsed during 11726 * decoding. 11727 * 11728 * \param[in] ctx Pointer to this instance's context 11729 * \param[in,out] si Pointer to stream info to update. The size 11730 * member \ref MUST be properly initialized, 11731 * but \ref MAY be clobbered by the 11732 * algorithm. This parameter \ref MAY be 11733 * NULL. 11734 * 11735 * \retval #VPX_CODEC_OK 11736 * Bitstream is parsable and stream information updated 11737 */ 11738 typedef vpx_codec_err_t (*vpx_codec_get_si_fn_t)( 11739 vpx_codec_alg_priv_t *ctx, 11740 vpx_codec_stream_info_t *si); 11742 /*!\brief control function pointer prototype 11743 * 11744 * This function is used to exchange algorithm specific data with the 11745 * decoder instance. This can be used to implement features specific 11746 * to a particular algorithm. 11747 * 11748 * This function is called by the generic vpx_codec_control() wrapper 11749 * function, so plugins implementing this interface may trust the 11750 * input parameters to be properly initialized. However, this 11751 * interface does not provide type safety for the exchanged data or 11752 * assign meanings to the control codes. Those details should be 11753 * specified in the algorithm's header file. In particular, the 11754 * ctrl_id parameter is guaranteed to exist in the algorithm's 11755 * control mapping table, and the data paramter may be NULL. 11756 * 11757 * 11758 * \param[in] ctx Pointer to this instance's context 11759 * \param[in] ctrl_id Algorithm specific control identifier 11760 * \param[in,out] data Data to exchange with algorithm instance. 11761 * 11762 * \retval #VPX_CODEC_OK 11763 * The internal state data was deserialized. 11764 */ 11765 typedef vpx_codec_err_t (*vpx_codec_control_fn_t)( 11766 vpx_codec_alg_priv_t *ctx, 11767 int ctrl_id, 11768 va_list ap); 11770 /*!\brief control function pointer mapping 11771 * 11772 * This structure stores the mapping between control identifiers and 11773 * implementing functions. Each algorithm provides a list of these 11774 * mappings. This list is searched by the vpx_codec_control() wrapper 11775 * function to determine which function to invoke. The special 11776 * value {0, NULL} is used to indicate end-of-list, and must be 11777 * present. The special value {0, } can be used as a 11778 * catch-all mapping. This implies that ctrl_id values chosen by the 11779 * algorithm \ref MUST be non-zero. 11780 */ 11781 typedef const struct 11782 { 11783 int ctrl_id; 11784 vpx_codec_control_fn_t fn; 11785 } vpx_codec_ctrl_fn_map_t; 11787 /*!\brief decode data function pointer prototype 11788 * 11789 * Processes a buffer of coded data. If the processing results in a 11790 * new decoded frame becoming available, #VPX_CODEC_CB_PUT_SLICE and 11791 * #VPX_CODEC_CB_PUT_FRAME events are generated as appropriate. This 11792 * function is called by the generic vpx_codec_decode() wrapper 11793 * function, so plugins implementing this interface may trust the 11794 * input parameters to be properly initialized. 11795 * 11796 * \param[in] ctx Pointer to this instance's context 11797 * \param[in] data Pointer to this block of new coded data. If 11798 * NULL, a #VPX_CODEC_CB_PUT_FRAME event is 11799 * posted for the previously decoded frame. 11800 * \param[in] data_sz Size of the coded data, in bytes. 11801 * 11802 * \return Returns #VPX_CODEC_OK if the coded data was processed 11803 * completely and future pictures can be decoded without 11804 * error. Otherwise, see the descriptions of the other error 11805 * codes in ::vpx_codec_err_t for recoverability 11806 * capabilities. 11807 */ 11808 typedef vpx_codec_err_t (*vpx_codec_decode_fn_t)( 11809 vpx_codec_alg_priv_t *ctx, 11810 const uint8_t *data, 11811 unsigned int data_sz, 11812 void *user_priv, 11813 long deadline); 11815 /*!\brief Decoded frames iterator 11816 * 11817 * Iterates over a list of the frames available for display. The 11818 * iterator storage should be initialized to NULL to start the 11819 * iteration. Iteration is complete when this function returns NULL. 11820 * 11821 * The list of available frames becomes valid upon completion of the 11822 * vpx_codec_decode call, and remains valid until the next call to 11823 * vpx_codec_decode. 11824 * 11825 * \param[in] ctx Pointer to this instance's context 11826 * \param[in out] iter Iterator storage, initialized to NULL 11827 * 11828 * \return Returns a pointer to an image, if one is ready for 11829 * display. Frames produced will always be in PTS 11830 * (presentation time stamp) order. 11831 */ 11832 typedef vpx_image_t*(*vpx_codec_get_frame_fn_t)( 11833 vpx_codec_alg_priv_t *ctx, 11834 vpx_codec_iter_t *iter); 11836 /*\brief e_xternal Memory Allocation memory map get iterator 11837 * 11838 * Iterates over a list of the memory maps requested by the decoder. 11839 * The iterator storage should be initialized to NULL to start the 11840 * iteration. Iteration is complete when this function returns NULL. 11842 * 11843 * \param[in out] iter Iterator storage, initialized to NULL 11844 * 11845 * \return Returns a pointer to an memory segment descriptor, or NULL 11846 * to indicate end-of-list. 11847 */ 11848 typedef vpx_codec_err_t (*vpx_codec_get_mmap_fn_t)( 11849 const vpx_codec_ctx_t *ctx, 11850 vpx_codec_mmap_t *mmap, 11851 vpx_codec_iter_t *iter); 11853 /*\brief e_xternal Memory Allocation memory map set iterator 11854 * 11855 * Sets a memory descriptor inside the decoder instance. 11856 * 11857 * \param[in] ctx Pointer to this instance's context 11858 * \param[in] mmap Memory map to store. 11859 * 11860 * \retval #VPX_CODEC_OK 11861 * The memory map was accepted and stored. 11862 * \retval #VPX_CODEC_MEM_ERROR 11863 * The memory map was rejected. 11864 */ 11865 typedef vpx_codec_err_t (*vpx_codec_set_mmap_fn_t)( 11866 vpx_codec_ctx_t *ctx, 11867 const vpx_codec_mmap_t *mmap); 11869 typedef vpx_codec_err_t (*vpx_codec_encode_fn_t)( 11870 vpx_codec_alg_priv_t *ctx, 11871 const vpx_image_t *img, 11872 vpx_codec_pts_t pts, 11873 unsigned long duration, 11874 vpx_enc_frame_flags_t flags, 11875 unsigned long deadline); 11876 typedef const vpx_codec_cx_pkt_t*(*vpx_codec_get_cx_data_fn_t)( 11877 vpx_codec_alg_priv_t *ctx, 11878 vpx_codec_iter_t *iter); 11880 typedef vpx_codec_err_t 11881 (*vpx_codec_enc_config_set_fn_t)( 11882 vpx_codec_alg_priv_t *ctx, 11883 const vpx_codec_enc_cfg_t *cfg); 11884 typedef vpx_fixed_buf_t * 11885 (*vpx_codec_get_global_headers_fn_t)(vpx_codec_alg_priv_t *ctx); 11887 typedef vpx_image_t * 11888 (*vpx_codec_get_preview_frame_fn_t)(vpx_codec_alg_priv_t *ctx); 11890 /*!\brief usage configuration mapping 11891 * 11892 * This structure stores the mapping between usage identifiers and 11893 * configuration structures. Each algorithm provides a list of these 11894 * mappings. This list is searched by the 11895 * vpx_codec_enc_config_default() wrapper function to determine which 11896 * config to return. The special value {-1, {0}} is used to indicate 11897 * end-of-list, and must be present. At least one mapping must be 11898 * present, in addition to the end-of-list. 11899 * 11900 */ 11901 typedef const struct 11902 { 11903 int usage; 11904 vpx_codec_enc_cfg_t cfg; 11905 } vpx_codec_enc_cfg_map_t; 11907 #define NOT_IMPLEMENTED 0 11909 /*!\brief Decoder algorithm interface interface 11910 * 11911 * All decoders \ref MUST expose a variable of this type. 11912 */ 11913 struct vpx_codec_iface 11914 { 11915 const char *name; 11916 int abi_version; 11917 vpx_codec_caps_t caps; 11918 vpx_codec_init_fn_t init; 11919 vpx_codec_destroy_fn_t destroy; 11920 vpx_codec_ctrl_fn_map_t *ctrl_maps; 11921 vpx_codec_get_mmap_fn_t get_mmap; 11922 vpx_codec_set_mmap_fn_t set_mmap; 11923 struct 11924 { 11925 vpx_codec_peek_si_fn_t peek_si; 11926 vpx_codec_get_si_fn_t get_si; 11927 vpx_codec_decode_fn_t decode; 11928 vpx_codec_get_frame_fn_t get_frame; 11929 } dec; 11930 struct 11931 { 11932 vpx_codec_enc_cfg_map_t *cfg_maps; 11933 vpx_codec_encode_fn_t encode; 11934 vpx_codec_get_cx_data_fn_t get_cx_data; 11935 vpx_codec_enc_config_set_fn_t cfg_set; 11936 vpx_codec_get_global_headers_fn_t get_glob_hdrs; 11937 vpx_codec_get_preview_frame_fn_t get_preview; 11938 } enc; 11939 }; 11941 /*!\brief Callback function pointer / user data pair storage */ 11942 typedef struct vpx_codec_priv_cb_pair 11943 { 11944 union 11945 { 11946 vpx_codec_put_frame_cb_fn_t put_frame; 11947 vpx_codec_put_slice_cb_fn_t put_slice; 11948 }; 11949 void *user_priv; 11950 } vpx_codec_priv_cb_pair_t; 11952 /*!\brief Instance private storage 11953 * 11954 * This structure is allocated by the algorithm's init function. It 11955 * can be extended in one of two ways. First, a second, algorithm 11956 * specific structure can be allocated and the priv member pointed to 11957 * it. Alternatively, this structure can be made the first member of 11958 * the algorithm specific structure, and the pointer casted to the 11959 * proper type. 11960 */ 11961 struct vpx_codec_priv 11962 { 11963 unsigned int sz; 11964 vpx_codec_iface_t *iface; 11965 struct vpx_codec_alg_priv *alg_priv; 11966 const char *err_detail; 11967 vpx_codec_flags_t init_flags; 11968 struct 11969 { 11970 vpx_codec_priv_cb_pair_t put_frame_cb; 11971 vpx_codec_priv_cb_pair_t put_slice_cb; 11972 } dec; 11973 struct 11974 { 11975 int tbd; 11976 struct vpx_fixed_buf cx_data_dst_buf; 11977 unsigned int cx_data_pad_before; 11978 unsigned int cx_data_pad_after; 11979 vpx_codec_cx_pkt_t cx_data_pkt; 11980 } enc; 11981 }; 11982 #undef VPX_CTRL_USE_TYPE 11983 #define VPX_CTRL_USE_TYPE(id, typ) \ 11984 static typ id##__value(va_list args) \ 11985 {return va_arg(args, typ);} \ 11986 static typ id##__convert(void *x)\ 11987 {\ 11988 union\ 11989 {\ 11990 void *x;\ 11991 typ d;\ 11992 } u;\ 11993 u.x = x;\ 11994 return u.d;\ 11995 } 11997 #undef VPX_CTRL_USE_TYPE_DEPRECATED 11998 #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \ 11999 static typ id##__value(va_list args) \ 12000 {return va_arg(args, typ);} \ 12001 static typ id##__convert(void *x)\ 12002 {\ 12003 union\ 12004 {\ 12005 void *x;\ 12006 typ d;\ 12007 } u;\ 12008 u.x = x;\ 12009 return u.d;\ 12010 } 12012 #define CAST(id, arg) id##__value(arg) 12013 #define RECAST(id, x) id##__convert(x) 12015 /* Internal Utility Functions 12016 * 12017 * The following functions are indended to be used inside algorithms 12018 * as utilities for manipulating vpx_codec_* data structures. 12019 */ 12020 struct vpx_codec_pkt_list 12021 { 12022 unsigned int cnt; 12023 unsigned int max; 12024 struct vpx_codec_cx_pkt pkts[1]; 12025 }; 12027 #define vpx_codec_pkt_list_decl(n)\ 12028 union {struct vpx_codec_pkt_list head;\ 12029 struct {struct vpx_codec_pkt_list head;\ 12030 struct vpx_codec_cx_pkt pkts[n];} alloc;} 12032 #define vpx_codec_pkt_list_init(m)\ 12033 (m)->alloc.head.cnt = 0,\ 12034 (m)->alloc.head.max = \ 12035 sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0]) 12037 int 12038 vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *, 12039 const struct vpx_codec_cx_pkt *); 12041 const vpx_codec_cx_pkt_t* 12042 vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list, 12043 vpx_codec_iter_t *iter); 12045 #include 12046 #include 12047 struct vpx_internal_error_info 12048 { 12049 vpx_codec_err_t error_code; 12050 int has_detail; 12051 char detail[80]; 12052 int setjmp; 12053 jmp_buf jmp; 12054 }; 12056 static void vpx_internal_error(struct vpx_internal_error_info *info, 12057 vpx_codec_err_t error, 12058 const char *fmt, 12059 ...) 12060 { 12061 va_list ap; 12063 info->error_code = error; 12064 info->has_detail = 0; 12066 if (fmt) 12067 { 12068 size_t sz = sizeof(info->detail); 12070 info->has_detail = 1; 12071 va_start(ap, fmt); 12072 vsnprintf(info->detail, sz - 1, fmt, ap); 12073 va_end(ap); 12074 info->detail[sz-1] = '\0'; 12076 } 12078 if (info->setjmp) 12079 longjmp(info->jmp, info->error_code); 12080 } 12081 #endif 12083 ---- End code block ---------------------------------------- 12085 21.20. vpx_decoder.h 12087 ---- Begin code block -------------------------------------- 12089 /* 12090 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 12091 * 12092 * Use of this source code is governed by a BSD-style license 12093 * that can be found in the LICENSE file in the root of the source 12094 * tree. An additional intellectual property rights grant can be 12095 * found in the file PATENTS. All contributing project authors may 12096 * be found in the AUTHORS file in the root of the source tree. 12097 */ 12099 /*!\defgroup decoder Decoder Algorithm Interface 12100 * \ingroup codec 12101 * This abstraction allows applications using this decoder to easily 12102 * support multiple video formats with minimal code duplication. This 12103 * section describes the interface common to all decoders. 12104 * @{ 12105 */ 12107 /*!\file vpx_decoder.h 12108 * \brief Describes the decoder algorithm interface to applications. 12109 * 12110 * This file describes the interface between an application and a 12111 * video decoder algorithm. 12112 * 12113 */ 12114 #ifdef __cplusplus 12115 extern "C" { 12116 #endif 12118 #ifndef VPX_DECODER_H 12119 #define VPX_DECODER_H 12120 #include "vpx_codec.h" 12121 /*!\brief Current ABI version number 12122 * 12123 * \internal 12124 * If this file is altered in any way that changes the ABI, this 12125 * value must be bumped. Examples include, but are not limited 12126 * to, changing types, removing or reassigning enums, 12127 * adding/removing/rearranging fields to structures 12128 */ 12129 #define VPX_DECODER_ABI_VERSION (2 + VPX_CODEC_ABI_VERSION) 12131 /*! \brief Decoder capabilities bitfield 12132 * 12133 * Each decoder advertises the capabilities it supports as part 12134 * of its ::vpx_codec_iface_t interface structure. Capabilities 12135 * are extra interfaces or functionality, and are not required 12136 * to be supported by a decoder. 12137 * 12138 * The available flags are specifiedby VPX_CODEC_CAP_* defines. 12139 */ 12140 #define VPX_CODEC_CAP_PUT_SLICE 0x10000 /**< Will issue put_slice 12141 callbacks */ 12142 #define VPX_CODEC_CAP_PUT_FRAME 0x20000 /**< Will issue put_frame 12143 callbacks */ 12144 #define VPX_CODEC_CAP_POSTPROC 0x40000 /**< Can postprocess decoded 12145 frame */ 12147 /*! \brief Initialization-time Feature Enabling 12148 * 12149 * Certain codec features must be known at initialization time, 12150 * to allow for proper memory allocation. 12151 * 12152 * The available flags are specified by VPX_CODEC_USE_* defines. 12153 */ 12154 #define VPX_CODEC_USE_POSTPROC 0x10000 /**< Postprocess decoded 12155 frame */ 12157 /*!\brief Stream properties 12158 * 12159 * This structure is used to query or set properties of the 12160 * decoded stream. Algorithms may extend this structure with data 12161 * specific to their bitstream by setting the sz member 12162 * appropriately. 12163 */ 12164 typedef struct vpx_codec_stream_info 12165 { 12166 unsigned int sz; /**< Size of this structure */ 12167 unsigned int w; /**< Width (or 0 for unknown/default) */ 12168 unsigned int h; /**< Height (or 0 for unknown/default) */ 12169 unsigned int is_kf; /**< Current frame is a keyframe */ 12170 } vpx_codec_stream_info_t; 12172 /* REQUIRED FUNCTIONS 12173 * 12174 * The following functions are required to be implemented for all 12175 * decoders. They represent the base case functionality expected 12176 * of all decoders. 12177 */ 12179 /*!\brief Initialization Configurations 12180 * 12181 * This structure is used to pass init time configuration options 12182 * to the decoder. 12183 */ 12184 typedef struct vpx_codec_dec_cfg 12185 { 12186 unsigned int threads; /**< Maximum number of threads to use, 12187 default 1 */ 12188 unsigned int w; /**< Width */ 12189 unsigned int h; /**< Height */ 12190 } vpx_codec_dec_cfg_t; /**< alias for struct vpx_codec_dec_cfg */ 12192 /*!\brief Initialize a decoder instance 12193 * 12194 * Initializes a decoder context using the given interface. 12195 * Applications should call the vpx_codec_dec_init convenience 12196 * macro instead of this function directly, to ensure that the 12197 * ABI version number parameter is properly initialized. 12198 * 12199 * In XMA mode (activated by setting VPX_CODEC_USE_XMA in the 12200 * flags parameter), the storage pointed to by the cfg parameter 12201 * must be kept readable and stable until all memory maps have 12202 * been set. 12203 * 12204 * \param[in] ctx Pointer to this instance's context. 12205 * \param[in] iface Pointer to the alogrithm interface to 12206 * use. 12207 * \param[in] cfg Configuration to use, if known. May be 12208 * NULL. 12209 * \param[in] flags Bitfield of VPX_CODEC_USE_* flags 12210 * \param[in] ver ABI version number. Must be set to 12211 * VPX_DECODER_ABI_VERSION 12212 * \retval #VPX_CODEC_OK 12213 * The decoder algorithm initialized. 12214 * \retval #VPX_CODEC_MEM_ERROR 12215 * Memory allocation failed. 12216 */ 12217 vpx_codec_err_t vpx_codec_dec_init_ver( 12218 vpx_codec_ctx_t *ctx, 12219 vpx_codec_iface_t *iface, 12220 vpx_codec_dec_cfg_t *cfg, 12221 vpx_codec_flags_t flags, 12222 int ver); 12224 /*!\brief Convenience macro for vpx_codec_dec_init_ver() 12225 * 12226 * Ensures the ABI version parameter is properly set. 12227 */ 12228 #define vpx_codec_dec_init(ctx, iface, cfg, flags) \ 12229 vpx_codec_dec_init_ver(ctx, iface, cfg, flags, \ 12230 VPX_DECODER_ABI_VERSION) 12232 /*!\brief Parse stream info from a buffer 12233 * 12234 * Performs high level parsing of the bitstream. Construction of 12235 * a decoder context is not necessary. Can be used to determine 12236 * if the bitstream is of the proper format, and to extract 12237 * information from the stream. 12238 * 12239 * \param[in] iface Pointer to the alogrithm interface 12240 * \param[in] data Pointer to a block of data to parse 12241 * \param[in] data_sz Size of the data buffer 12242 * \param[in,out] si Pointer to stream info to update. The 12243 * size member 12244 * \ref MUST be properly initialized, but 12245 * \ref MAY be clobbered by the 12246 * algorithm. This parameter \ref MAY be 12247 * NULL. 12248 * 12249 * \retval #VPX_CODEC_OK 12250 * Bitstream is parsable and stream information updated 12251 */ 12252 vpx_codec_err_t vpx_codec_peek_stream_info( 12253 vpx_codec_iface_t *iface, 12254 const uint8_t *data, 12255 unsigned int data_sz, 12256 vpx_codec_stream_info_t *si); 12258 /*!\brief Return information about the current stream. 12259 * 12260 * Returns information about the stream that has been parsed 12261 * during decoding. 12262 * 12263 * \param[in] ctx Pointer to this instance's context 12264 * \param[in,out] si Pointer to stream info to update. The 12265 * size member \ref MUST be properly 12266 * initialized, but \ref MAY be clobbered 12267 * by the algorithm. This parameter \ref 12268 * MAY be NULL. 12269 * 12270 * \retval #VPX_CODEC_OK 12271 * Bitstream is parsable and stream information updated 12272 */ 12273 vpx_codec_err_t vpx_codec_get_stream_info( 12274 vpx_codec_ctx_t *ctx, 12275 vpx_codec_stream_info_t *si); 12277 /*!\brief Decode data 12278 * 12279 * Processes a buffer of coded data. If the processing results in 12280 * a new decoded frame becoming available, PUT_SLICE and 12281 * PUT_FRAME events may be generated, as appropriate. Encoded 12282 * data \ref MUST be passed in DTS (decode time stamp) order. 12283 * Frames produced will always be in PTS (presentation time 12284 * stamp) order. 12285 * 12286 * \param[in] ctx Pointer to this instance's context 12287 * \param[in] data Pointer to this block of new coded 12288 * data. If NULL, a 12289 * VPX_CODEC_CB_PUT_FRAME event is posted 12290 * for the previously decoded frame. 12291 * \param[in] data_sz Size of the coded data, in bytes. 12292 * \param[in] user_priv Application specific data to associate 12293 * with this frame. 12294 * \param[in] deadline Soft deadline the decoder should 12295 * attempt to meet, in us. Set to zero 12296 * for unlimited. 12297 * 12298 * \return Returns #VPX_CODEC_OK if the coded data was processed 12299 * completely and future pictures can be decoded without 12300 * error. Otherwise, see the descriptions of the other 12301 * error codes in ::vpx_codec_err_t for recoverability 12302 * capabilities. 12303 */ 12304 vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, 12305 const uint8_t *data, 12306 unsigned int data_sz, 12307 void *user_priv, 12308 long deadline); 12310 /*!\brief Decoded frames iterator 12311 * 12312 * Iterates over a list of the frames available for display. The 12313 * iterator storage should be initialized to NULL to start the 12314 * iteration. Iteration is complete when this function returns 12315 * NULL. 12316 * 12317 * The list of available frames becomes valid upon completion of 12318 * the vpx_codec_decode call, and remains valid until the next 12319 * call to vpx_codec_decode. 12320 * 12321 * \param[in] ctx Pointer to this instance's context 12322 * \param[in,out] iter Iterator storage, initialized to NULL 12323 * 12324 * \return Returns a pointer to an image, if one is ready for 12325 * display. Frames produced will always be in PTS 12326 * (presentation time stamp) order. 12327 */ 12328 vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, 12329 vpx_codec_iter_t *iter); 12331 /*!\defgroup cap_put_frame Frame-Based Decoding Functions 12332 * 12333 * The following functions are required to be implemented for all 12334 * decoders that advertise the VPX_CODEC_CAP_PUT_FRAME 12335 * capability. Calling these functions for codecs that don't 12336 * advertise this capability will result in an error code being 12337 * returned, usually VPX_CODEC_ERROR 12338 * @{ 12339 */ 12341 /*!\brief put frame callback prototype 12342 * 12343 * This callback is invoked by the decoder to notify the 12344 * application of the availability of decoded image data. 12345 */ 12346 typedef void (*vpx_codec_put_frame_cb_fn_t)( 12347 void *user_priv, 12348 const vpx_image_t *img); 12350 /*!\brief Register for notification of frame completion. 12351 * 12352 * Registers a given function to be called when a decoded frame 12353 * is available. 12354 * 12355 * \param[in] ctx Pointer to this instance's context 12356 * \param[in] cb Pointer to the callback function 12357 * \param[in] user_priv User's private data 12358 * 12359 * \retval #VPX_CODEC_OK 12360 * Callback successfully registered. 12361 * \retval #VPX_CODEC_ERROR 12362 * Decoder context not initialized, or algorithm not capable 12363 * of posting slice completion. 12364 */ 12365 vpx_codec_err_t vpx_codec_register_put_frame_cb( 12366 vpx_codec_ctx_t *ctx, 12367 vpx_codec_put_frame_cb_fn_t cb, 12368 void *user_priv); 12370 /*!@} - end defgroup cap_put_frame */ 12372 /*!\defgroup cap_put_slice Slice-Based Decoding Functions 12373 * 12374 * The following functions are required to be implemented for all 12375 * decoders that advertise the VPX_CODEC_CAP_PUT_SLICE 12376 * capability. Calling these functions for codecs that don't 12377 * advertise this capability will result in an error code being 12378 * returned, usually VPX_CODEC_ERROR 12379 * @{ 12380 */ 12382 /*!\brief put slice callback prototype 12383 * 12384 * This callback is invoked by the decoder to notify the 12385 * application of the availability of partially decoded image 12386 * data. 12387 */ 12388 typedef void (*vpx_codec_put_slice_cb_fn_t)( 12389 void *user_priv, 12390 const vpx_image_t *img, 12391 const vpx_image_rect_t *valid, 12392 const vpx_image_rect_t *update); 12394 /*!\brief Register for notification of slice completion. 12395 * 12396 * Registers a given function to be called when a decoded slice 12397 * is available. 12398 * 12399 * \param[in] ctx Pointer to this instance's context 12400 * \param[in] cb Pointer to the callback function 12401 * \param[in] user_priv User's private data 12402 * 12403 * \retval #VPX_CODEC_OK 12404 * Callback successfully registered. 12405 * \retval #VPX_CODEC_ERROR 12406 * Decoder context not initialized, or algorithm not capable 12407 * of posting slice completion. 12408 */ 12409 vpx_codec_err_t vpx_codec_register_put_slice_cb( 12410 vpx_codec_ctx_t *ctx, 12411 vpx_codec_put_slice_cb_fn_t cb, 12412 void *user_priv); 12414 /*!@} - end defgroup cap_put_slice*/ 12416 /*!@} - end defgroup decoder*/ 12418 #endif 12420 #ifdef __cplusplus 12421 } 12422 #endif 12424 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 12425 #include "vpx_decoder_compat.h" 12426 #endif 12428 ---- End code block ---------------------------------------- 12430 21.21. vpx_integer.h 12432 ---- Begin code block -------------------------------------- 12434 /* 12435 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 12436 * 12437 * Use of this source code is governed by a BSD-style license 12438 * that can be found in the LICENSE file in the root of the source 12439 * tree. An additional intellectual property rights grant can be 12440 * found in the file PATENTS. All contributing project authors may 12441 * be found in the AUTHORS file in the root of the source tree. 12442 */ 12444 #ifndef VPX_INTEGER_H 12445 #define VPX_INTEGER_H 12447 /* get ptrdiff_t, size_t, wchar_t, NULL */ 12448 #include 12450 #if defined(_MSC_VER) || defined(VPX_EMULATE_INTTYPES) 12451 typedef signed char int8_t; 12452 typedef signed short int16_t; 12453 typedef signed int int32_t; 12455 typedef unsigned char uint8_t; 12456 typedef unsigned short uint16_t; 12457 typedef unsigned int uint32_t; 12459 #if defined(_MSC_VER) 12460 typedef signed __int64 int64_t; 12461 typedef unsigned __int64 uint64_t; 12462 #define PRId64 "I64d" 12463 #endif 12465 #ifdef HAVE_ARMV6 12466 typedef unsigned int int_fast16_t; 12467 #else 12468 typedef signed short int_fast16_t; 12469 #endif 12470 typedef signed char int_fast8_t; 12471 typedef unsigned char uint_fast8_t; 12473 #ifndef _UINTPTR_T_DEFINED 12474 typedef unsigned int uintptr_t; 12475 #endif 12477 #else 12479 /* Most platforms have the C99 standard integer types. */ 12481 #if defined(__cplusplus) && !defined(__STDC_FORMAT_MACROS) 12482 #define __STDC_FORMAT_MACROS 12483 #endif 12484 #include 12485 #include 12487 #endif 12489 #endif 12490 ---- End code block ---------------------------------------- 12492 22. References 12494 [ITU-R_BT.601] 12495 International Telecommunication Union, "ITU BT.601: Studio 12496 encoding parameters of digital television for standard 4:3 12497 and wide screen 16:9 aspect ratios", January 2007. 12499 [Bell] Bell, T., Cleary, J., and I. Witten, "Text Compression", 12500 1990. 12502 [Kernighan] 12503 Kernighan, B. and D. Ritchie, "The C Programming Language 12504 (2nd edition)", April 1988. 12506 [Loeffler] 12507 Loeffler, C., Ligtenberg , A., and G. Moschytz, "Practical 12508 Fast 1-D DCT Algorithms with 11 Multiplications", 12509 May 1989. 12511 [Shannon] Shannon, C., "A Mathematical Theory of Communication", 12512 Bell System Technical Journal Vol. 27, pp. 379-423, 623- 12513 656, July, October 1948. 12515 Authors' Addresses 12517 James Bankoski 12518 Google, Inc. 12520 Email: jimbankoski@google.com 12522 Paul Wilkins 12523 Google, Inc. 12525 Email: paulwilkins@google.com 12527 Yaowu Xu 12528 Google, Inc. 12530 Email: yaowu@google.com