idnits 2.17.00 (12 Aug 2021) /tmp/idnits15721/draft-bankoski-vp8-bitstream-02.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 11686: '... member \ref MUST be properly init...' RFC 2119 keyword, line 11687: '... but \ref MAY be clobbered by t...' RFC 2119 keyword, line 11688: '... algorithm. This parameter \ref MAY be...' RFC 2119 keyword, line 11706: '... member \ref MUST be properly init...' RFC 2119 keyword, line 11707: '... 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 634 has weird spacing: '...typedef sign...' == Line 643 has weird spacing: '...def int int32...' == Line 6200 has weird spacing: '...ned int split...' == Line 6641 has weird spacing: '...decoder bool;...' == Line 7076 has weird spacing: '...tioning parti...' == (34 more instances...) -- The document date (May 18, 2011) is 4020 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 9958 == Missing Reference: 'A' is mentioned on line 2047, but not defined == Missing Reference: 'L' is mentioned on line 2047, but not defined -- Looks like a reference, but probably isn't: '8' on line 11084 == Missing Reference: '-1' is mentioned on line 9541, but not defined -- Looks like a reference, but probably isn't: '0' on line 12011 -- Looks like a reference, but probably isn't: '1' on line 12000 -- Looks like a reference, but probably isn't: '4' on line 9931 -- Looks like a reference, but probably isn't: '7' on line 9208 -- Looks like a reference, but probably isn't: '9' on line 8667 -- Looks like a reference, but probably isn't: '2' on line 11561 -- Looks like a reference, but probably isn't: '5' on line 9578 -- Looks like a reference, but probably isn't: '6' on line 9208 -- Looks like a reference, but probably isn't: '16' on line 10840 -- Looks like a reference, but probably isn't: '12' on line 10794 == Missing Reference: 'MVPcount' is mentioned on line 4887, but not defined == Missing Reference: 'MVPsign' is mentioned on line 4936, but not defined == Missing Reference: '-4096' is mentioned on line 5067, but not defined -- Looks like a reference, but probably isn't: '4095' on line 5067 -- Looks like a reference, but probably isn't: '128' on line 6311 == Missing Reference: 'SIGN' is mentioned on line 8148, but not defined == Missing Reference: 'NEAREST' is mentioned on line 8463, but not defined == Missing Reference: 'NEAR' is mentioned on line 8467, but not defined == Missing Reference: 'BEST' is mentioned on line 8482, but not defined -- Looks like a reference, but probably isn't: '15' on line 8484 -- Looks like a reference, but probably isn't: '10' on line 8667 -- Looks like a reference, but probably isn't: '18' on line 8810 -- Looks like a reference, but probably isn't: '14' on line 8822 == Missing Reference: '-stride' is mentioned on line 9064, but not defined == Missing Reference: '-2' is mentioned on line 9540, but not defined -- Looks like a reference, but probably isn't: '25' on line 10801 -- Looks like a reference, but probably isn't: '80' on line 12027 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: November 19, 2011 Google, Inc. 6 May 18, 2011 8 VP8 Data Format and Decoding Guide 9 draft-bankoski-vp8-bitstream-02 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 November 19, 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 . . . . . . . . . . . . . . . . . . . . . . . . 4 48 2. Format Overview . . . . . . . . . . . . . . . . . . . . . . . 6 49 3. Compressed Frame Types . . . . . . . . . . . . . . . . . . . 8 50 4. Overview of Compressed Data Format . . . . . . . . . . . . . 9 51 5. Overview of the Decoding Process . . . . . . . . . . . . . . 11 52 6. Description of Algorithms . . . . . . . . . . . . . . . . . . 16 53 7. Boolean Entropy Decoder . . . . . . . . . . . . . . . . . . . 19 54 7.1. Underlying Theory of Coding . . . . . . . . . . . . . . 20 55 7.2. Practical Algorithm Description . . . . . . . . . . . . 21 56 7.3. Actual Implementation . . . . . . . . . . . . . . . . . 23 57 8. Compressed Data Components . . . . . . . . . . . . . . . . . 28 58 8.1. Tree Coding Implementation . . . . . . . . . . . . . . . 30 59 8.2. Tree Coding Example . . . . . . . . . . . . . . . . . . 31 60 9. Frame Header . . . . . . . . . . . . . . . . . . . . . . . . 34 61 9.1. Uncompressed Data Chunk . . . . . . . . . . . . . . . . 34 62 9.2. Color Space and Pixel Type (Key Frames-only) . . . . . . 37 63 9.3. Segment-based Adjustments . . . . . . . . . . . . . . . 37 64 9.4. Loop Filter Type and Levels . . . . . . . . . . . . . . 38 65 9.5. Token Partition and Partition Data Offsets . . . . . . . 39 66 9.6. Dequantization Indices . . . . . . . . . . . . . . . . . 40 67 9.7. Refresh Golden Frame and AltRef Frame . . . . . . . . . 41 68 9.8. Refresh Last Frame Buffer . . . . . . . . . . . . . . . 42 69 9.9. DCT Coefficient Probability Update . . . . . . . . . . . 42 70 9.10. Remaining Frame Header Data (non-Key Frame) . . . . . . 43 71 9.11. Remaining Frame Header Data (Key Frame) . . . . . . . . 44 72 10. Segment-based Feature Adjustments . . . . . . . . . . . . . . 45 73 11. Key Frame Macroblock Prediction Records . . . . . . . . . . . 46 74 11.1. mb_skip_coeff . . . . . . . . . . . . . . . . . . . . . 46 75 11.2. Luma Modes . . . . . . . . . . . . . . . . . . . . . . . 46 76 11.3. Subblock Mode Contexts . . . . . . . . . . . . . . . . . 49 77 11.4. Chroma Modes . . . . . . . . . . . . . . . . . . . . . . 50 78 11.5. Subblock Mode Probability Table . . . . . . . . . . . . 51 79 12. Intraframe Prediction . . . . . . . . . . . . . . . . . . . . 55 80 12.1. mb_skip_coeff . . . . . . . . . . . . . . . . . . . . . 55 81 12.2. Chroma Prediction . . . . . . . . . . . . . . . . . . . 56 82 12.3. Luma Prediction . . . . . . . . . . . . . . . . . . . . 58 83 13. DCT Coefficient Decoding . . . . . . . . . . . . . . . . . . 65 84 13.1. MB Without non-Zero Coefficient Values . . . . . . . . . 65 85 13.2. Coding of Individual Coefficient Values . . . . . . . . 66 86 13.3. Token Probabilities . . . . . . . . . . . . . . . . . . 68 87 13.4. Token Probability Updates . . . . . . . . . . . . . . . 72 88 13.5. Default Token Probability Table . . . . . . . . . . . . 77 89 14. DCT and WHT Inversion and Macroblock Reconstruction . . . . . 82 90 14.1. Dequantization . . . . . . . . . . . . . . . . . . . . . 82 91 14.2. Inverse Transforms . . . . . . . . . . . . . . . . . . . 83 92 14.3. Implementation of the WHT Inversion . . . . . . . . . . 84 93 14.4. Implementation of the DCT Inversion . . . . . . . . . . 86 94 14.5. Summation of Predictor and Residue . . . . . . . . . . . 89 95 15. Loop Filter . . . . . . . . . . . . . . . . . . . . . . . . . 90 96 15.1. Filter Geometry and Overall Procedure . . . . . . . . . 91 97 15.2. Simple Filter . . . . . . . . . . . . . . . . . . . . . 93 98 15.3. Normal Filter . . . . . . . . . . . . . . . . . . . . . 97 99 15.4. Calculation of Control Parameters . . . . . . . . . . . 102 100 16. Interframe Macroblock Prediction Records . . . . . . . . . . 104 101 16.1. Intra-Predicted Macroblocks . . . . . . . . . . . . . . 104 102 16.2. Inter-Predicted Macroblocks . . . . . . . . . . . . . . 105 103 16.3. Mode and Motion Vector Contexts . . . . . . . . . . . . 106 104 16.4. Split Prediction . . . . . . . . . . . . . . . . . . . . 112 105 17. Motion Vector Decoding . . . . . . . . . . . . . . . . . . . 116 106 17.1. Coding of Each Component . . . . . . . . . . . . . . . . 116 107 17.2. Probability Updates . . . . . . . . . . . . . . . . . . 118 108 18. Interframe Prediction . . . . . . . . . . . . . . . . . . . . 121 109 18.1. Bounds on and Adjustment of Motion Vectors . . . . . . . 121 110 18.2. Prediction Subblocks . . . . . . . . . . . . . . . . . . 122 111 18.3. Sub-pixel Interpolation . . . . . . . . . . . . . . . . 123 112 18.4. Filter Properties . . . . . . . . . . . . . . . . . . . 126 113 19. Annex A: Bitstream Syntax . . . . . . . . . . . . . . . . . . 129 114 19.1. Uncompressed Data Chunk . . . . . . . . . . . . . . . . 129 115 19.2. Frame Header . . . . . . . . . . . . . . . . . . . . . . 131 116 19.3. Macroblock Data . . . . . . . . . . . . . . . . . . . . 143 117 20. Attachment One: Reference Decoder Source Code . . . . . . . . 147 118 20.1. bit_ops.h . . . . . . . . . . . . . . . . . . . . . . . 147 119 20.2. bool_decoder.h . . . . . . . . . . . . . . . . . . . . . 147 120 20.3. dequant_data.h . . . . . . . . . . . . . . . . . . . . . 151 121 20.4. dixie.c . . . . . . . . . . . . . . . . . . . . . . . . 151 122 20.5. dixie.h . . . . . . . . . . . . . . . . . . . . . . . . 163 123 20.6. dixie_loopfilter.c . . . . . . . . . . . . . . . . . . . 170 124 20.7. dixie_loopfilter.h . . . . . . . . . . . . . . . . . . . 182 125 20.8. idct_add.c . . . . . . . . . . . . . . . . . . . . . . . 182 126 20.9. idct_add.h . . . . . . . . . . . . . . . . . . . . . . . 186 127 20.10. mem.h . . . . . . . . . . . . . . . . . . . . . . . . . 186 128 20.11. modemv.c . . . . . . . . . . . . . . . . . . . . . . . . 188 129 20.12. modemv.h . . . . . . . . . . . . . . . . . . . . . . . . 203 130 20.13. modemv_data.h . . . . . . . . . . . . . . . . . . . . . 203 131 20.14. predict.c . . . . . . . . . . . . . . . . . . . . . . . 208 132 20.15. predict.h . . . . . . . . . . . . . . . . . . . . . . . 238 133 20.16. tokens.c . . . . . . . . . . . . . . . . . . . . . . . . 238 134 20.17. tokens.h . . . . . . . . . . . . . . . . . . . . . . . . 248 135 20.18. vp8_prob_data.h . . . . . . . . . . . . . . . . . . . . 257 136 20.19. vpx_codec_internal.h . . . . . . . . . . . . . . . . . . 266 137 20.20. vpx_decoder.h . . . . . . . . . . . . . . . . . . . . . 276 138 20.21. vpx_integer.h . . . . . . . . . . . . . . . . . . . . . 283 139 21. References . . . . . . . . . . . . . . . . . . . . . . . . . 286 140 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 287 142 1. Introduction 144 This document describes the VP8 compressed video data format, 145 together with a discussion of the decoding procedure for the format. 146 It is intended to be used in conjunction with and as a guide to the 147 reference decoder source code provided in Attachment One. If there 148 are any conflicts between this narrative and the reference source 149 code, the reference source code should be considered correct. The 150 bitstream is defined by the reference source code and not this 151 narrative. 153 Like many modern video compression schemes, VP8 is based on 154 decomposition of frames into square subblocks of pixels, prediction 155 of such subblocks using previously constructed blocks, and adjustment 156 of such predictions (as well as synthesis of unpredicted blocks) 157 using a discrete cosine transform (hereafter abbreviated as DCT). In 158 one special case, however, VP8 uses a "Walsh-Hadamard" transform 159 (hereafter abbreviated as WHT) instead of a DCT. 161 Roughly speaking, such systems reduce datarate by exploiting the 162 temporal and spatial coherence of most video signals. It is more 163 efficient to specify the location of a visually similar portion of a 164 prior frame than it is to specify pixel values. The frequency 165 segregation provided by the DCT and WHT facilitate the exploitation 166 of both spatial coherence in the original signal and the tolerance of 167 the human visual system to moderate losses of fidelity in the 168 reconstituted signal. 170 VP8 augments these basic concepts with, among other things, 171 sophisticated usage of contextual probabilities. The result is a 172 significant reduction in datarate at a given quality. 174 Unlike some similar schemes (the older MPEG formats, for example), 175 VP8 specifies exact values for reconstructed pixels. Specifically, 176 the specification for the DCT and WHT portions of the reconstruction 177 does not allow for any "drift" caused by truncation of fractions. 178 Rather, the algorithm is specified using fixed-precision integer 179 operations exclusively. This greatly facilitates the verification of 180 the correctness of a decoder implementation as well as avoiding 181 difficult-to-predict visual incongruities between such 182 implementations. 184 It should be remarked that, in a complete video playback system, the 185 displayed frames may or may not be identical to the reconstructed 186 frames. Many systems apply a final level of filtering (commonly 187 referred to as postprocessing) to the reconstructed frames prior to 188 viewing. Such postprocessing has no effect on the decoding and 189 reconstruction of subsequent frames (which are predicted using the 190 completely-specified reconstructed frames) and is beyond the scope of 191 this document. In practice, the nature and extent of this sort of 192 postprocessing is dependent on both the taste of the user and on the 193 computational facilities of the playback environment. 195 2. Format Overview 197 VP8 works exclusively with an 8-bit YUV 4:2:0 image format. In this 198 format, each 8-bit pixel in the two chroma planes (U and V) 199 corresponds positionally to a 2x2 block of 8-bit luma pixels in the Y 200 plane; coordinates of the upper left corner of the Y block are of 201 course exactly twice the coordinates of the corresponding chroma 202 pixels. When we refer to pixels or pixel distances without 203 specifying a plane, we are implicitly referring to the Y plane or to 204 the complete image, both of which have the same (full) resolution. 206 As is usually the case, the pixels are simply a large array of bytes 207 stored in rows from top to bottom, each row being stored from left to 208 right. This "left to right" then "top to bottom" raster-scan order 209 is reflected in the layout of the compressed data as well. 211 Provision has been made in the VP8 bitstream header for the support 212 of a secondary YUV color format, in the form of a reserved bit. 214 Occasionally, at very low datarates, a compression system may decide 215 to reduce the resolution of the input signal to facilitate efficient 216 compression. The VP8 data format supports this via optional 217 upscaling of its internal reconstruction buffer prior to output (this 218 is completely distinct from the optional postprocessing discussed 219 earlier, which has nothing to do with decoding per se). This 220 upsampling restores the video frames to their original resolution. 221 In other words, the compression/decompression system can be viewed as 222 a "black box", where the input and output is always at a given 223 resolution. The compressor might decide to "cheat" and process the 224 signal at a lower resolution. In that case, the decompressor needs 225 the ability to restore the signal to its original resolution. 227 Internally, VP8 decomposes each output frame into an array of 228 macroblocks. A macroblock is a square array of pixels whose Y 229 dimensions are 16x16 and whose U and V dimensions are 8x8. 230 Macroblock-level data in a compressed frame occurs (and must be 231 processed) in a raster order similar to that of the pixels comprising 232 the frame. 234 Macroblocks are further decomposed into 4x4 subblocks. Every 235 macroblock has 16 Y subblocks, 4 U subblocks, and 4 V subblocks. Any 236 subblock-level data (and processing of such data) again occurs in 237 raster order, this time in raster order within the containing 238 macroblock. 240 As discussed in further detail below, data can be specified at the 241 levels of both macroblocks and their subblocks. 243 Pixels are always treated, at a minimum, at the level of subblocks, 244 which may be thought of as the "atoms" of the VP8 algorithm. In 245 particular, the 2x2 chroma blocks corresponding to 4x4 Y subblocks 246 are never treated explicitly in the data format or in the algorithm 247 specification. 249 The DCT and WHT always operate at a 4x4 resolution. The DCT is used 250 for the 16Y, 4U and 4V subblocks. The WHT is used (with some but not 251 all prediction modes) to encode a 4x4 array comprising the average 252 intensities of the 16 Y subblocks of a macroblock. These average 253 intensities are, up to a constant normalization factor, nothing more 254 that the zeroth DCT coefficients of the Y subblocks. This "higher- 255 level" WHT is a substitute for the explicit specification of those 256 coefficients, in exactly the same way as the DCT of a subblock 257 substitutes for the specification of the pixel values comprising the 258 subblock. We consider this 4x4 array as a second-order subblock 259 called Y2, and think of a macroblock as containing 24 "real" 260 subblocks and, sometimes, a 25th "virtual" subblock. This is dealt 261 with further in Chapter 13. 263 The frame layout used by the reference decoder may be found in the 264 file yv12config.h. 266 3. Compressed Frame Types 268 There are only two types of frames in VP8. 270 Intraframes (also called key frames and, in MPEG terminology, 271 I-frames) are decoded without reference to any other frame in a 272 sequence, that is, the decompressor reconstructs such frames 273 beginning from its "default" state. Key frames provide random access 274 (or seeking) points in a video stream. 276 Interframes (also called prediction frames and, in MPEG terminology, 277 P-frames) are encoded with reference to prior frames, specifically 278 all prior frames up to and including the most recent key frame. 279 Generally speaking, the correct decoding of an interframe depends on 280 the correct decoding of the most recent key frame and all ensuing 281 frames. Consequently, the decoding algorithm is not tolerant of 282 dropped frames: In an environment in which frames may be dropped or 283 corrupted, correct decoding will not be possible until a key frame is 284 correctly received. 286 In contrast to MPEG, there is no use of bidirectional prediction. No 287 frame is predicted using frames temporally subsequent to it; there is 288 no analog to an MPEG B-frame. 290 Secondly, VP8 augments these notions with that of alternate 291 prediction frames, called golden frames and altref frames 292 (alternative reference frames). Blocks in an interframe may be 293 predicted using blocks in the immediately previous frame as well as 294 the most recent golden frame or altref frame. Every key frame is 295 automatically golden and altref, and any interframe may optionally 296 replace the most recent golden or altref frame. 298 Golden frames and altref frames may also be used to partially 299 overcome the intolerance to dropped frames discussed above: If a 300 compressor is configured to code golden frames only with reference to 301 the prior golden frame (and key frame) then the "substream" of key 302 and golden frames may be decoded regardless of loss of other 303 interframes. Roughly speaking, the implementation requires (on the 304 compressor side) that golden frames subsume and recode any context 305 updates effected by the intervening interframes. A typical 306 application of this approach is video conferencing, in which 307 retransmission of a prior golden frame and/or a delay in playback 308 until receipt of the next golden frame is preferable to a larger 309 retransmit and/or delay until the next key frame. 311 4. Overview of Compressed Data Format 313 The input to a VP8 decoder is a sequence of compressed frames whose 314 order matches their order in time. Issues such as the duration of 315 frames, the corresponding audio, and synchronization are generally 316 provided by the playback environment and are irrelevant to the 317 decoding process itself, however, to aid in fast seeking a start code 318 is included in the header of each key frame. 320 The decoder is simply presented with a sequence of compressed frames 321 and produces a sequence of decompressed (reconstructed) YUV frames 322 corresponding to the input sequence. As stated in the introduction, 323 the exact pixel values in the reconstructed frame are part of VP8's 324 specification. This document specifies the layout of the compressed 325 frames and gives unambiguous algorithms for the correct production of 326 reconstructed frames. 328 The first frame presented to the decompressor is of course a key 329 frame. This may be followed by any number of interframes; the 330 correct reconstruction of each frame depends on all prior frames up 331 to the key frame. The next key frame restarts this process: The 332 decompressor resets to its default initial condition upon reception 333 of a key frame and the decoding of a key frame (and its ensuing 334 interframes) is completely independent of any prior decoding. 336 At the highest level, every compressed frame has three or more 337 pieces. It begins with an uncompressed data chunk comprising 10 338 bytes in the case of key frames and 3-bytes for inter frames. This 339 is followed by two or more blocks of compressed data (called 340 partitions). These compressed data partitions begin and end on byte 341 boundaries. 343 The first compressed partition has two subsections: 345 1. Header information that applies to the frame as a whole. 347 2. Per-macroblock information specifying how each macroblock is 348 predicted from the already-reconstructed data that is available 349 to the decompressor. 351 As stated above, the macroblock-level information occurs in raster- 352 scan order. 354 The rest of the partitions contain, for each block, the DCT/WHT 355 coefficients (quantized and logically compressed) of the residue 356 signal to be added to the predicted block values. It typically 357 accounts for roughly 70% of the overall datarate. VP8 supports 358 packing the compressed DCT/WHT coefficients' data from macroblock 359 rows into separate partitions. If there is more than one partition 360 for these coefficients, the sizes of the partitions -- except the 361 last partition -- in bytes are also present in the bitstream right 362 after the above first partition. Each of the sizes is a 3-byte data 363 item written in little endian format. These sizes provide the 364 decoder direct access to all DCT/WHT coefficient partitions, which 365 enables parallel processing of the coefficients in a decoder. 367 The separate partitioning of the prediction data and coefficient data 368 also allows flexibility in the implementation of a decompressor: An 369 implementation may decode and store the prediction information for 370 the whole frame and then decode, transform, and add the residue 371 signal to the entire frame, or it may simultaneously decode both 372 partitions, calculating prediction information and adding in the 373 residue signal for each block in order. The length field in the 374 frame tag, which allows decoding of the second partition to begin 375 before the first partition has been completely decoded, is necessary 376 for the second "block-at-a-time" decoder implementation. 378 All partitions are decoded using separate instances of the boolean 379 entropy decoder described in Chapter 7. Although some of the data 380 represented within the partitions is conceptually "flat" (a bit is 381 just a bit with no probabilistic expectation one way or the other), 382 because of the way such coders work, there is never a direct 383 correspondence between a "conceptual bit" and an actual physical bit 384 in the compressed data partitions. Only in the 3 or 10 byte 385 uncompressed chunk described above is there such a physical 386 correspondence. 388 A related matter, which is true for most lossless compression 389 formats, is that seeking within a partition is not supported. The 390 data must be decompressed and processed (or at least stored) in the 391 order in which it occurs in the partition. 393 While this document specifies the ordering of the partition data 394 correctly, the details and semantics of this data are discussed in a 395 more logical fashion to facilitate comprehension. For example, the 396 frame header contains updates to many probability tables used in 397 decoding per-macroblock data. The latter is often described before 398 the layouts of the probabilities and their updates, even though this 399 is the opposite of their order in the bitstream. 401 5. Overview of the Decoding Process 403 A VP8 decoder needs to maintain four YUV frame buffers whose 404 resolutions are at least equal to that of the encoded image. These 405 buffers hold the current frame being reconstructed, the immediately 406 previous reconstructed frame, the most recent golden frame, and the 407 most recent altref frame. 409 Most implementations will wish to "pad" these buffers with 410 "invisible" pixels that extend a moderate number of pixels beyond all 411 four edges of the visible image. This simplifies interframe 412 prediction by allowing all (or most) prediction blocks -- which are 413 not guaranteed to lie within the visible area of a prior frame -- to 414 address usable image data. 416 Regardless of the amount of padding chosen, the invisible rows above 417 (below) the image are filled with copies of the top (bottom) row of 418 the image; the invisible columns to the left (right) of the image are 419 filled with copies of the leftmost (rightmost) visible row; and the 420 four invisible corners are filled with copies of the corresponding 421 visible corner pixels. The use of these prediction buffers (and 422 suggested sizes for the halo) will be elaborated on in the discussion 423 of motion vectors, interframe prediction, and sub-pixel interpolation 424 later in this document. 426 As will be seen in the description of the frame header, the image 427 dimensions are specified (and can change) with every key frame. 428 These buffers (and any other data structures whose size depends on 429 the size of the image) should be allocated (or re-allocated) 430 immediately after the dimensions are decoded. 432 Leaving most of the details for later elaboration, the following is 433 an outline the decoding process. 435 First, the frame header (beginning of the first data partition) is 436 decoded. Altering or augmenting the maintained state of the decoder, 437 this provides the context in which the per-macroblock data can be 438 interpreted. 440 The macroblock data occurs (and must be processed) in raster-scan 441 order. This data comes in two or more parts. The first (prediction 442 or mode) part comes in the remainder of the first data partition. 443 The other parts comprise the data partition(s) for the DCT/WHT 444 coefficients of the residue signal. For each macroblock, the 445 prediction data must be processed before the residue. 447 Each macroblock is predicted using one (and only one) of four 448 possible frames. All macroblocks in a key frame, and all intra-coded 449 macroblocks in an interframe, are predicted using the already-decoded 450 macroblocks in the current frame. Macroblocks in an interframe may 451 also be predicted using the previous frame, the golden frame or the 452 altref frame. Such macroblocks are said to be inter-coded. 454 The purpose of prediction is to use already-constructed image data to 455 approximate the portion of the original image being reconstructed. 456 The effect of any of the prediction modes is then to write a 457 macroblock-sized prediction buffer containing this approximation. 459 Regardless of the prediction method, the residue DCT signal is 460 decoded, dequantized, reverse-transformed, and added to the 461 prediction buffer to produce the (almost final) reconstruction value 462 of the macroblock, which is stored in the correct position of the 463 current frame buffer. 465 The residue signal consists of 24 (sixteen Y, four U, and four V) 4x4 466 quantized and losslessly-compressed DCT transforms approximating the 467 difference between the original macroblock in the uncompressed source 468 and the prediction buffer. For most prediction modes, the zeroth 469 coefficients of the sixteen Y subblocks are expressed via a 25th WHT 470 of the second-order virtual Y2 subblock discussed above. 472 Intra-prediction exploits the spatial coherence of frames. The 16x16 473 luma (Y) and 8x8 chroma (UV) components are predicted independently 474 of each other using one of four simple means of pixel propagation, 475 starting from the already-reconstructed (16-pixel long luma, 8-pixel 476 long chroma) row above and column to the left of the current 477 macroblock. The four methods are: 479 1. Copying the row from above throughout the prediction buffer. 481 2. Copying the column from left throughout the prediction buffer. 483 3. Copying the average value of the row and column throughout the 484 prediction buffer. 486 4. Extrapolation from the row and column using the (fixed) second 487 difference (horizontal and vertical) from the upper left corner. 489 Additionally, the sixteen Y subblocks may be predicted independently 490 of each other using one of ten different modes, four of which are 4x4 491 analogs of those described above, augmented with six "diagonal" 492 prediction methods. There are two types of predictions, one intra 493 and one prediction (among all the modes), for which the residue 494 signal does not use the Y2 block to encode the DC portion of the 495 sixteen 4x4 Y subblock DCTs. This "independent Y subblock" mode has 496 no effect on the 8x8 chroma prediction. 498 Inter-prediction exploits the temporal coherence between nearby 499 frames. Except for the choice of the prediction frame itself, there 500 is no difference between inter-prediction based on the previous frame 501 and that based on the golden frame or altref frame. 503 Inter-prediction is conceptually very simple. While, for reasons of 504 efficiency, there are several methods of encoding the relationship 505 between the current macroblock and corresponding sections of the 506 prediction frame, ultimately each of the sixteen Y subblocks is 507 related to a 4x4 subblock of the prediction frame, whose position in 508 that frame differs from the current subblock position by a (usually 509 small) displacement. These two-dimensional displacements are called 510 motion vectors. 512 The motion vectors used by VP8 have quarter-pixel precision. 513 Prediction of a subblock using a motion vector that happens to have 514 integer (whole number) components is very easy: the 4x4 block of 515 pixels from the displaced block in the previous, golden, or altref 516 frame are simply copied into the correct position of the current 517 macroblock's prediction buffer. 519 Fractional displacements are conceptually and implementationally more 520 complex. They require the inference (or synthesis) of sample values 521 that, strictly speaking, do not exist. This is one of the most basic 522 problems in signal processing and readers conversant with that 523 subject will see that the approach taken by VP8 provides a good 524 balance of robustness, accuracy, and efficiency. 526 Leaving the details for the implementation discussion below, the 527 pixel interpolation is calculated by applying a kernel filter (using 528 reasonable-precision integer math) three pixels on either side, both 529 horizontally and vertically, of the pixel to be synthesized. The 530 resulting 4x4 block of synthetic pixels is then copied into position 531 exactly as in the case of integer displacements. 533 Each of the eight chroma subblocks is handled similarly. Their 534 motion vectors are never specified explicitly; instead, the motion 535 vector for each chroma subblock is calculated by averaging the 536 vectors of the four Y subblocks that occupy the same area of the 537 frame. Since chroma pixels have twice the diameter (and four times 538 the area) of luma pixels, the calculated chroma motion vectors have 539 1/8 pixel resolution, but the procedure for copying or generating 540 pixels for each subblock is essentially identical to that done in the 541 luma plane. 543 After all the macroblocks have been generated (predicted and 544 corrected with the DCT/WHT residue), a filtering step (the loop 545 filter) is applied to the entire frame. The purpose of the loop 546 filter is to reduce blocking artifacts at the boundaries between 547 macroblocks and between subblocks of the macroblocks. The term loop 548 filter is used because this filter is part of the "coding loop," that 549 is, it affects the reconstructed frame buffers that are used to 550 predict ensuing frames. This is distinguished from the 551 postprocessing filters discussed earlier which affect only the viewed 552 video and do not "feed into" subsequent frames. 554 Next, if signaled in the data, the current frame may replace the 555 golden frame prediction buffer and/or the altref frame buffer. 557 The halos of the frame buffers are next filled as specified above. 558 Finally, at least as far as decoding is concerned, the (references 559 to) the "current" and "last" frame buffers should be exchanged in 560 preparation for the next frame. 562 Various processes may be required (or desired) before viewing the 563 generated frame. As discussed in the frame dimension information 564 below, truncation and/or upscaling of the frame may be required. 565 Some playback systems may require a different frame format (RGB, 566 YUY2, etc.). Finally, as mentioned in the introduction, further 567 postprocessing or filtering of the image prior to viewing may be 568 desired. Since the primary purpose of this document is a decoding 569 specification, the postprocessing is not specified in this document. 571 While the basic ideas of prediction and correction used by VP8 are 572 straightforward, many of the details are quite complex. The 573 management of probabilities is particularly elaborate. Not only do 574 the various modes of intra-prediction and motion vector specification 575 have associated probabilities but they, together with the coding of 576 DCT coefficients and motion vectors, often base these probabilities 577 on a variety of contextual information (calculated from what has been 578 decoded so far), as well as on explicit modification via the frame 579 header. 581 The "top-level" of decoding and frame reconstruction is implemented 582 in the reference decoder files onyxd_if.c and decodframe.c. 584 This concludes our summary of decoding and reconstruction; we 585 continue by discussing the individual aspects in more depth. 587 A reasonable "divide and conquer" approach to implementation of a 588 decoder is to begin by decoding streams composed exclusively of key 589 frames. After that works reliably, interframe handling can be added 590 more easily than if complete functionality were attempted 591 immediately. In accordance with this, we first discuss components 592 needed to decode key frames (most of which are also used in the 593 decoding of interframes) and conclude with topics exclusive to 594 interframes. 596 6. Description of Algorithms 598 As the intent of this document, together with the reference decoder 599 source code, is to specify a platform-independent procedure for the 600 decoding and reconstruction of a VP8 video stream, many (small) 601 algorithms must be described exactly. 603 Due to its near-universality, terseness, ability to easily describe 604 calculation at specific precisions, and the fact that On2's reference 605 VP8 decoder is written in C, these algorithm fragments are written 606 using the C programming language, augmented with a few simple 607 definitions below. 609 The standard (and best) reference for C is [Kernighan]. 611 Many code fragments will be presented in this document. Some will be 612 nearly identical to corresponding sections of the reference decoder; 613 others will differ. Roughly speaking, there are three reasons for 614 such differences: 616 1. For reasons of efficiency, the reference decoder version may be 617 less obvious. 619 2. The reference decoder often uses large data structures to 620 maintain context that need not be described or used here. 622 3. The authors of this document felt that a different expression of 623 the same algorithm might facilitate exposition. 625 Regardless of the chosen presentation, the calculation effected by 626 any of the algorithms described here is identical to that effected by 627 the corresponding portion of the reference decoder. 629 All VP8 decoding algorithms use integer math. To facilitate 630 specification of arithmetic precision, we define the following types. 632 ---- Begin code block -------------------------------------- 634 typedef signed char int8; /* signed int exactly 8 bits wide */ 635 typedef unsigned char uint8; /* unsigned "" */ 637 typedef short int16; /* signed int exactly 16 bits wide */ 638 typedef unsigned int16 uint16; /* unsigned "" */ 640 /* int32 is a signed integer type at least 32 bits wide */ 642 typedef long int32; /* guaranteed to work on all systems */ 643 typedef int int32; /* will be more efficient on some systems */ 645 typedef unsigned int32 uint32; 647 /* unsigned integer type, at least 16 bits wide, whose exact size 648 is most convenient to whatever processor we are using */ 650 typedef unsigned int uint; 652 /* While pixels themselves are 8-bit unsigned integers, 653 pixel arithmetic often occurs at 16- or 32-bit precision and 654 the results need to be "saturated" or clamped to an 8-bit 655 range. */ 657 typedef uint8 Pixel; 659 Pixel clamp255( int32 v) { return v < 0? 0 : (v < 255? v : 255);} 661 /* As is elaborated in the discussion of the bool_decoder below, 662 VP8 represents probabilities as unsigned 8-bit numbers. */ 664 typedef uint8 Prob; 666 ---- End code block ---------------------------------------- 668 We occasionally need to discuss mathematical functions involving 669 honest-to-goodness "infinite precision" real numbers. The DCT is 670 first described via the cosine function cos; the ratio of the lengths 671 of the circumference and diameter of a circle is denoted pi; at one 672 point, we take a (base 1/2) logarithm denoted log; and pow( x, y) 673 denotes x raised to the power y. If x = 2 and y is a small non- 674 negative integer, pow( 2, y) may be expressed in C as 1 << y. 676 Finally, we sometimes need to divide signed integers by powers of 677 two, that is, we occasionally right-shift signed numbers. The 678 behavior of such shifts (i.e., the propagation of the sign bit) is, 679 perhaps surprisingly, not defined by the C language itself and is 680 left up to individual compilers. Because of the utility of this 681 frequently needed operation, it is at least arguable that it should 682 be defined by the language (to naturally propagate the sign bit) and, 683 at a minimum, should be correctly implemented by any reasonable 684 compiler. In the interest of strict portability, we attempt to call 685 attention to these shifts when they arise. 687 7. Boolean Entropy Decoder 689 As discussed in the overview above, essentially the entire VP8 data 690 stream is encoded using a boolean entropy coder. 692 An understanding of the bool_decoder is critical to the 693 implementation of a VP8 decompressor, so we discuss in detail. It is 694 easier to comprehend the bool_decoder in conjunction with the 695 bool_encoder used by the compressor to write the compressed data 696 partitions. 698 The bool_encoder encodes (and the bool_decoder decodes) one bool 699 (zero-or-one boolean value) at a time. Its purpose is to losslessly 700 compress a sequence of bools for which the probability of their being 701 zero or one can be well-estimated (via constant or previously-coded 702 information) at the time they are written, using identical 703 corresponding probabilities at the time they are read. 705 As the reader is probably aware, if a bool is much more likely to be 706 zero than one (for instance), it can, on average, be faithfully 707 encoded using much less than one bit per value. The bool_encoder 708 exploits this. 710 In the 1940s, [Shannon] proved that there is a lower bound for the 711 average datarate of a faithful encoding of a sequence of bools (whose 712 probability distributions are known and are independent of each 713 other) and also that there are encoding algorithms that approximate 714 this lower bound as closely as one wishes. 716 If we encode a sequence of bools whose probability of being zero is p 717 (and whose probability of being 1 is 1-p), the lowest possible 718 datarate per value is 720 plog(p) + (1-p)log(1-p); 722 taking the logarithms to the base 1/2 expresses the datarate in bits/ 723 value. 725 We give two simple examples. At one extreme, if p=1/2, then log(p) = 726 log(1-p) = 1 and the lowest possible datarate per bool is 1/2 + 1/2 = 727 1, that is, we cannot do any better than simply literally writing out 728 bits. At another extreme, if p is very small, say p=1/1024, then 729 log(p)=10, log(1-p) is roughly .0014, and the lowest possible 730 datarate is approximately 10/1024 + .0014, roughly 1/100 of a bit per 731 bool. 733 Because most of the bools in the VP8 datastream have zero- 734 probabilities nowhere near 1/2, the compression provided by the 735 bool_encoder is critical to the performance of VP8. 737 The bool coder used by VP8 is a variant of an arithmetic coder. An 738 excellent discussion of arithmetic coding (and other lossless 739 compression techniques) can be found in [Bell]. 741 7.1. Underlying Theory of Coding 743 The basic idea used by the bool coder is to consider the entire data 744 stream (either of the partitions in our case) as the binary expansion 745 of a single number x with 0 <= x < 1. The bits (or bytes) in x are 746 of course written from high to low order and if b[j] (B[j]) is the 747 j^(th) bit (byte) in the partition, the value x is simply the sum 748 (starting with j = 1) of pow(2, -j) * b[j] or pow(256, -j) * B[j]. 750 Before the first bool is coded, all values of x are possible. 752 The coding of each bool restricts the possible values of x in 753 proportion to the probability of what is coded. If p1 is the 754 probability of the first bool being zero and a zero is coded, the 755 range of possible x is restricted to 0 <= x < p1. If a one is coded, 756 the range becomes p1 <= x < 1. 758 The coding continues by repeating the same idea. At every stage, 759 there is an interval a <= x < b of possible values of x. If p is the 760 probability of a zero being coded at this stage and a zero is coded, 761 the interval becomes a <= x < a + (p(b-a)). If a one is coded, the 762 possible x are restricted to a + (p(b-a)) <= x < b. 764 Assuming only finitely many values are to be coded, after the encoder 765 has received the last bool, it can write as its output any value x 766 that lies in the final interval. VP8 simply writes the left endpoint 767 of the final interval. Consequently, the output it would make if 768 encoding were to stop at any time either increases or stays the same 769 as each bool is encoded. 771 Decoding parallels encoding. The decoder is presented with the 772 number x, which has only the initial restriction 0 <= x < 1. To 773 decode the first bool, the decoder is given the first probability p1. 774 If x < p1, a zero is decoded; if x >= p1, a one is decoded. In 775 either case, the new restriction on x, that is, the interval of 776 possible x, is remembered. 778 Decoding continues in exactly the same way: If a <= x < b is the 779 current interval and we are to decode a bool with zero-probability p, 780 we return a zero if a <= x < a + (p(b-a)) and a one if a + (p(b-a)) 781 <= x < b. In either case, the new restriction is remembered in 782 preparation for decoding the next bool. 784 The process outlined above uses real numbers of infinite precision to 785 express the probabilities and ranges. It is true that, if one could 786 actualize this process and coded a large number of bools whose 787 supplied probabilities matched their value distributions, the 788 datarate achieved would approach the theoretical minimum as the 789 number of bools encoded increased. 791 Unfortunately, computers operate at finite precision and an 792 approximation to the theoretically perfect process described above is 793 necessary. Such approximation increases the datarate but, at quite 794 moderate precision and for a wide variety of data sets, this increase 795 is negligible. 797 The only conceptual limitations are, first, that coder probabilities 798 must be expressed at finite precision and, second, that the decoder 799 be able to detect each individual modification to the value interval 800 via examination of a fixed amount of input. As a practical matter, 801 many of the implementation details stem from the fact that the coder 802 can function using only a small "window" to incrementally read or 803 write the arbitrarily precise number x. 805 7.2. Practical Algorithm Description 807 VP8's bool coder works with 8-bit probabilities p. The range of such 808 p is 0 <= p <= 255; the actual probability represented by p is p/256. 809 Also, the coder is designed so that decoding of a bool requires no 810 more than an 8-bit comparison and so that the state of both the 811 encoder and decoder can be easily represented using a small number of 812 unsigned 16-bit integers. 814 The details are most easily understood if we first describe the 815 algorithm using bit-at-a-time input and output. Aside from the 816 ability to maintain a position in this bitstream and write/read bits, 817 the encoder also needs the ability to add 1 to the bits already 818 output; after writing n bits, adding 1 to the existing output is the 819 same thing as adding pow( 2, -n) to x. 821 Together with the bit position, the encoder must maintain two 822 unsigned 8-bit numbers which we call bottom and range. Writing w for 823 the n bits already written and S = pow( 2, - n - 8) for the scale of 824 the current bit position one byte out, we have the following 825 constraint on all future values v of w (including the final value v = 826 x): 828 w + ( S * bottom ) <= v < w + ( S * ( bottom + range ) ) 830 Thus, appending bottom to the already-written bits w gives the left 831 endpoint of the interval of possible values, appending bottom + range 832 gives the right endpoint, range itself (scaled to the current output 833 position) is the length of the interval. 835 So that our probabilistic encodings are reasonably accurate, we do 836 not let range vary by more than a factor of two: It stays within the 837 bounds 128 <= range <= 255. 839 The process for encoding a boolean value val whose probability of 840 being zero is prob / 256 -- and whose probability of being one is ( 841 256 - prob ) / 256 -- with 1 <= prob <= 255 is as follows. 843 Using an unsigned 16-bit multiply followed by an unsigned right 844 shift, we calculate an unsigned 8-bit split value: 846 split = 1 + (((range - 1) * probability)]] >> 8) 848 split is approximately ( prob / 256 ) * range and lies within the 849 bounds 1 <= split <= range - 1. These bounds ensure the correctness 850 of the decoding procedure described below. 852 If the incoming boolean val to be encoded is false, we leave the left 853 interval endpoint bottom alone and reduce range, replacing it by 854 split. If the incoming val is true, we move up the left endpoint to 855 bottom + split, propagating any carry to the already-written value w 856 (this is where we need the ability to add 1 to w), and reduce range 857 to range - split. 859 Regardless of the value encoded, range has been reduced and now has 860 the bounds 1 <= range <= 254. If range < 128, the encoder doubles it 861 and shifts the high-order bit out of bottom to the output as it also 862 doubles bottom, repeating this process one bit at a time until 128 <= 863 range <= 255. Once this is completed, the encoder is ready to accept 864 another bool, maintaining the constraints described above. 866 After encoding the last bool, the partition may be completed by 867 appending bottom to the bitstream. 869 The decoder mimics the state of the encoder. It maintains, together 870 with an input bit position, two unsigned 8-bit numbers, a range 871 identical to that maintained by the encoder and a value. Decoding 872 one bool at a time, the decoder (in effect) tracks the same left 873 interval endpoint as does the encoder and subtracts it from the 874 remaining input. Appending the unread portion of the bitstream to 875 the 8-bit value gives the difference between the actual value encoded 876 and the known left endpoint. 878 The decoder is initialized by setting range = 255 and reading the 879 first 16 input bits into value. The decoder maintains range and 880 calculates split in exactly the same way as does the encoder. 882 To decode a bool, it compares value to split; if value < split, the 883 bool is zero, and range is replaced with split. If value >= split, 884 the bool is one, range is replaced with range - split, and value is 885 replaced with value - split. 887 Again, range is doubled one bit at a time until it is at least 128. 888 The value is doubled in parallel, shifting a new input bit into the 889 bottom each time. 891 Writing Value for value together with the unread input bits and Range 892 for range extended indefinitely on the right by zeros, the condition 893 Value < Range is maintained at all times by the decoder. In 894 particular, the bits shifted out of value as it is doubled are always 895 zero. 897 7.3. Actual Implementation 899 The C code below gives complete implementations of the encoder and 900 decoder described above. While they are logically identical to the 901 "bit-at-a-time" versions, they internally buffer a couple of extra 902 bytes of the bitstream. This allows I/O to be done (more 903 practically) a byte at a time and drastically reduces the number of 904 carries the encoder has to propagate into the already-written data. 906 Another (logically equivalent) implementation may be found in the 907 reference decoder files dboolhuff.h and dboolhuff.c. 909 ---- Begin code block -------------------------------------- 911 /* Encoder first */ 913 typedef struct { 914 uint8 *output; /* ptr to next byte to be written */ 915 uint32 range; /* 128 <= range <= 255 */ 916 uint32 bottom; /* minimum value of remaining output */ 917 int bit_count; /* # of shifts before an output byte 918 is available */ 919 } bool_encoder; 921 /* Must set initial state of encoder before writing any bools. */ 923 void init_bool_encoder( bool_encoder *e, uint8 *start_partition) 924 { 925 e->output = start_partition; 926 e->range = 255; 927 e->bottom = 0; 928 e->bit_count = 24; 929 } 931 /* Encoding very rarely produces a carry that must be propagated 932 to the already-written output. The arithmetic guarantees that 933 the propagation will never go beyond the beginning of the 934 output. Put another way, the encoded value x is always less 935 than one. */ 937 void add_one_to_output( uint8 *q) 938 { 939 while( *--q == 255) 940 *q = 0; 941 ++*q; 942 } 944 /* Main function writes a bool_value whose probability of being 945 zero is (expected to be) prob/256. */ 947 void write_bool( bool_encoder *e, Prob prob, int bool_value) 948 { 949 /* split is approximately (range * prob) / 256 and, 950 crucially, is strictly bigger than zero and strictly 951 smaller than range */ 953 uint32 split = 1 + ( ((e->range - 1) * prob) >> 8); 955 if( bool_value) { 956 e->bottom += split; /* move up bottom of interval */ 957 e->range -= split; /* with corresponding decrease in range */ 958 } else 959 e->range = split; /* decrease range, leaving bottom alone */ 961 while( e->range < 128) 962 { 963 e->range <<= 1; 965 if( e->bottom & (1 << 31)) /* detect carry */ 966 add_one_to_output( e->output); 968 e->bottom <<= 1; /* before shifting bottom */ 970 if( !--e->bit_count) { /* write out high byte of bottom ... */ 972 *e->output++ = (uint8) (e->bottom >> 24); 974 e->bottom &= (1 << 24) - 1; /* ... keeping low 3 bytes */ 975 e->bit_count = 8; /* 8 shifts until next output */ 976 } 977 } 978 } 980 /* Call this function (exactly once) after encoding the last 981 bool value for the partition being written */ 983 void flush_bool_encoder( bool_encoder *e) 984 { 985 int c = e->bit_count; 986 uint32 v = e->bottom; 988 if( v & (1 << (32 - c))) /* propagate (unlikely) carry */ 989 add_one_to_output( e->output); 990 v <<= c & 7; /* before shifting remaining output */ 991 c >>= 3; /* to top of internal buffer */ 992 while( --c >= 0) 993 v <<= 8; 994 c = 4; 995 while( --c >= 0) { /* write remaining data, possibly padded */ 996 *e->output++ = (uint8) (v >> 24); 997 v <<= 8; 998 } 999 } 1001 /* Decoder state exactly parallels that of the encoder. 1002 "value", together with the remaining input, equals the 1003 complete encoded number x less the left endpoint of the 1004 current coding interval. */ 1006 typedef struct { 1007 uint8 *input; /* pointer to next compressed data byte */ 1008 uint32 range; /* always identical to encoder's range */ 1009 uint32 value; /* contains at least 8 significant bits */ 1010 int bit_count; /* # of bits shifted out of 1011 value, at most 7 */ 1012 } bool_decoder; 1014 /* Call this function before reading any bools from the 1015 partition.*/ 1017 void init_bool_decoder( bool_decoder *d, uint8 *start_partition) 1018 { 1019 { 1020 int i = 0; 1021 d->value = 0; /* value = first 2 input bytes */ 1022 while( ++i <= 2) 1023 d->value = (d->value << 8) | *start_partition++; 1024 } 1026 d->input = start_partition; /* ptr to next byte to be read */ 1027 d->range = 255; /* initial range is full */ 1028 d->bit_count = 0; /* have not yet shifted out any bits */ 1029 } 1031 /* Main function reads a bool encoded at probability prob/256, 1032 which of course must agree with the probability used when the 1033 bool was written. */ 1035 int read_bool( bool_decoder *d, Prob prob) 1036 { 1037 /* range and split are identical to the corresponding values 1038 used by the encoder when this bool was written */ 1040 uint32 split = 1 + ( ((d->range - 1) * prob) >> 8); 1041 uint32 SPLIT = split << 8; 1042 int retval; /* will be 0 or 1 */ 1044 if( d->value >= SPLIT) { /* encoded a one */ 1045 retval = 1; 1046 d->range -= split; /* reduce range */ 1047 d->value -= SPLIT; /* subtract off left endpoint of interval */ 1048 } else { /* encoded a zero */ 1049 retval = 0; 1050 d->range = split; /* reduce range, no change in left endpoint */ 1051 } 1053 while( d->range < 128) { /* shift out irrelevant value bits */ 1054 d->value <<= 1; 1055 d->range <<= 1; 1056 if( ++d->bit_count == 8) { /* shift in new bits 8 at a time */ 1057 d->bit_count = 0; 1058 d->value |= *d->input++; 1059 } 1060 } 1061 return retval; 1062 } 1064 /* Convenience function reads a "literal", that is, a "num_bits" 1065 wide unsigned value whose bits come high- to low-order, with 1066 each bit encoded at probability 128 (i.e., 1/2). */ 1068 uint32 read_literal( bool_decoder *d, int num_bits) 1069 { 1070 uint32 v = 0; 1071 while( num_bits--) 1072 v = (v << 1) + read_bool( d, 128); 1073 return v; 1074 } 1076 /* Variant reads a signed number */ 1078 int32 read_signed_literal( bool_decoder *d, int num_bits) 1079 { 1080 int32 v = 0; 1081 if( !num_bits) 1082 return 0; 1083 if( read_bool( d, 128)) 1084 v = -1; 1085 while( --num_bits) 1086 v = (v << 1) + read_bool( d, 128); 1087 return v; 1088 } 1090 ---- End code block ---------------------------------------- 1092 8. Compressed Data Components 1094 At the lowest level, VP8's compressed data is simply a sequence of 1095 probabilistically-encoded bools. Most of this data is composed of 1096 (slightly) larger semantic units fashioned from bools, which we 1097 describe here. 1099 We sometimes use these descriptions in C expressions within data 1100 format specifications. In this context, they refer to the return 1101 value of a call to an appropriate bool_decoder d, reading (as always) 1102 from its current reference point. 1104 +--------------+-------+--------------------------------------------+ 1105 | Call | Alt. | Return | 1106 +--------------+-------+--------------------------------------------+ 1107 | Bool(p) | B(p) | Bool with probability p/256 of being 0. | 1108 | | | Return value of read_bool(d, p). | 1109 | | | | 1110 | Flag | F | A one-bit flag (same thing as a B(128) or | 1111 | | | an L(1)). Abbreviated F. Return value of | 1112 | | | read_bool(d, 128). | 1113 | | | | 1114 | Lit(n) | L(n) | Unsigned n-bit number encoded as n flags | 1115 | | | (a "literal"). Abbreviated L(n). The | 1116 | | | bits are read from high to low order. | 1117 | | | Return value of read_literal(d, n). | 1118 | | | | 1119 | SignedLit(n) | | Signed n-bit number encoded similarly to | 1120 | | | an L(n). Return value of | 1121 | | | read_signed_literal(d, n). These are | 1122 | | | rare. | 1123 | | | | 1124 | P(8) | | An 8-bit probability. No different from | 1125 | | | an L(8), but we sometimes use this | 1126 | | | notation to emphasize that a probability | 1127 | | | is being 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 Offsets are embedded in the bitstream to provide the decoder direct 1614 access to token partitions. If the number of data partitions is 1615 greater then 1, the size of each partition (except the last) is 1616 written in 3 bytes (24 bits). The size of the last partition is the 1617 remainder of the data not used by any of the previous partitions. 1618 The partitioned data are consecutive in the bitstream, so the size 1619 can also be used to calculate the offset of each partition. The 1620 following pseudo code illustrates how the size/offset is defined by 1621 the three bytes in the bitstream. 1623 ---- Begin code block -------------------------------------- 1625 Offset/size = (uint32)(byte0) + ((uint32)(byte1)<<8) 1626 + ((uint32)(byte2)<<16); 1628 ---- End code block ---------------------------------------- 1630 9.6. Dequantization Indices 1632 All residue signals are specified via a quantized 4x4 DCT applied to 1633 the Y, U, V, or Y2 subblocks of a macroblock. As detailed in Chapter 1634 14, before inverting the transform, each decoded coefficient is 1635 multiplied by one of six dequantization factors, the choice of which 1636 depends on the plane (Y, chroma = U or V, Y2) and coefficient 1637 position (DC = coefficient 0, AC = coefficients 1-15). The six 1638 values are specified using 7-bit indices into six corresponding fixed 1639 tables (the tables are given in Chapter 14). 1641 The first 7-bit index gives the dequantization table index for Y 1642 plane AC coefficients, called yac_qi. It is always coded and acts as 1643 a baseline for the other 5 quantization indices, each of which is 1644 represented by a delta from this baseline index. Following is pseudo 1645 code for reading the indices: 1647 ---- Begin code block -------------------------------------- 1649 yac_qi = L(7); /* Y ac index always specified */ 1650 ydc_delta = F? delta(): 0; /* Y dc delta specified if 1651 flag is true */ 1653 y2dc_delta = F? delta(): 0; /* Y2 dc delta specified if 1654 flag is true */ 1655 y2ac_delta = F? delta(): 0; /* Y2 ac delta specified if 1656 flag is true */ 1658 uvdc_delta = F? delta(): 0; /* chroma dc delta specified 1659 if flag is true */ 1660 uvac_delta = F? delta(): 0; /* chroma ac delta specified 1661 if flag is true */ 1663 ---- End code block ---------------------------------------- 1664 Where delta() is the process to read 5 bits from the bitstream to 1665 determine a signed delta value: 1667 +-------+--------------------------------------------------+ 1668 | Index | Description | 1669 +-------+--------------------------------------------------+ 1670 | L(4) | Magnitude of delta | 1671 | | | 1672 | L(1) | Sign of delta, 0 for positive and 1 for negative | 1673 +-------+--------------------------------------------------+ 1675 9.7. Refresh Golden Frame and AltRef Frame 1677 For key frames, both golden frame and altref frame are refreshed/ 1678 replaced by the current reconstructed frame, by default. For non-key 1679 frames, VP8 uses two bits to indicate whether the two frame buffers 1680 are refreshed, using the reconstructed current frame: 1682 +-------+----------------------------------------------------------+ 1683 | Index | Description | 1684 +-------+----------------------------------------------------------+ 1685 | L(1) | Whether golden frame is refreshed (0 for no, 1 for yes). | 1686 | | | 1687 | L(1) | Whether altref frame is refreshed (0 for no, 1 for yes). | 1688 +-------+----------------------------------------------------------+ 1690 When the flag for golden frame is 0, VP8 uses 2 more bits in the 1691 bitstream to indicate whether the buffer (and which buffer) is copied 1692 to the golden frame, or if no buffer is copied: 1694 +-------+------------------------------------------+ 1695 | Index | Description | 1696 +-------+------------------------------------------+ 1697 | L(2) | Buffer copy flag for golden frame buffer | 1698 +-------+------------------------------------------+ 1700 Where: 1702 o 0 means no buffer is copied to golden frame 1704 o 1 means last_frame is copied to golden frame 1706 o 2 means alt_ref_frame is copied to golden frame 1708 Similarly, when the flag for altref is 0, VP8 uses 2 bits in the 1709 bitstream to indicate which buffer is copied to alt_ref_frame. 1711 +-------+------------------------------------------+ 1712 | Index | Description | 1713 +-------+------------------------------------------+ 1714 | L(2) | Buffer copy flag for altref frame buffer | 1715 +-------+------------------------------------------+ 1717 Where: 1719 o 0 means no buffer is copied to altref frame 1721 o 1 means last_frame is copied to altref frame 1723 o 2 means golden_frame is copied to altref frame 1725 Two bits are transmitted for ref_frame_sign_bias for golden_frame and 1726 alt_ref_frame respectively. 1728 +-------+---------------------------------+ 1729 | Index | Description | 1730 +-------+---------------------------------+ 1731 | L(1) | Sign bias flag for golden frame | 1732 | | | 1733 | L(1) | Sign bias flag for altref frame | 1734 +-------+---------------------------------+ 1736 These values are used to control the sign of the motion vectors when 1737 a golden frame or an altref frame is used as the reference frame for 1738 a macroblock. 1740 9.8. Refresh Last Frame Buffer 1742 VP8 uses one bit, L(1), to indicate if the last frame reference 1743 buffer is refreshed using the constructed current frame. On key 1744 frame this bit is overridden, and the last frame buffer is always 1745 refreshed. 1747 9.9. DCT Coefficient Probability Update 1749 Contains a partial update of the probability tables used to decode 1750 DCT coefficients. These tables are maintained across interframes but 1751 are of course replaced with their defaults at the beginning of every 1752 key frame. 1754 The layout and semantics of this field will be taken up in Chapter 1755 13. 1757 9.10. Remaining Frame Header Data (non-Key Frame) 1759 +-------+-----------------------------------------------------------+ 1760 | Index | Description | 1761 +-------+-----------------------------------------------------------+ 1762 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1763 | | if skipping of macroblocks with no non-zero coefficients | 1764 | | is enabled. If it is set to 0 then prob_skip_false is | 1765 | | not read and mb_skip_coeff is forced to 0 for all | 1766 | | macroblocks (see Sections 11.1 and 12.1). | 1767 | | | 1768 | L(8) | prob_skip_false = probability used for decoding a | 1769 | | macroblock level flag, which indicates if a macroblock | 1770 | | has any non-zero coefficients. Only read if | 1771 | | mb_no_coeff_skip is 1. | 1772 | | | 1773 | L(8) | prob_intra = probability that a macroblock is "intra" | 1774 | | predicted, that is, predicted from the already-encoded | 1775 | | portions of the current frame as opposed to "inter" | 1776 | | predicted, that is, predicted from the contents of a | 1777 | | prior frame. | 1778 | | | 1779 | L(8) | prob_last = probability that an inter-predicted | 1780 | | macroblock is predicted from the immediately previous | 1781 | | frame, as opposed to the most recent golden frame or | 1782 | | altref frame.. | 1783 | | | 1784 | L(8) | prob_gf = probability that an inter-predicted macroblock | 1785 | | is predicted from the most recent golden frame, as | 1786 | | opposed to the altref frame | 1787 | | | 1788 | F | If true, followed by four L(8)s updating the | 1789 | | probabilities for the different types of intra-prediction | 1790 | | for the Y plane. These probabilities correspond to the | 1791 | | four interior nodes of the decoding tree for intra Y | 1792 | | modes in an interframe, that is, the even positions in | 1793 | | the ymode_tree array given above. | 1794 | | | 1795 | F | If true, followed by three L(8)s updating the | 1796 | | probabilities for the different types of intra-prediction | 1797 | | for the chroma planes. These probabilities correspond to | 1798 | | the even positions in the uv_mode_tree array given above. | 1799 | | | 1800 | X | Motion vector probability update. The details will be | 1801 | | given after the discussion of motion vector decoding. | 1802 +-------+-----------------------------------------------------------+ 1804 Decoding of this portion (only) of the frame header is handled in the 1805 reference decoder file decodemv.c. 1807 9.11. Remaining Frame Header Data (Key Frame) 1809 +-------+-----------------------------------------------------------+ 1810 | Index | Description | 1811 +-------+-----------------------------------------------------------+ 1812 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1813 | | if skipping of macroblocks with no non-zero coefficients | 1814 | | is enabled. If it is set to 0 then prob_skip_false is | 1815 | | not read and mb_skip_coeff is forced to 0 for all | 1816 | | macroblocks (see Sections 11.1 and 12.1). | 1817 | | | 1818 | L(8) | prob_skip_false = Probability used for decoding a | 1819 | | macroblock level flag, which indicates if a macroblock | 1820 | | has any non-zero coefficients. Only read if | 1821 | | mb_no_coeff_skip is 1. | 1822 +-------+-----------------------------------------------------------+ 1824 Decoding of this portion of the frame header is handled in the 1825 reference decoder file demode.c. 1827 This completes the layout of the frame header. The remainder of the 1828 first data partition consists of macroblock-level prediction data. 1830 After the frame header is processed, all probabilities needed to 1831 decode the prediction and residue data are known and will not change 1832 until the next frame. 1834 10. Segment-based Feature Adjustments 1836 Every macroblock may optionally override some of the default 1837 behaviors of the decoder. Specifically, VP8 uses segment based 1838 adjustments to support changing quantizer level and loop filter level 1839 for a macroblock. When the segment-based adjustment feature is 1840 enabled for a frame, each macroblock within the frame is coded with a 1841 segment_id. This effectively segments all the macroblocks in the 1842 current frame into a number of different segments. Macroblocks 1843 within the same segment behave exactly the same for quantizer and 1844 loop filter level adjustments. 1846 If both the segmentation_enabled and update_mb_segmentation_map flags 1847 in subsection B of the frame header take a value of 1, the prediction 1848 data for each (intra- or inter-coded) macroblock begins with a 1849 specification of segment_id for the current macroblock. It is 1850 decoded using this simple tree ... 1852 ---- Begin code block -------------------------------------- 1854 const tree_index mb_segment_tree [2 * (4-1)] = 1855 { 1856 2, 4, /* root: "0", "1" subtrees */ 1857 -0, -1, /* "00" = 0th value, "01" = 1st value */ 1858 -2, -3 /* "10" = 2nd value, "11" = 3rd value */ 1859 } 1861 ---- End code block ---------------------------------------- 1863 ... combined with a 3-entry probability table 1864 mb_segment_tree_probs[3]. The macroblock's segment_id is used later 1865 in the decoding process to look into the segment_feature_data table 1866 and determine how the quantizer and loop filter levels are adjusted. 1868 The decoding of segment_id, together with the parsing of intra- 1869 prediction modes (which is taken up next), is implemented in the 1870 reference decoder file demode.c. 1872 11. Key Frame Macroblock Prediction Records 1874 After the features described above, the macroblock prediction record 1875 next specifies the prediction mode used for the macroblock. 1877 11.1. mb_skip_coeff 1879 The single bool flag is decoded using prob_skip_false if and only if 1880 mb_no_coeff_skip is set to 1 (see sections 9.10 and 9.11). If 1881 mb_no_coeff_skip is set to 0 then this value defaults to 0. 1883 11.2. Luma Modes 1885 First comes the luma specification of type intra_mbmode, coded using 1886 the kf_ymode_tree, as described in Chapter 8 and repeated here for 1887 convenience: 1889 ---- Begin code block -------------------------------------- 1891 typedef enum 1892 { 1893 DC_PRED, /* predict DC using row above and column to the left */ 1894 V_PRED, /* predict rows using row above */ 1895 H_PRED, /* predict columns using column to the left */ 1896 TM_PRED, /* propagate second differences a la "true motion" */ 1898 B_PRED, /* each Y subblock is independently predicted */ 1900 num_uv_modes = B_PRED, /* first four modes apply to chroma */ 1901 num_ymodes /* all modes apply to luma */ 1902 } 1903 intra_mbmode; 1905 const tree_index kf_ymode_tree [2 * (num_ymodes - 1)] = 1906 { 1907 -B_PRED, 2, /* root: B_PRED = "0", "1" subtree */ 1908 4, 6, /* "1" subtree has 2 descendant subtrees */ 1909 -DC_PRED, -V_PRED, /* "10" subtree: DC_PRED = "100", 1910 V_PRED = "101" */ 1911 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1912 TM_PRED = "111" */ 1913 }; 1915 ---- End code block ---------------------------------------- 1917 For key frames, the Y mode is decoded using a fixed probability array 1918 as follows: 1920 ---- Begin code block -------------------------------------- 1922 const Prob kf_ymode_prob [num_ymodes - 1] = { 145, 156, 163, 128}; 1923 Ymode = (intra_mbmode) treed_read( d, kf_ymode_tree, kf_ymode_prob); 1925 ---- End code block ---------------------------------------- 1927 d is of course the bool_decoder being used to read the first data 1928 partition. 1930 If the Ymode is B_PRED, it is followed by a (tree-coded) mode for 1931 each of the 16 Y subblocks. The 10 subblock modes and their coding 1932 tree as follows: 1934 ---- Begin code block -------------------------------------- 1936 typedef enum 1937 { 1938 B_DC_PRED, /* predict DC using row above and column 1939 to the left */ 1940 B_TM_PRED, /* propagate second differences a la 1941 "true motion" */ 1943 B_VE_PRED, /* predict rows using row above */ 1944 B_HE_PRED, /* predict columns using column to the left */ 1946 B_LD_PRED, /* southwest (left and down) 45 degree diagonal 1947 prediction */ 1948 B_RD_PRED, /* southeast (right and down) "" */ 1950 B_VR_PRED, /* SSE (vertical right) diagonal prediction */ 1951 B_VL_PRED, /* SSW (vertical left) "" */ 1952 B_HD_PRED, /* ESE (horizontal down) "" */ 1953 B_HU_PRED, /* ENE (horizontal up) "" */ 1955 num_intra_bmodes 1956 } 1957 intra_bmode; 1959 /* Coding tree for the above, with implied codings as comments */ 1961 const tree_index bmode_tree [2 * (num_intra_bmodes - 1)] = 1962 { 1963 -B_DC_PRED, 2, /* B_DC_PRED = "0" */ 1964 -B_TM_PRED, 4, /* B_TM_PRED = "10" */ 1965 -B_VE_PRED, 6, /* B_VE_PRED = "110" */ 1966 8, 12, 1967 -B_HE_PRED, 10, /* B_HE_PRED = "11100" */ 1968 -B_RD_PRED, -B_VR_PRED, /* B_RD_PRED = "111010", 1969 B_VR_PRED = "111011" */ 1970 -B_LD_PRED, 14, /* B_LD_PRED = "111110" */ 1971 -B_VL_PRED, 16, /* B_VL_PRED = "1111110" */ 1972 -B_HD_PRED, -B_HU_PRED /* HD = "11111110", 1973 HU = "11111111" */ 1974 }; 1976 ---- End code block ---------------------------------------- 1978 The first four modes are smaller versions of the similarly-named 1979 16x16 modes above, albeit with slightly different numbering. The 1980 last six "diagonal" modes are unique to luma subblocks. 1982 11.3. Subblock Mode Contexts 1984 The coding of subblock modes in key frames uses the modes already 1985 coded for the subblocks to the left of and above the subblock to 1986 select a probability array for decoding the current subblock mode. 1987 This is our first instance of contextual prediction, and there are 1988 several caveats associated with it: 1990 1. The adjacency relationships between subblocks are based on the 1991 normal default raster placement of the subblocks. 1993 2. The adjacent subblocks need not lie in the current macroblock. 1994 The subblocks to the left of the left-edge subblocks 0, 4, 8, and 1995 12 are the right-edge subblocks 3, 7, 11, and 15, respectively, 1996 of the (already coded) macroblock immediately to the left. 1997 Similarly, the subblocks above the top-edge subblocks 0, 1, 2, 1998 and 3 are the bottom-edge subblocks 12, 13, 14, and 15 of the 1999 already-coded macroblock immediately above us. 2001 3. For macroblocks on the top row or left edge of the image, some of 2002 the predictors will be non-existent. Such predictors are taken 2003 to have had the value B_DC_PRED which, perhaps conveniently, 2004 takes the value 0 in the enumeration above. A simple management 2005 scheme for these contexts might maintain a row of above 2006 predictors and four left predictors. Before decoding the frame, 2007 the entire row is initialized to B_DC_PRED; before decoding each 2008 row of macroblocks, the four left predictors are also set to 2009 B_DC_PRED. After decoding a macroblock, the bottom four subblock 2010 modes are copied into the row predictor (at the current position, 2011 which then advances to be above the next macroblock) and the 2012 right four subblock modes are copied into the left predictor. 2014 4. Many macroblocks will of course be coded using a 16x16 luma 2015 prediction mode. For the purpose of predicting ensuing subblock 2016 modes (only), such macroblocks derive a subblock mode, constant 2017 throughout the macroblock, from the 16x16 luma mode as follows: 2018 DC_PRED uses B_DC_PRED, V_PRED uses B_VE_PRED, H_PRED uses 2019 B_HE_PRED, and TM_PRED uses B_TM_PRED. 2021 5. Although we discuss interframe modes later, we remark here that, 2022 while interframes do use all the intra-coding modes described 2023 here and below, the subblock modes in an interframe are coded 2024 using a single constant probability array that does not depend on 2025 any context. 2027 The dependence of subblock mode probability on the nearby subblock 2028 mode context is most easily handled using a three-dimensional 2029 constant array: 2031 ---- Begin code block -------------------------------------- 2033 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2034 [num_intra_bmodes-1]; 2036 ---- End code block ---------------------------------------- 2038 The outer two dimensions of this array are indexed by the already- 2039 coded subblock modes above and to the left of the current block, 2040 respectively. The inner dimension is a typical tree probability list 2041 whose indices correspond to the even indices of the bmode_tree above. 2042 The mode for the j^(th) luma subblock is then 2044 ---- Begin code block -------------------------------------- 2046 Bmode = (intra_bmode) treed_read( d, bmode_tree, kf_bmode_prob 2047 [A] [L]); 2049 ---- End code block ---------------------------------------- 2051 where the 4x4 Y subblock index j varies from 0 to 15 in raster order 2052 and A and L are the modes used above and to-the-left of the j^(th) 2053 subblock. 2055 The contents of the kf_bmode_prob array are given at the end of this 2056 chapter. 2058 11.4. Chroma Modes 2060 After the Y mode (and optional subblock mode) specification comes the 2061 chroma mode. The chroma modes are a subset of the Y modes and are 2062 coded using the uv_mode_tree described in Chapter 8, again repeated 2063 here for convenience: 2065 ---- Begin code block -------------------------------------- 2067 const tree_index uv_mode_tree [2 * (num_uv_modes - 1)] = 2068 { 2069 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 2070 -V_PRED, 4, /* "1" subtree: V_PRED = "10", 2071 "11" subtree */ 2072 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 2073 TM_PRED = "111" */ 2074 }; 2076 ---- End code block ---------------------------------------- 2078 As for the Y modes (in a key frame), the chroma modes are coded using 2079 a fixed, contextless probability table: 2081 ---- Begin code block -------------------------------------- 2083 const Prob kf_uv_mode_prob [num_uv_modes - 1] = { 142, 114, 183}; 2084 uv_mode = (intra_mbmode) treed_read( d, uv_mode_tree, 2085 kf_uv_mode_prob); 2087 ---- End code block ---------------------------------------- 2089 This completes the description of macroblock prediction coding for 2090 key frames. As will be discussed in Chapter 16, the coding of intra 2091 modes within interframes is similar, but not identical, to that 2092 described here (and in the reference code) for prediction modes and, 2093 indeed, for all tree-coded data in VP8. 2095 11.5. Subblock Mode Probability Table 2097 Finally, here is the fixed probability table used to decode subblock 2098 modes in key frames. 2100 ---- Begin code block -------------------------------------- 2102 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2103 [num_intra_bmodes-1] = 2104 { 2105 { 2106 { 231, 120, 48, 89, 115, 113, 120, 152, 112}, 2107 { 152, 179, 64, 126, 170, 118, 46, 70, 95}, 2108 { 175, 69, 143, 80, 85, 82, 72, 155, 103}, 2109 { 56, 58, 10, 171, 218, 189, 17, 13, 152}, 2110 { 144, 71, 10, 38, 171, 213, 144, 34, 26}, 2111 { 114, 26, 17, 163, 44, 195, 21, 10, 173}, 2112 { 121, 24, 80, 195, 26, 62, 44, 64, 85}, 2113 { 170, 46, 55, 19, 136, 160, 33, 206, 71}, 2114 { 63, 20, 8, 114, 114, 208, 12, 9, 226}, 2115 { 81, 40, 11, 96, 182, 84, 29, 16, 36} 2116 }, 2117 { 2118 { 134, 183, 89, 137, 98, 101, 106, 165, 148}, 2119 { 72, 187, 100, 130, 157, 111, 32, 75, 80}, 2120 { 66, 102, 167, 99, 74, 62, 40, 234, 128}, 2121 { 41, 53, 9, 178, 241, 141, 26, 8, 107}, 2122 { 104, 79, 12, 27, 217, 255, 87, 17, 7}, 2123 { 74, 43, 26, 146, 73, 166, 49, 23, 157}, 2124 { 65, 38, 105, 160, 51, 52, 31, 115, 128}, 2125 { 87, 68, 71, 44, 114, 51, 15, 186, 23}, 2126 { 47, 41, 14, 110, 182, 183, 21, 17, 194}, 2127 { 66, 45, 25, 102, 197, 189, 23, 18, 22} 2128 }, 2129 { 2130 { 88, 88, 147, 150, 42, 46, 45, 196, 205}, 2131 { 43, 97, 183, 117, 85, 38, 35, 179, 61}, 2132 { 39, 53, 200, 87, 26, 21, 43, 232, 171}, 2133 { 56, 34, 51, 104, 114, 102, 29, 93, 77}, 2134 { 107, 54, 32, 26, 51, 1, 81, 43, 31}, 2135 { 39, 28, 85, 171, 58, 165, 90, 98, 64}, 2136 { 34, 22, 116, 206, 23, 34, 43, 166, 73}, 2137 { 68, 25, 106, 22, 64, 171, 36, 225, 114}, 2138 { 34, 19, 21, 102, 132, 188, 16, 76, 124}, 2139 { 62, 18, 78, 95, 85, 57, 50, 48, 51} 2140 }, 2141 { 2142 { 193, 101, 35, 159, 215, 111, 89, 46, 111}, 2143 { 60, 148, 31, 172, 219, 228, 21, 18, 111}, 2144 { 112, 113, 77, 85, 179, 255, 38, 120, 114}, 2145 { 40, 42, 1, 196, 245, 209, 10, 25, 109}, 2146 { 100, 80, 8, 43, 154, 1, 51, 26, 71}, 2147 { 88, 43, 29, 140, 166, 213, 37, 43, 154}, 2148 { 61, 63, 30, 155, 67, 45, 68, 1, 209}, 2149 { 142, 78, 78, 16, 255, 128, 34, 197, 171}, 2150 { 41, 40, 5, 102, 211, 183, 4, 1, 221}, 2151 { 51, 50, 17, 168, 209, 192, 23, 25, 82} 2152 }, 2153 { 2154 { 125, 98, 42, 88, 104, 85, 117, 175, 82}, 2155 { 95, 84, 53, 89, 128, 100, 113, 101, 45}, 2156 { 75, 79, 123, 47, 51, 128, 81, 171, 1}, 2157 { 57, 17, 5, 71, 102, 57, 53, 41, 49}, 2158 { 115, 21, 2, 10, 102, 255, 166, 23, 6}, 2159 { 38, 33, 13, 121, 57, 73, 26, 1, 85}, 2160 { 41, 10, 67, 138, 77, 110, 90, 47, 114}, 2161 { 101, 29, 16, 10, 85, 128, 101, 196, 26}, 2162 { 57, 18, 10, 102, 102, 213, 34, 20, 43}, 2163 { 117, 20, 15, 36, 163, 128, 68, 1, 26} 2164 }, 2165 { 2166 { 138, 31, 36, 171, 27, 166, 38, 44, 229}, 2167 { 67, 87, 58, 169, 82, 115, 26, 59, 179}, 2168 { 63, 59, 90, 180, 59, 166, 93, 73, 154}, 2169 { 40, 40, 21, 116, 143, 209, 34, 39, 175}, 2170 { 57, 46, 22, 24, 128, 1, 54, 17, 37}, 2171 { 47, 15, 16, 183, 34, 223, 49, 45, 183}, 2172 { 46, 17, 33, 183, 6, 98, 15, 32, 183}, 2173 { 65, 32, 73, 115, 28, 128, 23, 128, 205}, 2174 { 40, 3, 9, 115, 51, 192, 18, 6, 223}, 2175 { 87, 37, 9, 115, 59, 77, 64, 21, 47} 2176 }, 2177 { 2178 { 104, 55, 44, 218, 9, 54, 53, 130, 226}, 2179 { 64, 90, 70, 205, 40, 41, 23, 26, 57}, 2180 { 54, 57, 112, 184, 5, 41, 38, 166, 213}, 2181 { 30, 34, 26, 133, 152, 116, 10, 32, 134}, 2182 { 75, 32, 12, 51, 192, 255, 160, 43, 51}, 2183 { 39, 19, 53, 221, 26, 114, 32, 73, 255}, 2184 { 31, 9, 65, 234, 2, 15, 1, 118, 73}, 2185 { 88, 31, 35, 67, 102, 85, 55, 186, 85}, 2186 { 56, 21, 23, 111, 59, 205, 45, 37, 192}, 2187 { 55, 38, 70, 124, 73, 102, 1, 34, 98} 2188 }, 2189 { 2190 { 102, 61, 71, 37, 34, 53, 31, 243, 192}, 2191 { 69, 60, 71, 38, 73, 119, 28, 222, 37}, 2192 { 68, 45, 128, 34, 1, 47, 11, 245, 171}, 2193 { 62, 17, 19, 70, 146, 85, 55, 62, 70}, 2194 { 75, 15, 9, 9, 64, 255, 184, 119, 16}, 2195 { 37, 43, 37, 154, 100, 163, 85, 160, 1}, 2196 { 63, 9, 92, 136, 28, 64, 32, 201, 85}, 2197 { 86, 6, 28, 5, 64, 255, 25, 248, 1}, 2198 { 56, 8, 17, 132, 137, 255, 55, 116, 128}, 2199 { 58, 15, 20, 82, 135, 57, 26, 121, 40} 2200 }, 2201 { 2202 { 164, 50, 31, 137, 154, 133, 25, 35, 218}, 2203 { 51, 103, 44, 131, 131, 123, 31, 6, 158}, 2204 { 86, 40, 64, 135, 148, 224, 45, 183, 128}, 2205 { 22, 26, 17, 131, 240, 154, 14, 1, 209}, 2206 { 83, 12, 13, 54, 192, 255, 68, 47, 28}, 2207 { 45, 16, 21, 91, 64, 222, 7, 1, 197}, 2208 { 56, 21, 39, 155, 60, 138, 23, 102, 213}, 2209 { 85, 26, 85, 85, 128, 128, 32, 146, 171}, 2210 { 18, 11, 7, 63, 144, 171, 4, 4, 246}, 2211 { 35, 27, 10, 146, 174, 171, 12, 26, 128} 2212 }, 2213 { 2214 { 190, 80, 35, 99, 180, 80, 126, 54, 45}, 2215 { 85, 126, 47, 87, 176, 51, 41, 20, 32}, 2216 { 101, 75, 128, 139, 118, 146, 116, 128, 85}, 2217 { 56, 41, 15, 176, 236, 85, 37, 9, 62}, 2218 { 146, 36, 19, 30, 171, 255, 97, 27, 20}, 2219 { 71, 30, 17, 119, 118, 255, 17, 18, 138}, 2220 { 101, 38, 60, 138, 55, 70, 43, 26, 142}, 2221 { 138, 45, 61, 62, 219, 1, 81, 188, 64}, 2222 { 32, 41, 20, 117, 151, 142, 20, 21, 163}, 2223 { 112, 19, 12, 61, 195, 128, 48, 4, 24} 2224 } 2225 }; 2227 ---- End code block ---------------------------------------- 2229 12. Intraframe Prediction 2231 Intraframe prediction uses already-coded macroblocks within the 2232 current frame to approximate the contents of the current macroblock. 2233 It applies to intra-coded macroblocks in an interframe and to all 2234 macroblocks in a key frame. 2236 Relative to the current macroblock "M", the already-coded macroblocks 2237 include all macroblocks above M together with the macroblocks on the 2238 same row as, and to the left of, M, though at most four of these 2239 macroblocks are actually used: the block "A" directly above M, the 2240 blocks immediately to the left and right of A, and the block 2241 immediately to the left of M. 2243 Each of the prediction modes (i.e., means of extrapolation from 2244 already-calculated values) uses fairly simple arithmetic on pixel 2245 values whose positions, relative to the current position, are defined 2246 by the mode. 2248 The chroma (U and V) and luma (Y) predictions are independent of each 2249 other. 2251 The relative addressing of pixels applied to macroblocks on the upper 2252 row or left column of the frame will sometimes cause pixels outside 2253 the visible frame to be referenced. Usually such out-of-bounds 2254 pixels have an assumed value of 129 for pixels to the left of the 2255 leftmost column of the visible frame and 127 for pixels above the top 2256 row of the visible frame (including the special case of the pixel 2257 above and to the left of the top-left pixel in the visible frame). 2258 Exceptions to this (associated to certain modes) will be noted below. 2260 The already-coded macroblocks referenced by intra-prediction have 2261 been "reconstructed", that is, have been predicted and residue- 2262 adjusted (as described in Chapter 14), but have not been loop- 2263 filtered. While it does process the edges between individual 2264 macroblocks and individual subblocks, loop filtering (described in 2265 Chapter 15) is applied to the frame as a whole, after all of the 2266 macroblocks have been reconstructed. 2268 12.1. mb_skip_coeff 2270 The single bool flag is decoded using prob_skip_false if and only if 2271 mb_no_coeff_skip is set to 1 (see Sections 9.10 and 9.11). If 2272 mb_no_coeff_skip is set to 0 then this value defaults to 0. 2274 12.2. Chroma Prediction 2276 The chroma prediction is a little simpler than the luma prediction, 2277 so we treat it first. Each of the chroma modes treats U and V 2278 identically, that is, the U and V prediction values are calculated in 2279 parallel, using the same relative addressing and arithmetic in each 2280 of the two planes. 2282 The modes extrapolate prediction values using the 8-pixel row "A" 2283 lying immediately above the block (that is, the bottom chroma row of 2284 the macroblock immediately above the current macroblock) and the 2285 8-pixel column "L" immediately to the left of the block (that is, the 2286 rightmost chroma column of the macroblock immediately to the left of 2287 the current macroblock). 2289 Vertical prediction (chroma mode V_PRED) simply fills each 8-pixel 2290 row of the 8x8 chroma block with a copy of the "above" row (A). If 2291 the current macroblock lies on the top row of the frame, all 8 of the 2292 pixel values in A are assigned the value 127. 2294 Similarly, horizontal prediction (H_PRED) fills each 8-pixel column 2295 of the 8x8 chroma block with a copy of the "left" column (L). If the 2296 current macroblock is in the left column of the frame, all 8 pixel 2297 values in L are assigned the value 129. 2299 DC prediction (DC_PRED) fills the 8x8 chroma block with a single 2300 value. In the generic case of a macroblock lying below the top row 2301 and right of the leftmost column of the frame, this value is the 2302 average of the 16 (genuinely visible) pixels in the (union of the) 2303 above row A and left column L. 2305 Otherwise, if the current macroblock lies on the top row of the 2306 frame, the average of the 8 pixels in L is used; if it lies in the 2307 left column of the frame, the average of the 8 pixels in A is used. 2308 Note that the averages used in these exceptional cases are not the 2309 same as those that would be arrived at by using the out-of-bounds A 2310 and L values defined for V_PRED and H_PRED. In the case of the 2311 leftmost macroblock on the top row of the frame the 8x8 block is 2312 simply filled with the constant value 128. 2314 For DC_PRED, apart from the exceptional case of the top left 2315 macroblock, we are averaging either 16 or 8 pixel values to get a 2316 single prediction value that fills the 8x8 block. The rounding is 2317 done as follows: 2319 ---- Begin code block -------------------------------------- 2321 int sum; /* sum of 8 or 16 pixels at (at least) 16-bit precision */ 2322 int shf; /* base 2 logarithm of the number of pixels (3 or 4) */ 2324 Pixel DCvalue = (sum + (1 << (shf-1))) >> shf; 2326 ---- End code block ---------------------------------------- 2328 Because the summands are all valid pixels, no "clamp" is necessary in 2329 the calculation of DCvalue. 2331 The remaining "True Motion" (TM_PRED) chroma mode gets its name from 2332 an older technique of video compression used by On2 Technologies, to 2333 which it bears some relation. In addition to the row "A" and column 2334 "L", TM_PRED uses the pixel "P" above and to the left of the chroma 2335 block. 2337 The following figure gives an example of how TM_PRED works: 2339 ---- Begin code block -------------------------------------- 2341 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2342 | P | A0 | A1 | A2 | A3 | A4 | A5 | A6 | A7 | 2343 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2344 | L0 | X00 | X01 | X02 | X03 | X04 | X05 | X06 | X07 | 2345 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2346 | L1 | X10 | X11 | X12 | X13 | X14 | X15 | X16 | X17 | 2347 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2348 | L2 | X20 | X21 | X22 | X23 | X24 | X25 | X26 | X27 | 2349 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2350 | L3 | X30 | X31 | X32 | X33 | X34 | X35 | X36 | X37 | 2351 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2352 | L4 | X40 | X41 | X42 | X43 | X44 | X45 | X46 | X47 | 2353 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2354 | L5 | X50 | X51 | X52 | X53 | X54 | X55 | X56 | X57 | 2355 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2356 | L6 | X60 | X61 | X62 | X63 | X64 | X65 | X66 | X67 | 2357 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2358 | L7 | X70 | X71 | X72 | X73 | X74 | X75 | X76 | X77 | 2359 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2361 ---- End code block ---------------------------------------- 2363 Where P, As and Ls represent reconstructed pixel values from 2364 previously coded blocks, and X00 through X77 represent predicted 2365 values for the current block. TM_PRED uses the following equation to 2366 calculate X_ij: 2368 X_ij = L_i + A_j - P (i, j=0, 1, 2, 3) 2370 The exact algorithm is as follows: 2372 ---- Begin code block -------------------------------------- 2374 void TMpred( 2375 Pixel b[8][8], /* chroma (U or V) prediction block */ 2376 const Pixel A[8], /* row of already-constructed pixels 2377 above block */ 2378 const Pixel L[8], /* column of "" just to the left of 2379 block */ 2380 const Pixel P /* pixel just to the left of A and 2381 above L*/ 2382 ) { 2383 int r = 0; /* row */ 2384 do { 2385 int c = 0; /* column */ 2386 do { 2387 b[r][c] = clamp255( L[r]+ A[c] - P); 2388 } while( ++c < 8); 2389 } while( ++r < 8); 2390 } 2392 ---- End code block ---------------------------------------- 2394 Note that the process could equivalently be described as propagating 2395 the vertical differences between pixels in L (starting from P), using 2396 the pixels from A to start each column. 2398 An implementation of chroma intra-prediction may be found in the 2399 reference decoder file reconintra.c. 2401 Unlike DC_PRED, for macroblocks on the top row or left edge TM_PRED 2402 does use the out-of-bounds values of 127 and 129 (respectively) 2403 defined for V_PRED and H_PRED. 2405 12.3. Luma Prediction 2407 The prediction processes for the first four 16x16 luma modes 2408 (DC_PRED, V_PRED, H_PRED, and TM_PRED) are essentially identical to 2409 the corresponding chroma prediction processes described above, the 2410 only difference being that we are predicting a single 16x16 luma 2411 block instead of two 8x8 chroma blocks. 2413 Thus, the row "A" and column "L" here contain 16 pixels, the DC 2414 prediction is calculated using 16 or 32 pixels (and shf is 4 or 5), 2415 and we of course fill the entire prediction buffer, that is, 16 rows 2416 (or columns) containing 16 pixels each. The reference implementation 2417 of 16x16 luma prediction is also in reconintra.c. 2419 In the remaining luma mode (B_PRED), each 4x4 Y subblock is 2420 independently predicted using one of ten modes (listed, along with 2421 their encodings, in Chapter 11). 2423 Also, unlike the full-macroblock modes already described, some of the 2424 subblock modes use prediction pixels above and to the right of the 2425 current subblock. In detail, each 4x4 subblock "B" is predicted 2426 using (at most) the 4-pixel column "L" immediately to the left of B 2427 and the 8-pixel row "A" immediately above B, consisting of the 4 2428 pixels above B followed by the 4 adjacent pixels above and to the 2429 right of B, together with the single pixel "P" immediately to the 2430 left of A (and immediately above L). 2432 For the purpose of subblock intra-prediction, the pixels immediately 2433 to the left and right of a pixel in a subblock are the same as the 2434 pixels immediately to the left and right of the corresponding pixel 2435 in the frame buffer "F". Vertical offsets behave similarly: The 2436 above row A lies immediately above B in F, and the adjacent pixels in 2437 the left column L are separated by a single row in F. 2439 Because entire macroblocks (as opposed to their constituent 2440 subblocks) are reconstructed in raster-scan order, for subblocks 2441 lying along the right edge (and not along the top row) of the current 2442 macroblock, the four "extra" prediction pixels in A above and to the 2443 right of B have not yet actually been constructed. 2445 Subblocks 7, 11, and 15 are affected. All three of these subblocks 2446 use the same extra pixels as does subblock 3 (at the upper right 2447 corner of the macroblock), namely the 4 pixels immediately above and 2448 to the right of subblock 3. Writing (R,C) for a frame buffer 2449 position offset from the upper left corner of the current macroblock 2450 by R rows and C columns, the extra pixels for all the right-edge 2451 subblocks (3, 7, 11, and 15) are at positions (-1,16), (-1,17), 2452 (-1,18), and (-1,19). For the right-most macroblock in each 2453 macroblock row except the top row, the extra pixels shall use the 2454 same value as the pixel at position (-1, 15), which is the right-most 2455 visible pixel on the line immediately above the macroblock row. For 2456 the top macroblock row, all the extra pixels assume a value of 127. 2458 The details of the prediction modes are most easily described in 2459 code. 2461 ---- Begin code block -------------------------------------- 2463 /* Result pixels are often averages of two or three predictor 2464 pixels. The following subroutines are used to calculate 2465 these averages. Because the arguments are valid pixels, no 2466 clamping is necessary. An actual implementation would 2467 probably use inline functions or macros. */ 2469 /* Compute weighted average centered at y w/adjacent x, z */ 2471 Pixel avg3( Pixel x, Pixel y, Pixel z) { 2472 return (x + y + y + z + 2) >> 2;} 2474 /* Weighted average of 3 adjacent pixels centered at p */ 2476 Pixel avg3p( const Pixel *p) { return avg3( p[-1], p[0], p[1]);} 2478 /* Simple average of x and y */ 2480 Pixel avg2( Pixel x, Pixel y) { return (x + y + 1) >> 1;} 2482 /* Average of p[0] and p[1] may be considered to be a synthetic 2483 pixel lying between the two, that is, one half-step past p. */ 2485 Pixel avg2p( const Pixel *p) { return avg2( p[0], p[1]);} 2487 void subblock_intra_predict( 2488 Pixel B[4][4], /* Y subblock prediction buffer */ 2489 const Pixel *A, /* A[0]...A[7] = above row, A[-1] = P */ 2490 const Pixel *L, /* L[0]...L[3] = left column, L[-1] = P */ 2491 intra_bmode mode /* enum is in section 11.1 above */ 2492 ) { 2493 Pixel E[9]; /* 9 already-constructed edge pixels */ 2494 E[0] = L[3]; E[1] = L[2]; E[2] = L[1]; E[3] = L[0]; 2495 E[4] = A[-1]; /* == L[-1] == P */ 2496 E[5] = A[0]; E[6] = A[1]; E[7] = A[2]; E[8] = A[3]; 2498 switch( mode) { 2499 /* First four modes are similar to corresponding 2500 full-block modes. */ 2502 case B_DC_PRED: 2503 { 2504 int v = 4; /* DC sum/avg, 4 is rounding adjustment */ 2505 int i = 0; do { v += A[i] + L[i];} while( ++i < 4); 2506 v >>= 3; /* averaging 8 pixels */ 2507 i = 0; do { /* fill prediction buffer with constant DC 2508 value */ 2509 int j = 0; do { B[i][j] = v;} while( ++j < 4); 2510 } while( ++i < 4); 2511 break; 2513 } 2515 case B_TM_PRED: /* just like 16x16 TM_PRED */ 2516 { 2517 int r = 0; do { 2518 int c = 0; do { 2519 B[r][c] = clamp255( L[r] + A[c] - A[-1]); 2520 } while( ++c < 4); 2521 } while( ++r < 4); 2522 break; 2523 } 2525 case B_VE_PRED: /* like 16x16 V_PRED except using averages */ 2526 { 2527 int c = 0; do { /* all 4 rows = smoothed top row */ 2528 B[0][c] = B[1][c] = B[2][c] = B[3][c] = avg3p( A + c); 2529 } while( ++c < 4); 2530 break; 2531 } 2533 case B_HE_PRED: /* like 16x16 H_PRED except using averages */ 2534 { 2535 /* Bottom row is exceptional because L[4] does not exist */ 2536 int v = avg3( L[2], L[3], L[3]); 2537 int r = 3; while( 1) { /* all 4 columns = smoothed left 2538 column */ 2539 B[r][0] = B[r][1] = B[r][2] = B[r][3] = v; 2540 if( --r < 0) 2541 break; 2542 v = avg3p( L + r); /* upper 3 rows use average of 2543 3 pixels */ 2544 } 2545 break; 2546 } 2548 /* The remaining six "diagonal" modes subdivide the 2549 prediction buffer into diagonal lines. All the pixels 2550 on each line are assigned the same value; this value is 2551 (a smoothed or synthetic version of) an 2552 already-constructed predictor value lying on the same 2553 line. For clarity, in the comments, we express the 2554 positions of these predictor pixels relative to the 2555 upper left corner of the destination array B. 2557 These modes are unique to subblock prediction and have 2558 no full-block analogues. The first two use lines at 2559 +|- 45 degrees from horizontal (or, equivalently, 2560 vertical), that is, lines whose slopes are +|- 1. */ 2562 case B_LD_PRED: /* southwest (left and down) step = 2563 (-1, 1) or (1,-1) */ 2564 /* avg3p( A + j) is the "smoothed" pixel at (-1,j) */ 2565 B[0][0] = avg3p( A + 1); 2566 B[0][1] = B[1][0] = avg3p( A + 2); 2567 B[0][2] = B[1][1] = B[2][0] = avg3p( A + 3); 2568 B[0][3] = B[1][2] = B[2][1] = B[3][0] = avg3p( A + 4); 2569 B[1][3] = B[2][2] = B[3][1] = avg3p( A + 5); 2570 B[2][3] = B[3][2] = avg3p( A + 6); 2571 B[3][3] = avg3( A[6], A[7], A[7]); /* A[8] does not exist */ 2572 break; 2574 case B_RD_PRED: /* southeast (right and down) step = 2575 (1,1) or (-1,-1) */ 2576 B[3][0] = avg3p( E + 1); /* predictor is from (2, -1) */ 2577 B[3][1] = B[2][0] = avg3p( E + 2); /* (1, -1) */ 2578 B[3][2] = B[2][1] = B[1][0] = avg3p( E + 3); /* (0, -1) */ 2579 B[3][3] = B[2][2] = B[1][1] = B[0][0] = 2580 avg3p( E + 4); /* (-1, -1) */ 2581 B[2][3] = B[1][2] = B[0][1] = avg3p( E + 5); /* (-1, 0) */ 2582 B[1][3] = B[0][2] = avg3p( E + 6); /* (-1, 1) */ 2583 B[0][3] = avg3p( E + 7); /* (-1, 2) */ 2584 break; 2586 /* The remaining 4 diagonal modes use lines whose slopes are 2587 +|- 2 and +|- 1/2. The angles of these lines are roughly 2588 +|- 27 degrees from horizontal or vertical. 2590 Unlike the 45 degree diagonals, here we often need to 2591 "synthesize" predictor pixels midway between two actual 2592 predictors using avg2p(p), which we think of as returning 2593 the pixel "at" p[1/2]. */ 2595 case B_VR_PRED: /* SSE (vertical right) step = 2596 (2,1) or (-2,-1) */ 2597 B[3][0] = avg3p( E + 2); /* predictor is from (1, -1) */ 2598 B[2][0] = avg3p( E + 3); /* (0, -1) */ 2599 B[3][1] = B[1][0] = avg3p( E + 4); /* (-1, -1) */ 2600 B[2][1] = B[0][0] = avg2p( E + 4); /* (-1, -1/2) */ 2601 B[3][2] = B[1][1] = avg3p( E + 5); /* (-1, 0) */ 2602 B[2][2] = B[0][1] = avg2p( E + 5); /* (-1, 1/2) */ 2603 B[3][3] = B[1][2] = avg3p( E + 6); /* (-1, 1) */ 2604 B[2][3] = B[0][2] = avg2p( E + 6); /* (-1, 3/2) */ 2605 B[1][3] = avg3p( E + 7); /* (-1, 2) */ 2606 B[0][3] = avg2p( E + 7); /* (-1, 5/2) */ 2607 break; 2609 case B_VL_PRED: /* SSW (vertical left) step = 2610 (2,-1) or (-2,1) */ 2611 B[0][0] = avg2p( A); /* predictor is from (-1, 1/2) */ 2612 B[1][0] = avg3p( A + 1); /* (-1, 1) */ 2613 B[2][0] = B[0][1] = avg2p( A + 1); /* (-1, 3/2) */ 2614 B[1][1] = B[3][0] = avg3p( A + 2); /* (-1, 2) */ 2615 B[2][1] = B[0][2] = avg2p( A + 2); /* (-1, 5/2) */ 2616 B[3][1] = B[1][2] = avg3p( A + 3); /* (-1, 3) */ 2617 B[2][2] = B[0][3] = avg2p( A + 3); /* (-1, 7/2) */ 2618 B[3][2] = B[1][3] = avg3p( A + 4); /* (-1, 4) */ 2619 /* Last two values do not strictly follow the pattern. */ 2620 B[2][3] = avg3p( A + 5); /* (-1, 5) [avg2p( A + 4) = 2621 (-1,9/2)] */ 2622 B[3][3] = avg3p( A + 6); /* (-1, 6) [avg3p( A + 5) = 2623 (-1,5)] */ 2624 break; 2626 case B_HD_PRED: /* ESE (horizontal down) step = 2627 (1,2) or (-1,-2) */ 2628 B[3][0] = avg2p( E); /* predictor is from (5/2, -1) */ 2629 B[3][1] = avg3p( E + 1); /* (2, -1) */ 2630 B[2][0] = B[3][2] = svg2p( E + 1); /* ( 3/2, -1) */ 2631 B[2][1] = B[3][3] = avg3p( E + 2); /* ( 1, -1) */ 2632 B[2][2] = B[1][0] = avg2p( E + 2); /* ( 1/2, -1) */ 2633 B[2][3] = B[1][1] = avg3p( E + 3); /* ( 0, -1) */ 2634 B[1][2] = B[0][0] = avg2p( E + 3); /* (-1/2, -1) */ 2635 B[1][3] = B[0][1] = avg3p( E + 4); /* ( -1, -1) */ 2636 B[0][2] = avg3p( E + 5); /* (-1, 0) */ 2637 B[0][3] = avg3p( E + 6); /* (-1, 1) */ 2638 break; 2640 case B_HU_PRED: /* ENE (horizontal up) step = (1,-2) 2641 or (-1,2) */ 2642 B[0][0] = avg2p( L); /* predictor is from ( 1/2, -1) */ 2643 B[0][1] = avg3p( L + 1); /* ( 1, -1) */ 2644 B[0][2] = B[1][0] = avg2p( L + 1); /* (3/2, -1) */ 2645 B[0][3] = B[1][1] = avg3p( L + 2); /* ( 2, -1) */ 2646 B[1][2] = B[2][0] = avg2p( L + 2); /* (5/2, -1) */ 2647 B[1][3] = B[2][1] = avg3( L[2], L[3], L[3]); /* ( 3, -1) */ 2648 /* Not possible to follow pattern for much of the bottom 2649 row because no (nearby) already-constructed pixels lie 2650 on the diagonals in question. */ 2651 B[2][2] = B[2][3] = B[3][0] = B[3][1] = B[3][2] = B[3][3] 2652 = L[3]; 2653 } 2654 } 2656 ---- End code block ---------------------------------------- 2657 The reference decoder implementation of subblock intra-prediction may 2658 be found in reconintra4x4.c. 2660 13. DCT Coefficient Decoding 2662 The second data partition consists of an encoding of the quantized 2663 DCT (and WHT) coefficients of the residue signal. As discussed in 2664 the format overview (Chapter 2), for each macroblock, the residue is 2665 added to the (intra- or inter-generated) prediction buffer to produce 2666 the final (except for loop-filtering) reconstructed macroblock. 2668 VP8 works exclusively with 4x4 DCTs and WHTs, applied to the 24 (or 2669 25 with the Y2 subblock) 4x4 subblocks of a macroblock. The ordering 2670 of macroblocks within any of the "residue" partitions in general 2671 follows the same raster-scan as used in the first "prediction" 2672 partition. 2674 For all intra- and inter-prediction modes apart from B_PRED (intra: 2675 whose Y subblocks are independently predicted) and SPLIT_MV (inter) 2676 each macroblock's residue record begins with the Y2 component of the 2677 residue, coded using a WHT. B_PRED and SPLIT_MV coded macroblocks 2678 omit this WHT, instead specifying the 0th DCT coefficient of each of 2679 the 16 Y subblocks as part of its DCT. 2681 After the optional Y2 block, the residue record continues with 16 2682 DCTs for the Y subblocks, followed by 4 DCTs for the U subblocks, 2683 ending with 4 DCTs for the V subblocks. The subblocks occur in the 2684 usual order. 2686 The DCTs and WHT are tree-coded using a 12-element alphabet whose 2687 members we call tokens. Except for the end of block token (which 2688 sets the remaining subblock coefficients to zero and is followed by 2689 the next block), each token (sometimes augmented with data 2690 immediately following the token) specifies the value of the single 2691 coefficient at the current (implicit) position and is followed by a 2692 token applying to the next (implicit) position. 2694 For all the Y and chroma subblocks, the ordering of the coefficients 2695 follows a so-called zig-zag order. DCTs begin at coefficient 1 if Y2 2696 is present, and begin at coefficient 0 if Y2 is absent. The WHT for 2697 a Y2 subblock always begins at coefficient 0. 2699 13.1. MB Without non-Zero Coefficient Values 2701 If the flag within macroblock mode info indicates that a macroblock 2702 does not have any non-zero coefficients, the decoding process of DCT 2703 coefficients is skipped for the macroblock. 2705 13.2. Coding of Individual Coefficient Values 2707 The coding of coefficient tokens is the same for the DCT and WHT and 2708 for the remainder of this chapter DCT should be taken to mean either 2709 DCT or WHT. 2711 All tokens (except end-of-block) specify either a single unsigned 2712 value or a range of unsigned values (immediately) followed by a 2713 simple probabilistic encoding of the offset of the value from the 2714 base of that range. 2716 Non-zero values (of either type) are then followed by a flag 2717 indicating the sign of the coded value (negative if 1, positive if 2718 0). 2720 Here are the tokens and decoding tree. 2722 ---- Begin code block -------------------------------------- 2724 typedef enum 2725 { 2726 DCT_0, /* value 0 */ 2727 DCT_1, /* 1 */ 2728 DCT_2, /* 2 */ 2729 DCT_3, /* 3 */ 2730 DCT_4, /* 4 */ 2731 dct_cat1, /* range 5 - 6 (size 2) */ 2732 dct_cat2, /* 7 - 10 (4) */ 2733 dct_cat3, /* 11 - 18 (8) */ 2734 dct_cat4, /* 19 - 34 (16) */ 2735 dct_cat5, /* 35 - 66 (32) */ 2736 dct_cat6, /* 67 - 2048 (1982) */ 2737 dct_eob, /* end of block */ 2739 num_dct_tokens /* 12 */ 2740 } 2741 dct_token; 2743 const tree_index coef_tree [2 * (num_dct_tokens - 1)] = 2744 { 2745 -dct_eob, 2, /* eob = "0" */ 2746 -DCT_0, 4, /* 0 = "10" */ 2747 -DCT_1, 6, /* 1 = "110" */ 2748 8, 12, 2749 -DCT_2, 10, /* 2 = "11100" */ 2750 -DCT_3, -DCT_4, /* 3 = "111010", 4 = "111011" */ 2751 14, 16, 2752 -dct_cat1, -dct_cat2, /* cat1 = "111100", 2753 cat2 = "111101" */ 2754 18, 20, 2755 -dct_cat3, -dct_cat4, /* cat3 = "1111100", 2756 cat4 = "1111101" */ 2757 -dct_cat5, -dct_cat6 /* cat4 = "1111110", 2758 cat4 = "1111111" */ 2759 }; 2761 ---- End code block ---------------------------------------- 2763 While in general all DCT coefficients are decoded using the same 2764 tree, decoding of certain DCT coefficients may skip the first branch, 2765 whose preceding coefficient is a DCT_0. This makes use of the fact 2766 that in any block last non zero coefficient before the end of the 2767 block is not 0, therefore no dct_eob follows a DCT_0 coefficient in 2768 any block. 2770 The tokens dct_cat1 ... dct_cat6 specify ranges of unsigned values, 2771 the value within the range being formed by adding an unsigned offset 2772 (whose width is 1, 2, 3, 4, 5, or 11 bits, respectively) to the base 2773 of the range, using the following algorithm and fixed probability 2774 tables. 2776 ---- Begin code block -------------------------------------- 2778 uint DCTextra( bool_decoder *d, const Prob *p) 2779 { 2780 uint v = 0; 2781 do { v += v + read_bool( d, *p);} while( *++p); 2782 return v; 2783 } 2785 const Prob Pcat1[] = { 159, 0}; 2786 const Prob Pcat2[] = { 165, 145, 0}; 2787 const Prob Pcat3[] = { 173, 148, 140, 0}; 2788 const Prob Pcat4[] = { 176, 155, 140, 135, 0}; 2789 const Prob Pcat5[] = { 180, 157, 141, 134, 130, 0}; 2790 const Prob Pcat6[] = 2791 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0}; 2793 ---- End code block ---------------------------------------- 2795 If v, the unsigned value decoded using the coefficient tree, possibly 2796 augmented by the process above, is non-zero, its sign is set by 2797 simply reading a flag: 2799 ---- Begin code block -------------------------------------- 2801 if( read_bool( d, 128)) 2802 v = -v; 2804 ---- End code block ---------------------------------------- 2806 13.3. Token Probabilities 2808 The probability specification for the token tree (unlike that for the 2809 "extra bits" described above) is rather involved. It uses three 2810 pieces of context to index a large probability table, the contents of 2811 which may be incrementally modified in the frame header. The full 2812 (non-constant) probability table is laid out as follows. 2814 ---- Begin code block -------------------------------------- 2816 Prob coef_probs [4] [8] [3] [num_dct_tokens-1]; 2817 ---- End code block ---------------------------------------- 2819 Working from the outside in, the outermost dimension is indexed by 2820 the type of plane being decoded: 2822 o 0 - Y beginning at coefficient 1 (i.e., Y after Y2) 2824 o 1 - Y2 2826 o 2 - U or V 2828 o 3 - Y beginning at coefficient 0 (i.e., Y in the absence of Y2). 2830 The next dimension is selected by the position of the coefficient 2831 being decoded. That position c steps by ones up to 15, starting from 2832 zero for block types 1, 2, or 3 and starting from one for block type 2833 0. The second array index is then 2835 ---- Begin code block -------------------------------------- 2837 coef_bands [c] 2839 ---- End code block ---------------------------------------- 2841 where 2843 ---- Begin code block -------------------------------------- 2845 const int coef_bands [16] = { 2846 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7 2847 }; 2849 ---- End code block ---------------------------------------- 2851 is a fixed mapping of position to "band". 2853 The third dimension is the trickiest. Roughly speaking, it measures 2854 the "local complexity" or extent to which nearby coefficients are 2855 non-zero. 2857 For the first coefficient (DC, unless the block type is 0), we 2858 consider the (already encoded) blocks within the same plane (Y2, Y, 2859 U, or V) above and to the left of the current block. The context 2860 index is then the number (0, 1 or 2) of these blocks that had at 2861 least one non-zero coefficient in their residue record. 2863 Beyond the first coefficient, the context index is determined by the 2864 absolute value of the most recently decoded coefficient (necessarily 2865 within the current block) and is 0 if the last coefficient was a 2866 zero, 1 if it was plus or minus one, and 2 if its absolute value 2867 exceeded one. 2869 Note that the intuitive meaning of this measure changes as 2870 coefficients are decoded. For example, prior to the first token, a 2871 zero means that the neighbors are empty, suggesting that the current 2872 block may also be empty. After the first token, because an end-of- 2873 block token must have at least one non-zero value before it, a zero 2874 means that we just decoded a zero and hence guarantees that a non- 2875 zero coefficient will appear later in this block. However, this 2876 shift in meaning is perfectly okay because the complete context 2877 depends also on the coefficient band (and since band 0 is occupied 2878 exclusively by position 0). 2880 As with other contexts used by VP8, the "neighboring block" context 2881 described here needs a special definition for subblocks lying along 2882 the top row or left edge of the frame. These "non-existent" 2883 predictors above and to the left of the image are simply taken to be 2884 empty -- that is, taken to contain no non-zero coefficients. 2886 The residue decoding of each macroblock then requires, in each of two 2887 directions (above and to the left), an aggregate coefficient 2888 predictor consisting of a single Y2 predictor, two predictors for 2889 each of U and V, and four predictors for Y. In accordance with the 2890 scan-ordering of macroblocks, a decoder needs to maintain a single 2891 "left" aggregate predictor and a row of "above" aggregate predictors. 2893 Before decoding any residue, these maintained predictors may simply 2894 be cleared, in compliance with the definition of "non-existent" 2895 prediction. After each block is decoded, the two predictors 2896 referenced by the block are replaced with the (empty or non-empty) 2897 state of the block, in preparation for the later decoding of the 2898 blocks below and to the right of the block just decoded. 2900 The fourth, and final, dimension of the token probability array is of 2901 course indexed by (half) the position in the token tree structure, as 2902 are all tree probability arrays. 2904 The below pseudo-code illustrates the decoding process. Note that 2905 criteria, functions, etc. delimited with ** are either dependent on 2906 decoder architecture or are elaborated on elsewhere in this document. 2908 ---- Begin code block -------------------------------------- 2910 int block[16] = { 0 }; /* current 4x4 block coeffs */ 2911 int firstCoeff = 0; 2912 int plane; 2913 int ctx2; 2914 int ctx3 = 0; /* the 3rd context referred to in above description */ 2915 Prob *probTable; 2916 int token; 2917 int sign; 2918 int absValue; 2919 int extraBits; 2920 bool prevCoeffWasZero = false; 2921 bool currentBlockHasCoeffs = false; 2922 /* base coeff abs values per each category, elem #0 is 2923 DCT_VAL_CATEGORY1, * #1 is DCT_VAL_CATEGORY2 etc */ 2924 int categoryBase[6] = { 5, 7, 11, 19, 35, 67 }; 2926 /* Determine plane to use */ 2927 if( **current_block_is_Y2_block** ) plane = 0; 2928 else if ( **current_block_is_chroma** ) plane = 2; 2929 else if ( **current_macroblock_has_Y2** ) plane = 1; 2930 else plane = 3; 2932 /* For luma blocks of an "Y2 macroblock" we skip coeff index #0 */ 2933 if( plane == 1 ) 2934 firstCoeff++; 2936 /* Determine whether neighbour 4x4 blocks have coeffiecients. 2937 This is dependant of the plane we are currently decoding; 2938 i.e. we check only coefficients from same plane as current 2939 block. */ 2940 if( **left_neighbor_block_has_coefficients(plane)** ) 2941 ctx3++; 2942 if( **above_neighbor_block_has_coefficients(plane)** ) 2943 ctx3++; 2945 for( i = firstCoeff ; i < 16 ; ++i ) 2946 { 2947 ctx2 = coef_bands[i]; 2948 probTable = coef_probs[plane][ctx2][ctx3]; 2950 /* skip first code (dct_eob) if previous token was DCT_0 */ 2951 if( prevCoeffWasZero ) 2952 token = treed_read ( d, **coef_tree_without_eob**, 2953 probTable ); 2954 else 2955 token = treed_read ( d, coef_tree, probTable ); 2957 if( token == dct_eob ) 2958 break; 2960 if( token != DCT_0 ) 2961 { 2962 currentBlockHasCoeffs = true; 2963 if( **token_has_extra_bits(token)** ) 2964 { 2965 extraBits = DCTextra( token ); 2966 absValue = 2967 categoryBase[**token_to_cat_index(token)**] + 2968 extraBits; 2969 } 2970 else 2971 { 2972 absValue = **token_to_abs_value(token)**; 2973 } 2975 sign = read_bool(d, 128); 2976 block[i] = sign ? -absValue : absValue; 2977 } 2978 else 2979 { 2980 absValue = 0; 2981 } 2983 /* Set contexts and stuff for next coeff */ 2984 if( absValue == 0 ) ctx3 = 0; 2985 else if ( absValue == 1 ) ctx3 = 1; 2986 else ctx3 = 2; 2987 prevCoeffWasZero = true; 2988 } 2990 /* Store current block status to decoder internals */ 2991 **block_has_coefficients[currentMb][currentBlock]** = 2992 currentBlockHasCoeffs; 2994 ---- End code block ---------------------------------------- 2996 While we have in fact completely described the coefficient decoding 2997 procedure, the reader will probably find it helpful to consult the 2998 reference implementation, which can be found in the file 2999 detokenize.c. 3001 13.4. Token Probability Updates 3003 As mentioned above, the token-decoding probabilities may change from 3004 frame to frame. After detection of a key frame, they are of course 3005 set to their defaults shown in Section 13.5; this must occur before 3006 decoding the remainder of the header, as both key frames and 3007 interframes may adjust these probabilities. 3009 The layout and semantics of the coefficient probability update record 3010 (Section I of the frame header) are straightforward. For each 3011 position in the coef_probs array there occurs a fixed-probability 3012 bool indicating whether or not the corresponding probability should 3013 be updated. If the bool is true, there follows a P(8) replacing that 3014 probability. Note that updates are cumulative, that is, a 3015 probability updated on one frame is in effect for all ensuing frames 3016 until the next key frame, or until the probability is explicitly 3017 updated by another frame. 3019 The algorithm to effect the foregoing is simple: 3021 ---- Begin code block -------------------------------------- 3023 int i = 0; do { 3024 int j = 0; do { 3025 int k = 0; do { 3026 int t = 0; do { 3028 if( read_bool( d, coef_update_probs [i] [j] [k] [t])) 3029 coef_probs [i] [j] [k] [t] = read_literal( d, 8); 3031 } while( ++t < num_dct_tokens - 1); 3032 } while( ++k < 3); 3033 } while( ++j < 8); 3034 } while( ++i < 4); 3036 ---- End code block ---------------------------------------- 3038 The (constant) update probabilities are as follows (they may also be 3039 found in the reference decoder file coef_update_probs.c). 3041 ---- Begin code block -------------------------------------- 3043 const Prob coef_update_probs [4] [8] [3] [num_dct_tokens-1] = 3044 { 3045 { 3046 { 3047 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3048 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3049 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3050 }, 3051 { 3052 { 176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3053 { 223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3054 { 249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255} 3055 }, 3056 { 3057 { 255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3058 { 234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3059 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3060 }, 3061 { 3062 { 255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3063 { 239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3064 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3065 }, 3066 { 3067 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3068 { 251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3069 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3070 }, 3071 { 3072 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3073 { 251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3074 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3075 }, 3076 { 3077 { 255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255}, 3078 { 250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255}, 3079 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3080 }, 3081 { 3082 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3083 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3084 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3085 } 3086 }, 3087 { 3088 { 3089 { 217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3090 { 225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255}, 3091 { 234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255} 3092 }, 3093 { 3094 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3095 { 223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3096 { 238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3097 }, 3098 { 3099 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3100 { 249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3101 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3102 }, 3103 { 3104 { 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3105 { 247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3106 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3107 }, 3108 { 3109 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3110 { 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3111 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3112 }, 3113 { 3114 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3115 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3116 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3117 }, 3118 { 3119 { 255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3120 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3121 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3122 }, 3123 { 3124 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3125 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3126 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3127 } 3128 }, 3129 { 3130 { 3131 { 186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255}, 3132 { 234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255}, 3133 { 251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255} 3134 }, 3135 { 3136 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3137 { 236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3138 { 251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255} 3139 }, 3140 { 3141 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3142 { 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3143 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3144 }, 3145 { 3146 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3147 { 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3148 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3149 }, 3150 { 3151 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3152 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3153 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3154 }, 3155 { 3156 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3157 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3158 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3159 }, 3160 { 3161 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 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 }, 3165 { 3166 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 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 } 3170 }, 3171 { 3172 { 3173 { 248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3174 { 250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255}, 3175 { 248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255} 3176 }, 3177 { 3178 { 255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3179 { 246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3180 { 252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255} 3181 }, 3182 { 3183 { 255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3184 { 248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3185 { 253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3186 }, 3187 { 3188 { 255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3189 { 245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3190 { 253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3191 }, 3192 { 3193 { 255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3194 { 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3195 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3196 }, 3197 { 3198 { 255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3199 { 249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3200 { 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3202 }, 3203 { 3204 { 255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3205 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3206 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3207 }, 3208 { 3209 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3210 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3211 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3212 } 3213 } 3214 }; 3216 ---- End code block ---------------------------------------- 3218 13.5. Default Token Probability Table 3220 The default token probabilities are as follows. 3222 ---- Begin code block -------------------------------------- 3224 const Prob default_coef_probs [4] [8] [3] [num_dct_tokens - 1] = 3225 { 3226 { 3227 { 3228 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3229 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3230 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3231 }, 3232 { 3233 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 3234 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 3235 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 3236 }, 3237 { 3238 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 3239 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 3240 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 3241 }, 3242 { 3243 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 3244 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 3245 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 3246 }, 3247 { 3248 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 3249 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 3250 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 3251 }, 3252 { 3253 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 3254 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 3255 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 3256 }, 3257 { 3258 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 3259 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 3260 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 3261 }, 3262 { 3263 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3264 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3265 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3266 } 3267 }, 3268 { 3269 { 3270 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 3271 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 3272 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 3273 }, 3274 { 3275 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 3276 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 3277 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 3278 }, 3279 { 3280 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 3281 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 3282 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 3283 }, 3284 { 3285 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 3286 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 3287 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 3288 }, 3289 { 3290 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 3291 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 3292 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 3293 }, 3294 { 3295 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 3296 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 3297 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 3299 }, 3300 { 3301 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 3302 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 3303 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 3304 }, 3305 { 3306 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 3307 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3308 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 3309 } 3310 }, 3311 { 3312 { 3313 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 3314 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 3315 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 3316 }, 3317 { 3318 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 3319 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 3320 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 3321 }, 3322 { 3323 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 3324 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 3325 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 3326 }, 3327 { 3328 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 3329 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 3330 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 3331 }, 3332 { 3333 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3334 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 3335 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3336 }, 3337 { 3338 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3339 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3340 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3341 }, 3342 { 3343 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 3344 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 3345 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3346 }, 3347 { 3348 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3349 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3350 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3351 } 3352 }, 3353 { 3354 { 3355 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 3356 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 3357 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 3358 }, 3359 { 3360 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 3361 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 3362 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 3363 }, 3364 { 3365 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 3366 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 3367 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 3368 }, 3369 { 3370 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 3371 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 3372 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 3373 }, 3374 { 3375 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 3376 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 3377 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 3378 }, 3379 { 3380 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 3381 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 3382 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 3383 }, 3384 { 3385 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 3386 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 3387 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 3388 }, 3389 { 3390 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3391 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3392 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3393 } 3394 } 3396 }; 3398 ---- End code block ---------------------------------------- 3400 14. DCT and WHT Inversion and Macroblock Reconstruction 3402 14.1. Dequantization 3404 After decoding the DCTs/WHTs as described above, each (quantized) 3405 coefficient in each subblock is multiplied by one of six 3406 dequantization factors, the choice of factor depending on the plane 3407 (Y2, Y, or chroma) and position (DC = coefficient zero, AC = any 3408 other coefficient). If the current macroblock has overridden the 3409 quantization level (as described in Chapter 10) then the six factors 3410 are looked up from two dequantization tables with appropriate scaling 3411 and clamping using the single index supplied by the override. 3412 Otherwise, the frame-level dequantization factors (as described in 3413 Section 9.6) are used. In either case, the multiplies are computed 3414 and stored using 16-bit signed integers. 3416 The two dequantization tables, which may also be found in the 3417 reference decoder file quant_common.c, are as follows. 3419 ---- Begin code block -------------------------------------- 3421 static const int dc_qlookup[QINDEX_RANGE] = 3422 { 3423 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 3424 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 3425 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 3426 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 3427 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 3428 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 3429 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 3430 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 3431 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, 126, 128, 130, 3432 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, 3433 }; 3435 static const int ac_qlookup[QINDEX_RANGE] = 3436 { 3437 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 3438 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3439 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 3440 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 3441 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 3442 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 3443 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 3444 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 3445 181, 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, 3446 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, 3447 }; 3449 ---- End code block ---------------------------------------- 3451 Lookup values from the above two tables are directly used the DC and 3452 AC coefficients in Y1 respectively. For Y2 and chroma, values from 3453 above tables undergo either a scaling process or clamping processing 3454 before the multiplies. Details to these scaling and clamping can be 3455 found related lookup functions in quant_common.c. 3457 14.2. Inverse Transforms 3459 If the Y2 residue block exists (i.e., the macroblock luma mode is not 3460 SPLITMV or B_PRED), it is inverted first (using the inverse WHT) and 3461 the element of the result at row i, column j is used as the 0th 3462 coefficient of the Y subblock at position (i, j), that is, the Y 3463 subblock whose index is (i * 4) + j. As discussed in Chapter 13, if 3464 the luma mode is B_PRED or SPLITMV, the 0th Y coefficients are part 3465 of the residue signal for the subblocks themselves. 3467 In either case, the inverse transforms for the sixteen Y subblocks 3468 and eight chroma subblocks are computed next. All 24 of these 3469 inversions are independent of each other; their results may (at least 3470 conceptually) be stored in 24 separate 4x4 arrays. 3472 As is done by the reference decoder, an implementation may wish to 3473 represent the prediction and residue buffers as macroblock-sized 3474 arrays (that is, a 16x16 Y buffer and two 8x8 chroma buffers). 3475 Regarding the inverse DCT implementation given below, this requires a 3476 simple adjustment to the address calculation for the resulting 3477 residue pixels. 3479 14.3. Implementation of the WHT Inversion 3481 As previously discussed (see Chapters 2 and 13), for macroblocks 3482 encoded using prediction modes other than B_PRED and SPLITMV, the DC 3483 values derived from the DCT transform on the 16 Y blocks are 3484 collected to construct a 25th block of a macroblock (16 Y, 4 U, 4 V 3485 constitute the 24 blocks). This 25th block is transformed using a 3486 Walsh-Hadamard transform (WHT). 3488 The inputs to the inverse WHT (that is, the dequantized 3489 coefficients), the intermediate "horizontally detransformed" signal, 3490 and the completely detransformed residue signal are all stored as 3491 arrays of 16-bit signed integers. 3493 Following the tradition of specifying bitstream format using the 3494 decoding process, we specify the inverse WHT in the decoding process 3495 using the following C style source code: 3497 ---- Begin code block -------------------------------------- 3499 void vp8_short_inv_walsh4x4_c(short *input, short *output) 3500 { 3501 int i; 3502 int a1, b1, c1, d1; 3503 int a2, b2, c2, d2; 3504 short *ip = input; 3505 short *op = output; 3506 int temp1, temp2; 3508 for(i=0;i<4;i++) 3509 { 3510 a1 = ip[0] + ip[12]; 3511 b1 = ip[4] + ip[8]; 3512 c1 = ip[4] - ip[8]; 3513 d1 = ip[0] - ip[12]; 3514 op[0] = a1 + b1; 3515 op[4] = c1 + d1; 3516 op[8] = a1 - b1; 3517 op[12]= d1 - c1; 3518 ip++; 3519 op++; 3520 } 3521 ip = output; 3522 op = output; 3523 for(i=0;i<4;i++) 3524 { 3525 a1 = ip[0] + ip[3]; 3526 b1 = ip[1] + ip[2]; 3527 c1 = ip[1] - ip[2]; 3528 d1 = ip[0] - ip[3]; 3530 a2 = a1 + b1; 3531 b2 = c1 + d1; 3532 c2 = a1 - b1; 3533 d2 = d1 - c1; 3535 op[0] = (a2+3)>>3; 3536 op[1] = (b2+3)>>3; 3537 op[2] = (c2+3)>>3; 3538 op[3] = (d2+3)>>3; 3540 ip+=4; 3541 op+=4; 3542 } 3543 } 3545 ---- End code block ---------------------------------------- 3547 In the case that there is only one non-zero DC value in input, the 3548 inverse transform can be simplified to the following: 3550 ---- Begin code block -------------------------------------- 3552 void vp8_short_inv_walsh4x4_1_c(short *input, short *output) 3553 { 3554 int i; 3555 int a1; 3556 short *op=output; 3558 a1 = ((input[0] + 3)>>3); 3560 for(i=0;i<4;i++) 3561 { 3562 op[0] = a1; 3563 op[1] = a1; 3564 op[2] = a1; 3565 op[3] = a1; 3566 op+=4; 3567 } 3568 } 3570 ---- End code block ---------------------------------------- 3572 It should be noted, a conforming decoder should implement the inverse 3573 transform using exactly the same rounding to achieve bit-wise 3574 matching output to the output of the process specified by the above 3575 "C" source code. 3577 The reference decoder WHT inversion may be found in the files 3578 invtrans.c and idctllm.c. 3580 14.4. Implementation of the DCT Inversion 3582 All of the DCT inversions are computed in exactly the same way. In 3583 principle, VP8 uses a classical 2D inverse discrete cosine transform, 3584 implemented as two passes of 1-D inverse DCT. The 1-D inverse DCT 3585 was calculated using a similar algorithm to what was described in 3586 [Loeffler]. However, the paper only provided the 8-point and 16- 3587 point version of the algorithms, which was adapted by On2 to perform 3588 the 4-point 1-D DCT. 3590 Accurate calculation of 1-D DCT of the above algorithm requires 3591 infinite precision. VP8 of course can use only a finite-precision 3592 approximation. Also, the inverse DCT used by VP8 takes care of 3593 normalization of the standard unitary transform, that is, every 3594 dequantized coefficient has roughly double the size of the 3595 corresponding unitary coefficient. However, at all but the highest 3596 datarates, the discrepancy between transmitted and ideal coefficients 3597 is due almost entirely to (lossy) compression and not to errors 3598 induced by finite-precision arithmetic. 3600 The inputs to the inverse DCT (that is, the dequantized 3601 coefficients), the intermediate "horizontally detransformed" signal, 3602 and the completely detransformed residue signal are all stored as 3603 arrays of 16-bit signed integers. The details of the computation are 3604 as follows. 3606 It should also be noted that this implementation makes use of 16-bit 3607 fixed point version of two multiplication constants: 3609 sqrt(2) * cos (pi/8) 3611 sqrt(2) * sin (pi/8) 3613 Because the first constant is bigger than 1, to maintain the same 16- 3614 bit fixed point precision as the second one, we make use of the fact 3615 that 3617 x * a = x + x*(a-1) 3619 therefore 3621 x * sqrt(2) * cos (pi/8) = x + x * ( sqrt(2) * cos(pi/8)-1) 3623 ---- Begin code block -------------------------------------- 3625 /* IDCT implementation */ 3626 static const int cospi8sqrt2minus1=20091; 3627 static const int sinpi8sqrt2 =35468; 3628 void short_idct4x4llm_c(short *input, short *output, int pitch) 3629 { 3630 int i; 3631 int a1, b1, c1, d1; 3633 short *ip=input; 3634 short *op=output; 3635 int temp1, temp2; 3636 int shortpitch = pitch>>1; 3638 for(i=0;i<4;i++) 3639 { 3640 a1 = ip[0]+ip[8]; 3641 b1 = ip[0]-ip[8]; 3643 temp1 = (ip[4] * sinpi8sqrt2)>>16; 3644 temp2 = ip[12]+((ip[12] * cospi8sqrt2minus1)>>16); 3645 c1 = temp1 - temp2; 3647 temp1 = ip[4] + ((ip[4] * cospi8sqrt2minus1)>>16); 3648 temp2 = (ip[12] * sinpi8sqrt2)>>16; 3649 d1 = temp1 + temp2; 3651 op[shortpitch*0] = a1+d1; 3652 op[shortpitch*3] = a1-d1; 3653 op[shortpitch*1] = b1+c1; 3654 op[shortpitch*2] = b1-c1; 3656 ip++; 3657 op++; 3658 } 3659 ip = output; 3660 op = output; 3661 for(i=0;i<4;i++) 3662 { 3663 a1 = ip[0]+ip[2]; 3664 b1 = ip[0]-ip[2]; 3666 temp1 = (ip[1] * sinpi8sqrt2)>>16; 3667 temp2 = ip[3]+((ip[3] * cospi8sqrt2minus1)>>16); 3668 c1 = temp1 - temp2; 3670 temp1 = ip[1] + ((ip[1] * cospi8sqrt2minus1)>>16); 3671 temp2 = (ip[3] * sinpi8sqrt2)>>16; 3672 d1 = temp1 + temp2; 3674 op[0] = (a1+d1+4)>>3; 3675 op[3] = (a1-d1+4)>>3; 3676 op[1] = (b1+c1+4)>>3; 3677 op[2] = (b1-c1+4)>>3; 3679 ip+=shortpitch; 3680 op+=shortpitch; 3681 } 3682 } 3684 ---- End code block ---------------------------------------- 3686 The reference decoder DCT inversion may be found in the files 3687 invtrans.c and idctllm.c. 3689 14.5. Summation of Predictor and Residue 3691 Finally, the prediction and residue signals are summed to form the 3692 reconstructed macroblock, which, except for loop filtering (taken up 3693 next), completes the decoding process. 3695 The summing procedure is fairly straightforward, having only a couple 3696 of details. The prediction and residue buffers are both arrays of 3697 16-bit signed integers. Each individual (Y, U, and V pixel) result 3698 is calculated first as a 32-bit sum of the prediction and residue, 3699 and is then saturated to 8-bit unsigned range (using, say, the 3700 clamp255 function defined above) before being stored as an 8-bit 3701 unsigned pixel value. 3703 VP8 also supports a mode where the encoding of a bitstream guarantees 3704 all reconstructed pixel values between 0 and 255, compliant 3705 bitstreams of such requirements have the clamp_type bit in the frame 3706 header set to 1. In such case, the clamp255 is no longer required. 3708 The summation process is the same, regardless of the (intra or inter) 3709 mode of prediction in effect for the macroblock. The reference 3710 decoder implementation of reconstruction may be found in the file 3711 recon.c. 3713 15. Loop Filter 3715 Loop filtering is the last stage of frame reconstruction and the 3716 next-to-last stage of the decoding process. The loop filter is 3717 applied to the entire frame after the summation of predictor and 3718 residue described in Chapter 14. 3720 The purpose of the loop filter is to eliminate (or at least reduce) 3721 visually objectionable artifacts associated with the semi- 3722 independence of the coding of macroblocks and their constituent 3723 subblocks. 3725 As was discussed in Chapter 5, the loop filter is "integral" to 3726 decoding, in that the results of loop filtering are used in the 3727 prediction of subsequent frames. Consequently, a functional decoder 3728 implementation must perform loop filtering exactly as described here. 3729 This is in distinction to any postprocessing that may be applied only 3730 to the image immediately before display; such postprocessing is 3731 entirely at the option of the implementor (and/or user) and has no 3732 effect on decoding per se. 3734 The baseline frame level parameters controlling the loop filter are 3735 defined in the frame header (Chapter 9.4) along with a mechanism for 3736 adjustment based on a macroblock's prediction mode and/or reference 3737 frame. The first is a flag selecting the type of filter (normal or 3738 simple), the other two are numbers (loop_filter_level and 3739 sharpness_level) that adjust the strength or sensitivity of the 3740 filter. As described in Chapters 9.3 and 10, loop_filter_level may 3741 be also overridden on a per-macroblock basis using segmentation. 3743 Loop filtering is one of the more computationally-intensive aspects 3744 of VP8 decoding. This is the reason for the existence of the 3745 optional less-demanding simple filter type. Also, the loop filter is 3746 completely disabled if the loop_filter_level in the frame header is 3747 zero; macroblock-level overrides are ignored in this case. (It is of 3748 course possible for a compressor to encode a frame in which only a 3749 few macroblocks are loop filtered: The global loop_filter_level must 3750 be non-zero and each macroblock can select one of four levels, most 3751 of which could be zero.) 3753 To facilitate efficient implementation, the VP8 decoding algorithms 3754 generally, and the loop filter especially, were designed with SIMD 3755 ("Single Instruction Multiple Datum" or "integer vector") processors 3756 in mind. The reference decoder implementation of loop filtering 3757 (found in loopfilter.c) is, in effect, a portable SIMD specification 3758 of the loop filtering algorithms intended to simplify a realization 3759 on an actual SIMD processor. 3761 Unfortunately, the approach taken there does not lead to maximal 3762 efficency (restricted to the C language, that is) and, as far as a 3763 pure algorithm specification is concerned, is in places obscure. For 3764 example, various aspects of filtering are conditioned on absolute 3765 differences lying below certain thresholds. An ordinary C 3766 implementation would simply discriminate amongst these behaviors 3767 using if statements. The reference decoder instead effects this by 3768 "masking arithmetic", that is, using "and" operations to 3769 (conditionally) zero-out values to be added or subtracted to pixels. 3770 Furthermore, the structure holding the various threshold values is 3771 artificially parallelized. While this mimics closely the approach 3772 taken in vector-processor machine language, it is not how one usually 3773 programs in C. 3775 In this document, we take a different approach and present the 3776 algorithms in a more straightforward, idiomatic, and terse C style. 3777 Together with the reference version, we hope to provide the "best of 3778 both worlds", that is, a pure algorithm specification here and a 3779 strong suggestion as to an optimal actual implementation in 3780 loopfilter.c. 3782 We begin by discussing the aspects of loop filtering that are 3783 independent of the controlling parameters and type of filter chosen. 3785 15.1. Filter Geometry and Overall Procedure 3787 The Y, U, and V planes are processed independently and, except for 3788 the values of certain control parameters (derived from the 3789 loop_filter_level and sharpness_level), identically. 3791 The loop filter acts on the edges between adjacent macroblocks and on 3792 the edges between adjacent subblocks of a macroblock. All such edges 3793 are horizontal or vertical. For each pixel position on an edge, a 3794 small number (two or three) of pixels adjacent to either side of the 3795 position are examined and possibly modified. The displacements of 3796 these pixels are at a right angle to the edge orientation, that is, 3797 for a horizontal edge, we treat the pixels immediately above and 3798 below the edge position, for a vertical edge, we treat the pixels 3799 immediately to the left and right of the edge. 3801 We call this collection of pixels associated to an edge position a 3802 segment; the length of a segment is 2, 4, 6, or 8. Excepting that 3803 the normal filter uses slightly different algorithms for, and that 3804 either filter may apply different control parameters to, the edges 3805 between macroblocks and those between subblocks, the treatment of 3806 edges is quite uniform: All segments straddling an edge are treated 3807 identically, there is no distinction between the treatment of 3808 horizontal and vertical edges, whether between macroblocks or between 3809 subblocks. 3811 As a consequence, adjacent subblock edges within a macroblock may be 3812 concatenated and processed in their entirety. There is a single 3813 8-pixel long vertical edge horizontally centered in each of the U and 3814 V blocks (the concatenation of upper and lower 4-pixel edges between 3815 chroma subblocks), and three 16-pixel long vertical edges at 3816 horizontal positions 1/4, 1/2, and 3/4 the width of the luma 3817 macroblock, each representing the concatenation of four 4-pixel sub- 3818 edges between pairs of Y subblocks. 3820 The macroblocks comprising the frame are processed in the usual 3821 raster-scan order. Each macroblock is "responsible for" the inter- 3822 macroblock edges immediately above and left of it (but not the edges 3823 below and right of it), as well as the edges between its subblocks. 3825 For each macroblock M, there are four filtering steps, which are, 3826 (almost) in order: 3828 1. If M is not on the leftmost column of macroblocks, filter across 3829 the left (vertical) inter-macroblock edge of M. 3831 2. Filter across the vertical subblock edges within M. 3833 3. If M is not on the topmost row of macroblocks, filter across the 3834 top (horizontal) inter-macroblock edge of M. 3836 4. Filter across the horizontal subblock edges within M. 3838 We write MY, MU, and MV for the planar constituents of M, that is, 3839 the 16x16 luma block, 8x8 U block, and 8x8 V block comprising M. 3841 In step 1, for each of the three blocks MY, MU, and MV, we filter 3842 each of the (16 luma or 8 chroma) segments straddling the column 3843 separating the block from the block immediately to the left of it, 3844 using the inter-macroblock filter and controls associated to the 3845 loop_filter_level and sharpness_level. 3847 In step 4, we filter across the (three luma and one each for U and V) 3848 vertical subblock edges described above, this time using the inter- 3849 subblock filter and controls. 3851 Step 2 and 4 are skipped for macroblocks that satisfy both of the 3852 following two conditions: 3854 1. Macroblock coding mode is neither B_PRED nor SPLTMV; and 3855 2. There is no DCT coefficient coded for the whole macroblock. 3857 For these macroblocks, loop filtering for edges between subblocks 3858 internal to a macroblock is effectively skipped. This skip strategy 3859 significantly reduces VP8 loop-filtering complexity. 3861 Edges between macroblocks and those between subblocks are treated 3862 with different control parameters (and, in the case of the normal 3863 filter, with different algorithms); luma and chroma edges are also 3864 treated with different control parameters. Except for pixel 3865 addressing, there is no distinction between the treatment of vertical 3866 and horizontal edges. Luma edges are always 16 pixels long, chroma 3867 edges are always 8 pixels long, and the segments straddling an edge 3868 are treated identically; this of course facilitates vector 3869 processing. 3871 Because many pixels belong to segments straddling two or more edges, 3872 and so will be filtered more than once, the order in which edges are 3873 processed given above must be respected by any implementation. 3874 Within a single edge, however, the segments straddling that edge are 3875 disjoint and the order in which these segments are processed is 3876 immaterial. 3878 Before taking up the filtering algorithms themselves, we should 3879 emphasize a point already made: Even though the pixel segments 3880 associated to a macroblock are antecedent to the macroblock (that is, 3881 lie within the macroblock or in already-constructed macroblocks), a 3882 macroblock must not be filtered immediately after its 3883 "reconstruction" (described in Chapter 14). Rather, the loop filter 3884 applies after all the macroblocks have been "reconstructed" (i.e., 3885 had their predictor summed with their residue); correct decoding is 3886 predicated on the fact that already-constructed portions of the 3887 current frame referenced via intra-prediction (described in Chapter 3888 12) are not yet filtered. 3890 15.2. Simple Filter 3892 Having described the overall procedure of, and pixels affected by, 3893 the loop filter, we turn our attention to the treatment of individual 3894 segments straddling edges. We begin by describing the simple filter, 3895 which, as the reader might guess, is somewhat simpler than the normal 3896 filter. 3898 Note that the simple filter only applies to luma edges. Chroma edges 3899 are left unfiltered. 3901 Roughly speaking, the idea of loop filtering is, within limits, to 3902 reduce the difference between pixels straddling an edge. Differences 3903 in excess of a threshold (associated to the loop_filter_level) are 3904 assumed to be "natural" and are unmodified; differences below the 3905 threshold are assumed to be artifacts of quantization and the 3906 (partially) separate coding of blocks, and are reduced via the 3907 procedures described below. While the loop_filter_level is in 3908 principle arbitrary, the levels chosen by a VP8 compressor tend to be 3909 correlated to quantization levels. 3911 Most of the filtering arithmetic is done using 8-bit signed operands 3912 (having a range -128 to +127, inclusive), supplemented by 16-bit 3913 temporaries holding results of multiplies. 3915 Sums and other temporaries need to be "clamped" to a valid signed 3916 8-bit range: 3918 ---- Begin code block -------------------------------------- 3920 int8 c( int v) 3921 { 3922 return (int8) (v < -128 ? -128 : (v < 128 ? v : 127)); 3923 } 3925 ---- End code block ---------------------------------------- 3927 Since pixel values themselves are unsigned 8-bit numbers, we need to 3928 convert between signed and unsigned values: 3930 ---- Begin code block -------------------------------------- 3932 /* Convert pixel value (0 <= v <= 255) to an 8-bit signed 3933 number. */ 3934 int8 u2s( Pixel v) { return (int8) (v - 128);} 3936 /* Clamp, then convert signed number back to pixel value. */ 3937 Pixel s2u( int v) { return (Pixel) ( c(v) + 128);} 3939 ---- End code block ---------------------------------------- 3941 Filtering is often predicated on absolute-value thresholds. The 3942 following function is the equivalent of the standard library function 3943 abs, whose prototype is found in the standard header file stdlib.h. 3944 For us, the argument v is always the difference between two pixels 3945 and lies in the range -255 <= v <= +255. 3947 ---- Begin code block -------------------------------------- 3949 int abs( int v) { return v < 0? -v : v;} 3950 ---- End code block ---------------------------------------- 3952 An actual implementation would of course use inline functions or 3953 macros to accomplish these trivial procedures (which are used by both 3954 the normal and simple loop filters). An optimal implementation would 3955 probably express them in machine language, perhaps using SIMD vector 3956 instructions. On many SIMD processors, the saturation accomplished 3957 by the above clamping function is often folded into the arithmetic 3958 instructions themselves, obviating the explicit step taken here. 3960 To simplify the specification of relative pixel positions, we use the 3961 word before to mean "immediately above" (for a vertical segment 3962 straddling a horizontal edge) or "immediately to the left of" (for a 3963 horizontal segment straddling a vertical edge) and the word after to 3964 mean "immediately below" or "immediately to the right of". 3966 Given an edge, a segment, and a limit value, the simple loop filter 3967 computes a value based on the four pixels that straddle the edge (two 3968 either side). If that value is below a supplied limit, then, very 3969 roughly speaking, the two pixel values are brought closer to each 3970 other, "shaving off" something like a quarter of the difference. The 3971 same procedure is used for all segments straddling any type of edge, 3972 regardless of the nature (inter-macroblock, inter-subblock, luma, or 3973 chroma) of the edge; only the limit value depends on the edge-type. 3975 The exact procedure (for a single segment) is as follows; the 3976 subroutine common_adjust is used by both the simple filter presented 3977 here and the normal filters discussed in Section 15.3. 3979 ---- Begin code block -------------------------------------- 3981 int8 common_adjust( 3982 int use_outer_taps, /* filter is 2 or 4 taps wide */ 3983 const Pixel *P1, /* pixel before P0 */ 3984 Pixel *P0, /* pixel before edge */ 3985 Pixel *Q0, /* pixel after edge */ 3986 const Pixel *Q1 /* pixel after Q0 */ 3987 ) { 3988 cint8 p1 = u2s( *P1); /* retrieve and convert all 4 pixels */ 3989 cint8 p0 = u2s( *P0); 3990 cint8 q0 = u2s( *Q0); 3991 cint8 q1 = u2s( *Q1); 3993 /* Disregarding clamping, when "use_outer_taps" is false, 3994 "a" is 3*(q0-p0). Since we are about to divide "a" by 3995 8, in this case we end up multiplying the edge 3996 difference by 5/8. 3998 When "use_outer_taps" is true (as for the simple filter), 3999 "a" is p1 - 3*p0 + 3*q0 - q1, which can be thought of as 4000 a refinement of 2*(q0 - p0) and the adjustment is 4001 something like (q0 - p0)/4. */ 4003 int8 a = c( ( use_outer_taps? c(p1 - q1) : 0 ) + 3*(q0 - p0) ); 4005 /* b is used to balance the rounding of a/8 in the case where 4006 the "fractional" part "f" of a/8 is exactly 1/2. */ 4008 cint8 b = (c(a + 3)) >> 3; 4010 /* Divide a by 8, rounding up when f >= 1/2. 4011 Although not strictly part of the "C" language, 4012 the right-shift is assumed to propagate the sign bit. */ 4014 a = c( a + 4) >> 3; 4016 /* Subtract "a" from q0, "bringing it closer" to p0. */ 4018 *Q0 = s2u( q0 - a); 4020 /* Add "a" (with adjustment "b") to p0, "bringing it closer" 4021 to q0. 4023 The clamp of "a+b", while present in the reference decoder, 4024 is superfluous; we have -16 <= a <= 15 at this point. */ 4026 *P0 = s2u( p0 + b); 4028 return a; 4029 } 4031 ---- End code block ---------------------------------------- 4032 ---- Begin code block -------------------------------------- 4034 void simple_segment( 4035 uint8 edge_limit, /* do nothing if edge difference 4036 exceeds limit */ 4037 const Pixel *P1, /* pixel before P0 */ 4038 Pixel *P0, /* pixel before edge */ 4039 Pixel *Q0, /* pixel after edge */ 4040 const Pixel *Q1 /* pixel after Q0 */ 4041 ) { 4042 if( (abs(*P0 - *Q0)*2 + abs(*P1-*Q1)/2) <= edge_limit)) 4043 common_adjust( 1, P1, P0, Q0, Q1); /* use outer taps */ 4044 } 4046 ---- End code block ---------------------------------------- 4048 We make a couple of remarks about the rounding procedure above. When 4049 b is zero (that is, when the "fractional part" of a is not 1/2 ), we 4050 are (except for clamping) adding the same number to p0 as we are 4051 subtracting from q0. This preserves the average value of p0 and q0 4052 but the resulting difference between p0 and q0 is always even; in 4053 particular, the smallest non-zero gradation +-1 is not possible here. 4055 When b is one, the value we add to p0 (again except for clamping) is 4056 one less than the value we are subtracting from q0. In this case, 4057 the resulting difference is always odd (and the small gradation +-1 4058 is possible) but the average value is reduced by 1/2, yielding, for 4059 instance, a very slight darkening in the luma plane. (In the very 4060 unlikely event of appreciable darkening after a large number of 4061 interframes, a compressor would of course eventually compensate for 4062 this in the selection of predictor and/or residue.) 4064 The derivation of the edge_limit value used above, which depends on 4065 the loop_filter_level and sharpness_level, as well as the type of 4066 edge being processed, will be taken up after we describe the normal 4067 loop filtering algorithm below. 4069 15.3. Normal Filter 4071 The normal loop filter is a refinement of the simple loop filter; all 4072 of the general discussion above applies here as well. In particular, 4073 the functions c, u2s, s2u, abs, and common_adjust are used by both 4074 the normal and simple filters. 4076 As mentioned above, the normal algorithms for inter-macroblock and 4077 inter-subblock edges differ. Nonetheless, they have a great deal in 4078 common: They use similar threshold algorithms to disable the filter 4079 and to detect high internal edge variance (which influences the 4080 filtering algorithm). Both algorithms also use, at least 4081 conditionally, the simple filter adjustment procedure described 4082 above. 4084 The common thresholding algorithms are as follows. 4086 ---- Begin code block -------------------------------------- 4088 /* All functions take (among other things) a segment (of length 4089 at most 4 + 4 = 8) symmetrically straddling an edge. 4091 The pixel values (or pointers) are always given in order, 4092 from the "beforemost" to the "aftermost". So, for a 4093 horizontal edge (written "|"), an 8-pixel segment would be 4094 ordered p3 p2 p1 p0 | q0 q1 q2 q3. */ 4096 /* Filtering is disabled if the difference between any two 4097 adjacent "interior" pixels in the 8-pixel segment exceeds 4098 the relevant threshold (I). A more complex thresholding 4099 calculation is done for the group of four pixels that 4100 straddle the edge, in line with the calculation in 4101 simple_segment() above. */ 4103 int filter_yes( 4104 uint8 I, /* limit on interior differences */ 4105 uint8 E, /* limit at the edge */ 4107 cint8 p3, cint8 p2, cint8 p1, cint8 p0, /* pixels before 4108 edge */ 4109 cint8 q0, cint8 q1, cint8 q2, cint8 q3 /* pixels after 4110 edge */ 4111 ) { 4112 return (abs(p0 - q0)*2 + abs(p1-q1)/2) <= E 4113 && abs(p3 - p2) <= I && abs(p2 - p1) <= I && 4114 abs(p1 - p0) <= I 4115 && abs(q3 - q2) <= I && abs(q2 - q1) <= I && 4116 abs(q1 - q0) <= I; 4117 } 4119 ---- End code block ---------------------------------------- 4120 ---- Begin code block -------------------------------------- 4122 /* Filtering is altered if (at least) one of the differences 4123 on either side of the edge exceeds a threshold (we have 4124 "high edge variance"). */ 4126 int hev( 4127 uint8 threshold, 4128 cint8 p1, cint8 p0, /* pixels before edge */ 4129 cint8 q0, cint8 q1 /* pixels after edge */ 4130 ) { 4131 return abs(p1 - p0) > threshold || abs(q1 - q0) > threshold; 4132 } 4134 ---- End code block ---------------------------------------- 4136 The subblock filter is a variant of the simple filter. In fact, if 4137 we have high edge variance, the adjustment is exactly as for the 4138 simple filter. Otherwise, the simple adjustment (without outer taps) 4139 is applied and the two pixels one step in from the edge pixels are 4140 adjusted by roughly half the amount by which the two edge pixels are 4141 adjusted; since the edge adjustment here is essentially 3/8 the edge 4142 difference, the inner adjustment is approximately 3/16 the edge 4143 difference. 4145 ---- Begin code block -------------------------------------- 4147 void subblock_filter( 4148 uint8 hev_threshold, /* detect high edge variance */ 4149 uint8 interior_limit, /* possibly disable filter */ 4150 uint8 edge_limit, 4151 cint8 *P3, cint8 *P2, int8 *P1, int8 *P0, /* pixels before 4152 edge */ 4153 int8 *Q0, int8 *Q1, cint8 *Q2, cint8 *Q3 /* pixels after 4154 edge */ 4155 ) { 4156 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4157 p0 = u2s(*P0); 4158 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4159 q3 = u2s(*Q3); 4161 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4162 p0, p1, p2, p3)) 4163 { 4164 const int hv = hev( hev_threshold, p1, p0, q0, q1); 4166 cint8 a = ( common_adjust( hv, P1, P0, Q0, Q1) + 1) >> 1; 4168 if( !hv) { 4169 *Q1 = s2u( q1 - a); 4170 *P1 = s2u( p1 + a); 4171 } 4172 } 4173 } 4175 ---- End code block ---------------------------------------- 4177 The inter-macroblock filter has potentially wider scope. If the edge 4178 variance is high, it performs the simple adjustment (using the outer 4179 taps, just like the simple filter and the corresponding case of the 4180 normal subblock filter). If the edge variance is low, we begin with 4181 the same basic filter calculation and apply multiples of it to pixel 4182 pairs symmetric about the edge; the magnitude of adjustment decays as 4183 we move away from the edge and six of the pixels in the segment are 4184 affected. 4186 ---- Begin code block -------------------------------------- 4188 void MBfilter( 4189 uint8 hev_threshold, /* detect high edge variance */ 4190 uint8 interior_limit, /* possibly disable filter */ 4191 uint8 edge_limit, 4192 cint8 *P3, int8 *P2, int8 *P1, int8 *P0, /* pixels before 4193 edge */ 4194 int8 *Q0, int8 *Q1, int8 *Q2, cint8 *Q3 /* pixels after 4195 edge */ 4196 ) { 4197 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4198 p0 = u2s(*P0); 4199 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4200 q3 = u2s(*Q3); 4202 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4203 p0, p1, p2, p3)) 4204 { 4205 if( !hev( hev_threshold, p1, p0, q0, q1)) 4206 { 4207 /* Same as the initial calculation in "common_adjust", 4208 w is something like twice the edge difference */ 4210 const int8 w = c( c(p1 - q1) + 3*(q0 - p0) ); 4212 /* 9/64 is approximately 9/63 = 1/7 and 1<<7 = 128 = 4213 2*64. So this a, used to adjust the pixels adjacent 4214 to the edge, is something like 3/7 the edge 4215 difference. */ 4217 int8 a = c( (27*w + 63) >> 7); 4219 *Q0 = s2u( q0 - a); *P0 = s2u( p0 + a); 4221 /* Next two are adjusted by 2/7 the edge difference */ 4223 a = c( (18*w + 63) >> 7); 4225 *Q1 = s2u( q1 - a); *P1 = s2u( p1 + a); 4227 /* Last two are adjusted by 1/7 the edge difference */ 4229 a = c( (9*w + 63) >> 7); 4231 *Q2 = s2u( q2 - a); *P2 = s2u( p2 + a); 4233 } else /* if hev, do simple filter */ 4234 common_adjust( 1, P1, P0, Q0, Q1); /* using outer 4235 taps */ 4236 } 4238 } 4240 ---- End code block ---------------------------------------- 4242 15.4. Calculation of Control Parameters 4244 We conclude the discussion of loop filtering by showing how the 4245 thresholds supplied to the procedures above are derived from the two 4246 control parameters sharpness_level (an unsigned 3-bit number having 4247 maximum value 7) and loop_filter_level (an unsigned 6-bit number 4248 having maximum value 63). 4250 While the sharpness_level is constant over the frame, individual 4251 macroblocks may override the loop_filter_level with one of four 4252 possibilities supplied in the frame header (as described in Chapter 4253 10). 4255 Both the simple and normal filters disable filtering if a value 4256 derived from the four pixels that straddle the edge (2 either side) 4257 exceeds a threshold / limit value. 4259 ---- Begin code block -------------------------------------- 4261 /* Luma and Chroma use the same inter-macroblock edge limit */ 4262 uint8 mbedge_limit = ((loop_filter_level + 2) * 2) + 4263 interior_limit; 4265 /* Luma and Chroma use the same inter-subblock edge limit */ 4266 uint8 sub_bedge_limit = (loop_filter_level * 2) + interior_limit; 4268 ---- End code block ---------------------------------------- 4270 The remaining thresholds are used only by the normal filters. The 4271 filter-disabling interior difference limit is the same for all edges 4272 (luma, chroma, inter-subblock, inter-macroblock) and is given by the 4273 following. 4275 ---- Begin code block -------------------------------------- 4277 uint8 interior_limit = loop_filter_level; 4279 if( sharpness_level) 4280 { 4281 interior_limit >>= sharpness_level > 4 ? 2 : 1; 4282 if( interior_limit > 9 - sharpness_level) 4283 interior_limit = 9 - sharpness_level; 4284 } 4285 if( !interior_limit) 4286 interior_limit = 1; 4288 ---- End code block ---------------------------------------- 4289 Finally, we give the derivation of the high edge-variance threshold, 4290 which is also the same for all edge types. 4292 ---- Begin code block -------------------------------------- 4294 uint8 hev_threshold = 0; 4296 if( we_are_decoding_akey_frame) /* current frame is a key frame */ 4297 { 4298 if( loop_filter_level >= 40) 4299 hev_threshold = 2; 4300 else if( loop_filter_level >= 15) 4301 hev_threshold = 1; 4302 } 4303 else /* current frame is an interframe */ 4304 { 4305 if( loop_filter_level >= 40) 4306 hev_threshold = 3; 4307 else if( loop_filter_level >= 20) 4308 hev_threshold = 2; 4309 else if( loop_filter_level >= 15) 4310 hev_threshold = 1; 4311 } 4313 ---- End code block ---------------------------------------- 4315 16. Interframe Macroblock Prediction Records 4317 We describe the layout and semantics of the prediction records for 4318 macroblocks in an interframe. 4320 After the feature specification (which is described in Chapter 10 and 4321 is identical for intraframes and interframes), there comes a Bool( 4322 prob_intra), which indicates inter-prediction (i.e., prediction from 4323 prior frames) when true and intra-prediction (i.e., prediction from 4324 already-coded portions of the current frame) when false. The zero- 4325 probability prob_intra is set by field J of the frame header. 4327 16.1. Intra-Predicted Macroblocks 4329 For intra-prediction, the layout of the prediction data is 4330 essentially the same as the layout for key frames, although the 4331 contexts used by the decoding process are slightly different. 4333 As discussed in Chapter 8, the "outer" Y mode here uses a different 4334 tree from that used in key frames, repeated here for convenience. 4336 ---- Begin code block -------------------------------------- 4338 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 4339 { 4340 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 4341 4, 6, /* "1" subtree has 2 descendant subtrees */ 4342 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 4343 H_PRED = "101" */ 4344 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 4345 B_PRED = "111" */ 4346 }; 4348 ---- End code block ---------------------------------------- 4350 The probability table used to decode this tree is variable. As 4351 described in Section 9, it (along with the similarly-treated UV 4352 table) can be updated by field J of the frame header. Similar to the 4353 coefficient-decoding probabilities, such updates are cumulative and 4354 affect all ensuing frames until the next key frame or explicit 4355 update. The default probabilities for the Y and UV tables are 4357 ---- Begin code block -------------------------------------- 4359 Prob ymode_prob [num_ymodes - 1] = { 112, 86, 140, 37}; 4360 Prob uv_mode_prob [num_uv_modes - 1] = { 162, 101, 204}; 4362 ---- End code block ---------------------------------------- 4363 These defaults must be restored after detection of a key frame. 4365 Just as for key frames, if the Y mode is B_PRED, there next comes an 4366 encoding of the intra_bpred mode used by each of the sixteen Y 4367 subblocks. These encodings use the same tree as does that for key 4368 frames but, in place of the contexts used in key frames, use the 4369 single fixed probability table 4371 ---- Begin code block -------------------------------------- 4373 const Prob bmode_prob [num_intra_bmodes - 1] = { 4374 120, 90, 79, 133, 87, 85, 80, 111, 151 4375 }; 4377 ---- End code block ---------------------------------------- 4379 Last comes the chroma mode, again coded using the same tree as that 4380 for key frames, this time using the dynamic uv_mode_prob table 4381 described above. 4383 The calculation of the intra-prediction buffer is identical to that 4384 described for key frames in Chapter 12. 4386 16.2. Inter-Predicted Macroblocks 4388 Otherwise (when the above bool is true), we are using inter- 4389 prediction (which of course only happens for interframes), to which 4390 we now restrict our attention. 4392 The next datum is then another bool, B( prob_last), selecting the 4393 reference frame. If 0, the reference frame is previous frame (last 4394 frame); if 1, another bool (prob_gf) selects the reference frame 4395 between golden frame (0) or altref frame (1). The probabilities 4396 prob_last and prob_gf are set in field J of the frame header. 4398 Together with setting the reference frame, the purpose of inter-mode 4399 decoding is to set a motion vector for each of the sixteen Y 4400 subblocks of the current macroblock. This then defines the 4401 calculation of the inter-prediction buffer (detailed in Chapter 18). 4402 While the net effect of inter-mode decoding is straightforward, the 4403 implementation is somewhat complex; the (lossless) compression 4404 achieved by this method justifies the complexity. 4406 After the reference frame selector comes the mode (or motion vector 4407 reference) applied to the macroblock as a whole, coded using the 4408 following enumeration and tree. Setting mv_nearest = num_ymodes is a 4409 convenience that allows a single variable to unambiguously hold an 4410 inter- or intraprediction mode. 4412 ---- Begin code block -------------------------------------- 4414 typedef enum 4415 { 4416 mv_nearest = num_ymodes, /* use "nearest" motion vector 4417 for entire MB */ 4418 mv_near, /* use "next nearest" "" */ 4419 mv_zero, /* use zero "" */ 4420 mv_new, /* use explicit offset from 4421 implicit "" */ 4422 mv_split, /* use multiple motion vectors */ 4424 num_mv_refs = mv_split + 1 - mv_nearest 4425 } 4426 mv_ref; 4428 const tree_index mv_ref_tree [2 * (num_mv_refs - 1)] = 4429 { 4430 -mv_zero, 2, /* zero = "0" */ 4431 -mv_nearest, 4, /* nearest = "10" */ 4432 -mv_near, 6, /* near = "110" */ 4433 -mv_new, -mv_split /* new = "1110", split = "1111" */ 4434 }; 4436 ---- End code block ---------------------------------------- 4438 16.3. Mode and Motion Vector Contexts 4440 The probability table used to decode the mv_ref, along with three 4441 reference motion vectors used by the selected mode, is calculated via 4442 a survey of the already-decoded motion vectors in (up to) 3 nearby 4443 macroblocks. 4445 The algorithm generates a sorted list of distinct motion vectors 4446 adjacent to the search site. The best_mv is the vector with the 4447 highest score. The nearest_mv is the non-zero vector with the 4448 highest score. The near_mv is the non-zero vector with the next 4449 highest score. The number of motion vectors coded using the SPLITMV 4450 mode is scored using the same weighting and is returned with the 4451 scores of the best, nearest, and near vectors. 4453 The three adjacent macroblocks above, left, and above-left are 4454 considered in order. If the macroblock is intra-coded, no action is 4455 taken. Otherwise, the motion vector is compared to other previously 4456 found motion vectors to determine if it has been seen before, and if 4457 so contributes its weight to that vector, otherwise enters a new 4458 vector in the list. The above and left vectors have twice the weight 4459 of the above-left vector. 4461 As is the case with many contexts used by VP8, it is possible for 4462 macroblocks near the top or left edges of the image to reference 4463 blocks that are outside the visible image. VP8 provides a border of 4464 1 macroblock filled with 0x0 motion vectors left of the left edge, 4465 and a border filled with 0,0 motion vectors of 1 macroblocks above 4466 the top edge. 4468 Much of the process is more easily described in C than in English. 4469 The reference code for this can be found in findnearmv.c. The 4470 calculation of reference vectors, probability table, and, finally, 4471 the inter-prediction mode itself is implemented as follows. 4473 ---- Begin code block -------------------------------------- 4475 typedef union 4476 { 4477 unsigned int as_int; 4478 MV as_mv; 4479 } int_mv; /* facilitates rapid equality tests */ 4481 static void mv_bias(MODE_INFO *x,int refframe, int_mv *mvp, 4482 int * ref_frame_sign_bias ) 4483 { 4484 MV xmv; 4485 xmv = x->mbmi.mv.as_mv; 4486 if ( ref_frame_sign_bias[x->mbmi.ref_frame] != 4487 ref_frame_sign_bias[refframe] ) 4488 { 4489 xmv.row*=-1; 4490 xmv.col*=-1; 4491 } 4492 mvp->as_mv = xmv; 4493 } 4495 ---- End code block ---------------------------------------- 4496 ---- Begin code block -------------------------------------- 4498 void vp8_clamp_mv(MV *mv, const MACROBLOCKD *xd) 4499 { 4500 if ( mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN) ) 4501 mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN; 4502 else if ( mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN ) 4503 mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN; 4505 if ( mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN) ) 4506 mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN; 4507 else if ( mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN ) 4508 mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN; 4509 } 4511 ---- End code block ---------------------------------------- 4513 In the function vp8_find_near_mvs(), the vectors "nearest" and "near" 4514 are used by the corresponding modes. 4516 The vector best_mv is used as a base for explicitly-coded motion 4517 vectors. 4519 The first three entries in the return value cnt are (in order) 4520 weighted census values for "zero", "nearest", and "near" vectors. 4521 The final value indicates the extent to which SPLIT_MV was used by 4522 the neighboring macroblocks. The largest possible "weight" value in 4523 each case is 5. 4525 ---- Begin code block -------------------------------------- 4527 void vp8_find_near_mvs 4528 ( 4529 MACROBLOCKD *xd, 4530 const MODE_INFO *here, 4531 MV *nearest, 4532 MV *near, 4533 MV *best_mv, 4534 int cnt[4], 4535 int refframe, 4536 int * ref_frame_sign_bias 4537 ) 4538 { 4539 const MODE_INFO *above = here - xd->mode_info_stride; 4540 const MODE_INFO *left = here - 1; 4541 const MODE_INFO *aboveleft = above - 1; 4542 int_mv near_mvs[4]; 4543 int_mv *mv = near_mvs; 4544 int *cntx = cnt; 4545 enum {CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV}; 4547 /* Zero accumulators */ 4548 mv[0].as_int = mv[1].as_int = mv[2].as_int = 0; 4549 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 4551 /* Process above */ 4552 if(above->mbmi.ref_frame != INTRA_FRAME) { 4553 if(above->mbmi.mv.as_int) { 4554 (++mv)->as_int = above->mbmi.mv.as_int; 4555 mv_bias(above, refframe, mv, ref_frame_sign_bias); 4556 ++cntx; 4557 } 4558 *cntx += 2; 4559 } 4561 /* Process left */ 4562 if(left->mbmi.ref_frame != INTRA_FRAME) { 4563 if(left->mbmi.mv.as_int) { 4564 int_mv this_mv; 4566 this_mv.as_int = left->mbmi.mv.as_int; 4567 mv_bias(left, refframe, &this_mv, ref_frame_sign_bias); 4569 if(this_mv.as_int != mv->as_int) { 4570 (++mv)->as_int = this_mv.as_int; 4571 ++cntx; 4572 } 4573 *cntx += 2; 4574 } else 4575 cnt[CNT_ZERO] += 2; 4576 } 4578 /* Process above left */ 4579 if(aboveleft->mbmi.ref_frame != INTRA_FRAME) { 4580 if(aboveleft->mbmi.mv.as_int) { 4581 int_mv this_mv; 4583 this_mv.as_int = aboveleft->mbmi.mv.as_int; 4584 mv_bias(aboveleft, refframe, &this_mv, 4585 ref_frame_sign_bias); 4587 if(this_mv.as_int != mv->as_int) { 4588 (++mv)->as_int = this_mv.as_int; 4589 ++cntx; 4590 } 4591 *cntx += 1; 4593 } else 4594 cnt[CNT_ZERO] += 1; 4595 } 4597 /* If we have three distinct MV's ... */ 4598 if(cnt[CNT_SPLITMV]) { 4599 /* See if above-left MV can be merged with NEAREST */ 4600 if(mv->as_int == near_mvs[CNT_NEAREST].as_int) 4601 cnt[CNT_NEAREST] += 1; 4602 } 4604 cnt[CNT_SPLITMV] = ((above->mbmi.mode == SPLITMV) 4605 + (left->mbmi.mode == SPLITMV)) * 2 4606 + (aboveleft->mbmi.mode == SPLITMV); 4608 /* Swap near and nearest if necessary */ 4609 if(cnt[CNT_NEAR] > cnt[CNT_NEAREST]) { 4610 int tmp; 4611 tmp = cnt[CNT_NEAREST]; 4612 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 4613 cnt[CNT_NEAR] = tmp; 4614 tmp = near_mvs[CNT_NEAREST].as_int; 4615 near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int; 4616 near_mvs[CNT_NEAR].as_int = tmp; 4617 } 4619 /* Use near_mvs[0] to store the "best" MV */ 4620 if(cnt[CNT_NEAREST] >= cnt[CNT_ZERO]) 4621 near_mvs[CNT_ZERO] = near_mvs[CNT_NEAREST]; 4623 /* Set up return values */ 4624 *best_mv = near_mvs[0].as_mv; 4625 *nearest = near_mvs[CNT_NEAREST].as_mv; 4626 *near = near_mvs[CNT_NEAR].as_mv; 4628 vp8_clamp_mv(nearest, xd); 4629 vp8_clamp_mv(near, xd); 4630 vp8_clamp_mv(best_mv, xd); //TODO: move this up before 4631 the copy 4632 } 4634 ---- End code block ---------------------------------------- 4636 The mv_ref probability table (mv_ref_p) is then derived from the 4637 census as follows. 4639 ---- Begin code block -------------------------------------- 4641 const int vp8_mode_contexts[6][4] = 4642 { 4643 { 7, 1, 1, 143, }, 4644 { 14, 18, 14, 107, }, 4645 { 135, 64, 57, 68, }, 4646 { 60, 56, 128, 65, }, 4647 { 159, 134, 128, 34, }, 4648 { 234, 188, 128, 28, }, 4649 } 4651 ---- End code block ---------------------------------------- 4653 ---- Begin code block -------------------------------------- 4655 vp8_prob *vp8_mv_ref_probs(vp8_prob mv_ref_p[VP8_MVREFS-1], 4656 int cnt[4]) 4657 { 4658 mv_ref_p[0] = vp8_mode_contexts [cnt[0]] [0]; 4659 mv_ref_p[1] = vp8_mode_contexts [cnt[1]] [1]; 4660 mv_ref_p[2] = vp8_mode_contexts [cnt[2]] [2]; 4661 mv_ref_p[3] = vp8_mode_contexts [cnt[3]] [3]; 4662 return p; 4663 } 4665 ---- End code block ---------------------------------------- 4667 Once mv_ref_p is established, the mv_ref is decoded as usual. 4669 ---- Begin code block -------------------------------------- 4671 mvr = (mv_ref) treed_read( d, mv_ref_tree, mv_ref_p); 4673 ---- End code block ---------------------------------------- 4675 For the first four inter-coding modes, the same motion vector is used 4676 for all the Y subblocks. The first three modes use an implicit 4677 motion vector. 4679 +------------+------------------------------------------------------+ 4680 | Mode | Instruction | 4681 +------------+------------------------------------------------------+ 4682 | mv_nearest | Use the nearest vector returned by | 4683 | | vp8_find_near_mvs. | 4684 | | | 4685 | mv_near | Use the near vector returned by vp8_find_near_mvs. | 4686 | | | 4687 | mv_zero | Use a zero vector, that is, predict the current | 4688 | | macroblock from the corresponding macroblock in the | 4689 | | prediction frame. | 4690 | | | 4691 | NEWMV | This mode is followed by an explicitly-coded motion | 4692 | | vector (the format of which is described in the next | 4693 | | chapter) that is added (component-wise) to the | 4694 | | best_mv reference vector returned by find_near_mvs | 4695 | | and applied to all 16 subblocks. | 4696 +------------+------------------------------------------------------+ 4698 16.4. Split Prediction 4700 The remaining mode (SPLITMV) causes multiple vectors to be applied to 4701 the Y subblocks. It is immediately followed by a partition 4702 specification that determines how many vectors will be specified and 4703 how they will be assigned to the subblocks. The possible partitions, 4704 with indicated subdivisions and coding tree, are as follows. 4706 ---- Begin code block -------------------------------------- 4708 typedef enum 4709 { 4710 mv_top_bottom, /* two pieces {0...7} and {8...15} */ 4711 mv_left_right, /* {0,1,4,5,8,9,12,13} and 4712 {2,3,6,7,10,11,14,15} */ 4713 mv_quarters, /* {0,1,4,5}, {2,3,6,7}, {8,9,12,13}, 4714 {10,11,14,15} */ 4715 MV_16, /* every subblock gets its own vector 4716 {0} ... {15} */ 4718 mv_num_partitions 4719 } 4720 MVpartition; 4722 const tree_index mvpartition_tree [2 * (mvnum_partition - 1)] = 4723 { 4724 -MV_16, 2, /* MV_16 = "0" */ 4725 -mv_quarters, 4, /* mv_quarters = "10" */ 4726 -mv_top_bottom, -mv_left_right /* top_bottom = "110", 4727 left_right = "111" */ 4728 }; 4730 ---- End code block ---------------------------------------- 4732 The partition is decoded using a fixed, constant probability table: 4734 ---- Begin code block -------------------------------------- 4736 const Prob mvpartition_probs [mvnum_partition - 1] = 4737 { 110, 111, 150}; 4738 part = (MVpartition) treed_read( d, mvpartition_tree, 4739 mvpartition_probs); 4741 ---- End code block ---------------------------------------- 4743 After the partition come two (for mv_top_bottom or mv_left_right), 4744 four (for mv_quarters), or sixteen (for MV_16) subblock inter- 4745 prediction modes. These modes occur in the order indicated by the 4746 partition layouts (given as comments to the MVpartition enum) and are 4747 coded as follows. (As was done for the macroblock-level modes, we 4748 offset the mode enumeration so that a single variable may 4749 unambiguously hold either an intra- or inter-subblock mode.) 4751 Prior to decoding each subblock, a decoding tree context is chosen as 4752 illustrated in the code snippet below. The context is based on the 4753 immediate left and above subblock neighbors, and whether they are 4754 equal, are zero, or a combination of those. 4756 ---- Begin code block -------------------------------------- 4758 typedef enum 4759 { 4760 LEFT4x4 = num_intra_bmodes, /* use already-coded MV to 4761 my left */ 4762 ABOVE4x4, /* use already-coded MV above me */ 4763 ZERO4x4, /* use zero MV */ 4764 NEW4x4, /* explicit offset from "best" */ 4766 num_sub_mv_ref 4767 }; 4768 sub_mv_ref; 4770 const tree_index sub_mv_ref_tree [2 * (num_sub_mv_ref - 1)] = 4771 { 4772 -LEFT4X4, 2, /* LEFT = "0" */ 4773 -ABOVE4X4, 4, /* ABOVE = "10" */ 4774 -ZERO4X4, -NEW4X4 /* ZERO = "110", NEW = "111" */ 4775 }; 4777 /* Choose correct decoding tree context 4778 * Function parameters are left subblock neighbor MV and above 4779 * subblock neighbor MV */ 4780 int vp8_mvCont(MV *l, MV*a) 4781 { 4782 int lez = (l->row == 0 && l->col == 0); /* left neighbour 4783 is zero */ 4784 int aez = (a->row == 0 && a->col == 0); /* above neighbour 4785 is zero */ 4786 int lea = (l->row == a->row && l->col == a->col); /* left 4787 neighbour equals above neighbour */ 4789 if(lea && lez) 4790 return SUBMVREF_LEFT_ABOVE_ZED; /* =4 */ 4792 if(lea) 4793 return SUBMVREF_LEFT_ABOVE_SAME; /* =3 */ 4795 if(aez) 4796 return SUBMVREF_ABOVE_ZED; /* =2 */ 4798 if(lez) 4799 return SUBMVREF_LEFT_ZED; /* =1*/ 4801 return SUBMVREF_NORMAL; /* =0 */ 4803 } 4805 /* Constant probabilities and decoding procedure. */ 4807 const Prob sub_mv_ref_prob [5][num_sub_mv_ref - 1] = { 4808 { 147,136,18 }, 4809 { 106,145,1 }, 4810 { 179,121,1 }, 4811 { 223,1 ,34 }, 4812 { 208,1 ,1 } }; 4814 sub_ref = (sub_mv_ref) treed_read( d, sub_mv_ref_tree, 4815 sub_mv_ref_prob[context]); 4817 ---- End code block ---------------------------------------- 4819 The first two sub-prediction modes simply copy the already-coded 4820 motion vectors used by the blocks above and to-the-left of the 4821 subblock at the upper left corner of the current subset (i.e., 4822 collection of subblocks being predicted). These prediction blocks 4823 need not lie in the current macroblock and, if the current subset 4824 lies at the top or left edges of the frame, need not lie in the 4825 frame. In this latter case, their motion vectors are taken to be 4826 zero, as are subblock motion vectors within an intra-predicted 4827 macroblock. Also, to ensure the correctness of prediction within 4828 this macroblock, all subblocks lying in an already-decoded subset of 4829 the current macroblock must have their motion vectors set. 4831 ZERO4x4 uses a zero motion vector and predicts the current subset 4832 using the corresponding subset from the prediction frame. 4834 NEW4x4 is exactly like NEWMV except applied only to the current 4835 subset. It is followed by a 2-dimensional motion vector offset 4836 (described in the next chapter) that is added to the best vector 4837 returned by the earlier call to find_near_mvs to form the motion 4838 vector in effect for the subset. 4840 Parsing of both inter-prediction modes and motion vectors (described 4841 next) can be found in the reference decoder file decodemv.c. 4843 17. Motion Vector Decoding 4845 As discussed above, motion vectors appear in two places in the VP8 4846 datastream: applied to whole macroblocks in NEWMV mode and applied to 4847 subsets of macroblocks in NEW4x4 mode. The format of the vectors is 4848 identical in both cases. 4850 Each vector has two pieces: A vertical component (row) followed by a 4851 horizontal component (column). The row and column use separate 4852 coding probabilities but are otherwise represented identically. 4854 17.1. Coding of Each Component 4856 Each component is a signed integer V representing a vertical or 4857 horizontal luma displacement of V quarter-pixels (and a chroma 4858 displacement of V eighth-pixels). The absolute value of V, if non- 4859 zero, is followed by a boolean sign. V may take any value between 4860 -1023 and +1023, inclusive. 4862 The absolute value A is coded in one of two different ways according 4863 to its size. For 0 <= A <= 7, A is tree-coded, and for 8 <= A <= 4864 1023, the bits in the binary expansion of A are coded using 4865 independent boolean probabilities. The coding of A begins with a 4866 bool specifying which range is in effect. 4868 Decoding a motion vector component then requires a 19-position 4869 probability table, whose offsets, along with the procedure used to 4870 decode components, are as follows: 4872 ---- Begin code block -------------------------------------- 4874 typedef enum 4875 { 4876 mvpis_short, /* short (<= 7) vs long (>= 8) */ 4877 MVPsign, /* sign for non-zero */ 4878 MVPshort, /* 8 short values = 7-position tree */ 4880 MVPbits = MVPshort + 7, /* 8 long value bits 4881 w/independent probs */ 4883 MVPcount = MVPbits + 10 /* 19 probabilities in total */ 4884 } 4885 MVPindices; 4887 typedef Prob MV_CONTEXT [MVPcount]; /* Decoding spec for 4888 a single component */ 4890 /* Tree used for small absolute values (has expected 4891 correspondence). */ 4893 const tree_index small_mvtree [2 * (8 - 1)] = 4894 { 4895 2, 8, /* "0" subtree, "1" subtree */ 4896 4, 6, /* "00" subtree", "01" subtree */ 4897 -0, -1, /* 0 = "000", 1 = "001" */ 4898 -2, -3, /* 2 = "010", 3 = "011" */ 4899 10, 12, /* "10" subtree, "11" subtree */ 4900 -4, -5, /* 4 = "100", 5 = "101" */ 4901 -6, -7 /* 6 = "110", 7 = "111" */ 4902 }; 4904 /* Read MV component at current decoder position, using 4905 supplied probs. */ 4907 int read_mvcomponent( bool_decoder *d, const MV_CONTEXT *mvc) 4908 { 4909 const Prob * const p = (const Prob *) mvc; 4910 int A = 0; 4912 if( read_bool( d, p [mvpis_short])) /* 8 <= A <= 1023 */ 4913 { 4914 /* Read bits 0, 1, 2 */ 4916 int i = 0; 4917 do { A += read_bool( d, p [MVPbits + i]) << i;} 4918 while( ++i < 3); 4920 /* Read bits 9, 8, 7, 6, 5, 4 */ 4922 i = 9; 4923 do { A += read_bool( d, p [MVPbits + i]) << i;} 4924 while( --i > 3); 4926 /* We know that A >= 8 because it is coded long, 4927 so if A <= 15, bit 3 is one and is not 4928 explicitly coded. */ 4930 if( !(A & 0xfff0) || read_bool( d, p [MVPbits + 3])) 4931 A += 8; 4932 } 4933 else /* 0 <= A <= 7 */ 4934 A = treed_read( d, small_mvtree, p + MVPshort); 4936 return A && read_bool( r, p [MVPsign]) ? -A : A; 4937 } 4938 ---- End code block ---------------------------------------- 4940 17.2. Probability Updates 4942 The decoder should maintain an array of two MV_CONTEXTs for decoding 4943 row and column components, respectively. These MV_CONTEXTs should be 4944 set to their defaults every key frame. Each individual probability 4945 may be updated every interframe (by field J of the frame header) 4946 using a constant table of update probabilities. Each optional update 4947 is of the form B? P(7), that is, a bool followed by a 7-bit 4948 probability specification if true. 4950 As with other dynamic probabilities used by VP8, the updates remain 4951 in effect until the next key frame or until replaced via another 4952 update. 4954 In detail, the probabilities should then be managed as follows. 4956 ---- Begin code block -------------------------------------- 4958 /* Never-changing table of update probabilities for each 4959 individual probability used in decoding motion vectors. */ 4961 const MV_CONTEXT vp8_mv_update_probs[2] = 4962 { 4963 { 4964 237, 4965 246, 4966 253, 253, 254, 254, 254, 254, 254, 4967 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 4968 }, 4969 { 4970 231, 4971 243, 4972 245, 253, 254, 254, 254, 254, 254, 4973 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 4974 } 4975 }; 4977 /* Default MV decoding probabilities. */ 4979 const MV_CONTEXT default_mv_context[2] = 4980 { 4981 { // row 4982 162, // is short 4983 128, // sign 4984 225, 146, 172, 147, 214, 39, 156, // short tree 4985 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 // long bits 4987 }, 4989 { // same for column 4990 164, // is short 4991 128, 4992 204, 170, 119, 235, 140, 230, 228, 4993 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 // long bits 4995 } 4996 }; 4998 /* Current MV decoding probabilities, set to above defaults 4999 every key frame. */ 5001 MV_CONTEXT mvc [2]; /* always row, then column */ 5003 /* Procedure for decoding a complete motion vector. */ 5005 typedef struct { int16 row, col;} MV; /* as in previous chapter */ 5007 MV read_mv( bool_decoder *d) 5008 { 5009 MV v; 5010 v.row = (int16) read_mvcomponent( d, mvc); 5011 v.col = (int16) read_mvcomponent( d, mvc + 1); 5012 return v; 5013 } 5015 /* Procedure for updating MV decoding probabilities, called 5016 every interframe with "d" at the appropriate position in 5017 the frame header. */ 5019 void update_mvcontexts( bool_decoder *d) 5020 { 5021 int i = 0; 5022 do { /* component = row, then column */ 5023 const Prob *up = mv_update_probs[i]; /* update probs 5024 for component */ 5025 Prob *p = mvc[i]; /* start decode tbl "" */ 5026 Prob * const pstop = p + MVPcount; /* end decode tbl "" */ 5027 do { 5028 if( read_bool( d, *up++)) /* update this position */ 5029 { 5030 const Prob x = read_literal( d, 7); 5032 *p = x? x<<1 : 1; 5033 } 5034 } while( ++p < pstop); /* next position */ 5036 } while( ++i < 2); /* next component */ 5037 } 5039 ---- End code block ---------------------------------------- 5041 This completes the description of the motion-vector decoding 5042 procedure and, with it, the procedure for decoding interframe 5043 macroblock prediction records. 5045 18. Interframe Prediction 5047 Given an inter-prediction specification for the current macroblock, 5048 that is, a reference frame together with a motion vector for each of 5049 the sixteen Y subblocks, we describe the calculation of the 5050 prediction buffer for the macroblock. Frame reconstruction is then 5051 completed via the previously-described processes of residue summation 5052 (Section 14) and loop filtering (Section 15). 5054 The management of inter-predicted subblocks may be found in the 5055 reference decoder file reconinter.c; sub-pixel interpolation is 5056 implemented in filter_c.c. 5058 18.1. Bounds on and Adjustment of Motion Vectors 5060 Since each motion vector is differentially encoded from a neighboring 5061 block or macroblock and the only clamp is to ensure that the 5062 referenced motion vector represents a valid location inside a 5063 reference frame buffer, it is technically possible within the VP8 5064 format for a block or macroblock to have arbitrarily large motion 5065 vectors, up to the size of the input image plus the extended border 5066 areas. For practical reasons, VP8 imposes a motion vector size range 5067 limit of [-4096, 4095] full pixels, regardless of image size (VP8 5068 defines 14 raw bits for width and height; 16383x16383 is the maximum 5069 possible image size). Bitstream-compliant encoders and decoders 5070 shall enforce this limit. 5072 Because the motion vectors applied to the chroma subblocks have 1/8 5073 pixel resolution, the synthetic pixel calculation, outlined in 5074 Chapter 5 and detailed below, uses this resolution for the luma 5075 subblocks as well. In accordance, the stored luma motion vectors are 5076 all doubled, each component of each luma vector becoming an even 5077 integer in the range -2046 to +2046, inclusive. 5079 The vector applied to each chroma subblock is calculated by averaging 5080 the vectors for the 4 luma subblocks occupying the same visible area 5081 as the chroma subblock in the usual correspondence, that is, the 5082 vector for U and V block 0 is the average of the vectors for the Y 5083 subblocks { 0, 1, 4, 5}, chroma block 1 corresponds to Y blocks { 2, 5084 3, 6, 7}, chroma block 2 to Y blocks { 8, 9, 12, 13}, and chroma 5085 block 3 to Y blocks { 10, 11, 14, 15}. 5087 In detail, each of the two components of the vectors for each of the 5088 chroma subblocks is calculated from the corresponding luma vector 5089 components as follows: 5091 ---- Begin code block -------------------------------------- 5093 int avg( int c1, int c2, int c3, int c4) 5094 { 5095 int s = c1 + c2 + c3 + c4; 5097 /* The shift divides by 8 (not 4) because chroma pixels 5098 have twice the diameter of luma pixels. The handling 5099 of negative motion vector components is slightly 5100 cumbersome because, strictly speaking, right shifts 5101 of negative numbers are not well-defined in C. */ 5103 return s >= 0 ? (s + 4) >> 3 : -( (-s + 4) >> 3); 5104 } 5106 ---- End code block ---------------------------------------- 5108 Furthermore, if the version number in the frame tag specifies only 5109 full-pel chroma motion vectors, then the fractional parts of both 5110 components of the vector are truncated to zero, as illustrated in the 5111 following pseudo-code (assuming 3 bits of fraction for both luma and 5112 chroma vectors): 5114 ---- Begin code block -------------------------------------- 5116 x = x & (~7); 5117 y = y & (~7); 5119 ---- End code block ---------------------------------------- 5121 Earlier in this document we described the vp8_clamp_mv() function to 5122 limit "nearest" and "near" motion vector predictors inside specified 5123 margins within the frame boundaries. Additional clamping is 5124 performed for NEW_MV macroblocks, for which the final motion vector 5125 is clamped again after combining the "best" predictor and the 5126 differential vector decoded from the stream. 5128 However, the secondary clamping is not performed for SPLIT_MV 5129 macroblocks, meaning that any subblock's motion vector within the 5130 SPLIT_MV macroblock may point outside the clamping zone. These non- 5131 clamped vectors are also used when determining the decoding tree 5132 context for subsequent subblocks' modes in the vp8_mvCont() function. 5134 18.2. Prediction Subblocks 5136 The prediction calculation for each subblock is then as follows. 5137 Temporarily disregarding the fractional part of the motion vector 5138 (that is, rounding "up" or "left" by right-shifting each component 3 5139 bits with sign propagation) and adding the origin (upper left 5140 position) of the (16x16 luma or 8x8 chroma) current macroblock gives 5141 us an origin in the Y, U, or V plane of the predictor frame (either 5142 the golden frame or previous frame). 5144 Considering that origin to be the upper left corner of a (luma or 5145 chroma) macroblock, we need to specify the relative positions of the 5146 pixels associated to that subblock, that is, any pixels that might be 5147 involved in the sub-pixel interpolation processes for the subblock. 5149 18.3. Sub-pixel Interpolation 5151 The sub-pixel interpolation is effected via two one-dimensional 5152 convolutions. These convolutions may be thought of as operating on a 5153 two-dimensional array of pixels whose origin is the subblock origin, 5154 that is the origin of the prediction macroblock described above plus 5155 the offset to the subblock. Because motion vectors are arbitrary, so 5156 are these "prediction subblock origins". 5158 The integer part of the motion vector is subsumed in the origin of 5159 the prediction subblock, the 16 (synthetic) pixels we need to 5160 construct are given by 16 offsets from the origin. The integer part 5161 of each of these offsets is the offset of the corresponding pixel 5162 from the subblock origin (using the vertical stride). To these 5163 integer parts is added a constant fractional part, which is simply 5164 the difference between the actual motion vector and its integer 5165 truncation used to calculate the origins of the prediction macroblock 5166 and subblock. Each component of this fractional part is an integer 5167 between 0 and 7, representing a forward displacement in eighths of a 5168 pixel. 5170 It is these fractional displacements that determine the filtering 5171 process. If they both happen to be zero (that is, we had a "whole 5172 pixel" motion vector), the prediction subblock is simply copied into 5173 the corresponding piece of the current macroblock's prediction 5174 buffer. As discussed in Chapter 14, the layout of the macroblock's 5175 prediction buffer can depend on the specifics of the reconstruction 5176 implementation chosen. Of course, the vertical displacement between 5177 lines of the prediction subblock is given by the stride, as are all 5178 vertical displacements used here. 5180 Otherwise, at least one of the fractional displacements is non-zero. 5181 We then synthesize the missing pixels via a horizontal, followed by a 5182 vertical, one-dimensional interpolation. 5184 The two interpolations are essentially identical. Each uses an (at 5185 most) six-tap filter (the choice of which of course depends on the 5186 one-dimensional offset). Thus, every calculated pixel references at 5187 most three pixels before (above or to-the-left of) it and at most 5188 three pixels after (below or to-the-right of) it. The horizontal 5189 interpolation must calculate two extra rows above and three extra 5190 rows below the 4x4 block, to provide enough samples for the vertical 5191 interpolation to proceed. 5193 Depending on the reconstruction filter type given in the field 5194 Version Number in the frame tag, either a bicubic or a bilinear tap 5195 set is used. 5197 The exact implementation of subsampling is as follows. 5199 ---- Begin code block -------------------------------------- 5201 /* Filter taps taken to 7-bit precision. 5202 Because DC is always passed, taps always sum to 128. */ 5204 const int BilinearFilters[8][6] = 5205 { 5206 { 0, 0, 128, 0, 0, 0 }, 5207 { 0, 0, 112, 16, 0, 0 }, 5208 { 0, 0, 96, 32, 0, 0 }, 5209 { 0, 0, 80, 48, 0, 0 }, 5210 { 0, 0, 64, 64, 0, 0 }, 5211 { 0, 0, 48, 80, 0, 0 }, 5212 { 0, 0, 32, 96, 0, 0 }, 5213 { 0, 0, 16, 112, 0, 0 } 5214 }; 5216 const int filters [8] [6] = { /* indexed by displacement */ 5217 { 0, 0, 128, 0, 0, 0 }, /* degenerate whole-pixel */ 5218 { 0, -6, 123, 12, -1, 0 }, /* 1/8 */ 5219 { 2, -11, 108, 36, -8, 1 }, /* 1/4 */ 5220 { 0, -9, 93, 50, -6, 0 }, /* 3/8 */ 5221 { 3, -16, 77, 77, -16, 3 }, /* 1/2 is symmetric */ 5222 { 0, -6, 50, 93, -9, 0 }, /* 5/8 = reverse of 3/8 */ 5223 { 1, -8, 36, 108, -11, 2 }, /* 3/4 = reverse of 1/4 */ 5224 { 0, -1, 12, 123, -6, 0 } /* 7/8 = reverse of 1/8 */ 5225 }; 5227 /* One-dimensional synthesis of a single sample. 5228 Filter is determined by fractional displacement */ 5230 Pixel interp( 5231 const int fil[6], /* filter to apply */ 5232 const Pixel *p, /* origin (rounded "before") in 5233 prediction area */ 5235 const int s /* size of one forward step "" */ 5236 ) { 5237 int32 a = 0; 5238 int i = 0; 5239 p -= s + s; /* move back two positions */ 5241 do { 5242 a += *p * fil[i]; 5243 p += s; 5244 } while( ++i < 6); 5246 return clamp255( (a + 64) >> 7); /* round to nearest 5247 8-bit value */ 5248 } 5250 /* First do horizontal interpolation, producing intermediate 5251 buffer. */ 5253 void Hinterp( 5254 Pixel temp[9][4], /* 9 rows of 4 (intermediate) 5255 destination values */ 5256 const Pixel *p, /* subblock origin in prediction 5257 frame */ 5258 int s, /* vertical stride to be used in 5259 prediction frame */ 5260 uint hfrac, /* 0 <= horizontal displacement <= 7 */ 5261 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5262 ) { 5263 const int * const fil = bicubic ? filters [hfrac] : 5264 BilinearFilters[hfrac]; 5266 int r = 0; do /* for each row */ 5267 { 5268 int c = 0; do /* for each destination sample */ 5269 { 5270 /* Pixel separation = one horizontal step = 1 */ 5272 temp[r][c] = interp( fil, p + c, 1); 5273 } 5274 while( ++c < 4); 5275 } 5276 while( p += s, ++r < 9); /* advance p to next row */ 5277 } 5279 /* Finish with vertical interpolation, producing final results. 5280 Input array "temp" is of course that computed above. */ 5282 void Vinterp( 5283 Pixel final[4][4], /* 4 rows of 4 (final) destination values */ 5284 const Pixel temp[9][4], 5285 uint vfrac, /* 0 <= vertical displacement <= 7 */ 5286 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5287 ) { 5288 const int * const fil = bicubic ? filters [vfrac] : 5289 BilinearFilters[vfrac]; 5291 int r = 0; do /* for each row */ 5292 { 5293 int c = 0; do /* for each destination sample */ 5294 { 5295 /* Pixel separation = one vertical step = width 5296 of array = 4 */ 5298 final[r][c] = interp( fil, temp[r] + c, 4); 5299 } 5300 while( ++c < 4); 5301 } 5302 while( ++r < 4); 5303 } 5305 ---- End code block ---------------------------------------- 5307 18.4. Filter Properties 5309 We discuss briefly the rationale behind the choice of filters. Our 5310 approach is necessarily cursory; a genuinely accurate discussion 5311 would require a couple of books. Readers unfamiliar with signal 5312 processing may or may not wish to skip this. 5314 All digital signals are of course sampled in some fashion. The case 5315 where the inter-sample spacing (say in time for audio samples, or 5316 space for pixels) is uniform, that is, the same at all positions, is 5317 particularly common and amenable to analysis. Many aspects of the 5318 treatment of such signals are best-understood in the frequency domain 5319 via Fourier Analysis, particularly those aspects of the signal that 5320 are not changed by shifts in position, especially when those 5321 positional shifts are not given by a whole number of samples. 5323 Non-integral translates of a sampled signal are a textbook example of 5324 the foregoing. In our case of non-integral motion vectors, we wish 5325 to say what the underlying image "really is" at these pixels we don't 5326 have values for but feel that it makes sense to talk about. The 5327 correctness of this feeling is predicated on the underlying signal 5328 being band-limited, that is, not containing any energy in spatial 5329 frequencies that cannot be faithfully rendered at the pixel 5330 resolution at our disposal. In one dimension, this range of "OK" 5331 frequencies is called the Nyquist band; in our two-dimensional case 5332 of integer-grid samples, this range might be termed a Nyquist 5333 rectangle. The finer the grid, the more we know about the image, and 5334 the wider the Nyquist rectangle. 5336 It turns out that, for such band-limited signals, there is indeed an 5337 exact mathematical formula to produce the correct sample value at an 5338 arbitrary point. Unfortunately, this calculation requires the 5339 consideration of every single sample in the image, as well as needing 5340 to operate at infinite precision. Also, strictly speaking, all band- 5341 limited signals have infinite spatial (or temporal) extent, so 5342 everything we are discussing is really some sort of approximation. 5344 It is true that the theoretically correct subsampling procedure, as 5345 well as any approximation thereof, is always given by a translation- 5346 invariant weighted sum (or filter) similar to that used by VP8. It 5347 is also true that the reconstruction error made by such a filter can 5348 be simply represented as a multiplier in the frequency domain, that 5349 is, such filters simply multiply the Fourier transform of any signal 5350 to which they are applied by a fixed function associated to the 5351 filter. This fixed function is usually called the frequency response 5352 (or transfer function); the ideal subsampling filter has a frequency 5353 response equal to one in the Nyquist rectangle and zero everywhere 5354 else. 5356 Another basic fact about approximations to "truly correct" 5357 subsampling is that, the wider the subrectangle (within the Nyquist 5358 rectangle) of spatial frequencies one wishes to "pass" (that is, 5359 correctly render) or, put more accurately, the closer one wishes to 5360 approximate the ideal transfer function, the more samples of the 5361 original signal must be considered by the subsampling, and the wider 5362 the calculation precision necessitated. 5364 The filters chosen by VP8 were chosen, within the constraints of 4 or 5365 6 taps and 7-bit precision, to do the best possible job of handling 5366 the low spatial frequencies near the zeroth DC frequency along with 5367 introducing no resonances (places where the absolute value of the 5368 frequency response exceeds one). 5370 The justification for the foregoing has two parts. First, resonances 5371 can produce extremely objectionable visible artifacts when, as often 5372 happens in actual compressed video streams, filters are applied 5373 repeatedly. Second, the vast majority of energy in real-world images 5374 lies near DC and not at the high-end. 5376 To get slightly more specific, the filters chosen by VP8 are the best 5377 resonance-free 4- or 6-tap filters possible, where "best" describes 5378 the frequency response near the origin: the response at 0 is required 5379 to be 1 and the graph of the response at 0 is as flat as possible. 5381 To provide an intuitively more obvious point of reference, the "best" 5382 2-tap filter is given by simple linear interpolation between the 5383 surrounding actual pixels. 5385 Finally, it should be noted that, because of the way motion vectors 5386 are calculated, the (shorter) 4-tap filters (used for odd fractional 5387 displacements) are applied in the chroma plane only. Human color 5388 perception is notoriously poor, especially where higher spatial 5389 frequencies are involved. The shorter filters are easier to 5390 understand mathematically, and the difference between them and a 5391 theoretically slightly better 6-tap filter is negligible where chroma 5392 is concerned. 5394 19. Annex A: Bitstream Syntax 5396 This annex presents the bitstream syntax in a tabular form. All the 5397 information elements have been introduced and explained in the 5398 previous chapters but are collected here for a quick reference. Each 5399 syntax element is shortly described after the tabular representation 5400 along with a reference to the corresponding paragraph in the main 5401 document. The meaning of each syntax element value is not repeated 5402 here. 5404 The top-level hierarchy of the bitstream is introduced in Section 4. 5406 Definition of syntax element coding types can be found in Section 8. 5407 The types used in the representation in this annex are: 5409 o f(n), n-bit value from stream (n successive bits, not boolean 5410 encoded) 5412 o L(n), n-bit number encoded as n booleans (with equal probability 5413 of being 0 or 1) 5415 o B(p), bool with probability p of being 0 5417 o T, tree-encoded value 5419 19.1. Uncompressed Data Chunk 5421 +----------------------+-------+ 5422 | Frame Tag | Type | 5423 +----------------------+-------+ 5424 | frame_tag | f(24) | 5425 | | | 5426 | if (key_frame) { | | 5427 | | | 5428 | start_code | f(24) | 5429 | | | 5430 | horizontal_size_code | f(16) | 5431 | | | 5432 | vertical_size_code | f(16) | 5433 | | | 5434 | } | | 5435 +----------------------+-------+ 5437 The 3-byte frame tag can be parsed as follows: 5439 ---- Begin code block -------------------------------------- 5441 unsigned char *c = pbi->Source; 5442 unsigned int tmp; 5444 tmp = (c[2] << 16) | (c[1] << 8) | c[0]; 5446 key_frame = tmp & 0x1; 5447 version = (tmp >> 1) & 0x7; 5448 show_frame = (tmp >> 4) & 0x1; 5449 first_part_size = (tmp >> 5) & 0x7FFFF; 5451 ---- End code block ---------------------------------------- 5453 Where: 5455 o key_frame indicates if the current frame is a key frame or not. 5457 o version determines the bitstream version. 5459 o show_frame indicates if the current frame is meant to be displayed 5460 or not. 5462 o first_part_size determines the size of the first partition 5463 (control partition), excluding the uncompressed data chunk. 5465 The start_code is a constant 3-byte pattern having value 0x9d012a. 5466 The latter part of the uncompressed chunk (after the start_code) can 5467 be parsed as follows: 5469 ---- Begin code block -------------------------------------- 5471 unsigned char *c = pbi->Source + 6; 5472 unsigned int tmp; 5474 tmp = (c[1] << 8) | c[0]; 5476 width = tmp & 0x3FFF; 5477 horizontal_scale = tmp >> 14; 5479 tmp = (c[3] << 8) | c[2]; 5481 height = tmp & 0x3FFF; 5482 vertical_scale = tmp >> 14; 5484 ---- End code block ---------------------------------------- 5486 19.2. Frame Header 5488 +-------------------------------+------+ 5489 | Frame Header | Type | 5490 +-------------------------------+------+ 5491 | if (key_frame) { | | 5492 | | | 5493 | color_space | L(1) | 5494 | | | 5495 | clamping_type | L(1) | 5496 | | | 5497 | } | | 5498 | | | 5499 | segmentation_enabled | L(1) | 5500 | | | 5501 | if (segmentation_enabled) { | | 5502 | | | 5503 | update_segmentation() | | 5504 | | | 5505 | } | | 5506 | | | 5507 | filter_type | L(1) | 5508 | | | 5509 | loop_filter_level | L(6) | 5510 | | | 5511 | sharpness_level | L(3) | 5512 | | | 5513 | mb_lf_adjustments() | | 5514 | | | 5515 | log2_nbr_of_dct_partitions | L(2) | 5516 | | | 5517 | quant_indices() | | 5518 | | | 5519 | if (key_frame) { | | 5520 | | | 5521 | refresh_entropy_probs | L(1) | 5522 | | | 5523 | } else { | | 5524 | | | 5525 | refresh_golden_frame | L(1) | 5526 | | | 5527 | refresh_alternate_frame | L(1) | 5528 | | | 5529 | if (!refresh_golden_frame) | | 5530 | | | 5531 | copy_buffer_to_golden | L(2) | 5532 | | | 5533 | if (!refresh_alternate_frame) | | 5534 | copy_buffer_to_alternate | L(2) | 5535 | | | 5536 | sign_bias_golden | L(1) | 5537 | | | 5538 | sign_bias_alternate | L(1) | 5539 | | | 5540 | refresh_entropy_probs | L(1) | 5541 | | | 5542 | refresh_last | L(1) | 5543 | | | 5544 | } | | 5545 | | | 5546 | token_prob_update() | | 5547 | | | 5548 | mb_no_coeff_skip | L(1) | 5549 +-------------------------------+------+ 5551 +--------------------------------------+------+ 5552 | Frame Header | Type | 5553 +--------------------------------------+------+ 5554 | prob_skip_false | L(8) | 5555 | | | 5556 | if (!key_frame) { | | 5557 | | | 5558 | prob_intra | L(8) | 5559 | | | 5560 | prob_last | L(8) | 5561 | | | 5562 | prob_golden | L(8) | 5563 | | | 5564 | intra_16x16_prob_update_flag | L(1) | 5565 | | | 5566 | if (intra_16x16_prob_update_flag) { | | 5567 | | | 5568 | for (i = 0; i < 4; i++) | | 5569 | | | 5570 | intra_16x16_prob | L(8) | 5571 | | | 5572 | } | | 5573 | | | 5574 | intra_chroma prob_update_flag | L(1) | 5575 | | | 5576 | if (intra_chroma_prob_update_flag) { | | 5577 | | | 5578 | for (i = 0; i < 3; i++) | | 5579 | | | 5580 | intra_chroma_prob | L(8) | 5581 | | | 5582 | } | | 5583 | | | 5584 | mv_prob_update() | | 5585 | | | 5586 | } | | 5587 +--------------------------------------+------+ 5589 o color_space defines the YUV color space of the sequence 5590 (Section 9.2) 5592 o clamping_type specifies if the decoder is required to clamp the 5593 reconstructed pixel values (Section 9.2) 5595 o segmentation_enabled enables the segmentation feature for the 5596 current frame (Section 9.3) 5598 o filter_type determines whether the normal or the simple loop 5599 filter is used (Section 9.4, Section 15) 5601 o loop_filter_level controls the deblocking filter (Section 9.4, 5602 Section 15) 5604 o sharpness_level controls the deblocking filter (Section 9.4, 5605 Section 15) 5607 o log2_nbr_of_dct_partitions determines the number of separate 5608 partitions containing the DCT coefficients of the macroblocks 5609 (Section 9.5) 5611 o refresh_entropy_probs determines whether updated token 5612 probabilities are used only for this frame or until further update 5614 o refresh_golden_frame determines if the current decoded frame 5615 refreshes the golden frame (Section 9.7) 5617 o refresh_alternate_frame determines if the current decoded frame 5618 refreshes the alternate reference frame (Section 9.7) 5620 o copy_buffer_to_golden determines if the golden reference is 5621 replaced by another reference (Section 9.7) 5623 o copy_buffer_to_alternate determines if the alternate reference is 5624 replaced by another reference (Section 9.7) 5626 o sign_bias_golden controls the sign of motion vectors when the 5627 golden frame is referenced (Section 9.7) 5629 o sign_bias_alternate controls the sign of motion vectors when the 5630 alternate frame is referenced (Section 9.7) 5632 o refresh_last determines if the current decoded frame refreshes the 5633 last frame reference buffer (Section 9.8) 5635 o mb_no_coeff_skip enables or disables the skipping of macroblocks 5636 containing no non-zero coefficients (Section 9.10) 5638 o prob_skip_false the probability that the macroblock is not skipped 5639 (flag indicating skipped macroblock is false) (Section 9.10) 5641 o prob_intra the probability of an intra macroblock (Section 9.10) 5643 o prob_last the probability that the last reference frame is used 5644 for inter prediction (Section 9.10) 5646 o prob_golden the probability that the golden reference frame is 5647 used for inter prediction (Section 9.10) 5649 o intra_16x16_prob_update_flag indicates if the branch probabilies 5650 used in the decoding of luma intra prediction mode are updated 5651 (Section 9.10) 5653 o intra_16x16_prob the branch probabilities of the luma intra 5654 prediction mode decoding tree 5656 o intra_chroma_prob_update_flag indicates if the branch probabilies 5657 used in the decoding of chroma intra prediction mode are updated 5658 (Section 9.10) 5660 o intra_chroma_prob the branch probabilities of the chroma intra 5661 prediction mode decoding tree 5663 +------------------------------------+------+ 5664 | update_segmentation() | Type | 5665 +------------------------------------+------+ 5666 | update_mb_segmentation_map | L(1) | 5667 | | | 5668 | update_segment_feature_data | L(1) | 5669 | | | 5670 | if (update_segment_feature_data) { | | 5671 | | | 5672 | segment_feature_mode | L(1) | 5673 | | | 5674 | for (i = 0; i < 4; i++) { | | 5675 | | | 5676 | quantizer_update | L(1) | 5677 | | | 5678 | if (quantizer_update) { | | 5679 | | | 5680 | quantizer_update_value | L(7) | 5681 | | | 5682 | quantizer_update_sign | L(1) | 5683 | | | 5684 | } | | 5685 | | | 5686 | } | | 5687 | | | 5688 | for (i = 0; i < 4; i++) { | | 5689 | | | 5690 | loop_filter_update | L(1) | 5691 | | | 5692 | if (loop_filter_update) { | | 5693 | | | 5694 | lf_update_value | L(6) | 5695 | | | 5696 | lf_update_sign | L(1) | 5697 | | | 5698 | } | | 5699 | | | 5700 | } | | 5701 | | | 5702 | } | | 5703 | | | 5704 | if (update_mb_segmentation_map) { | | 5705 | | | 5706 | for (i = 0; i < 3; i++) { | | 5707 | | | 5708 | segment_prob_update | L(1) | 5709 | | | 5710 | if (segment_prob_update) { | | 5711 | | | 5712 | segment_prob | L(8) | 5713 | | | 5714 | } | | 5715 | | | 5716 | } | | 5717 | | | 5718 | } | | 5719 +------------------------------------+------+ 5721 o update_mb_segmentation_map determines if the MB segmentation map 5722 is updated in the current frame (Section 9.3) 5724 o update_segment_feature_data indicates if the segment feature data 5725 is updated in the current frame (Section 9.3) 5727 o segment_feature_mode indicates the feature data update mode, 0 for 5728 delta and 1 for the absolute value (Section 9.3) 5730 o quantizer_update indicates if the quantizer value is updated for 5731 the i^(th) segment (Section 9.3) 5733 o quantizer_update_value indicates the update value for the segment 5734 quantizer (Section 9.3) 5736 o quantizer_update_sign indicates the update sign for the segment 5737 quantizer (Section 9.3) 5739 o loop_filter_update indicates if the loop filter level value is 5740 updated for the i^(th) segment (Section 9.3) 5742 o lf_update_value indicates the update value for the loop filter 5743 level (Section 9.3) 5745 o lf_update_sign indicates the update sign for the loop filter level 5746 (Section 9.3) 5748 o segment_prob_update indicates if the branch probabilities used to 5749 decode the segment_id in the MB header are decoded from the stream 5750 or use the default value of 255 (Section 9.3) 5752 o segment_prob the branch probabilities of the segment_id decoding 5753 tree (Section 9.3) 5754 +------------------------------------+------+ 5755 | mb_lf_adjustments() | Type | 5756 +------------------------------------+------+ 5757 | loop_filter_adj_enable | L(1) | 5758 | | | 5759 | if (loop_filter_adj_enable) { | | 5760 | | | 5761 | mode_ref_lf_delta_update | L(1) | 5762 | | | 5763 | if (mode_ref_lf_delta_update) { | | 5764 | | | 5765 | for (i = 0; i < 4; i++) { | | 5766 | | | 5767 | ref_frame_delta_update_flag | L(1) | 5768 | | | 5769 | if (ref_frame_delta_update_flag) { | | 5770 | | | 5771 | delta_magnitude | L(6) | 5772 | | | 5773 | delta_sign | L(1) | 5774 | | | 5775 | } | | 5776 | | | 5777 | } | | 5778 | | | 5779 | for (i = 0; i < 4; i++) { | | 5780 | | | 5781 | mb_mode_delta_update_flag | L(1) | 5782 | | | 5783 | if (mb_mode_delta_update_flag) { | | 5784 | | | 5785 | delta_magnitude | L(6) | 5786 | | | 5787 | delta_sign | L(1) | 5788 | | | 5789 | } | | 5790 | | | 5791 | } | | 5792 | | | 5793 | } | | 5794 | | | 5795 | } | | 5796 +------------------------------------+------+ 5798 o loop_filter_adj_enable indicates if the MB-level loop filter 5799 adjustment (based on the used reference frame and coding mode) is 5800 on for the current frame (Section 9.4) 5802 o mode_ref_lf_delta_update indicates if the delta values used in 5803 adjustment are updated in the current frame (Section 9.4) 5805 o ref_frame_delta_update_flag indicates if the adjustment delta 5806 value corresponding to a certain used reference frame is updated 5807 (Section 9.4) 5809 o delta_magnitude is the absolute value of the delta value 5811 o delta_sign is the sign of the delta value 5813 o mb_mode_delta_update_flag indicates if the adjustment delta value 5814 corresponding to certain MB prediction mode is updated 5815 (Section 9.4) 5816 +----------------------------+------+ 5817 | quant_indices() | Type | 5818 +----------------------------+------+ 5819 | y_ac_qi | L(7) | 5820 | | | 5821 | y_dc_delta_present | L(1) | 5822 | | | 5823 | if (y_dc_delta_present) { | | 5824 | | | 5825 | y_dc_delta_magnitude | L(4) | 5826 | | | 5827 | y_dc_delta_sign | L(1) | 5828 | | | 5829 | } | | 5830 | | | 5831 | if (y2_dc_delta_present) { | | 5832 | | | 5833 | y2_dc_delta_magnitude | L(4) | 5834 | | | 5835 | y2_dc_delta_sign | L(1) | 5836 | | | 5837 | } | | 5838 | | | 5839 | if (y2_ac_delta_present) { | | 5840 | | | 5841 | y2_ac_delta_magnitude | L(4) | 5842 | | | 5843 | y2_ac_delta_sign | L(1) | 5844 | | | 5845 | } | | 5846 | | | 5847 | if (uv_dc_delta_present) { | | 5848 | | | 5849 | uv_dc_delta_magnitude | L(4) | 5850 | | | 5851 | uv_dc_delta_sign | L(1) | 5852 | | | 5853 | } | | 5854 | | | 5855 | if (uv_ac_delta_present) { | | 5856 | | | 5857 | uv_ac_delta_magnitude | L(4) | 5858 | | | 5859 | uv_ac_delta_sign | L(1) | 5860 | | | 5861 | } | | 5862 +----------------------------+------+ 5864 o y_ac_qi is the dequantization table index used for the luma AC 5865 coefficients (and other coefficient groups if no delta value is 5866 present) (Section 9.6) 5868 o y_dc_delta_present indicates if the stream contains a delta value 5869 that is added to the baseline index to obtain the luma DC 5870 coefficient dequantization index (Section 9.6) 5872 o y_dc_delta_magnitude the magnitude of the delta value 5873 (Section 9.6) 5875 o y_dc_delta_sign the sign of the delta value (Section 9.6) 5877 o y2_dc_delta_present indicates if the stream contains a delta value 5878 that is added to the baseline index to obtain the Y2 block DC 5879 coefficient dequantization index (Section 9.6) 5881 o y2_ac_delta_present indicates if the stream contains a delta value 5882 that is added to the baseline index to obtain the Y2 block AC 5883 coefficient dequantization index (Section 9.6) 5885 o uv_dc_delta_present indicates if the stream contains a delta value 5886 that is added to the baseline index to obtain the chroma DC 5887 coefficient dequantization index (Section 9.6) 5889 o uv_ac_delta_present indicates if the stream contains a delta value 5890 that is added to the baseline index to obtain the chroma AC 5891 coefficient dequantization index (Section 9.6) 5892 +-------------------------------+------+ 5893 | token_prob_update() | Type | 5894 +-------------------------------+------+ 5895 | for (i = 0; i < 4; i++) { | | 5896 | | | 5897 | for (j = 0; j < 8; j++) { | | 5898 | | | 5899 | for (k = 0; k < 3; k++) { | | 5900 | | | 5901 | for (l = 0; l < 11; l++) { | | 5902 | | | 5903 | coeff_prob_update_flag | L(1) | 5904 | | | 5905 | if (coeff_prob_update_flag) { | | 5906 | | | 5907 | coeff_prob | L(8) | 5908 | | | 5909 | } | | 5910 | | | 5911 | } | | 5912 | | | 5913 | } | | 5914 | | | 5915 | } | | 5916 | | | 5917 | } | | 5918 +-------------------------------+------+ 5920 o coeff_prob_update_flag indicates if the corresponding branch 5921 probability is updated in the current frame (Section 13.4) 5923 o coeff_prob is the new branch probability (Section 13.4) 5924 +----------------------------+------+ 5925 | mv_prob_update() | Type | 5926 +----------------------------+------+ 5927 | for (i = 0; i < 2; i++) { | | 5928 | | | 5929 | for (j = 0; j < 19; j++) { | | 5930 | | | 5931 | mv_prob_update_flag | L(1) | 5932 | | | 5933 | if (mv_prob_update_flag) { | | 5934 | | | 5935 | prob | L(7) | 5936 | | | 5937 | } | | 5938 | | | 5939 | } | | 5940 | | | 5941 | } | | 5942 +----------------------------+------+ 5944 o mv_prob_update_flag indicates if the corresponding MV decoding 5945 probability is updated in the current frame (Section 17.2) 5947 o prob is the updated probability (Section 17.2) 5949 19.3. Macroblock Data 5951 +---------------------+------+ 5952 | Macroblock Data | Type | 5953 +---------------------+------+ 5954 | macroblock_header() | | 5955 | | | 5956 | residual_data() | | 5957 +---------------------+------+ 5959 +--------------------------------+------+ 5960 | macroblock_header() | Type | 5961 +--------------------------------+------+ 5962 | if (segmentation_map_update) { | | 5963 | | | 5964 | segment_id | T | 5965 | | | 5966 | if (mb_no_coeff_skip) { | | 5967 | | | 5968 | mb_coeff_skip | B(p) | 5969 | | | 5970 | } | | 5971 | | | 5972 | if (!key_frame) { | | 5973 | | | 5974 | is_inter_mb | B(p) | 5975 | | | 5976 | if (is_inter_mb) { | | 5977 | | | 5978 | mb_ref_frame_sel1 | B(p) | 5979 | | | 5980 | if (mb_ref_frame_sel1) | | 5981 | | | 5982 | mb_ref_frame_sel2 | B(p) | 5983 | | | 5984 | mv_mode | T | 5985 | | | 5986 | if (mv_mode == SPLITMV) { | | 5987 | | | 5988 | mv_split_mode | T | 5989 | | | 5990 | for (i = 0; i < numMvs; i++) { | | 5991 | | | 5992 | sub_mv_mode | T | 5993 | | | 5994 | if (sub_mv_mode == NEWMV4x4) { | | 5995 | | | 5996 | read_mvcomponent() | | 5997 | | | 5998 | read_mvcomponent() | | 5999 | | | 6000 | } | | 6001 | | | 6002 | } | | 6003 | | | 6004 | } else if (mv_mode == NEWMV) { | | 6005 | | | 6006 | read_mvcomponent() | | 6007 | | | 6008 | read_mvcomponent() | | 6009 | | | 6010 | } | | 6011 | | | 6012 | } else { /* intra mb */ | | 6013 | | | 6014 | intra_y_mode | T | 6015 +--------------------------------+------+ 6016 +-------------------------------+------+ 6017 | macroblock_header() | Type | 6018 +-------------------------------+------+ 6019 | if (intra_y_mode == B_PRED) { | | 6020 | | | 6021 | for (i = 0; i < 16; i++) | | 6022 | | | 6023 | intra_b_mode | T | 6024 | | | 6025 | } | | 6026 | | | 6027 | intra_uv_mode | T | 6028 | | | 6029 | } | | 6030 +-------------------------------+------+ 6032 o segment_id indicates to which segment the macroblock belongs 6033 (Section 10) 6035 o mb_coeff_skip indicates if the macroblock contains any coded 6036 coefficients or not (Section 11.1) 6038 o is_inter_mb indicates if the macroblock is intra or inter coded 6039 (Section 16) 6041 o mb_ref_frame_sel1 selects the reference frame to be used; last 6042 frame (0), golden/alternate (1) (Section 16.2) 6044 o mb_ref_frame_sel2 selects whether the golden (0) or alternate 6045 reference frame (1) is used (Section 16.2) 6047 o mv_mode determines the macroblock motion vector mode 6048 (Section 16.2) 6050 o mv_split_mode gives macroblock partitioning specification and 6051 determines number of motion vectors used (numMvs)(Section 16.2) 6053 o sub_mv_mode determines the sub-macroblock motion vector mode for 6054 macroblocks coded using SPLITMV motion vector mode (Section 16.2) 6056 o intra_y_mode selects the luminance intra prediction mode 6057 (Section 16.1) 6059 o intra_b_mode selects the sub-macroblock luminance prediction mode 6060 for macroblocks coded using B_PRED mode (Section 16.1) 6062 o intra_uv_mode selects the chrominance intra prediction mode 6063 (Section 16.1) 6064 +----------------------------------------------+------+ 6065 | residual_data() | Type | 6066 +----------------------------------------------+------+ 6067 | if (!mb_coeff_skip) { | | 6068 | | | 6069 | if ( (is_inter_mb && mv_mode != SPLITMV) || | | 6070 | | | 6071 | (!is_inter_mb && intra_y_mode != B_PRED) ) { | | 6072 | | | 6073 | residual_block() /* Y2 */ | | 6074 | | | 6075 | } | | 6076 | | | 6077 | for (i = 0; i < 24; i++) | | 6078 | | | 6079 | residual_block() /* 16 Y, 4 U, 4 V */ | | 6080 | | | 6081 | } | | 6082 +----------------------------------------------+------+ 6084 +-------------------------------------+------+ 6085 | residual_block() | Type | 6086 +-------------------------------------+------+ 6087 | for (i = firstCoeff; i < 16; i++) { | | 6088 | | | 6089 | token | T | 6090 | | | 6091 | if (token == EOB) break; | | 6092 | | | 6093 | if (token_has_extra_bits) { | | 6094 | | | 6095 | extra_bits | L(n) | 6096 | | | 6097 | sign | L(1) | 6098 +-------------------------------------+------+ 6100 o firstCoeff is 1 for luma blocks of macroblocks containing Y2 6101 subblock, otherwise 0 6103 o token defines the value of the coefficient, the value range of the 6104 coefficient or the end of block (Section 13.2) 6106 o extra_bits determine the value of the coefficient within the value 6107 range defined by token (Section 13.2) 6109 o sign indicates the sign of the coefficient (Section 13.2) 6111 20. Attachment One: Reference Decoder Source Code 6113 Note that the code in this attachment may exhibit bugs, and should be 6114 considered a draft until this document reaches RFC status. 6116 20.1. bit_ops.h 6118 ---- Begin code block -------------------------------------- 6120 /* 6121 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6122 * 6123 * Use of this source code is governed by a BSD-style license 6124 * that can be found in the LICENSE file in the root of the source 6125 * tree. An additional intellectual property rights grant can be 6126 * found in the file PATENTS. All contributing project authors may 6127 * be found in the AUTHORS file in the root of the source tree. 6128 */ 6129 #ifndef BIT_OPS_H 6130 #define BIT_OPS_H 6132 /* Evaluates to a mask with n bits set */ 6133 #define BITS_MASK(n) ((1<<(n))-1) 6135 /* Returns len bits, with the LSB at position bit */ 6136 #define BITS_GET(val, bit, len) (((val)>>(bit))&BITS_MASK(len)) 6138 #endif 6140 ---- End code block ---------------------------------------- 6142 20.2. bool_decoder.h 6144 ---- Begin code block -------------------------------------- 6146 /* 6147 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6148 * 6149 * Use of this source code is governed by a BSD-style license and 6150 * patent grant that can be found in the LICENSE file in the root of 6151 * the source tree. All contributing project authors may be found in 6152 * the AUTHORS file in the root of the source tree. 6153 */ 6155 #ifndef BOOL_DECODER_H 6156 #define BOOL_DECODER_H 6157 #include 6159 struct bool_decoder 6160 { 6161 const unsigned char *input; /* next compressed data byte */ 6162 size_t input_len; /* length of the input buffer */ 6163 unsigned int range; /* identical to encoder's 6164 * range */ 6165 unsigned int value; /* contains at least 8 6166 * significant bits */ 6167 int bit_count; /* # of bits shifted out of 6168 * value, max 7 */ 6169 }; 6171 static void 6172 init_bool_decoder(struct bool_decoder *d, 6173 const unsigned char *start_partition, 6174 size_t sz) 6175 { 6176 if (sz >= 2) 6177 { 6178 d->value = (start_partition[0] << 8) /* first 2 input 6179 * bytes */ 6180 | start_partition[1]; 6181 d->input = start_partition + 2; /* ptr to next byte */ 6182 d->input_len = sz - 2; 6183 } 6184 else 6185 { 6186 d->value = 0; 6187 d->input = NULL; 6188 d->input_len = 0; 6189 } 6191 d->range = 255; /* initial range is full */ 6192 d->bit_count = 0; /* have not yet shifted out any bits */ 6193 } 6195 static int bool_get(struct bool_decoder *d, int probability) 6196 { 6197 /* range and split are identical to the corresponding values 6198 used by the encoder when this bool was written */ 6200 unsigned int split = 1 + (((d->range - 1) * probability) >> 8); 6201 unsigned int SPLIT = split << 8; 6202 int retval; /* will be 0 or 1 */ 6203 if (d->value >= SPLIT) /* encoded a one */ 6204 { 6205 retval = 1; 6206 d->range -= split; /* reduce range */ 6207 d->value -= SPLIT; /* subtract off left endpoint of 6208 * interval */ 6209 } 6210 else /* encoded a zero */ 6211 { 6212 retval = 0; 6213 d->range = split; /* reduce range, no change in left 6214 * endpoint */ 6215 } 6217 while (d->range < 128) /* shift out irrelevant value bits */ 6218 { 6219 d->value <<= 1; 6220 d->range <<= 1; 6222 if (++d->bit_count == 8) /* shift in new bits 8 at a time */ 6223 { 6224 d->bit_count = 0; 6226 if (d->input_len) 6227 { 6228 d->value |= *d->input++; 6229 d->input_len--; 6230 } 6231 } 6232 } 6234 return retval; 6235 } 6237 static int bool_get_bit(struct bool_decoder *br) 6238 { 6239 return bool_get(br, 128); 6240 } 6242 static int bool_get_uint(struct bool_decoder *br, int bits) 6243 { 6244 int z = 0; 6245 int bit; 6247 for (bit = bits - 1; bit >= 0; bit--) 6248 { 6249 z |= (bool_get_bit(br) << bit); 6250 } 6252 return z; 6253 } 6255 static int bool_get_int(struct bool_decoder *br, int bits) 6256 { 6257 int z = 0; 6258 int bit; 6260 for (bit = bits - 1; bit >= 0; bit--) 6261 { 6262 z |= (bool_get_bit(br) << bit); 6263 } 6265 return bool_get_bit(br) ? -z : z; 6266 } 6268 static int bool_maybe_get_int(struct bool_decoder *br, int bits) 6269 { 6270 return bool_get_bit(br) ? bool_get_int(br, bits) : 0; 6271 } 6273 static int 6274 bool_read_tree(struct bool_decoder *bool, 6275 const int *t, 6276 const unsigned char *p) 6277 { 6278 int i = 0; 6280 while ((i = t[ i + bool_get(bool, p[i>>1])]) > 0) ; 6282 return -i; 6283 } 6284 #endif 6286 ---- End code block ---------------------------------------- 6288 20.3. dequant_data.h 6290 ---- Begin code block -------------------------------------- 6292 static const int dc_q_lookup[128] = 6293 { 6294 4, 5, 6, 7, 8, 9, 10, 10, 6295 11, 12, 13, 14, 15, 16, 17, 17, 6296 18, 19, 20, 20, 21, 21, 22, 22, 6297 23, 23, 24, 25, 25, 26, 27, 28, 6298 29, 30, 31, 32, 33, 34, 35, 36, 6299 37, 37, 38, 39, 40, 41, 42, 43, 6300 44, 45, 46, 46, 47, 48, 49, 50, 6301 51, 52, 53, 54, 55, 56, 57, 58, 6302 59, 60, 61, 62, 63, 64, 65, 66, 6303 67, 68, 69, 70, 71, 72, 73, 74, 6304 75, 76, 76, 77, 78, 79, 80, 81, 6305 82, 83, 84, 85, 86, 87, 88, 89, 6306 91, 93, 95, 96, 98, 100, 101, 102, 6307 104, 106, 108, 110, 112, 114, 116, 118, 6308 122, 124, 126, 128, 130, 132, 134, 136, 6309 138, 140, 143, 145, 148, 151, 154, 157 6310 }; 6311 static const int ac_q_lookup[128] = 6312 { 6313 4, 5, 6, 7, 8, 9, 10, 11, 6314 12, 13, 14, 15, 16, 17, 18, 19, 6315 20, 21, 22, 23, 24, 25, 26, 27, 6316 28, 29, 30, 31, 32, 33, 34, 35, 6317 36, 37, 38, 39, 40, 41, 42, 43, 6318 44, 45, 46, 47, 48, 49, 50, 51, 6319 52, 53, 54, 55, 56, 57, 58, 60, 6320 62, 64, 66, 68, 70, 72, 74, 76, 6321 78, 80, 82, 84, 86, 88, 90, 92, 6322 94, 96, 98, 100, 102, 104, 106, 108, 6323 110, 112, 114, 116, 119, 122, 125, 128, 6324 131, 134, 137, 140, 143, 146, 149, 152, 6325 155, 158, 161, 164, 167, 170, 173, 177, 6326 181, 185, 189, 193, 197, 201, 205, 209, 6327 213, 217, 221, 225, 229, 234, 239, 245, 6328 249, 254, 259, 264, 269, 274, 279, 284 6329 }; 6331 ---- End code block ---------------------------------------- 6333 20.4. dixie.c 6334 ---- Begin code block -------------------------------------- 6336 /* 6337 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6338 * 6339 * Use of this source code is governed by a BSD-style license 6340 * that can be found in the LICENSE file in the root of the source 6341 * tree. An additional intellectual property rights grant can be 6342 * found in the file PATENTS. All contributing project authors may 6343 * be found in the AUTHORS file in the root of the source tree. 6344 */ 6345 #include "vpx_codec_internal.h" 6346 #include "bit_ops.h" 6347 #include "dixie.h" 6348 #include "vp8_prob_data.h" 6349 #include "dequant_data.h" 6350 #include "modemv.h" 6351 #include "tokens.h" 6352 #include "predict.h" 6353 #include "dixie_loopfilter.h" 6354 #include 6355 #include 6357 enum 6358 { 6359 FRAME_HEADER_SZ = 3, 6360 KEYFRAME_HEADER_SZ = 7 6361 }; 6363 #define ARRAY_COPY(a,b) {\ 6364 assert(sizeof(a)==sizeof(b));memcpy(a,b,sizeof(a));} 6365 static void 6366 decode_entropy_header(struct vp8_decoder_ctx *ctx, 6367 struct bool_decoder *bool, 6368 struct vp8_entropy_hdr *hdr) 6369 { 6370 int i, j, k, l; 6372 /* Read coefficient probability updates */ 6373 for (i = 0; i < BLOCK_TYPES; i++) 6374 for (j = 0; j < COEF_BANDS; j++) 6375 for (k = 0; k < PREV_COEF_CONTEXTS; k++) 6376 for (l = 0; l < ENTROPY_NODES; l++) 6377 if (bool_get(bool, 6378 k_coeff_entropy_update_probs 6379 [i][j][k][l])) 6380 hdr->coeff_probs[i][j][k][l] = 6381 bool_get_uint(bool, 8); 6383 /* Read coefficient skip mode probability */ 6384 hdr->coeff_skip_enabled = bool_get_bit(bool); 6386 if (hdr->coeff_skip_enabled) 6387 hdr->coeff_skip_prob = bool_get_uint(bool, 8); 6389 /* Parse interframe probability updates */ 6390 if (!ctx->frame_hdr.is_keyframe) 6391 { 6392 hdr->prob_inter = bool_get_uint(bool, 8); 6393 hdr->prob_last = bool_get_uint(bool, 8); 6394 hdr->prob_gf = bool_get_uint(bool, 8); 6396 if (bool_get_bit(bool)) 6397 for (i = 0; i < 4; i++) 6398 hdr->y_mode_probs[i] = bool_get_uint(bool, 8); 6400 if (bool_get_bit(bool)) 6401 for (i = 0; i < 3; i++) 6402 hdr->uv_mode_probs[i] = bool_get_uint(bool, 8); 6404 for (i = 0; i < 2; i++) 6405 for (j = 0; j < MV_PROB_CNT; j++) 6406 if (bool_get(bool, k_mv_entropy_update_probs[i][j])) 6407 { 6408 int x = bool_get_uint(bool, 7); 6409 hdr->mv_probs[i][j] = x ? x << 1 : 1; 6410 } 6411 } 6412 } 6414 static void 6415 decode_reference_header(struct vp8_decoder_ctx *ctx, 6416 struct bool_decoder *bool, 6417 struct vp8_reference_hdr *hdr) 6418 { 6419 unsigned int key = ctx->frame_hdr.is_keyframe; 6421 hdr->refresh_gf = key ? 1 : bool_get_bit(bool); 6422 hdr->refresh_arf = key ? 1 : bool_get_bit(bool); 6423 hdr->copy_gf = key ? 0 : !hdr->refresh_gf 6424 ? bool_get_uint(bool, 2) : 0; 6425 hdr->copy_arf = key ? 0 : !hdr->refresh_arf 6426 ? bool_get_uint(bool, 2) : 0; 6427 hdr->sign_bias[GOLDEN_FRAME] = key ? 0 : bool_get_bit(bool); 6428 hdr->sign_bias[ALTREF_FRAME] = key ? 0 : bool_get_bit(bool); 6429 hdr->refresh_entropy = bool_get_bit(bool); 6430 hdr->refresh_last = key ? 1 : bool_get_bit(bool); 6431 } 6433 static void 6434 decode_quantizer_header(struct vp8_decoder_ctx *ctx, 6435 struct bool_decoder *bool, 6436 struct vp8_quant_hdr *hdr) 6437 { 6438 int update; 6439 int last_q = hdr->q_index; 6441 hdr->q_index = bool_get_uint(bool, 7); 6442 update = last_q != hdr->q_index; 6443 update |= (hdr->y1_dc_delta_q = bool_maybe_get_int(bool, 4)); 6444 update |= (hdr->y2_dc_delta_q = bool_maybe_get_int(bool, 4)); 6445 update |= (hdr->y2_ac_delta_q = bool_maybe_get_int(bool, 4)); 6446 update |= (hdr->uv_dc_delta_q = bool_maybe_get_int(bool, 4)); 6447 update |= (hdr->uv_ac_delta_q = bool_maybe_get_int(bool, 4)); 6448 hdr->delta_update = update; 6449 } 6451 static void 6452 decode_and_init_token_partitions(struct vp8_decoder_ctx *ctx, 6453 struct bool_decoder *bool, 6454 const unsigned char *data, 6455 unsigned int sz, 6456 struct vp8_token_hdr *hdr) 6457 { 6458 int i; 6460 hdr->partitions = 1 << bool_get_uint(bool, 2); 6462 if (sz < 3 *(hdr->partitions - 1)) 6463 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6464 "Truncated packet found parsing partition" 6465 " lengths."); 6467 sz -= 3 * (hdr->partitions - 1); 6469 for (i = 0; i < hdr->partitions; i++) 6470 { 6471 if (i < hdr->partitions - 1) 6472 { 6473 hdr->partition_sz[i] = (data[2] << 16) 6474 | (data[1] << 8) | data[0]; 6475 data += 3; 6476 } 6477 else 6478 hdr->partition_sz[i] = sz; 6480 if (sz < hdr->partition_sz[i]) 6481 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6482 "Truncated partition %d", i); 6484 sz -= hdr->partition_sz[i]; 6485 } 6487 for (i = 0; i < ctx->token_hdr.partitions; i++) 6488 { 6489 init_bool_decoder(&ctx->tokens[i].bool, data, 6490 ctx->token_hdr.partition_sz[i]); 6491 data += ctx->token_hdr.partition_sz[i]; 6492 } 6493 } 6495 static void 6496 decode_loopfilter_header(struct vp8_decoder_ctx *ctx, 6497 struct bool_decoder *bool, 6498 struct vp8_loopfilter_hdr *hdr) 6499 { 6500 if (ctx->frame_hdr.is_keyframe) 6501 memset(hdr, 0, sizeof(*hdr)); 6503 hdr->use_simple = bool_get_bit(bool); 6504 hdr->level = bool_get_uint(bool, 6); 6505 hdr->sharpness = bool_get_uint(bool, 3); 6506 hdr->delta_enabled = bool_get_bit(bool); 6508 if (hdr->delta_enabled && bool_get_bit(bool)) 6509 { 6510 int i; 6512 for (i = 0; i < BLOCK_CONTEXTS; i++) 6513 hdr->ref_delta[i] = bool_maybe_get_int(bool, 6); 6515 for (i = 0; i < BLOCK_CONTEXTS; i++) 6516 hdr->mode_delta[i] = bool_maybe_get_int(bool, 6); 6517 } 6518 } 6519 static void 6520 decode_segmentation_header(struct vp8_decoder_ctx *ctx, 6521 struct bool_decoder *bool, 6522 struct vp8_segment_hdr *hdr) 6523 { 6524 if (ctx->frame_hdr.is_keyframe) 6525 memset(hdr, 0, sizeof(*hdr)); 6527 hdr->enabled = bool_get_bit(bool); 6529 if (hdr->enabled) 6530 { 6531 int i; 6533 hdr->update_map = bool_get_bit(bool); 6534 hdr->update_data = bool_get_bit(bool); 6536 if (hdr->update_data) 6537 { 6538 hdr->abs = bool_get_bit(bool); 6540 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6541 hdr->quant_idx[i] = bool_maybe_get_int(bool, 7); 6543 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6544 hdr->lf_level[i] = bool_maybe_get_int(bool, 6); 6545 } 6547 if (hdr->update_map) 6548 { 6549 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) 6550 hdr->tree_probs[i] = bool_get_bit(bool) 6551 ? bool_get_uint(bool, 8) 6552 : 255; 6553 } 6554 } 6555 else 6556 { 6557 hdr->update_map = 0; 6558 hdr->update_data = 0; 6559 } 6560 } 6562 static void 6563 dequant_global_init(struct dequant_factors dqf[MAX_MB_SEGMENTS]) 6564 { 6565 int i; 6566 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6567 dqf[i].quant_idx = -1; 6568 } 6570 static int 6571 clamp_q(int q) 6572 { 6573 if (q < 0) return 0; 6574 else if (q > 127) return 127; 6576 return q; 6577 } 6579 static int 6580 dc_q(int q) 6581 { 6582 return dc_q_lookup[clamp_q(q)]; 6583 } 6585 static int 6586 ac_q(int q) 6587 { 6588 return ac_q_lookup[clamp_q(q)]; 6589 } 6591 static void 6592 dequant_init(struct dequant_factors factors[MAX_MB_SEGMENTS], 6593 const struct vp8_segment_hdr *seg, 6594 const struct vp8_quant_hdr *quant_hdr) 6595 { 6596 int i, q; 6597 struct dequant_factors *dqf = factors; 6599 for (i = 0; i < (seg->enabled ? MAX_MB_SEGMENTS : 1); i++) 6600 { 6601 q = quant_hdr->q_index; 6603 if (seg->enabled) 6604 q = (!seg->abs) ? q + seg->quant_idx[i] 6605 : seg->quant_idx[i]; 6607 if (dqf->quant_idx != q || quant_hdr->delta_update) 6608 { 6609 dqf->factor[TOKEN_BLOCK_Y1][0] = 6610 dc_q(q + quant_hdr->y1_dc_delta_q); 6611 dqf->factor[TOKEN_BLOCK_Y1][1] = 6612 ac_q(q); 6613 dqf->factor[TOKEN_BLOCK_UV][0] = 6614 dc_q(q + quant_hdr->uv_dc_delta_q); 6615 dqf->factor[TOKEN_BLOCK_UV][1] = 6616 ac_q(q + quant_hdr->uv_ac_delta_q); 6617 dqf->factor[TOKEN_BLOCK_Y2][0] = 6618 dc_q(q + quant_hdr->y2_dc_delta_q) * 2; 6619 dqf->factor[TOKEN_BLOCK_Y2][1] = 6620 ac_q(q + quant_hdr->y2_ac_delta_q) * 155 / 100; 6622 if (dqf->factor[TOKEN_BLOCK_Y2][1] < 8) 6623 dqf->factor[TOKEN_BLOCK_Y2][1] = 8; 6625 if (dqf->factor[TOKEN_BLOCK_UV][0] > 132) 6626 dqf->factor[TOKEN_BLOCK_UV][0] = 132; 6628 dqf->quant_idx = q; 6629 } 6631 dqf++; 6632 } 6633 } 6635 static void 6636 decode_frame(struct vp8_decoder_ctx *ctx, 6637 const unsigned char *data, 6638 unsigned int sz) 6639 { 6640 vpx_codec_err_t res; 6641 struct bool_decoder bool; 6642 int i, row, partition; 6644 ctx->saved_entropy_valid = 0; 6646 if ((res = vp8_parse_frame_header(data, sz, &ctx->frame_hdr))) 6647 vpx_internal_error(&ctx->error, res, 6648 "Failed to parse frame header"); 6650 if (ctx->frame_hdr.is_experimental) 6651 vpx_internal_error(&ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6652 "Experimental bitstreams not supported."); 6654 data += FRAME_HEADER_SZ; 6655 sz -= FRAME_HEADER_SZ; 6656 if (ctx->frame_hdr.is_keyframe) 6657 { 6658 data += KEYFRAME_HEADER_SZ; 6659 sz -= KEYFRAME_HEADER_SZ; 6660 ctx->mb_cols = (ctx->frame_hdr.kf.w + 15) / 16; 6661 ctx->mb_rows = (ctx->frame_hdr.kf.h + 15) / 16; 6662 } 6664 /* Start the bitreader for the header/entropy partition */ 6665 init_bool_decoder(&bool, data, ctx->frame_hdr.part0_sz); 6667 /* Skip the colorspace and clamping bits */ 6668 if (ctx->frame_hdr.is_keyframe) 6669 if (bool_get_uint(&bool, 2)) 6670 vpx_internal_error( 6671 &ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6672 "Reserved bits not supported."); 6674 decode_segmentation_header(ctx, &bool, &ctx->segment_hdr); 6675 decode_loopfilter_header(ctx, &bool, &ctx->loopfilter_hdr); 6676 decode_and_init_token_partitions(ctx, 6677 &bool, 6678 data + ctx->frame_hdr.part0_sz, 6679 sz - ctx->frame_hdr.part0_sz, 6680 &ctx->token_hdr); 6681 decode_quantizer_header(ctx, &bool, &ctx->quant_hdr); 6682 decode_reference_header(ctx, &bool, &ctx->reference_hdr); 6684 /* Set keyframe entropy defaults. These get updated on keyframes 6685 * regardless of the refresh_entropy setting. 6686 */ 6687 if (ctx->frame_hdr.is_keyframe) 6688 { 6689 ARRAY_COPY(ctx->entropy_hdr.coeff_probs, 6690 k_default_coeff_probs); 6691 ARRAY_COPY(ctx->entropy_hdr.mv_probs, 6692 k_default_mv_probs); 6693 ARRAY_COPY(ctx->entropy_hdr.y_mode_probs, 6694 k_default_y_mode_probs); 6695 ARRAY_COPY(ctx->entropy_hdr.uv_mode_probs, 6696 k_default_uv_mode_probs); 6697 } 6699 if (!ctx->reference_hdr.refresh_entropy) 6700 { 6701 ctx->saved_entropy = ctx->entropy_hdr; 6702 ctx->saved_entropy_valid = 1; 6703 } 6704 decode_entropy_header(ctx, &bool, &ctx->entropy_hdr); 6706 vp8_dixie_modemv_init(ctx); 6707 vp8_dixie_tokens_init(ctx); 6708 vp8_dixie_predict_init(ctx); 6709 dequant_init(ctx->dequant_factors, &ctx->segment_hdr, 6710 &ctx->quant_hdr); 6712 for (row = 0, partition = 0; row < ctx->mb_rows; row++) 6713 { 6714 vp8_dixie_modemv_process_row( 6715 ctx, &bool, row, 0, ctx->mb_cols); 6716 vp8_dixie_tokens_process_row(ctx, partition, row, 0, 6717 ctx->mb_cols); 6718 vp8_dixie_predict_process_row(ctx, row, 0, ctx->mb_cols); 6720 if (ctx->loopfilter_hdr.level && row) 6721 vp8_dixie_loopfilter_process_row(ctx, row - 1, 0, 6722 ctx->mb_cols); 6724 if (++partition == ctx->token_hdr.partitions) 6725 partition = 0; 6726 } 6728 if (ctx->loopfilter_hdr.level) 6729 vp8_dixie_loopfilter_process_row( 6730 ctx, row - 1, 0, ctx->mb_cols); 6732 ctx->frame_cnt++; 6734 if (!ctx->reference_hdr.refresh_entropy) 6735 { 6736 ctx->entropy_hdr = ctx->saved_entropy; 6737 ctx->saved_entropy_valid = 0; 6738 } 6740 /* Handle reference frame updates */ 6741 if (ctx->reference_hdr.copy_arf == 1) 6742 { 6743 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6744 ctx->ref_frames[ALTREF_FRAME] = 6745 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6746 } 6747 else if (ctx->reference_hdr.copy_arf == 2) 6748 { 6749 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6750 ctx->ref_frames[ALTREF_FRAME] = 6751 vp8_dixie_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6753 } 6755 if (ctx->reference_hdr.copy_gf == 1) 6756 { 6757 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6758 ctx->ref_frames[GOLDEN_FRAME] = 6759 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6760 } 6761 else if (ctx->reference_hdr.copy_gf == 2) 6762 { 6763 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6764 ctx->ref_frames[GOLDEN_FRAME] = 6765 vp8_dixie_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6766 } 6768 if (ctx->reference_hdr.refresh_gf) 6769 { 6770 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6771 ctx->ref_frames[GOLDEN_FRAME] = 6772 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6773 } 6775 if (ctx->reference_hdr.refresh_arf) 6776 { 6777 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6778 ctx->ref_frames[ALTREF_FRAME] = 6779 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6780 } 6782 if (ctx->reference_hdr.refresh_last) 6783 { 6784 vp8_dixie_release_ref_frame(ctx->ref_frames[LAST_FRAME]); 6785 ctx->ref_frames[LAST_FRAME] = 6786 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6787 } 6789 } 6791 void 6792 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx) 6793 { 6794 dequant_global_init(ctx->dequant_factors); 6795 } 6797 #define CHECK_FOR_UPDATE(lval,rval,update_flag) do {\ 6798 unsigned int old = lval; \ 6799 update_flag |= (old != (lval = rval)); \ 6800 } while(0) 6802 vpx_codec_err_t 6803 vp8_parse_frame_header(const unsigned char *data, 6804 unsigned int sz, 6805 struct vp8_frame_hdr *hdr) 6806 { 6807 unsigned long raw; 6809 if (sz < 10) 6810 return VPX_CODEC_CORRUPT_FRAME; 6812 /* The frame header is defined as a three byte little endian 6813 * value 6814 */ 6815 raw = data[0] | (data[1] << 8) | (data[2] << 16); 6816 hdr->is_keyframe = !BITS_GET(raw, 0, 1); 6817 hdr->version = BITS_GET(raw, 1, 2); 6818 hdr->is_experimental = BITS_GET(raw, 3, 1); 6819 hdr->is_shown = BITS_GET(raw, 4, 1); 6820 hdr->part0_sz = BITS_GET(raw, 5, 19); 6822 if (sz <= hdr->part0_sz + (hdr->is_keyframe ? 10 : 3)) 6823 return VPX_CODEC_CORRUPT_FRAME; 6825 hdr->frame_size_updated = 0; 6827 if (hdr->is_keyframe) 6828 { 6829 unsigned int update = 0; 6831 /* Keyframe header consists of a three byte sync code 6832 * followed by the width and height and associated scaling 6833 * factors. 6834 */ 6835 if (data[3] != 0x9d || data[4] != 0x01 || data[5] != 0x2a) 6836 return VPX_CODEC_UNSUP_BITSTREAM; 6838 raw = data[6] | (data[7] << 8) 6839 | (data[8] << 16) | (data[9] << 24); 6840 CHECK_FOR_UPDATE(hdr->kf.w, BITS_GET(raw, 0, 14), 6841 update); 6842 CHECK_FOR_UPDATE(hdr->kf.scale_w, BITS_GET(raw, 14, 2), 6843 update); 6844 CHECK_FOR_UPDATE(hdr->kf.h, BITS_GET(raw, 16, 14), 6845 update); 6846 CHECK_FOR_UPDATE(hdr->kf.scale_h, BITS_GET(raw, 30, 2), 6847 update); 6849 hdr->frame_size_updated = update; 6851 if (!hdr->kf.w || !hdr->kf.h) 6852 return VPX_CODEC_UNSUP_BITSTREAM; 6853 } 6855 return VPX_CODEC_OK; 6856 } 6858 vpx_codec_err_t 6859 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 6860 const unsigned char *data, 6861 unsigned int sz) 6862 { 6863 volatile struct vp8_decoder_ctx *ctx_ = ctx; 6865 ctx->error.error_code = VPX_CODEC_OK; 6866 ctx->error.has_detail = 0; 6868 if (!setjmp(ctx->error.jmp)) 6869 decode_frame(ctx, data, sz); 6871 return ctx_->error.error_code; 6872 } 6874 void 6875 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx) 6876 { 6877 vp8_dixie_predict_destroy(ctx); 6878 vp8_dixie_tokens_destroy(ctx); 6879 vp8_dixie_modemv_destroy(ctx); 6880 } 6882 ---- End code block ---------------------------------------- 6884 20.5. dixie.h 6886 ---- Begin code block -------------------------------------- 6888 /* 6889 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6890 * 6891 * Use of this source code is governed by a BSD-style license 6892 * that can be found in the LICENSE file in the root of the source 6893 * tree. An additional intellectual property rights grant can be 6894 * found in the file PATENTS. All contributing project authors may 6895 * be found in the AUTHORS file in the root of the source tree. 6896 */ 6897 #ifndef DIXIE_H 6898 #define DIXIE_H 6899 #include "vpx_codec_internal.h" 6900 #include "bool_decoder.h" 6902 struct vp8_frame_hdr 6903 { 6904 unsigned int is_keyframe; /* Frame is a keyframe */ 6905 unsigned int is_experimental; /* Frame is a keyframe */ 6906 unsigned int version; /* Bitstream version */ 6907 unsigned int is_shown; /* Frame is to be displayed. */ 6908 unsigned int part0_sz; /* Partition 0 length, in bytes */ 6910 struct vp8_kf_hdr 6911 { 6912 unsigned int w; /* Width */ 6913 unsigned int h; /* Height */ 6914 unsigned int scale_w; /* Scaling factor, Width */ 6915 unsigned int scale_h; /* Scaling factor, Height */ 6916 } kf; 6918 unsigned int frame_size_updated; /* Flag to indicate a resolution 6919 * update. 6920 */ 6921 }; 6923 enum 6924 { 6925 MB_FEATURE_TREE_PROBS = 3, 6926 MAX_MB_SEGMENTS = 4 6927 }; 6929 struct vp8_segment_hdr 6930 { 6931 unsigned int enabled; 6932 unsigned int update_data; 6933 unsigned int update_map; 6934 unsigned int abs; /* 0=deltas, 1=absolute values */ 6935 unsigned int tree_probs[MB_FEATURE_TREE_PROBS]; 6936 int lf_level[MAX_MB_SEGMENTS]; 6937 int quant_idx[MAX_MB_SEGMENTS]; 6939 }; 6941 enum 6942 { 6943 BLOCK_CONTEXTS = 4 6944 }; 6946 struct vp8_loopfilter_hdr 6947 { 6948 unsigned int use_simple; 6949 unsigned int level; 6950 unsigned int sharpness; 6951 unsigned int delta_enabled; 6952 int ref_delta[BLOCK_CONTEXTS]; 6953 int mode_delta[BLOCK_CONTEXTS]; 6954 }; 6956 enum 6957 { 6958 MAX_PARTITIONS = 8 6959 }; 6961 struct vp8_token_hdr 6962 { 6963 unsigned int partitions; 6964 unsigned int partition_sz[MAX_PARTITIONS]; 6965 }; 6967 struct vp8_quant_hdr 6968 { 6969 unsigned int q_index; 6970 int delta_update; 6971 int y1_dc_delta_q; 6972 int y2_dc_delta_q; 6973 int y2_ac_delta_q; 6974 int uv_dc_delta_q; 6975 int uv_ac_delta_q; 6976 }; 6978 struct vp8_reference_hdr 6979 { 6980 unsigned int refresh_last; 6981 unsigned int refresh_gf; 6982 unsigned int refresh_arf; 6983 unsigned int copy_gf; 6984 unsigned int copy_arf; 6985 unsigned int sign_bias[4]; 6986 unsigned int refresh_entropy; 6987 }; 6989 enum 6990 { 6991 BLOCK_TYPES = 4, 6992 PREV_COEF_CONTEXTS = 3, 6993 COEF_BANDS = 8, 6994 ENTROPY_NODES = 11, 6995 }; 6996 typedef unsigned char coeff_probs_table_t[BLOCK_TYPES][COEF_BANDS] 6997 [PREV_COEF_CONTEXTS] 6998 [ENTROPY_NODES]; 7000 enum 7001 { 7002 MV_PROB_CNT = 2 + 8 - 1 + 10 /* from entropymv.h */ 7003 }; 7004 typedef unsigned char mv_component_probs_t[MV_PROB_CNT]; 7006 struct vp8_entropy_hdr 7007 { 7008 coeff_probs_table_t coeff_probs; 7009 mv_component_probs_t mv_probs[2]; 7010 unsigned int coeff_skip_enabled; 7011 unsigned char coeff_skip_prob; 7012 unsigned char y_mode_probs[4]; 7013 unsigned char uv_mode_probs[3]; 7014 unsigned char prob_inter; 7015 unsigned char prob_last; 7016 unsigned char prob_gf; 7017 }; 7019 enum reference_frame 7020 { 7021 CURRENT_FRAME, 7022 LAST_FRAME, 7023 GOLDEN_FRAME, 7024 ALTREF_FRAME, 7025 NUM_REF_FRAMES 7027 }; 7029 enum prediction_mode 7030 { 7031 /* 16x16 intra modes */ 7032 DC_PRED, V_PRED, H_PRED, TM_PRED, B_PRED, 7034 /* 16x16 inter modes */ 7035 NEARESTMV, NEARMV, ZEROMV, NEWMV, SPLITMV, 7037 MB_MODE_COUNT, 7039 /* 4x4 intra modes */ 7040 B_DC_PRED = 0, B_TM_PRED, B_VE_PRED, B_HE_PRED, B_LD_PRED, 7041 B_RD_PRED, B_VR_PRED, B_VL_PRED, B_HD_PRED, B_HU_PRED, 7043 /* 4x4 inter modes */ 7044 LEFT4X4, ABOVE4X4, ZERO4X4, NEW4X4, 7046 B_MODE_COUNT 7047 }; 7049 enum splitmv_partitioning 7050 { 7051 SPLITMV_16X8, 7052 SPLITMV_8X16, 7053 SPLITMV_8X8, 7054 SPLITMV_4X4 7055 }; 7057 typedef short filter_t[6]; 7059 typedef union mv 7060 { 7061 struct 7062 { 7063 int16_t x, y; 7064 } d; 7065 uint32_t raw; 7066 } mv_t; 7068 struct mb_base_info 7069 { 7070 unsigned char y_mode : 4; 7071 unsigned char uv_mode : 4; 7072 unsigned char segment_id : 2; 7073 unsigned char ref_frame : 2; 7074 unsigned char skip_coeff : 1; 7075 unsigned char need_mc_border : 1; 7076 enum splitmv_partitioning partitioning : 2; 7077 union mv mv; 7078 unsigned int eob_mask; 7079 }; 7081 struct mb_info 7082 { 7083 struct mb_base_info base; 7084 union 7085 { 7086 union mv mvs[16]; 7087 enum prediction_mode modes[16]; 7088 } split; 7089 }; 7091 /* A "token entropy context" has 4 Y values, 2 U, 2 V, and 1 Y2 */ 7092 typedef int token_entropy_ctx_t[4 + 2 + 2 + 1]; 7094 struct token_decoder 7095 { 7096 struct bool_decoder bool; 7097 token_entropy_ctx_t left_token_entropy_ctx; 7098 short *coeffs; 7099 }; 7101 enum token_block_type 7102 { 7103 TOKEN_BLOCK_Y1, 7104 TOKEN_BLOCK_UV, 7105 TOKEN_BLOCK_Y2, 7106 TOKEN_BLOCK_TYPES, 7107 }; 7109 struct dequant_factors 7110 { 7111 int quant_idx; 7112 short factor[TOKEN_BLOCK_TYPES][2]; /* [ Y1, UV, Y2 ] 7113 * [ DC, AC ] */ 7114 }; 7115 struct ref_cnt_img 7116 { 7117 vpx_image_t img; 7118 unsigned int ref_cnt; 7119 }; 7121 struct vp8_decoder_ctx 7122 { 7123 struct vpx_internal_error_info error; 7124 unsigned int frame_cnt; 7126 struct vp8_frame_hdr frame_hdr; 7127 struct vp8_segment_hdr segment_hdr; 7128 struct vp8_loopfilter_hdr loopfilter_hdr; 7129 struct vp8_token_hdr token_hdr; 7130 struct vp8_quant_hdr quant_hdr; 7131 struct vp8_reference_hdr reference_hdr; 7132 struct vp8_entropy_hdr entropy_hdr; 7134 struct vp8_entropy_hdr saved_entropy; 7135 unsigned int saved_entropy_valid; 7137 unsigned int mb_rows; 7138 unsigned int mb_cols; 7139 struct mb_info *mb_info_storage; 7140 struct mb_info **mb_info_rows_storage; 7141 struct mb_info **mb_info_rows; 7143 token_entropy_ctx_t *above_token_entropy_ctx; 7144 struct token_decoder tokens[MAX_PARTITIONS]; 7145 struct dequant_factors dequant_factors[MAX_MB_SEGMENTS]; 7147 struct ref_cnt_img frame_strg[NUM_REF_FRAMES]; 7148 struct ref_cnt_img *ref_frames[NUM_REF_FRAMES]; 7149 ptrdiff_t ref_frame_offsets[4]; 7151 const filter_t *subpixel_filters; 7152 }; 7154 void 7155 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx); 7157 void 7158 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx); 7159 vpx_codec_err_t 7160 vp8_parse_frame_header(const unsigned char *data, 7161 unsigned int sz, 7162 struct vp8_frame_hdr *hdr); 7164 vpx_codec_err_t 7165 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 7166 const unsigned char *data, 7167 unsigned int sz); 7169 #define CLAMP_255(x) ((x)<0?0:((x)>255?255:(x))) 7171 #endif 7173 ---- End code block ---------------------------------------- 7175 20.6. dixie_loopfilter.c 7177 ---- Begin code block -------------------------------------- 7179 /* 7180 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7181 * 7182 * Use of this source code is governed by a BSD-style license 7183 * that can be found in the LICENSE file in the root of the source 7184 * tree. An additional intellectual property rights grant can be 7185 * found in the file PATENTS. All contributing project authors may 7186 * be found in the AUTHORS file in the root of the source tree. 7187 */ 7188 #include "dixie.h" 7189 #include "dixie_loopfilter.h" 7191 #define ABS(x) ((x) >= 0 ? (x) : -(x)) 7193 #define p3 pixels[-4*stride] 7194 #define p2 pixels[-3*stride] 7195 #define p1 pixels[-2*stride] 7196 #define p0 pixels[-1*stride] 7197 #define q0 pixels[ 0*stride] 7198 #define q1 pixels[ 1*stride] 7199 #define q2 pixels[ 2*stride] 7200 #define q3 pixels[ 3*stride] 7202 #define static 7203 static int 7204 saturate_int8(int x) 7205 { 7206 if (x < -128) 7207 return -128; 7209 if (x > 127) 7210 return 127; 7212 return x; 7213 } 7215 static int 7216 saturate_uint8(int x) 7217 { 7218 if (x < 0) 7219 return 0; 7221 if (x > 255) 7222 return 255; 7224 return x; 7225 } 7227 static int 7228 high_edge_variance(unsigned char *pixels, 7229 int stride, 7230 int hev_threshold) 7231 { 7232 return ABS(p1 - p0) > hev_threshold || 7233 ABS(q1 - q0) > hev_threshold; 7234 } 7236 static int 7237 simple_threshold(unsigned char *pixels, 7238 int stride, 7239 int filter_limit) 7240 { 7241 return (ABS(p0 - q0) * 2 + (ABS(p1 - q1) >> 1)) <= filter_limit; 7242 } 7244 static int 7245 normal_threshold(unsigned char *pixels, 7246 int stride, 7247 int edge_limit, 7248 int interior_limit) 7249 { 7250 int E = edge_limit; 7251 int I = interior_limit; 7253 return simple_threshold(pixels, stride, 2 * E + I) 7254 && ABS(p3 - p2) <= I && ABS(p2 - p1) <= I 7255 && ABS(p1 - p0) <= I && ABS(q3 - q2) <= I 7256 && ABS(q2 - q1) <= I && ABS(q1 - q0) <= I; 7257 } 7259 static void 7260 filter_common(unsigned char *pixels, 7261 int stride, 7262 int use_outer_taps) 7263 { 7264 int a, f1, f2; 7266 a = 3 * (q0 - p0); 7268 if (use_outer_taps) 7269 a += saturate_int8(p1 - q1); 7271 a = saturate_int8(a); 7273 f1 = ((a + 4 > 127) ? 127 : a + 4) >> 3; 7274 f2 = ((a + 3 > 127) ? 127 : a + 3) >> 3; 7276 p0 = saturate_uint8(p0 + f2); 7277 q0 = saturate_uint8(q0 - f1); 7279 if (!use_outer_taps) 7280 { 7281 /* This handles the case of subblock_filter() 7282 * (from the bitstream guide. 7283 */ 7284 a = (f1 + 1) >> 1; 7285 p1 = saturate_uint8(p1 + a); 7286 q1 = saturate_uint8(q1 - a); 7287 } 7288 } 7290 static void 7291 filter_mb_edge(unsigned char *pixels, 7292 int stride) 7293 { 7294 int w, a; 7296 w = saturate_int8(saturate_int8(p1 - q1) + 3 * (q0 - p0)); 7298 a = (27 * w + 63) >> 7; 7299 p0 = saturate_uint8(p0 + a); 7300 q0 = saturate_uint8(q0 - a); 7302 a = (18 * w + 63) >> 7; 7303 p1 = saturate_uint8(p1 + a); 7304 q1 = saturate_uint8(q1 - a); 7306 a = (9 * w + 63) >> 7; 7307 p2 = saturate_uint8(p2 + a); 7308 q2 = saturate_uint8(q2 - a); 7310 } 7312 static void 7313 filter_mb_v_edge(unsigned char *src, 7314 int stride, 7315 int edge_limit, 7316 int interior_limit, 7317 int hev_threshold, 7318 int size) 7319 { 7320 int i; 7322 for (i = 0; i < 8 * size; i++) 7323 { 7324 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7325 { 7326 if (high_edge_variance(src, 1, hev_threshold)) 7327 filter_common(src, 1, 1); 7328 else 7329 filter_mb_edge(src, 1); 7330 } 7332 src += stride; 7333 } 7334 } 7336 static void 7337 filter_subblock_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 filter_common(src, 1, 7350 high_edge_variance(src, 1, hev_threshold)); 7352 src += stride; 7353 } 7354 } 7356 static void 7357 filter_mb_h_edge(unsigned char *src, 7358 int stride, 7359 int edge_limit, 7360 int interior_limit, 7361 int hev_threshold, 7362 int size) 7363 { 7364 int i; 7366 for (i = 0; i < 8 * size; i++) 7367 { 7368 if (normal_threshold(src, stride, edge_limit, interior_limit)) 7369 { 7370 if (high_edge_variance(src, stride, hev_threshold)) 7371 filter_common(src, stride, 1); 7372 else 7373 filter_mb_edge(src, stride); 7374 } 7376 src += 1; 7377 } 7378 } 7380 static void 7381 filter_subblock_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 filter_common(src, stride, 7394 high_edge_variance(src, stride, 7395 hev_threshold)); 7397 src += 1; 7398 } 7399 } 7401 static void 7402 filter_v_edge_simple(unsigned char *src, 7403 int stride, 7404 int filter_limit) 7405 { 7406 int i; 7408 for (i = 0; i < 16; i++) 7409 { 7410 if (simple_threshold(src, 1, filter_limit)) 7411 filter_common(src, 1, 1); 7413 src += stride; 7414 } 7415 } 7417 static void 7418 filter_h_edge_simple(unsigned char *src, 7419 int stride, 7420 int filter_limit) 7421 { 7422 int i; 7424 for (i = 0; i < 16; i++) 7425 { 7426 if (simple_threshold(src, stride, filter_limit)) 7427 filter_common(src, stride, 1); 7429 src += 1; 7430 } 7431 } 7432 static void 7433 calculate_filter_parameters(struct vp8_decoder_ctx *ctx, 7434 struct mb_info *mbi, 7435 int *edge_limit_, 7436 int *interior_limit_, 7437 int *hev_threshold_) 7438 { 7439 int filter_level, interior_limit, hev_threshold; 7441 /* Reference code/spec seems to conflate filter_level and 7442 * edge_limit 7443 */ 7445 filter_level = ctx->loopfilter_hdr.level; 7447 if (ctx->segment_hdr.enabled) 7448 { 7449 if (!ctx->segment_hdr.abs) 7450 filter_level += 7451 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7452 else 7453 filter_level = 7454 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7455 } 7457 if (ctx->loopfilter_hdr.delta_enabled) 7458 { 7459 filter_level += 7460 ctx->loopfilter_hdr.ref_delta[mbi->base.ref_frame]; 7462 if (mbi->base.ref_frame == CURRENT_FRAME) 7463 { 7464 if (mbi->base.y_mode == B_PRED) 7465 filter_level += ctx->loopfilter_hdr.mode_delta[0]; 7466 } 7467 else if (mbi->base.y_mode == ZEROMV) 7468 filter_level += ctx->loopfilter_hdr.mode_delta[1]; 7469 else if (mbi->base.y_mode == SPLITMV) 7470 filter_level += ctx->loopfilter_hdr.mode_delta[3]; 7471 else 7472 filter_level += ctx->loopfilter_hdr.mode_delta[2]; 7473 } 7475 if (filter_level > 63) 7476 filter_level = 63; 7477 else if (filter_level < 0) 7478 filter_level = 0; 7480 interior_limit = filter_level; 7482 if (ctx->loopfilter_hdr.sharpness) 7483 { 7484 interior_limit >>= ctx->loopfilter_hdr.sharpness > 4 ? 2 : 1; 7486 if (interior_limit > 9 - ctx->loopfilter_hdr.sharpness) 7487 interior_limit = 9 - ctx->loopfilter_hdr.sharpness; 7488 } 7490 if (interior_limit < 1) 7491 interior_limit = 1; 7493 hev_threshold = (filter_level >= 15); 7495 if (filter_level >= 40) 7496 hev_threshold++; 7498 if (filter_level >= 20 && !ctx->frame_hdr.is_keyframe) 7499 hev_threshold++; 7501 *edge_limit_ = filter_level; 7502 *interior_limit_ = interior_limit; 7503 *hev_threshold_ = hev_threshold; 7504 } 7506 static void 7507 filter_row_normal(struct vp8_decoder_ctx *ctx, 7508 unsigned int row, 7509 unsigned int start_col, 7510 unsigned int num_cols) 7511 { 7512 unsigned char *y, *u, *v; 7513 int stride, uv_stride; 7514 struct mb_info *mbi; 7515 unsigned int col; 7517 /* Adjust pointers based on row, start_col */ 7518 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7519 uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 7520 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7521 u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 7522 v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 7523 y += (stride * row + start_col) * 16; 7524 u += (uv_stride * row + start_col) * 8; 7525 v += (uv_stride * row + start_col) * 8; 7526 mbi = ctx->mb_info_rows[row] + start_col; 7527 for (col = start_col; col < start_col + num_cols; col++) 7528 { 7529 int edge_limit, interior_limit, hev_threshold; 7531 /* TODO: only need to recalculate every MB if segmentation is 7532 * enabled. 7533 */ 7534 calculate_filter_parameters(ctx, mbi, &edge_limit, 7535 &interior_limit, &hev_threshold); 7537 if (edge_limit) 7538 { 7539 if (col) 7540 { 7541 filter_mb_v_edge(y, stride, edge_limit + 2, 7542 interior_limit, hev_threshold, 2); 7543 filter_mb_v_edge(u, uv_stride, edge_limit + 2, 7544 interior_limit, hev_threshold, 1); 7545 filter_mb_v_edge(v, uv_stride, edge_limit + 2, 7546 interior_limit, hev_threshold, 1); 7547 } 7549 /* NOTE: This conditional is actually dependent on the 7550 * number of coefficients decoded, not the skip flag as 7551 * coded in the bitstream. The tokens task is expected to 7552 * set 31 if there is *any* non-zero data. 7553 */ 7554 if (mbi->base.eob_mask 7555 || mbi->base.y_mode == SPLITMV 7556 || mbi->base.y_mode == B_PRED) 7557 { 7558 filter_subblock_v_edge(y + 4, stride, edge_limit, 7559 interior_limit, hev_threshold, 7560 2); 7561 filter_subblock_v_edge(y + 8, stride, edge_limit, 7562 interior_limit, hev_threshold, 7563 2); 7564 filter_subblock_v_edge(y + 12, stride, edge_limit, 7565 interior_limit, hev_threshold, 7566 2); 7567 filter_subblock_v_edge(u + 4, uv_stride, edge_limit, 7568 interior_limit, hev_threshold, 7569 1); 7570 filter_subblock_v_edge(v + 4, uv_stride, edge_limit, 7571 interior_limit, hev_threshold, 7572 1); 7573 } 7574 if (row) 7575 { 7576 filter_mb_h_edge(y, stride, edge_limit + 2, 7577 interior_limit, hev_threshold, 2); 7578 filter_mb_h_edge(u, uv_stride, edge_limit + 2, 7579 interior_limit, hev_threshold, 1); 7580 filter_mb_h_edge(v, uv_stride, edge_limit + 2, 7581 interior_limit, hev_threshold, 1); 7582 } 7584 if (mbi->base.eob_mask 7585 || mbi->base.y_mode == SPLITMV 7586 || mbi->base.y_mode == B_PRED) 7587 { 7588 filter_subblock_h_edge(y + 4 * stride, stride, 7589 edge_limit, interior_limit, 7590 hev_threshold, 2); 7591 filter_subblock_h_edge(y + 8 * stride, stride, 7592 edge_limit, interior_limit, 7593 hev_threshold, 2); 7594 filter_subblock_h_edge(y + 12 * stride, stride, 7595 edge_limit, interior_limit, 7596 hev_threshold, 2); 7597 filter_subblock_h_edge(u + 4 * uv_stride, uv_stride, 7598 edge_limit, interior_limit, 7599 hev_threshold, 1); 7600 filter_subblock_h_edge(v + 4 * uv_stride, uv_stride, 7601 edge_limit, interior_limit, 7602 hev_threshold, 1); 7603 } 7604 } 7606 y += 16; 7607 u += 8; 7608 v += 8; 7609 mbi++; 7610 } 7611 } 7613 static void 7614 filter_row_simple(struct vp8_decoder_ctx *ctx, 7615 unsigned int row, 7616 unsigned int start_col, 7617 unsigned int num_cols) 7618 { 7619 unsigned char *y; 7620 int stride; 7621 struct mb_info *mbi; 7622 unsigned int col; 7624 /* Adjust pointers based on row, start_col */ 7625 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7626 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7627 y += (stride * row + start_col) * 16; 7628 mbi = ctx->mb_info_rows[row] + start_col; 7630 for (col = start_col; col < start_col + num_cols; col++) 7631 { 7632 int edge_limit, interior_limit, hev_threshold; 7634 /* TODO: only need to recalculate every MB if segmentation is 7635 * enabled. 7636 */ 7637 calculate_filter_parameters(ctx, mbi, &edge_limit, 7638 &interior_limit, &hev_threshold); 7640 if (edge_limit) 7641 { 7643 /* NOTE: This conditional is actually dependent on the 7644 * number of coefficients decoded, not the skip flag as 7645 * coded in the bitstream. The tokens task is expected to 7646 * set 31 if there is *any* non-zero data. 7647 */ 7648 int filter_subblocks = (mbi->base.eob_mask 7649 || mbi->base.y_mode == SPLITMV 7650 || mbi->base.y_mode == B_PRED); 7651 int mb_limit = (edge_limit + 2) * 2 + interior_limit; 7652 int b_limit = edge_limit * 2 + interior_limit; 7654 if (col) 7655 filter_v_edge_simple(y, stride, mb_limit); 7657 if (filter_subblocks) 7658 { 7659 filter_v_edge_simple(y + 4, stride, b_limit); 7660 filter_v_edge_simple(y + 8, stride, b_limit); 7661 filter_v_edge_simple(y + 12, stride, b_limit); 7662 } 7664 if (row) 7665 filter_h_edge_simple(y, stride, mb_limit); 7667 if (filter_subblocks) 7668 { 7669 filter_h_edge_simple(y + 4 * stride, stride, b_limit); 7670 filter_h_edge_simple(y + 8 * stride, stride, b_limit); 7671 filter_h_edge_simple(y + 12 * stride, stride, b_limit); 7672 } 7673 } 7675 y += 16; 7676 mbi++; 7677 } 7678 } 7680 void 7681 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7682 unsigned int row, 7683 unsigned int start_col, 7684 unsigned int num_cols) 7685 { 7686 if (ctx->loopfilter_hdr.use_simple) 7687 filter_row_simple(ctx, row, start_col, num_cols); 7688 else 7689 filter_row_normal(ctx, row, start_col, num_cols); 7690 } 7692 ---- End code block ---------------------------------------- 7694 20.7. dixie_loopfilter.h 7696 ---- Begin code block -------------------------------------- 7698 /* 7699 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7700 * 7701 * Use of this source code is governed by a BSD-style license 7702 * that can be found in the LICENSE file in the root of the source 7703 * tree. An additional intellectual property rights grant can be 7704 * found in the file PATENTS. All contributing project authors may 7705 * be found in the AUTHORS file in the root of the source tree. 7706 */ 7707 #ifndef DIXIE_LOOPFILTER_H 7708 #define DIXIE_LOOPFILTER_H 7710 void 7711 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7712 unsigned int row, 7713 unsigned int start_col, 7714 unsigned int num_cols); 7716 #endif 7718 ---- End code block ---------------------------------------- 7720 20.8. idct_add.c 7722 ---- Begin code block -------------------------------------- 7724 /* 7725 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7726 * 7727 * Use of this source code is governed by a BSD-style license 7728 * that can be found in the LICENSE file in the root of the source 7729 * tree. An additional intellectual property rights grant can be 7730 * found in the file PATENTS. All contributing project authors may 7731 * be found in the AUTHORS file in the root of the source tree. 7732 */ 7733 #include "dixie.h" 7734 #include "idct_add.h" 7735 #include 7737 void 7738 vp8_dixie_walsh(const short *input, short *output) 7739 { 7740 int i; 7741 int a1, b1, c1, d1; 7742 int a2, b2, c2, d2; 7743 const short *ip = input; 7744 short *op = output; 7746 for (i = 0; i < 4; i++) 7747 { 7748 a1 = ip[0] + ip[12]; 7749 b1 = ip[4] + ip[8]; 7750 c1 = ip[4] - ip[8]; 7751 d1 = ip[0] - ip[12]; 7753 op[0] = a1 + b1; 7754 op[4] = c1 + d1; 7755 op[8] = a1 - b1; 7756 op[12] = d1 - c1; 7757 ip++; 7758 op++; 7759 } 7761 ip = output; 7762 op = output; 7764 for (i = 0; i < 4; i++) 7765 { 7766 a1 = ip[0] + ip[3]; 7767 b1 = ip[1] + ip[2]; 7768 c1 = ip[1] - ip[2]; 7769 d1 = ip[0] - ip[3]; 7771 a2 = a1 + b1; 7772 b2 = c1 + d1; 7773 c2 = a1 - b1; 7774 d2 = d1 - c1; 7776 op[0] = (a2 + 3) >> 3; 7777 op[1] = (b2 + 3) >> 3; 7778 op[2] = (c2 + 3) >> 3; 7779 op[3] = (d2 + 3) >> 3; 7781 ip += 4; 7782 op += 4; 7783 } 7784 } 7786 #define cospi8sqrt2minus1 20091 7787 #define sinpi8sqrt2 35468 7788 #define rounding 0 7789 static void 7790 idct_columns(const short *input, short *output) 7791 { 7792 int i; 7793 int a1, b1, c1, d1; 7795 const short *ip = input; 7796 short *op = output; 7797 int temp1, temp2; 7798 int shortpitch = 4; 7800 for (i = 0; i < 4; i++) 7801 { 7802 a1 = ip[0] + ip[8]; 7803 b1 = ip[0] - ip[8]; 7805 temp1 = (ip[4] * sinpi8sqrt2 + rounding) >> 16; 7806 temp2 = ip[12] + 7807 ((ip[12] * cospi8sqrt2minus1 + rounding) >> 16); 7808 c1 = temp1 - temp2; 7810 temp1 = ip[4] + 7811 ((ip[4] * cospi8sqrt2minus1 + rounding) >> 16); 7812 temp2 = (ip[12] * sinpi8sqrt2 + rounding) >> 16; 7813 d1 = temp1 + temp2; 7815 op[shortpitch*0] = a1 + d1; 7816 op[shortpitch*3] = a1 - d1; 7818 op[shortpitch*1] = b1 + c1; 7819 op[shortpitch*2] = b1 - c1; 7821 ip++; 7822 op++; 7823 } 7824 } 7826 void 7827 vp8_dixie_idct_add(unsigned char *recon, 7828 const unsigned char *predict, 7829 int stride, 7830 const short *coeffs) 7831 { 7832 int i; 7833 int a1, b1, c1, d1, temp1, temp2; 7834 short tmp[16]; 7835 idct_columns(coeffs, tmp); 7836 coeffs = tmp; 7838 for (i = 0; i < 4; i++) 7839 { 7840 a1 = coeffs[0] + coeffs[2]; 7841 b1 = coeffs[0] - coeffs[2]; 7843 temp1 = (coeffs[1] * sinpi8sqrt2 + rounding) >> 16; 7844 temp2 = coeffs[3] + 7845 ((coeffs[3] * cospi8sqrt2minus1 + rounding) >> 16); 7846 c1 = temp1 - temp2; 7848 temp1 = coeffs[1] + 7849 ((coeffs[1] * cospi8sqrt2minus1 + rounding) >> 16); 7850 temp2 = (coeffs[3] * sinpi8sqrt2 + rounding) >> 16; 7851 d1 = temp1 + temp2; 7853 recon[0] = CLAMP_255(predict[0] + ((a1 + d1 + 4) >> 3)); 7854 recon[3] = CLAMP_255(predict[3] + ((a1 - d1 + 4) >> 3)); 7855 recon[1] = CLAMP_255(predict[1] + ((b1 + c1 + 4) >> 3)); 7856 recon[2] = CLAMP_255(predict[2] + ((b1 - c1 + 4) >> 3)); 7858 coeffs += 4; 7859 recon += stride; 7860 predict += stride; 7861 } 7862 } 7864 ---- End code block ---------------------------------------- 7866 20.9. idct_add.h 7868 ---- Begin code block -------------------------------------- 7870 /* 7871 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7872 * 7873 * Use of this source code is governed by a BSD-style license 7874 * that can be found in the LICENSE file in the root of the source 7875 * tree. An additional intellectual property rights grant can be 7876 * found in the file PATENTS. All contributing project authors may 7877 * be found in the AUTHORS file in the root of the source tree. 7878 */ 7879 #ifndef IDCT_ADD_H 7880 #define IDCT_ADD_H 7882 void 7883 vp8_dixie_idct_add_init(struct vp8_decoder_ctx *ctx); 7885 void 7886 vp8_dixie_idct_add(unsigned char *recon, 7887 const unsigned char *predict, 7888 int stride, 7889 const short *coeffs); 7891 void 7892 vp8_dixie_walsh(const short *in, short *out); 7894 void 7895 vp8_dixie_idct_add_process_row(struct vp8_decoder_ctx *ctx, 7896 short *coeffs, 7897 unsigned int row, 7898 unsigned int start_col, 7899 unsigned int num_cols); 7901 #endif 7903 ---- End code block ---------------------------------------- 7905 20.10. mem.h 7907 ---- Begin code block -------------------------------------- 7909 /* 7910 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7911 * 7912 * Use of this source code is governed by a BSD-style license 7913 * that can be found in the LICENSE file in the root of the source 7914 * tree. An additional intellectual property rights grant can be 7915 * found in the file PATENTS. All contributing project authors may 7916 * be found in the AUTHORS file in the root of the source tree. 7917 */ 7919 #ifndef VPX_PORTS_MEM_H 7920 #define VPX_PORTS_MEM_H 7921 #include "vpx_config.h" 7922 #include "vpx_integer.h" 7924 #if defined(__GNUC__) && __GNUC__ 7925 #define DECLARE_ALIGNED(n,typ,val) typ val __attribute__ \ 7926 ((aligned (n))) 7927 #elif defined(_MSC_VER) 7928 #define DECLARE_ALIGNED(n,typ,val) __declspec(align(n)) typ val 7929 #else 7930 #warning No alignment directives known for this compiler. 7931 #define DECLARE_ALIGNED(n,typ,val) typ val 7932 #endif 7933 #endif 7935 /* Declare an aligned array on the stack, for situations where the 7936 * stack pointer may not have the alignment we expect. Creates an 7937 * array with a modified name, then defines val to be a pointer, and 7938 * aligns that pointer within the array. 7939 */ 7940 #define DECLARE_ALIGNED_ARRAY(a,typ,val,n)\ 7941 typ val##_[(n)+(a)/sizeof(typ)+1];\ 7942 typ *val = (typ*)((((intptr_t)val##_)+(a)-1)&((intptr_t)-(a))) 7944 /* Indicates that the usage of the specified variable has been 7945 * audited to assure that it's safe to use uninitialized. Silences 7946 * 'may be used uninitialized' warnings on gcc. 7947 */ 7948 #if defined(__GNUC__) && __GNUC__ 7949 #define UNINITIALIZED_IS_SAFE(x) x=x 7950 #else 7951 #define UNINITIALIZED_IS_SAFE(x) x 7952 #endif 7953 ---- End code block ---------------------------------------- 7955 20.11. modemv.c 7957 ---- Begin code block -------------------------------------- 7959 /* 7960 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7961 * 7962 * Use of this source code is governed by a BSD-style license 7963 * that can be found in the LICENSE file in the root of the source 7964 * tree. An additional intellectual property rights grant can be 7965 * found in the file PATENTS. All contributing project authors may 7966 * be found in the AUTHORS file in the root of the source tree. 7967 */ 7968 #include "dixie.h" 7969 #include "modemv_data.h" 7970 #include 7971 #include 7973 struct mv_clamp_rect 7974 { 7975 int to_left, to_right, to_top, to_bottom; 7976 }; 7978 static union mv 7979 clamp_mv(union mv raw, const struct mv_clamp_rect *bounds) 7980 { 7981 union mv newmv; 7983 newmv.d.x = (raw.d.x < bounds->to_left) 7984 ? bounds->to_left : raw.d.x; 7985 newmv.d.x = (raw.d.x > bounds->to_right) 7986 ? bounds->to_right : newmv.d.x; 7987 newmv.d.y = (raw.d.y < bounds->to_top) 7988 ? bounds->to_top : raw.d.y; 7989 newmv.d.y = (raw.d.y > bounds->to_bottom) 7990 ? bounds->to_bottom : newmv.d.y; 7991 return newmv; 7992 } 7994 static int 7995 read_segment_id(struct bool_decoder *bool, 7996 struct vp8_segment_hdr *seg) 7997 { 7998 return bool_get(bool, seg->tree_probs[0]) 7999 ? 2 + bool_get(bool, seg->tree_probs[2]) 8000 : bool_get(bool, seg->tree_probs[1]); 8001 } 8003 static enum prediction_mode 8004 above_block_mode(const struct mb_info *this, 8005 const struct mb_info *above, 8006 unsigned int b) 8007 { 8008 if (b < 4) 8009 { 8010 switch (above->base.y_mode) 8011 { 8012 case DC_PRED: 8013 return B_DC_PRED; 8014 case V_PRED: 8015 return B_VE_PRED; 8016 case H_PRED: 8017 return B_HE_PRED; 8018 case TM_PRED: 8019 return B_TM_PRED; 8020 case B_PRED: 8021 return above->split.modes[b+12]; 8022 default: 8023 assert(0); 8024 } 8025 } 8027 return this->split.modes[b-4]; 8028 } 8030 static enum prediction_mode 8031 left_block_mode(const struct mb_info *this, 8032 const struct mb_info *left, 8033 unsigned int b) 8034 { 8035 if (!(b & 3)) 8036 { 8037 switch (left->base.y_mode) 8038 { 8039 case DC_PRED: 8040 return B_DC_PRED; 8041 case V_PRED: 8042 return B_VE_PRED; 8043 case H_PRED: 8045 return B_HE_PRED; 8046 case TM_PRED: 8047 return B_TM_PRED; 8048 case B_PRED: 8049 return left->split.modes[b+3]; 8050 default: 8051 assert(0); 8052 } 8053 } 8055 return this->split.modes[b-1]; 8056 } 8058 static void 8059 decode_kf_mb_mode(struct mb_info *this, 8060 struct mb_info *left, 8061 struct mb_info *above, 8062 struct bool_decoder *bool) 8063 { 8064 int y_mode, uv_mode; 8066 y_mode = bool_read_tree(bool, kf_y_mode_tree, kf_y_mode_probs); 8068 if (y_mode == B_PRED) 8069 { 8070 unsigned int i; 8072 for (i = 0; i < 16; i++) 8073 { 8074 enum prediction_mode a = above_block_mode(this, above, i); 8075 enum prediction_mode l = left_block_mode(this, left, i); 8076 enum prediction_mode b; 8078 b = bool_read_tree(bool, b_mode_tree, 8079 kf_b_mode_probs[a][l]); 8080 this->split.modes[i] = b; 8081 } 8082 } 8084 uv_mode = bool_read_tree(bool, uv_mode_tree, kf_uv_mode_probs); 8086 this->base.y_mode = y_mode; 8087 this->base.uv_mode = uv_mode; 8088 this->base.mv.raw = 0; 8089 this->base.ref_frame = 0; 8090 } 8091 static void 8092 decode_intra_mb_mode(struct mb_info *this, 8093 struct vp8_entropy_hdr *hdr, 8094 struct bool_decoder *bool) 8095 { 8096 /* Like decode_kf_mb_mode, but with probabilities transmitted in the 8097 * bitstream and no context on the above/left block mode. 8098 */ 8099 int y_mode, uv_mode; 8101 y_mode = bool_read_tree(bool, y_mode_tree, hdr->y_mode_probs); 8103 if (y_mode == B_PRED) 8104 { 8105 unsigned int i; 8107 for (i = 0; i < 16; i++) 8108 { 8109 enum prediction_mode b; 8111 b = bool_read_tree(bool, b_mode_tree, default_b_mode_probs); 8112 this->split.modes[i] = b; 8113 } 8114 } 8116 uv_mode = bool_read_tree(bool, uv_mode_tree, hdr->uv_mode_probs); 8118 this->base.y_mode = y_mode; 8119 this->base.uv_mode = uv_mode; 8120 this->base.mv.raw = 0; 8121 this->base.ref_frame = CURRENT_FRAME; 8122 } 8124 static int 8125 read_mv_component(struct bool_decoder *bool, 8126 const unsigned char mvc[MV_PROB_CNT]) 8127 { 8128 enum {IS_SHORT, SIGN, SHORT, BITS = SHORT + 8 - 1, LONG_WIDTH = 10}; 8129 int x = 0; 8131 if (bool_get(bool, mvc[IS_SHORT])) /* Large */ 8132 { 8133 int i = 0; 8135 for (i = 0; i < 3; i++) 8136 x += bool_get(bool, mvc[BITS + i]) << i; 8138 /* Skip bit 3, which is sometimes implicit */ 8139 for (i = LONG_WIDTH - 1; i > 3; i--) 8140 x += bool_get(bool, mvc[BITS + i]) << i; 8142 if (!(x & 0xFFF0) || bool_get(bool, mvc[BITS + 3])) 8143 x += 8; 8144 } 8145 else /* small */ 8146 x = bool_read_tree(bool, small_mv_tree, mvc + SHORT); 8148 if (x && bool_get(bool, mvc[SIGN])) 8149 x = -x; 8151 return x << 1; 8152 } 8154 static mv_t 8155 above_block_mv(const struct mb_info *this, 8156 const struct mb_info *above, 8157 unsigned int b) 8158 { 8159 if (b < 4) 8160 { 8161 if (above->base.y_mode == SPLITMV) 8162 return above->split.mvs[b+12]; 8164 return above->base.mv; 8165 } 8167 return this->split.mvs[b-4]; 8168 } 8170 static mv_t 8171 left_block_mv(const struct mb_info *this, 8172 const struct mb_info *left, 8173 unsigned int b) 8174 { 8175 if (!(b & 3)) 8176 { 8177 if (left->base.y_mode == SPLITMV) 8178 return left->split.mvs[b+3]; 8180 return left->base.mv; 8181 } 8183 return this->split.mvs[b-1]; 8185 } 8187 static enum prediction_mode 8188 submv_ref(struct bool_decoder *bool, union mv l, union mv a) 8189 { 8190 enum subblock_mv_ref 8191 { 8192 SUBMVREF_NORMAL, 8193 SUBMVREF_LEFT_ZED, 8194 SUBMVREF_ABOVE_ZED, 8195 SUBMVREF_LEFT_ABOVE_SAME, 8196 SUBMVREF_LEFT_ABOVE_ZED 8197 }; 8199 int lez = !(l.raw); 8200 int aez = !(a.raw); 8201 int lea = l.raw == a.raw; 8202 enum subblock_mv_ref ctx = SUBMVREF_NORMAL; 8204 if (lea && lez) 8205 ctx = SUBMVREF_LEFT_ABOVE_ZED; 8206 else if (lea) 8207 ctx = SUBMVREF_LEFT_ABOVE_SAME; 8208 else if (aez) 8209 ctx = SUBMVREF_ABOVE_ZED; 8210 else if (lez) 8211 ctx = SUBMVREF_LEFT_ZED; 8213 return bool_read_tree(bool, submv_ref_tree, submv_ref_probs2[ctx]); 8214 } 8216 static void 8217 read_mv(struct bool_decoder *bool, 8218 union mv *mv, 8219 mv_component_probs_t mvc[2]) 8220 { 8221 mv->d.y = read_mv_component(bool, mvc[0]); 8222 mv->d.x = read_mv_component(bool, mvc[1]); 8223 } 8225 static void 8226 mv_bias(const struct mb_info *mb, 8227 const unsigned int sign_bias[3], 8228 enum reference_frame ref_frame, 8229 union mv *mv) 8231 { 8232 if (sign_bias[mb->base.ref_frame] ^ sign_bias[ref_frame]) 8233 { 8234 mv->d.x *= -1; 8235 mv->d.y *= -1; 8236 } 8237 } 8239 enum near_mv_v 8240 { 8241 CNT_BEST = 0, 8242 CNT_ZEROZERO = 0, 8243 CNT_NEAREST, 8244 CNT_NEAR, 8245 CNT_SPLITMV 8246 }; 8248 static void 8249 find_near_mvs(const struct mb_info *this, 8250 const struct mb_info *left, 8251 const struct mb_info *above, 8252 const unsigned int sign_bias[3], 8253 union mv near_mvs[4], 8254 int cnt[4]) 8255 { 8256 const struct mb_info *aboveleft = above - 1; 8257 union mv *mv = near_mvs; 8258 int *cntx = cnt; 8260 /* Zero accumulators */ 8261 mv[0].raw = mv[1].raw = mv[2].raw = 0; 8262 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 8264 /* Process above */ 8265 if (above->base.ref_frame != CURRENT_FRAME) 8266 { 8267 if (above->base.mv.raw) 8268 { 8269 (++mv)->raw = above->base.mv.raw; 8270 mv_bias(above, sign_bias, this->base.ref_frame, mv); 8271 ++cntx; 8272 } 8274 *cntx += 2; 8275 } 8276 /* Process left */ 8277 if (left->base.ref_frame != CURRENT_FRAME) 8278 { 8279 if (left->base.mv.raw) 8280 { 8281 union mv this_mv; 8283 this_mv.raw = left->base.mv.raw; 8284 mv_bias(left, sign_bias, this->base.ref_frame, &this_mv); 8286 if (this_mv.raw != mv->raw) 8287 { 8288 (++mv)->raw = this_mv.raw; 8289 ++cntx; 8290 } 8292 *cntx += 2; 8293 } 8294 else 8295 cnt[CNT_ZEROZERO] += 2; 8296 } 8298 /* Process above left */ 8299 if (aboveleft->base.ref_frame != CURRENT_FRAME) 8300 { 8301 if (aboveleft->base.mv.raw) 8302 { 8303 union mv this_mv; 8305 this_mv.raw = aboveleft->base.mv.raw; 8306 mv_bias(aboveleft, sign_bias, this->base.ref_frame, 8307 &this_mv); 8309 if (this_mv.raw != mv->raw) 8310 { 8311 (++mv)->raw = this_mv.raw; 8312 ++cntx; 8313 } 8315 *cntx += 1; 8316 } 8317 else 8318 cnt[CNT_ZEROZERO] += 1; 8319 } 8321 /* If we have three distinct MV's ... */ 8322 if (cnt[CNT_SPLITMV]) 8323 { 8324 /* See if above-left MV can be merged with NEAREST */ 8325 if (mv->raw == near_mvs[CNT_NEAREST].raw) 8326 cnt[CNT_NEAREST] += 1; 8327 } 8329 cnt[CNT_SPLITMV] = ((above->base.y_mode == SPLITMV) 8330 + (left->base.y_mode == SPLITMV)) * 2 8331 + (aboveleft->base.y_mode == SPLITMV); 8333 /* Swap near and nearest if necessary */ 8334 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) 8335 { 8336 int tmp; 8337 tmp = cnt[CNT_NEAREST]; 8338 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 8339 cnt[CNT_NEAR] = tmp; 8340 tmp = near_mvs[CNT_NEAREST].raw; 8341 near_mvs[CNT_NEAREST].raw = near_mvs[CNT_NEAR].raw; 8342 near_mvs[CNT_NEAR].raw = tmp; 8343 } 8345 /* Use near_mvs[CNT_BEST] to store the "best" MV. Note that this 8346 * storage shares the same address as near_mvs[CNT_ZEROZERO]. 8347 */ 8348 if (cnt[CNT_NEAREST] >= cnt[CNT_BEST]) 8349 near_mvs[CNT_BEST] = near_mvs[CNT_NEAREST]; 8350 } 8352 static void 8353 decode_split_mv(struct mb_info *this, 8354 const struct mb_info *left, 8355 const struct mb_info *above, 8356 struct vp8_entropy_hdr *hdr, 8357 union mv *best_mv, 8358 struct bool_decoder *bool) 8359 { 8360 const int *partition; 8361 int j, k, mask, partition_id; 8363 partition_id = bool_read_tree(bool, split_mv_tree, split_mv_probs); 8364 partition = mv_partitions[partition_id]; 8365 this->base.partitioning = partition_id; 8367 for (j = 0, mask = 0; mask < 65535; j++) 8368 { 8369 union mv mv, left_mv, above_mv; 8370 enum prediction_mode subblock_mode; 8371 /* Find the first subblock in this partition. */ 8372 for (k = 0; j != partition[k]; k++); 8374 /* Decode the next MV */ 8375 left_mv = left_block_mv(this, left, k); 8376 above_mv = above_block_mv(this, above, k); 8377 subblock_mode = submv_ref(bool, left_mv, above_mv); 8379 switch (subblock_mode) 8380 { 8381 case LEFT4X4: 8382 mv = left_mv; 8383 break; 8384 case ABOVE4X4: 8385 mv = above_mv; 8386 break; 8387 case ZERO4X4: 8388 mv.raw = 0; 8389 break; 8390 case NEW4X4: 8391 read_mv(bool, &mv, hdr->mv_probs); 8392 mv.d.x += best_mv->d.x; 8393 mv.d.y += best_mv->d.y; 8394 break; 8395 default: 8396 assert(0); 8397 } 8399 /* Fill the MV's for this partition */ 8400 for (; k < 16; k++) 8401 if (j == partition[k]) 8402 { 8403 this->split.mvs[k] = mv; 8404 mask |= 1 << k; 8405 } 8406 } 8407 } 8409 static int 8410 need_mc_border(union mv mv, int l, int t, int b_w, int w, int h) 8411 { 8412 int b, r; 8414 /* Get distance to edge for top-left pixel */ 8415 l += (mv.d.x >> 3); 8416 t += (mv.d.y >> 3); 8417 /* Get distance to edge for bottom-right pixel */ 8418 r = w - (l + b_w); 8419 b = h - (t + b_w); 8421 return (l >> 1 < 2 || r >> 1 < 3 || t >> 1 < 2 || b >> 1 < 3); 8422 } 8424 static void 8425 decode_mvs(struct vp8_decoder_ctx *ctx, 8426 struct mb_info *this, 8427 const struct mb_info *left, 8428 const struct mb_info *above, 8429 const struct mv_clamp_rect *bounds, 8430 struct bool_decoder *bool) 8431 { 8432 struct vp8_entropy_hdr *hdr = &ctx->entropy_hdr; 8433 union mv near_mvs[4]; 8434 union mv clamped_best_mv; 8435 int mv_cnts[4]; 8436 unsigned char probs[4]; 8437 enum {BEST, NEAREST, NEAR}; 8438 int x, y, w, h, b; 8440 this->base.ref_frame = bool_get(bool, hdr->prob_last) 8441 ? 2 + bool_get(bool, hdr->prob_gf) 8442 : 1; 8444 find_near_mvs(this, this - 1, above, ctx->reference_hdr.sign_bias, 8445 near_mvs, mv_cnts); 8446 probs[0] = mv_counts_to_probs[mv_cnts[0]][0]; 8447 probs[1] = mv_counts_to_probs[mv_cnts[1]][1]; 8448 probs[2] = mv_counts_to_probs[mv_cnts[2]][2]; 8449 probs[3] = mv_counts_to_probs[mv_cnts[3]][3]; 8451 this->base.y_mode = bool_read_tree(bool, mv_ref_tree, probs); 8452 this->base.uv_mode = this->base.y_mode; 8454 this->base.need_mc_border = 0; 8455 x = (-bounds->to_left - 128) >> 3; 8456 y = (-bounds->to_top - 128) >> 3; 8457 w = ctx->mb_cols * 16; 8458 h = ctx->mb_rows * 16; 8460 switch (this->base.y_mode) 8461 { 8462 case NEARESTMV: 8463 this->base.mv = clamp_mv(near_mvs[NEAREST], bounds); 8464 break; 8466 case NEARMV: 8467 this->base.mv = clamp_mv(near_mvs[NEAR], bounds); 8468 break; 8469 case ZEROMV: 8470 this->base.mv.raw = 0; 8471 return; //skip need_mc_border check 8472 case NEWMV: 8473 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8474 read_mv(bool, &this->base.mv, hdr->mv_probs); 8475 this->base.mv.d.x += clamped_best_mv.d.x; 8476 this->base.mv.d.y += clamped_best_mv.d.y; 8477 break; 8478 case SPLITMV: 8479 { 8480 union mv chroma_mv[4] = {{{0}}}; 8482 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8483 decode_split_mv(this, left, above, hdr, &clamped_best_mv, bool); 8484 this->base.mv = this->split.mvs[15]; 8486 for (b = 0; b < 16; b++) 8487 { 8488 chroma_mv[(b>>1&1) + (b>>2&2)].d.x += 8489 this->split.mvs[b].d.x; 8490 chroma_mv[(b>>1&1) + (b>>2&2)].d.y += 8491 this->split.mvs[b].d.y; 8493 if (need_mc_border(this->split.mvs[b], 8494 x + (b & 3) * 4, y + (b & ~3), 4, w, h)) 8495 { 8496 this->base.need_mc_border = 1; 8497 break; 8498 } 8499 } 8501 for (b = 0; b < 4; b++) 8502 { 8503 chroma_mv[b].d.x += 4 + 8 * (chroma_mv[b].d.x >> 31); 8504 chroma_mv[b].d.y += 4 + 8 * (chroma_mv[b].d.y >> 31); 8505 chroma_mv[b].d.x /= 4; 8506 chroma_mv[b].d.y /= 4; 8508 //note we're passing in non-subsampled coordinates 8509 if (need_mc_border(chroma_mv[b], 8510 x + (b & 1) * 8, y + (b >> 1) * 8, 16, w, h)) 8511 { 8512 this->base.need_mc_border = 1; 8513 break; 8515 } 8516 } 8518 return; //skip need_mc_border check 8519 } 8520 default: 8521 assert(0); 8522 } 8524 if (need_mc_border(this->base.mv, x, y, 16, w, h)) 8525 this->base.need_mc_border = 1; 8526 } 8528 void 8529 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8530 struct bool_decoder *bool, 8531 int row, 8532 int start_col, 8533 int num_cols) 8534 { 8535 struct mb_info *above, *this; 8536 unsigned int col; 8537 struct mv_clamp_rect bounds; 8539 this = ctx->mb_info_rows[row] + start_col; 8540 above = ctx->mb_info_rows[row - 1] + start_col; 8542 /* Calculate the eighth-pel MV bounds using a 1 MB border. */ 8543 bounds.to_left = -((start_col + 1) << 7); 8544 bounds.to_right = (ctx->mb_cols - start_col) << 7; 8545 bounds.to_top = -((row + 1) << 7); 8546 bounds.to_bottom = (ctx->mb_rows - row) << 7; 8548 for (col = start_col; col < start_col + num_cols; col++) 8549 { 8550 if (ctx->segment_hdr.update_map) 8551 this->base.segment_id = read_segment_id(bool, 8552 &ctx->segment_hdr); 8554 if (ctx->entropy_hdr.coeff_skip_enabled) 8555 this->base.skip_coeff = bool_get(bool, 8556 ctx->entropy_hdr.coeff_skip_prob); 8558 if (ctx->frame_hdr.is_keyframe) 8559 { 8560 if (!ctx->segment_hdr.update_map) 8561 this->base.segment_id = 0; 8563 decode_kf_mb_mode(this, this - 1, above, bool); 8564 } 8565 else 8566 { 8567 if (bool_get(bool, ctx->entropy_hdr.prob_inter)) 8568 decode_mvs(ctx, this, this - 1, above, &bounds, bool); 8569 else 8570 decode_intra_mb_mode(this, &ctx->entropy_hdr, bool); 8572 bounds.to_left -= 16 << 3; 8573 bounds.to_right -= 16 << 3; 8574 } 8576 /* Advance to next mb */ 8577 this++; 8578 above++; 8579 } 8580 } 8582 void 8583 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx) 8584 { 8585 unsigned int mbi_w, mbi_h, i; 8586 struct mb_info *mbi; 8588 mbi_w = ctx->mb_cols + 1; /* For left border col */ 8589 mbi_h = ctx->mb_rows + 1; /* For above border row */ 8591 if (ctx->frame_hdr.frame_size_updated) 8592 { 8593 free(ctx->mb_info_storage); 8594 ctx->mb_info_storage = NULL; 8595 free(ctx->mb_info_rows_storage); 8596 ctx->mb_info_rows_storage = NULL; 8597 } 8599 if (!ctx->mb_info_storage) 8600 ctx->mb_info_storage = calloc(mbi_w * mbi_h, 8601 sizeof(*ctx->mb_info_storage)); 8603 if (!ctx->mb_info_rows_storage) 8604 ctx->mb_info_rows_storage = calloc(mbi_h, 8605 sizeof(*ctx->mb_info_rows_storage)); 8607 /* Set up row pointers */ 8608 mbi = ctx->mb_info_storage + 1; 8609 for (i = 0; i < mbi_h; i++) 8610 { 8611 ctx->mb_info_rows_storage[i] = mbi; 8612 mbi += mbi_w; 8613 } 8615 ctx->mb_info_rows = ctx->mb_info_rows_storage + 1; 8616 } 8618 void 8619 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx) 8620 { 8621 free(ctx->mb_info_storage); 8622 ctx->mb_info_storage = NULL; 8623 free(ctx->mb_info_rows_storage); 8624 ctx->mb_info_rows_storage = NULL; 8625 } 8627 ---- End code block ---------------------------------------- 8628 20.12. modemv.h 8630 ---- Begin code block -------------------------------------- 8632 /* 8633 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8634 * 8635 * Use of this source code is governed by a BSD-style license 8636 * that can be found in the LICENSE file in the root of the source 8637 * tree. An additional intellectual property rights grant can be 8638 * found in the file PATENTS. All contributing project authors may 8639 * be found in the AUTHORS file in the root of the source tree. 8640 */ 8641 #ifndef MODEMV_H 8642 #define MODEMV_H 8644 void 8645 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx); 8647 void 8648 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx); 8650 void 8651 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8652 struct bool_decoder *bool, 8653 int row, 8654 int start_col, 8655 int num_cols); 8657 #endif 8659 ---- End code block ---------------------------------------- 8661 20.13. modemv_data.h 8663 ---- Begin code block -------------------------------------- 8665 static const unsigned char kf_y_mode_probs[] = { 145, 156, 163, 128}; 8666 static const unsigned char kf_uv_mode_probs[] = { 142, 114, 183}; 8667 static const unsigned char kf_b_mode_probs[10][10][9] = 8668 { 8669 { /* above mode 0 */ 8670 { /* left mode 0 */ 231, 120, 48, 89, 115, 113, 120, 152, 112}, 8671 { /* left mode 1 */ 152, 179, 64, 126, 170, 118, 46, 70, 95}, 8672 { /* left mode 2 */ 175, 69, 143, 80, 85, 82, 72, 155, 103}, 8673 { /* left mode 3 */ 56, 58, 10, 171, 218, 189, 17, 13, 152}, 8674 { /* left mode 4 */ 144, 71, 10, 38, 171, 213, 144, 34, 26}, 8675 { /* left mode 5 */ 114, 26, 17, 163, 44, 195, 21, 10, 173}, 8676 { /* left mode 6 */ 121, 24, 80, 195, 26, 62, 44, 64, 85}, 8677 { /* left mode 7 */ 170, 46, 55, 19, 136, 160, 33, 206, 71}, 8678 { /* left mode 8 */ 63, 20, 8, 114, 114, 208, 12, 9, 226}, 8679 { /* left mode 9 */ 81, 40, 11, 96, 182, 84, 29, 16, 36} 8680 }, 8681 { /* above mode 1 */ 8682 { /* left mode 0 */ 134, 183, 89, 137, 98, 101, 106, 165, 148}, 8683 { /* left mode 1 */ 72, 187, 100, 130, 157, 111, 32, 75, 80}, 8684 { /* left mode 2 */ 66, 102, 167, 99, 74, 62, 40, 234, 128}, 8685 { /* left mode 3 */ 41, 53, 9, 178, 241, 141, 26, 8, 107}, 8686 { /* left mode 4 */ 104, 79, 12, 27, 217, 255, 87, 17, 7}, 8687 { /* left mode 5 */ 74, 43, 26, 146, 73, 166, 49, 23, 157}, 8688 { /* left mode 6 */ 65, 38, 105, 160, 51, 52, 31, 115, 128}, 8689 { /* left mode 7 */ 87, 68, 71, 44, 114, 51, 15, 186, 23}, 8690 { /* left mode 8 */ 47, 41, 14, 110, 182, 183, 21, 17, 194}, 8691 { /* left mode 9 */ 66, 45, 25, 102, 197, 189, 23, 18, 22} 8692 }, 8693 { /* above mode 2 */ 8694 { /* left mode 0 */ 88, 88, 147, 150, 42, 46, 45, 196, 205}, 8695 { /* left mode 1 */ 43, 97, 183, 117, 85, 38, 35, 179, 61}, 8696 { /* left mode 2 */ 39, 53, 200, 87, 26, 21, 43, 232, 171}, 8697 { /* left mode 3 */ 56, 34, 51, 104, 114, 102, 29, 93, 77}, 8698 { /* left mode 4 */ 107, 54, 32, 26, 51, 1, 81, 43, 31}, 8699 { /* left mode 5 */ 39, 28, 85, 171, 58, 165, 90, 98, 64}, 8700 { /* left mode 6 */ 34, 22, 116, 206, 23, 34, 43, 166, 73}, 8701 { /* left mode 7 */ 68, 25, 106, 22, 64, 171, 36, 225, 114}, 8702 { /* left mode 8 */ 34, 19, 21, 102, 132, 188, 16, 76, 124}, 8703 { /* left mode 9 */ 62, 18, 78, 95, 85, 57, 50, 48, 51} 8704 }, 8705 { /* above mode 3 */ 8706 { /* left mode 0 */ 193, 101, 35, 159, 215, 111, 89, 46, 111}, 8707 { /* left mode 1 */ 60, 148, 31, 172, 219, 228, 21, 18, 111}, 8708 { /* left mode 2 */ 112, 113, 77, 85, 179, 255, 38, 120, 114}, 8709 { /* left mode 3 */ 40, 42, 1, 196, 245, 209, 10, 25, 109}, 8710 { /* left mode 4 */ 100, 80, 8, 43, 154, 1, 51, 26, 71}, 8711 { /* left mode 5 */ 88, 43, 29, 140, 166, 213, 37, 43, 154}, 8712 { /* left mode 6 */ 61, 63, 30, 155, 67, 45, 68, 1, 209}, 8713 { /* left mode 7 */ 142, 78, 78, 16, 255, 128, 34, 197, 171}, 8714 { /* left mode 8 */ 41, 40, 5, 102, 211, 183, 4, 1, 221}, 8715 { /* left mode 9 */ 51, 50, 17, 168, 209, 192, 23, 25, 82} 8716 }, 8717 { /* above mode 4 */ 8718 { /* left mode 0 */ 125, 98, 42, 88, 104, 85, 117, 175, 82}, 8719 { /* left mode 1 */ 95, 84, 53, 89, 128, 100, 113, 101, 45}, 8720 { /* left mode 2 */ 75, 79, 123, 47, 51, 128, 81, 171, 1}, 8721 { /* left mode 3 */ 57, 17, 5, 71, 102, 57, 53, 41, 49}, 8722 { /* left mode 4 */ 115, 21, 2, 10, 102, 255, 166, 23, 6}, 8723 { /* left mode 5 */ 38, 33, 13, 121, 57, 73, 26, 1, 85}, 8724 { /* left mode 6 */ 41, 10, 67, 138, 77, 110, 90, 47, 114}, 8725 { /* left mode 7 */ 101, 29, 16, 10, 85, 128, 101, 196, 26}, 8726 { /* left mode 8 */ 57, 18, 10, 102, 102, 213, 34, 20, 43}, 8727 { /* left mode 9 */ 117, 20, 15, 36, 163, 128, 68, 1, 26} 8728 }, 8729 { /* above mode 5 */ 8730 { /* left mode 0 */ 138, 31, 36, 171, 27, 166, 38, 44, 229}, 8731 { /* left mode 1 */ 67, 87, 58, 169, 82, 115, 26, 59, 179}, 8732 { /* left mode 2 */ 63, 59, 90, 180, 59, 166, 93, 73, 154}, 8733 { /* left mode 3 */ 40, 40, 21, 116, 143, 209, 34, 39, 175}, 8734 { /* left mode 4 */ 57, 46, 22, 24, 128, 1, 54, 17, 37}, 8735 { /* left mode 5 */ 47, 15, 16, 183, 34, 223, 49, 45, 183}, 8736 { /* left mode 6 */ 46, 17, 33, 183, 6, 98, 15, 32, 183}, 8737 { /* left mode 7 */ 65, 32, 73, 115, 28, 128, 23, 128, 205}, 8738 { /* left mode 8 */ 40, 3, 9, 115, 51, 192, 18, 6, 223}, 8739 { /* left mode 9 */ 87, 37, 9, 115, 59, 77, 64, 21, 47} 8740 }, 8741 { /* above mode 6 */ 8742 { /* left mode 0 */ 104, 55, 44, 218, 9, 54, 53, 130, 226}, 8743 { /* left mode 1 */ 64, 90, 70, 205, 40, 41, 23, 26, 57}, 8744 { /* left mode 2 */ 54, 57, 112, 184, 5, 41, 38, 166, 213}, 8745 { /* left mode 3 */ 30, 34, 26, 133, 152, 116, 10, 32, 134}, 8746 { /* left mode 4 */ 75, 32, 12, 51, 192, 255, 160, 43, 51}, 8747 { /* left mode 5 */ 39, 19, 53, 221, 26, 114, 32, 73, 255}, 8748 { /* left mode 6 */ 31, 9, 65, 234, 2, 15, 1, 118, 73}, 8749 { /* left mode 7 */ 88, 31, 35, 67, 102, 85, 55, 186, 85}, 8750 { /* left mode 8 */ 56, 21, 23, 111, 59, 205, 45, 37, 192}, 8751 { /* left mode 9 */ 55, 38, 70, 124, 73, 102, 1, 34, 98} 8752 }, 8753 { /* above mode 7 */ 8754 { /* left mode 0 */ 102, 61, 71, 37, 34, 53, 31, 243, 192}, 8755 { /* left mode 1 */ 69, 60, 71, 38, 73, 119, 28, 222, 37}, 8756 { /* left mode 2 */ 68, 45, 128, 34, 1, 47, 11, 245, 171}, 8757 { /* left mode 3 */ 62, 17, 19, 70, 146, 85, 55, 62, 70}, 8758 { /* left mode 4 */ 75, 15, 9, 9, 64, 255, 184, 119, 16}, 8759 { /* left mode 5 */ 37, 43, 37, 154, 100, 163, 85, 160, 1}, 8760 { /* left mode 6 */ 63, 9, 92, 136, 28, 64, 32, 201, 85}, 8761 { /* left mode 7 */ 86, 6, 28, 5, 64, 255, 25, 248, 1}, 8762 { /* left mode 8 */ 56, 8, 17, 132, 137, 255, 55, 116, 128}, 8763 { /* left mode 9 */ 58, 15, 20, 82, 135, 57, 26, 121, 40} 8764 }, 8765 { /* above mode 8 */ 8766 { /* left mode 0 */ 164, 50, 31, 137, 154, 133, 25, 35, 218}, 8767 { /* left mode 1 */ 51, 103, 44, 131, 131, 123, 31, 6, 158}, 8768 { /* left mode 2 */ 86, 40, 64, 135, 148, 224, 45, 183, 128}, 8769 { /* left mode 3 */ 22, 26, 17, 131, 240, 154, 14, 1, 209}, 8770 { /* left mode 4 */ 83, 12, 13, 54, 192, 255, 68, 47, 28}, 8771 { /* left mode 5 */ 45, 16, 21, 91, 64, 222, 7, 1, 197}, 8772 { /* left mode 6 */ 56, 21, 39, 155, 60, 138, 23, 102, 213}, 8773 { /* left mode 7 */ 85, 26, 85, 85, 128, 128, 32, 146, 171}, 8774 { /* left mode 8 */ 18, 11, 7, 63, 144, 171, 4, 4, 246}, 8775 { /* left mode 9 */ 35, 27, 10, 146, 174, 171, 12, 26, 128} 8776 }, 8777 { /* above mode 9 */ 8778 { /* left mode 0 */ 190, 80, 35, 99, 180, 80, 126, 54, 45}, 8779 { /* left mode 1 */ 85, 126, 47, 87, 176, 51, 41, 20, 32}, 8780 { /* left mode 2 */ 101, 75, 128, 139, 118, 146, 116, 128, 85}, 8781 { /* left mode 3 */ 56, 41, 15, 176, 236, 85, 37, 9, 62}, 8782 { /* left mode 4 */ 146, 36, 19, 30, 171, 255, 97, 27, 20}, 8783 { /* left mode 5 */ 71, 30, 17, 119, 118, 255, 17, 18, 138}, 8784 { /* left mode 6 */ 101, 38, 60, 138, 55, 70, 43, 26, 142}, 8785 { /* left mode 7 */ 138, 45, 61, 62, 219, 1, 81, 188, 64}, 8786 { /* left mode 8 */ 32, 41, 20, 117, 151, 142, 20, 21, 163}, 8787 { /* left mode 9 */ 112, 19, 12, 61, 195, 128, 48, 4, 24} 8788 } 8789 }; 8790 static const int kf_y_mode_tree[] = 8791 { 8792 -B_PRED, 2, 8793 4, 6, 8794 -DC_PRED, -V_PRED, 8795 -H_PRED, -TM_PRED 8796 }; 8797 static const int y_mode_tree[] = 8798 { 8799 -DC_PRED, 2, 8800 4, 6, 8801 -V_PRED, -H_PRED, 8802 -TM_PRED, -B_PRED 8803 }; 8804 static const int uv_mode_tree[6] = 8805 { 8806 -DC_PRED, 2, 8807 -V_PRED, 4, 8808 -H_PRED, -TM_PRED 8809 }; 8810 static const int b_mode_tree[18] = 8811 { 8812 -B_DC_PRED, 2, /* 0 = DC_NODE */ 8813 -B_TM_PRED, 4, /* 1 = TM_NODE */ 8814 -B_VE_PRED, 6, /* 2 = VE_NODE */ 8815 8, 12, /* 3 = COM_NODE */ 8816 -B_HE_PRED, 10, /* 4 = HE_NODE */ 8817 -B_RD_PRED, -B_VR_PRED, /* 5 = RD_NODE */ 8818 -B_LD_PRED, 14, /* 6 = LD_NODE */ 8819 -B_VL_PRED, 16, /* 7 = VL_NODE */ 8820 -B_HD_PRED, -B_HU_PRED /* 8 = HD_NODE */ 8821 }; 8822 static const int small_mv_tree[14] = 8823 { 8824 2, 8, 8825 4, 6, 8826 -0, -1, 8827 -2, -3, 8828 10, 12, 8829 -4, -5, 8830 -6, -7 8831 }; 8832 static const int mv_ref_tree[8] = 8833 { 8834 -ZEROMV, 2, 8835 -NEARESTMV, 4, 8836 -NEARMV, 6, 8837 -NEWMV, -SPLITMV 8838 }; 8839 static const int submv_ref_tree[6] = 8840 { 8841 -LEFT4X4, 2, 8842 -ABOVE4X4, 4, 8843 -ZERO4X4, -NEW4X4 8844 }; 8845 static const int split_mv_tree[6] = 8846 { 8847 -3, 2, 8848 -2, 4, 8849 -0, -1 8850 }; 8851 static const unsigned char default_b_mode_probs[] = 8852 { 120, 90, 79, 133, 87, 85, 80, 111, 151}; 8853 static const unsigned char mv_counts_to_probs[6][4] = 8854 { 8855 { 7, 1, 1, 143 }, 8856 { 14, 18, 14, 107 }, 8857 { 135, 64, 57, 68 }, 8858 { 60, 56, 128, 65 }, 8859 { 159, 134, 128, 34 }, 8860 { 234, 188, 128, 28 } 8862 }; 8863 static const unsigned char split_mv_probs[3] = 8864 { 110, 111, 150}; 8865 static const unsigned char submv_ref_probs2[5][3] = 8866 { 8867 { 147, 136, 18 }, 8868 { 106, 145, 1 }, 8869 { 179, 121, 1 }, 8870 { 223, 1, 34 }, 8871 { 208, 1, 1 } 8872 }; 8874 const static int mv_partitions[4][16] = 8875 { 8876 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, 8877 {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1 }, 8878 {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3 }, 8879 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } 8880 }; 8882 ---- End code block ---------------------------------------- 8884 20.14. predict.c 8886 ---- Begin code block -------------------------------------- 8888 /* 8889 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8890 * 8891 * Use of this source code is governed by a BSD-style license 8892 * that can be found in the LICENSE file in the root of the source 8893 * tree. An additional intellectual property rights grant can be 8894 * found in the file PATENTS. All contributing project authors may 8895 * be found in the AUTHORS file in the root of the source tree. 8896 */ 8897 #include "dixie.h" 8898 #include "predict.h" 8899 #include "idct_add.h" 8900 #include "mem.h" 8901 #include 8902 #include 8904 enum 8905 { 8906 BORDER_PIXELS = 16, 8907 }; 8909 static const filter_t sixtap_filters[8] = 8910 { 8911 { 0, 0, 128, 0, 0, 0 }, 8912 { 0, -6, 123, 12, -1, 0 }, 8913 { 2, -11, 108, 36, -8, 1 }, 8914 { 0, -9, 93, 50, -6, 0 }, 8915 { 3, -16, 77, 77, -16, 3 }, 8916 { 0, -6, 50, 93, -9, 0 }, 8917 { 1, -8, 36, 108, -11, 2 }, 8918 { 0, -1, 12, 123, -6, 0 }, 8919 }; 8921 static const filter_t bilinear_filters[8] = 8922 { 8924 { 0, 0, 128, 0, 0, 0 }, 8925 { 0, 0, 112, 16, 0, 0 }, 8926 { 0, 0, 96, 32, 0, 0 }, 8927 { 0, 0, 80, 48, 0, 0 }, 8928 { 0, 0, 64, 64, 0, 0 }, 8929 { 0, 0, 48, 80, 0, 0 }, 8930 { 0, 0, 32, 96, 0, 0 }, 8931 { 0, 0, 16, 112, 0, 0 } 8932 }; 8934 static void 8935 predict_h_nxn(unsigned char *predict, 8936 int stride, 8937 int n) 8938 { 8939 unsigned char *left = predict - 1; 8940 int i, j; 8942 for (i = 0; i < n; i++) 8943 for (j = 0; j < n; j++) 8944 predict[i *stride + j] = left[i * stride]; 8945 } 8947 static void 8948 predict_v_nxn(unsigned char *predict, 8949 int stride, 8950 int n) 8951 { 8952 unsigned char *above = predict - stride; 8953 int i, j; 8955 for (i = 0; i < n; i++) 8956 for (j = 0; j < n; j++) 8957 predict[i *stride + j] = above[j]; 8958 } 8960 static void 8961 predict_tm_nxn(unsigned char *predict, 8962 int stride, 8963 int n) 8964 { 8965 /* Transposes the left column to the top row for later consumption 8966 * by the idct/recon stage 8967 */ 8968 unsigned char *left = predict - 1; 8969 unsigned char *above = predict - stride; 8970 unsigned char p = above[-1]; 8971 int i, j; 8973 for (j = 0; j < n; j++) 8974 { 8975 for (i = 0; i < n; i++) 8976 predict[i] = CLAMP_255(*left + above[i] - p); 8978 predict += stride; 8979 left += stride; 8980 } 8981 } 8983 static void 8984 predict_dc_nxn(unsigned char *predict, 8985 int stride, 8986 int n) 8987 { 8988 unsigned char *left = predict - 1; 8989 unsigned char *above = predict - stride; 8990 int i, j, dc = 0; 8992 for (i = 0; i < n; i++) 8993 { 8994 dc += *left + above[i]; 8995 left += stride; 8996 } 8998 switch (n) 8999 { 9000 case 16: 9001 dc = (dc + 16) >> 5; 9002 break; 9004 case 8: 9005 dc = (dc + 8) >> 4; 9006 break; 9007 case 4: 9008 dc = (dc + 4) >> 3; 9009 break; 9010 } 9012 for (i = 0; i < n; i++) 9013 for (j = 0; j < n; j++) 9014 predict[i *stride + j] = dc; 9015 } 9017 static void 9018 predict_ve_4x4(unsigned char *predict, 9019 int stride) 9020 { 9021 unsigned char *above = predict - stride; 9022 int i, j; 9024 predict[0] = (above[-1] + 2 * above[0] + above[1] + 2) >> 2; 9025 predict[1] = (above[ 0] + 2 * above[1] + above[2] + 2) >> 2; 9026 predict[2] = (above[ 1] + 2 * above[2] + above[3] + 2) >> 2; 9027 predict[3] = (above[ 2] + 2 * above[3] + above[4] + 2) >> 2; 9029 for (i = 1; i < 4; i++) 9030 for (j = 0; j < 4; j++) 9031 predict[i *stride + j] = predict[j]; 9032 } 9034 static void 9035 predict_he_4x4(unsigned char *predict, 9036 int stride) 9037 { 9038 unsigned char *left = predict - 1; 9040 predict[0] = 9041 predict[1] = 9042 predict[2] = 9043 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9044 predict += stride; 9045 left += stride; 9047 predict[0] = 9048 predict[1] = 9049 predict[2] = 9050 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9051 predict += stride; 9052 left += stride; 9054 predict[0] = 9055 predict[1] = 9056 predict[2] = 9057 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9058 predict += stride; 9059 left += stride; 9061 predict[0] = 9062 predict[1] = 9063 predict[2] = 9064 predict[3] = (left[-stride] + 2 * left[0] + left[0] + 2) >> 2; 9065 } 9067 static void 9068 predict_ld_4x4(unsigned char *predict, 9069 int stride) 9070 { 9071 unsigned char *above = predict - stride; 9072 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9074 predict[0] = pred0 = (above[0] + 2 * above[1] + above[2] + 2) >> 2; 9075 predict[1] = pred1 = (above[1] + 2 * above[2] + above[3] + 2) >> 2; 9076 predict[2] = pred2 = (above[2] + 2 * above[3] + above[4] + 2) >> 2; 9077 predict[3] = pred3 = (above[3] + 2 * above[4] + above[5] + 2) >> 2; 9078 predict += stride; 9080 predict[0] = pred1; 9081 predict[1] = pred2; 9082 predict[2] = pred3; 9083 predict[3] = pred4 = (above[4] + 2 * above[5] + above[6] + 2) >> 2; 9084 predict += stride; 9086 predict[0] = pred2; 9087 predict[1] = pred3; 9088 predict[2] = pred4; 9089 predict[3] = pred5 = (above[5] + 2 * above[6] + above[7] + 2) >> 2; 9090 predict += stride; 9092 predict[0] = pred3; 9093 predict[1] = pred4; 9094 predict[2] = pred5; 9095 predict[3] = pred6 = (above[6] + 2 * above[7] + above[7] + 2) >> 2; 9096 } 9097 static void 9098 predict_rd_4x4(unsigned char *predict, 9099 int stride) 9100 { 9101 unsigned char *left = predict - 1; 9102 unsigned char *above = predict - stride; 9103 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9105 predict[0] = pred0 = 9106 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9107 predict[1] = pred1 = 9108 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9109 predict[2] = pred2 = 9110 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9111 predict[3] = pred3 = 9112 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9113 predict += stride; 9115 predict[0] = pred4 = 9116 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9117 predict[1] = pred0; 9118 predict[2] = pred1; 9119 predict[3] = pred2; 9120 predict += stride; 9122 predict[0] = pred5 = 9123 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9124 predict[1] = pred4; 9125 predict[2] = pred0; 9126 predict[3] = pred1; 9127 predict += stride; 9129 predict[0] = pred6 = 9130 (left[stride*3] + 2 * left[stride*2] + left[stride] + 2) >> 2; 9131 predict[1] = pred5; 9132 predict[2] = pred4; 9133 predict[3] = pred0; 9134 } 9136 static void 9137 predict_vr_4x4(unsigned char *predict, 9138 int stride) 9139 { 9140 unsigned char *left = predict - 1; 9141 unsigned char *above = predict - stride; 9142 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9143 pred7, pred8, pred9; 9145 predict[0] = pred0 = (above[-1] + above[0] + 1) >> 1; 9146 predict[1] = pred1 = (above[ 0] + above[1] + 1) >> 1; 9147 predict[2] = pred2 = (above[ 1] + above[2] + 1) >> 1; 9148 predict[3] = pred3 = (above[ 2] + above[3] + 1) >> 1; 9149 predict += stride; 9151 predict[0] = pred4 = 9152 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9153 predict[1] = pred5 = 9154 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9155 predict[2] = pred6 = 9156 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9157 predict[3] = pred7 = 9158 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9159 predict += stride; 9161 predict[0] = pred8 = 9162 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9163 predict[1] = pred0; 9164 predict[2] = pred1; 9165 predict[3] = pred2; 9166 predict += stride; 9168 predict[0] = pred9 = 9169 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9170 predict[1] = pred4; 9171 predict[2] = pred5; 9172 predict[3] = pred6; 9173 } 9175 static void 9176 predict_vl_4x4(unsigned char *predict, 9177 int stride) 9178 { 9179 unsigned char *above = predict - stride; 9180 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9181 pred7, pred8, pred9; 9183 predict[0] = pred0 = (above[0] + above[1] + 1) >> 1; 9184 predict[1] = pred1 = (above[1] + above[2] + 1) >> 1; 9185 predict[2] = pred2 = (above[2] + above[3] + 1) >> 1; 9186 predict[3] = pred3 = (above[3] + above[4] + 1) >> 1; 9187 predict += stride; 9189 predict[0] = pred4 = 9190 (above[0] + 2 * above[1] + above[2] + 2) >> 2; 9191 predict[1] = pred5 = 9192 (above[1] + 2 * above[2] + above[3] + 2) >> 2; 9193 predict[2] = pred6 = 9194 (above[2] + 2 * above[3] + above[4] + 2) >> 2; 9195 predict[3] = pred7 = 9196 (above[3] + 2 * above[4] + above[5] + 2) >> 2; 9197 predict += stride; 9199 predict[0] = pred1; 9200 predict[1] = pred2; 9201 predict[2] = pred3; 9202 predict[3] = pred8 = (above[4] + 2 * above[5] + above[6] + 2) >> 2; 9203 predict += stride; 9205 predict[0] = pred5; 9206 predict[1] = pred6; 9207 predict[2] = pred7; 9208 predict[3] = pred9 = (above[5] + 2 * above[6] + above[7] + 2) >> 2; 9209 } 9211 static void 9212 predict_hd_4x4(unsigned char *predict, 9213 int stride) 9214 { 9215 unsigned char *left = predict - 1; 9216 unsigned char *above = predict - stride; 9217 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9218 pred7, pred8, pred9; 9220 predict[0] = pred0 = 9221 (left[ 0] + above[-1] + 1) >> 1; 9222 predict[1] = pred1 = 9223 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9224 predict[2] = pred2 = 9225 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9226 predict[3] = pred3 = 9227 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9228 predict += stride; 9230 predict[0] = pred4 = 9231 (left[stride] + left[0] + 1) >> 1; 9232 predict[1] = pred5 = 9233 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9234 predict[2] = pred0; 9235 predict[3] = pred1; 9236 predict += stride; 9238 predict[0] = pred6 = 9239 (left[stride*2] + left[stride] + 1) >> 1; 9240 predict[1] = pred7 = 9241 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9242 predict[2] = pred4; 9243 predict[3] = pred5; 9244 predict += stride; 9246 predict[0] = pred8 = 9247 (left[stride*3] + left[stride*2] + 1) >> 1; 9248 predict[1] = pred9 = 9249 (left[stride*3] + 2 * left[stride*2] + left[stride] + 2) >> 2; 9250 predict[2] = pred6; 9251 predict[3] = pred7; 9252 } 9254 static void 9255 predict_hu_4x4(unsigned char *predict, 9256 int stride) 9257 { 9258 unsigned char *left = predict - 1; 9259 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9261 predict[0] = pred0 = 9262 (left[stride*0] + left[stride*1] + 1) >> 1; 9263 predict[1] = pred1 = 9264 (left[stride*0] + 2 * left[stride*1] + left[stride*2] + 2) >> 2; 9265 predict[2] = pred2 = 9266 (left[stride*1] + left[stride*2] + 1) >> 1; 9267 predict[3] = pred3 = 9268 (left[stride*1] + 2 * left[stride*2] + left[stride*3] + 2) >> 2; 9269 predict += stride; 9271 predict[0] = pred2; 9272 predict[1] = pred3; 9273 predict[2] = pred4 = 9274 (left[stride*2] + left[stride*3] + 1) >> 1; 9275 predict[3] = pred5 = 9276 (left[stride*2] + 2 * left[stride*3] + left[stride*3] + 2) >> 2; 9277 predict += stride; 9279 predict[0] = pred4; 9280 predict[1] = pred5; 9281 predict[2] = pred6 = left[stride*3]; 9282 predict[3] = pred6; 9283 predict += stride; 9285 predict[0] = pred6; 9286 predict[1] = pred6; 9287 predict[2] = pred6; 9288 predict[3] = pred6; 9289 } 9291 static void 9292 predict_h_16x16(unsigned char *predict, int stride) 9293 { 9294 predict_h_nxn(predict, stride, 16); 9295 } 9297 static void 9298 predict_v_16x16(unsigned char *predict, int stride) 9299 { 9300 predict_v_nxn(predict, stride, 16); 9301 } 9303 static void 9304 predict_tm_16x16(unsigned char *predict, int stride) 9305 { 9306 predict_tm_nxn(predict, stride, 16); 9307 } 9309 static void 9310 predict_h_8x8(unsigned char *predict, int stride) 9311 { 9312 predict_h_nxn(predict, stride, 8); 9313 } 9315 static void 9316 predict_v_8x8(unsigned char *predict, int stride) 9317 { 9318 predict_v_nxn(predict, stride, 8); 9319 } 9321 static void 9322 predict_tm_8x8(unsigned char *predict, int stride) 9323 { 9324 predict_tm_nxn(predict, stride, 8); 9325 } 9326 static void 9327 predict_tm_4x4(unsigned char *predict, int stride) 9328 { 9329 predict_tm_nxn(predict, stride, 4); 9330 } 9332 static void 9333 copy_down(unsigned char *recon, 9334 int stride) 9335 { 9336 /* Copy the four pixels above-right of subblock 3 to 9337 * above-right of subblocks 7, 11, and 15 9338 */ 9339 uint32_t tmp, *copy = (void *)(recon + 16 - stride); 9341 stride = stride / sizeof(unsigned int); 9342 tmp = *copy; 9343 copy += stride * 4; 9344 *copy = tmp; 9345 copy += stride * 4; 9346 *copy = tmp; 9347 copy += stride * 4; 9348 *copy = tmp; 9349 } 9351 static void 9352 b_pred(unsigned char *predict, 9353 int stride, 9354 struct mb_info *mbi, 9355 short *coeffs) 9356 { 9357 int i; 9359 copy_down(predict, stride); 9361 for (i = 0; i < 16; i++) 9362 { 9363 unsigned char *b_predict = predict + (i & 3) * 4; 9365 switch (mbi->split.modes[i]) 9366 { 9367 case B_DC_PRED: 9368 predict_dc_nxn(b_predict, stride, 4); 9369 break; 9370 case B_TM_PRED: 9371 predict_tm_4x4(b_predict, stride); 9372 break; 9373 case B_VE_PRED: 9374 predict_ve_4x4(b_predict, stride); 9375 break; 9376 case B_HE_PRED: 9377 predict_he_4x4(b_predict, stride); 9378 break; 9379 case B_LD_PRED: 9380 predict_ld_4x4(b_predict, stride); 9381 break; 9382 case B_RD_PRED: 9383 predict_rd_4x4(b_predict, stride); 9384 break; 9385 case B_VR_PRED: 9386 predict_vr_4x4(b_predict, stride); 9387 break; 9388 case B_VL_PRED: 9389 predict_vl_4x4(b_predict, stride); 9390 break; 9391 case B_HD_PRED: 9392 predict_hd_4x4(b_predict, stride); 9393 break; 9394 case B_HU_PRED: 9395 predict_hu_4x4(b_predict, stride); 9396 break; 9397 default: 9398 assert(0); 9399 } 9401 vp8_dixie_idct_add(b_predict, b_predict, stride, coeffs); 9402 coeffs += 16; 9404 if ((i & 3) == 3) 9405 { 9406 predict += stride * 4; 9407 } 9408 } 9409 } 9411 static void 9412 fixup_dc_coeffs(struct mb_info *mbi, 9413 short *coeffs) 9414 { 9415 short y2[16]; 9416 int i; 9418 vp8_dixie_walsh(coeffs + 24 * 16, y2); 9419 for (i = 0; i < 16; i++) 9420 coeffs[i*16] = y2[i]; 9421 } 9423 static void 9424 predict_intra_luma(unsigned char *predict, 9425 int stride, 9426 struct mb_info *mbi, 9427 short *coeffs) 9428 { 9429 if (mbi->base.y_mode == B_PRED) 9430 b_pred(predict, stride, mbi, coeffs); 9431 else 9432 { 9433 int i; 9435 switch (mbi->base.y_mode) 9436 { 9437 case DC_PRED: 9438 predict_dc_nxn(predict, stride, 16); 9439 break; 9440 case V_PRED: 9441 predict_v_16x16(predict, stride); 9442 break; 9443 case H_PRED: 9444 predict_h_16x16(predict, stride); 9445 break; 9446 case TM_PRED: 9447 predict_tm_16x16(predict, stride); 9448 break; 9449 default: 9450 assert(0); 9451 } 9453 fixup_dc_coeffs(mbi, coeffs); 9455 for (i = 0; i < 16; i++) 9456 { 9457 vp8_dixie_idct_add(predict, predict, stride, coeffs); 9458 coeffs += 16; 9459 predict += 4; 9461 if ((i & 3) == 3) 9462 predict += stride * 4 - 16; 9463 } 9465 } 9467 } 9469 static void 9470 predict_intra_chroma(unsigned char *predict_u, 9471 unsigned char *predict_v, 9472 int stride, 9473 struct mb_info *mbi, 9474 short *coeffs) 9475 { 9476 int i; 9478 switch (mbi->base.uv_mode) 9479 { 9480 case DC_PRED: 9481 predict_dc_nxn(predict_u, stride, 8); 9482 predict_dc_nxn(predict_v, stride, 8); 9483 break; 9484 case V_PRED: 9485 predict_v_8x8(predict_u, stride); 9486 predict_v_8x8(predict_v, stride); 9487 break; 9488 case H_PRED: 9489 predict_h_8x8(predict_u, stride); 9490 predict_h_8x8(predict_v, stride); 9491 break; 9492 case TM_PRED: 9493 predict_tm_8x8(predict_u, stride); 9494 predict_tm_8x8(predict_v, stride); 9495 break; 9496 default: 9497 assert(0); 9498 } 9500 coeffs += 16 * 16; 9502 for (i = 16; i < 20; i++) 9503 { 9504 vp8_dixie_idct_add(predict_u, predict_u, stride, coeffs); 9505 coeffs += 16; 9506 predict_u += 4; 9508 if (i & 1) 9509 predict_u += stride * 4 - 8; 9510 } 9512 for (i = 20; i < 24; i++) 9513 { 9514 vp8_dixie_idct_add(predict_v, predict_v, stride, coeffs); 9515 coeffs += 16; 9516 predict_v += 4; 9518 if (i & 1) 9519 predict_v += stride * 4 - 8; 9520 } 9522 } 9524 static void 9525 sixtap_horiz(unsigned char *output, 9526 int output_stride, 9527 const unsigned char *reference, 9528 int reference_stride, 9529 int cols, 9530 int rows, 9531 const filter_t filter 9532 ) 9533 { 9534 int r, c, temp; 9536 for (r = 0; r < rows; r++) 9537 { 9538 for (c = 0; c < cols; c++) 9539 { 9540 temp = (reference[-2] * filter[0]) + 9541 (reference[-1] * filter[1]) + 9542 (reference[ 0] * filter[2]) + 9543 (reference[ 1] * filter[3]) + 9544 (reference[ 2] * filter[4]) + 9545 (reference[ 3] * filter[5]) + 9546 64; 9547 temp >>= 7; 9548 output[c] = CLAMP_255(temp); 9549 reference++; 9550 } 9552 reference += reference_stride - cols; 9553 output += output_stride; 9554 } 9555 } 9557 static void 9558 sixtap_vert(unsigned char *output, 9559 int output_stride, 9560 const unsigned char *reference, 9561 int reference_stride, 9562 int cols, 9563 int rows, 9564 const filter_t filter 9565 ) 9566 { 9567 int r, c, temp; 9569 for (r = 0; r < rows; r++) 9570 { 9571 for (c = 0; c < cols; c++) 9572 { 9573 temp = (reference[-2*reference_stride] * filter[0]) + 9574 (reference[-1*reference_stride] * filter[1]) + 9575 (reference[ 0*reference_stride] * filter[2]) + 9576 (reference[ 1*reference_stride] * filter[3]) + 9577 (reference[ 2*reference_stride] * filter[4]) + 9578 (reference[ 3*reference_stride] * filter[5]) + 9579 64; 9580 temp >>= 7; 9581 output[c] = CLAMP_255(temp); 9582 reference++; 9583 } 9585 reference += reference_stride - cols; 9586 output += output_stride; 9587 } 9588 } 9590 static void 9591 sixtap_2d(unsigned char *output, 9592 int output_stride, 9593 const unsigned char *reference, 9594 int reference_stride, 9595 int cols, 9596 int rows, 9597 int mx, 9598 int my, 9599 const filter_t filters[8] 9600 ) 9601 { 9602 DECLARE_ALIGNED(16, unsigned char, temp[16*(16+5)]); 9604 sixtap_horiz(temp, 16, 9605 reference - 2 * reference_stride, reference_stride, 9606 cols, rows + 5, filters[mx]); 9608 sixtap_vert(output, output_stride, 9609 temp + 2 * 16, 16, 9610 cols, rows, filters[my]); 9611 } 9613 struct img_index 9614 { 9615 unsigned char *y, *u, *v; 9616 int stride, uv_stride; 9617 }; 9619 static const unsigned char * 9620 filter_block(unsigned char *output, 9621 const unsigned char *reference, 9622 int stride, 9623 const union mv *mv, 9624 const filter_t filters[8]) 9625 { 9626 int mx, my; 9628 /* Handle 0,0 as a special case. TODO: does this make it any 9629 * faster? 9630 */ 9631 if (!mv->raw) 9632 return reference; 9634 mx = mv->d.x & 7; 9635 my = mv->d.y & 7; 9636 reference += ((mv->d.y >> 3) * stride) + (mv->d.x >> 3); 9638 if (mx | my) 9639 { 9640 sixtap_2d(output, stride, reference, stride, 4, 4, mx, my, 9641 filters); 9642 reference = output; 9643 } 9645 return reference; 9646 } 9648 static void 9649 recon_1_block(unsigned char *output, 9650 const unsigned char *reference, 9651 int stride, 9652 const union mv *mv, 9653 const filter_t filters[8], 9654 short *coeffs, 9655 struct mb_info *mbi, 9656 int b 9657 ) 9658 { 9659 const unsigned char *predict; 9661 predict = filter_block(output, reference, stride, mv, filters); 9662 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9663 } 9665 static mv_t 9666 calculate_chroma_splitmv(struct mb_info *mbi, 9667 int b, 9668 int full_pixel) 9669 { 9670 int temp; 9671 union mv mv; 9673 temp = mbi->split.mvs[b].d.x + 9674 mbi->split.mvs[b+1].d.x + 9675 mbi->split.mvs[b+4].d.x + 9676 mbi->split.mvs[b+5].d.x; 9678 if (temp < 0) 9679 temp -= 4; 9680 else 9681 temp += 4; 9683 mv.d.x = temp / 8; 9685 temp = mbi->split.mvs[b].d.y + 9686 mbi->split.mvs[b+1].d.y + 9687 mbi->split.mvs[b+4].d.y + 9688 mbi->split.mvs[b+5].d.y; 9690 if (temp < 0) 9691 temp -= 4; 9692 else 9693 temp += 4; 9695 mv.d.y = temp / 8; 9697 if (full_pixel) 9698 { 9699 mv.d.x &= ~7; 9700 mv.d.y &= ~7; 9701 } 9703 return mv; 9704 } 9706 /* Note: We rely on the reconstructed border having the same stride as 9707 * the reference buffer because the filter_block can't adjust the 9708 * stride with its return value, only the reference pointer. 9709 */ 9710 static void 9711 build_mc_border(unsigned char *dst, 9712 const unsigned char *src, 9713 int stride, 9714 int x, 9715 int y, 9716 int b_w, 9717 int b_h, 9718 int w, 9719 int h 9720 ) 9721 { 9722 const unsigned char *ref_row; 9724 /* Get a pointer to the start of the real data for this row */ 9725 ref_row = src - x - y * stride; 9727 if (y >= h) 9728 ref_row += (h - 1) * stride; 9729 else if (y > 0) 9730 ref_row += y * stride; 9732 do 9733 { 9734 int left, right = 0, copy; 9736 left = x < 0 ? -x : 0; 9738 if (left > b_w) 9739 left = b_w; 9741 if (x + b_w > w) 9742 right = x + b_w - w; 9744 if (right > b_w) 9745 right = b_w; 9747 copy = b_w - left - right; 9749 if (left) 9750 memset(dst, ref_row[0], left); 9752 if (copy) 9753 memcpy(dst + left, ref_row + x + left, copy); 9755 if (right) 9756 memset(dst + left + copy, ref_row[w-1], right); 9758 dst += stride; 9759 y++; 9761 if (y < h && y > 0) 9762 ref_row += stride; 9763 } 9764 while (--b_h); 9765 } 9767 static void 9768 recon_1_edge_block(unsigned char *output, 9769 unsigned char *emul_block, 9770 const unsigned char *reference, 9771 int stride, 9772 const union mv *mv, 9773 const filter_t filters[8], 9774 short *coeffs, 9775 struct mb_info *mbi, 9776 int x, 9777 int y, 9778 int w, 9779 int h, 9780 int start_b 9781 ) 9782 { 9783 const unsigned char *predict; 9784 int b = start_b; 9785 const int b_w = 4; 9786 const int b_h = 4; 9788 x += mv->d.x >> 3; 9789 y += mv->d.y >> 3; 9791 /* Need two pixels left/above, 3 right/below for 6-tap */ 9792 if (x < 2 || x + b_w - 1 + 3 >= w || y < 2 || y + b_h - 1 + 3 >= h) 9793 { 9794 reference += (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9795 build_mc_border(emul_block, 9796 reference - 2 - 2 * stride, stride, 9797 x - 2, y - 2, b_w + 5, b_h + 5, w, h); 9798 reference = emul_block + 2 * stride + 2; 9799 reference -= (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9800 } 9802 predict = filter_block(output, reference, stride, mv, filters); 9803 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9804 } 9806 static void 9807 predict_inter_emulated_edge(struct vp8_decoder_ctx *ctx, 9808 struct img_index *img, 9809 short *coeffs, 9810 struct mb_info *mbi, 9811 int mb_col, 9812 int mb_row) 9813 { 9814 /* TODO: move this into its own buffer. This only works because we 9815 * still have a border allocated. 9816 */ 9817 unsigned char *emul_block = ctx->frame_strg[0].img.img_data; 9818 unsigned char *reference; 9819 unsigned char *output; 9820 ptrdiff_t reference_offset; 9821 int w, h, x, y, b; 9822 union mv chroma_mv[4]; 9823 unsigned char *u = img->u, *v = img->v; 9824 int full_pixel = ctx->frame_hdr.version == 3; 9826 x = mb_col * 16; 9827 y = mb_row * 16; 9828 w = ctx->mb_cols * 16; 9829 h = ctx->mb_rows * 16; 9830 output = img->y; 9831 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9832 reference = output + reference_offset; 9834 if (mbi->base.y_mode != SPLITMV) 9835 { 9836 union mv uvmv; 9838 uvmv = mbi->base.mv; 9839 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9840 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9842 if (full_pixel) 9843 { 9844 uvmv.d.x &= ~7; 9845 uvmv.d.y &= ~7; 9846 } 9848 chroma_mv[0] = uvmv; 9849 chroma_mv[1] = uvmv; 9850 chroma_mv[2] = uvmv; 9851 chroma_mv[3] = uvmv; 9852 } 9853 else 9854 { 9855 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9856 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9857 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9858 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9859 } 9861 /* Luma */ 9862 for (b = 0; b < 16; b++) 9863 { 9864 union mv *ymv; 9866 if (mbi->base.y_mode != SPLITMV) 9867 ymv = &mbi->base.mv; 9868 else 9869 ymv = mbi->split.mvs + b; 9871 recon_1_edge_block(output, emul_block, reference, img->stride, 9872 ymv, ctx->subpixel_filters, 9873 coeffs, mbi, x, y, w, h, b); 9875 x += 4; 9876 output += 4; 9877 reference += 4; 9879 if ((b & 3) == 3) 9880 { 9881 x -= 16; 9882 y += 4; 9883 output += 4 * img->stride - 16; 9884 reference += 4 * img->stride - 16; 9885 } 9886 } 9887 x = mb_col * 16; 9888 y = mb_row * 16; 9890 /* Chroma */ 9891 x >>= 1; 9892 y >>= 1; 9893 w >>= 1; 9894 h >>= 1; 9896 for (b = 0; b < 4; b++) 9897 { 9898 recon_1_edge_block(u, emul_block, u + reference_offset, 9899 img->uv_stride, 9900 &chroma_mv[b], ctx->subpixel_filters, 9901 coeffs, mbi, x, y, w, h, b + 16); 9902 recon_1_edge_block(v, emul_block, v + reference_offset, 9903 img->uv_stride, 9904 &chroma_mv[b], ctx->subpixel_filters, 9905 coeffs, mbi, x, y, w, h, b + 20); 9906 u += 4; 9907 v += 4; 9908 x += 4; 9910 if (b & 1) 9911 { 9912 x -= 8; 9913 y += 4; 9914 u += 4 * img->uv_stride - 8; 9915 v += 4 * img->uv_stride - 8; 9916 } 9917 } 9919 } 9921 static void 9922 predict_inter(struct vp8_decoder_ctx *ctx, 9923 struct img_index *img, 9924 short *coeffs, 9925 struct mb_info *mbi) 9926 { 9927 unsigned char *y = img->y; 9928 unsigned char *u = img->u; 9929 unsigned char *v = img->v; 9930 ptrdiff_t reference_offset; 9931 union mv chroma_mv[4]; 9932 int full_pixel = ctx->frame_hdr.version == 3; 9933 int b; 9934 if (mbi->base.y_mode != SPLITMV) 9935 { 9936 union mv uvmv; 9938 uvmv = mbi->base.mv; 9939 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9940 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9942 if (full_pixel) 9943 { 9944 uvmv.d.x &= ~7; 9945 uvmv.d.y &= ~7; 9946 } 9948 chroma_mv[0] = 9949 chroma_mv[1] = 9950 chroma_mv[2] = 9951 chroma_mv[3] = uvmv; 9952 } 9953 else 9954 { 9955 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9956 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9957 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9958 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9959 } 9961 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9963 for (b = 0; b < 16; b++) 9964 { 9965 union mv *ymv; 9967 if (mbi->base.y_mode != SPLITMV) 9968 ymv = &mbi->base.mv; 9969 else 9970 ymv = mbi->split.mvs + b; 9972 recon_1_block(y, y + reference_offset, img->stride, 9973 ymv, ctx->subpixel_filters, coeffs, mbi, b); 9974 y += 4; 9976 if ((b & 3) == 3) 9977 y += 4 * img->stride - 16; 9978 } 9980 for (b = 0; b < 4; b++) 9981 { 9982 recon_1_block(u, u + reference_offset, 9983 img->uv_stride, &chroma_mv[b], 9984 ctx->subpixel_filters, coeffs, mbi, b + 16); 9985 recon_1_block(v, v + reference_offset, 9986 img->uv_stride, &chroma_mv[b], 9987 ctx->subpixel_filters, coeffs, mbi, b + 20); 9988 u += 4; 9989 v += 4; 9991 if (b & 1) 9992 { 9993 u += 4 * img->uv_stride - 8; 9994 v += 4 * img->uv_stride - 8; 9995 } 9996 } 9997 } 9999 void 10000 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg) 10001 { 10002 if (rcimg) 10003 { 10004 assert(rcimg->ref_cnt); 10005 rcimg->ref_cnt--; 10006 } 10007 } 10009 struct ref_cnt_img * 10010 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg) 10011 { 10012 rcimg->ref_cnt++; 10013 return rcimg; 10014 } 10016 struct ref_cnt_img * 10017 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames) 10018 { 10019 int i; 10021 for (i = 0; i < NUM_REF_FRAMES; i++) 10022 if (frames[i].ref_cnt == 0) 10023 { 10024 frames[i].ref_cnt = 1; 10025 return &frames[i]; 10027 } 10029 assert(0); 10030 return NULL; 10031 } 10033 static void 10034 fixup_left(unsigned char *predict, 10035 int width, 10036 int stride, 10037 unsigned int row, 10038 enum prediction_mode mode) 10039 { 10040 /* The left column of out-of-frame pixels is taken to be 129, 10041 * unless we're doing DC_PRED, in which case we duplicate the 10042 * above row, unless this is also row 0, in which case we use 10043 * 129. 10044 */ 10045 unsigned char *left = predict - 1; 10046 int i; 10048 if (mode == DC_PRED && row) 10049 { 10050 unsigned char *above = predict - stride; 10052 for (i = 0; i < width; i++) 10053 { 10054 *left = above[i]; 10055 left += stride; 10056 } 10057 } 10058 else 10059 { 10060 /* Need to re-set the above row, in case the above MB was 10061 * DC_PRED. 10062 */ 10063 left -= stride; 10065 for (i = -1; i < width; i++) 10066 { 10067 *left = 129; 10068 left += stride; 10069 } 10070 } 10071 } 10072 static void 10073 fixup_above(unsigned char *predict, 10074 int width, 10075 int stride, 10076 unsigned int col, 10077 enum prediction_mode mode) 10078 { 10079 /* The above row of out-of-frame pixels is taken to be 127, 10080 * unless we're doing DC_PRED, in which case we duplicate the 10081 * left col, unless this is also col 0, in which case we use 10082 * 127. 10083 */ 10084 unsigned char *above = predict - stride; 10085 int i; 10087 if (mode == DC_PRED && col) 10088 { 10089 unsigned char *left = predict - 1; 10091 for (i = 0; i < width; i++) 10092 { 10093 above[i] = *left; 10094 left += stride; 10095 } 10096 } 10097 else 10098 /* Need to re-set the left col, in case the last MB was 10099 * DC_PRED. 10100 */ 10101 memset(above - 1, 127, width + 1); 10103 memset(above + width, 127, 4); // for above-right subblock modes 10104 } 10106 void 10107 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx) 10108 { 10110 int i; 10111 unsigned char *this_frame_base; 10113 if (ctx->frame_hdr.frame_size_updated) 10114 { 10115 for (i = 0; i < NUM_REF_FRAMES; i++) 10116 { 10117 unsigned int w = ctx->mb_cols * 16 + BORDER_PIXELS * 2; 10118 unsigned int h = ctx->mb_rows * 16 + BORDER_PIXELS * 2; 10119 vpx_img_free(&ctx->frame_strg[i].img); 10120 ctx->frame_strg[i].ref_cnt = 0; 10121 ctx->ref_frames[i] = NULL; 10123 if (!vpx_img_alloc(&ctx->frame_strg[i].img, 10124 IMG_FMT_I420, w, h, 16)) 10125 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10126 "Failed to allocate %dx%d" 10127 " framebuffer", 10128 w, h); 10130 vpx_img_set_rect(&ctx->frame_strg[i].img, 10131 BORDER_PIXELS, BORDER_PIXELS, 10132 ctx->frame_hdr.kf.w, ctx->frame_hdr.kf.h); 10134 } 10136 if (ctx->frame_hdr.version) 10137 ctx->subpixel_filters = bilinear_filters; 10138 else 10139 ctx->subpixel_filters = sixtap_filters; 10140 } 10142 /* Find a free framebuffer to predict into */ 10143 if (ctx->ref_frames[CURRENT_FRAME]) 10144 vp8_dixie_release_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 10146 ctx->ref_frames[CURRENT_FRAME] = 10147 vp8_dixie_find_free_ref_frame(ctx->frame_strg); 10148 this_frame_base = ctx->ref_frames[CURRENT_FRAME]->img.img_data; 10150 /* Calculate offsets to the other reference frames */ 10151 for (i = 0; i < NUM_REF_FRAMES; i++) 10152 { 10153 struct ref_cnt_img *ref = ctx->ref_frames[i]; 10155 ctx->ref_frame_offsets[i] = 10156 ref ? ref->img.img_data - this_frame_base : 0; 10157 } 10159 /* TODO: No need to do this on every frame... */ 10160 } 10162 void 10163 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx) 10164 { 10165 int i; 10166 for (i = 0; i < NUM_REF_FRAMES; i++) 10167 { 10168 vpx_img_free(&ctx->frame_strg[i].img); 10169 ctx->frame_strg[i].ref_cnt = 0; 10170 ctx->ref_frames[i] = NULL; 10171 } 10172 } 10174 void 10175 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10176 unsigned int row, 10177 unsigned int start_col, 10178 unsigned int num_cols) 10179 { 10180 struct img_index img; 10181 struct mb_info *mbi; 10182 unsigned int col; 10183 short *coeffs; 10185 /* Adjust pointers based on row, start_col */ 10186 img.stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 10187 img.uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 10188 img.y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 10189 img.u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 10190 img.v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 10191 img.y += (img.stride * row + start_col) * 16; 10192 img.u += (img.uv_stride * row + start_col) * 8; 10193 img.v += (img.uv_stride * row + start_col) * 8; 10194 mbi = ctx->mb_info_rows[row] + start_col; 10195 coeffs = ctx->tokens[row & (ctx->token_hdr.partitions - 1)].coeffs 10196 + 25 * 16 * start_col; 10198 /* Fix up the out-of-frame pixels */ 10199 if (start_col == 0) 10200 { 10201 fixup_left(img.y, 16, img.stride, row, mbi->base.y_mode); 10202 fixup_left(img.u, 8, img.uv_stride, row, mbi->base.uv_mode); 10203 fixup_left(img.v, 8, img.uv_stride, row, mbi->base.uv_mode); 10205 if (row == 0) 10206 *(img.y - img.stride - 1) = 127; 10207 } 10209 for (col = start_col; col < start_col + num_cols; col++) 10210 { 10211 if (row == 0) 10212 { 10213 fixup_above(img.y, 16, img.stride, col, mbi->base.y_mode); 10214 fixup_above(img.u, 8, img.uv_stride, col, 10215 mbi->base.uv_mode); 10216 fixup_above(img.v, 8, img.uv_stride, col, 10217 mbi->base.uv_mode); 10218 } 10220 if (mbi->base.y_mode <= B_PRED) 10221 { 10222 predict_intra_luma(img.y, img.stride, mbi, coeffs); 10223 predict_intra_chroma(img.u, img.v, img.uv_stride, mbi, 10224 coeffs); 10225 } 10226 else 10227 { 10228 if (mbi->base.y_mode != SPLITMV) // && != BPRED 10229 fixup_dc_coeffs(mbi, coeffs); 10231 if (mbi->base.need_mc_border) 10232 predict_inter_emulated_edge(ctx, &img, coeffs, mbi, col, 10233 row); 10234 else 10235 predict_inter(ctx, &img, coeffs, mbi); 10236 } 10238 /* Advance to the next macroblock */ 10239 mbi++; 10240 img.y += 16; 10241 img.u += 8; 10242 img.v += 8; 10243 coeffs += 25 * 16; 10244 } 10246 if (col == ctx->mb_cols) 10247 { 10248 /* Extend the last row by four pixels for intra prediction. 10249 * This will be propagated later by copy_down. 10250 */ 10251 uint32_t *extend = (uint32_t *)(img.y + 15 * img.stride); 10252 uint32_t val = 0x01010101 * img.y[-1 + 15 * img.stride]; 10253 *extend = val; 10254 } 10255 } 10257 ---- End code block ---------------------------------------- 10258 20.15. predict.h 10260 ---- Begin code block -------------------------------------- 10262 /* 10263 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10264 * 10265 * Use of this source code is governed by a BSD-style license 10266 * that can be found in the LICENSE file in the root of the source 10267 * tree. An additional intellectual property rights grant can be 10268 * found in the file PATENTS. All contributing project authors may 10269 * be found in the AUTHORS file in the root of the source tree. 10270 */ 10271 #ifndef PREDICT_H 10272 #define PREDICT_H 10274 void 10275 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx); 10277 void 10278 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx); 10280 void 10281 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10282 unsigned int row, 10283 unsigned int start_col, 10284 unsigned int num_cols); 10286 void 10287 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg); 10289 struct ref_cnt_img * 10290 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg); 10292 struct ref_cnt_img * 10293 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames); 10295 #endif 10297 ---- End code block ---------------------------------------- 10299 20.16. tokens.c 10301 ---- Begin code block -------------------------------------- 10302 /* 10303 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10304 * 10305 * Use of this source code is governed by a BSD-style license 10306 * that can be found in the LICENSE file in the root of the source 10307 * tree. An additional intellectual property rights grant can be 10308 * found in the file PATENTS. All contributing project authors may 10309 * be found in the AUTHORS file in the root of the source tree. 10310 */ 10311 #include "vpx_codec_internal.h" 10312 #include "dixie.h" 10313 #include "tokens.h" 10314 #include 10315 #include 10316 #include 10318 enum 10319 { 10320 EOB_CONTEXT_NODE, 10321 ZERO_CONTEXT_NODE, 10322 ONE_CONTEXT_NODE, 10323 LOW_VAL_CONTEXT_NODE, 10324 TWO_CONTEXT_NODE, 10325 THREE_CONTEXT_NODE, 10326 HIGH_LOW_CONTEXT_NODE, 10327 CAT_ONE_CONTEXT_NODE, 10328 CAT_THREEFOUR_CONTEXT_NODE, 10329 CAT_THREE_CONTEXT_NODE, 10330 CAT_FIVE_CONTEXT_NODE 10331 }; 10332 enum 10333 { 10334 ZERO_TOKEN, 10335 ONE_TOKEN, 10336 TWO_TOKEN, 10337 THREE_TOKEN, 10338 FOUR_TOKEN, 10339 DCT_VAL_CATEGORY1, 10340 DCT_VAL_CATEGORY2, 10341 DCT_VAL_CATEGORY3, 10342 DCT_VAL_CATEGORY4, 10343 DCT_VAL_CATEGORY5, 10344 DCT_VAL_CATEGORY6, 10345 DCT_EOB_TOKEN, 10346 MAX_ENTROPY_TOKENS 10347 }; 10348 struct extrabits 10349 { 10350 short min_val; 10351 short length; 10352 unsigned char probs[12]; 10353 }; 10354 static const unsigned int left_context_index[25] = 10355 { 10356 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10357 4, 4, 5, 5, 6, 6, 7, 7, 8 10358 }; 10359 static const unsigned int above_context_index[25] = 10360 { 10361 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10362 4, 5, 4, 5, 6, 7, 6, 7, 8 10363 }; 10364 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10365 static const unsigned int bands_x[16] = 10366 { 10367 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10368 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10369 }; 10370 #undef X 10371 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10372 { 10373 { 0, -1, { 0, 0, 0, 0, 0, 0, 10374 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10375 { 1, 0, { 0, 0, 0, 0, 0, 0, 10376 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10377 { 2, 0, { 0, 0, 0, 0, 0, 0, 10378 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10379 { 3, 0, { 0, 0, 0, 0, 0, 0, 10380 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10381 { 4, 0, { 0, 0, 0, 0, 0, 0, 10382 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10383 { 5, 0, { 159, 0, 0, 0, 0, 0, 10384 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10385 { 7, 1, { 145, 165, 0, 0, 0, 0, 10386 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10387 { 11, 2, { 140, 148, 173, 0, 0, 0, 10388 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10389 { 19, 3, { 135, 140, 155, 176, 0, 0, 10390 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10391 { 35, 4, { 130, 134, 141, 157, 180, 0, 10392 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10393 { 67, 10, { 129, 130, 133, 140, 153, 177, 10394 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10395 { 0, -1, { 0, 0, 0, 0, 0, 0, 10396 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10398 }; 10399 static const unsigned int zigzag[16] = 10400 { 10401 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10402 }; 10404 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10405 v = (bool_get_bit(bool) ? -value_to_sign \ 10406 : value_to_sign) * dqf[!!c]; 10408 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10409 if (!bool_get(bool, probability)) goto branch; 10411 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10412 if (!bool_get(bool, probability)) \ 10413 { \ 10414 prob = type_probs; \ 10415 if(c<15) {\ 10416 ++c; \ 10417 prob += bands_x[c]; \ 10418 goto branch; \ 10419 }\ 10420 else \ 10421 goto BLOCK_FINISHED; /*for malformed input */\ 10422 } 10424 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10425 DECODE_AND_APPLYSIGN(val) \ 10426 prob = type_probs + (ENTROPY_NODES*2); \ 10427 if(c < 15){\ 10428 b_tokens[zigzag[c]] = v; \ 10429 ++c; \ 10430 goto DO_WHILE; }\ 10431 b_tokens[zigzag[15]] = v; \ 10432 goto BLOCK_FINISHED; 10434 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10435 val += bool_get(bool, extrabits[t].probs[bits_count]) << bits_count; 10437 static int 10438 decode_mb_tokens(struct bool_decoder *bool, 10439 token_entropy_ctx_t left, 10440 token_entropy_ctx_t above, 10441 short *tokens, 10442 enum prediction_mode mode, 10443 coeff_probs_table_t probs, 10444 short factor[TOKEN_BLOCK_TYPES][2]) 10445 { 10446 int i, stop, type; 10447 int c, t, v; 10448 int val, bits_count; 10449 int eob_mask; 10450 short *b_tokens; /* tokens for this block */ 10451 unsigned char *type_probs; /* probabilities for this block type */ 10452 unsigned char *prob; 10453 short *dqf; 10455 eob_mask = 0; 10457 if (mode != B_PRED && mode != SPLITMV) 10458 { 10459 i = 24; 10460 stop = 24; 10461 type = 1; 10462 b_tokens = tokens + 24 * 16; 10463 dqf = factor[TOKEN_BLOCK_Y2]; 10464 } 10465 else 10466 { 10467 i = 0; 10468 stop = 16; 10469 type = 3; 10470 b_tokens = tokens; 10471 dqf = factor[TOKEN_BLOCK_Y1]; 10472 } 10474 /* Save a pointer to the coefficient probs for the current type. 10475 * Need to repeat this whenever type changes. 10476 */ 10477 type_probs = probs[type][0][0]; 10479 BLOCK_LOOP: 10480 t = left[left_context_index[i]] + above[above_context_index[i]]; 10481 c = !type; /* all blocks start at 0 except type 0, which starts 10482 * at 1. */ 10484 prob = type_probs; 10485 prob += t * ENTROPY_NODES; 10487 DO_WHILE: 10488 prob += bands_x[c]; 10489 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], BLOCK_FINISHED); 10491 CHECK_0_: 10493 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10494 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10495 ONE_CONTEXT_NODE_0_); 10496 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10497 LOW_VAL_CONTEXT_NODE_0_); 10498 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10499 HIGH_LOW_CONTEXT_NODE_0_); 10500 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10501 CAT_THREEFOUR_CONTEXT_NODE_0_); 10502 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10503 CAT_FIVE_CONTEXT_NODE_0_); 10504 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10505 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10507 do 10508 { 10509 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count); 10510 bits_count -- ; 10511 } 10512 while (bits_count >= 0); 10514 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10516 CAT_FIVE_CONTEXT_NODE_0_: 10517 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10518 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10519 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10520 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10521 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10522 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10523 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10525 CAT_THREEFOUR_CONTEXT_NODE_0_: 10526 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10527 CAT_THREE_CONTEXT_NODE_0_); 10528 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10529 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10530 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10531 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10532 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10533 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10535 CAT_THREE_CONTEXT_NODE_0_: 10536 val = extrabits[DCT_VAL_CATEGORY3].min_val; 10537 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 10538 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 10539 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 10540 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10542 HIGH_LOW_CONTEXT_NODE_0_: 10543 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 10544 CAT_ONE_CONTEXT_NODE_0_); 10546 val = extrabits[DCT_VAL_CATEGORY2].min_val; 10547 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 10548 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 10549 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10551 CAT_ONE_CONTEXT_NODE_0_: 10552 val = extrabits[DCT_VAL_CATEGORY1].min_val; 10553 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 10554 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10556 LOW_VAL_CONTEXT_NODE_0_: 10557 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 10558 TWO_CONTEXT_NODE_0_); 10559 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 10560 THREE_CONTEXT_NODE_0_); 10561 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 10563 THREE_CONTEXT_NODE_0_: 10564 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 10566 TWO_CONTEXT_NODE_0_: 10567 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 10569 ONE_CONTEXT_NODE_0_: 10570 DECODE_AND_APPLYSIGN(1); 10571 prob = type_probs + ENTROPY_NODES; 10573 if (c < 15) 10574 { 10575 b_tokens[zigzag[c]] = v; 10576 ++c; 10577 goto DO_WHILE; 10578 } 10580 b_tokens[zigzag[15]] = v; 10581 BLOCK_FINISHED: 10582 eob_mask |= (c > 1) << i; 10583 t = (c != !type); // any nonzero data? 10584 eob_mask |= t << 31; 10586 left[left_context_index[i]] = above[above_context_index[i]] = t; 10587 b_tokens += 16; 10589 i++; 10590 if (i < stop) 10591 goto BLOCK_LOOP; 10593 if (i == 25) 10594 { 10595 type = 0; 10596 i = 0; 10597 stop = 16; 10598 type_probs = probs[type][0][0]; 10599 b_tokens = tokens; 10600 dqf = factor[TOKEN_BLOCK_Y1]; 10601 goto BLOCK_LOOP; 10602 } 10604 if (i == 16) 10605 { 10606 type = 2; 10607 type_probs = probs[type][0][0]; 10608 stop = 24; 10609 dqf = factor[TOKEN_BLOCK_UV]; 10610 goto BLOCK_LOOP; 10611 } 10613 return eob_mask; 10614 } 10616 static void 10617 reset_row_context(token_entropy_ctx_t *left) 10618 { 10619 memset(left, 0, sizeof(*left)); 10620 } 10622 static void 10623 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 10624 { 10625 memset(above, 0, cols * sizeof(*above)); 10626 } 10628 static void 10629 reset_mb_context(token_entropy_ctx_t *left, 10630 token_entropy_ctx_t *above, 10631 enum prediction_mode mode) 10632 { 10633 /* Reset the macroblock context on the left and right. We have to 10634 * preserve the context of the second order block if this mode 10635 * would not have updated it. 10636 */ 10637 memset(left, 0, sizeof((*left)[0]) * 8); 10638 memset(above, 0, sizeof((*above)[0]) * 8); 10640 if (mode != B_PRED && mode != SPLITMV) 10641 { 10642 (*left)[8] = 0; 10643 (*above)[8] = 0; 10644 } 10645 } 10647 void 10648 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 10649 unsigned int partition, 10650 unsigned int row, 10651 unsigned int start_col, 10652 unsigned int num_cols) 10653 { 10654 struct token_decoder *tokens = &ctx->tokens[partition]; 10655 short *coeffs = tokens->coeffs + 25 * 16 * start_col; 10656 unsigned int col; 10657 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 10658 + start_col; 10659 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 10660 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 10662 if (row == 0) 10663 reset_above_context(above, num_cols); 10665 if (start_col == 0) 10666 reset_row_context(left); 10668 for (col = start_col; col < start_col + num_cols; col++) 10669 { 10670 memset(coeffs, 0, 25 * 16 * sizeof(short)); 10672 if (mbi->base.skip_coeff) 10673 { 10674 reset_mb_context(left, above, mbi->base.y_mode); 10675 mbi->base.eob_mask = 0; 10676 } 10677 else 10678 { 10679 struct dequant_factors *dqf; 10681 dqf = ctx->dequant_factors + mbi->base.segment_id; 10682 mbi->base.eob_mask = 10683 decode_mb_tokens(&tokens->bool, 10684 *left, *above, 10685 coeffs, 10686 mbi->base.y_mode, 10687 ctx->entropy_hdr.coeff_probs, 10688 dqf->factor); 10689 } 10691 above++; 10692 mbi++; 10693 coeffs += 25 * 16; 10694 } 10695 } 10697 void 10698 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 10699 { 10700 unsigned int partitions = ctx->token_hdr.partitions; 10702 if (ctx->frame_hdr.frame_size_updated) 10703 { 10704 unsigned int i; 10705 unsigned int coeff_row_sz = 10706 ctx->mb_cols * 25 * 16 * sizeof(short); 10708 for (i = 0; i < partitions; i++) 10709 { 10710 free(ctx->tokens[i].coeffs); 10711 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 10713 if (!ctx->tokens[i].coeffs) 10714 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10715 NULL); 10716 } 10718 free(ctx->above_token_entropy_ctx); 10719 ctx->above_token_entropy_ctx = 10720 calloc(ctx->mb_cols, sizeof(*ctx->above_token_entropy_ctx)); 10722 if (!ctx->above_token_entropy_ctx) 10723 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, NULL); 10724 } 10725 } 10727 void 10728 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 10729 { 10730 int i; 10732 for (i = 0; i < MAX_PARTITIONS; i++) 10733 free(ctx->tokens[i].coeffs); 10735 free(ctx->above_token_entropy_ctx); 10736 } 10738 ---- End code block ---------------------------------------- 10740 20.17. tokens.h 10742 ---- Begin code block -------------------------------------- 10744 /* 10745 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10746 * 10747 * Use of this source code is governed by a BSD-style license 10748 * that can be found in the LICENSE file in the root of the source 10749 * tree. An additional intellectual property rights grant can be 10750 * found in the file PATENTS. All contributing project authors may 10751 * be found in the AUTHORS file in the root of the source tree. 10752 */ 10753 #include "vpx_codec_internal.h" 10754 #include "dixie.h" 10755 #include "tokens.h" 10756 #include 10757 #include 10758 #include 10760 enum 10761 { 10762 EOB_CONTEXT_NODE, 10763 ZERO_CONTEXT_NODE, 10764 ONE_CONTEXT_NODE, 10765 LOW_VAL_CONTEXT_NODE, 10766 TWO_CONTEXT_NODE, 10767 THREE_CONTEXT_NODE, 10768 HIGH_LOW_CONTEXT_NODE, 10769 CAT_ONE_CONTEXT_NODE, 10770 CAT_THREEFOUR_CONTEXT_NODE, 10771 CAT_THREE_CONTEXT_NODE, 10772 CAT_FIVE_CONTEXT_NODE 10773 }; 10774 enum 10775 { 10776 ZERO_TOKEN, 10777 ONE_TOKEN, 10778 TWO_TOKEN, 10779 THREE_TOKEN, 10780 FOUR_TOKEN, 10781 DCT_VAL_CATEGORY1, 10782 DCT_VAL_CATEGORY2, 10783 DCT_VAL_CATEGORY3, 10784 DCT_VAL_CATEGORY4, 10785 DCT_VAL_CATEGORY5, 10786 DCT_VAL_CATEGORY6, 10787 DCT_EOB_TOKEN, 10788 MAX_ENTROPY_TOKENS 10789 }; 10790 struct extrabits 10791 { 10792 short min_val; 10793 short length; 10794 unsigned char probs[12]; 10795 }; 10796 static const unsigned int left_context_index[25] = 10797 { 10798 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10799 4, 4, 5, 5, 6, 6, 7, 7, 8 10800 }; 10801 static const unsigned int above_context_index[25] = 10802 { 10803 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10804 4, 5, 4, 5, 6, 7, 6, 7, 8 10805 }; 10806 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10807 static const unsigned int bands_x[16] = 10808 { 10809 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10810 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10811 }; 10812 #undef X 10813 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10814 { 10815 { 0, -1, { 0, 0, 0, 0, 0, 0, 10816 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10817 { 1, 0, { 0, 0, 0, 0, 0, 0, 10818 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10819 { 2, 0, { 0, 0, 0, 0, 0, 0, 10820 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10821 { 3, 0, { 0, 0, 0, 0, 0, 0, 10822 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10823 { 4, 0, { 0, 0, 0, 0, 0, 0, 10824 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10825 { 5, 0, { 159, 0, 0, 0, 0, 0, 10826 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10827 { 7, 1, { 145, 165, 0, 0, 0, 0, 10828 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10829 { 11, 2, { 140, 148, 173, 0, 0, 0, 10830 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10831 { 19, 3, { 135, 140, 155, 176, 0, 0, 10832 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10833 { 35, 4, { 130, 134, 141, 157, 180, 0, 10834 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10835 { 67, 10, { 129, 130, 133, 140, 153, 177, 10836 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10837 { 0, -1, { 0, 0, 0, 0, 0, 0, 10838 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10839 }; 10840 static const unsigned int zigzag[16] = 10841 { 10842 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10843 }; 10845 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10846 v = (bool_get_bit(bool) ? -value_to_sign \ 10847 : value_to_sign) * dqf[!!c]; 10849 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10850 if (!bool_get(bool, probability)) goto branch; 10852 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10853 if (!bool_get(bool, probability)) \ 10854 { \ 10855 prob = type_probs; \ 10856 if(c<15) {\ 10857 ++c; \ 10858 prob += bands_x[c]; \ 10859 goto branch; \ 10860 }\ 10861 else \ 10862 goto BLOCK_FINISHED; /*for malformed input */\ 10863 } 10865 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10866 DECODE_AND_APPLYSIGN(val) \ 10867 prob = type_probs + (ENTROPY_NODES*2); \ 10868 if(c < 15){\ 10869 b_tokens[zigzag[c]] = v; \ 10870 ++c; \ 10871 goto DO_WHILE; }\ 10872 b_tokens[zigzag[15]] = v; \ 10873 goto BLOCK_FINISHED; 10875 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10876 val += bool_get(bool, extrabits[t].probs[bits_count]) << bits_count; 10878 static int 10879 decode_mb_tokens(struct bool_decoder *bool, 10880 token_entropy_ctx_t left, 10881 token_entropy_ctx_t above, 10882 short *tokens, 10883 enum prediction_mode mode, 10884 coeff_probs_table_t probs, 10885 short factor[TOKEN_BLOCK_TYPES][2]) 10886 { 10887 int i, stop, type; 10888 int c, t, v; 10889 int val, bits_count; 10890 int eob_mask; 10891 short *b_tokens; /* tokens for this block */ 10892 unsigned char *type_probs; /* probabilities for this block type */ 10893 unsigned char *prob; 10894 short *dqf; 10896 eob_mask = 0; 10898 if (mode != B_PRED && mode != SPLITMV) 10899 { 10900 i = 24; 10901 stop = 24; 10902 type = 1; 10903 b_tokens = tokens + 24 * 16; 10904 dqf = factor[TOKEN_BLOCK_Y2]; 10905 } 10906 else 10907 { 10908 i = 0; 10909 stop = 16; 10910 type = 3; 10911 b_tokens = tokens; 10912 dqf = factor[TOKEN_BLOCK_Y1]; 10913 } 10915 /* Save a pointer to the coefficient probs for the current type. 10917 * Need to repeat this whenever type changes. 10918 */ 10919 type_probs = probs[type][0][0]; 10921 BLOCK_LOOP: 10922 t = left[left_context_index[i]] + above[above_context_index[i]]; 10923 c = !type; /* all blocks start at 0 except type 0, which starts 10924 * at 1. */ 10926 prob = type_probs; 10927 prob += t * ENTROPY_NODES; 10929 DO_WHILE: 10930 prob += bands_x[c]; 10931 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], BLOCK_FINISHED); 10933 CHECK_0_: 10934 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10935 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10936 ONE_CONTEXT_NODE_0_); 10937 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10938 LOW_VAL_CONTEXT_NODE_0_); 10939 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10940 HIGH_LOW_CONTEXT_NODE_0_); 10941 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10942 CAT_THREEFOUR_CONTEXT_NODE_0_); 10943 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10944 CAT_FIVE_CONTEXT_NODE_0_); 10945 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10946 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10948 do 10949 { 10950 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count); 10951 bits_count -- ; 10952 } 10953 while (bits_count >= 0); 10955 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10957 CAT_FIVE_CONTEXT_NODE_0_: 10958 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10959 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10960 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10961 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10962 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10963 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10964 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10966 CAT_THREEFOUR_CONTEXT_NODE_0_: 10967 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10968 CAT_THREE_CONTEXT_NODE_0_); 10969 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10970 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10971 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10972 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10973 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10974 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10976 CAT_THREE_CONTEXT_NODE_0_: 10977 val = extrabits[DCT_VAL_CATEGORY3].min_val; 10978 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 10979 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 10980 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 10981 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10983 HIGH_LOW_CONTEXT_NODE_0_: 10984 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 10985 CAT_ONE_CONTEXT_NODE_0_); 10987 val = extrabits[DCT_VAL_CATEGORY2].min_val; 10988 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 10989 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 10990 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10992 CAT_ONE_CONTEXT_NODE_0_: 10993 val = extrabits[DCT_VAL_CATEGORY1].min_val; 10994 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 10995 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10997 LOW_VAL_CONTEXT_NODE_0_: 10998 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 10999 TWO_CONTEXT_NODE_0_); 11000 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 11001 THREE_CONTEXT_NODE_0_); 11002 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 11004 THREE_CONTEXT_NODE_0_: 11005 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 11007 TWO_CONTEXT_NODE_0_: 11008 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 11010 ONE_CONTEXT_NODE_0_: 11011 DECODE_AND_APPLYSIGN(1); 11012 prob = type_probs + ENTROPY_NODES; 11013 if (c < 15) 11014 { 11015 b_tokens[zigzag[c]] = v; 11016 ++c; 11017 goto DO_WHILE; 11018 } 11020 b_tokens[zigzag[15]] = v; 11021 BLOCK_FINISHED: 11022 eob_mask |= (c > 1) << i; 11023 t = (c != !type); // any nonzero data? 11024 eob_mask |= t << 31; 11026 left[left_context_index[i]] = above[above_context_index[i]] = t; 11027 b_tokens += 16; 11029 i++; 11031 if (i < stop) 11032 goto BLOCK_LOOP; 11034 if (i == 25) 11035 { 11036 type = 0; 11037 i = 0; 11038 stop = 16; 11039 type_probs = probs[type][0][0]; 11040 b_tokens = tokens; 11041 dqf = factor[TOKEN_BLOCK_Y1]; 11042 goto BLOCK_LOOP; 11043 } 11045 if (i == 16) 11046 { 11047 type = 2; 11048 type_probs = probs[type][0][0]; 11049 stop = 24; 11050 dqf = factor[TOKEN_BLOCK_UV]; 11051 goto BLOCK_LOOP; 11052 } 11054 return eob_mask; 11055 } 11057 static void 11058 reset_row_context(token_entropy_ctx_t *left) 11059 { 11060 memset(left, 0, sizeof(*left)); 11061 } 11063 static void 11064 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 11065 { 11066 memset(above, 0, cols * sizeof(*above)); 11067 } 11069 static void 11070 reset_mb_context(token_entropy_ctx_t *left, 11071 token_entropy_ctx_t *above, 11072 enum prediction_mode mode) 11073 { 11074 /* Reset the macroblock context on the left and right. We have to 11075 * preserve the context of the second order block if this mode 11076 * would not have updated it. 11077 */ 11078 memset(left, 0, sizeof((*left)[0]) * 8); 11079 memset(above, 0, sizeof((*above)[0]) * 8); 11081 if (mode != B_PRED && mode != SPLITMV) 11082 { 11083 (*left)[8] = 0; 11084 (*above)[8] = 0; 11085 } 11086 } 11088 void 11089 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 11090 unsigned int partition, 11091 unsigned int row, 11092 unsigned int start_col, 11093 unsigned int num_cols) 11094 { 11095 struct token_decoder *tokens = &ctx->tokens[partition]; 11096 short *coeffs = tokens->coeffs + 25 * 16 * start_col; 11097 unsigned int col; 11098 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 11099 + start_col; 11100 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 11101 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 11103 if (row == 0) 11104 reset_above_context(above, num_cols); 11106 if (start_col == 0) 11107 reset_row_context(left); 11109 for (col = start_col; col < start_col + num_cols; col++) 11110 { 11111 memset(coeffs, 0, 25 * 16 * sizeof(short)); 11113 if (mbi->base.skip_coeff) 11114 { 11115 reset_mb_context(left, above, mbi->base.y_mode); 11116 mbi->base.eob_mask = 0; 11117 } 11118 else 11119 { 11120 struct dequant_factors *dqf; 11122 dqf = ctx->dequant_factors + mbi->base.segment_id; 11123 mbi->base.eob_mask = 11124 decode_mb_tokens(&tokens->bool, 11125 *left, *above, 11126 coeffs, 11127 mbi->base.y_mode, 11128 ctx->entropy_hdr.coeff_probs, 11129 dqf->factor); 11130 } 11132 above++; 11133 mbi++; 11134 coeffs += 25 * 16; 11135 } 11136 } 11138 void 11139 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 11140 { 11141 unsigned int partitions = ctx->token_hdr.partitions; 11143 if (ctx->frame_hdr.frame_size_updated) 11144 { 11145 unsigned int i; 11146 unsigned int coeff_row_sz = 11147 ctx->mb_cols * 25 * 16 * sizeof(short); 11149 for (i = 0; i < partitions; i++) 11150 { 11151 free(ctx->tokens[i].coeffs); 11152 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 11153 if (!ctx->tokens[i].coeffs) 11154 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 11155 NULL); 11156 } 11158 free(ctx->above_token_entropy_ctx); 11159 ctx->above_token_entropy_ctx = 11160 calloc(ctx->mb_cols, sizeof(*ctx->above_token_entropy_ctx)); 11162 if (!ctx->above_token_entropy_ctx) 11163 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, NULL); 11164 } 11165 } 11167 void 11168 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 11169 { 11170 int i; 11172 for (i = 0; i < MAX_PARTITIONS; i++) 11173 free(ctx->tokens[i].coeffs); 11175 free(ctx->above_token_entropy_ctx); 11176 } 11178 ---- End code block ---------------------------------------- 11180 20.18. vp8_prob_data.h 11182 ---- Begin code block -------------------------------------- 11184 static const 11185 unsigned char k_coeff_entropy_update_probs[BLOCK_TYPES][COEF_BANDS] 11186 [PREV_COEF_CONTEXTS] 11187 [ENTROPY_NODES] = 11188 { 11189 { 11190 { 11191 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11192 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11193 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11194 }, 11195 { 11196 {176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11197 {223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11198 {249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11200 }, 11201 { 11202 {255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11203 {234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11204 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11205 }, 11206 { 11207 {255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11208 {239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11209 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11210 }, 11211 { 11212 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11213 {251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11214 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11215 }, 11216 { 11217 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11218 {251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11219 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11220 }, 11221 { 11222 {255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255, }, 11223 {250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255, }, 11224 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11225 }, 11226 { 11227 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11228 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11229 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11230 }, 11231 }, 11232 { 11233 { 11234 {217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11235 {225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255, }, 11236 {234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255, }, 11237 }, 11238 { 11239 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11240 {223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11241 {238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, }, 11242 }, 11243 { 11244 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11245 {249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11246 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11247 }, 11248 { 11249 {255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11250 {247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11251 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11252 }, 11253 { 11254 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11255 {252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11256 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11257 }, 11258 { 11259 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11260 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11261 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11262 }, 11263 { 11264 {255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11265 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11266 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11267 }, 11268 { 11269 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11270 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11271 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11272 }, 11273 }, 11274 { 11275 { 11276 {186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255, }, 11277 {234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255, }, 11278 {251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255, }, 11279 }, 11280 { 11281 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11282 {236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11283 {251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255, }, 11284 }, 11285 { 11286 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11287 {254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11288 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11289 }, 11290 { 11291 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11292 {254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11293 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11294 }, 11295 { 11296 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11297 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11298 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11299 }, 11300 { 11301 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11302 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11303 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11304 }, 11305 { 11306 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11307 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11308 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11309 }, 11310 { 11311 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11312 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11313 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11314 }, 11315 }, 11316 { 11317 { 11318 {248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11319 {250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255, }, 11320 {248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255, }, 11321 }, 11322 { 11323 {255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11324 {246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11325 {252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255, }, 11326 }, 11327 { 11328 {255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11329 {248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11330 {253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, }, 11331 }, 11332 { 11333 {255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11334 {245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11335 {253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11336 }, 11337 { 11338 {255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11339 {252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11340 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11341 }, 11342 { 11343 {255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11344 {249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11345 {255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11346 }, 11347 { 11348 {255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11349 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11350 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11351 }, 11352 { 11353 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11354 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11355 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11356 }, 11357 }, 11358 }; 11360 static const 11361 unsigned char k_default_y_mode_probs [] = 11362 { 112, 86, 140, 37}; 11364 static const 11365 unsigned char k_default_uv_mode_probs [] = 11366 { 162, 101, 204}; 11368 static const 11369 unsigned char k_default_coeff_probs [BLOCK_TYPES][COEF_BANDS] 11370 [PREV_COEF_CONTEXTS][ENTROPY_NODES] = 11371 { 11372 { /* block type 0 */ 11373 { /* coeff band 0 */ 11374 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11375 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11376 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11377 }, 11378 { /* coeff band 1 */ 11379 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 11380 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 11381 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 11382 }, 11383 { /* coeff band 2 */ 11384 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 11385 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 11386 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 11387 }, 11388 { /* coeff band 3 */ 11389 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 11390 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 11391 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 11392 }, 11393 { /* coeff band 4 */ 11394 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 11395 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 11396 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 11397 }, 11398 { /* coeff band 5 */ 11399 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 11400 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 11401 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 11402 }, 11403 { /* coeff band 6 */ 11404 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 11405 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 11406 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 11407 }, 11408 { /* coeff band 7 */ 11409 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11410 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11411 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11412 } 11413 }, 11414 { /* block type 1 */ 11415 { /* coeff band 0 */ 11416 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 11417 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 11418 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 11419 }, 11420 { /* coeff band 1 */ 11421 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 11422 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 11423 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 11424 }, 11425 { /* coeff band 2 */ 11426 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 11427 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 11428 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 11429 }, 11430 { /* coeff band 3 */ 11431 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 11432 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 11433 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 11434 }, 11435 { /* coeff band 4 */ 11436 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 11437 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 11438 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 11439 }, 11440 { /* coeff band 5 */ 11441 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 11442 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 11443 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 11444 }, 11445 { /* coeff band 6 */ 11446 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 11447 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 11448 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 11449 }, 11450 { /* coeff band 7 */ 11451 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 11452 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11453 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 11454 } 11455 }, 11456 { /* block type 2 */ 11457 { /* coeff band 0 */ 11458 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 11459 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 11460 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 11461 }, 11462 { /* coeff band 1 */ 11463 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 11464 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 11465 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 11466 }, 11467 { /* coeff band 2 */ 11468 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 11469 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 11470 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 11471 }, 11472 { /* coeff band 3 */ 11473 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 11474 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 11475 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 11476 }, 11477 { /* coeff band 4 */ 11478 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11479 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 11480 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11481 }, 11482 { /* coeff band 5 */ 11483 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11484 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11485 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11486 }, 11487 { /* coeff band 6 */ 11488 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 11489 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 11490 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11491 }, 11492 { /* coeff band 7 */ 11493 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11494 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11495 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11496 } 11497 }, 11498 { /* block type 3 */ 11499 { /* coeff band 0 */ 11500 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 11501 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 11502 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 11503 }, 11504 { /* coeff band 1 */ 11505 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 11506 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 11507 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 11508 }, 11509 { /* coeff band 2 */ 11510 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 11511 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 11512 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 11513 }, 11514 { /* coeff band 3 */ 11515 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 11516 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 11517 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 11518 }, 11519 { /* coeff band 4 */ 11520 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 11521 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 11522 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 11523 }, 11524 { /* coeff band 5 */ 11525 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 11526 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 11527 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 11528 }, 11529 { /* coeff band 6 */ 11530 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 11531 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 11532 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 11534 }, 11535 { /* coeff band 7 */ 11536 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11537 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11538 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11539 } 11540 } 11541 }; 11543 static const 11544 unsigned char k_mv_entropy_update_probs[2][MV_PROB_CNT] = 11545 { 11546 { 11547 237, 11548 246, 11549 253, 253, 254, 254, 254, 254, 254, 11550 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 11551 }, 11552 { 11553 231, 11554 243, 11555 245, 253, 254, 254, 254, 254, 254, 11556 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 11557 } 11558 }; 11560 static const 11561 unsigned char k_default_mv_probs[2][MV_PROB_CNT] = 11562 { 11563 { /* row */ 11564 162, /* is short */ 11565 128, /* sign */ 11566 225, 146, 172, 147, 214, 39, 156, /* short tree */ 11567 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 /* long bits */ 11568 }, 11569 { 11570 164, 11571 128, 11572 204, 170, 119, 235, 140, 230, 228, 11573 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 11575 } 11576 }; 11578 ---- End code block ---------------------------------------- 11579 20.19. vpx_codec_internal.h 11581 ---- Begin code block -------------------------------------- 11583 /* 11584 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 11585 * 11586 * Use of this source code is governed by a BSD-style license 11587 * that can be found in the LICENSE file in the root of the source 11588 * tree. An additional intellectual property rights grant can be 11589 * found in the file PATENTS. All contributing project authors may 11590 * be found in the AUTHORS file in the root of the source tree. 11591 */ 11593 /*!\file decoder_impl.h 11594 * \brief Describes the decoder algorithm interface for algorithm 11595 * implementations. 11596 * 11597 * This file defines the private structures and data types that are 11598 * only relevant to implementing an algorithm, as opposed to using 11599 * it. 11600 * 11601 * To create a decoder algorithm class, an interface structure is put 11602 * into the global namespace: 11603 *
11604	    *     my_codec.c:
11605	    *       vpx_codec_iface_t my_codec = {
11606	    *           "My Codec v1.0",
11607	    *           VPX_CODEC_ALG_ABI_VERSION,
11608	    *           ...
11609	    *       };
11610	    *     
11611 * 11612 * An application instantiates a specific decoder instance by using 11613 * vpx_codec_init() and a pointer to the algorithm's interface 11614 * structure: 11615 *
11616	    *     my_app.c:
11617	    *       extern vpx_codec_iface_t my_codec;
11618	    *       {
11619	    *           vpx_codec_ctx_t algo;
11620	    *           res = vpx_codec_init(&algo, &my_codec);
11621	    *       }
11622	    *     
11623 * 11624 * Once initialized, the instance is manged using other functions 11625 * from the vpx_codec_* family. 11627 */ 11628 #ifndef VPX_CODEC_INTERNAL_H 11629 #define VPX_CODEC_INTERNAL_H 11630 #include "vpx_decoder.h" 11631 #include 11633 /*!\brief Current ABI version number 11634 * 11635 * \internal 11636 * If this file is altered in any way that changes the ABI, this 11637 * value must be bumped. Examples include, but are not limited to, 11638 * changing types, removing or reassigning enums, 11639 * adding/removing/rearranging fields to structures 11640 */ 11641 #define VPX_CODEC_INTERNAL_ABI_VERSION (3) 11643 typedef struct vpx_codec_alg_priv vpx_codec_alg_priv_t; 11645 /*!\brief init function pointer prototype 11646 * 11647 * Performs algorithm-specific initialization of the decoder context. 11648 * This function is called by the generic vpx_codec_init() wrapper 11649 * function, so plugins implementing this interface may trust the 11650 * input parameters to be properly initialized. 11651 * 11652 * \param[in] ctx Pointer to this instance's context 11653 * \retval #VPX_CODEC_OK 11654 * The input stream was recognized and decoder initialized. 11655 * \retval #VPX_CODEC_MEM_ERROR 11656 * Memory operation failed. 11657 */ 11658 typedef vpx_codec_err_t (*vpx_codec_init_fn_t)(vpx_codec_ctx_t *ctx); 11660 /*!\brief destroy function pointer prototype 11661 * 11662 * Performs algorithm-specific destruction of the decoder context. 11663 * This function is called by the generic vpx_codec_destroy() wrapper 11664 * function, so plugins implementing this interface may trust the 11665 * input parameters to be properly initialized. 11666 * 11667 * \param[in] ctx Pointer to this instance's context 11668 * \retval #VPX_CODEC_OK 11669 * The input stream was recognized and decoder initialized. 11670 * \retval #VPX_CODEC_MEM_ERROR 11671 * Memory operation failed. 11672 */ 11673 typedef vpx_codec_err_t (*vpx_codec_destroy_fn_t)( 11674 vpx_codec_alg_priv_t *ctx); 11676 /*!\brief parse stream info function pointer prototype 11677 * 11678 * Performs high level parsing of the bitstream. This function is 11679 * called by the generic vpx_codec_parse_stream() wrapper function, 11680 * so plugins implementing this interface may trust the input 11681 * parameters to be properly initialized. 11682 * 11683 * \param[in] data Pointer to a block of data to parse 11684 * \param[in] data_sz Size of the data buffer 11685 * \param[in,out] si Pointer to stream info to update. The size 11686 * member \ref MUST be properly initialized, 11687 * but \ref MAY be clobbered by the 11688 * algorithm. This parameter \ref MAY be 11689 * NULL. 11690 * 11691 * \retval #VPX_CODEC_OK 11692 * Bitstream is parsable and stream information updated 11693 */ 11694 typedef vpx_codec_err_t (*vpx_codec_peek_si_fn_t)( 11695 const uint8_t *data, 11696 unsigned int data_sz, 11697 vpx_codec_stream_info_t *si); 11699 /*!\brief Return information about the current stream. 11700 * 11701 * Returns information about the stream that has been parsed during 11702 * decoding. 11703 * 11704 * \param[in] ctx Pointer to this instance's context 11705 * \param[in,out] si Pointer to stream info to update. The size 11706 * member \ref MUST be properly initialized, 11707 * but \ref MAY be clobbered by the 11708 * algorithm. This parameter \ref MAY be 11709 * NULL. 11710 * 11711 * \retval #VPX_CODEC_OK 11712 * Bitstream is parsable and stream information updated 11713 */ 11714 typedef vpx_codec_err_t (*vpx_codec_get_si_fn_t)( 11715 vpx_codec_alg_priv_t *ctx, 11716 vpx_codec_stream_info_t *si); 11718 /*!\brief control function pointer prototype 11719 * 11720 * This function is used to exchange algorithm specific data with the 11721 * decoder instance. This can be used to implement features specific 11722 * to a particular algorithm. 11723 * 11724 * This function is called by the generic vpx_codec_control() wrapper 11725 * function, so plugins implementing this interface may trust the 11726 * input parameters to be properly initialized. However, this 11727 * interface does not provide type safety for the exchanged data or 11728 * assign meanings to the control codes. Those details should be 11729 * specified in the algorithm's header file. In particular, the 11730 * ctrl_id parameter is guaranteed to exist in the algorithm's 11731 * control mapping table, and the data paramter may be NULL. 11732 * 11733 * 11734 * \param[in] ctx Pointer to this instance's context 11735 * \param[in] ctrl_id Algorithm specific control identifier 11736 * \param[in,out] data Data to exchange with algorithm instance. 11737 * 11738 * \retval #VPX_CODEC_OK 11739 * The internal state data was deserialized. 11740 */ 11741 typedef vpx_codec_err_t (*vpx_codec_control_fn_t)( 11742 vpx_codec_alg_priv_t *ctx, 11743 int ctrl_id, 11744 va_list ap); 11746 /*!\brief control function pointer mapping 11747 * 11748 * This structure stores the mapping between control identifiers and 11749 * implementing functions. Each algorithm provides a list of these 11750 * mappings. This list is searched by the vpx_codec_control() wrapper 11751 * function to determine which function to invoke. The special 11752 * value {0, NULL} is used to indicate end-of-list, and must be 11753 * present. The special value {0, } can be used as a 11754 * catch-all mapping. This implies that ctrl_id values chosen by the 11755 * algorithm \ref MUST be non-zero. 11756 */ 11757 typedef const struct 11758 { 11759 int ctrl_id; 11760 vpx_codec_control_fn_t fn; 11761 } vpx_codec_ctrl_fn_map_t; 11763 /*!\brief decode data function pointer prototype 11764 * 11765 * Processes a buffer of coded data. If the processing results in a 11766 * new decoded frame becoming available, #VPX_CODEC_CB_PUT_SLICE and 11767 * #VPX_CODEC_CB_PUT_FRAME events are generated as appropriate. This 11768 * function is called by the generic vpx_codec_decode() wrapper 11769 * function, so plugins implementing this interface may trust the 11770 * input parameters to be properly initialized. 11771 * 11772 * \param[in] ctx Pointer to this instance's context 11773 * \param[in] data Pointer to this block of new coded data. If 11774 * NULL, a #VPX_CODEC_CB_PUT_FRAME event is 11775 * posted for the previously decoded frame. 11776 * \param[in] data_sz Size of the coded data, in bytes. 11777 * 11778 * \return Returns #VPX_CODEC_OK if the coded data was processed 11779 * completely and future pictures can be decoded without 11780 * error. Otherwise, see the descriptions of the other error 11781 * codes in ::vpx_codec_err_t for recoverability 11782 * capabilities. 11783 */ 11784 typedef vpx_codec_err_t (*vpx_codec_decode_fn_t)( 11785 vpx_codec_alg_priv_t *ctx, 11786 const uint8_t *data, 11787 unsigned int data_sz, 11788 void *user_priv, 11789 long deadline); 11791 /*!\brief Decoded frames iterator 11792 * 11793 * Iterates over a list of the frames available for display. The 11794 * iterator storage should be initialized to NULL to start the 11795 * iteration. Iteration is complete when this function returns NULL. 11796 * 11797 * The list of available frames becomes valid upon completion of the 11798 * vpx_codec_decode call, and remains valid until the next call to 11799 * vpx_codec_decode. 11800 * 11801 * \param[in] ctx Pointer to this instance's context 11802 * \param[in out] iter Iterator storage, initialized to NULL 11803 * 11804 * \return Returns a pointer to an image, if one is ready for 11805 * display. Frames produced will always be in PTS 11806 * (presentation time stamp) order. 11807 */ 11808 typedef vpx_image_t*(*vpx_codec_get_frame_fn_t)( 11809 vpx_codec_alg_priv_t *ctx, 11810 vpx_codec_iter_t *iter); 11812 /*\brief e_xternal Memory Allocation memory map get iterator 11813 * 11814 * Iterates over a list of the memory maps requested by the decoder. 11815 * The iterator storage should be initialized to NULL to start the 11816 * iteration. Iteration is complete when this function returns NULL. 11818 * 11819 * \param[in out] iter Iterator storage, initialized to NULL 11820 * 11821 * \return Returns a pointer to an memory segment descriptor, or NULL 11822 * to indicate end-of-list. 11823 */ 11824 typedef vpx_codec_err_t (*vpx_codec_get_mmap_fn_t)( 11825 const vpx_codec_ctx_t *ctx, 11826 vpx_codec_mmap_t *mmap, 11827 vpx_codec_iter_t *iter); 11829 /*\brief e_xternal Memory Allocation memory map set iterator 11830 * 11831 * Sets a memory descriptor inside the decoder instance. 11832 * 11833 * \param[in] ctx Pointer to this instance's context 11834 * \param[in] mmap Memory map to store. 11835 * 11836 * \retval #VPX_CODEC_OK 11837 * The memory map was accepted and stored. 11838 * \retval #VPX_CODEC_MEM_ERROR 11839 * The memory map was rejected. 11840 */ 11841 typedef vpx_codec_err_t (*vpx_codec_set_mmap_fn_t)( 11842 vpx_codec_ctx_t *ctx, 11843 const vpx_codec_mmap_t *mmap); 11845 typedef vpx_codec_err_t (*vpx_codec_encode_fn_t)( 11846 vpx_codec_alg_priv_t *ctx, 11847 const vpx_image_t *img, 11848 vpx_codec_pts_t pts, 11849 unsigned long duration, 11850 vpx_enc_frame_flags_t flags, 11851 unsigned long deadline); 11852 typedef const vpx_codec_cx_pkt_t*(*vpx_codec_get_cx_data_fn_t)( 11853 vpx_codec_alg_priv_t *ctx, 11854 vpx_codec_iter_t *iter); 11856 typedef vpx_codec_err_t 11857 (*vpx_codec_enc_config_set_fn_t)( 11858 vpx_codec_alg_priv_t *ctx, 11859 const vpx_codec_enc_cfg_t *cfg); 11860 typedef vpx_fixed_buf_t * 11861 (*vpx_codec_get_global_headers_fn_t)(vpx_codec_alg_priv_t *ctx); 11863 typedef vpx_image_t * 11864 (*vpx_codec_get_preview_frame_fn_t)(vpx_codec_alg_priv_t *ctx); 11866 /*!\brief usage configuration mapping 11867 * 11868 * This structure stores the mapping between usage identifiers and 11869 * configuration structures. Each algorithm provides a list of these 11870 * mappings. This list is searched by the 11871 * vpx_codec_enc_config_default() wrapper function to determine which 11872 * config to return. The special value {-1, {0}} is used to indicate 11873 * end-of-list, and must be present. At least one mapping must be 11874 * present, in addition to the end-of-list. 11875 * 11876 */ 11877 typedef const struct 11878 { 11879 int usage; 11880 vpx_codec_enc_cfg_t cfg; 11881 } vpx_codec_enc_cfg_map_t; 11883 #define NOT_IMPLEMENTED 0 11885 /*!\brief Decoder algorithm interface interface 11886 * 11887 * All decoders \ref MUST expose a variable of this type. 11888 */ 11889 struct vpx_codec_iface 11890 { 11891 const char *name; 11892 int abi_version; 11893 vpx_codec_caps_t caps; 11894 vpx_codec_init_fn_t init; 11895 vpx_codec_destroy_fn_t destroy; 11896 vpx_codec_ctrl_fn_map_t *ctrl_maps; 11897 vpx_codec_get_mmap_fn_t get_mmap; 11898 vpx_codec_set_mmap_fn_t set_mmap; 11899 struct 11900 { 11901 vpx_codec_peek_si_fn_t peek_si; 11902 vpx_codec_get_si_fn_t get_si; 11903 vpx_codec_decode_fn_t decode; 11904 vpx_codec_get_frame_fn_t get_frame; 11905 } dec; 11906 struct 11907 { 11908 vpx_codec_enc_cfg_map_t *cfg_maps; 11909 vpx_codec_encode_fn_t encode; 11910 vpx_codec_get_cx_data_fn_t get_cx_data; 11911 vpx_codec_enc_config_set_fn_t cfg_set; 11912 vpx_codec_get_global_headers_fn_t get_glob_hdrs; 11913 vpx_codec_get_preview_frame_fn_t get_preview; 11914 } enc; 11915 }; 11917 /*!\brief Callback function pointer / user data pair storage */ 11918 typedef struct vpx_codec_priv_cb_pair 11919 { 11920 union 11921 { 11922 vpx_codec_put_frame_cb_fn_t put_frame; 11923 vpx_codec_put_slice_cb_fn_t put_slice; 11924 }; 11925 void *user_priv; 11926 } vpx_codec_priv_cb_pair_t; 11928 /*!\brief Instance private storage 11929 * 11930 * This structure is allocated by the algorithm's init function. It 11931 * can be extended in one of two ways. First, a second, algorithm 11932 * specific structure can be allocated and the priv member pointed to 11933 * it. Alternatively, this structure can be made the first member of 11934 * the algorithm specific structure, and the pointer casted to the 11935 * proper type. 11936 */ 11937 struct vpx_codec_priv 11938 { 11939 unsigned int sz; 11940 vpx_codec_iface_t *iface; 11941 struct vpx_codec_alg_priv *alg_priv; 11942 const char *err_detail; 11943 vpx_codec_flags_t init_flags; 11944 struct 11945 { 11946 vpx_codec_priv_cb_pair_t put_frame_cb; 11947 vpx_codec_priv_cb_pair_t put_slice_cb; 11948 } dec; 11949 struct 11950 { 11951 int tbd; 11952 struct vpx_fixed_buf cx_data_dst_buf; 11953 unsigned int cx_data_pad_before; 11954 unsigned int cx_data_pad_after; 11955 vpx_codec_cx_pkt_t cx_data_pkt; 11956 } enc; 11957 }; 11958 #undef VPX_CTRL_USE_TYPE 11959 #define VPX_CTRL_USE_TYPE(id, typ) \ 11960 static typ id##__value(va_list args) \ 11961 {return va_arg(args, typ);} \ 11962 static typ id##__convert(void *x)\ 11963 {\ 11964 union\ 11965 {\ 11966 void *x;\ 11967 typ d;\ 11968 } u;\ 11969 u.x = x;\ 11970 return u.d;\ 11971 } 11973 #undef VPX_CTRL_USE_TYPE_DEPRECATED 11974 #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \ 11975 static typ id##__value(va_list args) \ 11976 {return va_arg(args, typ);} \ 11977 static typ id##__convert(void *x)\ 11978 {\ 11979 union\ 11980 {\ 11981 void *x;\ 11982 typ d;\ 11983 } u;\ 11984 u.x = x;\ 11985 return u.d;\ 11986 } 11988 #define CAST(id, arg) id##__value(arg) 11989 #define RECAST(id, x) id##__convert(x) 11991 /* Internal Utility Functions 11992 * 11993 * The following functions are indended to be used inside algorithms 11994 * as utilities for manipulating vpx_codec_* data structures. 11995 */ 11996 struct vpx_codec_pkt_list 11997 { 11998 unsigned int cnt; 11999 unsigned int max; 12000 struct vpx_codec_cx_pkt pkts[1]; 12001 }; 12003 #define vpx_codec_pkt_list_decl(n)\ 12004 union {struct vpx_codec_pkt_list head;\ 12005 struct {struct vpx_codec_pkt_list head;\ 12006 struct vpx_codec_cx_pkt pkts[n];} alloc;} 12008 #define vpx_codec_pkt_list_init(m)\ 12009 (m)->alloc.head.cnt = 0,\ 12010 (m)->alloc.head.max = \ 12011 sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0]) 12013 int 12014 vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *, 12015 const struct vpx_codec_cx_pkt *); 12017 const vpx_codec_cx_pkt_t* 12018 vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list, 12019 vpx_codec_iter_t *iter); 12021 #include 12022 #include 12023 struct vpx_internal_error_info 12024 { 12025 vpx_codec_err_t error_code; 12026 int has_detail; 12027 char detail[80]; 12028 int setjmp; 12029 jmp_buf jmp; 12030 }; 12032 static void vpx_internal_error(struct vpx_internal_error_info *info, 12033 vpx_codec_err_t error, 12034 const char *fmt, 12035 ...) 12036 { 12037 va_list ap; 12039 info->error_code = error; 12040 info->has_detail = 0; 12042 if (fmt) 12043 { 12044 size_t sz = sizeof(info->detail); 12046 info->has_detail = 1; 12047 va_start(ap, fmt); 12048 vsnprintf(info->detail, sz - 1, fmt, ap); 12049 va_end(ap); 12050 info->detail[sz-1] = '\0'; 12052 } 12054 if (info->setjmp) 12055 longjmp(info->jmp, info->error_code); 12056 } 12057 #endif 12059 ---- End code block ---------------------------------------- 12061 20.20. vpx_decoder.h 12063 ---- Begin code block -------------------------------------- 12065 /* 12066 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 12067 * 12068 * Use of this source code is governed by a BSD-style license 12069 * that can be found in the LICENSE file in the root of the source 12070 * tree. An additional intellectual property rights grant can be 12071 * found in the file PATENTS. All contributing project authors may 12072 * be found in the AUTHORS file in the root of the source tree. 12073 */ 12075 /*!\defgroup decoder Decoder Algorithm Interface 12076 * \ingroup codec 12077 * This abstraction allows applications using this decoder to easily 12078 * support multiple video formats with minimal code duplication. This 12079 * section describes the interface common to all decoders. 12080 * @{ 12081 */ 12083 /*!\file vpx_decoder.h 12084 * \brief Describes the decoder algorithm interface to applications. 12085 * 12086 * This file describes the interface between an application and a 12087 * video decoder algorithm. 12088 * 12089 */ 12090 #ifdef __cplusplus 12091 extern "C" { 12092 #endif 12094 #ifndef VPX_DECODER_H 12095 #define VPX_DECODER_H 12096 #include "vpx_codec.h" 12097 /*!\brief Current ABI version number 12098 * 12099 * \internal 12100 * If this file is altered in any way that changes the ABI, this 12101 * value must be bumped. Examples include, but are not limited 12102 * to, changing types, removing or reassigning enums, 12103 * adding/removing/rearranging fields to structures 12104 */ 12105 #define VPX_DECODER_ABI_VERSION (2 + VPX_CODEC_ABI_VERSION) 12107 /*! \brief Decoder capabilities bitfield 12108 * 12109 * Each decoder advertises the capabilities it supports as part 12110 * of its ::vpx_codec_iface_t interface structure. Capabilities 12111 * are extra interfaces or functionality, and are not required 12112 * to be supported by a decoder. 12113 * 12114 * The available flags are specifiedby VPX_CODEC_CAP_* defines. 12115 */ 12116 #define VPX_CODEC_CAP_PUT_SLICE 0x10000 /**< Will issue put_slice 12117 callbacks */ 12118 #define VPX_CODEC_CAP_PUT_FRAME 0x20000 /**< Will issue put_frame 12119 callbacks */ 12120 #define VPX_CODEC_CAP_POSTPROC 0x40000 /**< Can postprocess decoded 12121 frame */ 12123 /*! \brief Initialization-time Feature Enabling 12124 * 12125 * Certain codec features must be known at initialization time, 12126 * to allow for proper memory allocation. 12127 * 12128 * The available flags are specified by VPX_CODEC_USE_* defines. 12129 */ 12130 #define VPX_CODEC_USE_POSTPROC 0x10000 /**< Postprocess decoded 12131 frame */ 12133 /*!\brief Stream properties 12134 * 12135 * This structure is used to query or set properties of the 12136 * decoded stream. Algorithms may extend this structure with data 12137 * specific to their bitstream by setting the sz member 12138 * appropriately. 12139 */ 12140 typedef struct vpx_codec_stream_info 12141 { 12142 unsigned int sz; /**< Size of this structure */ 12143 unsigned int w; /**< Width (or 0 for unknown/default) */ 12144 unsigned int h; /**< Height (or 0 for unknown/default) */ 12145 unsigned int is_kf; /**< Current frame is a keyframe */ 12146 } vpx_codec_stream_info_t; 12148 /* REQUIRED FUNCTIONS 12149 * 12150 * The following functions are required to be implemented for all 12151 * decoders. They represent the base case functionality expected 12152 * of all decoders. 12153 */ 12155 /*!\brief Initialization Configurations 12156 * 12157 * This structure is used to pass init time configuration options 12158 * to the decoder. 12159 */ 12160 typedef struct vpx_codec_dec_cfg 12161 { 12162 unsigned int threads; /**< Maximum number of threads to use, 12163 default 1 */ 12164 unsigned int w; /**< Width */ 12165 unsigned int h; /**< Height */ 12166 } vpx_codec_dec_cfg_t; /**< alias for struct vpx_codec_dec_cfg */ 12168 /*!\brief Initialize a decoder instance 12169 * 12170 * Initializes a decoder context using the given interface. 12171 * Applications should call the vpx_codec_dec_init convenience 12172 * macro instead of this function directly, to ensure that the 12173 * ABI version number parameter is properly initialized. 12174 * 12175 * In XMA mode (activated by setting VPX_CODEC_USE_XMA in the 12176 * flags parameter), the storage pointed to by the cfg parameter 12177 * must be kept readable and stable until all memory maps have 12178 * been set. 12179 * 12180 * \param[in] ctx Pointer to this instance's context. 12181 * \param[in] iface Pointer to the alogrithm interface to 12182 * use. 12183 * \param[in] cfg Configuration to use, if known. May be 12184 * NULL. 12185 * \param[in] flags Bitfield of VPX_CODEC_USE_* flags 12186 * \param[in] ver ABI version number. Must be set to 12187 * VPX_DECODER_ABI_VERSION 12188 * \retval #VPX_CODEC_OK 12189 * The decoder algorithm initialized. 12190 * \retval #VPX_CODEC_MEM_ERROR 12191 * Memory allocation failed. 12192 */ 12193 vpx_codec_err_t vpx_codec_dec_init_ver( 12194 vpx_codec_ctx_t *ctx, 12195 vpx_codec_iface_t *iface, 12196 vpx_codec_dec_cfg_t *cfg, 12197 vpx_codec_flags_t flags, 12198 int ver); 12200 /*!\brief Convenience macro for vpx_codec_dec_init_ver() 12201 * 12202 * Ensures the ABI version parameter is properly set. 12203 */ 12204 #define vpx_codec_dec_init(ctx, iface, cfg, flags) \ 12205 vpx_codec_dec_init_ver(ctx, iface, cfg, flags, \ 12206 VPX_DECODER_ABI_VERSION) 12208 /*!\brief Parse stream info from a buffer 12209 * 12210 * Performs high level parsing of the bitstream. Construction of 12211 * a decoder context is not necessary. Can be used to determine 12212 * if the bitstream is of the proper format, and to extract 12213 * information from the stream. 12214 * 12215 * \param[in] iface Pointer to the alogrithm interface 12216 * \param[in] data Pointer to a block of data to parse 12217 * \param[in] data_sz Size of the data buffer 12218 * \param[in,out] si Pointer to stream info to update. The 12219 * size member 12220 * \ref MUST be properly initialized, but 12221 * \ref MAY be clobbered by the 12222 * algorithm. This parameter \ref MAY be 12223 * NULL. 12224 * 12225 * \retval #VPX_CODEC_OK 12226 * Bitstream is parsable and stream information updated 12227 */ 12228 vpx_codec_err_t vpx_codec_peek_stream_info( 12229 vpx_codec_iface_t *iface, 12230 const uint8_t *data, 12231 unsigned int data_sz, 12232 vpx_codec_stream_info_t *si); 12234 /*!\brief Return information about the current stream. 12235 * 12236 * Returns information about the stream that has been parsed 12237 * during decoding. 12238 * 12239 * \param[in] ctx Pointer to this instance's context 12240 * \param[in,out] si Pointer to stream info to update. The 12241 * size member \ref MUST be properly 12242 * initialized, but \ref MAY be clobbered 12243 * by the algorithm. This parameter \ref 12244 * MAY be NULL. 12245 * 12246 * \retval #VPX_CODEC_OK 12247 * Bitstream is parsable and stream information updated 12248 */ 12249 vpx_codec_err_t vpx_codec_get_stream_info( 12250 vpx_codec_ctx_t *ctx, 12251 vpx_codec_stream_info_t *si); 12253 /*!\brief Decode data 12254 * 12255 * Processes a buffer of coded data. If the processing results in 12256 * a new decoded frame becoming available, PUT_SLICE and 12257 * PUT_FRAME events may be generated, as appropriate. Encoded 12258 * data \ref MUST be passed in DTS (decode time stamp) order. 12259 * Frames produced will always be in PTS (presentation time 12260 * stamp) order. 12261 * 12262 * \param[in] ctx Pointer to this instance's context 12263 * \param[in] data Pointer to this block of new coded 12264 * data. If NULL, a 12265 * VPX_CODEC_CB_PUT_FRAME event is posted 12266 * for the previously decoded frame. 12267 * \param[in] data_sz Size of the coded data, in bytes. 12268 * \param[in] user_priv Application specific data to associate 12269 * with this frame. 12270 * \param[in] deadline Soft deadline the decoder should 12271 * attempt to meet, in us. Set to zero 12272 * for unlimited. 12273 * 12274 * \return Returns #VPX_CODEC_OK if the coded data was processed 12275 * completely and future pictures can be decoded without 12276 * error. Otherwise, see the descriptions of the other 12277 * error codes in ::vpx_codec_err_t for recoverability 12278 * capabilities. 12279 */ 12280 vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, 12281 const uint8_t *data, 12282 unsigned int data_sz, 12283 void *user_priv, 12284 long deadline); 12286 /*!\brief Decoded frames iterator 12287 * 12288 * Iterates over a list of the frames available for display. The 12289 * iterator storage should be initialized to NULL to start the 12290 * iteration. Iteration is complete when this function returns 12291 * NULL. 12292 * 12293 * The list of available frames becomes valid upon completion of 12294 * the vpx_codec_decode call, and remains valid until the next 12295 * call to vpx_codec_decode. 12296 * 12297 * \param[in] ctx Pointer to this instance's context 12298 * \param[in,out] iter Iterator storage, initialized to NULL 12299 * 12300 * \return Returns a pointer to an image, if one is ready for 12301 * display. Frames produced will always be in PTS 12302 * (presentation time stamp) order. 12303 */ 12304 vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, 12305 vpx_codec_iter_t *iter); 12307 /*!\defgroup cap_put_frame Frame-Based Decoding Functions 12308 * 12309 * The following functions are required to be implemented for all 12310 * decoders that advertise the VPX_CODEC_CAP_PUT_FRAME 12311 * capability. Calling these functions for codecs that don't 12312 * advertise this capability will result in an error code being 12313 * returned, usually VPX_CODEC_ERROR 12314 * @{ 12315 */ 12317 /*!\brief put frame callback prototype 12318 * 12319 * This callback is invoked by the decoder to notify the 12320 * application of the availability of decoded image data. 12321 */ 12322 typedef void (*vpx_codec_put_frame_cb_fn_t)( 12323 void *user_priv, 12324 const vpx_image_t *img); 12326 /*!\brief Register for notification of frame completion. 12327 * 12328 * Registers a given function to be called when a decoded frame 12329 * is available. 12330 * 12331 * \param[in] ctx Pointer to this instance's context 12332 * \param[in] cb Pointer to the callback function 12333 * \param[in] user_priv User's private data 12334 * 12335 * \retval #VPX_CODEC_OK 12336 * Callback successfully registered. 12337 * \retval #VPX_CODEC_ERROR 12338 * Decoder context not initialized, or algorithm not capable 12339 * of posting slice completion. 12340 */ 12341 vpx_codec_err_t vpx_codec_register_put_frame_cb( 12342 vpx_codec_ctx_t *ctx, 12343 vpx_codec_put_frame_cb_fn_t cb, 12344 void *user_priv); 12346 /*!@} - end defgroup cap_put_frame */ 12348 /*!\defgroup cap_put_slice Slice-Based Decoding Functions 12349 * 12350 * The following functions are required to be implemented for all 12351 * decoders that advertise the VPX_CODEC_CAP_PUT_SLICE 12352 * capability. Calling these functions for codecs that don't 12353 * advertise this capability will result in an error code being 12354 * returned, usually VPX_CODEC_ERROR 12355 * @{ 12356 */ 12358 /*!\brief put slice callback prototype 12359 * 12360 * This callback is invoked by the decoder to notify the 12361 * application of the availability of partially decoded image 12362 * data. 12363 */ 12364 typedef void (*vpx_codec_put_slice_cb_fn_t)( 12365 void *user_priv, 12366 const vpx_image_t *img, 12367 const vpx_image_rect_t *valid, 12368 const vpx_image_rect_t *update); 12370 /*!\brief Register for notification of slice completion. 12371 * 12372 * Registers a given function to be called when a decoded slice 12373 * is available. 12374 * 12375 * \param[in] ctx Pointer to this instance's context 12376 * \param[in] cb Pointer to the callback function 12377 * \param[in] user_priv User's private data 12378 * 12379 * \retval #VPX_CODEC_OK 12380 * Callback successfully registered. 12381 * \retval #VPX_CODEC_ERROR 12382 * Decoder context not initialized, or algorithm not capable 12383 * of posting slice completion. 12384 */ 12385 vpx_codec_err_t vpx_codec_register_put_slice_cb( 12386 vpx_codec_ctx_t *ctx, 12387 vpx_codec_put_slice_cb_fn_t cb, 12388 void *user_priv); 12390 /*!@} - end defgroup cap_put_slice*/ 12392 /*!@} - end defgroup decoder*/ 12394 #endif 12396 #ifdef __cplusplus 12397 } 12398 #endif 12400 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 12401 #include "vpx_decoder_compat.h" 12402 #endif 12404 ---- End code block ---------------------------------------- 12406 20.21. vpx_integer.h 12408 ---- Begin code block -------------------------------------- 12410 /* 12411 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 12412 * 12413 * Use of this source code is governed by a BSD-style license 12414 * that can be found in the LICENSE file in the root of the source 12415 * tree. An additional intellectual property rights grant can be 12416 * found in the file PATENTS. All contributing project authors may 12417 * be found in the AUTHORS file in the root of the source tree. 12418 */ 12420 #ifndef VPX_INTEGER_H 12421 #define VPX_INTEGER_H 12423 /* get ptrdiff_t, size_t, wchar_t, NULL */ 12424 #include 12426 #if defined(_MSC_VER) || defined(VPX_EMULATE_INTTYPES) 12427 typedef signed char int8_t; 12428 typedef signed short int16_t; 12429 typedef signed int int32_t; 12431 typedef unsigned char uint8_t; 12432 typedef unsigned short uint16_t; 12433 typedef unsigned int uint32_t; 12435 #if defined(_MSC_VER) 12436 typedef signed __int64 int64_t; 12437 typedef unsigned __int64 uint64_t; 12438 #define PRId64 "I64d" 12439 #endif 12441 #ifdef HAVE_ARMV6 12442 typedef unsigned int int_fast16_t; 12443 #else 12444 typedef signed short int_fast16_t; 12445 #endif 12446 typedef signed char int_fast8_t; 12447 typedef unsigned char uint_fast8_t; 12449 #ifndef _UINTPTR_T_DEFINED 12450 typedef unsigned int uintptr_t; 12451 #endif 12453 #else 12455 /* Most platforms have the C99 standard integer types. */ 12457 #if defined(__cplusplus) && !defined(__STDC_FORMAT_MACROS) 12458 #define __STDC_FORMAT_MACROS 12459 #endif 12460 #include 12461 #include 12463 #endif 12465 #endif 12466 ---- End code block ---------------------------------------- 12468 21. References 12470 [ITU-R_BT.601] 12471 International Telecommunication Union, "ITU BT.601: Studio 12472 encoding parameters of digital television for standard 4:3 12473 and wide screen 16:9 aspect ratios", January 2007. 12475 [Bell] Bell, T., Cleary, J., and I. Witten, "Text Compression", 12476 1990. 12478 [Kernighan] 12479 Kernighan, B. and D. Ritchie, "The C Programming Language 12480 (2nd edition)", April 1988. 12482 [Loeffler] 12483 Loeffler, C., Ligtenberg , A., and G. Moschytz, "Practical 12484 Fast 1-D DCT Algorithms with 11 Multiplications", 12485 May 1989. 12487 [Shannon] Shannon, C., "A Mathematical Theory of Communication", 12488 Bell System Technical Journal Vol. 27, pp. 379-423, 623- 12489 656, July, October 1948. 12491 Authors' Addresses 12493 James Bankoski 12494 Google, Inc. 12496 Email: jimbankoski@google.com 12498 Paul Wilkins 12499 Google, Inc. 12501 Email: paulwilkins@google.com 12503 Yaowu Xu 12504 Google, Inc. 12506 Email: yaowu@google.com