idnits 2.17.00 (12 Aug 2021) /tmp/idnits20678/draft-bankoski-vp8-bitstream-03.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** 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 11692: '... member \ref MUST be properly init...' RFC 2119 keyword, line 11693: '... but \ref MAY be clobbered by t...' RFC 2119 keyword, line 11694: '... algorithm. This parameter \ref MAY be...' RFC 2119 keyword, line 11712: '... member \ref MUST be properly init...' RFC 2119 keyword, line 11713: '... 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 640 has weird spacing: '...typedef sign...' == Line 649 has weird spacing: '...def int int32...' == Line 6206 has weird spacing: '...ned int split...' == Line 6647 has weird spacing: '...decoder bool;...' == Line 7082 has weird spacing: '...tioning parti...' == (34 more instances...) -- The document date (June 15, 2011) is 3992 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 9964 == Missing Reference: 'A' is mentioned on line 2053, but not defined == Missing Reference: 'L' is mentioned on line 2053, but not defined -- Looks like a reference, but probably isn't: '8' on line 11090 == Missing Reference: '-1' is mentioned on line 9547, but not defined -- Looks like a reference, but probably isn't: '0' on line 12017 -- Looks like a reference, but probably isn't: '1' on line 12006 -- Looks like a reference, but probably isn't: '4' on line 9937 -- Looks like a reference, but probably isn't: '7' on line 9214 -- Looks like a reference, but probably isn't: '9' on line 8673 -- Looks like a reference, but probably isn't: '2' on line 11567 -- Looks like a reference, but probably isn't: '5' on line 9584 -- Looks like a reference, but probably isn't: '6' on line 9214 -- Looks like a reference, but probably isn't: '16' on line 10846 -- Looks like a reference, but probably isn't: '12' on line 10800 == Missing Reference: 'MVPcount' is mentioned on line 4893, but not defined == Missing Reference: 'MVPsign' is mentioned on line 4942, but not defined == Missing Reference: '-4096' is mentioned on line 5073, but not defined -- Looks like a reference, but probably isn't: '4095' on line 5073 -- Looks like a reference, but probably isn't: '128' on line 6317 == Missing Reference: 'SIGN' is mentioned on line 8154, but not defined == Missing Reference: 'NEAREST' is mentioned on line 8469, but not defined == Missing Reference: 'NEAR' is mentioned on line 8473, but not defined == Missing Reference: 'BEST' is mentioned on line 8488, but not defined -- Looks like a reference, but probably isn't: '15' on line 8490 -- Looks like a reference, but probably isn't: '10' on line 8673 -- Looks like a reference, but probably isn't: '18' on line 8816 -- Looks like a reference, but probably isn't: '14' on line 8828 == Missing Reference: '-stride' is mentioned on line 9070, but not defined == Missing Reference: '-2' is mentioned on line 9546, but not defined -- Looks like a reference, but probably isn't: '25' on line 10807 -- Looks like a reference, but probably isn't: '80' on line 12033 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 J. Koleszar 4 Intended status: Informational L. Quillio 5 Expires: December 17, 2011 J. Salonen 6 P. Wilkins 7 Y. Xu 8 Google Inc. 9 June 15, 2011 11 VP8 Data Format and Decoding Guide 12 draft-bankoski-vp8-bitstream-03 14 Abstract 16 This document describes the VP8 compressed video data format, 17 together with a discussion of the decoding procedure for the format. 19 Status of this Memo 21 This Internet-Draft is submitted in full conformance with the 22 provisions of BCP 78 and BCP 79. 24 Internet-Drafts are working documents of the Internet Engineering 25 Task Force (IETF). Note that other groups may also distribute 26 working documents as Internet-Drafts. The list of current Internet- 27 Drafts is at http://datatracker.ietf.org/drafts/current/. 29 Internet-Drafts are draft documents valid for a maximum of six months 30 and may be updated, replaced, or obsoleted by other documents at any 31 time. It is inappropriate to use Internet-Drafts as reference 32 material or to cite them other than as "work in progress." 34 This Internet-Draft will expire on December 17, 2011. 36 Copyright Notice 38 Copyright (c) 2011 IETF Trust and the persons identified as the 39 document authors. All rights reserved. 41 This document is subject to BCP 78 and the IETF Trust's Legal 42 Provisions Relating to IETF Documents 43 (http://trustee.ietf.org/license-info) in effect on the date of 44 publication of this document. Please review these documents 45 carefully, as they describe your rights and restrictions with respect 46 to this document. 48 Table of Contents 50 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 5 51 2. Format Overview . . . . . . . . . . . . . . . . . . . . . . . 7 52 3. Compressed Frame Types . . . . . . . . . . . . . . . . . . . 9 53 4. Overview of Compressed Data Format . . . . . . . . . . . . . 10 54 5. Overview of the Decoding Process . . . . . . . . . . . . . . 12 55 6. Description of Algorithms . . . . . . . . . . . . . . . . . . 17 56 7. Boolean Entropy Decoder . . . . . . . . . . . . . . . . . . . 20 57 7.1. Underlying Theory of Coding . . . . . . . . . . . . . . 21 58 7.2. Practical Algorithm Description . . . . . . . . . . . . 22 59 7.3. Actual Implementation . . . . . . . . . . . . . . . . . 24 60 8. Compressed Data Components . . . . . . . . . . . . . . . . . 29 61 8.1. Tree Coding Implementation . . . . . . . . . . . . . . . 31 62 8.2. Tree Coding Example . . . . . . . . . . . . . . . . . . 32 63 9. Frame Header . . . . . . . . . . . . . . . . . . . . . . . . 35 64 9.1. Uncompressed Data Chunk . . . . . . . . . . . . . . . . 35 65 9.2. Color Space and Pixel Type (Key Frames-only) . . . . . . 38 66 9.3. Segment-based Adjustments . . . . . . . . . . . . . . . 38 67 9.4. Loop Filter Type and Levels . . . . . . . . . . . . . . 39 68 9.5. Token Partition and Partition Data Offsets . . . . . . . 40 69 9.6. Dequantization Indices . . . . . . . . . . . . . . . . . 41 70 9.7. Refresh Golden Frame and AltRef Frame . . . . . . . . . 42 71 9.8. Refresh Last Frame Buffer . . . . . . . . . . . . . . . 43 72 9.9. DCT Coefficient Probability Update . . . . . . . . . . . 43 73 9.10. Remaining Frame Header Data (non-Key Frame) . . . . . . 44 74 9.11. Remaining Frame Header Data (Key Frame) . . . . . . . . 45 75 10. Segment-based Feature Adjustments . . . . . . . . . . . . . . 46 76 11. Key Frame Macroblock Prediction Records . . . . . . . . . . . 47 77 11.1. mb_skip_coeff . . . . . . . . . . . . . . . . . . . . . 47 78 11.2. Luma Modes . . . . . . . . . . . . . . . . . . . . . . . 47 79 11.3. Subblock Mode Contexts . . . . . . . . . . . . . . . . . 50 80 11.4. Chroma Modes . . . . . . . . . . . . . . . . . . . . . . 51 81 11.5. Subblock Mode Probability Table . . . . . . . . . . . . 52 82 12. Intraframe Prediction . . . . . . . . . . . . . . . . . . . . 56 83 12.1. mb_skip_coeff . . . . . . . . . . . . . . . . . . . . . 56 84 12.2. Chroma Prediction . . . . . . . . . . . . . . . . . . . 57 85 12.3. Luma Prediction . . . . . . . . . . . . . . . . . . . . 59 86 13. DCT Coefficient Decoding . . . . . . . . . . . . . . . . . . 66 87 13.1. MB Without non-Zero Coefficient Values . . . . . . . . . 66 88 13.2. Coding of Individual Coefficient Values . . . . . . . . 67 89 13.3. Token Probabilities . . . . . . . . . . . . . . . . . . 69 90 13.4. Token Probability Updates . . . . . . . . . . . . . . . 73 91 13.5. Default Token Probability Table . . . . . . . . . . . . 78 92 14. DCT and WHT Inversion and Macroblock Reconstruction . . . . . 83 93 14.1. Dequantization . . . . . . . . . . . . . . . . . . . . . 83 94 14.2. Inverse Transforms . . . . . . . . . . . . . . . . . . . 84 95 14.3. Implementation of the WHT Inversion . . . . . . . . . . 85 96 14.4. Implementation of the DCT Inversion . . . . . . . . . . 87 97 14.5. Summation of Predictor and Residue . . . . . . . . . . . 90 98 15. Loop Filter . . . . . . . . . . . . . . . . . . . . . . . . . 91 99 15.1. Filter Geometry and Overall Procedure . . . . . . . . . 92 100 15.2. Simple Filter . . . . . . . . . . . . . . . . . . . . . 94 101 15.3. Normal Filter . . . . . . . . . . . . . . . . . . . . . 98 102 15.4. Calculation of Control Parameters . . . . . . . . . . . 103 103 16. Interframe Macroblock Prediction Records . . . . . . . . . . 105 104 16.1. Intra-Predicted Macroblocks . . . . . . . . . . . . . . 105 105 16.2. Inter-Predicted Macroblocks . . . . . . . . . . . . . . 106 106 16.3. Mode and Motion Vector Contexts . . . . . . . . . . . . 107 107 16.4. Split Prediction . . . . . . . . . . . . . . . . . . . . 113 108 17. Motion Vector Decoding . . . . . . . . . . . . . . . . . . . 117 109 17.1. Coding of Each Component . . . . . . . . . . . . . . . . 117 110 17.2. Probability Updates . . . . . . . . . . . . . . . . . . 119 111 18. Interframe Prediction . . . . . . . . . . . . . . . . . . . . 122 112 18.1. Bounds on and Adjustment of Motion Vectors . . . . . . . 122 113 18.2. Prediction Subblocks . . . . . . . . . . . . . . . . . . 123 114 18.3. Sub-pixel Interpolation . . . . . . . . . . . . . . . . 124 115 18.4. Filter Properties . . . . . . . . . . . . . . . . . . . 127 116 19. Annex A: Bitstream Syntax . . . . . . . . . . . . . . . . . . 130 117 19.1. Uncompressed Data Chunk . . . . . . . . . . . . . . . . 130 118 19.2. Frame Header . . . . . . . . . . . . . . . . . . . . . . 132 119 19.3. Macroblock Data . . . . . . . . . . . . . . . . . . . . 144 120 20. Attachment One: Reference Decoder Source Code . . . . . . . . 148 121 20.1. bit_ops.h . . . . . . . . . . . . . . . . . . . . . . . 148 122 20.2. bool_decoder.h . . . . . . . . . . . . . . . . . . . . . 148 123 20.3. dequant_data.h . . . . . . . . . . . . . . . . . . . . . 152 124 20.4. dixie.c . . . . . . . . . . . . . . . . . . . . . . . . 152 125 20.5. dixie.h . . . . . . . . . . . . . . . . . . . . . . . . 164 126 20.6. dixie_loopfilter.c . . . . . . . . . . . . . . . . . . . 171 127 20.7. dixie_loopfilter.h . . . . . . . . . . . . . . . . . . . 183 128 20.8. idct_add.c . . . . . . . . . . . . . . . . . . . . . . . 183 129 20.9. idct_add.h . . . . . . . . . . . . . . . . . . . . . . . 187 130 20.10. mem.h . . . . . . . . . . . . . . . . . . . . . . . . . 187 131 20.11. modemv.c . . . . . . . . . . . . . . . . . . . . . . . . 189 132 20.12. modemv.h . . . . . . . . . . . . . . . . . . . . . . . . 204 133 20.13. modemv_data.h . . . . . . . . . . . . . . . . . . . . . 204 134 20.14. predict.c . . . . . . . . . . . . . . . . . . . . . . . 209 135 20.15. predict.h . . . . . . . . . . . . . . . . . . . . . . . 239 136 20.16. tokens.c . . . . . . . . . . . . . . . . . . . . . . . . 239 137 20.17. tokens.h . . . . . . . . . . . . . . . . . . . . . . . . 249 138 20.18. vp8_prob_data.h . . . . . . . . . . . . . . . . . . . . 258 139 20.19. vpx_codec_internal.h . . . . . . . . . . . . . . . . . . 267 140 20.20. vpx_decoder.h . . . . . . . . . . . . . . . . . . . . . 277 141 20.21. vpx_integer.h . . . . . . . . . . . . . . . . . . . . . 284 142 20.22. AUTHORS . . . . . . . . . . . . . . . . . . . . . . . . 286 143 20.23. LICENSE . . . . . . . . . . . . . . . . . . . . . . . . 287 144 20.24. PATENTS . . . . . . . . . . . . . . . . . . . . . . . . 288 145 21. References . . . . . . . . . . . . . . . . . . . . . . . . . 290 146 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 291 148 1. Introduction 150 This document describes the VP8 compressed video data format, 151 together with a discussion of the decoding procedure for the format. 152 It is intended to be used in conjunction with and as a guide to the 153 reference decoder source code provided in Attachment One. If there 154 are any conflicts between this narrative and the reference source 155 code, the reference source code should be considered correct. The 156 bitstream is defined by the reference source code and not this 157 narrative. 159 Like many modern video compression schemes, VP8 is based on 160 decomposition of frames into square subblocks of pixels, prediction 161 of such subblocks using previously constructed blocks, and adjustment 162 of such predictions (as well as synthesis of unpredicted blocks) 163 using a discrete cosine transform (hereafter abbreviated as DCT). In 164 one special case, however, VP8 uses a "Walsh-Hadamard" transform 165 (hereafter abbreviated as WHT) instead of a DCT. 167 Roughly speaking, such systems reduce datarate by exploiting the 168 temporal and spatial coherence of most video signals. It is more 169 efficient to specify the location of a visually similar portion of a 170 prior frame than it is to specify pixel values. The frequency 171 segregation provided by the DCT and WHT facilitate the exploitation 172 of both spatial coherence in the original signal and the tolerance of 173 the human visual system to moderate losses of fidelity in the 174 reconstituted signal. 176 VP8 augments these basic concepts with, among other things, 177 sophisticated usage of contextual probabilities. The result is a 178 significant reduction in datarate at a given quality. 180 Unlike some similar schemes (the older MPEG formats, for example), 181 VP8 specifies exact values for reconstructed pixels. Specifically, 182 the specification for the DCT and WHT portions of the reconstruction 183 does not allow for any "drift" caused by truncation of fractions. 184 Rather, the algorithm is specified using fixed-precision integer 185 operations exclusively. This greatly facilitates the verification of 186 the correctness of a decoder implementation as well as avoiding 187 difficult-to-predict visual incongruities between such 188 implementations. 190 It should be remarked that, in a complete video playback system, the 191 displayed frames may or may not be identical to the reconstructed 192 frames. Many systems apply a final level of filtering (commonly 193 referred to as postprocessing) to the reconstructed frames prior to 194 viewing. Such postprocessing has no effect on the decoding and 195 reconstruction of subsequent frames (which are predicted using the 196 completely-specified reconstructed frames) and is beyond the scope of 197 this document. In practice, the nature and extent of this sort of 198 postprocessing is dependent on both the taste of the user and on the 199 computational facilities of the playback environment. 201 2. Format Overview 203 VP8 works exclusively with an 8-bit YUV 4:2:0 image format. In this 204 format, each 8-bit pixel in the two chroma planes (U and V) 205 corresponds positionally to a 2x2 block of 8-bit luma pixels in the Y 206 plane; coordinates of the upper left corner of the Y block are of 207 course exactly twice the coordinates of the corresponding chroma 208 pixels. When we refer to pixels or pixel distances without 209 specifying a plane, we are implicitly referring to the Y plane or to 210 the complete image, both of which have the same (full) resolution. 212 As is usually the case, the pixels are simply a large array of bytes 213 stored in rows from top to bottom, each row being stored from left to 214 right. This "left to right" then "top to bottom" raster-scan order 215 is reflected in the layout of the compressed data as well. 217 Provision has been made in the VP8 bitstream header for the support 218 of a secondary YUV color format, in the form of a reserved bit. 220 Occasionally, at very low datarates, a compression system may decide 221 to reduce the resolution of the input signal to facilitate efficient 222 compression. The VP8 data format supports this via optional 223 upscaling of its internal reconstruction buffer prior to output (this 224 is completely distinct from the optional postprocessing discussed 225 earlier, which has nothing to do with decoding per se). This 226 upsampling restores the video frames to their original resolution. 227 In other words, the compression/decompression system can be viewed as 228 a "black box", where the input and output is always at a given 229 resolution. The compressor might decide to "cheat" and process the 230 signal at a lower resolution. In that case, the decompressor needs 231 the ability to restore the signal to its original resolution. 233 Internally, VP8 decomposes each output frame into an array of 234 macroblocks. A macroblock is a square array of pixels whose Y 235 dimensions are 16x16 and whose U and V dimensions are 8x8. 236 Macroblock-level data in a compressed frame occurs (and must be 237 processed) in a raster order similar to that of the pixels comprising 238 the frame. 240 Macroblocks are further decomposed into 4x4 subblocks. Every 241 macroblock has 16 Y subblocks, 4 U subblocks, and 4 V subblocks. Any 242 subblock-level data (and processing of such data) again occurs in 243 raster order, this time in raster order within the containing 244 macroblock. 246 As discussed in further detail below, data can be specified at the 247 levels of both macroblocks and their subblocks. 249 Pixels are always treated, at a minimum, at the level of subblocks, 250 which may be thought of as the "atoms" of the VP8 algorithm. In 251 particular, the 2x2 chroma blocks corresponding to 4x4 Y subblocks 252 are never treated explicitly in the data format or in the algorithm 253 specification. 255 The DCT and WHT always operate at a 4x4 resolution. The DCT is used 256 for the 16Y, 4U and 4V subblocks. The WHT is used (with some but not 257 all prediction modes) to encode a 4x4 array comprising the average 258 intensities of the 16 Y subblocks of a macroblock. These average 259 intensities are, up to a constant normalization factor, nothing more 260 that the zeroth DCT coefficients of the Y subblocks. This "higher- 261 level" WHT is a substitute for the explicit specification of those 262 coefficients, in exactly the same way as the DCT of a subblock 263 substitutes for the specification of the pixel values comprising the 264 subblock. We consider this 4x4 array as a second-order subblock 265 called Y2, and think of a macroblock as containing 24 "real" 266 subblocks and, sometimes, a 25th "virtual" subblock. This is dealt 267 with further in Chapter 13. 269 The frame layout used by the reference decoder may be found in the 270 file yv12config.h. 272 3. Compressed Frame Types 274 There are only two types of frames in VP8. 276 Intraframes (also called key frames and, in MPEG terminology, 277 I-frames) are decoded without reference to any other frame in a 278 sequence, that is, the decompressor reconstructs such frames 279 beginning from its "default" state. Key frames provide random access 280 (or seeking) points in a video stream. 282 Interframes (also called prediction frames and, in MPEG terminology, 283 P-frames) are encoded with reference to prior frames, specifically 284 all prior frames up to and including the most recent key frame. 285 Generally speaking, the correct decoding of an interframe depends on 286 the correct decoding of the most recent key frame and all ensuing 287 frames. Consequently, the decoding algorithm is not tolerant of 288 dropped frames: In an environment in which frames may be dropped or 289 corrupted, correct decoding will not be possible until a key frame is 290 correctly received. 292 In contrast to MPEG, there is no use of bidirectional prediction. No 293 frame is predicted using frames temporally subsequent to it; there is 294 no analog to an MPEG B-frame. 296 Secondly, VP8 augments these notions with that of alternate 297 prediction frames, called golden frames and altref frames 298 (alternative reference frames). Blocks in an interframe may be 299 predicted using blocks in the immediately previous frame as well as 300 the most recent golden frame or altref frame. Every key frame is 301 automatically golden and altref, and any interframe may optionally 302 replace the most recent golden or altref frame. 304 Golden frames and altref frames may also be used to partially 305 overcome the intolerance to dropped frames discussed above: If a 306 compressor is configured to code golden frames only with reference to 307 the prior golden frame (and key frame) then the "substream" of key 308 and golden frames may be decoded regardless of loss of other 309 interframes. Roughly speaking, the implementation requires (on the 310 compressor side) that golden frames subsume and recode any context 311 updates effected by the intervening interframes. A typical 312 application of this approach is video conferencing, in which 313 retransmission of a prior golden frame and/or a delay in playback 314 until receipt of the next golden frame is preferable to a larger 315 retransmit and/or delay until the next key frame. 317 4. Overview of Compressed Data Format 319 The input to a VP8 decoder is a sequence of compressed frames whose 320 order matches their order in time. Issues such as the duration of 321 frames, the corresponding audio, and synchronization are generally 322 provided by the playback environment and are irrelevant to the 323 decoding process itself, however, to aid in fast seeking a start code 324 is included in the header of each key frame. 326 The decoder is simply presented with a sequence of compressed frames 327 and produces a sequence of decompressed (reconstructed) YUV frames 328 corresponding to the input sequence. As stated in the introduction, 329 the exact pixel values in the reconstructed frame are part of VP8's 330 specification. This document specifies the layout of the compressed 331 frames and gives unambiguous algorithms for the correct production of 332 reconstructed frames. 334 The first frame presented to the decompressor is of course a key 335 frame. This may be followed by any number of interframes; the 336 correct reconstruction of each frame depends on all prior frames up 337 to the key frame. The next key frame restarts this process: The 338 decompressor resets to its default initial condition upon reception 339 of a key frame and the decoding of a key frame (and its ensuing 340 interframes) is completely independent of any prior decoding. 342 At the highest level, every compressed frame has three or more 343 pieces. It begins with an uncompressed data chunk comprising 10 344 bytes in the case of key frames and 3-bytes for inter frames. This 345 is followed by two or more blocks of compressed data (called 346 partitions). These compressed data partitions begin and end on byte 347 boundaries. 349 The first compressed partition has two subsections: 351 1. Header information that applies to the frame as a whole. 353 2. Per-macroblock information specifying how each macroblock is 354 predicted from the already-reconstructed data that is available 355 to the decompressor. 357 As stated above, the macroblock-level information occurs in raster- 358 scan order. 360 The rest of the partitions contain, for each block, the DCT/WHT 361 coefficients (quantized and logically compressed) of the residue 362 signal to be added to the predicted block values. It typically 363 accounts for roughly 70% of the overall datarate. VP8 supports 364 packing the compressed DCT/WHT coefficients' data from macroblock 365 rows into separate partitions. If there is more than one partition 366 for these coefficients, the sizes of the partitions -- except the 367 last partition -- in bytes are also present in the bitstream right 368 after the above first partition. Each of the sizes is a 3-byte data 369 item written in little endian format. These sizes provide the 370 decoder direct access to all DCT/WHT coefficient partitions, which 371 enables parallel processing of the coefficients in a decoder. 373 The separate partitioning of the prediction data and coefficient data 374 also allows flexibility in the implementation of a decompressor: An 375 implementation may decode and store the prediction information for 376 the whole frame and then decode, transform, and add the residue 377 signal to the entire frame, or it may simultaneously decode both 378 partitions, calculating prediction information and adding in the 379 residue signal for each block in order. The length field in the 380 frame tag, which allows decoding of the second partition to begin 381 before the first partition has been completely decoded, is necessary 382 for the second "block-at-a-time" decoder implementation. 384 All partitions are decoded using separate instances of the boolean 385 entropy decoder described in Chapter 7. Although some of the data 386 represented within the partitions is conceptually "flat" (a bit is 387 just a bit with no probabilistic expectation one way or the other), 388 because of the way such coders work, there is never a direct 389 correspondence between a "conceptual bit" and an actual physical bit 390 in the compressed data partitions. Only in the 3 or 10 byte 391 uncompressed chunk described above is there such a physical 392 correspondence. 394 A related matter, which is true for most lossless compression 395 formats, is that seeking within a partition is not supported. The 396 data must be decompressed and processed (or at least stored) in the 397 order in which it occurs in the partition. 399 While this document specifies the ordering of the partition data 400 correctly, the details and semantics of this data are discussed in a 401 more logical fashion to facilitate comprehension. For example, the 402 frame header contains updates to many probability tables used in 403 decoding per-macroblock data. The latter is often described before 404 the layouts of the probabilities and their updates, even though this 405 is the opposite of their order in the bitstream. 407 5. Overview of the Decoding Process 409 A VP8 decoder needs to maintain four YUV frame buffers whose 410 resolutions are at least equal to that of the encoded image. These 411 buffers hold the current frame being reconstructed, the immediately 412 previous reconstructed frame, the most recent golden frame, and the 413 most recent altref frame. 415 Most implementations will wish to "pad" these buffers with 416 "invisible" pixels that extend a moderate number of pixels beyond all 417 four edges of the visible image. This simplifies interframe 418 prediction by allowing all (or most) prediction blocks -- which are 419 not guaranteed to lie within the visible area of a prior frame -- to 420 address usable image data. 422 Regardless of the amount of padding chosen, the invisible rows above 423 (below) the image are filled with copies of the top (bottom) row of 424 the image; the invisible columns to the left (right) of the image are 425 filled with copies of the leftmost (rightmost) visible row; and the 426 four invisible corners are filled with copies of the corresponding 427 visible corner pixels. The use of these prediction buffers (and 428 suggested sizes for the halo) will be elaborated on in the discussion 429 of motion vectors, interframe prediction, and sub-pixel interpolation 430 later in this document. 432 As will be seen in the description of the frame header, the image 433 dimensions are specified (and can change) with every key frame. 434 These buffers (and any other data structures whose size depends on 435 the size of the image) should be allocated (or re-allocated) 436 immediately after the dimensions are decoded. 438 Leaving most of the details for later elaboration, the following is 439 an outline the decoding process. 441 First, the frame header (beginning of the first data partition) is 442 decoded. Altering or augmenting the maintained state of the decoder, 443 this provides the context in which the per-macroblock data can be 444 interpreted. 446 The macroblock data occurs (and must be processed) in raster-scan 447 order. This data comes in two or more parts. The first (prediction 448 or mode) part comes in the remainder of the first data partition. 449 The other parts comprise the data partition(s) for the DCT/WHT 450 coefficients of the residue signal. For each macroblock, the 451 prediction data must be processed before the residue. 453 Each macroblock is predicted using one (and only one) of four 454 possible frames. All macroblocks in a key frame, and all intra-coded 455 macroblocks in an interframe, are predicted using the already-decoded 456 macroblocks in the current frame. Macroblocks in an interframe may 457 also be predicted using the previous frame, the golden frame or the 458 altref frame. Such macroblocks are said to be inter-coded. 460 The purpose of prediction is to use already-constructed image data to 461 approximate the portion of the original image being reconstructed. 462 The effect of any of the prediction modes is then to write a 463 macroblock-sized prediction buffer containing this approximation. 465 Regardless of the prediction method, the residue DCT signal is 466 decoded, dequantized, reverse-transformed, and added to the 467 prediction buffer to produce the (almost final) reconstruction value 468 of the macroblock, which is stored in the correct position of the 469 current frame buffer. 471 The residue signal consists of 24 (sixteen Y, four U, and four V) 4x4 472 quantized and losslessly-compressed DCT transforms approximating the 473 difference between the original macroblock in the uncompressed source 474 and the prediction buffer. For most prediction modes, the zeroth 475 coefficients of the sixteen Y subblocks are expressed via a 25th WHT 476 of the second-order virtual Y2 subblock discussed above. 478 Intra-prediction exploits the spatial coherence of frames. The 16x16 479 luma (Y) and 8x8 chroma (UV) components are predicted independently 480 of each other using one of four simple means of pixel propagation, 481 starting from the already-reconstructed (16-pixel long luma, 8-pixel 482 long chroma) row above and column to the left of the current 483 macroblock. The four methods are: 485 1. Copying the row from above throughout the prediction buffer. 487 2. Copying the column from left throughout the prediction buffer. 489 3. Copying the average value of the row and column throughout the 490 prediction buffer. 492 4. Extrapolation from the row and column using the (fixed) second 493 difference (horizontal and vertical) from the upper left corner. 495 Additionally, the sixteen Y subblocks may be predicted independently 496 of each other using one of ten different modes, four of which are 4x4 497 analogs of those described above, augmented with six "diagonal" 498 prediction methods. There are two types of predictions, one intra 499 and one prediction (among all the modes), for which the residue 500 signal does not use the Y2 block to encode the DC portion of the 501 sixteen 4x4 Y subblock DCTs. This "independent Y subblock" mode has 502 no effect on the 8x8 chroma prediction. 504 Inter-prediction exploits the temporal coherence between nearby 505 frames. Except for the choice of the prediction frame itself, there 506 is no difference between inter-prediction based on the previous frame 507 and that based on the golden frame or altref frame. 509 Inter-prediction is conceptually very simple. While, for reasons of 510 efficiency, there are several methods of encoding the relationship 511 between the current macroblock and corresponding sections of the 512 prediction frame, ultimately each of the sixteen Y subblocks is 513 related to a 4x4 subblock of the prediction frame, whose position in 514 that frame differs from the current subblock position by a (usually 515 small) displacement. These two-dimensional displacements are called 516 motion vectors. 518 The motion vectors used by VP8 have quarter-pixel precision. 519 Prediction of a subblock using a motion vector that happens to have 520 integer (whole number) components is very easy: the 4x4 block of 521 pixels from the displaced block in the previous, golden, or altref 522 frame are simply copied into the correct position of the current 523 macroblock's prediction buffer. 525 Fractional displacements are conceptually and implementationally more 526 complex. They require the inference (or synthesis) of sample values 527 that, strictly speaking, do not exist. This is one of the most basic 528 problems in signal processing and readers conversant with that 529 subject will see that the approach taken by VP8 provides a good 530 balance of robustness, accuracy, and efficiency. 532 Leaving the details for the implementation discussion below, the 533 pixel interpolation is calculated by applying a kernel filter (using 534 reasonable-precision integer math) three pixels on either side, both 535 horizontally and vertically, of the pixel to be synthesized. The 536 resulting 4x4 block of synthetic pixels is then copied into position 537 exactly as in the case of integer displacements. 539 Each of the eight chroma subblocks is handled similarly. Their 540 motion vectors are never specified explicitly; instead, the motion 541 vector for each chroma subblock is calculated by averaging the 542 vectors of the four Y subblocks that occupy the same area of the 543 frame. Since chroma pixels have twice the diameter (and four times 544 the area) of luma pixels, the calculated chroma motion vectors have 545 1/8 pixel resolution, but the procedure for copying or generating 546 pixels for each subblock is essentially identical to that done in the 547 luma plane. 549 After all the macroblocks have been generated (predicted and 550 corrected with the DCT/WHT residue), a filtering step (the loop 551 filter) is applied to the entire frame. The purpose of the loop 552 filter is to reduce blocking artifacts at the boundaries between 553 macroblocks and between subblocks of the macroblocks. The term loop 554 filter is used because this filter is part of the "coding loop," that 555 is, it affects the reconstructed frame buffers that are used to 556 predict ensuing frames. This is distinguished from the 557 postprocessing filters discussed earlier which affect only the viewed 558 video and do not "feed into" subsequent frames. 560 Next, if signaled in the data, the current frame may replace the 561 golden frame prediction buffer and/or the altref frame buffer. 563 The halos of the frame buffers are next filled as specified above. 564 Finally, at least as far as decoding is concerned, the (references 565 to) the "current" and "last" frame buffers should be exchanged in 566 preparation for the next frame. 568 Various processes may be required (or desired) before viewing the 569 generated frame. As discussed in the frame dimension information 570 below, truncation and/or upscaling of the frame may be required. 571 Some playback systems may require a different frame format (RGB, 572 YUY2, etc.). Finally, as mentioned in the introduction, further 573 postprocessing or filtering of the image prior to viewing may be 574 desired. Since the primary purpose of this document is a decoding 575 specification, the postprocessing is not specified in this document. 577 While the basic ideas of prediction and correction used by VP8 are 578 straightforward, many of the details are quite complex. The 579 management of probabilities is particularly elaborate. Not only do 580 the various modes of intra-prediction and motion vector specification 581 have associated probabilities but they, together with the coding of 582 DCT coefficients and motion vectors, often base these probabilities 583 on a variety of contextual information (calculated from what has been 584 decoded so far), as well as on explicit modification via the frame 585 header. 587 The "top-level" of decoding and frame reconstruction is implemented 588 in the reference decoder files onyxd_if.c and decodframe.c. 590 This concludes our summary of decoding and reconstruction; we 591 continue by discussing the individual aspects in more depth. 593 A reasonable "divide and conquer" approach to implementation of a 594 decoder is to begin by decoding streams composed exclusively of key 595 frames. After that works reliably, interframe handling can be added 596 more easily than if complete functionality were attempted 597 immediately. In accordance with this, we first discuss components 598 needed to decode key frames (most of which are also used in the 599 decoding of interframes) and conclude with topics exclusive to 600 interframes. 602 6. Description of Algorithms 604 As the intent of this document, together with the reference decoder 605 source code, is to specify a platform-independent procedure for the 606 decoding and reconstruction of a VP8 video stream, many (small) 607 algorithms must be described exactly. 609 Due to its near-universality, terseness, ability to easily describe 610 calculation at specific precisions, and the fact that On2's reference 611 VP8 decoder is written in C, these algorithm fragments are written 612 using the C programming language, augmented with a few simple 613 definitions below. 615 The standard (and best) reference for C is [Kernighan]. 617 Many code fragments will be presented in this document. Some will be 618 nearly identical to corresponding sections of the reference decoder; 619 others will differ. Roughly speaking, there are three reasons for 620 such differences: 622 1. For reasons of efficiency, the reference decoder version may be 623 less obvious. 625 2. The reference decoder often uses large data structures to 626 maintain context that need not be described or used here. 628 3. The authors of this document felt that a different expression of 629 the same algorithm might facilitate exposition. 631 Regardless of the chosen presentation, the calculation effected by 632 any of the algorithms described here is identical to that effected by 633 the corresponding portion of the reference decoder. 635 All VP8 decoding algorithms use integer math. To facilitate 636 specification of arithmetic precision, we define the following types. 638 ---- Begin code block -------------------------------------- 640 typedef signed char int8; /* signed int exactly 8 bits wide */ 641 typedef unsigned char uint8; /* unsigned "" */ 643 typedef short int16; /* signed int exactly 16 bits wide */ 644 typedef unsigned int16 uint16; /* unsigned "" */ 646 /* int32 is a signed integer type at least 32 bits wide */ 648 typedef long int32; /* guaranteed to work on all systems */ 649 typedef int int32; /* will be more efficient on some systems */ 651 typedef unsigned int32 uint32; 653 /* unsigned integer type, at least 16 bits wide, whose exact size 654 is most convenient to whatever processor we are using */ 656 typedef unsigned int uint; 658 /* While pixels themselves are 8-bit unsigned integers, 659 pixel arithmetic often occurs at 16- or 32-bit precision and 660 the results need to be "saturated" or clamped to an 8-bit 661 range. */ 663 typedef uint8 Pixel; 665 Pixel clamp255( int32 v) { return v < 0? 0 : (v < 255? v : 255);} 667 /* As is elaborated in the discussion of the bool_decoder below, 668 VP8 represents probabilities as unsigned 8-bit numbers. */ 670 typedef uint8 Prob; 672 ---- End code block ---------------------------------------- 674 We occasionally need to discuss mathematical functions involving 675 honest-to-goodness "infinite precision" real numbers. The DCT is 676 first described via the cosine function cos; the ratio of the lengths 677 of the circumference and diameter of a circle is denoted pi; at one 678 point, we take a (base 1/2) logarithm denoted log; and pow( x, y) 679 denotes x raised to the power y. If x = 2 and y is a small non- 680 negative integer, pow( 2, y) may be expressed in C as 1 << y. 682 Finally, we sometimes need to divide signed integers by powers of 683 two, that is, we occasionally right-shift signed numbers. The 684 behavior of such shifts (i.e., the propagation of the sign bit) is, 685 perhaps surprisingly, not defined by the C language itself and is 686 left up to individual compilers. Because of the utility of this 687 frequently needed operation, it is at least arguable that it should 688 be defined by the language (to naturally propagate the sign bit) and, 689 at a minimum, should be correctly implemented by any reasonable 690 compiler. In the interest of strict portability, we attempt to call 691 attention to these shifts when they arise. 693 7. Boolean Entropy Decoder 695 As discussed in the overview above, essentially the entire VP8 data 696 stream is encoded using a boolean entropy coder. 698 An understanding of the bool_decoder is critical to the 699 implementation of a VP8 decompressor, so we discuss in detail. It is 700 easier to comprehend the bool_decoder in conjunction with the 701 bool_encoder used by the compressor to write the compressed data 702 partitions. 704 The bool_encoder encodes (and the bool_decoder decodes) one bool 705 (zero-or-one boolean value) at a time. Its purpose is to losslessly 706 compress a sequence of bools for which the probability of their being 707 zero or one can be well-estimated (via constant or previously-coded 708 information) at the time they are written, using identical 709 corresponding probabilities at the time they are read. 711 As the reader is probably aware, if a bool is much more likely to be 712 zero than one (for instance), it can, on average, be faithfully 713 encoded using much less than one bit per value. The bool_encoder 714 exploits this. 716 In the 1940s, [Shannon] proved that there is a lower bound for the 717 average datarate of a faithful encoding of a sequence of bools (whose 718 probability distributions are known and are independent of each 719 other) and also that there are encoding algorithms that approximate 720 this lower bound as closely as one wishes. 722 If we encode a sequence of bools whose probability of being zero is p 723 (and whose probability of being 1 is 1-p), the lowest possible 724 datarate per value is 726 plog(p) + (1-p)log(1-p); 728 taking the logarithms to the base 1/2 expresses the datarate in bits/ 729 value. 731 We give two simple examples. At one extreme, if p=1/2, then log(p) = 732 log(1-p) = 1 and the lowest possible datarate per bool is 1/2 + 1/2 = 733 1, that is, we cannot do any better than simply literally writing out 734 bits. At another extreme, if p is very small, say p=1/1024, then 735 log(p)=10, log(1-p) is roughly .0014, and the lowest possible 736 datarate is approximately 10/1024 + .0014, roughly 1/100 of a bit per 737 bool. 739 Because most of the bools in the VP8 datastream have zero- 740 probabilities nowhere near 1/2, the compression provided by the 741 bool_encoder is critical to the performance of VP8. 743 The bool coder used by VP8 is a variant of an arithmetic coder. An 744 excellent discussion of arithmetic coding (and other lossless 745 compression techniques) can be found in [Bell]. 747 7.1. Underlying Theory of Coding 749 The basic idea used by the bool coder is to consider the entire data 750 stream (either of the partitions in our case) as the binary expansion 751 of a single number x with 0 <= x < 1. The bits (or bytes) in x are 752 of course written from high to low order and if b[j] (B[j]) is the 753 j^(th) bit (byte) in the partition, the value x is simply the sum 754 (starting with j = 1) of pow(2, -j) * b[j] or pow(256, -j) * B[j]. 756 Before the first bool is coded, all values of x are possible. 758 The coding of each bool restricts the possible values of x in 759 proportion to the probability of what is coded. If p1 is the 760 probability of the first bool being zero and a zero is coded, the 761 range of possible x is restricted to 0 <= x < p1. If a one is coded, 762 the range becomes p1 <= x < 1. 764 The coding continues by repeating the same idea. At every stage, 765 there is an interval a <= x < b of possible values of x. If p is the 766 probability of a zero being coded at this stage and a zero is coded, 767 the interval becomes a <= x < a + (p(b-a)). If a one is coded, the 768 possible x are restricted to a + (p(b-a)) <= x < b. 770 Assuming only finitely many values are to be coded, after the encoder 771 has received the last bool, it can write as its output any value x 772 that lies in the final interval. VP8 simply writes the left endpoint 773 of the final interval. Consequently, the output it would make if 774 encoding were to stop at any time either increases or stays the same 775 as each bool is encoded. 777 Decoding parallels encoding. The decoder is presented with the 778 number x, which has only the initial restriction 0 <= x < 1. To 779 decode the first bool, the decoder is given the first probability p1. 780 If x < p1, a zero is decoded; if x >= p1, a one is decoded. In 781 either case, the new restriction on x, that is, the interval of 782 possible x, is remembered. 784 Decoding continues in exactly the same way: If a <= x < b is the 785 current interval and we are to decode a bool with zero-probability p, 786 we return a zero if a <= x < a + (p(b-a)) and a one if a + (p(b-a)) 787 <= x < b. In either case, the new restriction is remembered in 788 preparation for decoding the next bool. 790 The process outlined above uses real numbers of infinite precision to 791 express the probabilities and ranges. It is true that, if one could 792 actualize this process and coded a large number of bools whose 793 supplied probabilities matched their value distributions, the 794 datarate achieved would approach the theoretical minimum as the 795 number of bools encoded increased. 797 Unfortunately, computers operate at finite precision and an 798 approximation to the theoretically perfect process described above is 799 necessary. Such approximation increases the datarate but, at quite 800 moderate precision and for a wide variety of data sets, this increase 801 is negligible. 803 The only conceptual limitations are, first, that coder probabilities 804 must be expressed at finite precision and, second, that the decoder 805 be able to detect each individual modification to the value interval 806 via examination of a fixed amount of input. As a practical matter, 807 many of the implementation details stem from the fact that the coder 808 can function using only a small "window" to incrementally read or 809 write the arbitrarily precise number x. 811 7.2. Practical Algorithm Description 813 VP8's bool coder works with 8-bit probabilities p. The range of such 814 p is 0 <= p <= 255; the actual probability represented by p is p/256. 815 Also, the coder is designed so that decoding of a bool requires no 816 more than an 8-bit comparison and so that the state of both the 817 encoder and decoder can be easily represented using a small number of 818 unsigned 16-bit integers. 820 The details are most easily understood if we first describe the 821 algorithm using bit-at-a-time input and output. Aside from the 822 ability to maintain a position in this bitstream and write/read bits, 823 the encoder also needs the ability to add 1 to the bits already 824 output; after writing n bits, adding 1 to the existing output is the 825 same thing as adding pow( 2, -n) to x. 827 Together with the bit position, the encoder must maintain two 828 unsigned 8-bit numbers which we call bottom and range. Writing w for 829 the n bits already written and S = pow( 2, - n - 8) for the scale of 830 the current bit position one byte out, we have the following 831 constraint on all future values v of w (including the final value v = 832 x): 834 w + ( S * bottom ) <= v < w + ( S * ( bottom + range ) ) 836 Thus, appending bottom to the already-written bits w gives the left 837 endpoint of the interval of possible values, appending bottom + range 838 gives the right endpoint, range itself (scaled to the current output 839 position) is the length of the interval. 841 So that our probabilistic encodings are reasonably accurate, we do 842 not let range vary by more than a factor of two: It stays within the 843 bounds 128 <= range <= 255. 845 The process for encoding a boolean value val whose probability of 846 being zero is prob / 256 -- and whose probability of being one is ( 847 256 - prob ) / 256 -- with 1 <= prob <= 255 is as follows. 849 Using an unsigned 16-bit multiply followed by an unsigned right 850 shift, we calculate an unsigned 8-bit split value: 852 split = 1 + (((range - 1) * probability)]] >> 8) 854 split is approximately ( prob / 256 ) * range and lies within the 855 bounds 1 <= split <= range - 1. These bounds ensure the correctness 856 of the decoding procedure described below. 858 If the incoming boolean val to be encoded is false, we leave the left 859 interval endpoint bottom alone and reduce range, replacing it by 860 split. If the incoming val is true, we move up the left endpoint to 861 bottom + split, propagating any carry to the already-written value w 862 (this is where we need the ability to add 1 to w), and reduce range 863 to range - split. 865 Regardless of the value encoded, range has been reduced and now has 866 the bounds 1 <= range <= 254. If range < 128, the encoder doubles it 867 and shifts the high-order bit out of bottom to the output as it also 868 doubles bottom, repeating this process one bit at a time until 128 <= 869 range <= 255. Once this is completed, the encoder is ready to accept 870 another bool, maintaining the constraints described above. 872 After encoding the last bool, the partition may be completed by 873 appending bottom to the bitstream. 875 The decoder mimics the state of the encoder. It maintains, together 876 with an input bit position, two unsigned 8-bit numbers, a range 877 identical to that maintained by the encoder and a value. Decoding 878 one bool at a time, the decoder (in effect) tracks the same left 879 interval endpoint as does the encoder and subtracts it from the 880 remaining input. Appending the unread portion of the bitstream to 881 the 8-bit value gives the difference between the actual value encoded 882 and the known left endpoint. 884 The decoder is initialized by setting range = 255 and reading the 885 first 16 input bits into value. The decoder maintains range and 886 calculates split in exactly the same way as does the encoder. 888 To decode a bool, it compares value to split; if value < split, the 889 bool is zero, and range is replaced with split. If value >= split, 890 the bool is one, range is replaced with range - split, and value is 891 replaced with value - split. 893 Again, range is doubled one bit at a time until it is at least 128. 894 The value is doubled in parallel, shifting a new input bit into the 895 bottom each time. 897 Writing Value for value together with the unread input bits and Range 898 for range extended indefinitely on the right by zeros, the condition 899 Value < Range is maintained at all times by the decoder. In 900 particular, the bits shifted out of value as it is doubled are always 901 zero. 903 7.3. Actual Implementation 905 The C code below gives complete implementations of the encoder and 906 decoder described above. While they are logically identical to the 907 "bit-at-a-time" versions, they internally buffer a couple of extra 908 bytes of the bitstream. This allows I/O to be done (more 909 practically) a byte at a time and drastically reduces the number of 910 carries the encoder has to propagate into the already-written data. 912 Another (logically equivalent) implementation may be found in the 913 reference decoder files dboolhuff.h and dboolhuff.c. 915 ---- Begin code block -------------------------------------- 917 /* Encoder first */ 919 typedef struct { 920 uint8 *output; /* ptr to next byte to be written */ 921 uint32 range; /* 128 <= range <= 255 */ 922 uint32 bottom; /* minimum value of remaining output */ 923 int bit_count; /* # of shifts before an output byte 924 is available */ 925 } bool_encoder; 927 /* Must set initial state of encoder before writing any bools. */ 929 void init_bool_encoder( bool_encoder *e, uint8 *start_partition) 930 { 931 e->output = start_partition; 932 e->range = 255; 933 e->bottom = 0; 934 e->bit_count = 24; 935 } 937 /* Encoding very rarely produces a carry that must be propagated 938 to the already-written output. The arithmetic guarantees that 939 the propagation will never go beyond the beginning of the 940 output. Put another way, the encoded value x is always less 941 than one. */ 943 void add_one_to_output( uint8 *q) 944 { 945 while( *--q == 255) 946 *q = 0; 947 ++*q; 948 } 950 /* Main function writes a bool_value whose probability of being 951 zero is (expected to be) prob/256. */ 953 void write_bool( bool_encoder *e, Prob prob, int bool_value) 954 { 955 /* split is approximately (range * prob) / 256 and, 956 crucially, is strictly bigger than zero and strictly 957 smaller than range */ 959 uint32 split = 1 + ( ((e->range - 1) * prob) >> 8); 961 if( bool_value) { 962 e->bottom += split; /* move up bottom of interval */ 963 e->range -= split; /* with corresponding decrease in range */ 964 } else 965 e->range = split; /* decrease range, leaving bottom alone */ 967 while( e->range < 128) 968 { 969 e->range <<= 1; 971 if( e->bottom & (1 << 31)) /* detect carry */ 972 add_one_to_output( e->output); 974 e->bottom <<= 1; /* before shifting bottom */ 976 if( !--e->bit_count) { /* write out high byte of bottom ... */ 978 *e->output++ = (uint8) (e->bottom >> 24); 980 e->bottom &= (1 << 24) - 1; /* ... keeping low 3 bytes */ 981 e->bit_count = 8; /* 8 shifts until next output */ 982 } 983 } 984 } 986 /* Call this function (exactly once) after encoding the last 987 bool value for the partition being written */ 989 void flush_bool_encoder( bool_encoder *e) 990 { 991 int c = e->bit_count; 992 uint32 v = e->bottom; 994 if( v & (1 << (32 - c))) /* propagate (unlikely) carry */ 995 add_one_to_output( e->output); 996 v <<= c & 7; /* before shifting remaining output */ 997 c >>= 3; /* to top of internal buffer */ 998 while( --c >= 0) 999 v <<= 8; 1000 c = 4; 1001 while( --c >= 0) { /* write remaining data, possibly padded */ 1002 *e->output++ = (uint8) (v >> 24); 1003 v <<= 8; 1004 } 1005 } 1007 /* Decoder state exactly parallels that of the encoder. 1008 "value", together with the remaining input, equals the 1009 complete encoded number x less the left endpoint of the 1010 current coding interval. */ 1012 typedef struct { 1013 uint8 *input; /* pointer to next compressed data byte */ 1014 uint32 range; /* always identical to encoder's range */ 1015 uint32 value; /* contains at least 8 significant bits */ 1016 int bit_count; /* # of bits shifted out of 1017 value, at most 7 */ 1018 } bool_decoder; 1020 /* Call this function before reading any bools from the 1021 partition.*/ 1023 void init_bool_decoder( bool_decoder *d, uint8 *start_partition) 1024 { 1025 { 1026 int i = 0; 1027 d->value = 0; /* value = first 2 input bytes */ 1028 while( ++i <= 2) 1029 d->value = (d->value << 8) | *start_partition++; 1030 } 1032 d->input = start_partition; /* ptr to next byte to be read */ 1033 d->range = 255; /* initial range is full */ 1034 d->bit_count = 0; /* have not yet shifted out any bits */ 1035 } 1037 /* Main function reads a bool encoded at probability prob/256, 1038 which of course must agree with the probability used when the 1039 bool was written. */ 1041 int read_bool( bool_decoder *d, Prob prob) 1042 { 1043 /* range and split are identical to the corresponding values 1044 used by the encoder when this bool was written */ 1046 uint32 split = 1 + ( ((d->range - 1) * prob) >> 8); 1047 uint32 SPLIT = split << 8; 1048 int retval; /* will be 0 or 1 */ 1050 if( d->value >= SPLIT) { /* encoded a one */ 1051 retval = 1; 1052 d->range -= split; /* reduce range */ 1053 d->value -= SPLIT; /* subtract off left endpoint of interval */ 1054 } else { /* encoded a zero */ 1055 retval = 0; 1056 d->range = split; /* reduce range, no change in left endpoint */ 1057 } 1059 while( d->range < 128) { /* shift out irrelevant value bits */ 1060 d->value <<= 1; 1061 d->range <<= 1; 1062 if( ++d->bit_count == 8) { /* shift in new bits 8 at a time */ 1063 d->bit_count = 0; 1064 d->value |= *d->input++; 1065 } 1066 } 1067 return retval; 1068 } 1070 /* Convenience function reads a "literal", that is, a "num_bits" 1071 wide unsigned value whose bits come high- to low-order, with 1072 each bit encoded at probability 128 (i.e., 1/2). */ 1074 uint32 read_literal( bool_decoder *d, int num_bits) 1075 { 1076 uint32 v = 0; 1077 while( num_bits--) 1078 v = (v << 1) + read_bool( d, 128); 1079 return v; 1080 } 1082 /* Variant reads a signed number */ 1084 int32 read_signed_literal( bool_decoder *d, int num_bits) 1085 { 1086 int32 v = 0; 1087 if( !num_bits) 1088 return 0; 1089 if( read_bool( d, 128)) 1090 v = -1; 1091 while( --num_bits) 1092 v = (v << 1) + read_bool( d, 128); 1093 return v; 1094 } 1096 ---- End code block ---------------------------------------- 1098 8. Compressed Data Components 1100 At the lowest level, VP8's compressed data is simply a sequence of 1101 probabilistically-encoded bools. Most of this data is composed of 1102 (slightly) larger semantic units fashioned from bools, which we 1103 describe here. 1105 We sometimes use these descriptions in C expressions within data 1106 format specifications. In this context, they refer to the return 1107 value of a call to an appropriate bool_decoder d, reading (as always) 1108 from its current reference point. 1110 +--------------+-------+--------------------------------------------+ 1111 | Call | Alt. | Return | 1112 +--------------+-------+--------------------------------------------+ 1113 | Bool(p) | B(p) | Bool with probability p/256 of being 0. | 1114 | | | Return value of read_bool(d, p). | 1115 | | | | 1116 | Flag | F | A one-bit flag (same thing as a B(128) or | 1117 | | | an L(1)). Abbreviated F. Return value of | 1118 | | | read_bool(d, 128). | 1119 | | | | 1120 | Lit(n) | L(n) | Unsigned n-bit number encoded as n flags | 1121 | | | (a "literal"). Abbreviated L(n). The | 1122 | | | bits are read from high to low order. | 1123 | | | Return value of read_literal(d, n). | 1124 | | | | 1125 | SignedLit(n) | | Signed n-bit number encoded similarly to | 1126 | | | an L(n). Return value of | 1127 | | | read_signed_literal(d, n). These are | 1128 | | | rare. | 1129 | | | | 1130 | P(8) | | An 8-bit probability. No different from | 1131 | | | an L(8), but we sometimes use this | 1132 | | | notation to emphasize that a probability | 1133 | | | is being coded. | 1134 | | | | 1135 | P(7) | | A 7-bit specification of an 8-bit | 1136 | | | probability. Coded as an L(7) number x; | 1137 | | | the resulting 8-bit probability is x ? x | 1138 | | | << 1 : 1. | 1139 | | | | 1140 | F? X | | A flag which, if true, is followed by a | 1141 | | | piece of data X. | 1142 | | | | 1143 | F? X:Y | | A flag which, if true, is followed by X | 1144 | | | and, if false, is followed by Y. Also used | 1145 | | | to express a value where Y is an implicit | 1146 | | | default (not encoded in the data stream), | 1147 | | | as in F? P(8):255, which expresses an | 1148 | | | optional probability: if the flag is true, | 1149 | | | the probability is specified as an 8-bit | 1150 | | | literal, while if the flag is false, the | 1151 | | | probability defaults to 255. | 1152 | | | | 1153 | B(p)? X | B(p)? | Variants of the above using a boolean | 1154 | | X:Y | indicator whose probability is not | 1155 | | | necessarily 128. | 1156 | | | | 1157 | X | | Multi-component field, the specifics of | 1158 | | | which will be given at a more appropriate | 1159 | | | point in the discussion. | 1160 | | | | 1161 | T | | Tree-encoded value from small alphabet. | 1162 +--------------+-------+--------------------------------------------+ 1164 The last type requires elaboration. We often wish to encode 1165 something whose value is restricted to a small number of 1166 possibilities (the alphabet). 1168 This is done by representing the alphabet as the leaves of a small 1169 binary tree. The (non-leaf) nodes of the tree have associated 1170 probabilities p and correspond to calls to read_bool(d, p). We think 1171 of a zero as choosing the left branch below the node and a one as 1172 choosing the right branch. 1174 Thus every value (leaf) whose tree depth is x is decoded after 1175 exactly x calls to read_bool. 1177 A tree representing an encoding of an alphabet of n possible values 1178 always contains n-1 non-leaf nodes, regardless of its shape (this is 1179 easily seen by induction on n). 1181 There are many ways that a given alphabet can be so represented. The 1182 choice of tree has little impact on datarate but does affect decoder 1183 performance. The trees used by VP8 are chosen to (on average) 1184 minimize the number of calls to read_bool. This amounts to shaping 1185 the tree so that more probable values have smaller tree depth than do 1186 less probable values. 1188 Readers familiar with Huffman coding will notice that, given an 1189 alphabet together with probabilities for each value, the associated 1190 Huffman tree minimizes the expected number of calls to read_bool. 1192 Such readers will also realize that the coding method described here 1193 never results in higher datarates than does the Huffman method and, 1194 indeed, often results in much lower datarates. Huffman coding is, in 1195 fact, nothing more than a special case of this method in which each 1196 node probability is fixed at 128 (i.e., 1/2). 1198 8.1. Tree Coding Implementation 1200 We give a suggested implementation of a tree data structure followed 1201 by a couple of actual examples of its usage by VP8. 1203 It is most convenient to represent the values using small positive 1204 integers, typically an enum counting up from zero. The largest 1205 alphabet (used to code DCT coefficients, described in Chapter 13 that 1206 is tree-coded by VP8 has only 12 values. The tree for this alphabet 1207 adds 11 interior nodes and so has a total of 23 positions. Thus, an 1208 8-bit number easily accommodates both a tree position and a return 1209 value. 1211 A tree may then be compactly represented as an array of (pairs of) 1212 8-bit integers. Each (even) array index corresponds to an interior 1213 node of the tree;, the zeroth index of course corresponds to the root 1214 of the tree. The array entries come in pairs corresponding to the 1215 left (0) and right (1) branches of the subtree below the interior 1216 node. We use the convention that a positive (even) branch entry is 1217 the index of a deeper interior node, while a nonpositive entry v 1218 corresponds to a leaf whose value is -v. 1220 The node probabilities associated to a tree-coded value are stored in 1221 an array whose indices are half the indices of the corresponding tree 1222 positions. The length of the probability array is one less than the 1223 size of the alphabet. 1225 Here is C code implementing the foregoing. The advantages of our 1226 data structure should be noted. Aside from the smallness of the 1227 structure itself, the tree-directed reading algorithm is essentially 1228 a single line of code. 1230 ---- Begin code block -------------------------------------- 1232 /* A tree specification is simply an array of 8-bit integers. */ 1234 typedef int8 tree_index; 1235 typedef const tree_index Tree[]; 1237 /* Read and return a tree-coded value at the current decoder 1238 position. */ 1240 int treed_read( 1241 bool_decoder * const d, /* bool_decoder always returns a 0 or 1 */ 1242 Tree t, /* tree specification */ 1243 const Prob p[] /* corresponding interior node probabilities */ 1244 ) { 1245 register tree_index i = 0; /* begin at root */ 1247 /* Descend tree until leaf is reached */ 1249 while( ( i = t[ i + read_bool( d, p[i>>1]) ] ) > 0) {} 1251 return -i; /* return value is negation of nonpositive index */ 1252 } 1254 ---- End code block ---------------------------------------- 1256 Tree-based decoding is implemented in the reference decoder file 1257 tree_reader.h. 1259 8.2. Tree Coding Example 1261 As a multi-part example, without getting too far into the semantics 1262 of macroblock decoding (which is of course taken up below), we look 1263 at the "mode" coding for intra-predicted macroblocks. 1265 It so happens that, because of a difference in statistics, the Y (or 1266 luma) mode encoding uses two different trees: one for key frames and 1267 another for interframes. This is the only instance in VP8 of the 1268 same dataset being coded by different trees under different 1269 circumstances. The UV (or chroma) modes are a proper subset of the Y 1270 modes and, as such, have their own decoding tree. 1272 ---- Begin code block -------------------------------------- 1274 typedef enum 1275 { 1276 DC_PRED, /* predict DC using row above and column to the left */ 1277 V_PRED, /* predict rows using row above */ 1278 H_PRED, /* predict columns using column to the left */ 1279 TM_PRED, /* propagate second differences a la "true motion" */ 1281 B_PRED, /* each Y subblock is independently predicted */ 1283 num_uv_modes = B_PRED, /* first four modes apply to chroma */ 1284 num_ymodes /* all modes apply to luma */ 1285 } 1286 intra_mbmode; 1288 /* The aforementioned trees together with the implied codings as 1289 comments. 1290 Actual (i.e., positive) indices are always even. 1291 Value (i.e., nonpositive) indices are arbitrary. */ 1293 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 1294 { 1295 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 1296 4, 6, /* "1" subtree has 2 descendant subtrees */ 1297 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 1298 H_PRED = "101" */ 1299 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 1300 B_PRED = "111" */ 1301 }; 1303 const tree_index kf_ymode_tree [2 * (num_ymodes - 1)] = 1304 { 1305 -B_PRED, 2, /* root: B_PRED = "0", "1" subtree */ 1306 4, 6, /* "1" subtree has 2 descendant subtrees */ 1307 -DC_PRED, -V_PRED, /* "10" subtree: DC_PRED = "100", 1308 V_PRED = "101" */ 1309 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1310 TM_PRED = "111" */ 1311 }; 1313 const tree_index uv_mode_tree [2 * (num_uv_modes - 1)] = 1314 { 1315 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 1316 -V_PRED, 4, /* "1" subtree: V_PRED = "10", 1317 "11" subtree */ 1318 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1319 TM_PRED = "111" */ 1320 }; 1322 /* Given a bool_decoder d, a Y mode might be decoded as follows.*/ 1324 const Prob pretend_its_huffman [num_ymodes - 1] = 1325 { 128, 128, 128, 128}; 1327 Ymode = (intra_mbmode) treed_read( d, ymode_tree, 1328 pretend_its_huffman); 1330 ---- End code block ---------------------------------------- 1332 Since it greatly facilitates re-use of reference code and since there 1333 is no real reason to do otherwise, it is strongly suggested that any 1334 decoder implementation use exactly the same enumeration values and 1335 probability table layouts as described in this document (and in the 1336 reference code) for all tree-coded data in VP8. 1338 9. Frame Header 1340 The uncompressed data chunk at the start of each frame and the first 1341 part of the first data partition contains information pertaining to 1342 the frame as a whole. We list the fields in the order of occurrence, 1343 giving details for some of the fields. Other details are postponed 1344 until a more logical point in our overall description. Most of the 1345 header decoding occurs in the reference decoder file decodeframe.c. 1347 9.1. Uncompressed Data Chunk 1349 The uncompressed data chunk comprises a common (for key frames and 1350 interframes) 3-byte frame tag that contains four fields, as follows: 1352 1. A 1-bit frame type (0 for key frames, 1 for interframes). 1354 2. A 3-bit version number (0 - 3 are defined as four different 1355 profiles with different decoding complexity; other values may be 1356 defined for future variants of the VP8 data format). 1358 3. A 1-bit show_frame flag (0 when current frame is not for display, 1359 1 when current frame is for display). 1361 4. A 19-bit field containing the size of the first data partition in 1362 bytes. 1364 Version number enables or disables certain features in the bitstream, 1365 as follows: 1367 +---------+-------------------------+-------------+ 1368 | Version | Reconstruction filter | Loop filter | 1369 +---------+-------------------------+-------------+ 1370 | 0 | Bicubic | Normal | 1371 | | | | 1372 | 1 | Bilinear | Simple | 1373 | | | | 1374 | 2 | Bilinear | None | 1375 | | | | 1376 | 3 | None | None | 1377 | | | | 1378 | Other | Reserved for future use | | 1379 +---------+-------------------------+-------------+ 1381 The reference software also adjusts the loop filter based on version 1382 number, as per the table above. Version number 1 implies a "simple" 1383 loop filter and version numbers 2 and 3 imply no loop filter. 1384 However, the "simple" filter setting in this context has no effect 1385 whatsoever on the decoding process, and the "no loop filter" setting 1386 only forces the reference encoder to set filter level equal to 0. 1387 Neither affect the decoding process. In decoding, the only loop 1388 filter settings that matter are those in the frame header. 1390 For key frames the frame tag is followed by a further 7 bytes of 1391 uncompressed data, as follows: 1393 ---- Begin code block -------------------------------------- 1395 Start code byte 0 0x9d 1396 Start code byte 1 0x01 1397 Start code byte 2 0x2a 1399 16 bits : (2 bits Horizontal Scale << 14) | Width (14 bits) 1400 16 bits : (2 bits Vertical Scale << 14) | Height (14 bits) 1402 ---- End code block ---------------------------------------- 1404 The following source code segment illustrates validation of the start 1405 code and reading the width, height and scale factors for a key frame. 1407 ---- Begin code block -------------------------------------- 1409 unsigned char *c = pbi->Source+3; 1411 // vet via sync code 1412 if(c[0]!=0x9d||c[1]!=0x01||c[2]!=0x2a) 1413 return -1; 1415 ---- End code block ---------------------------------------- 1417 where pbi->source points to the beginning of the frame. 1419 The following code reads the image dimension from the bitstream: 1421 ---- Begin code block -------------------------------------- 1423 pc->Width = swap2(*(unsigned short*)(c+3))&0x3fff; 1424 pc->horiz_scale = swap2(*(unsigned short*)(c+3))>>14; 1425 pc->Height = swap2(*(unsigned short*)(c+5))&0x3fff; 1426 pc->vert_scale = swap2(*(unsigned short*)(c+5))>>14; 1428 ---- End code block ---------------------------------------- 1430 where swap2 macro takes care of the endian on different platform: 1432 ---- Begin code block -------------------------------------- 1434 #if defined(__ppc__) || defined(__ppc64__) 1435 # define swap2(d) \ 1436 ((d&0x000000ff)<<8) | \ 1437 ((d&0x0000ff00)>>8) 1438 #else 1439 # define swap2(d) d 1440 #endif 1442 ---- End code block ---------------------------------------- 1444 While each frame is encoded as a raster scan of 16x16 macroblocks, 1445 the frame dimensions are not necessarily evenly divisible by 16. In 1446 this case, write ew = 16 - (width & 15) and eh = 16 - (height & 15) 1447 for the excess width and height, respectively. Although they are 1448 encoded, the last ew columns and eh rows are not actually part of the 1449 image and should be discarded before final output. However, these 1450 "excess pixels" should be maintained in the internal reconstruction 1451 buffer used to predict ensuing frames. 1453 The scaling specifications for each dimension are encoded as follows. 1455 +-------+--------------------------------------+ 1456 | Value | Scaling | 1457 +-------+--------------------------------------+ 1458 | 0 | No upscaling (the most common case). | 1459 | | | 1460 | 1 | Upscale by 5/4. | 1461 | | | 1462 | 2 | Upscale by 5/3. | 1463 | | | 1464 | 3 | Upscale by 2. | 1465 +-------+--------------------------------------+ 1467 Upscaling does not affect the reconstruction buffer, which should be 1468 maintained at the encoded resolution. Any reasonable method of 1469 upsampling (including any that may be supported by video hardware in 1470 the playback environment) may be used. Since scaling has no effect 1471 on decoding, we do not discuss it any further. 1473 As discussed in Chapter 5, allocation (or re-allocation) of data 1474 structures (such as the reconstruction buffer) whose size depends on 1475 dimension will be triggered here. 1477 9.2. Color Space and Pixel Type (Key Frames-only) 1479 +-------+------------------------------------------+ 1480 | Field | Value | 1481 +-------+------------------------------------------+ 1482 | L(1) | 1-bit color space type specification | 1483 | | | 1484 | L(1) | 1-bit pixel value clamping specification | 1485 +-------+------------------------------------------+ 1487 The color space type bit is encoded as the following: 1489 o 0 - YUV color space similar to the YCrCb color space defined in 1490 [ITU-R_BT.601] 1492 o 1 - Reserved for future use 1494 The pixel value clamping type bit is encoded as the following: 1496 o 0 - Decoders are required to clamp the reconstructed pixel values 1497 to between 0 and 255 (inclusive). 1499 o 1 - Reconstructed pixel values are guaranteed to be between 0 and 1500 255, no clamping is necessary. 1502 Information in this subsection does not appear in interframes. 1504 9.3. Segment-based Adjustments 1506 This subsection contains probability and value information for 1507 implementing segment adaptive adjustments to default decoder 1508 behaviors. The data in this section is used in the decoding of the 1509 ensuing per-segment information and applies to the entire frame. 1510 When segment adaptive adjustments are enabled, each macroblock will 1511 be assigned a segment ID. Macroblocks with the same segment ID 1512 belong to same segment, and have the same adaptive adjustments over 1513 default baseline values for the frame. The adjustments can be 1514 quantization level or loop filter strength. 1516 The context for decoding this feature at macroblock level is provided 1517 by a subsection in the frame header, which contains:: 1519 1. A segmentation_enabled Flag which if 1 (0), enables (disables) 1520 the feature for this frame. The remaining fields occur if the 1521 feature is enabled. 1523 2. L(1) indicates if the segment map is updated for the current 1524 frame (update_mb_segmentaton_map) 1526 3. L(1) indicates if the segment feature data items are updated for 1527 the current frame 1529 4. If flag in 3 is 1, the following fields occur: 1531 1. L(1) the mode of segment feature data, can be absolute value 1532 mode or delta value mode, later mode, feature data is the 1533 difference against current frame defaults. 1535 2. Segment feature data items are decoded segment by each 1536 segment for each segment feature. For every data item, a one 1537 bit flag indicating if the item is 0 or a non-zero value to 1538 be decoded. If there is non-zero value, the value is decoded 1539 as a magnitude L(n) followed by a one bit sign (L(1), 0 for 1540 positive and 1 for negative). The length n can be looked up 1541 from a pre-defined length table for all feature data. 1543 5. If flag in 2 is 1, the probabilities of the decoding tree for 1544 segment map are decoded from the bitstream. Each probability is 1545 decoded with one bit flag indicating if the probability is the 1546 default value of 255 (flag is 0), or the probability is an 8-bit 1547 value, L(8), from the bitstream. 1549 The layout and semantics supporting this feature at the macroblock 1550 level will be described in Chapter 10. 1552 9.4. Loop Filter Type and Levels 1554 VP8 supports two types of loop filter, having different computational 1555 complexity. The following bits occur in the header to support the 1556 selection of the baseline type, strength and sharpness behavior of 1557 the loop filter used for the current frame. 1559 +-------+-------------------+ 1560 | Index | Description | 1561 +-------+-------------------+ 1562 | L(1) | filter_type | 1563 | | | 1564 | L(6) | loop_filter_level | 1565 | | | 1566 | L(3) | sharpness_level | 1567 +-------+-------------------+ 1569 The meaning of these numbers will be further explained in Chapter 15. 1571 VP8 has a feature in the bitstream that enables adjustment of the 1572 loop filter level based on a macroblock's prediction mode and 1573 reference frame. The per-macroblock adjustment is done through delta 1574 values against default loop filter level for the current frame. This 1575 subsection contains flag and value information for implementing per- 1576 macroblock loop filter level adjustment to default decoder behaviors. 1577 The data in this section is used in the decoding of the ensuing per- 1578 macroblock information and applies to the entire frame. 1580 L(1) is a one-bit flag indicating if macroblock loop filter 1581 adjustment is on for the current frame. 0 means such feature is not 1582 supported in the current frame and 1 means this feature is enabled 1583 for the current frame. 1585 Whether the adjustment is based on reference frame or encoding mode, 1586 the adjustment of loop filter level is done via a delta value against 1587 a baseline loop filter value. The delta values are updated for the 1588 current frame if an L(1) bit, mode_ref_lf_delta_update, takes the 1589 value 1. There are two groups of delta values, one group of delta 1590 values are for reference frame-based adjustments, the other group is 1591 for mode-based adjustments. The number of delta values in the two 1592 groups is MAX_REF_LF_DELTAS and MAX_MODE_LF_DELTAS, respectively. 1593 For every value within the two groups, there is one bit L(1) to 1594 indicate if the particular value is updated. When one is updated 1595 (1), it is transmitted as a six-bit magnitude L(6) followed by a one- 1596 bit sign flag (L(1), 0 for positive and 1 for negative). 1598 9.5. Token Partition and Partition Data Offsets 1600 VP8 allows DCT coefficients to be packed into multiple partitions 1601 besides the first partition with header and per-macroblock prediction 1602 information, so the decoder can perform parallel decoding in an 1603 efficient manner. There are two bits L(2) used to indicate the 1604 number of coefficient data partitions within a compressed frame. The 1605 two bits are defined in the following table: 1607 +-------+-------+----------------------+ 1608 | Bit 1 | Bit 0 | Number of Partitions | 1609 +-------+-------+----------------------+ 1610 | 0 | 0 | 1 | 1611 | | | | 1612 | 0 | 1 | 2 | 1613 | | | | 1614 | 1 | 0 | 4 | 1615 | | | | 1616 | 1 | 1 | 8 | 1617 +-------+-------+----------------------+ 1619 Offsets are embedded in the bitstream to provide the decoder direct 1620 access to token partitions. If the number of data partitions is 1621 greater then 1, the size of each partition (except the last) is 1622 written in 3 bytes (24 bits). The size of the last partition is the 1623 remainder of the data not used by any of the previous partitions. 1624 The partitioned data are consecutive in the bitstream, so the size 1625 can also be used to calculate the offset of each partition. The 1626 following pseudo code illustrates how the size/offset is defined by 1627 the three bytes in the bitstream. 1629 ---- Begin code block -------------------------------------- 1631 Offset/size = (uint32)(byte0) + ((uint32)(byte1)<<8) 1632 + ((uint32)(byte2)<<16); 1634 ---- End code block ---------------------------------------- 1636 9.6. Dequantization Indices 1638 All residue signals are specified via a quantized 4x4 DCT applied to 1639 the Y, U, V, or Y2 subblocks of a macroblock. As detailed in Chapter 1640 14, before inverting the transform, each decoded coefficient is 1641 multiplied by one of six dequantization factors, the choice of which 1642 depends on the plane (Y, chroma = U or V, Y2) and coefficient 1643 position (DC = coefficient 0, AC = coefficients 1-15). The six 1644 values are specified using 7-bit indices into six corresponding fixed 1645 tables (the tables are given in Chapter 14). 1647 The first 7-bit index gives the dequantization table index for Y 1648 plane AC coefficients, called yac_qi. It is always coded and acts as 1649 a baseline for the other 5 quantization indices, each of which is 1650 represented by a delta from this baseline index. Following is pseudo 1651 code for reading the indices: 1653 ---- Begin code block -------------------------------------- 1655 yac_qi = L(7); /* Y ac index always specified */ 1656 ydc_delta = F? delta(): 0; /* Y dc delta specified if 1657 flag is true */ 1659 y2dc_delta = F? delta(): 0; /* Y2 dc delta specified if 1660 flag is true */ 1661 y2ac_delta = F? delta(): 0; /* Y2 ac delta specified if 1662 flag is true */ 1664 uvdc_delta = F? delta(): 0; /* chroma dc delta specified 1665 if flag is true */ 1666 uvac_delta = F? delta(): 0; /* chroma ac delta specified 1667 if flag is true */ 1669 ---- End code block ---------------------------------------- 1670 Where delta() is the process to read 5 bits from the bitstream to 1671 determine a signed delta value: 1673 +-------+--------------------------------------------------+ 1674 | Index | Description | 1675 +-------+--------------------------------------------------+ 1676 | L(4) | Magnitude of delta | 1677 | | | 1678 | L(1) | Sign of delta, 0 for positive and 1 for negative | 1679 +-------+--------------------------------------------------+ 1681 9.7. Refresh Golden Frame and AltRef Frame 1683 For key frames, both golden frame and altref frame are refreshed/ 1684 replaced by the current reconstructed frame, by default. For non-key 1685 frames, VP8 uses two bits to indicate whether the two frame buffers 1686 are refreshed, using the reconstructed current frame: 1688 +-------+----------------------------------------------------------+ 1689 | Index | Description | 1690 +-------+----------------------------------------------------------+ 1691 | L(1) | Whether golden frame is refreshed (0 for no, 1 for yes). | 1692 | | | 1693 | L(1) | Whether altref frame is refreshed (0 for no, 1 for yes). | 1694 +-------+----------------------------------------------------------+ 1696 When the flag for golden frame is 0, VP8 uses 2 more bits in the 1697 bitstream to indicate whether the buffer (and which buffer) is copied 1698 to the golden frame, or if no buffer is copied: 1700 +-------+------------------------------------------+ 1701 | Index | Description | 1702 +-------+------------------------------------------+ 1703 | L(2) | Buffer copy flag for golden frame buffer | 1704 +-------+------------------------------------------+ 1706 Where: 1708 o 0 means no buffer is copied to golden frame 1710 o 1 means last_frame is copied to golden frame 1712 o 2 means alt_ref_frame is copied to golden frame 1714 Similarly, when the flag for altref is 0, VP8 uses 2 bits in the 1715 bitstream to indicate which buffer is copied to alt_ref_frame. 1717 +-------+------------------------------------------+ 1718 | Index | Description | 1719 +-------+------------------------------------------+ 1720 | L(2) | Buffer copy flag for altref frame buffer | 1721 +-------+------------------------------------------+ 1723 Where: 1725 o 0 means no buffer is copied to altref frame 1727 o 1 means last_frame is copied to altref frame 1729 o 2 means golden_frame is copied to altref frame 1731 Two bits are transmitted for ref_frame_sign_bias for golden_frame and 1732 alt_ref_frame respectively. 1734 +-------+---------------------------------+ 1735 | Index | Description | 1736 +-------+---------------------------------+ 1737 | L(1) | Sign bias flag for golden frame | 1738 | | | 1739 | L(1) | Sign bias flag for altref frame | 1740 +-------+---------------------------------+ 1742 These values are used to control the sign of the motion vectors when 1743 a golden frame or an altref frame is used as the reference frame for 1744 a macroblock. 1746 9.8. Refresh Last Frame Buffer 1748 VP8 uses one bit, L(1), to indicate if the last frame reference 1749 buffer is refreshed using the constructed current frame. On key 1750 frame this bit is overridden, and the last frame buffer is always 1751 refreshed. 1753 9.9. DCT Coefficient Probability Update 1755 Contains a partial update of the probability tables used to decode 1756 DCT coefficients. These tables are maintained across interframes but 1757 are of course replaced with their defaults at the beginning of every 1758 key frame. 1760 The layout and semantics of this field will be taken up in Chapter 1761 13. 1763 9.10. Remaining Frame Header Data (non-Key Frame) 1765 +-------+-----------------------------------------------------------+ 1766 | Index | Description | 1767 +-------+-----------------------------------------------------------+ 1768 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1769 | | if skipping of macroblocks with no non-zero coefficients | 1770 | | is enabled. If it is set to 0 then prob_skip_false is | 1771 | | not read and mb_skip_coeff is forced to 0 for all | 1772 | | macroblocks (see Sections 11.1 and 12.1). | 1773 | | | 1774 | L(8) | prob_skip_false = probability used for decoding a | 1775 | | macroblock level flag, which indicates if a macroblock | 1776 | | has any non-zero coefficients. Only read if | 1777 | | mb_no_coeff_skip is 1. | 1778 | | | 1779 | L(8) | prob_intra = probability that a macroblock is "intra" | 1780 | | predicted, that is, predicted from the already-encoded | 1781 | | portions of the current frame as opposed to "inter" | 1782 | | predicted, that is, predicted from the contents of a | 1783 | | prior frame. | 1784 | | | 1785 | L(8) | prob_last = probability that an inter-predicted | 1786 | | macroblock is predicted from the immediately previous | 1787 | | frame, as opposed to the most recent golden frame or | 1788 | | altref frame.. | 1789 | | | 1790 | L(8) | prob_gf = probability that an inter-predicted macroblock | 1791 | | is predicted from the most recent golden frame, as | 1792 | | opposed to the altref frame | 1793 | | | 1794 | F | If true, followed by four L(8)s updating the | 1795 | | probabilities for the different types of intra-prediction | 1796 | | for the Y plane. These probabilities correspond to the | 1797 | | four interior nodes of the decoding tree for intra Y | 1798 | | modes in an interframe, that is, the even positions in | 1799 | | the ymode_tree array given above. | 1800 | | | 1801 | F | If true, followed by three L(8)s updating the | 1802 | | probabilities for the different types of intra-prediction | 1803 | | for the chroma planes. These probabilities correspond to | 1804 | | the even positions in the uv_mode_tree array given above. | 1805 | | | 1806 | X | Motion vector probability update. The details will be | 1807 | | given after the discussion of motion vector decoding. | 1808 +-------+-----------------------------------------------------------+ 1810 Decoding of this portion (only) of the frame header is handled in the 1811 reference decoder file decodemv.c. 1813 9.11. Remaining Frame Header Data (Key Frame) 1815 +-------+-----------------------------------------------------------+ 1816 | Index | Description | 1817 +-------+-----------------------------------------------------------+ 1818 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1819 | | if skipping of macroblocks with no non-zero coefficients | 1820 | | is enabled. If it is set to 0 then prob_skip_false is | 1821 | | not read and mb_skip_coeff is forced to 0 for all | 1822 | | macroblocks (see Sections 11.1 and 12.1). | 1823 | | | 1824 | L(8) | prob_skip_false = Probability used for decoding a | 1825 | | macroblock level flag, which indicates if a macroblock | 1826 | | has any non-zero coefficients. Only read if | 1827 | | mb_no_coeff_skip is 1. | 1828 +-------+-----------------------------------------------------------+ 1830 Decoding of this portion of the frame header is handled in the 1831 reference decoder file demode.c. 1833 This completes the layout of the frame header. The remainder of the 1834 first data partition consists of macroblock-level prediction data. 1836 After the frame header is processed, all probabilities needed to 1837 decode the prediction and residue data are known and will not change 1838 until the next frame. 1840 10. Segment-based Feature Adjustments 1842 Every macroblock may optionally override some of the default 1843 behaviors of the decoder. Specifically, VP8 uses segment based 1844 adjustments to support changing quantizer level and loop filter level 1845 for a macroblock. When the segment-based adjustment feature is 1846 enabled for a frame, each macroblock within the frame is coded with a 1847 segment_id. This effectively segments all the macroblocks in the 1848 current frame into a number of different segments. Macroblocks 1849 within the same segment behave exactly the same for quantizer and 1850 loop filter level adjustments. 1852 If both the segmentation_enabled and update_mb_segmentation_map flags 1853 in subsection B of the frame header take a value of 1, the prediction 1854 data for each (intra- or inter-coded) macroblock begins with a 1855 specification of segment_id for the current macroblock. It is 1856 decoded using this simple tree ... 1858 ---- Begin code block -------------------------------------- 1860 const tree_index mb_segment_tree [2 * (4-1)] = 1861 { 1862 2, 4, /* root: "0", "1" subtrees */ 1863 -0, -1, /* "00" = 0th value, "01" = 1st value */ 1864 -2, -3 /* "10" = 2nd value, "11" = 3rd value */ 1865 } 1867 ---- End code block ---------------------------------------- 1869 ... combined with a 3-entry probability table 1870 mb_segment_tree_probs[3]. The macroblock's segment_id is used later 1871 in the decoding process to look into the segment_feature_data table 1872 and determine how the quantizer and loop filter levels are adjusted. 1874 The decoding of segment_id, together with the parsing of intra- 1875 prediction modes (which is taken up next), is implemented in the 1876 reference decoder file demode.c. 1878 11. Key Frame Macroblock Prediction Records 1880 After the features described above, the macroblock prediction record 1881 next specifies the prediction mode used for the macroblock. 1883 11.1. mb_skip_coeff 1885 The single bool flag is decoded using prob_skip_false if and only if 1886 mb_no_coeff_skip is set to 1 (see sections 9.10 and 9.11). If 1887 mb_no_coeff_skip is set to 0 then this value defaults to 0. 1889 11.2. Luma Modes 1891 First comes the luma specification of type intra_mbmode, coded using 1892 the kf_ymode_tree, as described in Chapter 8 and repeated here for 1893 convenience: 1895 ---- Begin code block -------------------------------------- 1897 typedef enum 1898 { 1899 DC_PRED, /* predict DC using row above and column to the left */ 1900 V_PRED, /* predict rows using row above */ 1901 H_PRED, /* predict columns using column to the left */ 1902 TM_PRED, /* propagate second differences a la "true motion" */ 1904 B_PRED, /* each Y subblock is independently predicted */ 1906 num_uv_modes = B_PRED, /* first four modes apply to chroma */ 1907 num_ymodes /* all modes apply to luma */ 1908 } 1909 intra_mbmode; 1911 const tree_index kf_ymode_tree [2 * (num_ymodes - 1)] = 1912 { 1913 -B_PRED, 2, /* root: B_PRED = "0", "1" subtree */ 1914 4, 6, /* "1" subtree has 2 descendant subtrees */ 1915 -DC_PRED, -V_PRED, /* "10" subtree: DC_PRED = "100", 1916 V_PRED = "101" */ 1917 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1918 TM_PRED = "111" */ 1919 }; 1921 ---- End code block ---------------------------------------- 1923 For key frames, the Y mode is decoded using a fixed probability array 1924 as follows: 1926 ---- Begin code block -------------------------------------- 1928 const Prob kf_ymode_prob [num_ymodes - 1] = { 145, 156, 163, 128}; 1929 Ymode = (intra_mbmode) treed_read( d, kf_ymode_tree, kf_ymode_prob); 1931 ---- End code block ---------------------------------------- 1933 d is of course the bool_decoder being used to read the first data 1934 partition. 1936 If the Ymode is B_PRED, it is followed by a (tree-coded) mode for 1937 each of the 16 Y subblocks. The 10 subblock modes and their coding 1938 tree as follows: 1940 ---- Begin code block -------------------------------------- 1942 typedef enum 1943 { 1944 B_DC_PRED, /* predict DC using row above and column 1945 to the left */ 1946 B_TM_PRED, /* propagate second differences a la 1947 "true motion" */ 1949 B_VE_PRED, /* predict rows using row above */ 1950 B_HE_PRED, /* predict columns using column to the left */ 1952 B_LD_PRED, /* southwest (left and down) 45 degree diagonal 1953 prediction */ 1954 B_RD_PRED, /* southeast (right and down) "" */ 1956 B_VR_PRED, /* SSE (vertical right) diagonal prediction */ 1957 B_VL_PRED, /* SSW (vertical left) "" */ 1958 B_HD_PRED, /* ESE (horizontal down) "" */ 1959 B_HU_PRED, /* ENE (horizontal up) "" */ 1961 num_intra_bmodes 1962 } 1963 intra_bmode; 1965 /* Coding tree for the above, with implied codings as comments */ 1967 const tree_index bmode_tree [2 * (num_intra_bmodes - 1)] = 1968 { 1969 -B_DC_PRED, 2, /* B_DC_PRED = "0" */ 1970 -B_TM_PRED, 4, /* B_TM_PRED = "10" */ 1971 -B_VE_PRED, 6, /* B_VE_PRED = "110" */ 1972 8, 12, 1973 -B_HE_PRED, 10, /* B_HE_PRED = "11100" */ 1974 -B_RD_PRED, -B_VR_PRED, /* B_RD_PRED = "111010", 1975 B_VR_PRED = "111011" */ 1976 -B_LD_PRED, 14, /* B_LD_PRED = "111110" */ 1977 -B_VL_PRED, 16, /* B_VL_PRED = "1111110" */ 1978 -B_HD_PRED, -B_HU_PRED /* HD = "11111110", 1979 HU = "11111111" */ 1980 }; 1982 ---- End code block ---------------------------------------- 1984 The first four modes are smaller versions of the similarly-named 1985 16x16 modes above, albeit with slightly different numbering. The 1986 last six "diagonal" modes are unique to luma subblocks. 1988 11.3. Subblock Mode Contexts 1990 The coding of subblock modes in key frames uses the modes already 1991 coded for the subblocks to the left of and above the subblock to 1992 select a probability array for decoding the current subblock mode. 1993 This is our first instance of contextual prediction, and there are 1994 several caveats associated with it: 1996 1. The adjacency relationships between subblocks are based on the 1997 normal default raster placement of the subblocks. 1999 2. The adjacent subblocks need not lie in the current macroblock. 2000 The subblocks to the left of the left-edge subblocks 0, 4, 8, and 2001 12 are the right-edge subblocks 3, 7, 11, and 15, respectively, 2002 of the (already coded) macroblock immediately to the left. 2003 Similarly, the subblocks above the top-edge subblocks 0, 1, 2, 2004 and 3 are the bottom-edge subblocks 12, 13, 14, and 15 of the 2005 already-coded macroblock immediately above us. 2007 3. For macroblocks on the top row or left edge of the image, some of 2008 the predictors will be non-existent. Such predictors are taken 2009 to have had the value B_DC_PRED which, perhaps conveniently, 2010 takes the value 0 in the enumeration above. A simple management 2011 scheme for these contexts might maintain a row of above 2012 predictors and four left predictors. Before decoding the frame, 2013 the entire row is initialized to B_DC_PRED; before decoding each 2014 row of macroblocks, the four left predictors are also set to 2015 B_DC_PRED. After decoding a macroblock, the bottom four subblock 2016 modes are copied into the row predictor (at the current position, 2017 which then advances to be above the next macroblock) and the 2018 right four subblock modes are copied into the left predictor. 2020 4. Many macroblocks will of course be coded using a 16x16 luma 2021 prediction mode. For the purpose of predicting ensuing subblock 2022 modes (only), such macroblocks derive a subblock mode, constant 2023 throughout the macroblock, from the 16x16 luma mode as follows: 2024 DC_PRED uses B_DC_PRED, V_PRED uses B_VE_PRED, H_PRED uses 2025 B_HE_PRED, and TM_PRED uses B_TM_PRED. 2027 5. Although we discuss interframe modes later, we remark here that, 2028 while interframes do use all the intra-coding modes described 2029 here and below, the subblock modes in an interframe are coded 2030 using a single constant probability array that does not depend on 2031 any context. 2033 The dependence of subblock mode probability on the nearby subblock 2034 mode context is most easily handled using a three-dimensional 2035 constant array: 2037 ---- Begin code block -------------------------------------- 2039 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2040 [num_intra_bmodes-1]; 2042 ---- End code block ---------------------------------------- 2044 The outer two dimensions of this array are indexed by the already- 2045 coded subblock modes above and to the left of the current block, 2046 respectively. The inner dimension is a typical tree probability list 2047 whose indices correspond to the even indices of the bmode_tree above. 2048 The mode for the j^(th) luma subblock is then 2050 ---- Begin code block -------------------------------------- 2052 Bmode = (intra_bmode) treed_read( d, bmode_tree, kf_bmode_prob 2053 [A] [L]); 2055 ---- End code block ---------------------------------------- 2057 where the 4x4 Y subblock index j varies from 0 to 15 in raster order 2058 and A and L are the modes used above and to-the-left of the j^(th) 2059 subblock. 2061 The contents of the kf_bmode_prob array are given at the end of this 2062 chapter. 2064 11.4. Chroma Modes 2066 After the Y mode (and optional subblock mode) specification comes the 2067 chroma mode. The chroma modes are a subset of the Y modes and are 2068 coded using the uv_mode_tree described in Chapter 8, again repeated 2069 here for convenience: 2071 ---- Begin code block -------------------------------------- 2073 const tree_index uv_mode_tree [2 * (num_uv_modes - 1)] = 2074 { 2075 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 2076 -V_PRED, 4, /* "1" subtree: V_PRED = "10", 2077 "11" subtree */ 2078 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 2079 TM_PRED = "111" */ 2080 }; 2082 ---- End code block ---------------------------------------- 2084 As for the Y modes (in a key frame), the chroma modes are coded using 2085 a fixed, contextless probability table: 2087 ---- Begin code block -------------------------------------- 2089 const Prob kf_uv_mode_prob [num_uv_modes - 1] = { 142, 114, 183}; 2090 uv_mode = (intra_mbmode) treed_read( d, uv_mode_tree, 2091 kf_uv_mode_prob); 2093 ---- End code block ---------------------------------------- 2095 This completes the description of macroblock prediction coding for 2096 key frames. As will be discussed in Chapter 16, the coding of intra 2097 modes within interframes is similar, but not identical, to that 2098 described here (and in the reference code) for prediction modes and, 2099 indeed, for all tree-coded data in VP8. 2101 11.5. Subblock Mode Probability Table 2103 Finally, here is the fixed probability table used to decode subblock 2104 modes in key frames. 2106 ---- Begin code block -------------------------------------- 2108 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2109 [num_intra_bmodes-1] = 2110 { 2111 { 2112 { 231, 120, 48, 89, 115, 113, 120, 152, 112}, 2113 { 152, 179, 64, 126, 170, 118, 46, 70, 95}, 2114 { 175, 69, 143, 80, 85, 82, 72, 155, 103}, 2115 { 56, 58, 10, 171, 218, 189, 17, 13, 152}, 2116 { 144, 71, 10, 38, 171, 213, 144, 34, 26}, 2117 { 114, 26, 17, 163, 44, 195, 21, 10, 173}, 2118 { 121, 24, 80, 195, 26, 62, 44, 64, 85}, 2119 { 170, 46, 55, 19, 136, 160, 33, 206, 71}, 2120 { 63, 20, 8, 114, 114, 208, 12, 9, 226}, 2121 { 81, 40, 11, 96, 182, 84, 29, 16, 36} 2122 }, 2123 { 2124 { 134, 183, 89, 137, 98, 101, 106, 165, 148}, 2125 { 72, 187, 100, 130, 157, 111, 32, 75, 80}, 2126 { 66, 102, 167, 99, 74, 62, 40, 234, 128}, 2127 { 41, 53, 9, 178, 241, 141, 26, 8, 107}, 2128 { 104, 79, 12, 27, 217, 255, 87, 17, 7}, 2129 { 74, 43, 26, 146, 73, 166, 49, 23, 157}, 2130 { 65, 38, 105, 160, 51, 52, 31, 115, 128}, 2131 { 87, 68, 71, 44, 114, 51, 15, 186, 23}, 2132 { 47, 41, 14, 110, 182, 183, 21, 17, 194}, 2133 { 66, 45, 25, 102, 197, 189, 23, 18, 22} 2134 }, 2135 { 2136 { 88, 88, 147, 150, 42, 46, 45, 196, 205}, 2137 { 43, 97, 183, 117, 85, 38, 35, 179, 61}, 2138 { 39, 53, 200, 87, 26, 21, 43, 232, 171}, 2139 { 56, 34, 51, 104, 114, 102, 29, 93, 77}, 2140 { 107, 54, 32, 26, 51, 1, 81, 43, 31}, 2141 { 39, 28, 85, 171, 58, 165, 90, 98, 64}, 2142 { 34, 22, 116, 206, 23, 34, 43, 166, 73}, 2143 { 68, 25, 106, 22, 64, 171, 36, 225, 114}, 2144 { 34, 19, 21, 102, 132, 188, 16, 76, 124}, 2145 { 62, 18, 78, 95, 85, 57, 50, 48, 51} 2146 }, 2147 { 2148 { 193, 101, 35, 159, 215, 111, 89, 46, 111}, 2149 { 60, 148, 31, 172, 219, 228, 21, 18, 111}, 2150 { 112, 113, 77, 85, 179, 255, 38, 120, 114}, 2151 { 40, 42, 1, 196, 245, 209, 10, 25, 109}, 2152 { 100, 80, 8, 43, 154, 1, 51, 26, 71}, 2153 { 88, 43, 29, 140, 166, 213, 37, 43, 154}, 2154 { 61, 63, 30, 155, 67, 45, 68, 1, 209}, 2155 { 142, 78, 78, 16, 255, 128, 34, 197, 171}, 2156 { 41, 40, 5, 102, 211, 183, 4, 1, 221}, 2157 { 51, 50, 17, 168, 209, 192, 23, 25, 82} 2158 }, 2159 { 2160 { 125, 98, 42, 88, 104, 85, 117, 175, 82}, 2161 { 95, 84, 53, 89, 128, 100, 113, 101, 45}, 2162 { 75, 79, 123, 47, 51, 128, 81, 171, 1}, 2163 { 57, 17, 5, 71, 102, 57, 53, 41, 49}, 2164 { 115, 21, 2, 10, 102, 255, 166, 23, 6}, 2165 { 38, 33, 13, 121, 57, 73, 26, 1, 85}, 2166 { 41, 10, 67, 138, 77, 110, 90, 47, 114}, 2167 { 101, 29, 16, 10, 85, 128, 101, 196, 26}, 2168 { 57, 18, 10, 102, 102, 213, 34, 20, 43}, 2169 { 117, 20, 15, 36, 163, 128, 68, 1, 26} 2170 }, 2171 { 2172 { 138, 31, 36, 171, 27, 166, 38, 44, 229}, 2173 { 67, 87, 58, 169, 82, 115, 26, 59, 179}, 2174 { 63, 59, 90, 180, 59, 166, 93, 73, 154}, 2175 { 40, 40, 21, 116, 143, 209, 34, 39, 175}, 2176 { 57, 46, 22, 24, 128, 1, 54, 17, 37}, 2177 { 47, 15, 16, 183, 34, 223, 49, 45, 183}, 2178 { 46, 17, 33, 183, 6, 98, 15, 32, 183}, 2179 { 65, 32, 73, 115, 28, 128, 23, 128, 205}, 2180 { 40, 3, 9, 115, 51, 192, 18, 6, 223}, 2181 { 87, 37, 9, 115, 59, 77, 64, 21, 47} 2182 }, 2183 { 2184 { 104, 55, 44, 218, 9, 54, 53, 130, 226}, 2185 { 64, 90, 70, 205, 40, 41, 23, 26, 57}, 2186 { 54, 57, 112, 184, 5, 41, 38, 166, 213}, 2187 { 30, 34, 26, 133, 152, 116, 10, 32, 134}, 2188 { 75, 32, 12, 51, 192, 255, 160, 43, 51}, 2189 { 39, 19, 53, 221, 26, 114, 32, 73, 255}, 2190 { 31, 9, 65, 234, 2, 15, 1, 118, 73}, 2191 { 88, 31, 35, 67, 102, 85, 55, 186, 85}, 2192 { 56, 21, 23, 111, 59, 205, 45, 37, 192}, 2193 { 55, 38, 70, 124, 73, 102, 1, 34, 98} 2194 }, 2195 { 2196 { 102, 61, 71, 37, 34, 53, 31, 243, 192}, 2197 { 69, 60, 71, 38, 73, 119, 28, 222, 37}, 2198 { 68, 45, 128, 34, 1, 47, 11, 245, 171}, 2199 { 62, 17, 19, 70, 146, 85, 55, 62, 70}, 2200 { 75, 15, 9, 9, 64, 255, 184, 119, 16}, 2201 { 37, 43, 37, 154, 100, 163, 85, 160, 1}, 2202 { 63, 9, 92, 136, 28, 64, 32, 201, 85}, 2203 { 86, 6, 28, 5, 64, 255, 25, 248, 1}, 2204 { 56, 8, 17, 132, 137, 255, 55, 116, 128}, 2205 { 58, 15, 20, 82, 135, 57, 26, 121, 40} 2206 }, 2207 { 2208 { 164, 50, 31, 137, 154, 133, 25, 35, 218}, 2209 { 51, 103, 44, 131, 131, 123, 31, 6, 158}, 2210 { 86, 40, 64, 135, 148, 224, 45, 183, 128}, 2211 { 22, 26, 17, 131, 240, 154, 14, 1, 209}, 2212 { 83, 12, 13, 54, 192, 255, 68, 47, 28}, 2213 { 45, 16, 21, 91, 64, 222, 7, 1, 197}, 2214 { 56, 21, 39, 155, 60, 138, 23, 102, 213}, 2215 { 85, 26, 85, 85, 128, 128, 32, 146, 171}, 2216 { 18, 11, 7, 63, 144, 171, 4, 4, 246}, 2217 { 35, 27, 10, 146, 174, 171, 12, 26, 128} 2218 }, 2219 { 2220 { 190, 80, 35, 99, 180, 80, 126, 54, 45}, 2221 { 85, 126, 47, 87, 176, 51, 41, 20, 32}, 2222 { 101, 75, 128, 139, 118, 146, 116, 128, 85}, 2223 { 56, 41, 15, 176, 236, 85, 37, 9, 62}, 2224 { 146, 36, 19, 30, 171, 255, 97, 27, 20}, 2225 { 71, 30, 17, 119, 118, 255, 17, 18, 138}, 2226 { 101, 38, 60, 138, 55, 70, 43, 26, 142}, 2227 { 138, 45, 61, 62, 219, 1, 81, 188, 64}, 2228 { 32, 41, 20, 117, 151, 142, 20, 21, 163}, 2229 { 112, 19, 12, 61, 195, 128, 48, 4, 24} 2230 } 2231 }; 2233 ---- End code block ---------------------------------------- 2235 12. Intraframe Prediction 2237 Intraframe prediction uses already-coded macroblocks within the 2238 current frame to approximate the contents of the current macroblock. 2239 It applies to intra-coded macroblocks in an interframe and to all 2240 macroblocks in a key frame. 2242 Relative to the current macroblock "M", the already-coded macroblocks 2243 include all macroblocks above M together with the macroblocks on the 2244 same row as, and to the left of, M, though at most four of these 2245 macroblocks are actually used: the block "A" directly above M, the 2246 blocks immediately to the left and right of A, and the block 2247 immediately to the left of M. 2249 Each of the prediction modes (i.e., means of extrapolation from 2250 already-calculated values) uses fairly simple arithmetic on pixel 2251 values whose positions, relative to the current position, are defined 2252 by the mode. 2254 The chroma (U and V) and luma (Y) predictions are independent of each 2255 other. 2257 The relative addressing of pixels applied to macroblocks on the upper 2258 row or left column of the frame will sometimes cause pixels outside 2259 the visible frame to be referenced. Usually such out-of-bounds 2260 pixels have an assumed value of 129 for pixels to the left of the 2261 leftmost column of the visible frame and 127 for pixels above the top 2262 row of the visible frame (including the special case of the pixel 2263 above and to the left of the top-left pixel in the visible frame). 2264 Exceptions to this (associated to certain modes) will be noted below. 2266 The already-coded macroblocks referenced by intra-prediction have 2267 been "reconstructed", that is, have been predicted and residue- 2268 adjusted (as described in Chapter 14), but have not been loop- 2269 filtered. While it does process the edges between individual 2270 macroblocks and individual subblocks, loop filtering (described in 2271 Chapter 15) is applied to the frame as a whole, after all of the 2272 macroblocks have been reconstructed. 2274 12.1. mb_skip_coeff 2276 The single bool flag is decoded using prob_skip_false if and only if 2277 mb_no_coeff_skip is set to 1 (see Sections 9.10 and 9.11). If 2278 mb_no_coeff_skip is set to 0 then this value defaults to 0. 2280 12.2. Chroma Prediction 2282 The chroma prediction is a little simpler than the luma prediction, 2283 so we treat it first. Each of the chroma modes treats U and V 2284 identically, that is, the U and V prediction values are calculated in 2285 parallel, using the same relative addressing and arithmetic in each 2286 of the two planes. 2288 The modes extrapolate prediction values using the 8-pixel row "A" 2289 lying immediately above the block (that is, the bottom chroma row of 2290 the macroblock immediately above the current macroblock) and the 2291 8-pixel column "L" immediately to the left of the block (that is, the 2292 rightmost chroma column of the macroblock immediately to the left of 2293 the current macroblock). 2295 Vertical prediction (chroma mode V_PRED) simply fills each 8-pixel 2296 row of the 8x8 chroma block with a copy of the "above" row (A). If 2297 the current macroblock lies on the top row of the frame, all 8 of the 2298 pixel values in A are assigned the value 127. 2300 Similarly, horizontal prediction (H_PRED) fills each 8-pixel column 2301 of the 8x8 chroma block with a copy of the "left" column (L). If the 2302 current macroblock is in the left column of the frame, all 8 pixel 2303 values in L are assigned the value 129. 2305 DC prediction (DC_PRED) fills the 8x8 chroma block with a single 2306 value. In the generic case of a macroblock lying below the top row 2307 and right of the leftmost column of the frame, this value is the 2308 average of the 16 (genuinely visible) pixels in the (union of the) 2309 above row A and left column L. 2311 Otherwise, if the current macroblock lies on the top row of the 2312 frame, the average of the 8 pixels in L is used; if it lies in the 2313 left column of the frame, the average of the 8 pixels in A is used. 2314 Note that the averages used in these exceptional cases are not the 2315 same as those that would be arrived at by using the out-of-bounds A 2316 and L values defined for V_PRED and H_PRED. In the case of the 2317 leftmost macroblock on the top row of the frame the 8x8 block is 2318 simply filled with the constant value 128. 2320 For DC_PRED, apart from the exceptional case of the top left 2321 macroblock, we are averaging either 16 or 8 pixel values to get a 2322 single prediction value that fills the 8x8 block. The rounding is 2323 done as follows: 2325 ---- Begin code block -------------------------------------- 2327 int sum; /* sum of 8 or 16 pixels at (at least) 16-bit precision */ 2328 int shf; /* base 2 logarithm of the number of pixels (3 or 4) */ 2330 Pixel DCvalue = (sum + (1 << (shf-1))) >> shf; 2332 ---- End code block ---------------------------------------- 2334 Because the summands are all valid pixels, no "clamp" is necessary in 2335 the calculation of DCvalue. 2337 The remaining "True Motion" (TM_PRED) chroma mode gets its name from 2338 an older technique of video compression used by On2 Technologies, to 2339 which it bears some relation. In addition to the row "A" and column 2340 "L", TM_PRED uses the pixel "P" above and to the left of the chroma 2341 block. 2343 The following figure gives an example of how TM_PRED works: 2345 ---- Begin code block -------------------------------------- 2347 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2348 | P | A0 | A1 | A2 | A3 | A4 | A5 | A6 | A7 | 2349 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2350 | L0 | X00 | X01 | X02 | X03 | X04 | X05 | X06 | X07 | 2351 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2352 | L1 | X10 | X11 | X12 | X13 | X14 | X15 | X16 | X17 | 2353 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2354 | L2 | X20 | X21 | X22 | X23 | X24 | X25 | X26 | X27 | 2355 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2356 | L3 | X30 | X31 | X32 | X33 | X34 | X35 | X36 | X37 | 2357 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2358 | L4 | X40 | X41 | X42 | X43 | X44 | X45 | X46 | X47 | 2359 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2360 | L5 | X50 | X51 | X52 | X53 | X54 | X55 | X56 | X57 | 2361 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2362 | L6 | X60 | X61 | X62 | X63 | X64 | X65 | X66 | X67 | 2363 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2364 | L7 | X70 | X71 | X72 | X73 | X74 | X75 | X76 | X77 | 2365 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2367 ---- End code block ---------------------------------------- 2369 Where P, As and Ls represent reconstructed pixel values from 2370 previously coded blocks, and X00 through X77 represent predicted 2371 values for the current block. TM_PRED uses the following equation to 2372 calculate X_ij: 2374 X_ij = L_i + A_j - P (i, j=0, 1, 2, 3) 2376 The exact algorithm is as follows: 2378 ---- Begin code block -------------------------------------- 2380 void TMpred( 2381 Pixel b[8][8], /* chroma (U or V) prediction block */ 2382 const Pixel A[8], /* row of already-constructed pixels 2383 above block */ 2384 const Pixel L[8], /* column of "" just to the left of 2385 block */ 2386 const Pixel P /* pixel just to the left of A and 2387 above L*/ 2388 ) { 2389 int r = 0; /* row */ 2390 do { 2391 int c = 0; /* column */ 2392 do { 2393 b[r][c] = clamp255( L[r]+ A[c] - P); 2394 } while( ++c < 8); 2395 } while( ++r < 8); 2396 } 2398 ---- End code block ---------------------------------------- 2400 Note that the process could equivalently be described as propagating 2401 the vertical differences between pixels in L (starting from P), using 2402 the pixels from A to start each column. 2404 An implementation of chroma intra-prediction may be found in the 2405 reference decoder file reconintra.c. 2407 Unlike DC_PRED, for macroblocks on the top row or left edge TM_PRED 2408 does use the out-of-bounds values of 127 and 129 (respectively) 2409 defined for V_PRED and H_PRED. 2411 12.3. Luma Prediction 2413 The prediction processes for the first four 16x16 luma modes 2414 (DC_PRED, V_PRED, H_PRED, and TM_PRED) are essentially identical to 2415 the corresponding chroma prediction processes described above, the 2416 only difference being that we are predicting a single 16x16 luma 2417 block instead of two 8x8 chroma blocks. 2419 Thus, the row "A" and column "L" here contain 16 pixels, the DC 2420 prediction is calculated using 16 or 32 pixels (and shf is 4 or 5), 2421 and we of course fill the entire prediction buffer, that is, 16 rows 2422 (or columns) containing 16 pixels each. The reference implementation 2423 of 16x16 luma prediction is also in reconintra.c. 2425 In the remaining luma mode (B_PRED), each 4x4 Y subblock is 2426 independently predicted using one of ten modes (listed, along with 2427 their encodings, in Chapter 11). 2429 Also, unlike the full-macroblock modes already described, some of the 2430 subblock modes use prediction pixels above and to the right of the 2431 current subblock. In detail, each 4x4 subblock "B" is predicted 2432 using (at most) the 4-pixel column "L" immediately to the left of B 2433 and the 8-pixel row "A" immediately above B, consisting of the 4 2434 pixels above B followed by the 4 adjacent pixels above and to the 2435 right of B, together with the single pixel "P" immediately to the 2436 left of A (and immediately above L). 2438 For the purpose of subblock intra-prediction, the pixels immediately 2439 to the left and right of a pixel in a subblock are the same as the 2440 pixels immediately to the left and right of the corresponding pixel 2441 in the frame buffer "F". Vertical offsets behave similarly: The 2442 above row A lies immediately above B in F, and the adjacent pixels in 2443 the left column L are separated by a single row in F. 2445 Because entire macroblocks (as opposed to their constituent 2446 subblocks) are reconstructed in raster-scan order, for subblocks 2447 lying along the right edge (and not along the top row) of the current 2448 macroblock, the four "extra" prediction pixels in A above and to the 2449 right of B have not yet actually been constructed. 2451 Subblocks 7, 11, and 15 are affected. All three of these subblocks 2452 use the same extra pixels as does subblock 3 (at the upper right 2453 corner of the macroblock), namely the 4 pixels immediately above and 2454 to the right of subblock 3. Writing (R,C) for a frame buffer 2455 position offset from the upper left corner of the current macroblock 2456 by R rows and C columns, the extra pixels for all the right-edge 2457 subblocks (3, 7, 11, and 15) are at positions (-1,16), (-1,17), 2458 (-1,18), and (-1,19). For the right-most macroblock in each 2459 macroblock row except the top row, the extra pixels shall use the 2460 same value as the pixel at position (-1, 15), which is the right-most 2461 visible pixel on the line immediately above the macroblock row. For 2462 the top macroblock row, all the extra pixels assume a value of 127. 2464 The details of the prediction modes are most easily described in 2465 code. 2467 ---- Begin code block -------------------------------------- 2469 /* Result pixels are often averages of two or three predictor 2470 pixels. The following subroutines are used to calculate 2471 these averages. Because the arguments are valid pixels, no 2472 clamping is necessary. An actual implementation would 2473 probably use inline functions or macros. */ 2475 /* Compute weighted average centered at y w/adjacent x, z */ 2477 Pixel avg3( Pixel x, Pixel y, Pixel z) { 2478 return (x + y + y + z + 2) >> 2;} 2480 /* Weighted average of 3 adjacent pixels centered at p */ 2482 Pixel avg3p( const Pixel *p) { return avg3( p[-1], p[0], p[1]);} 2484 /* Simple average of x and y */ 2486 Pixel avg2( Pixel x, Pixel y) { return (x + y + 1) >> 1;} 2488 /* Average of p[0] and p[1] may be considered to be a synthetic 2489 pixel lying between the two, that is, one half-step past p. */ 2491 Pixel avg2p( const Pixel *p) { return avg2( p[0], p[1]);} 2493 void subblock_intra_predict( 2494 Pixel B[4][4], /* Y subblock prediction buffer */ 2495 const Pixel *A, /* A[0]...A[7] = above row, A[-1] = P */ 2496 const Pixel *L, /* L[0]...L[3] = left column, L[-1] = P */ 2497 intra_bmode mode /* enum is in section 11.1 above */ 2498 ) { 2499 Pixel E[9]; /* 9 already-constructed edge pixels */ 2500 E[0] = L[3]; E[1] = L[2]; E[2] = L[1]; E[3] = L[0]; 2501 E[4] = A[-1]; /* == L[-1] == P */ 2502 E[5] = A[0]; E[6] = A[1]; E[7] = A[2]; E[8] = A[3]; 2504 switch( mode) { 2505 /* First four modes are similar to corresponding 2506 full-block modes. */ 2508 case B_DC_PRED: 2509 { 2510 int v = 4; /* DC sum/avg, 4 is rounding adjustment */ 2511 int i = 0; do { v += A[i] + L[i];} while( ++i < 4); 2512 v >>= 3; /* averaging 8 pixels */ 2513 i = 0; do { /* fill prediction buffer with constant DC 2514 value */ 2515 int j = 0; do { B[i][j] = v;} while( ++j < 4); 2516 } while( ++i < 4); 2517 break; 2519 } 2521 case B_TM_PRED: /* just like 16x16 TM_PRED */ 2522 { 2523 int r = 0; do { 2524 int c = 0; do { 2525 B[r][c] = clamp255( L[r] + A[c] - A[-1]); 2526 } while( ++c < 4); 2527 } while( ++r < 4); 2528 break; 2529 } 2531 case B_VE_PRED: /* like 16x16 V_PRED except using averages */ 2532 { 2533 int c = 0; do { /* all 4 rows = smoothed top row */ 2534 B[0][c] = B[1][c] = B[2][c] = B[3][c] = avg3p( A + c); 2535 } while( ++c < 4); 2536 break; 2537 } 2539 case B_HE_PRED: /* like 16x16 H_PRED except using averages */ 2540 { 2541 /* Bottom row is exceptional because L[4] does not exist */ 2542 int v = avg3( L[2], L[3], L[3]); 2543 int r = 3; while( 1) { /* all 4 columns = smoothed left 2544 column */ 2545 B[r][0] = B[r][1] = B[r][2] = B[r][3] = v; 2546 if( --r < 0) 2547 break; 2548 v = avg3p( L + r); /* upper 3 rows use average of 2549 3 pixels */ 2550 } 2551 break; 2552 } 2554 /* The remaining six "diagonal" modes subdivide the 2555 prediction buffer into diagonal lines. All the pixels 2556 on each line are assigned the same value; this value is 2557 (a smoothed or synthetic version of) an 2558 already-constructed predictor value lying on the same 2559 line. For clarity, in the comments, we express the 2560 positions of these predictor pixels relative to the 2561 upper left corner of the destination array B. 2563 These modes are unique to subblock prediction and have 2564 no full-block analogues. The first two use lines at 2565 +|- 45 degrees from horizontal (or, equivalently, 2566 vertical), that is, lines whose slopes are +|- 1. */ 2568 case B_LD_PRED: /* southwest (left and down) step = 2569 (-1, 1) or (1,-1) */ 2570 /* avg3p( A + j) is the "smoothed" pixel at (-1,j) */ 2571 B[0][0] = avg3p( A + 1); 2572 B[0][1] = B[1][0] = avg3p( A + 2); 2573 B[0][2] = B[1][1] = B[2][0] = avg3p( A + 3); 2574 B[0][3] = B[1][2] = B[2][1] = B[3][0] = avg3p( A + 4); 2575 B[1][3] = B[2][2] = B[3][1] = avg3p( A + 5); 2576 B[2][3] = B[3][2] = avg3p( A + 6); 2577 B[3][3] = avg3( A[6], A[7], A[7]); /* A[8] does not exist */ 2578 break; 2580 case B_RD_PRED: /* southeast (right and down) step = 2581 (1,1) or (-1,-1) */ 2582 B[3][0] = avg3p( E + 1); /* predictor is from (2, -1) */ 2583 B[3][1] = B[2][0] = avg3p( E + 2); /* (1, -1) */ 2584 B[3][2] = B[2][1] = B[1][0] = avg3p( E + 3); /* (0, -1) */ 2585 B[3][3] = B[2][2] = B[1][1] = B[0][0] = 2586 avg3p( E + 4); /* (-1, -1) */ 2587 B[2][3] = B[1][2] = B[0][1] = avg3p( E + 5); /* (-1, 0) */ 2588 B[1][3] = B[0][2] = avg3p( E + 6); /* (-1, 1) */ 2589 B[0][3] = avg3p( E + 7); /* (-1, 2) */ 2590 break; 2592 /* The remaining 4 diagonal modes use lines whose slopes are 2593 +|- 2 and +|- 1/2. The angles of these lines are roughly 2594 +|- 27 degrees from horizontal or vertical. 2596 Unlike the 45 degree diagonals, here we often need to 2597 "synthesize" predictor pixels midway between two actual 2598 predictors using avg2p(p), which we think of as returning 2599 the pixel "at" p[1/2]. */ 2601 case B_VR_PRED: /* SSE (vertical right) step = 2602 (2,1) or (-2,-1) */ 2603 B[3][0] = avg3p( E + 2); /* predictor is from (1, -1) */ 2604 B[2][0] = avg3p( E + 3); /* (0, -1) */ 2605 B[3][1] = B[1][0] = avg3p( E + 4); /* (-1, -1) */ 2606 B[2][1] = B[0][0] = avg2p( E + 4); /* (-1, -1/2) */ 2607 B[3][2] = B[1][1] = avg3p( E + 5); /* (-1, 0) */ 2608 B[2][2] = B[0][1] = avg2p( E + 5); /* (-1, 1/2) */ 2609 B[3][3] = B[1][2] = avg3p( E + 6); /* (-1, 1) */ 2610 B[2][3] = B[0][2] = avg2p( E + 6); /* (-1, 3/2) */ 2611 B[1][3] = avg3p( E + 7); /* (-1, 2) */ 2612 B[0][3] = avg2p( E + 7); /* (-1, 5/2) */ 2613 break; 2615 case B_VL_PRED: /* SSW (vertical left) step = 2616 (2,-1) or (-2,1) */ 2617 B[0][0] = avg2p( A); /* predictor is from (-1, 1/2) */ 2618 B[1][0] = avg3p( A + 1); /* (-1, 1) */ 2619 B[2][0] = B[0][1] = avg2p( A + 1); /* (-1, 3/2) */ 2620 B[1][1] = B[3][0] = avg3p( A + 2); /* (-1, 2) */ 2621 B[2][1] = B[0][2] = avg2p( A + 2); /* (-1, 5/2) */ 2622 B[3][1] = B[1][2] = avg3p( A + 3); /* (-1, 3) */ 2623 B[2][2] = B[0][3] = avg2p( A + 3); /* (-1, 7/2) */ 2624 B[3][2] = B[1][3] = avg3p( A + 4); /* (-1, 4) */ 2625 /* Last two values do not strictly follow the pattern. */ 2626 B[2][3] = avg3p( A + 5); /* (-1, 5) [avg2p( A + 4) = 2627 (-1,9/2)] */ 2628 B[3][3] = avg3p( A + 6); /* (-1, 6) [avg3p( A + 5) = 2629 (-1,5)] */ 2630 break; 2632 case B_HD_PRED: /* ESE (horizontal down) step = 2633 (1,2) or (-1,-2) */ 2634 B[3][0] = avg2p( E); /* predictor is from (5/2, -1) */ 2635 B[3][1] = avg3p( E + 1); /* (2, -1) */ 2636 B[2][0] = B[3][2] = svg2p( E + 1); /* ( 3/2, -1) */ 2637 B[2][1] = B[3][3] = avg3p( E + 2); /* ( 1, -1) */ 2638 B[2][2] = B[1][0] = avg2p( E + 2); /* ( 1/2, -1) */ 2639 B[2][3] = B[1][1] = avg3p( E + 3); /* ( 0, -1) */ 2640 B[1][2] = B[0][0] = avg2p( E + 3); /* (-1/2, -1) */ 2641 B[1][3] = B[0][1] = avg3p( E + 4); /* ( -1, -1) */ 2642 B[0][2] = avg3p( E + 5); /* (-1, 0) */ 2643 B[0][3] = avg3p( E + 6); /* (-1, 1) */ 2644 break; 2646 case B_HU_PRED: /* ENE (horizontal up) step = (1,-2) 2647 or (-1,2) */ 2648 B[0][0] = avg2p( L); /* predictor is from ( 1/2, -1) */ 2649 B[0][1] = avg3p( L + 1); /* ( 1, -1) */ 2650 B[0][2] = B[1][0] = avg2p( L + 1); /* (3/2, -1) */ 2651 B[0][3] = B[1][1] = avg3p( L + 2); /* ( 2, -1) */ 2652 B[1][2] = B[2][0] = avg2p( L + 2); /* (5/2, -1) */ 2653 B[1][3] = B[2][1] = avg3( L[2], L[3], L[3]); /* ( 3, -1) */ 2654 /* Not possible to follow pattern for much of the bottom 2655 row because no (nearby) already-constructed pixels lie 2656 on the diagonals in question. */ 2657 B[2][2] = B[2][3] = B[3][0] = B[3][1] = B[3][2] = B[3][3] 2658 = L[3]; 2659 } 2660 } 2662 ---- End code block ---------------------------------------- 2663 The reference decoder implementation of subblock intra-prediction may 2664 be found in reconintra4x4.c. 2666 13. DCT Coefficient Decoding 2668 The second data partition consists of an encoding of the quantized 2669 DCT (and WHT) coefficients of the residue signal. As discussed in 2670 the format overview (Chapter 2), for each macroblock, the residue is 2671 added to the (intra- or inter-generated) prediction buffer to produce 2672 the final (except for loop-filtering) reconstructed macroblock. 2674 VP8 works exclusively with 4x4 DCTs and WHTs, applied to the 24 (or 2675 25 with the Y2 subblock) 4x4 subblocks of a macroblock. The ordering 2676 of macroblocks within any of the "residue" partitions in general 2677 follows the same raster-scan as used in the first "prediction" 2678 partition. 2680 For all intra- and inter-prediction modes apart from B_PRED (intra: 2681 whose Y subblocks are independently predicted) and SPLIT_MV (inter) 2682 each macroblock's residue record begins with the Y2 component of the 2683 residue, coded using a WHT. B_PRED and SPLIT_MV coded macroblocks 2684 omit this WHT, instead specifying the 0th DCT coefficient of each of 2685 the 16 Y subblocks as part of its DCT. 2687 After the optional Y2 block, the residue record continues with 16 2688 DCTs for the Y subblocks, followed by 4 DCTs for the U subblocks, 2689 ending with 4 DCTs for the V subblocks. The subblocks occur in the 2690 usual order. 2692 The DCTs and WHT are tree-coded using a 12-element alphabet whose 2693 members we call tokens. Except for the end of block token (which 2694 sets the remaining subblock coefficients to zero and is followed by 2695 the next block), each token (sometimes augmented with data 2696 immediately following the token) specifies the value of the single 2697 coefficient at the current (implicit) position and is followed by a 2698 token applying to the next (implicit) position. 2700 For all the Y and chroma subblocks, the ordering of the coefficients 2701 follows a so-called zig-zag order. DCTs begin at coefficient 1 if Y2 2702 is present, and begin at coefficient 0 if Y2 is absent. The WHT for 2703 a Y2 subblock always begins at coefficient 0. 2705 13.1. MB Without non-Zero Coefficient Values 2707 If the flag within macroblock mode info indicates that a macroblock 2708 does not have any non-zero coefficients, the decoding process of DCT 2709 coefficients is skipped for the macroblock. 2711 13.2. Coding of Individual Coefficient Values 2713 The coding of coefficient tokens is the same for the DCT and WHT and 2714 for the remainder of this chapter DCT should be taken to mean either 2715 DCT or WHT. 2717 All tokens (except end-of-block) specify either a single unsigned 2718 value or a range of unsigned values (immediately) followed by a 2719 simple probabilistic encoding of the offset of the value from the 2720 base of that range. 2722 Non-zero values (of either type) are then followed by a flag 2723 indicating the sign of the coded value (negative if 1, positive if 2724 0). 2726 Here are the tokens and decoding tree. 2728 ---- Begin code block -------------------------------------- 2730 typedef enum 2731 { 2732 DCT_0, /* value 0 */ 2733 DCT_1, /* 1 */ 2734 DCT_2, /* 2 */ 2735 DCT_3, /* 3 */ 2736 DCT_4, /* 4 */ 2737 dct_cat1, /* range 5 - 6 (size 2) */ 2738 dct_cat2, /* 7 - 10 (4) */ 2739 dct_cat3, /* 11 - 18 (8) */ 2740 dct_cat4, /* 19 - 34 (16) */ 2741 dct_cat5, /* 35 - 66 (32) */ 2742 dct_cat6, /* 67 - 2048 (1982) */ 2743 dct_eob, /* end of block */ 2745 num_dct_tokens /* 12 */ 2746 } 2747 dct_token; 2749 const tree_index coef_tree [2 * (num_dct_tokens - 1)] = 2750 { 2751 -dct_eob, 2, /* eob = "0" */ 2752 -DCT_0, 4, /* 0 = "10" */ 2753 -DCT_1, 6, /* 1 = "110" */ 2754 8, 12, 2755 -DCT_2, 10, /* 2 = "11100" */ 2756 -DCT_3, -DCT_4, /* 3 = "111010", 4 = "111011" */ 2757 14, 16, 2758 -dct_cat1, -dct_cat2, /* cat1 = "111100", 2759 cat2 = "111101" */ 2760 18, 20, 2761 -dct_cat3, -dct_cat4, /* cat3 = "1111100", 2762 cat4 = "1111101" */ 2763 -dct_cat5, -dct_cat6 /* cat4 = "1111110", 2764 cat4 = "1111111" */ 2765 }; 2767 ---- End code block ---------------------------------------- 2769 While in general all DCT coefficients are decoded using the same 2770 tree, decoding of certain DCT coefficients may skip the first branch, 2771 whose preceding coefficient is a DCT_0. This makes use of the fact 2772 that in any block last non zero coefficient before the end of the 2773 block is not 0, therefore no dct_eob follows a DCT_0 coefficient in 2774 any block. 2776 The tokens dct_cat1 ... dct_cat6 specify ranges of unsigned values, 2777 the value within the range being formed by adding an unsigned offset 2778 (whose width is 1, 2, 3, 4, 5, or 11 bits, respectively) to the base 2779 of the range, using the following algorithm and fixed probability 2780 tables. 2782 ---- Begin code block -------------------------------------- 2784 uint DCTextra( bool_decoder *d, const Prob *p) 2785 { 2786 uint v = 0; 2787 do { v += v + read_bool( d, *p);} while( *++p); 2788 return v; 2789 } 2791 const Prob Pcat1[] = { 159, 0}; 2792 const Prob Pcat2[] = { 165, 145, 0}; 2793 const Prob Pcat3[] = { 173, 148, 140, 0}; 2794 const Prob Pcat4[] = { 176, 155, 140, 135, 0}; 2795 const Prob Pcat5[] = { 180, 157, 141, 134, 130, 0}; 2796 const Prob Pcat6[] = 2797 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0}; 2799 ---- End code block ---------------------------------------- 2801 If v, the unsigned value decoded using the coefficient tree, possibly 2802 augmented by the process above, is non-zero, its sign is set by 2803 simply reading a flag: 2805 ---- Begin code block -------------------------------------- 2807 if( read_bool( d, 128)) 2808 v = -v; 2810 ---- End code block ---------------------------------------- 2812 13.3. Token Probabilities 2814 The probability specification for the token tree (unlike that for the 2815 "extra bits" described above) is rather involved. It uses three 2816 pieces of context to index a large probability table, the contents of 2817 which may be incrementally modified in the frame header. The full 2818 (non-constant) probability table is laid out as follows. 2820 ---- Begin code block -------------------------------------- 2822 Prob coef_probs [4] [8] [3] [num_dct_tokens-1]; 2823 ---- End code block ---------------------------------------- 2825 Working from the outside in, the outermost dimension is indexed by 2826 the type of plane being decoded: 2828 o 0 - Y beginning at coefficient 1 (i.e., Y after Y2) 2830 o 1 - Y2 2832 o 2 - U or V 2834 o 3 - Y beginning at coefficient 0 (i.e., Y in the absence of Y2). 2836 The next dimension is selected by the position of the coefficient 2837 being decoded. That position c steps by ones up to 15, starting from 2838 zero for block types 1, 2, or 3 and starting from one for block type 2839 0. The second array index is then 2841 ---- Begin code block -------------------------------------- 2843 coef_bands [c] 2845 ---- End code block ---------------------------------------- 2847 where 2849 ---- Begin code block -------------------------------------- 2851 const int coef_bands [16] = { 2852 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7 2853 }; 2855 ---- End code block ---------------------------------------- 2857 is a fixed mapping of position to "band". 2859 The third dimension is the trickiest. Roughly speaking, it measures 2860 the "local complexity" or extent to which nearby coefficients are 2861 non-zero. 2863 For the first coefficient (DC, unless the block type is 0), we 2864 consider the (already encoded) blocks within the same plane (Y2, Y, 2865 U, or V) above and to the left of the current block. The context 2866 index is then the number (0, 1 or 2) of these blocks that had at 2867 least one non-zero coefficient in their residue record. 2869 Beyond the first coefficient, the context index is determined by the 2870 absolute value of the most recently decoded coefficient (necessarily 2871 within the current block) and is 0 if the last coefficient was a 2872 zero, 1 if it was plus or minus one, and 2 if its absolute value 2873 exceeded one. 2875 Note that the intuitive meaning of this measure changes as 2876 coefficients are decoded. For example, prior to the first token, a 2877 zero means that the neighbors are empty, suggesting that the current 2878 block may also be empty. After the first token, because an end-of- 2879 block token must have at least one non-zero value before it, a zero 2880 means that we just decoded a zero and hence guarantees that a non- 2881 zero coefficient will appear later in this block. However, this 2882 shift in meaning is perfectly okay because the complete context 2883 depends also on the coefficient band (and since band 0 is occupied 2884 exclusively by position 0). 2886 As with other contexts used by VP8, the "neighboring block" context 2887 described here needs a special definition for subblocks lying along 2888 the top row or left edge of the frame. These "non-existent" 2889 predictors above and to the left of the image are simply taken to be 2890 empty -- that is, taken to contain no non-zero coefficients. 2892 The residue decoding of each macroblock then requires, in each of two 2893 directions (above and to the left), an aggregate coefficient 2894 predictor consisting of a single Y2 predictor, two predictors for 2895 each of U and V, and four predictors for Y. In accordance with the 2896 scan-ordering of macroblocks, a decoder needs to maintain a single 2897 "left" aggregate predictor and a row of "above" aggregate predictors. 2899 Before decoding any residue, these maintained predictors may simply 2900 be cleared, in compliance with the definition of "non-existent" 2901 prediction. After each block is decoded, the two predictors 2902 referenced by the block are replaced with the (empty or non-empty) 2903 state of the block, in preparation for the later decoding of the 2904 blocks below and to the right of the block just decoded. 2906 The fourth, and final, dimension of the token probability array is of 2907 course indexed by (half) the position in the token tree structure, as 2908 are all tree probability arrays. 2910 The below pseudo-code illustrates the decoding process. Note that 2911 criteria, functions, etc. delimited with ** are either dependent on 2912 decoder architecture or are elaborated on elsewhere in this document. 2914 ---- Begin code block -------------------------------------- 2916 int block[16] = { 0 }; /* current 4x4 block coeffs */ 2917 int firstCoeff = 0; 2918 int plane; 2919 int ctx2; 2920 int ctx3 = 0; /* the 3rd context referred to in above description */ 2921 Prob *probTable; 2922 int token; 2923 int sign; 2924 int absValue; 2925 int extraBits; 2926 bool prevCoeffWasZero = false; 2927 bool currentBlockHasCoeffs = false; 2928 /* base coeff abs values per each category, elem #0 is 2929 DCT_VAL_CATEGORY1, * #1 is DCT_VAL_CATEGORY2 etc */ 2930 int categoryBase[6] = { 5, 7, 11, 19, 35, 67 }; 2932 /* Determine plane to use */ 2933 if( **current_block_is_Y2_block** ) plane = 0; 2934 else if ( **current_block_is_chroma** ) plane = 2; 2935 else if ( **current_macroblock_has_Y2** ) plane = 1; 2936 else plane = 3; 2938 /* For luma blocks of an "Y2 macroblock" we skip coeff index #0 */ 2939 if( plane == 1 ) 2940 firstCoeff++; 2942 /* Determine whether neighbour 4x4 blocks have coeffiecients. 2943 This is dependant of the plane we are currently decoding; 2944 i.e. we check only coefficients from same plane as current 2945 block. */ 2946 if( **left_neighbor_block_has_coefficients(plane)** ) 2947 ctx3++; 2948 if( **above_neighbor_block_has_coefficients(plane)** ) 2949 ctx3++; 2951 for( i = firstCoeff ; i < 16 ; ++i ) 2952 { 2953 ctx2 = coef_bands[i]; 2954 probTable = coef_probs[plane][ctx2][ctx3]; 2956 /* skip first code (dct_eob) if previous token was DCT_0 */ 2957 if( prevCoeffWasZero ) 2958 token = treed_read ( d, **coef_tree_without_eob**, 2959 probTable ); 2960 else 2961 token = treed_read ( d, coef_tree, probTable ); 2963 if( token == dct_eob ) 2964 break; 2966 if( token != DCT_0 ) 2967 { 2968 currentBlockHasCoeffs = true; 2969 if( **token_has_extra_bits(token)** ) 2970 { 2971 extraBits = DCTextra( token ); 2972 absValue = 2973 categoryBase[**token_to_cat_index(token)**] + 2974 extraBits; 2975 } 2976 else 2977 { 2978 absValue = **token_to_abs_value(token)**; 2979 } 2981 sign = read_bool(d, 128); 2982 block[i] = sign ? -absValue : absValue; 2983 } 2984 else 2985 { 2986 absValue = 0; 2987 } 2989 /* Set contexts and stuff for next coeff */ 2990 if( absValue == 0 ) ctx3 = 0; 2991 else if ( absValue == 1 ) ctx3 = 1; 2992 else ctx3 = 2; 2993 prevCoeffWasZero = true; 2994 } 2996 /* Store current block status to decoder internals */ 2997 **block_has_coefficients[currentMb][currentBlock]** = 2998 currentBlockHasCoeffs; 3000 ---- End code block ---------------------------------------- 3002 While we have in fact completely described the coefficient decoding 3003 procedure, the reader will probably find it helpful to consult the 3004 reference implementation, which can be found in the file 3005 detokenize.c. 3007 13.4. Token Probability Updates 3009 As mentioned above, the token-decoding probabilities may change from 3010 frame to frame. After detection of a key frame, they are of course 3011 set to their defaults shown in Section 13.5; this must occur before 3012 decoding the remainder of the header, as both key frames and 3013 interframes may adjust these probabilities. 3015 The layout and semantics of the coefficient probability update record 3016 (Section I of the frame header) are straightforward. For each 3017 position in the coef_probs array there occurs a fixed-probability 3018 bool indicating whether or not the corresponding probability should 3019 be updated. If the bool is true, there follows a P(8) replacing that 3020 probability. Note that updates are cumulative, that is, a 3021 probability updated on one frame is in effect for all ensuing frames 3022 until the next key frame, or until the probability is explicitly 3023 updated by another frame. 3025 The algorithm to effect the foregoing is simple: 3027 ---- Begin code block -------------------------------------- 3029 int i = 0; do { 3030 int j = 0; do { 3031 int k = 0; do { 3032 int t = 0; do { 3034 if( read_bool( d, coef_update_probs [i] [j] [k] [t])) 3035 coef_probs [i] [j] [k] [t] = read_literal( d, 8); 3037 } while( ++t < num_dct_tokens - 1); 3038 } while( ++k < 3); 3039 } while( ++j < 8); 3040 } while( ++i < 4); 3042 ---- End code block ---------------------------------------- 3044 The (constant) update probabilities are as follows (they may also be 3045 found in the reference decoder file coef_update_probs.c). 3047 ---- Begin code block -------------------------------------- 3049 const Prob coef_update_probs [4] [8] [3] [num_dct_tokens-1] = 3050 { 3051 { 3052 { 3053 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3054 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3055 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3056 }, 3057 { 3058 { 176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3059 { 223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3060 { 249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255} 3061 }, 3062 { 3063 { 255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3064 { 234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3065 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3066 }, 3067 { 3068 { 255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3069 { 239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3070 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3071 }, 3072 { 3073 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3074 { 251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3075 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3076 }, 3077 { 3078 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3079 { 251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3080 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3081 }, 3082 { 3083 { 255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255}, 3084 { 250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255}, 3085 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3086 }, 3087 { 3088 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3089 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3090 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3091 } 3092 }, 3093 { 3094 { 3095 { 217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3096 { 225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255}, 3097 { 234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255} 3098 }, 3099 { 3100 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3101 { 223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3102 { 238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3103 }, 3104 { 3105 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3106 { 249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3107 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3108 }, 3109 { 3110 { 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3111 { 247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3112 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3113 }, 3114 { 3115 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3116 { 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3117 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3118 }, 3119 { 3120 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3121 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3122 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3123 }, 3124 { 3125 { 255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3126 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3127 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3128 }, 3129 { 3130 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3131 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3132 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3133 } 3134 }, 3135 { 3136 { 3137 { 186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255}, 3138 { 234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255}, 3139 { 251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255} 3140 }, 3141 { 3142 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3143 { 236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3144 { 251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255} 3145 }, 3146 { 3147 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3148 { 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3149 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3150 }, 3151 { 3152 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3153 { 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3154 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3155 }, 3156 { 3157 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3158 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3159 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3160 }, 3161 { 3162 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3163 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3164 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3165 }, 3166 { 3167 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3168 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3169 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3170 }, 3171 { 3172 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3173 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3174 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3175 } 3176 }, 3177 { 3178 { 3179 { 248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3180 { 250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255}, 3181 { 248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255} 3182 }, 3183 { 3184 { 255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3185 { 246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3186 { 252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255} 3187 }, 3188 { 3189 { 255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3190 { 248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3191 { 253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3192 }, 3193 { 3194 { 255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3195 { 245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3196 { 253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3197 }, 3198 { 3199 { 255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3200 { 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3201 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3202 }, 3203 { 3204 { 255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3205 { 249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3206 { 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3208 }, 3209 { 3210 { 255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3211 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3212 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3213 }, 3214 { 3215 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3216 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3217 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3218 } 3219 } 3220 }; 3222 ---- End code block ---------------------------------------- 3224 13.5. Default Token Probability Table 3226 The default token probabilities are as follows. 3228 ---- Begin code block -------------------------------------- 3230 const Prob default_coef_probs [4] [8] [3] [num_dct_tokens - 1] = 3231 { 3232 { 3233 { 3234 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3235 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3236 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3237 }, 3238 { 3239 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 3240 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 3241 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 3242 }, 3243 { 3244 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 3245 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 3246 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 3247 }, 3248 { 3249 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 3250 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 3251 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 3252 }, 3253 { 3254 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 3255 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 3256 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 3257 }, 3258 { 3259 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 3260 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 3261 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 3262 }, 3263 { 3264 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 3265 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 3266 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 3267 }, 3268 { 3269 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3270 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3271 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3272 } 3273 }, 3274 { 3275 { 3276 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 3277 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 3278 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 3279 }, 3280 { 3281 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 3282 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 3283 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 3284 }, 3285 { 3286 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 3287 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 3288 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 3289 }, 3290 { 3291 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 3292 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 3293 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 3294 }, 3295 { 3296 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 3297 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 3298 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 3299 }, 3300 { 3301 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 3302 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 3303 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 3305 }, 3306 { 3307 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 3308 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 3309 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 3310 }, 3311 { 3312 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 3313 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3314 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 3315 } 3316 }, 3317 { 3318 { 3319 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 3320 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 3321 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 3322 }, 3323 { 3324 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 3325 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 3326 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 3327 }, 3328 { 3329 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 3330 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 3331 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 3332 }, 3333 { 3334 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 3335 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 3336 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 3337 }, 3338 { 3339 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3340 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 3341 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3342 }, 3343 { 3344 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3345 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3346 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3347 }, 3348 { 3349 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 3350 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 3351 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3352 }, 3353 { 3354 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3355 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3356 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3357 } 3358 }, 3359 { 3360 { 3361 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 3362 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 3363 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 3364 }, 3365 { 3366 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 3367 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 3368 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 3369 }, 3370 { 3371 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 3372 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 3373 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 3374 }, 3375 { 3376 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 3377 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 3378 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 3379 }, 3380 { 3381 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 3382 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 3383 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 3384 }, 3385 { 3386 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 3387 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 3388 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 3389 }, 3390 { 3391 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 3392 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 3393 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 3394 }, 3395 { 3396 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3397 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3398 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3399 } 3400 } 3402 }; 3404 ---- End code block ---------------------------------------- 3406 14. DCT and WHT Inversion and Macroblock Reconstruction 3408 14.1. Dequantization 3410 After decoding the DCTs/WHTs as described above, each (quantized) 3411 coefficient in each subblock is multiplied by one of six 3412 dequantization factors, the choice of factor depending on the plane 3413 (Y2, Y, or chroma) and position (DC = coefficient zero, AC = any 3414 other coefficient). If the current macroblock has overridden the 3415 quantization level (as described in Chapter 10) then the six factors 3416 are looked up from two dequantization tables with appropriate scaling 3417 and clamping using the single index supplied by the override. 3418 Otherwise, the frame-level dequantization factors (as described in 3419 Section 9.6) are used. In either case, the multiplies are computed 3420 and stored using 16-bit signed integers. 3422 The two dequantization tables, which may also be found in the 3423 reference decoder file quant_common.c, are as follows. 3425 ---- Begin code block -------------------------------------- 3427 static const int dc_qlookup[QINDEX_RANGE] = 3428 { 3429 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 3430 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 3431 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 3432 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 3433 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 3434 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 3435 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 3436 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 3437 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, 126, 128, 130, 3438 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, 3439 }; 3441 static const int ac_qlookup[QINDEX_RANGE] = 3442 { 3443 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 3444 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3445 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 3446 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 3447 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 3448 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 3449 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 3450 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 3451 181, 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, 3452 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, 3453 }; 3455 ---- End code block ---------------------------------------- 3457 Lookup values from the above two tables are directly used the DC and 3458 AC coefficients in Y1 respectively. For Y2 and chroma, values from 3459 above tables undergo either a scaling process or clamping processing 3460 before the multiplies. Details to these scaling and clamping can be 3461 found related lookup functions in quant_common.c. 3463 14.2. Inverse Transforms 3465 If the Y2 residue block exists (i.e., the macroblock luma mode is not 3466 SPLITMV or B_PRED), it is inverted first (using the inverse WHT) and 3467 the element of the result at row i, column j is used as the 0th 3468 coefficient of the Y subblock at position (i, j), that is, the Y 3469 subblock whose index is (i * 4) + j. As discussed in Chapter 13, if 3470 the luma mode is B_PRED or SPLITMV, the 0th Y coefficients are part 3471 of the residue signal for the subblocks themselves. 3473 In either case, the inverse transforms for the sixteen Y subblocks 3474 and eight chroma subblocks are computed next. All 24 of these 3475 inversions are independent of each other; their results may (at least 3476 conceptually) be stored in 24 separate 4x4 arrays. 3478 As is done by the reference decoder, an implementation may wish to 3479 represent the prediction and residue buffers as macroblock-sized 3480 arrays (that is, a 16x16 Y buffer and two 8x8 chroma buffers). 3481 Regarding the inverse DCT implementation given below, this requires a 3482 simple adjustment to the address calculation for the resulting 3483 residue pixels. 3485 14.3. Implementation of the WHT Inversion 3487 As previously discussed (see Chapters 2 and 13), for macroblocks 3488 encoded using prediction modes other than B_PRED and SPLITMV, the DC 3489 values derived from the DCT transform on the 16 Y blocks are 3490 collected to construct a 25th block of a macroblock (16 Y, 4 U, 4 V 3491 constitute the 24 blocks). This 25th block is transformed using a 3492 Walsh-Hadamard transform (WHT). 3494 The inputs to the inverse WHT (that is, the dequantized 3495 coefficients), the intermediate "horizontally detransformed" signal, 3496 and the completely detransformed residue signal are all stored as 3497 arrays of 16-bit signed integers. 3499 Following the tradition of specifying bitstream format using the 3500 decoding process, we specify the inverse WHT in the decoding process 3501 using the following C style source code: 3503 ---- Begin code block -------------------------------------- 3505 void vp8_short_inv_walsh4x4_c(short *input, short *output) 3506 { 3507 int i; 3508 int a1, b1, c1, d1; 3509 int a2, b2, c2, d2; 3510 short *ip = input; 3511 short *op = output; 3512 int temp1, temp2; 3514 for(i=0;i<4;i++) 3515 { 3516 a1 = ip[0] + ip[12]; 3517 b1 = ip[4] + ip[8]; 3518 c1 = ip[4] - ip[8]; 3519 d1 = ip[0] - ip[12]; 3520 op[0] = a1 + b1; 3521 op[4] = c1 + d1; 3522 op[8] = a1 - b1; 3523 op[12]= d1 - c1; 3524 ip++; 3525 op++; 3526 } 3527 ip = output; 3528 op = output; 3529 for(i=0;i<4;i++) 3530 { 3531 a1 = ip[0] + ip[3]; 3532 b1 = ip[1] + ip[2]; 3533 c1 = ip[1] - ip[2]; 3534 d1 = ip[0] - ip[3]; 3536 a2 = a1 + b1; 3537 b2 = c1 + d1; 3538 c2 = a1 - b1; 3539 d2 = d1 - c1; 3541 op[0] = (a2+3)>>3; 3542 op[1] = (b2+3)>>3; 3543 op[2] = (c2+3)>>3; 3544 op[3] = (d2+3)>>3; 3546 ip+=4; 3547 op+=4; 3548 } 3549 } 3551 ---- End code block ---------------------------------------- 3553 In the case that there is only one non-zero DC value in input, the 3554 inverse transform can be simplified to the following: 3556 ---- Begin code block -------------------------------------- 3558 void vp8_short_inv_walsh4x4_1_c(short *input, short *output) 3559 { 3560 int i; 3561 int a1; 3562 short *op=output; 3564 a1 = ((input[0] + 3)>>3); 3566 for(i=0;i<4;i++) 3567 { 3568 op[0] = a1; 3569 op[1] = a1; 3570 op[2] = a1; 3571 op[3] = a1; 3572 op+=4; 3573 } 3574 } 3576 ---- End code block ---------------------------------------- 3578 It should be noted, a conforming decoder should implement the inverse 3579 transform using exactly the same rounding to achieve bit-wise 3580 matching output to the output of the process specified by the above 3581 "C" source code. 3583 The reference decoder WHT inversion may be found in the files 3584 invtrans.c and idctllm.c. 3586 14.4. Implementation of the DCT Inversion 3588 All of the DCT inversions are computed in exactly the same way. In 3589 principle, VP8 uses a classical 2D inverse discrete cosine transform, 3590 implemented as two passes of 1-D inverse DCT. The 1-D inverse DCT 3591 was calculated using a similar algorithm to what was described in 3592 [Loeffler]. However, the paper only provided the 8-point and 16- 3593 point version of the algorithms, which was adapted by On2 to perform 3594 the 4-point 1-D DCT. 3596 Accurate calculation of 1-D DCT of the above algorithm requires 3597 infinite precision. VP8 of course can use only a finite-precision 3598 approximation. Also, the inverse DCT used by VP8 takes care of 3599 normalization of the standard unitary transform, that is, every 3600 dequantized coefficient has roughly double the size of the 3601 corresponding unitary coefficient. However, at all but the highest 3602 datarates, the discrepancy between transmitted and ideal coefficients 3603 is due almost entirely to (lossy) compression and not to errors 3604 induced by finite-precision arithmetic. 3606 The inputs to the inverse DCT (that is, the dequantized 3607 coefficients), the intermediate "horizontally detransformed" signal, 3608 and the completely detransformed residue signal are all stored as 3609 arrays of 16-bit signed integers. The details of the computation are 3610 as follows. 3612 It should also be noted that this implementation makes use of 16-bit 3613 fixed point version of two multiplication constants: 3615 sqrt(2) * cos (pi/8) 3617 sqrt(2) * sin (pi/8) 3619 Because the first constant is bigger than 1, to maintain the same 16- 3620 bit fixed point precision as the second one, we make use of the fact 3621 that 3623 x * a = x + x*(a-1) 3625 therefore 3627 x * sqrt(2) * cos (pi/8) = x + x * ( sqrt(2) * cos(pi/8)-1) 3629 ---- Begin code block -------------------------------------- 3631 /* IDCT implementation */ 3632 static const int cospi8sqrt2minus1=20091; 3633 static const int sinpi8sqrt2 =35468; 3634 void short_idct4x4llm_c(short *input, short *output, int pitch) 3635 { 3636 int i; 3637 int a1, b1, c1, d1; 3639 short *ip=input; 3640 short *op=output; 3641 int temp1, temp2; 3642 int shortpitch = pitch>>1; 3644 for(i=0;i<4;i++) 3645 { 3646 a1 = ip[0]+ip[8]; 3647 b1 = ip[0]-ip[8]; 3649 temp1 = (ip[4] * sinpi8sqrt2)>>16; 3650 temp2 = ip[12]+((ip[12] * cospi8sqrt2minus1)>>16); 3651 c1 = temp1 - temp2; 3653 temp1 = ip[4] + ((ip[4] * cospi8sqrt2minus1)>>16); 3654 temp2 = (ip[12] * sinpi8sqrt2)>>16; 3655 d1 = temp1 + temp2; 3657 op[shortpitch*0] = a1+d1; 3658 op[shortpitch*3] = a1-d1; 3659 op[shortpitch*1] = b1+c1; 3660 op[shortpitch*2] = b1-c1; 3662 ip++; 3663 op++; 3664 } 3665 ip = output; 3666 op = output; 3667 for(i=0;i<4;i++) 3668 { 3669 a1 = ip[0]+ip[2]; 3670 b1 = ip[0]-ip[2]; 3672 temp1 = (ip[1] * sinpi8sqrt2)>>16; 3673 temp2 = ip[3]+((ip[3] * cospi8sqrt2minus1)>>16); 3674 c1 = temp1 - temp2; 3676 temp1 = ip[1] + ((ip[1] * cospi8sqrt2minus1)>>16); 3677 temp2 = (ip[3] * sinpi8sqrt2)>>16; 3678 d1 = temp1 + temp2; 3680 op[0] = (a1+d1+4)>>3; 3681 op[3] = (a1-d1+4)>>3; 3682 op[1] = (b1+c1+4)>>3; 3683 op[2] = (b1-c1+4)>>3; 3685 ip+=shortpitch; 3686 op+=shortpitch; 3687 } 3688 } 3690 ---- End code block ---------------------------------------- 3692 The reference decoder DCT inversion may be found in the files 3693 invtrans.c and idctllm.c. 3695 14.5. Summation of Predictor and Residue 3697 Finally, the prediction and residue signals are summed to form the 3698 reconstructed macroblock, which, except for loop filtering (taken up 3699 next), completes the decoding process. 3701 The summing procedure is fairly straightforward, having only a couple 3702 of details. The prediction and residue buffers are both arrays of 3703 16-bit signed integers. Each individual (Y, U, and V pixel) result 3704 is calculated first as a 32-bit sum of the prediction and residue, 3705 and is then saturated to 8-bit unsigned range (using, say, the 3706 clamp255 function defined above) before being stored as an 8-bit 3707 unsigned pixel value. 3709 VP8 also supports a mode where the encoding of a bitstream guarantees 3710 all reconstructed pixel values between 0 and 255, compliant 3711 bitstreams of such requirements have the clamp_type bit in the frame 3712 header set to 1. In such case, the clamp255 is no longer required. 3714 The summation process is the same, regardless of the (intra or inter) 3715 mode of prediction in effect for the macroblock. The reference 3716 decoder implementation of reconstruction may be found in the file 3717 recon.c. 3719 15. Loop Filter 3721 Loop filtering is the last stage of frame reconstruction and the 3722 next-to-last stage of the decoding process. The loop filter is 3723 applied to the entire frame after the summation of predictor and 3724 residue described in Chapter 14. 3726 The purpose of the loop filter is to eliminate (or at least reduce) 3727 visually objectionable artifacts associated with the semi- 3728 independence of the coding of macroblocks and their constituent 3729 subblocks. 3731 As was discussed in Chapter 5, the loop filter is "integral" to 3732 decoding, in that the results of loop filtering are used in the 3733 prediction of subsequent frames. Consequently, a functional decoder 3734 implementation must perform loop filtering exactly as described here. 3735 This is in distinction to any postprocessing that may be applied only 3736 to the image immediately before display; such postprocessing is 3737 entirely at the option of the implementor (and/or user) and has no 3738 effect on decoding per se. 3740 The baseline frame level parameters controlling the loop filter are 3741 defined in the frame header (Chapter 9.4) along with a mechanism for 3742 adjustment based on a macroblock's prediction mode and/or reference 3743 frame. The first is a flag selecting the type of filter (normal or 3744 simple), the other two are numbers (loop_filter_level and 3745 sharpness_level) that adjust the strength or sensitivity of the 3746 filter. As described in Chapters 9.3 and 10, loop_filter_level may 3747 be also overridden on a per-macroblock basis using segmentation. 3749 Loop filtering is one of the more computationally-intensive aspects 3750 of VP8 decoding. This is the reason for the existence of the 3751 optional less-demanding simple filter type. Also, the loop filter is 3752 completely disabled if the loop_filter_level in the frame header is 3753 zero; macroblock-level overrides are ignored in this case. (It is of 3754 course possible for a compressor to encode a frame in which only a 3755 few macroblocks are loop filtered: The global loop_filter_level must 3756 be non-zero and each macroblock can select one of four levels, most 3757 of which could be zero.) 3759 To facilitate efficient implementation, the VP8 decoding algorithms 3760 generally, and the loop filter especially, were designed with SIMD 3761 ("Single Instruction Multiple Datum" or "integer vector") processors 3762 in mind. The reference decoder implementation of loop filtering 3763 (found in loopfilter.c) is, in effect, a portable SIMD specification 3764 of the loop filtering algorithms intended to simplify a realization 3765 on an actual SIMD processor. 3767 Unfortunately, the approach taken there does not lead to maximal 3768 efficency (restricted to the C language, that is) and, as far as a 3769 pure algorithm specification is concerned, is in places obscure. For 3770 example, various aspects of filtering are conditioned on absolute 3771 differences lying below certain thresholds. An ordinary C 3772 implementation would simply discriminate amongst these behaviors 3773 using if statements. The reference decoder instead effects this by 3774 "masking arithmetic", that is, using "and" operations to 3775 (conditionally) zero-out values to be added or subtracted to pixels. 3776 Furthermore, the structure holding the various threshold values is 3777 artificially parallelized. While this mimics closely the approach 3778 taken in vector-processor machine language, it is not how one usually 3779 programs in C. 3781 In this document, we take a different approach and present the 3782 algorithms in a more straightforward, idiomatic, and terse C style. 3783 Together with the reference version, we hope to provide the "best of 3784 both worlds", that is, a pure algorithm specification here and a 3785 strong suggestion as to an optimal actual implementation in 3786 loopfilter.c. 3788 We begin by discussing the aspects of loop filtering that are 3789 independent of the controlling parameters and type of filter chosen. 3791 15.1. Filter Geometry and Overall Procedure 3793 The Y, U, and V planes are processed independently and, except for 3794 the values of certain control parameters (derived from the 3795 loop_filter_level and sharpness_level), identically. 3797 The loop filter acts on the edges between adjacent macroblocks and on 3798 the edges between adjacent subblocks of a macroblock. All such edges 3799 are horizontal or vertical. For each pixel position on an edge, a 3800 small number (two or three) of pixels adjacent to either side of the 3801 position are examined and possibly modified. The displacements of 3802 these pixels are at a right angle to the edge orientation, that is, 3803 for a horizontal edge, we treat the pixels immediately above and 3804 below the edge position, for a vertical edge, we treat the pixels 3805 immediately to the left and right of the edge. 3807 We call this collection of pixels associated to an edge position a 3808 segment; the length of a segment is 2, 4, 6, or 8. Excepting that 3809 the normal filter uses slightly different algorithms for, and that 3810 either filter may apply different control parameters to, the edges 3811 between macroblocks and those between subblocks, the treatment of 3812 edges is quite uniform: All segments straddling an edge are treated 3813 identically, there is no distinction between the treatment of 3814 horizontal and vertical edges, whether between macroblocks or between 3815 subblocks. 3817 As a consequence, adjacent subblock edges within a macroblock may be 3818 concatenated and processed in their entirety. There is a single 3819 8-pixel long vertical edge horizontally centered in each of the U and 3820 V blocks (the concatenation of upper and lower 4-pixel edges between 3821 chroma subblocks), and three 16-pixel long vertical edges at 3822 horizontal positions 1/4, 1/2, and 3/4 the width of the luma 3823 macroblock, each representing the concatenation of four 4-pixel sub- 3824 edges between pairs of Y subblocks. 3826 The macroblocks comprising the frame are processed in the usual 3827 raster-scan order. Each macroblock is "responsible for" the inter- 3828 macroblock edges immediately above and left of it (but not the edges 3829 below and right of it), as well as the edges between its subblocks. 3831 For each macroblock M, there are four filtering steps, which are, 3832 (almost) in order: 3834 1. If M is not on the leftmost column of macroblocks, filter across 3835 the left (vertical) inter-macroblock edge of M. 3837 2. Filter across the vertical subblock edges within M. 3839 3. If M is not on the topmost row of macroblocks, filter across the 3840 top (horizontal) inter-macroblock edge of M. 3842 4. Filter across the horizontal subblock edges within M. 3844 We write MY, MU, and MV for the planar constituents of M, that is, 3845 the 16x16 luma block, 8x8 U block, and 8x8 V block comprising M. 3847 In step 1, for each of the three blocks MY, MU, and MV, we filter 3848 each of the (16 luma or 8 chroma) segments straddling the column 3849 separating the block from the block immediately to the left of it, 3850 using the inter-macroblock filter and controls associated to the 3851 loop_filter_level and sharpness_level. 3853 In step 4, we filter across the (three luma and one each for U and V) 3854 vertical subblock edges described above, this time using the inter- 3855 subblock filter and controls. 3857 Step 2 and 4 are skipped for macroblocks that satisfy both of the 3858 following two conditions: 3860 1. Macroblock coding mode is neither B_PRED nor SPLTMV; and 3861 2. There is no DCT coefficient coded for the whole macroblock. 3863 For these macroblocks, loop filtering for edges between subblocks 3864 internal to a macroblock is effectively skipped. This skip strategy 3865 significantly reduces VP8 loop-filtering complexity. 3867 Edges between macroblocks and those between subblocks are treated 3868 with different control parameters (and, in the case of the normal 3869 filter, with different algorithms); luma and chroma edges are also 3870 treated with different control parameters. Except for pixel 3871 addressing, there is no distinction between the treatment of vertical 3872 and horizontal edges. Luma edges are always 16 pixels long, chroma 3873 edges are always 8 pixels long, and the segments straddling an edge 3874 are treated identically; this of course facilitates vector 3875 processing. 3877 Because many pixels belong to segments straddling two or more edges, 3878 and so will be filtered more than once, the order in which edges are 3879 processed given above must be respected by any implementation. 3880 Within a single edge, however, the segments straddling that edge are 3881 disjoint and the order in which these segments are processed is 3882 immaterial. 3884 Before taking up the filtering algorithms themselves, we should 3885 emphasize a point already made: Even though the pixel segments 3886 associated to a macroblock are antecedent to the macroblock (that is, 3887 lie within the macroblock or in already-constructed macroblocks), a 3888 macroblock must not be filtered immediately after its 3889 "reconstruction" (described in Chapter 14). Rather, the loop filter 3890 applies after all the macroblocks have been "reconstructed" (i.e., 3891 had their predictor summed with their residue); correct decoding is 3892 predicated on the fact that already-constructed portions of the 3893 current frame referenced via intra-prediction (described in Chapter 3894 12) are not yet filtered. 3896 15.2. Simple Filter 3898 Having described the overall procedure of, and pixels affected by, 3899 the loop filter, we turn our attention to the treatment of individual 3900 segments straddling edges. We begin by describing the simple filter, 3901 which, as the reader might guess, is somewhat simpler than the normal 3902 filter. 3904 Note that the simple filter only applies to luma edges. Chroma edges 3905 are left unfiltered. 3907 Roughly speaking, the idea of loop filtering is, within limits, to 3908 reduce the difference between pixels straddling an edge. Differences 3909 in excess of a threshold (associated to the loop_filter_level) are 3910 assumed to be "natural" and are unmodified; differences below the 3911 threshold are assumed to be artifacts of quantization and the 3912 (partially) separate coding of blocks, and are reduced via the 3913 procedures described below. While the loop_filter_level is in 3914 principle arbitrary, the levels chosen by a VP8 compressor tend to be 3915 correlated to quantization levels. 3917 Most of the filtering arithmetic is done using 8-bit signed operands 3918 (having a range -128 to +127, inclusive), supplemented by 16-bit 3919 temporaries holding results of multiplies. 3921 Sums and other temporaries need to be "clamped" to a valid signed 3922 8-bit range: 3924 ---- Begin code block -------------------------------------- 3926 int8 c( int v) 3927 { 3928 return (int8) (v < -128 ? -128 : (v < 128 ? v : 127)); 3929 } 3931 ---- End code block ---------------------------------------- 3933 Since pixel values themselves are unsigned 8-bit numbers, we need to 3934 convert between signed and unsigned values: 3936 ---- Begin code block -------------------------------------- 3938 /* Convert pixel value (0 <= v <= 255) to an 8-bit signed 3939 number. */ 3940 int8 u2s( Pixel v) { return (int8) (v - 128);} 3942 /* Clamp, then convert signed number back to pixel value. */ 3943 Pixel s2u( int v) { return (Pixel) ( c(v) + 128);} 3945 ---- End code block ---------------------------------------- 3947 Filtering is often predicated on absolute-value thresholds. The 3948 following function is the equivalent of the standard library function 3949 abs, whose prototype is found in the standard header file stdlib.h. 3950 For us, the argument v is always the difference between two pixels 3951 and lies in the range -255 <= v <= +255. 3953 ---- Begin code block -------------------------------------- 3955 int abs( int v) { return v < 0? -v : v;} 3956 ---- End code block ---------------------------------------- 3958 An actual implementation would of course use inline functions or 3959 macros to accomplish these trivial procedures (which are used by both 3960 the normal and simple loop filters). An optimal implementation would 3961 probably express them in machine language, perhaps using SIMD vector 3962 instructions. On many SIMD processors, the saturation accomplished 3963 by the above clamping function is often folded into the arithmetic 3964 instructions themselves, obviating the explicit step taken here. 3966 To simplify the specification of relative pixel positions, we use the 3967 word before to mean "immediately above" (for a vertical segment 3968 straddling a horizontal edge) or "immediately to the left of" (for a 3969 horizontal segment straddling a vertical edge) and the word after to 3970 mean "immediately below" or "immediately to the right of". 3972 Given an edge, a segment, and a limit value, the simple loop filter 3973 computes a value based on the four pixels that straddle the edge (two 3974 either side). If that value is below a supplied limit, then, very 3975 roughly speaking, the two pixel values are brought closer to each 3976 other, "shaving off" something like a quarter of the difference. The 3977 same procedure is used for all segments straddling any type of edge, 3978 regardless of the nature (inter-macroblock, inter-subblock, luma, or 3979 chroma) of the edge; only the limit value depends on the edge-type. 3981 The exact procedure (for a single segment) is as follows; the 3982 subroutine common_adjust is used by both the simple filter presented 3983 here and the normal filters discussed in Section 15.3. 3985 ---- Begin code block -------------------------------------- 3987 int8 common_adjust( 3988 int use_outer_taps, /* filter is 2 or 4 taps wide */ 3989 const Pixel *P1, /* pixel before P0 */ 3990 Pixel *P0, /* pixel before edge */ 3991 Pixel *Q0, /* pixel after edge */ 3992 const Pixel *Q1 /* pixel after Q0 */ 3993 ) { 3994 cint8 p1 = u2s( *P1); /* retrieve and convert all 4 pixels */ 3995 cint8 p0 = u2s( *P0); 3996 cint8 q0 = u2s( *Q0); 3997 cint8 q1 = u2s( *Q1); 3999 /* Disregarding clamping, when "use_outer_taps" is false, 4000 "a" is 3*(q0-p0). Since we are about to divide "a" by 4001 8, in this case we end up multiplying the edge 4002 difference by 5/8. 4004 When "use_outer_taps" is true (as for the simple filter), 4005 "a" is p1 - 3*p0 + 3*q0 - q1, which can be thought of as 4006 a refinement of 2*(q0 - p0) and the adjustment is 4007 something like (q0 - p0)/4. */ 4009 int8 a = c( ( use_outer_taps? c(p1 - q1) : 0 ) + 3*(q0 - p0) ); 4011 /* b is used to balance the rounding of a/8 in the case where 4012 the "fractional" part "f" of a/8 is exactly 1/2. */ 4014 cint8 b = (c(a + 3)) >> 3; 4016 /* Divide a by 8, rounding up when f >= 1/2. 4017 Although not strictly part of the "C" language, 4018 the right-shift is assumed to propagate the sign bit. */ 4020 a = c( a + 4) >> 3; 4022 /* Subtract "a" from q0, "bringing it closer" to p0. */ 4024 *Q0 = s2u( q0 - a); 4026 /* Add "a" (with adjustment "b") to p0, "bringing it closer" 4027 to q0. 4029 The clamp of "a+b", while present in the reference decoder, 4030 is superfluous; we have -16 <= a <= 15 at this point. */ 4032 *P0 = s2u( p0 + b); 4034 return a; 4035 } 4037 ---- End code block ---------------------------------------- 4038 ---- Begin code block -------------------------------------- 4040 void simple_segment( 4041 uint8 edge_limit, /* do nothing if edge difference 4042 exceeds limit */ 4043 const Pixel *P1, /* pixel before P0 */ 4044 Pixel *P0, /* pixel before edge */ 4045 Pixel *Q0, /* pixel after edge */ 4046 const Pixel *Q1 /* pixel after Q0 */ 4047 ) { 4048 if( (abs(*P0 - *Q0)*2 + abs(*P1-*Q1)/2) <= edge_limit)) 4049 common_adjust( 1, P1, P0, Q0, Q1); /* use outer taps */ 4050 } 4052 ---- End code block ---------------------------------------- 4054 We make a couple of remarks about the rounding procedure above. When 4055 b is zero (that is, when the "fractional part" of a is not 1/2 ), we 4056 are (except for clamping) adding the same number to p0 as we are 4057 subtracting from q0. This preserves the average value of p0 and q0 4058 but the resulting difference between p0 and q0 is always even; in 4059 particular, the smallest non-zero gradation +-1 is not possible here. 4061 When b is one, the value we add to p0 (again except for clamping) is 4062 one less than the value we are subtracting from q0. In this case, 4063 the resulting difference is always odd (and the small gradation +-1 4064 is possible) but the average value is reduced by 1/2, yielding, for 4065 instance, a very slight darkening in the luma plane. (In the very 4066 unlikely event of appreciable darkening after a large number of 4067 interframes, a compressor would of course eventually compensate for 4068 this in the selection of predictor and/or residue.) 4070 The derivation of the edge_limit value used above, which depends on 4071 the loop_filter_level and sharpness_level, as well as the type of 4072 edge being processed, will be taken up after we describe the normal 4073 loop filtering algorithm below. 4075 15.3. Normal Filter 4077 The normal loop filter is a refinement of the simple loop filter; all 4078 of the general discussion above applies here as well. In particular, 4079 the functions c, u2s, s2u, abs, and common_adjust are used by both 4080 the normal and simple filters. 4082 As mentioned above, the normal algorithms for inter-macroblock and 4083 inter-subblock edges differ. Nonetheless, they have a great deal in 4084 common: They use similar threshold algorithms to disable the filter 4085 and to detect high internal edge variance (which influences the 4086 filtering algorithm). Both algorithms also use, at least 4087 conditionally, the simple filter adjustment procedure described 4088 above. 4090 The common thresholding algorithms are as follows. 4092 ---- Begin code block -------------------------------------- 4094 /* All functions take (among other things) a segment (of length 4095 at most 4 + 4 = 8) symmetrically straddling an edge. 4097 The pixel values (or pointers) are always given in order, 4098 from the "beforemost" to the "aftermost". So, for a 4099 horizontal edge (written "|"), an 8-pixel segment would be 4100 ordered p3 p2 p1 p0 | q0 q1 q2 q3. */ 4102 /* Filtering is disabled if the difference between any two 4103 adjacent "interior" pixels in the 8-pixel segment exceeds 4104 the relevant threshold (I). A more complex thresholding 4105 calculation is done for the group of four pixels that 4106 straddle the edge, in line with the calculation in 4107 simple_segment() above. */ 4109 int filter_yes( 4110 uint8 I, /* limit on interior differences */ 4111 uint8 E, /* limit at the edge */ 4113 cint8 p3, cint8 p2, cint8 p1, cint8 p0, /* pixels before 4114 edge */ 4115 cint8 q0, cint8 q1, cint8 q2, cint8 q3 /* pixels after 4116 edge */ 4117 ) { 4118 return (abs(p0 - q0)*2 + abs(p1-q1)/2) <= E 4119 && abs(p3 - p2) <= I && abs(p2 - p1) <= I && 4120 abs(p1 - p0) <= I 4121 && abs(q3 - q2) <= I && abs(q2 - q1) <= I && 4122 abs(q1 - q0) <= I; 4123 } 4125 ---- End code block ---------------------------------------- 4126 ---- Begin code block -------------------------------------- 4128 /* Filtering is altered if (at least) one of the differences 4129 on either side of the edge exceeds a threshold (we have 4130 "high edge variance"). */ 4132 int hev( 4133 uint8 threshold, 4134 cint8 p1, cint8 p0, /* pixels before edge */ 4135 cint8 q0, cint8 q1 /* pixels after edge */ 4136 ) { 4137 return abs(p1 - p0) > threshold || abs(q1 - q0) > threshold; 4138 } 4140 ---- End code block ---------------------------------------- 4142 The subblock filter is a variant of the simple filter. In fact, if 4143 we have high edge variance, the adjustment is exactly as for the 4144 simple filter. Otherwise, the simple adjustment (without outer taps) 4145 is applied and the two pixels one step in from the edge pixels are 4146 adjusted by roughly half the amount by which the two edge pixels are 4147 adjusted; since the edge adjustment here is essentially 3/8 the edge 4148 difference, the inner adjustment is approximately 3/16 the edge 4149 difference. 4151 ---- Begin code block -------------------------------------- 4153 void subblock_filter( 4154 uint8 hev_threshold, /* detect high edge variance */ 4155 uint8 interior_limit, /* possibly disable filter */ 4156 uint8 edge_limit, 4157 cint8 *P3, cint8 *P2, int8 *P1, int8 *P0, /* pixels before 4158 edge */ 4159 int8 *Q0, int8 *Q1, cint8 *Q2, cint8 *Q3 /* pixels after 4160 edge */ 4161 ) { 4162 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4163 p0 = u2s(*P0); 4164 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4165 q3 = u2s(*Q3); 4167 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4168 p0, p1, p2, p3)) 4169 { 4170 const int hv = hev( hev_threshold, p1, p0, q0, q1); 4172 cint8 a = ( common_adjust( hv, P1, P0, Q0, Q1) + 1) >> 1; 4174 if( !hv) { 4175 *Q1 = s2u( q1 - a); 4176 *P1 = s2u( p1 + a); 4177 } 4178 } 4179 } 4181 ---- End code block ---------------------------------------- 4183 The inter-macroblock filter has potentially wider scope. If the edge 4184 variance is high, it performs the simple adjustment (using the outer 4185 taps, just like the simple filter and the corresponding case of the 4186 normal subblock filter). If the edge variance is low, we begin with 4187 the same basic filter calculation and apply multiples of it to pixel 4188 pairs symmetric about the edge; the magnitude of adjustment decays as 4189 we move away from the edge and six of the pixels in the segment are 4190 affected. 4192 ---- Begin code block -------------------------------------- 4194 void MBfilter( 4195 uint8 hev_threshold, /* detect high edge variance */ 4196 uint8 interior_limit, /* possibly disable filter */ 4197 uint8 edge_limit, 4198 cint8 *P3, int8 *P2, int8 *P1, int8 *P0, /* pixels before 4199 edge */ 4200 int8 *Q0, int8 *Q1, int8 *Q2, cint8 *Q3 /* pixels after 4201 edge */ 4202 ) { 4203 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4204 p0 = u2s(*P0); 4205 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4206 q3 = u2s(*Q3); 4208 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4209 p0, p1, p2, p3)) 4210 { 4211 if( !hev( hev_threshold, p1, p0, q0, q1)) 4212 { 4213 /* Same as the initial calculation in "common_adjust", 4214 w is something like twice the edge difference */ 4216 const int8 w = c( c(p1 - q1) + 3*(q0 - p0) ); 4218 /* 9/64 is approximately 9/63 = 1/7 and 1<<7 = 128 = 4219 2*64. So this a, used to adjust the pixels adjacent 4220 to the edge, is something like 3/7 the edge 4221 difference. */ 4223 int8 a = c( (27*w + 63) >> 7); 4225 *Q0 = s2u( q0 - a); *P0 = s2u( p0 + a); 4227 /* Next two are adjusted by 2/7 the edge difference */ 4229 a = c( (18*w + 63) >> 7); 4231 *Q1 = s2u( q1 - a); *P1 = s2u( p1 + a); 4233 /* Last two are adjusted by 1/7 the edge difference */ 4235 a = c( (9*w + 63) >> 7); 4237 *Q2 = s2u( q2 - a); *P2 = s2u( p2 + a); 4239 } else /* if hev, do simple filter */ 4240 common_adjust( 1, P1, P0, Q0, Q1); /* using outer 4241 taps */ 4242 } 4244 } 4246 ---- End code block ---------------------------------------- 4248 15.4. Calculation of Control Parameters 4250 We conclude the discussion of loop filtering by showing how the 4251 thresholds supplied to the procedures above are derived from the two 4252 control parameters sharpness_level (an unsigned 3-bit number having 4253 maximum value 7) and loop_filter_level (an unsigned 6-bit number 4254 having maximum value 63). 4256 While the sharpness_level is constant over the frame, individual 4257 macroblocks may override the loop_filter_level with one of four 4258 possibilities supplied in the frame header (as described in Chapter 4259 10). 4261 Both the simple and normal filters disable filtering if a value 4262 derived from the four pixels that straddle the edge (2 either side) 4263 exceeds a threshold / limit value. 4265 ---- Begin code block -------------------------------------- 4267 /* Luma and Chroma use the same inter-macroblock edge limit */ 4268 uint8 mbedge_limit = ((loop_filter_level + 2) * 2) + 4269 interior_limit; 4271 /* Luma and Chroma use the same inter-subblock edge limit */ 4272 uint8 sub_bedge_limit = (loop_filter_level * 2) + interior_limit; 4274 ---- End code block ---------------------------------------- 4276 The remaining thresholds are used only by the normal filters. The 4277 filter-disabling interior difference limit is the same for all edges 4278 (luma, chroma, inter-subblock, inter-macroblock) and is given by the 4279 following. 4281 ---- Begin code block -------------------------------------- 4283 uint8 interior_limit = loop_filter_level; 4285 if( sharpness_level) 4286 { 4287 interior_limit >>= sharpness_level > 4 ? 2 : 1; 4288 if( interior_limit > 9 - sharpness_level) 4289 interior_limit = 9 - sharpness_level; 4290 } 4291 if( !interior_limit) 4292 interior_limit = 1; 4294 ---- End code block ---------------------------------------- 4295 Finally, we give the derivation of the high edge-variance threshold, 4296 which is also the same for all edge types. 4298 ---- Begin code block -------------------------------------- 4300 uint8 hev_threshold = 0; 4302 if( we_are_decoding_akey_frame) /* current frame is a key frame */ 4303 { 4304 if( loop_filter_level >= 40) 4305 hev_threshold = 2; 4306 else if( loop_filter_level >= 15) 4307 hev_threshold = 1; 4308 } 4309 else /* current frame is an interframe */ 4310 { 4311 if( loop_filter_level >= 40) 4312 hev_threshold = 3; 4313 else if( loop_filter_level >= 20) 4314 hev_threshold = 2; 4315 else if( loop_filter_level >= 15) 4316 hev_threshold = 1; 4317 } 4319 ---- End code block ---------------------------------------- 4321 16. Interframe Macroblock Prediction Records 4323 We describe the layout and semantics of the prediction records for 4324 macroblocks in an interframe. 4326 After the feature specification (which is described in Chapter 10 and 4327 is identical for intraframes and interframes), there comes a Bool( 4328 prob_intra), which indicates inter-prediction (i.e., prediction from 4329 prior frames) when true and intra-prediction (i.e., prediction from 4330 already-coded portions of the current frame) when false. The zero- 4331 probability prob_intra is set by field J of the frame header. 4333 16.1. Intra-Predicted Macroblocks 4335 For intra-prediction, the layout of the prediction data is 4336 essentially the same as the layout for key frames, although the 4337 contexts used by the decoding process are slightly different. 4339 As discussed in Chapter 8, the "outer" Y mode here uses a different 4340 tree from that used in key frames, repeated here for convenience. 4342 ---- Begin code block -------------------------------------- 4344 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 4345 { 4346 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 4347 4, 6, /* "1" subtree has 2 descendant subtrees */ 4348 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 4349 H_PRED = "101" */ 4350 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 4351 B_PRED = "111" */ 4352 }; 4354 ---- End code block ---------------------------------------- 4356 The probability table used to decode this tree is variable. As 4357 described in Section 9, it (along with the similarly-treated UV 4358 table) can be updated by field J of the frame header. Similar to the 4359 coefficient-decoding probabilities, such updates are cumulative and 4360 affect all ensuing frames until the next key frame or explicit 4361 update. The default probabilities for the Y and UV tables are 4363 ---- Begin code block -------------------------------------- 4365 Prob ymode_prob [num_ymodes - 1] = { 112, 86, 140, 37}; 4366 Prob uv_mode_prob [num_uv_modes - 1] = { 162, 101, 204}; 4368 ---- End code block ---------------------------------------- 4369 These defaults must be restored after detection of a key frame. 4371 Just as for key frames, if the Y mode is B_PRED, there next comes an 4372 encoding of the intra_bpred mode used by each of the sixteen Y 4373 subblocks. These encodings use the same tree as does that for key 4374 frames but, in place of the contexts used in key frames, use the 4375 single fixed probability table 4377 ---- Begin code block -------------------------------------- 4379 const Prob bmode_prob [num_intra_bmodes - 1] = { 4380 120, 90, 79, 133, 87, 85, 80, 111, 151 4381 }; 4383 ---- End code block ---------------------------------------- 4385 Last comes the chroma mode, again coded using the same tree as that 4386 for key frames, this time using the dynamic uv_mode_prob table 4387 described above. 4389 The calculation of the intra-prediction buffer is identical to that 4390 described for key frames in Chapter 12. 4392 16.2. Inter-Predicted Macroblocks 4394 Otherwise (when the above bool is true), we are using inter- 4395 prediction (which of course only happens for interframes), to which 4396 we now restrict our attention. 4398 The next datum is then another bool, B( prob_last), selecting the 4399 reference frame. If 0, the reference frame is previous frame (last 4400 frame); if 1, another bool (prob_gf) selects the reference frame 4401 between golden frame (0) or altref frame (1). The probabilities 4402 prob_last and prob_gf are set in field J of the frame header. 4404 Together with setting the reference frame, the purpose of inter-mode 4405 decoding is to set a motion vector for each of the sixteen Y 4406 subblocks of the current macroblock. This then defines the 4407 calculation of the inter-prediction buffer (detailed in Chapter 18). 4408 While the net effect of inter-mode decoding is straightforward, the 4409 implementation is somewhat complex; the (lossless) compression 4410 achieved by this method justifies the complexity. 4412 After the reference frame selector comes the mode (or motion vector 4413 reference) applied to the macroblock as a whole, coded using the 4414 following enumeration and tree. Setting mv_nearest = num_ymodes is a 4415 convenience that allows a single variable to unambiguously hold an 4416 inter- or intraprediction mode. 4418 ---- Begin code block -------------------------------------- 4420 typedef enum 4421 { 4422 mv_nearest = num_ymodes, /* use "nearest" motion vector 4423 for entire MB */ 4424 mv_near, /* use "next nearest" "" */ 4425 mv_zero, /* use zero "" */ 4426 mv_new, /* use explicit offset from 4427 implicit "" */ 4428 mv_split, /* use multiple motion vectors */ 4430 num_mv_refs = mv_split + 1 - mv_nearest 4431 } 4432 mv_ref; 4434 const tree_index mv_ref_tree [2 * (num_mv_refs - 1)] = 4435 { 4436 -mv_zero, 2, /* zero = "0" */ 4437 -mv_nearest, 4, /* nearest = "10" */ 4438 -mv_near, 6, /* near = "110" */ 4439 -mv_new, -mv_split /* new = "1110", split = "1111" */ 4440 }; 4442 ---- End code block ---------------------------------------- 4444 16.3. Mode and Motion Vector Contexts 4446 The probability table used to decode the mv_ref, along with three 4447 reference motion vectors used by the selected mode, is calculated via 4448 a survey of the already-decoded motion vectors in (up to) 3 nearby 4449 macroblocks. 4451 The algorithm generates a sorted list of distinct motion vectors 4452 adjacent to the search site. The best_mv is the vector with the 4453 highest score. The nearest_mv is the non-zero vector with the 4454 highest score. The near_mv is the non-zero vector with the next 4455 highest score. The number of motion vectors coded using the SPLITMV 4456 mode is scored using the same weighting and is returned with the 4457 scores of the best, nearest, and near vectors. 4459 The three adjacent macroblocks above, left, and above-left are 4460 considered in order. If the macroblock is intra-coded, no action is 4461 taken. Otherwise, the motion vector is compared to other previously 4462 found motion vectors to determine if it has been seen before, and if 4463 so contributes its weight to that vector, otherwise enters a new 4464 vector in the list. The above and left vectors have twice the weight 4465 of the above-left vector. 4467 As is the case with many contexts used by VP8, it is possible for 4468 macroblocks near the top or left edges of the image to reference 4469 blocks that are outside the visible image. VP8 provides a border of 4470 1 macroblock filled with 0x0 motion vectors left of the left edge, 4471 and a border filled with 0,0 motion vectors of 1 macroblocks above 4472 the top edge. 4474 Much of the process is more easily described in C than in English. 4475 The reference code for this can be found in findnearmv.c. The 4476 calculation of reference vectors, probability table, and, finally, 4477 the inter-prediction mode itself is implemented as follows. 4479 ---- Begin code block -------------------------------------- 4481 typedef union 4482 { 4483 unsigned int as_int; 4484 MV as_mv; 4485 } int_mv; /* facilitates rapid equality tests */ 4487 static void mv_bias(MODE_INFO *x,int refframe, int_mv *mvp, 4488 int * ref_frame_sign_bias ) 4489 { 4490 MV xmv; 4491 xmv = x->mbmi.mv.as_mv; 4492 if ( ref_frame_sign_bias[x->mbmi.ref_frame] != 4493 ref_frame_sign_bias[refframe] ) 4494 { 4495 xmv.row*=-1; 4496 xmv.col*=-1; 4497 } 4498 mvp->as_mv = xmv; 4499 } 4501 ---- End code block ---------------------------------------- 4502 ---- Begin code block -------------------------------------- 4504 void vp8_clamp_mv(MV *mv, const MACROBLOCKD *xd) 4505 { 4506 if ( mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN) ) 4507 mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN; 4508 else if ( mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN ) 4509 mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN; 4511 if ( mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN) ) 4512 mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN; 4513 else if ( mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN ) 4514 mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN; 4515 } 4517 ---- End code block ---------------------------------------- 4519 In the function vp8_find_near_mvs(), the vectors "nearest" and "near" 4520 are used by the corresponding modes. 4522 The vector best_mv is used as a base for explicitly-coded motion 4523 vectors. 4525 The first three entries in the return value cnt are (in order) 4526 weighted census values for "zero", "nearest", and "near" vectors. 4527 The final value indicates the extent to which SPLIT_MV was used by 4528 the neighboring macroblocks. The largest possible "weight" value in 4529 each case is 5. 4531 ---- Begin code block -------------------------------------- 4533 void vp8_find_near_mvs 4534 ( 4535 MACROBLOCKD *xd, 4536 const MODE_INFO *here, 4537 MV *nearest, 4538 MV *near, 4539 MV *best_mv, 4540 int cnt[4], 4541 int refframe, 4542 int * ref_frame_sign_bias 4543 ) 4544 { 4545 const MODE_INFO *above = here - xd->mode_info_stride; 4546 const MODE_INFO *left = here - 1; 4547 const MODE_INFO *aboveleft = above - 1; 4548 int_mv near_mvs[4]; 4549 int_mv *mv = near_mvs; 4550 int *cntx = cnt; 4551 enum {CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV}; 4553 /* Zero accumulators */ 4554 mv[0].as_int = mv[1].as_int = mv[2].as_int = 0; 4555 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 4557 /* Process above */ 4558 if(above->mbmi.ref_frame != INTRA_FRAME) { 4559 if(above->mbmi.mv.as_int) { 4560 (++mv)->as_int = above->mbmi.mv.as_int; 4561 mv_bias(above, refframe, mv, ref_frame_sign_bias); 4562 ++cntx; 4563 } 4564 *cntx += 2; 4565 } 4567 /* Process left */ 4568 if(left->mbmi.ref_frame != INTRA_FRAME) { 4569 if(left->mbmi.mv.as_int) { 4570 int_mv this_mv; 4572 this_mv.as_int = left->mbmi.mv.as_int; 4573 mv_bias(left, refframe, &this_mv, ref_frame_sign_bias); 4575 if(this_mv.as_int != mv->as_int) { 4576 (++mv)->as_int = this_mv.as_int; 4577 ++cntx; 4578 } 4579 *cntx += 2; 4580 } else 4581 cnt[CNT_ZERO] += 2; 4582 } 4584 /* Process above left */ 4585 if(aboveleft->mbmi.ref_frame != INTRA_FRAME) { 4586 if(aboveleft->mbmi.mv.as_int) { 4587 int_mv this_mv; 4589 this_mv.as_int = aboveleft->mbmi.mv.as_int; 4590 mv_bias(aboveleft, refframe, &this_mv, 4591 ref_frame_sign_bias); 4593 if(this_mv.as_int != mv->as_int) { 4594 (++mv)->as_int = this_mv.as_int; 4595 ++cntx; 4596 } 4597 *cntx += 1; 4599 } else 4600 cnt[CNT_ZERO] += 1; 4601 } 4603 /* If we have three distinct MV's ... */ 4604 if(cnt[CNT_SPLITMV]) { 4605 /* See if above-left MV can be merged with NEAREST */ 4606 if(mv->as_int == near_mvs[CNT_NEAREST].as_int) 4607 cnt[CNT_NEAREST] += 1; 4608 } 4610 cnt[CNT_SPLITMV] = ((above->mbmi.mode == SPLITMV) 4611 + (left->mbmi.mode == SPLITMV)) * 2 4612 + (aboveleft->mbmi.mode == SPLITMV); 4614 /* Swap near and nearest if necessary */ 4615 if(cnt[CNT_NEAR] > cnt[CNT_NEAREST]) { 4616 int tmp; 4617 tmp = cnt[CNT_NEAREST]; 4618 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 4619 cnt[CNT_NEAR] = tmp; 4620 tmp = near_mvs[CNT_NEAREST].as_int; 4621 near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int; 4622 near_mvs[CNT_NEAR].as_int = tmp; 4623 } 4625 /* Use near_mvs[0] to store the "best" MV */ 4626 if(cnt[CNT_NEAREST] >= cnt[CNT_ZERO]) 4627 near_mvs[CNT_ZERO] = near_mvs[CNT_NEAREST]; 4629 /* Set up return values */ 4630 *best_mv = near_mvs[0].as_mv; 4631 *nearest = near_mvs[CNT_NEAREST].as_mv; 4632 *near = near_mvs[CNT_NEAR].as_mv; 4634 vp8_clamp_mv(nearest, xd); 4635 vp8_clamp_mv(near, xd); 4636 vp8_clamp_mv(best_mv, xd); //TODO: move this up before 4637 the copy 4638 } 4640 ---- End code block ---------------------------------------- 4642 The mv_ref probability table (mv_ref_p) is then derived from the 4643 census as follows. 4645 ---- Begin code block -------------------------------------- 4647 const int vp8_mode_contexts[6][4] = 4648 { 4649 { 7, 1, 1, 143, }, 4650 { 14, 18, 14, 107, }, 4651 { 135, 64, 57, 68, }, 4652 { 60, 56, 128, 65, }, 4653 { 159, 134, 128, 34, }, 4654 { 234, 188, 128, 28, }, 4655 } 4657 ---- End code block ---------------------------------------- 4659 ---- Begin code block -------------------------------------- 4661 vp8_prob *vp8_mv_ref_probs(vp8_prob mv_ref_p[VP8_MVREFS-1], 4662 int cnt[4]) 4663 { 4664 mv_ref_p[0] = vp8_mode_contexts [cnt[0]] [0]; 4665 mv_ref_p[1] = vp8_mode_contexts [cnt[1]] [1]; 4666 mv_ref_p[2] = vp8_mode_contexts [cnt[2]] [2]; 4667 mv_ref_p[3] = vp8_mode_contexts [cnt[3]] [3]; 4668 return p; 4669 } 4671 ---- End code block ---------------------------------------- 4673 Once mv_ref_p is established, the mv_ref is decoded as usual. 4675 ---- Begin code block -------------------------------------- 4677 mvr = (mv_ref) treed_read( d, mv_ref_tree, mv_ref_p); 4679 ---- End code block ---------------------------------------- 4681 For the first four inter-coding modes, the same motion vector is used 4682 for all the Y subblocks. The first three modes use an implicit 4683 motion vector. 4685 +------------+------------------------------------------------------+ 4686 | Mode | Instruction | 4687 +------------+------------------------------------------------------+ 4688 | mv_nearest | Use the nearest vector returned by | 4689 | | vp8_find_near_mvs. | 4690 | | | 4691 | mv_near | Use the near vector returned by vp8_find_near_mvs. | 4692 | | | 4693 | mv_zero | Use a zero vector, that is, predict the current | 4694 | | macroblock from the corresponding macroblock in the | 4695 | | prediction frame. | 4696 | | | 4697 | NEWMV | This mode is followed by an explicitly-coded motion | 4698 | | vector (the format of which is described in the next | 4699 | | chapter) that is added (component-wise) to the | 4700 | | best_mv reference vector returned by find_near_mvs | 4701 | | and applied to all 16 subblocks. | 4702 +------------+------------------------------------------------------+ 4704 16.4. Split Prediction 4706 The remaining mode (SPLITMV) causes multiple vectors to be applied to 4707 the Y subblocks. It is immediately followed by a partition 4708 specification that determines how many vectors will be specified and 4709 how they will be assigned to the subblocks. The possible partitions, 4710 with indicated subdivisions and coding tree, are as follows. 4712 ---- Begin code block -------------------------------------- 4714 typedef enum 4715 { 4716 mv_top_bottom, /* two pieces {0...7} and {8...15} */ 4717 mv_left_right, /* {0,1,4,5,8,9,12,13} and 4718 {2,3,6,7,10,11,14,15} */ 4719 mv_quarters, /* {0,1,4,5}, {2,3,6,7}, {8,9,12,13}, 4720 {10,11,14,15} */ 4721 MV_16, /* every subblock gets its own vector 4722 {0} ... {15} */ 4724 mv_num_partitions 4725 } 4726 MVpartition; 4728 const tree_index mvpartition_tree [2 * (mvnum_partition - 1)] = 4729 { 4730 -MV_16, 2, /* MV_16 = "0" */ 4731 -mv_quarters, 4, /* mv_quarters = "10" */ 4732 -mv_top_bottom, -mv_left_right /* top_bottom = "110", 4733 left_right = "111" */ 4734 }; 4736 ---- End code block ---------------------------------------- 4738 The partition is decoded using a fixed, constant probability table: 4740 ---- Begin code block -------------------------------------- 4742 const Prob mvpartition_probs [mvnum_partition - 1] = 4743 { 110, 111, 150}; 4744 part = (MVpartition) treed_read( d, mvpartition_tree, 4745 mvpartition_probs); 4747 ---- End code block ---------------------------------------- 4749 After the partition come two (for mv_top_bottom or mv_left_right), 4750 four (for mv_quarters), or sixteen (for MV_16) subblock inter- 4751 prediction modes. These modes occur in the order indicated by the 4752 partition layouts (given as comments to the MVpartition enum) and are 4753 coded as follows. (As was done for the macroblock-level modes, we 4754 offset the mode enumeration so that a single variable may 4755 unambiguously hold either an intra- or inter-subblock mode.) 4757 Prior to decoding each subblock, a decoding tree context is chosen as 4758 illustrated in the code snippet below. The context is based on the 4759 immediate left and above subblock neighbors, and whether they are 4760 equal, are zero, or a combination of those. 4762 ---- Begin code block -------------------------------------- 4764 typedef enum 4765 { 4766 LEFT4x4 = num_intra_bmodes, /* use already-coded MV to 4767 my left */ 4768 ABOVE4x4, /* use already-coded MV above me */ 4769 ZERO4x4, /* use zero MV */ 4770 NEW4x4, /* explicit offset from "best" */ 4772 num_sub_mv_ref 4773 }; 4774 sub_mv_ref; 4776 const tree_index sub_mv_ref_tree [2 * (num_sub_mv_ref - 1)] = 4777 { 4778 -LEFT4X4, 2, /* LEFT = "0" */ 4779 -ABOVE4X4, 4, /* ABOVE = "10" */ 4780 -ZERO4X4, -NEW4X4 /* ZERO = "110", NEW = "111" */ 4781 }; 4783 /* Choose correct decoding tree context 4784 * Function parameters are left subblock neighbor MV and above 4785 * subblock neighbor MV */ 4786 int vp8_mvCont(MV *l, MV*a) 4787 { 4788 int lez = (l->row == 0 && l->col == 0); /* left neighbour 4789 is zero */ 4790 int aez = (a->row == 0 && a->col == 0); /* above neighbour 4791 is zero */ 4792 int lea = (l->row == a->row && l->col == a->col); /* left 4793 neighbour equals above neighbour */ 4795 if(lea && lez) 4796 return SUBMVREF_LEFT_ABOVE_ZED; /* =4 */ 4798 if(lea) 4799 return SUBMVREF_LEFT_ABOVE_SAME; /* =3 */ 4801 if(aez) 4802 return SUBMVREF_ABOVE_ZED; /* =2 */ 4804 if(lez) 4805 return SUBMVREF_LEFT_ZED; /* =1*/ 4807 return SUBMVREF_NORMAL; /* =0 */ 4809 } 4811 /* Constant probabilities and decoding procedure. */ 4813 const Prob sub_mv_ref_prob [5][num_sub_mv_ref - 1] = { 4814 { 147,136,18 }, 4815 { 106,145,1 }, 4816 { 179,121,1 }, 4817 { 223,1 ,34 }, 4818 { 208,1 ,1 } }; 4820 sub_ref = (sub_mv_ref) treed_read( d, sub_mv_ref_tree, 4821 sub_mv_ref_prob[context]); 4823 ---- End code block ---------------------------------------- 4825 The first two sub-prediction modes simply copy the already-coded 4826 motion vectors used by the blocks above and to-the-left of the 4827 subblock at the upper left corner of the current subset (i.e., 4828 collection of subblocks being predicted). These prediction blocks 4829 need not lie in the current macroblock and, if the current subset 4830 lies at the top or left edges of the frame, need not lie in the 4831 frame. In this latter case, their motion vectors are taken to be 4832 zero, as are subblock motion vectors within an intra-predicted 4833 macroblock. Also, to ensure the correctness of prediction within 4834 this macroblock, all subblocks lying in an already-decoded subset of 4835 the current macroblock must have their motion vectors set. 4837 ZERO4x4 uses a zero motion vector and predicts the current subset 4838 using the corresponding subset from the prediction frame. 4840 NEW4x4 is exactly like NEWMV except applied only to the current 4841 subset. It is followed by a 2-dimensional motion vector offset 4842 (described in the next chapter) that is added to the best vector 4843 returned by the earlier call to find_near_mvs to form the motion 4844 vector in effect for the subset. 4846 Parsing of both inter-prediction modes and motion vectors (described 4847 next) can be found in the reference decoder file decodemv.c. 4849 17. Motion Vector Decoding 4851 As discussed above, motion vectors appear in two places in the VP8 4852 datastream: applied to whole macroblocks in NEWMV mode and applied to 4853 subsets of macroblocks in NEW4x4 mode. The format of the vectors is 4854 identical in both cases. 4856 Each vector has two pieces: A vertical component (row) followed by a 4857 horizontal component (column). The row and column use separate 4858 coding probabilities but are otherwise represented identically. 4860 17.1. Coding of Each Component 4862 Each component is a signed integer V representing a vertical or 4863 horizontal luma displacement of V quarter-pixels (and a chroma 4864 displacement of V eighth-pixels). The absolute value of V, if non- 4865 zero, is followed by a boolean sign. V may take any value between 4866 -1023 and +1023, inclusive. 4868 The absolute value A is coded in one of two different ways according 4869 to its size. For 0 <= A <= 7, A is tree-coded, and for 8 <= A <= 4870 1023, the bits in the binary expansion of A are coded using 4871 independent boolean probabilities. The coding of A begins with a 4872 bool specifying which range is in effect. 4874 Decoding a motion vector component then requires a 19-position 4875 probability table, whose offsets, along with the procedure used to 4876 decode components, are as follows: 4878 ---- Begin code block -------------------------------------- 4880 typedef enum 4881 { 4882 mvpis_short, /* short (<= 7) vs long (>= 8) */ 4883 MVPsign, /* sign for non-zero */ 4884 MVPshort, /* 8 short values = 7-position tree */ 4886 MVPbits = MVPshort + 7, /* 8 long value bits 4887 w/independent probs */ 4889 MVPcount = MVPbits + 10 /* 19 probabilities in total */ 4890 } 4891 MVPindices; 4893 typedef Prob MV_CONTEXT [MVPcount]; /* Decoding spec for 4894 a single component */ 4896 /* Tree used for small absolute values (has expected 4897 correspondence). */ 4899 const tree_index small_mvtree [2 * (8 - 1)] = 4900 { 4901 2, 8, /* "0" subtree, "1" subtree */ 4902 4, 6, /* "00" subtree", "01" subtree */ 4903 -0, -1, /* 0 = "000", 1 = "001" */ 4904 -2, -3, /* 2 = "010", 3 = "011" */ 4905 10, 12, /* "10" subtree, "11" subtree */ 4906 -4, -5, /* 4 = "100", 5 = "101" */ 4907 -6, -7 /* 6 = "110", 7 = "111" */ 4908 }; 4910 /* Read MV component at current decoder position, using 4911 supplied probs. */ 4913 int read_mvcomponent( bool_decoder *d, const MV_CONTEXT *mvc) 4914 { 4915 const Prob * const p = (const Prob *) mvc; 4916 int A = 0; 4918 if( read_bool( d, p [mvpis_short])) /* 8 <= A <= 1023 */ 4919 { 4920 /* Read bits 0, 1, 2 */ 4922 int i = 0; 4923 do { A += read_bool( d, p [MVPbits + i]) << i;} 4924 while( ++i < 3); 4926 /* Read bits 9, 8, 7, 6, 5, 4 */ 4928 i = 9; 4929 do { A += read_bool( d, p [MVPbits + i]) << i;} 4930 while( --i > 3); 4932 /* We know that A >= 8 because it is coded long, 4933 so if A <= 15, bit 3 is one and is not 4934 explicitly coded. */ 4936 if( !(A & 0xfff0) || read_bool( d, p [MVPbits + 3])) 4937 A += 8; 4938 } 4939 else /* 0 <= A <= 7 */ 4940 A = treed_read( d, small_mvtree, p + MVPshort); 4942 return A && read_bool( r, p [MVPsign]) ? -A : A; 4943 } 4944 ---- End code block ---------------------------------------- 4946 17.2. Probability Updates 4948 The decoder should maintain an array of two MV_CONTEXTs for decoding 4949 row and column components, respectively. These MV_CONTEXTs should be 4950 set to their defaults every key frame. Each individual probability 4951 may be updated every interframe (by field J of the frame header) 4952 using a constant table of update probabilities. Each optional update 4953 is of the form B? P(7), that is, a bool followed by a 7-bit 4954 probability specification if true. 4956 As with other dynamic probabilities used by VP8, the updates remain 4957 in effect until the next key frame or until replaced via another 4958 update. 4960 In detail, the probabilities should then be managed as follows. 4962 ---- Begin code block -------------------------------------- 4964 /* Never-changing table of update probabilities for each 4965 individual probability used in decoding motion vectors. */ 4967 const MV_CONTEXT vp8_mv_update_probs[2] = 4968 { 4969 { 4970 237, 4971 246, 4972 253, 253, 254, 254, 254, 254, 254, 4973 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 4974 }, 4975 { 4976 231, 4977 243, 4978 245, 253, 254, 254, 254, 254, 254, 4979 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 4980 } 4981 }; 4983 /* Default MV decoding probabilities. */ 4985 const MV_CONTEXT default_mv_context[2] = 4986 { 4987 { // row 4988 162, // is short 4989 128, // sign 4990 225, 146, 172, 147, 214, 39, 156, // short tree 4991 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 // long bits 4993 }, 4995 { // same for column 4996 164, // is short 4997 128, 4998 204, 170, 119, 235, 140, 230, 228, 4999 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 // long bits 5001 } 5002 }; 5004 /* Current MV decoding probabilities, set to above defaults 5005 every key frame. */ 5007 MV_CONTEXT mvc [2]; /* always row, then column */ 5009 /* Procedure for decoding a complete motion vector. */ 5011 typedef struct { int16 row, col;} MV; /* as in previous chapter */ 5013 MV read_mv( bool_decoder *d) 5014 { 5015 MV v; 5016 v.row = (int16) read_mvcomponent( d, mvc); 5017 v.col = (int16) read_mvcomponent( d, mvc + 1); 5018 return v; 5019 } 5021 /* Procedure for updating MV decoding probabilities, called 5022 every interframe with "d" at the appropriate position in 5023 the frame header. */ 5025 void update_mvcontexts( bool_decoder *d) 5026 { 5027 int i = 0; 5028 do { /* component = row, then column */ 5029 const Prob *up = mv_update_probs[i]; /* update probs 5030 for component */ 5031 Prob *p = mvc[i]; /* start decode tbl "" */ 5032 Prob * const pstop = p + MVPcount; /* end decode tbl "" */ 5033 do { 5034 if( read_bool( d, *up++)) /* update this position */ 5035 { 5036 const Prob x = read_literal( d, 7); 5038 *p = x? x<<1 : 1; 5039 } 5040 } while( ++p < pstop); /* next position */ 5042 } while( ++i < 2); /* next component */ 5043 } 5045 ---- End code block ---------------------------------------- 5047 This completes the description of the motion-vector decoding 5048 procedure and, with it, the procedure for decoding interframe 5049 macroblock prediction records. 5051 18. Interframe Prediction 5053 Given an inter-prediction specification for the current macroblock, 5054 that is, a reference frame together with a motion vector for each of 5055 the sixteen Y subblocks, we describe the calculation of the 5056 prediction buffer for the macroblock. Frame reconstruction is then 5057 completed via the previously-described processes of residue summation 5058 (Section 14) and loop filtering (Section 15). 5060 The management of inter-predicted subblocks may be found in the 5061 reference decoder file reconinter.c; sub-pixel interpolation is 5062 implemented in filter_c.c. 5064 18.1. Bounds on and Adjustment of Motion Vectors 5066 Since each motion vector is differentially encoded from a neighboring 5067 block or macroblock and the only clamp is to ensure that the 5068 referenced motion vector represents a valid location inside a 5069 reference frame buffer, it is technically possible within the VP8 5070 format for a block or macroblock to have arbitrarily large motion 5071 vectors, up to the size of the input image plus the extended border 5072 areas. For practical reasons, VP8 imposes a motion vector size range 5073 limit of [-4096, 4095] full pixels, regardless of image size (VP8 5074 defines 14 raw bits for width and height; 16383x16383 is the maximum 5075 possible image size). Bitstream-compliant encoders and decoders 5076 shall enforce this limit. 5078 Because the motion vectors applied to the chroma subblocks have 1/8 5079 pixel resolution, the synthetic pixel calculation, outlined in 5080 Chapter 5 and detailed below, uses this resolution for the luma 5081 subblocks as well. In accordance, the stored luma motion vectors are 5082 all doubled, each component of each luma vector becoming an even 5083 integer in the range -2046 to +2046, inclusive. 5085 The vector applied to each chroma subblock is calculated by averaging 5086 the vectors for the 4 luma subblocks occupying the same visible area 5087 as the chroma subblock in the usual correspondence, that is, the 5088 vector for U and V block 0 is the average of the vectors for the Y 5089 subblocks { 0, 1, 4, 5}, chroma block 1 corresponds to Y blocks { 2, 5090 3, 6, 7}, chroma block 2 to Y blocks { 8, 9, 12, 13}, and chroma 5091 block 3 to Y blocks { 10, 11, 14, 15}. 5093 In detail, each of the two components of the vectors for each of the 5094 chroma subblocks is calculated from the corresponding luma vector 5095 components as follows: 5097 ---- Begin code block -------------------------------------- 5099 int avg( int c1, int c2, int c3, int c4) 5100 { 5101 int s = c1 + c2 + c3 + c4; 5103 /* The shift divides by 8 (not 4) because chroma pixels 5104 have twice the diameter of luma pixels. The handling 5105 of negative motion vector components is slightly 5106 cumbersome because, strictly speaking, right shifts 5107 of negative numbers are not well-defined in C. */ 5109 return s >= 0 ? (s + 4) >> 3 : -( (-s + 4) >> 3); 5110 } 5112 ---- End code block ---------------------------------------- 5114 Furthermore, if the version number in the frame tag specifies only 5115 full-pel chroma motion vectors, then the fractional parts of both 5116 components of the vector are truncated to zero, as illustrated in the 5117 following pseudo-code (assuming 3 bits of fraction for both luma and 5118 chroma vectors): 5120 ---- Begin code block -------------------------------------- 5122 x = x & (~7); 5123 y = y & (~7); 5125 ---- End code block ---------------------------------------- 5127 Earlier in this document we described the vp8_clamp_mv() function to 5128 limit "nearest" and "near" motion vector predictors inside specified 5129 margins within the frame boundaries. Additional clamping is 5130 performed for NEW_MV macroblocks, for which the final motion vector 5131 is clamped again after combining the "best" predictor and the 5132 differential vector decoded from the stream. 5134 However, the secondary clamping is not performed for SPLIT_MV 5135 macroblocks, meaning that any subblock's motion vector within the 5136 SPLIT_MV macroblock may point outside the clamping zone. These non- 5137 clamped vectors are also used when determining the decoding tree 5138 context for subsequent subblocks' modes in the vp8_mvCont() function. 5140 18.2. Prediction Subblocks 5142 The prediction calculation for each subblock is then as follows. 5143 Temporarily disregarding the fractional part of the motion vector 5144 (that is, rounding "up" or "left" by right-shifting each component 3 5145 bits with sign propagation) and adding the origin (upper left 5146 position) of the (16x16 luma or 8x8 chroma) current macroblock gives 5147 us an origin in the Y, U, or V plane of the predictor frame (either 5148 the golden frame or previous frame). 5150 Considering that origin to be the upper left corner of a (luma or 5151 chroma) macroblock, we need to specify the relative positions of the 5152 pixels associated to that subblock, that is, any pixels that might be 5153 involved in the sub-pixel interpolation processes for the subblock. 5155 18.3. Sub-pixel Interpolation 5157 The sub-pixel interpolation is effected via two one-dimensional 5158 convolutions. These convolutions may be thought of as operating on a 5159 two-dimensional array of pixels whose origin is the subblock origin, 5160 that is the origin of the prediction macroblock described above plus 5161 the offset to the subblock. Because motion vectors are arbitrary, so 5162 are these "prediction subblock origins". 5164 The integer part of the motion vector is subsumed in the origin of 5165 the prediction subblock, the 16 (synthetic) pixels we need to 5166 construct are given by 16 offsets from the origin. The integer part 5167 of each of these offsets is the offset of the corresponding pixel 5168 from the subblock origin (using the vertical stride). To these 5169 integer parts is added a constant fractional part, which is simply 5170 the difference between the actual motion vector and its integer 5171 truncation used to calculate the origins of the prediction macroblock 5172 and subblock. Each component of this fractional part is an integer 5173 between 0 and 7, representing a forward displacement in eighths of a 5174 pixel. 5176 It is these fractional displacements that determine the filtering 5177 process. If they both happen to be zero (that is, we had a "whole 5178 pixel" motion vector), the prediction subblock is simply copied into 5179 the corresponding piece of the current macroblock's prediction 5180 buffer. As discussed in Chapter 14, the layout of the macroblock's 5181 prediction buffer can depend on the specifics of the reconstruction 5182 implementation chosen. Of course, the vertical displacement between 5183 lines of the prediction subblock is given by the stride, as are all 5184 vertical displacements used here. 5186 Otherwise, at least one of the fractional displacements is non-zero. 5187 We then synthesize the missing pixels via a horizontal, followed by a 5188 vertical, one-dimensional interpolation. 5190 The two interpolations are essentially identical. Each uses an (at 5191 most) six-tap filter (the choice of which of course depends on the 5192 one-dimensional offset). Thus, every calculated pixel references at 5193 most three pixels before (above or to-the-left of) it and at most 5194 three pixels after (below or to-the-right of) it. The horizontal 5195 interpolation must calculate two extra rows above and three extra 5196 rows below the 4x4 block, to provide enough samples for the vertical 5197 interpolation to proceed. 5199 Depending on the reconstruction filter type given in the field 5200 Version Number in the frame tag, either a bicubic or a bilinear tap 5201 set is used. 5203 The exact implementation of subsampling is as follows. 5205 ---- Begin code block -------------------------------------- 5207 /* Filter taps taken to 7-bit precision. 5208 Because DC is always passed, taps always sum to 128. */ 5210 const int BilinearFilters[8][6] = 5211 { 5212 { 0, 0, 128, 0, 0, 0 }, 5213 { 0, 0, 112, 16, 0, 0 }, 5214 { 0, 0, 96, 32, 0, 0 }, 5215 { 0, 0, 80, 48, 0, 0 }, 5216 { 0, 0, 64, 64, 0, 0 }, 5217 { 0, 0, 48, 80, 0, 0 }, 5218 { 0, 0, 32, 96, 0, 0 }, 5219 { 0, 0, 16, 112, 0, 0 } 5220 }; 5222 const int filters [8] [6] = { /* indexed by displacement */ 5223 { 0, 0, 128, 0, 0, 0 }, /* degenerate whole-pixel */ 5224 { 0, -6, 123, 12, -1, 0 }, /* 1/8 */ 5225 { 2, -11, 108, 36, -8, 1 }, /* 1/4 */ 5226 { 0, -9, 93, 50, -6, 0 }, /* 3/8 */ 5227 { 3, -16, 77, 77, -16, 3 }, /* 1/2 is symmetric */ 5228 { 0, -6, 50, 93, -9, 0 }, /* 5/8 = reverse of 3/8 */ 5229 { 1, -8, 36, 108, -11, 2 }, /* 3/4 = reverse of 1/4 */ 5230 { 0, -1, 12, 123, -6, 0 } /* 7/8 = reverse of 1/8 */ 5231 }; 5233 /* One-dimensional synthesis of a single sample. 5234 Filter is determined by fractional displacement */ 5236 Pixel interp( 5237 const int fil[6], /* filter to apply */ 5238 const Pixel *p, /* origin (rounded "before") in 5239 prediction area */ 5241 const int s /* size of one forward step "" */ 5242 ) { 5243 int32 a = 0; 5244 int i = 0; 5245 p -= s + s; /* move back two positions */ 5247 do { 5248 a += *p * fil[i]; 5249 p += s; 5250 } while( ++i < 6); 5252 return clamp255( (a + 64) >> 7); /* round to nearest 5253 8-bit value */ 5254 } 5256 /* First do horizontal interpolation, producing intermediate 5257 buffer. */ 5259 void Hinterp( 5260 Pixel temp[9][4], /* 9 rows of 4 (intermediate) 5261 destination values */ 5262 const Pixel *p, /* subblock origin in prediction 5263 frame */ 5264 int s, /* vertical stride to be used in 5265 prediction frame */ 5266 uint hfrac, /* 0 <= horizontal displacement <= 7 */ 5267 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5268 ) { 5269 const int * const fil = bicubic ? filters [hfrac] : 5270 BilinearFilters[hfrac]; 5272 int r = 0; do /* for each row */ 5273 { 5274 int c = 0; do /* for each destination sample */ 5275 { 5276 /* Pixel separation = one horizontal step = 1 */ 5278 temp[r][c] = interp( fil, p + c, 1); 5279 } 5280 while( ++c < 4); 5281 } 5282 while( p += s, ++r < 9); /* advance p to next row */ 5283 } 5285 /* Finish with vertical interpolation, producing final results. 5286 Input array "temp" is of course that computed above. */ 5288 void Vinterp( 5289 Pixel final[4][4], /* 4 rows of 4 (final) destination values */ 5290 const Pixel temp[9][4], 5291 uint vfrac, /* 0 <= vertical displacement <= 7 */ 5292 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5293 ) { 5294 const int * const fil = bicubic ? filters [vfrac] : 5295 BilinearFilters[vfrac]; 5297 int r = 0; do /* for each row */ 5298 { 5299 int c = 0; do /* for each destination sample */ 5300 { 5301 /* Pixel separation = one vertical step = width 5302 of array = 4 */ 5304 final[r][c] = interp( fil, temp[r] + c, 4); 5305 } 5306 while( ++c < 4); 5307 } 5308 while( ++r < 4); 5309 } 5311 ---- End code block ---------------------------------------- 5313 18.4. Filter Properties 5315 We discuss briefly the rationale behind the choice of filters. Our 5316 approach is necessarily cursory; a genuinely accurate discussion 5317 would require a couple of books. Readers unfamiliar with signal 5318 processing may or may not wish to skip this. 5320 All digital signals are of course sampled in some fashion. The case 5321 where the inter-sample spacing (say in time for audio samples, or 5322 space for pixels) is uniform, that is, the same at all positions, is 5323 particularly common and amenable to analysis. Many aspects of the 5324 treatment of such signals are best-understood in the frequency domain 5325 via Fourier Analysis, particularly those aspects of the signal that 5326 are not changed by shifts in position, especially when those 5327 positional shifts are not given by a whole number of samples. 5329 Non-integral translates of a sampled signal are a textbook example of 5330 the foregoing. In our case of non-integral motion vectors, we wish 5331 to say what the underlying image "really is" at these pixels we don't 5332 have values for but feel that it makes sense to talk about. The 5333 correctness of this feeling is predicated on the underlying signal 5334 being band-limited, that is, not containing any energy in spatial 5335 frequencies that cannot be faithfully rendered at the pixel 5336 resolution at our disposal. In one dimension, this range of "OK" 5337 frequencies is called the Nyquist band; in our two-dimensional case 5338 of integer-grid samples, this range might be termed a Nyquist 5339 rectangle. The finer the grid, the more we know about the image, and 5340 the wider the Nyquist rectangle. 5342 It turns out that, for such band-limited signals, there is indeed an 5343 exact mathematical formula to produce the correct sample value at an 5344 arbitrary point. Unfortunately, this calculation requires the 5345 consideration of every single sample in the image, as well as needing 5346 to operate at infinite precision. Also, strictly speaking, all band- 5347 limited signals have infinite spatial (or temporal) extent, so 5348 everything we are discussing is really some sort of approximation. 5350 It is true that the theoretically correct subsampling procedure, as 5351 well as any approximation thereof, is always given by a translation- 5352 invariant weighted sum (or filter) similar to that used by VP8. It 5353 is also true that the reconstruction error made by such a filter can 5354 be simply represented as a multiplier in the frequency domain, that 5355 is, such filters simply multiply the Fourier transform of any signal 5356 to which they are applied by a fixed function associated to the 5357 filter. This fixed function is usually called the frequency response 5358 (or transfer function); the ideal subsampling filter has a frequency 5359 response equal to one in the Nyquist rectangle and zero everywhere 5360 else. 5362 Another basic fact about approximations to "truly correct" 5363 subsampling is that, the wider the subrectangle (within the Nyquist 5364 rectangle) of spatial frequencies one wishes to "pass" (that is, 5365 correctly render) or, put more accurately, the closer one wishes to 5366 approximate the ideal transfer function, the more samples of the 5367 original signal must be considered by the subsampling, and the wider 5368 the calculation precision necessitated. 5370 The filters chosen by VP8 were chosen, within the constraints of 4 or 5371 6 taps and 7-bit precision, to do the best possible job of handling 5372 the low spatial frequencies near the zeroth DC frequency along with 5373 introducing no resonances (places where the absolute value of the 5374 frequency response exceeds one). 5376 The justification for the foregoing has two parts. First, resonances 5377 can produce extremely objectionable visible artifacts when, as often 5378 happens in actual compressed video streams, filters are applied 5379 repeatedly. Second, the vast majority of energy in real-world images 5380 lies near DC and not at the high-end. 5382 To get slightly more specific, the filters chosen by VP8 are the best 5383 resonance-free 4- or 6-tap filters possible, where "best" describes 5384 the frequency response near the origin: the response at 0 is required 5385 to be 1 and the graph of the response at 0 is as flat as possible. 5387 To provide an intuitively more obvious point of reference, the "best" 5388 2-tap filter is given by simple linear interpolation between the 5389 surrounding actual pixels. 5391 Finally, it should be noted that, because of the way motion vectors 5392 are calculated, the (shorter) 4-tap filters (used for odd fractional 5393 displacements) are applied in the chroma plane only. Human color 5394 perception is notoriously poor, especially where higher spatial 5395 frequencies are involved. The shorter filters are easier to 5396 understand mathematically, and the difference between them and a 5397 theoretically slightly better 6-tap filter is negligible where chroma 5398 is concerned. 5400 19. Annex A: Bitstream Syntax 5402 This annex presents the bitstream syntax in a tabular form. All the 5403 information elements have been introduced and explained in the 5404 previous chapters but are collected here for a quick reference. Each 5405 syntax element is shortly described after the tabular representation 5406 along with a reference to the corresponding paragraph in the main 5407 document. The meaning of each syntax element value is not repeated 5408 here. 5410 The top-level hierarchy of the bitstream is introduced in Section 4. 5412 Definition of syntax element coding types can be found in Section 8. 5413 The types used in the representation in this annex are: 5415 o f(n), n-bit value from stream (n successive bits, not boolean 5416 encoded) 5418 o L(n), n-bit number encoded as n booleans (with equal probability 5419 of being 0 or 1) 5421 o B(p), bool with probability p of being 0 5423 o T, tree-encoded value 5425 19.1. Uncompressed Data Chunk 5427 +----------------------+-------+ 5428 | Frame Tag | Type | 5429 +----------------------+-------+ 5430 | frame_tag | f(24) | 5431 | | | 5432 | if (key_frame) { | | 5433 | | | 5434 | start_code | f(24) | 5435 | | | 5436 | horizontal_size_code | f(16) | 5437 | | | 5438 | vertical_size_code | f(16) | 5439 | | | 5440 | } | | 5441 +----------------------+-------+ 5443 The 3-byte frame tag can be parsed as follows: 5445 ---- Begin code block -------------------------------------- 5447 unsigned char *c = pbi->Source; 5448 unsigned int tmp; 5450 tmp = (c[2] << 16) | (c[1] << 8) | c[0]; 5452 key_frame = tmp & 0x1; 5453 version = (tmp >> 1) & 0x7; 5454 show_frame = (tmp >> 4) & 0x1; 5455 first_part_size = (tmp >> 5) & 0x7FFFF; 5457 ---- End code block ---------------------------------------- 5459 Where: 5461 o key_frame indicates if the current frame is a key frame or not. 5463 o version determines the bitstream version. 5465 o show_frame indicates if the current frame is meant to be displayed 5466 or not. 5468 o first_part_size determines the size of the first partition 5469 (control partition), excluding the uncompressed data chunk. 5471 The start_code is a constant 3-byte pattern having value 0x9d012a. 5472 The latter part of the uncompressed chunk (after the start_code) can 5473 be parsed as follows: 5475 ---- Begin code block -------------------------------------- 5477 unsigned char *c = pbi->Source + 6; 5478 unsigned int tmp; 5480 tmp = (c[1] << 8) | c[0]; 5482 width = tmp & 0x3FFF; 5483 horizontal_scale = tmp >> 14; 5485 tmp = (c[3] << 8) | c[2]; 5487 height = tmp & 0x3FFF; 5488 vertical_scale = tmp >> 14; 5490 ---- End code block ---------------------------------------- 5492 19.2. Frame Header 5494 +-------------------------------+------+ 5495 | Frame Header | Type | 5496 +-------------------------------+------+ 5497 | if (key_frame) { | | 5498 | | | 5499 | color_space | L(1) | 5500 | | | 5501 | clamping_type | L(1) | 5502 | | | 5503 | } | | 5504 | | | 5505 | segmentation_enabled | L(1) | 5506 | | | 5507 | if (segmentation_enabled) { | | 5508 | | | 5509 | update_segmentation() | | 5510 | | | 5511 | } | | 5512 | | | 5513 | filter_type | L(1) | 5514 | | | 5515 | loop_filter_level | L(6) | 5516 | | | 5517 | sharpness_level | L(3) | 5518 | | | 5519 | mb_lf_adjustments() | | 5520 | | | 5521 | log2_nbr_of_dct_partitions | L(2) | 5522 | | | 5523 | quant_indices() | | 5524 | | | 5525 | if (key_frame) { | | 5526 | | | 5527 | refresh_entropy_probs | L(1) | 5528 | | | 5529 | } else { | | 5530 | | | 5531 | refresh_golden_frame | L(1) | 5532 | | | 5533 | refresh_alternate_frame | L(1) | 5534 | | | 5535 | if (!refresh_golden_frame) | | 5536 | | | 5537 | copy_buffer_to_golden | L(2) | 5538 | | | 5539 | if (!refresh_alternate_frame) | | 5540 | copy_buffer_to_alternate | L(2) | 5541 | | | 5542 | sign_bias_golden | L(1) | 5543 | | | 5544 | sign_bias_alternate | L(1) | 5545 | | | 5546 | refresh_entropy_probs | L(1) | 5547 | | | 5548 | refresh_last | L(1) | 5549 | | | 5550 | } | | 5551 | | | 5552 | token_prob_update() | | 5553 | | | 5554 | mb_no_coeff_skip | L(1) | 5555 +-------------------------------+------+ 5557 +--------------------------------------+------+ 5558 | Frame Header | Type | 5559 +--------------------------------------+------+ 5560 | prob_skip_false | L(8) | 5561 | | | 5562 | if (!key_frame) { | | 5563 | | | 5564 | prob_intra | L(8) | 5565 | | | 5566 | prob_last | L(8) | 5567 | | | 5568 | prob_golden | L(8) | 5569 | | | 5570 | intra_16x16_prob_update_flag | L(1) | 5571 | | | 5572 | if (intra_16x16_prob_update_flag) { | | 5573 | | | 5574 | for (i = 0; i < 4; i++) | | 5575 | | | 5576 | intra_16x16_prob | L(8) | 5577 | | | 5578 | } | | 5579 | | | 5580 | intra_chroma prob_update_flag | L(1) | 5581 | | | 5582 | if (intra_chroma_prob_update_flag) { | | 5583 | | | 5584 | for (i = 0; i < 3; i++) | | 5585 | | | 5586 | intra_chroma_prob | L(8) | 5587 | | | 5588 | } | | 5589 | | | 5590 | mv_prob_update() | | 5591 | | | 5592 | } | | 5593 +--------------------------------------+------+ 5595 o color_space defines the YUV color space of the sequence 5596 (Section 9.2) 5598 o clamping_type specifies if the decoder is required to clamp the 5599 reconstructed pixel values (Section 9.2) 5601 o segmentation_enabled enables the segmentation feature for the 5602 current frame (Section 9.3) 5604 o filter_type determines whether the normal or the simple loop 5605 filter is used (Section 9.4, Section 15) 5607 o loop_filter_level controls the deblocking filter (Section 9.4, 5608 Section 15) 5610 o sharpness_level controls the deblocking filter (Section 9.4, 5611 Section 15) 5613 o log2_nbr_of_dct_partitions determines the number of separate 5614 partitions containing the DCT coefficients of the macroblocks 5615 (Section 9.5) 5617 o refresh_entropy_probs determines whether updated token 5618 probabilities are used only for this frame or until further update 5620 o refresh_golden_frame determines if the current decoded frame 5621 refreshes the golden frame (Section 9.7) 5623 o refresh_alternate_frame determines if the current decoded frame 5624 refreshes the alternate reference frame (Section 9.7) 5626 o copy_buffer_to_golden determines if the golden reference is 5627 replaced by another reference (Section 9.7) 5629 o copy_buffer_to_alternate determines if the alternate reference is 5630 replaced by another reference (Section 9.7) 5632 o sign_bias_golden controls the sign of motion vectors when the 5633 golden frame is referenced (Section 9.7) 5635 o sign_bias_alternate controls the sign of motion vectors when the 5636 alternate frame is referenced (Section 9.7) 5638 o refresh_last determines if the current decoded frame refreshes the 5639 last frame reference buffer (Section 9.8) 5641 o mb_no_coeff_skip enables or disables the skipping of macroblocks 5642 containing no non-zero coefficients (Section 9.10) 5644 o prob_skip_false the probability that the macroblock is not skipped 5645 (flag indicating skipped macroblock is false) (Section 9.10) 5647 o prob_intra the probability of an intra macroblock (Section 9.10) 5649 o prob_last the probability that the last reference frame is used 5650 for inter prediction (Section 9.10) 5652 o prob_golden the probability that the golden reference frame is 5653 used for inter prediction (Section 9.10) 5655 o intra_16x16_prob_update_flag indicates if the branch probabilies 5656 used in the decoding of luma intra prediction mode are updated 5657 (Section 9.10) 5659 o intra_16x16_prob the branch probabilities of the luma intra 5660 prediction mode decoding tree 5662 o intra_chroma_prob_update_flag indicates if the branch probabilies 5663 used in the decoding of chroma intra prediction mode are updated 5664 (Section 9.10) 5666 o intra_chroma_prob the branch probabilities of the chroma intra 5667 prediction mode decoding tree 5669 +------------------------------------+------+ 5670 | update_segmentation() | Type | 5671 +------------------------------------+------+ 5672 | update_mb_segmentation_map | L(1) | 5673 | | | 5674 | update_segment_feature_data | L(1) | 5675 | | | 5676 | if (update_segment_feature_data) { | | 5677 | | | 5678 | segment_feature_mode | L(1) | 5679 | | | 5680 | for (i = 0; i < 4; i++) { | | 5681 | | | 5682 | quantizer_update | L(1) | 5683 | | | 5684 | if (quantizer_update) { | | 5685 | | | 5686 | quantizer_update_value | L(7) | 5687 | | | 5688 | quantizer_update_sign | L(1) | 5689 | | | 5690 | } | | 5691 | | | 5692 | } | | 5693 | | | 5694 | for (i = 0; i < 4; i++) { | | 5695 | | | 5696 | loop_filter_update | L(1) | 5697 | | | 5698 | if (loop_filter_update) { | | 5699 | | | 5700 | lf_update_value | L(6) | 5701 | | | 5702 | lf_update_sign | L(1) | 5703 | | | 5704 | } | | 5705 | | | 5706 | } | | 5707 | | | 5708 | } | | 5709 | | | 5710 | if (update_mb_segmentation_map) { | | 5711 | | | 5712 | for (i = 0; i < 3; i++) { | | 5713 | | | 5714 | segment_prob_update | L(1) | 5715 | | | 5716 | if (segment_prob_update) { | | 5717 | | | 5718 | segment_prob | L(8) | 5719 | | | 5720 | } | | 5721 | | | 5722 | } | | 5723 | | | 5724 | } | | 5725 +------------------------------------+------+ 5727 o update_mb_segmentation_map determines if the MB segmentation map 5728 is updated in the current frame (Section 9.3) 5730 o update_segment_feature_data indicates if the segment feature data 5731 is updated in the current frame (Section 9.3) 5733 o segment_feature_mode indicates the feature data update mode, 0 for 5734 delta and 1 for the absolute value (Section 9.3) 5736 o quantizer_update indicates if the quantizer value is updated for 5737 the i^(th) segment (Section 9.3) 5739 o quantizer_update_value indicates the update value for the segment 5740 quantizer (Section 9.3) 5742 o quantizer_update_sign indicates the update sign for the segment 5743 quantizer (Section 9.3) 5745 o loop_filter_update indicates if the loop filter level value is 5746 updated for the i^(th) segment (Section 9.3) 5748 o lf_update_value indicates the update value for the loop filter 5749 level (Section 9.3) 5751 o lf_update_sign indicates the update sign for the loop filter level 5752 (Section 9.3) 5754 o segment_prob_update indicates if the branch probabilities used to 5755 decode the segment_id in the MB header are decoded from the stream 5756 or use the default value of 255 (Section 9.3) 5758 o segment_prob the branch probabilities of the segment_id decoding 5759 tree (Section 9.3) 5760 +------------------------------------+------+ 5761 | mb_lf_adjustments() | Type | 5762 +------------------------------------+------+ 5763 | loop_filter_adj_enable | L(1) | 5764 | | | 5765 | if (loop_filter_adj_enable) { | | 5766 | | | 5767 | mode_ref_lf_delta_update | L(1) | 5768 | | | 5769 | if (mode_ref_lf_delta_update) { | | 5770 | | | 5771 | for (i = 0; i < 4; i++) { | | 5772 | | | 5773 | ref_frame_delta_update_flag | L(1) | 5774 | | | 5775 | if (ref_frame_delta_update_flag) { | | 5776 | | | 5777 | delta_magnitude | L(6) | 5778 | | | 5779 | delta_sign | L(1) | 5780 | | | 5781 | } | | 5782 | | | 5783 | } | | 5784 | | | 5785 | for (i = 0; i < 4; i++) { | | 5786 | | | 5787 | mb_mode_delta_update_flag | L(1) | 5788 | | | 5789 | if (mb_mode_delta_update_flag) { | | 5790 | | | 5791 | delta_magnitude | L(6) | 5792 | | | 5793 | delta_sign | L(1) | 5794 | | | 5795 | } | | 5796 | | | 5797 | } | | 5798 | | | 5799 | } | | 5800 | | | 5801 | } | | 5802 +------------------------------------+------+ 5804 o loop_filter_adj_enable indicates if the MB-level loop filter 5805 adjustment (based on the used reference frame and coding mode) is 5806 on for the current frame (Section 9.4) 5808 o mode_ref_lf_delta_update indicates if the delta values used in 5809 adjustment are updated in the current frame (Section 9.4) 5811 o ref_frame_delta_update_flag indicates if the adjustment delta 5812 value corresponding to a certain used reference frame is updated 5813 (Section 9.4) 5815 o delta_magnitude is the absolute value of the delta value 5817 o delta_sign is the sign of the delta value 5819 o mb_mode_delta_update_flag indicates if the adjustment delta value 5820 corresponding to certain MB prediction mode is updated 5821 (Section 9.4) 5822 +----------------------------+------+ 5823 | quant_indices() | Type | 5824 +----------------------------+------+ 5825 | y_ac_qi | L(7) | 5826 | | | 5827 | y_dc_delta_present | L(1) | 5828 | | | 5829 | if (y_dc_delta_present) { | | 5830 | | | 5831 | y_dc_delta_magnitude | L(4) | 5832 | | | 5833 | y_dc_delta_sign | L(1) | 5834 | | | 5835 | } | | 5836 | | | 5837 | if (y2_dc_delta_present) { | | 5838 | | | 5839 | y2_dc_delta_magnitude | L(4) | 5840 | | | 5841 | y2_dc_delta_sign | L(1) | 5842 | | | 5843 | } | | 5844 | | | 5845 | if (y2_ac_delta_present) { | | 5846 | | | 5847 | y2_ac_delta_magnitude | L(4) | 5848 | | | 5849 | y2_ac_delta_sign | L(1) | 5850 | | | 5851 | } | | 5852 | | | 5853 | if (uv_dc_delta_present) { | | 5854 | | | 5855 | uv_dc_delta_magnitude | L(4) | 5856 | | | 5857 | uv_dc_delta_sign | L(1) | 5858 | | | 5859 | } | | 5860 | | | 5861 | if (uv_ac_delta_present) { | | 5862 | | | 5863 | uv_ac_delta_magnitude | L(4) | 5864 | | | 5865 | uv_ac_delta_sign | L(1) | 5866 | | | 5867 | } | | 5868 +----------------------------+------+ 5870 o y_ac_qi is the dequantization table index used for the luma AC 5871 coefficients (and other coefficient groups if no delta value is 5872 present) (Section 9.6) 5874 o y_dc_delta_present indicates if the stream contains a delta value 5875 that is added to the baseline index to obtain the luma DC 5876 coefficient dequantization index (Section 9.6) 5878 o y_dc_delta_magnitude the magnitude of the delta value 5879 (Section 9.6) 5881 o y_dc_delta_sign the sign of the delta value (Section 9.6) 5883 o y2_dc_delta_present indicates if the stream contains a delta value 5884 that is added to the baseline index to obtain the Y2 block DC 5885 coefficient dequantization index (Section 9.6) 5887 o y2_ac_delta_present indicates if the stream contains a delta value 5888 that is added to the baseline index to obtain the Y2 block AC 5889 coefficient dequantization index (Section 9.6) 5891 o uv_dc_delta_present indicates if the stream contains a delta value 5892 that is added to the baseline index to obtain the chroma DC 5893 coefficient dequantization index (Section 9.6) 5895 o uv_ac_delta_present indicates if the stream contains a delta value 5896 that is added to the baseline index to obtain the chroma AC 5897 coefficient dequantization index (Section 9.6) 5898 +-------------------------------+------+ 5899 | token_prob_update() | Type | 5900 +-------------------------------+------+ 5901 | for (i = 0; i < 4; i++) { | | 5902 | | | 5903 | for (j = 0; j < 8; j++) { | | 5904 | | | 5905 | for (k = 0; k < 3; k++) { | | 5906 | | | 5907 | for (l = 0; l < 11; l++) { | | 5908 | | | 5909 | coeff_prob_update_flag | L(1) | 5910 | | | 5911 | if (coeff_prob_update_flag) { | | 5912 | | | 5913 | coeff_prob | L(8) | 5914 | | | 5915 | } | | 5916 | | | 5917 | } | | 5918 | | | 5919 | } | | 5920 | | | 5921 | } | | 5922 | | | 5923 | } | | 5924 +-------------------------------+------+ 5926 o coeff_prob_update_flag indicates if the corresponding branch 5927 probability is updated in the current frame (Section 13.4) 5929 o coeff_prob is the new branch probability (Section 13.4) 5930 +----------------------------+------+ 5931 | mv_prob_update() | Type | 5932 +----------------------------+------+ 5933 | for (i = 0; i < 2; i++) { | | 5934 | | | 5935 | for (j = 0; j < 19; j++) { | | 5936 | | | 5937 | mv_prob_update_flag | L(1) | 5938 | | | 5939 | if (mv_prob_update_flag) { | | 5940 | | | 5941 | prob | L(7) | 5942 | | | 5943 | } | | 5944 | | | 5945 | } | | 5946 | | | 5947 | } | | 5948 +----------------------------+------+ 5950 o mv_prob_update_flag indicates if the corresponding MV decoding 5951 probability is updated in the current frame (Section 17.2) 5953 o prob is the updated probability (Section 17.2) 5955 19.3. Macroblock Data 5957 +---------------------+------+ 5958 | Macroblock Data | Type | 5959 +---------------------+------+ 5960 | macroblock_header() | | 5961 | | | 5962 | residual_data() | | 5963 +---------------------+------+ 5965 +--------------------------------+------+ 5966 | macroblock_header() | Type | 5967 +--------------------------------+------+ 5968 | if (segmentation_map_update) { | | 5969 | | | 5970 | segment_id | T | 5971 | | | 5972 | if (mb_no_coeff_skip) { | | 5973 | | | 5974 | mb_coeff_skip | B(p) | 5975 | | | 5976 | } | | 5977 | | | 5978 | if (!key_frame) { | | 5979 | | | 5980 | is_inter_mb | B(p) | 5981 | | | 5982 | if (is_inter_mb) { | | 5983 | | | 5984 | mb_ref_frame_sel1 | B(p) | 5985 | | | 5986 | if (mb_ref_frame_sel1) | | 5987 | | | 5988 | mb_ref_frame_sel2 | B(p) | 5989 | | | 5990 | mv_mode | T | 5991 | | | 5992 | if (mv_mode == SPLITMV) { | | 5993 | | | 5994 | mv_split_mode | T | 5995 | | | 5996 | for (i = 0; i < numMvs; i++) { | | 5997 | | | 5998 | sub_mv_mode | T | 5999 | | | 6000 | if (sub_mv_mode == NEWMV4x4) { | | 6001 | | | 6002 | read_mvcomponent() | | 6003 | | | 6004 | read_mvcomponent() | | 6005 | | | 6006 | } | | 6007 | | | 6008 | } | | 6009 | | | 6010 | } else if (mv_mode == NEWMV) { | | 6011 | | | 6012 | read_mvcomponent() | | 6013 | | | 6014 | read_mvcomponent() | | 6015 | | | 6016 | } | | 6017 | | | 6018 | } else { /* intra mb */ | | 6019 | | | 6020 | intra_y_mode | T | 6021 +--------------------------------+------+ 6022 +-------------------------------+------+ 6023 | macroblock_header() | Type | 6024 +-------------------------------+------+ 6025 | if (intra_y_mode == B_PRED) { | | 6026 | | | 6027 | for (i = 0; i < 16; i++) | | 6028 | | | 6029 | intra_b_mode | T | 6030 | | | 6031 | } | | 6032 | | | 6033 | intra_uv_mode | T | 6034 | | | 6035 | } | | 6036 +-------------------------------+------+ 6038 o segment_id indicates to which segment the macroblock belongs 6039 (Section 10) 6041 o mb_coeff_skip indicates if the macroblock contains any coded 6042 coefficients or not (Section 11.1) 6044 o is_inter_mb indicates if the macroblock is intra or inter coded 6045 (Section 16) 6047 o mb_ref_frame_sel1 selects the reference frame to be used; last 6048 frame (0), golden/alternate (1) (Section 16.2) 6050 o mb_ref_frame_sel2 selects whether the golden (0) or alternate 6051 reference frame (1) is used (Section 16.2) 6053 o mv_mode determines the macroblock motion vector mode 6054 (Section 16.2) 6056 o mv_split_mode gives macroblock partitioning specification and 6057 determines number of motion vectors used (numMvs)(Section 16.2) 6059 o sub_mv_mode determines the sub-macroblock motion vector mode for 6060 macroblocks coded using SPLITMV motion vector mode (Section 16.2) 6062 o intra_y_mode selects the luminance intra prediction mode 6063 (Section 16.1) 6065 o intra_b_mode selects the sub-macroblock luminance prediction mode 6066 for macroblocks coded using B_PRED mode (Section 16.1) 6068 o intra_uv_mode selects the chrominance intra prediction mode 6069 (Section 16.1) 6070 +----------------------------------------------+------+ 6071 | residual_data() | Type | 6072 +----------------------------------------------+------+ 6073 | if (!mb_coeff_skip) { | | 6074 | | | 6075 | if ( (is_inter_mb && mv_mode != SPLITMV) || | | 6076 | | | 6077 | (!is_inter_mb && intra_y_mode != B_PRED) ) { | | 6078 | | | 6079 | residual_block() /* Y2 */ | | 6080 | | | 6081 | } | | 6082 | | | 6083 | for (i = 0; i < 24; i++) | | 6084 | | | 6085 | residual_block() /* 16 Y, 4 U, 4 V */ | | 6086 | | | 6087 | } | | 6088 +----------------------------------------------+------+ 6090 +-------------------------------------+------+ 6091 | residual_block() | Type | 6092 +-------------------------------------+------+ 6093 | for (i = firstCoeff; i < 16; i++) { | | 6094 | | | 6095 | token | T | 6096 | | | 6097 | if (token == EOB) break; | | 6098 | | | 6099 | if (token_has_extra_bits) { | | 6100 | | | 6101 | extra_bits | L(n) | 6102 | | | 6103 | sign | L(1) | 6104 +-------------------------------------+------+ 6106 o firstCoeff is 1 for luma blocks of macroblocks containing Y2 6107 subblock, otherwise 0 6109 o token defines the value of the coefficient, the value range of the 6110 coefficient or the end of block (Section 13.2) 6112 o extra_bits determine the value of the coefficient within the value 6113 range defined by token (Section 13.2) 6115 o sign indicates the sign of the coefficient (Section 13.2) 6117 20. Attachment One: Reference Decoder Source Code 6119 Note that the code in this attachment may exhibit bugs, and should be 6120 considered a draft until this document reaches RFC status. 6122 20.1. bit_ops.h 6124 ---- Begin code block -------------------------------------- 6126 /* 6127 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6128 * 6129 * Use of this source code is governed by a BSD-style license 6130 * that can be found in the LICENSE file in the root of the source 6131 * tree. An additional intellectual property rights grant can be 6132 * found in the file PATENTS. All contributing project authors may 6133 * be found in the AUTHORS file in the root of the source tree. 6134 */ 6135 #ifndef BIT_OPS_H 6136 #define BIT_OPS_H 6138 /* Evaluates to a mask with n bits set */ 6139 #define BITS_MASK(n) ((1<<(n))-1) 6141 /* Returns len bits, with the LSB at position bit */ 6142 #define BITS_GET(val, bit, len) (((val)>>(bit))&BITS_MASK(len)) 6144 #endif 6146 ---- End code block ---------------------------------------- 6148 20.2. bool_decoder.h 6150 ---- Begin code block -------------------------------------- 6152 /* 6153 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6154 * 6155 * Use of this source code is governed by a BSD-style license and 6156 * patent grant that can be found in the LICENSE file in the root of 6157 * the source tree. All contributing project authors may be found in 6158 * the AUTHORS file in the root of the source tree. 6159 */ 6161 #ifndef BOOL_DECODER_H 6162 #define BOOL_DECODER_H 6163 #include 6165 struct bool_decoder 6166 { 6167 const unsigned char *input; /* next compressed data byte */ 6168 size_t input_len; /* length of the input buffer */ 6169 unsigned int range; /* identical to encoder's 6170 * range */ 6171 unsigned int value; /* contains at least 8 6172 * significant bits */ 6173 int bit_count; /* # of bits shifted out of 6174 * value, max 7 */ 6175 }; 6177 static void 6178 init_bool_decoder(struct bool_decoder *d, 6179 const unsigned char *start_partition, 6180 size_t sz) 6181 { 6182 if (sz >= 2) 6183 { 6184 d->value = (start_partition[0] << 8) /* first 2 input 6185 * bytes */ 6186 | start_partition[1]; 6187 d->input = start_partition + 2; /* ptr to next byte */ 6188 d->input_len = sz - 2; 6189 } 6190 else 6191 { 6192 d->value = 0; 6193 d->input = NULL; 6194 d->input_len = 0; 6195 } 6197 d->range = 255; /* initial range is full */ 6198 d->bit_count = 0; /* have not yet shifted out any bits */ 6199 } 6201 static int bool_get(struct bool_decoder *d, int probability) 6202 { 6203 /* range and split are identical to the corresponding values 6204 used by the encoder when this bool was written */ 6206 unsigned int split = 1 + (((d->range - 1) * probability) >> 8); 6207 unsigned int SPLIT = split << 8; 6208 int retval; /* will be 0 or 1 */ 6209 if (d->value >= SPLIT) /* encoded a one */ 6210 { 6211 retval = 1; 6212 d->range -= split; /* reduce range */ 6213 d->value -= SPLIT; /* subtract off left endpoint of 6214 * interval */ 6215 } 6216 else /* encoded a zero */ 6217 { 6218 retval = 0; 6219 d->range = split; /* reduce range, no change in left 6220 * endpoint */ 6221 } 6223 while (d->range < 128) /* shift out irrelevant value bits */ 6224 { 6225 d->value <<= 1; 6226 d->range <<= 1; 6228 if (++d->bit_count == 8) /* shift in new bits 8 at a time */ 6229 { 6230 d->bit_count = 0; 6232 if (d->input_len) 6233 { 6234 d->value |= *d->input++; 6235 d->input_len--; 6236 } 6237 } 6238 } 6240 return retval; 6241 } 6243 static int bool_get_bit(struct bool_decoder *br) 6244 { 6245 return bool_get(br, 128); 6246 } 6248 static int bool_get_uint(struct bool_decoder *br, int bits) 6249 { 6250 int z = 0; 6251 int bit; 6253 for (bit = bits - 1; bit >= 0; bit--) 6254 { 6255 z |= (bool_get_bit(br) << bit); 6256 } 6258 return z; 6259 } 6261 static int bool_get_int(struct bool_decoder *br, int bits) 6262 { 6263 int z = 0; 6264 int bit; 6266 for (bit = bits - 1; bit >= 0; bit--) 6267 { 6268 z |= (bool_get_bit(br) << bit); 6269 } 6271 return bool_get_bit(br) ? -z : z; 6272 } 6274 static int bool_maybe_get_int(struct bool_decoder *br, int bits) 6275 { 6276 return bool_get_bit(br) ? bool_get_int(br, bits) : 0; 6277 } 6279 static int 6280 bool_read_tree(struct bool_decoder *bool, 6281 const int *t, 6282 const unsigned char *p) 6283 { 6284 int i = 0; 6286 while ((i = t[ i + bool_get(bool, p[i>>1])]) > 0) ; 6288 return -i; 6289 } 6290 #endif 6292 ---- End code block ---------------------------------------- 6294 20.3. dequant_data.h 6296 ---- Begin code block -------------------------------------- 6298 static const int dc_q_lookup[128] = 6299 { 6300 4, 5, 6, 7, 8, 9, 10, 10, 6301 11, 12, 13, 14, 15, 16, 17, 17, 6302 18, 19, 20, 20, 21, 21, 22, 22, 6303 23, 23, 24, 25, 25, 26, 27, 28, 6304 29, 30, 31, 32, 33, 34, 35, 36, 6305 37, 37, 38, 39, 40, 41, 42, 43, 6306 44, 45, 46, 46, 47, 48, 49, 50, 6307 51, 52, 53, 54, 55, 56, 57, 58, 6308 59, 60, 61, 62, 63, 64, 65, 66, 6309 67, 68, 69, 70, 71, 72, 73, 74, 6310 75, 76, 76, 77, 78, 79, 80, 81, 6311 82, 83, 84, 85, 86, 87, 88, 89, 6312 91, 93, 95, 96, 98, 100, 101, 102, 6313 104, 106, 108, 110, 112, 114, 116, 118, 6314 122, 124, 126, 128, 130, 132, 134, 136, 6315 138, 140, 143, 145, 148, 151, 154, 157 6316 }; 6317 static const int ac_q_lookup[128] = 6318 { 6319 4, 5, 6, 7, 8, 9, 10, 11, 6320 12, 13, 14, 15, 16, 17, 18, 19, 6321 20, 21, 22, 23, 24, 25, 26, 27, 6322 28, 29, 30, 31, 32, 33, 34, 35, 6323 36, 37, 38, 39, 40, 41, 42, 43, 6324 44, 45, 46, 47, 48, 49, 50, 51, 6325 52, 53, 54, 55, 56, 57, 58, 60, 6326 62, 64, 66, 68, 70, 72, 74, 76, 6327 78, 80, 82, 84, 86, 88, 90, 92, 6328 94, 96, 98, 100, 102, 104, 106, 108, 6329 110, 112, 114, 116, 119, 122, 125, 128, 6330 131, 134, 137, 140, 143, 146, 149, 152, 6331 155, 158, 161, 164, 167, 170, 173, 177, 6332 181, 185, 189, 193, 197, 201, 205, 209, 6333 213, 217, 221, 225, 229, 234, 239, 245, 6334 249, 254, 259, 264, 269, 274, 279, 284 6335 }; 6337 ---- End code block ---------------------------------------- 6339 20.4. dixie.c 6340 ---- Begin code block -------------------------------------- 6342 /* 6343 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6344 * 6345 * Use of this source code is governed by a BSD-style license 6346 * that can be found in the LICENSE file in the root of the source 6347 * tree. An additional intellectual property rights grant can be 6348 * found in the file PATENTS. All contributing project authors may 6349 * be found in the AUTHORS file in the root of the source tree. 6350 */ 6351 #include "vpx_codec_internal.h" 6352 #include "bit_ops.h" 6353 #include "dixie.h" 6354 #include "vp8_prob_data.h" 6355 #include "dequant_data.h" 6356 #include "modemv.h" 6357 #include "tokens.h" 6358 #include "predict.h" 6359 #include "dixie_loopfilter.h" 6360 #include 6361 #include 6363 enum 6364 { 6365 FRAME_HEADER_SZ = 3, 6366 KEYFRAME_HEADER_SZ = 7 6367 }; 6369 #define ARRAY_COPY(a,b) {\ 6370 assert(sizeof(a)==sizeof(b));memcpy(a,b,sizeof(a));} 6371 static void 6372 decode_entropy_header(struct vp8_decoder_ctx *ctx, 6373 struct bool_decoder *bool, 6374 struct vp8_entropy_hdr *hdr) 6375 { 6376 int i, j, k, l; 6378 /* Read coefficient probability updates */ 6379 for (i = 0; i < BLOCK_TYPES; i++) 6380 for (j = 0; j < COEF_BANDS; j++) 6381 for (k = 0; k < PREV_COEF_CONTEXTS; k++) 6382 for (l = 0; l < ENTROPY_NODES; l++) 6383 if (bool_get(bool, 6384 k_coeff_entropy_update_probs 6385 [i][j][k][l])) 6386 hdr->coeff_probs[i][j][k][l] = 6387 bool_get_uint(bool, 8); 6389 /* Read coefficient skip mode probability */ 6390 hdr->coeff_skip_enabled = bool_get_bit(bool); 6392 if (hdr->coeff_skip_enabled) 6393 hdr->coeff_skip_prob = bool_get_uint(bool, 8); 6395 /* Parse interframe probability updates */ 6396 if (!ctx->frame_hdr.is_keyframe) 6397 { 6398 hdr->prob_inter = bool_get_uint(bool, 8); 6399 hdr->prob_last = bool_get_uint(bool, 8); 6400 hdr->prob_gf = bool_get_uint(bool, 8); 6402 if (bool_get_bit(bool)) 6403 for (i = 0; i < 4; i++) 6404 hdr->y_mode_probs[i] = bool_get_uint(bool, 8); 6406 if (bool_get_bit(bool)) 6407 for (i = 0; i < 3; i++) 6408 hdr->uv_mode_probs[i] = bool_get_uint(bool, 8); 6410 for (i = 0; i < 2; i++) 6411 for (j = 0; j < MV_PROB_CNT; j++) 6412 if (bool_get(bool, k_mv_entropy_update_probs[i][j])) 6413 { 6414 int x = bool_get_uint(bool, 7); 6415 hdr->mv_probs[i][j] = x ? x << 1 : 1; 6416 } 6417 } 6418 } 6420 static void 6421 decode_reference_header(struct vp8_decoder_ctx *ctx, 6422 struct bool_decoder *bool, 6423 struct vp8_reference_hdr *hdr) 6424 { 6425 unsigned int key = ctx->frame_hdr.is_keyframe; 6427 hdr->refresh_gf = key ? 1 : bool_get_bit(bool); 6428 hdr->refresh_arf = key ? 1 : bool_get_bit(bool); 6429 hdr->copy_gf = key ? 0 : !hdr->refresh_gf 6430 ? bool_get_uint(bool, 2) : 0; 6431 hdr->copy_arf = key ? 0 : !hdr->refresh_arf 6432 ? bool_get_uint(bool, 2) : 0; 6433 hdr->sign_bias[GOLDEN_FRAME] = key ? 0 : bool_get_bit(bool); 6434 hdr->sign_bias[ALTREF_FRAME] = key ? 0 : bool_get_bit(bool); 6435 hdr->refresh_entropy = bool_get_bit(bool); 6436 hdr->refresh_last = key ? 1 : bool_get_bit(bool); 6437 } 6439 static void 6440 decode_quantizer_header(struct vp8_decoder_ctx *ctx, 6441 struct bool_decoder *bool, 6442 struct vp8_quant_hdr *hdr) 6443 { 6444 int update; 6445 int last_q = hdr->q_index; 6447 hdr->q_index = bool_get_uint(bool, 7); 6448 update = last_q != hdr->q_index; 6449 update |= (hdr->y1_dc_delta_q = bool_maybe_get_int(bool, 4)); 6450 update |= (hdr->y2_dc_delta_q = bool_maybe_get_int(bool, 4)); 6451 update |= (hdr->y2_ac_delta_q = bool_maybe_get_int(bool, 4)); 6452 update |= (hdr->uv_dc_delta_q = bool_maybe_get_int(bool, 4)); 6453 update |= (hdr->uv_ac_delta_q = bool_maybe_get_int(bool, 4)); 6454 hdr->delta_update = update; 6455 } 6457 static void 6458 decode_and_init_token_partitions(struct vp8_decoder_ctx *ctx, 6459 struct bool_decoder *bool, 6460 const unsigned char *data, 6461 unsigned int sz, 6462 struct vp8_token_hdr *hdr) 6463 { 6464 int i; 6466 hdr->partitions = 1 << bool_get_uint(bool, 2); 6468 if (sz < 3 *(hdr->partitions - 1)) 6469 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6470 "Truncated packet found parsing partition" 6471 " lengths."); 6473 sz -= 3 * (hdr->partitions - 1); 6475 for (i = 0; i < hdr->partitions; i++) 6476 { 6477 if (i < hdr->partitions - 1) 6478 { 6479 hdr->partition_sz[i] = (data[2] << 16) 6480 | (data[1] << 8) | data[0]; 6481 data += 3; 6482 } 6483 else 6484 hdr->partition_sz[i] = sz; 6486 if (sz < hdr->partition_sz[i]) 6487 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6488 "Truncated partition %d", i); 6490 sz -= hdr->partition_sz[i]; 6491 } 6493 for (i = 0; i < ctx->token_hdr.partitions; i++) 6494 { 6495 init_bool_decoder(&ctx->tokens[i].bool, data, 6496 ctx->token_hdr.partition_sz[i]); 6497 data += ctx->token_hdr.partition_sz[i]; 6498 } 6499 } 6501 static void 6502 decode_loopfilter_header(struct vp8_decoder_ctx *ctx, 6503 struct bool_decoder *bool, 6504 struct vp8_loopfilter_hdr *hdr) 6505 { 6506 if (ctx->frame_hdr.is_keyframe) 6507 memset(hdr, 0, sizeof(*hdr)); 6509 hdr->use_simple = bool_get_bit(bool); 6510 hdr->level = bool_get_uint(bool, 6); 6511 hdr->sharpness = bool_get_uint(bool, 3); 6512 hdr->delta_enabled = bool_get_bit(bool); 6514 if (hdr->delta_enabled && bool_get_bit(bool)) 6515 { 6516 int i; 6518 for (i = 0; i < BLOCK_CONTEXTS; i++) 6519 hdr->ref_delta[i] = bool_maybe_get_int(bool, 6); 6521 for (i = 0; i < BLOCK_CONTEXTS; i++) 6522 hdr->mode_delta[i] = bool_maybe_get_int(bool, 6); 6523 } 6524 } 6525 static void 6526 decode_segmentation_header(struct vp8_decoder_ctx *ctx, 6527 struct bool_decoder *bool, 6528 struct vp8_segment_hdr *hdr) 6529 { 6530 if (ctx->frame_hdr.is_keyframe) 6531 memset(hdr, 0, sizeof(*hdr)); 6533 hdr->enabled = bool_get_bit(bool); 6535 if (hdr->enabled) 6536 { 6537 int i; 6539 hdr->update_map = bool_get_bit(bool); 6540 hdr->update_data = bool_get_bit(bool); 6542 if (hdr->update_data) 6543 { 6544 hdr->abs = bool_get_bit(bool); 6546 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6547 hdr->quant_idx[i] = bool_maybe_get_int(bool, 7); 6549 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6550 hdr->lf_level[i] = bool_maybe_get_int(bool, 6); 6551 } 6553 if (hdr->update_map) 6554 { 6555 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) 6556 hdr->tree_probs[i] = bool_get_bit(bool) 6557 ? bool_get_uint(bool, 8) 6558 : 255; 6559 } 6560 } 6561 else 6562 { 6563 hdr->update_map = 0; 6564 hdr->update_data = 0; 6565 } 6566 } 6568 static void 6569 dequant_global_init(struct dequant_factors dqf[MAX_MB_SEGMENTS]) 6570 { 6571 int i; 6572 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6573 dqf[i].quant_idx = -1; 6574 } 6576 static int 6577 clamp_q(int q) 6578 { 6579 if (q < 0) return 0; 6580 else if (q > 127) return 127; 6582 return q; 6583 } 6585 static int 6586 dc_q(int q) 6587 { 6588 return dc_q_lookup[clamp_q(q)]; 6589 } 6591 static int 6592 ac_q(int q) 6593 { 6594 return ac_q_lookup[clamp_q(q)]; 6595 } 6597 static void 6598 dequant_init(struct dequant_factors factors[MAX_MB_SEGMENTS], 6599 const struct vp8_segment_hdr *seg, 6600 const struct vp8_quant_hdr *quant_hdr) 6601 { 6602 int i, q; 6603 struct dequant_factors *dqf = factors; 6605 for (i = 0; i < (seg->enabled ? MAX_MB_SEGMENTS : 1); i++) 6606 { 6607 q = quant_hdr->q_index; 6609 if (seg->enabled) 6610 q = (!seg->abs) ? q + seg->quant_idx[i] 6611 : seg->quant_idx[i]; 6613 if (dqf->quant_idx != q || quant_hdr->delta_update) 6614 { 6615 dqf->factor[TOKEN_BLOCK_Y1][0] = 6616 dc_q(q + quant_hdr->y1_dc_delta_q); 6617 dqf->factor[TOKEN_BLOCK_Y1][1] = 6618 ac_q(q); 6619 dqf->factor[TOKEN_BLOCK_UV][0] = 6620 dc_q(q + quant_hdr->uv_dc_delta_q); 6621 dqf->factor[TOKEN_BLOCK_UV][1] = 6622 ac_q(q + quant_hdr->uv_ac_delta_q); 6623 dqf->factor[TOKEN_BLOCK_Y2][0] = 6624 dc_q(q + quant_hdr->y2_dc_delta_q) * 2; 6625 dqf->factor[TOKEN_BLOCK_Y2][1] = 6626 ac_q(q + quant_hdr->y2_ac_delta_q) * 155 / 100; 6628 if (dqf->factor[TOKEN_BLOCK_Y2][1] < 8) 6629 dqf->factor[TOKEN_BLOCK_Y2][1] = 8; 6631 if (dqf->factor[TOKEN_BLOCK_UV][0] > 132) 6632 dqf->factor[TOKEN_BLOCK_UV][0] = 132; 6634 dqf->quant_idx = q; 6635 } 6637 dqf++; 6638 } 6639 } 6641 static void 6642 decode_frame(struct vp8_decoder_ctx *ctx, 6643 const unsigned char *data, 6644 unsigned int sz) 6645 { 6646 vpx_codec_err_t res; 6647 struct bool_decoder bool; 6648 int i, row, partition; 6650 ctx->saved_entropy_valid = 0; 6652 if ((res = vp8_parse_frame_header(data, sz, &ctx->frame_hdr))) 6653 vpx_internal_error(&ctx->error, res, 6654 "Failed to parse frame header"); 6656 if (ctx->frame_hdr.is_experimental) 6657 vpx_internal_error(&ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6658 "Experimental bitstreams not supported."); 6660 data += FRAME_HEADER_SZ; 6661 sz -= FRAME_HEADER_SZ; 6662 if (ctx->frame_hdr.is_keyframe) 6663 { 6664 data += KEYFRAME_HEADER_SZ; 6665 sz -= KEYFRAME_HEADER_SZ; 6666 ctx->mb_cols = (ctx->frame_hdr.kf.w + 15) / 16; 6667 ctx->mb_rows = (ctx->frame_hdr.kf.h + 15) / 16; 6668 } 6670 /* Start the bitreader for the header/entropy partition */ 6671 init_bool_decoder(&bool, data, ctx->frame_hdr.part0_sz); 6673 /* Skip the colorspace and clamping bits */ 6674 if (ctx->frame_hdr.is_keyframe) 6675 if (bool_get_uint(&bool, 2)) 6676 vpx_internal_error( 6677 &ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6678 "Reserved bits not supported."); 6680 decode_segmentation_header(ctx, &bool, &ctx->segment_hdr); 6681 decode_loopfilter_header(ctx, &bool, &ctx->loopfilter_hdr); 6682 decode_and_init_token_partitions(ctx, 6683 &bool, 6684 data + ctx->frame_hdr.part0_sz, 6685 sz - ctx->frame_hdr.part0_sz, 6686 &ctx->token_hdr); 6687 decode_quantizer_header(ctx, &bool, &ctx->quant_hdr); 6688 decode_reference_header(ctx, &bool, &ctx->reference_hdr); 6690 /* Set keyframe entropy defaults. These get updated on keyframes 6691 * regardless of the refresh_entropy setting. 6692 */ 6693 if (ctx->frame_hdr.is_keyframe) 6694 { 6695 ARRAY_COPY(ctx->entropy_hdr.coeff_probs, 6696 k_default_coeff_probs); 6697 ARRAY_COPY(ctx->entropy_hdr.mv_probs, 6698 k_default_mv_probs); 6699 ARRAY_COPY(ctx->entropy_hdr.y_mode_probs, 6700 k_default_y_mode_probs); 6701 ARRAY_COPY(ctx->entropy_hdr.uv_mode_probs, 6702 k_default_uv_mode_probs); 6703 } 6705 if (!ctx->reference_hdr.refresh_entropy) 6706 { 6707 ctx->saved_entropy = ctx->entropy_hdr; 6708 ctx->saved_entropy_valid = 1; 6709 } 6710 decode_entropy_header(ctx, &bool, &ctx->entropy_hdr); 6712 vp8_dixie_modemv_init(ctx); 6713 vp8_dixie_tokens_init(ctx); 6714 vp8_dixie_predict_init(ctx); 6715 dequant_init(ctx->dequant_factors, &ctx->segment_hdr, 6716 &ctx->quant_hdr); 6718 for (row = 0, partition = 0; row < ctx->mb_rows; row++) 6719 { 6720 vp8_dixie_modemv_process_row( 6721 ctx, &bool, row, 0, ctx->mb_cols); 6722 vp8_dixie_tokens_process_row(ctx, partition, row, 0, 6723 ctx->mb_cols); 6724 vp8_dixie_predict_process_row(ctx, row, 0, ctx->mb_cols); 6726 if (ctx->loopfilter_hdr.level && row) 6727 vp8_dixie_loopfilter_process_row(ctx, row - 1, 0, 6728 ctx->mb_cols); 6730 if (++partition == ctx->token_hdr.partitions) 6731 partition = 0; 6732 } 6734 if (ctx->loopfilter_hdr.level) 6735 vp8_dixie_loopfilter_process_row( 6736 ctx, row - 1, 0, ctx->mb_cols); 6738 ctx->frame_cnt++; 6740 if (!ctx->reference_hdr.refresh_entropy) 6741 { 6742 ctx->entropy_hdr = ctx->saved_entropy; 6743 ctx->saved_entropy_valid = 0; 6744 } 6746 /* Handle reference frame updates */ 6747 if (ctx->reference_hdr.copy_arf == 1) 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[LAST_FRAME]); 6752 } 6753 else if (ctx->reference_hdr.copy_arf == 2) 6754 { 6755 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6756 ctx->ref_frames[ALTREF_FRAME] = 6757 vp8_dixie_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6759 } 6761 if (ctx->reference_hdr.copy_gf == 1) 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[LAST_FRAME]); 6766 } 6767 else if (ctx->reference_hdr.copy_gf == 2) 6768 { 6769 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6770 ctx->ref_frames[GOLDEN_FRAME] = 6771 vp8_dixie_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6772 } 6774 if (ctx->reference_hdr.refresh_gf) 6775 { 6776 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6777 ctx->ref_frames[GOLDEN_FRAME] = 6778 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6779 } 6781 if (ctx->reference_hdr.refresh_arf) 6782 { 6783 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6784 ctx->ref_frames[ALTREF_FRAME] = 6785 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6786 } 6788 if (ctx->reference_hdr.refresh_last) 6789 { 6790 vp8_dixie_release_ref_frame(ctx->ref_frames[LAST_FRAME]); 6791 ctx->ref_frames[LAST_FRAME] = 6792 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6793 } 6795 } 6797 void 6798 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx) 6799 { 6800 dequant_global_init(ctx->dequant_factors); 6801 } 6803 #define CHECK_FOR_UPDATE(lval,rval,update_flag) do {\ 6804 unsigned int old = lval; \ 6805 update_flag |= (old != (lval = rval)); \ 6806 } while(0) 6808 vpx_codec_err_t 6809 vp8_parse_frame_header(const unsigned char *data, 6810 unsigned int sz, 6811 struct vp8_frame_hdr *hdr) 6812 { 6813 unsigned long raw; 6815 if (sz < 10) 6816 return VPX_CODEC_CORRUPT_FRAME; 6818 /* The frame header is defined as a three byte little endian 6819 * value 6820 */ 6821 raw = data[0] | (data[1] << 8) | (data[2] << 16); 6822 hdr->is_keyframe = !BITS_GET(raw, 0, 1); 6823 hdr->version = BITS_GET(raw, 1, 2); 6824 hdr->is_experimental = BITS_GET(raw, 3, 1); 6825 hdr->is_shown = BITS_GET(raw, 4, 1); 6826 hdr->part0_sz = BITS_GET(raw, 5, 19); 6828 if (sz <= hdr->part0_sz + (hdr->is_keyframe ? 10 : 3)) 6829 return VPX_CODEC_CORRUPT_FRAME; 6831 hdr->frame_size_updated = 0; 6833 if (hdr->is_keyframe) 6834 { 6835 unsigned int update = 0; 6837 /* Keyframe header consists of a three byte sync code 6838 * followed by the width and height and associated scaling 6839 * factors. 6840 */ 6841 if (data[3] != 0x9d || data[4] != 0x01 || data[5] != 0x2a) 6842 return VPX_CODEC_UNSUP_BITSTREAM; 6844 raw = data[6] | (data[7] << 8) 6845 | (data[8] << 16) | (data[9] << 24); 6846 CHECK_FOR_UPDATE(hdr->kf.w, BITS_GET(raw, 0, 14), 6847 update); 6848 CHECK_FOR_UPDATE(hdr->kf.scale_w, BITS_GET(raw, 14, 2), 6849 update); 6850 CHECK_FOR_UPDATE(hdr->kf.h, BITS_GET(raw, 16, 14), 6851 update); 6852 CHECK_FOR_UPDATE(hdr->kf.scale_h, BITS_GET(raw, 30, 2), 6853 update); 6855 hdr->frame_size_updated = update; 6857 if (!hdr->kf.w || !hdr->kf.h) 6858 return VPX_CODEC_UNSUP_BITSTREAM; 6859 } 6861 return VPX_CODEC_OK; 6862 } 6864 vpx_codec_err_t 6865 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 6866 const unsigned char *data, 6867 unsigned int sz) 6868 { 6869 volatile struct vp8_decoder_ctx *ctx_ = ctx; 6871 ctx->error.error_code = VPX_CODEC_OK; 6872 ctx->error.has_detail = 0; 6874 if (!setjmp(ctx->error.jmp)) 6875 decode_frame(ctx, data, sz); 6877 return ctx_->error.error_code; 6878 } 6880 void 6881 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx) 6882 { 6883 vp8_dixie_predict_destroy(ctx); 6884 vp8_dixie_tokens_destroy(ctx); 6885 vp8_dixie_modemv_destroy(ctx); 6886 } 6888 ---- End code block ---------------------------------------- 6890 20.5. dixie.h 6892 ---- Begin code block -------------------------------------- 6894 /* 6895 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6896 * 6897 * Use of this source code is governed by a BSD-style license 6898 * that can be found in the LICENSE file in the root of the source 6899 * tree. An additional intellectual property rights grant can be 6900 * found in the file PATENTS. All contributing project authors may 6901 * be found in the AUTHORS file in the root of the source tree. 6902 */ 6903 #ifndef DIXIE_H 6904 #define DIXIE_H 6905 #include "vpx_codec_internal.h" 6906 #include "bool_decoder.h" 6908 struct vp8_frame_hdr 6909 { 6910 unsigned int is_keyframe; /* Frame is a keyframe */ 6911 unsigned int is_experimental; /* Frame is a keyframe */ 6912 unsigned int version; /* Bitstream version */ 6913 unsigned int is_shown; /* Frame is to be displayed. */ 6914 unsigned int part0_sz; /* Partition 0 length, in bytes */ 6916 struct vp8_kf_hdr 6917 { 6918 unsigned int w; /* Width */ 6919 unsigned int h; /* Height */ 6920 unsigned int scale_w; /* Scaling factor, Width */ 6921 unsigned int scale_h; /* Scaling factor, Height */ 6922 } kf; 6924 unsigned int frame_size_updated; /* Flag to indicate a resolution 6925 * update. 6926 */ 6927 }; 6929 enum 6930 { 6931 MB_FEATURE_TREE_PROBS = 3, 6932 MAX_MB_SEGMENTS = 4 6933 }; 6935 struct vp8_segment_hdr 6936 { 6937 unsigned int enabled; 6938 unsigned int update_data; 6939 unsigned int update_map; 6940 unsigned int abs; /* 0=deltas, 1=absolute values */ 6941 unsigned int tree_probs[MB_FEATURE_TREE_PROBS]; 6942 int lf_level[MAX_MB_SEGMENTS]; 6943 int quant_idx[MAX_MB_SEGMENTS]; 6945 }; 6947 enum 6948 { 6949 BLOCK_CONTEXTS = 4 6950 }; 6952 struct vp8_loopfilter_hdr 6953 { 6954 unsigned int use_simple; 6955 unsigned int level; 6956 unsigned int sharpness; 6957 unsigned int delta_enabled; 6958 int ref_delta[BLOCK_CONTEXTS]; 6959 int mode_delta[BLOCK_CONTEXTS]; 6960 }; 6962 enum 6963 { 6964 MAX_PARTITIONS = 8 6965 }; 6967 struct vp8_token_hdr 6968 { 6969 unsigned int partitions; 6970 unsigned int partition_sz[MAX_PARTITIONS]; 6971 }; 6973 struct vp8_quant_hdr 6974 { 6975 unsigned int q_index; 6976 int delta_update; 6977 int y1_dc_delta_q; 6978 int y2_dc_delta_q; 6979 int y2_ac_delta_q; 6980 int uv_dc_delta_q; 6981 int uv_ac_delta_q; 6982 }; 6984 struct vp8_reference_hdr 6985 { 6986 unsigned int refresh_last; 6987 unsigned int refresh_gf; 6988 unsigned int refresh_arf; 6989 unsigned int copy_gf; 6990 unsigned int copy_arf; 6991 unsigned int sign_bias[4]; 6992 unsigned int refresh_entropy; 6993 }; 6995 enum 6996 { 6997 BLOCK_TYPES = 4, 6998 PREV_COEF_CONTEXTS = 3, 6999 COEF_BANDS = 8, 7000 ENTROPY_NODES = 11, 7001 }; 7002 typedef unsigned char coeff_probs_table_t[BLOCK_TYPES][COEF_BANDS] 7003 [PREV_COEF_CONTEXTS] 7004 [ENTROPY_NODES]; 7006 enum 7007 { 7008 MV_PROB_CNT = 2 + 8 - 1 + 10 /* from entropymv.h */ 7009 }; 7010 typedef unsigned char mv_component_probs_t[MV_PROB_CNT]; 7012 struct vp8_entropy_hdr 7013 { 7014 coeff_probs_table_t coeff_probs; 7015 mv_component_probs_t mv_probs[2]; 7016 unsigned int coeff_skip_enabled; 7017 unsigned char coeff_skip_prob; 7018 unsigned char y_mode_probs[4]; 7019 unsigned char uv_mode_probs[3]; 7020 unsigned char prob_inter; 7021 unsigned char prob_last; 7022 unsigned char prob_gf; 7023 }; 7025 enum reference_frame 7026 { 7027 CURRENT_FRAME, 7028 LAST_FRAME, 7029 GOLDEN_FRAME, 7030 ALTREF_FRAME, 7031 NUM_REF_FRAMES 7033 }; 7035 enum prediction_mode 7036 { 7037 /* 16x16 intra modes */ 7038 DC_PRED, V_PRED, H_PRED, TM_PRED, B_PRED, 7040 /* 16x16 inter modes */ 7041 NEARESTMV, NEARMV, ZEROMV, NEWMV, SPLITMV, 7043 MB_MODE_COUNT, 7045 /* 4x4 intra modes */ 7046 B_DC_PRED = 0, B_TM_PRED, B_VE_PRED, B_HE_PRED, B_LD_PRED, 7047 B_RD_PRED, B_VR_PRED, B_VL_PRED, B_HD_PRED, B_HU_PRED, 7049 /* 4x4 inter modes */ 7050 LEFT4X4, ABOVE4X4, ZERO4X4, NEW4X4, 7052 B_MODE_COUNT 7053 }; 7055 enum splitmv_partitioning 7056 { 7057 SPLITMV_16X8, 7058 SPLITMV_8X16, 7059 SPLITMV_8X8, 7060 SPLITMV_4X4 7061 }; 7063 typedef short filter_t[6]; 7065 typedef union mv 7066 { 7067 struct 7068 { 7069 int16_t x, y; 7070 } d; 7071 uint32_t raw; 7072 } mv_t; 7074 struct mb_base_info 7075 { 7076 unsigned char y_mode : 4; 7077 unsigned char uv_mode : 4; 7078 unsigned char segment_id : 2; 7079 unsigned char ref_frame : 2; 7080 unsigned char skip_coeff : 1; 7081 unsigned char need_mc_border : 1; 7082 enum splitmv_partitioning partitioning : 2; 7083 union mv mv; 7084 unsigned int eob_mask; 7085 }; 7087 struct mb_info 7088 { 7089 struct mb_base_info base; 7090 union 7091 { 7092 union mv mvs[16]; 7093 enum prediction_mode modes[16]; 7094 } split; 7095 }; 7097 /* A "token entropy context" has 4 Y values, 2 U, 2 V, and 1 Y2 */ 7098 typedef int token_entropy_ctx_t[4 + 2 + 2 + 1]; 7100 struct token_decoder 7101 { 7102 struct bool_decoder bool; 7103 token_entropy_ctx_t left_token_entropy_ctx; 7104 short *coeffs; 7105 }; 7107 enum token_block_type 7108 { 7109 TOKEN_BLOCK_Y1, 7110 TOKEN_BLOCK_UV, 7111 TOKEN_BLOCK_Y2, 7112 TOKEN_BLOCK_TYPES, 7113 }; 7115 struct dequant_factors 7116 { 7117 int quant_idx; 7118 short factor[TOKEN_BLOCK_TYPES][2]; /* [ Y1, UV, Y2 ] 7119 * [ DC, AC ] */ 7120 }; 7121 struct ref_cnt_img 7122 { 7123 vpx_image_t img; 7124 unsigned int ref_cnt; 7125 }; 7127 struct vp8_decoder_ctx 7128 { 7129 struct vpx_internal_error_info error; 7130 unsigned int frame_cnt; 7132 struct vp8_frame_hdr frame_hdr; 7133 struct vp8_segment_hdr segment_hdr; 7134 struct vp8_loopfilter_hdr loopfilter_hdr; 7135 struct vp8_token_hdr token_hdr; 7136 struct vp8_quant_hdr quant_hdr; 7137 struct vp8_reference_hdr reference_hdr; 7138 struct vp8_entropy_hdr entropy_hdr; 7140 struct vp8_entropy_hdr saved_entropy; 7141 unsigned int saved_entropy_valid; 7143 unsigned int mb_rows; 7144 unsigned int mb_cols; 7145 struct mb_info *mb_info_storage; 7146 struct mb_info **mb_info_rows_storage; 7147 struct mb_info **mb_info_rows; 7149 token_entropy_ctx_t *above_token_entropy_ctx; 7150 struct token_decoder tokens[MAX_PARTITIONS]; 7151 struct dequant_factors dequant_factors[MAX_MB_SEGMENTS]; 7153 struct ref_cnt_img frame_strg[NUM_REF_FRAMES]; 7154 struct ref_cnt_img *ref_frames[NUM_REF_FRAMES]; 7155 ptrdiff_t ref_frame_offsets[4]; 7157 const filter_t *subpixel_filters; 7158 }; 7160 void 7161 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx); 7163 void 7164 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx); 7165 vpx_codec_err_t 7166 vp8_parse_frame_header(const unsigned char *data, 7167 unsigned int sz, 7168 struct vp8_frame_hdr *hdr); 7170 vpx_codec_err_t 7171 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 7172 const unsigned char *data, 7173 unsigned int sz); 7175 #define CLAMP_255(x) ((x)<0?0:((x)>255?255:(x))) 7177 #endif 7179 ---- End code block ---------------------------------------- 7181 20.6. dixie_loopfilter.c 7183 ---- Begin code block -------------------------------------- 7185 /* 7186 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7187 * 7188 * Use of this source code is governed by a BSD-style license 7189 * that can be found in the LICENSE file in the root of the source 7190 * tree. An additional intellectual property rights grant can be 7191 * found in the file PATENTS. All contributing project authors may 7192 * be found in the AUTHORS file in the root of the source tree. 7193 */ 7194 #include "dixie.h" 7195 #include "dixie_loopfilter.h" 7197 #define ABS(x) ((x) >= 0 ? (x) : -(x)) 7199 #define p3 pixels[-4*stride] 7200 #define p2 pixels[-3*stride] 7201 #define p1 pixels[-2*stride] 7202 #define p0 pixels[-1*stride] 7203 #define q0 pixels[ 0*stride] 7204 #define q1 pixels[ 1*stride] 7205 #define q2 pixels[ 2*stride] 7206 #define q3 pixels[ 3*stride] 7208 #define static 7209 static int 7210 saturate_int8(int x) 7211 { 7212 if (x < -128) 7213 return -128; 7215 if (x > 127) 7216 return 127; 7218 return x; 7219 } 7221 static int 7222 saturate_uint8(int x) 7223 { 7224 if (x < 0) 7225 return 0; 7227 if (x > 255) 7228 return 255; 7230 return x; 7231 } 7233 static int 7234 high_edge_variance(unsigned char *pixels, 7235 int stride, 7236 int hev_threshold) 7237 { 7238 return ABS(p1 - p0) > hev_threshold || 7239 ABS(q1 - q0) > hev_threshold; 7240 } 7242 static int 7243 simple_threshold(unsigned char *pixels, 7244 int stride, 7245 int filter_limit) 7246 { 7247 return (ABS(p0 - q0) * 2 + (ABS(p1 - q1) >> 1)) <= filter_limit; 7248 } 7250 static int 7251 normal_threshold(unsigned char *pixels, 7252 int stride, 7253 int edge_limit, 7254 int interior_limit) 7255 { 7256 int E = edge_limit; 7257 int I = interior_limit; 7259 return simple_threshold(pixels, stride, 2 * E + I) 7260 && ABS(p3 - p2) <= I && ABS(p2 - p1) <= I 7261 && ABS(p1 - p0) <= I && ABS(q3 - q2) <= I 7262 && ABS(q2 - q1) <= I && ABS(q1 - q0) <= I; 7263 } 7265 static void 7266 filter_common(unsigned char *pixels, 7267 int stride, 7268 int use_outer_taps) 7269 { 7270 int a, f1, f2; 7272 a = 3 * (q0 - p0); 7274 if (use_outer_taps) 7275 a += saturate_int8(p1 - q1); 7277 a = saturate_int8(a); 7279 f1 = ((a + 4 > 127) ? 127 : a + 4) >> 3; 7280 f2 = ((a + 3 > 127) ? 127 : a + 3) >> 3; 7282 p0 = saturate_uint8(p0 + f2); 7283 q0 = saturate_uint8(q0 - f1); 7285 if (!use_outer_taps) 7286 { 7287 /* This handles the case of subblock_filter() 7288 * (from the bitstream guide. 7289 */ 7290 a = (f1 + 1) >> 1; 7291 p1 = saturate_uint8(p1 + a); 7292 q1 = saturate_uint8(q1 - a); 7293 } 7294 } 7296 static void 7297 filter_mb_edge(unsigned char *pixels, 7298 int stride) 7299 { 7300 int w, a; 7302 w = saturate_int8(saturate_int8(p1 - q1) + 3 * (q0 - p0)); 7304 a = (27 * w + 63) >> 7; 7305 p0 = saturate_uint8(p0 + a); 7306 q0 = saturate_uint8(q0 - a); 7308 a = (18 * w + 63) >> 7; 7309 p1 = saturate_uint8(p1 + a); 7310 q1 = saturate_uint8(q1 - a); 7312 a = (9 * w + 63) >> 7; 7313 p2 = saturate_uint8(p2 + a); 7314 q2 = saturate_uint8(q2 - a); 7316 } 7318 static void 7319 filter_mb_v_edge(unsigned char *src, 7320 int stride, 7321 int edge_limit, 7322 int interior_limit, 7323 int hev_threshold, 7324 int size) 7325 { 7326 int i; 7328 for (i = 0; i < 8 * size; i++) 7329 { 7330 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7331 { 7332 if (high_edge_variance(src, 1, hev_threshold)) 7333 filter_common(src, 1, 1); 7334 else 7335 filter_mb_edge(src, 1); 7336 } 7338 src += stride; 7339 } 7340 } 7342 static void 7343 filter_subblock_v_edge(unsigned char *src, 7344 int stride, 7345 int edge_limit, 7346 int interior_limit, 7347 int hev_threshold, 7348 int size) 7349 { 7350 int i; 7352 for (i = 0; i < 8 * size; i++) 7353 { 7354 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7355 filter_common(src, 1, 7356 high_edge_variance(src, 1, hev_threshold)); 7358 src += stride; 7359 } 7360 } 7362 static void 7363 filter_mb_h_edge(unsigned char *src, 7364 int stride, 7365 int edge_limit, 7366 int interior_limit, 7367 int hev_threshold, 7368 int size) 7369 { 7370 int i; 7372 for (i = 0; i < 8 * size; i++) 7373 { 7374 if (normal_threshold(src, stride, edge_limit, interior_limit)) 7375 { 7376 if (high_edge_variance(src, stride, hev_threshold)) 7377 filter_common(src, stride, 1); 7378 else 7379 filter_mb_edge(src, stride); 7380 } 7382 src += 1; 7383 } 7384 } 7386 static void 7387 filter_subblock_h_edge(unsigned char *src, 7388 int stride, 7389 int edge_limit, 7390 int interior_limit, 7391 int hev_threshold, 7392 int size) 7393 { 7394 int i; 7396 for (i = 0; i < 8 * size; i++) 7397 { 7398 if (normal_threshold(src, stride, edge_limit, interior_limit)) 7399 filter_common(src, stride, 7400 high_edge_variance(src, stride, 7401 hev_threshold)); 7403 src += 1; 7404 } 7405 } 7407 static void 7408 filter_v_edge_simple(unsigned char *src, 7409 int stride, 7410 int filter_limit) 7411 { 7412 int i; 7414 for (i = 0; i < 16; i++) 7415 { 7416 if (simple_threshold(src, 1, filter_limit)) 7417 filter_common(src, 1, 1); 7419 src += stride; 7420 } 7421 } 7423 static void 7424 filter_h_edge_simple(unsigned char *src, 7425 int stride, 7426 int filter_limit) 7427 { 7428 int i; 7430 for (i = 0; i < 16; i++) 7431 { 7432 if (simple_threshold(src, stride, filter_limit)) 7433 filter_common(src, stride, 1); 7435 src += 1; 7436 } 7437 } 7438 static void 7439 calculate_filter_parameters(struct vp8_decoder_ctx *ctx, 7440 struct mb_info *mbi, 7441 int *edge_limit_, 7442 int *interior_limit_, 7443 int *hev_threshold_) 7444 { 7445 int filter_level, interior_limit, hev_threshold; 7447 /* Reference code/spec seems to conflate filter_level and 7448 * edge_limit 7449 */ 7451 filter_level = ctx->loopfilter_hdr.level; 7453 if (ctx->segment_hdr.enabled) 7454 { 7455 if (!ctx->segment_hdr.abs) 7456 filter_level += 7457 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7458 else 7459 filter_level = 7460 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7461 } 7463 if (ctx->loopfilter_hdr.delta_enabled) 7464 { 7465 filter_level += 7466 ctx->loopfilter_hdr.ref_delta[mbi->base.ref_frame]; 7468 if (mbi->base.ref_frame == CURRENT_FRAME) 7469 { 7470 if (mbi->base.y_mode == B_PRED) 7471 filter_level += ctx->loopfilter_hdr.mode_delta[0]; 7472 } 7473 else if (mbi->base.y_mode == ZEROMV) 7474 filter_level += ctx->loopfilter_hdr.mode_delta[1]; 7475 else if (mbi->base.y_mode == SPLITMV) 7476 filter_level += ctx->loopfilter_hdr.mode_delta[3]; 7477 else 7478 filter_level += ctx->loopfilter_hdr.mode_delta[2]; 7479 } 7481 if (filter_level > 63) 7482 filter_level = 63; 7483 else if (filter_level < 0) 7484 filter_level = 0; 7486 interior_limit = filter_level; 7488 if (ctx->loopfilter_hdr.sharpness) 7489 { 7490 interior_limit >>= ctx->loopfilter_hdr.sharpness > 4 ? 2 : 1; 7492 if (interior_limit > 9 - ctx->loopfilter_hdr.sharpness) 7493 interior_limit = 9 - ctx->loopfilter_hdr.sharpness; 7494 } 7496 if (interior_limit < 1) 7497 interior_limit = 1; 7499 hev_threshold = (filter_level >= 15); 7501 if (filter_level >= 40) 7502 hev_threshold++; 7504 if (filter_level >= 20 && !ctx->frame_hdr.is_keyframe) 7505 hev_threshold++; 7507 *edge_limit_ = filter_level; 7508 *interior_limit_ = interior_limit; 7509 *hev_threshold_ = hev_threshold; 7510 } 7512 static void 7513 filter_row_normal(struct vp8_decoder_ctx *ctx, 7514 unsigned int row, 7515 unsigned int start_col, 7516 unsigned int num_cols) 7517 { 7518 unsigned char *y, *u, *v; 7519 int stride, uv_stride; 7520 struct mb_info *mbi; 7521 unsigned int col; 7523 /* Adjust pointers based on row, start_col */ 7524 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7525 uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 7526 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7527 u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 7528 v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 7529 y += (stride * row + start_col) * 16; 7530 u += (uv_stride * row + start_col) * 8; 7531 v += (uv_stride * row + start_col) * 8; 7532 mbi = ctx->mb_info_rows[row] + start_col; 7533 for (col = start_col; col < start_col + num_cols; col++) 7534 { 7535 int edge_limit, interior_limit, hev_threshold; 7537 /* TODO: only need to recalculate every MB if segmentation is 7538 * enabled. 7539 */ 7540 calculate_filter_parameters(ctx, mbi, &edge_limit, 7541 &interior_limit, &hev_threshold); 7543 if (edge_limit) 7544 { 7545 if (col) 7546 { 7547 filter_mb_v_edge(y, stride, edge_limit + 2, 7548 interior_limit, hev_threshold, 2); 7549 filter_mb_v_edge(u, uv_stride, edge_limit + 2, 7550 interior_limit, hev_threshold, 1); 7551 filter_mb_v_edge(v, uv_stride, edge_limit + 2, 7552 interior_limit, hev_threshold, 1); 7553 } 7555 /* NOTE: This conditional is actually dependent on the 7556 * number of coefficients decoded, not the skip flag as 7557 * coded in the bitstream. The tokens task is expected to 7558 * set 31 if there is *any* non-zero data. 7559 */ 7560 if (mbi->base.eob_mask 7561 || mbi->base.y_mode == SPLITMV 7562 || mbi->base.y_mode == B_PRED) 7563 { 7564 filter_subblock_v_edge(y + 4, stride, edge_limit, 7565 interior_limit, hev_threshold, 7566 2); 7567 filter_subblock_v_edge(y + 8, stride, edge_limit, 7568 interior_limit, hev_threshold, 7569 2); 7570 filter_subblock_v_edge(y + 12, stride, edge_limit, 7571 interior_limit, hev_threshold, 7572 2); 7573 filter_subblock_v_edge(u + 4, uv_stride, edge_limit, 7574 interior_limit, hev_threshold, 7575 1); 7576 filter_subblock_v_edge(v + 4, uv_stride, edge_limit, 7577 interior_limit, hev_threshold, 7578 1); 7579 } 7580 if (row) 7581 { 7582 filter_mb_h_edge(y, stride, edge_limit + 2, 7583 interior_limit, hev_threshold, 2); 7584 filter_mb_h_edge(u, uv_stride, edge_limit + 2, 7585 interior_limit, hev_threshold, 1); 7586 filter_mb_h_edge(v, uv_stride, edge_limit + 2, 7587 interior_limit, hev_threshold, 1); 7588 } 7590 if (mbi->base.eob_mask 7591 || mbi->base.y_mode == SPLITMV 7592 || mbi->base.y_mode == B_PRED) 7593 { 7594 filter_subblock_h_edge(y + 4 * stride, stride, 7595 edge_limit, interior_limit, 7596 hev_threshold, 2); 7597 filter_subblock_h_edge(y + 8 * stride, stride, 7598 edge_limit, interior_limit, 7599 hev_threshold, 2); 7600 filter_subblock_h_edge(y + 12 * stride, stride, 7601 edge_limit, interior_limit, 7602 hev_threshold, 2); 7603 filter_subblock_h_edge(u + 4 * uv_stride, uv_stride, 7604 edge_limit, interior_limit, 7605 hev_threshold, 1); 7606 filter_subblock_h_edge(v + 4 * uv_stride, uv_stride, 7607 edge_limit, interior_limit, 7608 hev_threshold, 1); 7609 } 7610 } 7612 y += 16; 7613 u += 8; 7614 v += 8; 7615 mbi++; 7616 } 7617 } 7619 static void 7620 filter_row_simple(struct vp8_decoder_ctx *ctx, 7621 unsigned int row, 7622 unsigned int start_col, 7623 unsigned int num_cols) 7624 { 7625 unsigned char *y; 7626 int stride; 7627 struct mb_info *mbi; 7628 unsigned int col; 7630 /* Adjust pointers based on row, start_col */ 7631 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7632 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7633 y += (stride * row + start_col) * 16; 7634 mbi = ctx->mb_info_rows[row] + start_col; 7636 for (col = start_col; col < start_col + num_cols; col++) 7637 { 7638 int edge_limit, interior_limit, hev_threshold; 7640 /* TODO: only need to recalculate every MB if segmentation is 7641 * enabled. 7642 */ 7643 calculate_filter_parameters(ctx, mbi, &edge_limit, 7644 &interior_limit, &hev_threshold); 7646 if (edge_limit) 7647 { 7649 /* NOTE: This conditional is actually dependent on the 7650 * number of coefficients decoded, not the skip flag as 7651 * coded in the bitstream. The tokens task is expected to 7652 * set 31 if there is *any* non-zero data. 7653 */ 7654 int filter_subblocks = (mbi->base.eob_mask 7655 || mbi->base.y_mode == SPLITMV 7656 || mbi->base.y_mode == B_PRED); 7657 int mb_limit = (edge_limit + 2) * 2 + interior_limit; 7658 int b_limit = edge_limit * 2 + interior_limit; 7660 if (col) 7661 filter_v_edge_simple(y, stride, mb_limit); 7663 if (filter_subblocks) 7664 { 7665 filter_v_edge_simple(y + 4, stride, b_limit); 7666 filter_v_edge_simple(y + 8, stride, b_limit); 7667 filter_v_edge_simple(y + 12, stride, b_limit); 7668 } 7670 if (row) 7671 filter_h_edge_simple(y, stride, mb_limit); 7673 if (filter_subblocks) 7674 { 7675 filter_h_edge_simple(y + 4 * stride, stride, b_limit); 7676 filter_h_edge_simple(y + 8 * stride, stride, b_limit); 7677 filter_h_edge_simple(y + 12 * stride, stride, b_limit); 7678 } 7679 } 7681 y += 16; 7682 mbi++; 7683 } 7684 } 7686 void 7687 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7688 unsigned int row, 7689 unsigned int start_col, 7690 unsigned int num_cols) 7691 { 7692 if (ctx->loopfilter_hdr.use_simple) 7693 filter_row_simple(ctx, row, start_col, num_cols); 7694 else 7695 filter_row_normal(ctx, row, start_col, num_cols); 7696 } 7698 ---- End code block ---------------------------------------- 7700 20.7. dixie_loopfilter.h 7702 ---- Begin code block -------------------------------------- 7704 /* 7705 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7706 * 7707 * Use of this source code is governed by a BSD-style license 7708 * that can be found in the LICENSE file in the root of the source 7709 * tree. An additional intellectual property rights grant can be 7710 * found in the file PATENTS. All contributing project authors may 7711 * be found in the AUTHORS file in the root of the source tree. 7712 */ 7713 #ifndef DIXIE_LOOPFILTER_H 7714 #define DIXIE_LOOPFILTER_H 7716 void 7717 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7718 unsigned int row, 7719 unsigned int start_col, 7720 unsigned int num_cols); 7722 #endif 7724 ---- End code block ---------------------------------------- 7726 20.8. idct_add.c 7728 ---- Begin code block -------------------------------------- 7730 /* 7731 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7732 * 7733 * Use of this source code is governed by a BSD-style license 7734 * that can be found in the LICENSE file in the root of the source 7735 * tree. An additional intellectual property rights grant can be 7736 * found in the file PATENTS. All contributing project authors may 7737 * be found in the AUTHORS file in the root of the source tree. 7738 */ 7739 #include "dixie.h" 7740 #include "idct_add.h" 7741 #include 7743 void 7744 vp8_dixie_walsh(const short *input, short *output) 7745 { 7746 int i; 7747 int a1, b1, c1, d1; 7748 int a2, b2, c2, d2; 7749 const short *ip = input; 7750 short *op = output; 7752 for (i = 0; i < 4; i++) 7753 { 7754 a1 = ip[0] + ip[12]; 7755 b1 = ip[4] + ip[8]; 7756 c1 = ip[4] - ip[8]; 7757 d1 = ip[0] - ip[12]; 7759 op[0] = a1 + b1; 7760 op[4] = c1 + d1; 7761 op[8] = a1 - b1; 7762 op[12] = d1 - c1; 7763 ip++; 7764 op++; 7765 } 7767 ip = output; 7768 op = output; 7770 for (i = 0; i < 4; i++) 7771 { 7772 a1 = ip[0] + ip[3]; 7773 b1 = ip[1] + ip[2]; 7774 c1 = ip[1] - ip[2]; 7775 d1 = ip[0] - ip[3]; 7777 a2 = a1 + b1; 7778 b2 = c1 + d1; 7779 c2 = a1 - b1; 7780 d2 = d1 - c1; 7782 op[0] = (a2 + 3) >> 3; 7783 op[1] = (b2 + 3) >> 3; 7784 op[2] = (c2 + 3) >> 3; 7785 op[3] = (d2 + 3) >> 3; 7787 ip += 4; 7788 op += 4; 7789 } 7790 } 7792 #define cospi8sqrt2minus1 20091 7793 #define sinpi8sqrt2 35468 7794 #define rounding 0 7795 static void 7796 idct_columns(const short *input, short *output) 7797 { 7798 int i; 7799 int a1, b1, c1, d1; 7801 const short *ip = input; 7802 short *op = output; 7803 int temp1, temp2; 7804 int shortpitch = 4; 7806 for (i = 0; i < 4; i++) 7807 { 7808 a1 = ip[0] + ip[8]; 7809 b1 = ip[0] - ip[8]; 7811 temp1 = (ip[4] * sinpi8sqrt2 + rounding) >> 16; 7812 temp2 = ip[12] + 7813 ((ip[12] * cospi8sqrt2minus1 + rounding) >> 16); 7814 c1 = temp1 - temp2; 7816 temp1 = ip[4] + 7817 ((ip[4] * cospi8sqrt2minus1 + rounding) >> 16); 7818 temp2 = (ip[12] * sinpi8sqrt2 + rounding) >> 16; 7819 d1 = temp1 + temp2; 7821 op[shortpitch*0] = a1 + d1; 7822 op[shortpitch*3] = a1 - d1; 7824 op[shortpitch*1] = b1 + c1; 7825 op[shortpitch*2] = b1 - c1; 7827 ip++; 7828 op++; 7829 } 7830 } 7832 void 7833 vp8_dixie_idct_add(unsigned char *recon, 7834 const unsigned char *predict, 7835 int stride, 7836 const short *coeffs) 7837 { 7838 int i; 7839 int a1, b1, c1, d1, temp1, temp2; 7840 short tmp[16]; 7841 idct_columns(coeffs, tmp); 7842 coeffs = tmp; 7844 for (i = 0; i < 4; i++) 7845 { 7846 a1 = coeffs[0] + coeffs[2]; 7847 b1 = coeffs[0] - coeffs[2]; 7849 temp1 = (coeffs[1] * sinpi8sqrt2 + rounding) >> 16; 7850 temp2 = coeffs[3] + 7851 ((coeffs[3] * cospi8sqrt2minus1 + rounding) >> 16); 7852 c1 = temp1 - temp2; 7854 temp1 = coeffs[1] + 7855 ((coeffs[1] * cospi8sqrt2minus1 + rounding) >> 16); 7856 temp2 = (coeffs[3] * sinpi8sqrt2 + rounding) >> 16; 7857 d1 = temp1 + temp2; 7859 recon[0] = CLAMP_255(predict[0] + ((a1 + d1 + 4) >> 3)); 7860 recon[3] = CLAMP_255(predict[3] + ((a1 - d1 + 4) >> 3)); 7861 recon[1] = CLAMP_255(predict[1] + ((b1 + c1 + 4) >> 3)); 7862 recon[2] = CLAMP_255(predict[2] + ((b1 - c1 + 4) >> 3)); 7864 coeffs += 4; 7865 recon += stride; 7866 predict += stride; 7867 } 7868 } 7870 ---- End code block ---------------------------------------- 7872 20.9. idct_add.h 7874 ---- Begin code block -------------------------------------- 7876 /* 7877 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7878 * 7879 * Use of this source code is governed by a BSD-style license 7880 * that can be found in the LICENSE file in the root of the source 7881 * tree. An additional intellectual property rights grant can be 7882 * found in the file PATENTS. All contributing project authors may 7883 * be found in the AUTHORS file in the root of the source tree. 7884 */ 7885 #ifndef IDCT_ADD_H 7886 #define IDCT_ADD_H 7888 void 7889 vp8_dixie_idct_add_init(struct vp8_decoder_ctx *ctx); 7891 void 7892 vp8_dixie_idct_add(unsigned char *recon, 7893 const unsigned char *predict, 7894 int stride, 7895 const short *coeffs); 7897 void 7898 vp8_dixie_walsh(const short *in, short *out); 7900 void 7901 vp8_dixie_idct_add_process_row(struct vp8_decoder_ctx *ctx, 7902 short *coeffs, 7903 unsigned int row, 7904 unsigned int start_col, 7905 unsigned int num_cols); 7907 #endif 7909 ---- End code block ---------------------------------------- 7911 20.10. mem.h 7913 ---- Begin code block -------------------------------------- 7915 /* 7916 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7917 * 7918 * Use of this source code is governed by a BSD-style license 7919 * that can be found in the LICENSE file in the root of the source 7920 * tree. An additional intellectual property rights grant can be 7921 * found in the file PATENTS. All contributing project authors may 7922 * be found in the AUTHORS file in the root of the source tree. 7923 */ 7925 #ifndef VPX_PORTS_MEM_H 7926 #define VPX_PORTS_MEM_H 7927 #include "vpx_config.h" 7928 #include "vpx_integer.h" 7930 #if defined(__GNUC__) && __GNUC__ 7931 #define DECLARE_ALIGNED(n,typ,val) typ val __attribute__ \ 7932 ((aligned (n))) 7933 #elif defined(_MSC_VER) 7934 #define DECLARE_ALIGNED(n,typ,val) __declspec(align(n)) typ val 7935 #else 7936 #warning No alignment directives known for this compiler. 7937 #define DECLARE_ALIGNED(n,typ,val) typ val 7938 #endif 7939 #endif 7941 /* Declare an aligned array on the stack, for situations where the 7942 * stack pointer may not have the alignment we expect. Creates an 7943 * array with a modified name, then defines val to be a pointer, and 7944 * aligns that pointer within the array. 7945 */ 7946 #define DECLARE_ALIGNED_ARRAY(a,typ,val,n)\ 7947 typ val##_[(n)+(a)/sizeof(typ)+1];\ 7948 typ *val = (typ*)((((intptr_t)val##_)+(a)-1)&((intptr_t)-(a))) 7950 /* Indicates that the usage of the specified variable has been 7951 * audited to assure that it's safe to use uninitialized. Silences 7952 * 'may be used uninitialized' warnings on gcc. 7953 */ 7954 #if defined(__GNUC__) && __GNUC__ 7955 #define UNINITIALIZED_IS_SAFE(x) x=x 7956 #else 7957 #define UNINITIALIZED_IS_SAFE(x) x 7958 #endif 7959 ---- End code block ---------------------------------------- 7961 20.11. modemv.c 7963 ---- Begin code block -------------------------------------- 7965 /* 7966 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7967 * 7968 * Use of this source code is governed by a BSD-style license 7969 * that can be found in the LICENSE file in the root of the source 7970 * tree. An additional intellectual property rights grant can be 7971 * found in the file PATENTS. All contributing project authors may 7972 * be found in the AUTHORS file in the root of the source tree. 7973 */ 7974 #include "dixie.h" 7975 #include "modemv_data.h" 7976 #include 7977 #include 7979 struct mv_clamp_rect 7980 { 7981 int to_left, to_right, to_top, to_bottom; 7982 }; 7984 static union mv 7985 clamp_mv(union mv raw, const struct mv_clamp_rect *bounds) 7986 { 7987 union mv newmv; 7989 newmv.d.x = (raw.d.x < bounds->to_left) 7990 ? bounds->to_left : raw.d.x; 7991 newmv.d.x = (raw.d.x > bounds->to_right) 7992 ? bounds->to_right : newmv.d.x; 7993 newmv.d.y = (raw.d.y < bounds->to_top) 7994 ? bounds->to_top : raw.d.y; 7995 newmv.d.y = (raw.d.y > bounds->to_bottom) 7996 ? bounds->to_bottom : newmv.d.y; 7997 return newmv; 7998 } 8000 static int 8001 read_segment_id(struct bool_decoder *bool, 8002 struct vp8_segment_hdr *seg) 8003 { 8004 return bool_get(bool, seg->tree_probs[0]) 8005 ? 2 + bool_get(bool, seg->tree_probs[2]) 8006 : bool_get(bool, seg->tree_probs[1]); 8007 } 8009 static enum prediction_mode 8010 above_block_mode(const struct mb_info *this, 8011 const struct mb_info *above, 8012 unsigned int b) 8013 { 8014 if (b < 4) 8015 { 8016 switch (above->base.y_mode) 8017 { 8018 case DC_PRED: 8019 return B_DC_PRED; 8020 case V_PRED: 8021 return B_VE_PRED; 8022 case H_PRED: 8023 return B_HE_PRED; 8024 case TM_PRED: 8025 return B_TM_PRED; 8026 case B_PRED: 8027 return above->split.modes[b+12]; 8028 default: 8029 assert(0); 8030 } 8031 } 8033 return this->split.modes[b-4]; 8034 } 8036 static enum prediction_mode 8037 left_block_mode(const struct mb_info *this, 8038 const struct mb_info *left, 8039 unsigned int b) 8040 { 8041 if (!(b & 3)) 8042 { 8043 switch (left->base.y_mode) 8044 { 8045 case DC_PRED: 8046 return B_DC_PRED; 8047 case V_PRED: 8048 return B_VE_PRED; 8049 case H_PRED: 8051 return B_HE_PRED; 8052 case TM_PRED: 8053 return B_TM_PRED; 8054 case B_PRED: 8055 return left->split.modes[b+3]; 8056 default: 8057 assert(0); 8058 } 8059 } 8061 return this->split.modes[b-1]; 8062 } 8064 static void 8065 decode_kf_mb_mode(struct mb_info *this, 8066 struct mb_info *left, 8067 struct mb_info *above, 8068 struct bool_decoder *bool) 8069 { 8070 int y_mode, uv_mode; 8072 y_mode = bool_read_tree(bool, kf_y_mode_tree, kf_y_mode_probs); 8074 if (y_mode == B_PRED) 8075 { 8076 unsigned int i; 8078 for (i = 0; i < 16; i++) 8079 { 8080 enum prediction_mode a = above_block_mode(this, above, i); 8081 enum prediction_mode l = left_block_mode(this, left, i); 8082 enum prediction_mode b; 8084 b = bool_read_tree(bool, b_mode_tree, 8085 kf_b_mode_probs[a][l]); 8086 this->split.modes[i] = b; 8087 } 8088 } 8090 uv_mode = bool_read_tree(bool, uv_mode_tree, kf_uv_mode_probs); 8092 this->base.y_mode = y_mode; 8093 this->base.uv_mode = uv_mode; 8094 this->base.mv.raw = 0; 8095 this->base.ref_frame = 0; 8096 } 8097 static void 8098 decode_intra_mb_mode(struct mb_info *this, 8099 struct vp8_entropy_hdr *hdr, 8100 struct bool_decoder *bool) 8101 { 8102 /* Like decode_kf_mb_mode, but with probabilities transmitted in the 8103 * bitstream and no context on the above/left block mode. 8104 */ 8105 int y_mode, uv_mode; 8107 y_mode = bool_read_tree(bool, y_mode_tree, hdr->y_mode_probs); 8109 if (y_mode == B_PRED) 8110 { 8111 unsigned int i; 8113 for (i = 0; i < 16; i++) 8114 { 8115 enum prediction_mode b; 8117 b = bool_read_tree(bool, b_mode_tree, default_b_mode_probs); 8118 this->split.modes[i] = b; 8119 } 8120 } 8122 uv_mode = bool_read_tree(bool, uv_mode_tree, hdr->uv_mode_probs); 8124 this->base.y_mode = y_mode; 8125 this->base.uv_mode = uv_mode; 8126 this->base.mv.raw = 0; 8127 this->base.ref_frame = CURRENT_FRAME; 8128 } 8130 static int 8131 read_mv_component(struct bool_decoder *bool, 8132 const unsigned char mvc[MV_PROB_CNT]) 8133 { 8134 enum {IS_SHORT, SIGN, SHORT, BITS = SHORT + 8 - 1, LONG_WIDTH = 10}; 8135 int x = 0; 8137 if (bool_get(bool, mvc[IS_SHORT])) /* Large */ 8138 { 8139 int i = 0; 8141 for (i = 0; i < 3; i++) 8142 x += bool_get(bool, mvc[BITS + i]) << i; 8144 /* Skip bit 3, which is sometimes implicit */ 8145 for (i = LONG_WIDTH - 1; i > 3; i--) 8146 x += bool_get(bool, mvc[BITS + i]) << i; 8148 if (!(x & 0xFFF0) || bool_get(bool, mvc[BITS + 3])) 8149 x += 8; 8150 } 8151 else /* small */ 8152 x = bool_read_tree(bool, small_mv_tree, mvc + SHORT); 8154 if (x && bool_get(bool, mvc[SIGN])) 8155 x = -x; 8157 return x << 1; 8158 } 8160 static mv_t 8161 above_block_mv(const struct mb_info *this, 8162 const struct mb_info *above, 8163 unsigned int b) 8164 { 8165 if (b < 4) 8166 { 8167 if (above->base.y_mode == SPLITMV) 8168 return above->split.mvs[b+12]; 8170 return above->base.mv; 8171 } 8173 return this->split.mvs[b-4]; 8174 } 8176 static mv_t 8177 left_block_mv(const struct mb_info *this, 8178 const struct mb_info *left, 8179 unsigned int b) 8180 { 8181 if (!(b & 3)) 8182 { 8183 if (left->base.y_mode == SPLITMV) 8184 return left->split.mvs[b+3]; 8186 return left->base.mv; 8187 } 8189 return this->split.mvs[b-1]; 8191 } 8193 static enum prediction_mode 8194 submv_ref(struct bool_decoder *bool, union mv l, union mv a) 8195 { 8196 enum subblock_mv_ref 8197 { 8198 SUBMVREF_NORMAL, 8199 SUBMVREF_LEFT_ZED, 8200 SUBMVREF_ABOVE_ZED, 8201 SUBMVREF_LEFT_ABOVE_SAME, 8202 SUBMVREF_LEFT_ABOVE_ZED 8203 }; 8205 int lez = !(l.raw); 8206 int aez = !(a.raw); 8207 int lea = l.raw == a.raw; 8208 enum subblock_mv_ref ctx = SUBMVREF_NORMAL; 8210 if (lea && lez) 8211 ctx = SUBMVREF_LEFT_ABOVE_ZED; 8212 else if (lea) 8213 ctx = SUBMVREF_LEFT_ABOVE_SAME; 8214 else if (aez) 8215 ctx = SUBMVREF_ABOVE_ZED; 8216 else if (lez) 8217 ctx = SUBMVREF_LEFT_ZED; 8219 return bool_read_tree(bool, submv_ref_tree, submv_ref_probs2[ctx]); 8220 } 8222 static void 8223 read_mv(struct bool_decoder *bool, 8224 union mv *mv, 8225 mv_component_probs_t mvc[2]) 8226 { 8227 mv->d.y = read_mv_component(bool, mvc[0]); 8228 mv->d.x = read_mv_component(bool, mvc[1]); 8229 } 8231 static void 8232 mv_bias(const struct mb_info *mb, 8233 const unsigned int sign_bias[3], 8234 enum reference_frame ref_frame, 8235 union mv *mv) 8237 { 8238 if (sign_bias[mb->base.ref_frame] ^ sign_bias[ref_frame]) 8239 { 8240 mv->d.x *= -1; 8241 mv->d.y *= -1; 8242 } 8243 } 8245 enum near_mv_v 8246 { 8247 CNT_BEST = 0, 8248 CNT_ZEROZERO = 0, 8249 CNT_NEAREST, 8250 CNT_NEAR, 8251 CNT_SPLITMV 8252 }; 8254 static void 8255 find_near_mvs(const struct mb_info *this, 8256 const struct mb_info *left, 8257 const struct mb_info *above, 8258 const unsigned int sign_bias[3], 8259 union mv near_mvs[4], 8260 int cnt[4]) 8261 { 8262 const struct mb_info *aboveleft = above - 1; 8263 union mv *mv = near_mvs; 8264 int *cntx = cnt; 8266 /* Zero accumulators */ 8267 mv[0].raw = mv[1].raw = mv[2].raw = 0; 8268 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 8270 /* Process above */ 8271 if (above->base.ref_frame != CURRENT_FRAME) 8272 { 8273 if (above->base.mv.raw) 8274 { 8275 (++mv)->raw = above->base.mv.raw; 8276 mv_bias(above, sign_bias, this->base.ref_frame, mv); 8277 ++cntx; 8278 } 8280 *cntx += 2; 8281 } 8282 /* Process left */ 8283 if (left->base.ref_frame != CURRENT_FRAME) 8284 { 8285 if (left->base.mv.raw) 8286 { 8287 union mv this_mv; 8289 this_mv.raw = left->base.mv.raw; 8290 mv_bias(left, sign_bias, this->base.ref_frame, &this_mv); 8292 if (this_mv.raw != mv->raw) 8293 { 8294 (++mv)->raw = this_mv.raw; 8295 ++cntx; 8296 } 8298 *cntx += 2; 8299 } 8300 else 8301 cnt[CNT_ZEROZERO] += 2; 8302 } 8304 /* Process above left */ 8305 if (aboveleft->base.ref_frame != CURRENT_FRAME) 8306 { 8307 if (aboveleft->base.mv.raw) 8308 { 8309 union mv this_mv; 8311 this_mv.raw = aboveleft->base.mv.raw; 8312 mv_bias(aboveleft, sign_bias, this->base.ref_frame, 8313 &this_mv); 8315 if (this_mv.raw != mv->raw) 8316 { 8317 (++mv)->raw = this_mv.raw; 8318 ++cntx; 8319 } 8321 *cntx += 1; 8322 } 8323 else 8324 cnt[CNT_ZEROZERO] += 1; 8325 } 8327 /* If we have three distinct MV's ... */ 8328 if (cnt[CNT_SPLITMV]) 8329 { 8330 /* See if above-left MV can be merged with NEAREST */ 8331 if (mv->raw == near_mvs[CNT_NEAREST].raw) 8332 cnt[CNT_NEAREST] += 1; 8333 } 8335 cnt[CNT_SPLITMV] = ((above->base.y_mode == SPLITMV) 8336 + (left->base.y_mode == SPLITMV)) * 2 8337 + (aboveleft->base.y_mode == SPLITMV); 8339 /* Swap near and nearest if necessary */ 8340 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) 8341 { 8342 int tmp; 8343 tmp = cnt[CNT_NEAREST]; 8344 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 8345 cnt[CNT_NEAR] = tmp; 8346 tmp = near_mvs[CNT_NEAREST].raw; 8347 near_mvs[CNT_NEAREST].raw = near_mvs[CNT_NEAR].raw; 8348 near_mvs[CNT_NEAR].raw = tmp; 8349 } 8351 /* Use near_mvs[CNT_BEST] to store the "best" MV. Note that this 8352 * storage shares the same address as near_mvs[CNT_ZEROZERO]. 8353 */ 8354 if (cnt[CNT_NEAREST] >= cnt[CNT_BEST]) 8355 near_mvs[CNT_BEST] = near_mvs[CNT_NEAREST]; 8356 } 8358 static void 8359 decode_split_mv(struct mb_info *this, 8360 const struct mb_info *left, 8361 const struct mb_info *above, 8362 struct vp8_entropy_hdr *hdr, 8363 union mv *best_mv, 8364 struct bool_decoder *bool) 8365 { 8366 const int *partition; 8367 int j, k, mask, partition_id; 8369 partition_id = bool_read_tree(bool, split_mv_tree, split_mv_probs); 8370 partition = mv_partitions[partition_id]; 8371 this->base.partitioning = partition_id; 8373 for (j = 0, mask = 0; mask < 65535; j++) 8374 { 8375 union mv mv, left_mv, above_mv; 8376 enum prediction_mode subblock_mode; 8377 /* Find the first subblock in this partition. */ 8378 for (k = 0; j != partition[k]; k++); 8380 /* Decode the next MV */ 8381 left_mv = left_block_mv(this, left, k); 8382 above_mv = above_block_mv(this, above, k); 8383 subblock_mode = submv_ref(bool, left_mv, above_mv); 8385 switch (subblock_mode) 8386 { 8387 case LEFT4X4: 8388 mv = left_mv; 8389 break; 8390 case ABOVE4X4: 8391 mv = above_mv; 8392 break; 8393 case ZERO4X4: 8394 mv.raw = 0; 8395 break; 8396 case NEW4X4: 8397 read_mv(bool, &mv, hdr->mv_probs); 8398 mv.d.x += best_mv->d.x; 8399 mv.d.y += best_mv->d.y; 8400 break; 8401 default: 8402 assert(0); 8403 } 8405 /* Fill the MV's for this partition */ 8406 for (; k < 16; k++) 8407 if (j == partition[k]) 8408 { 8409 this->split.mvs[k] = mv; 8410 mask |= 1 << k; 8411 } 8412 } 8413 } 8415 static int 8416 need_mc_border(union mv mv, int l, int t, int b_w, int w, int h) 8417 { 8418 int b, r; 8420 /* Get distance to edge for top-left pixel */ 8421 l += (mv.d.x >> 3); 8422 t += (mv.d.y >> 3); 8423 /* Get distance to edge for bottom-right pixel */ 8424 r = w - (l + b_w); 8425 b = h - (t + b_w); 8427 return (l >> 1 < 2 || r >> 1 < 3 || t >> 1 < 2 || b >> 1 < 3); 8428 } 8430 static void 8431 decode_mvs(struct vp8_decoder_ctx *ctx, 8432 struct mb_info *this, 8433 const struct mb_info *left, 8434 const struct mb_info *above, 8435 const struct mv_clamp_rect *bounds, 8436 struct bool_decoder *bool) 8437 { 8438 struct vp8_entropy_hdr *hdr = &ctx->entropy_hdr; 8439 union mv near_mvs[4]; 8440 union mv clamped_best_mv; 8441 int mv_cnts[4]; 8442 unsigned char probs[4]; 8443 enum {BEST, NEAREST, NEAR}; 8444 int x, y, w, h, b; 8446 this->base.ref_frame = bool_get(bool, hdr->prob_last) 8447 ? 2 + bool_get(bool, hdr->prob_gf) 8448 : 1; 8450 find_near_mvs(this, this - 1, above, ctx->reference_hdr.sign_bias, 8451 near_mvs, mv_cnts); 8452 probs[0] = mv_counts_to_probs[mv_cnts[0]][0]; 8453 probs[1] = mv_counts_to_probs[mv_cnts[1]][1]; 8454 probs[2] = mv_counts_to_probs[mv_cnts[2]][2]; 8455 probs[3] = mv_counts_to_probs[mv_cnts[3]][3]; 8457 this->base.y_mode = bool_read_tree(bool, mv_ref_tree, probs); 8458 this->base.uv_mode = this->base.y_mode; 8460 this->base.need_mc_border = 0; 8461 x = (-bounds->to_left - 128) >> 3; 8462 y = (-bounds->to_top - 128) >> 3; 8463 w = ctx->mb_cols * 16; 8464 h = ctx->mb_rows * 16; 8466 switch (this->base.y_mode) 8467 { 8468 case NEARESTMV: 8469 this->base.mv = clamp_mv(near_mvs[NEAREST], bounds); 8470 break; 8472 case NEARMV: 8473 this->base.mv = clamp_mv(near_mvs[NEAR], bounds); 8474 break; 8475 case ZEROMV: 8476 this->base.mv.raw = 0; 8477 return; //skip need_mc_border check 8478 case NEWMV: 8479 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8480 read_mv(bool, &this->base.mv, hdr->mv_probs); 8481 this->base.mv.d.x += clamped_best_mv.d.x; 8482 this->base.mv.d.y += clamped_best_mv.d.y; 8483 break; 8484 case SPLITMV: 8485 { 8486 union mv chroma_mv[4] = {{{0}}}; 8488 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8489 decode_split_mv(this, left, above, hdr, &clamped_best_mv, bool); 8490 this->base.mv = this->split.mvs[15]; 8492 for (b = 0; b < 16; b++) 8493 { 8494 chroma_mv[(b>>1&1) + (b>>2&2)].d.x += 8495 this->split.mvs[b].d.x; 8496 chroma_mv[(b>>1&1) + (b>>2&2)].d.y += 8497 this->split.mvs[b].d.y; 8499 if (need_mc_border(this->split.mvs[b], 8500 x + (b & 3) * 4, y + (b & ~3), 4, w, h)) 8501 { 8502 this->base.need_mc_border = 1; 8503 break; 8504 } 8505 } 8507 for (b = 0; b < 4; b++) 8508 { 8509 chroma_mv[b].d.x += 4 + 8 * (chroma_mv[b].d.x >> 31); 8510 chroma_mv[b].d.y += 4 + 8 * (chroma_mv[b].d.y >> 31); 8511 chroma_mv[b].d.x /= 4; 8512 chroma_mv[b].d.y /= 4; 8514 //note we're passing in non-subsampled coordinates 8515 if (need_mc_border(chroma_mv[b], 8516 x + (b & 1) * 8, y + (b >> 1) * 8, 16, w, h)) 8517 { 8518 this->base.need_mc_border = 1; 8519 break; 8521 } 8522 } 8524 return; //skip need_mc_border check 8525 } 8526 default: 8527 assert(0); 8528 } 8530 if (need_mc_border(this->base.mv, x, y, 16, w, h)) 8531 this->base.need_mc_border = 1; 8532 } 8534 void 8535 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8536 struct bool_decoder *bool, 8537 int row, 8538 int start_col, 8539 int num_cols) 8540 { 8541 struct mb_info *above, *this; 8542 unsigned int col; 8543 struct mv_clamp_rect bounds; 8545 this = ctx->mb_info_rows[row] + start_col; 8546 above = ctx->mb_info_rows[row - 1] + start_col; 8548 /* Calculate the eighth-pel MV bounds using a 1 MB border. */ 8549 bounds.to_left = -((start_col + 1) << 7); 8550 bounds.to_right = (ctx->mb_cols - start_col) << 7; 8551 bounds.to_top = -((row + 1) << 7); 8552 bounds.to_bottom = (ctx->mb_rows - row) << 7; 8554 for (col = start_col; col < start_col + num_cols; col++) 8555 { 8556 if (ctx->segment_hdr.update_map) 8557 this->base.segment_id = read_segment_id(bool, 8558 &ctx->segment_hdr); 8560 if (ctx->entropy_hdr.coeff_skip_enabled) 8561 this->base.skip_coeff = bool_get(bool, 8562 ctx->entropy_hdr.coeff_skip_prob); 8564 if (ctx->frame_hdr.is_keyframe) 8565 { 8566 if (!ctx->segment_hdr.update_map) 8567 this->base.segment_id = 0; 8569 decode_kf_mb_mode(this, this - 1, above, bool); 8570 } 8571 else 8572 { 8573 if (bool_get(bool, ctx->entropy_hdr.prob_inter)) 8574 decode_mvs(ctx, this, this - 1, above, &bounds, bool); 8575 else 8576 decode_intra_mb_mode(this, &ctx->entropy_hdr, bool); 8578 bounds.to_left -= 16 << 3; 8579 bounds.to_right -= 16 << 3; 8580 } 8582 /* Advance to next mb */ 8583 this++; 8584 above++; 8585 } 8586 } 8588 void 8589 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx) 8590 { 8591 unsigned int mbi_w, mbi_h, i; 8592 struct mb_info *mbi; 8594 mbi_w = ctx->mb_cols + 1; /* For left border col */ 8595 mbi_h = ctx->mb_rows + 1; /* For above border row */ 8597 if (ctx->frame_hdr.frame_size_updated) 8598 { 8599 free(ctx->mb_info_storage); 8600 ctx->mb_info_storage = NULL; 8601 free(ctx->mb_info_rows_storage); 8602 ctx->mb_info_rows_storage = NULL; 8603 } 8605 if (!ctx->mb_info_storage) 8606 ctx->mb_info_storage = calloc(mbi_w * mbi_h, 8607 sizeof(*ctx->mb_info_storage)); 8609 if (!ctx->mb_info_rows_storage) 8610 ctx->mb_info_rows_storage = calloc(mbi_h, 8611 sizeof(*ctx->mb_info_rows_storage)); 8613 /* Set up row pointers */ 8614 mbi = ctx->mb_info_storage + 1; 8615 for (i = 0; i < mbi_h; i++) 8616 { 8617 ctx->mb_info_rows_storage[i] = mbi; 8618 mbi += mbi_w; 8619 } 8621 ctx->mb_info_rows = ctx->mb_info_rows_storage + 1; 8622 } 8624 void 8625 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx) 8626 { 8627 free(ctx->mb_info_storage); 8628 ctx->mb_info_storage = NULL; 8629 free(ctx->mb_info_rows_storage); 8630 ctx->mb_info_rows_storage = NULL; 8631 } 8633 ---- End code block ---------------------------------------- 8634 20.12. modemv.h 8636 ---- Begin code block -------------------------------------- 8638 /* 8639 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8640 * 8641 * Use of this source code is governed by a BSD-style license 8642 * that can be found in the LICENSE file in the root of the source 8643 * tree. An additional intellectual property rights grant can be 8644 * found in the file PATENTS. All contributing project authors may 8645 * be found in the AUTHORS file in the root of the source tree. 8646 */ 8647 #ifndef MODEMV_H 8648 #define MODEMV_H 8650 void 8651 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx); 8653 void 8654 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx); 8656 void 8657 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8658 struct bool_decoder *bool, 8659 int row, 8660 int start_col, 8661 int num_cols); 8663 #endif 8665 ---- End code block ---------------------------------------- 8667 20.13. modemv_data.h 8669 ---- Begin code block -------------------------------------- 8671 static const unsigned char kf_y_mode_probs[] = { 145, 156, 163, 128}; 8672 static const unsigned char kf_uv_mode_probs[] = { 142, 114, 183}; 8673 static const unsigned char kf_b_mode_probs[10][10][9] = 8674 { 8675 { /* above mode 0 */ 8676 { /* left mode 0 */ 231, 120, 48, 89, 115, 113, 120, 152, 112}, 8677 { /* left mode 1 */ 152, 179, 64, 126, 170, 118, 46, 70, 95}, 8678 { /* left mode 2 */ 175, 69, 143, 80, 85, 82, 72, 155, 103}, 8679 { /* left mode 3 */ 56, 58, 10, 171, 218, 189, 17, 13, 152}, 8680 { /* left mode 4 */ 144, 71, 10, 38, 171, 213, 144, 34, 26}, 8681 { /* left mode 5 */ 114, 26, 17, 163, 44, 195, 21, 10, 173}, 8682 { /* left mode 6 */ 121, 24, 80, 195, 26, 62, 44, 64, 85}, 8683 { /* left mode 7 */ 170, 46, 55, 19, 136, 160, 33, 206, 71}, 8684 { /* left mode 8 */ 63, 20, 8, 114, 114, 208, 12, 9, 226}, 8685 { /* left mode 9 */ 81, 40, 11, 96, 182, 84, 29, 16, 36} 8686 }, 8687 { /* above mode 1 */ 8688 { /* left mode 0 */ 134, 183, 89, 137, 98, 101, 106, 165, 148}, 8689 { /* left mode 1 */ 72, 187, 100, 130, 157, 111, 32, 75, 80}, 8690 { /* left mode 2 */ 66, 102, 167, 99, 74, 62, 40, 234, 128}, 8691 { /* left mode 3 */ 41, 53, 9, 178, 241, 141, 26, 8, 107}, 8692 { /* left mode 4 */ 104, 79, 12, 27, 217, 255, 87, 17, 7}, 8693 { /* left mode 5 */ 74, 43, 26, 146, 73, 166, 49, 23, 157}, 8694 { /* left mode 6 */ 65, 38, 105, 160, 51, 52, 31, 115, 128}, 8695 { /* left mode 7 */ 87, 68, 71, 44, 114, 51, 15, 186, 23}, 8696 { /* left mode 8 */ 47, 41, 14, 110, 182, 183, 21, 17, 194}, 8697 { /* left mode 9 */ 66, 45, 25, 102, 197, 189, 23, 18, 22} 8698 }, 8699 { /* above mode 2 */ 8700 { /* left mode 0 */ 88, 88, 147, 150, 42, 46, 45, 196, 205}, 8701 { /* left mode 1 */ 43, 97, 183, 117, 85, 38, 35, 179, 61}, 8702 { /* left mode 2 */ 39, 53, 200, 87, 26, 21, 43, 232, 171}, 8703 { /* left mode 3 */ 56, 34, 51, 104, 114, 102, 29, 93, 77}, 8704 { /* left mode 4 */ 107, 54, 32, 26, 51, 1, 81, 43, 31}, 8705 { /* left mode 5 */ 39, 28, 85, 171, 58, 165, 90, 98, 64}, 8706 { /* left mode 6 */ 34, 22, 116, 206, 23, 34, 43, 166, 73}, 8707 { /* left mode 7 */ 68, 25, 106, 22, 64, 171, 36, 225, 114}, 8708 { /* left mode 8 */ 34, 19, 21, 102, 132, 188, 16, 76, 124}, 8709 { /* left mode 9 */ 62, 18, 78, 95, 85, 57, 50, 48, 51} 8710 }, 8711 { /* above mode 3 */ 8712 { /* left mode 0 */ 193, 101, 35, 159, 215, 111, 89, 46, 111}, 8713 { /* left mode 1 */ 60, 148, 31, 172, 219, 228, 21, 18, 111}, 8714 { /* left mode 2 */ 112, 113, 77, 85, 179, 255, 38, 120, 114}, 8715 { /* left mode 3 */ 40, 42, 1, 196, 245, 209, 10, 25, 109}, 8716 { /* left mode 4 */ 100, 80, 8, 43, 154, 1, 51, 26, 71}, 8717 { /* left mode 5 */ 88, 43, 29, 140, 166, 213, 37, 43, 154}, 8718 { /* left mode 6 */ 61, 63, 30, 155, 67, 45, 68, 1, 209}, 8719 { /* left mode 7 */ 142, 78, 78, 16, 255, 128, 34, 197, 171}, 8720 { /* left mode 8 */ 41, 40, 5, 102, 211, 183, 4, 1, 221}, 8721 { /* left mode 9 */ 51, 50, 17, 168, 209, 192, 23, 25, 82} 8722 }, 8723 { /* above mode 4 */ 8724 { /* left mode 0 */ 125, 98, 42, 88, 104, 85, 117, 175, 82}, 8725 { /* left mode 1 */ 95, 84, 53, 89, 128, 100, 113, 101, 45}, 8726 { /* left mode 2 */ 75, 79, 123, 47, 51, 128, 81, 171, 1}, 8727 { /* left mode 3 */ 57, 17, 5, 71, 102, 57, 53, 41, 49}, 8728 { /* left mode 4 */ 115, 21, 2, 10, 102, 255, 166, 23, 6}, 8729 { /* left mode 5 */ 38, 33, 13, 121, 57, 73, 26, 1, 85}, 8730 { /* left mode 6 */ 41, 10, 67, 138, 77, 110, 90, 47, 114}, 8731 { /* left mode 7 */ 101, 29, 16, 10, 85, 128, 101, 196, 26}, 8732 { /* left mode 8 */ 57, 18, 10, 102, 102, 213, 34, 20, 43}, 8733 { /* left mode 9 */ 117, 20, 15, 36, 163, 128, 68, 1, 26} 8734 }, 8735 { /* above mode 5 */ 8736 { /* left mode 0 */ 138, 31, 36, 171, 27, 166, 38, 44, 229}, 8737 { /* left mode 1 */ 67, 87, 58, 169, 82, 115, 26, 59, 179}, 8738 { /* left mode 2 */ 63, 59, 90, 180, 59, 166, 93, 73, 154}, 8739 { /* left mode 3 */ 40, 40, 21, 116, 143, 209, 34, 39, 175}, 8740 { /* left mode 4 */ 57, 46, 22, 24, 128, 1, 54, 17, 37}, 8741 { /* left mode 5 */ 47, 15, 16, 183, 34, 223, 49, 45, 183}, 8742 { /* left mode 6 */ 46, 17, 33, 183, 6, 98, 15, 32, 183}, 8743 { /* left mode 7 */ 65, 32, 73, 115, 28, 128, 23, 128, 205}, 8744 { /* left mode 8 */ 40, 3, 9, 115, 51, 192, 18, 6, 223}, 8745 { /* left mode 9 */ 87, 37, 9, 115, 59, 77, 64, 21, 47} 8746 }, 8747 { /* above mode 6 */ 8748 { /* left mode 0 */ 104, 55, 44, 218, 9, 54, 53, 130, 226}, 8749 { /* left mode 1 */ 64, 90, 70, 205, 40, 41, 23, 26, 57}, 8750 { /* left mode 2 */ 54, 57, 112, 184, 5, 41, 38, 166, 213}, 8751 { /* left mode 3 */ 30, 34, 26, 133, 152, 116, 10, 32, 134}, 8752 { /* left mode 4 */ 75, 32, 12, 51, 192, 255, 160, 43, 51}, 8753 { /* left mode 5 */ 39, 19, 53, 221, 26, 114, 32, 73, 255}, 8754 { /* left mode 6 */ 31, 9, 65, 234, 2, 15, 1, 118, 73}, 8755 { /* left mode 7 */ 88, 31, 35, 67, 102, 85, 55, 186, 85}, 8756 { /* left mode 8 */ 56, 21, 23, 111, 59, 205, 45, 37, 192}, 8757 { /* left mode 9 */ 55, 38, 70, 124, 73, 102, 1, 34, 98} 8758 }, 8759 { /* above mode 7 */ 8760 { /* left mode 0 */ 102, 61, 71, 37, 34, 53, 31, 243, 192}, 8761 { /* left mode 1 */ 69, 60, 71, 38, 73, 119, 28, 222, 37}, 8762 { /* left mode 2 */ 68, 45, 128, 34, 1, 47, 11, 245, 171}, 8763 { /* left mode 3 */ 62, 17, 19, 70, 146, 85, 55, 62, 70}, 8764 { /* left mode 4 */ 75, 15, 9, 9, 64, 255, 184, 119, 16}, 8765 { /* left mode 5 */ 37, 43, 37, 154, 100, 163, 85, 160, 1}, 8766 { /* left mode 6 */ 63, 9, 92, 136, 28, 64, 32, 201, 85}, 8767 { /* left mode 7 */ 86, 6, 28, 5, 64, 255, 25, 248, 1}, 8768 { /* left mode 8 */ 56, 8, 17, 132, 137, 255, 55, 116, 128}, 8769 { /* left mode 9 */ 58, 15, 20, 82, 135, 57, 26, 121, 40} 8770 }, 8771 { /* above mode 8 */ 8772 { /* left mode 0 */ 164, 50, 31, 137, 154, 133, 25, 35, 218}, 8773 { /* left mode 1 */ 51, 103, 44, 131, 131, 123, 31, 6, 158}, 8774 { /* left mode 2 */ 86, 40, 64, 135, 148, 224, 45, 183, 128}, 8775 { /* left mode 3 */ 22, 26, 17, 131, 240, 154, 14, 1, 209}, 8776 { /* left mode 4 */ 83, 12, 13, 54, 192, 255, 68, 47, 28}, 8777 { /* left mode 5 */ 45, 16, 21, 91, 64, 222, 7, 1, 197}, 8778 { /* left mode 6 */ 56, 21, 39, 155, 60, 138, 23, 102, 213}, 8779 { /* left mode 7 */ 85, 26, 85, 85, 128, 128, 32, 146, 171}, 8780 { /* left mode 8 */ 18, 11, 7, 63, 144, 171, 4, 4, 246}, 8781 { /* left mode 9 */ 35, 27, 10, 146, 174, 171, 12, 26, 128} 8782 }, 8783 { /* above mode 9 */ 8784 { /* left mode 0 */ 190, 80, 35, 99, 180, 80, 126, 54, 45}, 8785 { /* left mode 1 */ 85, 126, 47, 87, 176, 51, 41, 20, 32}, 8786 { /* left mode 2 */ 101, 75, 128, 139, 118, 146, 116, 128, 85}, 8787 { /* left mode 3 */ 56, 41, 15, 176, 236, 85, 37, 9, 62}, 8788 { /* left mode 4 */ 146, 36, 19, 30, 171, 255, 97, 27, 20}, 8789 { /* left mode 5 */ 71, 30, 17, 119, 118, 255, 17, 18, 138}, 8790 { /* left mode 6 */ 101, 38, 60, 138, 55, 70, 43, 26, 142}, 8791 { /* left mode 7 */ 138, 45, 61, 62, 219, 1, 81, 188, 64}, 8792 { /* left mode 8 */ 32, 41, 20, 117, 151, 142, 20, 21, 163}, 8793 { /* left mode 9 */ 112, 19, 12, 61, 195, 128, 48, 4, 24} 8794 } 8795 }; 8796 static const int kf_y_mode_tree[] = 8797 { 8798 -B_PRED, 2, 8799 4, 6, 8800 -DC_PRED, -V_PRED, 8801 -H_PRED, -TM_PRED 8802 }; 8803 static const int y_mode_tree[] = 8804 { 8805 -DC_PRED, 2, 8806 4, 6, 8807 -V_PRED, -H_PRED, 8808 -TM_PRED, -B_PRED 8809 }; 8810 static const int uv_mode_tree[6] = 8811 { 8812 -DC_PRED, 2, 8813 -V_PRED, 4, 8814 -H_PRED, -TM_PRED 8815 }; 8816 static const int b_mode_tree[18] = 8817 { 8818 -B_DC_PRED, 2, /* 0 = DC_NODE */ 8819 -B_TM_PRED, 4, /* 1 = TM_NODE */ 8820 -B_VE_PRED, 6, /* 2 = VE_NODE */ 8821 8, 12, /* 3 = COM_NODE */ 8822 -B_HE_PRED, 10, /* 4 = HE_NODE */ 8823 -B_RD_PRED, -B_VR_PRED, /* 5 = RD_NODE */ 8824 -B_LD_PRED, 14, /* 6 = LD_NODE */ 8825 -B_VL_PRED, 16, /* 7 = VL_NODE */ 8826 -B_HD_PRED, -B_HU_PRED /* 8 = HD_NODE */ 8827 }; 8828 static const int small_mv_tree[14] = 8829 { 8830 2, 8, 8831 4, 6, 8832 -0, -1, 8833 -2, -3, 8834 10, 12, 8835 -4, -5, 8836 -6, -7 8837 }; 8838 static const int mv_ref_tree[8] = 8839 { 8840 -ZEROMV, 2, 8841 -NEARESTMV, 4, 8842 -NEARMV, 6, 8843 -NEWMV, -SPLITMV 8844 }; 8845 static const int submv_ref_tree[6] = 8846 { 8847 -LEFT4X4, 2, 8848 -ABOVE4X4, 4, 8849 -ZERO4X4, -NEW4X4 8850 }; 8851 static const int split_mv_tree[6] = 8852 { 8853 -3, 2, 8854 -2, 4, 8855 -0, -1 8856 }; 8857 static const unsigned char default_b_mode_probs[] = 8858 { 120, 90, 79, 133, 87, 85, 80, 111, 151}; 8859 static const unsigned char mv_counts_to_probs[6][4] = 8860 { 8861 { 7, 1, 1, 143 }, 8862 { 14, 18, 14, 107 }, 8863 { 135, 64, 57, 68 }, 8864 { 60, 56, 128, 65 }, 8865 { 159, 134, 128, 34 }, 8866 { 234, 188, 128, 28 } 8868 }; 8869 static const unsigned char split_mv_probs[3] = 8870 { 110, 111, 150}; 8871 static const unsigned char submv_ref_probs2[5][3] = 8872 { 8873 { 147, 136, 18 }, 8874 { 106, 145, 1 }, 8875 { 179, 121, 1 }, 8876 { 223, 1, 34 }, 8877 { 208, 1, 1 } 8878 }; 8880 const static int mv_partitions[4][16] = 8881 { 8882 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, 8883 {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1 }, 8884 {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3 }, 8885 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } 8886 }; 8888 ---- End code block ---------------------------------------- 8890 20.14. predict.c 8892 ---- Begin code block -------------------------------------- 8894 /* 8895 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8896 * 8897 * Use of this source code is governed by a BSD-style license 8898 * that can be found in the LICENSE file in the root of the source 8899 * tree. An additional intellectual property rights grant can be 8900 * found in the file PATENTS. All contributing project authors may 8901 * be found in the AUTHORS file in the root of the source tree. 8902 */ 8903 #include "dixie.h" 8904 #include "predict.h" 8905 #include "idct_add.h" 8906 #include "mem.h" 8907 #include 8908 #include 8910 enum 8911 { 8912 BORDER_PIXELS = 16, 8913 }; 8915 static const filter_t sixtap_filters[8] = 8916 { 8917 { 0, 0, 128, 0, 0, 0 }, 8918 { 0, -6, 123, 12, -1, 0 }, 8919 { 2, -11, 108, 36, -8, 1 }, 8920 { 0, -9, 93, 50, -6, 0 }, 8921 { 3, -16, 77, 77, -16, 3 }, 8922 { 0, -6, 50, 93, -9, 0 }, 8923 { 1, -8, 36, 108, -11, 2 }, 8924 { 0, -1, 12, 123, -6, 0 }, 8925 }; 8927 static const filter_t bilinear_filters[8] = 8928 { 8930 { 0, 0, 128, 0, 0, 0 }, 8931 { 0, 0, 112, 16, 0, 0 }, 8932 { 0, 0, 96, 32, 0, 0 }, 8933 { 0, 0, 80, 48, 0, 0 }, 8934 { 0, 0, 64, 64, 0, 0 }, 8935 { 0, 0, 48, 80, 0, 0 }, 8936 { 0, 0, 32, 96, 0, 0 }, 8937 { 0, 0, 16, 112, 0, 0 } 8938 }; 8940 static void 8941 predict_h_nxn(unsigned char *predict, 8942 int stride, 8943 int n) 8944 { 8945 unsigned char *left = predict - 1; 8946 int i, j; 8948 for (i = 0; i < n; i++) 8949 for (j = 0; j < n; j++) 8950 predict[i *stride + j] = left[i * stride]; 8951 } 8953 static void 8954 predict_v_nxn(unsigned char *predict, 8955 int stride, 8956 int n) 8957 { 8958 unsigned char *above = predict - stride; 8959 int i, j; 8961 for (i = 0; i < n; i++) 8962 for (j = 0; j < n; j++) 8963 predict[i *stride + j] = above[j]; 8964 } 8966 static void 8967 predict_tm_nxn(unsigned char *predict, 8968 int stride, 8969 int n) 8970 { 8971 /* Transposes the left column to the top row for later consumption 8972 * by the idct/recon stage 8973 */ 8974 unsigned char *left = predict - 1; 8975 unsigned char *above = predict - stride; 8976 unsigned char p = above[-1]; 8977 int i, j; 8979 for (j = 0; j < n; j++) 8980 { 8981 for (i = 0; i < n; i++) 8982 predict[i] = CLAMP_255(*left + above[i] - p); 8984 predict += stride; 8985 left += stride; 8986 } 8987 } 8989 static void 8990 predict_dc_nxn(unsigned char *predict, 8991 int stride, 8992 int n) 8993 { 8994 unsigned char *left = predict - 1; 8995 unsigned char *above = predict - stride; 8996 int i, j, dc = 0; 8998 for (i = 0; i < n; i++) 8999 { 9000 dc += *left + above[i]; 9001 left += stride; 9002 } 9004 switch (n) 9005 { 9006 case 16: 9007 dc = (dc + 16) >> 5; 9008 break; 9010 case 8: 9011 dc = (dc + 8) >> 4; 9012 break; 9013 case 4: 9014 dc = (dc + 4) >> 3; 9015 break; 9016 } 9018 for (i = 0; i < n; i++) 9019 for (j = 0; j < n; j++) 9020 predict[i *stride + j] = dc; 9021 } 9023 static void 9024 predict_ve_4x4(unsigned char *predict, 9025 int stride) 9026 { 9027 unsigned char *above = predict - stride; 9028 int i, j; 9030 predict[0] = (above[-1] + 2 * above[0] + above[1] + 2) >> 2; 9031 predict[1] = (above[ 0] + 2 * above[1] + above[2] + 2) >> 2; 9032 predict[2] = (above[ 1] + 2 * above[2] + above[3] + 2) >> 2; 9033 predict[3] = (above[ 2] + 2 * above[3] + above[4] + 2) >> 2; 9035 for (i = 1; i < 4; i++) 9036 for (j = 0; j < 4; j++) 9037 predict[i *stride + j] = predict[j]; 9038 } 9040 static void 9041 predict_he_4x4(unsigned char *predict, 9042 int stride) 9043 { 9044 unsigned char *left = predict - 1; 9046 predict[0] = 9047 predict[1] = 9048 predict[2] = 9049 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9050 predict += stride; 9051 left += stride; 9053 predict[0] = 9054 predict[1] = 9055 predict[2] = 9056 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9057 predict += stride; 9058 left += stride; 9060 predict[0] = 9061 predict[1] = 9062 predict[2] = 9063 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9064 predict += stride; 9065 left += stride; 9067 predict[0] = 9068 predict[1] = 9069 predict[2] = 9070 predict[3] = (left[-stride] + 2 * left[0] + left[0] + 2) >> 2; 9071 } 9073 static void 9074 predict_ld_4x4(unsigned char *predict, 9075 int stride) 9076 { 9077 unsigned char *above = predict - stride; 9078 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9080 predict[0] = pred0 = (above[0] + 2 * above[1] + above[2] + 2) >> 2; 9081 predict[1] = pred1 = (above[1] + 2 * above[2] + above[3] + 2) >> 2; 9082 predict[2] = pred2 = (above[2] + 2 * above[3] + above[4] + 2) >> 2; 9083 predict[3] = pred3 = (above[3] + 2 * above[4] + above[5] + 2) >> 2; 9084 predict += stride; 9086 predict[0] = pred1; 9087 predict[1] = pred2; 9088 predict[2] = pred3; 9089 predict[3] = pred4 = (above[4] + 2 * above[5] + above[6] + 2) >> 2; 9090 predict += stride; 9092 predict[0] = pred2; 9093 predict[1] = pred3; 9094 predict[2] = pred4; 9095 predict[3] = pred5 = (above[5] + 2 * above[6] + above[7] + 2) >> 2; 9096 predict += stride; 9098 predict[0] = pred3; 9099 predict[1] = pred4; 9100 predict[2] = pred5; 9101 predict[3] = pred6 = (above[6] + 2 * above[7] + above[7] + 2) >> 2; 9102 } 9103 static void 9104 predict_rd_4x4(unsigned char *predict, 9105 int stride) 9106 { 9107 unsigned char *left = predict - 1; 9108 unsigned char *above = predict - stride; 9109 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9111 predict[0] = pred0 = 9112 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9113 predict[1] = pred1 = 9114 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9115 predict[2] = pred2 = 9116 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9117 predict[3] = pred3 = 9118 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9119 predict += stride; 9121 predict[0] = pred4 = 9122 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9123 predict[1] = pred0; 9124 predict[2] = pred1; 9125 predict[3] = pred2; 9126 predict += stride; 9128 predict[0] = pred5 = 9129 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9130 predict[1] = pred4; 9131 predict[2] = pred0; 9132 predict[3] = pred1; 9133 predict += stride; 9135 predict[0] = pred6 = 9136 (left[stride*3] + 2 * left[stride*2] + left[stride] + 2) >> 2; 9137 predict[1] = pred5; 9138 predict[2] = pred4; 9139 predict[3] = pred0; 9140 } 9142 static void 9143 predict_vr_4x4(unsigned char *predict, 9144 int stride) 9145 { 9146 unsigned char *left = predict - 1; 9147 unsigned char *above = predict - stride; 9148 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9149 pred7, pred8, pred9; 9151 predict[0] = pred0 = (above[-1] + above[0] + 1) >> 1; 9152 predict[1] = pred1 = (above[ 0] + above[1] + 1) >> 1; 9153 predict[2] = pred2 = (above[ 1] + above[2] + 1) >> 1; 9154 predict[3] = pred3 = (above[ 2] + above[3] + 1) >> 1; 9155 predict += stride; 9157 predict[0] = pred4 = 9158 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9159 predict[1] = pred5 = 9160 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9161 predict[2] = pred6 = 9162 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9163 predict[3] = pred7 = 9164 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9165 predict += stride; 9167 predict[0] = pred8 = 9168 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9169 predict[1] = pred0; 9170 predict[2] = pred1; 9171 predict[3] = pred2; 9172 predict += stride; 9174 predict[0] = pred9 = 9175 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9176 predict[1] = pred4; 9177 predict[2] = pred5; 9178 predict[3] = pred6; 9179 } 9181 static void 9182 predict_vl_4x4(unsigned char *predict, 9183 int stride) 9184 { 9185 unsigned char *above = predict - stride; 9186 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9187 pred7, pred8, pred9; 9189 predict[0] = pred0 = (above[0] + above[1] + 1) >> 1; 9190 predict[1] = pred1 = (above[1] + above[2] + 1) >> 1; 9191 predict[2] = pred2 = (above[2] + above[3] + 1) >> 1; 9192 predict[3] = pred3 = (above[3] + above[4] + 1) >> 1; 9193 predict += stride; 9195 predict[0] = pred4 = 9196 (above[0] + 2 * above[1] + above[2] + 2) >> 2; 9197 predict[1] = pred5 = 9198 (above[1] + 2 * above[2] + above[3] + 2) >> 2; 9199 predict[2] = pred6 = 9200 (above[2] + 2 * above[3] + above[4] + 2) >> 2; 9201 predict[3] = pred7 = 9202 (above[3] + 2 * above[4] + above[5] + 2) >> 2; 9203 predict += stride; 9205 predict[0] = pred1; 9206 predict[1] = pred2; 9207 predict[2] = pred3; 9208 predict[3] = pred8 = (above[4] + 2 * above[5] + above[6] + 2) >> 2; 9209 predict += stride; 9211 predict[0] = pred5; 9212 predict[1] = pred6; 9213 predict[2] = pred7; 9214 predict[3] = pred9 = (above[5] + 2 * above[6] + above[7] + 2) >> 2; 9215 } 9217 static void 9218 predict_hd_4x4(unsigned char *predict, 9219 int stride) 9220 { 9221 unsigned char *left = predict - 1; 9222 unsigned char *above = predict - stride; 9223 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9224 pred7, pred8, pred9; 9226 predict[0] = pred0 = 9227 (left[ 0] + above[-1] + 1) >> 1; 9228 predict[1] = pred1 = 9229 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9230 predict[2] = pred2 = 9231 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9232 predict[3] = pred3 = 9233 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9234 predict += stride; 9236 predict[0] = pred4 = 9237 (left[stride] + left[0] + 1) >> 1; 9238 predict[1] = pred5 = 9239 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9240 predict[2] = pred0; 9241 predict[3] = pred1; 9242 predict += stride; 9244 predict[0] = pred6 = 9245 (left[stride*2] + left[stride] + 1) >> 1; 9246 predict[1] = pred7 = 9247 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9248 predict[2] = pred4; 9249 predict[3] = pred5; 9250 predict += stride; 9252 predict[0] = pred8 = 9253 (left[stride*3] + left[stride*2] + 1) >> 1; 9254 predict[1] = pred9 = 9255 (left[stride*3] + 2 * left[stride*2] + left[stride] + 2) >> 2; 9256 predict[2] = pred6; 9257 predict[3] = pred7; 9258 } 9260 static void 9261 predict_hu_4x4(unsigned char *predict, 9262 int stride) 9263 { 9264 unsigned char *left = predict - 1; 9265 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9267 predict[0] = pred0 = 9268 (left[stride*0] + left[stride*1] + 1) >> 1; 9269 predict[1] = pred1 = 9270 (left[stride*0] + 2 * left[stride*1] + left[stride*2] + 2) >> 2; 9271 predict[2] = pred2 = 9272 (left[stride*1] + left[stride*2] + 1) >> 1; 9273 predict[3] = pred3 = 9274 (left[stride*1] + 2 * left[stride*2] + left[stride*3] + 2) >> 2; 9275 predict += stride; 9277 predict[0] = pred2; 9278 predict[1] = pred3; 9279 predict[2] = pred4 = 9280 (left[stride*2] + left[stride*3] + 1) >> 1; 9281 predict[3] = pred5 = 9282 (left[stride*2] + 2 * left[stride*3] + left[stride*3] + 2) >> 2; 9283 predict += stride; 9285 predict[0] = pred4; 9286 predict[1] = pred5; 9287 predict[2] = pred6 = left[stride*3]; 9288 predict[3] = pred6; 9289 predict += stride; 9291 predict[0] = pred6; 9292 predict[1] = pred6; 9293 predict[2] = pred6; 9294 predict[3] = pred6; 9295 } 9297 static void 9298 predict_h_16x16(unsigned char *predict, int stride) 9299 { 9300 predict_h_nxn(predict, stride, 16); 9301 } 9303 static void 9304 predict_v_16x16(unsigned char *predict, int stride) 9305 { 9306 predict_v_nxn(predict, stride, 16); 9307 } 9309 static void 9310 predict_tm_16x16(unsigned char *predict, int stride) 9311 { 9312 predict_tm_nxn(predict, stride, 16); 9313 } 9315 static void 9316 predict_h_8x8(unsigned char *predict, int stride) 9317 { 9318 predict_h_nxn(predict, stride, 8); 9319 } 9321 static void 9322 predict_v_8x8(unsigned char *predict, int stride) 9323 { 9324 predict_v_nxn(predict, stride, 8); 9325 } 9327 static void 9328 predict_tm_8x8(unsigned char *predict, int stride) 9329 { 9330 predict_tm_nxn(predict, stride, 8); 9331 } 9332 static void 9333 predict_tm_4x4(unsigned char *predict, int stride) 9334 { 9335 predict_tm_nxn(predict, stride, 4); 9336 } 9338 static void 9339 copy_down(unsigned char *recon, 9340 int stride) 9341 { 9342 /* Copy the four pixels above-right of subblock 3 to 9343 * above-right of subblocks 7, 11, and 15 9344 */ 9345 uint32_t tmp, *copy = (void *)(recon + 16 - stride); 9347 stride = stride / sizeof(unsigned int); 9348 tmp = *copy; 9349 copy += stride * 4; 9350 *copy = tmp; 9351 copy += stride * 4; 9352 *copy = tmp; 9353 copy += stride * 4; 9354 *copy = tmp; 9355 } 9357 static void 9358 b_pred(unsigned char *predict, 9359 int stride, 9360 struct mb_info *mbi, 9361 short *coeffs) 9362 { 9363 int i; 9365 copy_down(predict, stride); 9367 for (i = 0; i < 16; i++) 9368 { 9369 unsigned char *b_predict = predict + (i & 3) * 4; 9371 switch (mbi->split.modes[i]) 9372 { 9373 case B_DC_PRED: 9374 predict_dc_nxn(b_predict, stride, 4); 9375 break; 9376 case B_TM_PRED: 9377 predict_tm_4x4(b_predict, stride); 9378 break; 9379 case B_VE_PRED: 9380 predict_ve_4x4(b_predict, stride); 9381 break; 9382 case B_HE_PRED: 9383 predict_he_4x4(b_predict, stride); 9384 break; 9385 case B_LD_PRED: 9386 predict_ld_4x4(b_predict, stride); 9387 break; 9388 case B_RD_PRED: 9389 predict_rd_4x4(b_predict, stride); 9390 break; 9391 case B_VR_PRED: 9392 predict_vr_4x4(b_predict, stride); 9393 break; 9394 case B_VL_PRED: 9395 predict_vl_4x4(b_predict, stride); 9396 break; 9397 case B_HD_PRED: 9398 predict_hd_4x4(b_predict, stride); 9399 break; 9400 case B_HU_PRED: 9401 predict_hu_4x4(b_predict, stride); 9402 break; 9403 default: 9404 assert(0); 9405 } 9407 vp8_dixie_idct_add(b_predict, b_predict, stride, coeffs); 9408 coeffs += 16; 9410 if ((i & 3) == 3) 9411 { 9412 predict += stride * 4; 9413 } 9414 } 9415 } 9417 static void 9418 fixup_dc_coeffs(struct mb_info *mbi, 9419 short *coeffs) 9420 { 9421 short y2[16]; 9422 int i; 9424 vp8_dixie_walsh(coeffs + 24 * 16, y2); 9425 for (i = 0; i < 16; i++) 9426 coeffs[i*16] = y2[i]; 9427 } 9429 static void 9430 predict_intra_luma(unsigned char *predict, 9431 int stride, 9432 struct mb_info *mbi, 9433 short *coeffs) 9434 { 9435 if (mbi->base.y_mode == B_PRED) 9436 b_pred(predict, stride, mbi, coeffs); 9437 else 9438 { 9439 int i; 9441 switch (mbi->base.y_mode) 9442 { 9443 case DC_PRED: 9444 predict_dc_nxn(predict, stride, 16); 9445 break; 9446 case V_PRED: 9447 predict_v_16x16(predict, stride); 9448 break; 9449 case H_PRED: 9450 predict_h_16x16(predict, stride); 9451 break; 9452 case TM_PRED: 9453 predict_tm_16x16(predict, stride); 9454 break; 9455 default: 9456 assert(0); 9457 } 9459 fixup_dc_coeffs(mbi, coeffs); 9461 for (i = 0; i < 16; i++) 9462 { 9463 vp8_dixie_idct_add(predict, predict, stride, coeffs); 9464 coeffs += 16; 9465 predict += 4; 9467 if ((i & 3) == 3) 9468 predict += stride * 4 - 16; 9469 } 9471 } 9473 } 9475 static void 9476 predict_intra_chroma(unsigned char *predict_u, 9477 unsigned char *predict_v, 9478 int stride, 9479 struct mb_info *mbi, 9480 short *coeffs) 9481 { 9482 int i; 9484 switch (mbi->base.uv_mode) 9485 { 9486 case DC_PRED: 9487 predict_dc_nxn(predict_u, stride, 8); 9488 predict_dc_nxn(predict_v, stride, 8); 9489 break; 9490 case V_PRED: 9491 predict_v_8x8(predict_u, stride); 9492 predict_v_8x8(predict_v, stride); 9493 break; 9494 case H_PRED: 9495 predict_h_8x8(predict_u, stride); 9496 predict_h_8x8(predict_v, stride); 9497 break; 9498 case TM_PRED: 9499 predict_tm_8x8(predict_u, stride); 9500 predict_tm_8x8(predict_v, stride); 9501 break; 9502 default: 9503 assert(0); 9504 } 9506 coeffs += 16 * 16; 9508 for (i = 16; i < 20; i++) 9509 { 9510 vp8_dixie_idct_add(predict_u, predict_u, stride, coeffs); 9511 coeffs += 16; 9512 predict_u += 4; 9514 if (i & 1) 9515 predict_u += stride * 4 - 8; 9516 } 9518 for (i = 20; i < 24; i++) 9519 { 9520 vp8_dixie_idct_add(predict_v, predict_v, stride, coeffs); 9521 coeffs += 16; 9522 predict_v += 4; 9524 if (i & 1) 9525 predict_v += stride * 4 - 8; 9526 } 9528 } 9530 static void 9531 sixtap_horiz(unsigned char *output, 9532 int output_stride, 9533 const unsigned char *reference, 9534 int reference_stride, 9535 int cols, 9536 int rows, 9537 const filter_t filter 9538 ) 9539 { 9540 int r, c, temp; 9542 for (r = 0; r < rows; r++) 9543 { 9544 for (c = 0; c < cols; c++) 9545 { 9546 temp = (reference[-2] * filter[0]) + 9547 (reference[-1] * filter[1]) + 9548 (reference[ 0] * filter[2]) + 9549 (reference[ 1] * filter[3]) + 9550 (reference[ 2] * filter[4]) + 9551 (reference[ 3] * filter[5]) + 9552 64; 9553 temp >>= 7; 9554 output[c] = CLAMP_255(temp); 9555 reference++; 9556 } 9558 reference += reference_stride - cols; 9559 output += output_stride; 9560 } 9561 } 9563 static void 9564 sixtap_vert(unsigned char *output, 9565 int output_stride, 9566 const unsigned char *reference, 9567 int reference_stride, 9568 int cols, 9569 int rows, 9570 const filter_t filter 9571 ) 9572 { 9573 int r, c, temp; 9575 for (r = 0; r < rows; r++) 9576 { 9577 for (c = 0; c < cols; c++) 9578 { 9579 temp = (reference[-2*reference_stride] * filter[0]) + 9580 (reference[-1*reference_stride] * filter[1]) + 9581 (reference[ 0*reference_stride] * filter[2]) + 9582 (reference[ 1*reference_stride] * filter[3]) + 9583 (reference[ 2*reference_stride] * filter[4]) + 9584 (reference[ 3*reference_stride] * filter[5]) + 9585 64; 9586 temp >>= 7; 9587 output[c] = CLAMP_255(temp); 9588 reference++; 9589 } 9591 reference += reference_stride - cols; 9592 output += output_stride; 9593 } 9594 } 9596 static void 9597 sixtap_2d(unsigned char *output, 9598 int output_stride, 9599 const unsigned char *reference, 9600 int reference_stride, 9601 int cols, 9602 int rows, 9603 int mx, 9604 int my, 9605 const filter_t filters[8] 9606 ) 9607 { 9608 DECLARE_ALIGNED(16, unsigned char, temp[16*(16+5)]); 9610 sixtap_horiz(temp, 16, 9611 reference - 2 * reference_stride, reference_stride, 9612 cols, rows + 5, filters[mx]); 9614 sixtap_vert(output, output_stride, 9615 temp + 2 * 16, 16, 9616 cols, rows, filters[my]); 9617 } 9619 struct img_index 9620 { 9621 unsigned char *y, *u, *v; 9622 int stride, uv_stride; 9623 }; 9625 static const unsigned char * 9626 filter_block(unsigned char *output, 9627 const unsigned char *reference, 9628 int stride, 9629 const union mv *mv, 9630 const filter_t filters[8]) 9631 { 9632 int mx, my; 9634 /* Handle 0,0 as a special case. TODO: does this make it any 9635 * faster? 9636 */ 9637 if (!mv->raw) 9638 return reference; 9640 mx = mv->d.x & 7; 9641 my = mv->d.y & 7; 9642 reference += ((mv->d.y >> 3) * stride) + (mv->d.x >> 3); 9644 if (mx | my) 9645 { 9646 sixtap_2d(output, stride, reference, stride, 4, 4, mx, my, 9647 filters); 9648 reference = output; 9649 } 9651 return reference; 9652 } 9654 static void 9655 recon_1_block(unsigned char *output, 9656 const unsigned char *reference, 9657 int stride, 9658 const union mv *mv, 9659 const filter_t filters[8], 9660 short *coeffs, 9661 struct mb_info *mbi, 9662 int b 9663 ) 9664 { 9665 const unsigned char *predict; 9667 predict = filter_block(output, reference, stride, mv, filters); 9668 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9669 } 9671 static mv_t 9672 calculate_chroma_splitmv(struct mb_info *mbi, 9673 int b, 9674 int full_pixel) 9675 { 9676 int temp; 9677 union mv mv; 9679 temp = mbi->split.mvs[b].d.x + 9680 mbi->split.mvs[b+1].d.x + 9681 mbi->split.mvs[b+4].d.x + 9682 mbi->split.mvs[b+5].d.x; 9684 if (temp < 0) 9685 temp -= 4; 9686 else 9687 temp += 4; 9689 mv.d.x = temp / 8; 9691 temp = mbi->split.mvs[b].d.y + 9692 mbi->split.mvs[b+1].d.y + 9693 mbi->split.mvs[b+4].d.y + 9694 mbi->split.mvs[b+5].d.y; 9696 if (temp < 0) 9697 temp -= 4; 9698 else 9699 temp += 4; 9701 mv.d.y = temp / 8; 9703 if (full_pixel) 9704 { 9705 mv.d.x &= ~7; 9706 mv.d.y &= ~7; 9707 } 9709 return mv; 9710 } 9712 /* Note: We rely on the reconstructed border having the same stride as 9713 * the reference buffer because the filter_block can't adjust the 9714 * stride with its return value, only the reference pointer. 9715 */ 9716 static void 9717 build_mc_border(unsigned char *dst, 9718 const unsigned char *src, 9719 int stride, 9720 int x, 9721 int y, 9722 int b_w, 9723 int b_h, 9724 int w, 9725 int h 9726 ) 9727 { 9728 const unsigned char *ref_row; 9730 /* Get a pointer to the start of the real data for this row */ 9731 ref_row = src - x - y * stride; 9733 if (y >= h) 9734 ref_row += (h - 1) * stride; 9735 else if (y > 0) 9736 ref_row += y * stride; 9738 do 9739 { 9740 int left, right = 0, copy; 9742 left = x < 0 ? -x : 0; 9744 if (left > b_w) 9745 left = b_w; 9747 if (x + b_w > w) 9748 right = x + b_w - w; 9750 if (right > b_w) 9751 right = b_w; 9753 copy = b_w - left - right; 9755 if (left) 9756 memset(dst, ref_row[0], left); 9758 if (copy) 9759 memcpy(dst + left, ref_row + x + left, copy); 9761 if (right) 9762 memset(dst + left + copy, ref_row[w-1], right); 9764 dst += stride; 9765 y++; 9767 if (y < h && y > 0) 9768 ref_row += stride; 9769 } 9770 while (--b_h); 9771 } 9773 static void 9774 recon_1_edge_block(unsigned char *output, 9775 unsigned char *emul_block, 9776 const unsigned char *reference, 9777 int stride, 9778 const union mv *mv, 9779 const filter_t filters[8], 9780 short *coeffs, 9781 struct mb_info *mbi, 9782 int x, 9783 int y, 9784 int w, 9785 int h, 9786 int start_b 9787 ) 9788 { 9789 const unsigned char *predict; 9790 int b = start_b; 9791 const int b_w = 4; 9792 const int b_h = 4; 9794 x += mv->d.x >> 3; 9795 y += mv->d.y >> 3; 9797 /* Need two pixels left/above, 3 right/below for 6-tap */ 9798 if (x < 2 || x + b_w - 1 + 3 >= w || y < 2 || y + b_h - 1 + 3 >= h) 9799 { 9800 reference += (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9801 build_mc_border(emul_block, 9802 reference - 2 - 2 * stride, stride, 9803 x - 2, y - 2, b_w + 5, b_h + 5, w, h); 9804 reference = emul_block + 2 * stride + 2; 9805 reference -= (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9806 } 9808 predict = filter_block(output, reference, stride, mv, filters); 9809 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9810 } 9812 static void 9813 predict_inter_emulated_edge(struct vp8_decoder_ctx *ctx, 9814 struct img_index *img, 9815 short *coeffs, 9816 struct mb_info *mbi, 9817 int mb_col, 9818 int mb_row) 9819 { 9820 /* TODO: move this into its own buffer. This only works because we 9821 * still have a border allocated. 9822 */ 9823 unsigned char *emul_block = ctx->frame_strg[0].img.img_data; 9824 unsigned char *reference; 9825 unsigned char *output; 9826 ptrdiff_t reference_offset; 9827 int w, h, x, y, b; 9828 union mv chroma_mv[4]; 9829 unsigned char *u = img->u, *v = img->v; 9830 int full_pixel = ctx->frame_hdr.version == 3; 9832 x = mb_col * 16; 9833 y = mb_row * 16; 9834 w = ctx->mb_cols * 16; 9835 h = ctx->mb_rows * 16; 9836 output = img->y; 9837 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9838 reference = output + reference_offset; 9840 if (mbi->base.y_mode != SPLITMV) 9841 { 9842 union mv uvmv; 9844 uvmv = mbi->base.mv; 9845 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9846 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9848 if (full_pixel) 9849 { 9850 uvmv.d.x &= ~7; 9851 uvmv.d.y &= ~7; 9852 } 9854 chroma_mv[0] = uvmv; 9855 chroma_mv[1] = uvmv; 9856 chroma_mv[2] = uvmv; 9857 chroma_mv[3] = uvmv; 9858 } 9859 else 9860 { 9861 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9862 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9863 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9864 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9865 } 9867 /* Luma */ 9868 for (b = 0; b < 16; b++) 9869 { 9870 union mv *ymv; 9872 if (mbi->base.y_mode != SPLITMV) 9873 ymv = &mbi->base.mv; 9874 else 9875 ymv = mbi->split.mvs + b; 9877 recon_1_edge_block(output, emul_block, reference, img->stride, 9878 ymv, ctx->subpixel_filters, 9879 coeffs, mbi, x, y, w, h, b); 9881 x += 4; 9882 output += 4; 9883 reference += 4; 9885 if ((b & 3) == 3) 9886 { 9887 x -= 16; 9888 y += 4; 9889 output += 4 * img->stride - 16; 9890 reference += 4 * img->stride - 16; 9891 } 9892 } 9893 x = mb_col * 16; 9894 y = mb_row * 16; 9896 /* Chroma */ 9897 x >>= 1; 9898 y >>= 1; 9899 w >>= 1; 9900 h >>= 1; 9902 for (b = 0; b < 4; b++) 9903 { 9904 recon_1_edge_block(u, emul_block, u + reference_offset, 9905 img->uv_stride, 9906 &chroma_mv[b], ctx->subpixel_filters, 9907 coeffs, mbi, x, y, w, h, b + 16); 9908 recon_1_edge_block(v, emul_block, v + reference_offset, 9909 img->uv_stride, 9910 &chroma_mv[b], ctx->subpixel_filters, 9911 coeffs, mbi, x, y, w, h, b + 20); 9912 u += 4; 9913 v += 4; 9914 x += 4; 9916 if (b & 1) 9917 { 9918 x -= 8; 9919 y += 4; 9920 u += 4 * img->uv_stride - 8; 9921 v += 4 * img->uv_stride - 8; 9922 } 9923 } 9925 } 9927 static void 9928 predict_inter(struct vp8_decoder_ctx *ctx, 9929 struct img_index *img, 9930 short *coeffs, 9931 struct mb_info *mbi) 9932 { 9933 unsigned char *y = img->y; 9934 unsigned char *u = img->u; 9935 unsigned char *v = img->v; 9936 ptrdiff_t reference_offset; 9937 union mv chroma_mv[4]; 9938 int full_pixel = ctx->frame_hdr.version == 3; 9939 int b; 9940 if (mbi->base.y_mode != SPLITMV) 9941 { 9942 union mv uvmv; 9944 uvmv = mbi->base.mv; 9945 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9946 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9948 if (full_pixel) 9949 { 9950 uvmv.d.x &= ~7; 9951 uvmv.d.y &= ~7; 9952 } 9954 chroma_mv[0] = 9955 chroma_mv[1] = 9956 chroma_mv[2] = 9957 chroma_mv[3] = uvmv; 9958 } 9959 else 9960 { 9961 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9962 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9963 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9964 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9965 } 9967 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9969 for (b = 0; b < 16; b++) 9970 { 9971 union mv *ymv; 9973 if (mbi->base.y_mode != SPLITMV) 9974 ymv = &mbi->base.mv; 9975 else 9976 ymv = mbi->split.mvs + b; 9978 recon_1_block(y, y + reference_offset, img->stride, 9979 ymv, ctx->subpixel_filters, coeffs, mbi, b); 9980 y += 4; 9982 if ((b & 3) == 3) 9983 y += 4 * img->stride - 16; 9984 } 9986 for (b = 0; b < 4; b++) 9987 { 9988 recon_1_block(u, u + reference_offset, 9989 img->uv_stride, &chroma_mv[b], 9990 ctx->subpixel_filters, coeffs, mbi, b + 16); 9991 recon_1_block(v, v + reference_offset, 9992 img->uv_stride, &chroma_mv[b], 9993 ctx->subpixel_filters, coeffs, mbi, b + 20); 9994 u += 4; 9995 v += 4; 9997 if (b & 1) 9998 { 9999 u += 4 * img->uv_stride - 8; 10000 v += 4 * img->uv_stride - 8; 10001 } 10002 } 10003 } 10005 void 10006 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg) 10007 { 10008 if (rcimg) 10009 { 10010 assert(rcimg->ref_cnt); 10011 rcimg->ref_cnt--; 10012 } 10013 } 10015 struct ref_cnt_img * 10016 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg) 10017 { 10018 rcimg->ref_cnt++; 10019 return rcimg; 10020 } 10022 struct ref_cnt_img * 10023 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames) 10024 { 10025 int i; 10027 for (i = 0; i < NUM_REF_FRAMES; i++) 10028 if (frames[i].ref_cnt == 0) 10029 { 10030 frames[i].ref_cnt = 1; 10031 return &frames[i]; 10033 } 10035 assert(0); 10036 return NULL; 10037 } 10039 static void 10040 fixup_left(unsigned char *predict, 10041 int width, 10042 int stride, 10043 unsigned int row, 10044 enum prediction_mode mode) 10045 { 10046 /* The left column of out-of-frame pixels is taken to be 129, 10047 * unless we're doing DC_PRED, in which case we duplicate the 10048 * above row, unless this is also row 0, in which case we use 10049 * 129. 10050 */ 10051 unsigned char *left = predict - 1; 10052 int i; 10054 if (mode == DC_PRED && row) 10055 { 10056 unsigned char *above = predict - stride; 10058 for (i = 0; i < width; i++) 10059 { 10060 *left = above[i]; 10061 left += stride; 10062 } 10063 } 10064 else 10065 { 10066 /* Need to re-set the above row, in case the above MB was 10067 * DC_PRED. 10068 */ 10069 left -= stride; 10071 for (i = -1; i < width; i++) 10072 { 10073 *left = 129; 10074 left += stride; 10075 } 10076 } 10077 } 10078 static void 10079 fixup_above(unsigned char *predict, 10080 int width, 10081 int stride, 10082 unsigned int col, 10083 enum prediction_mode mode) 10084 { 10085 /* The above row of out-of-frame pixels is taken to be 127, 10086 * unless we're doing DC_PRED, in which case we duplicate the 10087 * left col, unless this is also col 0, in which case we use 10088 * 127. 10089 */ 10090 unsigned char *above = predict - stride; 10091 int i; 10093 if (mode == DC_PRED && col) 10094 { 10095 unsigned char *left = predict - 1; 10097 for (i = 0; i < width; i++) 10098 { 10099 above[i] = *left; 10100 left += stride; 10101 } 10102 } 10103 else 10104 /* Need to re-set the left col, in case the last MB was 10105 * DC_PRED. 10106 */ 10107 memset(above - 1, 127, width + 1); 10109 memset(above + width, 127, 4); // for above-right subblock modes 10110 } 10112 void 10113 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx) 10114 { 10116 int i; 10117 unsigned char *this_frame_base; 10119 if (ctx->frame_hdr.frame_size_updated) 10120 { 10121 for (i = 0; i < NUM_REF_FRAMES; i++) 10122 { 10123 unsigned int w = ctx->mb_cols * 16 + BORDER_PIXELS * 2; 10124 unsigned int h = ctx->mb_rows * 16 + BORDER_PIXELS * 2; 10125 vpx_img_free(&ctx->frame_strg[i].img); 10126 ctx->frame_strg[i].ref_cnt = 0; 10127 ctx->ref_frames[i] = NULL; 10129 if (!vpx_img_alloc(&ctx->frame_strg[i].img, 10130 IMG_FMT_I420, w, h, 16)) 10131 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10132 "Failed to allocate %dx%d" 10133 " framebuffer", 10134 w, h); 10136 vpx_img_set_rect(&ctx->frame_strg[i].img, 10137 BORDER_PIXELS, BORDER_PIXELS, 10138 ctx->frame_hdr.kf.w, ctx->frame_hdr.kf.h); 10140 } 10142 if (ctx->frame_hdr.version) 10143 ctx->subpixel_filters = bilinear_filters; 10144 else 10145 ctx->subpixel_filters = sixtap_filters; 10146 } 10148 /* Find a free framebuffer to predict into */ 10149 if (ctx->ref_frames[CURRENT_FRAME]) 10150 vp8_dixie_release_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 10152 ctx->ref_frames[CURRENT_FRAME] = 10153 vp8_dixie_find_free_ref_frame(ctx->frame_strg); 10154 this_frame_base = ctx->ref_frames[CURRENT_FRAME]->img.img_data; 10156 /* Calculate offsets to the other reference frames */ 10157 for (i = 0; i < NUM_REF_FRAMES; i++) 10158 { 10159 struct ref_cnt_img *ref = ctx->ref_frames[i]; 10161 ctx->ref_frame_offsets[i] = 10162 ref ? ref->img.img_data - this_frame_base : 0; 10163 } 10165 /* TODO: No need to do this on every frame... */ 10166 } 10168 void 10169 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx) 10170 { 10171 int i; 10172 for (i = 0; i < NUM_REF_FRAMES; i++) 10173 { 10174 vpx_img_free(&ctx->frame_strg[i].img); 10175 ctx->frame_strg[i].ref_cnt = 0; 10176 ctx->ref_frames[i] = NULL; 10177 } 10178 } 10180 void 10181 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10182 unsigned int row, 10183 unsigned int start_col, 10184 unsigned int num_cols) 10185 { 10186 struct img_index img; 10187 struct mb_info *mbi; 10188 unsigned int col; 10189 short *coeffs; 10191 /* Adjust pointers based on row, start_col */ 10192 img.stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 10193 img.uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 10194 img.y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 10195 img.u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 10196 img.v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 10197 img.y += (img.stride * row + start_col) * 16; 10198 img.u += (img.uv_stride * row + start_col) * 8; 10199 img.v += (img.uv_stride * row + start_col) * 8; 10200 mbi = ctx->mb_info_rows[row] + start_col; 10201 coeffs = ctx->tokens[row & (ctx->token_hdr.partitions - 1)].coeffs 10202 + 25 * 16 * start_col; 10204 /* Fix up the out-of-frame pixels */ 10205 if (start_col == 0) 10206 { 10207 fixup_left(img.y, 16, img.stride, row, mbi->base.y_mode); 10208 fixup_left(img.u, 8, img.uv_stride, row, mbi->base.uv_mode); 10209 fixup_left(img.v, 8, img.uv_stride, row, mbi->base.uv_mode); 10211 if (row == 0) 10212 *(img.y - img.stride - 1) = 127; 10213 } 10215 for (col = start_col; col < start_col + num_cols; col++) 10216 { 10217 if (row == 0) 10218 { 10219 fixup_above(img.y, 16, img.stride, col, mbi->base.y_mode); 10220 fixup_above(img.u, 8, img.uv_stride, col, 10221 mbi->base.uv_mode); 10222 fixup_above(img.v, 8, img.uv_stride, col, 10223 mbi->base.uv_mode); 10224 } 10226 if (mbi->base.y_mode <= B_PRED) 10227 { 10228 predict_intra_luma(img.y, img.stride, mbi, coeffs); 10229 predict_intra_chroma(img.u, img.v, img.uv_stride, mbi, 10230 coeffs); 10231 } 10232 else 10233 { 10234 if (mbi->base.y_mode != SPLITMV) // && != BPRED 10235 fixup_dc_coeffs(mbi, coeffs); 10237 if (mbi->base.need_mc_border) 10238 predict_inter_emulated_edge(ctx, &img, coeffs, mbi, col, 10239 row); 10240 else 10241 predict_inter(ctx, &img, coeffs, mbi); 10242 } 10244 /* Advance to the next macroblock */ 10245 mbi++; 10246 img.y += 16; 10247 img.u += 8; 10248 img.v += 8; 10249 coeffs += 25 * 16; 10250 } 10252 if (col == ctx->mb_cols) 10253 { 10254 /* Extend the last row by four pixels for intra prediction. 10255 * This will be propagated later by copy_down. 10256 */ 10257 uint32_t *extend = (uint32_t *)(img.y + 15 * img.stride); 10258 uint32_t val = 0x01010101 * img.y[-1 + 15 * img.stride]; 10259 *extend = val; 10260 } 10261 } 10263 ---- End code block ---------------------------------------- 10264 20.15. predict.h 10266 ---- Begin code block -------------------------------------- 10268 /* 10269 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10270 * 10271 * Use of this source code is governed by a BSD-style license 10272 * that can be found in the LICENSE file in the root of the source 10273 * tree. An additional intellectual property rights grant can be 10274 * found in the file PATENTS. All contributing project authors may 10275 * be found in the AUTHORS file in the root of the source tree. 10276 */ 10277 #ifndef PREDICT_H 10278 #define PREDICT_H 10280 void 10281 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx); 10283 void 10284 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx); 10286 void 10287 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10288 unsigned int row, 10289 unsigned int start_col, 10290 unsigned int num_cols); 10292 void 10293 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg); 10295 struct ref_cnt_img * 10296 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg); 10298 struct ref_cnt_img * 10299 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames); 10301 #endif 10303 ---- End code block ---------------------------------------- 10305 20.16. tokens.c 10307 ---- Begin code block -------------------------------------- 10308 /* 10309 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10310 * 10311 * Use of this source code is governed by a BSD-style license 10312 * that can be found in the LICENSE file in the root of the source 10313 * tree. An additional intellectual property rights grant can be 10314 * found in the file PATENTS. All contributing project authors may 10315 * be found in the AUTHORS file in the root of the source tree. 10316 */ 10317 #include "vpx_codec_internal.h" 10318 #include "dixie.h" 10319 #include "tokens.h" 10320 #include 10321 #include 10322 #include 10324 enum 10325 { 10326 EOB_CONTEXT_NODE, 10327 ZERO_CONTEXT_NODE, 10328 ONE_CONTEXT_NODE, 10329 LOW_VAL_CONTEXT_NODE, 10330 TWO_CONTEXT_NODE, 10331 THREE_CONTEXT_NODE, 10332 HIGH_LOW_CONTEXT_NODE, 10333 CAT_ONE_CONTEXT_NODE, 10334 CAT_THREEFOUR_CONTEXT_NODE, 10335 CAT_THREE_CONTEXT_NODE, 10336 CAT_FIVE_CONTEXT_NODE 10337 }; 10338 enum 10339 { 10340 ZERO_TOKEN, 10341 ONE_TOKEN, 10342 TWO_TOKEN, 10343 THREE_TOKEN, 10344 FOUR_TOKEN, 10345 DCT_VAL_CATEGORY1, 10346 DCT_VAL_CATEGORY2, 10347 DCT_VAL_CATEGORY3, 10348 DCT_VAL_CATEGORY4, 10349 DCT_VAL_CATEGORY5, 10350 DCT_VAL_CATEGORY6, 10351 DCT_EOB_TOKEN, 10352 MAX_ENTROPY_TOKENS 10353 }; 10354 struct extrabits 10355 { 10356 short min_val; 10357 short length; 10358 unsigned char probs[12]; 10359 }; 10360 static const unsigned int left_context_index[25] = 10361 { 10362 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10363 4, 4, 5, 5, 6, 6, 7, 7, 8 10364 }; 10365 static const unsigned int above_context_index[25] = 10366 { 10367 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10368 4, 5, 4, 5, 6, 7, 6, 7, 8 10369 }; 10370 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10371 static const unsigned int bands_x[16] = 10372 { 10373 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10374 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10375 }; 10376 #undef X 10377 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10378 { 10379 { 0, -1, { 0, 0, 0, 0, 0, 0, 10380 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10381 { 1, 0, { 0, 0, 0, 0, 0, 0, 10382 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10383 { 2, 0, { 0, 0, 0, 0, 0, 0, 10384 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10385 { 3, 0, { 0, 0, 0, 0, 0, 0, 10386 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10387 { 4, 0, { 0, 0, 0, 0, 0, 0, 10388 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10389 { 5, 0, { 159, 0, 0, 0, 0, 0, 10390 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10391 { 7, 1, { 145, 165, 0, 0, 0, 0, 10392 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10393 { 11, 2, { 140, 148, 173, 0, 0, 0, 10394 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10395 { 19, 3, { 135, 140, 155, 176, 0, 0, 10396 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10397 { 35, 4, { 130, 134, 141, 157, 180, 0, 10398 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10399 { 67, 10, { 129, 130, 133, 140, 153, 177, 10400 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10401 { 0, -1, { 0, 0, 0, 0, 0, 0, 10402 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10404 }; 10405 static const unsigned int zigzag[16] = 10406 { 10407 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10408 }; 10410 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10411 v = (bool_get_bit(bool) ? -value_to_sign \ 10412 : value_to_sign) * dqf[!!c]; 10414 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10415 if (!bool_get(bool, probability)) goto branch; 10417 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10418 if (!bool_get(bool, probability)) \ 10419 { \ 10420 prob = type_probs; \ 10421 if(c<15) {\ 10422 ++c; \ 10423 prob += bands_x[c]; \ 10424 goto branch; \ 10425 }\ 10426 else \ 10427 goto BLOCK_FINISHED; /*for malformed input */\ 10428 } 10430 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10431 DECODE_AND_APPLYSIGN(val) \ 10432 prob = type_probs + (ENTROPY_NODES*2); \ 10433 if(c < 15){\ 10434 b_tokens[zigzag[c]] = v; \ 10435 ++c; \ 10436 goto DO_WHILE; }\ 10437 b_tokens[zigzag[15]] = v; \ 10438 goto BLOCK_FINISHED; 10440 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10441 val += bool_get(bool, extrabits[t].probs[bits_count]) << bits_count; 10443 static int 10444 decode_mb_tokens(struct bool_decoder *bool, 10445 token_entropy_ctx_t left, 10446 token_entropy_ctx_t above, 10447 short *tokens, 10448 enum prediction_mode mode, 10449 coeff_probs_table_t probs, 10450 short factor[TOKEN_BLOCK_TYPES][2]) 10451 { 10452 int i, stop, type; 10453 int c, t, v; 10454 int val, bits_count; 10455 int eob_mask; 10456 short *b_tokens; /* tokens for this block */ 10457 unsigned char *type_probs; /* probabilities for this block type */ 10458 unsigned char *prob; 10459 short *dqf; 10461 eob_mask = 0; 10463 if (mode != B_PRED && mode != SPLITMV) 10464 { 10465 i = 24; 10466 stop = 24; 10467 type = 1; 10468 b_tokens = tokens + 24 * 16; 10469 dqf = factor[TOKEN_BLOCK_Y2]; 10470 } 10471 else 10472 { 10473 i = 0; 10474 stop = 16; 10475 type = 3; 10476 b_tokens = tokens; 10477 dqf = factor[TOKEN_BLOCK_Y1]; 10478 } 10480 /* Save a pointer to the coefficient probs for the current type. 10481 * Need to repeat this whenever type changes. 10482 */ 10483 type_probs = probs[type][0][0]; 10485 BLOCK_LOOP: 10486 t = left[left_context_index[i]] + above[above_context_index[i]]; 10487 c = !type; /* all blocks start at 0 except type 0, which starts 10488 * at 1. */ 10490 prob = type_probs; 10491 prob += t * ENTROPY_NODES; 10493 DO_WHILE: 10494 prob += bands_x[c]; 10495 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], BLOCK_FINISHED); 10497 CHECK_0_: 10499 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10500 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10501 ONE_CONTEXT_NODE_0_); 10502 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10503 LOW_VAL_CONTEXT_NODE_0_); 10504 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10505 HIGH_LOW_CONTEXT_NODE_0_); 10506 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10507 CAT_THREEFOUR_CONTEXT_NODE_0_); 10508 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10509 CAT_FIVE_CONTEXT_NODE_0_); 10510 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10511 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10513 do 10514 { 10515 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count); 10516 bits_count -- ; 10517 } 10518 while (bits_count >= 0); 10520 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10522 CAT_FIVE_CONTEXT_NODE_0_: 10523 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10524 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10525 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10526 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10527 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10528 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10529 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10531 CAT_THREEFOUR_CONTEXT_NODE_0_: 10532 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10533 CAT_THREE_CONTEXT_NODE_0_); 10534 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10535 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10536 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10537 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10538 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10539 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10541 CAT_THREE_CONTEXT_NODE_0_: 10542 val = extrabits[DCT_VAL_CATEGORY3].min_val; 10543 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 10544 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 10545 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 10546 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10548 HIGH_LOW_CONTEXT_NODE_0_: 10549 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 10550 CAT_ONE_CONTEXT_NODE_0_); 10552 val = extrabits[DCT_VAL_CATEGORY2].min_val; 10553 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 10554 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 10555 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10557 CAT_ONE_CONTEXT_NODE_0_: 10558 val = extrabits[DCT_VAL_CATEGORY1].min_val; 10559 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 10560 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10562 LOW_VAL_CONTEXT_NODE_0_: 10563 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 10564 TWO_CONTEXT_NODE_0_); 10565 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 10566 THREE_CONTEXT_NODE_0_); 10567 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 10569 THREE_CONTEXT_NODE_0_: 10570 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 10572 TWO_CONTEXT_NODE_0_: 10573 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 10575 ONE_CONTEXT_NODE_0_: 10576 DECODE_AND_APPLYSIGN(1); 10577 prob = type_probs + ENTROPY_NODES; 10579 if (c < 15) 10580 { 10581 b_tokens[zigzag[c]] = v; 10582 ++c; 10583 goto DO_WHILE; 10584 } 10586 b_tokens[zigzag[15]] = v; 10587 BLOCK_FINISHED: 10588 eob_mask |= (c > 1) << i; 10589 t = (c != !type); // any nonzero data? 10590 eob_mask |= t << 31; 10592 left[left_context_index[i]] = above[above_context_index[i]] = t; 10593 b_tokens += 16; 10595 i++; 10596 if (i < stop) 10597 goto BLOCK_LOOP; 10599 if (i == 25) 10600 { 10601 type = 0; 10602 i = 0; 10603 stop = 16; 10604 type_probs = probs[type][0][0]; 10605 b_tokens = tokens; 10606 dqf = factor[TOKEN_BLOCK_Y1]; 10607 goto BLOCK_LOOP; 10608 } 10610 if (i == 16) 10611 { 10612 type = 2; 10613 type_probs = probs[type][0][0]; 10614 stop = 24; 10615 dqf = factor[TOKEN_BLOCK_UV]; 10616 goto BLOCK_LOOP; 10617 } 10619 return eob_mask; 10620 } 10622 static void 10623 reset_row_context(token_entropy_ctx_t *left) 10624 { 10625 memset(left, 0, sizeof(*left)); 10626 } 10628 static void 10629 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 10630 { 10631 memset(above, 0, cols * sizeof(*above)); 10632 } 10634 static void 10635 reset_mb_context(token_entropy_ctx_t *left, 10636 token_entropy_ctx_t *above, 10637 enum prediction_mode mode) 10638 { 10639 /* Reset the macroblock context on the left and right. We have to 10640 * preserve the context of the second order block if this mode 10641 * would not have updated it. 10642 */ 10643 memset(left, 0, sizeof((*left)[0]) * 8); 10644 memset(above, 0, sizeof((*above)[0]) * 8); 10646 if (mode != B_PRED && mode != SPLITMV) 10647 { 10648 (*left)[8] = 0; 10649 (*above)[8] = 0; 10650 } 10651 } 10653 void 10654 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 10655 unsigned int partition, 10656 unsigned int row, 10657 unsigned int start_col, 10658 unsigned int num_cols) 10659 { 10660 struct token_decoder *tokens = &ctx->tokens[partition]; 10661 short *coeffs = tokens->coeffs + 25 * 16 * start_col; 10662 unsigned int col; 10663 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 10664 + start_col; 10665 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 10666 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 10668 if (row == 0) 10669 reset_above_context(above, num_cols); 10671 if (start_col == 0) 10672 reset_row_context(left); 10674 for (col = start_col; col < start_col + num_cols; col++) 10675 { 10676 memset(coeffs, 0, 25 * 16 * sizeof(short)); 10678 if (mbi->base.skip_coeff) 10679 { 10680 reset_mb_context(left, above, mbi->base.y_mode); 10681 mbi->base.eob_mask = 0; 10682 } 10683 else 10684 { 10685 struct dequant_factors *dqf; 10687 dqf = ctx->dequant_factors + mbi->base.segment_id; 10688 mbi->base.eob_mask = 10689 decode_mb_tokens(&tokens->bool, 10690 *left, *above, 10691 coeffs, 10692 mbi->base.y_mode, 10693 ctx->entropy_hdr.coeff_probs, 10694 dqf->factor); 10695 } 10697 above++; 10698 mbi++; 10699 coeffs += 25 * 16; 10700 } 10701 } 10703 void 10704 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 10705 { 10706 unsigned int partitions = ctx->token_hdr.partitions; 10708 if (ctx->frame_hdr.frame_size_updated) 10709 { 10710 unsigned int i; 10711 unsigned int coeff_row_sz = 10712 ctx->mb_cols * 25 * 16 * sizeof(short); 10714 for (i = 0; i < partitions; i++) 10715 { 10716 free(ctx->tokens[i].coeffs); 10717 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 10719 if (!ctx->tokens[i].coeffs) 10720 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10721 NULL); 10722 } 10724 free(ctx->above_token_entropy_ctx); 10725 ctx->above_token_entropy_ctx = 10726 calloc(ctx->mb_cols, sizeof(*ctx->above_token_entropy_ctx)); 10728 if (!ctx->above_token_entropy_ctx) 10729 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, NULL); 10730 } 10731 } 10733 void 10734 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 10735 { 10736 int i; 10738 for (i = 0; i < MAX_PARTITIONS; i++) 10739 free(ctx->tokens[i].coeffs); 10741 free(ctx->above_token_entropy_ctx); 10742 } 10744 ---- End code block ---------------------------------------- 10746 20.17. tokens.h 10748 ---- Begin code block -------------------------------------- 10750 /* 10751 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10752 * 10753 * Use of this source code is governed by a BSD-style license 10754 * that can be found in the LICENSE file in the root of the source 10755 * tree. An additional intellectual property rights grant can be 10756 * found in the file PATENTS. All contributing project authors may 10757 * be found in the AUTHORS file in the root of the source tree. 10758 */ 10759 #include "vpx_codec_internal.h" 10760 #include "dixie.h" 10761 #include "tokens.h" 10762 #include 10763 #include 10764 #include 10766 enum 10767 { 10768 EOB_CONTEXT_NODE, 10769 ZERO_CONTEXT_NODE, 10770 ONE_CONTEXT_NODE, 10771 LOW_VAL_CONTEXT_NODE, 10772 TWO_CONTEXT_NODE, 10773 THREE_CONTEXT_NODE, 10774 HIGH_LOW_CONTEXT_NODE, 10775 CAT_ONE_CONTEXT_NODE, 10776 CAT_THREEFOUR_CONTEXT_NODE, 10777 CAT_THREE_CONTEXT_NODE, 10778 CAT_FIVE_CONTEXT_NODE 10779 }; 10780 enum 10781 { 10782 ZERO_TOKEN, 10783 ONE_TOKEN, 10784 TWO_TOKEN, 10785 THREE_TOKEN, 10786 FOUR_TOKEN, 10787 DCT_VAL_CATEGORY1, 10788 DCT_VAL_CATEGORY2, 10789 DCT_VAL_CATEGORY3, 10790 DCT_VAL_CATEGORY4, 10791 DCT_VAL_CATEGORY5, 10792 DCT_VAL_CATEGORY6, 10793 DCT_EOB_TOKEN, 10794 MAX_ENTROPY_TOKENS 10795 }; 10796 struct extrabits 10797 { 10798 short min_val; 10799 short length; 10800 unsigned char probs[12]; 10801 }; 10802 static const unsigned int left_context_index[25] = 10803 { 10804 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10805 4, 4, 5, 5, 6, 6, 7, 7, 8 10806 }; 10807 static const unsigned int above_context_index[25] = 10808 { 10809 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10810 4, 5, 4, 5, 6, 7, 6, 7, 8 10811 }; 10812 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10813 static const unsigned int bands_x[16] = 10814 { 10815 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10816 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10817 }; 10818 #undef X 10819 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10820 { 10821 { 0, -1, { 0, 0, 0, 0, 0, 0, 10822 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10823 { 1, 0, { 0, 0, 0, 0, 0, 0, 10824 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10825 { 2, 0, { 0, 0, 0, 0, 0, 0, 10826 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10827 { 3, 0, { 0, 0, 0, 0, 0, 0, 10828 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10829 { 4, 0, { 0, 0, 0, 0, 0, 0, 10830 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10831 { 5, 0, { 159, 0, 0, 0, 0, 0, 10832 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10833 { 7, 1, { 145, 165, 0, 0, 0, 0, 10834 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10835 { 11, 2, { 140, 148, 173, 0, 0, 0, 10836 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10837 { 19, 3, { 135, 140, 155, 176, 0, 0, 10838 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10839 { 35, 4, { 130, 134, 141, 157, 180, 0, 10840 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10841 { 67, 10, { 129, 130, 133, 140, 153, 177, 10842 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10843 { 0, -1, { 0, 0, 0, 0, 0, 0, 10844 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10845 }; 10846 static const unsigned int zigzag[16] = 10847 { 10848 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10849 }; 10851 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10852 v = (bool_get_bit(bool) ? -value_to_sign \ 10853 : value_to_sign) * dqf[!!c]; 10855 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10856 if (!bool_get(bool, probability)) goto branch; 10858 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10859 if (!bool_get(bool, probability)) \ 10860 { \ 10861 prob = type_probs; \ 10862 if(c<15) {\ 10863 ++c; \ 10864 prob += bands_x[c]; \ 10865 goto branch; \ 10866 }\ 10867 else \ 10868 goto BLOCK_FINISHED; /*for malformed input */\ 10869 } 10871 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10872 DECODE_AND_APPLYSIGN(val) \ 10873 prob = type_probs + (ENTROPY_NODES*2); \ 10874 if(c < 15){\ 10875 b_tokens[zigzag[c]] = v; \ 10876 ++c; \ 10877 goto DO_WHILE; }\ 10878 b_tokens[zigzag[15]] = v; \ 10879 goto BLOCK_FINISHED; 10881 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10882 val += bool_get(bool, extrabits[t].probs[bits_count]) << bits_count; 10884 static int 10885 decode_mb_tokens(struct bool_decoder *bool, 10886 token_entropy_ctx_t left, 10887 token_entropy_ctx_t above, 10888 short *tokens, 10889 enum prediction_mode mode, 10890 coeff_probs_table_t probs, 10891 short factor[TOKEN_BLOCK_TYPES][2]) 10892 { 10893 int i, stop, type; 10894 int c, t, v; 10895 int val, bits_count; 10896 int eob_mask; 10897 short *b_tokens; /* tokens for this block */ 10898 unsigned char *type_probs; /* probabilities for this block type */ 10899 unsigned char *prob; 10900 short *dqf; 10902 eob_mask = 0; 10904 if (mode != B_PRED && mode != SPLITMV) 10905 { 10906 i = 24; 10907 stop = 24; 10908 type = 1; 10909 b_tokens = tokens + 24 * 16; 10910 dqf = factor[TOKEN_BLOCK_Y2]; 10911 } 10912 else 10913 { 10914 i = 0; 10915 stop = 16; 10916 type = 3; 10917 b_tokens = tokens; 10918 dqf = factor[TOKEN_BLOCK_Y1]; 10919 } 10921 /* Save a pointer to the coefficient probs for the current type. 10923 * Need to repeat this whenever type changes. 10924 */ 10925 type_probs = probs[type][0][0]; 10927 BLOCK_LOOP: 10928 t = left[left_context_index[i]] + above[above_context_index[i]]; 10929 c = !type; /* all blocks start at 0 except type 0, which starts 10930 * at 1. */ 10932 prob = type_probs; 10933 prob += t * ENTROPY_NODES; 10935 DO_WHILE: 10936 prob += bands_x[c]; 10937 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], BLOCK_FINISHED); 10939 CHECK_0_: 10940 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10941 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10942 ONE_CONTEXT_NODE_0_); 10943 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10944 LOW_VAL_CONTEXT_NODE_0_); 10945 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10946 HIGH_LOW_CONTEXT_NODE_0_); 10947 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10948 CAT_THREEFOUR_CONTEXT_NODE_0_); 10949 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10950 CAT_FIVE_CONTEXT_NODE_0_); 10951 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10952 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10954 do 10955 { 10956 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count); 10957 bits_count -- ; 10958 } 10959 while (bits_count >= 0); 10961 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10963 CAT_FIVE_CONTEXT_NODE_0_: 10964 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10965 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10966 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10967 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10968 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10969 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10970 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10972 CAT_THREEFOUR_CONTEXT_NODE_0_: 10973 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10974 CAT_THREE_CONTEXT_NODE_0_); 10975 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10976 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10977 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10978 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10979 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10980 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10982 CAT_THREE_CONTEXT_NODE_0_: 10983 val = extrabits[DCT_VAL_CATEGORY3].min_val; 10984 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 10985 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 10986 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 10987 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10989 HIGH_LOW_CONTEXT_NODE_0_: 10990 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 10991 CAT_ONE_CONTEXT_NODE_0_); 10993 val = extrabits[DCT_VAL_CATEGORY2].min_val; 10994 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 10995 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 10996 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10998 CAT_ONE_CONTEXT_NODE_0_: 10999 val = extrabits[DCT_VAL_CATEGORY1].min_val; 11000 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 11001 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 11003 LOW_VAL_CONTEXT_NODE_0_: 11004 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 11005 TWO_CONTEXT_NODE_0_); 11006 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 11007 THREE_CONTEXT_NODE_0_); 11008 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 11010 THREE_CONTEXT_NODE_0_: 11011 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 11013 TWO_CONTEXT_NODE_0_: 11014 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 11016 ONE_CONTEXT_NODE_0_: 11017 DECODE_AND_APPLYSIGN(1); 11018 prob = type_probs + ENTROPY_NODES; 11019 if (c < 15) 11020 { 11021 b_tokens[zigzag[c]] = v; 11022 ++c; 11023 goto DO_WHILE; 11024 } 11026 b_tokens[zigzag[15]] = v; 11027 BLOCK_FINISHED: 11028 eob_mask |= (c > 1) << i; 11029 t = (c != !type); // any nonzero data? 11030 eob_mask |= t << 31; 11032 left[left_context_index[i]] = above[above_context_index[i]] = t; 11033 b_tokens += 16; 11035 i++; 11037 if (i < stop) 11038 goto BLOCK_LOOP; 11040 if (i == 25) 11041 { 11042 type = 0; 11043 i = 0; 11044 stop = 16; 11045 type_probs = probs[type][0][0]; 11046 b_tokens = tokens; 11047 dqf = factor[TOKEN_BLOCK_Y1]; 11048 goto BLOCK_LOOP; 11049 } 11051 if (i == 16) 11052 { 11053 type = 2; 11054 type_probs = probs[type][0][0]; 11055 stop = 24; 11056 dqf = factor[TOKEN_BLOCK_UV]; 11057 goto BLOCK_LOOP; 11058 } 11060 return eob_mask; 11061 } 11063 static void 11064 reset_row_context(token_entropy_ctx_t *left) 11065 { 11066 memset(left, 0, sizeof(*left)); 11067 } 11069 static void 11070 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 11071 { 11072 memset(above, 0, cols * sizeof(*above)); 11073 } 11075 static void 11076 reset_mb_context(token_entropy_ctx_t *left, 11077 token_entropy_ctx_t *above, 11078 enum prediction_mode mode) 11079 { 11080 /* Reset the macroblock context on the left and right. We have to 11081 * preserve the context of the second order block if this mode 11082 * would not have updated it. 11083 */ 11084 memset(left, 0, sizeof((*left)[0]) * 8); 11085 memset(above, 0, sizeof((*above)[0]) * 8); 11087 if (mode != B_PRED && mode != SPLITMV) 11088 { 11089 (*left)[8] = 0; 11090 (*above)[8] = 0; 11091 } 11092 } 11094 void 11095 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 11096 unsigned int partition, 11097 unsigned int row, 11098 unsigned int start_col, 11099 unsigned int num_cols) 11100 { 11101 struct token_decoder *tokens = &ctx->tokens[partition]; 11102 short *coeffs = tokens->coeffs + 25 * 16 * start_col; 11103 unsigned int col; 11104 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 11105 + start_col; 11106 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 11107 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 11109 if (row == 0) 11110 reset_above_context(above, num_cols); 11112 if (start_col == 0) 11113 reset_row_context(left); 11115 for (col = start_col; col < start_col + num_cols; col++) 11116 { 11117 memset(coeffs, 0, 25 * 16 * sizeof(short)); 11119 if (mbi->base.skip_coeff) 11120 { 11121 reset_mb_context(left, above, mbi->base.y_mode); 11122 mbi->base.eob_mask = 0; 11123 } 11124 else 11125 { 11126 struct dequant_factors *dqf; 11128 dqf = ctx->dequant_factors + mbi->base.segment_id; 11129 mbi->base.eob_mask = 11130 decode_mb_tokens(&tokens->bool, 11131 *left, *above, 11132 coeffs, 11133 mbi->base.y_mode, 11134 ctx->entropy_hdr.coeff_probs, 11135 dqf->factor); 11136 } 11138 above++; 11139 mbi++; 11140 coeffs += 25 * 16; 11141 } 11142 } 11144 void 11145 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 11146 { 11147 unsigned int partitions = ctx->token_hdr.partitions; 11149 if (ctx->frame_hdr.frame_size_updated) 11150 { 11151 unsigned int i; 11152 unsigned int coeff_row_sz = 11153 ctx->mb_cols * 25 * 16 * sizeof(short); 11155 for (i = 0; i < partitions; i++) 11156 { 11157 free(ctx->tokens[i].coeffs); 11158 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 11159 if (!ctx->tokens[i].coeffs) 11160 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 11161 NULL); 11162 } 11164 free(ctx->above_token_entropy_ctx); 11165 ctx->above_token_entropy_ctx = 11166 calloc(ctx->mb_cols, sizeof(*ctx->above_token_entropy_ctx)); 11168 if (!ctx->above_token_entropy_ctx) 11169 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, NULL); 11170 } 11171 } 11173 void 11174 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 11175 { 11176 int i; 11178 for (i = 0; i < MAX_PARTITIONS; i++) 11179 free(ctx->tokens[i].coeffs); 11181 free(ctx->above_token_entropy_ctx); 11182 } 11184 ---- End code block ---------------------------------------- 11186 20.18. vp8_prob_data.h 11188 ---- Begin code block -------------------------------------- 11190 static const 11191 unsigned char k_coeff_entropy_update_probs[BLOCK_TYPES][COEF_BANDS] 11192 [PREV_COEF_CONTEXTS] 11193 [ENTROPY_NODES] = 11194 { 11195 { 11196 { 11197 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11198 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11199 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11200 }, 11201 { 11202 {176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11203 {223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11204 {249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11206 }, 11207 { 11208 {255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11209 {234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11210 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11211 }, 11212 { 11213 {255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11214 {239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11215 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11216 }, 11217 { 11218 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11219 {251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11220 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11221 }, 11222 { 11223 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11224 {251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11225 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11226 }, 11227 { 11228 {255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255, }, 11229 {250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255, }, 11230 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11231 }, 11232 { 11233 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11234 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11235 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11236 }, 11237 }, 11238 { 11239 { 11240 {217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11241 {225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255, }, 11242 {234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255, }, 11243 }, 11244 { 11245 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11246 {223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11247 {238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, }, 11248 }, 11249 { 11250 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11251 {249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11252 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11253 }, 11254 { 11255 {255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11256 {247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11257 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11258 }, 11259 { 11260 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11261 {252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11262 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11263 }, 11264 { 11265 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11266 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11267 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11268 }, 11269 { 11270 {255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11271 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11272 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11273 }, 11274 { 11275 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11276 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11277 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11278 }, 11279 }, 11280 { 11281 { 11282 {186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255, }, 11283 {234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255, }, 11284 {251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255, }, 11285 }, 11286 { 11287 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11288 {236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11289 {251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255, }, 11290 }, 11291 { 11292 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11293 {254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11294 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11295 }, 11296 { 11297 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11298 {254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11299 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11300 }, 11301 { 11302 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11303 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11304 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11305 }, 11306 { 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 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11310 }, 11311 { 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 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11315 }, 11316 { 11317 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11318 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11319 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11320 }, 11321 }, 11322 { 11323 { 11324 {248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11325 {250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255, }, 11326 {248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255, }, 11327 }, 11328 { 11329 {255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11330 {246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11331 {252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255, }, 11332 }, 11333 { 11334 {255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11335 {248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11336 {253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, }, 11337 }, 11338 { 11339 {255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11340 {245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11341 {253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11342 }, 11343 { 11344 {255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11345 {252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11346 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11347 }, 11348 { 11349 {255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11350 {249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11351 {255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11352 }, 11353 { 11354 {255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11355 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11356 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11357 }, 11358 { 11359 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11360 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11361 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11362 }, 11363 }, 11364 }; 11366 static const 11367 unsigned char k_default_y_mode_probs [] = 11368 { 112, 86, 140, 37}; 11370 static const 11371 unsigned char k_default_uv_mode_probs [] = 11372 { 162, 101, 204}; 11374 static const 11375 unsigned char k_default_coeff_probs [BLOCK_TYPES][COEF_BANDS] 11376 [PREV_COEF_CONTEXTS][ENTROPY_NODES] = 11377 { 11378 { /* block type 0 */ 11379 { /* coeff band 0 */ 11380 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11381 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11382 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11383 }, 11384 { /* coeff band 1 */ 11385 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 11386 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 11387 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 11388 }, 11389 { /* coeff band 2 */ 11390 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 11391 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 11392 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 11393 }, 11394 { /* coeff band 3 */ 11395 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 11396 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 11397 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 11398 }, 11399 { /* coeff band 4 */ 11400 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 11401 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 11402 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 11403 }, 11404 { /* coeff band 5 */ 11405 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 11406 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 11407 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 11408 }, 11409 { /* coeff band 6 */ 11410 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 11411 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 11412 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 11413 }, 11414 { /* coeff band 7 */ 11415 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11416 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11417 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11418 } 11419 }, 11420 { /* block type 1 */ 11421 { /* coeff band 0 */ 11422 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 11423 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 11424 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 11425 }, 11426 { /* coeff band 1 */ 11427 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 11428 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 11429 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 11430 }, 11431 { /* coeff band 2 */ 11432 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 11433 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 11434 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 11435 }, 11436 { /* coeff band 3 */ 11437 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 11438 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 11439 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 11440 }, 11441 { /* coeff band 4 */ 11442 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 11443 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 11444 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 11445 }, 11446 { /* coeff band 5 */ 11447 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 11448 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 11449 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 11450 }, 11451 { /* coeff band 6 */ 11452 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 11453 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 11454 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 11455 }, 11456 { /* coeff band 7 */ 11457 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 11458 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11459 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 11460 } 11461 }, 11462 { /* block type 2 */ 11463 { /* coeff band 0 */ 11464 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 11465 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 11466 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 11467 }, 11468 { /* coeff band 1 */ 11469 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 11470 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 11471 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 11472 }, 11473 { /* coeff band 2 */ 11474 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 11475 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 11476 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 11477 }, 11478 { /* coeff band 3 */ 11479 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 11480 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 11481 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 11482 }, 11483 { /* coeff band 4 */ 11484 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11485 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 11486 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11487 }, 11488 { /* coeff band 5 */ 11489 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11490 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11491 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11492 }, 11493 { /* coeff band 6 */ 11494 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 11495 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 11496 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11497 }, 11498 { /* coeff band 7 */ 11499 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11500 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11501 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11502 } 11503 }, 11504 { /* block type 3 */ 11505 { /* coeff band 0 */ 11506 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 11507 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 11508 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 11509 }, 11510 { /* coeff band 1 */ 11511 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 11512 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 11513 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 11514 }, 11515 { /* coeff band 2 */ 11516 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 11517 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 11518 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 11519 }, 11520 { /* coeff band 3 */ 11521 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 11522 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 11523 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 11524 }, 11525 { /* coeff band 4 */ 11526 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 11527 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 11528 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 11529 }, 11530 { /* coeff band 5 */ 11531 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 11532 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 11533 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 11534 }, 11535 { /* coeff band 6 */ 11536 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 11537 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 11538 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 11540 }, 11541 { /* coeff band 7 */ 11542 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11543 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11544 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11545 } 11546 } 11547 }; 11549 static const 11550 unsigned char k_mv_entropy_update_probs[2][MV_PROB_CNT] = 11551 { 11552 { 11553 237, 11554 246, 11555 253, 253, 254, 254, 254, 254, 254, 11556 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 11557 }, 11558 { 11559 231, 11560 243, 11561 245, 253, 254, 254, 254, 254, 254, 11562 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 11563 } 11564 }; 11566 static const 11567 unsigned char k_default_mv_probs[2][MV_PROB_CNT] = 11568 { 11569 { /* row */ 11570 162, /* is short */ 11571 128, /* sign */ 11572 225, 146, 172, 147, 214, 39, 156, /* short tree */ 11573 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 /* long bits */ 11574 }, 11575 { 11576 164, 11577 128, 11578 204, 170, 119, 235, 140, 230, 228, 11579 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 11581 } 11582 }; 11584 ---- End code block ---------------------------------------- 11585 20.19. vpx_codec_internal.h 11587 ---- Begin code block -------------------------------------- 11589 /* 11590 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 11591 * 11592 * Use of this source code is governed by a BSD-style license 11593 * that can be found in the LICENSE file in the root of the source 11594 * tree. An additional intellectual property rights grant can be 11595 * found in the file PATENTS. All contributing project authors may 11596 * be found in the AUTHORS file in the root of the source tree. 11597 */ 11599 /*!\file decoder_impl.h 11600 * \brief Describes the decoder algorithm interface for algorithm 11601 * implementations. 11602 * 11603 * This file defines the private structures and data types that are 11604 * only relevant to implementing an algorithm, as opposed to using 11605 * it. 11606 * 11607 * To create a decoder algorithm class, an interface structure is put 11608 * into the global namespace: 11609 *
11610	    *     my_codec.c:
11611	    *       vpx_codec_iface_t my_codec = {
11612	    *           "My Codec v1.0",
11613	    *           VPX_CODEC_ALG_ABI_VERSION,
11614	    *           ...
11615	    *       };
11616	    *     
11617 * 11618 * An application instantiates a specific decoder instance by using 11619 * vpx_codec_init() and a pointer to the algorithm's interface 11620 * structure: 11621 *
11622	    *     my_app.c:
11623	    *       extern vpx_codec_iface_t my_codec;
11624	    *       {
11625	    *           vpx_codec_ctx_t algo;
11626	    *           res = vpx_codec_init(&algo, &my_codec);
11627	    *       }
11628	    *     
11629 * 11630 * Once initialized, the instance is manged using other functions 11631 * from the vpx_codec_* family. 11633 */ 11634 #ifndef VPX_CODEC_INTERNAL_H 11635 #define VPX_CODEC_INTERNAL_H 11636 #include "vpx_decoder.h" 11637 #include 11639 /*!\brief Current ABI version number 11640 * 11641 * \internal 11642 * If this file is altered in any way that changes the ABI, this 11643 * value must be bumped. Examples include, but are not limited to, 11644 * changing types, removing or reassigning enums, 11645 * adding/removing/rearranging fields to structures 11646 */ 11647 #define VPX_CODEC_INTERNAL_ABI_VERSION (3) 11649 typedef struct vpx_codec_alg_priv vpx_codec_alg_priv_t; 11651 /*!\brief init function pointer prototype 11652 * 11653 * Performs algorithm-specific initialization of the decoder context. 11654 * This function is called by the generic vpx_codec_init() wrapper 11655 * function, so plugins implementing this interface may trust the 11656 * input parameters to be properly initialized. 11657 * 11658 * \param[in] ctx Pointer to this instance's context 11659 * \retval #VPX_CODEC_OK 11660 * The input stream was recognized and decoder initialized. 11661 * \retval #VPX_CODEC_MEM_ERROR 11662 * Memory operation failed. 11663 */ 11664 typedef vpx_codec_err_t (*vpx_codec_init_fn_t)(vpx_codec_ctx_t *ctx); 11666 /*!\brief destroy function pointer prototype 11667 * 11668 * Performs algorithm-specific destruction of the decoder context. 11669 * This function is called by the generic vpx_codec_destroy() wrapper 11670 * function, so plugins implementing this interface may trust the 11671 * input parameters to be properly initialized. 11672 * 11673 * \param[in] ctx Pointer to this instance's context 11674 * \retval #VPX_CODEC_OK 11675 * The input stream was recognized and decoder initialized. 11676 * \retval #VPX_CODEC_MEM_ERROR 11677 * Memory operation failed. 11678 */ 11679 typedef vpx_codec_err_t (*vpx_codec_destroy_fn_t)( 11680 vpx_codec_alg_priv_t *ctx); 11682 /*!\brief parse stream info function pointer prototype 11683 * 11684 * Performs high level parsing of the bitstream. This function is 11685 * called by the generic vpx_codec_parse_stream() wrapper function, 11686 * so plugins implementing this interface may trust the input 11687 * parameters to be properly initialized. 11688 * 11689 * \param[in] data Pointer to a block of data to parse 11690 * \param[in] data_sz Size of the data buffer 11691 * \param[in,out] si Pointer to stream info to update. The size 11692 * member \ref MUST be properly initialized, 11693 * but \ref MAY be clobbered by the 11694 * algorithm. This parameter \ref MAY be 11695 * NULL. 11696 * 11697 * \retval #VPX_CODEC_OK 11698 * Bitstream is parsable and stream information updated 11699 */ 11700 typedef vpx_codec_err_t (*vpx_codec_peek_si_fn_t)( 11701 const uint8_t *data, 11702 unsigned int data_sz, 11703 vpx_codec_stream_info_t *si); 11705 /*!\brief Return information about the current stream. 11706 * 11707 * Returns information about the stream that has been parsed during 11708 * decoding. 11709 * 11710 * \param[in] ctx Pointer to this instance's context 11711 * \param[in,out] si Pointer to stream info to update. The size 11712 * member \ref MUST be properly initialized, 11713 * but \ref MAY be clobbered by the 11714 * algorithm. This parameter \ref MAY be 11715 * NULL. 11716 * 11717 * \retval #VPX_CODEC_OK 11718 * Bitstream is parsable and stream information updated 11719 */ 11720 typedef vpx_codec_err_t (*vpx_codec_get_si_fn_t)( 11721 vpx_codec_alg_priv_t *ctx, 11722 vpx_codec_stream_info_t *si); 11724 /*!\brief control function pointer prototype 11725 * 11726 * This function is used to exchange algorithm specific data with the 11727 * decoder instance. This can be used to implement features specific 11728 * to a particular algorithm. 11729 * 11730 * This function is called by the generic vpx_codec_control() wrapper 11731 * function, so plugins implementing this interface may trust the 11732 * input parameters to be properly initialized. However, this 11733 * interface does not provide type safety for the exchanged data or 11734 * assign meanings to the control codes. Those details should be 11735 * specified in the algorithm's header file. In particular, the 11736 * ctrl_id parameter is guaranteed to exist in the algorithm's 11737 * control mapping table, and the data paramter may be NULL. 11738 * 11739 * 11740 * \param[in] ctx Pointer to this instance's context 11741 * \param[in] ctrl_id Algorithm specific control identifier 11742 * \param[in,out] data Data to exchange with algorithm instance. 11743 * 11744 * \retval #VPX_CODEC_OK 11745 * The internal state data was deserialized. 11746 */ 11747 typedef vpx_codec_err_t (*vpx_codec_control_fn_t)( 11748 vpx_codec_alg_priv_t *ctx, 11749 int ctrl_id, 11750 va_list ap); 11752 /*!\brief control function pointer mapping 11753 * 11754 * This structure stores the mapping between control identifiers and 11755 * implementing functions. Each algorithm provides a list of these 11756 * mappings. This list is searched by the vpx_codec_control() wrapper 11757 * function to determine which function to invoke. The special 11758 * value {0, NULL} is used to indicate end-of-list, and must be 11759 * present. The special value {0, } can be used as a 11760 * catch-all mapping. This implies that ctrl_id values chosen by the 11761 * algorithm \ref MUST be non-zero. 11762 */ 11763 typedef const struct 11764 { 11765 int ctrl_id; 11766 vpx_codec_control_fn_t fn; 11767 } vpx_codec_ctrl_fn_map_t; 11769 /*!\brief decode data function pointer prototype 11770 * 11771 * Processes a buffer of coded data. If the processing results in a 11772 * new decoded frame becoming available, #VPX_CODEC_CB_PUT_SLICE and 11773 * #VPX_CODEC_CB_PUT_FRAME events are generated as appropriate. This 11774 * function is called by the generic vpx_codec_decode() wrapper 11775 * function, so plugins implementing this interface may trust the 11776 * input parameters to be properly initialized. 11777 * 11778 * \param[in] ctx Pointer to this instance's context 11779 * \param[in] data Pointer to this block of new coded data. If 11780 * NULL, a #VPX_CODEC_CB_PUT_FRAME event is 11781 * posted for the previously decoded frame. 11782 * \param[in] data_sz Size of the coded data, in bytes. 11783 * 11784 * \return Returns #VPX_CODEC_OK if the coded data was processed 11785 * completely and future pictures can be decoded without 11786 * error. Otherwise, see the descriptions of the other error 11787 * codes in ::vpx_codec_err_t for recoverability 11788 * capabilities. 11789 */ 11790 typedef vpx_codec_err_t (*vpx_codec_decode_fn_t)( 11791 vpx_codec_alg_priv_t *ctx, 11792 const uint8_t *data, 11793 unsigned int data_sz, 11794 void *user_priv, 11795 long deadline); 11797 /*!\brief Decoded frames iterator 11798 * 11799 * Iterates over a list of the frames available for display. The 11800 * iterator storage should be initialized to NULL to start the 11801 * iteration. Iteration is complete when this function returns NULL. 11802 * 11803 * The list of available frames becomes valid upon completion of the 11804 * vpx_codec_decode call, and remains valid until the next call to 11805 * vpx_codec_decode. 11806 * 11807 * \param[in] ctx Pointer to this instance's context 11808 * \param[in out] iter Iterator storage, initialized to NULL 11809 * 11810 * \return Returns a pointer to an image, if one is ready for 11811 * display. Frames produced will always be in PTS 11812 * (presentation time stamp) order. 11813 */ 11814 typedef vpx_image_t*(*vpx_codec_get_frame_fn_t)( 11815 vpx_codec_alg_priv_t *ctx, 11816 vpx_codec_iter_t *iter); 11818 /*\brief e_xternal Memory Allocation memory map get iterator 11819 * 11820 * Iterates over a list of the memory maps requested by the decoder. 11821 * The iterator storage should be initialized to NULL to start the 11822 * iteration. Iteration is complete when this function returns NULL. 11824 * 11825 * \param[in out] iter Iterator storage, initialized to NULL 11826 * 11827 * \return Returns a pointer to an memory segment descriptor, or NULL 11828 * to indicate end-of-list. 11829 */ 11830 typedef vpx_codec_err_t (*vpx_codec_get_mmap_fn_t)( 11831 const vpx_codec_ctx_t *ctx, 11832 vpx_codec_mmap_t *mmap, 11833 vpx_codec_iter_t *iter); 11835 /*\brief e_xternal Memory Allocation memory map set iterator 11836 * 11837 * Sets a memory descriptor inside the decoder instance. 11838 * 11839 * \param[in] ctx Pointer to this instance's context 11840 * \param[in] mmap Memory map to store. 11841 * 11842 * \retval #VPX_CODEC_OK 11843 * The memory map was accepted and stored. 11844 * \retval #VPX_CODEC_MEM_ERROR 11845 * The memory map was rejected. 11846 */ 11847 typedef vpx_codec_err_t (*vpx_codec_set_mmap_fn_t)( 11848 vpx_codec_ctx_t *ctx, 11849 const vpx_codec_mmap_t *mmap); 11851 typedef vpx_codec_err_t (*vpx_codec_encode_fn_t)( 11852 vpx_codec_alg_priv_t *ctx, 11853 const vpx_image_t *img, 11854 vpx_codec_pts_t pts, 11855 unsigned long duration, 11856 vpx_enc_frame_flags_t flags, 11857 unsigned long deadline); 11858 typedef const vpx_codec_cx_pkt_t*(*vpx_codec_get_cx_data_fn_t)( 11859 vpx_codec_alg_priv_t *ctx, 11860 vpx_codec_iter_t *iter); 11862 typedef vpx_codec_err_t 11863 (*vpx_codec_enc_config_set_fn_t)( 11864 vpx_codec_alg_priv_t *ctx, 11865 const vpx_codec_enc_cfg_t *cfg); 11866 typedef vpx_fixed_buf_t * 11867 (*vpx_codec_get_global_headers_fn_t)(vpx_codec_alg_priv_t *ctx); 11869 typedef vpx_image_t * 11870 (*vpx_codec_get_preview_frame_fn_t)(vpx_codec_alg_priv_t *ctx); 11872 /*!\brief usage configuration mapping 11873 * 11874 * This structure stores the mapping between usage identifiers and 11875 * configuration structures. Each algorithm provides a list of these 11876 * mappings. This list is searched by the 11877 * vpx_codec_enc_config_default() wrapper function to determine which 11878 * config to return. The special value {-1, {0}} is used to indicate 11879 * end-of-list, and must be present. At least one mapping must be 11880 * present, in addition to the end-of-list. 11881 * 11882 */ 11883 typedef const struct 11884 { 11885 int usage; 11886 vpx_codec_enc_cfg_t cfg; 11887 } vpx_codec_enc_cfg_map_t; 11889 #define NOT_IMPLEMENTED 0 11891 /*!\brief Decoder algorithm interface interface 11892 * 11893 * All decoders \ref MUST expose a variable of this type. 11894 */ 11895 struct vpx_codec_iface 11896 { 11897 const char *name; 11898 int abi_version; 11899 vpx_codec_caps_t caps; 11900 vpx_codec_init_fn_t init; 11901 vpx_codec_destroy_fn_t destroy; 11902 vpx_codec_ctrl_fn_map_t *ctrl_maps; 11903 vpx_codec_get_mmap_fn_t get_mmap; 11904 vpx_codec_set_mmap_fn_t set_mmap; 11905 struct 11906 { 11907 vpx_codec_peek_si_fn_t peek_si; 11908 vpx_codec_get_si_fn_t get_si; 11909 vpx_codec_decode_fn_t decode; 11910 vpx_codec_get_frame_fn_t get_frame; 11911 } dec; 11912 struct 11913 { 11914 vpx_codec_enc_cfg_map_t *cfg_maps; 11915 vpx_codec_encode_fn_t encode; 11916 vpx_codec_get_cx_data_fn_t get_cx_data; 11917 vpx_codec_enc_config_set_fn_t cfg_set; 11918 vpx_codec_get_global_headers_fn_t get_glob_hdrs; 11919 vpx_codec_get_preview_frame_fn_t get_preview; 11920 } enc; 11921 }; 11923 /*!\brief Callback function pointer / user data pair storage */ 11924 typedef struct vpx_codec_priv_cb_pair 11925 { 11926 union 11927 { 11928 vpx_codec_put_frame_cb_fn_t put_frame; 11929 vpx_codec_put_slice_cb_fn_t put_slice; 11930 }; 11931 void *user_priv; 11932 } vpx_codec_priv_cb_pair_t; 11934 /*!\brief Instance private storage 11935 * 11936 * This structure is allocated by the algorithm's init function. It 11937 * can be extended in one of two ways. First, a second, algorithm 11938 * specific structure can be allocated and the priv member pointed to 11939 * it. Alternatively, this structure can be made the first member of 11940 * the algorithm specific structure, and the pointer casted to the 11941 * proper type. 11942 */ 11943 struct vpx_codec_priv 11944 { 11945 unsigned int sz; 11946 vpx_codec_iface_t *iface; 11947 struct vpx_codec_alg_priv *alg_priv; 11948 const char *err_detail; 11949 vpx_codec_flags_t init_flags; 11950 struct 11951 { 11952 vpx_codec_priv_cb_pair_t put_frame_cb; 11953 vpx_codec_priv_cb_pair_t put_slice_cb; 11954 } dec; 11955 struct 11956 { 11957 int tbd; 11958 struct vpx_fixed_buf cx_data_dst_buf; 11959 unsigned int cx_data_pad_before; 11960 unsigned int cx_data_pad_after; 11961 vpx_codec_cx_pkt_t cx_data_pkt; 11962 } enc; 11963 }; 11964 #undef VPX_CTRL_USE_TYPE 11965 #define VPX_CTRL_USE_TYPE(id, typ) \ 11966 static typ id##__value(va_list args) \ 11967 {return va_arg(args, typ);} \ 11968 static typ id##__convert(void *x)\ 11969 {\ 11970 union\ 11971 {\ 11972 void *x;\ 11973 typ d;\ 11974 } u;\ 11975 u.x = x;\ 11976 return u.d;\ 11977 } 11979 #undef VPX_CTRL_USE_TYPE_DEPRECATED 11980 #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \ 11981 static typ id##__value(va_list args) \ 11982 {return va_arg(args, typ);} \ 11983 static typ id##__convert(void *x)\ 11984 {\ 11985 union\ 11986 {\ 11987 void *x;\ 11988 typ d;\ 11989 } u;\ 11990 u.x = x;\ 11991 return u.d;\ 11992 } 11994 #define CAST(id, arg) id##__value(arg) 11995 #define RECAST(id, x) id##__convert(x) 11997 /* Internal Utility Functions 11998 * 11999 * The following functions are indended to be used inside algorithms 12000 * as utilities for manipulating vpx_codec_* data structures. 12001 */ 12002 struct vpx_codec_pkt_list 12003 { 12004 unsigned int cnt; 12005 unsigned int max; 12006 struct vpx_codec_cx_pkt pkts[1]; 12007 }; 12009 #define vpx_codec_pkt_list_decl(n)\ 12010 union {struct vpx_codec_pkt_list head;\ 12011 struct {struct vpx_codec_pkt_list head;\ 12012 struct vpx_codec_cx_pkt pkts[n];} alloc;} 12014 #define vpx_codec_pkt_list_init(m)\ 12015 (m)->alloc.head.cnt = 0,\ 12016 (m)->alloc.head.max = \ 12017 sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0]) 12019 int 12020 vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *, 12021 const struct vpx_codec_cx_pkt *); 12023 const vpx_codec_cx_pkt_t* 12024 vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list, 12025 vpx_codec_iter_t *iter); 12027 #include 12028 #include 12029 struct vpx_internal_error_info 12030 { 12031 vpx_codec_err_t error_code; 12032 int has_detail; 12033 char detail[80]; 12034 int setjmp; 12035 jmp_buf jmp; 12036 }; 12038 static void vpx_internal_error(struct vpx_internal_error_info *info, 12039 vpx_codec_err_t error, 12040 const char *fmt, 12041 ...) 12042 { 12043 va_list ap; 12045 info->error_code = error; 12046 info->has_detail = 0; 12048 if (fmt) 12049 { 12050 size_t sz = sizeof(info->detail); 12052 info->has_detail = 1; 12053 va_start(ap, fmt); 12054 vsnprintf(info->detail, sz - 1, fmt, ap); 12055 va_end(ap); 12056 info->detail[sz-1] = '\0'; 12058 } 12060 if (info->setjmp) 12061 longjmp(info->jmp, info->error_code); 12062 } 12063 #endif 12065 ---- End code block ---------------------------------------- 12067 20.20. vpx_decoder.h 12069 ---- Begin code block -------------------------------------- 12071 /* 12072 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 12073 * 12074 * Use of this source code is governed by a BSD-style license 12075 * that can be found in the LICENSE file in the root of the source 12076 * tree. An additional intellectual property rights grant can be 12077 * found in the file PATENTS. All contributing project authors may 12078 * be found in the AUTHORS file in the root of the source tree. 12079 */ 12081 /*!\defgroup decoder Decoder Algorithm Interface 12082 * \ingroup codec 12083 * This abstraction allows applications using this decoder to easily 12084 * support multiple video formats with minimal code duplication. This 12085 * section describes the interface common to all decoders. 12086 * @{ 12087 */ 12089 /*!\file vpx_decoder.h 12090 * \brief Describes the decoder algorithm interface to applications. 12091 * 12092 * This file describes the interface between an application and a 12093 * video decoder algorithm. 12094 * 12095 */ 12096 #ifdef __cplusplus 12097 extern "C" { 12098 #endif 12100 #ifndef VPX_DECODER_H 12101 #define VPX_DECODER_H 12102 #include "vpx_codec.h" 12103 /*!\brief Current ABI version number 12104 * 12105 * \internal 12106 * If this file is altered in any way that changes the ABI, this 12107 * value must be bumped. Examples include, but are not limited 12108 * to, changing types, removing or reassigning enums, 12109 * adding/removing/rearranging fields to structures 12110 */ 12111 #define VPX_DECODER_ABI_VERSION (2 + VPX_CODEC_ABI_VERSION) 12113 /*! \brief Decoder capabilities bitfield 12114 * 12115 * Each decoder advertises the capabilities it supports as part 12116 * of its ::vpx_codec_iface_t interface structure. Capabilities 12117 * are extra interfaces or functionality, and are not required 12118 * to be supported by a decoder. 12119 * 12120 * The available flags are specifiedby VPX_CODEC_CAP_* defines. 12121 */ 12122 #define VPX_CODEC_CAP_PUT_SLICE 0x10000 /**< Will issue put_slice 12123 callbacks */ 12124 #define VPX_CODEC_CAP_PUT_FRAME 0x20000 /**< Will issue put_frame 12125 callbacks */ 12126 #define VPX_CODEC_CAP_POSTPROC 0x40000 /**< Can postprocess decoded 12127 frame */ 12129 /*! \brief Initialization-time Feature Enabling 12130 * 12131 * Certain codec features must be known at initialization time, 12132 * to allow for proper memory allocation. 12133 * 12134 * The available flags are specified by VPX_CODEC_USE_* defines. 12135 */ 12136 #define VPX_CODEC_USE_POSTPROC 0x10000 /**< Postprocess decoded 12137 frame */ 12139 /*!\brief Stream properties 12140 * 12141 * This structure is used to query or set properties of the 12142 * decoded stream. Algorithms may extend this structure with data 12143 * specific to their bitstream by setting the sz member 12144 * appropriately. 12145 */ 12146 typedef struct vpx_codec_stream_info 12147 { 12148 unsigned int sz; /**< Size of this structure */ 12149 unsigned int w; /**< Width (or 0 for unknown/default) */ 12150 unsigned int h; /**< Height (or 0 for unknown/default) */ 12151 unsigned int is_kf; /**< Current frame is a keyframe */ 12152 } vpx_codec_stream_info_t; 12154 /* REQUIRED FUNCTIONS 12155 * 12156 * The following functions are required to be implemented for all 12157 * decoders. They represent the base case functionality expected 12158 * of all decoders. 12159 */ 12161 /*!\brief Initialization Configurations 12162 * 12163 * This structure is used to pass init time configuration options 12164 * to the decoder. 12165 */ 12166 typedef struct vpx_codec_dec_cfg 12167 { 12168 unsigned int threads; /**< Maximum number of threads to use, 12169 default 1 */ 12170 unsigned int w; /**< Width */ 12171 unsigned int h; /**< Height */ 12172 } vpx_codec_dec_cfg_t; /**< alias for struct vpx_codec_dec_cfg */ 12174 /*!\brief Initialize a decoder instance 12175 * 12176 * Initializes a decoder context using the given interface. 12177 * Applications should call the vpx_codec_dec_init convenience 12178 * macro instead of this function directly, to ensure that the 12179 * ABI version number parameter is properly initialized. 12180 * 12181 * In XMA mode (activated by setting VPX_CODEC_USE_XMA in the 12182 * flags parameter), the storage pointed to by the cfg parameter 12183 * must be kept readable and stable until all memory maps have 12184 * been set. 12185 * 12186 * \param[in] ctx Pointer to this instance's context. 12187 * \param[in] iface Pointer to the alogrithm interface to 12188 * use. 12189 * \param[in] cfg Configuration to use, if known. May be 12190 * NULL. 12191 * \param[in] flags Bitfield of VPX_CODEC_USE_* flags 12192 * \param[in] ver ABI version number. Must be set to 12193 * VPX_DECODER_ABI_VERSION 12194 * \retval #VPX_CODEC_OK 12195 * The decoder algorithm initialized. 12196 * \retval #VPX_CODEC_MEM_ERROR 12197 * Memory allocation failed. 12198 */ 12199 vpx_codec_err_t vpx_codec_dec_init_ver( 12200 vpx_codec_ctx_t *ctx, 12201 vpx_codec_iface_t *iface, 12202 vpx_codec_dec_cfg_t *cfg, 12203 vpx_codec_flags_t flags, 12204 int ver); 12206 /*!\brief Convenience macro for vpx_codec_dec_init_ver() 12207 * 12208 * Ensures the ABI version parameter is properly set. 12209 */ 12210 #define vpx_codec_dec_init(ctx, iface, cfg, flags) \ 12211 vpx_codec_dec_init_ver(ctx, iface, cfg, flags, \ 12212 VPX_DECODER_ABI_VERSION) 12214 /*!\brief Parse stream info from a buffer 12215 * 12216 * Performs high level parsing of the bitstream. Construction of 12217 * a decoder context is not necessary. Can be used to determine 12218 * if the bitstream is of the proper format, and to extract 12219 * information from the stream. 12220 * 12221 * \param[in] iface Pointer to the alogrithm interface 12222 * \param[in] data Pointer to a block of data to parse 12223 * \param[in] data_sz Size of the data buffer 12224 * \param[in,out] si Pointer to stream info to update. The 12225 * size member 12226 * \ref MUST be properly initialized, but 12227 * \ref MAY be clobbered by the 12228 * algorithm. This parameter \ref MAY be 12229 * NULL. 12230 * 12231 * \retval #VPX_CODEC_OK 12232 * Bitstream is parsable and stream information updated 12233 */ 12234 vpx_codec_err_t vpx_codec_peek_stream_info( 12235 vpx_codec_iface_t *iface, 12236 const uint8_t *data, 12237 unsigned int data_sz, 12238 vpx_codec_stream_info_t *si); 12240 /*!\brief Return information about the current stream. 12241 * 12242 * Returns information about the stream that has been parsed 12243 * during decoding. 12244 * 12245 * \param[in] ctx Pointer to this instance's context 12246 * \param[in,out] si Pointer to stream info to update. The 12247 * size member \ref MUST be properly 12248 * initialized, but \ref MAY be clobbered 12249 * by the algorithm. This parameter \ref 12250 * MAY be NULL. 12251 * 12252 * \retval #VPX_CODEC_OK 12253 * Bitstream is parsable and stream information updated 12254 */ 12255 vpx_codec_err_t vpx_codec_get_stream_info( 12256 vpx_codec_ctx_t *ctx, 12257 vpx_codec_stream_info_t *si); 12259 /*!\brief Decode data 12260 * 12261 * Processes a buffer of coded data. If the processing results in 12262 * a new decoded frame becoming available, PUT_SLICE and 12263 * PUT_FRAME events may be generated, as appropriate. Encoded 12264 * data \ref MUST be passed in DTS (decode time stamp) order. 12265 * Frames produced will always be in PTS (presentation time 12266 * stamp) order. 12267 * 12268 * \param[in] ctx Pointer to this instance's context 12269 * \param[in] data Pointer to this block of new coded 12270 * data. If NULL, a 12271 * VPX_CODEC_CB_PUT_FRAME event is posted 12272 * for the previously decoded frame. 12273 * \param[in] data_sz Size of the coded data, in bytes. 12274 * \param[in] user_priv Application specific data to associate 12275 * with this frame. 12276 * \param[in] deadline Soft deadline the decoder should 12277 * attempt to meet, in us. Set to zero 12278 * for unlimited. 12279 * 12280 * \return Returns #VPX_CODEC_OK if the coded data was processed 12281 * completely and future pictures can be decoded without 12282 * error. Otherwise, see the descriptions of the other 12283 * error codes in ::vpx_codec_err_t for recoverability 12284 * capabilities. 12285 */ 12286 vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, 12287 const uint8_t *data, 12288 unsigned int data_sz, 12289 void *user_priv, 12290 long deadline); 12292 /*!\brief Decoded frames iterator 12293 * 12294 * Iterates over a list of the frames available for display. The 12295 * iterator storage should be initialized to NULL to start the 12296 * iteration. Iteration is complete when this function returns 12297 * NULL. 12298 * 12299 * The list of available frames becomes valid upon completion of 12300 * the vpx_codec_decode call, and remains valid until the next 12301 * call to vpx_codec_decode. 12302 * 12303 * \param[in] ctx Pointer to this instance's context 12304 * \param[in,out] iter Iterator storage, initialized to NULL 12305 * 12306 * \return Returns a pointer to an image, if one is ready for 12307 * display. Frames produced will always be in PTS 12308 * (presentation time stamp) order. 12309 */ 12310 vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, 12311 vpx_codec_iter_t *iter); 12313 /*!\defgroup cap_put_frame Frame-Based Decoding Functions 12314 * 12315 * The following functions are required to be implemented for all 12316 * decoders that advertise the VPX_CODEC_CAP_PUT_FRAME 12317 * capability. Calling these functions for codecs that don't 12318 * advertise this capability will result in an error code being 12319 * returned, usually VPX_CODEC_ERROR 12320 * @{ 12321 */ 12323 /*!\brief put frame callback prototype 12324 * 12325 * This callback is invoked by the decoder to notify the 12326 * application of the availability of decoded image data. 12327 */ 12328 typedef void (*vpx_codec_put_frame_cb_fn_t)( 12329 void *user_priv, 12330 const vpx_image_t *img); 12332 /*!\brief Register for notification of frame completion. 12333 * 12334 * Registers a given function to be called when a decoded frame 12335 * is available. 12336 * 12337 * \param[in] ctx Pointer to this instance's context 12338 * \param[in] cb Pointer to the callback function 12339 * \param[in] user_priv User's private data 12340 * 12341 * \retval #VPX_CODEC_OK 12342 * Callback successfully registered. 12343 * \retval #VPX_CODEC_ERROR 12344 * Decoder context not initialized, or algorithm not capable 12345 * of posting slice completion. 12346 */ 12347 vpx_codec_err_t vpx_codec_register_put_frame_cb( 12348 vpx_codec_ctx_t *ctx, 12349 vpx_codec_put_frame_cb_fn_t cb, 12350 void *user_priv); 12352 /*!@} - end defgroup cap_put_frame */ 12354 /*!\defgroup cap_put_slice Slice-Based Decoding Functions 12355 * 12356 * The following functions are required to be implemented for all 12357 * decoders that advertise the VPX_CODEC_CAP_PUT_SLICE 12358 * capability. Calling these functions for codecs that don't 12359 * advertise this capability will result in an error code being 12360 * returned, usually VPX_CODEC_ERROR 12361 * @{ 12362 */ 12364 /*!\brief put slice callback prototype 12365 * 12366 * This callback is invoked by the decoder to notify the 12367 * application of the availability of partially decoded image 12368 * data. 12369 */ 12370 typedef void (*vpx_codec_put_slice_cb_fn_t)( 12371 void *user_priv, 12372 const vpx_image_t *img, 12373 const vpx_image_rect_t *valid, 12374 const vpx_image_rect_t *update); 12376 /*!\brief Register for notification of slice completion. 12377 * 12378 * Registers a given function to be called when a decoded slice 12379 * is available. 12380 * 12381 * \param[in] ctx Pointer to this instance's context 12382 * \param[in] cb Pointer to the callback function 12383 * \param[in] user_priv User's private data 12384 * 12385 * \retval #VPX_CODEC_OK 12386 * Callback successfully registered. 12387 * \retval #VPX_CODEC_ERROR 12388 * Decoder context not initialized, or algorithm not capable 12389 * of posting slice completion. 12390 */ 12391 vpx_codec_err_t vpx_codec_register_put_slice_cb( 12392 vpx_codec_ctx_t *ctx, 12393 vpx_codec_put_slice_cb_fn_t cb, 12394 void *user_priv); 12396 /*!@} - end defgroup cap_put_slice*/ 12398 /*!@} - end defgroup decoder*/ 12400 #endif 12402 #ifdef __cplusplus 12403 } 12404 #endif 12406 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 12407 #include "vpx_decoder_compat.h" 12408 #endif 12410 ---- End code block ---------------------------------------- 12412 20.21. vpx_integer.h 12414 ---- Begin code block -------------------------------------- 12416 /* 12417 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 12418 * 12419 * Use of this source code is governed by a BSD-style license 12420 * that can be found in the LICENSE file in the root of the source 12421 * tree. An additional intellectual property rights grant can be 12422 * found in the file PATENTS. All contributing project authors may 12423 * be found in the AUTHORS file in the root of the source tree. 12424 */ 12426 #ifndef VPX_INTEGER_H 12427 #define VPX_INTEGER_H 12429 /* get ptrdiff_t, size_t, wchar_t, NULL */ 12430 #include 12432 #if defined(_MSC_VER) || defined(VPX_EMULATE_INTTYPES) 12433 typedef signed char int8_t; 12434 typedef signed short int16_t; 12435 typedef signed int int32_t; 12437 typedef unsigned char uint8_t; 12438 typedef unsigned short uint16_t; 12439 typedef unsigned int uint32_t; 12441 #if defined(_MSC_VER) 12442 typedef signed __int64 int64_t; 12443 typedef unsigned __int64 uint64_t; 12444 #define PRId64 "I64d" 12445 #endif 12447 #ifdef HAVE_ARMV6 12448 typedef unsigned int int_fast16_t; 12449 #else 12450 typedef signed short int_fast16_t; 12451 #endif 12452 typedef signed char int_fast8_t; 12453 typedef unsigned char uint_fast8_t; 12455 #ifndef _UINTPTR_T_DEFINED 12456 typedef unsigned int uintptr_t; 12457 #endif 12459 #else 12461 /* Most platforms have the C99 standard integer types. */ 12463 #if defined(__cplusplus) && !defined(__STDC_FORMAT_MACROS) 12464 #define __STDC_FORMAT_MACROS 12465 #endif 12466 #include 12467 #include 12469 #endif 12471 #endif 12472 ---- End code block ---------------------------------------- 12474 20.22. AUTHORS 12476 Aaron Watry 12478 Adrian Grange 12480 Alex Converse 12482 Andoni Morales Alastruey 12484 Andres Mejia 12486 Attila Nagy 12488 Fabio Pedretti 12490 Frank Galligan 12492 Fredrik Soederquist 12494 Fritz Koenig 12496 Gaute Strokkenes 12498 Giuseppe Scrivano 12500 Guillermo Ballester Valor 12502 Henrik Lundin 12504 James Berry 12506 James Zern 12508 Jan Kratochvil 12510 Jeff Muizelaar 12512 Jim Bankoski 12514 Johann Koenig 12516 John Koleszar 12518 Justin Clift 12519 Justin Lebar 12521 Luca Barbato 12523 Makoto Kato 12525 Martin Ettl 12527 Michael Kohler 12529 Mikhal Shemer 12531 Pascal Massimino 12533 Patrik Westin 12535 Paul Wilkins 12537 Pavol Rusnak 12539 Philip Jaegenstedt 12541 Scott LaVarnway 12543 Tero Rintaluoma 12545 Timothy B. Terriberry 12547 Tom Finegan 12549 Yaowu Xu 12551 Yunqing Wang 12553 Google Inc. 12555 The Mozilla Foundation 12557 The Xiph.Org Foundation 12559 20.23. LICENSE 12561 Copyright (c) 2010, Google Inc. All rights reserved. 12563 Redistribution and use in source and binary forms, with or without 12564 modification, are permitted provided that the following conditions 12565 are met: 12567 o Redistributions of source code must retain the above copyright 12568 notice, this list of conditions and the following disclaimer. 12570 o Redistributions in binary form must reproduce the above copyright 12571 notice, this list of conditions and the following disclaimer in 12572 the documentation and/or other materials provided with the 12573 distribution. 12575 o Neither the name of Google nor the names of its contributors may 12576 be used to endorse or promote products derived from this software 12577 without specific prior written permission. 12579 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 12580 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 12581 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 12582 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 12583 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 12584 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 12585 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 12586 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 12587 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 12588 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 12589 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 12590 POSSIBILITY OF SUCH DAMAGE. 12592 20.24. PATENTS 12594 Additional IP Rights Grant (Patents) 12596 "This implementation" means the copyrightable works distributed by 12597 Google as part of the WebM Project. 12599 Google hereby grants to you a perpetual, worldwide, non-exclusive, 12600 no-charge, royalty-free, irrevocable (except as stated in this 12601 section) patent license to make, have made, use, offer to sell, sell, 12602 import, transfer, and otherwise run, modify and propagate the 12603 contents of this implementation of VP8, where such license applies 12604 only to those patent claims, both currently owned by Google and 12605 acquired in the future, licensable by Google that are necessarily 12606 infringed by this implementation of VP8. This grant does not include 12607 claims that would be infringed only as a consequence of further 12608 modification of this implementation. If you or your agent or 12609 exclusive licensee institute or order or agree to the institution of 12610 patent litigation against any entity (including a cross-claim or 12611 counterclaim in a lawsuit) alleging that this implementation of VP8 12612 or any code incorporated within this implementation of VP8 12613 constitutes direct or contributory patent infringement, or inducement 12614 of patent infringement, then any patent rights granted to you under 12615 this License for this implementation of VP8 shall terminate as of the 12616 date such litigation is filed. 12618 21. References 12620 [ITU-R_BT.601] 12621 International Telecommunication Union, "ITU BT.601: Studio 12622 encoding parameters of digital television for standard 4:3 12623 and wide screen 16:9 aspect ratios", January 2007. 12625 [Bell] Bell, T., Cleary, J., and I. Witten, "Text Compression", 12626 1990. 12628 [Kernighan] 12629 Kernighan, B. and D. Ritchie, "The C Programming Language 12630 (2nd edition)", April 1988. 12632 [Loeffler] 12633 Loeffler, C., Ligtenberg , A., and G. Moschytz, "Practical 12634 Fast 1-D DCT Algorithms with 11 Multiplications", 12635 May 1989. 12637 [Shannon] Shannon, C., "A Mathematical Theory of Communication", 12638 Bell System Technical Journal Vol. 27, pp. 379-423, 623- 12639 656, July, October 1948. 12641 Authors' Addresses 12643 James Bankoski 12644 Google Inc. 12646 Email: jimbankoski@google.com 12648 John Koleszar 12649 Google Inc. 12651 Email: jkoleszar@google.com 12653 Lou Quillio 12654 Google Inc. 12656 Email: louquillio@google.com 12658 Janne Salonen 12659 Google Inc. 12661 Email: jsalonen@google.com 12663 Paul Wilkins 12664 Google Inc. 12666 Email: paulwilkins@google.com 12668 Yaowu Xu 12669 Google Inc. 12671 Email: yaowu@google.com