idnits 2.17.00 (12 Aug 2021) /tmp/idnits23406/draft-bankoski-vp8-bitstream-04.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 11704: '... member \ref MUST be properly init...' RFC 2119 keyword, line 11705: '... but \ref MAY be clobbered by t...' RFC 2119 keyword, line 11706: '... algorithm. This parameter \ref MAY be...' RFC 2119 keyword, line 11724: '... member \ref MUST be properly init...' RFC 2119 keyword, line 11725: '... 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 639 has weird spacing: '...typedef sign...' == Line 648 has weird spacing: '...def int int32...' == Line 6218 has weird spacing: '...ned int split...' == Line 6659 has weird spacing: '...decoder bool;...' == Line 7094 has weird spacing: '...tioning parti...' == (34 more instances...) -- The document date (June 16, 2011) is 3991 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 9976 == Missing Reference: 'A' is mentioned on line 2052, but not defined == Missing Reference: 'L' is mentioned on line 2052, but not defined -- Looks like a reference, but probably isn't: '8' on line 11102 == Missing Reference: '-1' is mentioned on line 9559, but not defined -- Looks like a reference, but probably isn't: '0' on line 12029 -- Looks like a reference, but probably isn't: '1' on line 12018 -- Looks like a reference, but probably isn't: '4' on line 9949 -- Looks like a reference, but probably isn't: '7' on line 9226 -- Looks like a reference, but probably isn't: '9' on line 8685 -- Looks like a reference, but probably isn't: '2' on line 11579 -- Looks like a reference, but probably isn't: '5' on line 9596 -- Looks like a reference, but probably isn't: '6' on line 9226 -- Looks like a reference, but probably isn't: '16' on line 10858 -- Looks like a reference, but probably isn't: '12' on line 10812 == Missing Reference: 'MVPcount' is mentioned on line 4895, but not defined == Missing Reference: 'MVPsign' is mentioned on line 4944, but not defined == Missing Reference: '-4096' is mentioned on line 5075, but not defined -- Looks like a reference, but probably isn't: '4095' on line 5075 -- Looks like a reference, but probably isn't: '128' on line 6329 == Missing Reference: 'SIGN' is mentioned on line 8166, but not defined == Missing Reference: 'NEAREST' is mentioned on line 8481, but not defined == Missing Reference: 'NEAR' is mentioned on line 8485, but not defined == Missing Reference: 'BEST' is mentioned on line 8500, but not defined -- Looks like a reference, but probably isn't: '15' on line 8502 -- Looks like a reference, but probably isn't: '10' on line 8685 -- Looks like a reference, but probably isn't: '18' on line 8828 -- Looks like a reference, but probably isn't: '14' on line 8840 == Missing Reference: '-stride' is mentioned on line 9082, but not defined == Missing Reference: '-2' is mentioned on line 9558, but not defined -- Looks like a reference, but probably isn't: '25' on line 10819 -- Looks like a reference, but probably isn't: '80' on line 12045 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 18, 2011 J. Salonen 6 P. Wilkins 7 Y. Xu 8 Google Inc. 9 June 16, 2011 11 VP8 Data Format and Decoding Guide 12 draft-bankoski-vp8-bitstream-04 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 18, 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 . . . . . . . . . . . . . . . . . . . . 143 120 20. Attachment One: Reference Decoder Source Code . . . . . . . . 147 121 20.1. bit_ops.h . . . . . . . . . . . . . . . . . . . . . . . 147 122 20.2. bool_decoder.h . . . . . . . . . . . . . . . . . . . . . 147 123 20.3. dequant_data.h . . . . . . . . . . . . . . . . . . . . . 151 124 20.4. dixie.c . . . . . . . . . . . . . . . . . . . . . . . . 151 125 20.5. dixie.h . . . . . . . . . . . . . . . . . . . . . . . . 163 126 20.6. dixie_loopfilter.c . . . . . . . . . . . . . . . . . . . 170 127 20.7. dixie_loopfilter.h . . . . . . . . . . . . . . . . . . . 182 128 20.8. idct_add.c . . . . . . . . . . . . . . . . . . . . . . . 182 129 20.9. idct_add.h . . . . . . . . . . . . . . . . . . . . . . . 186 130 20.10. mem.h . . . . . . . . . . . . . . . . . . . . . . . . . 186 131 20.11. modemv.c . . . . . . . . . . . . . . . . . . . . . . . . 188 132 20.12. modemv.h . . . . . . . . . . . . . . . . . . . . . . . . 203 133 20.13. modemv_data.h . . . . . . . . . . . . . . . . . . . . . 203 134 20.14. predict.c . . . . . . . . . . . . . . . . . . . . . . . 208 135 20.15. predict.h . . . . . . . . . . . . . . . . . . . . . . . 238 136 20.16. tokens.c . . . . . . . . . . . . . . . . . . . . . . . . 238 137 20.17. tokens.h . . . . . . . . . . . . . . . . . . . . . . . . 248 138 20.18. vp8_prob_data.h . . . . . . . . . . . . . . . . . . . . 257 139 20.19. vpx_codec_internal.h . . . . . . . . . . . . . . . . . . 266 140 20.20. vpx_decoder.h . . . . . . . . . . . . . . . . . . . . . 276 141 20.21. vpx_integer.h . . . . . . . . . . . . . . . . . . . . . 283 142 20.22. AUTHORS . . . . . . . . . . . . . . . . . . . . . . . . 285 143 20.23. LICENSE . . . . . . . . . . . . . . . . . . . . . . . . 286 144 20.24. PATENTS . . . . . . . . . . . . . . . . . . . . . . . . 287 145 21. References . . . . . . . . . . . . . . . . . . . . . . . . . 289 146 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 290 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 is that seeking within a partition is not supported. 395 The data must be decompressed and processed (or at least stored) in 396 the order in which it occurs in the partition. 398 While this document specifies the ordering of the partition data 399 correctly, the details and semantics of this data are discussed in a 400 more logical fashion to facilitate comprehension. For example, the 401 frame header contains updates to many probability tables used in 402 decoding per-macroblock data. The latter is often described before 403 the layouts of the probabilities and their updates, even though this 404 is the opposite of their order in the bitstream. 406 5. Overview of the Decoding Process 408 A VP8 decoder needs to maintain four YUV frame buffers whose 409 resolutions are at least equal to that of the encoded image. These 410 buffers hold the current frame being reconstructed, the immediately 411 previous reconstructed frame, the most recent golden frame, and the 412 most recent altref frame. 414 Most implementations will wish to "pad" these buffers with 415 "invisible" pixels that extend a moderate number of pixels beyond all 416 four edges of the visible image. This simplifies interframe 417 prediction by allowing all (or most) prediction blocks -- which are 418 not guaranteed to lie within the visible area of a prior frame -- to 419 address usable image data. 421 Regardless of the amount of padding chosen, the invisible rows above 422 (below) the image are filled with copies of the top (bottom) row of 423 the image; the invisible columns to the left (right) of the image are 424 filled with copies of the leftmost (rightmost) visible row; and the 425 four invisible corners are filled with copies of the corresponding 426 visible corner pixels. The use of these prediction buffers (and 427 suggested sizes for the halo) will be elaborated on in the discussion 428 of motion vectors, interframe prediction, and sub-pixel interpolation 429 later in this document. 431 As will be seen in the description of the frame header, the image 432 dimensions are specified (and can change) with every key frame. 433 These buffers (and any other data structures whose size depends on 434 the size of the image) should be allocated (or re-allocated) 435 immediately after the dimensions are decoded. 437 Leaving most of the details for later elaboration, the following is 438 an outline the decoding process. 440 First, the frame header (beginning of the first data partition) is 441 decoded. Altering or augmenting the maintained state of the decoder, 442 this provides the context in which the per-macroblock data can be 443 interpreted. 445 The macroblock data occurs (and must be processed) in raster-scan 446 order. This data comes in two or more parts. The first (prediction 447 or mode) part comes in the remainder of the first data partition. 448 The other parts comprise the data partition(s) for the DCT/WHT 449 coefficients of the residue signal. For each macroblock, the 450 prediction data must be processed before the residue. 452 Each macroblock is predicted using one (and only one) of four 453 possible frames. All macroblocks in a key frame, and all intra-coded 454 macroblocks in an interframe, are predicted using the already-decoded 455 macroblocks in the current frame. Macroblocks in an interframe may 456 also be predicted using the previous frame, the golden frame or the 457 altref frame. Such macroblocks are said to be inter-coded. 459 The purpose of prediction is to use already-constructed image data to 460 approximate the portion of the original image being reconstructed. 461 The effect of any of the prediction modes is then to write a 462 macroblock-sized prediction buffer containing this approximation. 464 Regardless of the prediction method, the residue DCT signal is 465 decoded, dequantized, reverse-transformed, and added to the 466 prediction buffer to produce the (almost final) reconstruction value 467 of the macroblock, which is stored in the correct position of the 468 current frame buffer. 470 The residue signal consists of 24 (sixteen Y, four U, and four V) 4x4 471 quantized and losslessly-compressed DCT transforms approximating the 472 difference between the original macroblock in the uncompressed source 473 and the prediction buffer. For most prediction modes, the zeroth 474 coefficients of the sixteen Y subblocks are expressed via a 25th WHT 475 of the second-order virtual Y2 subblock discussed above. 477 Intra-prediction exploits the spatial coherence of frames. The 16x16 478 luma (Y) and 8x8 chroma (UV) components are predicted independently 479 of each other using one of four simple means of pixel propagation, 480 starting from the already-reconstructed (16-pixel long luma, 8-pixel 481 long chroma) row above and column to the left of the current 482 macroblock. The four methods are: 484 1. Copying the row from above throughout the prediction buffer. 486 2. Copying the column from left throughout the prediction buffer. 488 3. Copying the average value of the row and column throughout the 489 prediction buffer. 491 4. Extrapolation from the row and column using the (fixed) second 492 difference (horizontal and vertical) from the upper left corner. 494 Additionally, the sixteen Y subblocks may be predicted independently 495 of each other using one of ten different modes, four of which are 4x4 496 analogs of those described above, augmented with six "diagonal" 497 prediction methods. There are two types of predictions, one intra 498 and one prediction (among all the modes), for which the residue 499 signal does not use the Y2 block to encode the DC portion of the 500 sixteen 4x4 Y subblock DCTs. This "independent Y subblock" mode has 501 no effect on the 8x8 chroma prediction. 503 Inter-prediction exploits the temporal coherence between nearby 504 frames. Except for the choice of the prediction frame itself, there 505 is no difference between inter-prediction based on the previous frame 506 and that based on the golden frame or altref frame. 508 Inter-prediction is conceptually very simple. While, for reasons of 509 efficiency, there are several methods of encoding the relationship 510 between the current macroblock and corresponding sections of the 511 prediction frame, ultimately each of the sixteen Y subblocks is 512 related to a 4x4 subblock of the prediction frame, whose position in 513 that frame differs from the current subblock position by a (usually 514 small) displacement. These two-dimensional displacements are called 515 motion vectors. 517 The motion vectors used by VP8 have quarter-pixel precision. 518 Prediction of a subblock using a motion vector that happens to have 519 integer (whole number) components is very easy: the 4x4 block of 520 pixels from the displaced block in the previous, golden, or altref 521 frame are simply copied into the correct position of the current 522 macroblock's prediction buffer. 524 Fractional displacements are conceptually and implementationally more 525 complex. They require the inference (or synthesis) of sample values 526 that, strictly speaking, do not exist. This is one of the most basic 527 problems in signal processing and readers conversant with that 528 subject will see that the approach taken by VP8 provides a good 529 balance of robustness, accuracy, and efficiency. 531 Leaving the details for the implementation discussion below, the 532 pixel interpolation is calculated by applying a kernel filter (using 533 reasonable-precision integer math) three pixels on either side, both 534 horizontally and vertically, of the pixel to be synthesized. The 535 resulting 4x4 block of synthetic pixels is then copied into position 536 exactly as in the case of integer displacements. 538 Each of the eight chroma subblocks is handled similarly. Their 539 motion vectors are never specified explicitly; instead, the motion 540 vector for each chroma subblock is calculated by averaging the 541 vectors of the four Y subblocks that occupy the same area of the 542 frame. Since chroma pixels have twice the diameter (and four times 543 the area) of luma pixels, the calculated chroma motion vectors have 544 1/8 pixel resolution, but the procedure for copying or generating 545 pixels for each subblock is essentially identical to that done in the 546 luma plane. 548 After all the macroblocks have been generated (predicted and 549 corrected with the DCT/WHT residue), a filtering step (the loop 550 filter) is applied to the entire frame. The purpose of the loop 551 filter is to reduce blocking artifacts at the boundaries between 552 macroblocks and between subblocks of the macroblocks. The term loop 553 filter is used because this filter is part of the "coding loop," that 554 is, it affects the reconstructed frame buffers that are used to 555 predict ensuing frames. This is distinguished from the 556 postprocessing filters discussed earlier which affect only the viewed 557 video and do not "feed into" subsequent frames. 559 Next, if signaled in the data, the current frame may replace the 560 golden frame prediction buffer and/or the altref frame buffer. 562 The halos of the frame buffers are next filled as specified above. 563 Finally, at least as far as decoding is concerned, the (references 564 to) the "current" and "last" frame buffers should be exchanged in 565 preparation for the next frame. 567 Various processes may be required (or desired) before viewing the 568 generated frame. As discussed in the frame dimension information 569 below, truncation and/or upscaling of the frame may be required. 570 Some playback systems may require a different frame format (RGB, 571 YUY2, etc.). Finally, as mentioned in the introduction, further 572 postprocessing or filtering of the image prior to viewing may be 573 desired. Since the primary purpose of this document is a decoding 574 specification, the postprocessing is not specified in this document. 576 While the basic ideas of prediction and correction used by VP8 are 577 straightforward, many of the details are quite complex. The 578 management of probabilities is particularly elaborate. Not only do 579 the various modes of intra-prediction and motion vector specification 580 have associated probabilities but they, together with the coding of 581 DCT coefficients and motion vectors, often base these probabilities 582 on a variety of contextual information (calculated from what has been 583 decoded so far), as well as on explicit modification via the frame 584 header. 586 The "top-level" of decoding and frame reconstruction is implemented 587 in the reference decoder files onyxd_if.c and decodframe.c. 589 This concludes our summary of decoding and reconstruction; we 590 continue by discussing the individual aspects in more depth. 592 A reasonable "divide and conquer" approach to implementation of a 593 decoder is to begin by decoding streams composed exclusively of key 594 frames. After that works reliably, interframe handling can be added 595 more easily than if complete functionality were attempted 596 immediately. In accordance with this, we first discuss components 597 needed to decode key frames (most of which are also used in the 598 decoding of interframes) and conclude with topics exclusive to 599 interframes. 601 6. Description of Algorithms 603 As the intent of this document, together with the reference decoder 604 source code, is to specify a platform-independent procedure for the 605 decoding and reconstruction of a VP8 video stream, many (small) 606 algorithms must be described exactly. 608 Due to its near-universality, terseness, ability to easily describe 609 calculation at specific precisions, and the fact that On2's reference 610 VP8 decoder is written in C, these algorithm fragments are written 611 using the C programming language, augmented with a few simple 612 definitions below. 614 The standard (and best) reference for C is [Kernighan]. 616 Many code fragments will be presented in this document. Some will be 617 nearly identical to corresponding sections of the reference decoder; 618 others will differ. Roughly speaking, there are three reasons for 619 such differences: 621 1. For reasons of efficiency, the reference decoder version may be 622 less obvious. 624 2. The reference decoder often uses large data structures to 625 maintain context that need not be described or used here. 627 3. The authors of this document felt that a different expression of 628 the same algorithm might facilitate exposition. 630 Regardless of the chosen presentation, the calculation effected by 631 any of the algorithms described here is identical to that effected by 632 the corresponding portion of the reference decoder. 634 All VP8 decoding algorithms use integer math. To facilitate 635 specification of arithmetic precision, we define the following types. 637 ---- Begin code block -------------------------------------- 639 typedef signed char int8; /* signed int exactly 8 bits wide */ 640 typedef unsigned char uint8; /* unsigned "" */ 642 typedef short int16; /* signed int exactly 16 bits wide */ 643 typedef unsigned int16 uint16; /* unsigned "" */ 645 /* int32 is a signed integer type at least 32 bits wide */ 647 typedef long int32; /* guaranteed to work on all systems */ 648 typedef int int32; /* will be more efficient on some systems */ 650 typedef unsigned int32 uint32; 652 /* unsigned integer type, at least 16 bits wide, whose exact size 653 is most convenient to whatever processor we are using */ 655 typedef unsigned int uint; 657 /* While pixels themselves are 8-bit unsigned integers, 658 pixel arithmetic often occurs at 16- or 32-bit precision and 659 the results need to be "saturated" or clamped to an 8-bit 660 range. */ 662 typedef uint8 Pixel; 664 Pixel clamp255( int32 v) { return v < 0? 0 : (v < 255? v : 255);} 666 /* As is elaborated in the discussion of the bool_decoder below, 667 VP8 represents probabilities as unsigned 8-bit numbers. */ 669 typedef uint8 Prob; 671 ---- End code block ---------------------------------------- 673 We occasionally need to discuss mathematical functions involving 674 honest-to-goodness "infinite precision" real numbers. The DCT is 675 first described via the cosine function cos; the ratio of the lengths 676 of the circumference and diameter of a circle is denoted pi; at one 677 point, we take a (base 1/2) logarithm denoted log; and pow( x, y) 678 denotes x raised to the power y. If x = 2 and y is a small non- 679 negative integer, pow( 2, y) may be expressed in C as 1 << y. 681 Finally, we sometimes need to divide signed integers by powers of 682 two, that is, we occasionally right-shift signed numbers. The 683 behavior of such shifts (i.e., the propagation of the sign bit) is, 684 perhaps surprisingly, not defined by the C language itself and is 685 left up to individual compilers. Because of the utility of this 686 frequently needed operation, it is at least arguable that it should 687 be defined by the language (to naturally propagate the sign bit) and, 688 at a minimum, should be correctly implemented by any reasonable 689 compiler. In the interest of strict portability, we attempt to call 690 attention to these shifts when they arise. 692 7. Boolean Entropy Decoder 694 As discussed in the overview above, essentially the entire VP8 data 695 stream is encoded using a boolean entropy coder. 697 An understanding of the bool_decoder is critical to the 698 implementation of a VP8 decompressor, so we discuss in detail. It is 699 easier to comprehend the bool_decoder in conjunction with the 700 bool_encoder used by the compressor to write the compressed data 701 partitions. 703 The bool_encoder encodes (and the bool_decoder decodes) one bool 704 (zero-or-one boolean value) at a time. Its purpose is to losslessly 705 compress a sequence of bools for which the probability of their being 706 zero or one can be well-estimated (via constant or previously-coded 707 information) at the time they are written, using identical 708 corresponding probabilities at the time they are read. 710 As the reader is probably aware, if a bool is much more likely to be 711 zero than one (for instance), it can, on average, be faithfully 712 encoded using much less than one bit per value. The bool_encoder 713 exploits this. 715 In the 1940s, [Shannon] proved that there is a lower bound for the 716 average datarate of a faithful encoding of a sequence of bools (whose 717 probability distributions are known and are independent of each 718 other) and also that there are encoding algorithms that approximate 719 this lower bound as closely as one wishes. 721 If we encode a sequence of bools whose probability of being zero is p 722 (and whose probability of being 1 is 1-p), the lowest possible 723 datarate per value is 725 plog(p) + (1-p)log(1-p); 727 taking the logarithms to the base 1/2 expresses the datarate in bits/ 728 value. 730 We give two simple examples. At one extreme, if p=1/2, then log(p) = 731 log(1-p) = 1 and the lowest possible datarate per bool is 1/2 + 1/2 = 732 1, that is, we cannot do any better than simply literally writing out 733 bits. At another extreme, if p is very small, say p=1/1024, then 734 log(p)=10, log(1-p) is roughly .0014, and the lowest possible 735 datarate is approximately 10/1024 + .0014, roughly 1/100 of a bit per 736 bool. 738 Because most of the bools in the VP8 datastream have zero- 739 probabilities nowhere near 1/2, the compression provided by the 740 bool_encoder is critical to the performance of VP8. 742 The bool coder used by VP8 is a variant of an arithmetic coder. An 743 excellent discussion of arithmetic coding (and other lossless 744 compression techniques) can be found in [Bell]. 746 7.1. Underlying Theory of Coding 748 The basic idea used by the bool coder is to consider the entire data 749 stream (either of the partitions in our case) as the binary expansion 750 of a single number x with 0 <= x < 1. The bits (or bytes) in x are 751 of course written from high to low order and if b[j] (B[j]) is the 752 j^(th) bit (byte) in the partition, the value x is simply the sum 753 (starting with j = 1) of pow(2, -j) * b[j] or pow(256, -j) * B[j]. 755 Before the first bool is coded, all values of x are possible. 757 The coding of each bool restricts the possible values of x in 758 proportion to the probability of what is coded. If p1 is the 759 probability of the first bool being zero and a zero is coded, the 760 range of possible x is restricted to 0 <= x < p1. If a one is coded, 761 the range becomes p1 <= x < 1. 763 The coding continues by repeating the same idea. At every stage, 764 there is an interval a <= x < b of possible values of x. If p is the 765 probability of a zero being coded at this stage and a zero is coded, 766 the interval becomes a <= x < a + (p(b-a)). If a one is coded, the 767 possible x are restricted to a + (p(b-a)) <= x < b. 769 Assuming only finitely many values are to be coded, after the encoder 770 has received the last bool, it can write as its output any value x 771 that lies in the final interval. VP8 simply writes the left endpoint 772 of the final interval. Consequently, the output it would make if 773 encoding were to stop at any time either increases or stays the same 774 as each bool is encoded. 776 Decoding parallels encoding. The decoder is presented with the 777 number x, which has only the initial restriction 0 <= x < 1. To 778 decode the first bool, the decoder is given the first probability p1. 779 If x < p1, a zero is decoded; if x >= p1, a one is decoded. In 780 either case, the new restriction on x, that is, the interval of 781 possible x, is remembered. 783 Decoding continues in exactly the same way: If a <= x < b is the 784 current interval and we are to decode a bool with zero-probability p, 785 we return a zero if a <= x < a + (p(b-a)) and a one if a + (p(b-a)) 786 <= x < b. In either case, the new restriction is remembered in 787 preparation for decoding the next bool. 789 The process outlined above uses real numbers of infinite precision to 790 express the probabilities and ranges. It is true that, if one could 791 actualize this process and coded a large number of bools whose 792 supplied probabilities matched their value distributions, the 793 datarate achieved would approach the theoretical minimum as the 794 number of bools encoded increased. 796 Unfortunately, computers operate at finite precision and an 797 approximation to the theoretically perfect process described above is 798 necessary. Such approximation increases the datarate but, at quite 799 moderate precision and for a wide variety of data sets, this increase 800 is negligible. 802 The only conceptual limitations are, first, that coder probabilities 803 must be expressed at finite precision and, second, that the decoder 804 be able to detect each individual modification to the value interval 805 via examination of a fixed amount of input. As a practical matter, 806 many of the implementation details stem from the fact that the coder 807 can function using only a small "window" to incrementally read or 808 write the arbitrarily precise number x. 810 7.2. Practical Algorithm Description 812 VP8's bool coder works with 8-bit probabilities p. The range of such 813 p is 0 <= p <= 255; the actual probability represented by p is p/256. 814 Also, the coder is designed so that decoding of a bool requires no 815 more than an 8-bit comparison and so that the state of both the 816 encoder and decoder can be easily represented using a small number of 817 unsigned 16-bit integers. 819 The details are most easily understood if we first describe the 820 algorithm using bit-at-a-time input and output. Aside from the 821 ability to maintain a position in this bitstream and write/read bits, 822 the encoder also needs the ability to add 1 to the bits already 823 output; after writing n bits, adding 1 to the existing output is the 824 same thing as adding pow( 2, -n) to x. 826 Together with the bit position, the encoder must maintain two 827 unsigned 8-bit numbers which we call bottom and range. Writing w for 828 the n bits already written and S = pow( 2, - n - 8) for the scale of 829 the current bit position one byte out, we have the following 830 constraint on all future values v of w (including the final value v = 831 x): 833 w + ( S * bottom ) <= v < w + ( S * ( bottom + range ) ) 835 Thus, appending bottom to the already-written bits w gives the left 836 endpoint of the interval of possible values, appending bottom + range 837 gives the right endpoint, range itself (scaled to the current output 838 position) is the length of the interval. 840 So that our probabilistic encodings are reasonably accurate, we do 841 not let range vary by more than a factor of two: It stays within the 842 bounds 128 <= range <= 255. 844 The process for encoding a boolean value val whose probability of 845 being zero is prob / 256 -- and whose probability of being one is ( 846 256 - prob ) / 256 -- with 1 <= prob <= 255 is as follows. 848 Using an unsigned 16-bit multiply followed by an unsigned right 849 shift, we calculate an unsigned 8-bit split value: 851 split = 1 + (((range - 1) * probability)]] >> 8) 853 split is approximately ( prob / 256 ) * range and lies within the 854 bounds 1 <= split <= range - 1. These bounds ensure the correctness 855 of the decoding procedure described below. 857 If the incoming boolean val to be encoded is false, we leave the left 858 interval endpoint bottom alone and reduce range, replacing it by 859 split. If the incoming val is true, we move up the left endpoint to 860 bottom + split, propagating any carry to the already-written value w 861 (this is where we need the ability to add 1 to w), and reduce range 862 to range - split. 864 Regardless of the value encoded, range has been reduced and now has 865 the bounds 1 <= range <= 254. If range < 128, the encoder doubles it 866 and shifts the high-order bit out of bottom to the output as it also 867 doubles bottom, repeating this process one bit at a time until 128 <= 868 range <= 255. Once this is completed, the encoder is ready to accept 869 another bool, maintaining the constraints described above. 871 After encoding the last bool, the partition may be completed by 872 appending bottom to the bitstream. 874 The decoder mimics the state of the encoder. It maintains, together 875 with an input bit position, two unsigned 8-bit numbers, a range 876 identical to that maintained by the encoder and a value. Decoding 877 one bool at a time, the decoder (in effect) tracks the same left 878 interval endpoint as does the encoder and subtracts it from the 879 remaining input. Appending the unread portion of the bitstream to 880 the 8-bit value gives the difference between the actual value encoded 881 and the known left endpoint. 883 The decoder is initialized by setting range = 255 and reading the 884 first 16 input bits into value. The decoder maintains range and 885 calculates split in exactly the same way as does the encoder. 887 To decode a bool, it compares value to split; if value < split, the 888 bool is zero, and range is replaced with split. If value >= split, 889 the bool is one, range is replaced with range - split, and value is 890 replaced with value - split. 892 Again, range is doubled one bit at a time until it is at least 128. 893 The value is doubled in parallel, shifting a new input bit into the 894 bottom each time. 896 Writing Value for value together with the unread input bits and Range 897 for range extended indefinitely on the right by zeros, the condition 898 Value < Range is maintained at all times by the decoder. In 899 particular, the bits shifted out of value as it is doubled are always 900 zero. 902 7.3. Actual Implementation 904 The C code below gives complete implementations of the encoder and 905 decoder described above. While they are logically identical to the 906 "bit-at-a-time" versions, they internally buffer a couple of extra 907 bytes of the bitstream. This allows I/O to be done (more 908 practically) a byte at a time and drastically reduces the number of 909 carries the encoder has to propagate into the already-written data. 911 Another (logically equivalent) implementation may be found in the 912 reference decoder files dboolhuff.h and dboolhuff.c. 914 ---- Begin code block -------------------------------------- 916 /* Encoder first */ 918 typedef struct { 919 uint8 *output; /* ptr to next byte to be written */ 920 uint32 range; /* 128 <= range <= 255 */ 921 uint32 bottom; /* minimum value of remaining output */ 922 int bit_count; /* # of shifts before an output byte 923 is available */ 924 } bool_encoder; 926 /* Must set initial state of encoder before writing any bools. */ 928 void init_bool_encoder( bool_encoder *e, uint8 *start_partition) 929 { 930 e->output = start_partition; 931 e->range = 255; 932 e->bottom = 0; 933 e->bit_count = 24; 934 } 936 /* Encoding very rarely produces a carry that must be propagated 937 to the already-written output. The arithmetic guarantees that 938 the propagation will never go beyond the beginning of the 939 output. Put another way, the encoded value x is always less 940 than one. */ 942 void add_one_to_output( uint8 *q) 943 { 944 while( *--q == 255) 945 *q = 0; 946 ++*q; 947 } 949 /* Main function writes a bool_value whose probability of being 950 zero is (expected to be) prob/256. */ 952 void write_bool( bool_encoder *e, Prob prob, int bool_value) 953 { 954 /* split is approximately (range * prob) / 256 and, 955 crucially, is strictly bigger than zero and strictly 956 smaller than range */ 958 uint32 split = 1 + ( ((e->range - 1) * prob) >> 8); 960 if( bool_value) { 961 e->bottom += split; /* move up bottom of interval */ 962 e->range -= split; /* with corresponding decrease in range */ 963 } else 964 e->range = split; /* decrease range, leaving bottom alone */ 966 while( e->range < 128) 967 { 968 e->range <<= 1; 970 if( e->bottom & (1 << 31)) /* detect carry */ 971 add_one_to_output( e->output); 973 e->bottom <<= 1; /* before shifting bottom */ 975 if( !--e->bit_count) { /* write out high byte of bottom ... */ 977 *e->output++ = (uint8) (e->bottom >> 24); 979 e->bottom &= (1 << 24) - 1; /* ... keeping low 3 bytes */ 980 e->bit_count = 8; /* 8 shifts until next output */ 981 } 982 } 983 } 985 /* Call this function (exactly once) after encoding the last 986 bool value for the partition being written */ 988 void flush_bool_encoder( bool_encoder *e) 989 { 990 int c = e->bit_count; 991 uint32 v = e->bottom; 993 if( v & (1 << (32 - c))) /* propagate (unlikely) carry */ 994 add_one_to_output( e->output); 995 v <<= c & 7; /* before shifting remaining output */ 996 c >>= 3; /* to top of internal buffer */ 997 while( --c >= 0) 998 v <<= 8; 999 c = 4; 1000 while( --c >= 0) { /* write remaining data, possibly padded */ 1001 *e->output++ = (uint8) (v >> 24); 1002 v <<= 8; 1003 } 1004 } 1006 /* Decoder state exactly parallels that of the encoder. 1007 "value", together with the remaining input, equals the 1008 complete encoded number x less the left endpoint of the 1009 current coding interval. */ 1011 typedef struct { 1012 uint8 *input; /* pointer to next compressed data byte */ 1013 uint32 range; /* always identical to encoder's range */ 1014 uint32 value; /* contains at least 8 significant bits */ 1015 int bit_count; /* # of bits shifted out of 1016 value, at most 7 */ 1017 } bool_decoder; 1019 /* Call this function before reading any bools from the 1020 partition.*/ 1022 void init_bool_decoder( bool_decoder *d, uint8 *start_partition) 1023 { 1024 { 1025 int i = 0; 1026 d->value = 0; /* value = first 2 input bytes */ 1027 while( ++i <= 2) 1028 d->value = (d->value << 8) | *start_partition++; 1029 } 1031 d->input = start_partition; /* ptr to next byte to be read */ 1032 d->range = 255; /* initial range is full */ 1033 d->bit_count = 0; /* have not yet shifted out any bits */ 1034 } 1036 /* Main function reads a bool encoded at probability prob/256, 1037 which of course must agree with the probability used when the 1038 bool was written. */ 1040 int read_bool( bool_decoder *d, Prob prob) 1041 { 1042 /* range and split are identical to the corresponding values 1043 used by the encoder when this bool was written */ 1045 uint32 split = 1 + ( ((d->range - 1) * prob) >> 8); 1046 uint32 SPLIT = split << 8; 1047 int retval; /* will be 0 or 1 */ 1049 if( d->value >= SPLIT) { /* encoded a one */ 1050 retval = 1; 1051 d->range -= split; /* reduce range */ 1052 d->value -= SPLIT; /* subtract off left endpoint of interval */ 1053 } else { /* encoded a zero */ 1054 retval = 0; 1055 d->range = split; /* reduce range, no change in left endpoint */ 1056 } 1058 while( d->range < 128) { /* shift out irrelevant value bits */ 1059 d->value <<= 1; 1060 d->range <<= 1; 1061 if( ++d->bit_count == 8) { /* shift in new bits 8 at a time */ 1062 d->bit_count = 0; 1063 d->value |= *d->input++; 1064 } 1065 } 1066 return retval; 1067 } 1069 /* Convenience function reads a "literal", that is, a "num_bits" 1070 wide unsigned value whose bits come high- to low-order, with 1071 each bit encoded at probability 128 (i.e., 1/2). */ 1073 uint32 read_literal( bool_decoder *d, int num_bits) 1074 { 1075 uint32 v = 0; 1076 while( num_bits--) 1077 v = (v << 1) + read_bool( d, 128); 1078 return v; 1079 } 1081 /* Variant reads a signed number */ 1083 int32 read_signed_literal( bool_decoder *d, int num_bits) 1084 { 1085 int32 v = 0; 1086 if( !num_bits) 1087 return 0; 1088 if( read_bool( d, 128)) 1089 v = -1; 1090 while( --num_bits) 1091 v = (v << 1) + read_bool( d, 128); 1092 return v; 1093 } 1095 ---- End code block ---------------------------------------- 1097 8. Compressed Data Components 1099 At the lowest level, VP8's compressed data is simply a sequence of 1100 probabilistically-encoded bools. Most of this data is composed of 1101 (slightly) larger semantic units fashioned from bools, which we 1102 describe here. 1104 We sometimes use these descriptions in C expressions within data 1105 format specifications. In this context, they refer to the return 1106 value of a call to an appropriate bool_decoder d, reading (as always) 1107 from its current reference point. 1109 +--------------+-------+--------------------------------------------+ 1110 | Call | Alt. | Return | 1111 +--------------+-------+--------------------------------------------+ 1112 | Bool(p) | B(p) | Bool with probability p/256 of being 0. | 1113 | | | Return value of read_bool(d, p). | 1114 | | | | 1115 | Flag | F | A one-bit flag (same thing as a B(128) or | 1116 | | | an L(1)). Abbreviated F. Return value of | 1117 | | | read_bool(d, 128). | 1118 | | | | 1119 | Lit(n) | L(n) | Unsigned n-bit number encoded as n flags | 1120 | | | (a "literal"). Abbreviated L(n). The | 1121 | | | bits are read from high to low order. | 1122 | | | Return value of read_literal(d, n). | 1123 | | | | 1124 | SignedLit(n) | | Signed n-bit number encoded similarly to | 1125 | | | an L(n). Return value of | 1126 | | | read_signed_literal(d, n). These are | 1127 | | | rare. | 1128 | | | | 1129 | P(8) | | An 8-bit probability. No different from | 1130 | | | an L(8), but we sometimes use this | 1131 | | | notation to emphasize that a probability | 1132 | | | is being coded. | 1133 | | | | 1134 | P(7) | | A 7-bit specification of an 8-bit | 1135 | | | probability. Coded as an L(7) number x; | 1136 | | | the resulting 8-bit probability is x ? x | 1137 | | | << 1 : 1. | 1138 | | | | 1139 | F? X | | A flag which, if true, is followed by a | 1140 | | | piece of data X. | 1141 | | | | 1142 | F? X:Y | | A flag which, if true, is followed by X | 1143 | | | and, if false, is followed by Y. Also used | 1144 | | | to express a value where Y is an implicit | 1145 | | | default (not encoded in the data stream), | 1146 | | | as in F? P(8):255, which expresses an | 1147 | | | optional probability: if the flag is true, | 1148 | | | the probability is specified as an 8-bit | 1149 | | | literal, while if the flag is false, the | 1150 | | | probability defaults to 255. | 1151 | | | | 1152 | B(p)? X | B(p)? | Variants of the above using a boolean | 1153 | | X:Y | indicator whose probability is not | 1154 | | | necessarily 128. | 1155 | | | | 1156 | X | | Multi-component field, the specifics of | 1157 | | | which will be given at a more appropriate | 1158 | | | point in the discussion. | 1159 | | | | 1160 | T | | Tree-encoded value from small alphabet. | 1161 +--------------+-------+--------------------------------------------+ 1163 The last type requires elaboration. We often wish to encode 1164 something whose value is restricted to a small number of 1165 possibilities (the alphabet). 1167 This is done by representing the alphabet as the leaves of a small 1168 binary tree. The (non-leaf) nodes of the tree have associated 1169 probabilities p and correspond to calls to read_bool(d, p). We think 1170 of a zero as choosing the left branch below the node and a one as 1171 choosing the right branch. 1173 Thus every value (leaf) whose tree depth is x is decoded after 1174 exactly x calls to read_bool. 1176 A tree representing an encoding of an alphabet of n possible values 1177 always contains n-1 non-leaf nodes, regardless of its shape (this is 1178 easily seen by induction on n). 1180 There are many ways that a given alphabet can be so represented. The 1181 choice of tree has little impact on datarate but does affect decoder 1182 performance. The trees used by VP8 are chosen to (on average) 1183 minimize the number of calls to read_bool. This amounts to shaping 1184 the tree so that more probable values have smaller tree depth than do 1185 less probable values. 1187 Readers familiar with Huffman coding will notice that, given an 1188 alphabet together with probabilities for each value, the associated 1189 Huffman tree minimizes the expected number of calls to read_bool. 1191 Such readers will also realize that the coding method described here 1192 never results in higher datarates than does the Huffman method and, 1193 indeed, often results in much lower datarates. Huffman coding is, in 1194 fact, nothing more than a special case of this method in which each 1195 node probability is fixed at 128 (i.e., 1/2). 1197 8.1. Tree Coding Implementation 1199 We give a suggested implementation of a tree data structure followed 1200 by a couple of actual examples of its usage by VP8. 1202 It is most convenient to represent the values using small positive 1203 integers, typically an enum counting up from zero. The largest 1204 alphabet (used to code DCT coefficients, described in Chapter 13 that 1205 is tree-coded by VP8 has only 12 values. The tree for this alphabet 1206 adds 11 interior nodes and so has a total of 23 positions. Thus, an 1207 8-bit number easily accommodates both a tree position and a return 1208 value. 1210 A tree may then be compactly represented as an array of (pairs of) 1211 8-bit integers. Each (even) array index corresponds to an interior 1212 node of the tree;, the zeroth index of course corresponds to the root 1213 of the tree. The array entries come in pairs corresponding to the 1214 left (0) and right (1) branches of the subtree below the interior 1215 node. We use the convention that a positive (even) branch entry is 1216 the index of a deeper interior node, while a nonpositive entry v 1217 corresponds to a leaf whose value is -v. 1219 The node probabilities associated to a tree-coded value are stored in 1220 an array whose indices are half the indices of the corresponding tree 1221 positions. The length of the probability array is one less than the 1222 size of the alphabet. 1224 Here is C code implementing the foregoing. The advantages of our 1225 data structure should be noted. Aside from the smallness of the 1226 structure itself, the tree-directed reading algorithm is essentially 1227 a single line of code. 1229 ---- Begin code block -------------------------------------- 1231 /* A tree specification is simply an array of 8-bit integers. */ 1233 typedef int8 tree_index; 1234 typedef const tree_index Tree[]; 1236 /* Read and return a tree-coded value at the current decoder 1237 position. */ 1239 int treed_read( 1240 bool_decoder * const d, /* bool_decoder always returns a 0 or 1 */ 1241 Tree t, /* tree specification */ 1242 const Prob p[] /* corresponding interior node probabilities */ 1243 ) { 1244 register tree_index i = 0; /* begin at root */ 1246 /* Descend tree until leaf is reached */ 1248 while( ( i = t[ i + read_bool( d, p[i>>1]) ] ) > 0) {} 1250 return -i; /* return value is negation of nonpositive index */ 1251 } 1253 ---- End code block ---------------------------------------- 1255 Tree-based decoding is implemented in the reference decoder file 1256 tree_reader.h. 1258 8.2. Tree Coding Example 1260 As a multi-part example, without getting too far into the semantics 1261 of macroblock decoding (which is of course taken up below), we look 1262 at the "mode" coding for intra-predicted macroblocks. 1264 It so happens that, because of a difference in statistics, the Y (or 1265 luma) mode encoding uses two different trees: one for key frames and 1266 another for interframes. This is the only instance in VP8 of the 1267 same dataset being coded by different trees under different 1268 circumstances. The UV (or chroma) modes are a proper subset of the Y 1269 modes and, as such, have their own decoding tree. 1271 ---- Begin code block -------------------------------------- 1273 typedef enum 1274 { 1275 DC_PRED, /* predict DC using row above and column to the left */ 1276 V_PRED, /* predict rows using row above */ 1277 H_PRED, /* predict columns using column to the left */ 1278 TM_PRED, /* propagate second differences a la "true motion" */ 1280 B_PRED, /* each Y subblock is independently predicted */ 1282 num_uv_modes = B_PRED, /* first four modes apply to chroma */ 1283 num_ymodes /* all modes apply to luma */ 1284 } 1285 intra_mbmode; 1287 /* The aforementioned trees together with the implied codings as 1288 comments. 1289 Actual (i.e., positive) indices are always even. 1290 Value (i.e., nonpositive) indices are arbitrary. */ 1292 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 1293 { 1294 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 1295 4, 6, /* "1" subtree has 2 descendant subtrees */ 1296 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 1297 H_PRED = "101" */ 1298 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 1299 B_PRED = "111" */ 1300 }; 1302 const tree_index kf_ymode_tree [2 * (num_ymodes - 1)] = 1303 { 1304 -B_PRED, 2, /* root: B_PRED = "0", "1" subtree */ 1305 4, 6, /* "1" subtree has 2 descendant subtrees */ 1306 -DC_PRED, -V_PRED, /* "10" subtree: DC_PRED = "100", 1307 V_PRED = "101" */ 1308 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1309 TM_PRED = "111" */ 1310 }; 1312 const tree_index uv_mode_tree [2 * (num_uv_modes - 1)] = 1313 { 1314 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 1315 -V_PRED, 4, /* "1" subtree: V_PRED = "10", 1316 "11" subtree */ 1317 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1318 TM_PRED = "111" */ 1319 }; 1321 /* Given a bool_decoder d, a Y mode might be decoded as follows.*/ 1323 const Prob pretend_its_huffman [num_ymodes - 1] = 1324 { 128, 128, 128, 128}; 1326 Ymode = (intra_mbmode) treed_read( d, ymode_tree, 1327 pretend_its_huffman); 1329 ---- End code block ---------------------------------------- 1331 Since it greatly facilitates re-use of reference code and since there 1332 is no real reason to do otherwise, it is strongly suggested that any 1333 decoder implementation use exactly the same enumeration values and 1334 probability table layouts as described in this document (and in the 1335 reference code) for all tree-coded data in VP8. 1337 9. Frame Header 1339 The uncompressed data chunk at the start of each frame and the first 1340 part of the first data partition contains information pertaining to 1341 the frame as a whole. We list the fields in the order of occurrence, 1342 giving details for some of the fields. Other details are postponed 1343 until a more logical point in our overall description. Most of the 1344 header decoding occurs in the reference decoder file decodeframe.c. 1346 9.1. Uncompressed Data Chunk 1348 The uncompressed data chunk comprises a common (for key frames and 1349 interframes) 3-byte frame tag that contains four fields, as follows: 1351 1. A 1-bit frame type (0 for key frames, 1 for interframes). 1353 2. A 3-bit version number (0 - 3 are defined as four different 1354 profiles with different decoding complexity; other values may be 1355 defined for future variants of the VP8 data format). 1357 3. A 1-bit show_frame flag (0 when current frame is not for display, 1358 1 when current frame is for display). 1360 4. A 19-bit field containing the size of the first data partition in 1361 bytes. 1363 Version number enables or disables certain features in the bitstream, 1364 as follows: 1366 +---------+-------------------------+-------------+ 1367 | Version | Reconstruction filter | Loop filter | 1368 +---------+-------------------------+-------------+ 1369 | 0 | Bicubic | Normal | 1370 | | | | 1371 | 1 | Bilinear | Simple | 1372 | | | | 1373 | 2 | Bilinear | None | 1374 | | | | 1375 | 3 | None | None | 1376 | | | | 1377 | Other | Reserved for future use | | 1378 +---------+-------------------------+-------------+ 1380 The reference software also adjusts the loop filter based on version 1381 number, as per the table above. Version number 1 implies a "simple" 1382 loop filter and version numbers 2 and 3 imply no loop filter. 1383 However, the "simple" filter setting in this context has no effect 1384 whatsoever on the decoding process, and the "no loop filter" setting 1385 only forces the reference encoder to set filter level equal to 0. 1386 Neither affect the decoding process. In decoding, the only loop 1387 filter settings that matter are those in the frame header. 1389 For key frames the frame tag is followed by a further 7 bytes of 1390 uncompressed data, as follows: 1392 ---- Begin code block -------------------------------------- 1394 Start code byte 0 0x9d 1395 Start code byte 1 0x01 1396 Start code byte 2 0x2a 1398 16 bits : (2 bits Horizontal Scale << 14) | Width (14 bits) 1399 16 bits : (2 bits Vertical Scale << 14) | Height (14 bits) 1401 ---- End code block ---------------------------------------- 1403 The following source code segment illustrates validation of the start 1404 code and reading the width, height and scale factors for a key frame. 1406 ---- Begin code block -------------------------------------- 1408 unsigned char *c = pbi->Source+3; 1410 // vet via sync code 1411 if(c[0]!=0x9d||c[1]!=0x01||c[2]!=0x2a) 1412 return -1; 1414 ---- End code block ---------------------------------------- 1416 where pbi->source points to the beginning of the frame. 1418 The following code reads the image dimension from the bitstream: 1420 ---- Begin code block -------------------------------------- 1422 pc->Width = swap2(*(unsigned short*)(c+3))&0x3fff; 1423 pc->horiz_scale = swap2(*(unsigned short*)(c+3))>>14; 1424 pc->Height = swap2(*(unsigned short*)(c+5))&0x3fff; 1425 pc->vert_scale = swap2(*(unsigned short*)(c+5))>>14; 1427 ---- End code block ---------------------------------------- 1429 where swap2 macro takes care of the endian on different platform: 1431 ---- Begin code block -------------------------------------- 1433 #if defined(__ppc__) || defined(__ppc64__) 1434 # define swap2(d) \ 1435 ((d&0x000000ff)<<8) | \ 1436 ((d&0x0000ff00)>>8) 1437 #else 1438 # define swap2(d) d 1439 #endif 1441 ---- End code block ---------------------------------------- 1443 While each frame is encoded as a raster scan of 16x16 macroblocks, 1444 the frame dimensions are not necessarily evenly divisible by 16. In 1445 this case, write ew = 16 - (width & 15) and eh = 16 - (height & 15) 1446 for the excess width and height, respectively. Although they are 1447 encoded, the last ew columns and eh rows are not actually part of the 1448 image and should be discarded before final output. However, these 1449 "excess pixels" should be maintained in the internal reconstruction 1450 buffer used to predict ensuing frames. 1452 The scaling specifications for each dimension are encoded as follows. 1454 +-------+--------------------------------------+ 1455 | Value | Scaling | 1456 +-------+--------------------------------------+ 1457 | 0 | No upscaling (the most common case). | 1458 | | | 1459 | 1 | Upscale by 5/4. | 1460 | | | 1461 | 2 | Upscale by 5/3. | 1462 | | | 1463 | 3 | Upscale by 2. | 1464 +-------+--------------------------------------+ 1466 Upscaling does not affect the reconstruction buffer, which should be 1467 maintained at the encoded resolution. Any reasonable method of 1468 upsampling (including any that may be supported by video hardware in 1469 the playback environment) may be used. Since scaling has no effect 1470 on decoding, we do not discuss it any further. 1472 As discussed in Chapter 5, allocation (or re-allocation) of data 1473 structures (such as the reconstruction buffer) whose size depends on 1474 dimension will be triggered here. 1476 9.2. Color Space and Pixel Type (Key Frames-only) 1478 +-------+------------------------------------------+ 1479 | Field | Value | 1480 +-------+------------------------------------------+ 1481 | L(1) | 1-bit color space type specification | 1482 | | | 1483 | L(1) | 1-bit pixel value clamping specification | 1484 +-------+------------------------------------------+ 1486 The color space type bit is encoded as the following: 1488 o 0 - YUV color space similar to the YCrCb color space defined in 1489 [ITU-R_BT.601] 1491 o 1 - Reserved for future use 1493 The pixel value clamping type bit is encoded as the following: 1495 o 0 - Decoders are required to clamp the reconstructed pixel values 1496 to between 0 and 255 (inclusive). 1498 o 1 - Reconstructed pixel values are guaranteed to be between 0 and 1499 255, no clamping is necessary. 1501 Information in this subsection does not appear in interframes. 1503 9.3. Segment-based Adjustments 1505 This subsection contains probability and value information for 1506 implementing segment adaptive adjustments to default decoder 1507 behaviors. The data in this section is used in the decoding of the 1508 ensuing per-segment information and applies to the entire frame. 1509 When segment adaptive adjustments are enabled, each macroblock will 1510 be assigned a segment ID. Macroblocks with the same segment ID 1511 belong to same segment, and have the same adaptive adjustments over 1512 default baseline values for the frame. The adjustments can be 1513 quantization level or loop filter strength. 1515 The context for decoding this feature at macroblock level is provided 1516 by a subsection in the frame header, which contains:: 1518 1. A segmentation_enabled Flag which if 1 (0), enables (disables) 1519 the feature for this frame. The remaining fields occur if the 1520 feature is enabled. 1522 2. L(1) indicates if the segment map is updated for the current 1523 frame (update_mb_segmentaton_map) 1525 3. L(1) indicates if the segment feature data items are updated for 1526 the current frame 1528 4. If flag in 3 is 1, the following fields occur: 1530 1. L(1) the mode of segment feature data, can be absolute value 1531 mode or delta value mode, later mode, feature data is the 1532 difference against current frame defaults. 1534 2. Segment feature data items are decoded segment by each 1535 segment for each segment feature. For every data item, a one 1536 bit flag indicating if the item is 0 or a non-zero value to 1537 be decoded. If there is non-zero value, the value is decoded 1538 as a magnitude L(n) followed by a one bit sign (L(1), 0 for 1539 positive and 1 for negative). The length n can be looked up 1540 from a pre-defined length table for all feature data. 1542 5. If flag in 2 is 1, the probabilities of the decoding tree for 1543 segment map are decoded from the bitstream. Each probability is 1544 decoded with one bit flag indicating if the probability is the 1545 default value of 255 (flag is 0), or the probability is an 8-bit 1546 value, L(8), from the bitstream. 1548 The layout and semantics supporting this feature at the macroblock 1549 level will be described in Chapter 10. 1551 9.4. Loop Filter Type and Levels 1553 VP8 supports two types of loop filter, having different computational 1554 complexity. The following bits occur in the header to support the 1555 selection of the baseline type, strength and sharpness behavior of 1556 the loop filter used for the current frame. 1558 +-------+-------------------+ 1559 | Index | Description | 1560 +-------+-------------------+ 1561 | L(1) | filter_type | 1562 | | | 1563 | L(6) | loop_filter_level | 1564 | | | 1565 | L(3) | sharpness_level | 1566 +-------+-------------------+ 1568 The meaning of these numbers will be further explained in Chapter 15. 1570 VP8 has a feature in the bitstream that enables adjustment of the 1571 loop filter level based on a macroblock's prediction mode and 1572 reference frame. The per-macroblock adjustment is done through delta 1573 values against default loop filter level for the current frame. This 1574 subsection contains flag and value information for implementing per- 1575 macroblock loop filter level adjustment to default decoder behaviors. 1576 The data in this section is used in the decoding of the ensuing per- 1577 macroblock information and applies to the entire frame. 1579 L(1) is a one-bit flag indicating if macroblock loop filter 1580 adjustment is on for the current frame. 0 means such feature is not 1581 supported in the current frame and 1 means this feature is enabled 1582 for the current frame. 1584 Whether the adjustment is based on reference frame or encoding mode, 1585 the adjustment of loop filter level is done via a delta value against 1586 a baseline loop filter value. The delta values are updated for the 1587 current frame if an L(1) bit, mode_ref_lf_delta_update, takes the 1588 value 1. There are two groups of delta values, one group of delta 1589 values are for reference frame-based adjustments, the other group is 1590 for mode-based adjustments. The number of delta values in the two 1591 groups is MAX_REF_LF_DELTAS and MAX_MODE_LF_DELTAS, respectively. 1592 For every value within the two groups, there is one bit L(1) to 1593 indicate if the particular value is updated. When one is updated 1594 (1), it is transmitted as a six-bit magnitude L(6) followed by a one- 1595 bit sign flag (L(1), 0 for positive and 1 for negative). 1597 9.5. Token Partition and Partition Data Offsets 1599 VP8 allows DCT coefficients to be packed into multiple partitions 1600 besides the first partition with header and per-macroblock prediction 1601 information, so the decoder can perform parallel decoding in an 1602 efficient manner. There are two bits L(2) used to indicate the 1603 number of coefficient data partitions within a compressed frame. The 1604 two bits are defined in the following table: 1606 +-------+-------+----------------------+ 1607 | Bit 1 | Bit 0 | Number of Partitions | 1608 +-------+-------+----------------------+ 1609 | 0 | 0 | 1 | 1610 | | | | 1611 | 0 | 1 | 2 | 1612 | | | | 1613 | 1 | 0 | 4 | 1614 | | | | 1615 | 1 | 1 | 8 | 1616 +-------+-------+----------------------+ 1618 Offsets are embedded in the bitstream to provide the decoder direct 1619 access to token partitions. If the number of data partitions is 1620 greater then 1, the size of each partition (except the last) is 1621 written in 3 bytes (24 bits). The size of the last partition is the 1622 remainder of the data not used by any of the previous partitions. 1623 The partitioned data are consecutive in the bitstream, so the size 1624 can also be used to calculate the offset of each partition. The 1625 following pseudo code illustrates how the size/offset is defined by 1626 the three bytes in the bitstream. 1628 ---- Begin code block -------------------------------------- 1630 Offset/size = (uint32)(byte0) + ((uint32)(byte1)<<8) 1631 + ((uint32)(byte2)<<16); 1633 ---- End code block ---------------------------------------- 1635 9.6. Dequantization Indices 1637 All residue signals are specified via a quantized 4x4 DCT applied to 1638 the Y, U, V, or Y2 subblocks of a macroblock. As detailed in Chapter 1639 14, before inverting the transform, each decoded coefficient is 1640 multiplied by one of six dequantization factors, the choice of which 1641 depends on the plane (Y, chroma = U or V, Y2) and coefficient 1642 position (DC = coefficient 0, AC = coefficients 1-15). The six 1643 values are specified using 7-bit indices into six corresponding fixed 1644 tables (the tables are given in Chapter 14). 1646 The first 7-bit index gives the dequantization table index for Y 1647 plane AC coefficients, called yac_qi. It is always coded and acts as 1648 a baseline for the other 5 quantization indices, each of which is 1649 represented by a delta from this baseline index. Following is pseudo 1650 code for reading the indices: 1652 ---- Begin code block -------------------------------------- 1654 yac_qi = L(7); /* Y ac index always specified */ 1655 ydc_delta = F? delta(): 0; /* Y dc delta specified if 1656 flag is true */ 1658 y2dc_delta = F? delta(): 0; /* Y2 dc delta specified if 1659 flag is true */ 1660 y2ac_delta = F? delta(): 0; /* Y2 ac delta specified if 1661 flag is true */ 1663 uvdc_delta = F? delta(): 0; /* chroma dc delta specified 1664 if flag is true */ 1665 uvac_delta = F? delta(): 0; /* chroma ac delta specified 1666 if flag is true */ 1668 ---- End code block ---------------------------------------- 1669 Where delta() is the process to read 5 bits from the bitstream to 1670 determine a signed delta value: 1672 +-------+--------------------------------------------------+ 1673 | Index | Description | 1674 +-------+--------------------------------------------------+ 1675 | L(4) | Magnitude of delta | 1676 | | | 1677 | L(1) | Sign of delta, 0 for positive and 1 for negative | 1678 +-------+--------------------------------------------------+ 1680 9.7. Refresh Golden Frame and AltRef Frame 1682 For key frames, both golden frame and altref frame are refreshed/ 1683 replaced by the current reconstructed frame, by default. For non-key 1684 frames, VP8 uses two bits to indicate whether the two frame buffers 1685 are refreshed, using the reconstructed current frame: 1687 +-------+----------------------------------------------------------+ 1688 | Index | Description | 1689 +-------+----------------------------------------------------------+ 1690 | L(1) | Whether golden frame is refreshed (0 for no, 1 for yes). | 1691 | | | 1692 | L(1) | Whether altref frame is refreshed (0 for no, 1 for yes). | 1693 +-------+----------------------------------------------------------+ 1695 When the flag for golden frame is 0, VP8 uses 2 more bits in the 1696 bitstream to indicate whether the buffer (and which buffer) is copied 1697 to the golden frame, or if no buffer is copied: 1699 +-------+------------------------------------------+ 1700 | Index | Description | 1701 +-------+------------------------------------------+ 1702 | L(2) | Buffer copy flag for golden frame buffer | 1703 +-------+------------------------------------------+ 1705 Where: 1707 o 0 means no buffer is copied to golden frame 1709 o 1 means last_frame is copied to golden frame 1711 o 2 means alt_ref_frame is copied to golden frame 1713 Similarly, when the flag for altref is 0, VP8 uses 2 bits in the 1714 bitstream to indicate which buffer is copied to alt_ref_frame. 1716 +-------+------------------------------------------+ 1717 | Index | Description | 1718 +-------+------------------------------------------+ 1719 | L(2) | Buffer copy flag for altref frame buffer | 1720 +-------+------------------------------------------+ 1722 Where: 1724 o 0 means no buffer is copied to altref frame 1726 o 1 means last_frame is copied to altref frame 1728 o 2 means golden_frame is copied to altref frame 1730 Two bits are transmitted for ref_frame_sign_bias for golden_frame and 1731 alt_ref_frame respectively. 1733 +-------+---------------------------------+ 1734 | Index | Description | 1735 +-------+---------------------------------+ 1736 | L(1) | Sign bias flag for golden frame | 1737 | | | 1738 | L(1) | Sign bias flag for altref frame | 1739 +-------+---------------------------------+ 1741 These values are used to control the sign of the motion vectors when 1742 a golden frame or an altref frame is used as the reference frame for 1743 a macroblock. 1745 9.8. Refresh Last Frame Buffer 1747 VP8 uses one bit, L(1), to indicate if the last frame reference 1748 buffer is refreshed using the constructed current frame. On key 1749 frame this bit is overridden, and the last frame buffer is always 1750 refreshed. 1752 9.9. DCT Coefficient Probability Update 1754 Contains a partial update of the probability tables used to decode 1755 DCT coefficients. These tables are maintained across interframes but 1756 are of course replaced with their defaults at the beginning of every 1757 key frame. 1759 The layout and semantics of this field will be taken up in Chapter 1760 13. 1762 9.10. Remaining Frame Header Data (non-Key Frame) 1764 +-------+-----------------------------------------------------------+ 1765 | Index | Description | 1766 +-------+-----------------------------------------------------------+ 1767 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1768 | | if skipping of macroblocks with no non-zero coefficients | 1769 | | is enabled. If it is set to 0 then prob_skip_false is | 1770 | | not read and mb_skip_coeff is forced to 0 for all | 1771 | | macroblocks (see Sections 11.1 and 12.1). | 1772 | | | 1773 | L(8) | prob_skip_false = probability used for decoding a | 1774 | | macroblock level flag, which indicates if a macroblock | 1775 | | has any non-zero coefficients. Only read if | 1776 | | mb_no_coeff_skip is 1. | 1777 | | | 1778 | L(8) | prob_intra = probability that a macroblock is "intra" | 1779 | | predicted, that is, predicted from the already-encoded | 1780 | | portions of the current frame as opposed to "inter" | 1781 | | predicted, that is, predicted from the contents of a | 1782 | | prior frame. | 1783 | | | 1784 | L(8) | prob_last = probability that an inter-predicted | 1785 | | macroblock is predicted from the immediately previous | 1786 | | frame, as opposed to the most recent golden frame or | 1787 | | altref frame.. | 1788 | | | 1789 | L(8) | prob_gf = probability that an inter-predicted macroblock | 1790 | | is predicted from the most recent golden frame, as | 1791 | | opposed to the altref frame | 1792 | | | 1793 | F | If true, followed by four L(8)s updating the | 1794 | | probabilities for the different types of intra-prediction | 1795 | | for the Y plane. These probabilities correspond to the | 1796 | | four interior nodes of the decoding tree for intra Y | 1797 | | modes in an interframe, that is, the even positions in | 1798 | | the ymode_tree array given above. | 1799 | | | 1800 | F | If true, followed by three L(8)s updating the | 1801 | | probabilities for the different types of intra-prediction | 1802 | | for the chroma planes. These probabilities correspond to | 1803 | | the even positions in the uv_mode_tree array given above. | 1804 | | | 1805 | X | Motion vector probability update. The details will be | 1806 | | given after the discussion of motion vector decoding. | 1807 +-------+-----------------------------------------------------------+ 1809 Decoding of this portion (only) of the frame header is handled in the 1810 reference decoder file decodemv.c. 1812 9.11. Remaining Frame Header Data (Key Frame) 1814 +-------+-----------------------------------------------------------+ 1815 | Index | Description | 1816 +-------+-----------------------------------------------------------+ 1817 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1818 | | if skipping of macroblocks with no non-zero coefficients | 1819 | | is enabled. If it is set to 0 then prob_skip_false is | 1820 | | not read and mb_skip_coeff is forced to 0 for all | 1821 | | macroblocks (see Sections 11.1 and 12.1). | 1822 | | | 1823 | L(8) | prob_skip_false = Probability used for decoding a | 1824 | | macroblock level flag, which indicates if a macroblock | 1825 | | has any non-zero coefficients. Only read if | 1826 | | mb_no_coeff_skip is 1. | 1827 +-------+-----------------------------------------------------------+ 1829 Decoding of this portion of the frame header is handled in the 1830 reference decoder file demode.c. 1832 This completes the layout of the frame header. The remainder of the 1833 first data partition consists of macroblock-level prediction data. 1835 After the frame header is processed, all probabilities needed to 1836 decode the prediction and residue data are known and will not change 1837 until the next frame. 1839 10. Segment-based Feature Adjustments 1841 Every macroblock may optionally override some of the default 1842 behaviors of the decoder. Specifically, VP8 uses segment based 1843 adjustments to support changing quantizer level and loop filter level 1844 for a macroblock. When the segment-based adjustment feature is 1845 enabled for a frame, each macroblock within the frame is coded with a 1846 segment_id. This effectively segments all the macroblocks in the 1847 current frame into a number of different segments. Macroblocks 1848 within the same segment behave exactly the same for quantizer and 1849 loop filter level adjustments. 1851 If both the segmentation_enabled and update_mb_segmentation_map flags 1852 in subsection B of the frame header take a value of 1, the prediction 1853 data for each (intra- or inter-coded) macroblock begins with a 1854 specification of segment_id for the current macroblock. It is 1855 decoded using this simple tree ... 1857 ---- Begin code block -------------------------------------- 1859 const tree_index mb_segment_tree [2 * (4-1)] = 1860 { 1861 2, 4, /* root: "0", "1" subtrees */ 1862 -0, -1, /* "00" = 0th value, "01" = 1st value */ 1863 -2, -3 /* "10" = 2nd value, "11" = 3rd value */ 1864 } 1866 ---- End code block ---------------------------------------- 1868 ... combined with a 3-entry probability table 1869 mb_segment_tree_probs[3]. The macroblock's segment_id is used later 1870 in the decoding process to look into the segment_feature_data table 1871 and determine how the quantizer and loop filter levels are adjusted. 1873 The decoding of segment_id, together with the parsing of intra- 1874 prediction modes (which is taken up next), is implemented in the 1875 reference decoder file demode.c. 1877 11. Key Frame Macroblock Prediction Records 1879 After the features described above, the macroblock prediction record 1880 next specifies the prediction mode used for the macroblock. 1882 11.1. mb_skip_coeff 1884 The single bool flag is decoded using prob_skip_false if and only if 1885 mb_no_coeff_skip is set to 1 (see sections 9.10 and 9.11). If 1886 mb_no_coeff_skip is set to 0 then this value defaults to 0. 1888 11.2. Luma Modes 1890 First comes the luma specification of type intra_mbmode, coded using 1891 the kf_ymode_tree, as described in Chapter 8 and repeated here for 1892 convenience: 1894 ---- Begin code block -------------------------------------- 1896 typedef enum 1897 { 1898 DC_PRED, /* predict DC using row above and column to the left */ 1899 V_PRED, /* predict rows using row above */ 1900 H_PRED, /* predict columns using column to the left */ 1901 TM_PRED, /* propagate second differences a la "true motion" */ 1903 B_PRED, /* each Y subblock is independently predicted */ 1905 num_uv_modes = B_PRED, /* first four modes apply to chroma */ 1906 num_ymodes /* all modes apply to luma */ 1907 } 1908 intra_mbmode; 1910 const tree_index kf_ymode_tree [2 * (num_ymodes - 1)] = 1911 { 1912 -B_PRED, 2, /* root: B_PRED = "0", "1" subtree */ 1913 4, 6, /* "1" subtree has 2 descendant subtrees */ 1914 -DC_PRED, -V_PRED, /* "10" subtree: DC_PRED = "100", 1915 V_PRED = "101" */ 1916 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1917 TM_PRED = "111" */ 1918 }; 1920 ---- End code block ---------------------------------------- 1922 For key frames, the Y mode is decoded using a fixed probability array 1923 as follows: 1925 ---- Begin code block -------------------------------------- 1927 const Prob kf_ymode_prob [num_ymodes - 1] = { 145, 156, 163, 128}; 1928 Ymode = (intra_mbmode) treed_read( d, kf_ymode_tree, kf_ymode_prob); 1930 ---- End code block ---------------------------------------- 1932 d is of course the bool_decoder being used to read the first data 1933 partition. 1935 If the Ymode is B_PRED, it is followed by a (tree-coded) mode for 1936 each of the 16 Y subblocks. The 10 subblock modes and their coding 1937 tree as follows: 1939 ---- Begin code block -------------------------------------- 1941 typedef enum 1942 { 1943 B_DC_PRED, /* predict DC using row above and column 1944 to the left */ 1945 B_TM_PRED, /* propagate second differences a la 1946 "true motion" */ 1948 B_VE_PRED, /* predict rows using row above */ 1949 B_HE_PRED, /* predict columns using column to the left */ 1951 B_LD_PRED, /* southwest (left and down) 45 degree diagonal 1952 prediction */ 1953 B_RD_PRED, /* southeast (right and down) "" */ 1955 B_VR_PRED, /* SSE (vertical right) diagonal prediction */ 1956 B_VL_PRED, /* SSW (vertical left) "" */ 1957 B_HD_PRED, /* ESE (horizontal down) "" */ 1958 B_HU_PRED, /* ENE (horizontal up) "" */ 1960 num_intra_bmodes 1961 } 1962 intra_bmode; 1964 /* Coding tree for the above, with implied codings as comments */ 1966 const tree_index bmode_tree [2 * (num_intra_bmodes - 1)] = 1967 { 1968 -B_DC_PRED, 2, /* B_DC_PRED = "0" */ 1969 -B_TM_PRED, 4, /* B_TM_PRED = "10" */ 1970 -B_VE_PRED, 6, /* B_VE_PRED = "110" */ 1971 8, 12, 1972 -B_HE_PRED, 10, /* B_HE_PRED = "11100" */ 1973 -B_RD_PRED, -B_VR_PRED, /* B_RD_PRED = "111010", 1974 B_VR_PRED = "111011" */ 1975 -B_LD_PRED, 14, /* B_LD_PRED = "111110" */ 1976 -B_VL_PRED, 16, /* B_VL_PRED = "1111110" */ 1977 -B_HD_PRED, -B_HU_PRED /* HD = "11111110", 1978 HU = "11111111" */ 1979 }; 1981 ---- End code block ---------------------------------------- 1983 The first four modes are smaller versions of the similarly-named 1984 16x16 modes above, albeit with slightly different numbering. The 1985 last six "diagonal" modes are unique to luma subblocks. 1987 11.3. Subblock Mode Contexts 1989 The coding of subblock modes in key frames uses the modes already 1990 coded for the subblocks to the left of and above the subblock to 1991 select a probability array for decoding the current subblock mode. 1992 This is our first instance of contextual prediction, and there are 1993 several caveats associated with it: 1995 1. The adjacency relationships between subblocks are based on the 1996 normal default raster placement of the subblocks. 1998 2. The adjacent subblocks need not lie in the current macroblock. 1999 The subblocks to the left of the left-edge subblocks 0, 4, 8, and 2000 12 are the right-edge subblocks 3, 7, 11, and 15, respectively, 2001 of the (already coded) macroblock immediately to the left. 2002 Similarly, the subblocks above the top-edge subblocks 0, 1, 2, 2003 and 3 are the bottom-edge subblocks 12, 13, 14, and 15 of the 2004 already-coded macroblock immediately above us. 2006 3. For macroblocks on the top row or left edge of the image, some of 2007 the predictors will be non-existent. Such predictors are taken 2008 to have had the value B_DC_PRED which, perhaps conveniently, 2009 takes the value 0 in the enumeration above. A simple management 2010 scheme for these contexts might maintain a row of above 2011 predictors and four left predictors. Before decoding the frame, 2012 the entire row is initialized to B_DC_PRED; before decoding each 2013 row of macroblocks, the four left predictors are also set to 2014 B_DC_PRED. After decoding a macroblock, the bottom four subblock 2015 modes are copied into the row predictor (at the current position, 2016 which then advances to be above the next macroblock) and the 2017 right four subblock modes are copied into the left predictor. 2019 4. Many macroblocks will of course be coded using a 16x16 luma 2020 prediction mode. For the purpose of predicting ensuing subblock 2021 modes (only), such macroblocks derive a subblock mode, constant 2022 throughout the macroblock, from the 16x16 luma mode as follows: 2023 DC_PRED uses B_DC_PRED, V_PRED uses B_VE_PRED, H_PRED uses 2024 B_HE_PRED, and TM_PRED uses B_TM_PRED. 2026 5. Although we discuss interframe modes later, we remark here that, 2027 while interframes do use all the intra-coding modes described 2028 here and below, the subblock modes in an interframe are coded 2029 using a single constant probability array that does not depend on 2030 any context. 2032 The dependence of subblock mode probability on the nearby subblock 2033 mode context is most easily handled using a three-dimensional 2034 constant array: 2036 ---- Begin code block -------------------------------------- 2038 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2039 [num_intra_bmodes-1]; 2041 ---- End code block ---------------------------------------- 2043 The outer two dimensions of this array are indexed by the already- 2044 coded subblock modes above and to the left of the current block, 2045 respectively. The inner dimension is a typical tree probability list 2046 whose indices correspond to the even indices of the bmode_tree above. 2047 The mode for the j^(th) luma subblock is then 2049 ---- Begin code block -------------------------------------- 2051 Bmode = (intra_bmode) treed_read( d, bmode_tree, kf_bmode_prob 2052 [A] [L]); 2054 ---- End code block ---------------------------------------- 2056 where the 4x4 Y subblock index j varies from 0 to 15 in raster order 2057 and A and L are the modes used above and to-the-left of the j^(th) 2058 subblock. 2060 The contents of the kf_bmode_prob array are given at the end of this 2061 chapter. 2063 11.4. Chroma Modes 2065 After the Y mode (and optional subblock mode) specification comes the 2066 chroma mode. The chroma modes are a subset of the Y modes and are 2067 coded using the uv_mode_tree described in Chapter 8, again repeated 2068 here for convenience: 2070 ---- Begin code block -------------------------------------- 2072 const tree_index uv_mode_tree [2 * (num_uv_modes - 1)] = 2073 { 2074 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 2075 -V_PRED, 4, /* "1" subtree: V_PRED = "10", 2076 "11" subtree */ 2077 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 2078 TM_PRED = "111" */ 2079 }; 2081 ---- End code block ---------------------------------------- 2083 As for the Y modes (in a key frame), the chroma modes are coded using 2084 a fixed, contextless probability table: 2086 ---- Begin code block -------------------------------------- 2088 const Prob kf_uv_mode_prob [num_uv_modes - 1] = { 142, 114, 183}; 2089 uv_mode = (intra_mbmode) treed_read( d, uv_mode_tree, 2090 kf_uv_mode_prob); 2092 ---- End code block ---------------------------------------- 2094 This completes the description of macroblock prediction coding for 2095 key frames. As will be discussed in Chapter 16, the coding of intra 2096 modes within interframes is similar, but not identical, to that 2097 described here (and in the reference code) for prediction modes and, 2098 indeed, for all tree-coded data in VP8. 2100 11.5. Subblock Mode Probability Table 2102 Finally, here is the fixed probability table used to decode subblock 2103 modes in key frames. 2105 ---- Begin code block -------------------------------------- 2107 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2108 [num_intra_bmodes-1] = 2109 { 2110 { 2111 { 231, 120, 48, 89, 115, 113, 120, 152, 112}, 2112 { 152, 179, 64, 126, 170, 118, 46, 70, 95}, 2113 { 175, 69, 143, 80, 85, 82, 72, 155, 103}, 2114 { 56, 58, 10, 171, 218, 189, 17, 13, 152}, 2115 { 144, 71, 10, 38, 171, 213, 144, 34, 26}, 2116 { 114, 26, 17, 163, 44, 195, 21, 10, 173}, 2117 { 121, 24, 80, 195, 26, 62, 44, 64, 85}, 2118 { 170, 46, 55, 19, 136, 160, 33, 206, 71}, 2119 { 63, 20, 8, 114, 114, 208, 12, 9, 226}, 2120 { 81, 40, 11, 96, 182, 84, 29, 16, 36} 2121 }, 2122 { 2123 { 134, 183, 89, 137, 98, 101, 106, 165, 148}, 2124 { 72, 187, 100, 130, 157, 111, 32, 75, 80}, 2125 { 66, 102, 167, 99, 74, 62, 40, 234, 128}, 2126 { 41, 53, 9, 178, 241, 141, 26, 8, 107}, 2127 { 104, 79, 12, 27, 217, 255, 87, 17, 7}, 2128 { 74, 43, 26, 146, 73, 166, 49, 23, 157}, 2129 { 65, 38, 105, 160, 51, 52, 31, 115, 128}, 2130 { 87, 68, 71, 44, 114, 51, 15, 186, 23}, 2131 { 47, 41, 14, 110, 182, 183, 21, 17, 194}, 2132 { 66, 45, 25, 102, 197, 189, 23, 18, 22} 2133 }, 2134 { 2135 { 88, 88, 147, 150, 42, 46, 45, 196, 205}, 2136 { 43, 97, 183, 117, 85, 38, 35, 179, 61}, 2137 { 39, 53, 200, 87, 26, 21, 43, 232, 171}, 2138 { 56, 34, 51, 104, 114, 102, 29, 93, 77}, 2139 { 107, 54, 32, 26, 51, 1, 81, 43, 31}, 2140 { 39, 28, 85, 171, 58, 165, 90, 98, 64}, 2141 { 34, 22, 116, 206, 23, 34, 43, 166, 73}, 2142 { 68, 25, 106, 22, 64, 171, 36, 225, 114}, 2143 { 34, 19, 21, 102, 132, 188, 16, 76, 124}, 2144 { 62, 18, 78, 95, 85, 57, 50, 48, 51} 2145 }, 2146 { 2147 { 193, 101, 35, 159, 215, 111, 89, 46, 111}, 2148 { 60, 148, 31, 172, 219, 228, 21, 18, 111}, 2149 { 112, 113, 77, 85, 179, 255, 38, 120, 114}, 2150 { 40, 42, 1, 196, 245, 209, 10, 25, 109}, 2151 { 100, 80, 8, 43, 154, 1, 51, 26, 71}, 2152 { 88, 43, 29, 140, 166, 213, 37, 43, 154}, 2153 { 61, 63, 30, 155, 67, 45, 68, 1, 209}, 2154 { 142, 78, 78, 16, 255, 128, 34, 197, 171}, 2155 { 41, 40, 5, 102, 211, 183, 4, 1, 221}, 2156 { 51, 50, 17, 168, 209, 192, 23, 25, 82} 2157 }, 2158 { 2159 { 125, 98, 42, 88, 104, 85, 117, 175, 82}, 2160 { 95, 84, 53, 89, 128, 100, 113, 101, 45}, 2161 { 75, 79, 123, 47, 51, 128, 81, 171, 1}, 2162 { 57, 17, 5, 71, 102, 57, 53, 41, 49}, 2163 { 115, 21, 2, 10, 102, 255, 166, 23, 6}, 2164 { 38, 33, 13, 121, 57, 73, 26, 1, 85}, 2165 { 41, 10, 67, 138, 77, 110, 90, 47, 114}, 2166 { 101, 29, 16, 10, 85, 128, 101, 196, 26}, 2167 { 57, 18, 10, 102, 102, 213, 34, 20, 43}, 2168 { 117, 20, 15, 36, 163, 128, 68, 1, 26} 2169 }, 2170 { 2171 { 138, 31, 36, 171, 27, 166, 38, 44, 229}, 2172 { 67, 87, 58, 169, 82, 115, 26, 59, 179}, 2173 { 63, 59, 90, 180, 59, 166, 93, 73, 154}, 2174 { 40, 40, 21, 116, 143, 209, 34, 39, 175}, 2175 { 57, 46, 22, 24, 128, 1, 54, 17, 37}, 2176 { 47, 15, 16, 183, 34, 223, 49, 45, 183}, 2177 { 46, 17, 33, 183, 6, 98, 15, 32, 183}, 2178 { 65, 32, 73, 115, 28, 128, 23, 128, 205}, 2179 { 40, 3, 9, 115, 51, 192, 18, 6, 223}, 2180 { 87, 37, 9, 115, 59, 77, 64, 21, 47} 2181 }, 2182 { 2183 { 104, 55, 44, 218, 9, 54, 53, 130, 226}, 2184 { 64, 90, 70, 205, 40, 41, 23, 26, 57}, 2185 { 54, 57, 112, 184, 5, 41, 38, 166, 213}, 2186 { 30, 34, 26, 133, 152, 116, 10, 32, 134}, 2187 { 75, 32, 12, 51, 192, 255, 160, 43, 51}, 2188 { 39, 19, 53, 221, 26, 114, 32, 73, 255}, 2189 { 31, 9, 65, 234, 2, 15, 1, 118, 73}, 2190 { 88, 31, 35, 67, 102, 85, 55, 186, 85}, 2191 { 56, 21, 23, 111, 59, 205, 45, 37, 192}, 2192 { 55, 38, 70, 124, 73, 102, 1, 34, 98} 2193 }, 2194 { 2195 { 102, 61, 71, 37, 34, 53, 31, 243, 192}, 2196 { 69, 60, 71, 38, 73, 119, 28, 222, 37}, 2197 { 68, 45, 128, 34, 1, 47, 11, 245, 171}, 2198 { 62, 17, 19, 70, 146, 85, 55, 62, 70}, 2199 { 75, 15, 9, 9, 64, 255, 184, 119, 16}, 2200 { 37, 43, 37, 154, 100, 163, 85, 160, 1}, 2201 { 63, 9, 92, 136, 28, 64, 32, 201, 85}, 2202 { 86, 6, 28, 5, 64, 255, 25, 248, 1}, 2203 { 56, 8, 17, 132, 137, 255, 55, 116, 128}, 2204 { 58, 15, 20, 82, 135, 57, 26, 121, 40} 2205 }, 2206 { 2207 { 164, 50, 31, 137, 154, 133, 25, 35, 218}, 2208 { 51, 103, 44, 131, 131, 123, 31, 6, 158}, 2209 { 86, 40, 64, 135, 148, 224, 45, 183, 128}, 2210 { 22, 26, 17, 131, 240, 154, 14, 1, 209}, 2211 { 83, 12, 13, 54, 192, 255, 68, 47, 28}, 2212 { 45, 16, 21, 91, 64, 222, 7, 1, 197}, 2213 { 56, 21, 39, 155, 60, 138, 23, 102, 213}, 2214 { 85, 26, 85, 85, 128, 128, 32, 146, 171}, 2215 { 18, 11, 7, 63, 144, 171, 4, 4, 246}, 2216 { 35, 27, 10, 146, 174, 171, 12, 26, 128} 2217 }, 2218 { 2219 { 190, 80, 35, 99, 180, 80, 126, 54, 45}, 2220 { 85, 126, 47, 87, 176, 51, 41, 20, 32}, 2221 { 101, 75, 128, 139, 118, 146, 116, 128, 85}, 2222 { 56, 41, 15, 176, 236, 85, 37, 9, 62}, 2223 { 146, 36, 19, 30, 171, 255, 97, 27, 20}, 2224 { 71, 30, 17, 119, 118, 255, 17, 18, 138}, 2225 { 101, 38, 60, 138, 55, 70, 43, 26, 142}, 2226 { 138, 45, 61, 62, 219, 1, 81, 188, 64}, 2227 { 32, 41, 20, 117, 151, 142, 20, 21, 163}, 2228 { 112, 19, 12, 61, 195, 128, 48, 4, 24} 2229 } 2230 }; 2232 ---- End code block ---------------------------------------- 2234 12. Intraframe Prediction 2236 Intraframe prediction uses already-coded macroblocks within the 2237 current frame to approximate the contents of the current macroblock. 2238 It applies to intra-coded macroblocks in an interframe and to all 2239 macroblocks in a key frame. 2241 Relative to the current macroblock "M", the already-coded macroblocks 2242 include all macroblocks above M together with the macroblocks on the 2243 same row as, and to the left of, M, though at most four of these 2244 macroblocks are actually used: the block "A" directly above M, the 2245 blocks immediately to the left and right of A, and the block 2246 immediately to the left of M. 2248 Each of the prediction modes (i.e., means of extrapolation from 2249 already-calculated values) uses fairly simple arithmetic on pixel 2250 values whose positions, relative to the current position, are defined 2251 by the mode. 2253 The chroma (U and V) and luma (Y) predictions are independent of each 2254 other. 2256 The relative addressing of pixels applied to macroblocks on the upper 2257 row or left column of the frame will sometimes cause pixels outside 2258 the visible frame to be referenced. Usually such out-of-bounds 2259 pixels have an assumed value of 129 for pixels to the left of the 2260 leftmost column of the visible frame and 127 for pixels above the top 2261 row of the visible frame (including the special case of the pixel 2262 above and to the left of the top-left pixel in the visible frame). 2263 Exceptions to this (associated to certain modes) will be noted below. 2265 The already-coded macroblocks referenced by intra-prediction have 2266 been "reconstructed", that is, have been predicted and residue- 2267 adjusted (as described in Chapter 14), but have not been loop- 2268 filtered. While it does process the edges between individual 2269 macroblocks and individual subblocks, loop filtering (described in 2270 Chapter 15) is applied to the frame as a whole, after all of the 2271 macroblocks have been reconstructed. 2273 12.1. mb_skip_coeff 2275 The single bool flag is decoded using prob_skip_false if and only if 2276 mb_no_coeff_skip is set to 1 (see Sections 9.10 and 9.11). If 2277 mb_no_coeff_skip is set to 0 then this value defaults to 0. 2279 12.2. Chroma Prediction 2281 The chroma prediction is a little simpler than the luma prediction, 2282 so we treat it first. Each of the chroma modes treats U and V 2283 identically, that is, the U and V prediction values are calculated in 2284 parallel, using the same relative addressing and arithmetic in each 2285 of the two planes. 2287 The modes extrapolate prediction values using the 8-pixel row "A" 2288 lying immediately above the block (that is, the bottom chroma row of 2289 the macroblock immediately above the current macroblock) and the 2290 8-pixel column "L" immediately to the left of the block (that is, the 2291 rightmost chroma column of the macroblock immediately to the left of 2292 the current macroblock). 2294 Vertical prediction (chroma mode V_PRED) simply fills each 8-pixel 2295 row of the 8x8 chroma block with a copy of the "above" row (A). If 2296 the current macroblock lies on the top row of the frame, all 8 of the 2297 pixel values in A are assigned the value 127. 2299 Similarly, horizontal prediction (H_PRED) fills each 8-pixel column 2300 of the 8x8 chroma block with a copy of the "left" column (L). If the 2301 current macroblock is in the left column of the frame, all 8 pixel 2302 values in L are assigned the value 129. 2304 DC prediction (DC_PRED) fills the 8x8 chroma block with a single 2305 value. In the generic case of a macroblock lying below the top row 2306 and right of the leftmost column of the frame, this value is the 2307 average of the 16 (genuinely visible) pixels in the (union of the) 2308 above row A and left column L. 2310 Otherwise, if the current macroblock lies on the top row of the 2311 frame, the average of the 8 pixels in L is used; if it lies in the 2312 left column of the frame, the average of the 8 pixels in A is used. 2313 Note that the averages used in these exceptional cases are not the 2314 same as those that would be arrived at by using the out-of-bounds A 2315 and L values defined for V_PRED and H_PRED. In the case of the 2316 leftmost macroblock on the top row of the frame the 8x8 block is 2317 simply filled with the constant value 128. 2319 For DC_PRED, apart from the exceptional case of the top left 2320 macroblock, we are averaging either 16 or 8 pixel values to get a 2321 single prediction value that fills the 8x8 block. The rounding is 2322 done as follows: 2324 ---- Begin code block -------------------------------------- 2326 int sum; /* sum of 8 or 16 pixels at (at least) 16-bit precision */ 2327 int shf; /* base 2 logarithm of the number of pixels (3 or 4) */ 2329 Pixel DCvalue = (sum + (1 << (shf-1))) >> shf; 2331 ---- End code block ---------------------------------------- 2333 Because the summands are all valid pixels, no "clamp" is necessary in 2334 the calculation of DCvalue. 2336 The remaining "True Motion" (TM_PRED) chroma mode gets its name from 2337 an older technique of video compression used by On2 Technologies, to 2338 which it bears some relation. In addition to the row "A" and column 2339 "L", TM_PRED uses the pixel "P" above and to the left of the chroma 2340 block. 2342 The following figure gives an example of how TM_PRED works: 2344 ---- Begin code block -------------------------------------- 2346 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2347 | P | A0 | A1 | A2 | A3 | A4 | A5 | A6 | A7 | 2348 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2349 | L0 | X00 | X01 | X02 | X03 | X04 | X05 | X06 | X07 | 2350 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2351 | L1 | X10 | X11 | X12 | X13 | X14 | X15 | X16 | X17 | 2352 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2353 | L2 | X20 | X21 | X22 | X23 | X24 | X25 | X26 | X27 | 2354 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2355 | L3 | X30 | X31 | X32 | X33 | X34 | X35 | X36 | X37 | 2356 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2357 | L4 | X40 | X41 | X42 | X43 | X44 | X45 | X46 | X47 | 2358 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2359 | L5 | X50 | X51 | X52 | X53 | X54 | X55 | X56 | X57 | 2360 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2361 | L6 | X60 | X61 | X62 | X63 | X64 | X65 | X66 | X67 | 2362 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2363 | L7 | X70 | X71 | X72 | X73 | X74 | X75 | X76 | X77 | 2364 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2366 ---- End code block ---------------------------------------- 2368 Where P, As and Ls represent reconstructed pixel values from 2369 previously coded blocks, and X00 through X77 represent predicted 2370 values for the current block. TM_PRED uses the following equation to 2371 calculate X_ij: 2373 X_ij = L_i + A_j - P (i, j=0, 1, 2, 3) 2375 The exact algorithm is as follows: 2377 ---- Begin code block -------------------------------------- 2379 void TMpred( 2380 Pixel b[8][8], /* chroma (U or V) prediction block */ 2381 const Pixel A[8], /* row of already-constructed pixels 2382 above block */ 2383 const Pixel L[8], /* column of "" just to the left of 2384 block */ 2385 const Pixel P /* pixel just to the left of A and 2386 above L*/ 2387 ) { 2388 int r = 0; /* row */ 2389 do { 2390 int c = 0; /* column */ 2391 do { 2392 b[r][c] = clamp255( L[r]+ A[c] - P); 2393 } while( ++c < 8); 2394 } while( ++r < 8); 2395 } 2397 ---- End code block ---------------------------------------- 2399 Note that the process could equivalently be described as propagating 2400 the vertical differences between pixels in L (starting from P), using 2401 the pixels from A to start each column. 2403 An implementation of chroma intra-prediction may be found in the 2404 reference decoder file reconintra.c. 2406 Unlike DC_PRED, for macroblocks on the top row or left edge TM_PRED 2407 does use the out-of-bounds values of 127 and 129 (respectively) 2408 defined for V_PRED and H_PRED. 2410 12.3. Luma Prediction 2412 The prediction processes for the first four 16x16 luma modes 2413 (DC_PRED, V_PRED, H_PRED, and TM_PRED) are essentially identical to 2414 the corresponding chroma prediction processes described above, the 2415 only difference being that we are predicting a single 16x16 luma 2416 block instead of two 8x8 chroma blocks. 2418 Thus, the row "A" and column "L" here contain 16 pixels, the DC 2419 prediction is calculated using 16 or 32 pixels (and shf is 4 or 5), 2420 and we of course fill the entire prediction buffer, that is, 16 rows 2421 (or columns) containing 16 pixels each. The reference implementation 2422 of 16x16 luma prediction is also in reconintra.c. 2424 In the remaining luma mode (B_PRED), each 4x4 Y subblock is 2425 independently predicted using one of ten modes (listed, along with 2426 their encodings, in Chapter 11). 2428 Also, unlike the full-macroblock modes already described, some of the 2429 subblock modes use prediction pixels above and to the right of the 2430 current subblock. In detail, each 4x4 subblock "B" is predicted 2431 using (at most) the 4-pixel column "L" immediately to the left of B 2432 and the 8-pixel row "A" immediately above B, consisting of the 4 2433 pixels above B followed by the 4 adjacent pixels above and to the 2434 right of B, together with the single pixel "P" immediately to the 2435 left of A (and immediately above L). 2437 For the purpose of subblock intra-prediction, the pixels immediately 2438 to the left and right of a pixel in a subblock are the same as the 2439 pixels immediately to the left and right of the corresponding pixel 2440 in the frame buffer "F". Vertical offsets behave similarly: The 2441 above row A lies immediately above B in F, and the adjacent pixels in 2442 the left column L are separated by a single row in F. 2444 Because entire macroblocks (as opposed to their constituent 2445 subblocks) are reconstructed in raster-scan order, for subblocks 2446 lying along the right edge (and not along the top row) of the current 2447 macroblock, the four "extra" prediction pixels in A above and to the 2448 right of B have not yet actually been constructed. 2450 Subblocks 7, 11, and 15 are affected. All three of these subblocks 2451 use the same extra pixels as does subblock 3 (at the upper right 2452 corner of the macroblock), namely the 4 pixels immediately above and 2453 to the right of subblock 3. Writing (R,C) for a frame buffer 2454 position offset from the upper left corner of the current macroblock 2455 by R rows and C columns, the extra pixels for all the right-edge 2456 subblocks (3, 7, 11, and 15) are at positions (-1,16), (-1,17), 2457 (-1,18), and (-1,19). For the right-most macroblock in each 2458 macroblock row except the top row, the extra pixels shall use the 2459 same value as the pixel at position (-1, 15), which is the right-most 2460 visible pixel on the line immediately above the macroblock row. For 2461 the top macroblock row, all the extra pixels assume a value of 127. 2463 The details of the prediction modes are most easily described in 2464 code. 2466 ---- Begin code block -------------------------------------- 2468 /* Result pixels are often averages of two or three predictor 2469 pixels. The following subroutines are used to calculate 2470 these averages. Because the arguments are valid pixels, no 2471 clamping is necessary. An actual implementation would 2472 probably use inline functions or macros. */ 2474 /* Compute weighted average centered at y w/adjacent x, z */ 2476 Pixel avg3( Pixel x, Pixel y, Pixel z) { 2477 return (x + y + y + z + 2) >> 2;} 2479 /* Weighted average of 3 adjacent pixels centered at p */ 2481 Pixel avg3p( const Pixel *p) { return avg3( p[-1], p[0], p[1]);} 2483 /* Simple average of x and y */ 2485 Pixel avg2( Pixel x, Pixel y) { return (x + y + 1) >> 1;} 2487 /* Average of p[0] and p[1] may be considered to be a synthetic 2488 pixel lying between the two, that is, one half-step past p. */ 2490 Pixel avg2p( const Pixel *p) { return avg2( p[0], p[1]);} 2492 void subblock_intra_predict( 2493 Pixel B[4][4], /* Y subblock prediction buffer */ 2494 const Pixel *A, /* A[0]...A[7] = above row, A[-1] = P */ 2495 const Pixel *L, /* L[0]...L[3] = left column, L[-1] = P */ 2496 intra_bmode mode /* enum is in section 11.1 above */ 2497 ) { 2498 Pixel E[9]; /* 9 already-constructed edge pixels */ 2499 E[0] = L[3]; E[1] = L[2]; E[2] = L[1]; E[3] = L[0]; 2500 E[4] = A[-1]; /* == L[-1] == P */ 2501 E[5] = A[0]; E[6] = A[1]; E[7] = A[2]; E[8] = A[3]; 2503 switch( mode) { 2504 /* First four modes are similar to corresponding 2505 full-block modes. */ 2507 case B_DC_PRED: 2508 { 2509 int v = 4; /* DC sum/avg, 4 is rounding adjustment */ 2510 int i = 0; do { v += A[i] + L[i];} while( ++i < 4); 2511 v >>= 3; /* averaging 8 pixels */ 2512 i = 0; do { /* fill prediction buffer with constant DC 2513 value */ 2514 int j = 0; do { B[i][j] = v;} while( ++j < 4); 2515 } while( ++i < 4); 2516 break; 2518 } 2520 case B_TM_PRED: /* just like 16x16 TM_PRED */ 2521 { 2522 int r = 0; do { 2523 int c = 0; do { 2524 B[r][c] = clamp255( L[r] + A[c] - A[-1]); 2525 } while( ++c < 4); 2526 } while( ++r < 4); 2527 break; 2528 } 2530 case B_VE_PRED: /* like 16x16 V_PRED except using averages */ 2531 { 2532 int c = 0; do { /* all 4 rows = smoothed top row */ 2533 B[0][c] = B[1][c] = B[2][c] = B[3][c] = avg3p( A + c); 2534 } while( ++c < 4); 2535 break; 2536 } 2538 case B_HE_PRED: /* like 16x16 H_PRED except using averages */ 2539 { 2540 /* Bottom row is exceptional because L[4] does not exist */ 2541 int v = avg3( L[2], L[3], L[3]); 2542 int r = 3; while( 1) { /* all 4 columns = smoothed left 2543 column */ 2544 B[r][0] = B[r][1] = B[r][2] = B[r][3] = v; 2545 if( --r < 0) 2546 break; 2547 v = avg3p( L + r); /* upper 3 rows use average of 2548 3 pixels */ 2549 } 2550 break; 2551 } 2553 /* The remaining six "diagonal" modes subdivide the 2554 prediction buffer into diagonal lines. All the pixels 2555 on each line are assigned the same value; this value is 2556 (a smoothed or synthetic version of) an 2557 already-constructed predictor value lying on the same 2558 line. For clarity, in the comments, we express the 2559 positions of these predictor pixels relative to the 2560 upper left corner of the destination array B. 2562 These modes are unique to subblock prediction and have 2563 no full-block analogues. The first two use lines at 2564 +|- 45 degrees from horizontal (or, equivalently, 2565 vertical), that is, lines whose slopes are +|- 1. */ 2567 case B_LD_PRED: /* southwest (left and down) step = 2568 (-1, 1) or (1,-1) */ 2569 /* avg3p( A + j) is the "smoothed" pixel at (-1,j) */ 2570 B[0][0] = avg3p( A + 1); 2571 B[0][1] = B[1][0] = avg3p( A + 2); 2572 B[0][2] = B[1][1] = B[2][0] = avg3p( A + 3); 2573 B[0][3] = B[1][2] = B[2][1] = B[3][0] = avg3p( A + 4); 2574 B[1][3] = B[2][2] = B[3][1] = avg3p( A + 5); 2575 B[2][3] = B[3][2] = avg3p( A + 6); 2576 B[3][3] = avg3( A[6], A[7], A[7]); /* A[8] does not exist */ 2577 break; 2579 case B_RD_PRED: /* southeast (right and down) step = 2580 (1,1) or (-1,-1) */ 2581 B[3][0] = avg3p( E + 1); /* predictor is from (2, -1) */ 2582 B[3][1] = B[2][0] = avg3p( E + 2); /* (1, -1) */ 2583 B[3][2] = B[2][1] = B[1][0] = avg3p( E + 3); /* (0, -1) */ 2584 B[3][3] = B[2][2] = B[1][1] = B[0][0] = 2585 avg3p( E + 4); /* (-1, -1) */ 2586 B[2][3] = B[1][2] = B[0][1] = avg3p( E + 5); /* (-1, 0) */ 2587 B[1][3] = B[0][2] = avg3p( E + 6); /* (-1, 1) */ 2588 B[0][3] = avg3p( E + 7); /* (-1, 2) */ 2589 break; 2591 /* The remaining 4 diagonal modes use lines whose slopes are 2592 +|- 2 and +|- 1/2. The angles of these lines are roughly 2593 +|- 27 degrees from horizontal or vertical. 2595 Unlike the 45 degree diagonals, here we often need to 2596 "synthesize" predictor pixels midway between two actual 2597 predictors using avg2p(p), which we think of as returning 2598 the pixel "at" p[1/2]. */ 2600 case B_VR_PRED: /* SSE (vertical right) step = 2601 (2,1) or (-2,-1) */ 2602 B[3][0] = avg3p( E + 2); /* predictor is from (1, -1) */ 2603 B[2][0] = avg3p( E + 3); /* (0, -1) */ 2604 B[3][1] = B[1][0] = avg3p( E + 4); /* (-1, -1) */ 2605 B[2][1] = B[0][0] = avg2p( E + 4); /* (-1, -1/2) */ 2606 B[3][2] = B[1][1] = avg3p( E + 5); /* (-1, 0) */ 2607 B[2][2] = B[0][1] = avg2p( E + 5); /* (-1, 1/2) */ 2608 B[3][3] = B[1][2] = avg3p( E + 6); /* (-1, 1) */ 2609 B[2][3] = B[0][2] = avg2p( E + 6); /* (-1, 3/2) */ 2610 B[1][3] = avg3p( E + 7); /* (-1, 2) */ 2611 B[0][3] = avg2p( E + 7); /* (-1, 5/2) */ 2612 break; 2614 case B_VL_PRED: /* SSW (vertical left) step = 2615 (2,-1) or (-2,1) */ 2616 B[0][0] = avg2p( A); /* predictor is from (-1, 1/2) */ 2617 B[1][0] = avg3p( A + 1); /* (-1, 1) */ 2618 B[2][0] = B[0][1] = avg2p( A + 1); /* (-1, 3/2) */ 2619 B[1][1] = B[3][0] = avg3p( A + 2); /* (-1, 2) */ 2620 B[2][1] = B[0][2] = avg2p( A + 2); /* (-1, 5/2) */ 2621 B[3][1] = B[1][2] = avg3p( A + 3); /* (-1, 3) */ 2622 B[2][2] = B[0][3] = avg2p( A + 3); /* (-1, 7/2) */ 2623 B[3][2] = B[1][3] = avg3p( A + 4); /* (-1, 4) */ 2624 /* Last two values do not strictly follow the pattern. */ 2625 B[2][3] = avg3p( A + 5); /* (-1, 5) [avg2p( A + 4) = 2626 (-1,9/2)] */ 2627 B[3][3] = avg3p( A + 6); /* (-1, 6) [avg3p( A + 5) = 2628 (-1,5)] */ 2629 break; 2631 case B_HD_PRED: /* ESE (horizontal down) step = 2632 (1,2) or (-1,-2) */ 2633 B[3][0] = avg2p( E); /* predictor is from (5/2, -1) */ 2634 B[3][1] = avg3p( E + 1); /* (2, -1) */ 2635 B[2][0] = B[3][2] = svg2p( E + 1); /* ( 3/2, -1) */ 2636 B[2][1] = B[3][3] = avg3p( E + 2); /* ( 1, -1) */ 2637 B[2][2] = B[1][0] = avg2p( E + 2); /* ( 1/2, -1) */ 2638 B[2][3] = B[1][1] = avg3p( E + 3); /* ( 0, -1) */ 2639 B[1][2] = B[0][0] = avg2p( E + 3); /* (-1/2, -1) */ 2640 B[1][3] = B[0][1] = avg3p( E + 4); /* ( -1, -1) */ 2641 B[0][2] = avg3p( E + 5); /* (-1, 0) */ 2642 B[0][3] = avg3p( E + 6); /* (-1, 1) */ 2643 break; 2645 case B_HU_PRED: /* ENE (horizontal up) step = (1,-2) 2646 or (-1,2) */ 2647 B[0][0] = avg2p( L); /* predictor is from ( 1/2, -1) */ 2648 B[0][1] = avg3p( L + 1); /* ( 1, -1) */ 2649 B[0][2] = B[1][0] = avg2p( L + 1); /* (3/2, -1) */ 2650 B[0][3] = B[1][1] = avg3p( L + 2); /* ( 2, -1) */ 2651 B[1][2] = B[2][0] = avg2p( L + 2); /* (5/2, -1) */ 2652 B[1][3] = B[2][1] = avg3( L[2], L[3], L[3]); /* ( 3, -1) */ 2653 /* Not possible to follow pattern for much of the bottom 2654 row because no (nearby) already-constructed pixels lie 2655 on the diagonals in question. */ 2656 B[2][2] = B[2][3] = B[3][0] = B[3][1] = B[3][2] = B[3][3] 2657 = L[3]; 2658 } 2659 } 2661 ---- End code block ---------------------------------------- 2662 The reference decoder implementation of subblock intra-prediction may 2663 be found in reconintra4x4.c. 2665 13. DCT Coefficient Decoding 2667 The second data partition consists of an encoding of the quantized 2668 DCT (and WHT) coefficients of the residue signal. As discussed in 2669 the format overview (Chapter 2), for each macroblock, the residue is 2670 added to the (intra- or inter-generated) prediction buffer to produce 2671 the final (except for loop-filtering) reconstructed macroblock. 2673 VP8 works exclusively with 4x4 DCTs and WHTs, applied to the 24 (or 2674 25 with the Y2 subblock) 4x4 subblocks of a macroblock. The ordering 2675 of macroblocks within any of the "residue" partitions in general 2676 follows the same raster-scan as used in the first "prediction" 2677 partition. 2679 For all intra- and inter-prediction modes apart from B_PRED (intra: 2680 whose Y subblocks are independently predicted) and SPLIT_MV (inter) 2681 each macroblock's residue record begins with the Y2 component of the 2682 residue, coded using a WHT. B_PRED and SPLIT_MV coded macroblocks 2683 omit this WHT, instead specifying the 0th DCT coefficient of each of 2684 the 16 Y subblocks as part of its DCT. 2686 After the optional Y2 block, the residue record continues with 16 2687 DCTs for the Y subblocks, followed by 4 DCTs for the U subblocks, 2688 ending with 4 DCTs for the V subblocks. The subblocks occur in the 2689 usual order. 2691 The DCTs and WHT are tree-coded using a 12-element alphabet whose 2692 members we call tokens. Except for the end of block token (which 2693 sets the remaining subblock coefficients to zero and is followed by 2694 the next block), each token (sometimes augmented with data 2695 immediately following the token) specifies the value of the single 2696 coefficient at the current (implicit) position and is followed by a 2697 token applying to the next (implicit) position. 2699 For all the Y and chroma subblocks, the ordering of the coefficients 2700 follows a so-called zig-zag order. DCTs begin at coefficient 1 if Y2 2701 is present, and begin at coefficient 0 if Y2 is absent. The WHT for 2702 a Y2 subblock always begins at coefficient 0. 2704 13.1. MB Without non-Zero Coefficient Values 2706 If the flag within macroblock mode info indicates that a macroblock 2707 does not have any non-zero coefficients, the decoding process of DCT 2708 coefficients is skipped for the macroblock. 2710 13.2. Coding of Individual Coefficient Values 2712 The coding of coefficient tokens is the same for the DCT and WHT and 2713 for the remainder of this chapter DCT should be taken to mean either 2714 DCT or WHT. 2716 All tokens (except end-of-block) specify either a single unsigned 2717 value or a range of unsigned values (immediately) followed by a 2718 simple probabilistic encoding of the offset of the value from the 2719 base of that range. 2721 Non-zero values (of either type) are then followed by a flag 2722 indicating the sign of the coded value (negative if 1, positive if 2723 0). 2725 Here are the tokens and decoding tree. 2727 ---- Begin code block -------------------------------------- 2729 typedef enum 2730 { 2731 DCT_0, /* value 0 */ 2732 DCT_1, /* 1 */ 2733 DCT_2, /* 2 */ 2734 DCT_3, /* 3 */ 2735 DCT_4, /* 4 */ 2736 dct_cat1, /* range 5 - 6 (size 2) */ 2737 dct_cat2, /* 7 - 10 (4) */ 2738 dct_cat3, /* 11 - 18 (8) */ 2739 dct_cat4, /* 19 - 34 (16) */ 2740 dct_cat5, /* 35 - 66 (32) */ 2741 dct_cat6, /* 67 - 2048 (1982) */ 2742 dct_eob, /* end of block */ 2744 num_dct_tokens /* 12 */ 2745 } 2746 dct_token; 2748 const tree_index coef_tree [2 * (num_dct_tokens - 1)] = 2749 { 2750 -dct_eob, 2, /* eob = "0" */ 2751 -DCT_0, 4, /* 0 = "10" */ 2752 -DCT_1, 6, /* 1 = "110" */ 2753 8, 12, 2754 -DCT_2, 10, /* 2 = "11100" */ 2755 -DCT_3, -DCT_4, /* 3 = "111010", 4 = "111011" */ 2756 14, 16, 2757 -dct_cat1, -dct_cat2, /* cat1 = "111100", 2758 cat2 = "111101" */ 2759 18, 20, 2760 -dct_cat3, -dct_cat4, /* cat3 = "1111100", 2761 cat4 = "1111101" */ 2762 -dct_cat5, -dct_cat6 /* cat4 = "1111110", 2763 cat4 = "1111111" */ 2764 }; 2766 ---- End code block ---------------------------------------- 2768 In general, all DCT coefficients are decoded using the same tree. 2769 However, if the preceding coefficient is a DCT_0, decoding will skip 2770 the first branch since it is not possible for dct_eob to follow a 2771 DCT_0. 2773 The tokens dct_cat1 ... dct_cat6 specify ranges of unsigned values, 2774 the value within the range being formed by adding an unsigned offset 2775 (whose width is 1, 2, 3, 4, 5, or 11 bits, respectively) to the base 2776 of the range, using the following algorithm and fixed probability 2777 tables. 2779 ---- Begin code block -------------------------------------- 2781 uint DCTextra( bool_decoder *d, const Prob *p) 2782 { 2783 uint v = 0; 2784 do { v += v + read_bool( d, *p);} while( *++p); 2785 return v; 2786 } 2788 const Prob Pcat1[] = { 159, 0}; 2789 const Prob Pcat2[] = { 165, 145, 0}; 2790 const Prob Pcat3[] = { 173, 148, 140, 0}; 2791 const Prob Pcat4[] = { 176, 155, 140, 135, 0}; 2792 const Prob Pcat5[] = { 180, 157, 141, 134, 130, 0}; 2793 const Prob Pcat6[] = 2794 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0}; 2796 ---- End code block ---------------------------------------- 2798 If v, the unsigned value decoded using the coefficient tree, possibly 2799 augmented by the process above, is non-zero, its sign is set by 2800 simply reading a flag: 2802 ---- Begin code block -------------------------------------- 2804 if( read_bool( d, 128)) 2805 v = -v; 2807 ---- End code block ---------------------------------------- 2809 13.3. Token Probabilities 2811 The probability specification for the token tree (unlike that for the 2812 "extra bits" described above) is rather involved. It uses three 2813 pieces of context to index a large probability table, the contents of 2814 which may be incrementally modified in the frame header. The full 2815 (non-constant) probability table is laid out as follows. 2817 ---- Begin code block -------------------------------------- 2819 Prob coef_probs [4] [8] [3] [num_dct_tokens-1]; 2821 ---- End code block ---------------------------------------- 2822 Working from the outside in, the outermost dimension is indexed by 2823 the type of plane being decoded: 2825 o 0 - Y beginning at coefficient 1 (i.e., Y after Y2) 2827 o 1 - Y2 2829 o 2 - U or V 2831 o 3 - Y beginning at coefficient 0 (i.e., Y in the absence of Y2). 2833 The next dimension is selected by the position of the coefficient 2834 being decoded. That position c steps by ones up to 15, starting from 2835 zero for block types 1, 2, or 3 and starting from one for block type 2836 0. The second array index is then 2838 ---- Begin code block -------------------------------------- 2840 coef_bands [c] 2842 ---- End code block ---------------------------------------- 2844 where 2846 ---- Begin code block -------------------------------------- 2848 const int coef_bands [16] = { 2849 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7 2850 }; 2852 ---- End code block ---------------------------------------- 2854 is a fixed mapping of position to "band". 2856 The third dimension is the trickiest. Roughly speaking, it measures 2857 the "local complexity" or extent to which nearby coefficients are 2858 non-zero. 2860 For the first coefficient (DC, unless the block type is 0), we 2861 consider the (already encoded) blocks within the same plane (Y2, Y, 2862 U, or V) above and to the left of the current block. The context 2863 index is then the number (0, 1 or 2) of these blocks that had at 2864 least one non-zero coefficient in their residue record. Specifically 2865 for Y2, because macroblocks above and to the left may or may not have 2866 a Y2 block, the block above is determined by the most recent 2867 macroblock in the same column that has a Y2 block, and the block to 2868 the left is determined by the most recent macroblock in the same row 2869 that has a Y2 block. 2871 Beyond the first coefficient, the context index is determined by the 2872 absolute value of the most recently decoded coefficient (necessarily 2873 within the current block) and is 0 if the last coefficient was a 2874 zero, 1 if it was plus or minus one, and 2 if its absolute value 2875 exceeded one. 2877 Note that the intuitive meaning of this measure changes as 2878 coefficients are decoded. For example, prior to the first token, a 2879 zero means that the neighbors are empty, suggesting that the current 2880 block may also be empty. After the first token, because an end-of- 2881 block token must have at least one non-zero value before it, a zero 2882 means that we just decoded a zero and hence guarantees that a non- 2883 zero coefficient will appear later in this block. However, this 2884 shift in meaning is perfectly okay because the complete context 2885 depends also on the coefficient band (and since band 0 is occupied 2886 exclusively by position 0). 2888 As with other contexts used by VP8, the "neighboring block" context 2889 described here needs a special definition for subblocks lying along 2890 the top row or left edge of the frame. These "non-existent" 2891 predictors above and to the left of the image are simply taken to be 2892 empty -- that is, taken to contain no non-zero coefficients. 2894 The residue decoding of each macroblock then requires, in each of two 2895 directions (above and to the left), an aggregate coefficient 2896 predictor consisting of a single Y2 predictor, two predictors for 2897 each of U and V, and four predictors for Y. In accordance with the 2898 scan-ordering of macroblocks, a decoder needs to maintain a single 2899 "left" aggregate predictor and a row of "above" aggregate predictors. 2901 Before decoding any residue, these maintained predictors may simply 2902 be cleared, in compliance with the definition of "non-existent" 2903 prediction. After each block is decoded, the two predictors 2904 referenced by the block are replaced with the (empty or non-empty) 2905 state of the block, in preparation for the later decoding of the 2906 blocks below and to the right of the block just decoded. 2908 The fourth, and final, dimension of the token probability array is of 2909 course indexed by (half) the position in the token tree structure, as 2910 are all tree probability arrays. 2912 The below pseudo-code illustrates the decoding process. Note that 2913 criteria, functions, etc. delimited with ** are either dependent on 2914 decoder architecture or are elaborated on elsewhere in this document. 2916 ---- Begin code block -------------------------------------- 2918 int block[16] = { 0 }; /* current 4x4 block coeffs */ 2919 int firstCoeff = 0; 2920 int plane; 2921 int ctx2; 2922 int ctx3 = 0; /* the 3rd context referred to in above description */ 2923 Prob *probTable; 2924 int token; 2925 int sign; 2926 int absValue; 2927 int extraBits; 2928 bool prevCoeffWasZero = false; 2929 bool currentBlockHasCoeffs = false; 2930 /* base coeff abs values per each category, elem #0 is 2931 DCT_VAL_CATEGORY1, * #1 is DCT_VAL_CATEGORY2 etc */ 2932 int categoryBase[6] = { 5, 7, 11, 19, 35, 67 }; 2934 /* Determine plane to use */ 2935 if( **current_block_is_Y2_block** ) plane = 0; 2936 else if ( **current_block_is_chroma** ) plane = 2; 2937 else if ( **current_macroblock_has_Y2** ) plane = 1; 2938 else plane = 3; 2940 /* For luma blocks of an "Y2 macroblock" we skip coeff index #0 */ 2941 if( plane == 1 ) 2942 firstCoeff++; 2944 /* Determine whether neighbour 4x4 blocks have coeffiecients. 2945 This is dependant of the plane we are currently decoding; 2946 i.e. we check only coefficients from same plane as current 2947 block. */ 2948 if( **left_neighbor_block_has_coefficients(plane)** ) 2949 ctx3++; 2950 if( **above_neighbor_block_has_coefficients(plane)** ) 2951 ctx3++; 2953 for( i = firstCoeff ; i < 16 ; ++i ) 2954 { 2955 ctx2 = coef_bands[i]; 2956 probTable = coef_probs[plane][ctx2][ctx3]; 2958 /* skip first code (dct_eob) if previous token was DCT_0 */ 2959 if( prevCoeffWasZero ) 2960 token = treed_read ( d, **coef_tree_without_eob**, 2961 probTable ); 2962 else 2963 token = treed_read ( d, coef_tree, probTable ); 2965 if( token == dct_eob ) 2966 break; 2968 if( token != DCT_0 ) 2969 { 2970 currentBlockHasCoeffs = true; 2971 if( **token_has_extra_bits(token)** ) 2972 { 2973 extraBits = DCTextra( token ); 2974 absValue = 2975 categoryBase[**token_to_cat_index(token)**] + 2976 extraBits; 2977 } 2978 else 2979 { 2980 absValue = **token_to_abs_value(token)**; 2981 } 2983 sign = read_bool(d, 128); 2984 block[i] = sign ? -absValue : absValue; 2985 } 2986 else 2987 { 2988 absValue = 0; 2989 } 2991 /* Set contexts and stuff for next coeff */ 2992 if( absValue == 0 ) ctx3 = 0; 2993 else if ( absValue == 1 ) ctx3 = 1; 2994 else ctx3 = 2; 2995 prevCoeffWasZero = true; 2996 } 2998 /* Store current block status to decoder internals */ 2999 **block_has_coefficients[currentMb][currentBlock]** = 3000 currentBlockHasCoeffs; 3002 ---- End code block ---------------------------------------- 3004 While we have in fact completely described the coefficient decoding 3005 procedure, the reader will probably find it helpful to consult the 3006 reference implementation, which can be found in the file 3007 detokenize.c. 3009 13.4. Token Probability Updates 3011 As mentioned above, the token-decoding probabilities may change from 3012 frame to frame. After detection of a key frame, they are of course 3013 set to their defaults shown in Section 13.5; this must occur before 3014 decoding the remainder of the header, as both key frames and 3015 interframes may adjust these probabilities. 3017 The layout and semantics of the coefficient probability update record 3018 (Section I of the frame header) are straightforward. For each 3019 position in the coef_probs array there occurs a fixed-probability 3020 bool indicating whether or not the corresponding probability should 3021 be updated. If the bool is true, there follows a P(8) replacing that 3022 probability. Note that updates are cumulative, that is, a 3023 probability updated on one frame is in effect for all ensuing frames 3024 until the next key frame, or until the probability is explicitly 3025 updated by another frame. 3027 The algorithm to effect the foregoing is simple: 3029 ---- Begin code block -------------------------------------- 3031 int i = 0; do { 3032 int j = 0; do { 3033 int k = 0; do { 3034 int t = 0; do { 3036 if( read_bool( d, coef_update_probs [i] [j] [k] [t])) 3037 coef_probs [i] [j] [k] [t] = read_literal( d, 8); 3039 } while( ++t < num_dct_tokens - 1); 3040 } while( ++k < 3); 3041 } while( ++j < 8); 3042 } while( ++i < 4); 3044 ---- End code block ---------------------------------------- 3046 The (constant) update probabilities are as follows (they may also be 3047 found in the reference decoder file coef_update_probs.c). 3049 ---- Begin code block -------------------------------------- 3051 const Prob coef_update_probs [4] [8] [3] [num_dct_tokens-1] = 3052 { 3053 { 3054 { 3055 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3056 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3057 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3058 }, 3059 { 3060 { 176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3061 { 223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3062 { 249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255} 3063 }, 3064 { 3065 { 255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3066 { 234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3067 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3068 }, 3069 { 3070 { 255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3071 { 239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3072 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3073 }, 3074 { 3075 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3076 { 251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3077 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3078 }, 3079 { 3080 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3081 { 251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3082 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3083 }, 3084 { 3085 { 255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255}, 3086 { 250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255}, 3087 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3088 }, 3089 { 3090 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3091 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3092 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3093 } 3094 }, 3095 { 3096 { 3097 { 217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3098 { 225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255}, 3099 { 234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255} 3100 }, 3101 { 3102 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3103 { 223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3104 { 238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3105 }, 3106 { 3107 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3108 { 249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3109 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3110 }, 3111 { 3112 { 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3113 { 247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3114 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3115 }, 3116 { 3117 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3118 { 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3119 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3120 }, 3121 { 3122 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3123 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3124 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3125 }, 3126 { 3127 { 255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3128 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3129 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3130 }, 3131 { 3132 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3133 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3134 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3135 } 3136 }, 3137 { 3138 { 3139 { 186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255}, 3140 { 234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255}, 3141 { 251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255} 3142 }, 3143 { 3144 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3145 { 236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3146 { 251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255} 3147 }, 3148 { 3149 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3150 { 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3151 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3152 }, 3153 { 3154 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3155 { 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3156 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3157 }, 3158 { 3159 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3160 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3161 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3162 }, 3163 { 3164 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3165 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3166 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3167 }, 3168 { 3169 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3170 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3171 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3172 }, 3173 { 3174 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3175 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3176 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3177 } 3178 }, 3179 { 3180 { 3181 { 248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3182 { 250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255}, 3183 { 248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255} 3184 }, 3185 { 3186 { 255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3187 { 246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3188 { 252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255} 3189 }, 3190 { 3191 { 255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3192 { 248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3193 { 253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3194 }, 3195 { 3196 { 255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3197 { 245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3198 { 253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3199 }, 3200 { 3201 { 255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3202 { 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3203 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3204 }, 3205 { 3206 { 255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3207 { 249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3208 { 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3210 }, 3211 { 3212 { 255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3213 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3214 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3215 }, 3216 { 3217 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3218 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3219 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3220 } 3221 } 3222 }; 3224 ---- End code block ---------------------------------------- 3226 13.5. Default Token Probability Table 3228 The default token probabilities are as follows. 3230 ---- Begin code block -------------------------------------- 3232 const Prob default_coef_probs [4] [8] [3] [num_dct_tokens - 1] = 3233 { 3234 { 3235 { 3236 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3237 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3238 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3239 }, 3240 { 3241 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 3242 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 3243 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 3244 }, 3245 { 3246 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 3247 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 3248 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 3249 }, 3250 { 3251 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 3252 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 3253 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 3254 }, 3255 { 3256 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 3257 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 3258 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 3259 }, 3260 { 3261 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 3262 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 3263 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 3264 }, 3265 { 3266 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 3267 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 3268 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 3269 }, 3270 { 3271 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3272 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3273 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3274 } 3275 }, 3276 { 3277 { 3278 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 3279 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 3280 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 3281 }, 3282 { 3283 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 3284 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 3285 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 3286 }, 3287 { 3288 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 3289 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 3290 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 3291 }, 3292 { 3293 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 3294 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 3295 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 3296 }, 3297 { 3298 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 3299 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 3300 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 3301 }, 3302 { 3303 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 3304 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 3305 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 3307 }, 3308 { 3309 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 3310 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 3311 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 3312 }, 3313 { 3314 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 3315 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3316 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 3317 } 3318 }, 3319 { 3320 { 3321 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 3322 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 3323 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 3324 }, 3325 { 3326 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 3327 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 3328 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 3329 }, 3330 { 3331 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 3332 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 3333 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 3334 }, 3335 { 3336 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 3337 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 3338 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 3339 }, 3340 { 3341 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3342 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 3343 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3344 }, 3345 { 3346 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3347 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3348 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3349 }, 3350 { 3351 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 3352 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 3353 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3354 }, 3355 { 3356 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3357 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3358 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3359 } 3360 }, 3361 { 3362 { 3363 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 3364 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 3365 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 3366 }, 3367 { 3368 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 3369 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 3370 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 3371 }, 3372 { 3373 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 3374 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 3375 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 3376 }, 3377 { 3378 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 3379 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 3380 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 3381 }, 3382 { 3383 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 3384 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 3385 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 3386 }, 3387 { 3388 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 3389 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 3390 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 3391 }, 3392 { 3393 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 3394 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 3395 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 3396 }, 3397 { 3398 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3399 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3400 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3401 } 3402 } 3404 }; 3406 ---- End code block ---------------------------------------- 3408 14. DCT and WHT Inversion and Macroblock Reconstruction 3410 14.1. Dequantization 3412 After decoding the DCTs/WHTs as described above, each (quantized) 3413 coefficient in each subblock is multiplied by one of six 3414 dequantization factors, the choice of factor depending on the plane 3415 (Y2, Y, or chroma) and position (DC = coefficient zero, AC = any 3416 other coefficient). If the current macroblock has overridden the 3417 quantization level (as described in Chapter 10) then the six factors 3418 are looked up from two dequantization tables with appropriate scaling 3419 and clamping using the single index supplied by the override. 3420 Otherwise, the frame-level dequantization factors (as described in 3421 Section 9.6) are used. In either case, the multiplies are computed 3422 and stored using 16-bit signed integers. 3424 The two dequantization tables, which may also be found in the 3425 reference decoder file quant_common.c, are as follows. 3427 ---- Begin code block -------------------------------------- 3429 static const int dc_qlookup[QINDEX_RANGE] = 3430 { 3431 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 3432 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 3433 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 3434 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 3435 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 3436 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 3437 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 3438 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 3439 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, 126, 128, 130, 3440 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, 3441 }; 3443 static const int ac_qlookup[QINDEX_RANGE] = 3444 { 3445 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 3446 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3447 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 3448 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 3449 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 3450 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 3451 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 3452 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 3453 181, 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, 3454 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, 3455 }; 3457 ---- End code block ---------------------------------------- 3459 Lookup values from the above two tables are directly used the DC and 3460 AC coefficients in Y1 respectively. For Y2 and chroma, values from 3461 above tables undergo either a scaling process or clamping processing 3462 before the multiplies. Details to these scaling and clamping can be 3463 found related lookup functions in quant_common.c. 3465 14.2. Inverse Transforms 3467 If the Y2 residue block exists (i.e., the macroblock luma mode is not 3468 SPLITMV or B_PRED), it is inverted first (using the inverse WHT) and 3469 the element of the result at row i, column j is used as the 0th 3470 coefficient of the Y subblock at position (i, j), that is, the Y 3471 subblock whose index is (i * 4) + j. As discussed in Chapter 13, if 3472 the luma mode is B_PRED or SPLITMV, the 0th Y coefficients are part 3473 of the residue signal for the subblocks themselves. 3475 In either case, the inverse transforms for the sixteen Y subblocks 3476 and eight chroma subblocks are computed next. All 24 of these 3477 inversions are independent of each other; their results may (at least 3478 conceptually) be stored in 24 separate 4x4 arrays. 3480 As is done by the reference decoder, an implementation may wish to 3481 represent the prediction and residue buffers as macroblock-sized 3482 arrays (that is, a 16x16 Y buffer and two 8x8 chroma buffers). 3483 Regarding the inverse DCT implementation given below, this requires a 3484 simple adjustment to the address calculation for the resulting 3485 residue pixels. 3487 14.3. Implementation of the WHT Inversion 3489 As previously discussed (see Chapters 2 and 13), for macroblocks 3490 encoded using prediction modes other than B_PRED and SPLITMV, the DC 3491 values derived from the DCT transform on the 16 Y blocks are 3492 collected to construct a 25th block of a macroblock (16 Y, 4 U, 4 V 3493 constitute the 24 blocks). This 25th block is transformed using a 3494 Walsh-Hadamard transform (WHT). 3496 The inputs to the inverse WHT (that is, the dequantized 3497 coefficients), the intermediate "horizontally detransformed" signal, 3498 and the completely detransformed residue signal are all stored as 3499 arrays of 16-bit signed integers. 3501 Following the tradition of specifying bitstream format using the 3502 decoding process, we specify the inverse WHT in the decoding process 3503 using the following C style source code: 3505 ---- Begin code block -------------------------------------- 3507 void vp8_short_inv_walsh4x4_c(short *input, short *output) 3508 { 3509 int i; 3510 int a1, b1, c1, d1; 3511 int a2, b2, c2, d2; 3512 short *ip = input; 3513 short *op = output; 3514 int temp1, temp2; 3516 for(i=0;i<4;i++) 3517 { 3518 a1 = ip[0] + ip[12]; 3519 b1 = ip[4] + ip[8]; 3520 c1 = ip[4] - ip[8]; 3521 d1 = ip[0] - ip[12]; 3522 op[0] = a1 + b1; 3523 op[4] = c1 + d1; 3524 op[8] = a1 - b1; 3525 op[12]= d1 - c1; 3526 ip++; 3527 op++; 3528 } 3529 ip = output; 3530 op = output; 3531 for(i=0;i<4;i++) 3532 { 3533 a1 = ip[0] + ip[3]; 3534 b1 = ip[1] + ip[2]; 3535 c1 = ip[1] - ip[2]; 3536 d1 = ip[0] - ip[3]; 3538 a2 = a1 + b1; 3539 b2 = c1 + d1; 3540 c2 = a1 - b1; 3541 d2 = d1 - c1; 3543 op[0] = (a2+3)>>3; 3544 op[1] = (b2+3)>>3; 3545 op[2] = (c2+3)>>3; 3546 op[3] = (d2+3)>>3; 3548 ip+=4; 3549 op+=4; 3550 } 3551 } 3553 ---- End code block ---------------------------------------- 3555 In the case that there is only one non-zero DC value in input, the 3556 inverse transform can be simplified to the following: 3558 ---- Begin code block -------------------------------------- 3560 void vp8_short_inv_walsh4x4_1_c(short *input, short *output) 3561 { 3562 int i; 3563 int a1; 3564 short *op=output; 3566 a1 = ((input[0] + 3)>>3); 3568 for(i=0;i<4;i++) 3569 { 3570 op[0] = a1; 3571 op[1] = a1; 3572 op[2] = a1; 3573 op[3] = a1; 3574 op+=4; 3575 } 3576 } 3578 ---- End code block ---------------------------------------- 3580 It should be noted, a conforming decoder should implement the inverse 3581 transform using exactly the same rounding to achieve bit-wise 3582 matching output to the output of the process specified by the above 3583 "C" source code. 3585 The reference decoder WHT inversion may be found in the files 3586 invtrans.c and idctllm.c. 3588 14.4. Implementation of the DCT Inversion 3590 All of the DCT inversions are computed in exactly the same way. In 3591 principle, VP8 uses a classical 2D inverse discrete cosine transform, 3592 implemented as two passes of 1-D inverse DCT. The 1-D inverse DCT 3593 was calculated using a similar algorithm to what was described in 3594 [Loeffler]. However, the paper only provided the 8-point and 16- 3595 point version of the algorithms, which was adapted by On2 to perform 3596 the 4-point 1-D DCT. 3598 Accurate calculation of 1-D DCT of the above algorithm requires 3599 infinite precision. VP8 of course can use only a finite-precision 3600 approximation. Also, the inverse DCT used by VP8 takes care of 3601 normalization of the standard unitary transform, that is, every 3602 dequantized coefficient has roughly double the size of the 3603 corresponding unitary coefficient. However, at all but the highest 3604 datarates, the discrepancy between transmitted and ideal coefficients 3605 is due almost entirely to (lossy) compression and not to errors 3606 induced by finite-precision arithmetic. 3608 The inputs to the inverse DCT (that is, the dequantized 3609 coefficients), the intermediate "horizontally detransformed" signal, 3610 and the completely detransformed residue signal are all stored as 3611 arrays of 16-bit signed integers. The details of the computation are 3612 as follows. 3614 It should also be noted that this implementation makes use of 16-bit 3615 fixed point version of two multiplication constants: 3617 sqrt(2) * cos (pi/8) 3619 sqrt(2) * sin (pi/8) 3621 Because the first constant is bigger than 1, to maintain the same 16- 3622 bit fixed point precision as the second one, we make use of the fact 3623 that 3625 x * a = x + x*(a-1) 3627 therefore 3629 x * sqrt(2) * cos (pi/8) = x + x * ( sqrt(2) * cos(pi/8)-1) 3631 ---- Begin code block -------------------------------------- 3633 /* IDCT implementation */ 3634 static const int cospi8sqrt2minus1=20091; 3635 static const int sinpi8sqrt2 =35468; 3636 void short_idct4x4llm_c(short *input, short *output, int pitch) 3637 { 3638 int i; 3639 int a1, b1, c1, d1; 3641 short *ip=input; 3642 short *op=output; 3643 int temp1, temp2; 3644 int shortpitch = pitch>>1; 3646 for(i=0;i<4;i++) 3647 { 3648 a1 = ip[0]+ip[8]; 3649 b1 = ip[0]-ip[8]; 3651 temp1 = (ip[4] * sinpi8sqrt2)>>16; 3652 temp2 = ip[12]+((ip[12] * cospi8sqrt2minus1)>>16); 3653 c1 = temp1 - temp2; 3655 temp1 = ip[4] + ((ip[4] * cospi8sqrt2minus1)>>16); 3656 temp2 = (ip[12] * sinpi8sqrt2)>>16; 3657 d1 = temp1 + temp2; 3659 op[shortpitch*0] = a1+d1; 3660 op[shortpitch*3] = a1-d1; 3661 op[shortpitch*1] = b1+c1; 3662 op[shortpitch*2] = b1-c1; 3664 ip++; 3665 op++; 3666 } 3667 ip = output; 3668 op = output; 3669 for(i=0;i<4;i++) 3670 { 3671 a1 = ip[0]+ip[2]; 3672 b1 = ip[0]-ip[2]; 3674 temp1 = (ip[1] * sinpi8sqrt2)>>16; 3675 temp2 = ip[3]+((ip[3] * cospi8sqrt2minus1)>>16); 3676 c1 = temp1 - temp2; 3678 temp1 = ip[1] + ((ip[1] * cospi8sqrt2minus1)>>16); 3679 temp2 = (ip[3] * sinpi8sqrt2)>>16; 3680 d1 = temp1 + temp2; 3682 op[0] = (a1+d1+4)>>3; 3683 op[3] = (a1-d1+4)>>3; 3684 op[1] = (b1+c1+4)>>3; 3685 op[2] = (b1-c1+4)>>3; 3687 ip+=shortpitch; 3688 op+=shortpitch; 3689 } 3690 } 3692 ---- End code block ---------------------------------------- 3694 The reference decoder DCT inversion may be found in the files 3695 invtrans.c and idctllm.c. 3697 14.5. Summation of Predictor and Residue 3699 Finally, the prediction and residue signals are summed to form the 3700 reconstructed macroblock, which, except for loop filtering (taken up 3701 next), completes the decoding process. 3703 The summing procedure is fairly straightforward, having only a couple 3704 of details. The prediction and residue buffers are both arrays of 3705 16-bit signed integers. Each individual (Y, U, and V pixel) result 3706 is calculated first as a 32-bit sum of the prediction and residue, 3707 and is then saturated to 8-bit unsigned range (using, say, the 3708 clamp255 function defined above) before being stored as an 8-bit 3709 unsigned pixel value. 3711 VP8 also supports a mode where the encoding of a bitstream guarantees 3712 all reconstructed pixel values between 0 and 255, compliant 3713 bitstreams of such requirements have the clamp_type bit in the frame 3714 header set to 1. In such case, the clamp255 is no longer required. 3716 The summation process is the same, regardless of the (intra or inter) 3717 mode of prediction in effect for the macroblock. The reference 3718 decoder implementation of reconstruction may be found in the file 3719 recon.c. 3721 15. Loop Filter 3723 Loop filtering is the last stage of frame reconstruction and the 3724 next-to-last stage of the decoding process. The loop filter is 3725 applied to the entire frame after the summation of predictor and 3726 residue described in Chapter 14. 3728 The purpose of the loop filter is to eliminate (or at least reduce) 3729 visually objectionable artifacts associated with the semi- 3730 independence of the coding of macroblocks and their constituent 3731 subblocks. 3733 As was discussed in Chapter 5, the loop filter is "integral" to 3734 decoding, in that the results of loop filtering are used in the 3735 prediction of subsequent frames. Consequently, a functional decoder 3736 implementation must perform loop filtering exactly as described here. 3737 This is in distinction to any postprocessing that may be applied only 3738 to the image immediately before display; such postprocessing is 3739 entirely at the option of the implementor (and/or user) and has no 3740 effect on decoding per se. 3742 The baseline frame level parameters controlling the loop filter are 3743 defined in the frame header (Chapter 9.4) along with a mechanism for 3744 adjustment based on a macroblock's prediction mode and/or reference 3745 frame. The first is a flag selecting the type of filter (normal or 3746 simple), the other two are numbers (loop_filter_level and 3747 sharpness_level) that adjust the strength or sensitivity of the 3748 filter. As described in Chapters 9.3 and 10, loop_filter_level may 3749 be also overridden on a per-macroblock basis using segmentation. 3751 Loop filtering is one of the more computationally-intensive aspects 3752 of VP8 decoding. This is the reason for the existence of the 3753 optional less-demanding simple filter type. Also, the loop filter is 3754 completely disabled if the loop_filter_level in the frame header is 3755 zero; macroblock-level overrides are ignored in this case. (It is of 3756 course possible for a compressor to encode a frame in which only a 3757 few macroblocks are loop filtered: The global loop_filter_level must 3758 be non-zero and each macroblock can select one of four levels, most 3759 of which could be zero.) 3761 To facilitate efficient implementation, the VP8 decoding algorithms 3762 generally, and the loop filter especially, were designed with SIMD 3763 ("Single Instruction Multiple Datum" or "integer vector") processors 3764 in mind. The reference decoder implementation of loop filtering 3765 (found in loopfilter.c) is, in effect, a portable SIMD specification 3766 of the loop filtering algorithms intended to simplify a realization 3767 on an actual SIMD processor. 3769 Unfortunately, the approach taken there does not lead to maximal 3770 efficency (restricted to the C language, that is) and, as far as a 3771 pure algorithm specification is concerned, is in places obscure. For 3772 example, various aspects of filtering are conditioned on absolute 3773 differences lying below certain thresholds. An ordinary C 3774 implementation would simply discriminate amongst these behaviors 3775 using if statements. The reference decoder instead effects this by 3776 "masking arithmetic", that is, using "and" operations to 3777 (conditionally) zero-out values to be added or subtracted to pixels. 3778 Furthermore, the structure holding the various threshold values is 3779 artificially parallelized. While this mimics closely the approach 3780 taken in vector-processor machine language, it is not how one usually 3781 programs in C. 3783 In this document, we take a different approach and present the 3784 algorithms in a more straightforward, idiomatic, and terse C style. 3785 Together with the reference version, we hope to provide the "best of 3786 both worlds", that is, a pure algorithm specification here and a 3787 strong suggestion as to an optimal actual implementation in 3788 loopfilter.c. 3790 We begin by discussing the aspects of loop filtering that are 3791 independent of the controlling parameters and type of filter chosen. 3793 15.1. Filter Geometry and Overall Procedure 3795 The Y, U, and V planes are processed independently and, except for 3796 the values of certain control parameters (derived from the 3797 loop_filter_level and sharpness_level), identically. 3799 The loop filter acts on the edges between adjacent macroblocks and on 3800 the edges between adjacent subblocks of a macroblock. All such edges 3801 are horizontal or vertical. For each pixel position on an edge, a 3802 small number (two or three) of pixels adjacent to either side of the 3803 position are examined and possibly modified. The displacements of 3804 these pixels are at a right angle to the edge orientation, that is, 3805 for a horizontal edge, we treat the pixels immediately above and 3806 below the edge position, for a vertical edge, we treat the pixels 3807 immediately to the left and right of the edge. 3809 We call this collection of pixels associated to an edge position a 3810 segment; the length of a segment is 2, 4, 6, or 8. Excepting that 3811 the normal filter uses slightly different algorithms for, and that 3812 either filter may apply different control parameters to, the edges 3813 between macroblocks and those between subblocks, the treatment of 3814 edges is quite uniform: All segments straddling an edge are treated 3815 identically, there is no distinction between the treatment of 3816 horizontal and vertical edges, whether between macroblocks or between 3817 subblocks. 3819 As a consequence, adjacent subblock edges within a macroblock may be 3820 concatenated and processed in their entirety. There is a single 3821 8-pixel long vertical edge horizontally centered in each of the U and 3822 V blocks (the concatenation of upper and lower 4-pixel edges between 3823 chroma subblocks), and three 16-pixel long vertical edges at 3824 horizontal positions 1/4, 1/2, and 3/4 the width of the luma 3825 macroblock, each representing the concatenation of four 4-pixel sub- 3826 edges between pairs of Y subblocks. 3828 The macroblocks comprising the frame are processed in the usual 3829 raster-scan order. Each macroblock is "responsible for" the inter- 3830 macroblock edges immediately above and left of it (but not the edges 3831 below and right of it), as well as the edges between its subblocks. 3833 For each macroblock M, there are four filtering steps, which are, 3834 (almost) in order: 3836 1. If M is not on the leftmost column of macroblocks, filter across 3837 the left (vertical) inter-macroblock edge of M. 3839 2. Filter across the vertical subblock edges within M. 3841 3. If M is not on the topmost row of macroblocks, filter across the 3842 top (horizontal) inter-macroblock edge of M. 3844 4. Filter across the horizontal subblock edges within M. 3846 We write MY, MU, and MV for the planar constituents of M, that is, 3847 the 16x16 luma block, 8x8 U block, and 8x8 V block comprising M. 3849 In step 1, for each of the three blocks MY, MU, and MV, we filter 3850 each of the (16 luma or 8 chroma) segments straddling the column 3851 separating the block from the block immediately to the left of it, 3852 using the inter-macroblock filter and controls associated to the 3853 loop_filter_level and sharpness_level. 3855 In step 4, we filter across the (three luma and one each for U and V) 3856 vertical subblock edges described above, this time using the inter- 3857 subblock filter and controls. 3859 Step 2 and 4 are skipped for macroblocks that satisfy both of the 3860 following two conditions: 3862 1. Macroblock coding mode is neither B_PRED nor SPLTMV; and 3863 2. There is no DCT coefficient coded for the whole macroblock. 3865 For these macroblocks, loop filtering for edges between subblocks 3866 internal to a macroblock is effectively skipped. This skip strategy 3867 significantly reduces VP8 loop-filtering complexity. 3869 Edges between macroblocks and those between subblocks are treated 3870 with different control parameters (and, in the case of the normal 3871 filter, with different algorithms); luma and chroma edges are also 3872 treated with different control parameters. Except for pixel 3873 addressing, there is no distinction between the treatment of vertical 3874 and horizontal edges. Luma edges are always 16 pixels long, chroma 3875 edges are always 8 pixels long, and the segments straddling an edge 3876 are treated identically; this of course facilitates vector 3877 processing. 3879 Because many pixels belong to segments straddling two or more edges, 3880 and so will be filtered more than once, the order in which edges are 3881 processed given above must be respected by any implementation. 3882 Within a single edge, however, the segments straddling that edge are 3883 disjoint and the order in which these segments are processed is 3884 immaterial. 3886 Before taking up the filtering algorithms themselves, we should 3887 emphasize a point already made: Even though the pixel segments 3888 associated to a macroblock are antecedent to the macroblock (that is, 3889 lie within the macroblock or in already-constructed macroblocks), a 3890 macroblock must not be filtered immediately after its 3891 "reconstruction" (described in Chapter 14). Rather, the loop filter 3892 applies after all the macroblocks have been "reconstructed" (i.e., 3893 had their predictor summed with their residue); correct decoding is 3894 predicated on the fact that already-constructed portions of the 3895 current frame referenced via intra-prediction (described in Chapter 3896 12) are not yet filtered. 3898 15.2. Simple Filter 3900 Having described the overall procedure of, and pixels affected by, 3901 the loop filter, we turn our attention to the treatment of individual 3902 segments straddling edges. We begin by describing the simple filter, 3903 which, as the reader might guess, is somewhat simpler than the normal 3904 filter. 3906 Note that the simple filter only applies to luma edges. Chroma edges 3907 are left unfiltered. 3909 Roughly speaking, the idea of loop filtering is, within limits, to 3910 reduce the difference between pixels straddling an edge. Differences 3911 in excess of a threshold (associated to the loop_filter_level) are 3912 assumed to be "natural" and are unmodified; differences below the 3913 threshold are assumed to be artifacts of quantization and the 3914 (partially) separate coding of blocks, and are reduced via the 3915 procedures described below. While the loop_filter_level is in 3916 principle arbitrary, the levels chosen by a VP8 compressor tend to be 3917 correlated to quantization levels. 3919 Most of the filtering arithmetic is done using 8-bit signed operands 3920 (having a range -128 to +127, inclusive), supplemented by 16-bit 3921 temporaries holding results of multiplies. 3923 Sums and other temporaries need to be "clamped" to a valid signed 3924 8-bit range: 3926 ---- Begin code block -------------------------------------- 3928 int8 c( int v) 3929 { 3930 return (int8) (v < -128 ? -128 : (v < 128 ? v : 127)); 3931 } 3933 ---- End code block ---------------------------------------- 3935 Since pixel values themselves are unsigned 8-bit numbers, we need to 3936 convert between signed and unsigned values: 3938 ---- Begin code block -------------------------------------- 3940 /* Convert pixel value (0 <= v <= 255) to an 8-bit signed 3941 number. */ 3942 int8 u2s( Pixel v) { return (int8) (v - 128);} 3944 /* Clamp, then convert signed number back to pixel value. */ 3945 Pixel s2u( int v) { return (Pixel) ( c(v) + 128);} 3947 ---- End code block ---------------------------------------- 3949 Filtering is often predicated on absolute-value thresholds. The 3950 following function is the equivalent of the standard library function 3951 abs, whose prototype is found in the standard header file stdlib.h. 3952 For us, the argument v is always the difference between two pixels 3953 and lies in the range -255 <= v <= +255. 3955 ---- Begin code block -------------------------------------- 3957 int abs( int v) { return v < 0? -v : v;} 3958 ---- End code block ---------------------------------------- 3960 An actual implementation would of course use inline functions or 3961 macros to accomplish these trivial procedures (which are used by both 3962 the normal and simple loop filters). An optimal implementation would 3963 probably express them in machine language, perhaps using SIMD vector 3964 instructions. On many SIMD processors, the saturation accomplished 3965 by the above clamping function is often folded into the arithmetic 3966 instructions themselves, obviating the explicit step taken here. 3968 To simplify the specification of relative pixel positions, we use the 3969 word before to mean "immediately above" (for a vertical segment 3970 straddling a horizontal edge) or "immediately to the left of" (for a 3971 horizontal segment straddling a vertical edge) and the word after to 3972 mean "immediately below" or "immediately to the right of". 3974 Given an edge, a segment, and a limit value, the simple loop filter 3975 computes a value based on the four pixels that straddle the edge (two 3976 either side). If that value is below a supplied limit, then, very 3977 roughly speaking, the two pixel values are brought closer to each 3978 other, "shaving off" something like a quarter of the difference. The 3979 same procedure is used for all segments straddling any type of edge, 3980 regardless of the nature (inter-macroblock, inter-subblock, luma, or 3981 chroma) of the edge; only the limit value depends on the edge-type. 3983 The exact procedure (for a single segment) is as follows; the 3984 subroutine common_adjust is used by both the simple filter presented 3985 here and the normal filters discussed in Section 15.3. 3987 ---- Begin code block -------------------------------------- 3989 int8 common_adjust( 3990 int use_outer_taps, /* filter is 2 or 4 taps wide */ 3991 const Pixel *P1, /* pixel before P0 */ 3992 Pixel *P0, /* pixel before edge */ 3993 Pixel *Q0, /* pixel after edge */ 3994 const Pixel *Q1 /* pixel after Q0 */ 3995 ) { 3996 cint8 p1 = u2s( *P1); /* retrieve and convert all 4 pixels */ 3997 cint8 p0 = u2s( *P0); 3998 cint8 q0 = u2s( *Q0); 3999 cint8 q1 = u2s( *Q1); 4001 /* Disregarding clamping, when "use_outer_taps" is false, 4002 "a" is 3*(q0-p0). Since we are about to divide "a" by 4003 8, in this case we end up multiplying the edge 4004 difference by 5/8. 4006 When "use_outer_taps" is true (as for the simple filter), 4007 "a" is p1 - 3*p0 + 3*q0 - q1, which can be thought of as 4008 a refinement of 2*(q0 - p0) and the adjustment is 4009 something like (q0 - p0)/4. */ 4011 int8 a = c( ( use_outer_taps? c(p1 - q1) : 0 ) + 3*(q0 - p0) ); 4013 /* b is used to balance the rounding of a/8 in the case where 4014 the "fractional" part "f" of a/8 is exactly 1/2. */ 4016 cint8 b = (c(a + 3)) >> 3; 4018 /* Divide a by 8, rounding up when f >= 1/2. 4019 Although not strictly part of the "C" language, 4020 the right-shift is assumed to propagate the sign bit. */ 4022 a = c( a + 4) >> 3; 4024 /* Subtract "a" from q0, "bringing it closer" to p0. */ 4026 *Q0 = s2u( q0 - a); 4028 /* Add "a" (with adjustment "b") to p0, "bringing it closer" 4029 to q0. 4031 The clamp of "a+b", while present in the reference decoder, 4032 is superfluous; we have -16 <= a <= 15 at this point. */ 4034 *P0 = s2u( p0 + b); 4036 return a; 4037 } 4039 ---- End code block ---------------------------------------- 4040 ---- Begin code block -------------------------------------- 4042 void simple_segment( 4043 uint8 edge_limit, /* do nothing if edge difference 4044 exceeds limit */ 4045 const Pixel *P1, /* pixel before P0 */ 4046 Pixel *P0, /* pixel before edge */ 4047 Pixel *Q0, /* pixel after edge */ 4048 const Pixel *Q1 /* pixel after Q0 */ 4049 ) { 4050 if( (abs(*P0 - *Q0)*2 + abs(*P1-*Q1)/2) <= edge_limit)) 4051 common_adjust( 1, P1, P0, Q0, Q1); /* use outer taps */ 4052 } 4054 ---- End code block ---------------------------------------- 4056 We make a couple of remarks about the rounding procedure above. When 4057 b is zero (that is, when the "fractional part" of a is not 1/2 ), we 4058 are (except for clamping) adding the same number to p0 as we are 4059 subtracting from q0. This preserves the average value of p0 and q0 4060 but the resulting difference between p0 and q0 is always even; in 4061 particular, the smallest non-zero gradation +-1 is not possible here. 4063 When b is one, the value we add to p0 (again except for clamping) is 4064 one less than the value we are subtracting from q0. In this case, 4065 the resulting difference is always odd (and the small gradation +-1 4066 is possible) but the average value is reduced by 1/2, yielding, for 4067 instance, a very slight darkening in the luma plane. (In the very 4068 unlikely event of appreciable darkening after a large number of 4069 interframes, a compressor would of course eventually compensate for 4070 this in the selection of predictor and/or residue.) 4072 The derivation of the edge_limit value used above, which depends on 4073 the loop_filter_level and sharpness_level, as well as the type of 4074 edge being processed, will be taken up after we describe the normal 4075 loop filtering algorithm below. 4077 15.3. Normal Filter 4079 The normal loop filter is a refinement of the simple loop filter; all 4080 of the general discussion above applies here as well. In particular, 4081 the functions c, u2s, s2u, abs, and common_adjust are used by both 4082 the normal and simple filters. 4084 As mentioned above, the normal algorithms for inter-macroblock and 4085 inter-subblock edges differ. Nonetheless, they have a great deal in 4086 common: They use similar threshold algorithms to disable the filter 4087 and to detect high internal edge variance (which influences the 4088 filtering algorithm). Both algorithms also use, at least 4089 conditionally, the simple filter adjustment procedure described 4090 above. 4092 The common thresholding algorithms are as follows. 4094 ---- Begin code block -------------------------------------- 4096 /* All functions take (among other things) a segment (of length 4097 at most 4 + 4 = 8) symmetrically straddling an edge. 4099 The pixel values (or pointers) are always given in order, 4100 from the "beforemost" to the "aftermost". So, for a 4101 horizontal edge (written "|"), an 8-pixel segment would be 4102 ordered p3 p2 p1 p0 | q0 q1 q2 q3. */ 4104 /* Filtering is disabled if the difference between any two 4105 adjacent "interior" pixels in the 8-pixel segment exceeds 4106 the relevant threshold (I). A more complex thresholding 4107 calculation is done for the group of four pixels that 4108 straddle the edge, in line with the calculation in 4109 simple_segment() above. */ 4111 int filter_yes( 4112 uint8 I, /* limit on interior differences */ 4113 uint8 E, /* limit at the edge */ 4115 cint8 p3, cint8 p2, cint8 p1, cint8 p0, /* pixels before 4116 edge */ 4117 cint8 q0, cint8 q1, cint8 q2, cint8 q3 /* pixels after 4118 edge */ 4119 ) { 4120 return (abs(p0 - q0)*2 + abs(p1-q1)/2) <= E 4121 && abs(p3 - p2) <= I && abs(p2 - p1) <= I && 4122 abs(p1 - p0) <= I 4123 && abs(q3 - q2) <= I && abs(q2 - q1) <= I && 4124 abs(q1 - q0) <= I; 4125 } 4127 ---- End code block ---------------------------------------- 4128 ---- Begin code block -------------------------------------- 4130 /* Filtering is altered if (at least) one of the differences 4131 on either side of the edge exceeds a threshold (we have 4132 "high edge variance"). */ 4134 int hev( 4135 uint8 threshold, 4136 cint8 p1, cint8 p0, /* pixels before edge */ 4137 cint8 q0, cint8 q1 /* pixels after edge */ 4138 ) { 4139 return abs(p1 - p0) > threshold || abs(q1 - q0) > threshold; 4140 } 4142 ---- End code block ---------------------------------------- 4144 The subblock filter is a variant of the simple filter. In fact, if 4145 we have high edge variance, the adjustment is exactly as for the 4146 simple filter. Otherwise, the simple adjustment (without outer taps) 4147 is applied and the two pixels one step in from the edge pixels are 4148 adjusted by roughly half the amount by which the two edge pixels are 4149 adjusted; since the edge adjustment here is essentially 3/8 the edge 4150 difference, the inner adjustment is approximately 3/16 the edge 4151 difference. 4153 ---- Begin code block -------------------------------------- 4155 void subblock_filter( 4156 uint8 hev_threshold, /* detect high edge variance */ 4157 uint8 interior_limit, /* possibly disable filter */ 4158 uint8 edge_limit, 4159 cint8 *P3, cint8 *P2, int8 *P1, int8 *P0, /* pixels before 4160 edge */ 4161 int8 *Q0, int8 *Q1, cint8 *Q2, cint8 *Q3 /* pixels after 4162 edge */ 4163 ) { 4164 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4165 p0 = u2s(*P0); 4166 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4167 q3 = u2s(*Q3); 4169 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4170 p0, p1, p2, p3)) 4171 { 4172 const int hv = hev( hev_threshold, p1, p0, q0, q1); 4174 cint8 a = ( common_adjust( hv, P1, P0, Q0, Q1) + 1) >> 1; 4176 if( !hv) { 4177 *Q1 = s2u( q1 - a); 4178 *P1 = s2u( p1 + a); 4179 } 4180 } 4181 } 4183 ---- End code block ---------------------------------------- 4185 The inter-macroblock filter has potentially wider scope. If the edge 4186 variance is high, it performs the simple adjustment (using the outer 4187 taps, just like the simple filter and the corresponding case of the 4188 normal subblock filter). If the edge variance is low, we begin with 4189 the same basic filter calculation and apply multiples of it to pixel 4190 pairs symmetric about the edge; the magnitude of adjustment decays as 4191 we move away from the edge and six of the pixels in the segment are 4192 affected. 4194 ---- Begin code block -------------------------------------- 4196 void MBfilter( 4197 uint8 hev_threshold, /* detect high edge variance */ 4198 uint8 interior_limit, /* possibly disable filter */ 4199 uint8 edge_limit, 4200 cint8 *P3, int8 *P2, int8 *P1, int8 *P0, /* pixels before 4201 edge */ 4202 int8 *Q0, int8 *Q1, int8 *Q2, cint8 *Q3 /* pixels after 4203 edge */ 4204 ) { 4205 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4206 p0 = u2s(*P0); 4207 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4208 q3 = u2s(*Q3); 4210 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4211 p0, p1, p2, p3)) 4212 { 4213 if( !hev( hev_threshold, p1, p0, q0, q1)) 4214 { 4215 /* Same as the initial calculation in "common_adjust", 4216 w is something like twice the edge difference */ 4218 const int8 w = c( c(p1 - q1) + 3*(q0 - p0) ); 4220 /* 9/64 is approximately 9/63 = 1/7 and 1<<7 = 128 = 4221 2*64. So this a, used to adjust the pixels adjacent 4222 to the edge, is something like 3/7 the edge 4223 difference. */ 4225 int8 a = c( (27*w + 63) >> 7); 4227 *Q0 = s2u( q0 - a); *P0 = s2u( p0 + a); 4229 /* Next two are adjusted by 2/7 the edge difference */ 4231 a = c( (18*w + 63) >> 7); 4233 *Q1 = s2u( q1 - a); *P1 = s2u( p1 + a); 4235 /* Last two are adjusted by 1/7 the edge difference */ 4237 a = c( (9*w + 63) >> 7); 4239 *Q2 = s2u( q2 - a); *P2 = s2u( p2 + a); 4241 } else /* if hev, do simple filter */ 4242 common_adjust( 1, P1, P0, Q0, Q1); /* using outer 4243 taps */ 4244 } 4246 } 4248 ---- End code block ---------------------------------------- 4250 15.4. Calculation of Control Parameters 4252 We conclude the discussion of loop filtering by showing how the 4253 thresholds supplied to the procedures above are derived from the two 4254 control parameters sharpness_level (an unsigned 3-bit number having 4255 maximum value 7) and loop_filter_level (an unsigned 6-bit number 4256 having maximum value 63). 4258 While the sharpness_level is constant over the frame, individual 4259 macroblocks may override the loop_filter_level with one of four 4260 possibilities supplied in the frame header (as described in Chapter 4261 10). 4263 Both the simple and normal filters disable filtering if a value 4264 derived from the four pixels that straddle the edge (2 either side) 4265 exceeds a threshold / limit value. 4267 ---- Begin code block -------------------------------------- 4269 /* Luma and Chroma use the same inter-macroblock edge limit */ 4270 uint8 mbedge_limit = ((loop_filter_level + 2) * 2) + 4271 interior_limit; 4273 /* Luma and Chroma use the same inter-subblock edge limit */ 4274 uint8 sub_bedge_limit = (loop_filter_level * 2) + interior_limit; 4276 ---- End code block ---------------------------------------- 4278 The remaining thresholds are used only by the normal filters. The 4279 filter-disabling interior difference limit is the same for all edges 4280 (luma, chroma, inter-subblock, inter-macroblock) and is given by the 4281 following. 4283 ---- Begin code block -------------------------------------- 4285 uint8 interior_limit = loop_filter_level; 4287 if( sharpness_level) 4288 { 4289 interior_limit >>= sharpness_level > 4 ? 2 : 1; 4290 if( interior_limit > 9 - sharpness_level) 4291 interior_limit = 9 - sharpness_level; 4292 } 4293 if( !interior_limit) 4294 interior_limit = 1; 4296 ---- End code block ---------------------------------------- 4297 Finally, we give the derivation of the high edge-variance threshold, 4298 which is also the same for all edge types. 4300 ---- Begin code block -------------------------------------- 4302 uint8 hev_threshold = 0; 4304 if( we_are_decoding_akey_frame) /* current frame is a key frame */ 4305 { 4306 if( loop_filter_level >= 40) 4307 hev_threshold = 2; 4308 else if( loop_filter_level >= 15) 4309 hev_threshold = 1; 4310 } 4311 else /* current frame is an interframe */ 4312 { 4313 if( loop_filter_level >= 40) 4314 hev_threshold = 3; 4315 else if( loop_filter_level >= 20) 4316 hev_threshold = 2; 4317 else if( loop_filter_level >= 15) 4318 hev_threshold = 1; 4319 } 4321 ---- End code block ---------------------------------------- 4323 16. Interframe Macroblock Prediction Records 4325 We describe the layout and semantics of the prediction records for 4326 macroblocks in an interframe. 4328 After the feature specification (which is described in Chapter 10 and 4329 is identical for intraframes and interframes), there comes a Bool( 4330 prob_intra), which indicates inter-prediction (i.e., prediction from 4331 prior frames) when true and intra-prediction (i.e., prediction from 4332 already-coded portions of the current frame) when false. The zero- 4333 probability prob_intra is set by field J of the frame header. 4335 16.1. Intra-Predicted Macroblocks 4337 For intra-prediction, the layout of the prediction data is 4338 essentially the same as the layout for key frames, although the 4339 contexts used by the decoding process are slightly different. 4341 As discussed in Chapter 8, the "outer" Y mode here uses a different 4342 tree from that used in key frames, repeated here for convenience. 4344 ---- Begin code block -------------------------------------- 4346 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 4347 { 4348 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 4349 4, 6, /* "1" subtree has 2 descendant subtrees */ 4350 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 4351 H_PRED = "101" */ 4352 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 4353 B_PRED = "111" */ 4354 }; 4356 ---- End code block ---------------------------------------- 4358 The probability table used to decode this tree is variable. As 4359 described in Section 9, it (along with the similarly-treated UV 4360 table) can be updated by field J of the frame header. Similar to the 4361 coefficient-decoding probabilities, such updates are cumulative and 4362 affect all ensuing frames until the next key frame or explicit 4363 update. The default probabilities for the Y and UV tables are 4365 ---- Begin code block -------------------------------------- 4367 Prob ymode_prob [num_ymodes - 1] = { 112, 86, 140, 37}; 4368 Prob uv_mode_prob [num_uv_modes - 1] = { 162, 101, 204}; 4370 ---- End code block ---------------------------------------- 4371 These defaults must be restored after detection of a key frame. 4373 Just as for key frames, if the Y mode is B_PRED, there next comes an 4374 encoding of the intra_bpred mode used by each of the sixteen Y 4375 subblocks. These encodings use the same tree as does that for key 4376 frames but, in place of the contexts used in key frames, use the 4377 single fixed probability table 4379 ---- Begin code block -------------------------------------- 4381 const Prob bmode_prob [num_intra_bmodes - 1] = { 4382 120, 90, 79, 133, 87, 85, 80, 111, 151 4383 }; 4385 ---- End code block ---------------------------------------- 4387 Last comes the chroma mode, again coded using the same tree as that 4388 for key frames, this time using the dynamic uv_mode_prob table 4389 described above. 4391 The calculation of the intra-prediction buffer is identical to that 4392 described for key frames in Chapter 12. 4394 16.2. Inter-Predicted Macroblocks 4396 Otherwise (when the above bool is true), we are using inter- 4397 prediction (which of course only happens for interframes), to which 4398 we now restrict our attention. 4400 The next datum is then another bool, B( prob_last), selecting the 4401 reference frame. If 0, the reference frame is previous frame (last 4402 frame); if 1, another bool (prob_gf) selects the reference frame 4403 between golden frame (0) or altref frame (1). The probabilities 4404 prob_last and prob_gf are set in field J of the frame header. 4406 Together with setting the reference frame, the purpose of inter-mode 4407 decoding is to set a motion vector for each of the sixteen Y 4408 subblocks of the current macroblock. This then defines the 4409 calculation of the inter-prediction buffer (detailed in Chapter 18). 4410 While the net effect of inter-mode decoding is straightforward, the 4411 implementation is somewhat complex; the (lossless) compression 4412 achieved by this method justifies the complexity. 4414 After the reference frame selector comes the mode (or motion vector 4415 reference) applied to the macroblock as a whole, coded using the 4416 following enumeration and tree. Setting mv_nearest = num_ymodes is a 4417 convenience that allows a single variable to unambiguously hold an 4418 inter- or intraprediction mode. 4420 ---- Begin code block -------------------------------------- 4422 typedef enum 4423 { 4424 mv_nearest = num_ymodes, /* use "nearest" motion vector 4425 for entire MB */ 4426 mv_near, /* use "next nearest" "" */ 4427 mv_zero, /* use zero "" */ 4428 mv_new, /* use explicit offset from 4429 implicit "" */ 4430 mv_split, /* use multiple motion vectors */ 4432 num_mv_refs = mv_split + 1 - mv_nearest 4433 } 4434 mv_ref; 4436 const tree_index mv_ref_tree [2 * (num_mv_refs - 1)] = 4437 { 4438 -mv_zero, 2, /* zero = "0" */ 4439 -mv_nearest, 4, /* nearest = "10" */ 4440 -mv_near, 6, /* near = "110" */ 4441 -mv_new, -mv_split /* new = "1110", split = "1111" */ 4442 }; 4444 ---- End code block ---------------------------------------- 4446 16.3. Mode and Motion Vector Contexts 4448 The probability table used to decode the mv_ref, along with three 4449 reference motion vectors used by the selected mode, is calculated via 4450 a survey of the already-decoded motion vectors in (up to) 3 nearby 4451 macroblocks. 4453 The algorithm generates a sorted list of distinct motion vectors 4454 adjacent to the search site. The best_mv is the vector with the 4455 highest score. The nearest_mv is the non-zero vector with the 4456 highest score. The near_mv is the non-zero vector with the next 4457 highest score. The number of motion vectors coded using the SPLITMV 4458 mode is scored using the same weighting and is returned with the 4459 scores of the best, nearest, and near vectors. 4461 The three adjacent macroblocks above, left, and above-left are 4462 considered in order. If the macroblock is intra-coded, no action is 4463 taken. Otherwise, the motion vector is compared to other previously 4464 found motion vectors to determine if it has been seen before, and if 4465 so contributes its weight to that vector, otherwise enters a new 4466 vector in the list. The above and left vectors have twice the weight 4467 of the above-left vector. 4469 As is the case with many contexts used by VP8, it is possible for 4470 macroblocks near the top or left edges of the image to reference 4471 blocks that are outside the visible image. VP8 provides a border of 4472 1 macroblock filled with 0x0 motion vectors left of the left edge, 4473 and a border filled with 0,0 motion vectors of 1 macroblocks above 4474 the top edge. 4476 Much of the process is more easily described in C than in English. 4477 The reference code for this can be found in findnearmv.c. The 4478 calculation of reference vectors, probability table, and, finally, 4479 the inter-prediction mode itself is implemented as follows. 4481 ---- Begin code block -------------------------------------- 4483 typedef union 4484 { 4485 unsigned int as_int; 4486 MV as_mv; 4487 } int_mv; /* facilitates rapid equality tests */ 4489 static void mv_bias(MODE_INFO *x,int refframe, int_mv *mvp, 4490 int * ref_frame_sign_bias ) 4491 { 4492 MV xmv; 4493 xmv = x->mbmi.mv.as_mv; 4494 if ( ref_frame_sign_bias[x->mbmi.ref_frame] != 4495 ref_frame_sign_bias[refframe] ) 4496 { 4497 xmv.row*=-1; 4498 xmv.col*=-1; 4499 } 4500 mvp->as_mv = xmv; 4501 } 4503 ---- End code block ---------------------------------------- 4504 ---- Begin code block -------------------------------------- 4506 void vp8_clamp_mv(MV *mv, const MACROBLOCKD *xd) 4507 { 4508 if ( mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN) ) 4509 mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN; 4510 else if ( mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN ) 4511 mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN; 4513 if ( mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN) ) 4514 mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN; 4515 else if ( mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN ) 4516 mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN; 4517 } 4519 ---- End code block ---------------------------------------- 4521 In the function vp8_find_near_mvs(), the vectors "nearest" and "near" 4522 are used by the corresponding modes. 4524 The vector best_mv is used as a base for explicitly-coded motion 4525 vectors. 4527 The first three entries in the return value cnt are (in order) 4528 weighted census values for "zero", "nearest", and "near" vectors. 4529 The final value indicates the extent to which SPLIT_MV was used by 4530 the neighboring macroblocks. The largest possible "weight" value in 4531 each case is 5. 4533 ---- Begin code block -------------------------------------- 4535 void vp8_find_near_mvs 4536 ( 4537 MACROBLOCKD *xd, 4538 const MODE_INFO *here, 4539 MV *nearest, 4540 MV *near, 4541 MV *best_mv, 4542 int cnt[4], 4543 int refframe, 4544 int * ref_frame_sign_bias 4545 ) 4546 { 4547 const MODE_INFO *above = here - xd->mode_info_stride; 4548 const MODE_INFO *left = here - 1; 4549 const MODE_INFO *aboveleft = above - 1; 4550 int_mv near_mvs[4]; 4551 int_mv *mv = near_mvs; 4552 int *cntx = cnt; 4553 enum {CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV}; 4555 /* Zero accumulators */ 4556 mv[0].as_int = mv[1].as_int = mv[2].as_int = 0; 4557 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 4559 /* Process above */ 4560 if(above->mbmi.ref_frame != INTRA_FRAME) { 4561 if(above->mbmi.mv.as_int) { 4562 (++mv)->as_int = above->mbmi.mv.as_int; 4563 mv_bias(above, refframe, mv, ref_frame_sign_bias); 4564 ++cntx; 4565 } 4566 *cntx += 2; 4567 } 4569 /* Process left */ 4570 if(left->mbmi.ref_frame != INTRA_FRAME) { 4571 if(left->mbmi.mv.as_int) { 4572 int_mv this_mv; 4574 this_mv.as_int = left->mbmi.mv.as_int; 4575 mv_bias(left, refframe, &this_mv, ref_frame_sign_bias); 4577 if(this_mv.as_int != mv->as_int) { 4578 (++mv)->as_int = this_mv.as_int; 4579 ++cntx; 4580 } 4581 *cntx += 2; 4582 } else 4583 cnt[CNT_ZERO] += 2; 4584 } 4586 /* Process above left */ 4587 if(aboveleft->mbmi.ref_frame != INTRA_FRAME) { 4588 if(aboveleft->mbmi.mv.as_int) { 4589 int_mv this_mv; 4591 this_mv.as_int = aboveleft->mbmi.mv.as_int; 4592 mv_bias(aboveleft, refframe, &this_mv, 4593 ref_frame_sign_bias); 4595 if(this_mv.as_int != mv->as_int) { 4596 (++mv)->as_int = this_mv.as_int; 4597 ++cntx; 4598 } 4599 *cntx += 1; 4601 } else 4602 cnt[CNT_ZERO] += 1; 4603 } 4605 /* If we have three distinct MV's ... */ 4606 if(cnt[CNT_SPLITMV]) { 4607 /* See if above-left MV can be merged with NEAREST */ 4608 if(mv->as_int == near_mvs[CNT_NEAREST].as_int) 4609 cnt[CNT_NEAREST] += 1; 4610 } 4612 cnt[CNT_SPLITMV] = ((above->mbmi.mode == SPLITMV) 4613 + (left->mbmi.mode == SPLITMV)) * 2 4614 + (aboveleft->mbmi.mode == SPLITMV); 4616 /* Swap near and nearest if necessary */ 4617 if(cnt[CNT_NEAR] > cnt[CNT_NEAREST]) { 4618 int tmp; 4619 tmp = cnt[CNT_NEAREST]; 4620 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 4621 cnt[CNT_NEAR] = tmp; 4622 tmp = near_mvs[CNT_NEAREST].as_int; 4623 near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int; 4624 near_mvs[CNT_NEAR].as_int = tmp; 4625 } 4627 /* Use near_mvs[0] to store the "best" MV */ 4628 if(cnt[CNT_NEAREST] >= cnt[CNT_ZERO]) 4629 near_mvs[CNT_ZERO] = near_mvs[CNT_NEAREST]; 4631 /* Set up return values */ 4632 *best_mv = near_mvs[0].as_mv; 4633 *nearest = near_mvs[CNT_NEAREST].as_mv; 4634 *near = near_mvs[CNT_NEAR].as_mv; 4636 vp8_clamp_mv(nearest, xd); 4637 vp8_clamp_mv(near, xd); 4638 vp8_clamp_mv(best_mv, xd); //TODO: move this up before 4639 the copy 4640 } 4642 ---- End code block ---------------------------------------- 4644 The mv_ref probability table (mv_ref_p) is then derived from the 4645 census as follows. 4647 ---- Begin code block -------------------------------------- 4649 const int vp8_mode_contexts[6][4] = 4650 { 4651 { 7, 1, 1, 143, }, 4652 { 14, 18, 14, 107, }, 4653 { 135, 64, 57, 68, }, 4654 { 60, 56, 128, 65, }, 4655 { 159, 134, 128, 34, }, 4656 { 234, 188, 128, 28, }, 4657 } 4659 ---- End code block ---------------------------------------- 4661 ---- Begin code block -------------------------------------- 4663 vp8_prob *vp8_mv_ref_probs(vp8_prob mv_ref_p[VP8_MVREFS-1], 4664 int cnt[4]) 4665 { 4666 mv_ref_p[0] = vp8_mode_contexts [cnt[0]] [0]; 4667 mv_ref_p[1] = vp8_mode_contexts [cnt[1]] [1]; 4668 mv_ref_p[2] = vp8_mode_contexts [cnt[2]] [2]; 4669 mv_ref_p[3] = vp8_mode_contexts [cnt[3]] [3]; 4670 return p; 4671 } 4673 ---- End code block ---------------------------------------- 4675 Once mv_ref_p is established, the mv_ref is decoded as usual. 4677 ---- Begin code block -------------------------------------- 4679 mvr = (mv_ref) treed_read( d, mv_ref_tree, mv_ref_p); 4681 ---- End code block ---------------------------------------- 4683 For the first four inter-coding modes, the same motion vector is used 4684 for all the Y subblocks. The first three modes use an implicit 4685 motion vector. 4687 +------------+------------------------------------------------------+ 4688 | Mode | Instruction | 4689 +------------+------------------------------------------------------+ 4690 | mv_nearest | Use the nearest vector returned by | 4691 | | vp8_find_near_mvs. | 4692 | | | 4693 | mv_near | Use the near vector returned by vp8_find_near_mvs. | 4694 | | | 4695 | mv_zero | Use a zero vector, that is, predict the current | 4696 | | macroblock from the corresponding macroblock in the | 4697 | | prediction frame. | 4698 | | | 4699 | NEWMV | This mode is followed by an explicitly-coded motion | 4700 | | vector (the format of which is described in the next | 4701 | | chapter) that is added (component-wise) to the | 4702 | | best_mv reference vector returned by find_near_mvs | 4703 | | and applied to all 16 subblocks. | 4704 +------------+------------------------------------------------------+ 4706 16.4. Split Prediction 4708 The remaining mode (SPLITMV) causes multiple vectors to be applied to 4709 the Y subblocks. It is immediately followed by a partition 4710 specification that determines how many vectors will be specified and 4711 how they will be assigned to the subblocks. The possible partitions, 4712 with indicated subdivisions and coding tree, are as follows. 4714 ---- Begin code block -------------------------------------- 4716 typedef enum 4717 { 4718 mv_top_bottom, /* two pieces {0...7} and {8...15} */ 4719 mv_left_right, /* {0,1,4,5,8,9,12,13} and 4720 {2,3,6,7,10,11,14,15} */ 4721 mv_quarters, /* {0,1,4,5}, {2,3,6,7}, {8,9,12,13}, 4722 {10,11,14,15} */ 4723 MV_16, /* every subblock gets its own vector 4724 {0} ... {15} */ 4726 mv_num_partitions 4727 } 4728 MVpartition; 4730 const tree_index mvpartition_tree [2 * (mvnum_partition - 1)] = 4731 { 4732 -MV_16, 2, /* MV_16 = "0" */ 4733 -mv_quarters, 4, /* mv_quarters = "10" */ 4734 -mv_top_bottom, -mv_left_right /* top_bottom = "110", 4735 left_right = "111" */ 4736 }; 4738 ---- End code block ---------------------------------------- 4740 The partition is decoded using a fixed, constant probability table: 4742 ---- Begin code block -------------------------------------- 4744 const Prob mvpartition_probs [mvnum_partition - 1] = 4745 { 110, 111, 150}; 4746 part = (MVpartition) treed_read( d, mvpartition_tree, 4747 mvpartition_probs); 4749 ---- End code block ---------------------------------------- 4751 After the partition come two (for mv_top_bottom or mv_left_right), 4752 four (for mv_quarters), or sixteen (for MV_16) subblock inter- 4753 prediction modes. These modes occur in the order indicated by the 4754 partition layouts (given as comments to the MVpartition enum) and are 4755 coded as follows. (As was done for the macroblock-level modes, we 4756 offset the mode enumeration so that a single variable may 4757 unambiguously hold either an intra- or inter-subblock mode.) 4759 Prior to decoding each subblock, a decoding tree context is chosen as 4760 illustrated in the code snippet below. The context is based on the 4761 immediate left and above subblock neighbors, and whether they are 4762 equal, are zero, or a combination of those. 4764 ---- Begin code block -------------------------------------- 4766 typedef enum 4767 { 4768 LEFT4x4 = num_intra_bmodes, /* use already-coded MV to 4769 my left */ 4770 ABOVE4x4, /* use already-coded MV above me */ 4771 ZERO4x4, /* use zero MV */ 4772 NEW4x4, /* explicit offset from "best" */ 4774 num_sub_mv_ref 4775 }; 4776 sub_mv_ref; 4778 const tree_index sub_mv_ref_tree [2 * (num_sub_mv_ref - 1)] = 4779 { 4780 -LEFT4X4, 2, /* LEFT = "0" */ 4781 -ABOVE4X4, 4, /* ABOVE = "10" */ 4782 -ZERO4X4, -NEW4X4 /* ZERO = "110", NEW = "111" */ 4783 }; 4785 /* Choose correct decoding tree context 4786 * Function parameters are left subblock neighbor MV and above 4787 * subblock neighbor MV */ 4788 int vp8_mvCont(MV *l, MV*a) 4789 { 4790 int lez = (l->row == 0 && l->col == 0); /* left neighbour 4791 is zero */ 4792 int aez = (a->row == 0 && a->col == 0); /* above neighbour 4793 is zero */ 4794 int lea = (l->row == a->row && l->col == a->col); /* left 4795 neighbour equals above neighbour */ 4797 if(lea && lez) 4798 return SUBMVREF_LEFT_ABOVE_ZED; /* =4 */ 4800 if(lea) 4801 return SUBMVREF_LEFT_ABOVE_SAME; /* =3 */ 4803 if(aez) 4804 return SUBMVREF_ABOVE_ZED; /* =2 */ 4806 if(lez) 4807 return SUBMVREF_LEFT_ZED; /* =1*/ 4809 return SUBMVREF_NORMAL; /* =0 */ 4811 } 4813 /* Constant probabilities and decoding procedure. */ 4815 const Prob sub_mv_ref_prob [5][num_sub_mv_ref - 1] = { 4816 { 147,136,18 }, 4817 { 106,145,1 }, 4818 { 179,121,1 }, 4819 { 223,1 ,34 }, 4820 { 208,1 ,1 } }; 4822 sub_ref = (sub_mv_ref) treed_read( d, sub_mv_ref_tree, 4823 sub_mv_ref_prob[context]); 4825 ---- End code block ---------------------------------------- 4827 The first two sub-prediction modes simply copy the already-coded 4828 motion vectors used by the blocks above and to-the-left of the 4829 subblock at the upper left corner of the current subset (i.e., 4830 collection of subblocks being predicted). These prediction blocks 4831 need not lie in the current macroblock and, if the current subset 4832 lies at the top or left edges of the frame, need not lie in the 4833 frame. In this latter case, their motion vectors are taken to be 4834 zero, as are subblock motion vectors within an intra-predicted 4835 macroblock. Also, to ensure the correctness of prediction within 4836 this macroblock, all subblocks lying in an already-decoded subset of 4837 the current macroblock must have their motion vectors set. 4839 ZERO4x4 uses a zero motion vector and predicts the current subset 4840 using the corresponding subset from the prediction frame. 4842 NEW4x4 is exactly like NEWMV except applied only to the current 4843 subset. It is followed by a 2-dimensional motion vector offset 4844 (described in the next chapter) that is added to the best vector 4845 returned by the earlier call to find_near_mvs to form the motion 4846 vector in effect for the subset. 4848 Parsing of both inter-prediction modes and motion vectors (described 4849 next) can be found in the reference decoder file decodemv.c. 4851 17. Motion Vector Decoding 4853 As discussed above, motion vectors appear in two places in the VP8 4854 datastream: applied to whole macroblocks in NEWMV mode and applied to 4855 subsets of macroblocks in NEW4x4 mode. The format of the vectors is 4856 identical in both cases. 4858 Each vector has two pieces: A vertical component (row) followed by a 4859 horizontal component (column). The row and column use separate 4860 coding probabilities but are otherwise represented identically. 4862 17.1. Coding of Each Component 4864 Each component is a signed integer V representing a vertical or 4865 horizontal luma displacement of V quarter-pixels (and a chroma 4866 displacement of V eighth-pixels). The absolute value of V, if non- 4867 zero, is followed by a boolean sign. V may take any value between 4868 -1023 and +1023, inclusive. 4870 The absolute value A is coded in one of two different ways according 4871 to its size. For 0 <= A <= 7, A is tree-coded, and for 8 <= A <= 4872 1023, the bits in the binary expansion of A are coded using 4873 independent boolean probabilities. The coding of A begins with a 4874 bool specifying which range is in effect. 4876 Decoding a motion vector component then requires a 19-position 4877 probability table, whose offsets, along with the procedure used to 4878 decode components, are as follows: 4880 ---- Begin code block -------------------------------------- 4882 typedef enum 4883 { 4884 mvpis_short, /* short (<= 7) vs long (>= 8) */ 4885 MVPsign, /* sign for non-zero */ 4886 MVPshort, /* 8 short values = 7-position tree */ 4888 MVPbits = MVPshort + 7, /* 8 long value bits 4889 w/independent probs */ 4891 MVPcount = MVPbits + 10 /* 19 probabilities in total */ 4892 } 4893 MVPindices; 4895 typedef Prob MV_CONTEXT [MVPcount]; /* Decoding spec for 4896 a single component */ 4898 /* Tree used for small absolute values (has expected 4899 correspondence). */ 4901 const tree_index small_mvtree [2 * (8 - 1)] = 4902 { 4903 2, 8, /* "0" subtree, "1" subtree */ 4904 4, 6, /* "00" subtree", "01" subtree */ 4905 -0, -1, /* 0 = "000", 1 = "001" */ 4906 -2, -3, /* 2 = "010", 3 = "011" */ 4907 10, 12, /* "10" subtree, "11" subtree */ 4908 -4, -5, /* 4 = "100", 5 = "101" */ 4909 -6, -7 /* 6 = "110", 7 = "111" */ 4910 }; 4912 /* Read MV component at current decoder position, using 4913 supplied probs. */ 4915 int read_mvcomponent( bool_decoder *d, const MV_CONTEXT *mvc) 4916 { 4917 const Prob * const p = (const Prob *) mvc; 4918 int A = 0; 4920 if( read_bool( d, p [mvpis_short])) /* 8 <= A <= 1023 */ 4921 { 4922 /* Read bits 0, 1, 2 */ 4924 int i = 0; 4925 do { A += read_bool( d, p [MVPbits + i]) << i;} 4926 while( ++i < 3); 4928 /* Read bits 9, 8, 7, 6, 5, 4 */ 4930 i = 9; 4931 do { A += read_bool( d, p [MVPbits + i]) << i;} 4932 while( --i > 3); 4934 /* We know that A >= 8 because it is coded long, 4935 so if A <= 15, bit 3 is one and is not 4936 explicitly coded. */ 4938 if( !(A & 0xfff0) || read_bool( d, p [MVPbits + 3])) 4939 A += 8; 4940 } 4941 else /* 0 <= A <= 7 */ 4942 A = treed_read( d, small_mvtree, p + MVPshort); 4944 return A && read_bool( r, p [MVPsign]) ? -A : A; 4945 } 4946 ---- End code block ---------------------------------------- 4948 17.2. Probability Updates 4950 The decoder should maintain an array of two MV_CONTEXTs for decoding 4951 row and column components, respectively. These MV_CONTEXTs should be 4952 set to their defaults every key frame. Each individual probability 4953 may be updated every interframe (by field J of the frame header) 4954 using a constant table of update probabilities. Each optional update 4955 is of the form B? P(7), that is, a bool followed by a 7-bit 4956 probability specification if true. 4958 As with other dynamic probabilities used by VP8, the updates remain 4959 in effect until the next key frame or until replaced via another 4960 update. 4962 In detail, the probabilities should then be managed as follows. 4964 ---- Begin code block -------------------------------------- 4966 /* Never-changing table of update probabilities for each 4967 individual probability used in decoding motion vectors. */ 4969 const MV_CONTEXT vp8_mv_update_probs[2] = 4970 { 4971 { 4972 237, 4973 246, 4974 253, 253, 254, 254, 254, 254, 254, 4975 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 4976 }, 4977 { 4978 231, 4979 243, 4980 245, 253, 254, 254, 254, 254, 254, 4981 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 4982 } 4983 }; 4985 /* Default MV decoding probabilities. */ 4987 const MV_CONTEXT default_mv_context[2] = 4988 { 4989 { // row 4990 162, // is short 4991 128, // sign 4992 225, 146, 172, 147, 214, 39, 156, // short tree 4993 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 // long bits 4995 }, 4997 { // same for column 4998 164, // is short 4999 128, 5000 204, 170, 119, 235, 140, 230, 228, 5001 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 // long bits 5003 } 5004 }; 5006 /* Current MV decoding probabilities, set to above defaults 5007 every key frame. */ 5009 MV_CONTEXT mvc [2]; /* always row, then column */ 5011 /* Procedure for decoding a complete motion vector. */ 5013 typedef struct { int16 row, col;} MV; /* as in previous chapter */ 5015 MV read_mv( bool_decoder *d) 5016 { 5017 MV v; 5018 v.row = (int16) read_mvcomponent( d, mvc); 5019 v.col = (int16) read_mvcomponent( d, mvc + 1); 5020 return v; 5021 } 5023 /* Procedure for updating MV decoding probabilities, called 5024 every interframe with "d" at the appropriate position in 5025 the frame header. */ 5027 void update_mvcontexts( bool_decoder *d) 5028 { 5029 int i = 0; 5030 do { /* component = row, then column */ 5031 const Prob *up = mv_update_probs[i]; /* update probs 5032 for component */ 5033 Prob *p = mvc[i]; /* start decode tbl "" */ 5034 Prob * const pstop = p + MVPcount; /* end decode tbl "" */ 5035 do { 5036 if( read_bool( d, *up++)) /* update this position */ 5037 { 5038 const Prob x = read_literal( d, 7); 5040 *p = x? x<<1 : 1; 5041 } 5042 } while( ++p < pstop); /* next position */ 5044 } while( ++i < 2); /* next component */ 5045 } 5047 ---- End code block ---------------------------------------- 5049 This completes the description of the motion-vector decoding 5050 procedure and, with it, the procedure for decoding interframe 5051 macroblock prediction records. 5053 18. Interframe Prediction 5055 Given an inter-prediction specification for the current macroblock, 5056 that is, a reference frame together with a motion vector for each of 5057 the sixteen Y subblocks, we describe the calculation of the 5058 prediction buffer for the macroblock. Frame reconstruction is then 5059 completed via the previously-described processes of residue summation 5060 (Section 14) and loop filtering (Section 15). 5062 The management of inter-predicted subblocks may be found in the 5063 reference decoder file reconinter.c; sub-pixel interpolation is 5064 implemented in filter_c.c. 5066 18.1. Bounds on and Adjustment of Motion Vectors 5068 Since each motion vector is differentially encoded from a neighboring 5069 block or macroblock and the only clamp is to ensure that the 5070 referenced motion vector represents a valid location inside a 5071 reference frame buffer, it is technically possible within the VP8 5072 format for a block or macroblock to have arbitrarily large motion 5073 vectors, up to the size of the input image plus the extended border 5074 areas. For practical reasons, VP8 imposes a motion vector size range 5075 limit of [-4096, 4095] full pixels, regardless of image size (VP8 5076 defines 14 raw bits for width and height; 16383x16383 is the maximum 5077 possible image size). Bitstream-compliant encoders and decoders 5078 shall enforce this limit. 5080 Because the motion vectors applied to the chroma subblocks have 1/8 5081 pixel resolution, the synthetic pixel calculation, outlined in 5082 Chapter 5 and detailed below, uses this resolution for the luma 5083 subblocks as well. In accordance, the stored luma motion vectors are 5084 all doubled, each component of each luma vector becoming an even 5085 integer in the range -2046 to +2046, inclusive. 5087 The vector applied to each chroma subblock is calculated by averaging 5088 the vectors for the 4 luma subblocks occupying the same visible area 5089 as the chroma subblock in the usual correspondence, that is, the 5090 vector for U and V block 0 is the average of the vectors for the Y 5091 subblocks { 0, 1, 4, 5}, chroma block 1 corresponds to Y blocks { 2, 5092 3, 6, 7}, chroma block 2 to Y blocks { 8, 9, 12, 13}, and chroma 5093 block 3 to Y blocks { 10, 11, 14, 15}. 5095 In detail, each of the two components of the vectors for each of the 5096 chroma subblocks is calculated from the corresponding luma vector 5097 components as follows: 5099 ---- Begin code block -------------------------------------- 5101 int avg( int c1, int c2, int c3, int c4) 5102 { 5103 int s = c1 + c2 + c3 + c4; 5105 /* The shift divides by 8 (not 4) because chroma pixels 5106 have twice the diameter of luma pixels. The handling 5107 of negative motion vector components is slightly 5108 cumbersome because, strictly speaking, right shifts 5109 of negative numbers are not well-defined in C. */ 5111 return s >= 0 ? (s + 4) >> 3 : -( (-s + 4) >> 3); 5112 } 5114 ---- End code block ---------------------------------------- 5116 Furthermore, if the version number in the frame tag specifies only 5117 full-pel chroma motion vectors, then the fractional parts of both 5118 components of the vector are truncated to zero, as illustrated in the 5119 following pseudo-code (assuming 3 bits of fraction for both luma and 5120 chroma vectors): 5122 ---- Begin code block -------------------------------------- 5124 x = x & (~7); 5125 y = y & (~7); 5127 ---- End code block ---------------------------------------- 5129 Earlier in this document we described the vp8_clamp_mv() function to 5130 limit "nearest" and "near" motion vector predictors inside specified 5131 margins within the frame boundaries. Additional clamping is 5132 performed for NEW_MV macroblocks, for which the final motion vector 5133 is clamped again after combining the "best" predictor and the 5134 differential vector decoded from the stream. 5136 However, the secondary clamping is not performed for SPLIT_MV 5137 macroblocks, meaning that any subblock's motion vector within the 5138 SPLIT_MV macroblock may point outside the clamping zone. These non- 5139 clamped vectors are also used when determining the decoding tree 5140 context for subsequent subblocks' modes in the vp8_mvCont() function. 5142 18.2. Prediction Subblocks 5144 The prediction calculation for each subblock is then as follows. 5145 Temporarily disregarding the fractional part of the motion vector 5146 (that is, rounding "up" or "left" by right-shifting each component 3 5147 bits with sign propagation) and adding the origin (upper left 5148 position) of the (16x16 luma or 8x8 chroma) current macroblock gives 5149 us an origin in the Y, U, or V plane of the predictor frame (either 5150 the golden frame or previous frame). 5152 Considering that origin to be the upper left corner of a (luma or 5153 chroma) macroblock, we need to specify the relative positions of the 5154 pixels associated to that subblock, that is, any pixels that might be 5155 involved in the sub-pixel interpolation processes for the subblock. 5157 18.3. Sub-pixel Interpolation 5159 The sub-pixel interpolation is effected via two one-dimensional 5160 convolutions. These convolutions may be thought of as operating on a 5161 two-dimensional array of pixels whose origin is the subblock origin, 5162 that is the origin of the prediction macroblock described above plus 5163 the offset to the subblock. Because motion vectors are arbitrary, so 5164 are these "prediction subblock origins". 5166 The integer part of the motion vector is subsumed in the origin of 5167 the prediction subblock, the 16 (synthetic) pixels we need to 5168 construct are given by 16 offsets from the origin. The integer part 5169 of each of these offsets is the offset of the corresponding pixel 5170 from the subblock origin (using the vertical stride). To these 5171 integer parts is added a constant fractional part, which is simply 5172 the difference between the actual motion vector and its integer 5173 truncation used to calculate the origins of the prediction macroblock 5174 and subblock. Each component of this fractional part is an integer 5175 between 0 and 7, representing a forward displacement in eighths of a 5176 pixel. 5178 It is these fractional displacements that determine the filtering 5179 process. If they both happen to be zero (that is, we had a "whole 5180 pixel" motion vector), the prediction subblock is simply copied into 5181 the corresponding piece of the current macroblock's prediction 5182 buffer. As discussed in Chapter 14, the layout of the macroblock's 5183 prediction buffer can depend on the specifics of the reconstruction 5184 implementation chosen. Of course, the vertical displacement between 5185 lines of the prediction subblock is given by the stride, as are all 5186 vertical displacements used here. 5188 Otherwise, at least one of the fractional displacements is non-zero. 5189 We then synthesize the missing pixels via a horizontal, followed by a 5190 vertical, one-dimensional interpolation. 5192 The two interpolations are essentially identical. Each uses an (at 5193 most) six-tap filter (the choice of which of course depends on the 5194 one-dimensional offset). Thus, every calculated pixel references at 5195 most three pixels before (above or to-the-left of) it and at most 5196 three pixels after (below or to-the-right of) it. The horizontal 5197 interpolation must calculate two extra rows above and three extra 5198 rows below the 4x4 block, to provide enough samples for the vertical 5199 interpolation to proceed. 5201 Depending on the reconstruction filter type given in the field 5202 Version Number in the frame tag, either a bicubic or a bilinear tap 5203 set is used. 5205 The exact implementation of subsampling is as follows. 5207 ---- Begin code block -------------------------------------- 5209 /* Filter taps taken to 7-bit precision. 5210 Because DC is always passed, taps always sum to 128. */ 5212 const int BilinearFilters[8][6] = 5213 { 5214 { 0, 0, 128, 0, 0, 0 }, 5215 { 0, 0, 112, 16, 0, 0 }, 5216 { 0, 0, 96, 32, 0, 0 }, 5217 { 0, 0, 80, 48, 0, 0 }, 5218 { 0, 0, 64, 64, 0, 0 }, 5219 { 0, 0, 48, 80, 0, 0 }, 5220 { 0, 0, 32, 96, 0, 0 }, 5221 { 0, 0, 16, 112, 0, 0 } 5222 }; 5224 const int filters [8] [6] = { /* indexed by displacement */ 5225 { 0, 0, 128, 0, 0, 0 }, /* degenerate whole-pixel */ 5226 { 0, -6, 123, 12, -1, 0 }, /* 1/8 */ 5227 { 2, -11, 108, 36, -8, 1 }, /* 1/4 */ 5228 { 0, -9, 93, 50, -6, 0 }, /* 3/8 */ 5229 { 3, -16, 77, 77, -16, 3 }, /* 1/2 is symmetric */ 5230 { 0, -6, 50, 93, -9, 0 }, /* 5/8 = reverse of 3/8 */ 5231 { 1, -8, 36, 108, -11, 2 }, /* 3/4 = reverse of 1/4 */ 5232 { 0, -1, 12, 123, -6, 0 } /* 7/8 = reverse of 1/8 */ 5233 }; 5235 /* One-dimensional synthesis of a single sample. 5236 Filter is determined by fractional displacement */ 5238 Pixel interp( 5239 const int fil[6], /* filter to apply */ 5240 const Pixel *p, /* origin (rounded "before") in 5241 prediction area */ 5243 const int s /* size of one forward step "" */ 5244 ) { 5245 int32 a = 0; 5246 int i = 0; 5247 p -= s + s; /* move back two positions */ 5249 do { 5250 a += *p * fil[i]; 5251 p += s; 5252 } while( ++i < 6); 5254 return clamp255( (a + 64) >> 7); /* round to nearest 5255 8-bit value */ 5256 } 5258 /* First do horizontal interpolation, producing intermediate 5259 buffer. */ 5261 void Hinterp( 5262 Pixel temp[9][4], /* 9 rows of 4 (intermediate) 5263 destination values */ 5264 const Pixel *p, /* subblock origin in prediction 5265 frame */ 5266 int s, /* vertical stride to be used in 5267 prediction frame */ 5268 uint hfrac, /* 0 <= horizontal displacement <= 7 */ 5269 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5270 ) { 5271 const int * const fil = bicubic ? filters [hfrac] : 5272 BilinearFilters[hfrac]; 5274 int r = 0; do /* for each row */ 5275 { 5276 int c = 0; do /* for each destination sample */ 5277 { 5278 /* Pixel separation = one horizontal step = 1 */ 5280 temp[r][c] = interp( fil, p + c, 1); 5281 } 5282 while( ++c < 4); 5283 } 5284 while( p += s, ++r < 9); /* advance p to next row */ 5285 } 5287 /* Finish with vertical interpolation, producing final results. 5288 Input array "temp" is of course that computed above. */ 5290 void Vinterp( 5291 Pixel final[4][4], /* 4 rows of 4 (final) destination values */ 5292 const Pixel temp[9][4], 5293 uint vfrac, /* 0 <= vertical displacement <= 7 */ 5294 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5295 ) { 5296 const int * const fil = bicubic ? filters [vfrac] : 5297 BilinearFilters[vfrac]; 5299 int r = 0; do /* for each row */ 5300 { 5301 int c = 0; do /* for each destination sample */ 5302 { 5303 /* Pixel separation = one vertical step = width 5304 of array = 4 */ 5306 final[r][c] = interp( fil, temp[r] + c, 4); 5307 } 5308 while( ++c < 4); 5309 } 5310 while( ++r < 4); 5311 } 5313 ---- End code block ---------------------------------------- 5315 18.4. Filter Properties 5317 We discuss briefly the rationale behind the choice of filters. Our 5318 approach is necessarily cursory; a genuinely accurate discussion 5319 would require a couple of books. Readers unfamiliar with signal 5320 processing may or may not wish to skip this. 5322 All digital signals are of course sampled in some fashion. The case 5323 where the inter-sample spacing (say in time for audio samples, or 5324 space for pixels) is uniform, that is, the same at all positions, is 5325 particularly common and amenable to analysis. Many aspects of the 5326 treatment of such signals are best-understood in the frequency domain 5327 via Fourier Analysis, particularly those aspects of the signal that 5328 are not changed by shifts in position, especially when those 5329 positional shifts are not given by a whole number of samples. 5331 Non-integral translates of a sampled signal are a textbook example of 5332 the foregoing. In our case of non-integral motion vectors, we wish 5333 to say what the underlying image "really is" at these pixels we don't 5334 have values for but feel that it makes sense to talk about. The 5335 correctness of this feeling is predicated on the underlying signal 5336 being band-limited, that is, not containing any energy in spatial 5337 frequencies that cannot be faithfully rendered at the pixel 5338 resolution at our disposal. In one dimension, this range of "OK" 5339 frequencies is called the Nyquist band; in our two-dimensional case 5340 of integer-grid samples, this range might be termed a Nyquist 5341 rectangle. The finer the grid, the more we know about the image, and 5342 the wider the Nyquist rectangle. 5344 It turns out that, for such band-limited signals, there is indeed an 5345 exact mathematical formula to produce the correct sample value at an 5346 arbitrary point. Unfortunately, this calculation requires the 5347 consideration of every single sample in the image, as well as needing 5348 to operate at infinite precision. Also, strictly speaking, all band- 5349 limited signals have infinite spatial (or temporal) extent, so 5350 everything we are discussing is really some sort of approximation. 5352 It is true that the theoretically correct subsampling procedure, as 5353 well as any approximation thereof, is always given by a translation- 5354 invariant weighted sum (or filter) similar to that used by VP8. It 5355 is also true that the reconstruction error made by such a filter can 5356 be simply represented as a multiplier in the frequency domain, that 5357 is, such filters simply multiply the Fourier transform of any signal 5358 to which they are applied by a fixed function associated to the 5359 filter. This fixed function is usually called the frequency response 5360 (or transfer function); the ideal subsampling filter has a frequency 5361 response equal to one in the Nyquist rectangle and zero everywhere 5362 else. 5364 Another basic fact about approximations to "truly correct" 5365 subsampling is that, the wider the subrectangle (within the Nyquist 5366 rectangle) of spatial frequencies one wishes to "pass" (that is, 5367 correctly render) or, put more accurately, the closer one wishes to 5368 approximate the ideal transfer function, the more samples of the 5369 original signal must be considered by the subsampling, and the wider 5370 the calculation precision necessitated. 5372 The filters chosen by VP8 were chosen, within the constraints of 4 or 5373 6 taps and 7-bit precision, to do the best possible job of handling 5374 the low spatial frequencies near the zeroth DC frequency along with 5375 introducing no resonances (places where the absolute value of the 5376 frequency response exceeds one). 5378 The justification for the foregoing has two parts. First, resonances 5379 can produce extremely objectionable visible artifacts when, as often 5380 happens in actual compressed video streams, filters are applied 5381 repeatedly. Second, the vast majority of energy in real-world images 5382 lies near DC and not at the high-end. 5384 To get slightly more specific, the filters chosen by VP8 are the best 5385 resonance-free 4- or 6-tap filters possible, where "best" describes 5386 the frequency response near the origin: the response at 0 is required 5387 to be 1 and the graph of the response at 0 is as flat as possible. 5389 To provide an intuitively more obvious point of reference, the "best" 5390 2-tap filter is given by simple linear interpolation between the 5391 surrounding actual pixels. 5393 Finally, it should be noted that, because of the way motion vectors 5394 are calculated, the (shorter) 4-tap filters (used for odd fractional 5395 displacements) are applied in the chroma plane only. Human color 5396 perception is notoriously poor, especially where higher spatial 5397 frequencies are involved. The shorter filters are easier to 5398 understand mathematically, and the difference between them and a 5399 theoretically slightly better 6-tap filter is negligible where chroma 5400 is concerned. 5402 19. Annex A: Bitstream Syntax 5404 This annex presents the bitstream syntax in a tabular form. All the 5405 information elements have been introduced and explained in the 5406 previous chapters but are collected here for a quick reference. Each 5407 syntax element is shortly described after the tabular representation 5408 along with a reference to the corresponding paragraph in the main 5409 document. The meaning of each syntax element value is not repeated 5410 here. 5412 The top-level hierarchy of the bitstream is introduced in Section 4. 5414 Definition of syntax element coding types can be found in Section 8. 5415 The types used in the representation in this annex are: 5417 o f(n), n-bit value from stream (n successive bits, not boolean 5418 encoded) 5420 o L(n), n-bit number encoded as n booleans (with equal probability 5421 of being 0 or 1) 5423 o B(p), bool with probability p of being 0 5425 o T, tree-encoded value 5427 19.1. Uncompressed Data Chunk 5429 +----------------------+-------+ 5430 | Frame Tag | Type | 5431 +----------------------+-------+ 5432 | frame_tag | f(24) | 5433 | | | 5434 | if (key_frame) { | | 5435 | | | 5436 | start_code | f(24) | 5437 | | | 5438 | horizontal_size_code | f(16) | 5439 | | | 5440 | vertical_size_code | f(16) | 5441 | | | 5442 | } | | 5443 +----------------------+-------+ 5445 The 3-byte frame tag can be parsed as follows: 5447 ---- Begin code block -------------------------------------- 5449 unsigned char *c = pbi->Source; 5450 unsigned int tmp; 5452 tmp = (c[2] << 16) | (c[1] << 8) | c[0]; 5454 key_frame = tmp & 0x1; 5455 version = (tmp >> 1) & 0x7; 5456 show_frame = (tmp >> 4) & 0x1; 5457 first_part_size = (tmp >> 5) & 0x7FFFF; 5459 ---- End code block ---------------------------------------- 5461 Where: 5463 o key_frame indicates if the current frame is a key frame or not. 5465 o version determines the bitstream version. 5467 o show_frame indicates if the current frame is meant to be displayed 5468 or not. 5470 o first_part_size determines the size of the first partition 5471 (control partition), excluding the uncompressed data chunk. 5473 The start_code is a constant 3-byte pattern having value 0x9d012a. 5474 The latter part of the uncompressed chunk (after the start_code) can 5475 be parsed as follows: 5477 ---- Begin code block -------------------------------------- 5479 unsigned char *c = pbi->Source + 6; 5480 unsigned int tmp; 5482 tmp = (c[1] << 8) | c[0]; 5484 width = tmp & 0x3FFF; 5485 horizontal_scale = tmp >> 14; 5487 tmp = (c[3] << 8) | c[2]; 5489 height = tmp & 0x3FFF; 5490 vertical_scale = tmp >> 14; 5492 ---- End code block ---------------------------------------- 5494 19.2. Frame Header 5496 +-------------------------------+------+ 5497 | Frame Header | Type | 5498 +-------------------------------+------+ 5499 | if (key_frame) { | | 5500 | | | 5501 | color_space | L(1) | 5502 | | | 5503 | clamping_type | L(1) | 5504 | | | 5505 | } | | 5506 | | | 5507 | segmentation_enabled | L(1) | 5508 | | | 5509 | if (segmentation_enabled) { | | 5510 | | | 5511 | update_segmentation() | | 5512 | | | 5513 | } | | 5514 | | | 5515 | filter_type | L(1) | 5516 | | | 5517 | loop_filter_level | L(6) | 5518 | | | 5519 | sharpness_level | L(3) | 5520 | | | 5521 | mb_lf_adjustments() | | 5522 | | | 5523 | log2_nbr_of_dct_partitions | L(2) | 5524 | | | 5525 | quant_indices() | | 5526 | | | 5527 | if (key_frame) { | | 5528 | | | 5529 | refresh_entropy_probs | L(1) | 5530 | | | 5531 | } else { | | 5532 | | | 5533 | refresh_golden_frame | L(1) | 5534 | | | 5535 | refresh_alternate_frame | L(1) | 5536 | | | 5537 | if (!refresh_golden_frame) | | 5538 | | | 5539 | copy_buffer_to_golden | L(2) | 5540 | | | 5541 | if (!refresh_alternate_frame) | | 5542 | copy_buffer_to_alternate | L(2) | 5543 | | | 5544 | sign_bias_golden | L(1) | 5545 | | | 5546 | sign_bias_alternate | L(1) | 5547 | | | 5548 | refresh_entropy_probs | L(1) | 5549 | | | 5550 | refresh_last | L(1) | 5551 | | | 5552 | } | | 5553 | | | 5554 | token_prob_update() | | 5555 | | | 5556 | mb_no_coeff_skip | L(1) | 5557 +-------------------------------+------+ 5559 +--------------------------------------+------+ 5560 | Frame Header | Type | 5561 +--------------------------------------+------+ 5562 | prob_skip_false | L(8) | 5563 | | | 5564 | if (!key_frame) { | | 5565 | | | 5566 | prob_intra | L(8) | 5567 | | | 5568 | prob_last | L(8) | 5569 | | | 5570 | prob_golden | L(8) | 5571 | | | 5572 | intra_16x16_prob_update_flag | L(1) | 5573 | | | 5574 | if (intra_16x16_prob_update_flag) { | | 5575 | | | 5576 | for (i = 0; i < 4; i++) | | 5577 | | | 5578 | intra_16x16_prob | L(8) | 5579 | | | 5580 | } | | 5581 | | | 5582 | intra_chroma prob_update_flag | L(1) | 5583 | | | 5584 | if (intra_chroma_prob_update_flag) { | | 5585 | | | 5586 | for (i = 0; i < 3; i++) | | 5587 | | | 5588 | intra_chroma_prob | L(8) | 5589 | | | 5590 | } | | 5591 | | | 5592 | mv_prob_update() | | 5593 | | | 5594 | } | | 5595 +--------------------------------------+------+ 5597 o color_space defines the YUV color space of the sequence 5598 (Section 9.2) 5600 o clamping_type specifies if the decoder is required to clamp the 5601 reconstructed pixel values (Section 9.2) 5603 o segmentation_enabled enables the segmentation feature for the 5604 current frame (Section 9.3) 5606 o filter_type determines whether the normal or the simple loop 5607 filter is used (Section 9.4, Section 15) 5609 o loop_filter_level controls the deblocking filter (Section 9.4, 5610 Section 15) 5612 o sharpness_level controls the deblocking filter (Section 9.4, 5613 Section 15) 5615 o log2_nbr_of_dct_partitions determines the number of separate 5616 partitions containing the DCT coefficients of the macroblocks 5617 (Section 9.5) 5619 o refresh_entropy_probs determines whether updated token 5620 probabilities are used only for this frame or until further update 5622 o refresh_golden_frame determines if the current decoded frame 5623 refreshes the golden frame (Section 9.7) 5625 o refresh_alternate_frame determines if the current decoded frame 5626 refreshes the alternate reference frame (Section 9.7) 5628 o copy_buffer_to_golden determines if the golden reference is 5629 replaced by another reference (Section 9.7) 5631 o copy_buffer_to_alternate determines if the alternate reference is 5632 replaced by another reference (Section 9.7) 5634 o sign_bias_golden controls the sign of motion vectors when the 5635 golden frame is referenced (Section 9.7) 5637 o sign_bias_alternate controls the sign of motion vectors when the 5638 alternate frame is referenced (Section 9.7) 5640 o refresh_last determines if the current decoded frame refreshes the 5641 last frame reference buffer (Section 9.8) 5643 o mb_no_coeff_skip enables or disables the skipping of macroblocks 5644 containing no non-zero coefficients (Section 9.10) 5646 o prob_skip_false the probability that the macroblock is not skipped 5647 (flag indicating skipped macroblock is false) (Section 9.10) 5649 o prob_intra the probability of an intra macroblock (Section 9.10) 5651 o prob_last the probability that the last reference frame is used 5652 for inter prediction (Section 9.10) 5654 o prob_golden the probability that the golden reference frame is 5655 used for inter prediction (Section 9.10) 5657 o intra_16x16_prob_update_flag indicates if the branch probabilies 5658 used in the decoding of luma intra prediction mode are updated 5659 (Section 9.10) 5661 o intra_16x16_prob the branch probabilities of the luma intra 5662 prediction mode decoding tree 5664 o intra_chroma_prob_update_flag indicates if the branch probabilies 5665 used in the decoding of chroma intra prediction mode are updated 5666 (Section 9.10) 5668 o intra_chroma_prob the branch probabilities of the chroma intra 5669 prediction mode decoding tree 5671 +------------------------------------+------+ 5672 | update_segmentation() | Type | 5673 +------------------------------------+------+ 5674 | update_mb_segmentation_map | L(1) | 5675 | | | 5676 | update_segment_feature_data | L(1) | 5677 | | | 5678 | if (update_segment_feature_data) { | | 5679 | | | 5680 | segment_feature_mode | L(1) | 5681 | | | 5682 | for (i = 0; i < 4; i++) { | | 5683 | | | 5684 | quantizer_update | L(1) | 5685 | | | 5686 | if (quantizer_update) { | | 5687 | | | 5688 | quantizer_update_value | L(7) | 5689 | | | 5690 | quantizer_update_sign | L(1) | 5691 | | | 5692 | } | | 5693 | | | 5694 | } | | 5695 | | | 5696 | for (i = 0; i < 4; i++) { | | 5697 | | | 5698 | loop_filter_update | L(1) | 5699 | | | 5700 | if (loop_filter_update) { | | 5701 | | | 5702 | lf_update_value | L(6) | 5703 | | | 5704 | lf_update_sign | L(1) | 5705 | | | 5706 | } | | 5707 | | | 5708 | } | | 5709 | | | 5710 | } | | 5711 | | | 5712 | if (update_mb_segmentation_map) { | | 5713 | | | 5714 | for (i = 0; i < 3; i++) { | | 5715 | | | 5716 | segment_prob_update | L(1) | 5717 | | | 5718 | if (segment_prob_update) { | | 5719 | | | 5720 | segment_prob | L(8) | 5721 | | | 5722 | } | | 5723 | | | 5724 | } | | 5725 | | | 5726 | } | | 5727 +------------------------------------+------+ 5729 o update_mb_segmentation_map determines if the MB segmentation map 5730 is updated in the current frame (Section 9.3) 5732 o update_segment_feature_data indicates if the segment feature data 5733 is updated in the current frame (Section 9.3) 5735 o segment_feature_mode indicates the feature data update mode, 0 for 5736 delta and 1 for the absolute value (Section 9.3) 5738 o quantizer_update indicates if the quantizer value is updated for 5739 the i^(th) segment (Section 9.3) 5741 o quantizer_update_value indicates the update value for the segment 5742 quantizer (Section 9.3) 5744 o quantizer_update_sign indicates the update sign for the segment 5745 quantizer (Section 9.3) 5747 o loop_filter_update indicates if the loop filter level value is 5748 updated for the i^(th) segment (Section 9.3) 5750 o lf_update_value indicates the update value for the loop filter 5751 level (Section 9.3) 5753 o lf_update_sign indicates the update sign for the loop filter level 5754 (Section 9.3) 5756 o segment_prob_update indicates if the branch probabilities used to 5757 decode the segment_id in the MB header are decoded from the stream 5758 or use the default value of 255 (Section 9.3) 5760 o segment_prob the branch probabilities of the segment_id decoding 5761 tree (Section 9.3) 5762 +------------------------------------+------+ 5763 | mb_lf_adjustments() | Type | 5764 +------------------------------------+------+ 5765 | loop_filter_adj_enable | L(1) | 5766 | | | 5767 | if (loop_filter_adj_enable) { | | 5768 | | | 5769 | mode_ref_lf_delta_update | L(1) | 5770 | | | 5771 | if (mode_ref_lf_delta_update) { | | 5772 | | | 5773 | for (i = 0; i < 4; i++) { | | 5774 | | | 5775 | ref_frame_delta_update_flag | L(1) | 5776 | | | 5777 | if (ref_frame_delta_update_flag) { | | 5778 | | | 5779 | delta_magnitude | L(6) | 5780 | | | 5781 | delta_sign | L(1) | 5782 | | | 5783 | } | | 5784 | | | 5785 | } | | 5786 | | | 5787 | for (i = 0; i < 4; i++) { | | 5788 | | | 5789 | mb_mode_delta_update_flag | L(1) | 5790 | | | 5791 | if (mb_mode_delta_update_flag) { | | 5792 | | | 5793 | delta_magnitude | L(6) | 5794 | | | 5795 | delta_sign | L(1) | 5796 | | | 5797 | } | | 5798 | | | 5799 | } | | 5800 | | | 5801 | } | | 5802 | | | 5803 | } | | 5804 +------------------------------------+------+ 5806 o loop_filter_adj_enable indicates if the MB-level loop filter 5807 adjustment (based on the used reference frame and coding mode) is 5808 on for the current frame (Section 9.4) 5810 o mode_ref_lf_delta_update indicates if the delta values used in 5811 adjustment are updated in the current frame (Section 9.4) 5813 o ref_frame_delta_update_flag indicates if the adjustment delta 5814 value corresponding to a certain used reference frame is updated 5815 (Section 9.4) 5817 o delta_magnitude is the absolute value of the delta value 5819 o delta_sign is the sign of the delta value 5821 o mb_mode_delta_update_flag indicates if the adjustment delta value 5822 corresponding to certain MB prediction mode is updated 5823 (Section 9.4) 5825 +----------------------------+------+ 5826 | quant_indices() | Type | 5827 +----------------------------+------+ 5828 | y_ac_qi | L(7) | 5829 | | | 5830 | y_dc_delta_present | L(1) | 5831 | | | 5832 | if (y_dc_delta_present) { | | 5833 | | | 5834 | y_dc_delta_magnitude | L(4) | 5835 | | | 5836 | y_dc_delta_sign | L(1) | 5837 | | | 5838 | } | | 5839 | | | 5840 | y2_dc_delta_present | L(1) | 5841 | | | 5842 | if (y2_dc_delta_present) { | | 5843 | | | 5844 | y2_dc_delta_magnitude | L(4) | 5845 | | | 5846 | y2_dc_delta_sign | L(1) | 5847 | | | 5848 | } | | 5849 | | | 5850 | y2_ac_delta_present | L(1) | 5851 | | | 5852 | if (y2_ac_delta_present) { | | 5853 | | | 5854 | y2_ac_delta_magnitude | L(4) | 5855 | | | 5856 | y2_ac_delta_sign | L(1) | 5857 | | | 5858 | } | | 5859 | | | 5860 | uv_dc_delta_present | L(1) | 5861 | | | 5862 | if (uv_dc_delta_present) { | | 5863 | | | 5864 | uv_dc_delta_magnitude | L(4) | 5865 | | | 5866 | uv_dc_delta_sign | L(1) | 5867 | | | 5868 | } | | 5869 | | | 5870 | uv_ac_delta_present | L(1) | 5871 | | | 5872 | if (uv_ac_delta_present) { | | 5873 | | | 5874 | uv_ac_delta_magnitude | L(4) | 5875 | | | 5876 | uv_ac_delta_sign | L(1) | 5877 | | | 5878 | } | | 5879 +----------------------------+------+ 5881 o y_ac_qi is the dequantization table index used for the luma AC 5882 coefficients (and other coefficient groups if no delta value is 5883 present) (Section 9.6) 5885 o y_dc_delta_present indicates if the stream contains a delta value 5886 that is added to the baseline index to obtain the luma DC 5887 coefficient dequantization index (Section 9.6) 5889 o y_dc_delta_magnitude the magnitude of the delta value 5890 (Section 9.6) 5892 o y_dc_delta_sign the sign of the delta value (Section 9.6) 5894 o y2_dc_delta_present indicates if the stream contains a delta value 5895 that is added to the baseline index to obtain the Y2 block DC 5896 coefficient dequantization index (Section 9.6) 5898 o y2_ac_delta_present indicates if the stream contains a delta value 5899 that is added to the baseline index to obtain the Y2 block AC 5900 coefficient dequantization index (Section 9.6) 5902 o uv_dc_delta_present indicates if the stream contains a delta value 5903 that is added to the baseline index to obtain the chroma DC 5904 coefficient dequantization index (Section 9.6) 5906 o uv_ac_delta_present indicates if the stream contains a delta value 5907 that is added to the baseline index to obtain the chroma AC 5908 coefficient dequantization index (Section 9.6) 5910 +-------------------------------+------+ 5911 | token_prob_update() | Type | 5912 +-------------------------------+------+ 5913 | for (i = 0; i < 4; i++) { | | 5914 | | | 5915 | for (j = 0; j < 8; j++) { | | 5916 | | | 5917 | for (k = 0; k < 3; k++) { | | 5918 | | | 5919 | for (l = 0; l < 11; l++) { | | 5920 | | | 5921 | coeff_prob_update_flag | L(1) | 5922 | | | 5923 | if (coeff_prob_update_flag) { | | 5924 | | | 5925 | coeff_prob | L(8) | 5926 | | | 5927 | } | | 5928 | | | 5929 | } | | 5930 | | | 5931 | } | | 5932 | | | 5933 | } | | 5934 | | | 5935 | } | | 5936 +-------------------------------+------+ 5938 o coeff_prob_update_flag indicates if the corresponding branch 5939 probability is updated in the current frame (Section 13.4) 5941 o coeff_prob is the new branch probability (Section 13.4) 5942 +----------------------------+------+ 5943 | mv_prob_update() | Type | 5944 +----------------------------+------+ 5945 | for (i = 0; i < 2; i++) { | | 5946 | | | 5947 | for (j = 0; j < 19; j++) { | | 5948 | | | 5949 | mv_prob_update_flag | L(1) | 5950 | | | 5951 | if (mv_prob_update_flag) { | | 5952 | | | 5953 | prob | L(7) | 5954 | | | 5955 | } | | 5956 | | | 5957 | } | | 5958 | | | 5959 | } | | 5960 +----------------------------+------+ 5962 o mv_prob_update_flag indicates if the corresponding MV decoding 5963 probability is updated in the current frame (Section 17.2) 5965 o prob is the updated probability (Section 17.2) 5967 19.3. Macroblock Data 5969 +---------------------+------+ 5970 | Macroblock Data | Type | 5971 +---------------------+------+ 5972 | macroblock_header() | | 5973 | | | 5974 | residual_data() | | 5975 +---------------------+------+ 5977 +--------------------------------+------+ 5978 | macroblock_header() | Type | 5979 +--------------------------------+------+ 5980 | if (segmentation_map_update) { | | 5981 | | | 5982 | segment_id | T | 5983 | | | 5984 | if (mb_no_coeff_skip) { | | 5985 | | | 5986 | mb_coeff_skip | B(p) | 5987 | | | 5988 | } | | 5989 | | | 5990 | if (!key_frame) { | | 5991 | | | 5992 | is_inter_mb | B(p) | 5993 | | | 5994 | if (is_inter_mb) { | | 5995 | | | 5996 | mb_ref_frame_sel1 | B(p) | 5997 | | | 5998 | if (mb_ref_frame_sel1) | | 5999 | | | 6000 | mb_ref_frame_sel2 | B(p) | 6001 | | | 6002 | mv_mode | T | 6003 | | | 6004 | if (mv_mode == SPLITMV) { | | 6005 | | | 6006 | mv_split_mode | T | 6007 | | | 6008 | for (i = 0; i < numMvs; i++) { | | 6009 | | | 6010 | sub_mv_mode | T | 6011 | | | 6012 | if (sub_mv_mode == NEWMV4x4) { | | 6013 | | | 6014 | read_mvcomponent() | | 6015 | | | 6016 | read_mvcomponent() | | 6017 | | | 6018 | } | | 6019 | | | 6020 | } | | 6021 | | | 6022 | } else if (mv_mode == NEWMV) { | | 6023 | | | 6024 | read_mvcomponent() | | 6025 | | | 6026 | read_mvcomponent() | | 6027 | | | 6028 | } | | 6029 | | | 6030 | } else { /* intra mb */ | | 6031 | | | 6032 | intra_y_mode | T | 6033 +--------------------------------+------+ 6034 +-------------------------------+------+ 6035 | macroblock_header() | Type | 6036 +-------------------------------+------+ 6037 | if (intra_y_mode == B_PRED) { | | 6038 | | | 6039 | for (i = 0; i < 16; i++) | | 6040 | | | 6041 | intra_b_mode | T | 6042 | | | 6043 | } | | 6044 | | | 6045 | intra_uv_mode | T | 6046 | | | 6047 | } | | 6048 +-------------------------------+------+ 6050 o segment_id indicates to which segment the macroblock belongs 6051 (Section 10) 6053 o mb_coeff_skip indicates if the macroblock contains any coded 6054 coefficients or not (Section 11.1) 6056 o is_inter_mb indicates if the macroblock is intra or inter coded 6057 (Section 16) 6059 o mb_ref_frame_sel1 selects the reference frame to be used; last 6060 frame (0), golden/alternate (1) (Section 16.2) 6062 o mb_ref_frame_sel2 selects whether the golden (0) or alternate 6063 reference frame (1) is used (Section 16.2) 6065 o mv_mode determines the macroblock motion vector mode 6066 (Section 16.2) 6068 o mv_split_mode gives macroblock partitioning specification and 6069 determines number of motion vectors used (numMvs)(Section 16.2) 6071 o sub_mv_mode determines the sub-macroblock motion vector mode for 6072 macroblocks coded using SPLITMV motion vector mode (Section 16.2) 6074 o intra_y_mode selects the luminance intra prediction mode 6075 (Section 16.1) 6077 o intra_b_mode selects the sub-macroblock luminance prediction mode 6078 for macroblocks coded using B_PRED mode (Section 16.1) 6080 o intra_uv_mode selects the chrominance intra prediction mode 6081 (Section 16.1) 6082 +----------------------------------------------+------+ 6083 | residual_data() | Type | 6084 +----------------------------------------------+------+ 6085 | if (!mb_coeff_skip) { | | 6086 | | | 6087 | if ( (is_inter_mb && mv_mode != SPLITMV) || | | 6088 | | | 6089 | (!is_inter_mb && intra_y_mode != B_PRED) ) { | | 6090 | | | 6091 | residual_block() /* Y2 */ | | 6092 | | | 6093 | } | | 6094 | | | 6095 | for (i = 0; i < 24; i++) | | 6096 | | | 6097 | residual_block() /* 16 Y, 4 U, 4 V */ | | 6098 | | | 6099 | } | | 6100 +----------------------------------------------+------+ 6102 +-------------------------------------+------+ 6103 | residual_block() | Type | 6104 +-------------------------------------+------+ 6105 | for (i = firstCoeff; i < 16; i++) { | | 6106 | | | 6107 | token | T | 6108 | | | 6109 | if (token == EOB) break; | | 6110 | | | 6111 | if (token_has_extra_bits) { | | 6112 | | | 6113 | extra_bits | L(n) | 6114 | | | 6115 | sign | L(1) | 6116 +-------------------------------------+------+ 6118 o firstCoeff is 1 for luma blocks of macroblocks containing Y2 6119 subblock, otherwise 0 6121 o token defines the value of the coefficient, the value range of the 6122 coefficient or the end of block (Section 13.2) 6124 o extra_bits determine the value of the coefficient within the value 6125 range defined by token (Section 13.2) 6127 o sign indicates the sign of the coefficient (Section 13.2) 6129 20. Attachment One: Reference Decoder Source Code 6131 Note that the code in this attachment may exhibit bugs, and should be 6132 considered a draft until this document reaches RFC status. 6134 20.1. bit_ops.h 6136 ---- Begin code block -------------------------------------- 6138 /* 6139 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6140 * 6141 * Use of this source code is governed by a BSD-style license 6142 * that can be found in the LICENSE file in the root of the source 6143 * tree. An additional intellectual property rights grant can be 6144 * found in the file PATENTS. All contributing project authors may 6145 * be found in the AUTHORS file in the root of the source tree. 6146 */ 6147 #ifndef BIT_OPS_H 6148 #define BIT_OPS_H 6150 /* Evaluates to a mask with n bits set */ 6151 #define BITS_MASK(n) ((1<<(n))-1) 6153 /* Returns len bits, with the LSB at position bit */ 6154 #define BITS_GET(val, bit, len) (((val)>>(bit))&BITS_MASK(len)) 6156 #endif 6158 ---- End code block ---------------------------------------- 6160 20.2. bool_decoder.h 6162 ---- Begin code block -------------------------------------- 6164 /* 6165 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6166 * 6167 * Use of this source code is governed by a BSD-style license and 6168 * patent grant that can be found in the LICENSE file in the root of 6169 * the source tree. All contributing project authors may be found in 6170 * the AUTHORS file in the root of the source tree. 6171 */ 6173 #ifndef BOOL_DECODER_H 6174 #define BOOL_DECODER_H 6175 #include 6177 struct bool_decoder 6178 { 6179 const unsigned char *input; /* next compressed data byte */ 6180 size_t input_len; /* length of the input buffer */ 6181 unsigned int range; /* identical to encoder's 6182 * range */ 6183 unsigned int value; /* contains at least 8 6184 * significant bits */ 6185 int bit_count; /* # of bits shifted out of 6186 * value, max 7 */ 6187 }; 6189 static void 6190 init_bool_decoder(struct bool_decoder *d, 6191 const unsigned char *start_partition, 6192 size_t sz) 6193 { 6194 if (sz >= 2) 6195 { 6196 d->value = (start_partition[0] << 8) /* first 2 input 6197 * bytes */ 6198 | start_partition[1]; 6199 d->input = start_partition + 2; /* ptr to next byte */ 6200 d->input_len = sz - 2; 6201 } 6202 else 6203 { 6204 d->value = 0; 6205 d->input = NULL; 6206 d->input_len = 0; 6207 } 6209 d->range = 255; /* initial range is full */ 6210 d->bit_count = 0; /* have not yet shifted out any bits */ 6211 } 6213 static int bool_get(struct bool_decoder *d, int probability) 6214 { 6215 /* range and split are identical to the corresponding values 6216 used by the encoder when this bool was written */ 6218 unsigned int split = 1 + (((d->range - 1) * probability) >> 8); 6219 unsigned int SPLIT = split << 8; 6220 int retval; /* will be 0 or 1 */ 6221 if (d->value >= SPLIT) /* encoded a one */ 6222 { 6223 retval = 1; 6224 d->range -= split; /* reduce range */ 6225 d->value -= SPLIT; /* subtract off left endpoint of 6226 * interval */ 6227 } 6228 else /* encoded a zero */ 6229 { 6230 retval = 0; 6231 d->range = split; /* reduce range, no change in left 6232 * endpoint */ 6233 } 6235 while (d->range < 128) /* shift out irrelevant value bits */ 6236 { 6237 d->value <<= 1; 6238 d->range <<= 1; 6240 if (++d->bit_count == 8) /* shift in new bits 8 at a time */ 6241 { 6242 d->bit_count = 0; 6244 if (d->input_len) 6245 { 6246 d->value |= *d->input++; 6247 d->input_len--; 6248 } 6249 } 6250 } 6252 return retval; 6253 } 6255 static int bool_get_bit(struct bool_decoder *br) 6256 { 6257 return bool_get(br, 128); 6258 } 6260 static int bool_get_uint(struct bool_decoder *br, int bits) 6261 { 6262 int z = 0; 6263 int bit; 6265 for (bit = bits - 1; bit >= 0; bit--) 6266 { 6267 z |= (bool_get_bit(br) << bit); 6268 } 6270 return z; 6271 } 6273 static int bool_get_int(struct bool_decoder *br, int bits) 6274 { 6275 int z = 0; 6276 int bit; 6278 for (bit = bits - 1; bit >= 0; bit--) 6279 { 6280 z |= (bool_get_bit(br) << bit); 6281 } 6283 return bool_get_bit(br) ? -z : z; 6284 } 6286 static int bool_maybe_get_int(struct bool_decoder *br, int bits) 6287 { 6288 return bool_get_bit(br) ? bool_get_int(br, bits) : 0; 6289 } 6291 static int 6292 bool_read_tree(struct bool_decoder *bool, 6293 const int *t, 6294 const unsigned char *p) 6295 { 6296 int i = 0; 6298 while ((i = t[ i + bool_get(bool, p[i>>1])]) > 0) ; 6300 return -i; 6301 } 6302 #endif 6304 ---- End code block ---------------------------------------- 6306 20.3. dequant_data.h 6308 ---- Begin code block -------------------------------------- 6310 static const int dc_q_lookup[128] = 6311 { 6312 4, 5, 6, 7, 8, 9, 10, 10, 6313 11, 12, 13, 14, 15, 16, 17, 17, 6314 18, 19, 20, 20, 21, 21, 22, 22, 6315 23, 23, 24, 25, 25, 26, 27, 28, 6316 29, 30, 31, 32, 33, 34, 35, 36, 6317 37, 37, 38, 39, 40, 41, 42, 43, 6318 44, 45, 46, 46, 47, 48, 49, 50, 6319 51, 52, 53, 54, 55, 56, 57, 58, 6320 59, 60, 61, 62, 63, 64, 65, 66, 6321 67, 68, 69, 70, 71, 72, 73, 74, 6322 75, 76, 76, 77, 78, 79, 80, 81, 6323 82, 83, 84, 85, 86, 87, 88, 89, 6324 91, 93, 95, 96, 98, 100, 101, 102, 6325 104, 106, 108, 110, 112, 114, 116, 118, 6326 122, 124, 126, 128, 130, 132, 134, 136, 6327 138, 140, 143, 145, 148, 151, 154, 157 6328 }; 6329 static const int ac_q_lookup[128] = 6330 { 6331 4, 5, 6, 7, 8, 9, 10, 11, 6332 12, 13, 14, 15, 16, 17, 18, 19, 6333 20, 21, 22, 23, 24, 25, 26, 27, 6334 28, 29, 30, 31, 32, 33, 34, 35, 6335 36, 37, 38, 39, 40, 41, 42, 43, 6336 44, 45, 46, 47, 48, 49, 50, 51, 6337 52, 53, 54, 55, 56, 57, 58, 60, 6338 62, 64, 66, 68, 70, 72, 74, 76, 6339 78, 80, 82, 84, 86, 88, 90, 92, 6340 94, 96, 98, 100, 102, 104, 106, 108, 6341 110, 112, 114, 116, 119, 122, 125, 128, 6342 131, 134, 137, 140, 143, 146, 149, 152, 6343 155, 158, 161, 164, 167, 170, 173, 177, 6344 181, 185, 189, 193, 197, 201, 205, 209, 6345 213, 217, 221, 225, 229, 234, 239, 245, 6346 249, 254, 259, 264, 269, 274, 279, 284 6347 }; 6349 ---- End code block ---------------------------------------- 6351 20.4. dixie.c 6352 ---- Begin code block -------------------------------------- 6354 /* 6355 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6356 * 6357 * Use of this source code is governed by a BSD-style license 6358 * that can be found in the LICENSE file in the root of the source 6359 * tree. An additional intellectual property rights grant can be 6360 * found in the file PATENTS. All contributing project authors may 6361 * be found in the AUTHORS file in the root of the source tree. 6362 */ 6363 #include "vpx_codec_internal.h" 6364 #include "bit_ops.h" 6365 #include "dixie.h" 6366 #include "vp8_prob_data.h" 6367 #include "dequant_data.h" 6368 #include "modemv.h" 6369 #include "tokens.h" 6370 #include "predict.h" 6371 #include "dixie_loopfilter.h" 6372 #include 6373 #include 6375 enum 6376 { 6377 FRAME_HEADER_SZ = 3, 6378 KEYFRAME_HEADER_SZ = 7 6379 }; 6381 #define ARRAY_COPY(a,b) {\ 6382 assert(sizeof(a)==sizeof(b));memcpy(a,b,sizeof(a));} 6383 static void 6384 decode_entropy_header(struct vp8_decoder_ctx *ctx, 6385 struct bool_decoder *bool, 6386 struct vp8_entropy_hdr *hdr) 6387 { 6388 int i, j, k, l; 6390 /* Read coefficient probability updates */ 6391 for (i = 0; i < BLOCK_TYPES; i++) 6392 for (j = 0; j < COEF_BANDS; j++) 6393 for (k = 0; k < PREV_COEF_CONTEXTS; k++) 6394 for (l = 0; l < ENTROPY_NODES; l++) 6395 if (bool_get(bool, 6396 k_coeff_entropy_update_probs 6397 [i][j][k][l])) 6398 hdr->coeff_probs[i][j][k][l] = 6399 bool_get_uint(bool, 8); 6401 /* Read coefficient skip mode probability */ 6402 hdr->coeff_skip_enabled = bool_get_bit(bool); 6404 if (hdr->coeff_skip_enabled) 6405 hdr->coeff_skip_prob = bool_get_uint(bool, 8); 6407 /* Parse interframe probability updates */ 6408 if (!ctx->frame_hdr.is_keyframe) 6409 { 6410 hdr->prob_inter = bool_get_uint(bool, 8); 6411 hdr->prob_last = bool_get_uint(bool, 8); 6412 hdr->prob_gf = bool_get_uint(bool, 8); 6414 if (bool_get_bit(bool)) 6415 for (i = 0; i < 4; i++) 6416 hdr->y_mode_probs[i] = bool_get_uint(bool, 8); 6418 if (bool_get_bit(bool)) 6419 for (i = 0; i < 3; i++) 6420 hdr->uv_mode_probs[i] = bool_get_uint(bool, 8); 6422 for (i = 0; i < 2; i++) 6423 for (j = 0; j < MV_PROB_CNT; j++) 6424 if (bool_get(bool, k_mv_entropy_update_probs[i][j])) 6425 { 6426 int x = bool_get_uint(bool, 7); 6427 hdr->mv_probs[i][j] = x ? x << 1 : 1; 6428 } 6429 } 6430 } 6432 static void 6433 decode_reference_header(struct vp8_decoder_ctx *ctx, 6434 struct bool_decoder *bool, 6435 struct vp8_reference_hdr *hdr) 6436 { 6437 unsigned int key = ctx->frame_hdr.is_keyframe; 6439 hdr->refresh_gf = key ? 1 : bool_get_bit(bool); 6440 hdr->refresh_arf = key ? 1 : bool_get_bit(bool); 6441 hdr->copy_gf = key ? 0 : !hdr->refresh_gf 6442 ? bool_get_uint(bool, 2) : 0; 6443 hdr->copy_arf = key ? 0 : !hdr->refresh_arf 6444 ? bool_get_uint(bool, 2) : 0; 6445 hdr->sign_bias[GOLDEN_FRAME] = key ? 0 : bool_get_bit(bool); 6446 hdr->sign_bias[ALTREF_FRAME] = key ? 0 : bool_get_bit(bool); 6447 hdr->refresh_entropy = bool_get_bit(bool); 6448 hdr->refresh_last = key ? 1 : bool_get_bit(bool); 6449 } 6451 static void 6452 decode_quantizer_header(struct vp8_decoder_ctx *ctx, 6453 struct bool_decoder *bool, 6454 struct vp8_quant_hdr *hdr) 6455 { 6456 int update; 6457 int last_q = hdr->q_index; 6459 hdr->q_index = bool_get_uint(bool, 7); 6460 update = last_q != hdr->q_index; 6461 update |= (hdr->y1_dc_delta_q = bool_maybe_get_int(bool, 4)); 6462 update |= (hdr->y2_dc_delta_q = bool_maybe_get_int(bool, 4)); 6463 update |= (hdr->y2_ac_delta_q = bool_maybe_get_int(bool, 4)); 6464 update |= (hdr->uv_dc_delta_q = bool_maybe_get_int(bool, 4)); 6465 update |= (hdr->uv_ac_delta_q = bool_maybe_get_int(bool, 4)); 6466 hdr->delta_update = update; 6467 } 6469 static void 6470 decode_and_init_token_partitions(struct vp8_decoder_ctx *ctx, 6471 struct bool_decoder *bool, 6472 const unsigned char *data, 6473 unsigned int sz, 6474 struct vp8_token_hdr *hdr) 6475 { 6476 int i; 6478 hdr->partitions = 1 << bool_get_uint(bool, 2); 6480 if (sz < 3 *(hdr->partitions - 1)) 6481 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6482 "Truncated packet found parsing partition" 6483 " lengths."); 6485 sz -= 3 * (hdr->partitions - 1); 6487 for (i = 0; i < hdr->partitions; i++) 6488 { 6489 if (i < hdr->partitions - 1) 6490 { 6491 hdr->partition_sz[i] = (data[2] << 16) 6492 | (data[1] << 8) | data[0]; 6493 data += 3; 6494 } 6495 else 6496 hdr->partition_sz[i] = sz; 6498 if (sz < hdr->partition_sz[i]) 6499 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6500 "Truncated partition %d", i); 6502 sz -= hdr->partition_sz[i]; 6503 } 6505 for (i = 0; i < ctx->token_hdr.partitions; i++) 6506 { 6507 init_bool_decoder(&ctx->tokens[i].bool, data, 6508 ctx->token_hdr.partition_sz[i]); 6509 data += ctx->token_hdr.partition_sz[i]; 6510 } 6511 } 6513 static void 6514 decode_loopfilter_header(struct vp8_decoder_ctx *ctx, 6515 struct bool_decoder *bool, 6516 struct vp8_loopfilter_hdr *hdr) 6517 { 6518 if (ctx->frame_hdr.is_keyframe) 6519 memset(hdr, 0, sizeof(*hdr)); 6521 hdr->use_simple = bool_get_bit(bool); 6522 hdr->level = bool_get_uint(bool, 6); 6523 hdr->sharpness = bool_get_uint(bool, 3); 6524 hdr->delta_enabled = bool_get_bit(bool); 6526 if (hdr->delta_enabled && bool_get_bit(bool)) 6527 { 6528 int i; 6530 for (i = 0; i < BLOCK_CONTEXTS; i++) 6531 hdr->ref_delta[i] = bool_maybe_get_int(bool, 6); 6533 for (i = 0; i < BLOCK_CONTEXTS; i++) 6534 hdr->mode_delta[i] = bool_maybe_get_int(bool, 6); 6535 } 6536 } 6537 static void 6538 decode_segmentation_header(struct vp8_decoder_ctx *ctx, 6539 struct bool_decoder *bool, 6540 struct vp8_segment_hdr *hdr) 6541 { 6542 if (ctx->frame_hdr.is_keyframe) 6543 memset(hdr, 0, sizeof(*hdr)); 6545 hdr->enabled = bool_get_bit(bool); 6547 if (hdr->enabled) 6548 { 6549 int i; 6551 hdr->update_map = bool_get_bit(bool); 6552 hdr->update_data = bool_get_bit(bool); 6554 if (hdr->update_data) 6555 { 6556 hdr->abs = bool_get_bit(bool); 6558 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6559 hdr->quant_idx[i] = bool_maybe_get_int(bool, 7); 6561 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6562 hdr->lf_level[i] = bool_maybe_get_int(bool, 6); 6563 } 6565 if (hdr->update_map) 6566 { 6567 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) 6568 hdr->tree_probs[i] = bool_get_bit(bool) 6569 ? bool_get_uint(bool, 8) 6570 : 255; 6571 } 6572 } 6573 else 6574 { 6575 hdr->update_map = 0; 6576 hdr->update_data = 0; 6577 } 6578 } 6580 static void 6581 dequant_global_init(struct dequant_factors dqf[MAX_MB_SEGMENTS]) 6582 { 6583 int i; 6584 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6585 dqf[i].quant_idx = -1; 6586 } 6588 static int 6589 clamp_q(int q) 6590 { 6591 if (q < 0) return 0; 6592 else if (q > 127) return 127; 6594 return q; 6595 } 6597 static int 6598 dc_q(int q) 6599 { 6600 return dc_q_lookup[clamp_q(q)]; 6601 } 6603 static int 6604 ac_q(int q) 6605 { 6606 return ac_q_lookup[clamp_q(q)]; 6607 } 6609 static void 6610 dequant_init(struct dequant_factors factors[MAX_MB_SEGMENTS], 6611 const struct vp8_segment_hdr *seg, 6612 const struct vp8_quant_hdr *quant_hdr) 6613 { 6614 int i, q; 6615 struct dequant_factors *dqf = factors; 6617 for (i = 0; i < (seg->enabled ? MAX_MB_SEGMENTS : 1); i++) 6618 { 6619 q = quant_hdr->q_index; 6621 if (seg->enabled) 6622 q = (!seg->abs) ? q + seg->quant_idx[i] 6623 : seg->quant_idx[i]; 6625 if (dqf->quant_idx != q || quant_hdr->delta_update) 6626 { 6627 dqf->factor[TOKEN_BLOCK_Y1][0] = 6628 dc_q(q + quant_hdr->y1_dc_delta_q); 6629 dqf->factor[TOKEN_BLOCK_Y1][1] = 6630 ac_q(q); 6631 dqf->factor[TOKEN_BLOCK_UV][0] = 6632 dc_q(q + quant_hdr->uv_dc_delta_q); 6633 dqf->factor[TOKEN_BLOCK_UV][1] = 6634 ac_q(q + quant_hdr->uv_ac_delta_q); 6635 dqf->factor[TOKEN_BLOCK_Y2][0] = 6636 dc_q(q + quant_hdr->y2_dc_delta_q) * 2; 6637 dqf->factor[TOKEN_BLOCK_Y2][1] = 6638 ac_q(q + quant_hdr->y2_ac_delta_q) * 155 / 100; 6640 if (dqf->factor[TOKEN_BLOCK_Y2][1] < 8) 6641 dqf->factor[TOKEN_BLOCK_Y2][1] = 8; 6643 if (dqf->factor[TOKEN_BLOCK_UV][0] > 132) 6644 dqf->factor[TOKEN_BLOCK_UV][0] = 132; 6646 dqf->quant_idx = q; 6647 } 6649 dqf++; 6650 } 6651 } 6653 static void 6654 decode_frame(struct vp8_decoder_ctx *ctx, 6655 const unsigned char *data, 6656 unsigned int sz) 6657 { 6658 vpx_codec_err_t res; 6659 struct bool_decoder bool; 6660 int i, row, partition; 6662 ctx->saved_entropy_valid = 0; 6664 if ((res = vp8_parse_frame_header(data, sz, &ctx->frame_hdr))) 6665 vpx_internal_error(&ctx->error, res, 6666 "Failed to parse frame header"); 6668 if (ctx->frame_hdr.is_experimental) 6669 vpx_internal_error(&ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6670 "Experimental bitstreams not supported."); 6672 data += FRAME_HEADER_SZ; 6673 sz -= FRAME_HEADER_SZ; 6674 if (ctx->frame_hdr.is_keyframe) 6675 { 6676 data += KEYFRAME_HEADER_SZ; 6677 sz -= KEYFRAME_HEADER_SZ; 6678 ctx->mb_cols = (ctx->frame_hdr.kf.w + 15) / 16; 6679 ctx->mb_rows = (ctx->frame_hdr.kf.h + 15) / 16; 6680 } 6682 /* Start the bitreader for the header/entropy partition */ 6683 init_bool_decoder(&bool, data, ctx->frame_hdr.part0_sz); 6685 /* Skip the colorspace and clamping bits */ 6686 if (ctx->frame_hdr.is_keyframe) 6687 if (bool_get_uint(&bool, 2)) 6688 vpx_internal_error( 6689 &ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6690 "Reserved bits not supported."); 6692 decode_segmentation_header(ctx, &bool, &ctx->segment_hdr); 6693 decode_loopfilter_header(ctx, &bool, &ctx->loopfilter_hdr); 6694 decode_and_init_token_partitions(ctx, 6695 &bool, 6696 data + ctx->frame_hdr.part0_sz, 6697 sz - ctx->frame_hdr.part0_sz, 6698 &ctx->token_hdr); 6699 decode_quantizer_header(ctx, &bool, &ctx->quant_hdr); 6700 decode_reference_header(ctx, &bool, &ctx->reference_hdr); 6702 /* Set keyframe entropy defaults. These get updated on keyframes 6703 * regardless of the refresh_entropy setting. 6704 */ 6705 if (ctx->frame_hdr.is_keyframe) 6706 { 6707 ARRAY_COPY(ctx->entropy_hdr.coeff_probs, 6708 k_default_coeff_probs); 6709 ARRAY_COPY(ctx->entropy_hdr.mv_probs, 6710 k_default_mv_probs); 6711 ARRAY_COPY(ctx->entropy_hdr.y_mode_probs, 6712 k_default_y_mode_probs); 6713 ARRAY_COPY(ctx->entropy_hdr.uv_mode_probs, 6714 k_default_uv_mode_probs); 6715 } 6717 if (!ctx->reference_hdr.refresh_entropy) 6718 { 6719 ctx->saved_entropy = ctx->entropy_hdr; 6720 ctx->saved_entropy_valid = 1; 6721 } 6722 decode_entropy_header(ctx, &bool, &ctx->entropy_hdr); 6724 vp8_dixie_modemv_init(ctx); 6725 vp8_dixie_tokens_init(ctx); 6726 vp8_dixie_predict_init(ctx); 6727 dequant_init(ctx->dequant_factors, &ctx->segment_hdr, 6728 &ctx->quant_hdr); 6730 for (row = 0, partition = 0; row < ctx->mb_rows; row++) 6731 { 6732 vp8_dixie_modemv_process_row( 6733 ctx, &bool, row, 0, ctx->mb_cols); 6734 vp8_dixie_tokens_process_row(ctx, partition, row, 0, 6735 ctx->mb_cols); 6736 vp8_dixie_predict_process_row(ctx, row, 0, ctx->mb_cols); 6738 if (ctx->loopfilter_hdr.level && row) 6739 vp8_dixie_loopfilter_process_row(ctx, row - 1, 0, 6740 ctx->mb_cols); 6742 if (++partition == ctx->token_hdr.partitions) 6743 partition = 0; 6744 } 6746 if (ctx->loopfilter_hdr.level) 6747 vp8_dixie_loopfilter_process_row( 6748 ctx, row - 1, 0, ctx->mb_cols); 6750 ctx->frame_cnt++; 6752 if (!ctx->reference_hdr.refresh_entropy) 6753 { 6754 ctx->entropy_hdr = ctx->saved_entropy; 6755 ctx->saved_entropy_valid = 0; 6756 } 6758 /* Handle reference frame updates */ 6759 if (ctx->reference_hdr.copy_arf == 1) 6760 { 6761 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6762 ctx->ref_frames[ALTREF_FRAME] = 6763 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6764 } 6765 else if (ctx->reference_hdr.copy_arf == 2) 6766 { 6767 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6768 ctx->ref_frames[ALTREF_FRAME] = 6769 vp8_dixie_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6771 } 6773 if (ctx->reference_hdr.copy_gf == 1) 6774 { 6775 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6776 ctx->ref_frames[GOLDEN_FRAME] = 6777 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6778 } 6779 else if (ctx->reference_hdr.copy_gf == 2) 6780 { 6781 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6782 ctx->ref_frames[GOLDEN_FRAME] = 6783 vp8_dixie_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6784 } 6786 if (ctx->reference_hdr.refresh_gf) 6787 { 6788 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6789 ctx->ref_frames[GOLDEN_FRAME] = 6790 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6791 } 6793 if (ctx->reference_hdr.refresh_arf) 6794 { 6795 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6796 ctx->ref_frames[ALTREF_FRAME] = 6797 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6798 } 6800 if (ctx->reference_hdr.refresh_last) 6801 { 6802 vp8_dixie_release_ref_frame(ctx->ref_frames[LAST_FRAME]); 6803 ctx->ref_frames[LAST_FRAME] = 6804 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6805 } 6807 } 6809 void 6810 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx) 6811 { 6812 dequant_global_init(ctx->dequant_factors); 6813 } 6815 #define CHECK_FOR_UPDATE(lval,rval,update_flag) do {\ 6816 unsigned int old = lval; \ 6817 update_flag |= (old != (lval = rval)); \ 6818 } while(0) 6820 vpx_codec_err_t 6821 vp8_parse_frame_header(const unsigned char *data, 6822 unsigned int sz, 6823 struct vp8_frame_hdr *hdr) 6824 { 6825 unsigned long raw; 6827 if (sz < 10) 6828 return VPX_CODEC_CORRUPT_FRAME; 6830 /* The frame header is defined as a three byte little endian 6831 * value 6832 */ 6833 raw = data[0] | (data[1] << 8) | (data[2] << 16); 6834 hdr->is_keyframe = !BITS_GET(raw, 0, 1); 6835 hdr->version = BITS_GET(raw, 1, 2); 6836 hdr->is_experimental = BITS_GET(raw, 3, 1); 6837 hdr->is_shown = BITS_GET(raw, 4, 1); 6838 hdr->part0_sz = BITS_GET(raw, 5, 19); 6840 if (sz <= hdr->part0_sz + (hdr->is_keyframe ? 10 : 3)) 6841 return VPX_CODEC_CORRUPT_FRAME; 6843 hdr->frame_size_updated = 0; 6845 if (hdr->is_keyframe) 6846 { 6847 unsigned int update = 0; 6849 /* Keyframe header consists of a three byte sync code 6850 * followed by the width and height and associated scaling 6851 * factors. 6852 */ 6853 if (data[3] != 0x9d || data[4] != 0x01 || data[5] != 0x2a) 6854 return VPX_CODEC_UNSUP_BITSTREAM; 6856 raw = data[6] | (data[7] << 8) 6857 | (data[8] << 16) | (data[9] << 24); 6858 CHECK_FOR_UPDATE(hdr->kf.w, BITS_GET(raw, 0, 14), 6859 update); 6860 CHECK_FOR_UPDATE(hdr->kf.scale_w, BITS_GET(raw, 14, 2), 6861 update); 6862 CHECK_FOR_UPDATE(hdr->kf.h, BITS_GET(raw, 16, 14), 6863 update); 6864 CHECK_FOR_UPDATE(hdr->kf.scale_h, BITS_GET(raw, 30, 2), 6865 update); 6867 hdr->frame_size_updated = update; 6869 if (!hdr->kf.w || !hdr->kf.h) 6870 return VPX_CODEC_UNSUP_BITSTREAM; 6871 } 6873 return VPX_CODEC_OK; 6874 } 6876 vpx_codec_err_t 6877 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 6878 const unsigned char *data, 6879 unsigned int sz) 6880 { 6881 volatile struct vp8_decoder_ctx *ctx_ = ctx; 6883 ctx->error.error_code = VPX_CODEC_OK; 6884 ctx->error.has_detail = 0; 6886 if (!setjmp(ctx->error.jmp)) 6887 decode_frame(ctx, data, sz); 6889 return ctx_->error.error_code; 6890 } 6892 void 6893 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx) 6894 { 6895 vp8_dixie_predict_destroy(ctx); 6896 vp8_dixie_tokens_destroy(ctx); 6897 vp8_dixie_modemv_destroy(ctx); 6898 } 6900 ---- End code block ---------------------------------------- 6902 20.5. dixie.h 6904 ---- Begin code block -------------------------------------- 6906 /* 6907 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6908 * 6909 * Use of this source code is governed by a BSD-style license 6910 * that can be found in the LICENSE file in the root of the source 6911 * tree. An additional intellectual property rights grant can be 6912 * found in the file PATENTS. All contributing project authors may 6913 * be found in the AUTHORS file in the root of the source tree. 6914 */ 6915 #ifndef DIXIE_H 6916 #define DIXIE_H 6917 #include "vpx_codec_internal.h" 6918 #include "bool_decoder.h" 6920 struct vp8_frame_hdr 6921 { 6922 unsigned int is_keyframe; /* Frame is a keyframe */ 6923 unsigned int is_experimental; /* Frame is a keyframe */ 6924 unsigned int version; /* Bitstream version */ 6925 unsigned int is_shown; /* Frame is to be displayed. */ 6926 unsigned int part0_sz; /* Partition 0 length, in bytes */ 6928 struct vp8_kf_hdr 6929 { 6930 unsigned int w; /* Width */ 6931 unsigned int h; /* Height */ 6932 unsigned int scale_w; /* Scaling factor, Width */ 6933 unsigned int scale_h; /* Scaling factor, Height */ 6934 } kf; 6936 unsigned int frame_size_updated; /* Flag to indicate a resolution 6937 * update. 6938 */ 6939 }; 6941 enum 6942 { 6943 MB_FEATURE_TREE_PROBS = 3, 6944 MAX_MB_SEGMENTS = 4 6945 }; 6947 struct vp8_segment_hdr 6948 { 6949 unsigned int enabled; 6950 unsigned int update_data; 6951 unsigned int update_map; 6952 unsigned int abs; /* 0=deltas, 1=absolute values */ 6953 unsigned int tree_probs[MB_FEATURE_TREE_PROBS]; 6954 int lf_level[MAX_MB_SEGMENTS]; 6955 int quant_idx[MAX_MB_SEGMENTS]; 6957 }; 6959 enum 6960 { 6961 BLOCK_CONTEXTS = 4 6962 }; 6964 struct vp8_loopfilter_hdr 6965 { 6966 unsigned int use_simple; 6967 unsigned int level; 6968 unsigned int sharpness; 6969 unsigned int delta_enabled; 6970 int ref_delta[BLOCK_CONTEXTS]; 6971 int mode_delta[BLOCK_CONTEXTS]; 6972 }; 6974 enum 6975 { 6976 MAX_PARTITIONS = 8 6977 }; 6979 struct vp8_token_hdr 6980 { 6981 unsigned int partitions; 6982 unsigned int partition_sz[MAX_PARTITIONS]; 6983 }; 6985 struct vp8_quant_hdr 6986 { 6987 unsigned int q_index; 6988 int delta_update; 6989 int y1_dc_delta_q; 6990 int y2_dc_delta_q; 6991 int y2_ac_delta_q; 6992 int uv_dc_delta_q; 6993 int uv_ac_delta_q; 6994 }; 6996 struct vp8_reference_hdr 6997 { 6998 unsigned int refresh_last; 6999 unsigned int refresh_gf; 7000 unsigned int refresh_arf; 7001 unsigned int copy_gf; 7002 unsigned int copy_arf; 7003 unsigned int sign_bias[4]; 7004 unsigned int refresh_entropy; 7005 }; 7007 enum 7008 { 7009 BLOCK_TYPES = 4, 7010 PREV_COEF_CONTEXTS = 3, 7011 COEF_BANDS = 8, 7012 ENTROPY_NODES = 11, 7013 }; 7014 typedef unsigned char coeff_probs_table_t[BLOCK_TYPES][COEF_BANDS] 7015 [PREV_COEF_CONTEXTS] 7016 [ENTROPY_NODES]; 7018 enum 7019 { 7020 MV_PROB_CNT = 2 + 8 - 1 + 10 /* from entropymv.h */ 7021 }; 7022 typedef unsigned char mv_component_probs_t[MV_PROB_CNT]; 7024 struct vp8_entropy_hdr 7025 { 7026 coeff_probs_table_t coeff_probs; 7027 mv_component_probs_t mv_probs[2]; 7028 unsigned int coeff_skip_enabled; 7029 unsigned char coeff_skip_prob; 7030 unsigned char y_mode_probs[4]; 7031 unsigned char uv_mode_probs[3]; 7032 unsigned char prob_inter; 7033 unsigned char prob_last; 7034 unsigned char prob_gf; 7035 }; 7037 enum reference_frame 7038 { 7039 CURRENT_FRAME, 7040 LAST_FRAME, 7041 GOLDEN_FRAME, 7042 ALTREF_FRAME, 7043 NUM_REF_FRAMES 7045 }; 7047 enum prediction_mode 7048 { 7049 /* 16x16 intra modes */ 7050 DC_PRED, V_PRED, H_PRED, TM_PRED, B_PRED, 7052 /* 16x16 inter modes */ 7053 NEARESTMV, NEARMV, ZEROMV, NEWMV, SPLITMV, 7055 MB_MODE_COUNT, 7057 /* 4x4 intra modes */ 7058 B_DC_PRED = 0, B_TM_PRED, B_VE_PRED, B_HE_PRED, B_LD_PRED, 7059 B_RD_PRED, B_VR_PRED, B_VL_PRED, B_HD_PRED, B_HU_PRED, 7061 /* 4x4 inter modes */ 7062 LEFT4X4, ABOVE4X4, ZERO4X4, NEW4X4, 7064 B_MODE_COUNT 7065 }; 7067 enum splitmv_partitioning 7068 { 7069 SPLITMV_16X8, 7070 SPLITMV_8X16, 7071 SPLITMV_8X8, 7072 SPLITMV_4X4 7073 }; 7075 typedef short filter_t[6]; 7077 typedef union mv 7078 { 7079 struct 7080 { 7081 int16_t x, y; 7082 } d; 7083 uint32_t raw; 7084 } mv_t; 7086 struct mb_base_info 7087 { 7088 unsigned char y_mode : 4; 7089 unsigned char uv_mode : 4; 7090 unsigned char segment_id : 2; 7091 unsigned char ref_frame : 2; 7092 unsigned char skip_coeff : 1; 7093 unsigned char need_mc_border : 1; 7094 enum splitmv_partitioning partitioning : 2; 7095 union mv mv; 7096 unsigned int eob_mask; 7097 }; 7099 struct mb_info 7100 { 7101 struct mb_base_info base; 7102 union 7103 { 7104 union mv mvs[16]; 7105 enum prediction_mode modes[16]; 7106 } split; 7107 }; 7109 /* A "token entropy context" has 4 Y values, 2 U, 2 V, and 1 Y2 */ 7110 typedef int token_entropy_ctx_t[4 + 2 + 2 + 1]; 7112 struct token_decoder 7113 { 7114 struct bool_decoder bool; 7115 token_entropy_ctx_t left_token_entropy_ctx; 7116 short *coeffs; 7117 }; 7119 enum token_block_type 7120 { 7121 TOKEN_BLOCK_Y1, 7122 TOKEN_BLOCK_UV, 7123 TOKEN_BLOCK_Y2, 7124 TOKEN_BLOCK_TYPES, 7125 }; 7127 struct dequant_factors 7128 { 7129 int quant_idx; 7130 short factor[TOKEN_BLOCK_TYPES][2]; /* [ Y1, UV, Y2 ] 7131 * [ DC, AC ] */ 7132 }; 7133 struct ref_cnt_img 7134 { 7135 vpx_image_t img; 7136 unsigned int ref_cnt; 7137 }; 7139 struct vp8_decoder_ctx 7140 { 7141 struct vpx_internal_error_info error; 7142 unsigned int frame_cnt; 7144 struct vp8_frame_hdr frame_hdr; 7145 struct vp8_segment_hdr segment_hdr; 7146 struct vp8_loopfilter_hdr loopfilter_hdr; 7147 struct vp8_token_hdr token_hdr; 7148 struct vp8_quant_hdr quant_hdr; 7149 struct vp8_reference_hdr reference_hdr; 7150 struct vp8_entropy_hdr entropy_hdr; 7152 struct vp8_entropy_hdr saved_entropy; 7153 unsigned int saved_entropy_valid; 7155 unsigned int mb_rows; 7156 unsigned int mb_cols; 7157 struct mb_info *mb_info_storage; 7158 struct mb_info **mb_info_rows_storage; 7159 struct mb_info **mb_info_rows; 7161 token_entropy_ctx_t *above_token_entropy_ctx; 7162 struct token_decoder tokens[MAX_PARTITIONS]; 7163 struct dequant_factors dequant_factors[MAX_MB_SEGMENTS]; 7165 struct ref_cnt_img frame_strg[NUM_REF_FRAMES]; 7166 struct ref_cnt_img *ref_frames[NUM_REF_FRAMES]; 7167 ptrdiff_t ref_frame_offsets[4]; 7169 const filter_t *subpixel_filters; 7170 }; 7172 void 7173 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx); 7175 void 7176 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx); 7177 vpx_codec_err_t 7178 vp8_parse_frame_header(const unsigned char *data, 7179 unsigned int sz, 7180 struct vp8_frame_hdr *hdr); 7182 vpx_codec_err_t 7183 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 7184 const unsigned char *data, 7185 unsigned int sz); 7187 #define CLAMP_255(x) ((x)<0?0:((x)>255?255:(x))) 7189 #endif 7191 ---- End code block ---------------------------------------- 7193 20.6. dixie_loopfilter.c 7195 ---- Begin code block -------------------------------------- 7197 /* 7198 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7199 * 7200 * Use of this source code is governed by a BSD-style license 7201 * that can be found in the LICENSE file in the root of the source 7202 * tree. An additional intellectual property rights grant can be 7203 * found in the file PATENTS. All contributing project authors may 7204 * be found in the AUTHORS file in the root of the source tree. 7205 */ 7206 #include "dixie.h" 7207 #include "dixie_loopfilter.h" 7209 #define ABS(x) ((x) >= 0 ? (x) : -(x)) 7211 #define p3 pixels[-4*stride] 7212 #define p2 pixels[-3*stride] 7213 #define p1 pixels[-2*stride] 7214 #define p0 pixels[-1*stride] 7215 #define q0 pixels[ 0*stride] 7216 #define q1 pixels[ 1*stride] 7217 #define q2 pixels[ 2*stride] 7218 #define q3 pixels[ 3*stride] 7220 #define static 7221 static int 7222 saturate_int8(int x) 7223 { 7224 if (x < -128) 7225 return -128; 7227 if (x > 127) 7228 return 127; 7230 return x; 7231 } 7233 static int 7234 saturate_uint8(int x) 7235 { 7236 if (x < 0) 7237 return 0; 7239 if (x > 255) 7240 return 255; 7242 return x; 7243 } 7245 static int 7246 high_edge_variance(unsigned char *pixels, 7247 int stride, 7248 int hev_threshold) 7249 { 7250 return ABS(p1 - p0) > hev_threshold || 7251 ABS(q1 - q0) > hev_threshold; 7252 } 7254 static int 7255 simple_threshold(unsigned char *pixels, 7256 int stride, 7257 int filter_limit) 7258 { 7259 return (ABS(p0 - q0) * 2 + (ABS(p1 - q1) >> 1)) <= filter_limit; 7260 } 7262 static int 7263 normal_threshold(unsigned char *pixels, 7264 int stride, 7265 int edge_limit, 7266 int interior_limit) 7267 { 7268 int E = edge_limit; 7269 int I = interior_limit; 7271 return simple_threshold(pixels, stride, 2 * E + I) 7272 && ABS(p3 - p2) <= I && ABS(p2 - p1) <= I 7273 && ABS(p1 - p0) <= I && ABS(q3 - q2) <= I 7274 && ABS(q2 - q1) <= I && ABS(q1 - q0) <= I; 7275 } 7277 static void 7278 filter_common(unsigned char *pixels, 7279 int stride, 7280 int use_outer_taps) 7281 { 7282 int a, f1, f2; 7284 a = 3 * (q0 - p0); 7286 if (use_outer_taps) 7287 a += saturate_int8(p1 - q1); 7289 a = saturate_int8(a); 7291 f1 = ((a + 4 > 127) ? 127 : a + 4) >> 3; 7292 f2 = ((a + 3 > 127) ? 127 : a + 3) >> 3; 7294 p0 = saturate_uint8(p0 + f2); 7295 q0 = saturate_uint8(q0 - f1); 7297 if (!use_outer_taps) 7298 { 7299 /* This handles the case of subblock_filter() 7300 * (from the bitstream guide. 7301 */ 7302 a = (f1 + 1) >> 1; 7303 p1 = saturate_uint8(p1 + a); 7304 q1 = saturate_uint8(q1 - a); 7305 } 7306 } 7308 static void 7309 filter_mb_edge(unsigned char *pixels, 7310 int stride) 7311 { 7312 int w, a; 7314 w = saturate_int8(saturate_int8(p1 - q1) + 3 * (q0 - p0)); 7316 a = (27 * w + 63) >> 7; 7317 p0 = saturate_uint8(p0 + a); 7318 q0 = saturate_uint8(q0 - a); 7320 a = (18 * w + 63) >> 7; 7321 p1 = saturate_uint8(p1 + a); 7322 q1 = saturate_uint8(q1 - a); 7324 a = (9 * w + 63) >> 7; 7325 p2 = saturate_uint8(p2 + a); 7326 q2 = saturate_uint8(q2 - a); 7328 } 7330 static void 7331 filter_mb_v_edge(unsigned char *src, 7332 int stride, 7333 int edge_limit, 7334 int interior_limit, 7335 int hev_threshold, 7336 int size) 7337 { 7338 int i; 7340 for (i = 0; i < 8 * size; i++) 7341 { 7342 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7343 { 7344 if (high_edge_variance(src, 1, hev_threshold)) 7345 filter_common(src, 1, 1); 7346 else 7347 filter_mb_edge(src, 1); 7348 } 7350 src += stride; 7351 } 7352 } 7354 static void 7355 filter_subblock_v_edge(unsigned char *src, 7356 int stride, 7357 int edge_limit, 7358 int interior_limit, 7359 int hev_threshold, 7360 int size) 7361 { 7362 int i; 7364 for (i = 0; i < 8 * size; i++) 7365 { 7366 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7367 filter_common(src, 1, 7368 high_edge_variance(src, 1, hev_threshold)); 7370 src += stride; 7371 } 7372 } 7374 static void 7375 filter_mb_h_edge(unsigned char *src, 7376 int stride, 7377 int edge_limit, 7378 int interior_limit, 7379 int hev_threshold, 7380 int size) 7381 { 7382 int i; 7384 for (i = 0; i < 8 * size; i++) 7385 { 7386 if (normal_threshold(src, stride, edge_limit, interior_limit)) 7387 { 7388 if (high_edge_variance(src, stride, hev_threshold)) 7389 filter_common(src, stride, 1); 7390 else 7391 filter_mb_edge(src, stride); 7392 } 7394 src += 1; 7395 } 7396 } 7398 static void 7399 filter_subblock_h_edge(unsigned char *src, 7400 int stride, 7401 int edge_limit, 7402 int interior_limit, 7403 int hev_threshold, 7404 int size) 7405 { 7406 int i; 7408 for (i = 0; i < 8 * size; i++) 7409 { 7410 if (normal_threshold(src, stride, edge_limit, interior_limit)) 7411 filter_common(src, stride, 7412 high_edge_variance(src, stride, 7413 hev_threshold)); 7415 src += 1; 7416 } 7417 } 7419 static void 7420 filter_v_edge_simple(unsigned char *src, 7421 int stride, 7422 int filter_limit) 7423 { 7424 int i; 7426 for (i = 0; i < 16; i++) 7427 { 7428 if (simple_threshold(src, 1, filter_limit)) 7429 filter_common(src, 1, 1); 7431 src += stride; 7432 } 7433 } 7435 static void 7436 filter_h_edge_simple(unsigned char *src, 7437 int stride, 7438 int filter_limit) 7439 { 7440 int i; 7442 for (i = 0; i < 16; i++) 7443 { 7444 if (simple_threshold(src, stride, filter_limit)) 7445 filter_common(src, stride, 1); 7447 src += 1; 7448 } 7449 } 7450 static void 7451 calculate_filter_parameters(struct vp8_decoder_ctx *ctx, 7452 struct mb_info *mbi, 7453 int *edge_limit_, 7454 int *interior_limit_, 7455 int *hev_threshold_) 7456 { 7457 int filter_level, interior_limit, hev_threshold; 7459 /* Reference code/spec seems to conflate filter_level and 7460 * edge_limit 7461 */ 7463 filter_level = ctx->loopfilter_hdr.level; 7465 if (ctx->segment_hdr.enabled) 7466 { 7467 if (!ctx->segment_hdr.abs) 7468 filter_level += 7469 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7470 else 7471 filter_level = 7472 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7473 } 7475 if (ctx->loopfilter_hdr.delta_enabled) 7476 { 7477 filter_level += 7478 ctx->loopfilter_hdr.ref_delta[mbi->base.ref_frame]; 7480 if (mbi->base.ref_frame == CURRENT_FRAME) 7481 { 7482 if (mbi->base.y_mode == B_PRED) 7483 filter_level += ctx->loopfilter_hdr.mode_delta[0]; 7484 } 7485 else if (mbi->base.y_mode == ZEROMV) 7486 filter_level += ctx->loopfilter_hdr.mode_delta[1]; 7487 else if (mbi->base.y_mode == SPLITMV) 7488 filter_level += ctx->loopfilter_hdr.mode_delta[3]; 7489 else 7490 filter_level += ctx->loopfilter_hdr.mode_delta[2]; 7491 } 7493 if (filter_level > 63) 7494 filter_level = 63; 7495 else if (filter_level < 0) 7496 filter_level = 0; 7498 interior_limit = filter_level; 7500 if (ctx->loopfilter_hdr.sharpness) 7501 { 7502 interior_limit >>= ctx->loopfilter_hdr.sharpness > 4 ? 2 : 1; 7504 if (interior_limit > 9 - ctx->loopfilter_hdr.sharpness) 7505 interior_limit = 9 - ctx->loopfilter_hdr.sharpness; 7506 } 7508 if (interior_limit < 1) 7509 interior_limit = 1; 7511 hev_threshold = (filter_level >= 15); 7513 if (filter_level >= 40) 7514 hev_threshold++; 7516 if (filter_level >= 20 && !ctx->frame_hdr.is_keyframe) 7517 hev_threshold++; 7519 *edge_limit_ = filter_level; 7520 *interior_limit_ = interior_limit; 7521 *hev_threshold_ = hev_threshold; 7522 } 7524 static void 7525 filter_row_normal(struct vp8_decoder_ctx *ctx, 7526 unsigned int row, 7527 unsigned int start_col, 7528 unsigned int num_cols) 7529 { 7530 unsigned char *y, *u, *v; 7531 int stride, uv_stride; 7532 struct mb_info *mbi; 7533 unsigned int col; 7535 /* Adjust pointers based on row, start_col */ 7536 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7537 uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 7538 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7539 u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 7540 v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 7541 y += (stride * row + start_col) * 16; 7542 u += (uv_stride * row + start_col) * 8; 7543 v += (uv_stride * row + start_col) * 8; 7544 mbi = ctx->mb_info_rows[row] + start_col; 7545 for (col = start_col; col < start_col + num_cols; col++) 7546 { 7547 int edge_limit, interior_limit, hev_threshold; 7549 /* TODO: only need to recalculate every MB if segmentation is 7550 * enabled. 7551 */ 7552 calculate_filter_parameters(ctx, mbi, &edge_limit, 7553 &interior_limit, &hev_threshold); 7555 if (edge_limit) 7556 { 7557 if (col) 7558 { 7559 filter_mb_v_edge(y, stride, edge_limit + 2, 7560 interior_limit, hev_threshold, 2); 7561 filter_mb_v_edge(u, uv_stride, edge_limit + 2, 7562 interior_limit, hev_threshold, 1); 7563 filter_mb_v_edge(v, uv_stride, edge_limit + 2, 7564 interior_limit, hev_threshold, 1); 7565 } 7567 /* NOTE: This conditional is actually dependent on the 7568 * number of coefficients decoded, not the skip flag as 7569 * coded in the bitstream. The tokens task is expected to 7570 * set 31 if there is *any* non-zero data. 7571 */ 7572 if (mbi->base.eob_mask 7573 || mbi->base.y_mode == SPLITMV 7574 || mbi->base.y_mode == B_PRED) 7575 { 7576 filter_subblock_v_edge(y + 4, stride, edge_limit, 7577 interior_limit, hev_threshold, 7578 2); 7579 filter_subblock_v_edge(y + 8, stride, edge_limit, 7580 interior_limit, hev_threshold, 7581 2); 7582 filter_subblock_v_edge(y + 12, stride, edge_limit, 7583 interior_limit, hev_threshold, 7584 2); 7585 filter_subblock_v_edge(u + 4, uv_stride, edge_limit, 7586 interior_limit, hev_threshold, 7587 1); 7588 filter_subblock_v_edge(v + 4, uv_stride, edge_limit, 7589 interior_limit, hev_threshold, 7590 1); 7591 } 7592 if (row) 7593 { 7594 filter_mb_h_edge(y, stride, edge_limit + 2, 7595 interior_limit, hev_threshold, 2); 7596 filter_mb_h_edge(u, uv_stride, edge_limit + 2, 7597 interior_limit, hev_threshold, 1); 7598 filter_mb_h_edge(v, uv_stride, edge_limit + 2, 7599 interior_limit, hev_threshold, 1); 7600 } 7602 if (mbi->base.eob_mask 7603 || mbi->base.y_mode == SPLITMV 7604 || mbi->base.y_mode == B_PRED) 7605 { 7606 filter_subblock_h_edge(y + 4 * stride, stride, 7607 edge_limit, interior_limit, 7608 hev_threshold, 2); 7609 filter_subblock_h_edge(y + 8 * stride, stride, 7610 edge_limit, interior_limit, 7611 hev_threshold, 2); 7612 filter_subblock_h_edge(y + 12 * stride, stride, 7613 edge_limit, interior_limit, 7614 hev_threshold, 2); 7615 filter_subblock_h_edge(u + 4 * uv_stride, uv_stride, 7616 edge_limit, interior_limit, 7617 hev_threshold, 1); 7618 filter_subblock_h_edge(v + 4 * uv_stride, uv_stride, 7619 edge_limit, interior_limit, 7620 hev_threshold, 1); 7621 } 7622 } 7624 y += 16; 7625 u += 8; 7626 v += 8; 7627 mbi++; 7628 } 7629 } 7631 static void 7632 filter_row_simple(struct vp8_decoder_ctx *ctx, 7633 unsigned int row, 7634 unsigned int start_col, 7635 unsigned int num_cols) 7636 { 7637 unsigned char *y; 7638 int stride; 7639 struct mb_info *mbi; 7640 unsigned int col; 7642 /* Adjust pointers based on row, start_col */ 7643 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7644 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7645 y += (stride * row + start_col) * 16; 7646 mbi = ctx->mb_info_rows[row] + start_col; 7648 for (col = start_col; col < start_col + num_cols; col++) 7649 { 7650 int edge_limit, interior_limit, hev_threshold; 7652 /* TODO: only need to recalculate every MB if segmentation is 7653 * enabled. 7654 */ 7655 calculate_filter_parameters(ctx, mbi, &edge_limit, 7656 &interior_limit, &hev_threshold); 7658 if (edge_limit) 7659 { 7661 /* NOTE: This conditional is actually dependent on the 7662 * number of coefficients decoded, not the skip flag as 7663 * coded in the bitstream. The tokens task is expected to 7664 * set 31 if there is *any* non-zero data. 7665 */ 7666 int filter_subblocks = (mbi->base.eob_mask 7667 || mbi->base.y_mode == SPLITMV 7668 || mbi->base.y_mode == B_PRED); 7669 int mb_limit = (edge_limit + 2) * 2 + interior_limit; 7670 int b_limit = edge_limit * 2 + interior_limit; 7672 if (col) 7673 filter_v_edge_simple(y, stride, mb_limit); 7675 if (filter_subblocks) 7676 { 7677 filter_v_edge_simple(y + 4, stride, b_limit); 7678 filter_v_edge_simple(y + 8, stride, b_limit); 7679 filter_v_edge_simple(y + 12, stride, b_limit); 7680 } 7682 if (row) 7683 filter_h_edge_simple(y, stride, mb_limit); 7685 if (filter_subblocks) 7686 { 7687 filter_h_edge_simple(y + 4 * stride, stride, b_limit); 7688 filter_h_edge_simple(y + 8 * stride, stride, b_limit); 7689 filter_h_edge_simple(y + 12 * stride, stride, b_limit); 7690 } 7691 } 7693 y += 16; 7694 mbi++; 7695 } 7696 } 7698 void 7699 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7700 unsigned int row, 7701 unsigned int start_col, 7702 unsigned int num_cols) 7703 { 7704 if (ctx->loopfilter_hdr.use_simple) 7705 filter_row_simple(ctx, row, start_col, num_cols); 7706 else 7707 filter_row_normal(ctx, row, start_col, num_cols); 7708 } 7710 ---- End code block ---------------------------------------- 7712 20.7. dixie_loopfilter.h 7714 ---- Begin code block -------------------------------------- 7716 /* 7717 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7718 * 7719 * Use of this source code is governed by a BSD-style license 7720 * that can be found in the LICENSE file in the root of the source 7721 * tree. An additional intellectual property rights grant can be 7722 * found in the file PATENTS. All contributing project authors may 7723 * be found in the AUTHORS file in the root of the source tree. 7724 */ 7725 #ifndef DIXIE_LOOPFILTER_H 7726 #define DIXIE_LOOPFILTER_H 7728 void 7729 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7730 unsigned int row, 7731 unsigned int start_col, 7732 unsigned int num_cols); 7734 #endif 7736 ---- End code block ---------------------------------------- 7738 20.8. idct_add.c 7740 ---- Begin code block -------------------------------------- 7742 /* 7743 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7744 * 7745 * Use of this source code is governed by a BSD-style license 7746 * that can be found in the LICENSE file in the root of the source 7747 * tree. An additional intellectual property rights grant can be 7748 * found in the file PATENTS. All contributing project authors may 7749 * be found in the AUTHORS file in the root of the source tree. 7750 */ 7751 #include "dixie.h" 7752 #include "idct_add.h" 7753 #include 7755 void 7756 vp8_dixie_walsh(const short *input, short *output) 7757 { 7758 int i; 7759 int a1, b1, c1, d1; 7760 int a2, b2, c2, d2; 7761 const short *ip = input; 7762 short *op = output; 7764 for (i = 0; i < 4; i++) 7765 { 7766 a1 = ip[0] + ip[12]; 7767 b1 = ip[4] + ip[8]; 7768 c1 = ip[4] - ip[8]; 7769 d1 = ip[0] - ip[12]; 7771 op[0] = a1 + b1; 7772 op[4] = c1 + d1; 7773 op[8] = a1 - b1; 7774 op[12] = d1 - c1; 7775 ip++; 7776 op++; 7777 } 7779 ip = output; 7780 op = output; 7782 for (i = 0; i < 4; i++) 7783 { 7784 a1 = ip[0] + ip[3]; 7785 b1 = ip[1] + ip[2]; 7786 c1 = ip[1] - ip[2]; 7787 d1 = ip[0] - ip[3]; 7789 a2 = a1 + b1; 7790 b2 = c1 + d1; 7791 c2 = a1 - b1; 7792 d2 = d1 - c1; 7794 op[0] = (a2 + 3) >> 3; 7795 op[1] = (b2 + 3) >> 3; 7796 op[2] = (c2 + 3) >> 3; 7797 op[3] = (d2 + 3) >> 3; 7799 ip += 4; 7800 op += 4; 7801 } 7802 } 7804 #define cospi8sqrt2minus1 20091 7805 #define sinpi8sqrt2 35468 7806 #define rounding 0 7807 static void 7808 idct_columns(const short *input, short *output) 7809 { 7810 int i; 7811 int a1, b1, c1, d1; 7813 const short *ip = input; 7814 short *op = output; 7815 int temp1, temp2; 7816 int shortpitch = 4; 7818 for (i = 0; i < 4; i++) 7819 { 7820 a1 = ip[0] + ip[8]; 7821 b1 = ip[0] - ip[8]; 7823 temp1 = (ip[4] * sinpi8sqrt2 + rounding) >> 16; 7824 temp2 = ip[12] + 7825 ((ip[12] * cospi8sqrt2minus1 + rounding) >> 16); 7826 c1 = temp1 - temp2; 7828 temp1 = ip[4] + 7829 ((ip[4] * cospi8sqrt2minus1 + rounding) >> 16); 7830 temp2 = (ip[12] * sinpi8sqrt2 + rounding) >> 16; 7831 d1 = temp1 + temp2; 7833 op[shortpitch*0] = a1 + d1; 7834 op[shortpitch*3] = a1 - d1; 7836 op[shortpitch*1] = b1 + c1; 7837 op[shortpitch*2] = b1 - c1; 7839 ip++; 7840 op++; 7841 } 7842 } 7844 void 7845 vp8_dixie_idct_add(unsigned char *recon, 7846 const unsigned char *predict, 7847 int stride, 7848 const short *coeffs) 7849 { 7850 int i; 7851 int a1, b1, c1, d1, temp1, temp2; 7852 short tmp[16]; 7853 idct_columns(coeffs, tmp); 7854 coeffs = tmp; 7856 for (i = 0; i < 4; i++) 7857 { 7858 a1 = coeffs[0] + coeffs[2]; 7859 b1 = coeffs[0] - coeffs[2]; 7861 temp1 = (coeffs[1] * sinpi8sqrt2 + rounding) >> 16; 7862 temp2 = coeffs[3] + 7863 ((coeffs[3] * cospi8sqrt2minus1 + rounding) >> 16); 7864 c1 = temp1 - temp2; 7866 temp1 = coeffs[1] + 7867 ((coeffs[1] * cospi8sqrt2minus1 + rounding) >> 16); 7868 temp2 = (coeffs[3] * sinpi8sqrt2 + rounding) >> 16; 7869 d1 = temp1 + temp2; 7871 recon[0] = CLAMP_255(predict[0] + ((a1 + d1 + 4) >> 3)); 7872 recon[3] = CLAMP_255(predict[3] + ((a1 - d1 + 4) >> 3)); 7873 recon[1] = CLAMP_255(predict[1] + ((b1 + c1 + 4) >> 3)); 7874 recon[2] = CLAMP_255(predict[2] + ((b1 - c1 + 4) >> 3)); 7876 coeffs += 4; 7877 recon += stride; 7878 predict += stride; 7879 } 7880 } 7882 ---- End code block ---------------------------------------- 7884 20.9. idct_add.h 7886 ---- Begin code block -------------------------------------- 7888 /* 7889 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7890 * 7891 * Use of this source code is governed by a BSD-style license 7892 * that can be found in the LICENSE file in the root of the source 7893 * tree. An additional intellectual property rights grant can be 7894 * found in the file PATENTS. All contributing project authors may 7895 * be found in the AUTHORS file in the root of the source tree. 7896 */ 7897 #ifndef IDCT_ADD_H 7898 #define IDCT_ADD_H 7900 void 7901 vp8_dixie_idct_add_init(struct vp8_decoder_ctx *ctx); 7903 void 7904 vp8_dixie_idct_add(unsigned char *recon, 7905 const unsigned char *predict, 7906 int stride, 7907 const short *coeffs); 7909 void 7910 vp8_dixie_walsh(const short *in, short *out); 7912 void 7913 vp8_dixie_idct_add_process_row(struct vp8_decoder_ctx *ctx, 7914 short *coeffs, 7915 unsigned int row, 7916 unsigned int start_col, 7917 unsigned int num_cols); 7919 #endif 7921 ---- End code block ---------------------------------------- 7923 20.10. mem.h 7925 ---- Begin code block -------------------------------------- 7927 /* 7928 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7929 * 7930 * Use of this source code is governed by a BSD-style license 7931 * that can be found in the LICENSE file in the root of the source 7932 * tree. An additional intellectual property rights grant can be 7933 * found in the file PATENTS. All contributing project authors may 7934 * be found in the AUTHORS file in the root of the source tree. 7935 */ 7937 #ifndef VPX_PORTS_MEM_H 7938 #define VPX_PORTS_MEM_H 7939 #include "vpx_config.h" 7940 #include "vpx_integer.h" 7942 #if defined(__GNUC__) && __GNUC__ 7943 #define DECLARE_ALIGNED(n,typ,val) typ val __attribute__ \ 7944 ((aligned (n))) 7945 #elif defined(_MSC_VER) 7946 #define DECLARE_ALIGNED(n,typ,val) __declspec(align(n)) typ val 7947 #else 7948 #warning No alignment directives known for this compiler. 7949 #define DECLARE_ALIGNED(n,typ,val) typ val 7950 #endif 7951 #endif 7953 /* Declare an aligned array on the stack, for situations where the 7954 * stack pointer may not have the alignment we expect. Creates an 7955 * array with a modified name, then defines val to be a pointer, and 7956 * aligns that pointer within the array. 7957 */ 7958 #define DECLARE_ALIGNED_ARRAY(a,typ,val,n)\ 7959 typ val##_[(n)+(a)/sizeof(typ)+1];\ 7960 typ *val = (typ*)((((intptr_t)val##_)+(a)-1)&((intptr_t)-(a))) 7962 /* Indicates that the usage of the specified variable has been 7963 * audited to assure that it's safe to use uninitialized. Silences 7964 * 'may be used uninitialized' warnings on gcc. 7965 */ 7966 #if defined(__GNUC__) && __GNUC__ 7967 #define UNINITIALIZED_IS_SAFE(x) x=x 7968 #else 7969 #define UNINITIALIZED_IS_SAFE(x) x 7970 #endif 7971 ---- End code block ---------------------------------------- 7973 20.11. modemv.c 7975 ---- Begin code block -------------------------------------- 7977 /* 7978 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7979 * 7980 * Use of this source code is governed by a BSD-style license 7981 * that can be found in the LICENSE file in the root of the source 7982 * tree. An additional intellectual property rights grant can be 7983 * found in the file PATENTS. All contributing project authors may 7984 * be found in the AUTHORS file in the root of the source tree. 7985 */ 7986 #include "dixie.h" 7987 #include "modemv_data.h" 7988 #include 7989 #include 7991 struct mv_clamp_rect 7992 { 7993 int to_left, to_right, to_top, to_bottom; 7994 }; 7996 static union mv 7997 clamp_mv(union mv raw, const struct mv_clamp_rect *bounds) 7998 { 7999 union mv newmv; 8001 newmv.d.x = (raw.d.x < bounds->to_left) 8002 ? bounds->to_left : raw.d.x; 8003 newmv.d.x = (raw.d.x > bounds->to_right) 8004 ? bounds->to_right : newmv.d.x; 8005 newmv.d.y = (raw.d.y < bounds->to_top) 8006 ? bounds->to_top : raw.d.y; 8007 newmv.d.y = (raw.d.y > bounds->to_bottom) 8008 ? bounds->to_bottom : newmv.d.y; 8009 return newmv; 8010 } 8012 static int 8013 read_segment_id(struct bool_decoder *bool, 8014 struct vp8_segment_hdr *seg) 8015 { 8016 return bool_get(bool, seg->tree_probs[0]) 8017 ? 2 + bool_get(bool, seg->tree_probs[2]) 8018 : bool_get(bool, seg->tree_probs[1]); 8019 } 8021 static enum prediction_mode 8022 above_block_mode(const struct mb_info *this, 8023 const struct mb_info *above, 8024 unsigned int b) 8025 { 8026 if (b < 4) 8027 { 8028 switch (above->base.y_mode) 8029 { 8030 case DC_PRED: 8031 return B_DC_PRED; 8032 case V_PRED: 8033 return B_VE_PRED; 8034 case H_PRED: 8035 return B_HE_PRED; 8036 case TM_PRED: 8037 return B_TM_PRED; 8038 case B_PRED: 8039 return above->split.modes[b+12]; 8040 default: 8041 assert(0); 8042 } 8043 } 8045 return this->split.modes[b-4]; 8046 } 8048 static enum prediction_mode 8049 left_block_mode(const struct mb_info *this, 8050 const struct mb_info *left, 8051 unsigned int b) 8052 { 8053 if (!(b & 3)) 8054 { 8055 switch (left->base.y_mode) 8056 { 8057 case DC_PRED: 8058 return B_DC_PRED; 8059 case V_PRED: 8060 return B_VE_PRED; 8061 case H_PRED: 8063 return B_HE_PRED; 8064 case TM_PRED: 8065 return B_TM_PRED; 8066 case B_PRED: 8067 return left->split.modes[b+3]; 8068 default: 8069 assert(0); 8070 } 8071 } 8073 return this->split.modes[b-1]; 8074 } 8076 static void 8077 decode_kf_mb_mode(struct mb_info *this, 8078 struct mb_info *left, 8079 struct mb_info *above, 8080 struct bool_decoder *bool) 8081 { 8082 int y_mode, uv_mode; 8084 y_mode = bool_read_tree(bool, kf_y_mode_tree, kf_y_mode_probs); 8086 if (y_mode == B_PRED) 8087 { 8088 unsigned int i; 8090 for (i = 0; i < 16; i++) 8091 { 8092 enum prediction_mode a = above_block_mode(this, above, i); 8093 enum prediction_mode l = left_block_mode(this, left, i); 8094 enum prediction_mode b; 8096 b = bool_read_tree(bool, b_mode_tree, 8097 kf_b_mode_probs[a][l]); 8098 this->split.modes[i] = b; 8099 } 8100 } 8102 uv_mode = bool_read_tree(bool, uv_mode_tree, kf_uv_mode_probs); 8104 this->base.y_mode = y_mode; 8105 this->base.uv_mode = uv_mode; 8106 this->base.mv.raw = 0; 8107 this->base.ref_frame = 0; 8108 } 8109 static void 8110 decode_intra_mb_mode(struct mb_info *this, 8111 struct vp8_entropy_hdr *hdr, 8112 struct bool_decoder *bool) 8113 { 8114 /* Like decode_kf_mb_mode, but with probabilities transmitted in the 8115 * bitstream and no context on the above/left block mode. 8116 */ 8117 int y_mode, uv_mode; 8119 y_mode = bool_read_tree(bool, y_mode_tree, hdr->y_mode_probs); 8121 if (y_mode == B_PRED) 8122 { 8123 unsigned int i; 8125 for (i = 0; i < 16; i++) 8126 { 8127 enum prediction_mode b; 8129 b = bool_read_tree(bool, b_mode_tree, default_b_mode_probs); 8130 this->split.modes[i] = b; 8131 } 8132 } 8134 uv_mode = bool_read_tree(bool, uv_mode_tree, hdr->uv_mode_probs); 8136 this->base.y_mode = y_mode; 8137 this->base.uv_mode = uv_mode; 8138 this->base.mv.raw = 0; 8139 this->base.ref_frame = CURRENT_FRAME; 8140 } 8142 static int 8143 read_mv_component(struct bool_decoder *bool, 8144 const unsigned char mvc[MV_PROB_CNT]) 8145 { 8146 enum {IS_SHORT, SIGN, SHORT, BITS = SHORT + 8 - 1, LONG_WIDTH = 10}; 8147 int x = 0; 8149 if (bool_get(bool, mvc[IS_SHORT])) /* Large */ 8150 { 8151 int i = 0; 8153 for (i = 0; i < 3; i++) 8154 x += bool_get(bool, mvc[BITS + i]) << i; 8156 /* Skip bit 3, which is sometimes implicit */ 8157 for (i = LONG_WIDTH - 1; i > 3; i--) 8158 x += bool_get(bool, mvc[BITS + i]) << i; 8160 if (!(x & 0xFFF0) || bool_get(bool, mvc[BITS + 3])) 8161 x += 8; 8162 } 8163 else /* small */ 8164 x = bool_read_tree(bool, small_mv_tree, mvc + SHORT); 8166 if (x && bool_get(bool, mvc[SIGN])) 8167 x = -x; 8169 return x << 1; 8170 } 8172 static mv_t 8173 above_block_mv(const struct mb_info *this, 8174 const struct mb_info *above, 8175 unsigned int b) 8176 { 8177 if (b < 4) 8178 { 8179 if (above->base.y_mode == SPLITMV) 8180 return above->split.mvs[b+12]; 8182 return above->base.mv; 8183 } 8185 return this->split.mvs[b-4]; 8186 } 8188 static mv_t 8189 left_block_mv(const struct mb_info *this, 8190 const struct mb_info *left, 8191 unsigned int b) 8192 { 8193 if (!(b & 3)) 8194 { 8195 if (left->base.y_mode == SPLITMV) 8196 return left->split.mvs[b+3]; 8198 return left->base.mv; 8199 } 8201 return this->split.mvs[b-1]; 8203 } 8205 static enum prediction_mode 8206 submv_ref(struct bool_decoder *bool, union mv l, union mv a) 8207 { 8208 enum subblock_mv_ref 8209 { 8210 SUBMVREF_NORMAL, 8211 SUBMVREF_LEFT_ZED, 8212 SUBMVREF_ABOVE_ZED, 8213 SUBMVREF_LEFT_ABOVE_SAME, 8214 SUBMVREF_LEFT_ABOVE_ZED 8215 }; 8217 int lez = !(l.raw); 8218 int aez = !(a.raw); 8219 int lea = l.raw == a.raw; 8220 enum subblock_mv_ref ctx = SUBMVREF_NORMAL; 8222 if (lea && lez) 8223 ctx = SUBMVREF_LEFT_ABOVE_ZED; 8224 else if (lea) 8225 ctx = SUBMVREF_LEFT_ABOVE_SAME; 8226 else if (aez) 8227 ctx = SUBMVREF_ABOVE_ZED; 8228 else if (lez) 8229 ctx = SUBMVREF_LEFT_ZED; 8231 return bool_read_tree(bool, submv_ref_tree, submv_ref_probs2[ctx]); 8232 } 8234 static void 8235 read_mv(struct bool_decoder *bool, 8236 union mv *mv, 8237 mv_component_probs_t mvc[2]) 8238 { 8239 mv->d.y = read_mv_component(bool, mvc[0]); 8240 mv->d.x = read_mv_component(bool, mvc[1]); 8241 } 8243 static void 8244 mv_bias(const struct mb_info *mb, 8245 const unsigned int sign_bias[3], 8246 enum reference_frame ref_frame, 8247 union mv *mv) 8249 { 8250 if (sign_bias[mb->base.ref_frame] ^ sign_bias[ref_frame]) 8251 { 8252 mv->d.x *= -1; 8253 mv->d.y *= -1; 8254 } 8255 } 8257 enum near_mv_v 8258 { 8259 CNT_BEST = 0, 8260 CNT_ZEROZERO = 0, 8261 CNT_NEAREST, 8262 CNT_NEAR, 8263 CNT_SPLITMV 8264 }; 8266 static void 8267 find_near_mvs(const struct mb_info *this, 8268 const struct mb_info *left, 8269 const struct mb_info *above, 8270 const unsigned int sign_bias[3], 8271 union mv near_mvs[4], 8272 int cnt[4]) 8273 { 8274 const struct mb_info *aboveleft = above - 1; 8275 union mv *mv = near_mvs; 8276 int *cntx = cnt; 8278 /* Zero accumulators */ 8279 mv[0].raw = mv[1].raw = mv[2].raw = 0; 8280 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 8282 /* Process above */ 8283 if (above->base.ref_frame != CURRENT_FRAME) 8284 { 8285 if (above->base.mv.raw) 8286 { 8287 (++mv)->raw = above->base.mv.raw; 8288 mv_bias(above, sign_bias, this->base.ref_frame, mv); 8289 ++cntx; 8290 } 8292 *cntx += 2; 8293 } 8294 /* Process left */ 8295 if (left->base.ref_frame != CURRENT_FRAME) 8296 { 8297 if (left->base.mv.raw) 8298 { 8299 union mv this_mv; 8301 this_mv.raw = left->base.mv.raw; 8302 mv_bias(left, sign_bias, this->base.ref_frame, &this_mv); 8304 if (this_mv.raw != mv->raw) 8305 { 8306 (++mv)->raw = this_mv.raw; 8307 ++cntx; 8308 } 8310 *cntx += 2; 8311 } 8312 else 8313 cnt[CNT_ZEROZERO] += 2; 8314 } 8316 /* Process above left */ 8317 if (aboveleft->base.ref_frame != CURRENT_FRAME) 8318 { 8319 if (aboveleft->base.mv.raw) 8320 { 8321 union mv this_mv; 8323 this_mv.raw = aboveleft->base.mv.raw; 8324 mv_bias(aboveleft, sign_bias, this->base.ref_frame, 8325 &this_mv); 8327 if (this_mv.raw != mv->raw) 8328 { 8329 (++mv)->raw = this_mv.raw; 8330 ++cntx; 8331 } 8333 *cntx += 1; 8334 } 8335 else 8336 cnt[CNT_ZEROZERO] += 1; 8337 } 8339 /* If we have three distinct MV's ... */ 8340 if (cnt[CNT_SPLITMV]) 8341 { 8342 /* See if above-left MV can be merged with NEAREST */ 8343 if (mv->raw == near_mvs[CNT_NEAREST].raw) 8344 cnt[CNT_NEAREST] += 1; 8345 } 8347 cnt[CNT_SPLITMV] = ((above->base.y_mode == SPLITMV) 8348 + (left->base.y_mode == SPLITMV)) * 2 8349 + (aboveleft->base.y_mode == SPLITMV); 8351 /* Swap near and nearest if necessary */ 8352 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) 8353 { 8354 int tmp; 8355 tmp = cnt[CNT_NEAREST]; 8356 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 8357 cnt[CNT_NEAR] = tmp; 8358 tmp = near_mvs[CNT_NEAREST].raw; 8359 near_mvs[CNT_NEAREST].raw = near_mvs[CNT_NEAR].raw; 8360 near_mvs[CNT_NEAR].raw = tmp; 8361 } 8363 /* Use near_mvs[CNT_BEST] to store the "best" MV. Note that this 8364 * storage shares the same address as near_mvs[CNT_ZEROZERO]. 8365 */ 8366 if (cnt[CNT_NEAREST] >= cnt[CNT_BEST]) 8367 near_mvs[CNT_BEST] = near_mvs[CNT_NEAREST]; 8368 } 8370 static void 8371 decode_split_mv(struct mb_info *this, 8372 const struct mb_info *left, 8373 const struct mb_info *above, 8374 struct vp8_entropy_hdr *hdr, 8375 union mv *best_mv, 8376 struct bool_decoder *bool) 8377 { 8378 const int *partition; 8379 int j, k, mask, partition_id; 8381 partition_id = bool_read_tree(bool, split_mv_tree, split_mv_probs); 8382 partition = mv_partitions[partition_id]; 8383 this->base.partitioning = partition_id; 8385 for (j = 0, mask = 0; mask < 65535; j++) 8386 { 8387 union mv mv, left_mv, above_mv; 8388 enum prediction_mode subblock_mode; 8389 /* Find the first subblock in this partition. */ 8390 for (k = 0; j != partition[k]; k++); 8392 /* Decode the next MV */ 8393 left_mv = left_block_mv(this, left, k); 8394 above_mv = above_block_mv(this, above, k); 8395 subblock_mode = submv_ref(bool, left_mv, above_mv); 8397 switch (subblock_mode) 8398 { 8399 case LEFT4X4: 8400 mv = left_mv; 8401 break; 8402 case ABOVE4X4: 8403 mv = above_mv; 8404 break; 8405 case ZERO4X4: 8406 mv.raw = 0; 8407 break; 8408 case NEW4X4: 8409 read_mv(bool, &mv, hdr->mv_probs); 8410 mv.d.x += best_mv->d.x; 8411 mv.d.y += best_mv->d.y; 8412 break; 8413 default: 8414 assert(0); 8415 } 8417 /* Fill the MV's for this partition */ 8418 for (; k < 16; k++) 8419 if (j == partition[k]) 8420 { 8421 this->split.mvs[k] = mv; 8422 mask |= 1 << k; 8423 } 8424 } 8425 } 8427 static int 8428 need_mc_border(union mv mv, int l, int t, int b_w, int w, int h) 8429 { 8430 int b, r; 8432 /* Get distance to edge for top-left pixel */ 8433 l += (mv.d.x >> 3); 8434 t += (mv.d.y >> 3); 8435 /* Get distance to edge for bottom-right pixel */ 8436 r = w - (l + b_w); 8437 b = h - (t + b_w); 8439 return (l >> 1 < 2 || r >> 1 < 3 || t >> 1 < 2 || b >> 1 < 3); 8440 } 8442 static void 8443 decode_mvs(struct vp8_decoder_ctx *ctx, 8444 struct mb_info *this, 8445 const struct mb_info *left, 8446 const struct mb_info *above, 8447 const struct mv_clamp_rect *bounds, 8448 struct bool_decoder *bool) 8449 { 8450 struct vp8_entropy_hdr *hdr = &ctx->entropy_hdr; 8451 union mv near_mvs[4]; 8452 union mv clamped_best_mv; 8453 int mv_cnts[4]; 8454 unsigned char probs[4]; 8455 enum {BEST, NEAREST, NEAR}; 8456 int x, y, w, h, b; 8458 this->base.ref_frame = bool_get(bool, hdr->prob_last) 8459 ? 2 + bool_get(bool, hdr->prob_gf) 8460 : 1; 8462 find_near_mvs(this, this - 1, above, ctx->reference_hdr.sign_bias, 8463 near_mvs, mv_cnts); 8464 probs[0] = mv_counts_to_probs[mv_cnts[0]][0]; 8465 probs[1] = mv_counts_to_probs[mv_cnts[1]][1]; 8466 probs[2] = mv_counts_to_probs[mv_cnts[2]][2]; 8467 probs[3] = mv_counts_to_probs[mv_cnts[3]][3]; 8469 this->base.y_mode = bool_read_tree(bool, mv_ref_tree, probs); 8470 this->base.uv_mode = this->base.y_mode; 8472 this->base.need_mc_border = 0; 8473 x = (-bounds->to_left - 128) >> 3; 8474 y = (-bounds->to_top - 128) >> 3; 8475 w = ctx->mb_cols * 16; 8476 h = ctx->mb_rows * 16; 8478 switch (this->base.y_mode) 8479 { 8480 case NEARESTMV: 8481 this->base.mv = clamp_mv(near_mvs[NEAREST], bounds); 8482 break; 8484 case NEARMV: 8485 this->base.mv = clamp_mv(near_mvs[NEAR], bounds); 8486 break; 8487 case ZEROMV: 8488 this->base.mv.raw = 0; 8489 return; //skip need_mc_border check 8490 case NEWMV: 8491 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8492 read_mv(bool, &this->base.mv, hdr->mv_probs); 8493 this->base.mv.d.x += clamped_best_mv.d.x; 8494 this->base.mv.d.y += clamped_best_mv.d.y; 8495 break; 8496 case SPLITMV: 8497 { 8498 union mv chroma_mv[4] = {{{0}}}; 8500 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8501 decode_split_mv(this, left, above, hdr, &clamped_best_mv, bool); 8502 this->base.mv = this->split.mvs[15]; 8504 for (b = 0; b < 16; b++) 8505 { 8506 chroma_mv[(b>>1&1) + (b>>2&2)].d.x += 8507 this->split.mvs[b].d.x; 8508 chroma_mv[(b>>1&1) + (b>>2&2)].d.y += 8509 this->split.mvs[b].d.y; 8511 if (need_mc_border(this->split.mvs[b], 8512 x + (b & 3) * 4, y + (b & ~3), 4, w, h)) 8513 { 8514 this->base.need_mc_border = 1; 8515 break; 8516 } 8517 } 8519 for (b = 0; b < 4; b++) 8520 { 8521 chroma_mv[b].d.x += 4 + 8 * (chroma_mv[b].d.x >> 31); 8522 chroma_mv[b].d.y += 4 + 8 * (chroma_mv[b].d.y >> 31); 8523 chroma_mv[b].d.x /= 4; 8524 chroma_mv[b].d.y /= 4; 8526 //note we're passing in non-subsampled coordinates 8527 if (need_mc_border(chroma_mv[b], 8528 x + (b & 1) * 8, y + (b >> 1) * 8, 16, w, h)) 8529 { 8530 this->base.need_mc_border = 1; 8531 break; 8533 } 8534 } 8536 return; //skip need_mc_border check 8537 } 8538 default: 8539 assert(0); 8540 } 8542 if (need_mc_border(this->base.mv, x, y, 16, w, h)) 8543 this->base.need_mc_border = 1; 8544 } 8546 void 8547 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8548 struct bool_decoder *bool, 8549 int row, 8550 int start_col, 8551 int num_cols) 8552 { 8553 struct mb_info *above, *this; 8554 unsigned int col; 8555 struct mv_clamp_rect bounds; 8557 this = ctx->mb_info_rows[row] + start_col; 8558 above = ctx->mb_info_rows[row - 1] + start_col; 8560 /* Calculate the eighth-pel MV bounds using a 1 MB border. */ 8561 bounds.to_left = -((start_col + 1) << 7); 8562 bounds.to_right = (ctx->mb_cols - start_col) << 7; 8563 bounds.to_top = -((row + 1) << 7); 8564 bounds.to_bottom = (ctx->mb_rows - row) << 7; 8566 for (col = start_col; col < start_col + num_cols; col++) 8567 { 8568 if (ctx->segment_hdr.update_map) 8569 this->base.segment_id = read_segment_id(bool, 8570 &ctx->segment_hdr); 8572 if (ctx->entropy_hdr.coeff_skip_enabled) 8573 this->base.skip_coeff = bool_get(bool, 8574 ctx->entropy_hdr.coeff_skip_prob); 8576 if (ctx->frame_hdr.is_keyframe) 8577 { 8578 if (!ctx->segment_hdr.update_map) 8579 this->base.segment_id = 0; 8581 decode_kf_mb_mode(this, this - 1, above, bool); 8582 } 8583 else 8584 { 8585 if (bool_get(bool, ctx->entropy_hdr.prob_inter)) 8586 decode_mvs(ctx, this, this - 1, above, &bounds, bool); 8587 else 8588 decode_intra_mb_mode(this, &ctx->entropy_hdr, bool); 8590 bounds.to_left -= 16 << 3; 8591 bounds.to_right -= 16 << 3; 8592 } 8594 /* Advance to next mb */ 8595 this++; 8596 above++; 8597 } 8598 } 8600 void 8601 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx) 8602 { 8603 unsigned int mbi_w, mbi_h, i; 8604 struct mb_info *mbi; 8606 mbi_w = ctx->mb_cols + 1; /* For left border col */ 8607 mbi_h = ctx->mb_rows + 1; /* For above border row */ 8609 if (ctx->frame_hdr.frame_size_updated) 8610 { 8611 free(ctx->mb_info_storage); 8612 ctx->mb_info_storage = NULL; 8613 free(ctx->mb_info_rows_storage); 8614 ctx->mb_info_rows_storage = NULL; 8615 } 8617 if (!ctx->mb_info_storage) 8618 ctx->mb_info_storage = calloc(mbi_w * mbi_h, 8619 sizeof(*ctx->mb_info_storage)); 8621 if (!ctx->mb_info_rows_storage) 8622 ctx->mb_info_rows_storage = calloc(mbi_h, 8623 sizeof(*ctx->mb_info_rows_storage)); 8625 /* Set up row pointers */ 8626 mbi = ctx->mb_info_storage + 1; 8627 for (i = 0; i < mbi_h; i++) 8628 { 8629 ctx->mb_info_rows_storage[i] = mbi; 8630 mbi += mbi_w; 8631 } 8633 ctx->mb_info_rows = ctx->mb_info_rows_storage + 1; 8634 } 8636 void 8637 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx) 8638 { 8639 free(ctx->mb_info_storage); 8640 ctx->mb_info_storage = NULL; 8641 free(ctx->mb_info_rows_storage); 8642 ctx->mb_info_rows_storage = NULL; 8643 } 8645 ---- End code block ---------------------------------------- 8646 20.12. modemv.h 8648 ---- Begin code block -------------------------------------- 8650 /* 8651 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8652 * 8653 * Use of this source code is governed by a BSD-style license 8654 * that can be found in the LICENSE file in the root of the source 8655 * tree. An additional intellectual property rights grant can be 8656 * found in the file PATENTS. All contributing project authors may 8657 * be found in the AUTHORS file in the root of the source tree. 8658 */ 8659 #ifndef MODEMV_H 8660 #define MODEMV_H 8662 void 8663 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx); 8665 void 8666 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx); 8668 void 8669 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8670 struct bool_decoder *bool, 8671 int row, 8672 int start_col, 8673 int num_cols); 8675 #endif 8677 ---- End code block ---------------------------------------- 8679 20.13. modemv_data.h 8681 ---- Begin code block -------------------------------------- 8683 static const unsigned char kf_y_mode_probs[] = { 145, 156, 163, 128}; 8684 static const unsigned char kf_uv_mode_probs[] = { 142, 114, 183}; 8685 static const unsigned char kf_b_mode_probs[10][10][9] = 8686 { 8687 { /* above mode 0 */ 8688 { /* left mode 0 */ 231, 120, 48, 89, 115, 113, 120, 152, 112}, 8689 { /* left mode 1 */ 152, 179, 64, 126, 170, 118, 46, 70, 95}, 8690 { /* left mode 2 */ 175, 69, 143, 80, 85, 82, 72, 155, 103}, 8691 { /* left mode 3 */ 56, 58, 10, 171, 218, 189, 17, 13, 152}, 8692 { /* left mode 4 */ 144, 71, 10, 38, 171, 213, 144, 34, 26}, 8693 { /* left mode 5 */ 114, 26, 17, 163, 44, 195, 21, 10, 173}, 8694 { /* left mode 6 */ 121, 24, 80, 195, 26, 62, 44, 64, 85}, 8695 { /* left mode 7 */ 170, 46, 55, 19, 136, 160, 33, 206, 71}, 8696 { /* left mode 8 */ 63, 20, 8, 114, 114, 208, 12, 9, 226}, 8697 { /* left mode 9 */ 81, 40, 11, 96, 182, 84, 29, 16, 36} 8698 }, 8699 { /* above mode 1 */ 8700 { /* left mode 0 */ 134, 183, 89, 137, 98, 101, 106, 165, 148}, 8701 { /* left mode 1 */ 72, 187, 100, 130, 157, 111, 32, 75, 80}, 8702 { /* left mode 2 */ 66, 102, 167, 99, 74, 62, 40, 234, 128}, 8703 { /* left mode 3 */ 41, 53, 9, 178, 241, 141, 26, 8, 107}, 8704 { /* left mode 4 */ 104, 79, 12, 27, 217, 255, 87, 17, 7}, 8705 { /* left mode 5 */ 74, 43, 26, 146, 73, 166, 49, 23, 157}, 8706 { /* left mode 6 */ 65, 38, 105, 160, 51, 52, 31, 115, 128}, 8707 { /* left mode 7 */ 87, 68, 71, 44, 114, 51, 15, 186, 23}, 8708 { /* left mode 8 */ 47, 41, 14, 110, 182, 183, 21, 17, 194}, 8709 { /* left mode 9 */ 66, 45, 25, 102, 197, 189, 23, 18, 22} 8710 }, 8711 { /* above mode 2 */ 8712 { /* left mode 0 */ 88, 88, 147, 150, 42, 46, 45, 196, 205}, 8713 { /* left mode 1 */ 43, 97, 183, 117, 85, 38, 35, 179, 61}, 8714 { /* left mode 2 */ 39, 53, 200, 87, 26, 21, 43, 232, 171}, 8715 { /* left mode 3 */ 56, 34, 51, 104, 114, 102, 29, 93, 77}, 8716 { /* left mode 4 */ 107, 54, 32, 26, 51, 1, 81, 43, 31}, 8717 { /* left mode 5 */ 39, 28, 85, 171, 58, 165, 90, 98, 64}, 8718 { /* left mode 6 */ 34, 22, 116, 206, 23, 34, 43, 166, 73}, 8719 { /* left mode 7 */ 68, 25, 106, 22, 64, 171, 36, 225, 114}, 8720 { /* left mode 8 */ 34, 19, 21, 102, 132, 188, 16, 76, 124}, 8721 { /* left mode 9 */ 62, 18, 78, 95, 85, 57, 50, 48, 51} 8722 }, 8723 { /* above mode 3 */ 8724 { /* left mode 0 */ 193, 101, 35, 159, 215, 111, 89, 46, 111}, 8725 { /* left mode 1 */ 60, 148, 31, 172, 219, 228, 21, 18, 111}, 8726 { /* left mode 2 */ 112, 113, 77, 85, 179, 255, 38, 120, 114}, 8727 { /* left mode 3 */ 40, 42, 1, 196, 245, 209, 10, 25, 109}, 8728 { /* left mode 4 */ 100, 80, 8, 43, 154, 1, 51, 26, 71}, 8729 { /* left mode 5 */ 88, 43, 29, 140, 166, 213, 37, 43, 154}, 8730 { /* left mode 6 */ 61, 63, 30, 155, 67, 45, 68, 1, 209}, 8731 { /* left mode 7 */ 142, 78, 78, 16, 255, 128, 34, 197, 171}, 8732 { /* left mode 8 */ 41, 40, 5, 102, 211, 183, 4, 1, 221}, 8733 { /* left mode 9 */ 51, 50, 17, 168, 209, 192, 23, 25, 82} 8734 }, 8735 { /* above mode 4 */ 8736 { /* left mode 0 */ 125, 98, 42, 88, 104, 85, 117, 175, 82}, 8737 { /* left mode 1 */ 95, 84, 53, 89, 128, 100, 113, 101, 45}, 8738 { /* left mode 2 */ 75, 79, 123, 47, 51, 128, 81, 171, 1}, 8739 { /* left mode 3 */ 57, 17, 5, 71, 102, 57, 53, 41, 49}, 8740 { /* left mode 4 */ 115, 21, 2, 10, 102, 255, 166, 23, 6}, 8741 { /* left mode 5 */ 38, 33, 13, 121, 57, 73, 26, 1, 85}, 8742 { /* left mode 6 */ 41, 10, 67, 138, 77, 110, 90, 47, 114}, 8743 { /* left mode 7 */ 101, 29, 16, 10, 85, 128, 101, 196, 26}, 8744 { /* left mode 8 */ 57, 18, 10, 102, 102, 213, 34, 20, 43}, 8745 { /* left mode 9 */ 117, 20, 15, 36, 163, 128, 68, 1, 26} 8746 }, 8747 { /* above mode 5 */ 8748 { /* left mode 0 */ 138, 31, 36, 171, 27, 166, 38, 44, 229}, 8749 { /* left mode 1 */ 67, 87, 58, 169, 82, 115, 26, 59, 179}, 8750 { /* left mode 2 */ 63, 59, 90, 180, 59, 166, 93, 73, 154}, 8751 { /* left mode 3 */ 40, 40, 21, 116, 143, 209, 34, 39, 175}, 8752 { /* left mode 4 */ 57, 46, 22, 24, 128, 1, 54, 17, 37}, 8753 { /* left mode 5 */ 47, 15, 16, 183, 34, 223, 49, 45, 183}, 8754 { /* left mode 6 */ 46, 17, 33, 183, 6, 98, 15, 32, 183}, 8755 { /* left mode 7 */ 65, 32, 73, 115, 28, 128, 23, 128, 205}, 8756 { /* left mode 8 */ 40, 3, 9, 115, 51, 192, 18, 6, 223}, 8757 { /* left mode 9 */ 87, 37, 9, 115, 59, 77, 64, 21, 47} 8758 }, 8759 { /* above mode 6 */ 8760 { /* left mode 0 */ 104, 55, 44, 218, 9, 54, 53, 130, 226}, 8761 { /* left mode 1 */ 64, 90, 70, 205, 40, 41, 23, 26, 57}, 8762 { /* left mode 2 */ 54, 57, 112, 184, 5, 41, 38, 166, 213}, 8763 { /* left mode 3 */ 30, 34, 26, 133, 152, 116, 10, 32, 134}, 8764 { /* left mode 4 */ 75, 32, 12, 51, 192, 255, 160, 43, 51}, 8765 { /* left mode 5 */ 39, 19, 53, 221, 26, 114, 32, 73, 255}, 8766 { /* left mode 6 */ 31, 9, 65, 234, 2, 15, 1, 118, 73}, 8767 { /* left mode 7 */ 88, 31, 35, 67, 102, 85, 55, 186, 85}, 8768 { /* left mode 8 */ 56, 21, 23, 111, 59, 205, 45, 37, 192}, 8769 { /* left mode 9 */ 55, 38, 70, 124, 73, 102, 1, 34, 98} 8770 }, 8771 { /* above mode 7 */ 8772 { /* left mode 0 */ 102, 61, 71, 37, 34, 53, 31, 243, 192}, 8773 { /* left mode 1 */ 69, 60, 71, 38, 73, 119, 28, 222, 37}, 8774 { /* left mode 2 */ 68, 45, 128, 34, 1, 47, 11, 245, 171}, 8775 { /* left mode 3 */ 62, 17, 19, 70, 146, 85, 55, 62, 70}, 8776 { /* left mode 4 */ 75, 15, 9, 9, 64, 255, 184, 119, 16}, 8777 { /* left mode 5 */ 37, 43, 37, 154, 100, 163, 85, 160, 1}, 8778 { /* left mode 6 */ 63, 9, 92, 136, 28, 64, 32, 201, 85}, 8779 { /* left mode 7 */ 86, 6, 28, 5, 64, 255, 25, 248, 1}, 8780 { /* left mode 8 */ 56, 8, 17, 132, 137, 255, 55, 116, 128}, 8781 { /* left mode 9 */ 58, 15, 20, 82, 135, 57, 26, 121, 40} 8782 }, 8783 { /* above mode 8 */ 8784 { /* left mode 0 */ 164, 50, 31, 137, 154, 133, 25, 35, 218}, 8785 { /* left mode 1 */ 51, 103, 44, 131, 131, 123, 31, 6, 158}, 8786 { /* left mode 2 */ 86, 40, 64, 135, 148, 224, 45, 183, 128}, 8787 { /* left mode 3 */ 22, 26, 17, 131, 240, 154, 14, 1, 209}, 8788 { /* left mode 4 */ 83, 12, 13, 54, 192, 255, 68, 47, 28}, 8789 { /* left mode 5 */ 45, 16, 21, 91, 64, 222, 7, 1, 197}, 8790 { /* left mode 6 */ 56, 21, 39, 155, 60, 138, 23, 102, 213}, 8791 { /* left mode 7 */ 85, 26, 85, 85, 128, 128, 32, 146, 171}, 8792 { /* left mode 8 */ 18, 11, 7, 63, 144, 171, 4, 4, 246}, 8793 { /* left mode 9 */ 35, 27, 10, 146, 174, 171, 12, 26, 128} 8794 }, 8795 { /* above mode 9 */ 8796 { /* left mode 0 */ 190, 80, 35, 99, 180, 80, 126, 54, 45}, 8797 { /* left mode 1 */ 85, 126, 47, 87, 176, 51, 41, 20, 32}, 8798 { /* left mode 2 */ 101, 75, 128, 139, 118, 146, 116, 128, 85}, 8799 { /* left mode 3 */ 56, 41, 15, 176, 236, 85, 37, 9, 62}, 8800 { /* left mode 4 */ 146, 36, 19, 30, 171, 255, 97, 27, 20}, 8801 { /* left mode 5 */ 71, 30, 17, 119, 118, 255, 17, 18, 138}, 8802 { /* left mode 6 */ 101, 38, 60, 138, 55, 70, 43, 26, 142}, 8803 { /* left mode 7 */ 138, 45, 61, 62, 219, 1, 81, 188, 64}, 8804 { /* left mode 8 */ 32, 41, 20, 117, 151, 142, 20, 21, 163}, 8805 { /* left mode 9 */ 112, 19, 12, 61, 195, 128, 48, 4, 24} 8806 } 8807 }; 8808 static const int kf_y_mode_tree[] = 8809 { 8810 -B_PRED, 2, 8811 4, 6, 8812 -DC_PRED, -V_PRED, 8813 -H_PRED, -TM_PRED 8814 }; 8815 static const int y_mode_tree[] = 8816 { 8817 -DC_PRED, 2, 8818 4, 6, 8819 -V_PRED, -H_PRED, 8820 -TM_PRED, -B_PRED 8821 }; 8822 static const int uv_mode_tree[6] = 8823 { 8824 -DC_PRED, 2, 8825 -V_PRED, 4, 8826 -H_PRED, -TM_PRED 8827 }; 8828 static const int b_mode_tree[18] = 8829 { 8830 -B_DC_PRED, 2, /* 0 = DC_NODE */ 8831 -B_TM_PRED, 4, /* 1 = TM_NODE */ 8832 -B_VE_PRED, 6, /* 2 = VE_NODE */ 8833 8, 12, /* 3 = COM_NODE */ 8834 -B_HE_PRED, 10, /* 4 = HE_NODE */ 8835 -B_RD_PRED, -B_VR_PRED, /* 5 = RD_NODE */ 8836 -B_LD_PRED, 14, /* 6 = LD_NODE */ 8837 -B_VL_PRED, 16, /* 7 = VL_NODE */ 8838 -B_HD_PRED, -B_HU_PRED /* 8 = HD_NODE */ 8839 }; 8840 static const int small_mv_tree[14] = 8841 { 8842 2, 8, 8843 4, 6, 8844 -0, -1, 8845 -2, -3, 8846 10, 12, 8847 -4, -5, 8848 -6, -7 8849 }; 8850 static const int mv_ref_tree[8] = 8851 { 8852 -ZEROMV, 2, 8853 -NEARESTMV, 4, 8854 -NEARMV, 6, 8855 -NEWMV, -SPLITMV 8856 }; 8857 static const int submv_ref_tree[6] = 8858 { 8859 -LEFT4X4, 2, 8860 -ABOVE4X4, 4, 8861 -ZERO4X4, -NEW4X4 8862 }; 8863 static const int split_mv_tree[6] = 8864 { 8865 -3, 2, 8866 -2, 4, 8867 -0, -1 8868 }; 8869 static const unsigned char default_b_mode_probs[] = 8870 { 120, 90, 79, 133, 87, 85, 80, 111, 151}; 8871 static const unsigned char mv_counts_to_probs[6][4] = 8872 { 8873 { 7, 1, 1, 143 }, 8874 { 14, 18, 14, 107 }, 8875 { 135, 64, 57, 68 }, 8876 { 60, 56, 128, 65 }, 8877 { 159, 134, 128, 34 }, 8878 { 234, 188, 128, 28 } 8880 }; 8881 static const unsigned char split_mv_probs[3] = 8882 { 110, 111, 150}; 8883 static const unsigned char submv_ref_probs2[5][3] = 8884 { 8885 { 147, 136, 18 }, 8886 { 106, 145, 1 }, 8887 { 179, 121, 1 }, 8888 { 223, 1, 34 }, 8889 { 208, 1, 1 } 8890 }; 8892 const static int mv_partitions[4][16] = 8893 { 8894 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, 8895 {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1 }, 8896 {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3 }, 8897 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } 8898 }; 8900 ---- End code block ---------------------------------------- 8902 20.14. predict.c 8904 ---- Begin code block -------------------------------------- 8906 /* 8907 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8908 * 8909 * Use of this source code is governed by a BSD-style license 8910 * that can be found in the LICENSE file in the root of the source 8911 * tree. An additional intellectual property rights grant can be 8912 * found in the file PATENTS. All contributing project authors may 8913 * be found in the AUTHORS file in the root of the source tree. 8914 */ 8915 #include "dixie.h" 8916 #include "predict.h" 8917 #include "idct_add.h" 8918 #include "mem.h" 8919 #include 8920 #include 8922 enum 8923 { 8924 BORDER_PIXELS = 16, 8925 }; 8927 static const filter_t sixtap_filters[8] = 8928 { 8929 { 0, 0, 128, 0, 0, 0 }, 8930 { 0, -6, 123, 12, -1, 0 }, 8931 { 2, -11, 108, 36, -8, 1 }, 8932 { 0, -9, 93, 50, -6, 0 }, 8933 { 3, -16, 77, 77, -16, 3 }, 8934 { 0, -6, 50, 93, -9, 0 }, 8935 { 1, -8, 36, 108, -11, 2 }, 8936 { 0, -1, 12, 123, -6, 0 }, 8937 }; 8939 static const filter_t bilinear_filters[8] = 8940 { 8942 { 0, 0, 128, 0, 0, 0 }, 8943 { 0, 0, 112, 16, 0, 0 }, 8944 { 0, 0, 96, 32, 0, 0 }, 8945 { 0, 0, 80, 48, 0, 0 }, 8946 { 0, 0, 64, 64, 0, 0 }, 8947 { 0, 0, 48, 80, 0, 0 }, 8948 { 0, 0, 32, 96, 0, 0 }, 8949 { 0, 0, 16, 112, 0, 0 } 8950 }; 8952 static void 8953 predict_h_nxn(unsigned char *predict, 8954 int stride, 8955 int n) 8956 { 8957 unsigned char *left = predict - 1; 8958 int i, j; 8960 for (i = 0; i < n; i++) 8961 for (j = 0; j < n; j++) 8962 predict[i *stride + j] = left[i * stride]; 8963 } 8965 static void 8966 predict_v_nxn(unsigned char *predict, 8967 int stride, 8968 int n) 8969 { 8970 unsigned char *above = predict - stride; 8971 int i, j; 8973 for (i = 0; i < n; i++) 8974 for (j = 0; j < n; j++) 8975 predict[i *stride + j] = above[j]; 8976 } 8978 static void 8979 predict_tm_nxn(unsigned char *predict, 8980 int stride, 8981 int n) 8982 { 8983 /* Transposes the left column to the top row for later consumption 8984 * by the idct/recon stage 8985 */ 8986 unsigned char *left = predict - 1; 8987 unsigned char *above = predict - stride; 8988 unsigned char p = above[-1]; 8989 int i, j; 8991 for (j = 0; j < n; j++) 8992 { 8993 for (i = 0; i < n; i++) 8994 predict[i] = CLAMP_255(*left + above[i] - p); 8996 predict += stride; 8997 left += stride; 8998 } 8999 } 9001 static void 9002 predict_dc_nxn(unsigned char *predict, 9003 int stride, 9004 int n) 9005 { 9006 unsigned char *left = predict - 1; 9007 unsigned char *above = predict - stride; 9008 int i, j, dc = 0; 9010 for (i = 0; i < n; i++) 9011 { 9012 dc += *left + above[i]; 9013 left += stride; 9014 } 9016 switch (n) 9017 { 9018 case 16: 9019 dc = (dc + 16) >> 5; 9020 break; 9022 case 8: 9023 dc = (dc + 8) >> 4; 9024 break; 9025 case 4: 9026 dc = (dc + 4) >> 3; 9027 break; 9028 } 9030 for (i = 0; i < n; i++) 9031 for (j = 0; j < n; j++) 9032 predict[i *stride + j] = dc; 9033 } 9035 static void 9036 predict_ve_4x4(unsigned char *predict, 9037 int stride) 9038 { 9039 unsigned char *above = predict - stride; 9040 int i, j; 9042 predict[0] = (above[-1] + 2 * above[0] + above[1] + 2) >> 2; 9043 predict[1] = (above[ 0] + 2 * above[1] + above[2] + 2) >> 2; 9044 predict[2] = (above[ 1] + 2 * above[2] + above[3] + 2) >> 2; 9045 predict[3] = (above[ 2] + 2 * above[3] + above[4] + 2) >> 2; 9047 for (i = 1; i < 4; i++) 9048 for (j = 0; j < 4; j++) 9049 predict[i *stride + j] = predict[j]; 9050 } 9052 static void 9053 predict_he_4x4(unsigned char *predict, 9054 int stride) 9055 { 9056 unsigned char *left = predict - 1; 9058 predict[0] = 9059 predict[1] = 9060 predict[2] = 9061 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9062 predict += stride; 9063 left += stride; 9065 predict[0] = 9066 predict[1] = 9067 predict[2] = 9068 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9069 predict += stride; 9070 left += stride; 9072 predict[0] = 9073 predict[1] = 9074 predict[2] = 9075 predict[3] = (left[-stride] + 2 * left[0] + left[stride] + 2) >> 2; 9076 predict += stride; 9077 left += stride; 9079 predict[0] = 9080 predict[1] = 9081 predict[2] = 9082 predict[3] = (left[-stride] + 2 * left[0] + left[0] + 2) >> 2; 9083 } 9085 static void 9086 predict_ld_4x4(unsigned char *predict, 9087 int stride) 9088 { 9089 unsigned char *above = predict - stride; 9090 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9092 predict[0] = pred0 = (above[0] + 2 * above[1] + above[2] + 2) >> 2; 9093 predict[1] = pred1 = (above[1] + 2 * above[2] + above[3] + 2) >> 2; 9094 predict[2] = pred2 = (above[2] + 2 * above[3] + above[4] + 2) >> 2; 9095 predict[3] = pred3 = (above[3] + 2 * above[4] + above[5] + 2) >> 2; 9096 predict += stride; 9098 predict[0] = pred1; 9099 predict[1] = pred2; 9100 predict[2] = pred3; 9101 predict[3] = pred4 = (above[4] + 2 * above[5] + above[6] + 2) >> 2; 9102 predict += stride; 9104 predict[0] = pred2; 9105 predict[1] = pred3; 9106 predict[2] = pred4; 9107 predict[3] = pred5 = (above[5] + 2 * above[6] + above[7] + 2) >> 2; 9108 predict += stride; 9110 predict[0] = pred3; 9111 predict[1] = pred4; 9112 predict[2] = pred5; 9113 predict[3] = pred6 = (above[6] + 2 * above[7] + above[7] + 2) >> 2; 9114 } 9115 static void 9116 predict_rd_4x4(unsigned char *predict, 9117 int stride) 9118 { 9119 unsigned char *left = predict - 1; 9120 unsigned char *above = predict - stride; 9121 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9123 predict[0] = pred0 = 9124 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9125 predict[1] = pred1 = 9126 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9127 predict[2] = pred2 = 9128 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9129 predict[3] = pred3 = 9130 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9131 predict += stride; 9133 predict[0] = pred4 = 9134 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9135 predict[1] = pred0; 9136 predict[2] = pred1; 9137 predict[3] = pred2; 9138 predict += stride; 9140 predict[0] = pred5 = 9141 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9142 predict[1] = pred4; 9143 predict[2] = pred0; 9144 predict[3] = pred1; 9145 predict += stride; 9147 predict[0] = pred6 = 9148 (left[stride*3] + 2 * left[stride*2] + left[stride] + 2) >> 2; 9149 predict[1] = pred5; 9150 predict[2] = pred4; 9151 predict[3] = pred0; 9152 } 9154 static void 9155 predict_vr_4x4(unsigned char *predict, 9156 int stride) 9157 { 9158 unsigned char *left = predict - 1; 9159 unsigned char *above = predict - stride; 9160 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9161 pred7, pred8, pred9; 9163 predict[0] = pred0 = (above[-1] + above[0] + 1) >> 1; 9164 predict[1] = pred1 = (above[ 0] + above[1] + 1) >> 1; 9165 predict[2] = pred2 = (above[ 1] + above[2] + 1) >> 1; 9166 predict[3] = pred3 = (above[ 2] + above[3] + 1) >> 1; 9167 predict += stride; 9169 predict[0] = pred4 = 9170 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9171 predict[1] = pred5 = 9172 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9173 predict[2] = pred6 = 9174 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9175 predict[3] = pred7 = 9176 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9177 predict += stride; 9179 predict[0] = pred8 = 9180 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9181 predict[1] = pred0; 9182 predict[2] = pred1; 9183 predict[3] = pred2; 9184 predict += stride; 9186 predict[0] = pred9 = 9187 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9188 predict[1] = pred4; 9189 predict[2] = pred5; 9190 predict[3] = pred6; 9191 } 9193 static void 9194 predict_vl_4x4(unsigned char *predict, 9195 int stride) 9196 { 9197 unsigned char *above = predict - stride; 9198 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9199 pred7, pred8, pred9; 9201 predict[0] = pred0 = (above[0] + above[1] + 1) >> 1; 9202 predict[1] = pred1 = (above[1] + above[2] + 1) >> 1; 9203 predict[2] = pred2 = (above[2] + above[3] + 1) >> 1; 9204 predict[3] = pred3 = (above[3] + above[4] + 1) >> 1; 9205 predict += stride; 9207 predict[0] = pred4 = 9208 (above[0] + 2 * above[1] + above[2] + 2) >> 2; 9209 predict[1] = pred5 = 9210 (above[1] + 2 * above[2] + above[3] + 2) >> 2; 9211 predict[2] = pred6 = 9212 (above[2] + 2 * above[3] + above[4] + 2) >> 2; 9213 predict[3] = pred7 = 9214 (above[3] + 2 * above[4] + above[5] + 2) >> 2; 9215 predict += stride; 9217 predict[0] = pred1; 9218 predict[1] = pred2; 9219 predict[2] = pred3; 9220 predict[3] = pred8 = (above[4] + 2 * above[5] + above[6] + 2) >> 2; 9221 predict += stride; 9223 predict[0] = pred5; 9224 predict[1] = pred6; 9225 predict[2] = pred7; 9226 predict[3] = pred9 = (above[5] + 2 * above[6] + above[7] + 2) >> 2; 9227 } 9229 static void 9230 predict_hd_4x4(unsigned char *predict, 9231 int stride) 9232 { 9233 unsigned char *left = predict - 1; 9234 unsigned char *above = predict - stride; 9235 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9236 pred7, pred8, pred9; 9238 predict[0] = pred0 = 9239 (left[ 0] + above[-1] + 1) >> 1; 9240 predict[1] = pred1 = 9241 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9242 predict[2] = pred2 = 9243 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9244 predict[3] = pred3 = 9245 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9246 predict += stride; 9248 predict[0] = pred4 = 9249 (left[stride] + left[0] + 1) >> 1; 9250 predict[1] = pred5 = 9251 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9252 predict[2] = pred0; 9253 predict[3] = pred1; 9254 predict += stride; 9256 predict[0] = pred6 = 9257 (left[stride*2] + left[stride] + 1) >> 1; 9258 predict[1] = pred7 = 9259 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9260 predict[2] = pred4; 9261 predict[3] = pred5; 9262 predict += stride; 9264 predict[0] = pred8 = 9265 (left[stride*3] + left[stride*2] + 1) >> 1; 9266 predict[1] = pred9 = 9267 (left[stride*3] + 2 * left[stride*2] + left[stride] + 2) >> 2; 9268 predict[2] = pred6; 9269 predict[3] = pred7; 9270 } 9272 static void 9273 predict_hu_4x4(unsigned char *predict, 9274 int stride) 9275 { 9276 unsigned char *left = predict - 1; 9277 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9279 predict[0] = pred0 = 9280 (left[stride*0] + left[stride*1] + 1) >> 1; 9281 predict[1] = pred1 = 9282 (left[stride*0] + 2 * left[stride*1] + left[stride*2] + 2) >> 2; 9283 predict[2] = pred2 = 9284 (left[stride*1] + left[stride*2] + 1) >> 1; 9285 predict[3] = pred3 = 9286 (left[stride*1] + 2 * left[stride*2] + left[stride*3] + 2) >> 2; 9287 predict += stride; 9289 predict[0] = pred2; 9290 predict[1] = pred3; 9291 predict[2] = pred4 = 9292 (left[stride*2] + left[stride*3] + 1) >> 1; 9293 predict[3] = pred5 = 9294 (left[stride*2] + 2 * left[stride*3] + left[stride*3] + 2) >> 2; 9295 predict += stride; 9297 predict[0] = pred4; 9298 predict[1] = pred5; 9299 predict[2] = pred6 = left[stride*3]; 9300 predict[3] = pred6; 9301 predict += stride; 9303 predict[0] = pred6; 9304 predict[1] = pred6; 9305 predict[2] = pred6; 9306 predict[3] = pred6; 9307 } 9309 static void 9310 predict_h_16x16(unsigned char *predict, int stride) 9311 { 9312 predict_h_nxn(predict, stride, 16); 9313 } 9315 static void 9316 predict_v_16x16(unsigned char *predict, int stride) 9317 { 9318 predict_v_nxn(predict, stride, 16); 9319 } 9321 static void 9322 predict_tm_16x16(unsigned char *predict, int stride) 9323 { 9324 predict_tm_nxn(predict, stride, 16); 9325 } 9327 static void 9328 predict_h_8x8(unsigned char *predict, int stride) 9329 { 9330 predict_h_nxn(predict, stride, 8); 9331 } 9333 static void 9334 predict_v_8x8(unsigned char *predict, int stride) 9335 { 9336 predict_v_nxn(predict, stride, 8); 9337 } 9339 static void 9340 predict_tm_8x8(unsigned char *predict, int stride) 9341 { 9342 predict_tm_nxn(predict, stride, 8); 9343 } 9344 static void 9345 predict_tm_4x4(unsigned char *predict, int stride) 9346 { 9347 predict_tm_nxn(predict, stride, 4); 9348 } 9350 static void 9351 copy_down(unsigned char *recon, 9352 int stride) 9353 { 9354 /* Copy the four pixels above-right of subblock 3 to 9355 * above-right of subblocks 7, 11, and 15 9356 */ 9357 uint32_t tmp, *copy = (void *)(recon + 16 - stride); 9359 stride = stride / sizeof(unsigned int); 9360 tmp = *copy; 9361 copy += stride * 4; 9362 *copy = tmp; 9363 copy += stride * 4; 9364 *copy = tmp; 9365 copy += stride * 4; 9366 *copy = tmp; 9367 } 9369 static void 9370 b_pred(unsigned char *predict, 9371 int stride, 9372 struct mb_info *mbi, 9373 short *coeffs) 9374 { 9375 int i; 9377 copy_down(predict, stride); 9379 for (i = 0; i < 16; i++) 9380 { 9381 unsigned char *b_predict = predict + (i & 3) * 4; 9383 switch (mbi->split.modes[i]) 9384 { 9385 case B_DC_PRED: 9386 predict_dc_nxn(b_predict, stride, 4); 9387 break; 9388 case B_TM_PRED: 9389 predict_tm_4x4(b_predict, stride); 9390 break; 9391 case B_VE_PRED: 9392 predict_ve_4x4(b_predict, stride); 9393 break; 9394 case B_HE_PRED: 9395 predict_he_4x4(b_predict, stride); 9396 break; 9397 case B_LD_PRED: 9398 predict_ld_4x4(b_predict, stride); 9399 break; 9400 case B_RD_PRED: 9401 predict_rd_4x4(b_predict, stride); 9402 break; 9403 case B_VR_PRED: 9404 predict_vr_4x4(b_predict, stride); 9405 break; 9406 case B_VL_PRED: 9407 predict_vl_4x4(b_predict, stride); 9408 break; 9409 case B_HD_PRED: 9410 predict_hd_4x4(b_predict, stride); 9411 break; 9412 case B_HU_PRED: 9413 predict_hu_4x4(b_predict, stride); 9414 break; 9415 default: 9416 assert(0); 9417 } 9419 vp8_dixie_idct_add(b_predict, b_predict, stride, coeffs); 9420 coeffs += 16; 9422 if ((i & 3) == 3) 9423 { 9424 predict += stride * 4; 9425 } 9426 } 9427 } 9429 static void 9430 fixup_dc_coeffs(struct mb_info *mbi, 9431 short *coeffs) 9432 { 9433 short y2[16]; 9434 int i; 9436 vp8_dixie_walsh(coeffs + 24 * 16, y2); 9437 for (i = 0; i < 16; i++) 9438 coeffs[i*16] = y2[i]; 9439 } 9441 static void 9442 predict_intra_luma(unsigned char *predict, 9443 int stride, 9444 struct mb_info *mbi, 9445 short *coeffs) 9446 { 9447 if (mbi->base.y_mode == B_PRED) 9448 b_pred(predict, stride, mbi, coeffs); 9449 else 9450 { 9451 int i; 9453 switch (mbi->base.y_mode) 9454 { 9455 case DC_PRED: 9456 predict_dc_nxn(predict, stride, 16); 9457 break; 9458 case V_PRED: 9459 predict_v_16x16(predict, stride); 9460 break; 9461 case H_PRED: 9462 predict_h_16x16(predict, stride); 9463 break; 9464 case TM_PRED: 9465 predict_tm_16x16(predict, stride); 9466 break; 9467 default: 9468 assert(0); 9469 } 9471 fixup_dc_coeffs(mbi, coeffs); 9473 for (i = 0; i < 16; i++) 9474 { 9475 vp8_dixie_idct_add(predict, predict, stride, coeffs); 9476 coeffs += 16; 9477 predict += 4; 9479 if ((i & 3) == 3) 9480 predict += stride * 4 - 16; 9481 } 9483 } 9485 } 9487 static void 9488 predict_intra_chroma(unsigned char *predict_u, 9489 unsigned char *predict_v, 9490 int stride, 9491 struct mb_info *mbi, 9492 short *coeffs) 9493 { 9494 int i; 9496 switch (mbi->base.uv_mode) 9497 { 9498 case DC_PRED: 9499 predict_dc_nxn(predict_u, stride, 8); 9500 predict_dc_nxn(predict_v, stride, 8); 9501 break; 9502 case V_PRED: 9503 predict_v_8x8(predict_u, stride); 9504 predict_v_8x8(predict_v, stride); 9505 break; 9506 case H_PRED: 9507 predict_h_8x8(predict_u, stride); 9508 predict_h_8x8(predict_v, stride); 9509 break; 9510 case TM_PRED: 9511 predict_tm_8x8(predict_u, stride); 9512 predict_tm_8x8(predict_v, stride); 9513 break; 9514 default: 9515 assert(0); 9516 } 9518 coeffs += 16 * 16; 9520 for (i = 16; i < 20; i++) 9521 { 9522 vp8_dixie_idct_add(predict_u, predict_u, stride, coeffs); 9523 coeffs += 16; 9524 predict_u += 4; 9526 if (i & 1) 9527 predict_u += stride * 4 - 8; 9528 } 9530 for (i = 20; i < 24; i++) 9531 { 9532 vp8_dixie_idct_add(predict_v, predict_v, stride, coeffs); 9533 coeffs += 16; 9534 predict_v += 4; 9536 if (i & 1) 9537 predict_v += stride * 4 - 8; 9538 } 9540 } 9542 static void 9543 sixtap_horiz(unsigned char *output, 9544 int output_stride, 9545 const unsigned char *reference, 9546 int reference_stride, 9547 int cols, 9548 int rows, 9549 const filter_t filter 9550 ) 9551 { 9552 int r, c, temp; 9554 for (r = 0; r < rows; r++) 9555 { 9556 for (c = 0; c < cols; c++) 9557 { 9558 temp = (reference[-2] * filter[0]) + 9559 (reference[-1] * filter[1]) + 9560 (reference[ 0] * filter[2]) + 9561 (reference[ 1] * filter[3]) + 9562 (reference[ 2] * filter[4]) + 9563 (reference[ 3] * filter[5]) + 9564 64; 9565 temp >>= 7; 9566 output[c] = CLAMP_255(temp); 9567 reference++; 9568 } 9570 reference += reference_stride - cols; 9571 output += output_stride; 9572 } 9573 } 9575 static void 9576 sixtap_vert(unsigned char *output, 9577 int output_stride, 9578 const unsigned char *reference, 9579 int reference_stride, 9580 int cols, 9581 int rows, 9582 const filter_t filter 9583 ) 9584 { 9585 int r, c, temp; 9587 for (r = 0; r < rows; r++) 9588 { 9589 for (c = 0; c < cols; c++) 9590 { 9591 temp = (reference[-2*reference_stride] * filter[0]) + 9592 (reference[-1*reference_stride] * filter[1]) + 9593 (reference[ 0*reference_stride] * filter[2]) + 9594 (reference[ 1*reference_stride] * filter[3]) + 9595 (reference[ 2*reference_stride] * filter[4]) + 9596 (reference[ 3*reference_stride] * filter[5]) + 9597 64; 9598 temp >>= 7; 9599 output[c] = CLAMP_255(temp); 9600 reference++; 9601 } 9603 reference += reference_stride - cols; 9604 output += output_stride; 9605 } 9606 } 9608 static void 9609 sixtap_2d(unsigned char *output, 9610 int output_stride, 9611 const unsigned char *reference, 9612 int reference_stride, 9613 int cols, 9614 int rows, 9615 int mx, 9616 int my, 9617 const filter_t filters[8] 9618 ) 9619 { 9620 DECLARE_ALIGNED(16, unsigned char, temp[16*(16+5)]); 9622 sixtap_horiz(temp, 16, 9623 reference - 2 * reference_stride, reference_stride, 9624 cols, rows + 5, filters[mx]); 9626 sixtap_vert(output, output_stride, 9627 temp + 2 * 16, 16, 9628 cols, rows, filters[my]); 9629 } 9631 struct img_index 9632 { 9633 unsigned char *y, *u, *v; 9634 int stride, uv_stride; 9635 }; 9637 static const unsigned char * 9638 filter_block(unsigned char *output, 9639 const unsigned char *reference, 9640 int stride, 9641 const union mv *mv, 9642 const filter_t filters[8]) 9643 { 9644 int mx, my; 9646 /* Handle 0,0 as a special case. TODO: does this make it any 9647 * faster? 9648 */ 9649 if (!mv->raw) 9650 return reference; 9652 mx = mv->d.x & 7; 9653 my = mv->d.y & 7; 9654 reference += ((mv->d.y >> 3) * stride) + (mv->d.x >> 3); 9656 if (mx | my) 9657 { 9658 sixtap_2d(output, stride, reference, stride, 4, 4, mx, my, 9659 filters); 9660 reference = output; 9661 } 9663 return reference; 9664 } 9666 static void 9667 recon_1_block(unsigned char *output, 9668 const unsigned char *reference, 9669 int stride, 9670 const union mv *mv, 9671 const filter_t filters[8], 9672 short *coeffs, 9673 struct mb_info *mbi, 9674 int b 9675 ) 9676 { 9677 const unsigned char *predict; 9679 predict = filter_block(output, reference, stride, mv, filters); 9680 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9681 } 9683 static mv_t 9684 calculate_chroma_splitmv(struct mb_info *mbi, 9685 int b, 9686 int full_pixel) 9687 { 9688 int temp; 9689 union mv mv; 9691 temp = mbi->split.mvs[b].d.x + 9692 mbi->split.mvs[b+1].d.x + 9693 mbi->split.mvs[b+4].d.x + 9694 mbi->split.mvs[b+5].d.x; 9696 if (temp < 0) 9697 temp -= 4; 9698 else 9699 temp += 4; 9701 mv.d.x = temp / 8; 9703 temp = mbi->split.mvs[b].d.y + 9704 mbi->split.mvs[b+1].d.y + 9705 mbi->split.mvs[b+4].d.y + 9706 mbi->split.mvs[b+5].d.y; 9708 if (temp < 0) 9709 temp -= 4; 9710 else 9711 temp += 4; 9713 mv.d.y = temp / 8; 9715 if (full_pixel) 9716 { 9717 mv.d.x &= ~7; 9718 mv.d.y &= ~7; 9719 } 9721 return mv; 9722 } 9724 /* Note: We rely on the reconstructed border having the same stride as 9725 * the reference buffer because the filter_block can't adjust the 9726 * stride with its return value, only the reference pointer. 9727 */ 9728 static void 9729 build_mc_border(unsigned char *dst, 9730 const unsigned char *src, 9731 int stride, 9732 int x, 9733 int y, 9734 int b_w, 9735 int b_h, 9736 int w, 9737 int h 9738 ) 9739 { 9740 const unsigned char *ref_row; 9742 /* Get a pointer to the start of the real data for this row */ 9743 ref_row = src - x - y * stride; 9745 if (y >= h) 9746 ref_row += (h - 1) * stride; 9747 else if (y > 0) 9748 ref_row += y * stride; 9750 do 9751 { 9752 int left, right = 0, copy; 9754 left = x < 0 ? -x : 0; 9756 if (left > b_w) 9757 left = b_w; 9759 if (x + b_w > w) 9760 right = x + b_w - w; 9762 if (right > b_w) 9763 right = b_w; 9765 copy = b_w - left - right; 9767 if (left) 9768 memset(dst, ref_row[0], left); 9770 if (copy) 9771 memcpy(dst + left, ref_row + x + left, copy); 9773 if (right) 9774 memset(dst + left + copy, ref_row[w-1], right); 9776 dst += stride; 9777 y++; 9779 if (y < h && y > 0) 9780 ref_row += stride; 9781 } 9782 while (--b_h); 9783 } 9785 static void 9786 recon_1_edge_block(unsigned char *output, 9787 unsigned char *emul_block, 9788 const unsigned char *reference, 9789 int stride, 9790 const union mv *mv, 9791 const filter_t filters[8], 9792 short *coeffs, 9793 struct mb_info *mbi, 9794 int x, 9795 int y, 9796 int w, 9797 int h, 9798 int start_b 9799 ) 9800 { 9801 const unsigned char *predict; 9802 int b = start_b; 9803 const int b_w = 4; 9804 const int b_h = 4; 9806 x += mv->d.x >> 3; 9807 y += mv->d.y >> 3; 9809 /* Need two pixels left/above, 3 right/below for 6-tap */ 9810 if (x < 2 || x + b_w - 1 + 3 >= w || y < 2 || y + b_h - 1 + 3 >= h) 9811 { 9812 reference += (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9813 build_mc_border(emul_block, 9814 reference - 2 - 2 * stride, stride, 9815 x - 2, y - 2, b_w + 5, b_h + 5, w, h); 9816 reference = emul_block + 2 * stride + 2; 9817 reference -= (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9818 } 9820 predict = filter_block(output, reference, stride, mv, filters); 9821 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9822 } 9824 static void 9825 predict_inter_emulated_edge(struct vp8_decoder_ctx *ctx, 9826 struct img_index *img, 9827 short *coeffs, 9828 struct mb_info *mbi, 9829 int mb_col, 9830 int mb_row) 9831 { 9832 /* TODO: move this into its own buffer. This only works because we 9833 * still have a border allocated. 9834 */ 9835 unsigned char *emul_block = ctx->frame_strg[0].img.img_data; 9836 unsigned char *reference; 9837 unsigned char *output; 9838 ptrdiff_t reference_offset; 9839 int w, h, x, y, b; 9840 union mv chroma_mv[4]; 9841 unsigned char *u = img->u, *v = img->v; 9842 int full_pixel = ctx->frame_hdr.version == 3; 9844 x = mb_col * 16; 9845 y = mb_row * 16; 9846 w = ctx->mb_cols * 16; 9847 h = ctx->mb_rows * 16; 9848 output = img->y; 9849 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9850 reference = output + reference_offset; 9852 if (mbi->base.y_mode != SPLITMV) 9853 { 9854 union mv uvmv; 9856 uvmv = mbi->base.mv; 9857 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9858 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9860 if (full_pixel) 9861 { 9862 uvmv.d.x &= ~7; 9863 uvmv.d.y &= ~7; 9864 } 9866 chroma_mv[0] = uvmv; 9867 chroma_mv[1] = uvmv; 9868 chroma_mv[2] = uvmv; 9869 chroma_mv[3] = uvmv; 9870 } 9871 else 9872 { 9873 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9874 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9875 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9876 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9877 } 9879 /* Luma */ 9880 for (b = 0; b < 16; b++) 9881 { 9882 union mv *ymv; 9884 if (mbi->base.y_mode != SPLITMV) 9885 ymv = &mbi->base.mv; 9886 else 9887 ymv = mbi->split.mvs + b; 9889 recon_1_edge_block(output, emul_block, reference, img->stride, 9890 ymv, ctx->subpixel_filters, 9891 coeffs, mbi, x, y, w, h, b); 9893 x += 4; 9894 output += 4; 9895 reference += 4; 9897 if ((b & 3) == 3) 9898 { 9899 x -= 16; 9900 y += 4; 9901 output += 4 * img->stride - 16; 9902 reference += 4 * img->stride - 16; 9903 } 9904 } 9905 x = mb_col * 16; 9906 y = mb_row * 16; 9908 /* Chroma */ 9909 x >>= 1; 9910 y >>= 1; 9911 w >>= 1; 9912 h >>= 1; 9914 for (b = 0; b < 4; b++) 9915 { 9916 recon_1_edge_block(u, emul_block, u + reference_offset, 9917 img->uv_stride, 9918 &chroma_mv[b], ctx->subpixel_filters, 9919 coeffs, mbi, x, y, w, h, b + 16); 9920 recon_1_edge_block(v, emul_block, v + reference_offset, 9921 img->uv_stride, 9922 &chroma_mv[b], ctx->subpixel_filters, 9923 coeffs, mbi, x, y, w, h, b + 20); 9924 u += 4; 9925 v += 4; 9926 x += 4; 9928 if (b & 1) 9929 { 9930 x -= 8; 9931 y += 4; 9932 u += 4 * img->uv_stride - 8; 9933 v += 4 * img->uv_stride - 8; 9934 } 9935 } 9937 } 9939 static void 9940 predict_inter(struct vp8_decoder_ctx *ctx, 9941 struct img_index *img, 9942 short *coeffs, 9943 struct mb_info *mbi) 9944 { 9945 unsigned char *y = img->y; 9946 unsigned char *u = img->u; 9947 unsigned char *v = img->v; 9948 ptrdiff_t reference_offset; 9949 union mv chroma_mv[4]; 9950 int full_pixel = ctx->frame_hdr.version == 3; 9951 int b; 9952 if (mbi->base.y_mode != SPLITMV) 9953 { 9954 union mv uvmv; 9956 uvmv = mbi->base.mv; 9957 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9958 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9960 if (full_pixel) 9961 { 9962 uvmv.d.x &= ~7; 9963 uvmv.d.y &= ~7; 9964 } 9966 chroma_mv[0] = 9967 chroma_mv[1] = 9968 chroma_mv[2] = 9969 chroma_mv[3] = uvmv; 9970 } 9971 else 9972 { 9973 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9974 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9975 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9976 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9977 } 9979 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9981 for (b = 0; b < 16; b++) 9982 { 9983 union mv *ymv; 9985 if (mbi->base.y_mode != SPLITMV) 9986 ymv = &mbi->base.mv; 9987 else 9988 ymv = mbi->split.mvs + b; 9990 recon_1_block(y, y + reference_offset, img->stride, 9991 ymv, ctx->subpixel_filters, coeffs, mbi, b); 9992 y += 4; 9994 if ((b & 3) == 3) 9995 y += 4 * img->stride - 16; 9996 } 9998 for (b = 0; b < 4; b++) 9999 { 10000 recon_1_block(u, u + reference_offset, 10001 img->uv_stride, &chroma_mv[b], 10002 ctx->subpixel_filters, coeffs, mbi, b + 16); 10003 recon_1_block(v, v + reference_offset, 10004 img->uv_stride, &chroma_mv[b], 10005 ctx->subpixel_filters, coeffs, mbi, b + 20); 10006 u += 4; 10007 v += 4; 10009 if (b & 1) 10010 { 10011 u += 4 * img->uv_stride - 8; 10012 v += 4 * img->uv_stride - 8; 10013 } 10014 } 10015 } 10017 void 10018 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg) 10019 { 10020 if (rcimg) 10021 { 10022 assert(rcimg->ref_cnt); 10023 rcimg->ref_cnt--; 10024 } 10025 } 10027 struct ref_cnt_img * 10028 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg) 10029 { 10030 rcimg->ref_cnt++; 10031 return rcimg; 10032 } 10034 struct ref_cnt_img * 10035 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames) 10036 { 10037 int i; 10039 for (i = 0; i < NUM_REF_FRAMES; i++) 10040 if (frames[i].ref_cnt == 0) 10041 { 10042 frames[i].ref_cnt = 1; 10043 return &frames[i]; 10045 } 10047 assert(0); 10048 return NULL; 10049 } 10051 static void 10052 fixup_left(unsigned char *predict, 10053 int width, 10054 int stride, 10055 unsigned int row, 10056 enum prediction_mode mode) 10057 { 10058 /* The left column of out-of-frame pixels is taken to be 129, 10059 * unless we're doing DC_PRED, in which case we duplicate the 10060 * above row, unless this is also row 0, in which case we use 10061 * 129. 10062 */ 10063 unsigned char *left = predict - 1; 10064 int i; 10066 if (mode == DC_PRED && row) 10067 { 10068 unsigned char *above = predict - stride; 10070 for (i = 0; i < width; i++) 10071 { 10072 *left = above[i]; 10073 left += stride; 10074 } 10075 } 10076 else 10077 { 10078 /* Need to re-set the above row, in case the above MB was 10079 * DC_PRED. 10080 */ 10081 left -= stride; 10083 for (i = -1; i < width; i++) 10084 { 10085 *left = 129; 10086 left += stride; 10087 } 10088 } 10089 } 10090 static void 10091 fixup_above(unsigned char *predict, 10092 int width, 10093 int stride, 10094 unsigned int col, 10095 enum prediction_mode mode) 10096 { 10097 /* The above row of out-of-frame pixels is taken to be 127, 10098 * unless we're doing DC_PRED, in which case we duplicate the 10099 * left col, unless this is also col 0, in which case we use 10100 * 127. 10101 */ 10102 unsigned char *above = predict - stride; 10103 int i; 10105 if (mode == DC_PRED && col) 10106 { 10107 unsigned char *left = predict - 1; 10109 for (i = 0; i < width; i++) 10110 { 10111 above[i] = *left; 10112 left += stride; 10113 } 10114 } 10115 else 10116 /* Need to re-set the left col, in case the last MB was 10117 * DC_PRED. 10118 */ 10119 memset(above - 1, 127, width + 1); 10121 memset(above + width, 127, 4); // for above-right subblock modes 10122 } 10124 void 10125 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx) 10126 { 10128 int i; 10129 unsigned char *this_frame_base; 10131 if (ctx->frame_hdr.frame_size_updated) 10132 { 10133 for (i = 0; i < NUM_REF_FRAMES; i++) 10134 { 10135 unsigned int w = ctx->mb_cols * 16 + BORDER_PIXELS * 2; 10136 unsigned int h = ctx->mb_rows * 16 + BORDER_PIXELS * 2; 10137 vpx_img_free(&ctx->frame_strg[i].img); 10138 ctx->frame_strg[i].ref_cnt = 0; 10139 ctx->ref_frames[i] = NULL; 10141 if (!vpx_img_alloc(&ctx->frame_strg[i].img, 10142 IMG_FMT_I420, w, h, 16)) 10143 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10144 "Failed to allocate %dx%d" 10145 " framebuffer", 10146 w, h); 10148 vpx_img_set_rect(&ctx->frame_strg[i].img, 10149 BORDER_PIXELS, BORDER_PIXELS, 10150 ctx->frame_hdr.kf.w, ctx->frame_hdr.kf.h); 10152 } 10154 if (ctx->frame_hdr.version) 10155 ctx->subpixel_filters = bilinear_filters; 10156 else 10157 ctx->subpixel_filters = sixtap_filters; 10158 } 10160 /* Find a free framebuffer to predict into */ 10161 if (ctx->ref_frames[CURRENT_FRAME]) 10162 vp8_dixie_release_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 10164 ctx->ref_frames[CURRENT_FRAME] = 10165 vp8_dixie_find_free_ref_frame(ctx->frame_strg); 10166 this_frame_base = ctx->ref_frames[CURRENT_FRAME]->img.img_data; 10168 /* Calculate offsets to the other reference frames */ 10169 for (i = 0; i < NUM_REF_FRAMES; i++) 10170 { 10171 struct ref_cnt_img *ref = ctx->ref_frames[i]; 10173 ctx->ref_frame_offsets[i] = 10174 ref ? ref->img.img_data - this_frame_base : 0; 10175 } 10177 /* TODO: No need to do this on every frame... */ 10178 } 10180 void 10181 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx) 10182 { 10183 int i; 10184 for (i = 0; i < NUM_REF_FRAMES; i++) 10185 { 10186 vpx_img_free(&ctx->frame_strg[i].img); 10187 ctx->frame_strg[i].ref_cnt = 0; 10188 ctx->ref_frames[i] = NULL; 10189 } 10190 } 10192 void 10193 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10194 unsigned int row, 10195 unsigned int start_col, 10196 unsigned int num_cols) 10197 { 10198 struct img_index img; 10199 struct mb_info *mbi; 10200 unsigned int col; 10201 short *coeffs; 10203 /* Adjust pointers based on row, start_col */ 10204 img.stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 10205 img.uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 10206 img.y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 10207 img.u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 10208 img.v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 10209 img.y += (img.stride * row + start_col) * 16; 10210 img.u += (img.uv_stride * row + start_col) * 8; 10211 img.v += (img.uv_stride * row + start_col) * 8; 10212 mbi = ctx->mb_info_rows[row] + start_col; 10213 coeffs = ctx->tokens[row & (ctx->token_hdr.partitions - 1)].coeffs 10214 + 25 * 16 * start_col; 10216 /* Fix up the out-of-frame pixels */ 10217 if (start_col == 0) 10218 { 10219 fixup_left(img.y, 16, img.stride, row, mbi->base.y_mode); 10220 fixup_left(img.u, 8, img.uv_stride, row, mbi->base.uv_mode); 10221 fixup_left(img.v, 8, img.uv_stride, row, mbi->base.uv_mode); 10223 if (row == 0) 10224 *(img.y - img.stride - 1) = 127; 10225 } 10227 for (col = start_col; col < start_col + num_cols; col++) 10228 { 10229 if (row == 0) 10230 { 10231 fixup_above(img.y, 16, img.stride, col, mbi->base.y_mode); 10232 fixup_above(img.u, 8, img.uv_stride, col, 10233 mbi->base.uv_mode); 10234 fixup_above(img.v, 8, img.uv_stride, col, 10235 mbi->base.uv_mode); 10236 } 10238 if (mbi->base.y_mode <= B_PRED) 10239 { 10240 predict_intra_luma(img.y, img.stride, mbi, coeffs); 10241 predict_intra_chroma(img.u, img.v, img.uv_stride, mbi, 10242 coeffs); 10243 } 10244 else 10245 { 10246 if (mbi->base.y_mode != SPLITMV) // && != BPRED 10247 fixup_dc_coeffs(mbi, coeffs); 10249 if (mbi->base.need_mc_border) 10250 predict_inter_emulated_edge(ctx, &img, coeffs, mbi, col, 10251 row); 10252 else 10253 predict_inter(ctx, &img, coeffs, mbi); 10254 } 10256 /* Advance to the next macroblock */ 10257 mbi++; 10258 img.y += 16; 10259 img.u += 8; 10260 img.v += 8; 10261 coeffs += 25 * 16; 10262 } 10264 if (col == ctx->mb_cols) 10265 { 10266 /* Extend the last row by four pixels for intra prediction. 10267 * This will be propagated later by copy_down. 10268 */ 10269 uint32_t *extend = (uint32_t *)(img.y + 15 * img.stride); 10270 uint32_t val = 0x01010101 * img.y[-1 + 15 * img.stride]; 10271 *extend = val; 10272 } 10273 } 10275 ---- End code block ---------------------------------------- 10276 20.15. predict.h 10278 ---- Begin code block -------------------------------------- 10280 /* 10281 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10282 * 10283 * Use of this source code is governed by a BSD-style license 10284 * that can be found in the LICENSE file in the root of the source 10285 * tree. An additional intellectual property rights grant can be 10286 * found in the file PATENTS. All contributing project authors may 10287 * be found in the AUTHORS file in the root of the source tree. 10288 */ 10289 #ifndef PREDICT_H 10290 #define PREDICT_H 10292 void 10293 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx); 10295 void 10296 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx); 10298 void 10299 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10300 unsigned int row, 10301 unsigned int start_col, 10302 unsigned int num_cols); 10304 void 10305 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg); 10307 struct ref_cnt_img * 10308 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg); 10310 struct ref_cnt_img * 10311 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames); 10313 #endif 10315 ---- End code block ---------------------------------------- 10317 20.16. tokens.c 10319 ---- Begin code block -------------------------------------- 10320 /* 10321 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10322 * 10323 * Use of this source code is governed by a BSD-style license 10324 * that can be found in the LICENSE file in the root of the source 10325 * tree. An additional intellectual property rights grant can be 10326 * found in the file PATENTS. All contributing project authors may 10327 * be found in the AUTHORS file in the root of the source tree. 10328 */ 10329 #include "vpx_codec_internal.h" 10330 #include "dixie.h" 10331 #include "tokens.h" 10332 #include 10333 #include 10334 #include 10336 enum 10337 { 10338 EOB_CONTEXT_NODE, 10339 ZERO_CONTEXT_NODE, 10340 ONE_CONTEXT_NODE, 10341 LOW_VAL_CONTEXT_NODE, 10342 TWO_CONTEXT_NODE, 10343 THREE_CONTEXT_NODE, 10344 HIGH_LOW_CONTEXT_NODE, 10345 CAT_ONE_CONTEXT_NODE, 10346 CAT_THREEFOUR_CONTEXT_NODE, 10347 CAT_THREE_CONTEXT_NODE, 10348 CAT_FIVE_CONTEXT_NODE 10349 }; 10350 enum 10351 { 10352 ZERO_TOKEN, 10353 ONE_TOKEN, 10354 TWO_TOKEN, 10355 THREE_TOKEN, 10356 FOUR_TOKEN, 10357 DCT_VAL_CATEGORY1, 10358 DCT_VAL_CATEGORY2, 10359 DCT_VAL_CATEGORY3, 10360 DCT_VAL_CATEGORY4, 10361 DCT_VAL_CATEGORY5, 10362 DCT_VAL_CATEGORY6, 10363 DCT_EOB_TOKEN, 10364 MAX_ENTROPY_TOKENS 10365 }; 10366 struct extrabits 10367 { 10368 short min_val; 10369 short length; 10370 unsigned char probs[12]; 10371 }; 10372 static const unsigned int left_context_index[25] = 10373 { 10374 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10375 4, 4, 5, 5, 6, 6, 7, 7, 8 10376 }; 10377 static const unsigned int above_context_index[25] = 10378 { 10379 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10380 4, 5, 4, 5, 6, 7, 6, 7, 8 10381 }; 10382 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10383 static const unsigned int bands_x[16] = 10384 { 10385 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10386 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10387 }; 10388 #undef X 10389 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10390 { 10391 { 0, -1, { 0, 0, 0, 0, 0, 0, 10392 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10393 { 1, 0, { 0, 0, 0, 0, 0, 0, 10394 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10395 { 2, 0, { 0, 0, 0, 0, 0, 0, 10396 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10397 { 3, 0, { 0, 0, 0, 0, 0, 0, 10398 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10399 { 4, 0, { 0, 0, 0, 0, 0, 0, 10400 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10401 { 5, 0, { 159, 0, 0, 0, 0, 0, 10402 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10403 { 7, 1, { 145, 165, 0, 0, 0, 0, 10404 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10405 { 11, 2, { 140, 148, 173, 0, 0, 0, 10406 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10407 { 19, 3, { 135, 140, 155, 176, 0, 0, 10408 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10409 { 35, 4, { 130, 134, 141, 157, 180, 0, 10410 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10411 { 67, 10, { 129, 130, 133, 140, 153, 177, 10412 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10413 { 0, -1, { 0, 0, 0, 0, 0, 0, 10414 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10416 }; 10417 static const unsigned int zigzag[16] = 10418 { 10419 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10420 }; 10422 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10423 v = (bool_get_bit(bool) ? -value_to_sign \ 10424 : value_to_sign) * dqf[!!c]; 10426 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10427 if (!bool_get(bool, probability)) goto branch; 10429 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10430 if (!bool_get(bool, probability)) \ 10431 { \ 10432 prob = type_probs; \ 10433 if(c<15) {\ 10434 ++c; \ 10435 prob += bands_x[c]; \ 10436 goto branch; \ 10437 }\ 10438 else \ 10439 goto BLOCK_FINISHED; /*for malformed input */\ 10440 } 10442 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10443 DECODE_AND_APPLYSIGN(val) \ 10444 prob = type_probs + (ENTROPY_NODES*2); \ 10445 if(c < 15){\ 10446 b_tokens[zigzag[c]] = v; \ 10447 ++c; \ 10448 goto DO_WHILE; }\ 10449 b_tokens[zigzag[15]] = v; \ 10450 goto BLOCK_FINISHED; 10452 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10453 val += bool_get(bool, extrabits[t].probs[bits_count]) << bits_count; 10455 static int 10456 decode_mb_tokens(struct bool_decoder *bool, 10457 token_entropy_ctx_t left, 10458 token_entropy_ctx_t above, 10459 short *tokens, 10460 enum prediction_mode mode, 10461 coeff_probs_table_t probs, 10462 short factor[TOKEN_BLOCK_TYPES][2]) 10463 { 10464 int i, stop, type; 10465 int c, t, v; 10466 int val, bits_count; 10467 int eob_mask; 10468 short *b_tokens; /* tokens for this block */ 10469 unsigned char *type_probs; /* probabilities for this block type */ 10470 unsigned char *prob; 10471 short *dqf; 10473 eob_mask = 0; 10475 if (mode != B_PRED && mode != SPLITMV) 10476 { 10477 i = 24; 10478 stop = 24; 10479 type = 1; 10480 b_tokens = tokens + 24 * 16; 10481 dqf = factor[TOKEN_BLOCK_Y2]; 10482 } 10483 else 10484 { 10485 i = 0; 10486 stop = 16; 10487 type = 3; 10488 b_tokens = tokens; 10489 dqf = factor[TOKEN_BLOCK_Y1]; 10490 } 10492 /* Save a pointer to the coefficient probs for the current type. 10493 * Need to repeat this whenever type changes. 10494 */ 10495 type_probs = probs[type][0][0]; 10497 BLOCK_LOOP: 10498 t = left[left_context_index[i]] + above[above_context_index[i]]; 10499 c = !type; /* all blocks start at 0 except type 0, which starts 10500 * at 1. */ 10502 prob = type_probs; 10503 prob += t * ENTROPY_NODES; 10505 DO_WHILE: 10506 prob += bands_x[c]; 10507 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], BLOCK_FINISHED); 10509 CHECK_0_: 10511 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10512 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10513 ONE_CONTEXT_NODE_0_); 10514 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10515 LOW_VAL_CONTEXT_NODE_0_); 10516 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10517 HIGH_LOW_CONTEXT_NODE_0_); 10518 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10519 CAT_THREEFOUR_CONTEXT_NODE_0_); 10520 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10521 CAT_FIVE_CONTEXT_NODE_0_); 10522 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10523 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10525 do 10526 { 10527 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count); 10528 bits_count -- ; 10529 } 10530 while (bits_count >= 0); 10532 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10534 CAT_FIVE_CONTEXT_NODE_0_: 10535 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10536 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10537 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10538 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10539 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10540 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10541 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10543 CAT_THREEFOUR_CONTEXT_NODE_0_: 10544 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10545 CAT_THREE_CONTEXT_NODE_0_); 10546 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10547 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10548 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10549 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10550 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10551 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10553 CAT_THREE_CONTEXT_NODE_0_: 10554 val = extrabits[DCT_VAL_CATEGORY3].min_val; 10555 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 10556 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 10557 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 10558 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10560 HIGH_LOW_CONTEXT_NODE_0_: 10561 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 10562 CAT_ONE_CONTEXT_NODE_0_); 10564 val = extrabits[DCT_VAL_CATEGORY2].min_val; 10565 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 10566 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 10567 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10569 CAT_ONE_CONTEXT_NODE_0_: 10570 val = extrabits[DCT_VAL_CATEGORY1].min_val; 10571 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 10572 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10574 LOW_VAL_CONTEXT_NODE_0_: 10575 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 10576 TWO_CONTEXT_NODE_0_); 10577 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 10578 THREE_CONTEXT_NODE_0_); 10579 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 10581 THREE_CONTEXT_NODE_0_: 10582 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 10584 TWO_CONTEXT_NODE_0_: 10585 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 10587 ONE_CONTEXT_NODE_0_: 10588 DECODE_AND_APPLYSIGN(1); 10589 prob = type_probs + ENTROPY_NODES; 10591 if (c < 15) 10592 { 10593 b_tokens[zigzag[c]] = v; 10594 ++c; 10595 goto DO_WHILE; 10596 } 10598 b_tokens[zigzag[15]] = v; 10599 BLOCK_FINISHED: 10600 eob_mask |= (c > 1) << i; 10601 t = (c != !type); // any nonzero data? 10602 eob_mask |= t << 31; 10604 left[left_context_index[i]] = above[above_context_index[i]] = t; 10605 b_tokens += 16; 10607 i++; 10608 if (i < stop) 10609 goto BLOCK_LOOP; 10611 if (i == 25) 10612 { 10613 type = 0; 10614 i = 0; 10615 stop = 16; 10616 type_probs = probs[type][0][0]; 10617 b_tokens = tokens; 10618 dqf = factor[TOKEN_BLOCK_Y1]; 10619 goto BLOCK_LOOP; 10620 } 10622 if (i == 16) 10623 { 10624 type = 2; 10625 type_probs = probs[type][0][0]; 10626 stop = 24; 10627 dqf = factor[TOKEN_BLOCK_UV]; 10628 goto BLOCK_LOOP; 10629 } 10631 return eob_mask; 10632 } 10634 static void 10635 reset_row_context(token_entropy_ctx_t *left) 10636 { 10637 memset(left, 0, sizeof(*left)); 10638 } 10640 static void 10641 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 10642 { 10643 memset(above, 0, cols * sizeof(*above)); 10644 } 10646 static void 10647 reset_mb_context(token_entropy_ctx_t *left, 10648 token_entropy_ctx_t *above, 10649 enum prediction_mode mode) 10650 { 10651 /* Reset the macroblock context on the left and right. We have to 10652 * preserve the context of the second order block if this mode 10653 * would not have updated it. 10654 */ 10655 memset(left, 0, sizeof((*left)[0]) * 8); 10656 memset(above, 0, sizeof((*above)[0]) * 8); 10658 if (mode != B_PRED && mode != SPLITMV) 10659 { 10660 (*left)[8] = 0; 10661 (*above)[8] = 0; 10662 } 10663 } 10665 void 10666 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 10667 unsigned int partition, 10668 unsigned int row, 10669 unsigned int start_col, 10670 unsigned int num_cols) 10671 { 10672 struct token_decoder *tokens = &ctx->tokens[partition]; 10673 short *coeffs = tokens->coeffs + 25 * 16 * start_col; 10674 unsigned int col; 10675 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 10676 + start_col; 10677 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 10678 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 10680 if (row == 0) 10681 reset_above_context(above, num_cols); 10683 if (start_col == 0) 10684 reset_row_context(left); 10686 for (col = start_col; col < start_col + num_cols; col++) 10687 { 10688 memset(coeffs, 0, 25 * 16 * sizeof(short)); 10690 if (mbi->base.skip_coeff) 10691 { 10692 reset_mb_context(left, above, mbi->base.y_mode); 10693 mbi->base.eob_mask = 0; 10694 } 10695 else 10696 { 10697 struct dequant_factors *dqf; 10699 dqf = ctx->dequant_factors + mbi->base.segment_id; 10700 mbi->base.eob_mask = 10701 decode_mb_tokens(&tokens->bool, 10702 *left, *above, 10703 coeffs, 10704 mbi->base.y_mode, 10705 ctx->entropy_hdr.coeff_probs, 10706 dqf->factor); 10707 } 10709 above++; 10710 mbi++; 10711 coeffs += 25 * 16; 10712 } 10713 } 10715 void 10716 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 10717 { 10718 unsigned int partitions = ctx->token_hdr.partitions; 10720 if (ctx->frame_hdr.frame_size_updated) 10721 { 10722 unsigned int i; 10723 unsigned int coeff_row_sz = 10724 ctx->mb_cols * 25 * 16 * sizeof(short); 10726 for (i = 0; i < partitions; i++) 10727 { 10728 free(ctx->tokens[i].coeffs); 10729 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 10731 if (!ctx->tokens[i].coeffs) 10732 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10733 NULL); 10734 } 10736 free(ctx->above_token_entropy_ctx); 10737 ctx->above_token_entropy_ctx = 10738 calloc(ctx->mb_cols, sizeof(*ctx->above_token_entropy_ctx)); 10740 if (!ctx->above_token_entropy_ctx) 10741 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, NULL); 10742 } 10743 } 10745 void 10746 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 10747 { 10748 int i; 10750 for (i = 0; i < MAX_PARTITIONS; i++) 10751 free(ctx->tokens[i].coeffs); 10753 free(ctx->above_token_entropy_ctx); 10754 } 10756 ---- End code block ---------------------------------------- 10758 20.17. tokens.h 10760 ---- Begin code block -------------------------------------- 10762 /* 10763 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10764 * 10765 * Use of this source code is governed by a BSD-style license 10766 * that can be found in the LICENSE file in the root of the source 10767 * tree. An additional intellectual property rights grant can be 10768 * found in the file PATENTS. All contributing project authors may 10769 * be found in the AUTHORS file in the root of the source tree. 10770 */ 10771 #include "vpx_codec_internal.h" 10772 #include "dixie.h" 10773 #include "tokens.h" 10774 #include 10775 #include 10776 #include 10778 enum 10779 { 10780 EOB_CONTEXT_NODE, 10781 ZERO_CONTEXT_NODE, 10782 ONE_CONTEXT_NODE, 10783 LOW_VAL_CONTEXT_NODE, 10784 TWO_CONTEXT_NODE, 10785 THREE_CONTEXT_NODE, 10786 HIGH_LOW_CONTEXT_NODE, 10787 CAT_ONE_CONTEXT_NODE, 10788 CAT_THREEFOUR_CONTEXT_NODE, 10789 CAT_THREE_CONTEXT_NODE, 10790 CAT_FIVE_CONTEXT_NODE 10791 }; 10792 enum 10793 { 10794 ZERO_TOKEN, 10795 ONE_TOKEN, 10796 TWO_TOKEN, 10797 THREE_TOKEN, 10798 FOUR_TOKEN, 10799 DCT_VAL_CATEGORY1, 10800 DCT_VAL_CATEGORY2, 10801 DCT_VAL_CATEGORY3, 10802 DCT_VAL_CATEGORY4, 10803 DCT_VAL_CATEGORY5, 10804 DCT_VAL_CATEGORY6, 10805 DCT_EOB_TOKEN, 10806 MAX_ENTROPY_TOKENS 10807 }; 10808 struct extrabits 10809 { 10810 short min_val; 10811 short length; 10812 unsigned char probs[12]; 10813 }; 10814 static const unsigned int left_context_index[25] = 10815 { 10816 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10817 4, 4, 5, 5, 6, 6, 7, 7, 8 10818 }; 10819 static const unsigned int above_context_index[25] = 10820 { 10821 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10822 4, 5, 4, 5, 6, 7, 6, 7, 8 10823 }; 10824 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10825 static const unsigned int bands_x[16] = 10826 { 10827 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10828 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10829 }; 10830 #undef X 10831 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10832 { 10833 { 0, -1, { 0, 0, 0, 0, 0, 0, 10834 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10835 { 1, 0, { 0, 0, 0, 0, 0, 0, 10836 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10837 { 2, 0, { 0, 0, 0, 0, 0, 0, 10838 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10839 { 3, 0, { 0, 0, 0, 0, 0, 0, 10840 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10841 { 4, 0, { 0, 0, 0, 0, 0, 0, 10842 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10843 { 5, 0, { 159, 0, 0, 0, 0, 0, 10844 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10845 { 7, 1, { 145, 165, 0, 0, 0, 0, 10846 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10847 { 11, 2, { 140, 148, 173, 0, 0, 0, 10848 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10849 { 19, 3, { 135, 140, 155, 176, 0, 0, 10850 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10851 { 35, 4, { 130, 134, 141, 157, 180, 0, 10852 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10853 { 67, 10, { 129, 130, 133, 140, 153, 177, 10854 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10855 { 0, -1, { 0, 0, 0, 0, 0, 0, 10856 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10857 }; 10858 static const unsigned int zigzag[16] = 10859 { 10860 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10861 }; 10863 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10864 v = (bool_get_bit(bool) ? -value_to_sign \ 10865 : value_to_sign) * dqf[!!c]; 10867 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10868 if (!bool_get(bool, probability)) goto branch; 10870 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10871 if (!bool_get(bool, probability)) \ 10872 { \ 10873 prob = type_probs; \ 10874 if(c<15) {\ 10875 ++c; \ 10876 prob += bands_x[c]; \ 10877 goto branch; \ 10878 }\ 10879 else \ 10880 goto BLOCK_FINISHED; /*for malformed input */\ 10881 } 10883 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10884 DECODE_AND_APPLYSIGN(val) \ 10885 prob = type_probs + (ENTROPY_NODES*2); \ 10886 if(c < 15){\ 10887 b_tokens[zigzag[c]] = v; \ 10888 ++c; \ 10889 goto DO_WHILE; }\ 10890 b_tokens[zigzag[15]] = v; \ 10891 goto BLOCK_FINISHED; 10893 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10894 val += bool_get(bool, extrabits[t].probs[bits_count]) << bits_count; 10896 static int 10897 decode_mb_tokens(struct bool_decoder *bool, 10898 token_entropy_ctx_t left, 10899 token_entropy_ctx_t above, 10900 short *tokens, 10901 enum prediction_mode mode, 10902 coeff_probs_table_t probs, 10903 short factor[TOKEN_BLOCK_TYPES][2]) 10904 { 10905 int i, stop, type; 10906 int c, t, v; 10907 int val, bits_count; 10908 int eob_mask; 10909 short *b_tokens; /* tokens for this block */ 10910 unsigned char *type_probs; /* probabilities for this block type */ 10911 unsigned char *prob; 10912 short *dqf; 10914 eob_mask = 0; 10916 if (mode != B_PRED && mode != SPLITMV) 10917 { 10918 i = 24; 10919 stop = 24; 10920 type = 1; 10921 b_tokens = tokens + 24 * 16; 10922 dqf = factor[TOKEN_BLOCK_Y2]; 10923 } 10924 else 10925 { 10926 i = 0; 10927 stop = 16; 10928 type = 3; 10929 b_tokens = tokens; 10930 dqf = factor[TOKEN_BLOCK_Y1]; 10931 } 10933 /* Save a pointer to the coefficient probs for the current type. 10935 * Need to repeat this whenever type changes. 10936 */ 10937 type_probs = probs[type][0][0]; 10939 BLOCK_LOOP: 10940 t = left[left_context_index[i]] + above[above_context_index[i]]; 10941 c = !type; /* all blocks start at 0 except type 0, which starts 10942 * at 1. */ 10944 prob = type_probs; 10945 prob += t * ENTROPY_NODES; 10947 DO_WHILE: 10948 prob += bands_x[c]; 10949 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], BLOCK_FINISHED); 10951 CHECK_0_: 10952 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10953 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10954 ONE_CONTEXT_NODE_0_); 10955 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10956 LOW_VAL_CONTEXT_NODE_0_); 10957 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10958 HIGH_LOW_CONTEXT_NODE_0_); 10959 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10960 CAT_THREEFOUR_CONTEXT_NODE_0_); 10961 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10962 CAT_FIVE_CONTEXT_NODE_0_); 10963 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10964 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10966 do 10967 { 10968 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count); 10969 bits_count -- ; 10970 } 10971 while (bits_count >= 0); 10973 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10975 CAT_FIVE_CONTEXT_NODE_0_: 10976 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10977 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10978 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10979 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10980 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10981 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10982 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10984 CAT_THREEFOUR_CONTEXT_NODE_0_: 10985 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10986 CAT_THREE_CONTEXT_NODE_0_); 10987 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10988 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10989 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10990 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10991 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10992 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10994 CAT_THREE_CONTEXT_NODE_0_: 10995 val = extrabits[DCT_VAL_CATEGORY3].min_val; 10996 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 10997 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 10998 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 10999 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 11001 HIGH_LOW_CONTEXT_NODE_0_: 11002 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 11003 CAT_ONE_CONTEXT_NODE_0_); 11005 val = extrabits[DCT_VAL_CATEGORY2].min_val; 11006 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 11007 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 11008 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 11010 CAT_ONE_CONTEXT_NODE_0_: 11011 val = extrabits[DCT_VAL_CATEGORY1].min_val; 11012 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 11013 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 11015 LOW_VAL_CONTEXT_NODE_0_: 11016 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 11017 TWO_CONTEXT_NODE_0_); 11018 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 11019 THREE_CONTEXT_NODE_0_); 11020 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 11022 THREE_CONTEXT_NODE_0_: 11023 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 11025 TWO_CONTEXT_NODE_0_: 11026 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 11028 ONE_CONTEXT_NODE_0_: 11029 DECODE_AND_APPLYSIGN(1); 11030 prob = type_probs + ENTROPY_NODES; 11031 if (c < 15) 11032 { 11033 b_tokens[zigzag[c]] = v; 11034 ++c; 11035 goto DO_WHILE; 11036 } 11038 b_tokens[zigzag[15]] = v; 11039 BLOCK_FINISHED: 11040 eob_mask |= (c > 1) << i; 11041 t = (c != !type); // any nonzero data? 11042 eob_mask |= t << 31; 11044 left[left_context_index[i]] = above[above_context_index[i]] = t; 11045 b_tokens += 16; 11047 i++; 11049 if (i < stop) 11050 goto BLOCK_LOOP; 11052 if (i == 25) 11053 { 11054 type = 0; 11055 i = 0; 11056 stop = 16; 11057 type_probs = probs[type][0][0]; 11058 b_tokens = tokens; 11059 dqf = factor[TOKEN_BLOCK_Y1]; 11060 goto BLOCK_LOOP; 11061 } 11063 if (i == 16) 11064 { 11065 type = 2; 11066 type_probs = probs[type][0][0]; 11067 stop = 24; 11068 dqf = factor[TOKEN_BLOCK_UV]; 11069 goto BLOCK_LOOP; 11070 } 11072 return eob_mask; 11073 } 11075 static void 11076 reset_row_context(token_entropy_ctx_t *left) 11077 { 11078 memset(left, 0, sizeof(*left)); 11079 } 11081 static void 11082 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 11083 { 11084 memset(above, 0, cols * sizeof(*above)); 11085 } 11087 static void 11088 reset_mb_context(token_entropy_ctx_t *left, 11089 token_entropy_ctx_t *above, 11090 enum prediction_mode mode) 11091 { 11092 /* Reset the macroblock context on the left and right. We have to 11093 * preserve the context of the second order block if this mode 11094 * would not have updated it. 11095 */ 11096 memset(left, 0, sizeof((*left)[0]) * 8); 11097 memset(above, 0, sizeof((*above)[0]) * 8); 11099 if (mode != B_PRED && mode != SPLITMV) 11100 { 11101 (*left)[8] = 0; 11102 (*above)[8] = 0; 11103 } 11104 } 11106 void 11107 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 11108 unsigned int partition, 11109 unsigned int row, 11110 unsigned int start_col, 11111 unsigned int num_cols) 11112 { 11113 struct token_decoder *tokens = &ctx->tokens[partition]; 11114 short *coeffs = tokens->coeffs + 25 * 16 * start_col; 11115 unsigned int col; 11116 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 11117 + start_col; 11118 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 11119 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 11121 if (row == 0) 11122 reset_above_context(above, num_cols); 11124 if (start_col == 0) 11125 reset_row_context(left); 11127 for (col = start_col; col < start_col + num_cols; col++) 11128 { 11129 memset(coeffs, 0, 25 * 16 * sizeof(short)); 11131 if (mbi->base.skip_coeff) 11132 { 11133 reset_mb_context(left, above, mbi->base.y_mode); 11134 mbi->base.eob_mask = 0; 11135 } 11136 else 11137 { 11138 struct dequant_factors *dqf; 11140 dqf = ctx->dequant_factors + mbi->base.segment_id; 11141 mbi->base.eob_mask = 11142 decode_mb_tokens(&tokens->bool, 11143 *left, *above, 11144 coeffs, 11145 mbi->base.y_mode, 11146 ctx->entropy_hdr.coeff_probs, 11147 dqf->factor); 11148 } 11150 above++; 11151 mbi++; 11152 coeffs += 25 * 16; 11153 } 11154 } 11156 void 11157 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 11158 { 11159 unsigned int partitions = ctx->token_hdr.partitions; 11161 if (ctx->frame_hdr.frame_size_updated) 11162 { 11163 unsigned int i; 11164 unsigned int coeff_row_sz = 11165 ctx->mb_cols * 25 * 16 * sizeof(short); 11167 for (i = 0; i < partitions; i++) 11168 { 11169 free(ctx->tokens[i].coeffs); 11170 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 11171 if (!ctx->tokens[i].coeffs) 11172 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 11173 NULL); 11174 } 11176 free(ctx->above_token_entropy_ctx); 11177 ctx->above_token_entropy_ctx = 11178 calloc(ctx->mb_cols, sizeof(*ctx->above_token_entropy_ctx)); 11180 if (!ctx->above_token_entropy_ctx) 11181 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, NULL); 11182 } 11183 } 11185 void 11186 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 11187 { 11188 int i; 11190 for (i = 0; i < MAX_PARTITIONS; i++) 11191 free(ctx->tokens[i].coeffs); 11193 free(ctx->above_token_entropy_ctx); 11194 } 11196 ---- End code block ---------------------------------------- 11198 20.18. vp8_prob_data.h 11200 ---- Begin code block -------------------------------------- 11202 static const 11203 unsigned char k_coeff_entropy_update_probs[BLOCK_TYPES][COEF_BANDS] 11204 [PREV_COEF_CONTEXTS] 11205 [ENTROPY_NODES] = 11206 { 11207 { 11208 { 11209 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11210 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11211 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11212 }, 11213 { 11214 {176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11215 {223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11216 {249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11218 }, 11219 { 11220 {255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11221 {234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11222 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11223 }, 11224 { 11225 {255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11226 {239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11227 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11228 }, 11229 { 11230 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11231 {251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11232 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11233 }, 11234 { 11235 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11236 {251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11237 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11238 }, 11239 { 11240 {255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255, }, 11241 {250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255, }, 11242 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11243 }, 11244 { 11245 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11246 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11247 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11248 }, 11249 }, 11250 { 11251 { 11252 {217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11253 {225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255, }, 11254 {234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255, }, 11255 }, 11256 { 11257 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11258 {223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11259 {238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, }, 11260 }, 11261 { 11262 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11263 {249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11264 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11265 }, 11266 { 11267 {255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11268 {247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11269 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11270 }, 11271 { 11272 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11273 {252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11274 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11275 }, 11276 { 11277 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11278 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11279 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11280 }, 11281 { 11282 {255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11283 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11284 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11285 }, 11286 { 11287 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11288 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11289 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11290 }, 11291 }, 11292 { 11293 { 11294 {186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255, }, 11295 {234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255, }, 11296 {251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255, }, 11297 }, 11298 { 11299 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11300 {236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11301 {251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255, }, 11302 }, 11303 { 11304 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11305 {254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11306 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11307 }, 11308 { 11309 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11310 {254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11311 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11312 }, 11313 { 11314 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11315 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11316 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11317 }, 11318 { 11319 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11320 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11321 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11322 }, 11323 { 11324 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11325 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11326 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11327 }, 11328 { 11329 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11330 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11331 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11332 }, 11333 }, 11334 { 11335 { 11336 {248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11337 {250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255, }, 11338 {248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255, }, 11339 }, 11340 { 11341 {255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11342 {246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11343 {252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255, }, 11344 }, 11345 { 11346 {255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255, }, 11347 {248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11348 {253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, }, 11349 }, 11350 { 11351 {255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11352 {245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11353 {253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11354 }, 11355 { 11356 {255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11357 {252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11358 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11359 }, 11360 { 11361 {255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11362 {249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11363 {255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, }, 11364 }, 11365 { 11366 {255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, }, 11367 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11368 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11369 }, 11370 { 11371 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11372 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11373 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, 11374 }, 11375 }, 11376 }; 11378 static const 11379 unsigned char k_default_y_mode_probs [] = 11380 { 112, 86, 140, 37}; 11382 static const 11383 unsigned char k_default_uv_mode_probs [] = 11384 { 162, 101, 204}; 11386 static const 11387 unsigned char k_default_coeff_probs [BLOCK_TYPES][COEF_BANDS] 11388 [PREV_COEF_CONTEXTS][ENTROPY_NODES] = 11389 { 11390 { /* block type 0 */ 11391 { /* coeff band 0 */ 11392 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11393 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11394 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11395 }, 11396 { /* coeff band 1 */ 11397 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 11398 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 11399 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 11400 }, 11401 { /* coeff band 2 */ 11402 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 11403 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 11404 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 11405 }, 11406 { /* coeff band 3 */ 11407 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 11408 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 11409 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 11410 }, 11411 { /* coeff band 4 */ 11412 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 11413 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 11414 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 11415 }, 11416 { /* coeff band 5 */ 11417 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 11418 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 11419 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 11420 }, 11421 { /* coeff band 6 */ 11422 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 11423 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 11424 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 11425 }, 11426 { /* coeff band 7 */ 11427 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11428 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11429 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11430 } 11431 }, 11432 { /* block type 1 */ 11433 { /* coeff band 0 */ 11434 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 11435 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 11436 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 11437 }, 11438 { /* coeff band 1 */ 11439 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 11440 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 11441 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 11442 }, 11443 { /* coeff band 2 */ 11444 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 11445 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 11446 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 11447 }, 11448 { /* coeff band 3 */ 11449 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 11450 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 11451 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 11452 }, 11453 { /* coeff band 4 */ 11454 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 11455 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 11456 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 11457 }, 11458 { /* coeff band 5 */ 11459 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 11460 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 11461 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 11462 }, 11463 { /* coeff band 6 */ 11464 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 11465 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 11466 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 11467 }, 11468 { /* coeff band 7 */ 11469 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 11470 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11471 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 11472 } 11473 }, 11474 { /* block type 2 */ 11475 { /* coeff band 0 */ 11476 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 11477 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 11478 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 11479 }, 11480 { /* coeff band 1 */ 11481 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 11482 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 11483 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 11484 }, 11485 { /* coeff band 2 */ 11486 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 11487 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 11488 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 11489 }, 11490 { /* coeff band 3 */ 11491 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 11492 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 11493 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 11494 }, 11495 { /* coeff band 4 */ 11496 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11497 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 11498 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11499 }, 11500 { /* coeff band 5 */ 11501 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11502 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11503 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11504 }, 11505 { /* coeff band 6 */ 11506 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 11507 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 11508 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11509 }, 11510 { /* coeff band 7 */ 11511 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11512 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11513 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11514 } 11515 }, 11516 { /* block type 3 */ 11517 { /* coeff band 0 */ 11518 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 11519 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 11520 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 11521 }, 11522 { /* coeff band 1 */ 11523 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 11524 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 11525 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 11526 }, 11527 { /* coeff band 2 */ 11528 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 11529 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 11530 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 11531 }, 11532 { /* coeff band 3 */ 11533 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 11534 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 11535 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 11536 }, 11537 { /* coeff band 4 */ 11538 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 11539 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 11540 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 11541 }, 11542 { /* coeff band 5 */ 11543 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 11544 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 11545 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 11546 }, 11547 { /* coeff band 6 */ 11548 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 11549 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 11550 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 11552 }, 11553 { /* coeff band 7 */ 11554 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11555 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11556 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11557 } 11558 } 11559 }; 11561 static const 11562 unsigned char k_mv_entropy_update_probs[2][MV_PROB_CNT] = 11563 { 11564 { 11565 237, 11566 246, 11567 253, 253, 254, 254, 254, 254, 254, 11568 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 11569 }, 11570 { 11571 231, 11572 243, 11573 245, 253, 254, 254, 254, 254, 254, 11574 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 11575 } 11576 }; 11578 static const 11579 unsigned char k_default_mv_probs[2][MV_PROB_CNT] = 11580 { 11581 { /* row */ 11582 162, /* is short */ 11583 128, /* sign */ 11584 225, 146, 172, 147, 214, 39, 156, /* short tree */ 11585 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 /* long bits */ 11586 }, 11587 { 11588 164, 11589 128, 11590 204, 170, 119, 235, 140, 230, 228, 11591 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 11593 } 11594 }; 11596 ---- End code block ---------------------------------------- 11597 20.19. vpx_codec_internal.h 11599 ---- Begin code block -------------------------------------- 11601 /* 11602 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 11603 * 11604 * Use of this source code is governed by a BSD-style license 11605 * that can be found in the LICENSE file in the root of the source 11606 * tree. An additional intellectual property rights grant can be 11607 * found in the file PATENTS. All contributing project authors may 11608 * be found in the AUTHORS file in the root of the source tree. 11609 */ 11611 /*!\file decoder_impl.h 11612 * \brief Describes the decoder algorithm interface for algorithm 11613 * implementations. 11614 * 11615 * This file defines the private structures and data types that are 11616 * only relevant to implementing an algorithm, as opposed to using 11617 * it. 11618 * 11619 * To create a decoder algorithm class, an interface structure is put 11620 * into the global namespace: 11621 *
11622	    *     my_codec.c:
11623	    *       vpx_codec_iface_t my_codec = {
11624	    *           "My Codec v1.0",
11625	    *           VPX_CODEC_ALG_ABI_VERSION,
11626	    *           ...
11627	    *       };
11628	    *     
11629 * 11630 * An application instantiates a specific decoder instance by using 11631 * vpx_codec_init() and a pointer to the algorithm's interface 11632 * structure: 11633 *
11634	    *     my_app.c:
11635	    *       extern vpx_codec_iface_t my_codec;
11636	    *       {
11637	    *           vpx_codec_ctx_t algo;
11638	    *           res = vpx_codec_init(&algo, &my_codec);
11639	    *       }
11640	    *     
11641 * 11642 * Once initialized, the instance is manged using other functions 11643 * from the vpx_codec_* family. 11645 */ 11646 #ifndef VPX_CODEC_INTERNAL_H 11647 #define VPX_CODEC_INTERNAL_H 11648 #include "vpx_decoder.h" 11649 #include 11651 /*!\brief Current ABI version number 11652 * 11653 * \internal 11654 * If this file is altered in any way that changes the ABI, this 11655 * value must be bumped. Examples include, but are not limited to, 11656 * changing types, removing or reassigning enums, 11657 * adding/removing/rearranging fields to structures 11658 */ 11659 #define VPX_CODEC_INTERNAL_ABI_VERSION (3) 11661 typedef struct vpx_codec_alg_priv vpx_codec_alg_priv_t; 11663 /*!\brief init function pointer prototype 11664 * 11665 * Performs algorithm-specific initialization of the decoder context. 11666 * This function is called by the generic vpx_codec_init() wrapper 11667 * function, so plugins implementing this interface may trust the 11668 * input parameters to be properly initialized. 11669 * 11670 * \param[in] ctx Pointer to this instance's context 11671 * \retval #VPX_CODEC_OK 11672 * The input stream was recognized and decoder initialized. 11673 * \retval #VPX_CODEC_MEM_ERROR 11674 * Memory operation failed. 11675 */ 11676 typedef vpx_codec_err_t (*vpx_codec_init_fn_t)(vpx_codec_ctx_t *ctx); 11678 /*!\brief destroy function pointer prototype 11679 * 11680 * Performs algorithm-specific destruction of the decoder context. 11681 * This function is called by the generic vpx_codec_destroy() wrapper 11682 * function, so plugins implementing this interface may trust the 11683 * input parameters to be properly initialized. 11684 * 11685 * \param[in] ctx Pointer to this instance's context 11686 * \retval #VPX_CODEC_OK 11687 * The input stream was recognized and decoder initialized. 11688 * \retval #VPX_CODEC_MEM_ERROR 11689 * Memory operation failed. 11690 */ 11691 typedef vpx_codec_err_t (*vpx_codec_destroy_fn_t)( 11692 vpx_codec_alg_priv_t *ctx); 11694 /*!\brief parse stream info function pointer prototype 11695 * 11696 * Performs high level parsing of the bitstream. This function is 11697 * called by the generic vpx_codec_parse_stream() wrapper function, 11698 * so plugins implementing this interface may trust the input 11699 * parameters to be properly initialized. 11700 * 11701 * \param[in] data Pointer to a block of data to parse 11702 * \param[in] data_sz Size of the data buffer 11703 * \param[in,out] si Pointer to stream info to update. The size 11704 * member \ref MUST be properly initialized, 11705 * but \ref MAY be clobbered by the 11706 * algorithm. This parameter \ref MAY be 11707 * NULL. 11708 * 11709 * \retval #VPX_CODEC_OK 11710 * Bitstream is parsable and stream information updated 11711 */ 11712 typedef vpx_codec_err_t (*vpx_codec_peek_si_fn_t)( 11713 const uint8_t *data, 11714 unsigned int data_sz, 11715 vpx_codec_stream_info_t *si); 11717 /*!\brief Return information about the current stream. 11718 * 11719 * Returns information about the stream that has been parsed during 11720 * decoding. 11721 * 11722 * \param[in] ctx Pointer to this instance's context 11723 * \param[in,out] si Pointer to stream info to update. The size 11724 * member \ref MUST be properly initialized, 11725 * but \ref MAY be clobbered by the 11726 * algorithm. This parameter \ref MAY be 11727 * NULL. 11728 * 11729 * \retval #VPX_CODEC_OK 11730 * Bitstream is parsable and stream information updated 11731 */ 11732 typedef vpx_codec_err_t (*vpx_codec_get_si_fn_t)( 11733 vpx_codec_alg_priv_t *ctx, 11734 vpx_codec_stream_info_t *si); 11736 /*!\brief control function pointer prototype 11737 * 11738 * This function is used to exchange algorithm specific data with the 11739 * decoder instance. This can be used to implement features specific 11740 * to a particular algorithm. 11741 * 11742 * This function is called by the generic vpx_codec_control() wrapper 11743 * function, so plugins implementing this interface may trust the 11744 * input parameters to be properly initialized. However, this 11745 * interface does not provide type safety for the exchanged data or 11746 * assign meanings to the control codes. Those details should be 11747 * specified in the algorithm's header file. In particular, the 11748 * ctrl_id parameter is guaranteed to exist in the algorithm's 11749 * control mapping table, and the data paramter may be NULL. 11750 * 11751 * 11752 * \param[in] ctx Pointer to this instance's context 11753 * \param[in] ctrl_id Algorithm specific control identifier 11754 * \param[in,out] data Data to exchange with algorithm instance. 11755 * 11756 * \retval #VPX_CODEC_OK 11757 * The internal state data was deserialized. 11758 */ 11759 typedef vpx_codec_err_t (*vpx_codec_control_fn_t)( 11760 vpx_codec_alg_priv_t *ctx, 11761 int ctrl_id, 11762 va_list ap); 11764 /*!\brief control function pointer mapping 11765 * 11766 * This structure stores the mapping between control identifiers and 11767 * implementing functions. Each algorithm provides a list of these 11768 * mappings. This list is searched by the vpx_codec_control() wrapper 11769 * function to determine which function to invoke. The special 11770 * value {0, NULL} is used to indicate end-of-list, and must be 11771 * present. The special value {0, } can be used as a 11772 * catch-all mapping. This implies that ctrl_id values chosen by the 11773 * algorithm \ref MUST be non-zero. 11774 */ 11775 typedef const struct 11776 { 11777 int ctrl_id; 11778 vpx_codec_control_fn_t fn; 11779 } vpx_codec_ctrl_fn_map_t; 11781 /*!\brief decode data function pointer prototype 11782 * 11783 * Processes a buffer of coded data. If the processing results in a 11784 * new decoded frame becoming available, #VPX_CODEC_CB_PUT_SLICE and 11785 * #VPX_CODEC_CB_PUT_FRAME events are generated as appropriate. This 11786 * function is called by the generic vpx_codec_decode() wrapper 11787 * function, so plugins implementing this interface may trust the 11788 * input parameters to be properly initialized. 11789 * 11790 * \param[in] ctx Pointer to this instance's context 11791 * \param[in] data Pointer to this block of new coded data. If 11792 * NULL, a #VPX_CODEC_CB_PUT_FRAME event is 11793 * posted for the previously decoded frame. 11794 * \param[in] data_sz Size of the coded data, in bytes. 11795 * 11796 * \return Returns #VPX_CODEC_OK if the coded data was processed 11797 * completely and future pictures can be decoded without 11798 * error. Otherwise, see the descriptions of the other error 11799 * codes in ::vpx_codec_err_t for recoverability 11800 * capabilities. 11801 */ 11802 typedef vpx_codec_err_t (*vpx_codec_decode_fn_t)( 11803 vpx_codec_alg_priv_t *ctx, 11804 const uint8_t *data, 11805 unsigned int data_sz, 11806 void *user_priv, 11807 long deadline); 11809 /*!\brief Decoded frames iterator 11810 * 11811 * Iterates over a list of the frames available for display. The 11812 * iterator storage should be initialized to NULL to start the 11813 * iteration. Iteration is complete when this function returns NULL. 11814 * 11815 * The list of available frames becomes valid upon completion of the 11816 * vpx_codec_decode call, and remains valid until the next call to 11817 * vpx_codec_decode. 11818 * 11819 * \param[in] ctx Pointer to this instance's context 11820 * \param[in out] iter Iterator storage, initialized to NULL 11821 * 11822 * \return Returns a pointer to an image, if one is ready for 11823 * display. Frames produced will always be in PTS 11824 * (presentation time stamp) order. 11825 */ 11826 typedef vpx_image_t*(*vpx_codec_get_frame_fn_t)( 11827 vpx_codec_alg_priv_t *ctx, 11828 vpx_codec_iter_t *iter); 11830 /*\brief e_xternal Memory Allocation memory map get iterator 11831 * 11832 * Iterates over a list of the memory maps requested by the decoder. 11833 * The iterator storage should be initialized to NULL to start the 11834 * iteration. Iteration is complete when this function returns NULL. 11836 * 11837 * \param[in out] iter Iterator storage, initialized to NULL 11838 * 11839 * \return Returns a pointer to an memory segment descriptor, or NULL 11840 * to indicate end-of-list. 11841 */ 11842 typedef vpx_codec_err_t (*vpx_codec_get_mmap_fn_t)( 11843 const vpx_codec_ctx_t *ctx, 11844 vpx_codec_mmap_t *mmap, 11845 vpx_codec_iter_t *iter); 11847 /*\brief e_xternal Memory Allocation memory map set iterator 11848 * 11849 * Sets a memory descriptor inside the decoder instance. 11850 * 11851 * \param[in] ctx Pointer to this instance's context 11852 * \param[in] mmap Memory map to store. 11853 * 11854 * \retval #VPX_CODEC_OK 11855 * The memory map was accepted and stored. 11856 * \retval #VPX_CODEC_MEM_ERROR 11857 * The memory map was rejected. 11858 */ 11859 typedef vpx_codec_err_t (*vpx_codec_set_mmap_fn_t)( 11860 vpx_codec_ctx_t *ctx, 11861 const vpx_codec_mmap_t *mmap); 11863 typedef vpx_codec_err_t (*vpx_codec_encode_fn_t)( 11864 vpx_codec_alg_priv_t *ctx, 11865 const vpx_image_t *img, 11866 vpx_codec_pts_t pts, 11867 unsigned long duration, 11868 vpx_enc_frame_flags_t flags, 11869 unsigned long deadline); 11870 typedef const vpx_codec_cx_pkt_t*(*vpx_codec_get_cx_data_fn_t)( 11871 vpx_codec_alg_priv_t *ctx, 11872 vpx_codec_iter_t *iter); 11874 typedef vpx_codec_err_t 11875 (*vpx_codec_enc_config_set_fn_t)( 11876 vpx_codec_alg_priv_t *ctx, 11877 const vpx_codec_enc_cfg_t *cfg); 11878 typedef vpx_fixed_buf_t * 11879 (*vpx_codec_get_global_headers_fn_t)(vpx_codec_alg_priv_t *ctx); 11881 typedef vpx_image_t * 11882 (*vpx_codec_get_preview_frame_fn_t)(vpx_codec_alg_priv_t *ctx); 11884 /*!\brief usage configuration mapping 11885 * 11886 * This structure stores the mapping between usage identifiers and 11887 * configuration structures. Each algorithm provides a list of these 11888 * mappings. This list is searched by the 11889 * vpx_codec_enc_config_default() wrapper function to determine which 11890 * config to return. The special value {-1, {0}} is used to indicate 11891 * end-of-list, and must be present. At least one mapping must be 11892 * present, in addition to the end-of-list. 11893 * 11894 */ 11895 typedef const struct 11896 { 11897 int usage; 11898 vpx_codec_enc_cfg_t cfg; 11899 } vpx_codec_enc_cfg_map_t; 11901 #define NOT_IMPLEMENTED 0 11903 /*!\brief Decoder algorithm interface interface 11904 * 11905 * All decoders \ref MUST expose a variable of this type. 11906 */ 11907 struct vpx_codec_iface 11908 { 11909 const char *name; 11910 int abi_version; 11911 vpx_codec_caps_t caps; 11912 vpx_codec_init_fn_t init; 11913 vpx_codec_destroy_fn_t destroy; 11914 vpx_codec_ctrl_fn_map_t *ctrl_maps; 11915 vpx_codec_get_mmap_fn_t get_mmap; 11916 vpx_codec_set_mmap_fn_t set_mmap; 11917 struct 11918 { 11919 vpx_codec_peek_si_fn_t peek_si; 11920 vpx_codec_get_si_fn_t get_si; 11921 vpx_codec_decode_fn_t decode; 11922 vpx_codec_get_frame_fn_t get_frame; 11923 } dec; 11924 struct 11925 { 11926 vpx_codec_enc_cfg_map_t *cfg_maps; 11927 vpx_codec_encode_fn_t encode; 11928 vpx_codec_get_cx_data_fn_t get_cx_data; 11929 vpx_codec_enc_config_set_fn_t cfg_set; 11930 vpx_codec_get_global_headers_fn_t get_glob_hdrs; 11931 vpx_codec_get_preview_frame_fn_t get_preview; 11932 } enc; 11933 }; 11935 /*!\brief Callback function pointer / user data pair storage */ 11936 typedef struct vpx_codec_priv_cb_pair 11937 { 11938 union 11939 { 11940 vpx_codec_put_frame_cb_fn_t put_frame; 11941 vpx_codec_put_slice_cb_fn_t put_slice; 11942 }; 11943 void *user_priv; 11944 } vpx_codec_priv_cb_pair_t; 11946 /*!\brief Instance private storage 11947 * 11948 * This structure is allocated by the algorithm's init function. It 11949 * can be extended in one of two ways. First, a second, algorithm 11950 * specific structure can be allocated and the priv member pointed to 11951 * it. Alternatively, this structure can be made the first member of 11952 * the algorithm specific structure, and the pointer casted to the 11953 * proper type. 11954 */ 11955 struct vpx_codec_priv 11956 { 11957 unsigned int sz; 11958 vpx_codec_iface_t *iface; 11959 struct vpx_codec_alg_priv *alg_priv; 11960 const char *err_detail; 11961 vpx_codec_flags_t init_flags; 11962 struct 11963 { 11964 vpx_codec_priv_cb_pair_t put_frame_cb; 11965 vpx_codec_priv_cb_pair_t put_slice_cb; 11966 } dec; 11967 struct 11968 { 11969 int tbd; 11970 struct vpx_fixed_buf cx_data_dst_buf; 11971 unsigned int cx_data_pad_before; 11972 unsigned int cx_data_pad_after; 11973 vpx_codec_cx_pkt_t cx_data_pkt; 11974 } enc; 11975 }; 11976 #undef VPX_CTRL_USE_TYPE 11977 #define VPX_CTRL_USE_TYPE(id, typ) \ 11978 static typ id##__value(va_list args) \ 11979 {return va_arg(args, typ);} \ 11980 static typ id##__convert(void *x)\ 11981 {\ 11982 union\ 11983 {\ 11984 void *x;\ 11985 typ d;\ 11986 } u;\ 11987 u.x = x;\ 11988 return u.d;\ 11989 } 11991 #undef VPX_CTRL_USE_TYPE_DEPRECATED 11992 #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \ 11993 static typ id##__value(va_list args) \ 11994 {return va_arg(args, typ);} \ 11995 static typ id##__convert(void *x)\ 11996 {\ 11997 union\ 11998 {\ 11999 void *x;\ 12000 typ d;\ 12001 } u;\ 12002 u.x = x;\ 12003 return u.d;\ 12004 } 12006 #define CAST(id, arg) id##__value(arg) 12007 #define RECAST(id, x) id##__convert(x) 12009 /* Internal Utility Functions 12010 * 12011 * The following functions are indended to be used inside algorithms 12012 * as utilities for manipulating vpx_codec_* data structures. 12013 */ 12014 struct vpx_codec_pkt_list 12015 { 12016 unsigned int cnt; 12017 unsigned int max; 12018 struct vpx_codec_cx_pkt pkts[1]; 12019 }; 12021 #define vpx_codec_pkt_list_decl(n)\ 12022 union {struct vpx_codec_pkt_list head;\ 12023 struct {struct vpx_codec_pkt_list head;\ 12024 struct vpx_codec_cx_pkt pkts[n];} alloc;} 12026 #define vpx_codec_pkt_list_init(m)\ 12027 (m)->alloc.head.cnt = 0,\ 12028 (m)->alloc.head.max = \ 12029 sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0]) 12031 int 12032 vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *, 12033 const struct vpx_codec_cx_pkt *); 12035 const vpx_codec_cx_pkt_t* 12036 vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list, 12037 vpx_codec_iter_t *iter); 12039 #include 12040 #include 12041 struct vpx_internal_error_info 12042 { 12043 vpx_codec_err_t error_code; 12044 int has_detail; 12045 char detail[80]; 12046 int setjmp; 12047 jmp_buf jmp; 12048 }; 12050 static void vpx_internal_error(struct vpx_internal_error_info *info, 12051 vpx_codec_err_t error, 12052 const char *fmt, 12053 ...) 12054 { 12055 va_list ap; 12057 info->error_code = error; 12058 info->has_detail = 0; 12060 if (fmt) 12061 { 12062 size_t sz = sizeof(info->detail); 12064 info->has_detail = 1; 12065 va_start(ap, fmt); 12066 vsnprintf(info->detail, sz - 1, fmt, ap); 12067 va_end(ap); 12068 info->detail[sz-1] = '\0'; 12070 } 12072 if (info->setjmp) 12073 longjmp(info->jmp, info->error_code); 12074 } 12075 #endif 12077 ---- End code block ---------------------------------------- 12079 20.20. vpx_decoder.h 12081 ---- Begin code block -------------------------------------- 12083 /* 12084 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 12085 * 12086 * Use of this source code is governed by a BSD-style license 12087 * that can be found in the LICENSE file in the root of the source 12088 * tree. An additional intellectual property rights grant can be 12089 * found in the file PATENTS. All contributing project authors may 12090 * be found in the AUTHORS file in the root of the source tree. 12091 */ 12093 /*!\defgroup decoder Decoder Algorithm Interface 12094 * \ingroup codec 12095 * This abstraction allows applications using this decoder to easily 12096 * support multiple video formats with minimal code duplication. This 12097 * section describes the interface common to all decoders. 12098 * @{ 12099 */ 12101 /*!\file vpx_decoder.h 12102 * \brief Describes the decoder algorithm interface to applications. 12103 * 12104 * This file describes the interface between an application and a 12105 * video decoder algorithm. 12106 * 12107 */ 12108 #ifdef __cplusplus 12109 extern "C" { 12110 #endif 12112 #ifndef VPX_DECODER_H 12113 #define VPX_DECODER_H 12114 #include "vpx_codec.h" 12115 /*!\brief Current ABI version number 12116 * 12117 * \internal 12118 * If this file is altered in any way that changes the ABI, this 12119 * value must be bumped. Examples include, but are not limited 12120 * to, changing types, removing or reassigning enums, 12121 * adding/removing/rearranging fields to structures 12122 */ 12123 #define VPX_DECODER_ABI_VERSION (2 + VPX_CODEC_ABI_VERSION) 12125 /*! \brief Decoder capabilities bitfield 12126 * 12127 * Each decoder advertises the capabilities it supports as part 12128 * of its ::vpx_codec_iface_t interface structure. Capabilities 12129 * are extra interfaces or functionality, and are not required 12130 * to be supported by a decoder. 12131 * 12132 * The available flags are specifiedby VPX_CODEC_CAP_* defines. 12133 */ 12134 #define VPX_CODEC_CAP_PUT_SLICE 0x10000 /**< Will issue put_slice 12135 callbacks */ 12136 #define VPX_CODEC_CAP_PUT_FRAME 0x20000 /**< Will issue put_frame 12137 callbacks */ 12138 #define VPX_CODEC_CAP_POSTPROC 0x40000 /**< Can postprocess decoded 12139 frame */ 12141 /*! \brief Initialization-time Feature Enabling 12142 * 12143 * Certain codec features must be known at initialization time, 12144 * to allow for proper memory allocation. 12145 * 12146 * The available flags are specified by VPX_CODEC_USE_* defines. 12147 */ 12148 #define VPX_CODEC_USE_POSTPROC 0x10000 /**< Postprocess decoded 12149 frame */ 12151 /*!\brief Stream properties 12152 * 12153 * This structure is used to query or set properties of the 12154 * decoded stream. Algorithms may extend this structure with data 12155 * specific to their bitstream by setting the sz member 12156 * appropriately. 12157 */ 12158 typedef struct vpx_codec_stream_info 12159 { 12160 unsigned int sz; /**< Size of this structure */ 12161 unsigned int w; /**< Width (or 0 for unknown/default) */ 12162 unsigned int h; /**< Height (or 0 for unknown/default) */ 12163 unsigned int is_kf; /**< Current frame is a keyframe */ 12164 } vpx_codec_stream_info_t; 12166 /* REQUIRED FUNCTIONS 12167 * 12168 * The following functions are required to be implemented for all 12169 * decoders. They represent the base case functionality expected 12170 * of all decoders. 12171 */ 12173 /*!\brief Initialization Configurations 12174 * 12175 * This structure is used to pass init time configuration options 12176 * to the decoder. 12177 */ 12178 typedef struct vpx_codec_dec_cfg 12179 { 12180 unsigned int threads; /**< Maximum number of threads to use, 12181 default 1 */ 12182 unsigned int w; /**< Width */ 12183 unsigned int h; /**< Height */ 12184 } vpx_codec_dec_cfg_t; /**< alias for struct vpx_codec_dec_cfg */ 12186 /*!\brief Initialize a decoder instance 12187 * 12188 * Initializes a decoder context using the given interface. 12189 * Applications should call the vpx_codec_dec_init convenience 12190 * macro instead of this function directly, to ensure that the 12191 * ABI version number parameter is properly initialized. 12192 * 12193 * In XMA mode (activated by setting VPX_CODEC_USE_XMA in the 12194 * flags parameter), the storage pointed to by the cfg parameter 12195 * must be kept readable and stable until all memory maps have 12196 * been set. 12197 * 12198 * \param[in] ctx Pointer to this instance's context. 12199 * \param[in] iface Pointer to the alogrithm interface to 12200 * use. 12201 * \param[in] cfg Configuration to use, if known. May be 12202 * NULL. 12203 * \param[in] flags Bitfield of VPX_CODEC_USE_* flags 12204 * \param[in] ver ABI version number. Must be set to 12205 * VPX_DECODER_ABI_VERSION 12206 * \retval #VPX_CODEC_OK 12207 * The decoder algorithm initialized. 12208 * \retval #VPX_CODEC_MEM_ERROR 12209 * Memory allocation failed. 12210 */ 12211 vpx_codec_err_t vpx_codec_dec_init_ver( 12212 vpx_codec_ctx_t *ctx, 12213 vpx_codec_iface_t *iface, 12214 vpx_codec_dec_cfg_t *cfg, 12215 vpx_codec_flags_t flags, 12216 int ver); 12218 /*!\brief Convenience macro for vpx_codec_dec_init_ver() 12219 * 12220 * Ensures the ABI version parameter is properly set. 12221 */ 12222 #define vpx_codec_dec_init(ctx, iface, cfg, flags) \ 12223 vpx_codec_dec_init_ver(ctx, iface, cfg, flags, \ 12224 VPX_DECODER_ABI_VERSION) 12226 /*!\brief Parse stream info from a buffer 12227 * 12228 * Performs high level parsing of the bitstream. Construction of 12229 * a decoder context is not necessary. Can be used to determine 12230 * if the bitstream is of the proper format, and to extract 12231 * information from the stream. 12232 * 12233 * \param[in] iface Pointer to the alogrithm interface 12234 * \param[in] data Pointer to a block of data to parse 12235 * \param[in] data_sz Size of the data buffer 12236 * \param[in,out] si Pointer to stream info to update. The 12237 * size member 12238 * \ref MUST be properly initialized, but 12239 * \ref MAY be clobbered by the 12240 * algorithm. This parameter \ref MAY be 12241 * NULL. 12242 * 12243 * \retval #VPX_CODEC_OK 12244 * Bitstream is parsable and stream information updated 12245 */ 12246 vpx_codec_err_t vpx_codec_peek_stream_info( 12247 vpx_codec_iface_t *iface, 12248 const uint8_t *data, 12249 unsigned int data_sz, 12250 vpx_codec_stream_info_t *si); 12252 /*!\brief Return information about the current stream. 12253 * 12254 * Returns information about the stream that has been parsed 12255 * during decoding. 12256 * 12257 * \param[in] ctx Pointer to this instance's context 12258 * \param[in,out] si Pointer to stream info to update. The 12259 * size member \ref MUST be properly 12260 * initialized, but \ref MAY be clobbered 12261 * by the algorithm. This parameter \ref 12262 * MAY be NULL. 12263 * 12264 * \retval #VPX_CODEC_OK 12265 * Bitstream is parsable and stream information updated 12266 */ 12267 vpx_codec_err_t vpx_codec_get_stream_info( 12268 vpx_codec_ctx_t *ctx, 12269 vpx_codec_stream_info_t *si); 12271 /*!\brief Decode data 12272 * 12273 * Processes a buffer of coded data. If the processing results in 12274 * a new decoded frame becoming available, PUT_SLICE and 12275 * PUT_FRAME events may be generated, as appropriate. Encoded 12276 * data \ref MUST be passed in DTS (decode time stamp) order. 12277 * Frames produced will always be in PTS (presentation time 12278 * stamp) order. 12279 * 12280 * \param[in] ctx Pointer to this instance's context 12281 * \param[in] data Pointer to this block of new coded 12282 * data. If NULL, a 12283 * VPX_CODEC_CB_PUT_FRAME event is posted 12284 * for the previously decoded frame. 12285 * \param[in] data_sz Size of the coded data, in bytes. 12286 * \param[in] user_priv Application specific data to associate 12287 * with this frame. 12288 * \param[in] deadline Soft deadline the decoder should 12289 * attempt to meet, in us. Set to zero 12290 * for unlimited. 12291 * 12292 * \return Returns #VPX_CODEC_OK if the coded data was processed 12293 * completely and future pictures can be decoded without 12294 * error. Otherwise, see the descriptions of the other 12295 * error codes in ::vpx_codec_err_t for recoverability 12296 * capabilities. 12297 */ 12298 vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, 12299 const uint8_t *data, 12300 unsigned int data_sz, 12301 void *user_priv, 12302 long deadline); 12304 /*!\brief Decoded frames iterator 12305 * 12306 * Iterates over a list of the frames available for display. The 12307 * iterator storage should be initialized to NULL to start the 12308 * iteration. Iteration is complete when this function returns 12309 * NULL. 12310 * 12311 * The list of available frames becomes valid upon completion of 12312 * the vpx_codec_decode call, and remains valid until the next 12313 * call to vpx_codec_decode. 12314 * 12315 * \param[in] ctx Pointer to this instance's context 12316 * \param[in,out] iter Iterator storage, initialized to NULL 12317 * 12318 * \return Returns a pointer to an image, if one is ready for 12319 * display. Frames produced will always be in PTS 12320 * (presentation time stamp) order. 12321 */ 12322 vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, 12323 vpx_codec_iter_t *iter); 12325 /*!\defgroup cap_put_frame Frame-Based Decoding Functions 12326 * 12327 * The following functions are required to be implemented for all 12328 * decoders that advertise the VPX_CODEC_CAP_PUT_FRAME 12329 * capability. Calling these functions for codecs that don't 12330 * advertise this capability will result in an error code being 12331 * returned, usually VPX_CODEC_ERROR 12332 * @{ 12333 */ 12335 /*!\brief put frame callback prototype 12336 * 12337 * This callback is invoked by the decoder to notify the 12338 * application of the availability of decoded image data. 12339 */ 12340 typedef void (*vpx_codec_put_frame_cb_fn_t)( 12341 void *user_priv, 12342 const vpx_image_t *img); 12344 /*!\brief Register for notification of frame completion. 12345 * 12346 * Registers a given function to be called when a decoded frame 12347 * is available. 12348 * 12349 * \param[in] ctx Pointer to this instance's context 12350 * \param[in] cb Pointer to the callback function 12351 * \param[in] user_priv User's private data 12352 * 12353 * \retval #VPX_CODEC_OK 12354 * Callback successfully registered. 12355 * \retval #VPX_CODEC_ERROR 12356 * Decoder context not initialized, or algorithm not capable 12357 * of posting slice completion. 12358 */ 12359 vpx_codec_err_t vpx_codec_register_put_frame_cb( 12360 vpx_codec_ctx_t *ctx, 12361 vpx_codec_put_frame_cb_fn_t cb, 12362 void *user_priv); 12364 /*!@} - end defgroup cap_put_frame */ 12366 /*!\defgroup cap_put_slice Slice-Based Decoding Functions 12367 * 12368 * The following functions are required to be implemented for all 12369 * decoders that advertise the VPX_CODEC_CAP_PUT_SLICE 12370 * capability. Calling these functions for codecs that don't 12371 * advertise this capability will result in an error code being 12372 * returned, usually VPX_CODEC_ERROR 12373 * @{ 12374 */ 12376 /*!\brief put slice callback prototype 12377 * 12378 * This callback is invoked by the decoder to notify the 12379 * application of the availability of partially decoded image 12380 * data. 12381 */ 12382 typedef void (*vpx_codec_put_slice_cb_fn_t)( 12383 void *user_priv, 12384 const vpx_image_t *img, 12385 const vpx_image_rect_t *valid, 12386 const vpx_image_rect_t *update); 12388 /*!\brief Register for notification of slice completion. 12389 * 12390 * Registers a given function to be called when a decoded slice 12391 * is available. 12392 * 12393 * \param[in] ctx Pointer to this instance's context 12394 * \param[in] cb Pointer to the callback function 12395 * \param[in] user_priv User's private data 12396 * 12397 * \retval #VPX_CODEC_OK 12398 * Callback successfully registered. 12399 * \retval #VPX_CODEC_ERROR 12400 * Decoder context not initialized, or algorithm not capable 12401 * of posting slice completion. 12402 */ 12403 vpx_codec_err_t vpx_codec_register_put_slice_cb( 12404 vpx_codec_ctx_t *ctx, 12405 vpx_codec_put_slice_cb_fn_t cb, 12406 void *user_priv); 12408 /*!@} - end defgroup cap_put_slice*/ 12410 /*!@} - end defgroup decoder*/ 12412 #endif 12414 #ifdef __cplusplus 12415 } 12416 #endif 12418 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 12419 #include "vpx_decoder_compat.h" 12420 #endif 12422 ---- End code block ---------------------------------------- 12424 20.21. vpx_integer.h 12426 ---- Begin code block -------------------------------------- 12428 /* 12429 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 12430 * 12431 * Use of this source code is governed by a BSD-style license 12432 * that can be found in the LICENSE file in the root of the source 12433 * tree. An additional intellectual property rights grant can be 12434 * found in the file PATENTS. All contributing project authors may 12435 * be found in the AUTHORS file in the root of the source tree. 12436 */ 12438 #ifndef VPX_INTEGER_H 12439 #define VPX_INTEGER_H 12441 /* get ptrdiff_t, size_t, wchar_t, NULL */ 12442 #include 12444 #if defined(_MSC_VER) || defined(VPX_EMULATE_INTTYPES) 12445 typedef signed char int8_t; 12446 typedef signed short int16_t; 12447 typedef signed int int32_t; 12449 typedef unsigned char uint8_t; 12450 typedef unsigned short uint16_t; 12451 typedef unsigned int uint32_t; 12453 #if defined(_MSC_VER) 12454 typedef signed __int64 int64_t; 12455 typedef unsigned __int64 uint64_t; 12456 #define PRId64 "I64d" 12457 #endif 12459 #ifdef HAVE_ARMV6 12460 typedef unsigned int int_fast16_t; 12461 #else 12462 typedef signed short int_fast16_t; 12463 #endif 12464 typedef signed char int_fast8_t; 12465 typedef unsigned char uint_fast8_t; 12467 #ifndef _UINTPTR_T_DEFINED 12468 typedef unsigned int uintptr_t; 12469 #endif 12471 #else 12473 /* Most platforms have the C99 standard integer types. */ 12475 #if defined(__cplusplus) && !defined(__STDC_FORMAT_MACROS) 12476 #define __STDC_FORMAT_MACROS 12477 #endif 12478 #include 12479 #include 12481 #endif 12483 #endif 12484 ---- End code block ---------------------------------------- 12486 20.22. AUTHORS 12488 Aaron Watry 12490 Adrian Grange 12492 Alex Converse 12494 Andoni Morales Alastruey 12496 Andres Mejia 12498 Attila Nagy 12500 Fabio Pedretti 12502 Frank Galligan 12504 Fredrik Soederquist 12506 Fritz Koenig 12508 Gaute Strokkenes 12510 Giuseppe Scrivano 12512 Guillermo Ballester Valor 12514 Henrik Lundin 12516 James Berry 12518 James Zern 12520 Jan Kratochvil 12522 Jeff Muizelaar 12524 Jim Bankoski 12526 Johann Koenig 12528 John Koleszar 12530 Justin Clift 12531 Justin Lebar 12533 Luca Barbato 12535 Makoto Kato 12537 Martin Ettl 12539 Michael Kohler 12541 Mikhal Shemer 12543 Pascal Massimino 12545 Patrik Westin 12547 Paul Wilkins 12549 Pavol Rusnak 12551 Philip Jaegenstedt 12553 Scott LaVarnway 12555 Tero Rintaluoma 12557 Timothy B. Terriberry 12559 Tom Finegan 12561 Yaowu Xu 12563 Yunqing Wang 12565 Google Inc. 12567 The Mozilla Foundation 12569 The Xiph.Org Foundation 12571 20.23. LICENSE 12573 Copyright (c) 2010, Google Inc. All rights reserved. 12575 Redistribution and use in source and binary forms, with or without 12576 modification, are permitted provided that the following conditions 12577 are met: 12579 o Redistributions of source code must retain the above copyright 12580 notice, this list of conditions and the following disclaimer. 12582 o Redistributions in binary form must reproduce the above copyright 12583 notice, this list of conditions and the following disclaimer in 12584 the documentation and/or other materials provided with the 12585 distribution. 12587 o Neither the name of Google nor the names of its contributors may 12588 be used to endorse or promote products derived from this software 12589 without specific prior written permission. 12591 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 12592 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 12593 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 12594 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 12595 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 12596 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 12597 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 12598 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 12599 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 12600 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 12601 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 12602 POSSIBILITY OF SUCH DAMAGE. 12604 20.24. PATENTS 12606 Additional IP Rights Grant (Patents) 12608 "This implementation" means the copyrightable works distributed by 12609 Google as part of the WebM Project. 12611 Google hereby grants to you a perpetual, worldwide, non-exclusive, 12612 no-charge, royalty-free, irrevocable (except as stated in this 12613 section) patent license to make, have made, use, offer to sell, sell, 12614 import, transfer, and otherwise run, modify and propagate the 12615 contents of this implementation of VP8, where such license applies 12616 only to those patent claims, both currently owned by Google and 12617 acquired in the future, licensable by Google that are necessarily 12618 infringed by this implementation of VP8. This grant does not include 12619 claims that would be infringed only as a consequence of further 12620 modification of this implementation. If you or your agent or 12621 exclusive licensee institute or order or agree to the institution of 12622 patent litigation against any entity (including a cross-claim or 12623 counterclaim in a lawsuit) alleging that this implementation of VP8 12624 or any code incorporated within this implementation of VP8 12625 constitutes direct or contributory patent infringement, or inducement 12626 of patent infringement, then any patent rights granted to you under 12627 this License for this implementation of VP8 shall terminate as of the 12628 date such litigation is filed. 12630 21. References 12632 [ITU-R_BT.601] 12633 International Telecommunication Union, "ITU BT.601: Studio 12634 encoding parameters of digital television for standard 4:3 12635 and wide screen 16:9 aspect ratios", January 2007. 12637 [Bell] Bell, T., Cleary, J., and I. Witten, "Text Compression", 12638 1990. 12640 [Kernighan] 12641 Kernighan, B. and D. Ritchie, "The C Programming Language 12642 (2nd edition)", April 1988. 12644 [Loeffler] 12645 Loeffler, C., Ligtenberg , A., and G. Moschytz, "Practical 12646 Fast 1-D DCT Algorithms with 11 Multiplications", 12647 May 1989. 12649 [Shannon] Shannon, C., "A Mathematical Theory of Communication", 12650 Bell System Technical Journal Vol. 27, pp. 379-423, 623- 12651 656, July, October 1948. 12653 Authors' Addresses 12655 James Bankoski 12656 Google Inc. 12658 Email: jimbankoski@google.com 12660 John Koleszar 12661 Google Inc. 12663 Email: jkoleszar@google.com 12665 Lou Quillio 12666 Google Inc. 12668 Email: louquillio@google.com 12670 Janne Salonen 12671 Google Inc. 12673 Email: jsalonen@google.com 12675 Paul Wilkins 12676 Google Inc. 12678 Email: paulwilkins@google.com 12680 Yaowu Xu 12681 Google Inc. 12683 Email: yaowu@google.com