idnits 2.17.1 draft-bankoski-vp8-bitstream-06.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 : ---------------------------------------------------------------------------- ** There are 69 instances of weird spacing in the document. Is it really formatted ragged-right, rather than justified? Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year == Line 644 has weird spacing: '...typedef sign...' == Line 653 has weird spacing: '...def int int32...' == Line 6185 has weird spacing: '...ned int split...' == Line 6626 has weird spacing: '...decoder bool;...' == Line 7061 has weird spacing: '...tioning parti...' == (64 more instances...) == The document seems to lack the recommended RFC 2119 boilerplate, even if it appears to use RFC 2119 keywords. (The document does seem to have the reference to RFC 2119 which the ID-Checklist requires). -- The document date (August 11, 2011) is 4641 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 9974 == Missing Reference: 'A' is mentioned on line 2057, but not defined == Missing Reference: 'L' is mentioned on line 2057, but not defined -- Looks like a reference, but probably isn't: '8' on line 10667 == Missing Reference: '-1' is mentioned on line 9554, but not defined -- Looks like a reference, but probably isn't: '0' on line 11631 -- Looks like a reference, but probably isn't: '1' on line 11620 -- Looks like a reference, but probably isn't: '4' on line 13111 -- Looks like a reference, but probably isn't: '7' on line 9227 -- Looks like a reference, but probably isn't: '9' on line 8673 -- Looks like a reference, but probably isn't: '2' on line 11180 -- Looks like a reference, but probably isn't: '5' on line 9591 -- Looks like a reference, but probably isn't: '6' on line 9226 -- Looks like a reference, but probably isn't: '16' on line 10420 -- Looks like a reference, but probably isn't: '12' on line 10373 == Missing Reference: 'MVPcount' is mentioned on line 4866, but not defined == Missing Reference: 'MVPsign' is mentioned on line 4915, but not defined -- Looks like a reference, but probably isn't: '128' on line 6296 == Missing Reference: 'SIGN' is mentioned on line 8150, but not defined == Missing Reference: 'NEAREST' is mentioned on line 8467, but not defined == Missing Reference: 'NEAR' is mentioned on line 8470, but not defined == Missing Reference: 'BEST' is mentioned on line 8485, but not defined -- Looks like a reference, but probably isn't: '15' on line 8488 -- Looks like a reference, but probably isn't: '10' on line 8673 -- Looks like a reference, but probably isn't: '18' on line 8816 -- Looks like a reference, but probably isn't: '14' on line 8828 == Missing Reference: '-stride' is mentioned on line 9073, but not defined == Missing Reference: '-2' is mentioned on line 9553, but not defined -- Looks like a reference, but probably isn't: '25' on line 10380 -- Looks like a reference, but probably isn't: '80' on line 11647 == Unused Reference: 'RFC2119' is defined on line 13476, but no explicit reference was found in the text == Unused Reference: 'RFC3979' is defined on line 13483, but no explicit reference was found in the text == Unused Reference: 'RFC5378' is defined on line 13489, but no explicit reference was found in the text -- Obsolete informational reference (is this intentional?): RFC 3979 (Obsoleted by RFC 8179) Summary: 1 error (**), 0 flaws (~~), 22 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: February 12, 2012 J. Salonen 6 P. Wilkins 7 Y. Xu 8 Google Inc. 9 August 11, 2011 11 VP8 Data Format and Decoding Guide 12 draft-bankoski-vp8-bitstream-06 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 February 12, 2012. 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 . . . . . . . . . 91 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 . . . . . . . . . . . . . . . . . . . . . . . 239 136 20.16. tokens.c . . . . . . . . . . . . . . . . . . . . . . . . 239 137 20.17. tokens.h . . . . . . . . . . . . . . . . . . . . . . . . 250 138 20.18. vp8_prob_data.h . . . . . . . . . . . . . . . . . . . . 250 139 20.19. vpx_codec_internal.h . . . . . . . . . . . . . . . . . . 259 140 20.20. vpx_decoder.h . . . . . . . . . . . . . . . . . . . . . 269 141 20.21. vpx_decoder_compat.h . . . . . . . . . . . . . . . . . . 276 142 20.22. vpx_image.c . . . . . . . . . . . . . . . . . . . . . . 290 143 20.23. vpx_image.h . . . . . . . . . . . . . . . . . . . . . . 296 144 20.24. vpx_integer.h . . . . . . . . . . . . . . . . . . . . . 302 145 20.25. AUTHORS . . . . . . . . . . . . . . . . . . . . . . . . 303 146 20.26. LICENSE . . . . . . . . . . . . . . . . . . . . . . . . 305 147 20.27. PATENTS . . . . . . . . . . . . . . . . . . . . . . . . 306 148 21. Security Considerations . . . . . . . . . . . . . . . . . . . 307 149 22. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 308 150 23. Informative References . . . . . . . . . . . . . . . . . . . 309 151 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 310 153 1. Introduction 155 This document describes the VP8 compressed video data format, 156 together with a discussion of the decoding procedure for the format. 157 It is intended to be used in conjunction with and as a guide to the 158 reference decoder source code provided in Attachment One. If there 159 are any conflicts between this narrative and the reference source 160 code, the reference source code should be considered correct. The 161 bitstream is defined by the reference source code and not this 162 narrative. 164 Like many modern video compression schemes, VP8 is based on 165 decomposition of frames into square subblocks of pixels, prediction 166 of such subblocks using previously constructed blocks, and adjustment 167 of such predictions (as well as synthesis of unpredicted blocks) 168 using a discrete cosine transform (hereafter abbreviated as DCT). In 169 one special case, however, VP8 uses a "Walsh-Hadamard" transform 170 (hereafter abbreviated as WHT) instead of a DCT. 172 Roughly speaking, such systems reduce datarate by exploiting the 173 temporal and spatial coherence of most video signals. It is more 174 efficient to specify the location of a visually similar portion of a 175 prior frame than it is to specify pixel values. The frequency 176 segregation provided by the DCT and WHT facilitate the exploitation 177 of both spatial coherence in the original signal and the tolerance of 178 the human visual system to moderate losses of fidelity in the 179 reconstituted signal. 181 VP8 augments these basic concepts with, among other things, 182 sophisticated usage of contextual probabilities. The result is a 183 significant reduction in datarate at a given quality. 185 Unlike some similar schemes (the older MPEG formats, for example), 186 VP8 specifies exact values for reconstructed pixels. Specifically, 187 the specification for the DCT and WHT portions of the reconstruction 188 does not allow for any "drift" caused by truncation of fractions. 189 Rather, the algorithm is specified using fixed-precision integer 190 operations exclusively. This greatly facilitates the verification of 191 the correctness of a decoder implementation as well as avoiding 192 difficult-to-predict visual incongruities between such 193 implementations. 195 It should be remarked that, in a complete video playback system, the 196 displayed frames may or may not be identical to the reconstructed 197 frames. Many systems apply a final level of filtering (commonly 198 referred to as postprocessing) to the reconstructed frames prior to 199 viewing. Such postprocessing has no effect on the decoding and 200 reconstruction of subsequent frames (which are predicted using the 201 completely-specified reconstructed frames) and is beyond the scope of 202 this document. In practice, the nature and extent of this sort of 203 postprocessing is dependent on both the taste of the user and on the 204 computational facilities of the playback environment. 206 2. Format Overview 208 VP8 works exclusively with an 8-bit YUV 4:2:0 image format. In this 209 format, each 8-bit pixel in the two chroma planes (U and V) 210 corresponds positionally to a 2x2 block of 8-bit luma pixels in the Y 211 plane; coordinates of the upper left corner of the Y block are of 212 course exactly twice the coordinates of the corresponding chroma 213 pixels. When we refer to pixels or pixel distances without 214 specifying a plane, we are implicitly referring to the Y plane or to 215 the complete image, both of which have the same (full) resolution. 217 As is usually the case, the pixels are simply a large array of bytes 218 stored in rows from top to bottom, each row being stored from left to 219 right. This "left to right" then "top to bottom" raster-scan order 220 is reflected in the layout of the compressed data as well. 222 Provision has been made in the VP8 bitstream header for the support 223 of a secondary YUV color format, in the form of a reserved bit. 225 Occasionally, at very low datarates, a compression system may decide 226 to reduce the resolution of the input signal to facilitate efficient 227 compression. The VP8 data format supports this via optional 228 upscaling of its internal reconstruction buffer prior to output (this 229 is completely distinct from the optional postprocessing discussed 230 earlier, which has nothing to do with decoding per se). This 231 upsampling restores the video frames to their original resolution. 232 In other words, the compression/decompression system can be viewed as 233 a "black box", where the input and output is always at a given 234 resolution. The compressor might decide to "cheat" and process the 235 signal at a lower resolution. In that case, the decompressor needs 236 the ability to restore the signal to its original resolution. 238 Internally, VP8 decomposes each output frame into an array of 239 macroblocks. A macroblock is a square array of pixels whose Y 240 dimensions are 16x16 and whose U and V dimensions are 8x8. 241 Macroblock-level data in a compressed frame occurs (and must be 242 processed) in a raster order similar to that of the pixels comprising 243 the frame. 245 Macroblocks are further decomposed into 4x4 subblocks. Every 246 macroblock has 16 Y subblocks, 4 U subblocks, and 4 V subblocks. Any 247 subblock-level data (and processing of such data) again occurs in 248 raster order, this time in raster order within the containing 249 macroblock. 251 As discussed in further detail below, data can be specified at the 252 levels of both macroblocks and their subblocks. 254 Pixels are always treated, at a minimum, at the level of subblocks, 255 which may be thought of as the "atoms" of the VP8 algorithm. In 256 particular, the 2x2 chroma blocks corresponding to 4x4 Y subblocks 257 are never treated explicitly in the data format or in the algorithm 258 specification. 260 The DCT and WHT always operate at a 4x4 resolution. The DCT is used 261 for the 16Y, 4U and 4V subblocks. The WHT is used (with some but not 262 all prediction modes) to encode a 4x4 array comprising the average 263 intensities of the 16 Y subblocks of a macroblock. These average 264 intensities are, up to a constant normalization factor, nothing more 265 that the zeroth DCT coefficients of the Y subblocks. This "higher- 266 level" WHT is a substitute for the explicit specification of those 267 coefficients, in exactly the same way as the DCT of a subblock 268 substitutes for the specification of the pixel values comprising the 269 subblock. We consider this 4x4 array as a second-order subblock 270 called Y2, and think of a macroblock as containing 24 "real" 271 subblocks and, sometimes, a 25th "virtual" subblock. This is dealt 272 with further in Chapter 13. 274 The frame layout used by the reference decoder may be found in the 275 file vpx_image.h. 277 3. Compressed Frame Types 279 There are only two types of frames in VP8. 281 Intraframes (also called key frames and, in MPEG terminology, 282 I-frames) are decoded without reference to any other frame in a 283 sequence, that is, the decompressor reconstructs such frames 284 beginning from its "default" state. Key frames provide random access 285 (or seeking) points in a video stream. 287 Interframes (also called prediction frames and, in MPEG terminology, 288 P-frames) are encoded with reference to prior frames, specifically 289 all prior frames up to and including the most recent key frame. 290 Generally speaking, the correct decoding of an interframe depends on 291 the correct decoding of the most recent key frame and all ensuing 292 frames. Consequently, the decoding algorithm is not tolerant of 293 dropped frames: In an environment in which frames may be dropped or 294 corrupted, correct decoding will not be possible until a key frame is 295 correctly received. 297 In contrast to MPEG, there is no use of bidirectional prediction. No 298 frame is predicted using frames temporally subsequent to it; there is 299 no analog to an MPEG B-frame. 301 Secondly, VP8 augments these notions with that of alternate 302 prediction frames, called golden frames and altref frames 303 (alternative reference frames). Blocks in an interframe may be 304 predicted using blocks in the immediately previous frame as well as 305 the most recent golden frame or altref frame. Every key frame is 306 automatically golden and altref, and any interframe may optionally 307 replace the most recent golden or altref frame. 309 Golden frames and altref frames may also be used to partially 310 overcome the intolerance to dropped frames discussed above: If a 311 compressor is configured to code golden frames only with reference to 312 the prior golden frame (and key frame) then the "substream" of key 313 and golden frames may be decoded regardless of loss of other 314 interframes. Roughly speaking, the implementation requires (on the 315 compressor side) that golden frames subsume and recode any context 316 updates effected by the intervening interframes. A typical 317 application of this approach is video conferencing, in which 318 retransmission of a prior golden frame and/or a delay in playback 319 until receipt of the next golden frame is preferable to a larger 320 retransmit and/or delay until the next key frame. 322 4. Overview of Compressed Data Format 324 The input to a VP8 decoder is a sequence of compressed frames whose 325 order matches their order in time. Issues such as the duration of 326 frames, the corresponding audio, and synchronization are generally 327 provided by the playback environment and are irrelevant to the 328 decoding process itself, however, to aid in fast seeking a start code 329 is included in the header of each key frame. 331 The decoder is simply presented with a sequence of compressed frames 332 and produces a sequence of decompressed (reconstructed) YUV frames 333 corresponding to the input sequence. As stated in the introduction, 334 the exact pixel values in the reconstructed frame are part of VP8's 335 specification. This document specifies the layout of the compressed 336 frames and gives unambiguous algorithms for the correct production of 337 reconstructed frames. 339 The first frame presented to the decompressor is of course a key 340 frame. This may be followed by any number of interframes; the 341 correct reconstruction of each frame depends on all prior frames up 342 to the key frame. The next key frame restarts this process: The 343 decompressor resets to its default initial condition upon reception 344 of a key frame and the decoding of a key frame (and its ensuing 345 interframes) is completely independent of any prior decoding. 347 At the highest level, every compressed frame has three or more 348 pieces. It begins with an uncompressed data chunk comprising 10 349 bytes in the case of key frames and 3-bytes for inter frames. This 350 is followed by two or more blocks of compressed data (called 351 partitions). These compressed data partitions begin and end on byte 352 boundaries. 354 The first compressed partition has two subsections: 356 1. Header information that applies to the frame as a whole. 358 2. Per-macroblock information specifying how each macroblock is 359 predicted from the already-reconstructed data that is available 360 to the decompressor. 362 As stated above, the macroblock-level information occurs in raster- 363 scan order. 365 The rest of the partitions contain, for each block, the DCT/WHT 366 coefficients (quantized and logically compressed) of the residue 367 signal to be added to the predicted block values. It typically 368 accounts for roughly 70% of the overall datarate. VP8 supports 369 packing the compressed DCT/WHT coefficients' data from macroblock 370 rows into separate partitions. If there is more than one partition 371 for these coefficients, the sizes of the partitions -- except the 372 last partition -- in bytes are also present in the bitstream right 373 after the above first partition. Each of the sizes is a 3-byte data 374 item written in little endian format. These sizes provide the 375 decoder direct access to all DCT/WHT coefficient partitions, which 376 enables parallel processing of the coefficients in a decoder. 378 The separate partitioning of the prediction data and coefficient data 379 also allows flexibility in the implementation of a decompressor: An 380 implementation may decode and store the prediction information for 381 the whole frame and then decode, transform, and add the residue 382 signal to the entire frame, or it may simultaneously decode both 383 partitions, calculating prediction information and adding in the 384 residue signal for each block in order. The length field in the 385 frame tag, which allows decoding of the second partition to begin 386 before the first partition has been completely decoded, is necessary 387 for the second "block-at-a-time" decoder implementation. 389 All partitions are decoded using separate instances of the boolean 390 entropy decoder described in Chapter 7. Although some of the data 391 represented within the partitions is conceptually "flat" (a bit is 392 just a bit with no probabilistic expectation one way or the other), 393 because of the way such coders work, there is never a direct 394 correspondence between a "conceptual bit" and an actual physical bit 395 in the compressed data partitions. Only in the 3 or 10 byte 396 uncompressed chunk described above is there such a physical 397 correspondence. 399 A related matter is that seeking within a partition is not supported. 400 The data must be decompressed and processed (or at least stored) in 401 the order in which it occurs in the partition. 403 While this document specifies the ordering of the partition data 404 correctly, the details and semantics of this data are discussed in a 405 more logical fashion to facilitate comprehension. For example, the 406 frame header contains updates to many probability tables used in 407 decoding per-macroblock data. The latter is often described before 408 the layouts of the probabilities and their updates, even though this 409 is the opposite of their order in the bitstream. 411 5. Overview of the Decoding Process 413 A VP8 decoder needs to maintain four YUV frame buffers whose 414 resolutions are at least equal to that of the encoded image. These 415 buffers hold the current frame being reconstructed, the immediately 416 previous reconstructed frame, the most recent golden frame, and the 417 most recent altref frame. 419 Most implementations will wish to "pad" these buffers with 420 "invisible" pixels that extend a moderate number of pixels beyond all 421 four edges of the visible image. This simplifies interframe 422 prediction by allowing all (or most) prediction blocks -- which are 423 not guaranteed to lie within the visible area of a prior frame -- to 424 address usable image data. 426 Regardless of the amount of padding chosen, the invisible rows above 427 (below) the image are filled with copies of the top (bottom) row of 428 the image; the invisible columns to the left (right) of the image are 429 filled with copies of the leftmost (rightmost) visible row; and the 430 four invisible corners are filled with copies of the corresponding 431 visible corner pixels. The use of these prediction buffers (and 432 suggested sizes for the halo) will be elaborated on in the discussion 433 of motion vectors, interframe prediction, and sub-pixel interpolation 434 later in this document. 436 As will be seen in the description of the frame header, the image 437 dimensions are specified (and can change) with every key frame. 438 These buffers (and any other data structures whose size depends on 439 the size of the image) should be allocated (or re-allocated) 440 immediately after the dimensions are decoded. 442 Leaving most of the details for later elaboration, the following is 443 an outline the decoding process. 445 First, the frame header (beginning of the first data partition) is 446 decoded. Altering or augmenting the maintained state of the decoder, 447 this provides the context in which the per-macroblock data can be 448 interpreted. 450 The macroblock data occurs (and must be processed) in raster-scan 451 order. This data comes in two or more parts. The first (prediction 452 or mode) part comes in the remainder of the first data partition. 453 The other parts comprise the data partition(s) for the DCT/WHT 454 coefficients of the residue signal. For each macroblock, the 455 prediction data must be processed before the residue. 457 Each macroblock is predicted using one (and only one) of four 458 possible frames. All macroblocks in a key frame, and all intra-coded 459 macroblocks in an interframe, are predicted using the already-decoded 460 macroblocks in the current frame. Macroblocks in an interframe may 461 also be predicted using the previous frame, the golden frame or the 462 altref frame. Such macroblocks are said to be inter-coded. 464 The purpose of prediction is to use already-constructed image data to 465 approximate the portion of the original image being reconstructed. 466 The effect of any of the prediction modes is then to write a 467 macroblock-sized prediction buffer containing this approximation. 469 Regardless of the prediction method, the residue DCT signal is 470 decoded, dequantized, reverse-transformed, and added to the 471 prediction buffer to produce the (almost final) reconstruction value 472 of the macroblock, which is stored in the correct position of the 473 current frame buffer. 475 The residue signal consists of 24 (sixteen Y, four U, and four V) 4x4 476 quantized and losslessly-compressed DCT transforms approximating the 477 difference between the original macroblock in the uncompressed source 478 and the prediction buffer. For most prediction modes, the zeroth 479 coefficients of the sixteen Y subblocks are expressed via a 25th WHT 480 of the second-order virtual Y2 subblock discussed above. 482 Intra-prediction exploits the spatial coherence of frames. The 16x16 483 luma (Y) and 8x8 chroma (UV) components are predicted independently 484 of each other using one of four simple means of pixel propagation, 485 starting from the already-reconstructed (16-pixel long luma, 8-pixel 486 long chroma) row above and column to the left of the current 487 macroblock. The four methods are: 489 1. Copying the row from above throughout the prediction buffer. 491 2. Copying the column from left throughout the prediction buffer. 493 3. Copying the average value of the row and column throughout the 494 prediction buffer. 496 4. Extrapolation from the row and column using the (fixed) second 497 difference (horizontal and vertical) from the upper left corner. 499 Additionally, the sixteen Y subblocks may be predicted independently 500 of each other using one of ten different modes, four of which are 4x4 501 analogs of those described above, augmented with six "diagonal" 502 prediction methods. There are two types of predictions, one intra 503 and one prediction (among all the modes), for which the residue 504 signal does not use the Y2 block to encode the DC portion of the 505 sixteen 4x4 Y subblock DCTs. This "independent Y subblock" mode has 506 no effect on the 8x8 chroma prediction. 508 Inter-prediction exploits the temporal coherence between nearby 509 frames. Except for the choice of the prediction frame itself, there 510 is no difference between inter-prediction based on the previous frame 511 and that based on the golden frame or altref frame. 513 Inter-prediction is conceptually very simple. While, for reasons of 514 efficiency, there are several methods of encoding the relationship 515 between the current macroblock and corresponding sections of the 516 prediction frame, ultimately each of the sixteen Y subblocks is 517 related to a 4x4 subblock of the prediction frame, whose position in 518 that frame differs from the current subblock position by a (usually 519 small) displacement. These two-dimensional displacements are called 520 motion vectors. 522 The motion vectors used by VP8 have quarter-pixel precision. 523 Prediction of a subblock using a motion vector that happens to have 524 integer (whole number) components is very easy: the 4x4 block of 525 pixels from the displaced block in the previous, golden, or altref 526 frame are simply copied into the correct position of the current 527 macroblock's prediction buffer. 529 Fractional displacements are conceptually and implementationally more 530 complex. They require the inference (or synthesis) of sample values 531 that, strictly speaking, do not exist. This is one of the most basic 532 problems in signal processing and readers conversant with that 533 subject will see that the approach taken by VP8 provides a good 534 balance of robustness, accuracy, and efficiency. 536 Leaving the details for the implementation discussion below, the 537 pixel interpolation is calculated by applying a kernel filter (using 538 reasonable-precision integer math) three pixels on either side, both 539 horizontally and vertically, of the pixel to be synthesized. The 540 resulting 4x4 block of synthetic pixels is then copied into position 541 exactly as in the case of integer displacements. 543 Each of the eight chroma subblocks is handled similarly. Their 544 motion vectors are never specified explicitly; instead, the motion 545 vector for each chroma subblock is calculated by averaging the 546 vectors of the four Y subblocks that occupy the same area of the 547 frame. Since chroma pixels have twice the diameter (and four times 548 the area) of luma pixels, the calculated chroma motion vectors have 549 1/8 pixel resolution, but the procedure for copying or generating 550 pixels for each subblock is essentially identical to that done in the 551 luma plane. 553 After all the macroblocks have been generated (predicted and 554 corrected with the DCT/WHT residue), a filtering step (the loop 555 filter) is applied to the entire frame. The purpose of the loop 556 filter is to reduce blocking artifacts at the boundaries between 557 macroblocks and between subblocks of the macroblocks. The term loop 558 filter is used because this filter is part of the "coding loop," that 559 is, it affects the reconstructed frame buffers that are used to 560 predict ensuing frames. This is distinguished from the 561 postprocessing filters discussed earlier which affect only the viewed 562 video and do not "feed into" subsequent frames. 564 Next, if signaled in the data, the current frame may replace the 565 golden frame prediction buffer and/or the altref frame buffer. 567 The halos of the frame buffers are next filled as specified above. 568 Finally, at least as far as decoding is concerned, the (references 569 to) the "current" and "last" frame buffers should be exchanged in 570 preparation for the next frame. 572 Various processes may be required (or desired) before viewing the 573 generated frame. As discussed in the frame dimension information 574 below, truncation and/or upscaling of the frame may be required. 575 Some playback systems may require a different frame format (RGB, 576 YUY2, etc.). Finally, as mentioned in the introduction, further 577 postprocessing or filtering of the image prior to viewing may be 578 desired. Since the primary purpose of this document is a decoding 579 specification, the postprocessing is not specified in this document. 581 While the basic ideas of prediction and correction used by VP8 are 582 straightforward, many of the details are quite complex. The 583 management of probabilities is particularly elaborate. Not only do 584 the various modes of intra-prediction and motion vector specification 585 have associated probabilities but they, together with the coding of 586 DCT coefficients and motion vectors, often base these probabilities 587 on a variety of contextual information (calculated from what has been 588 decoded so far), as well as on explicit modification via the frame 589 header. 591 The "top-level" of decoding and frame reconstruction is implemented 592 in the reference decoder files dixie.c. 594 This concludes our summary of decoding and reconstruction; we 595 continue by discussing the individual aspects in more depth. 597 A reasonable "divide and conquer" approach to implementation of a 598 decoder is to begin by decoding streams composed exclusively of key 599 frames. After that works reliably, interframe handling can be added 600 more easily than if complete functionality were attempted 601 immediately. In accordance with this, we first discuss components 602 needed to decode key frames (most of which are also used in the 603 decoding of interframes) and conclude with topics exclusive to 604 interframes. 606 6. Description of Algorithms 608 As the intent of this document, together with the reference decoder 609 source code, is to specify a platform-independent procedure for the 610 decoding and reconstruction of a VP8 video stream, many (small) 611 algorithms must be described exactly. 613 Due to its near-universality, terseness, ability to easily describe 614 calculation at specific precisions, and the fact that On2's reference 615 VP8 decoder is written in C, these algorithm fragments are written 616 using the C programming language, augmented with a few simple 617 definitions below. 619 The standard (and best) reference for C is [Kernighan]. 621 Many code fragments will be presented in this document. Some will be 622 nearly identical to corresponding sections of the reference decoder; 623 others will differ. Roughly speaking, there are three reasons for 624 such differences: 626 1. For reasons of efficiency, the reference decoder version may be 627 less obvious. 629 2. The reference decoder often uses large data structures to 630 maintain context that need not be described or used here. 632 3. The authors of this document felt that a different expression of 633 the same algorithm might facilitate exposition. 635 Regardless of the chosen presentation, the calculation effected by 636 any of the algorithms described here is identical to that effected by 637 the corresponding portion of the reference decoder. 639 All VP8 decoding algorithms use integer math. To facilitate 640 specification of arithmetic precision, we define the following types. 642 ---- Begin code block -------------------------------------- 644 typedef signed char int8; /* signed int exactly 8 bits wide */ 645 typedef unsigned char uint8; /* unsigned "" */ 647 typedef short int16; /* signed int exactly 16 bits wide */ 648 typedef unsigned int16 uint16; /* unsigned "" */ 650 /* int32 is a signed integer type at least 32 bits wide */ 652 typedef long int32; /* guaranteed to work on all systems */ 653 typedef int int32; /* will be more efficient on some systems */ 655 typedef unsigned int32 uint32; 657 /* unsigned integer type, at least 16 bits wide, whose exact size 658 is most convenient to whatever processor we are using */ 660 typedef unsigned int uint; 662 /* While pixels themselves are 8-bit unsigned integers, 663 pixel arithmetic often occurs at 16- or 32-bit precision and 664 the results need to be "saturated" or clamped to an 8-bit 665 range. */ 667 typedef uint8 Pixel; 669 Pixel clamp255( int32 v) { return v < 0? 0 : (v < 255? v : 255);} 671 /* As is elaborated in the discussion of the bool_decoder below, 672 VP8 represents probabilities as unsigned 8-bit numbers. */ 674 typedef uint8 Prob; 676 ---- End code block ---------------------------------------- 678 We occasionally need to discuss mathematical functions involving 679 honest-to-goodness "infinite precision" real numbers. The DCT is 680 first described via the cosine function cos; the ratio of the lengths 681 of the circumference and diameter of a circle is denoted pi; at one 682 point, we take a (base 1/2) logarithm denoted log; and pow( x, y) 683 denotes x raised to the power y. If x = 2 and y is a small non- 684 negative integer, pow( 2, y) may be expressed in C as 1 << y. 686 Finally, we sometimes need to divide signed integers by powers of 687 two, that is, we occasionally right-shift signed numbers. The 688 behavior of such shifts (i.e., the propagation of the sign bit) is, 689 perhaps surprisingly, not defined by the C language itself and is 690 left up to individual compilers. Because of the utility of this 691 frequently needed operation, it is at least arguable that it should 692 be defined by the language (to naturally propagate the sign bit) and, 693 at a minimum, should be correctly implemented by any reasonable 694 compiler. In the interest of strict portability, we attempt to call 695 attention to these shifts when they arise. 697 7. Boolean Entropy Decoder 699 As discussed in the overview above, essentially the entire VP8 data 700 stream is encoded using a boolean entropy coder. 702 An understanding of the bool_decoder is critical to the 703 implementation of a VP8 decompressor, so we discuss in detail. It is 704 easier to comprehend the bool_decoder in conjunction with the 705 bool_encoder used by the compressor to write the compressed data 706 partitions. 708 The bool_encoder encodes (and the bool_decoder decodes) one bool 709 (zero-or-one boolean value) at a time. Its purpose is to losslessly 710 compress a sequence of bools for which the probability of their being 711 zero or one can be well-estimated (via constant or previously-coded 712 information) at the time they are written, using identical 713 corresponding probabilities at the time they are read. 715 As the reader is probably aware, if a bool is much more likely to be 716 zero than one (for instance), it can, on average, be faithfully 717 encoded using much less than one bit per value. The bool_encoder 718 exploits this. 720 In the 1940s, [Shannon] proved that there is a lower bound for the 721 average datarate of a faithful encoding of a sequence of bools (whose 722 probability distributions are known and are independent of each 723 other) and also that there are encoding algorithms that approximate 724 this lower bound as closely as one wishes. 726 If we encode a sequence of bools whose probability of being zero is p 727 (and whose probability of being 1 is 1-p), the lowest possible 728 datarate per value is 730 plog(p) + (1-p)log(1-p); 732 taking the logarithms to the base 1/2 expresses the datarate in bits/ 733 value. 735 We give two simple examples. At one extreme, if p=1/2, then log(p) = 736 log(1-p) = 1 and the lowest possible datarate per bool is 1/2 + 1/2 = 737 1, that is, we cannot do any better than simply literally writing out 738 bits. At another extreme, if p is very small, say p=1/1024, then 739 log(p)=10, log(1-p) is roughly .0014, and the lowest possible 740 datarate is approximately 10/1024 + .0014, roughly 1/100 of a bit per 741 bool. 743 Because most of the bools in the VP8 datastream have zero- 744 probabilities nowhere near 1/2, the compression provided by the 745 bool_encoder is critical to the performance of VP8. 747 The bool coder used by VP8 is a variant of an arithmetic coder. An 748 excellent discussion of arithmetic coding (and other lossless 749 compression techniques) can be found in [Bell]. 751 7.1. Underlying Theory of Coding 753 The basic idea used by the bool coder is to consider the entire data 754 stream (either of the partitions in our case) as the binary expansion 755 of a single number x with 0 <= x < 1. The bits (or bytes) in x are 756 of course written from high to low order and if b[j] (B[j]) is the 757 j^(th) bit (byte) in the partition, the value x is simply the sum 758 (starting with j = 1) of pow(2, -j) * b[j] or pow(256, -j) * B[j]. 760 Before the first bool is coded, all values of x are possible. 762 The coding of each bool restricts the possible values of x in 763 proportion to the probability of what is coded. If p1 is the 764 probability of the first bool being zero and a zero is coded, the 765 range of possible x is restricted to 0 <= x < p1. If a one is coded, 766 the range becomes p1 <= x < 1. 768 The coding continues by repeating the same idea. At every stage, 769 there is an interval a <= x < b of possible values of x. If p is the 770 probability of a zero being coded at this stage and a zero is coded, 771 the interval becomes a <= x < a + (p(b-a)). If a one is coded, the 772 possible x are restricted to a + (p(b-a)) <= x < b. 774 Assuming only finitely many values are to be coded, after the encoder 775 has received the last bool, it can write as its output any value x 776 that lies in the final interval. VP8 simply writes the left endpoint 777 of the final interval. Consequently, the output it would make if 778 encoding were to stop at any time either increases or stays the same 779 as each bool is encoded. 781 Decoding parallels encoding. The decoder is presented with the 782 number x, which has only the initial restriction 0 <= x < 1. To 783 decode the first bool, the decoder is given the first probability p1. 784 If x < p1, a zero is decoded; if x >= p1, a one is decoded. In 785 either case, the new restriction on x, that is, the interval of 786 possible x, is remembered. 788 Decoding continues in exactly the same way: If a <= x < b is the 789 current interval and we are to decode a bool with zero-probability p, 790 we return a zero if a <= x < a + (p(b-a)) and a one if a + (p(b-a)) 791 <= x < b. In either case, the new restriction is remembered in 792 preparation for decoding the next bool. 794 The process outlined above uses real numbers of infinite precision to 795 express the probabilities and ranges. It is true that, if one could 796 actualize this process and coded a large number of bools whose 797 supplied probabilities matched their value distributions, the 798 datarate achieved would approach the theoretical minimum as the 799 number of bools encoded increased. 801 Unfortunately, computers operate at finite precision and an 802 approximation to the theoretically perfect process described above is 803 necessary. Such approximation increases the datarate but, at quite 804 moderate precision and for a wide variety of data sets, this increase 805 is negligible. 807 The only conceptual limitations are, first, that coder probabilities 808 must be expressed at finite precision and, second, that the decoder 809 be able to detect each individual modification to the value interval 810 via examination of a fixed amount of input. As a practical matter, 811 many of the implementation details stem from the fact that the coder 812 can function using only a small "window" to incrementally read or 813 write the arbitrarily precise number x. 815 7.2. Practical Algorithm Description 817 VP8's bool coder works with 8-bit probabilities p. The range of such 818 p is 0 <= p <= 255; the actual probability represented by p is p/256. 819 Also, the coder is designed so that decoding of a bool requires no 820 more than an 8-bit comparison and so that the state of both the 821 encoder and decoder can be easily represented using a small number of 822 unsigned 16-bit integers. 824 The details are most easily understood if we first describe the 825 algorithm using bit-at-a-time input and output. Aside from the 826 ability to maintain a position in this bitstream and write/read bits, 827 the encoder also needs the ability to add 1 to the bits already 828 output; after writing n bits, adding 1 to the existing output is the 829 same thing as adding pow( 2, -n) to x. 831 Together with the bit position, the encoder must maintain two 832 unsigned 8-bit numbers which we call bottom and range. Writing w for 833 the n bits already written and S = pow( 2, - n - 8) for the scale of 834 the current bit position one byte out, we have the following 835 constraint on all future values v of w (including the final value v = 836 x): 838 w + ( S * bottom ) <= v < w + ( S * ( bottom + range ) ) 840 Thus, appending bottom to the already-written bits w gives the left 841 endpoint of the interval of possible values, appending bottom + range 842 gives the right endpoint, range itself (scaled to the current output 843 position) is the length of the interval. 845 So that our probabilistic encodings are reasonably accurate, we do 846 not let range vary by more than a factor of two: It stays within the 847 bounds 128 <= range <= 255. 849 The process for encoding a boolean value val whose probability of 850 being zero is prob / 256 -- and whose probability of being one is ( 851 256 - prob ) / 256 -- with 1 <= prob <= 255 is as follows. 853 Using an unsigned 16-bit multiply followed by an unsigned right 854 shift, we calculate an unsigned 8-bit split value: 856 split = 1 + (((range - 1) * probability)]] >> 8) 858 split is approximately ( prob / 256 ) * range and lies within the 859 bounds 1 <= split <= range - 1. These bounds ensure the correctness 860 of the decoding procedure described below. 862 If the incoming boolean val to be encoded is false, we leave the left 863 interval endpoint bottom alone and reduce range, replacing it by 864 split. If the incoming val is true, we move up the left endpoint to 865 bottom + split, propagating any carry to the already-written value w 866 (this is where we need the ability to add 1 to w), and reduce range 867 to range - split. 869 Regardless of the value encoded, range has been reduced and now has 870 the bounds 1 <= range <= 254. If range < 128, the encoder doubles it 871 and shifts the high-order bit out of bottom to the output as it also 872 doubles bottom, repeating this process one bit at a time until 128 <= 873 range <= 255. Once this is completed, the encoder is ready to accept 874 another bool, maintaining the constraints described above. 876 After encoding the last bool, the partition may be completed by 877 appending bottom to the bitstream. 879 The decoder mimics the state of the encoder. It maintains, together 880 with an input bit position, two unsigned 8-bit numbers, a range 881 identical to that maintained by the encoder and a value. Decoding 882 one bool at a time, the decoder (in effect) tracks the same left 883 interval endpoint as does the encoder and subtracts it from the 884 remaining input. Appending the unread portion of the bitstream to 885 the 8-bit value gives the difference between the actual value encoded 886 and the known left endpoint. 888 The decoder is initialized by setting range = 255 and reading the 889 first 16 input bits into value. The decoder maintains range and 890 calculates split in exactly the same way as does the encoder. 892 To decode a bool, it compares value to split; if value < split, the 893 bool is zero, and range is replaced with split. If value >= split, 894 the bool is one, range is replaced with range - split, and value is 895 replaced with value - split. 897 Again, range is doubled one bit at a time until it is at least 128. 898 The value is doubled in parallel, shifting a new input bit into the 899 bottom each time. 901 Writing Value for value together with the unread input bits and Range 902 for range extended indefinitely on the right by zeros, the condition 903 Value < Range is maintained at all times by the decoder. In 904 particular, the bits shifted out of value as it is doubled are always 905 zero. 907 7.3. Actual Implementation 909 The C code below gives complete implementations of the encoder and 910 decoder described above. While they are logically identical to the 911 "bit-at-a-time" versions, they internally buffer a couple of extra 912 bytes of the bitstream. This allows I/O to be done (more 913 practically) a byte at a time and drastically reduces the number of 914 carries the encoder has to propagate into the already-written data. 916 Another (logically equivalent) implementation may be found in the 917 reference decoder files bool_decoder.h. 919 ---- Begin code block -------------------------------------- 921 /* Encoder first */ 923 typedef struct { 924 uint8 *output; /* ptr to next byte to be written */ 925 uint32 range; /* 128 <= range <= 255 */ 926 uint32 bottom; /* minimum value of remaining output */ 927 int bit_count; /* # of shifts before an output byte 928 is available */ 929 } bool_encoder; 931 /* Must set initial state of encoder before writing any bools. */ 933 void init_bool_encoder( bool_encoder *e, uint8 *start_partition) 934 { 935 e->output = start_partition; 936 e->range = 255; 937 e->bottom = 0; 938 e->bit_count = 24; 939 } 941 /* Encoding very rarely produces a carry that must be propagated 942 to the already-written output. The arithmetic guarantees that 943 the propagation will never go beyond the beginning of the 944 output. Put another way, the encoded value x is always less 945 than one. */ 947 void add_one_to_output( uint8 *q) 948 { 949 while( *--q == 255) 950 *q = 0; 951 ++*q; 952 } 954 /* Main function writes a bool_value whose probability of being 955 zero is (expected to be) prob/256. */ 957 void write_bool( bool_encoder *e, Prob prob, int bool_value) 958 { 959 /* split is approximately (range * prob) / 256 and, 960 crucially, is strictly bigger than zero and strictly 961 smaller than range */ 963 uint32 split = 1 + ( ((e->range - 1) * prob) >> 8); 965 if( bool_value) { 966 e->bottom += split; /* move up bottom of interval */ 967 e->range -= split; /* with corresponding decrease in range */ 968 } else 969 e->range = split; /* decrease range, leaving bottom alone */ 971 while( e->range < 128) 972 { 973 e->range <<= 1; 975 if( e->bottom & (1 << 31)) /* detect carry */ 976 add_one_to_output( e->output); 978 e->bottom <<= 1; /* before shifting bottom */ 980 if( !--e->bit_count) { /* write out high byte of bottom ... */ 982 *e->output++ = (uint8) (e->bottom >> 24); 984 e->bottom &= (1 << 24) - 1; /* ... keeping low 3 bytes */ 985 e->bit_count = 8; /* 8 shifts until next output */ 986 } 987 } 988 } 990 /* Call this function (exactly once) after encoding the last 991 bool value for the partition being written */ 993 void flush_bool_encoder( bool_encoder *e) 994 { 995 int c = e->bit_count; 996 uint32 v = e->bottom; 998 if( v & (1 << (32 - c))) /* propagate (unlikely) carry */ 999 add_one_to_output( e->output); 1000 v <<= c & 7; /* before shifting remaining output */ 1001 c >>= 3; /* to top of internal buffer */ 1002 while( --c >= 0) 1003 v <<= 8; 1004 c = 4; 1005 while( --c >= 0) { /* write remaining data, possibly padded */ 1006 *e->output++ = (uint8) (v >> 24); 1007 v <<= 8; 1008 } 1009 } 1011 /* Decoder state exactly parallels that of the encoder. 1012 "value", together with the remaining input, equals the 1013 complete encoded number x less the left endpoint of the 1014 current coding interval. */ 1016 typedef struct { 1017 uint8 *input; /* pointer to next compressed data byte */ 1018 uint32 range; /* always identical to encoder's range */ 1019 uint32 value; /* contains at least 8 significant bits */ 1020 int bit_count; /* # of bits shifted out of 1021 value, at most 7 */ 1022 } bool_decoder; 1024 /* Call this function before reading any bools from the 1025 partition.*/ 1027 void init_bool_decoder( bool_decoder *d, uint8 *start_partition) 1028 { 1029 { 1030 int i = 0; 1031 d->value = 0; /* value = first 2 input bytes */ 1032 while( ++i <= 2) 1033 d->value = (d->value << 8) | *start_partition++; 1034 } 1036 d->input = start_partition; /* ptr to next byte to be read */ 1037 d->range = 255; /* initial range is full */ 1038 d->bit_count = 0; /* have not yet shifted out any bits */ 1039 } 1041 /* Main function reads a bool encoded at probability prob/256, 1042 which of course must agree with the probability used when the 1043 bool was written. */ 1045 int read_bool( bool_decoder *d, Prob prob) 1046 { 1047 /* range and split are identical to the corresponding values 1048 used by the encoder when this bool was written */ 1050 uint32 split = 1 + ( ((d->range - 1) * prob) >> 8); 1051 uint32 SPLIT = split << 8; 1052 int retval; /* will be 0 or 1 */ 1054 if( d->value >= SPLIT) { /* encoded a one */ 1055 retval = 1; 1056 d->range -= split; /* reduce range */ 1057 d->value -= SPLIT; /* subtract off left endpoint of interval */ 1058 } else { /* encoded a zero */ 1059 retval = 0; 1060 d->range = split; /* reduce range, no change in left endpoint */ 1061 } 1063 while( d->range < 128) { /* shift out irrelevant value bits */ 1064 d->value <<= 1; 1065 d->range <<= 1; 1066 if( ++d->bit_count == 8) { /* shift in new bits 8 at a time */ 1067 d->bit_count = 0; 1068 d->value |= *d->input++; 1069 } 1070 } 1071 return retval; 1072 } 1074 /* Convenience function reads a "literal", that is, a "num_bits" 1075 wide unsigned value whose bits come high- to low-order, with 1076 each bit encoded at probability 128 (i.e., 1/2). */ 1078 uint32 read_literal( bool_decoder *d, int num_bits) 1079 { 1080 uint32 v = 0; 1081 while( num_bits--) 1082 v = (v << 1) + read_bool( d, 128); 1083 return v; 1084 } 1086 /* Variant reads a signed number */ 1088 int32 read_signed_literal( bool_decoder *d, int num_bits) 1089 { 1090 int32 v = 0; 1091 if( !num_bits) 1092 return 0; 1093 if( read_bool( d, 128)) 1094 v = -1; 1095 while( --num_bits) 1096 v = (v << 1) + read_bool( d, 128); 1097 return v; 1098 } 1100 ---- End code block ---------------------------------------- 1102 8. Compressed Data Components 1104 At the lowest level, VP8's compressed data is simply a sequence of 1105 probabilistically-encoded bools. Most of this data is composed of 1106 (slightly) larger semantic units fashioned from bools, which we 1107 describe here. 1109 We sometimes use these descriptions in C expressions within data 1110 format specifications. In this context, they refer to the return 1111 value of a call to an appropriate bool_decoder d, reading (as always) 1112 from its current reference point. 1114 +--------------+-------+--------------------------------------------+ 1115 | Call | Alt. | Return | 1116 +--------------+-------+--------------------------------------------+ 1117 | Bool(p) | B(p) | Bool with probability p/256 of being 0. | 1118 | | | Return value of read_bool(d, p). | 1119 | | | | 1120 | Flag | F | A one-bit flag (same thing as a B(128) or | 1121 | | | an L(1)). Abbreviated F. Return value of | 1122 | | | read_bool(d, 128). | 1123 | | | | 1124 | Lit(n) | L(n) | Unsigned n-bit number encoded as n flags | 1125 | | | (a "literal"). Abbreviated L(n). The | 1126 | | | bits are read from high to low order. | 1127 | | | Return value of read_literal(d, n). | 1128 | | | | 1129 | SignedLit(n) | | Signed n-bit number encoded similarly to | 1130 | | | an L(n). Return value of | 1131 | | | read_signed_literal(d, n). These are | 1132 | | | rare. | 1133 | | | | 1134 | P(8) | | An 8-bit probability. No different from | 1135 | | | an L(8), but we sometimes use this | 1136 | | | notation to emphasize that a probability | 1137 | | | is being coded. | 1138 | | | | 1139 | P(7) | | A 7-bit specification of an 8-bit | 1140 | | | probability. Coded as an L(7) number x; | 1141 | | | the resulting 8-bit probability is x ? x | 1142 | | | << 1 : 1. | 1143 | | | | 1144 | F? X | | A flag which, if true, is followed by a | 1145 | | | piece of data X. | 1146 | | | | 1147 | F? X:Y | | A flag which, if true, is followed by X | 1148 | | | and, if false, is followed by Y. Also used | 1149 | | | to express a value where Y is an implicit | 1150 | | | default (not encoded in the data stream), | 1151 | | | as in F? P(8):255, which expresses an | 1152 | | | optional probability: if the flag is true, | 1153 | | | the probability is specified as an 8-bit | 1154 | | | literal, while if the flag is false, the | 1155 | | | probability defaults to 255. | 1156 | | | | 1157 | B(p)? X | B(p)? | Variants of the above using a boolean | 1158 | | X:Y | indicator whose probability is not | 1159 | | | necessarily 128. | 1160 | | | | 1161 | X | | Multi-component field, the specifics of | 1162 | | | which will be given at a more appropriate | 1163 | | | point in the discussion. | 1164 | | | | 1165 | T | | Tree-encoded value from small alphabet. | 1166 +--------------+-------+--------------------------------------------+ 1168 The last type requires elaboration. We often wish to encode 1169 something whose value is restricted to a small number of 1170 possibilities (the alphabet). 1172 This is done by representing the alphabet as the leaves of a small 1173 binary tree. The (non-leaf) nodes of the tree have associated 1174 probabilities p and correspond to calls to read_bool(d, p). We think 1175 of a zero as choosing the left branch below the node and a one as 1176 choosing the right branch. 1178 Thus every value (leaf) whose tree depth is x is decoded after 1179 exactly x calls to read_bool. 1181 A tree representing an encoding of an alphabet of n possible values 1182 always contains n-1 non-leaf nodes, regardless of its shape (this is 1183 easily seen by induction on n). 1185 There are many ways that a given alphabet can be so represented. The 1186 choice of tree has little impact on datarate but does affect decoder 1187 performance. The trees used by VP8 are chosen to (on average) 1188 minimize the number of calls to read_bool. This amounts to shaping 1189 the tree so that more probable values have smaller tree depth than do 1190 less probable values. 1192 Readers familiar with Huffman coding will notice that, given an 1193 alphabet together with probabilities for each value, the associated 1194 Huffman tree minimizes the expected number of calls to read_bool. 1196 Such readers will also realize that the coding method described here 1197 never results in higher datarates than does the Huffman method and, 1198 indeed, often results in much lower datarates. Huffman coding is, in 1199 fact, nothing more than a special case of this method in which each 1200 node probability is fixed at 128 (i.e., 1/2). 1202 8.1. Tree Coding Implementation 1204 We give a suggested implementation of a tree data structure followed 1205 by a couple of actual examples of its usage by VP8. 1207 It is most convenient to represent the values using small positive 1208 integers, typically an enum counting up from zero. The largest 1209 alphabet (used to code DCT coefficients, described in Chapter 13 that 1210 is tree-coded by VP8 has only 12 values. The tree for this alphabet 1211 adds 11 interior nodes and so has a total of 23 positions. Thus, an 1212 8-bit number easily accommodates both a tree position and a return 1213 value. 1215 A tree may then be compactly represented as an array of (pairs of) 1216 8-bit integers. Each (even) array index corresponds to an interior 1217 node of the tree;, the zeroth index of course corresponds to the root 1218 of the tree. The array entries come in pairs corresponding to the 1219 left (0) and right (1) branches of the subtree below the interior 1220 node. We use the convention that a positive (even) branch entry is 1221 the index of a deeper interior node, while a nonpositive entry v 1222 corresponds to a leaf whose value is -v. 1224 The node probabilities associated to a tree-coded value are stored in 1225 an array whose indices are half the indices of the corresponding tree 1226 positions. The length of the probability array is one less than the 1227 size of the alphabet. 1229 Here is C code implementing the foregoing. The advantages of our 1230 data structure should be noted. Aside from the smallness of the 1231 structure itself, the tree-directed reading algorithm is essentially 1232 a single line of code. 1234 ---- Begin code block -------------------------------------- 1236 /* A tree specification is simply an array of 8-bit integers. */ 1238 typedef int8 tree_index; 1239 typedef const tree_index Tree[]; 1241 /* Read and return a tree-coded value at the current decoder 1242 position. */ 1244 int treed_read( 1245 bool_decoder * const d, /* bool_decoder always returns a 0 or 1 */ 1246 Tree t, /* tree specification */ 1247 const Prob p[] /* corresponding interior node probabilities */ 1248 ) { 1249 register tree_index i = 0; /* begin at root */ 1251 /* Descend tree until leaf is reached */ 1253 while( ( i = t[ i + read_bool( d, p[i>>1]) ] ) > 0) {} 1255 return -i; /* return value is negation of nonpositive index */ 1256 } 1258 ---- End code block ---------------------------------------- 1260 Tree-based decoding is implemented in the reference decoder file 1261 bool_decoder.h. 1263 8.2. Tree Coding Example 1265 As a multi-part example, without getting too far into the semantics 1266 of macroblock decoding (which is of course taken up below), we look 1267 at the "mode" coding for intra-predicted macroblocks. 1269 It so happens that, because of a difference in statistics, the Y (or 1270 luma) mode encoding uses two different trees: one for key frames and 1271 another for interframes. This is the only instance in VP8 of the 1272 same dataset being coded by different trees under different 1273 circumstances. The UV (or chroma) modes are a proper subset of the Y 1274 modes and, as such, have their own decoding tree. 1276 ---- Begin code block -------------------------------------- 1278 typedef enum 1279 { 1280 DC_PRED, /* predict DC using row above and column to the left */ 1281 V_PRED, /* predict rows using row above */ 1282 H_PRED, /* predict columns using column to the left */ 1283 TM_PRED, /* propagate second differences a la "true motion" */ 1285 B_PRED, /* each Y subblock is independently predicted */ 1287 num_uv_modes = B_PRED, /* first four modes apply to chroma */ 1288 num_ymodes /* all modes apply to luma */ 1289 } 1290 intra_mbmode; 1292 /* The aforementioned trees together with the implied codings as 1293 comments. 1294 Actual (i.e., positive) indices are always even. 1295 Value (i.e., nonpositive) indices are arbitrary. */ 1297 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 1298 { 1299 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 1300 4, 6, /* "1" subtree has 2 descendant subtrees */ 1301 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 1302 H_PRED = "101" */ 1303 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 1304 B_PRED = "111" */ 1305 }; 1307 const tree_index kf_ymode_tree [2 * (num_ymodes - 1)] = 1308 { 1309 -B_PRED, 2, /* root: B_PRED = "0", "1" subtree */ 1310 4, 6, /* "1" subtree has 2 descendant subtrees */ 1311 -DC_PRED, -V_PRED, /* "10" subtree: DC_PRED = "100", 1312 V_PRED = "101" */ 1313 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1314 TM_PRED = "111" */ 1315 }; 1317 const tree_index uv_mode_tree [2 * (num_uv_modes - 1)] = 1318 { 1319 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 1320 -V_PRED, 4, /* "1" subtree: V_PRED = "10", 1321 "11" subtree */ 1322 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1323 TM_PRED = "111" */ 1324 }; 1326 /* Given a bool_decoder d, a Y mode might be decoded as follows.*/ 1328 const Prob pretend_its_huffman [num_ymodes - 1] = 1329 { 128, 128, 128, 128}; 1331 Ymode = (intra_mbmode) treed_read( d, ymode_tree, 1332 pretend_its_huffman); 1334 ---- End code block ---------------------------------------- 1336 Since it greatly facilitates re-use of reference code and since there 1337 is no real reason to do otherwise, it is strongly suggested that any 1338 decoder implementation use exactly the same enumeration values and 1339 probability table layouts as described in this document (and in the 1340 reference code) for all tree-coded data in VP8. 1342 9. Frame Header 1344 The uncompressed data chunk at the start of each frame and the first 1345 part of the first data partition contains information pertaining to 1346 the frame as a whole. We list the fields in the order of occurrence, 1347 giving details for some of the fields. Other details are postponed 1348 until a more logical point in our overall description. Most of the 1349 header decoding occurs in the reference decoder file dixie.c. 1351 9.1. Uncompressed Data Chunk 1353 The uncompressed data chunk comprises a common (for key frames and 1354 interframes) 3-byte frame tag that contains four fields, as follows: 1356 1. A 1-bit frame type (0 for key frames, 1 for interframes). 1358 2. A 3-bit version number (0 - 3 are defined as four different 1359 profiles with different decoding complexity; other values may be 1360 defined for future variants of the VP8 data format). 1362 3. A 1-bit show_frame flag (0 when current frame is not for display, 1363 1 when current frame is for display). 1365 4. A 19-bit field containing the size of the first data partition in 1366 bytes. 1368 Version number enables or disables certain features in the bitstream, 1369 as follows: 1371 +---------+-------------------------+-------------+ 1372 | Version | Reconstruction filter | Loop filter | 1373 +---------+-------------------------+-------------+ 1374 | 0 | Bicubic | Normal | 1375 | | | | 1376 | 1 | Bilinear | Simple | 1377 | | | | 1378 | 2 | Bilinear | None | 1379 | | | | 1380 | 3 | None | None | 1381 | | | | 1382 | Other | Reserved for future use | | 1383 +---------+-------------------------+-------------+ 1385 The reference software also adjusts the loop filter based on version 1386 number, as per the table above. Version number 1 implies a "simple" 1387 loop filter and version numbers 2 and 3 imply no loop filter. 1388 However, the "simple" filter setting in this context has no effect 1389 whatsoever on the decoding process, and the "no loop filter" setting 1390 only forces the reference encoder to set filter level equal to 0. 1391 Neither affect the decoding process. In decoding, the only loop 1392 filter settings that matter are those in the frame header. 1394 For key frames the frame tag is followed by a further 7 bytes of 1395 uncompressed data, as follows: 1397 ---- Begin code block -------------------------------------- 1399 Start code byte 0 0x9d 1400 Start code byte 1 0x01 1401 Start code byte 2 0x2a 1403 16 bits : (2 bits Horizontal Scale << 14) | Width (14 bits) 1404 16 bits : (2 bits Vertical Scale << 14) | Height (14 bits) 1406 ---- End code block ---------------------------------------- 1408 The following source code segment illustrates validation of the start 1409 code and reading the width, height and scale factors for a key frame. 1411 ---- Begin code block -------------------------------------- 1413 unsigned char *c = pbi->Source+3; 1415 // vet via sync code 1416 if(c[0]!=0x9d||c[1]!=0x01||c[2]!=0x2a) 1417 return -1; 1419 ---- End code block ---------------------------------------- 1421 where pbi->source points to the beginning of the frame. 1423 The following code reads the image dimension from the bitstream: 1425 ---- Begin code block -------------------------------------- 1427 pc->Width = swap2(*(unsigned short*)(c+3))&0x3fff; 1428 pc->horiz_scale = swap2(*(unsigned short*)(c+3))>>14; 1429 pc->Height = swap2(*(unsigned short*)(c+5))&0x3fff; 1430 pc->vert_scale = swap2(*(unsigned short*)(c+5))>>14; 1432 ---- End code block ---------------------------------------- 1434 where swap2 macro takes care of the endian on different platform: 1436 ---- Begin code block -------------------------------------- 1438 #if defined(__ppc__) || defined(__ppc64__) 1439 # define swap2(d) \ 1440 ((d&0x000000ff)<<8) | \ 1441 ((d&0x0000ff00)>>8) 1442 #else 1443 # define swap2(d) d 1444 #endif 1446 ---- End code block ---------------------------------------- 1448 While each frame is encoded as a raster scan of 16x16 macroblocks, 1449 the frame dimensions are not necessarily evenly divisible by 16. In 1450 this case, write ew = 16 - (width & 15) and eh = 16 - (height & 15) 1451 for the excess width and height, respectively. Although they are 1452 encoded, the last ew columns and eh rows are not actually part of the 1453 image and should be discarded before final output. However, these 1454 "excess pixels" should be maintained in the internal reconstruction 1455 buffer used to predict ensuing frames. 1457 The scaling specifications for each dimension are encoded as follows. 1459 +-------+--------------------------------------+ 1460 | Value | Scaling | 1461 +-------+--------------------------------------+ 1462 | 0 | No upscaling (the most common case). | 1463 | | | 1464 | 1 | Upscale by 5/4. | 1465 | | | 1466 | 2 | Upscale by 5/3. | 1467 | | | 1468 | 3 | Upscale by 2. | 1469 +-------+--------------------------------------+ 1471 Upscaling does not affect the reconstruction buffer, which should be 1472 maintained at the encoded resolution. Any reasonable method of 1473 upsampling (including any that may be supported by video hardware in 1474 the playback environment) may be used. Since scaling has no effect 1475 on decoding, we do not discuss it any further. 1477 As discussed in Chapter 5, allocation (or re-allocation) of data 1478 structures (such as the reconstruction buffer) whose size depends on 1479 dimension will be triggered here. 1481 9.2. Color Space and Pixel Type (Key Frames-only) 1483 +-------+------------------------------------------+ 1484 | Field | Value | 1485 +-------+------------------------------------------+ 1486 | L(1) | 1-bit color space type specification | 1487 | | | 1488 | L(1) | 1-bit pixel value clamping specification | 1489 +-------+------------------------------------------+ 1491 The color space type bit is encoded as the following: 1493 o 0 - YUV color space similar to the YCrCb color space defined in 1494 [ITU-R_BT.601] 1496 o 1 - Reserved for future use 1498 The pixel value clamping type bit is encoded as the following: 1500 o 0 - Decoders are required to clamp the reconstructed pixel values 1501 to between 0 and 255 (inclusive). 1503 o 1 - Reconstructed pixel values are guaranteed to be between 0 and 1504 255, no clamping is necessary. 1506 Information in this subsection does not appear in interframes. 1508 9.3. Segment-based Adjustments 1510 This subsection contains probability and value information for 1511 implementing segment adaptive adjustments to default decoder 1512 behaviors. The data in this section is used in the decoding of the 1513 ensuing per-segment information and applies to the entire frame. 1514 When segment adaptive adjustments are enabled, each macroblock will 1515 be assigned a segment ID. Macroblocks with the same segment ID 1516 belong to same segment, and have the same adaptive adjustments over 1517 default baseline values for the frame. The adjustments can be 1518 quantization level or loop filter strength. 1520 The context for decoding this feature at macroblock level is provided 1521 by a subsection in the frame header, which contains:: 1523 1. A segmentation_enabled Flag which if 1 (0), enables (disables) 1524 the feature for this frame. The remaining fields occur if the 1525 feature is enabled. 1527 2. L(1) indicates if the segment map is updated for the current 1528 frame (update_mb_segmentaton_map) 1530 3. L(1) indicates if the segment feature data items are updated for 1531 the current frame 1533 4. If flag in 3 is 1, the following fields occur: 1535 1. L(1) the mode of segment feature data, can be absolute value 1536 mode or delta value mode, later mode, feature data is the 1537 difference against current frame defaults. 1539 2. Segment feature data items are decoded segment by each 1540 segment for each segment feature. For every data item, a one 1541 bit flag indicating if the item is 0 or a non-zero value to 1542 be decoded. If there is non-zero value, the value is decoded 1543 as a magnitude L(n) followed by a one bit sign (L(1), 0 for 1544 positive and 1 for negative). The length n can be looked up 1545 from a pre-defined length table for all feature data. 1547 5. If flag in 2 is 1, the probabilities of the decoding tree for 1548 segment map are decoded from the bitstream. Each probability is 1549 decoded with one bit flag indicating if the probability is the 1550 default value of 255 (flag is 0), or the probability is an 8-bit 1551 value, L(8), from the bitstream. 1553 The layout and semantics supporting this feature at the macroblock 1554 level will be described in Chapter 10. 1556 9.4. Loop Filter Type and Levels 1558 VP8 supports two types of loop filter, having different computational 1559 complexity. The following bits occur in the header to support the 1560 selection of the baseline type, strength and sharpness behavior of 1561 the loop filter used for the current frame. 1563 +-------+-------------------+ 1564 | Index | Description | 1565 +-------+-------------------+ 1566 | L(1) | filter_type | 1567 | | | 1568 | L(6) | loop_filter_level | 1569 | | | 1570 | L(3) | sharpness_level | 1571 +-------+-------------------+ 1573 The meaning of these numbers will be further explained in Chapter 15. 1575 VP8 has a feature in the bitstream that enables adjustment of the 1576 loop filter level based on a macroblock's prediction mode and 1577 reference frame. The per-macroblock adjustment is done through delta 1578 values against default loop filter level for the current frame. This 1579 subsection contains flag and value information for implementing per- 1580 macroblock loop filter level adjustment to default decoder behaviors. 1581 The data in this section is used in the decoding of the ensuing per- 1582 macroblock information and applies to the entire frame. 1584 L(1) is a one-bit flag indicating if macroblock loop filter 1585 adjustment is on for the current frame. 0 means such feature is not 1586 supported in the current frame and 1 means this feature is enabled 1587 for the current frame. 1589 Whether the adjustment is based on reference frame or encoding mode, 1590 the adjustment of loop filter level is done via a delta value against 1591 a baseline loop filter value. The delta values are updated for the 1592 current frame if an L(1) bit, mode_ref_lf_delta_update, takes the 1593 value 1. There are two groups of delta values, one group of delta 1594 values are for reference frame-based adjustments, the other group is 1595 for mode-based adjustments. The number of delta values in the two 1596 groups is MAX_REF_LF_DELTAS and MAX_MODE_LF_DELTAS, respectively. 1597 For every value within the two groups, there is one bit L(1) to 1598 indicate if the particular value is updated. When one is updated 1599 (1), it is transmitted as a six-bit magnitude L(6) followed by a one- 1600 bit sign flag (L(1), 0 for positive and 1 for negative). 1602 9.5. Token Partition and Partition Data Offsets 1604 VP8 allows DCT coefficients to be packed into multiple partitions 1605 besides the first partition with header and per-macroblock prediction 1606 information, so the decoder can perform parallel decoding in an 1607 efficient manner. There are two bits L(2) used to indicate the 1608 number of coefficient data partitions within a compressed frame. The 1609 two bits are defined in the following table: 1611 +-------+-------+----------------------+ 1612 | Bit 1 | Bit 0 | Number of Partitions | 1613 +-------+-------+----------------------+ 1614 | 0 | 0 | 1 | 1615 | | | | 1616 | 0 | 1 | 2 | 1617 | | | | 1618 | 1 | 0 | 4 | 1619 | | | | 1620 | 1 | 1 | 8 | 1621 +-------+-------+----------------------+ 1623 Offsets are embedded in the bitstream to provide the decoder direct 1624 access to token partitions. If the number of data partitions is 1625 greater then 1, the size of each partition (except the last) is 1626 written in 3 bytes (24 bits). The size of the last partition is the 1627 remainder of the data not used by any of the previous partitions. 1628 The partitioned data are consecutive in the bitstream, so the size 1629 can also be used to calculate the offset of each partition. The 1630 following pseudo code illustrates how the size/offset is defined by 1631 the three bytes in the bitstream. 1633 ---- Begin code block -------------------------------------- 1635 Offset/size = (uint32)(byte0) + ((uint32)(byte1)<<8) 1636 + ((uint32)(byte2)<<16); 1638 ---- End code block ---------------------------------------- 1640 9.6. Dequantization Indices 1642 All residue signals are specified via a quantized 4x4 DCT applied to 1643 the Y, U, V, or Y2 subblocks of a macroblock. As detailed in Chapter 1644 14, before inverting the transform, each decoded coefficient is 1645 multiplied by one of six dequantization factors, the choice of which 1646 depends on the plane (Y, chroma = U or V, Y2) and coefficient 1647 position (DC = coefficient 0, AC = coefficients 1-15). The six 1648 values are specified using 7-bit indices into six corresponding fixed 1649 tables (the tables are given in Chapter 14). 1651 The first 7-bit index gives the dequantization table index for Y 1652 plane AC coefficients, called yac_qi. It is always coded and acts as 1653 a baseline for the other 5 quantization indices, each of which is 1654 represented by a delta from this baseline index. Following is pseudo 1655 code for reading the indices: 1657 ---- Begin code block -------------------------------------- 1659 yac_qi = L(7); /* Y ac index always specified */ 1660 ydc_delta = F? delta(): 0; /* Y dc delta specified if 1661 flag is true */ 1663 y2dc_delta = F? delta(): 0; /* Y2 dc delta specified if 1664 flag is true */ 1665 y2ac_delta = F? delta(): 0; /* Y2 ac delta specified if 1666 flag is true */ 1668 uvdc_delta = F? delta(): 0; /* chroma dc delta specified 1669 if flag is true */ 1670 uvac_delta = F? delta(): 0; /* chroma ac delta specified 1671 if flag is true */ 1673 ---- End code block ---------------------------------------- 1674 Where delta() is the process to read 5 bits from the bitstream to 1675 determine a signed delta value: 1677 +-------+--------------------------------------------------+ 1678 | Index | Description | 1679 +-------+--------------------------------------------------+ 1680 | L(4) | Magnitude of delta | 1681 | | | 1682 | L(1) | Sign of delta, 0 for positive and 1 for negative | 1683 +-------+--------------------------------------------------+ 1685 9.7. Refresh Golden Frame and AltRef Frame 1687 For key frames, both golden frame and altref frame are refreshed/ 1688 replaced by the current reconstructed frame, by default. For non-key 1689 frames, VP8 uses two bits to indicate whether the two frame buffers 1690 are refreshed, using the reconstructed current frame: 1692 +-------+----------------------------------------------------------+ 1693 | Index | Description | 1694 +-------+----------------------------------------------------------+ 1695 | L(1) | Whether golden frame is refreshed (0 for no, 1 for yes). | 1696 | | | 1697 | L(1) | Whether altref frame is refreshed (0 for no, 1 for yes). | 1698 +-------+----------------------------------------------------------+ 1700 When the flag for golden frame is 0, VP8 uses 2 more bits in the 1701 bitstream to indicate whether the buffer (and which buffer) is copied 1702 to the golden frame, or if no buffer is copied: 1704 +-------+------------------------------------------+ 1705 | Index | Description | 1706 +-------+------------------------------------------+ 1707 | L(2) | Buffer copy flag for golden frame buffer | 1708 +-------+------------------------------------------+ 1710 Where: 1712 o 0 means no buffer is copied to golden frame 1714 o 1 means last_frame is copied to golden frame 1716 o 2 means alt_ref_frame is copied to golden frame 1718 Similarly, when the flag for altref is 0, VP8 uses 2 bits in the 1719 bitstream to indicate which buffer is copied to alt_ref_frame. 1721 +-------+------------------------------------------+ 1722 | Index | Description | 1723 +-------+------------------------------------------+ 1724 | L(2) | Buffer copy flag for altref frame buffer | 1725 +-------+------------------------------------------+ 1727 Where: 1729 o 0 means no buffer is copied to altref frame 1731 o 1 means last_frame is copied to altref frame 1733 o 2 means golden_frame is copied to altref frame 1735 Two bits are transmitted for ref_frame_sign_bias for golden_frame and 1736 alt_ref_frame respectively. 1738 +-------+---------------------------------+ 1739 | Index | Description | 1740 +-------+---------------------------------+ 1741 | L(1) | Sign bias flag for golden frame | 1742 | | | 1743 | L(1) | Sign bias flag for altref frame | 1744 +-------+---------------------------------+ 1746 These values are used to control the sign of the motion vectors when 1747 a golden frame or an altref frame is used as the reference frame for 1748 a macroblock. 1750 9.8. Refresh Last Frame Buffer 1752 VP8 uses one bit, L(1), to indicate if the last frame reference 1753 buffer is refreshed using the constructed current frame. On key 1754 frame this bit is overridden, and the last frame buffer is always 1755 refreshed. 1757 9.9. DCT Coefficient Probability Update 1759 Contains a partial update of the probability tables used to decode 1760 DCT coefficients. These tables are maintained across interframes but 1761 are of course replaced with their defaults at the beginning of every 1762 key frame. 1764 The layout and semantics of this field will be taken up in Chapter 1765 13. 1767 9.10. Remaining Frame Header Data (non-Key Frame) 1769 +-------+-----------------------------------------------------------+ 1770 | Index | Description | 1771 +-------+-----------------------------------------------------------+ 1772 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1773 | | if skipping of macroblocks with no non-zero coefficients | 1774 | | is enabled. If it is set to 0 then prob_skip_false is | 1775 | | not read and mb_skip_coeff is forced to 0 for all | 1776 | | macroblocks (see Sections 11.1 and 12.1). | 1777 | | | 1778 | L(8) | prob_skip_false = probability used for decoding a | 1779 | | macroblock level flag, which indicates if a macroblock | 1780 | | has any non-zero coefficients. Only read if | 1781 | | mb_no_coeff_skip is 1. | 1782 | | | 1783 | L(8) | prob_intra = probability that a macroblock is "intra" | 1784 | | predicted, that is, predicted from the already-encoded | 1785 | | portions of the current frame as opposed to "inter" | 1786 | | predicted, that is, predicted from the contents of a | 1787 | | prior frame. | 1788 | | | 1789 | L(8) | prob_last = probability that an inter-predicted | 1790 | | macroblock is predicted from the immediately previous | 1791 | | frame, as opposed to the most recent golden frame or | 1792 | | altref frame.. | 1793 | | | 1794 | L(8) | prob_gf = probability that an inter-predicted macroblock | 1795 | | is predicted from the most recent golden frame, as | 1796 | | opposed to the altref frame | 1797 | | | 1798 | F | If true, followed by four L(8)s updating the | 1799 | | probabilities for the different types of intra-prediction | 1800 | | for the Y plane. These probabilities correspond to the | 1801 | | four interior nodes of the decoding tree for intra Y | 1802 | | modes in an interframe, that is, the even positions in | 1803 | | the ymode_tree array given above. | 1804 | | | 1805 | F | If true, followed by three L(8)s updating the | 1806 | | probabilities for the different types of intra-prediction | 1807 | | for the chroma planes. These probabilities correspond to | 1808 | | the even positions in the uv_mode_tree array given above. | 1809 | | | 1810 | X | Motion vector probability update. The details will be | 1811 | | given after the discussion of motion vector decoding. | 1812 +-------+-----------------------------------------------------------+ 1814 Decoding of this portion of the frame header is handled in the 1815 reference decoder file dixie.c. 1817 9.11. Remaining Frame Header Data (Key Frame) 1819 +-------+-----------------------------------------------------------+ 1820 | Index | Description | 1821 +-------+-----------------------------------------------------------+ 1822 | L(1) | mb_no_coeff_skip. This flag indicates at the frame level | 1823 | | if skipping of macroblocks with no non-zero coefficients | 1824 | | is enabled. If it is set to 0 then prob_skip_false is | 1825 | | not read and mb_skip_coeff is forced to 0 for all | 1826 | | macroblocks (see Sections 11.1 and 12.1). | 1827 | | | 1828 | L(8) | prob_skip_false = Probability used for decoding a | 1829 | | macroblock level flag, which indicates if a macroblock | 1830 | | has any non-zero coefficients. Only read if | 1831 | | mb_no_coeff_skip is 1. | 1832 +-------+-----------------------------------------------------------+ 1834 Decoding of this portion of the frame header is handled in the 1835 reference decoder file modemv.c. 1837 This completes the layout of the frame header. The remainder of the 1838 first data partition consists of macroblock-level prediction data. 1840 After the frame header is processed, all probabilities needed to 1841 decode the prediction and residue data are known and will not change 1842 until the next frame. 1844 10. Segment-based Feature Adjustments 1846 Every macroblock may optionally override some of the default 1847 behaviors of the decoder. Specifically, VP8 uses segment based 1848 adjustments to support changing quantizer level and loop filter level 1849 for a macroblock. When the segment-based adjustment feature is 1850 enabled for a frame, each macroblock within the frame is coded with a 1851 segment_id. This effectively segments all the macroblocks in the 1852 current frame into a number of different segments. Macroblocks 1853 within the same segment behave exactly the same for quantizer and 1854 loop filter level adjustments. 1856 If both the segmentation_enabled and update_mb_segmentation_map flags 1857 in subsection B of the frame header take a value of 1, the prediction 1858 data for each (intra- or inter-coded) macroblock begins with a 1859 specification of segment_id for the current macroblock. It is 1860 decoded using this simple tree ... 1862 ---- Begin code block -------------------------------------- 1864 const tree_index mb_segment_tree [2 * (4-1)] = 1865 { 1866 2, 4, /* root: "0", "1" subtrees */ 1867 -0, -1, /* "00" = 0th value, "01" = 1st value */ 1868 -2, -3 /* "10" = 2nd value, "11" = 3rd value */ 1869 } 1871 ---- End code block ---------------------------------------- 1873 ... combined with a 3-entry probability table 1874 mb_segment_tree_probs[3]. The macroblock's segment_id is used later 1875 in the decoding process to look into the segment_feature_data table 1876 and determine how the quantizer and loop filter levels are adjusted. 1878 The decoding of segment_id, together with the parsing of intra- 1879 prediction modes (which is taken up next), is implemented in the 1880 reference decoder file modemv.c. 1882 11. Key Frame Macroblock Prediction Records 1884 After the features described above, the macroblock prediction record 1885 next specifies the prediction mode used for the macroblock. 1887 11.1. mb_skip_coeff 1889 The single bool flag is decoded using prob_skip_false if and only if 1890 mb_no_coeff_skip is set to 1 (see sections 9.10 and 9.11). If 1891 mb_no_coeff_skip is set to 0 then this value defaults to 0. 1893 11.2. Luma Modes 1895 First comes the luma specification of type intra_mbmode, coded using 1896 the kf_ymode_tree, as described in Chapter 8 and repeated here for 1897 convenience: 1899 ---- Begin code block -------------------------------------- 1901 typedef enum 1902 { 1903 DC_PRED, /* predict DC using row above and column to the left */ 1904 V_PRED, /* predict rows using row above */ 1905 H_PRED, /* predict columns using column to the left */ 1906 TM_PRED, /* propagate second differences a la "true motion" */ 1908 B_PRED, /* each Y subblock is independently predicted */ 1910 num_uv_modes = B_PRED, /* first four modes apply to chroma */ 1911 num_ymodes /* all modes apply to luma */ 1912 } 1913 intra_mbmode; 1915 const tree_index kf_ymode_tree [2 * (num_ymodes - 1)] = 1916 { 1917 -B_PRED, 2, /* root: B_PRED = "0", "1" subtree */ 1918 4, 6, /* "1" subtree has 2 descendant subtrees */ 1919 -DC_PRED, -V_PRED, /* "10" subtree: DC_PRED = "100", 1920 V_PRED = "101" */ 1921 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 1922 TM_PRED = "111" */ 1923 }; 1925 ---- End code block ---------------------------------------- 1927 For key frames, the Y mode is decoded using a fixed probability array 1928 as follows: 1930 ---- Begin code block -------------------------------------- 1932 const Prob kf_ymode_prob [num_ymodes - 1] = { 145, 156, 163, 128}; 1933 Ymode = (intra_mbmode) treed_read( d, kf_ymode_tree, kf_ymode_prob); 1935 ---- End code block ---------------------------------------- 1937 d is of course the bool_decoder being used to read the first data 1938 partition. 1940 If the Ymode is B_PRED, it is followed by a (tree-coded) mode for 1941 each of the 16 Y subblocks. The 10 subblock modes and their coding 1942 tree as follows: 1944 ---- Begin code block -------------------------------------- 1946 typedef enum 1947 { 1948 B_DC_PRED, /* predict DC using row above and column 1949 to the left */ 1950 B_TM_PRED, /* propagate second differences a la 1951 "true motion" */ 1953 B_VE_PRED, /* predict rows using row above */ 1954 B_HE_PRED, /* predict columns using column to the left */ 1956 B_LD_PRED, /* southwest (left and down) 45 degree diagonal 1957 prediction */ 1958 B_RD_PRED, /* southeast (right and down) "" */ 1960 B_VR_PRED, /* SSE (vertical right) diagonal prediction */ 1961 B_VL_PRED, /* SSW (vertical left) "" */ 1962 B_HD_PRED, /* ESE (horizontal down) "" */ 1963 B_HU_PRED, /* ENE (horizontal up) "" */ 1965 num_intra_bmodes 1966 } 1967 intra_bmode; 1969 /* Coding tree for the above, with implied codings as comments */ 1971 const tree_index bmode_tree [2 * (num_intra_bmodes - 1)] = 1972 { 1973 -B_DC_PRED, 2, /* B_DC_PRED = "0" */ 1974 -B_TM_PRED, 4, /* B_TM_PRED = "10" */ 1975 -B_VE_PRED, 6, /* B_VE_PRED = "110" */ 1976 8, 12, 1977 -B_HE_PRED, 10, /* B_HE_PRED = "11100" */ 1978 -B_RD_PRED, -B_VR_PRED, /* B_RD_PRED = "111010", 1979 B_VR_PRED = "111011" */ 1980 -B_LD_PRED, 14, /* B_LD_PRED = "111110" */ 1981 -B_VL_PRED, 16, /* B_VL_PRED = "1111110" */ 1982 -B_HD_PRED, -B_HU_PRED /* HD = "11111110", 1983 HU = "11111111" */ 1984 }; 1986 ---- End code block ---------------------------------------- 1988 The first four modes are smaller versions of the similarly-named 1989 16x16 modes above, albeit with slightly different numbering. The 1990 last six "diagonal" modes are unique to luma subblocks. 1992 11.3. Subblock Mode Contexts 1994 The coding of subblock modes in key frames uses the modes already 1995 coded for the subblocks to the left of and above the subblock to 1996 select a probability array for decoding the current subblock mode. 1997 This is our first instance of contextual prediction, and there are 1998 several caveats associated with it: 2000 1. The adjacency relationships between subblocks are based on the 2001 normal default raster placement of the subblocks. 2003 2. The adjacent subblocks need not lie in the current macroblock. 2004 The subblocks to the left of the left-edge subblocks 0, 4, 8, and 2005 12 are the right-edge subblocks 3, 7, 11, and 15, respectively, 2006 of the (already coded) macroblock immediately to the left. 2007 Similarly, the subblocks above the top-edge subblocks 0, 1, 2, 2008 and 3 are the bottom-edge subblocks 12, 13, 14, and 15 of the 2009 already-coded macroblock immediately above us. 2011 3. For macroblocks on the top row or left edge of the image, some of 2012 the predictors will be non-existent. Such predictors are taken 2013 to have had the value B_DC_PRED which, perhaps conveniently, 2014 takes the value 0 in the enumeration above. A simple management 2015 scheme for these contexts might maintain a row of above 2016 predictors and four left predictors. Before decoding the frame, 2017 the entire row is initialized to B_DC_PRED; before decoding each 2018 row of macroblocks, the four left predictors are also set to 2019 B_DC_PRED. After decoding a macroblock, the bottom four subblock 2020 modes are copied into the row predictor (at the current position, 2021 which then advances to be above the next macroblock) and the 2022 right four subblock modes are copied into the left predictor. 2024 4. Many macroblocks will of course be coded using a 16x16 luma 2025 prediction mode. For the purpose of predicting ensuing subblock 2026 modes (only), such macroblocks derive a subblock mode, constant 2027 throughout the macroblock, from the 16x16 luma mode as follows: 2028 DC_PRED uses B_DC_PRED, V_PRED uses B_VE_PRED, H_PRED uses 2029 B_HE_PRED, and TM_PRED uses B_TM_PRED. 2031 5. Although we discuss interframe modes later, we remark here that, 2032 while interframes do use all the intra-coding modes described 2033 here and below, the subblock modes in an interframe are coded 2034 using a single constant probability array that does not depend on 2035 any context. 2037 The dependence of subblock mode probability on the nearby subblock 2038 mode context is most easily handled using a three-dimensional 2039 constant array: 2041 ---- Begin code block -------------------------------------- 2043 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2044 [num_intra_bmodes-1]; 2046 ---- End code block ---------------------------------------- 2048 The outer two dimensions of this array are indexed by the already- 2049 coded subblock modes above and to the left of the current block, 2050 respectively. The inner dimension is a typical tree probability list 2051 whose indices correspond to the even indices of the bmode_tree above. 2052 The mode for the j^(th) luma subblock is then 2054 ---- Begin code block -------------------------------------- 2056 Bmode = (intra_bmode) treed_read( d, bmode_tree, kf_bmode_prob 2057 [A] [L]); 2059 ---- End code block ---------------------------------------- 2061 where the 4x4 Y subblock index j varies from 0 to 15 in raster order 2062 and A and L are the modes used above and to-the-left of the j^(th) 2063 subblock. 2065 The contents of the kf_bmode_prob array are given at the end of this 2066 chapter. 2068 11.4. Chroma Modes 2070 After the Y mode (and optional subblock mode) specification comes the 2071 chroma mode. The chroma modes are a subset of the Y modes and are 2072 coded using the uv_mode_tree described in Chapter 8, again repeated 2073 here for convenience: 2075 ---- Begin code block -------------------------------------- 2077 const tree_index uv_mode_tree [2 * (num_uv_modes - 1)] = 2078 { 2079 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 2080 -V_PRED, 4, /* "1" subtree: V_PRED = "10", 2081 "11" subtree */ 2082 -H_PRED, -TM_PRED /* "11" subtree: H_PRED = "110", 2083 TM_PRED = "111" */ 2084 }; 2086 ---- End code block ---------------------------------------- 2088 As for the Y modes (in a key frame), the chroma modes are coded using 2089 a fixed, contextless probability table: 2091 ---- Begin code block -------------------------------------- 2093 const Prob kf_uv_mode_prob [num_uv_modes - 1] = { 142, 114, 183}; 2094 uv_mode = (intra_mbmode) treed_read( d, uv_mode_tree, 2095 kf_uv_mode_prob); 2097 ---- End code block ---------------------------------------- 2099 This completes the description of macroblock prediction coding for 2100 key frames. As will be discussed in Chapter 16, the coding of intra 2101 modes within interframes is similar, but not identical, to that 2102 described here (and in the reference code) for prediction modes and, 2103 indeed, for all tree-coded data in VP8. 2105 11.5. Subblock Mode Probability Table 2107 Finally, here is the fixed probability table used to decode subblock 2108 modes in key frames. 2110 ---- Begin code block -------------------------------------- 2112 const Prob kf_bmode_prob [num_intra_bmodes] [num_intra_bmodes] 2113 [num_intra_bmodes-1] = 2114 { 2115 { 2116 { 231, 120, 48, 89, 115, 113, 120, 152, 112}, 2117 { 152, 179, 64, 126, 170, 118, 46, 70, 95}, 2118 { 175, 69, 143, 80, 85, 82, 72, 155, 103}, 2119 { 56, 58, 10, 171, 218, 189, 17, 13, 152}, 2120 { 144, 71, 10, 38, 171, 213, 144, 34, 26}, 2121 { 114, 26, 17, 163, 44, 195, 21, 10, 173}, 2122 { 121, 24, 80, 195, 26, 62, 44, 64, 85}, 2123 { 170, 46, 55, 19, 136, 160, 33, 206, 71}, 2124 { 63, 20, 8, 114, 114, 208, 12, 9, 226}, 2125 { 81, 40, 11, 96, 182, 84, 29, 16, 36} 2126 }, 2127 { 2128 { 134, 183, 89, 137, 98, 101, 106, 165, 148}, 2129 { 72, 187, 100, 130, 157, 111, 32, 75, 80}, 2130 { 66, 102, 167, 99, 74, 62, 40, 234, 128}, 2131 { 41, 53, 9, 178, 241, 141, 26, 8, 107}, 2132 { 104, 79, 12, 27, 217, 255, 87, 17, 7}, 2133 { 74, 43, 26, 146, 73, 166, 49, 23, 157}, 2134 { 65, 38, 105, 160, 51, 52, 31, 115, 128}, 2135 { 87, 68, 71, 44, 114, 51, 15, 186, 23}, 2136 { 47, 41, 14, 110, 182, 183, 21, 17, 194}, 2137 { 66, 45, 25, 102, 197, 189, 23, 18, 22} 2138 }, 2139 { 2140 { 88, 88, 147, 150, 42, 46, 45, 196, 205}, 2141 { 43, 97, 183, 117, 85, 38, 35, 179, 61}, 2142 { 39, 53, 200, 87, 26, 21, 43, 232, 171}, 2143 { 56, 34, 51, 104, 114, 102, 29, 93, 77}, 2144 { 107, 54, 32, 26, 51, 1, 81, 43, 31}, 2145 { 39, 28, 85, 171, 58, 165, 90, 98, 64}, 2146 { 34, 22, 116, 206, 23, 34, 43, 166, 73}, 2147 { 68, 25, 106, 22, 64, 171, 36, 225, 114}, 2148 { 34, 19, 21, 102, 132, 188, 16, 76, 124}, 2149 { 62, 18, 78, 95, 85, 57, 50, 48, 51} 2150 }, 2151 { 2152 { 193, 101, 35, 159, 215, 111, 89, 46, 111}, 2153 { 60, 148, 31, 172, 219, 228, 21, 18, 111}, 2154 { 112, 113, 77, 85, 179, 255, 38, 120, 114}, 2155 { 40, 42, 1, 196, 245, 209, 10, 25, 109}, 2156 { 100, 80, 8, 43, 154, 1, 51, 26, 71}, 2157 { 88, 43, 29, 140, 166, 213, 37, 43, 154}, 2158 { 61, 63, 30, 155, 67, 45, 68, 1, 209}, 2159 { 142, 78, 78, 16, 255, 128, 34, 197, 171}, 2160 { 41, 40, 5, 102, 211, 183, 4, 1, 221}, 2161 { 51, 50, 17, 168, 209, 192, 23, 25, 82} 2162 }, 2163 { 2164 { 125, 98, 42, 88, 104, 85, 117, 175, 82}, 2165 { 95, 84, 53, 89, 128, 100, 113, 101, 45}, 2166 { 75, 79, 123, 47, 51, 128, 81, 171, 1}, 2167 { 57, 17, 5, 71, 102, 57, 53, 41, 49}, 2168 { 115, 21, 2, 10, 102, 255, 166, 23, 6}, 2169 { 38, 33, 13, 121, 57, 73, 26, 1, 85}, 2170 { 41, 10, 67, 138, 77, 110, 90, 47, 114}, 2171 { 101, 29, 16, 10, 85, 128, 101, 196, 26}, 2172 { 57, 18, 10, 102, 102, 213, 34, 20, 43}, 2173 { 117, 20, 15, 36, 163, 128, 68, 1, 26} 2174 }, 2175 { 2176 { 138, 31, 36, 171, 27, 166, 38, 44, 229}, 2177 { 67, 87, 58, 169, 82, 115, 26, 59, 179}, 2178 { 63, 59, 90, 180, 59, 166, 93, 73, 154}, 2179 { 40, 40, 21, 116, 143, 209, 34, 39, 175}, 2180 { 57, 46, 22, 24, 128, 1, 54, 17, 37}, 2181 { 47, 15, 16, 183, 34, 223, 49, 45, 183}, 2182 { 46, 17, 33, 183, 6, 98, 15, 32, 183}, 2183 { 65, 32, 73, 115, 28, 128, 23, 128, 205}, 2184 { 40, 3, 9, 115, 51, 192, 18, 6, 223}, 2185 { 87, 37, 9, 115, 59, 77, 64, 21, 47} 2186 }, 2187 { 2188 { 104, 55, 44, 218, 9, 54, 53, 130, 226}, 2189 { 64, 90, 70, 205, 40, 41, 23, 26, 57}, 2190 { 54, 57, 112, 184, 5, 41, 38, 166, 213}, 2191 { 30, 34, 26, 133, 152, 116, 10, 32, 134}, 2192 { 75, 32, 12, 51, 192, 255, 160, 43, 51}, 2193 { 39, 19, 53, 221, 26, 114, 32, 73, 255}, 2194 { 31, 9, 65, 234, 2, 15, 1, 118, 73}, 2195 { 88, 31, 35, 67, 102, 85, 55, 186, 85}, 2196 { 56, 21, 23, 111, 59, 205, 45, 37, 192}, 2197 { 55, 38, 70, 124, 73, 102, 1, 34, 98} 2198 }, 2199 { 2200 { 102, 61, 71, 37, 34, 53, 31, 243, 192}, 2201 { 69, 60, 71, 38, 73, 119, 28, 222, 37}, 2202 { 68, 45, 128, 34, 1, 47, 11, 245, 171}, 2203 { 62, 17, 19, 70, 146, 85, 55, 62, 70}, 2204 { 75, 15, 9, 9, 64, 255, 184, 119, 16}, 2205 { 37, 43, 37, 154, 100, 163, 85, 160, 1}, 2206 { 63, 9, 92, 136, 28, 64, 32, 201, 85}, 2207 { 86, 6, 28, 5, 64, 255, 25, 248, 1}, 2208 { 56, 8, 17, 132, 137, 255, 55, 116, 128}, 2209 { 58, 15, 20, 82, 135, 57, 26, 121, 40} 2210 }, 2211 { 2212 { 164, 50, 31, 137, 154, 133, 25, 35, 218}, 2213 { 51, 103, 44, 131, 131, 123, 31, 6, 158}, 2214 { 86, 40, 64, 135, 148, 224, 45, 183, 128}, 2215 { 22, 26, 17, 131, 240, 154, 14, 1, 209}, 2216 { 83, 12, 13, 54, 192, 255, 68, 47, 28}, 2217 { 45, 16, 21, 91, 64, 222, 7, 1, 197}, 2218 { 56, 21, 39, 155, 60, 138, 23, 102, 213}, 2219 { 85, 26, 85, 85, 128, 128, 32, 146, 171}, 2220 { 18, 11, 7, 63, 144, 171, 4, 4, 246}, 2221 { 35, 27, 10, 146, 174, 171, 12, 26, 128} 2222 }, 2223 { 2224 { 190, 80, 35, 99, 180, 80, 126, 54, 45}, 2225 { 85, 126, 47, 87, 176, 51, 41, 20, 32}, 2226 { 101, 75, 128, 139, 118, 146, 116, 128, 85}, 2227 { 56, 41, 15, 176, 236, 85, 37, 9, 62}, 2228 { 146, 36, 19, 30, 171, 255, 97, 27, 20}, 2229 { 71, 30, 17, 119, 118, 255, 17, 18, 138}, 2230 { 101, 38, 60, 138, 55, 70, 43, 26, 142}, 2231 { 138, 45, 61, 62, 219, 1, 81, 188, 64}, 2232 { 32, 41, 20, 117, 151, 142, 20, 21, 163}, 2233 { 112, 19, 12, 61, 195, 128, 48, 4, 24} 2234 } 2235 }; 2237 ---- End code block ---------------------------------------- 2239 12. Intraframe Prediction 2241 Intraframe prediction uses already-coded macroblocks within the 2242 current frame to approximate the contents of the current macroblock. 2243 It applies to intra-coded macroblocks in an interframe and to all 2244 macroblocks in a key frame. 2246 Relative to the current macroblock "M", the already-coded macroblocks 2247 include all macroblocks above M together with the macroblocks on the 2248 same row as, and to the left of, M, though at most four of these 2249 macroblocks are actually used: the block "A" directly above M, the 2250 blocks immediately to the left and right of A, and the block 2251 immediately to the left of M. 2253 Each of the prediction modes (i.e., means of extrapolation from 2254 already-calculated values) uses fairly simple arithmetic on pixel 2255 values whose positions, relative to the current position, are defined 2256 by the mode. 2258 The chroma (U and V) and luma (Y) predictions are independent of each 2259 other. 2261 The relative addressing of pixels applied to macroblocks on the upper 2262 row or left column of the frame will sometimes cause pixels outside 2263 the visible frame to be referenced. Usually such out-of-bounds 2264 pixels have an assumed value of 129 for pixels to the left of the 2265 leftmost column of the visible frame and 127 for pixels above the top 2266 row of the visible frame (including the special case of the pixel 2267 above and to the left of the top-left pixel in the visible frame). 2268 Exceptions to this (associated to certain modes) will be noted below. 2270 The already-coded macroblocks referenced by intra-prediction have 2271 been "reconstructed", that is, have been predicted and residue- 2272 adjusted (as described in Chapter 14), but have not been loop- 2273 filtered. While it does process the edges between individual 2274 macroblocks and individual subblocks, loop filtering (described in 2275 Chapter 15) is applied to the frame as a whole, after all of the 2276 macroblocks have been reconstructed. 2278 12.1. mb_skip_coeff 2280 The single bool flag is decoded using prob_skip_false if and only if 2281 mb_no_coeff_skip is set to 1 (see Sections 9.10 and 9.11). If 2282 mb_no_coeff_skip is set to 0 then this value defaults to 0. 2284 12.2. Chroma Prediction 2286 The chroma prediction is a little simpler than the luma prediction, 2287 so we treat it first. Each of the chroma modes treats U and V 2288 identically, that is, the U and V prediction values are calculated in 2289 parallel, using the same relative addressing and arithmetic in each 2290 of the two planes. 2292 The modes extrapolate prediction values using the 8-pixel row "A" 2293 lying immediately above the block (that is, the bottom chroma row of 2294 the macroblock immediately above the current macroblock) and the 2295 8-pixel column "L" immediately to the left of the block (that is, the 2296 rightmost chroma column of the macroblock immediately to the left of 2297 the current macroblock). 2299 Vertical prediction (chroma mode V_PRED) simply fills each 8-pixel 2300 row of the 8x8 chroma block with a copy of the "above" row (A). If 2301 the current macroblock lies on the top row of the frame, all 8 of the 2302 pixel values in A are assigned the value 127. 2304 Similarly, horizontal prediction (H_PRED) fills each 8-pixel column 2305 of the 8x8 chroma block with a copy of the "left" column (L). If the 2306 current macroblock is in the left column of the frame, all 8 pixel 2307 values in L are assigned the value 129. 2309 DC prediction (DC_PRED) fills the 8x8 chroma block with a single 2310 value. In the generic case of a macroblock lying below the top row 2311 and right of the leftmost column of the frame, this value is the 2312 average of the 16 (genuinely visible) pixels in the (union of the) 2313 above row A and left column L. 2315 Otherwise, if the current macroblock lies on the top row of the 2316 frame, the average of the 8 pixels in L is used; if it lies in the 2317 left column of the frame, the average of the 8 pixels in A is used. 2318 Note that the averages used in these exceptional cases are not the 2319 same as those that would be arrived at by using the out-of-bounds A 2320 and L values defined for V_PRED and H_PRED. In the case of the 2321 leftmost macroblock on the top row of the frame the 8x8 block is 2322 simply filled with the constant value 128. 2324 For DC_PRED, apart from the exceptional case of the top left 2325 macroblock, we are averaging either 16 or 8 pixel values to get a 2326 single prediction value that fills the 8x8 block. The rounding is 2327 done as follows: 2329 ---- Begin code block -------------------------------------- 2331 int sum; /* sum of 8 or 16 pixels at (at least) 16-bit precision */ 2332 int shf; /* base 2 logarithm of the number of pixels (3 or 4) */ 2334 Pixel DCvalue = (sum + (1 << (shf-1))) >> shf; 2336 ---- End code block ---------------------------------------- 2338 Because the summands are all valid pixels, no "clamp" is necessary in 2339 the calculation of DCvalue. 2341 The remaining "True Motion" (TM_PRED) chroma mode gets its name from 2342 an older technique of video compression used by On2 Technologies, to 2343 which it bears some relation. In addition to the row "A" and column 2344 "L", TM_PRED uses the pixel "P" above and to the left of the chroma 2345 block. 2347 The following figure gives an example of how TM_PRED works: 2349 ---- Begin code block -------------------------------------- 2351 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2352 | P | A0 | A1 | A2 | A3 | A4 | A5 | A6 | A7 | 2353 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2354 | L0 | X00 | X01 | X02 | X03 | X04 | X05 | X06 | X07 | 2355 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2356 | L1 | X10 | X11 | X12 | X13 | X14 | X15 | X16 | X17 | 2357 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2358 | L2 | X20 | X21 | X22 | X23 | X24 | X25 | X26 | X27 | 2359 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2360 | L3 | X30 | X31 | X32 | X33 | X34 | X35 | X36 | X37 | 2361 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2362 | L4 | X40 | X41 | X42 | X43 | X44 | X45 | X46 | X47 | 2363 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2364 | L5 | X50 | X51 | X52 | X53 | X54 | X55 | X56 | X57 | 2365 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2366 | L6 | X60 | X61 | X62 | X63 | X64 | X65 | X66 | X67 | 2367 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2368 | L7 | X70 | X71 | X72 | X73 | X74 | X75 | X76 | X77 | 2369 |-----|-----|-----|-----|-----|-----|-----|-----|-----| 2371 ---- End code block ---------------------------------------- 2373 Where P, As and Ls represent reconstructed pixel values from 2374 previously coded blocks, and X00 through X77 represent predicted 2375 values for the current block. TM_PRED uses the following equation to 2376 calculate X_ij: 2378 X_ij = L_i + A_j - P (i, j=0, 1, 2, 3) 2380 The exact algorithm is as follows: 2382 ---- Begin code block -------------------------------------- 2384 void TMpred( 2385 Pixel b[8][8], /* chroma (U or V) prediction block */ 2386 const Pixel A[8], /* row of already-constructed pixels 2387 above block */ 2388 const Pixel L[8], /* column of "" just to the left of 2389 block */ 2390 const Pixel P /* pixel just to the left of A and 2391 above L*/ 2392 ) { 2393 int r = 0; /* row */ 2394 do { 2395 int c = 0; /* column */ 2396 do { 2397 b[r][c] = clamp255( L[r]+ A[c] - P); 2398 } while( ++c < 8); 2399 } while( ++r < 8); 2400 } 2402 ---- End code block ---------------------------------------- 2404 Note that the process could equivalently be described as propagating 2405 the vertical differences between pixels in L (starting from P), using 2406 the pixels from A to start each column. 2408 An implementation of chroma intra-prediction may be found in the 2409 reference decoder file predict.c. 2411 Unlike DC_PRED, for macroblocks on the top row or left edge TM_PRED 2412 does use the out-of-bounds values of 127 and 129 (respectively) 2413 defined for V_PRED and H_PRED. 2415 12.3. Luma Prediction 2417 The prediction processes for the first four 16x16 luma modes 2418 (DC_PRED, V_PRED, H_PRED, and TM_PRED) are essentially identical to 2419 the corresponding chroma prediction processes described above, the 2420 only difference being that we are predicting a single 16x16 luma 2421 block instead of two 8x8 chroma blocks. 2423 Thus, the row "A" and column "L" here contain 16 pixels, the DC 2424 prediction is calculated using 16 or 32 pixels (and shf is 4 or 5), 2425 and we of course fill the entire prediction buffer, that is, 16 rows 2426 (or columns) containing 16 pixels each. The reference implementation 2427 of 16x16 luma prediction is also in predict.c. 2429 In the remaining luma mode (B_PRED), each 4x4 Y subblock is 2430 independently predicted using one of ten modes (listed, along with 2431 their encodings, in Chapter 11). 2433 Also, unlike the full-macroblock modes already described, some of the 2434 subblock modes use prediction pixels above and to the right of the 2435 current subblock. In detail, each 4x4 subblock "B" is predicted 2436 using (at most) the 4-pixel column "L" immediately to the left of B 2437 and the 8-pixel row "A" immediately above B, consisting of the 4 2438 pixels above B followed by the 4 adjacent pixels above and to the 2439 right of B, together with the single pixel "P" immediately to the 2440 left of A (and immediately above L). 2442 For the purpose of subblock intra-prediction, the pixels immediately 2443 to the left and right of a pixel in a subblock are the same as the 2444 pixels immediately to the left and right of the corresponding pixel 2445 in the frame buffer "F". Vertical offsets behave similarly: The 2446 above row A lies immediately above B in F, and the adjacent pixels in 2447 the left column L are separated by a single row in F. 2449 Because entire macroblocks (as opposed to their constituent 2450 subblocks) are reconstructed in raster-scan order, for subblocks 2451 lying along the right edge (and not along the top row) of the current 2452 macroblock, the four "extra" prediction pixels in A above and to the 2453 right of B have not yet actually been constructed. 2455 Subblocks 7, 11, and 15 are affected. All three of these subblocks 2456 use the same extra pixels as does subblock 3 (at the upper right 2457 corner of the macroblock), namely the 4 pixels immediately above and 2458 to the right of subblock 3. Writing (R,C) for a frame buffer 2459 position offset from the upper left corner of the current macroblock 2460 by R rows and C columns, the extra pixels for all the right-edge 2461 subblocks (3, 7, 11, and 15) are at positions (-1,16), (-1,17), 2462 (-1,18), and (-1,19). For the right-most macroblock in each 2463 macroblock row except the top row, the extra pixels shall use the 2464 same value as the pixel at position (-1, 15), which is the right-most 2465 visible pixel on the line immediately above the macroblock row. For 2466 the top macroblock row, all the extra pixels assume a value of 127. 2468 The details of the prediction modes are most easily described in 2469 code. 2471 ---- Begin code block -------------------------------------- 2473 /* Result pixels are often averages of two or three predictor 2474 pixels. The following subroutines are used to calculate 2475 these averages. Because the arguments are valid pixels, no 2476 clamping is necessary. An actual implementation would 2477 probably use inline functions or macros. */ 2479 /* Compute weighted average centered at y w/adjacent x, z */ 2481 Pixel avg3( Pixel x, Pixel y, Pixel z) { 2482 return (x + y + y + z + 2) >> 2;} 2484 /* Weighted average of 3 adjacent pixels centered at p */ 2486 Pixel avg3p( const Pixel *p) { return avg3( p[-1], p[0], p[1]);} 2488 /* Simple average of x and y */ 2490 Pixel avg2( Pixel x, Pixel y) { return (x + y + 1) >> 1;} 2492 /* Average of p[0] and p[1] may be considered to be a synthetic 2493 pixel lying between the two, that is, one half-step past p. */ 2495 Pixel avg2p( const Pixel *p) { return avg2( p[0], p[1]);} 2497 void subblock_intra_predict( 2498 Pixel B[4][4], /* Y subblock prediction buffer */ 2499 const Pixel *A, /* A[0]...A[7] = above row, A[-1] = P */ 2500 const Pixel *L, /* L[0]...L[3] = left column, L[-1] = P */ 2501 intra_bmode mode /* enum is in section 11.1 above */ 2502 ) { 2503 Pixel E[9]; /* 9 already-constructed edge pixels */ 2504 E[0] = L[3]; E[1] = L[2]; E[2] = L[1]; E[3] = L[0]; 2505 E[4] = A[-1]; /* == L[-1] == P */ 2506 E[5] = A[0]; E[6] = A[1]; E[7] = A[2]; E[8] = A[3]; 2508 switch( mode) { 2509 /* First four modes are similar to corresponding 2510 full-block modes. */ 2512 case B_DC_PRED: 2513 { 2514 int v = 4; /* DC sum/avg, 4 is rounding adjustment */ 2515 int i = 0; do { v += A[i] + L[i];} while( ++i < 4); 2516 v >>= 3; /* averaging 8 pixels */ 2517 i = 0; do { /* fill prediction buffer with constant DC 2518 value */ 2519 int j = 0; do { B[i][j] = v;} while( ++j < 4); 2520 } while( ++i < 4); 2521 break; 2523 } 2525 case B_TM_PRED: /* just like 16x16 TM_PRED */ 2526 { 2527 int r = 0; do { 2528 int c = 0; do { 2529 B[r][c] = clamp255( L[r] + A[c] - A[-1]); 2530 } while( ++c < 4); 2531 } while( ++r < 4); 2532 break; 2533 } 2535 case B_VE_PRED: /* like 16x16 V_PRED except using averages */ 2536 { 2537 int c = 0; do { /* all 4 rows = smoothed top row */ 2538 B[0][c] = B[1][c] = B[2][c] = B[3][c] = avg3p( A + c); 2539 } while( ++c < 4); 2540 break; 2541 } 2543 case B_HE_PRED: /* like 16x16 H_PRED except using averages */ 2544 { 2545 /* Bottom row is exceptional because L[4] does not exist */ 2546 int v = avg3( L[2], L[3], L[3]); 2547 int r = 3; while( 1) { /* all 4 columns = smoothed left 2548 column */ 2549 B[r][0] = B[r][1] = B[r][2] = B[r][3] = v; 2550 if( --r < 0) 2551 break; 2552 v = avg3p( L + r); /* upper 3 rows use average of 2553 3 pixels */ 2554 } 2555 break; 2556 } 2558 /* The remaining six "diagonal" modes subdivide the 2559 prediction buffer into diagonal lines. All the pixels 2560 on each line are assigned the same value; this value is 2561 (a smoothed or synthetic version of) an 2562 already-constructed predictor value lying on the same 2563 line. For clarity, in the comments, we express the 2564 positions of these predictor pixels relative to the 2565 upper left corner of the destination array B. 2567 These modes are unique to subblock prediction and have 2568 no full-block analogues. The first two use lines at 2569 +|- 45 degrees from horizontal (or, equivalently, 2570 vertical), that is, lines whose slopes are +|- 1. */ 2572 case B_LD_PRED: /* southwest (left and down) step = 2573 (-1, 1) or (1,-1) */ 2574 /* avg3p( A + j) is the "smoothed" pixel at (-1,j) */ 2575 B[0][0] = avg3p( A + 1); 2576 B[0][1] = B[1][0] = avg3p( A + 2); 2577 B[0][2] = B[1][1] = B[2][0] = avg3p( A + 3); 2578 B[0][3] = B[1][2] = B[2][1] = B[3][0] = avg3p( A + 4); 2579 B[1][3] = B[2][2] = B[3][1] = avg3p( A + 5); 2580 B[2][3] = B[3][2] = avg3p( A + 6); 2581 B[3][3] = avg3( A[6], A[7], A[7]); /* A[8] does not exist */ 2582 break; 2584 case B_RD_PRED: /* southeast (right and down) step = 2585 (1,1) or (-1,-1) */ 2586 B[3][0] = avg3p( E + 1); /* predictor is from (2, -1) */ 2587 B[3][1] = B[2][0] = avg3p( E + 2); /* (1, -1) */ 2588 B[3][2] = B[2][1] = B[1][0] = avg3p( E + 3); /* (0, -1) */ 2589 B[3][3] = B[2][2] = B[1][1] = B[0][0] = 2590 avg3p( E + 4); /* (-1, -1) */ 2591 B[2][3] = B[1][2] = B[0][1] = avg3p( E + 5); /* (-1, 0) */ 2592 B[1][3] = B[0][2] = avg3p( E + 6); /* (-1, 1) */ 2593 B[0][3] = avg3p( E + 7); /* (-1, 2) */ 2594 break; 2596 /* The remaining 4 diagonal modes use lines whose slopes are 2597 +|- 2 and +|- 1/2. The angles of these lines are roughly 2598 +|- 27 degrees from horizontal or vertical. 2600 Unlike the 45 degree diagonals, here we often need to 2601 "synthesize" predictor pixels midway between two actual 2602 predictors using avg2p(p), which we think of as returning 2603 the pixel "at" p[1/2]. */ 2605 case B_VR_PRED: /* SSE (vertical right) step = 2606 (2,1) or (-2,-1) */ 2607 B[3][0] = avg3p( E + 2); /* predictor is from (1, -1) */ 2608 B[2][0] = avg3p( E + 3); /* (0, -1) */ 2609 B[3][1] = B[1][0] = avg3p( E + 4); /* (-1, -1) */ 2610 B[2][1] = B[0][0] = avg2p( E + 4); /* (-1, -1/2) */ 2611 B[3][2] = B[1][1] = avg3p( E + 5); /* (-1, 0) */ 2612 B[2][2] = B[0][1] = avg2p( E + 5); /* (-1, 1/2) */ 2613 B[3][3] = B[1][2] = avg3p( E + 6); /* (-1, 1) */ 2614 B[2][3] = B[0][2] = avg2p( E + 6); /* (-1, 3/2) */ 2615 B[1][3] = avg3p( E + 7); /* (-1, 2) */ 2616 B[0][3] = avg2p( E + 7); /* (-1, 5/2) */ 2617 break; 2619 case B_VL_PRED: /* SSW (vertical left) step = 2620 (2,-1) or (-2,1) */ 2621 B[0][0] = avg2p( A); /* predictor is from (-1, 1/2) */ 2622 B[1][0] = avg3p( A + 1); /* (-1, 1) */ 2623 B[2][0] = B[0][1] = avg2p( A + 1); /* (-1, 3/2) */ 2624 B[1][1] = B[3][0] = avg3p( A + 2); /* (-1, 2) */ 2625 B[2][1] = B[0][2] = avg2p( A + 2); /* (-1, 5/2) */ 2626 B[3][1] = B[1][2] = avg3p( A + 3); /* (-1, 3) */ 2627 B[2][2] = B[0][3] = avg2p( A + 3); /* (-1, 7/2) */ 2628 B[3][2] = B[1][3] = avg3p( A + 4); /* (-1, 4) */ 2629 /* Last two values do not strictly follow the pattern. */ 2630 B[2][3] = avg3p( A + 5); /* (-1, 5) [avg2p( A + 4) = 2631 (-1,9/2)] */ 2632 B[3][3] = avg3p( A + 6); /* (-1, 6) [avg3p( A + 5) = 2633 (-1,5)] */ 2634 break; 2636 case B_HD_PRED: /* ESE (horizontal down) step = 2637 (1,2) or (-1,-2) */ 2638 B[3][0] = avg2p( E); /* predictor is from (5/2, -1) */ 2639 B[3][1] = avg3p( E + 1); /* (2, -1) */ 2640 B[2][0] = B[3][2] = svg2p( E + 1); /* ( 3/2, -1) */ 2641 B[2][1] = B[3][3] = avg3p( E + 2); /* ( 1, -1) */ 2642 B[2][2] = B[1][0] = avg2p( E + 2); /* ( 1/2, -1) */ 2643 B[2][3] = B[1][1] = avg3p( E + 3); /* ( 0, -1) */ 2644 B[1][2] = B[0][0] = avg2p( E + 3); /* (-1/2, -1) */ 2645 B[1][3] = B[0][1] = avg3p( E + 4); /* ( -1, -1) */ 2646 B[0][2] = avg3p( E + 5); /* (-1, 0) */ 2647 B[0][3] = avg3p( E + 6); /* (-1, 1) */ 2648 break; 2650 case B_HU_PRED: /* ENE (horizontal up) step = (1,-2) 2651 or (-1,2) */ 2652 B[0][0] = avg2p( L); /* predictor is from ( 1/2, -1) */ 2653 B[0][1] = avg3p( L + 1); /* ( 1, -1) */ 2654 B[0][2] = B[1][0] = avg2p( L + 1); /* (3/2, -1) */ 2655 B[0][3] = B[1][1] = avg3p( L + 2); /* ( 2, -1) */ 2656 B[1][2] = B[2][0] = avg2p( L + 2); /* (5/2, -1) */ 2657 B[1][3] = B[2][1] = avg3( L[2], L[3], L[3]); /* ( 3, -1) */ 2658 /* Not possible to follow pattern for much of the bottom 2659 row because no (nearby) already-constructed pixels lie 2660 on the diagonals in question. */ 2661 B[2][2] = B[2][3] = B[3][0] = B[3][1] = B[3][2] = B[3][3] 2662 = L[3]; 2663 } 2664 } 2666 ---- End code block ---------------------------------------- 2667 The reference decoder implementation of subblock intra-prediction may 2668 be found in reconintra4x4.c. 2670 13. DCT Coefficient Decoding 2672 The second data partition consists of an encoding of the quantized 2673 DCT (and WHT) coefficients of the residue signal. As discussed in 2674 the format overview (Chapter 2), for each macroblock, the residue is 2675 added to the (intra- or inter-generated) prediction buffer to produce 2676 the final (except for loop-filtering) reconstructed macroblock. 2678 VP8 works exclusively with 4x4 DCTs and WHTs, applied to the 24 (or 2679 25 with the Y2 subblock) 4x4 subblocks of a macroblock. The ordering 2680 of macroblocks within any of the "residue" partitions in general 2681 follows the same raster-scan as used in the first "prediction" 2682 partition. 2684 For all intra- and inter-prediction modes apart from B_PRED (intra: 2685 whose Y subblocks are independently predicted) and SPLIT_MV (inter) 2686 each macroblock's residue record begins with the Y2 component of the 2687 residue, coded using a WHT. B_PRED and SPLIT_MV coded macroblocks 2688 omit this WHT, instead specifying the 0th DCT coefficient of each of 2689 the 16 Y subblocks as part of its DCT. 2691 After the optional Y2 block, the residue record continues with 16 2692 DCTs for the Y subblocks, followed by 4 DCTs for the U subblocks, 2693 ending with 4 DCTs for the V subblocks. The subblocks occur in the 2694 usual order. 2696 The DCTs and WHT are tree-coded using a 12-element alphabet whose 2697 members we call tokens. Except for the end of block token (which 2698 sets the remaining subblock coefficients to zero and is followed by 2699 the next block), each token (sometimes augmented with data 2700 immediately following the token) specifies the value of the single 2701 coefficient at the current (implicit) position and is followed by a 2702 token applying to the next (implicit) position. 2704 For all the Y and chroma subblocks, the ordering of the coefficients 2705 follows a so-called zig-zag order. DCTs begin at coefficient 1 if Y2 2706 is present, and begin at coefficient 0 if Y2 is absent. The WHT for 2707 a Y2 subblock always begins at coefficient 0. 2709 13.1. MB Without non-Zero Coefficient Values 2711 If the flag within macroblock mode info indicates that a macroblock 2712 does not have any non-zero coefficients, the decoding process of DCT 2713 coefficients is skipped for the macroblock. 2715 13.2. Coding of Individual Coefficient Values 2717 The coding of coefficient tokens is the same for the DCT and WHT and 2718 for the remainder of this chapter DCT should be taken to mean either 2719 DCT or WHT. 2721 All tokens (except end-of-block) specify either a single unsigned 2722 value or a range of unsigned values (immediately) followed by a 2723 simple probabilistic encoding of the offset of the value from the 2724 base of that range. 2726 Non-zero values (of either type) are then followed by a flag 2727 indicating the sign of the coded value (negative if 1, positive if 2728 0). 2730 Here are the tokens and decoding tree. 2732 ---- Begin code block -------------------------------------- 2734 typedef enum 2735 { 2736 DCT_0, /* value 0 */ 2737 DCT_1, /* 1 */ 2738 DCT_2, /* 2 */ 2739 DCT_3, /* 3 */ 2740 DCT_4, /* 4 */ 2741 dct_cat1, /* range 5 - 6 (size 2) */ 2742 dct_cat2, /* 7 - 10 (4) */ 2743 dct_cat3, /* 11 - 18 (8) */ 2744 dct_cat4, /* 19 - 34 (16) */ 2745 dct_cat5, /* 35 - 66 (32) */ 2746 dct_cat6, /* 67 - 2048 (1982) */ 2747 dct_eob, /* end of block */ 2749 num_dct_tokens /* 12 */ 2750 } 2751 dct_token; 2753 const tree_index coef_tree [2 * (num_dct_tokens - 1)] = 2754 { 2755 -dct_eob, 2, /* eob = "0" */ 2756 -DCT_0, 4, /* 0 = "10" */ 2757 -DCT_1, 6, /* 1 = "110" */ 2758 8, 12, 2759 -DCT_2, 10, /* 2 = "11100" */ 2760 -DCT_3, -DCT_4, /* 3 = "111010", 4 = "111011" */ 2761 14, 16, 2762 -dct_cat1, -dct_cat2, /* cat1 = "111100", 2763 cat2 = "111101" */ 2764 18, 20, 2765 -dct_cat3, -dct_cat4, /* cat3 = "1111100", 2766 cat4 = "1111101" */ 2767 -dct_cat5, -dct_cat6 /* cat4 = "1111110", 2768 cat4 = "1111111" */ 2769 }; 2771 ---- End code block ---------------------------------------- 2773 In general, all DCT coefficients are decoded using the same tree. 2774 However, if the preceding coefficient is a DCT_0, decoding will skip 2775 the first branch since it is not possible for dct_eob to follow a 2776 DCT_0. 2778 The tokens dct_cat1 ... dct_cat6 specify ranges of unsigned values, 2779 the value within the range being formed by adding an unsigned offset 2780 (whose width is 1, 2, 3, 4, 5, or 11 bits, respectively) to the base 2781 of the range, using the following algorithm and fixed probability 2782 tables. 2784 ---- Begin code block -------------------------------------- 2786 uint DCTextra( bool_decoder *d, const Prob *p) 2787 { 2788 uint v = 0; 2789 do { v += v + read_bool( d, *p);} while( *++p); 2790 return v; 2791 } 2793 const Prob Pcat1[] = { 159, 0}; 2794 const Prob Pcat2[] = { 165, 145, 0}; 2795 const Prob Pcat3[] = { 173, 148, 140, 0}; 2796 const Prob Pcat4[] = { 176, 155, 140, 135, 0}; 2797 const Prob Pcat5[] = { 180, 157, 141, 134, 130, 0}; 2798 const Prob Pcat6[] = 2799 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0}; 2801 ---- End code block ---------------------------------------- 2803 If v, the unsigned value decoded using the coefficient tree, possibly 2804 augmented by the process above, is non-zero, its sign is set by 2805 simply reading a flag: 2807 ---- Begin code block -------------------------------------- 2809 if( read_bool( d, 128)) 2810 v = -v; 2812 ---- End code block ---------------------------------------- 2814 13.3. Token Probabilities 2816 The probability specification for the token tree (unlike that for the 2817 "extra bits" described above) is rather involved. It uses three 2818 pieces of context to index a large probability table, the contents of 2819 which may be incrementally modified in the frame header. The full 2820 (non-constant) probability table is laid out as follows. 2822 ---- Begin code block -------------------------------------- 2824 Prob coef_probs [4] [8] [3] [num_dct_tokens-1]; 2826 ---- End code block ---------------------------------------- 2827 Working from the outside in, the outermost dimension is indexed by 2828 the type of plane being decoded: 2830 o 0 - Y beginning at coefficient 1 (i.e., Y after Y2) 2832 o 1 - Y2 2834 o 2 - U or V 2836 o 3 - Y beginning at coefficient 0 (i.e., Y in the absence of Y2). 2838 The next dimension is selected by the position of the coefficient 2839 being decoded. That position c steps by ones up to 15, starting from 2840 zero for block types 1, 2, or 3 and starting from one for block type 2841 0. The second array index is then 2843 ---- Begin code block -------------------------------------- 2845 coef_bands [c] 2847 ---- End code block ---------------------------------------- 2849 where 2851 ---- Begin code block -------------------------------------- 2853 const int coef_bands [16] = { 2854 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7 2855 }; 2857 ---- End code block ---------------------------------------- 2859 is a fixed mapping of position to "band". 2861 The third dimension is the trickiest. Roughly speaking, it measures 2862 the "local complexity" or extent to which nearby coefficients are 2863 non-zero. 2865 For the first coefficient (DC, unless the block type is 0), we 2866 consider the (already encoded) blocks within the same plane (Y2, Y, 2867 U, or V) above and to the left of the current block. The context 2868 index is then the number (0, 1 or 2) of these blocks that had at 2869 least one non-zero coefficient in their residue record. Specifically 2870 for Y2, because macroblocks above and to the left may or may not have 2871 a Y2 block, the block above is determined by the most recent 2872 macroblock in the same column that has a Y2 block, and the block to 2873 the left is determined by the most recent macroblock in the same row 2874 that has a Y2 block. 2876 Beyond the first coefficient, the context index is determined by the 2877 absolute value of the most recently decoded coefficient (necessarily 2878 within the current block) and is 0 if the last coefficient was a 2879 zero, 1 if it was plus or minus one, and 2 if its absolute value 2880 exceeded one. 2882 Note that the intuitive meaning of this measure changes as 2883 coefficients are decoded. For example, prior to the first token, a 2884 zero means that the neighbors are empty, suggesting that the current 2885 block may also be empty. After the first token, because an end-of- 2886 block token must have at least one non-zero value before it, a zero 2887 means that we just decoded a zero and hence guarantees that a non- 2888 zero coefficient will appear later in this block. However, this 2889 shift in meaning is perfectly okay because the complete context 2890 depends also on the coefficient band (and since band 0 is occupied 2891 exclusively by position 0). 2893 As with other contexts used by VP8, the "neighboring block" context 2894 described here needs a special definition for subblocks lying along 2895 the top row or left edge of the frame. These "non-existent" 2896 predictors above and to the left of the image are simply taken to be 2897 empty -- that is, taken to contain no non-zero coefficients. 2899 The residue decoding of each macroblock then requires, in each of two 2900 directions (above and to the left), an aggregate coefficient 2901 predictor consisting of a single Y2 predictor, two predictors for 2902 each of U and V, and four predictors for Y. In accordance with the 2903 scan-ordering of macroblocks, a decoder needs to maintain a single 2904 "left" aggregate predictor and a row of "above" aggregate predictors. 2906 Before decoding any residue, these maintained predictors may simply 2907 be cleared, in compliance with the definition of "non-existent" 2908 prediction. After each block is decoded, the two predictors 2909 referenced by the block are replaced with the (empty or non-empty) 2910 state of the block, in preparation for the later decoding of the 2911 blocks below and to the right of the block just decoded. 2913 The fourth, and final, dimension of the token probability array is of 2914 course indexed by (half) the position in the token tree structure, as 2915 are all tree probability arrays. 2917 The below pseudo-code illustrates the decoding process. Note that 2918 criteria, functions, etc. delimited with ** are either dependent on 2919 decoder architecture or are elaborated on elsewhere in this document. 2921 ---- Begin code block -------------------------------------- 2923 int block[16] = { 0 }; /* current 4x4 block coeffs */ 2924 int firstCoeff = 0; 2925 int plane; 2926 int ctx2; 2927 int ctx3 = 0; /* the 3rd context referred to in above description */ 2928 Prob *probTable; 2929 int token; 2930 int sign; 2931 int absValue; 2932 int extraBits; 2933 bool prevCoeffWasZero = false; 2934 bool currentBlockHasCoeffs = false; 2935 /* base coeff abs values per each category, elem #0 is 2936 DCT_VAL_CATEGORY1, * #1 is DCT_VAL_CATEGORY2 etc */ 2937 int categoryBase[6] = { 5, 7, 11, 19, 35, 67 }; 2939 /* Determine plane to use */ 2940 if( **current_block_is_Y2_block** ) plane = 0; 2941 else if ( **current_block_is_chroma** ) plane = 2; 2942 else if ( **current_macroblock_has_Y2** ) plane = 1; 2943 else plane = 3; 2945 /* For luma blocks of an "Y2 macroblock" we skip coeff index #0 */ 2946 if( plane == 1 ) 2947 firstCoeff++; 2949 /* Determine whether neighbour 4x4 blocks have coeffiecients. 2950 This is dependant of the plane we are currently decoding; 2951 i.e. we check only coefficients from same plane as current 2952 block. */ 2953 if( **left_neighbor_block_has_coefficients(plane)** ) 2954 ctx3++; 2955 if( **above_neighbor_block_has_coefficients(plane)** ) 2956 ctx3++; 2958 for( i = firstCoeff ; i < 16 ; ++i ) 2959 { 2960 ctx2 = coef_bands[i]; 2961 probTable = coef_probs[plane][ctx2][ctx3]; 2963 /* skip first code (dct_eob) if previous token was DCT_0 */ 2964 if( prevCoeffWasZero ) 2965 token = treed_read ( d, **coef_tree_without_eob**, 2966 probTable ); 2967 else 2968 token = treed_read ( d, coef_tree, probTable ); 2970 if( token == dct_eob ) 2971 break; 2973 if( token != DCT_0 ) 2974 { 2975 currentBlockHasCoeffs = true; 2976 if( **token_has_extra_bits(token)** ) 2977 { 2978 extraBits = DCTextra( token ); 2979 absValue = 2980 categoryBase[**token_to_cat_index(token)**] + 2981 extraBits; 2982 } 2983 else 2984 { 2985 absValue = **token_to_abs_value(token)**; 2986 } 2988 sign = read_bool(d, 128); 2989 block[i] = sign ? -absValue : absValue; 2990 } 2991 else 2992 { 2993 absValue = 0; 2994 } 2996 /* Set contexts and stuff for next coeff */ 2997 if( absValue == 0 ) ctx3 = 0; 2998 else if ( absValue == 1 ) ctx3 = 1; 2999 else ctx3 = 2; 3000 prevCoeffWasZero = true; 3001 } 3003 /* Store current block status to decoder internals */ 3004 **block_has_coefficients[currentMb][currentBlock]** = 3005 currentBlockHasCoeffs; 3007 ---- End code block ---------------------------------------- 3009 While we have in fact completely described the coefficient decoding 3010 procedure, the reader will probably find it helpful to consult the 3011 reference implementation, which can be found in the file 3012 detokenize.c. 3014 13.4. Token Probability Updates 3016 As mentioned above, the token-decoding probabilities may change from 3017 frame to frame. After detection of a key frame, they are of course 3018 set to their defaults shown in Section 13.5; this must occur before 3019 decoding the remainder of the header, as both key frames and 3020 interframes may adjust these probabilities. 3022 The layout and semantics of the coefficient probability update record 3023 (Section I of the frame header) are straightforward. For each 3024 position in the coef_probs array there occurs a fixed-probability 3025 bool indicating whether or not the corresponding probability should 3026 be updated. If the bool is true, there follows a P(8) replacing that 3027 probability. Note that updates are cumulative, that is, a 3028 probability updated on one frame is in effect for all ensuing frames 3029 until the next key frame, or until the probability is explicitly 3030 updated by another frame. 3032 The algorithm to effect the foregoing is simple: 3034 ---- Begin code block -------------------------------------- 3036 int i = 0; do { 3037 int j = 0; do { 3038 int k = 0; do { 3039 int t = 0; do { 3041 if( read_bool( d, coef_update_probs [i] [j] [k] [t])) 3042 coef_probs [i] [j] [k] [t] = read_literal( d, 8); 3044 } while( ++t < num_dct_tokens - 1); 3045 } while( ++k < 3); 3046 } while( ++j < 8); 3047 } while( ++i < 4); 3049 ---- End code block ---------------------------------------- 3051 The (constant) update probabilities are as follows: 3053 ---- Begin code block -------------------------------------- 3055 const Prob coef_update_probs [4] [8] [3] [num_dct_tokens-1] = 3056 { 3057 { 3058 { 3059 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3060 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3061 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3062 }, 3063 { 3064 { 176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3065 { 223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3066 { 249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255} 3067 }, 3068 { 3069 { 255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3070 { 234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3071 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3072 }, 3073 { 3074 { 255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3075 { 239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3076 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3077 }, 3078 { 3079 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3080 { 251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3081 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3082 }, 3083 { 3084 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3085 { 251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3086 { 254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3087 }, 3088 { 3089 { 255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255}, 3090 { 250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255}, 3091 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3092 }, 3093 { 3094 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3095 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3096 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3097 } 3098 }, 3099 { 3100 { 3101 { 217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3102 { 225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255}, 3103 { 234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255} 3104 }, 3105 { 3106 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3107 { 223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3108 { 238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3109 }, 3110 { 3111 { 255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3112 { 249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3113 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3114 }, 3115 { 3116 { 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3117 { 247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3118 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3119 }, 3120 { 3121 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3122 { 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3123 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3124 }, 3125 { 3126 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3127 { 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3128 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3129 }, 3130 { 3131 { 255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3132 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3133 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3134 }, 3135 { 3136 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3137 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3138 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3139 } 3140 }, 3141 { 3142 { 3143 { 186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255}, 3144 { 234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255}, 3145 { 251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255} 3146 }, 3147 { 3148 { 255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3149 { 236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3150 { 251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255} 3151 }, 3152 { 3153 { 255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3154 { 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3155 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3156 }, 3157 { 3158 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3159 { 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3160 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3161 }, 3162 { 3163 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3164 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3165 { 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 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3180 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3181 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3182 } 3183 }, 3184 { 3185 { 3186 { 248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3187 { 250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255}, 3188 { 248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255} 3189 }, 3190 { 3191 { 255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3192 { 246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3193 { 252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255} 3194 }, 3195 { 3196 { 255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 3197 { 248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3198 { 253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255} 3199 }, 3200 { 3201 { 255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3202 { 245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3203 { 253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3204 }, 3205 { 3206 { 255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3207 { 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3208 { 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3209 }, 3210 { 3211 { 255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3212 { 249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 3213 { 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255} 3214 }, 3215 { 3216 { 255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 3217 { 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3218 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3219 }, 3220 { 3221 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3222 { 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 3223 { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 3224 } 3225 } 3226 }; 3228 ---- End code block ---------------------------------------- 3230 13.5. Default Token Probability Table 3232 The default token probabilities are as follows. 3234 ---- Begin code block -------------------------------------- 3236 const Prob default_coef_probs [4] [8] [3] [num_dct_tokens - 1] = 3237 { 3238 { 3239 { 3240 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3241 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3242 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3243 }, 3244 { 3245 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 3246 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 3247 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 3248 }, 3249 { 3250 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 3251 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 3252 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 3253 }, 3254 { 3255 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 3256 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 3257 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 3258 }, 3259 { 3260 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 3261 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 3262 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 3264 }, 3265 { 3266 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 3267 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 3268 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 3269 }, 3270 { 3271 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 3272 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 3273 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 3274 }, 3275 { 3276 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3277 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3278 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3279 } 3280 }, 3281 { 3282 { 3283 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 3284 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 3285 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 3286 }, 3287 { 3288 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 3289 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 3290 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 3291 }, 3292 { 3293 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 3294 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 3295 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 3296 }, 3297 { 3298 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 3299 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 3300 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 3301 }, 3302 { 3303 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 3304 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 3305 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 3306 }, 3307 { 3308 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 3309 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 3310 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 3311 }, 3312 { 3313 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 3314 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 3315 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 3316 }, 3317 { 3318 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 3319 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3320 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 3321 } 3322 }, 3323 { 3324 { 3325 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 3326 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 3327 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 3328 }, 3329 { 3330 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 3331 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 3332 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 3333 }, 3334 { 3335 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 3336 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 3337 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 3338 }, 3339 { 3340 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 3341 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 3342 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 3343 }, 3344 { 3345 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 3346 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 3347 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3348 }, 3349 { 3350 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3351 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3352 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3353 }, 3354 { 3355 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 3356 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 3357 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3358 }, 3359 { 3360 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3361 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 3362 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 3363 } 3364 }, 3365 { 3366 { 3367 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 3368 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 3369 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 3370 }, 3371 { 3372 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 3373 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 3374 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 3375 }, 3376 { 3377 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 3378 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 3379 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 3380 }, 3381 { 3382 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 3383 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 3384 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 3385 }, 3386 { 3387 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 3388 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 3389 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 3390 }, 3391 { 3392 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 3393 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 3394 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 3395 }, 3396 { 3397 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 3398 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 3399 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 3400 }, 3401 { 3402 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3403 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 3404 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 3405 } 3406 } 3407 }; 3408 ---- End code block ---------------------------------------- 3410 14. DCT and WHT Inversion and Macroblock Reconstruction 3412 14.1. Dequantization 3414 After decoding the DCTs/WHTs as described above, each (quantized) 3415 coefficient in each subblock is multiplied by one of six 3416 dequantization factors, the choice of factor depending on the plane 3417 (Y2, Y, or chroma) and position (DC = coefficient zero, AC = any 3418 other coefficient). If the current macroblock has overridden the 3419 quantization level (as described in Chapter 10) then the six factors 3420 are looked up from two dequantization tables with appropriate scaling 3421 and clamping using the single index supplied by the override. 3422 Otherwise, the frame-level dequantization factors (as described in 3423 Section 9.6) are used. In either case, the multiplies are computed 3424 and stored using 16-bit signed integers. 3426 The two dequantization tables, which may also be found in the 3427 reference decoder file dequant_data.h, are as follows. 3429 ---- Begin code block -------------------------------------- 3431 static const int dc_qlookup[QINDEX_RANGE] = 3432 { 3433 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 3434 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 3435 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 3436 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 3437 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 3438 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 3439 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 3440 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 3441 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, 126, 128, 130, 3442 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, 3443 }; 3445 static const int ac_qlookup[QINDEX_RANGE] = 3446 { 3447 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 3448 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3449 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 3450 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 3451 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 3452 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 3453 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 3454 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 3455 181, 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, 3456 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, 3457 }; 3459 ---- End code block ---------------------------------------- 3461 Lookup values from the above two tables are directly used the DC and 3462 AC coefficients in Y1 respectively. For Y2 and chroma, values from 3463 above tables undergo either a scaling process or clamping processing 3464 before the multiplies. Details to these scaling and clamping can be 3465 found related lookup functions in dixie.c. 3467 14.2. Inverse Transforms 3469 If the Y2 residue block exists (i.e., the macroblock luma mode is not 3470 SPLITMV or B_PRED), it is inverted first (using the inverse WHT) and 3471 the element of the result at row i, column j is used as the 0th 3472 coefficient of the Y subblock at position (i, j), that is, the Y 3473 subblock whose index is (i * 4) + j. As discussed in Chapter 13, if 3474 the luma mode is B_PRED or SPLITMV, the 0th Y coefficients are part 3475 of the residue signal for the subblocks themselves. 3477 In either case, the inverse transforms for the sixteen Y subblocks 3478 and eight chroma subblocks are computed next. All 24 of these 3479 inversions are independent of each other; their results may (at least 3480 conceptually) be stored in 24 separate 4x4 arrays. 3482 As is done by the reference decoder, an implementation may wish to 3483 represent the prediction and residue buffers as macroblock-sized 3484 arrays (that is, a 16x16 Y buffer and two 8x8 chroma buffers). 3485 Regarding the inverse DCT implementation given below, this requires a 3486 simple adjustment to the address calculation for the resulting 3487 residue pixels. 3489 14.3. Implementation of the WHT Inversion 3491 As previously discussed (see Chapters 2 and 13), for macroblocks 3492 encoded using prediction modes other than B_PRED and SPLITMV, the DC 3493 values derived from the DCT transform on the 16 Y blocks are 3494 collected to construct a 25th block of a macroblock (16 Y, 4 U, 4 V 3495 constitute the 24 blocks). This 25th block is transformed using a 3496 Walsh-Hadamard transform (WHT). 3498 The inputs to the inverse WHT (that is, the dequantized 3499 coefficients), the intermediate "horizontally detransformed" signal, 3500 and the completely detransformed residue signal are all stored as 3501 arrays of 16-bit signed integers. 3503 Following the tradition of specifying bitstream format using the 3504 decoding process, we specify the inverse WHT in the decoding process 3505 using the following C style source code: 3507 ---- Begin code block -------------------------------------- 3509 void vp8_short_inv_walsh4x4_c(short *input, short *output) 3510 { 3511 int i; 3512 int a1, b1, c1, d1; 3513 int a2, b2, c2, d2; 3514 short *ip = input; 3515 short *op = output; 3516 int temp1, temp2; 3518 for(i=0;i<4;i++) 3519 { 3520 a1 = ip[0] + ip[12]; 3521 b1 = ip[4] + ip[8]; 3522 c1 = ip[4] - ip[8]; 3523 d1 = ip[0] - ip[12]; 3524 op[0] = a1 + b1; 3525 op[4] = c1 + d1; 3526 op[8] = a1 - b1; 3527 op[12]= d1 - c1; 3528 ip++; 3529 op++; 3530 } 3531 ip = output; 3532 op = output; 3533 for(i=0;i<4;i++) 3534 { 3535 a1 = ip[0] + ip[3]; 3536 b1 = ip[1] + ip[2]; 3537 c1 = ip[1] - ip[2]; 3538 d1 = ip[0] - ip[3]; 3540 a2 = a1 + b1; 3541 b2 = c1 + d1; 3542 c2 = a1 - b1; 3543 d2 = d1 - c1; 3545 op[0] = (a2+3)>>3; 3546 op[1] = (b2+3)>>3; 3547 op[2] = (c2+3)>>3; 3548 op[3] = (d2+3)>>3; 3550 ip+=4; 3551 op+=4; 3552 } 3553 } 3555 ---- End code block ---------------------------------------- 3557 In the case that there is only one non-zero DC value in input, the 3558 inverse transform can be simplified to the following: 3560 ---- Begin code block -------------------------------------- 3562 void vp8_short_inv_walsh4x4_1_c(short *input, short *output) 3563 { 3564 int i; 3565 int a1; 3566 short *op=output; 3568 a1 = ((input[0] + 3)>>3); 3570 for(i=0;i<4;i++) 3571 { 3572 op[0] = a1; 3573 op[1] = a1; 3574 op[2] = a1; 3575 op[3] = a1; 3576 op+=4; 3577 } 3578 } 3580 ---- End code block ---------------------------------------- 3582 It should be noted, a conforming decoder should implement the inverse 3583 transform using exactly the same rounding to achieve bit-wise 3584 matching output to the output of the process specified by the above 3585 "C" source code. 3587 The reference decoder WHT inversion may be found in the files 3588 invtrans.c and idctllm.c. 3590 14.4. Implementation of the DCT Inversion 3592 All of the DCT inversions are computed in exactly the same way. In 3593 principle, VP8 uses a classical 2D inverse discrete cosine transform, 3594 implemented as two passes of 1-D inverse DCT. The 1-D inverse DCT 3595 was calculated using a similar algorithm to what was described in 3596 [Loeffler]. However, the paper only provided the 8-point and 16- 3597 point version of the algorithms, which was adapted by On2 to perform 3598 the 4-point 1-D DCT. 3600 Accurate calculation of 1-D DCT of the above algorithm requires 3601 infinite precision. VP8 of course can use only a finite-precision 3602 approximation. Also, the inverse DCT used by VP8 takes care of 3603 normalization of the standard unitary transform, that is, every 3604 dequantized coefficient has roughly double the size of the 3605 corresponding unitary coefficient. However, at all but the highest 3606 datarates, the discrepancy between transmitted and ideal coefficients 3607 is due almost entirely to (lossy) compression and not to errors 3608 induced by finite-precision arithmetic. 3610 The inputs to the inverse DCT (that is, the dequantized 3611 coefficients), the intermediate "horizontally detransformed" signal, 3612 and the completely detransformed residue signal are all stored as 3613 arrays of 16-bit signed integers. The details of the computation are 3614 as follows. 3616 It should also be noted that this implementation makes use of 16-bit 3617 fixed point version of two multiplication constants: 3619 sqrt(2) * cos (pi/8) 3621 sqrt(2) * sin (pi/8) 3623 Because the first constant is bigger than 1, to maintain the same 16- 3624 bit fixed point precision as the second one, we make use of the fact 3625 that 3627 x * a = x + x*(a-1) 3629 therefore 3631 x * sqrt(2) * cos (pi/8) = x + x * ( sqrt(2) * cos(pi/8)-1) 3633 ---- Begin code block -------------------------------------- 3635 /* IDCT implementation */ 3636 static const int cospi8sqrt2minus1=20091; 3637 static const int sinpi8sqrt2 =35468; 3638 void short_idct4x4llm_c(short *input, short *output, int pitch) 3639 { 3640 int i; 3641 int a1, b1, c1, d1; 3643 short *ip=input; 3644 short *op=output; 3645 int temp1, temp2; 3646 int shortpitch = pitch>>1; 3648 for(i=0;i<4;i++) 3649 { 3650 a1 = ip[0]+ip[8]; 3651 b1 = ip[0]-ip[8]; 3653 temp1 = (ip[4] * sinpi8sqrt2)>>16; 3654 temp2 = ip[12]+((ip[12] * cospi8sqrt2minus1)>>16); 3655 c1 = temp1 - temp2; 3657 temp1 = ip[4] + ((ip[4] * cospi8sqrt2minus1)>>16); 3658 temp2 = (ip[12] * sinpi8sqrt2)>>16; 3659 d1 = temp1 + temp2; 3661 op[shortpitch*0] = a1+d1; 3662 op[shortpitch*3] = a1-d1; 3663 op[shortpitch*1] = b1+c1; 3664 op[shortpitch*2] = b1-c1; 3666 ip++; 3667 op++; 3668 } 3669 ip = output; 3670 op = output; 3671 for(i=0;i<4;i++) 3672 { 3673 a1 = ip[0]+ip[2]; 3674 b1 = ip[0]-ip[2]; 3676 temp1 = (ip[1] * sinpi8sqrt2)>>16; 3677 temp2 = ip[3]+((ip[3] * cospi8sqrt2minus1)>>16); 3678 c1 = temp1 - temp2; 3680 temp1 = ip[1] + ((ip[1] * cospi8sqrt2minus1)>>16); 3681 temp2 = (ip[3] * sinpi8sqrt2)>>16; 3682 d1 = temp1 + temp2; 3684 op[0] = (a1+d1+4)>>3; 3685 op[3] = (a1-d1+4)>>3; 3686 op[1] = (b1+c1+4)>>3; 3687 op[2] = (b1-c1+4)>>3; 3689 ip+=shortpitch; 3690 op+=shortpitch; 3691 } 3692 } 3694 ---- End code block ---------------------------------------- 3696 The reference decoder DCT inversion may be found in the files 3697 invtrans.c and idctllm.c. 3699 14.5. Summation of Predictor and Residue 3701 Finally, the prediction and residue signals are summed to form the 3702 reconstructed macroblock, which, except for loop filtering (taken up 3703 next), completes the decoding process. 3705 The summing procedure is fairly straightforward, having only a couple 3706 of details. The prediction and residue buffers are both arrays of 3707 16-bit signed integers. Each individual (Y, U, and V pixel) result 3708 is calculated first as a 32-bit sum of the prediction and residue, 3709 and is then saturated to 8-bit unsigned range (using, say, the 3710 clamp255 function defined above) before being stored as an 8-bit 3711 unsigned pixel value. 3713 VP8 also supports a mode where the encoding of a bitstream guarantees 3714 all reconstructed pixel values between 0 and 255, compliant 3715 bitstreams of such requirements have the clamp_type bit in the frame 3716 header set to 1. In such case, the clamp255 is no longer required. 3718 The summation process is the same, regardless of the (intra or inter) 3719 mode of prediction in effect for the macroblock. The reference 3720 decoder implementation of reconstruction may be found in the file 3721 recon.c. 3723 15. Loop Filter 3725 Loop filtering is the last stage of frame reconstruction and the 3726 next-to-last stage of the decoding process. The loop filter is 3727 applied to the entire frame after the summation of predictor and 3728 residue described in Chapter 14. 3730 The purpose of the loop filter is to eliminate (or at least reduce) 3731 visually objectionable artifacts associated with the semi- 3732 independence of the coding of macroblocks and their constituent 3733 subblocks. 3735 As was discussed in Chapter 5, the loop filter is "integral" to 3736 decoding, in that the results of loop filtering are used in the 3737 prediction of subsequent frames. Consequently, a functional decoder 3738 implementation must perform loop filtering exactly as described here. 3739 This is in distinction to any postprocessing that may be applied only 3740 to the image immediately before display; such postprocessing is 3741 entirely at the option of the implementor (and/or user) and has no 3742 effect on decoding per se. 3744 The baseline frame level parameters controlling the loop filter are 3745 defined in the frame header (Chapter 9.4) along with a mechanism for 3746 adjustment based on a macroblock's prediction mode and/or reference 3747 frame. The first is a flag selecting the type of filter (normal or 3748 simple), the other two are numbers (loop_filter_level and 3749 sharpness_level) that adjust the strength or sensitivity of the 3750 filter. As described in Chapters 9.3 and 10, loop_filter_level may 3751 also be overridden on a per-macroblock basis using segmentation. 3753 Loop filtering is one of the more computationally-intensive aspects 3754 of VP8 decoding. This is the reason for the existence of the 3755 optional, less-demanding simple filter type. 3757 Note carefully that loop filtering must be skipped entirely if 3758 loop_filter_level at either the frame header level or macroblock 3759 override level is 0. In no case should the loop filter be run with a 3760 value of 0; it should instead be skipped. 3762 We begin by discussing the aspects of loop filtering that are 3763 independent of the controlling parameters and type of filter chosen. 3765 15.1. Filter Geometry and Overall Procedure 3767 The Y, U, and V planes are processed independently and identically. 3769 The loop filter acts on the edges between adjacent macroblocks and on 3770 the edges between adjacent subblocks of a macroblock. All such edges 3771 are horizontal or vertical. For each pixel position on an edge, a 3772 small number (two or three) of pixels adjacent to either side of the 3773 position are examined and possibly modified. The displacements of 3774 these pixels are at a right angle to the edge orientation, that is, 3775 for a horizontal edge, we treat the pixels immediately above and 3776 below the edge position, for a vertical edge, we treat the pixels 3777 immediately to the left and right of the edge. 3779 We call this collection of pixels associated to an edge position a 3780 segment; the length of a segment is 2, 4, 6, or 8. Excepting that 3781 the normal filter uses slightly different algorithms for, and that 3782 either filter may apply different control parameters to, the edges 3783 between macroblocks and those between subblocks, the treatment of 3784 edges is quite uniform: All segments straddling an edge are treated 3785 identically, there is no distinction between the treatment of 3786 horizontal and vertical edges, whether between macroblocks or between 3787 subblocks. 3789 As a consequence, adjacent subblock edges within a macroblock may be 3790 concatenated and processed in their entirety. There is a single 3791 8-pixel long vertical edge horizontally centered in each of the U and 3792 V blocks (the concatenation of upper and lower 4-pixel edges between 3793 chroma subblocks), and three 16-pixel long vertical edges at 3794 horizontal positions 1/4, 1/2, and 3/4 the width of the luma 3795 macroblock, each representing the concatenation of four 4-pixel sub- 3796 edges between pairs of Y subblocks. 3798 The macroblocks comprising the frame are processed in the usual 3799 raster-scan order. Each macroblock is "responsible for" the inter- 3800 macroblock edges immediately above and left of it (but not the edges 3801 below and right of it), as well as the edges between its subblocks. 3803 For each macroblock M, there are four filtering steps, which are, 3804 (almost) in order: 3806 1. If M is not on the leftmost column of macroblocks, filter across 3807 the left (vertical) inter-macroblock edge of M. 3809 2. Filter across the vertical subblock edges within M. 3811 3. If M is not on the topmost row of macroblocks, filter across the 3812 top (horizontal) inter-macroblock edge of M. 3814 4. Filter across the horizontal subblock edges within M. 3816 We write MY, MU, and MV for the planar constituents of M, that is, 3817 the 16x16 luma block, 8x8 U block, and 8x8 V block comprising M. 3819 In step 1, for each of the three blocks MY, MU, and MV, we filter 3820 each of the (16 luma or 8 chroma) segments straddling the column 3821 separating the block from the block immediately to the left of it, 3822 using the inter-macroblock filter and controls associated to the 3823 loop_filter_level and sharpness_level. 3825 In step 4, we filter across the (three luma and one each for U and V) 3826 vertical subblock edges described above, this time using the inter- 3827 subblock filter and controls. 3829 Step 2 and 4 are skipped for macroblocks that satisfy both of the 3830 following two conditions: 3832 1. Macroblock coding mode is neither B_PRED nor SPLTMV; and 3834 2. There is no DCT coefficient coded for the whole macroblock. 3836 For these macroblocks, loop filtering for edges between subblocks 3837 internal to a macroblock is effectively skipped. This skip strategy 3838 significantly reduces VP8 loop-filtering complexity. 3840 Edges between macroblocks and those between subblocks are treated 3841 with different control parameters (and, in the case of the normal 3842 filter, with different algorithms). Except for pixel addressing, 3843 there is no distinction between the treatment of vertical and 3844 horizontal edges. Luma edges are always 16 pixels long, chroma edges 3845 are always 8 pixels long, and the segments straddling an edge are 3846 treated identically; this of course facilitates vector processing. 3848 Because many pixels belong to segments straddling two or more edges, 3849 and so will be filtered more than once, the order in which edges are 3850 processed given above must be respected by any implementation. 3851 Within a single edge, however, the segments straddling that edge are 3852 disjoint and the order in which these segments are processed is 3853 immaterial. 3855 Before taking up the filtering algorithms themselves, we should 3856 emphasize a point already made: Even though the pixel segments 3857 associated to a macroblock are antecedent to the macroblock (that is, 3858 lie within the macroblock or in already-constructed macroblocks), a 3859 macroblock must not be filtered immediately after its 3860 "reconstruction" (described in Chapter 14). Rather, the loop filter 3861 applies after all the macroblocks have been "reconstructed" (i.e., 3862 had their predictor summed with their residue); correct decoding is 3863 predicated on the fact that already-constructed portions of the 3864 current frame referenced via intra-prediction (described in Chapter 3865 12) are not yet filtered. 3867 15.2. Simple Filter 3869 Having described the overall procedure of, and pixels affected by, 3870 the loop filter, we turn our attention to the treatment of individual 3871 segments straddling edges. We begin by describing the simple filter, 3872 which, as the reader might guess, is somewhat simpler than the normal 3873 filter. 3875 Note that the simple filter only applies to luma edges. Chroma edges 3876 are left unfiltered. 3878 Roughly speaking, the idea of loop filtering is, within limits, to 3879 reduce the difference between pixels straddling an edge. Differences 3880 in excess of a threshold (associated to the loop_filter_level) are 3881 assumed to be "natural" and are unmodified; differences below the 3882 threshold are assumed to be artifacts of quantization and the 3883 (partially) separate coding of blocks, and are reduced via the 3884 procedures described below. While the loop_filter_level is in 3885 principle arbitrary, the levels chosen by a VP8 compressor tend to be 3886 correlated to quantization levels. 3888 Most of the filtering arithmetic is done using 8-bit signed operands 3889 (having a range -128 to +127, inclusive), supplemented by 16-bit 3890 temporaries holding results of multiplies. 3892 Sums and other temporaries need to be "clamped" to a valid signed 3893 8-bit range: 3895 ---- Begin code block -------------------------------------- 3897 int8 c( int v) 3898 { 3899 return (int8) (v < -128 ? -128 : (v < 128 ? v : 127)); 3900 } 3902 ---- End code block ---------------------------------------- 3904 Since pixel values themselves are unsigned 8-bit numbers, we need to 3905 convert between signed and unsigned values: 3907 ---- Begin code block -------------------------------------- 3909 /* Convert pixel value (0 <= v <= 255) to an 8-bit signed 3910 number. */ 3911 int8 u2s( Pixel v) { return (int8) (v - 128);} 3913 /* Clamp, then convert signed number back to pixel value. */ 3914 Pixel s2u( int v) { return (Pixel) ( c(v) + 128);} 3916 ---- End code block ---------------------------------------- 3918 Filtering is often predicated on absolute-value thresholds. The 3919 following function is the equivalent of the standard library function 3920 abs, whose prototype is found in the standard header file stdlib.h. 3921 For us, the argument v is always the difference between two pixels 3922 and lies in the range -255 <= v <= +255. 3924 ---- Begin code block -------------------------------------- 3926 int abs( int v) { return v < 0? -v : v;} 3928 ---- End code block ---------------------------------------- 3930 An actual implementation would of course use inline functions or 3931 macros to accomplish these trivial procedures (which are used by both 3932 the normal and simple loop filters). An optimal implementation would 3933 probably express them in machine language, perhaps using SIMD vector 3934 instructions. On many SIMD processors, the saturation accomplished 3935 by the above clamping function is often folded into the arithmetic 3936 instructions themselves, obviating the explicit step taken here. 3938 To simplify the specification of relative pixel positions, we use the 3939 word before to mean "immediately above" (for a vertical segment 3940 straddling a horizontal edge) or "immediately to the left of" (for a 3941 horizontal segment straddling a vertical edge) and the word after to 3942 mean "immediately below" or "immediately to the right of". 3944 Given an edge, a segment, and a limit value, the simple loop filter 3945 computes a value based on the four pixels that straddle the edge (two 3946 either side). If that value is below a supplied limit, then, very 3947 roughly speaking, the two pixel values are brought closer to each 3948 other, "shaving off" something like a quarter of the difference. The 3949 same procedure is used for all segments straddling any type of edge, 3950 regardless of the nature (inter-macroblock, inter-subblock, luma, or 3951 chroma) of the edge; only the limit value depends on the edge-type. 3953 The exact procedure (for a single segment) is as follows; the 3954 subroutine common_adjust is used by both the simple filter presented 3955 here and the normal filters discussed in Section 15.3. 3957 ---- Begin code block -------------------------------------- 3959 int8 common_adjust( 3960 int use_outer_taps, /* filter is 2 or 4 taps wide */ 3961 const Pixel *P1, /* pixel before P0 */ 3962 Pixel *P0, /* pixel before edge */ 3963 Pixel *Q0, /* pixel after edge */ 3964 const Pixel *Q1 /* pixel after Q0 */ 3965 ) { 3966 cint8 p1 = u2s( *P1); /* retrieve and convert all 4 pixels */ 3967 cint8 p0 = u2s( *P0); 3968 cint8 q0 = u2s( *Q0); 3969 cint8 q1 = u2s( *Q1); 3971 /* Disregarding clamping, when "use_outer_taps" is false, 3972 "a" is 3*(q0-p0). Since we are about to divide "a" by 3973 8, in this case we end up multiplying the edge 3974 difference by 5/8. 3976 When "use_outer_taps" is true (as for the simple filter), 3977 "a" is p1 - 3*p0 + 3*q0 - q1, which can be thought of as 3978 a refinement of 2*(q0 - p0) and the adjustment is 3979 something like (q0 - p0)/4. */ 3981 int8 a = c( ( use_outer_taps? c(p1 - q1) : 0 ) + 3*(q0 - p0) ); 3983 /* b is used to balance the rounding of a/8 in the case where 3984 the "fractional" part "f" of a/8 is exactly 1/2. */ 3986 cint8 b = (c(a + 3)) >> 3; 3988 /* Divide a by 8, rounding up when f >= 1/2. 3989 Although not strictly part of the "C" language, 3990 the right-shift is assumed to propagate the sign bit. */ 3992 a = c( a + 4) >> 3; 3994 /* Subtract "a" from q0, "bringing it closer" to p0. */ 3996 *Q0 = s2u( q0 - a); 3998 /* Add "a" (with adjustment "b") to p0, "bringing it closer" 3999 to q0. 4001 The clamp of "a+b", while present in the reference decoder, 4002 is superfluous; we have -16 <= a <= 15 at this point. */ 4004 *P0 = s2u( p0 + b); 4006 return a; 4007 } 4009 ---- End code block ---------------------------------------- 4011 ---- Begin code block -------------------------------------- 4013 void simple_segment( 4014 uint8 edge_limit, /* do nothing if edge difference 4015 exceeds limit */ 4016 const Pixel *P1, /* pixel before P0 */ 4017 Pixel *P0, /* pixel before edge */ 4018 Pixel *Q0, /* pixel after edge */ 4019 const Pixel *Q1 /* pixel after Q0 */ 4020 ) { 4021 if( (abs(*P0 - *Q0)*2 + abs(*P1-*Q1)/2) <= edge_limit)) 4022 common_adjust( 1, P1, P0, Q0, Q1); /* use outer taps */ 4023 } 4025 ---- End code block ---------------------------------------- 4027 We make a couple of remarks about the rounding procedure above. When 4028 b is zero (that is, when the "fractional part" of a is not 1/2 ), we 4029 are (except for clamping) adding the same number to p0 as we are 4030 subtracting from q0. This preserves the average value of p0 and q0 4031 but the resulting difference between p0 and q0 is always even; in 4032 particular, the smallest non-zero gradation +-1 is not possible here. 4034 When b is one, the value we add to p0 (again except for clamping) is 4035 one less than the value we are subtracting from q0. In this case, 4036 the resulting difference is always odd (and the small gradation +-1 4037 is possible) but the average value is reduced by 1/2, yielding, for 4038 instance, a very slight darkening in the luma plane. (In the very 4039 unlikely event of appreciable darkening after a large number of 4040 interframes, a compressor would of course eventually compensate for 4041 this in the selection of predictor and/or residue.) 4043 The derivation of the edge_limit value used above, which depends on 4044 the loop_filter_level and sharpness_level, as well as the type of 4045 edge being processed, will be taken up after we describe the normal 4046 loop filtering algorithm below. 4048 15.3. Normal Filter 4050 The normal loop filter is a refinement of the simple loop filter; all 4051 of the general discussion above applies here as well. In particular, 4052 the functions c, u2s, s2u, abs, and common_adjust are used by both 4053 the normal and simple filters. 4055 As mentioned above, the normal algorithms for inter-macroblock and 4056 inter-subblock edges differ. Nonetheless, they have a great deal in 4057 common: They use similar threshold algorithms to disable the filter 4058 and to detect high internal edge variance (which influences the 4059 filtering algorithm). Both algorithms also use, at least 4060 conditionally, the simple filter adjustment procedure described 4061 above. 4063 The common thresholding algorithms are as follows. 4065 ---- Begin code block -------------------------------------- 4067 /* All functions take (among other things) a segment (of length 4068 at most 4 + 4 = 8) symmetrically straddling an edge. 4070 The pixel values (or pointers) are always given in order, 4071 from the "beforemost" to the "aftermost". So, for a 4072 horizontal edge (written "|"), an 8-pixel segment would be 4073 ordered p3 p2 p1 p0 | q0 q1 q2 q3. */ 4075 /* Filtering is disabled if the difference between any two 4076 adjacent "interior" pixels in the 8-pixel segment exceeds 4077 the relevant threshold (I). A more complex thresholding 4078 calculation is done for the group of four pixels that 4079 straddle the edge, in line with the calculation in 4080 simple_segment() above. */ 4082 int filter_yes( 4083 uint8 I, /* limit on interior differences */ 4084 uint8 E, /* limit at the edge */ 4086 cint8 p3, cint8 p2, cint8 p1, cint8 p0, /* pixels before 4087 edge */ 4088 cint8 q0, cint8 q1, cint8 q2, cint8 q3 /* pixels after 4089 edge */ 4090 ) { 4091 return (abs(p0 - q0)*2 + abs(p1-q1)/2) <= E 4092 && abs(p3 - p2) <= I && abs(p2 - p1) <= I && 4093 abs(p1 - p0) <= I 4094 && abs(q3 - q2) <= I && abs(q2 - q1) <= I && 4095 abs(q1 - q0) <= I; 4096 } 4098 ---- End code block ---------------------------------------- 4099 ---- Begin code block -------------------------------------- 4101 /* Filtering is altered if (at least) one of the differences 4102 on either side of the edge exceeds a threshold (we have 4103 "high edge variance"). */ 4105 int hev( 4106 uint8 threshold, 4107 cint8 p1, cint8 p0, /* pixels before edge */ 4108 cint8 q0, cint8 q1 /* pixels after edge */ 4109 ) { 4110 return abs(p1 - p0) > threshold || abs(q1 - q0) > threshold; 4111 } 4113 ---- End code block ---------------------------------------- 4115 The subblock filter is a variant of the simple filter. In fact, if 4116 we have high edge variance, the adjustment is exactly as for the 4117 simple filter. Otherwise, the simple adjustment (without outer taps) 4118 is applied and the two pixels one step in from the edge pixels are 4119 adjusted by roughly half the amount by which the two edge pixels are 4120 adjusted; since the edge adjustment here is essentially 3/8 the edge 4121 difference, the inner adjustment is approximately 3/16 the edge 4122 difference. 4124 ---- Begin code block -------------------------------------- 4126 void subblock_filter( 4127 uint8 hev_threshold, /* detect high edge variance */ 4128 uint8 interior_limit, /* possibly disable filter */ 4129 uint8 edge_limit, 4130 cint8 *P3, cint8 *P2, int8 *P1, int8 *P0, /* pixels before 4131 edge */ 4132 int8 *Q0, int8 *Q1, cint8 *Q2, cint8 *Q3 /* pixels after 4133 edge */ 4134 ) { 4135 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4136 p0 = u2s(*P0); 4137 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4138 q3 = u2s(*Q3); 4140 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4141 p0, p1, p2, p3)) 4142 { 4143 const int hv = hev( hev_threshold, p1, p0, q0, q1); 4145 cint8 a = ( common_adjust( hv, P1, P0, Q0, Q1) + 1) >> 1; 4147 if( !hv) { 4148 *Q1 = s2u( q1 - a); 4149 *P1 = s2u( p1 + a); 4150 } 4151 } 4152 } 4154 ---- End code block ---------------------------------------- 4156 The inter-macroblock filter has potentially wider scope. If the edge 4157 variance is high, it performs the simple adjustment (using the outer 4158 taps, just like the simple filter and the corresponding case of the 4159 normal subblock filter). If the edge variance is low, we begin with 4160 the same basic filter calculation and apply multiples of it to pixel 4161 pairs symmetric about the edge; the magnitude of adjustment decays as 4162 we move away from the edge and six of the pixels in the segment are 4163 affected. 4165 ---- Begin code block -------------------------------------- 4167 void MBfilter( 4168 uint8 hev_threshold, /* detect high edge variance */ 4169 uint8 interior_limit, /* possibly disable filter */ 4170 uint8 edge_limit, 4171 cint8 *P3, int8 *P2, int8 *P1, int8 *P0, /* pixels before 4172 edge */ 4173 int8 *Q0, int8 *Q1, int8 *Q2, cint8 *Q3 /* pixels after 4174 edge */ 4175 ) { 4176 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4177 p0 = u2s(*P0); 4178 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4179 q3 = u2s(*Q3); 4181 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4182 p0, p1, p2, p3)) 4183 { 4184 if( !hev( hev_threshold, p1, p0, q0, q1)) 4185 { 4186 /* Same as the initial calculation in "common_adjust", 4187 w is something like twice the edge difference */ 4189 const int8 w = c( c(p1 - q1) + 3*(q0 - p0) ); 4191 /* 9/64 is approximately 9/63 = 1/7 and 1<<7 = 128 = 4192 2*64. So this a, used to adjust the pixels adjacent 4193 to the edge, is something like 3/7 the edge 4194 difference. */ 4196 int8 a = c( (27*w + 63) >> 7); 4198 *Q0 = s2u( q0 - a); *P0 = s2u( p0 + a); 4200 /* Next two are adjusted by 2/7 the edge difference */ 4202 a = c( (18*w + 63) >> 7); 4204 *Q1 = s2u( q1 - a); *P1 = s2u( p1 + a); 4206 /* Last two are adjusted by 1/7 the edge difference */ 4208 a = c( (9*w + 63) >> 7); 4210 *Q2 = s2u( q2 - a); *P2 = s2u( p2 + a); 4212 } else /* if hev, do simple filter */ 4213 common_adjust( 1, P1, P0, Q0, Q1); /* using outer 4214 taps */ 4215 } 4217 } 4219 ---- End code block ---------------------------------------- 4221 15.4. Calculation of Control Parameters 4223 We conclude the discussion of loop filtering by showing how the 4224 thresholds supplied to the procedures above are derived from the two 4225 control parameters sharpness_level (an unsigned 3-bit number having 4226 maximum value 7) and loop_filter_level (an unsigned 6-bit number 4227 having maximum value 63). 4229 While the sharpness_level is constant over the frame, individual 4230 macroblocks may override the loop_filter_level with one of four 4231 possibilities supplied in the frame header (as described in Chapter 4232 10). 4234 Both the simple and normal filters disable filtering if a value 4235 derived from the four pixels that straddle the edge (2 either side) 4236 exceeds a threshold / limit value. 4238 ---- Begin code block -------------------------------------- 4240 /* Luma and Chroma use the same inter-macroblock edge limit */ 4241 uint8 mbedge_limit = ((loop_filter_level + 2) * 2) + 4242 interior_limit; 4244 /* Luma and Chroma use the same inter-subblock edge limit */ 4245 uint8 sub_bedge_limit = (loop_filter_level * 2) + interior_limit; 4247 ---- End code block ---------------------------------------- 4249 The remaining thresholds are used only by the normal filters. The 4250 filter-disabling interior difference limit is the same for all edges 4251 (luma, chroma, inter-subblock, inter-macroblock) and is given by the 4252 following. 4254 ---- Begin code block -------------------------------------- 4256 uint8 interior_limit = loop_filter_level; 4258 if( sharpness_level) 4259 { 4260 interior_limit >>= sharpness_level > 4 ? 2 : 1; 4261 if( interior_limit > 9 - sharpness_level) 4262 interior_limit = 9 - sharpness_level; 4263 } 4264 if( !interior_limit) 4265 interior_limit = 1; 4267 ---- End code block ---------------------------------------- 4268 Finally, we give the derivation of the high edge-variance threshold, 4269 which is also the same for all edge types. 4271 ---- Begin code block -------------------------------------- 4273 uint8 hev_threshold = 0; 4275 if( we_are_decoding_akey_frame) /* current frame is a key frame */ 4276 { 4277 if( loop_filter_level >= 40) 4278 hev_threshold = 2; 4279 else if( loop_filter_level >= 15) 4280 hev_threshold = 1; 4281 } 4282 else /* current frame is an interframe */ 4283 { 4284 if( loop_filter_level >= 40) 4285 hev_threshold = 3; 4286 else if( loop_filter_level >= 20) 4287 hev_threshold = 2; 4288 else if( loop_filter_level >= 15) 4289 hev_threshold = 1; 4290 } 4292 ---- End code block ---------------------------------------- 4294 16. Interframe Macroblock Prediction Records 4296 We describe the layout and semantics of the prediction records for 4297 macroblocks in an interframe. 4299 After the feature specification (which is described in Chapter 10 and 4300 is identical for intraframes and interframes), there comes a Bool( 4301 prob_intra), which indicates inter-prediction (i.e., prediction from 4302 prior frames) when true and intra-prediction (i.e., prediction from 4303 already-coded portions of the current frame) when false. The zero- 4304 probability prob_intra is set by field J of the frame header. 4306 16.1. Intra-Predicted Macroblocks 4308 For intra-prediction, the layout of the prediction data is 4309 essentially the same as the layout for key frames, although the 4310 contexts used by the decoding process are slightly different. 4312 As discussed in Chapter 8, the "outer" Y mode here uses a different 4313 tree from that used in key frames, repeated here for convenience. 4315 ---- Begin code block -------------------------------------- 4317 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 4318 { 4319 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 4320 4, 6, /* "1" subtree has 2 descendant subtrees */ 4321 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 4322 H_PRED = "101" */ 4323 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 4324 B_PRED = "111" */ 4325 }; 4327 ---- End code block ---------------------------------------- 4329 The probability table used to decode this tree is variable. As 4330 described in Section 9, it (along with the similarly-treated UV 4331 table) can be updated by field J of the frame header. Similar to the 4332 coefficient-decoding probabilities, such updates are cumulative and 4333 affect all ensuing frames until the next key frame or explicit 4334 update. The default probabilities for the Y and UV tables are 4336 ---- Begin code block -------------------------------------- 4338 Prob ymode_prob [num_ymodes - 1] = { 112, 86, 140, 37}; 4339 Prob uv_mode_prob [num_uv_modes - 1] = { 162, 101, 204}; 4341 ---- End code block ---------------------------------------- 4342 These defaults must be restored after detection of a key frame. 4344 Just as for key frames, if the Y mode is B_PRED, there next comes an 4345 encoding of the intra_bpred mode used by each of the sixteen Y 4346 subblocks. These encodings use the same tree as does that for key 4347 frames but, in place of the contexts used in key frames, use the 4348 single fixed probability table 4350 ---- Begin code block -------------------------------------- 4352 const Prob bmode_prob [num_intra_bmodes - 1] = { 4353 120, 90, 79, 133, 87, 85, 80, 111, 151 4354 }; 4356 ---- End code block ---------------------------------------- 4358 Last comes the chroma mode, again coded using the same tree as that 4359 for key frames, this time using the dynamic uv_mode_prob table 4360 described above. 4362 The calculation of the intra-prediction buffer is identical to that 4363 described for key frames in Chapter 12. 4365 16.2. Inter-Predicted Macroblocks 4367 Otherwise (when the above bool is true), we are using inter- 4368 prediction (which of course only happens for interframes), to which 4369 we now restrict our attention. 4371 The next datum is then another bool, B( prob_last), selecting the 4372 reference frame. If 0, the reference frame is previous frame (last 4373 frame); if 1, another bool (prob_gf) selects the reference frame 4374 between golden frame (0) or altref frame (1). The probabilities 4375 prob_last and prob_gf are set in field J of the frame header. 4377 Together with setting the reference frame, the purpose of inter-mode 4378 decoding is to set a motion vector for each of the sixteen Y 4379 subblocks of the current macroblock. This then defines the 4380 calculation of the inter-prediction buffer (detailed in Chapter 18). 4381 While the net effect of inter-mode decoding is straightforward, the 4382 implementation is somewhat complex; the (lossless) compression 4383 achieved by this method justifies the complexity. 4385 After the reference frame selector comes the mode (or motion vector 4386 reference) applied to the macroblock as a whole, coded using the 4387 following enumeration and tree. Setting mv_nearest = num_ymodes is a 4388 convenience that allows a single variable to unambiguously hold an 4389 inter- or intraprediction mode. 4391 ---- Begin code block -------------------------------------- 4393 typedef enum 4394 { 4395 mv_nearest = num_ymodes, /* use "nearest" motion vector 4396 for entire MB */ 4397 mv_near, /* use "next nearest" "" */ 4398 mv_zero, /* use zero "" */ 4399 mv_new, /* use explicit offset from 4400 implicit "" */ 4401 mv_split, /* use multiple motion vectors */ 4403 num_mv_refs = mv_split + 1 - mv_nearest 4404 } 4405 mv_ref; 4407 const tree_index mv_ref_tree [2 * (num_mv_refs - 1)] = 4408 { 4409 -mv_zero, 2, /* zero = "0" */ 4410 -mv_nearest, 4, /* nearest = "10" */ 4411 -mv_near, 6, /* near = "110" */ 4412 -mv_new, -mv_split /* new = "1110", split = "1111" */ 4413 }; 4415 ---- End code block ---------------------------------------- 4417 16.3. Mode and Motion Vector Contexts 4419 The probability table used to decode the mv_ref, along with three 4420 reference motion vectors used by the selected mode, is calculated via 4421 a survey of the already-decoded motion vectors in (up to) 3 nearby 4422 macroblocks. 4424 The algorithm generates a sorted list of distinct motion vectors 4425 adjacent to the search site. The best_mv is the vector with the 4426 highest score. The nearest_mv is the non-zero vector with the 4427 highest score. The near_mv is the non-zero vector with the next 4428 highest score. The number of motion vectors coded using the SPLITMV 4429 mode is scored using the same weighting and is returned with the 4430 scores of the best, nearest, and near vectors. 4432 The three adjacent macroblocks above, left, and above-left are 4433 considered in order. If the macroblock is intra-coded, no action is 4434 taken. Otherwise, the motion vector is compared to other previously 4435 found motion vectors to determine if it has been seen before, and if 4436 so contributes its weight to that vector, otherwise enters a new 4437 vector in the list. The above and left vectors have twice the weight 4438 of the above-left vector. 4440 As is the case with many contexts used by VP8, it is possible for 4441 macroblocks near the top or left edges of the image to reference 4442 blocks that are outside the visible image. VP8 provides a border of 4443 1 macroblock filled with 0x0 motion vectors left of the left edge, 4444 and a border filled with 0,0 motion vectors of 1 macroblocks above 4445 the top edge. 4447 Much of the process is more easily described in C than in English. 4448 The reference code for this can be found in findnearmv.c. The 4449 calculation of reference vectors, probability table, and, finally, 4450 the inter-prediction mode itself is implemented as follows. 4452 ---- Begin code block -------------------------------------- 4454 typedef union 4455 { 4456 unsigned int as_int; 4457 MV as_mv; 4458 } int_mv; /* facilitates rapid equality tests */ 4460 static void mv_bias(MODE_INFO *x,int refframe, int_mv *mvp, 4461 int * ref_frame_sign_bias ) 4462 { 4463 MV xmv; 4464 xmv = x->mbmi.mv.as_mv; 4465 if ( ref_frame_sign_bias[x->mbmi.ref_frame] != 4466 ref_frame_sign_bias[refframe] ) 4467 { 4468 xmv.row*=-1; 4469 xmv.col*=-1; 4470 } 4471 mvp->as_mv = xmv; 4472 } 4474 ---- End code block ---------------------------------------- 4475 ---- Begin code block -------------------------------------- 4477 void vp8_clamp_mv(MV *mv, const MACROBLOCKD *xd) 4478 { 4479 if ( mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN) ) 4480 mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN; 4481 else if ( mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN ) 4482 mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN; 4484 if ( mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN) ) 4485 mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN; 4486 else if ( mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN ) 4487 mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN; 4488 } 4490 ---- End code block ---------------------------------------- 4492 In the function vp8_find_near_mvs(), the vectors "nearest" and "near" 4493 are used by the corresponding modes. 4495 The vector best_mv is used as a base for explicitly-coded motion 4496 vectors. 4498 The first three entries in the return value cnt are (in order) 4499 weighted census values for "zero", "nearest", and "near" vectors. 4500 The final value indicates the extent to which SPLIT_MV was used by 4501 the neighboring macroblocks. The largest possible "weight" value in 4502 each case is 5. 4504 ---- Begin code block -------------------------------------- 4506 void vp8_find_near_mvs 4507 ( 4508 MACROBLOCKD *xd, 4509 const MODE_INFO *here, 4510 MV *nearest, 4511 MV *near, 4512 MV *best_mv, 4513 int cnt[4], 4514 int refframe, 4515 int * ref_frame_sign_bias 4516 ) 4517 { 4518 const MODE_INFO *above = here - xd->mode_info_stride; 4519 const MODE_INFO *left = here - 1; 4520 const MODE_INFO *aboveleft = above - 1; 4521 int_mv near_mvs[4]; 4522 int_mv *mv = near_mvs; 4523 int *cntx = cnt; 4524 enum {CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV}; 4526 /* Zero accumulators */ 4527 mv[0].as_int = mv[1].as_int = mv[2].as_int = 0; 4528 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 4530 /* Process above */ 4531 if(above->mbmi.ref_frame != INTRA_FRAME) { 4532 if(above->mbmi.mv.as_int) { 4533 (++mv)->as_int = above->mbmi.mv.as_int; 4534 mv_bias(above, refframe, mv, ref_frame_sign_bias); 4535 ++cntx; 4536 } 4537 *cntx += 2; 4538 } 4540 /* Process left */ 4541 if(left->mbmi.ref_frame != INTRA_FRAME) { 4542 if(left->mbmi.mv.as_int) { 4543 int_mv this_mv; 4545 this_mv.as_int = left->mbmi.mv.as_int; 4546 mv_bias(left, refframe, &this_mv, ref_frame_sign_bias); 4548 if(this_mv.as_int != mv->as_int) { 4549 (++mv)->as_int = this_mv.as_int; 4550 ++cntx; 4551 } 4552 *cntx += 2; 4553 } else 4554 cnt[CNT_ZERO] += 2; 4555 } 4557 /* Process above left */ 4558 if(aboveleft->mbmi.ref_frame != INTRA_FRAME) { 4559 if(aboveleft->mbmi.mv.as_int) { 4560 int_mv this_mv; 4562 this_mv.as_int = aboveleft->mbmi.mv.as_int; 4563 mv_bias(aboveleft, refframe, &this_mv, 4564 ref_frame_sign_bias); 4566 if(this_mv.as_int != mv->as_int) { 4567 (++mv)->as_int = this_mv.as_int; 4568 ++cntx; 4569 } 4570 *cntx += 1; 4572 } else 4573 cnt[CNT_ZERO] += 1; 4574 } 4576 /* If we have three distinct MV's ... */ 4577 if(cnt[CNT_SPLITMV]) { 4578 /* See if above-left MV can be merged with NEAREST */ 4579 if(mv->as_int == near_mvs[CNT_NEAREST].as_int) 4580 cnt[CNT_NEAREST] += 1; 4581 } 4583 cnt[CNT_SPLITMV] = ((above->mbmi.mode == SPLITMV) 4584 + (left->mbmi.mode == SPLITMV)) * 2 4585 + (aboveleft->mbmi.mode == SPLITMV); 4587 /* Swap near and nearest if necessary */ 4588 if(cnt[CNT_NEAR] > cnt[CNT_NEAREST]) { 4589 int tmp; 4590 tmp = cnt[CNT_NEAREST]; 4591 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 4592 cnt[CNT_NEAR] = tmp; 4593 tmp = near_mvs[CNT_NEAREST].as_int; 4594 near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int; 4595 near_mvs[CNT_NEAR].as_int = tmp; 4596 } 4598 /* Use near_mvs[0] to store the "best" MV */ 4599 if(cnt[CNT_NEAREST] >= cnt[CNT_ZERO]) 4600 near_mvs[CNT_ZERO] = near_mvs[CNT_NEAREST]; 4602 /* Set up return values */ 4603 *best_mv = near_mvs[0].as_mv; 4604 *nearest = near_mvs[CNT_NEAREST].as_mv; 4605 *near = near_mvs[CNT_NEAR].as_mv; 4607 vp8_clamp_mv(nearest, xd); 4608 vp8_clamp_mv(near, xd); 4609 vp8_clamp_mv(best_mv, xd); //TODO: move this up before 4610 the copy 4611 } 4613 ---- End code block ---------------------------------------- 4615 The mv_ref probability table (mv_ref_p) is then derived from the 4616 census as follows. 4618 ---- Begin code block -------------------------------------- 4620 const int vp8_mode_contexts[6][4] = 4621 { 4622 { 7, 1, 1, 143, }, 4623 { 14, 18, 14, 107, }, 4624 { 135, 64, 57, 68, }, 4625 { 60, 56, 128, 65, }, 4626 { 159, 134, 128, 34, }, 4627 { 234, 188, 128, 28, }, 4628 } 4630 ---- End code block ---------------------------------------- 4632 ---- Begin code block -------------------------------------- 4634 vp8_prob *vp8_mv_ref_probs(vp8_prob mv_ref_p[VP8_MVREFS-1], 4635 int cnt[4]) 4636 { 4637 mv_ref_p[0] = vp8_mode_contexts [cnt[0]] [0]; 4638 mv_ref_p[1] = vp8_mode_contexts [cnt[1]] [1]; 4639 mv_ref_p[2] = vp8_mode_contexts [cnt[2]] [2]; 4640 mv_ref_p[3] = vp8_mode_contexts [cnt[3]] [3]; 4641 return p; 4642 } 4644 ---- End code block ---------------------------------------- 4646 Once mv_ref_p is established, the mv_ref is decoded as usual. 4648 ---- Begin code block -------------------------------------- 4650 mvr = (mv_ref) treed_read( d, mv_ref_tree, mv_ref_p); 4652 ---- End code block ---------------------------------------- 4654 For the first four inter-coding modes, the same motion vector is used 4655 for all the Y subblocks. The first three modes use an implicit 4656 motion vector. 4658 +------------+------------------------------------------------------+ 4659 | Mode | Instruction | 4660 +------------+------------------------------------------------------+ 4661 | mv_nearest | Use the nearest vector returned by | 4662 | | vp8_find_near_mvs. | 4663 | | | 4664 | mv_near | Use the near vector returned by vp8_find_near_mvs. | 4665 | | | 4666 | mv_zero | Use a zero vector, that is, predict the current | 4667 | | macroblock from the corresponding macroblock in the | 4668 | | prediction frame. | 4669 | | | 4670 | NEWMV | This mode is followed by an explicitly-coded motion | 4671 | | vector (the format of which is described in the next | 4672 | | chapter) that is added (component-wise) to the | 4673 | | best_mv reference vector returned by find_near_mvs | 4674 | | and applied to all 16 subblocks. | 4675 +------------+------------------------------------------------------+ 4677 16.4. Split Prediction 4679 The remaining mode (SPLITMV) causes multiple vectors to be applied to 4680 the Y subblocks. It is immediately followed by a partition 4681 specification that determines how many vectors will be specified and 4682 how they will be assigned to the subblocks. The possible partitions, 4683 with indicated subdivisions and coding tree, are as follows. 4685 ---- Begin code block -------------------------------------- 4687 typedef enum 4688 { 4689 mv_top_bottom, /* two pieces {0...7} and {8...15} */ 4690 mv_left_right, /* {0,1,4,5,8,9,12,13} and 4691 {2,3,6,7,10,11,14,15} */ 4692 mv_quarters, /* {0,1,4,5}, {2,3,6,7}, {8,9,12,13}, 4693 {10,11,14,15} */ 4694 MV_16, /* every subblock gets its own vector 4695 {0} ... {15} */ 4697 mv_num_partitions 4698 } 4699 MVpartition; 4701 const tree_index mvpartition_tree [2 * (mvnum_partition - 1)] = 4702 { 4703 -MV_16, 2, /* MV_16 = "0" */ 4704 -mv_quarters, 4, /* mv_quarters = "10" */ 4705 -mv_top_bottom, -mv_left_right /* top_bottom = "110", 4706 left_right = "111" */ 4707 }; 4709 ---- End code block ---------------------------------------- 4711 The partition is decoded using a fixed, constant probability table: 4713 ---- Begin code block -------------------------------------- 4715 const Prob mvpartition_probs [mvnum_partition - 1] = 4716 { 110, 111, 150}; 4717 part = (MVpartition) treed_read( d, mvpartition_tree, 4718 mvpartition_probs); 4720 ---- End code block ---------------------------------------- 4722 After the partition come two (for mv_top_bottom or mv_left_right), 4723 four (for mv_quarters), or sixteen (for MV_16) subblock inter- 4724 prediction modes. These modes occur in the order indicated by the 4725 partition layouts (given as comments to the MVpartition enum) and are 4726 coded as follows. (As was done for the macroblock-level modes, we 4727 offset the mode enumeration so that a single variable may 4728 unambiguously hold either an intra- or inter-subblock mode.) 4730 Prior to decoding each subblock, a decoding tree context is chosen as 4731 illustrated in the code snippet below. The context is based on the 4732 immediate left and above subblock neighbors, and whether they are 4733 equal, are zero, or a combination of those. 4735 ---- Begin code block -------------------------------------- 4737 typedef enum 4738 { 4739 LEFT4x4 = num_intra_bmodes, /* use already-coded MV to 4740 my left */ 4741 ABOVE4x4, /* use already-coded MV above me */ 4742 ZERO4x4, /* use zero MV */ 4743 NEW4x4, /* explicit offset from "best" */ 4745 num_sub_mv_ref 4746 }; 4747 sub_mv_ref; 4749 const tree_index sub_mv_ref_tree [2 * (num_sub_mv_ref - 1)] = 4750 { 4751 -LEFT4X4, 2, /* LEFT = "0" */ 4752 -ABOVE4X4, 4, /* ABOVE = "10" */ 4753 -ZERO4X4, -NEW4X4 /* ZERO = "110", NEW = "111" */ 4754 }; 4756 /* Choose correct decoding tree context 4757 * Function parameters are left subblock neighbor MV and above 4758 * subblock neighbor MV */ 4759 int vp8_mvCont(MV *l, MV*a) 4760 { 4761 int lez = (l->row == 0 && l->col == 0); /* left neighbour 4762 is zero */ 4763 int aez = (a->row == 0 && a->col == 0); /* above neighbour 4764 is zero */ 4765 int lea = (l->row == a->row && l->col == a->col); /* left 4766 neighbour equals above neighbour */ 4768 if(lea && lez) 4769 return SUBMVREF_LEFT_ABOVE_ZED; /* =4 */ 4771 if(lea) 4772 return SUBMVREF_LEFT_ABOVE_SAME; /* =3 */ 4774 if(aez) 4775 return SUBMVREF_ABOVE_ZED; /* =2 */ 4777 if(lez) 4778 return SUBMVREF_LEFT_ZED; /* =1*/ 4780 return SUBMVREF_NORMAL; /* =0 */ 4782 } 4784 /* Constant probabilities and decoding procedure. */ 4786 const Prob sub_mv_ref_prob [5][num_sub_mv_ref - 1] = { 4787 { 147,136,18 }, 4788 { 106,145,1 }, 4789 { 179,121,1 }, 4790 { 223,1 ,34 }, 4791 { 208,1 ,1 } }; 4793 sub_ref = (sub_mv_ref) treed_read( d, sub_mv_ref_tree, 4794 sub_mv_ref_prob[context]); 4796 ---- End code block ---------------------------------------- 4798 The first two sub-prediction modes simply copy the already-coded 4799 motion vectors used by the blocks above and to-the-left of the 4800 subblock at the upper left corner of the current subset (i.e., 4801 collection of subblocks being predicted). These prediction blocks 4802 need not lie in the current macroblock and, if the current subset 4803 lies at the top or left edges of the frame, need not lie in the 4804 frame. In this latter case, their motion vectors are taken to be 4805 zero, as are subblock motion vectors within an intra-predicted 4806 macroblock. Also, to ensure the correctness of prediction within 4807 this macroblock, all subblocks lying in an already-decoded subset of 4808 the current macroblock must have their motion vectors set. 4810 ZERO4x4 uses a zero motion vector and predicts the current subset 4811 using the corresponding subset from the prediction frame. 4813 NEW4x4 is exactly like NEWMV except applied only to the current 4814 subset. It is followed by a 2-dimensional motion vector offset 4815 (described in the next chapter) that is added to the best vector 4816 returned by the earlier call to find_near_mvs to form the motion 4817 vector in effect for the subset. 4819 Parsing of both inter-prediction modes and motion vectors (described 4820 next) can be found in the reference decoder file modemv.c. 4822 17. Motion Vector Decoding 4824 As discussed above, motion vectors appear in two places in the VP8 4825 datastream: applied to whole macroblocks in NEWMV mode and applied to 4826 subsets of macroblocks in NEW4x4 mode. The format of the vectors is 4827 identical in both cases. 4829 Each vector has two pieces: A vertical component (row) followed by a 4830 horizontal component (column). The row and column use separate 4831 coding probabilities but are otherwise represented identically. 4833 17.1. Coding of Each Component 4835 Each component is a signed integer V representing a vertical or 4836 horizontal luma displacement of V quarter-pixels (and a chroma 4837 displacement of V eighth-pixels). The absolute value of V, if non- 4838 zero, is followed by a boolean sign. V may take any value between 4839 -1023 and +1023, inclusive. 4841 The absolute value A is coded in one of two different ways according 4842 to its size. For 0 <= A <= 7, A is tree-coded, and for 8 <= A <= 4843 1023, the bits in the binary expansion of A are coded using 4844 independent boolean probabilities. The coding of A begins with a 4845 bool specifying which range is in effect. 4847 Decoding a motion vector component then requires a 19-position 4848 probability table, whose offsets, along with the procedure used to 4849 decode components, are as follows: 4851 ---- Begin code block -------------------------------------- 4853 typedef enum 4854 { 4855 mvpis_short, /* short (<= 7) vs long (>= 8) */ 4856 MVPsign, /* sign for non-zero */ 4857 MVPshort, /* 8 short values = 7-position tree */ 4859 MVPbits = MVPshort + 7, /* 8 long value bits 4860 w/independent probs */ 4862 MVPcount = MVPbits + 10 /* 19 probabilities in total */ 4863 } 4864 MVPindices; 4866 typedef Prob MV_CONTEXT [MVPcount]; /* Decoding spec for 4867 a single component */ 4869 /* Tree used for small absolute values (has expected 4870 correspondence). */ 4872 const tree_index small_mvtree [2 * (8 - 1)] = 4873 { 4874 2, 8, /* "0" subtree, "1" subtree */ 4875 4, 6, /* "00" subtree", "01" subtree */ 4876 -0, -1, /* 0 = "000", 1 = "001" */ 4877 -2, -3, /* 2 = "010", 3 = "011" */ 4878 10, 12, /* "10" subtree, "11" subtree */ 4879 -4, -5, /* 4 = "100", 5 = "101" */ 4880 -6, -7 /* 6 = "110", 7 = "111" */ 4881 }; 4883 /* Read MV component at current decoder position, using 4884 supplied probs. */ 4886 int read_mvcomponent( bool_decoder *d, const MV_CONTEXT *mvc) 4887 { 4888 const Prob * const p = (const Prob *) mvc; 4889 int A = 0; 4891 if( read_bool( d, p [mvpis_short])) /* 8 <= A <= 1023 */ 4892 { 4893 /* Read bits 0, 1, 2 */ 4895 int i = 0; 4896 do { A += read_bool( d, p [MVPbits + i]) << i;} 4897 while( ++i < 3); 4899 /* Read bits 9, 8, 7, 6, 5, 4 */ 4901 i = 9; 4902 do { A += read_bool( d, p [MVPbits + i]) << i;} 4903 while( --i > 3); 4905 /* We know that A >= 8 because it is coded long, 4906 so if A <= 15, bit 3 is one and is not 4907 explicitly coded. */ 4909 if( !(A & 0xfff0) || read_bool( d, p [MVPbits + 3])) 4910 A += 8; 4911 } 4912 else /* 0 <= A <= 7 */ 4913 A = treed_read( d, small_mvtree, p + MVPshort); 4915 return A && read_bool( r, p [MVPsign]) ? -A : A; 4916 } 4917 ---- End code block ---------------------------------------- 4919 17.2. Probability Updates 4921 The decoder should maintain an array of two MV_CONTEXTs for decoding 4922 row and column components, respectively. These MV_CONTEXTs should be 4923 set to their defaults every key frame. Each individual probability 4924 may be updated every interframe (by field J of the frame header) 4925 using a constant table of update probabilities. Each optional update 4926 is of the form B? P(7), that is, a bool followed by a 7-bit 4927 probability specification if true. 4929 As with other dynamic probabilities used by VP8, the updates remain 4930 in effect until the next key frame or until replaced via another 4931 update. 4933 In detail, the probabilities should then be managed as follows. 4935 ---- Begin code block -------------------------------------- 4937 /* Never-changing table of update probabilities for each 4938 individual probability used in decoding motion vectors. */ 4940 const MV_CONTEXT vp8_mv_update_probs[2] = 4941 { 4942 { 4943 237, 4944 246, 4945 253, 253, 254, 254, 254, 254, 254, 4946 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 4947 }, 4948 { 4949 231, 4950 243, 4951 245, 253, 254, 254, 254, 254, 254, 4952 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 4953 } 4954 }; 4956 /* Default MV decoding probabilities. */ 4958 const MV_CONTEXT default_mv_context[2] = 4959 { 4960 { // row 4961 162, // is short 4962 128, // sign 4963 225, 146, 172, 147, 214, 39, 156, // short tree 4964 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 // long bits 4966 }, 4968 { // same for column 4969 164, // is short 4970 128, 4971 204, 170, 119, 235, 140, 230, 228, 4972 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 // long bits 4974 } 4975 }; 4977 /* Current MV decoding probabilities, set to above defaults 4978 every key frame. */ 4980 MV_CONTEXT mvc [2]; /* always row, then column */ 4982 /* Procedure for decoding a complete motion vector. */ 4984 typedef struct { int16 row, col;} MV; /* as in previous chapter */ 4986 MV read_mv( bool_decoder *d) 4987 { 4988 MV v; 4989 v.row = (int16) read_mvcomponent( d, mvc); 4990 v.col = (int16) read_mvcomponent( d, mvc + 1); 4991 return v; 4992 } 4994 /* Procedure for updating MV decoding probabilities, called 4995 every interframe with "d" at the appropriate position in 4996 the frame header. */ 4998 void update_mvcontexts( bool_decoder *d) 4999 { 5000 int i = 0; 5001 do { /* component = row, then column */ 5002 const Prob *up = mv_update_probs[i]; /* update probs 5003 for component */ 5004 Prob *p = mvc[i]; /* start decode tbl "" */ 5005 Prob * const pstop = p + MVPcount; /* end decode tbl "" */ 5006 do { 5007 if( read_bool( d, *up++)) /* update this position */ 5008 { 5009 const Prob x = read_literal( d, 7); 5011 *p = x? x<<1 : 1; 5012 } 5013 } while( ++p < pstop); /* next position */ 5015 } while( ++i < 2); /* next component */ 5016 } 5018 ---- End code block ---------------------------------------- 5020 This completes the description of the motion-vector decoding 5021 procedure and, with it, the procedure for decoding interframe 5022 macroblock prediction records. 5024 18. Interframe Prediction 5026 Given an inter-prediction specification for the current macroblock, 5027 that is, a reference frame together with a motion vector for each of 5028 the sixteen Y subblocks, we describe the calculation of the 5029 prediction buffer for the macroblock. Frame reconstruction is then 5030 completed via the previously-described processes of residue summation 5031 (Section 14) and loop filtering (Section 15). 5033 The management of inter-predicted subblocks and sub-pixel 5034 interpolation may be found in the reference decoder file predict.c. 5036 18.1. Bounds on and Adjustment of Motion Vectors 5038 Since each motion vector is differentially encoded from a neighboring 5039 block or macroblock and the only clamp is to ensure that the 5040 referenced motion vector represents a valid location inside a 5041 reference frame buffer, it is technically possible within the VP8 5042 format for a block or macroblock to have arbitrarily large motion 5043 vectors, up to the size of the input image plus the extended border 5044 areas. For practical reasons, VP8 imposes a motion vector size range 5045 limit of -4096 to 4095 full pixels, regardless of image size (VP8 5046 defines 14 raw bits for width and height; 16383x16383 is the maximum 5047 possible image size). Bitstream-compliant encoders and decoders 5048 shall enforce this limit. 5050 Because the motion vectors applied to the chroma subblocks have 1/8 5051 pixel resolution, the synthetic pixel calculation, outlined in 5052 Chapter 5 and detailed below, uses this resolution for the luma 5053 subblocks as well. In accordance, the stored luma motion vectors are 5054 all doubled, each component of each luma vector becoming an even 5055 integer in the range -2046 to +2046, inclusive. 5057 The vector applied to each chroma subblock is calculated by averaging 5058 the vectors for the 4 luma subblocks occupying the same visible area 5059 as the chroma subblock in the usual correspondence, that is, the 5060 vector for U and V block 0 is the average of the vectors for the Y 5061 subblocks { 0, 1, 4, 5}, chroma block 1 corresponds to Y blocks { 2, 5062 3, 6, 7}, chroma block 2 to Y blocks { 8, 9, 12, 13}, and chroma 5063 block 3 to Y blocks { 10, 11, 14, 15}. 5065 In detail, each of the two components of the vectors for each of the 5066 chroma subblocks is calculated from the corresponding luma vector 5067 components as follows: 5069 ---- Begin code block -------------------------------------- 5071 int avg( int c1, int c2, int c3, int c4) 5072 { 5073 int s = c1 + c2 + c3 + c4; 5075 /* The shift divides by 8 (not 4) because chroma pixels 5076 have twice the diameter of luma pixels. The handling 5077 of negative motion vector components is slightly 5078 cumbersome because, strictly speaking, right shifts 5079 of negative numbers are not well-defined in C. */ 5081 return s >= 0 ? (s + 4) >> 3 : -( (-s + 4) >> 3); 5082 } 5084 ---- End code block ---------------------------------------- 5086 Furthermore, if the version number in the frame tag specifies only 5087 full-pel chroma motion vectors, then the fractional parts of both 5088 components of the vector are truncated to zero, as illustrated in the 5089 following pseudo-code (assuming 3 bits of fraction for both luma and 5090 chroma vectors): 5092 ---- Begin code block -------------------------------------- 5094 x = x & (~7); 5095 y = y & (~7); 5097 ---- End code block ---------------------------------------- 5099 Earlier in this document we described the vp8_clamp_mv() function to 5100 limit "nearest" and "near" motion vector predictors inside specified 5101 margins within the frame boundaries. Additional clamping is 5102 performed for NEW_MV macroblocks, for which the final motion vector 5103 is clamped again after combining the "best" predictor and the 5104 differential vector decoded from the stream. 5106 However, the secondary clamping is not performed for SPLIT_MV 5107 macroblocks, meaning that any subblock's motion vector within the 5108 SPLIT_MV macroblock may point outside the clamping zone. These non- 5109 clamped vectors are also used when determining the decoding tree 5110 context for subsequent subblocks' modes in the vp8_mvCont() function. 5112 18.2. Prediction Subblocks 5114 The prediction calculation for each subblock is then as follows. 5115 Temporarily disregarding the fractional part of the motion vector 5116 (that is, rounding "up" or "left" by right-shifting each component 3 5117 bits with sign propagation) and adding the origin (upper left 5118 position) of the (16x16 luma or 8x8 chroma) current macroblock gives 5119 us an origin in the Y, U, or V plane of the predictor frame (either 5120 the golden frame or previous frame). 5122 Considering that origin to be the upper left corner of a (luma or 5123 chroma) macroblock, we need to specify the relative positions of the 5124 pixels associated to that subblock, that is, any pixels that might be 5125 involved in the sub-pixel interpolation processes for the subblock. 5127 18.3. Sub-pixel Interpolation 5129 The sub-pixel interpolation is effected via two one-dimensional 5130 convolutions. These convolutions may be thought of as operating on a 5131 two-dimensional array of pixels whose origin is the subblock origin, 5132 that is the origin of the prediction macroblock described above plus 5133 the offset to the subblock. Because motion vectors are arbitrary, so 5134 are these "prediction subblock origins". 5136 The integer part of the motion vector is subsumed in the origin of 5137 the prediction subblock, the 16 (synthetic) pixels we need to 5138 construct are given by 16 offsets from the origin. The integer part 5139 of each of these offsets is the offset of the corresponding pixel 5140 from the subblock origin (using the vertical stride). To these 5141 integer parts is added a constant fractional part, which is simply 5142 the difference between the actual motion vector and its integer 5143 truncation used to calculate the origins of the prediction macroblock 5144 and subblock. Each component of this fractional part is an integer 5145 between 0 and 7, representing a forward displacement in eighths of a 5146 pixel. 5148 It is these fractional displacements that determine the filtering 5149 process. If they both happen to be zero (that is, we had a "whole 5150 pixel" motion vector), the prediction subblock is simply copied into 5151 the corresponding piece of the current macroblock's prediction 5152 buffer. As discussed in Chapter 14, the layout of the macroblock's 5153 prediction buffer can depend on the specifics of the reconstruction 5154 implementation chosen. Of course, the vertical displacement between 5155 lines of the prediction subblock is given by the stride, as are all 5156 vertical displacements used here. 5158 Otherwise, at least one of the fractional displacements is non-zero. 5159 We then synthesize the missing pixels via a horizontal, followed by a 5160 vertical, one-dimensional interpolation. 5162 The two interpolations are essentially identical. Each uses an (at 5163 most) six-tap filter (the choice of which of course depends on the 5164 one-dimensional offset). Thus, every calculated pixel references at 5165 most three pixels before (above or to-the-left of) it and at most 5166 three pixels after (below or to-the-right of) it. The horizontal 5167 interpolation must calculate two extra rows above and three extra 5168 rows below the 4x4 block, to provide enough samples for the vertical 5169 interpolation to proceed. 5171 Depending on the reconstruction filter type given in the field 5172 Version Number in the frame tag, either a bicubic or a bilinear tap 5173 set is used. 5175 The exact implementation of subsampling is as follows. 5177 ---- Begin code block -------------------------------------- 5179 /* Filter taps taken to 7-bit precision. 5180 Because DC is always passed, taps always sum to 128. */ 5182 const int BilinearFilters[8][6] = 5183 { 5184 { 0, 0, 128, 0, 0, 0 }, 5185 { 0, 0, 112, 16, 0, 0 }, 5186 { 0, 0, 96, 32, 0, 0 }, 5187 { 0, 0, 80, 48, 0, 0 }, 5188 { 0, 0, 64, 64, 0, 0 }, 5189 { 0, 0, 48, 80, 0, 0 }, 5190 { 0, 0, 32, 96, 0, 0 }, 5191 { 0, 0, 16, 112, 0, 0 } 5192 }; 5194 const int filters [8] [6] = { /* indexed by displacement */ 5195 { 0, 0, 128, 0, 0, 0 }, /* degenerate whole-pixel */ 5196 { 0, -6, 123, 12, -1, 0 }, /* 1/8 */ 5197 { 2, -11, 108, 36, -8, 1 }, /* 1/4 */ 5198 { 0, -9, 93, 50, -6, 0 }, /* 3/8 */ 5199 { 3, -16, 77, 77, -16, 3 }, /* 1/2 is symmetric */ 5200 { 0, -6, 50, 93, -9, 0 }, /* 5/8 = reverse of 3/8 */ 5201 { 1, -8, 36, 108, -11, 2 }, /* 3/4 = reverse of 1/4 */ 5202 { 0, -1, 12, 123, -6, 0 } /* 7/8 = reverse of 1/8 */ 5203 }; 5205 /* One-dimensional synthesis of a single sample. 5206 Filter is determined by fractional displacement */ 5208 Pixel interp( 5209 const int fil[6], /* filter to apply */ 5210 const Pixel *p, /* origin (rounded "before") in 5211 prediction area */ 5213 const int s /* size of one forward step "" */ 5214 ) { 5215 int32 a = 0; 5216 int i = 0; 5217 p -= s + s; /* move back two positions */ 5219 do { 5220 a += *p * fil[i]; 5221 p += s; 5222 } while( ++i < 6); 5224 return clamp255( (a + 64) >> 7); /* round to nearest 5225 8-bit value */ 5226 } 5228 /* First do horizontal interpolation, producing intermediate 5229 buffer. */ 5231 void Hinterp( 5232 Pixel temp[9][4], /* 9 rows of 4 (intermediate) 5233 destination values */ 5234 const Pixel *p, /* subblock origin in prediction 5235 frame */ 5236 int s, /* vertical stride to be used in 5237 prediction frame */ 5238 uint hfrac, /* 0 <= horizontal displacement <= 7 */ 5239 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5240 ) { 5241 const int * const fil = bicubic ? filters [hfrac] : 5242 BilinearFilters[hfrac]; 5244 int r = 0; do /* for each row */ 5245 { 5246 int c = 0; do /* for each destination sample */ 5247 { 5248 /* Pixel separation = one horizontal step = 1 */ 5250 temp[r][c] = interp( fil, p + c, 1); 5251 } 5252 while( ++c < 4); 5253 } 5254 while( p += s, ++r < 9); /* advance p to next row */ 5255 } 5257 /* Finish with vertical interpolation, producing final results. 5258 Input array "temp" is of course that computed above. */ 5260 void Vinterp( 5261 Pixel final[4][4], /* 4 rows of 4 (final) destination values */ 5262 const Pixel temp[9][4], 5263 uint vfrac, /* 0 <= vertical displacement <= 7 */ 5264 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5265 ) { 5266 const int * const fil = bicubic ? filters [vfrac] : 5267 BilinearFilters[vfrac]; 5269 int r = 0; do /* for each row */ 5270 { 5271 int c = 0; do /* for each destination sample */ 5272 { 5273 /* Pixel separation = one vertical step = width 5274 of array = 4 */ 5276 final[r][c] = interp( fil, temp[r] + c, 4); 5277 } 5278 while( ++c < 4); 5279 } 5280 while( ++r < 4); 5281 } 5283 ---- End code block ---------------------------------------- 5285 18.4. Filter Properties 5287 We discuss briefly the rationale behind the choice of filters. Our 5288 approach is necessarily cursory; a genuinely accurate discussion 5289 would require a couple of books. Readers unfamiliar with signal 5290 processing may or may not wish to skip this. 5292 All digital signals are of course sampled in some fashion. The case 5293 where the inter-sample spacing (say in time for audio samples, or 5294 space for pixels) is uniform, that is, the same at all positions, is 5295 particularly common and amenable to analysis. Many aspects of the 5296 treatment of such signals are best-understood in the frequency domain 5297 via Fourier Analysis, particularly those aspects of the signal that 5298 are not changed by shifts in position, especially when those 5299 positional shifts are not given by a whole number of samples. 5301 Non-integral translates of a sampled signal are a textbook example of 5302 the foregoing. In our case of non-integral motion vectors, we wish 5303 to say what the underlying image "really is" at these pixels we don't 5304 have values for but feel that it makes sense to talk about. The 5305 correctness of this feeling is predicated on the underlying signal 5306 being band-limited, that is, not containing any energy in spatial 5307 frequencies that cannot be faithfully rendered at the pixel 5308 resolution at our disposal. In one dimension, this range of "OK" 5309 frequencies is called the Nyquist band; in our two-dimensional case 5310 of integer-grid samples, this range might be termed a Nyquist 5311 rectangle. The finer the grid, the more we know about the image, and 5312 the wider the Nyquist rectangle. 5314 It turns out that, for such band-limited signals, there is indeed an 5315 exact mathematical formula to produce the correct sample value at an 5316 arbitrary point. Unfortunately, this calculation requires the 5317 consideration of every single sample in the image, as well as needing 5318 to operate at infinite precision. Also, strictly speaking, all band- 5319 limited signals have infinite spatial (or temporal) extent, so 5320 everything we are discussing is really some sort of approximation. 5322 It is true that the theoretically correct subsampling procedure, as 5323 well as any approximation thereof, is always given by a translation- 5324 invariant weighted sum (or filter) similar to that used by VP8. It 5325 is also true that the reconstruction error made by such a filter can 5326 be simply represented as a multiplier in the frequency domain, that 5327 is, such filters simply multiply the Fourier transform of any signal 5328 to which they are applied by a fixed function associated to the 5329 filter. This fixed function is usually called the frequency response 5330 (or transfer function); the ideal subsampling filter has a frequency 5331 response equal to one in the Nyquist rectangle and zero everywhere 5332 else. 5334 Another basic fact about approximations to "truly correct" 5335 subsampling is that, the wider the subrectangle (within the Nyquist 5336 rectangle) of spatial frequencies one wishes to "pass" (that is, 5337 correctly render) or, put more accurately, the closer one wishes to 5338 approximate the ideal transfer function, the more samples of the 5339 original signal must be considered by the subsampling, and the wider 5340 the calculation precision necessitated. 5342 The filters chosen by VP8 were chosen, within the constraints of 4 or 5343 6 taps and 7-bit precision, to do the best possible job of handling 5344 the low spatial frequencies near the zeroth DC frequency along with 5345 introducing no resonances (places where the absolute value of the 5346 frequency response exceeds one). 5348 The justification for the foregoing has two parts. First, resonances 5349 can produce extremely objectionable visible artifacts when, as often 5350 happens in actual compressed video streams, filters are applied 5351 repeatedly. Second, the vast majority of energy in real-world images 5352 lies near DC and not at the high-end. 5354 To get slightly more specific, the filters chosen by VP8 are the best 5355 resonance-free 4- or 6-tap filters possible, where "best" describes 5356 the frequency response near the origin: the response at 0 is required 5357 to be 1 and the graph of the response at 0 is as flat as possible. 5359 To provide an intuitively more obvious point of reference, the "best" 5360 2-tap filter is given by simple linear interpolation between the 5361 surrounding actual pixels. 5363 Finally, it should be noted that, because of the way motion vectors 5364 are calculated, the (shorter) 4-tap filters (used for odd fractional 5365 displacements) are applied in the chroma plane only. Human color 5366 perception is notoriously poor, especially where higher spatial 5367 frequencies are involved. The shorter filters are easier to 5368 understand mathematically, and the difference between them and a 5369 theoretically slightly better 6-tap filter is negligible where chroma 5370 is concerned. 5372 19. Annex A: Bitstream Syntax 5374 This annex presents the bitstream syntax in a tabular form. All the 5375 information elements have been introduced and explained in the 5376 previous chapters but are collected here for a quick reference. Each 5377 syntax element is shortly described after the tabular representation 5378 along with a reference to the corresponding paragraph in the main 5379 document. The meaning of each syntax element value is not repeated 5380 here. 5382 The top-level hierarchy of the bitstream is introduced in Section 4. 5384 Definition of syntax element coding types can be found in Section 8. 5385 The types used in the representation in this annex are: 5387 o f(n), n-bit value from stream (n successive bits, not boolean 5388 encoded) 5390 o L(n), n-bit number encoded as n booleans (with equal probability 5391 of being 0 or 1) 5393 o B(p), bool with probability p of being 0 5395 o T, tree-encoded value 5397 19.1. Uncompressed Data Chunk 5399 +----------------------+-------+ 5400 | Frame Tag | Type | 5401 +----------------------+-------+ 5402 | frame_tag | f(24) | 5403 | | | 5404 | if (key_frame) { | | 5405 | | | 5406 | start_code | f(24) | 5407 | | | 5408 | horizontal_size_code | f(16) | 5409 | | | 5410 | vertical_size_code | f(16) | 5411 | | | 5412 | } | | 5413 +----------------------+-------+ 5415 The 3-byte frame tag can be parsed as follows: 5417 ---- Begin code block -------------------------------------- 5419 unsigned char *c = pbi->Source; 5420 unsigned int tmp; 5422 tmp = (c[2] << 16) | (c[1] << 8) | c[0]; 5424 key_frame = tmp & 0x1; 5425 version = (tmp >> 1) & 0x7; 5426 show_frame = (tmp >> 4) & 0x1; 5427 first_part_size = (tmp >> 5) & 0x7FFFF; 5429 ---- End code block ---------------------------------------- 5431 Where: 5433 o key_frame indicates if the current frame is a key frame or not. 5435 o version determines the bitstream version. 5437 o show_frame indicates if the current frame is meant to be displayed 5438 or not. 5440 o first_part_size determines the size of the first partition 5441 (control partition), excluding the uncompressed data chunk. 5443 The start_code is a constant 3-byte pattern having value 0x9d012a. 5444 The latter part of the uncompressed chunk (after the start_code) can 5445 be parsed as follows: 5447 ---- Begin code block -------------------------------------- 5449 unsigned char *c = pbi->Source + 6; 5450 unsigned int tmp; 5452 tmp = (c[1] << 8) | c[0]; 5454 width = tmp & 0x3FFF; 5455 horizontal_scale = tmp >> 14; 5457 tmp = (c[3] << 8) | c[2]; 5459 height = tmp & 0x3FFF; 5460 vertical_scale = tmp >> 14; 5462 ---- End code block ---------------------------------------- 5464 19.2. Frame Header 5466 +-------------------------------+------+ 5467 | Frame Header | Type | 5468 +-------------------------------+------+ 5469 | if (key_frame) { | | 5470 | | | 5471 | color_space | L(1) | 5472 | | | 5473 | clamping_type | L(1) | 5474 | | | 5475 | } | | 5476 | | | 5477 | segmentation_enabled | L(1) | 5478 | | | 5479 | if (segmentation_enabled) { | | 5480 | | | 5481 | update_segmentation() | | 5482 | | | 5483 | } | | 5484 | | | 5485 | filter_type | L(1) | 5486 | | | 5487 | loop_filter_level | L(6) | 5488 | | | 5489 | sharpness_level | L(3) | 5490 | | | 5491 | mb_lf_adjustments() | | 5492 | | | 5493 | log2_nbr_of_dct_partitions | L(2) | 5494 | | | 5495 | quant_indices() | | 5496 | | | 5497 | if (key_frame) { | | 5498 | | | 5499 | refresh_entropy_probs | L(1) | 5500 | | | 5501 | } else { | | 5502 | | | 5503 | refresh_golden_frame | L(1) | 5504 | | | 5505 | refresh_alternate_frame | L(1) | 5506 | | | 5507 | if (!refresh_golden_frame) | | 5508 | | | 5509 | copy_buffer_to_golden | L(2) | 5510 | | | 5511 | if (!refresh_alternate_frame) | | 5512 | copy_buffer_to_alternate | L(2) | 5513 | | | 5514 | sign_bias_golden | L(1) | 5515 | | | 5516 | sign_bias_alternate | L(1) | 5517 | | | 5518 | refresh_entropy_probs | L(1) | 5519 | | | 5520 | refresh_last | L(1) | 5521 | | | 5522 | } | | 5523 | | | 5524 | token_prob_update() | | 5525 | | | 5526 | mb_no_coeff_skip | L(1) | 5527 +-------------------------------+------+ 5529 +--------------------------------------+------+ 5530 | Frame Header | Type | 5531 +--------------------------------------+------+ 5532 | prob_skip_false | L(8) | 5533 | | | 5534 | if (!key_frame) { | | 5535 | | | 5536 | prob_intra | L(8) | 5537 | | | 5538 | prob_last | L(8) | 5539 | | | 5540 | prob_golden | L(8) | 5541 | | | 5542 | intra_16x16_prob_update_flag | L(1) | 5543 | | | 5544 | if (intra_16x16_prob_update_flag) { | | 5545 | | | 5546 | for (i = 0; i < 4; i++) | | 5547 | | | 5548 | intra_16x16_prob | L(8) | 5549 | | | 5550 | } | | 5551 | | | 5552 | intra_chroma prob_update_flag | L(1) | 5553 | | | 5554 | if (intra_chroma_prob_update_flag) { | | 5555 | | | 5556 | for (i = 0; i < 3; i++) | | 5557 | | | 5558 | intra_chroma_prob | L(8) | 5559 | | | 5560 | } | | 5561 | | | 5562 | mv_prob_update() | | 5563 | | | 5564 | } | | 5565 +--------------------------------------+------+ 5567 o color_space defines the YUV color space of the sequence 5568 (Section 9.2) 5570 o clamping_type specifies if the decoder is required to clamp the 5571 reconstructed pixel values (Section 9.2) 5573 o segmentation_enabled enables the segmentation feature for the 5574 current frame (Section 9.3) 5576 o filter_type determines whether the normal or the simple loop 5577 filter is used (Section 9.4, Section 15) 5579 o loop_filter_level controls the deblocking filter (Section 9.4, 5580 Section 15) 5582 o sharpness_level controls the deblocking filter (Section 9.4, 5583 Section 15) 5585 o log2_nbr_of_dct_partitions determines the number of separate 5586 partitions containing the DCT coefficients of the macroblocks 5587 (Section 9.5) 5589 o refresh_entropy_probs determines whether updated token 5590 probabilities are used only for this frame or until further update 5592 o refresh_golden_frame determines if the current decoded frame 5593 refreshes the golden frame (Section 9.7) 5595 o refresh_alternate_frame determines if the current decoded frame 5596 refreshes the alternate reference frame (Section 9.7) 5598 o copy_buffer_to_golden determines if the golden reference is 5599 replaced by another reference (Section 9.7) 5601 o copy_buffer_to_alternate determines if the alternate reference is 5602 replaced by another reference (Section 9.7) 5604 o sign_bias_golden controls the sign of motion vectors when the 5605 golden frame is referenced (Section 9.7) 5607 o sign_bias_alternate controls the sign of motion vectors when the 5608 alternate frame is referenced (Section 9.7) 5610 o refresh_last determines if the current decoded frame refreshes the 5611 last frame reference buffer (Section 9.8) 5613 o mb_no_coeff_skip enables or disables the skipping of macroblocks 5614 containing no non-zero coefficients (Section 9.10) 5616 o prob_skip_false the probability that the macroblock is not skipped 5617 (flag indicating skipped macroblock is false) (Section 9.10) 5619 o prob_intra the probability of an intra macroblock (Section 9.10) 5621 o prob_last the probability that the last reference frame is used 5622 for inter prediction (Section 9.10) 5624 o prob_golden the probability that the golden reference frame is 5625 used for inter prediction (Section 9.10) 5627 o intra_16x16_prob_update_flag indicates if the branch probabilies 5628 used in the decoding of luma intra prediction mode are updated 5629 (Section 9.10) 5631 o intra_16x16_prob the branch probabilities of the luma intra 5632 prediction mode decoding tree 5634 o intra_chroma_prob_update_flag indicates if the branch probabilies 5635 used in the decoding of chroma intra prediction mode are updated 5636 (Section 9.10) 5638 o intra_chroma_prob the branch probabilities of the chroma intra 5639 prediction mode decoding tree 5641 +------------------------------------+------+ 5642 | update_segmentation() | Type | 5643 +------------------------------------+------+ 5644 | update_mb_segmentation_map | L(1) | 5645 | | | 5646 | update_segment_feature_data | L(1) | 5647 | | | 5648 | if (update_segment_feature_data) { | | 5649 | | | 5650 | segment_feature_mode | L(1) | 5651 | | | 5652 | for (i = 0; i < 4; i++) { | | 5653 | | | 5654 | quantizer_update | L(1) | 5655 | | | 5656 | if (quantizer_update) { | | 5657 | | | 5658 | quantizer_update_value | L(7) | 5659 | | | 5660 | quantizer_update_sign | L(1) | 5661 | | | 5662 | } | | 5663 | | | 5664 | } | | 5665 | | | 5666 | for (i = 0; i < 4; i++) { | | 5667 | | | 5668 | loop_filter_update | L(1) | 5669 | | | 5670 | if (loop_filter_update) { | | 5671 | | | 5672 | lf_update_value | L(6) | 5673 | | | 5674 | lf_update_sign | L(1) | 5675 | | | 5676 | } | | 5677 | | | 5678 | } | | 5679 | | | 5680 | } | | 5681 | | | 5682 | if (update_mb_segmentation_map) { | | 5683 | | | 5684 | for (i = 0; i < 3; i++) { | | 5685 | | | 5686 | segment_prob_update | L(1) | 5687 | | | 5688 | if (segment_prob_update) { | | 5689 | | | 5690 | segment_prob | L(8) | 5691 | | | 5692 | } | | 5693 | | | 5694 | } | | 5695 | | | 5696 | } | | 5697 +------------------------------------+------+ 5699 o update_mb_segmentation_map determines if the MB segmentation map 5700 is updated in the current frame (Section 9.3) 5702 o update_segment_feature_data indicates if the segment feature data 5703 is updated in the current frame (Section 9.3) 5705 o segment_feature_mode indicates the feature data update mode, 0 for 5706 delta and 1 for the absolute value (Section 9.3) 5708 o quantizer_update indicates if the quantizer value is updated for 5709 the i^(th) segment (Section 9.3) 5711 o quantizer_update_value indicates the update value for the segment 5712 quantizer (Section 9.3) 5714 o quantizer_update_sign indicates the update sign for the segment 5715 quantizer (Section 9.3) 5717 o loop_filter_update indicates if the loop filter level value is 5718 updated for the i^(th) segment (Section 9.3) 5720 o lf_update_value indicates the update value for the loop filter 5721 level (Section 9.3) 5723 o lf_update_sign indicates the update sign for the loop filter level 5724 (Section 9.3) 5726 o segment_prob_update indicates if the branch probabilities used to 5727 decode the segment_id in the MB header are decoded from the stream 5728 or use the default value of 255 (Section 9.3) 5730 o segment_prob the branch probabilities of the segment_id decoding 5731 tree (Section 9.3) 5732 +------------------------------------+------+ 5733 | mb_lf_adjustments() | Type | 5734 +------------------------------------+------+ 5735 | loop_filter_adj_enable | L(1) | 5736 | | | 5737 | if (loop_filter_adj_enable) { | | 5738 | | | 5739 | mode_ref_lf_delta_update | L(1) | 5740 | | | 5741 | if (mode_ref_lf_delta_update) { | | 5742 | | | 5743 | for (i = 0; i < 4; i++) { | | 5744 | | | 5745 | ref_frame_delta_update_flag | L(1) | 5746 | | | 5747 | if (ref_frame_delta_update_flag) { | | 5748 | | | 5749 | delta_magnitude | L(6) | 5750 | | | 5751 | delta_sign | L(1) | 5752 | | | 5753 | } | | 5754 | | | 5755 | } | | 5756 | | | 5757 | for (i = 0; i < 4; i++) { | | 5758 | | | 5759 | mb_mode_delta_update_flag | L(1) | 5760 | | | 5761 | if (mb_mode_delta_update_flag) { | | 5762 | | | 5763 | delta_magnitude | L(6) | 5764 | | | 5765 | delta_sign | L(1) | 5766 | | | 5767 | } | | 5768 | | | 5769 | } | | 5770 | | | 5771 | } | | 5772 | | | 5773 | } | | 5774 +------------------------------------+------+ 5776 o loop_filter_adj_enable indicates if the MB-level loop filter 5777 adjustment (based on the used reference frame and coding mode) is 5778 on for the current frame (Section 9.4) 5780 o mode_ref_lf_delta_update indicates if the delta values used in 5781 adjustment are updated in the current frame (Section 9.4) 5783 o ref_frame_delta_update_flag indicates if the adjustment delta 5784 value corresponding to a certain used reference frame is updated 5785 (Section 9.4) 5787 o delta_magnitude is the absolute value of the delta value 5789 o delta_sign is the sign of the delta value 5791 o mb_mode_delta_update_flag indicates if the adjustment delta value 5792 corresponding to certain MB prediction mode is updated 5793 (Section 9.4) 5795 +----------------------------+------+ 5796 | quant_indices() | Type | 5797 +----------------------------+------+ 5798 | y_ac_qi | L(7) | 5799 | | | 5800 | y_dc_delta_present | L(1) | 5801 | | | 5802 | if (y_dc_delta_present) { | | 5803 | | | 5804 | y_dc_delta_magnitude | L(4) | 5805 | | | 5806 | y_dc_delta_sign | L(1) | 5807 | | | 5808 | } | | 5809 | | | 5810 | y2_dc_delta_present | L(1) | 5811 | | | 5812 | if (y2_dc_delta_present) { | | 5813 | | | 5814 | y2_dc_delta_magnitude | L(4) | 5815 | | | 5816 | y2_dc_delta_sign | L(1) | 5817 | | | 5818 | } | | 5819 | | | 5820 | y2_ac_delta_present | L(1) | 5821 | | | 5822 | if (y2_ac_delta_present) { | | 5823 | | | 5824 | y2_ac_delta_magnitude | L(4) | 5825 | | | 5826 | y2_ac_delta_sign | L(1) | 5827 | | | 5828 | } | | 5829 | | | 5830 | uv_dc_delta_present | L(1) | 5831 | | | 5832 | if (uv_dc_delta_present) { | | 5833 | | | 5834 | uv_dc_delta_magnitude | L(4) | 5835 | | | 5836 | uv_dc_delta_sign | L(1) | 5837 | | | 5838 | } | | 5839 | | | 5840 | uv_ac_delta_present | L(1) | 5841 | | | 5842 | if (uv_ac_delta_present) { | | 5843 | | | 5844 | uv_ac_delta_magnitude | L(4) | 5845 | | | 5846 | uv_ac_delta_sign | L(1) | 5847 | | | 5848 | } | | 5849 +----------------------------+------+ 5851 o y_ac_qi is the dequantization table index used for the luma AC 5852 coefficients (and other coefficient groups if no delta value is 5853 present) (Section 9.6) 5855 o y_dc_delta_present indicates if the stream contains a delta value 5856 that is added to the baseline index to obtain the luma DC 5857 coefficient dequantization index (Section 9.6) 5859 o y_dc_delta_magnitude the magnitude of the delta value 5860 (Section 9.6) 5862 o y_dc_delta_sign the sign of the delta value (Section 9.6) 5864 o y2_dc_delta_present indicates if the stream contains a delta value 5865 that is added to the baseline index to obtain the Y2 block DC 5866 coefficient dequantization index (Section 9.6) 5868 o y2_ac_delta_present indicates if the stream contains a delta value 5869 that is added to the baseline index to obtain the Y2 block AC 5870 coefficient dequantization index (Section 9.6) 5872 o uv_dc_delta_present indicates if the stream contains a delta value 5873 that is added to the baseline index to obtain the chroma DC 5874 coefficient dequantization index (Section 9.6) 5876 o uv_ac_delta_present indicates if the stream contains a delta value 5877 that is added to the baseline index to obtain the chroma AC 5878 coefficient dequantization index (Section 9.6) 5880 +-------------------------------+------+ 5881 | token_prob_update() | Type | 5882 +-------------------------------+------+ 5883 | for (i = 0; i < 4; i++) { | | 5884 | | | 5885 | for (j = 0; j < 8; j++) { | | 5886 | | | 5887 | for (k = 0; k < 3; k++) { | | 5888 | | | 5889 | for (l = 0; l < 11; l++) { | | 5890 | | | 5891 | coeff_prob_update_flag | L(1) | 5892 | | | 5893 | if (coeff_prob_update_flag) { | | 5894 | | | 5895 | coeff_prob | L(8) | 5896 | | | 5897 | } | | 5898 | | | 5899 | } | | 5900 | | | 5901 | } | | 5902 | | | 5903 | } | | 5904 | | | 5905 | } | | 5906 +-------------------------------+------+ 5908 o coeff_prob_update_flag indicates if the corresponding branch 5909 probability is updated in the current frame (Section 13.4) 5911 o coeff_prob is the new branch probability (Section 13.4) 5912 +----------------------------+------+ 5913 | mv_prob_update() | Type | 5914 +----------------------------+------+ 5915 | for (i = 0; i < 2; i++) { | | 5916 | | | 5917 | for (j = 0; j < 19; j++) { | | 5918 | | | 5919 | mv_prob_update_flag | L(1) | 5920 | | | 5921 | if (mv_prob_update_flag) { | | 5922 | | | 5923 | prob | L(7) | 5924 | | | 5925 | } | | 5926 | | | 5927 | } | | 5928 | | | 5929 | } | | 5930 +----------------------------+------+ 5932 o mv_prob_update_flag indicates if the corresponding MV decoding 5933 probability is updated in the current frame (Section 17.2) 5935 o prob is the updated probability (Section 17.2) 5937 19.3. Macroblock Data 5939 +---------------------+------+ 5940 | Macroblock Data | Type | 5941 +---------------------+------+ 5942 | macroblock_header() | | 5943 | | | 5944 | residual_data() | | 5945 +---------------------+------+ 5947 +--------------------------------+------+ 5948 | macroblock_header() | Type | 5949 +--------------------------------+------+ 5950 | if (segmentation_map_update) { | | 5951 | | | 5952 | segment_id | T | 5953 | | | 5954 | if (mb_no_coeff_skip) { | | 5955 | | | 5956 | mb_coeff_skip | B(p) | 5957 | | | 5958 | } | | 5959 | | | 5960 | if (!key_frame) { | | 5961 | | | 5962 | is_inter_mb | B(p) | 5963 | | | 5964 | if (is_inter_mb) { | | 5965 | | | 5966 | mb_ref_frame_sel1 | B(p) | 5967 | | | 5968 | if (mb_ref_frame_sel1) | | 5969 | | | 5970 | mb_ref_frame_sel2 | B(p) | 5971 | | | 5972 | mv_mode | T | 5973 | | | 5974 | if (mv_mode == SPLITMV) { | | 5975 | | | 5976 | mv_split_mode | T | 5977 | | | 5978 | for (i = 0; i < numMvs; i++) { | | 5979 | | | 5980 | sub_mv_mode | T | 5981 | | | 5982 | if (sub_mv_mode == NEWMV4x4) { | | 5983 | | | 5984 | read_mvcomponent() | | 5985 | | | 5986 | read_mvcomponent() | | 5987 | | | 5988 | } | | 5989 | | | 5990 | } | | 5991 | | | 5992 | } else if (mv_mode == NEWMV) { | | 5993 | | | 5994 | read_mvcomponent() | | 5995 | | | 5996 | read_mvcomponent() | | 5997 | | | 5998 | } | | 5999 | | | 6000 | } else { /* intra mb */ | | 6001 | | | 6002 | intra_y_mode | T | 6003 +--------------------------------+------+ 6004 +-------------------------------+------+ 6005 | macroblock_header() | Type | 6006 +-------------------------------+------+ 6007 | if (intra_y_mode == B_PRED) { | | 6008 | | | 6009 | for (i = 0; i < 16; i++) | | 6010 | | | 6011 | intra_b_mode | T | 6012 | | | 6013 | } | | 6014 | | | 6015 | intra_uv_mode | T | 6016 | | | 6017 | } | | 6018 +-------------------------------+------+ 6020 o segment_id indicates to which segment the macroblock belongs 6021 (Section 10) 6023 o mb_coeff_skip indicates if the macroblock contains any coded 6024 coefficients or not (Section 11.1) 6026 o is_inter_mb indicates if the macroblock is intra or inter coded 6027 (Section 16) 6029 o mb_ref_frame_sel1 selects the reference frame to be used; last 6030 frame (0), golden/alternate (1) (Section 16.2) 6032 o mb_ref_frame_sel2 selects whether the golden (0) or alternate 6033 reference frame (1) is used (Section 16.2) 6035 o mv_mode determines the macroblock motion vector mode 6036 (Section 16.2) 6038 o mv_split_mode gives macroblock partitioning specification and 6039 determines number of motion vectors used (numMvs)(Section 16.2) 6041 o sub_mv_mode determines the sub-macroblock motion vector mode for 6042 macroblocks coded using SPLITMV motion vector mode (Section 16.2) 6044 o intra_y_mode selects the luminance intra prediction mode 6045 (Section 16.1) 6047 o intra_b_mode selects the sub-macroblock luminance prediction mode 6048 for macroblocks coded using B_PRED mode (Section 16.1) 6050 o intra_uv_mode selects the chrominance intra prediction mode 6051 (Section 16.1) 6052 +----------------------------------------------+------+ 6053 | residual_data() | Type | 6054 +----------------------------------------------+------+ 6055 | if (!mb_coeff_skip) { | | 6056 | | | 6057 | if ( (is_inter_mb && mv_mode != SPLITMV) || | | 6058 | | | 6059 | (!is_inter_mb && intra_y_mode != B_PRED) ) { | | 6060 | | | 6061 | residual_block() /* Y2 */ | | 6062 | | | 6063 | } | | 6064 | | | 6065 | for (i = 0; i < 24; i++) | | 6066 | | | 6067 | residual_block() /* 16 Y, 4 U, 4 V */ | | 6068 | | | 6069 | } | | 6070 +----------------------------------------------+------+ 6072 +-------------------------------------+------+ 6073 | residual_block() | Type | 6074 +-------------------------------------+------+ 6075 | for (i = firstCoeff; i < 16; i++) { | | 6076 | | | 6077 | token | T | 6078 | | | 6079 | if (token == EOB) break; | | 6080 | | | 6081 | if (token_has_extra_bits) { | | 6082 | | | 6083 | extra_bits | L(n) | 6084 | | | 6085 | sign | L(1) | 6086 +-------------------------------------+------+ 6088 o firstCoeff is 1 for luma blocks of macroblocks containing Y2 6089 subblock, otherwise 0 6091 o token defines the value of the coefficient, the value range of the 6092 coefficient or the end of block (Section 13.2) 6094 o extra_bits determine the value of the coefficient within the value 6095 range defined by token (Section 13.2) 6097 o sign indicates the sign of the coefficient (Section 13.2) 6099 20. Attachment One: Reference Decoder Source Code 6101 20.1. bit_ops.h 6103 ---- Begin code block -------------------------------------- 6105 /* 6106 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6107 * 6108 * Use of this source code is governed by a BSD-style license 6109 * that can be found in the LICENSE file in the root of the source 6110 * tree. An additional intellectual property rights grant can be 6111 * found in the file PATENTS. All contributing project authors may 6112 * be found in the AUTHORS file in the root of the source tree. 6113 */ 6114 #ifndef BIT_OPS_H 6115 #define BIT_OPS_H 6117 /* Evaluates to a mask with n bits set */ 6118 #define BITS_MASK(n) ((1<<(n))-1) 6120 /* Returns len bits, with the LSB at position bit */ 6121 #define BITS_GET(val, bit, len) (((val)>>(bit))&BITS_MASK(len)) 6123 #endif 6125 ---- End code block ---------------------------------------- 6127 20.2. bool_decoder.h 6129 ---- Begin code block -------------------------------------- 6131 /* 6132 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6133 * 6134 * Use of this source code is governed by a BSD-style license and 6135 * patent grant that can be found in the LICENSE file in the root of 6136 * the source tree. All contributing project authors may be found in 6137 * the AUTHORS file in the root of the source tree. 6138 */ 6140 #ifndef BOOL_DECODER_H 6141 #define BOOL_DECODER_H 6142 #include 6144 struct bool_decoder 6145 { 6146 const unsigned char *input; /* next compressed data byte */ 6147 size_t input_len; /* length of the input buffer */ 6148 unsigned int range; /* identical to encoder's 6149 * range */ 6150 unsigned int value; /* contains at least 8 6151 * significant bits */ 6152 int bit_count; /* # of bits shifted out of 6153 * value, max 7 */ 6154 }; 6156 static void 6157 init_bool_decoder(struct bool_decoder *d, 6158 const unsigned char *start_partition, 6159 size_t sz) 6160 { 6161 if (sz >= 2) 6162 { 6163 d->value = (start_partition[0] << 8) /* first 2 input 6164 * bytes */ 6165 | start_partition[1]; 6166 d->input = start_partition + 2; /* ptr to next byte */ 6167 d->input_len = sz - 2; 6168 } 6169 else 6170 { 6171 d->value = 0; 6172 d->input = NULL; 6173 d->input_len = 0; 6174 } 6176 d->range = 255; /* initial range is full */ 6177 d->bit_count = 0; /* have not yet shifted out any bits */ 6178 } 6180 static int bool_get(struct bool_decoder *d, int probability) 6181 { 6182 /* range and split are identical to the corresponding values 6183 used by the encoder when this bool was written */ 6185 unsigned int split = 1 + (((d->range - 1) * probability) >> 8); 6186 unsigned int SPLIT = split << 8; 6187 int retval; /* will be 0 or 1 */ 6189 if (d->value >= SPLIT) /* encoded a one */ 6190 { 6191 retval = 1; 6192 d->range -= split; /* reduce range */ 6193 d->value -= SPLIT; /* subtract off left endpoint of 6194 * interval */ 6195 } 6196 else /* encoded a zero */ 6197 { 6198 retval = 0; 6199 d->range = split; /* reduce range, no change in left 6200 * endpoint */ 6201 } 6203 while (d->range < 128) /* shift out irrelevant value bits */ 6204 { 6205 d->value <<= 1; 6206 d->range <<= 1; 6208 if (++d->bit_count == 8) /* shift in new bits 8 at a time */ 6209 { 6210 d->bit_count = 0; 6212 if (d->input_len) 6213 { 6214 d->value |= *d->input++; 6215 d->input_len--; 6216 } 6217 } 6218 } 6220 return retval; 6221 } 6223 static int bool_get_bit(struct bool_decoder *br) 6224 { 6225 return bool_get(br, 128); 6226 } 6228 static int bool_get_uint(struct bool_decoder *br, int bits) 6229 { 6230 int z = 0; 6231 int bit; 6233 for (bit = bits - 1; bit >= 0; bit--) 6234 { 6235 z |= (bool_get_bit(br) << bit); 6236 } 6237 return z; 6238 } 6240 static int bool_get_int(struct bool_decoder *br, int bits) 6241 { 6242 int z = 0; 6243 int bit; 6245 for (bit = bits - 1; bit >= 0; bit--) 6246 { 6247 z |= (bool_get_bit(br) << bit); 6248 } 6250 return bool_get_bit(br) ? -z : z; 6251 } 6253 static int bool_maybe_get_int(struct bool_decoder *br, int bits) 6254 { 6255 return bool_get_bit(br) ? bool_get_int(br, bits) : 0; 6256 } 6258 static int 6259 bool_read_tree(struct bool_decoder *bool, 6260 const int *t, 6261 const unsigned char *p) 6262 { 6263 int i = 0; 6265 while ((i = t[ i + bool_get(bool, p[i>>1])]) > 0) ; 6267 return -i; 6268 } 6269 #endif 6271 ---- End code block ---------------------------------------- 6273 20.3. dequant_data.h 6275 ---- Begin code block -------------------------------------- 6277 static const int dc_q_lookup[128] = 6278 { 6279 4, 5, 6, 7, 8, 9, 10, 10, 6280 11, 12, 13, 14, 15, 16, 17, 17, 6281 18, 19, 20, 20, 21, 21, 22, 22, 6282 23, 23, 24, 25, 25, 26, 27, 28, 6283 29, 30, 31, 32, 33, 34, 35, 36, 6284 37, 37, 38, 39, 40, 41, 42, 43, 6285 44, 45, 46, 46, 47, 48, 49, 50, 6286 51, 52, 53, 54, 55, 56, 57, 58, 6287 59, 60, 61, 62, 63, 64, 65, 66, 6288 67, 68, 69, 70, 71, 72, 73, 74, 6289 75, 76, 76, 77, 78, 79, 80, 81, 6290 82, 83, 84, 85, 86, 87, 88, 89, 6291 91, 93, 95, 96, 98, 100, 101, 102, 6292 104, 106, 108, 110, 112, 114, 116, 118, 6293 122, 124, 126, 128, 130, 132, 134, 136, 6294 138, 140, 143, 145, 148, 151, 154, 157 6295 }; 6296 static const int ac_q_lookup[128] = 6297 { 6298 4, 5, 6, 7, 8, 9, 10, 11, 6299 12, 13, 14, 15, 16, 17, 18, 19, 6300 20, 21, 22, 23, 24, 25, 26, 27, 6301 28, 29, 30, 31, 32, 33, 34, 35, 6302 36, 37, 38, 39, 40, 41, 42, 43, 6303 44, 45, 46, 47, 48, 49, 50, 51, 6304 52, 53, 54, 55, 56, 57, 58, 60, 6305 62, 64, 66, 68, 70, 72, 74, 76, 6306 78, 80, 82, 84, 86, 88, 90, 92, 6307 94, 96, 98, 100, 102, 104, 106, 108, 6308 110, 112, 114, 116, 119, 122, 125, 128, 6309 131, 134, 137, 140, 143, 146, 149, 152, 6310 155, 158, 161, 164, 167, 170, 173, 177, 6311 181, 185, 189, 193, 197, 201, 205, 209, 6312 213, 217, 221, 225, 229, 234, 239, 245, 6313 249, 254, 259, 264, 269, 274, 279, 284 6314 }; 6316 ---- End code block ---------------------------------------- 6318 20.4. dixie.c 6319 ---- Begin code block -------------------------------------- 6321 /* 6322 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6323 * 6324 * Use of this source code is governed by a BSD-style license 6325 * that can be found in the LICENSE file in the root of the source 6326 * tree. An additional intellectual property rights grant can be 6327 * found in the file PATENTS. All contributing project authors may 6328 * be found in the AUTHORS file in the root of the source tree. 6329 */ 6330 #include "vpx_codec_internal.h" 6331 #include "bit_ops.h" 6332 #include "dixie.h" 6333 #include "vp8_prob_data.h" 6334 #include "dequant_data.h" 6335 #include "modemv.h" 6336 #include "tokens.h" 6337 #include "predict.h" 6338 #include "dixie_loopfilter.h" 6339 #include 6340 #include 6342 enum 6343 { 6344 FRAME_HEADER_SZ = 3, 6345 KEYFRAME_HEADER_SZ = 7 6346 }; 6348 #define ARRAY_COPY(a,b) {\ 6349 assert(sizeof(a)==sizeof(b));memcpy(a,b,sizeof(a));} 6350 static void 6351 decode_entropy_header(struct vp8_decoder_ctx *ctx, 6352 struct bool_decoder *bool, 6353 struct vp8_entropy_hdr *hdr) 6354 { 6355 int i, j, k, l; 6357 /* Read coefficient probability updates */ 6358 for (i = 0; i < BLOCK_TYPES; i++) 6359 for (j = 0; j < COEF_BANDS; j++) 6360 for (k = 0; k < PREV_COEF_CONTEXTS; k++) 6361 for (l = 0; l < ENTROPY_NODES; l++) 6362 if (bool_get(bool, 6363 k_coeff_entropy_update_probs 6364 [i][j][k][l])) 6365 hdr->coeff_probs[i][j][k][l] = 6366 bool_get_uint(bool, 8); 6368 /* Read coefficient skip mode probability */ 6369 hdr->coeff_skip_enabled = bool_get_bit(bool); 6371 if (hdr->coeff_skip_enabled) 6372 hdr->coeff_skip_prob = bool_get_uint(bool, 8); 6374 /* Parse interframe probability updates */ 6375 if (!ctx->frame_hdr.is_keyframe) 6376 { 6377 hdr->prob_inter = bool_get_uint(bool, 8); 6378 hdr->prob_last = bool_get_uint(bool, 8); 6379 hdr->prob_gf = bool_get_uint(bool, 8); 6381 if (bool_get_bit(bool)) 6382 for (i = 0; i < 4; i++) 6383 hdr->y_mode_probs[i] = bool_get_uint(bool, 8); 6385 if (bool_get_bit(bool)) 6386 for (i = 0; i < 3; i++) 6387 hdr->uv_mode_probs[i] = bool_get_uint(bool, 8); 6389 for (i = 0; i < 2; i++) 6390 for (j = 0; j < MV_PROB_CNT; j++) 6391 if (bool_get(bool, k_mv_entropy_update_probs[i][j])) 6392 { 6393 int x = bool_get_uint(bool, 7); 6394 hdr->mv_probs[i][j] = x ? x << 1 : 1; 6395 } 6396 } 6397 } 6399 static void 6400 decode_reference_header(struct vp8_decoder_ctx *ctx, 6401 struct bool_decoder *bool, 6402 struct vp8_reference_hdr *hdr) 6403 { 6404 unsigned int key = ctx->frame_hdr.is_keyframe; 6406 hdr->refresh_gf = key ? 1 : bool_get_bit(bool); 6407 hdr->refresh_arf = key ? 1 : bool_get_bit(bool); 6408 hdr->copy_gf = key ? 0 : !hdr->refresh_gf 6409 ? bool_get_uint(bool, 2) : 0; 6410 hdr->copy_arf = key ? 0 : !hdr->refresh_arf 6411 ? bool_get_uint(bool, 2) : 0; 6412 hdr->sign_bias[GOLDEN_FRAME] = key ? 0 : bool_get_bit(bool); 6413 hdr->sign_bias[ALTREF_FRAME] = key ? 0 : bool_get_bit(bool); 6414 hdr->refresh_entropy = bool_get_bit(bool); 6415 hdr->refresh_last = key ? 1 : bool_get_bit(bool); 6416 } 6418 static void 6419 decode_quantizer_header(struct vp8_decoder_ctx *ctx, 6420 struct bool_decoder *bool, 6421 struct vp8_quant_hdr *hdr) 6422 { 6423 int update; 6424 int last_q = hdr->q_index; 6426 hdr->q_index = bool_get_uint(bool, 7); 6427 update = last_q != hdr->q_index; 6428 update |= (hdr->y1_dc_delta_q = bool_maybe_get_int(bool, 4)); 6429 update |= (hdr->y2_dc_delta_q = bool_maybe_get_int(bool, 4)); 6430 update |= (hdr->y2_ac_delta_q = bool_maybe_get_int(bool, 4)); 6431 update |= (hdr->uv_dc_delta_q = bool_maybe_get_int(bool, 4)); 6432 update |= (hdr->uv_ac_delta_q = bool_maybe_get_int(bool, 4)); 6433 hdr->delta_update = update; 6434 } 6436 static void 6437 decode_and_init_token_partitions(struct vp8_decoder_ctx *ctx, 6438 struct bool_decoder *bool, 6439 const unsigned char *data, 6440 unsigned int sz, 6441 struct vp8_token_hdr *hdr) 6442 { 6443 int i; 6445 hdr->partitions = 1 << bool_get_uint(bool, 2); 6447 if (sz < 3 *(hdr->partitions - 1)) 6448 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6449 "Truncated packet found parsing partition" 6450 " lengths."); 6452 sz -= 3 * (hdr->partitions - 1); 6454 for (i = 0; i < hdr->partitions; i++) 6455 { 6456 if (i < hdr->partitions - 1) 6457 { 6458 hdr->partition_sz[i] = (data[2] << 16) 6459 | (data[1] << 8) | data[0]; 6460 data += 3; 6461 } 6462 else 6463 hdr->partition_sz[i] = sz; 6465 if (sz < hdr->partition_sz[i]) 6466 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6467 "Truncated partition %d", i); 6469 sz -= hdr->partition_sz[i]; 6470 } 6472 for (i = 0; i < ctx->token_hdr.partitions; i++) 6473 { 6474 init_bool_decoder(&ctx->tokens[i].bool, data, 6475 ctx->token_hdr.partition_sz[i]); 6476 data += ctx->token_hdr.partition_sz[i]; 6477 } 6478 } 6480 static void 6481 decode_loopfilter_header(struct vp8_decoder_ctx *ctx, 6482 struct bool_decoder *bool, 6483 struct vp8_loopfilter_hdr *hdr) 6484 { 6485 if (ctx->frame_hdr.is_keyframe) 6486 memset(hdr, 0, sizeof(*hdr)); 6488 hdr->use_simple = bool_get_bit(bool); 6489 hdr->level = bool_get_uint(bool, 6); 6490 hdr->sharpness = bool_get_uint(bool, 3); 6491 hdr->delta_enabled = bool_get_bit(bool); 6493 if (hdr->delta_enabled && bool_get_bit(bool)) 6494 { 6495 int i; 6497 for (i = 0; i < BLOCK_CONTEXTS; i++) 6498 hdr->ref_delta[i] = bool_maybe_get_int(bool, 6); 6500 for (i = 0; i < BLOCK_CONTEXTS; i++) 6501 hdr->mode_delta[i] = bool_maybe_get_int(bool, 6); 6502 } 6503 } 6504 static void 6505 decode_segmentation_header(struct vp8_decoder_ctx *ctx, 6506 struct bool_decoder *bool, 6507 struct vp8_segment_hdr *hdr) 6508 { 6509 if (ctx->frame_hdr.is_keyframe) 6510 memset(hdr, 0, sizeof(*hdr)); 6512 hdr->enabled = bool_get_bit(bool); 6514 if (hdr->enabled) 6515 { 6516 int i; 6518 hdr->update_map = bool_get_bit(bool); 6519 hdr->update_data = bool_get_bit(bool); 6521 if (hdr->update_data) 6522 { 6523 hdr->abs = bool_get_bit(bool); 6525 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6526 hdr->quant_idx[i] = bool_maybe_get_int(bool, 7); 6528 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6529 hdr->lf_level[i] = bool_maybe_get_int(bool, 6); 6530 } 6532 if (hdr->update_map) 6533 { 6534 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) 6535 hdr->tree_probs[i] = bool_get_bit(bool) 6536 ? bool_get_uint(bool, 8) 6537 : 255; 6538 } 6539 } 6540 else 6541 { 6542 hdr->update_map = 0; 6543 hdr->update_data = 0; 6544 } 6545 } 6547 static void 6548 dequant_global_init(struct dequant_factors dqf[MAX_MB_SEGMENTS]) 6549 { 6550 int i; 6551 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6552 dqf[i].quant_idx = -1; 6553 } 6555 static int 6556 clamp_q(int q) 6557 { 6558 if (q < 0) return 0; 6559 else if (q > 127) return 127; 6561 return q; 6562 } 6564 static int 6565 dc_q(int q) 6566 { 6567 return dc_q_lookup[clamp_q(q)]; 6568 } 6570 static int 6571 ac_q(int q) 6572 { 6573 return ac_q_lookup[clamp_q(q)]; 6574 } 6576 static void 6577 dequant_init(struct dequant_factors factors[MAX_MB_SEGMENTS], 6578 const struct vp8_segment_hdr *seg, 6579 const struct vp8_quant_hdr *quant_hdr) 6580 { 6581 int i, q; 6582 struct dequant_factors *dqf = factors; 6584 for (i = 0; i < (seg->enabled ? MAX_MB_SEGMENTS : 1); i++) 6585 { 6586 q = quant_hdr->q_index; 6588 if (seg->enabled) 6589 q = (!seg->abs) ? q + seg->quant_idx[i] 6590 : seg->quant_idx[i]; 6592 if (dqf->quant_idx != q || quant_hdr->delta_update) 6593 { 6594 dqf->factor[TOKEN_BLOCK_Y1][0] = 6595 dc_q(q + quant_hdr->y1_dc_delta_q); 6596 dqf->factor[TOKEN_BLOCK_Y1][1] = 6597 ac_q(q); 6598 dqf->factor[TOKEN_BLOCK_UV][0] = 6599 dc_q(q + quant_hdr->uv_dc_delta_q); 6600 dqf->factor[TOKEN_BLOCK_UV][1] = 6601 ac_q(q + quant_hdr->uv_ac_delta_q); 6602 dqf->factor[TOKEN_BLOCK_Y2][0] = 6603 dc_q(q + quant_hdr->y2_dc_delta_q) * 2; 6604 dqf->factor[TOKEN_BLOCK_Y2][1] = 6605 ac_q(q + quant_hdr->y2_ac_delta_q) * 155 / 100; 6607 if (dqf->factor[TOKEN_BLOCK_Y2][1] < 8) 6608 dqf->factor[TOKEN_BLOCK_Y2][1] = 8; 6610 if (dqf->factor[TOKEN_BLOCK_UV][0] > 132) 6611 dqf->factor[TOKEN_BLOCK_UV][0] = 132; 6613 dqf->quant_idx = q; 6614 } 6616 dqf++; 6617 } 6618 } 6620 static void 6621 decode_frame(struct vp8_decoder_ctx *ctx, 6622 const unsigned char *data, 6623 unsigned int sz) 6624 { 6625 vpx_codec_err_t res; 6626 struct bool_decoder bool; 6627 int i, row, partition; 6629 ctx->saved_entropy_valid = 0; 6631 if ((res = vp8_parse_frame_header(data, sz, &ctx->frame_hdr))) 6632 vpx_internal_error(&ctx->error, res, 6633 "Failed to parse frame header"); 6635 if (ctx->frame_hdr.is_experimental) 6636 vpx_internal_error(&ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6637 "Experimental bitstreams not supported."); 6639 data += FRAME_HEADER_SZ; 6640 sz -= FRAME_HEADER_SZ; 6641 if (ctx->frame_hdr.is_keyframe) 6642 { 6643 data += KEYFRAME_HEADER_SZ; 6644 sz -= KEYFRAME_HEADER_SZ; 6645 ctx->mb_cols = (ctx->frame_hdr.kf.w + 15) / 16; 6646 ctx->mb_rows = (ctx->frame_hdr.kf.h + 15) / 16; 6647 } 6649 /* Start the bitreader for the header/entropy partition */ 6650 init_bool_decoder(&bool, data, ctx->frame_hdr.part0_sz); 6652 /* Skip the colorspace and clamping bits */ 6653 if (ctx->frame_hdr.is_keyframe) 6654 if (bool_get_uint(&bool, 2)) 6655 vpx_internal_error( 6656 &ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6657 "Reserved bits not supported."); 6659 decode_segmentation_header(ctx, &bool, &ctx->segment_hdr); 6660 decode_loopfilter_header(ctx, &bool, &ctx->loopfilter_hdr); 6661 decode_and_init_token_partitions(ctx, 6662 &bool, 6663 data + ctx->frame_hdr.part0_sz, 6664 sz - ctx->frame_hdr.part0_sz, 6665 &ctx->token_hdr); 6666 decode_quantizer_header(ctx, &bool, &ctx->quant_hdr); 6667 decode_reference_header(ctx, &bool, &ctx->reference_hdr); 6669 /* Set keyframe entropy defaults. These get updated on keyframes 6670 * regardless of the refresh_entropy setting. 6671 */ 6672 if (ctx->frame_hdr.is_keyframe) 6673 { 6674 ARRAY_COPY(ctx->entropy_hdr.coeff_probs, 6675 k_default_coeff_probs); 6676 ARRAY_COPY(ctx->entropy_hdr.mv_probs, 6677 k_default_mv_probs); 6678 ARRAY_COPY(ctx->entropy_hdr.y_mode_probs, 6679 k_default_y_mode_probs); 6680 ARRAY_COPY(ctx->entropy_hdr.uv_mode_probs, 6681 k_default_uv_mode_probs); 6682 } 6684 if (!ctx->reference_hdr.refresh_entropy) 6685 { 6686 ctx->saved_entropy = ctx->entropy_hdr; 6687 ctx->saved_entropy_valid = 1; 6688 } 6689 decode_entropy_header(ctx, &bool, &ctx->entropy_hdr); 6691 vp8_dixie_modemv_init(ctx); 6692 vp8_dixie_tokens_init(ctx); 6693 vp8_dixie_predict_init(ctx); 6694 dequant_init(ctx->dequant_factors, &ctx->segment_hdr, 6695 &ctx->quant_hdr); 6697 for (row = 0, partition = 0; row < ctx->mb_rows; row++) 6698 { 6699 vp8_dixie_modemv_process_row( 6700 ctx, &bool, row, 0, ctx->mb_cols); 6701 vp8_dixie_tokens_process_row(ctx, partition, row, 0, 6702 ctx->mb_cols); 6703 vp8_dixie_predict_process_row(ctx, row, 0, ctx->mb_cols); 6705 if (ctx->loopfilter_hdr.level && row) 6706 vp8_dixie_loopfilter_process_row(ctx, row - 1, 0, 6707 ctx->mb_cols); 6709 if (++partition == ctx->token_hdr.partitions) 6710 partition = 0; 6711 } 6713 if (ctx->loopfilter_hdr.level) 6714 vp8_dixie_loopfilter_process_row( 6715 ctx, row - 1, 0, ctx->mb_cols); 6717 ctx->frame_cnt++; 6719 if (!ctx->reference_hdr.refresh_entropy) 6720 { 6721 ctx->entropy_hdr = ctx->saved_entropy; 6722 ctx->saved_entropy_valid = 0; 6723 } 6725 /* Handle reference frame updates */ 6726 if (ctx->reference_hdr.copy_arf == 1) 6727 { 6728 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6729 ctx->ref_frames[ALTREF_FRAME] = 6730 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6731 } 6732 else if (ctx->reference_hdr.copy_arf == 2) 6733 { 6734 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6735 ctx->ref_frames[ALTREF_FRAME] = 6736 vp8_dixie_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6738 } 6740 if (ctx->reference_hdr.copy_gf == 1) 6741 { 6742 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6743 ctx->ref_frames[GOLDEN_FRAME] = 6744 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6745 } 6746 else if (ctx->reference_hdr.copy_gf == 2) 6747 { 6748 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6749 ctx->ref_frames[GOLDEN_FRAME] = 6750 vp8_dixie_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6751 } 6753 if (ctx->reference_hdr.refresh_gf) 6754 { 6755 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6756 ctx->ref_frames[GOLDEN_FRAME] = 6757 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6758 } 6760 if (ctx->reference_hdr.refresh_arf) 6761 { 6762 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6763 ctx->ref_frames[ALTREF_FRAME] = 6764 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6765 } 6767 if (ctx->reference_hdr.refresh_last) 6768 { 6769 vp8_dixie_release_ref_frame(ctx->ref_frames[LAST_FRAME]); 6770 ctx->ref_frames[LAST_FRAME] = 6771 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6772 } 6774 } 6776 void 6777 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx) 6778 { 6779 dequant_global_init(ctx->dequant_factors); 6780 } 6782 #define CHECK_FOR_UPDATE(lval,rval,update_flag) do {\ 6783 unsigned int old = lval; \ 6784 update_flag |= (old != (lval = rval)); \ 6785 } while(0) 6787 vpx_codec_err_t 6788 vp8_parse_frame_header(const unsigned char *data, 6789 unsigned int sz, 6790 struct vp8_frame_hdr *hdr) 6791 { 6792 unsigned long raw; 6794 if (sz < 10) 6795 return VPX_CODEC_CORRUPT_FRAME; 6797 /* The frame header is defined as a three byte little endian 6798 * value 6799 */ 6800 raw = data[0] | (data[1] << 8) | (data[2] << 16); 6801 hdr->is_keyframe = !BITS_GET(raw, 0, 1); 6802 hdr->version = BITS_GET(raw, 1, 2); 6803 hdr->is_experimental = BITS_GET(raw, 3, 1); 6804 hdr->is_shown = BITS_GET(raw, 4, 1); 6805 hdr->part0_sz = BITS_GET(raw, 5, 19); 6807 if (sz <= hdr->part0_sz + (hdr->is_keyframe ? 10 : 3)) 6808 return VPX_CODEC_CORRUPT_FRAME; 6810 hdr->frame_size_updated = 0; 6812 if (hdr->is_keyframe) 6813 { 6814 unsigned int update = 0; 6816 /* Keyframe header consists of a three byte sync code 6817 * followed by the width and height and associated scaling 6818 * factors. 6819 */ 6820 if (data[3] != 0x9d || data[4] != 0x01 || data[5] != 0x2a) 6821 return VPX_CODEC_UNSUP_BITSTREAM; 6823 raw = data[6] | (data[7] << 8) 6824 | (data[8] << 16) | (data[9] << 24); 6825 CHECK_FOR_UPDATE(hdr->kf.w, BITS_GET(raw, 0, 14), 6826 update); 6827 CHECK_FOR_UPDATE(hdr->kf.scale_w, BITS_GET(raw, 14, 2), 6828 update); 6829 CHECK_FOR_UPDATE(hdr->kf.h, BITS_GET(raw, 16, 14), 6830 update); 6831 CHECK_FOR_UPDATE(hdr->kf.scale_h, BITS_GET(raw, 30, 2), 6832 update); 6834 hdr->frame_size_updated = update; 6836 if (!hdr->kf.w || !hdr->kf.h) 6837 return VPX_CODEC_UNSUP_BITSTREAM; 6838 } 6840 return VPX_CODEC_OK; 6841 } 6843 vpx_codec_err_t 6844 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 6845 const unsigned char *data, 6846 unsigned int sz) 6847 { 6848 volatile struct vp8_decoder_ctx *ctx_ = ctx; 6850 ctx->error.error_code = VPX_CODEC_OK; 6851 ctx->error.has_detail = 0; 6853 if (!setjmp(ctx->error.jmp)) 6854 decode_frame(ctx, data, sz); 6856 return ctx_->error.error_code; 6857 } 6859 void 6860 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx) 6861 { 6862 vp8_dixie_predict_destroy(ctx); 6863 vp8_dixie_tokens_destroy(ctx); 6864 vp8_dixie_modemv_destroy(ctx); 6865 } 6867 ---- End code block ---------------------------------------- 6869 20.5. dixie.h 6871 ---- Begin code block -------------------------------------- 6873 /* 6874 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6875 * 6876 * Use of this source code is governed by a BSD-style license 6877 * that can be found in the LICENSE file in the root of the source 6878 * tree. An additional intellectual property rights grant can be 6879 * found in the file PATENTS. All contributing project authors may 6880 * be found in the AUTHORS file in the root of the source tree. 6881 */ 6882 #ifndef DIXIE_H 6883 #define DIXIE_H 6884 #include "vpx_codec_internal.h" 6885 #include "bool_decoder.h" 6887 struct vp8_frame_hdr 6888 { 6889 unsigned int is_keyframe; /* Frame is a keyframe */ 6890 unsigned int is_experimental; /* Frame is a keyframe */ 6891 unsigned int version; /* Bitstream version */ 6892 unsigned int is_shown; /* Frame is to be displayed. */ 6893 unsigned int part0_sz; /* Partition 0 length, in bytes */ 6895 struct vp8_kf_hdr 6896 { 6897 unsigned int w; /* Width */ 6898 unsigned int h; /* Height */ 6899 unsigned int scale_w; /* Scaling factor, Width */ 6900 unsigned int scale_h; /* Scaling factor, Height */ 6901 } kf; 6903 unsigned int frame_size_updated; /* Flag to indicate a resolution 6904 * update. 6905 */ 6906 }; 6908 enum 6909 { 6910 MB_FEATURE_TREE_PROBS = 3, 6911 MAX_MB_SEGMENTS = 4 6912 }; 6914 struct vp8_segment_hdr 6915 { 6916 unsigned int enabled; 6917 unsigned int update_data; 6918 unsigned int update_map; 6919 unsigned int abs; /* 0=deltas, 1=absolute values */ 6920 unsigned int tree_probs[MB_FEATURE_TREE_PROBS]; 6921 int lf_level[MAX_MB_SEGMENTS]; 6922 int quant_idx[MAX_MB_SEGMENTS]; 6924 }; 6926 enum 6927 { 6928 BLOCK_CONTEXTS = 4 6929 }; 6931 struct vp8_loopfilter_hdr 6932 { 6933 unsigned int use_simple; 6934 unsigned int level; 6935 unsigned int sharpness; 6936 unsigned int delta_enabled; 6937 int ref_delta[BLOCK_CONTEXTS]; 6938 int mode_delta[BLOCK_CONTEXTS]; 6939 }; 6941 enum 6942 { 6943 MAX_PARTITIONS = 8 6944 }; 6946 struct vp8_token_hdr 6947 { 6948 unsigned int partitions; 6949 unsigned int partition_sz[MAX_PARTITIONS]; 6950 }; 6952 struct vp8_quant_hdr 6953 { 6954 unsigned int q_index; 6955 int delta_update; 6956 int y1_dc_delta_q; 6957 int y2_dc_delta_q; 6958 int y2_ac_delta_q; 6959 int uv_dc_delta_q; 6960 int uv_ac_delta_q; 6961 }; 6963 struct vp8_reference_hdr 6964 { 6965 unsigned int refresh_last; 6966 unsigned int refresh_gf; 6967 unsigned int refresh_arf; 6968 unsigned int copy_gf; 6969 unsigned int copy_arf; 6970 unsigned int sign_bias[4]; 6971 unsigned int refresh_entropy; 6972 }; 6974 enum 6975 { 6976 BLOCK_TYPES = 4, 6977 PREV_COEF_CONTEXTS = 3, 6978 COEF_BANDS = 8, 6979 ENTROPY_NODES = 11, 6980 }; 6981 typedef unsigned char coeff_probs_table_t[BLOCK_TYPES][COEF_BANDS] 6982 [PREV_COEF_CONTEXTS] 6983 [ENTROPY_NODES]; 6985 enum 6986 { 6987 MV_PROB_CNT = 2 + 8 - 1 + 10 /* from entropymv.h */ 6988 }; 6989 typedef unsigned char mv_component_probs_t[MV_PROB_CNT]; 6991 struct vp8_entropy_hdr 6992 { 6993 coeff_probs_table_t coeff_probs; 6994 mv_component_probs_t mv_probs[2]; 6995 unsigned int coeff_skip_enabled; 6996 unsigned char coeff_skip_prob; 6997 unsigned char y_mode_probs[4]; 6998 unsigned char uv_mode_probs[3]; 6999 unsigned char prob_inter; 7000 unsigned char prob_last; 7001 unsigned char prob_gf; 7002 }; 7004 enum reference_frame 7005 { 7006 CURRENT_FRAME, 7007 LAST_FRAME, 7008 GOLDEN_FRAME, 7009 ALTREF_FRAME, 7010 NUM_REF_FRAMES 7012 }; 7014 enum prediction_mode 7015 { 7016 /* 16x16 intra modes */ 7017 DC_PRED, V_PRED, H_PRED, TM_PRED, B_PRED, 7019 /* 16x16 inter modes */ 7020 NEARESTMV, NEARMV, ZEROMV, NEWMV, SPLITMV, 7022 MB_MODE_COUNT, 7024 /* 4x4 intra modes */ 7025 B_DC_PRED = 0, B_TM_PRED, B_VE_PRED, B_HE_PRED, B_LD_PRED, 7026 B_RD_PRED, B_VR_PRED, B_VL_PRED, B_HD_PRED, B_HU_PRED, 7028 /* 4x4 inter modes */ 7029 LEFT4X4, ABOVE4X4, ZERO4X4, NEW4X4, 7031 B_MODE_COUNT 7032 }; 7034 enum splitmv_partitioning 7035 { 7036 SPLITMV_16X8, 7037 SPLITMV_8X16, 7038 SPLITMV_8X8, 7039 SPLITMV_4X4 7040 }; 7042 typedef short filter_t[6]; 7044 typedef union mv 7045 { 7046 struct 7047 { 7048 int16_t x, y; 7049 } d; 7050 uint32_t raw; 7051 } mv_t; 7053 struct mb_base_info 7054 { 7055 unsigned char y_mode : 4; 7056 unsigned char uv_mode : 4; 7057 unsigned char segment_id : 2; 7058 unsigned char ref_frame : 2; 7059 unsigned char skip_coeff : 1; 7060 unsigned char need_mc_border : 1; 7061 enum splitmv_partitioning partitioning : 2; 7062 union mv mv; 7063 unsigned int eob_mask; 7064 }; 7066 struct mb_info 7067 { 7068 struct mb_base_info base; 7069 union 7070 { 7071 union mv mvs[16]; 7072 enum prediction_mode modes[16]; 7073 } split; 7074 }; 7076 /* A "token entropy context" has 4 Y values, 2 U, 2 V, and 1 Y2 */ 7077 typedef int token_entropy_ctx_t[4 + 2 + 2 + 1]; 7079 struct token_decoder 7080 { 7081 struct bool_decoder bool; 7082 token_entropy_ctx_t left_token_entropy_ctx; 7083 short *coeffs; 7084 }; 7086 enum token_block_type 7087 { 7088 TOKEN_BLOCK_Y1, 7089 TOKEN_BLOCK_UV, 7090 TOKEN_BLOCK_Y2, 7091 TOKEN_BLOCK_TYPES, 7092 }; 7094 struct dequant_factors 7095 { 7096 int quant_idx; 7097 short factor[TOKEN_BLOCK_TYPES][2]; /* [ Y1, UV, Y2 ] 7098 * [ DC, AC ] */ 7099 }; 7100 struct ref_cnt_img 7101 { 7102 vpx_image_t img; 7103 unsigned int ref_cnt; 7104 }; 7106 struct vp8_decoder_ctx 7107 { 7108 struct vpx_internal_error_info error; 7109 unsigned int frame_cnt; 7111 struct vp8_frame_hdr frame_hdr; 7112 struct vp8_segment_hdr segment_hdr; 7113 struct vp8_loopfilter_hdr loopfilter_hdr; 7114 struct vp8_token_hdr token_hdr; 7115 struct vp8_quant_hdr quant_hdr; 7116 struct vp8_reference_hdr reference_hdr; 7117 struct vp8_entropy_hdr entropy_hdr; 7119 struct vp8_entropy_hdr saved_entropy; 7120 unsigned int saved_entropy_valid; 7122 unsigned int mb_rows; 7123 unsigned int mb_cols; 7124 struct mb_info *mb_info_storage; 7125 struct mb_info **mb_info_rows_storage; 7126 struct mb_info **mb_info_rows; 7128 token_entropy_ctx_t *above_token_entropy_ctx; 7129 struct token_decoder tokens[MAX_PARTITIONS]; 7130 struct dequant_factors dequant_factors[MAX_MB_SEGMENTS]; 7132 struct ref_cnt_img frame_strg[NUM_REF_FRAMES]; 7133 struct ref_cnt_img *ref_frames[NUM_REF_FRAMES]; 7134 ptrdiff_t ref_frame_offsets[4]; 7136 const filter_t *subpixel_filters; 7137 }; 7139 void 7140 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx); 7142 void 7143 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx); 7144 vpx_codec_err_t 7145 vp8_parse_frame_header(const unsigned char *data, 7146 unsigned int sz, 7147 struct vp8_frame_hdr *hdr); 7149 vpx_codec_err_t 7150 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 7151 const unsigned char *data, 7152 unsigned int sz); 7154 #define CLAMP_255(x) ((x)<0?0:((x)>255?255:(x))) 7156 #endif 7158 ---- End code block ---------------------------------------- 7160 20.6. dixie_loopfilter.c 7162 ---- Begin code block -------------------------------------- 7164 /* 7165 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7166 * 7167 * Use of this source code is governed by a BSD-style license 7168 * that can be found in the LICENSE file in the root of the source 7169 * tree. An additional intellectual property rights grant can be 7170 * found in the file PATENTS. All contributing project authors may 7171 * be found in the AUTHORS file in the root of the source tree. 7172 */ 7173 #include "dixie.h" 7174 #include "dixie_loopfilter.h" 7176 #define ABS(x) ((x) >= 0 ? (x) : -(x)) 7178 #define p3 pixels[-4*stride] 7179 #define p2 pixels[-3*stride] 7180 #define p1 pixels[-2*stride] 7181 #define p0 pixels[-1*stride] 7182 #define q0 pixels[ 0*stride] 7183 #define q1 pixels[ 1*stride] 7184 #define q2 pixels[ 2*stride] 7185 #define q3 pixels[ 3*stride] 7187 #define static 7188 static int 7189 saturate_int8(int x) 7190 { 7191 if (x < -128) 7192 return -128; 7194 if (x > 127) 7195 return 127; 7197 return x; 7198 } 7200 static int 7201 saturate_uint8(int x) 7202 { 7203 if (x < 0) 7204 return 0; 7206 if (x > 255) 7207 return 255; 7209 return x; 7210 } 7212 static int 7213 high_edge_variance(unsigned char *pixels, 7214 int stride, 7215 int hev_threshold) 7216 { 7217 return ABS(p1 - p0) > hev_threshold || 7218 ABS(q1 - q0) > hev_threshold; 7219 } 7221 static int 7222 simple_threshold(unsigned char *pixels, 7223 int stride, 7224 int filter_limit) 7225 { 7226 return (ABS(p0 - q0) * 2 + (ABS(p1 - q1) >> 1)) <= filter_limit; 7227 } 7229 static int 7230 normal_threshold(unsigned char *pixels, 7231 int stride, 7232 int edge_limit, 7233 int interior_limit) 7234 { 7235 int E = edge_limit; 7236 int I = interior_limit; 7238 return simple_threshold(pixels, stride, 2 * E + I) 7239 && ABS(p3 - p2) <= I && ABS(p2 - p1) <= I 7240 && ABS(p1 - p0) <= I && ABS(q3 - q2) <= I 7241 && ABS(q2 - q1) <= I && ABS(q1 - q0) <= I; 7242 } 7244 static void 7245 filter_common(unsigned char *pixels, 7246 int stride, 7247 int use_outer_taps) 7248 { 7249 int a, f1, f2; 7251 a = 3 * (q0 - p0); 7253 if (use_outer_taps) 7254 a += saturate_int8(p1 - q1); 7256 a = saturate_int8(a); 7258 f1 = ((a + 4 > 127) ? 127 : a + 4) >> 3; 7259 f2 = ((a + 3 > 127) ? 127 : a + 3) >> 3; 7261 p0 = saturate_uint8(p0 + f2); 7262 q0 = saturate_uint8(q0 - f1); 7264 if (!use_outer_taps) 7265 { 7266 /* This handles the case of subblock_filter() 7267 * (from the bitstream guide. 7268 */ 7269 a = (f1 + 1) >> 1; 7270 p1 = saturate_uint8(p1 + a); 7271 q1 = saturate_uint8(q1 - a); 7272 } 7273 } 7275 static void 7276 filter_mb_edge(unsigned char *pixels, 7277 int stride) 7278 { 7279 int w, a; 7281 w = saturate_int8(saturate_int8(p1 - q1) + 3 * (q0 - p0)); 7283 a = (27 * w + 63) >> 7; 7284 p0 = saturate_uint8(p0 + a); 7285 q0 = saturate_uint8(q0 - a); 7287 a = (18 * w + 63) >> 7; 7288 p1 = saturate_uint8(p1 + a); 7289 q1 = saturate_uint8(q1 - a); 7291 a = (9 * w + 63) >> 7; 7292 p2 = saturate_uint8(p2 + a); 7293 q2 = saturate_uint8(q2 - a); 7295 } 7297 static void 7298 filter_mb_v_edge(unsigned char *src, 7299 int stride, 7300 int edge_limit, 7301 int interior_limit, 7302 int hev_threshold, 7303 int size) 7304 { 7305 int i; 7307 for (i = 0; i < 8 * size; i++) 7308 { 7309 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7310 { 7311 if (high_edge_variance(src, 1, hev_threshold)) 7312 filter_common(src, 1, 1); 7313 else 7314 filter_mb_edge(src, 1); 7315 } 7317 src += stride; 7318 } 7319 } 7321 static void 7322 filter_subblock_v_edge(unsigned char *src, 7323 int stride, 7324 int edge_limit, 7325 int interior_limit, 7326 int hev_threshold, 7327 int size) 7328 { 7329 int i; 7331 for (i = 0; i < 8 * size; i++) 7332 { 7333 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7334 filter_common(src, 1, 7335 high_edge_variance(src, 1, hev_threshold)); 7337 src += stride; 7338 } 7339 } 7341 static void 7342 filter_mb_h_edge(unsigned char *src, 7343 int stride, 7344 int edge_limit, 7345 int interior_limit, 7346 int hev_threshold, 7347 int size) 7348 { 7349 int i; 7351 for (i = 0; i < 8 * size; i++) 7352 { 7353 if (normal_threshold(src, stride, edge_limit, 7354 interior_limit)) 7355 { 7356 if (high_edge_variance(src, stride, hev_threshold)) 7357 filter_common(src, stride, 1); 7358 else 7359 filter_mb_edge(src, stride); 7360 } 7362 src += 1; 7363 } 7364 } 7366 static void 7367 filter_subblock_h_edge(unsigned char *src, 7368 int stride, 7369 int edge_limit, 7370 int interior_limit, 7371 int hev_threshold, 7372 int size) 7373 { 7374 int i; 7376 for (i = 0; i < 8 * size; i++) 7377 { 7378 if (normal_threshold(src, stride, edge_limit, 7379 interior_limit)) 7380 filter_common(src, stride, 7381 high_edge_variance(src, stride, 7382 hev_threshold)); 7384 src += 1; 7385 } 7386 } 7388 static void 7389 filter_v_edge_simple(unsigned char *src, 7390 int stride, 7391 int filter_limit) 7392 { 7393 int i; 7395 for (i = 0; i < 16; i++) 7396 { 7397 if (simple_threshold(src, 1, filter_limit)) 7398 filter_common(src, 1, 1); 7400 src += stride; 7401 } 7402 } 7404 static void 7405 filter_h_edge_simple(unsigned char *src, 7406 int stride, 7407 int filter_limit) 7408 { 7409 int i; 7411 for (i = 0; i < 16; i++) 7412 { 7413 if (simple_threshold(src, stride, filter_limit)) 7414 filter_common(src, stride, 1); 7416 src += 1; 7418 } 7419 } 7421 static void 7422 calculate_filter_parameters(struct vp8_decoder_ctx *ctx, 7423 struct mb_info *mbi, 7424 int *edge_limit_, 7425 int *interior_limit_, 7426 int *hev_threshold_) 7427 { 7428 int filter_level, interior_limit, hev_threshold; 7430 /* Reference code/spec seems to conflate filter_level and 7431 * edge_limit 7432 */ 7434 filter_level = ctx->loopfilter_hdr.level; 7436 if (ctx->segment_hdr.enabled) 7437 { 7438 if (!ctx->segment_hdr.abs) 7439 filter_level += 7440 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7441 else 7442 filter_level = 7443 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7444 } 7446 if (filter_level > 63) 7447 filter_level = 63; 7448 else if (filter_level < 0) 7449 filter_level = 0; 7451 if (ctx->loopfilter_hdr.delta_enabled) 7452 { 7453 filter_level += 7454 ctx->loopfilter_hdr.ref_delta[mbi->base.ref_frame]; 7456 if (mbi->base.ref_frame == CURRENT_FRAME) 7457 { 7458 if (mbi->base.y_mode == B_PRED) 7459 filter_level += ctx->loopfilter_hdr.mode_delta[0]; 7460 } 7461 else if (mbi->base.y_mode == ZEROMV) 7462 filter_level += ctx->loopfilter_hdr.mode_delta[1]; 7463 else if (mbi->base.y_mode == SPLITMV) 7464 filter_level += ctx->loopfilter_hdr.mode_delta[3]; 7466 else 7467 filter_level += ctx->loopfilter_hdr.mode_delta[2]; 7468 } 7470 if (filter_level > 63) 7471 filter_level = 63; 7472 else if (filter_level < 0) 7473 filter_level = 0; 7475 interior_limit = filter_level; 7477 if (ctx->loopfilter_hdr.sharpness) 7478 { 7479 interior_limit >>= ctx->loopfilter_hdr.sharpness > 4 ? 2 : 1; 7481 if (interior_limit > 9 - ctx->loopfilter_hdr.sharpness) 7482 interior_limit = 9 - ctx->loopfilter_hdr.sharpness; 7483 } 7485 if (interior_limit < 1) 7486 interior_limit = 1; 7488 hev_threshold = (filter_level >= 15); 7490 if (filter_level >= 40) 7491 hev_threshold++; 7493 if (filter_level >= 20 && !ctx->frame_hdr.is_keyframe) 7494 hev_threshold++; 7496 *edge_limit_ = filter_level; 7497 *interior_limit_ = interior_limit; 7498 *hev_threshold_ = hev_threshold; 7499 } 7501 static void 7502 filter_row_normal(struct vp8_decoder_ctx *ctx, 7503 unsigned int row, 7504 unsigned int start_col, 7505 unsigned int num_cols) 7506 { 7507 unsigned char *y, *u, *v; 7508 int stride, uv_stride; 7509 struct mb_info *mbi; 7510 unsigned int col; 7512 /* Adjust pointers based on row, start_col */ 7513 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7514 uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 7515 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7516 u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 7517 v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 7518 y += (stride * row + start_col) * 16; 7519 u += (uv_stride * row + start_col) * 8; 7520 v += (uv_stride * row + start_col) * 8; 7521 mbi = ctx->mb_info_rows[row] + start_col; 7523 for (col = start_col; col < start_col + num_cols; col++) 7524 { 7525 int edge_limit, interior_limit, hev_threshold; 7527 /* TODO: only need to recalculate every MB if segmentation is 7528 * enabled. 7529 */ 7530 calculate_filter_parameters(ctx, mbi, &edge_limit, 7531 &interior_limit, &hev_threshold); 7533 if (edge_limit) 7534 { 7535 if (col) 7536 { 7537 filter_mb_v_edge(y, stride, edge_limit + 2, 7538 interior_limit, hev_threshold, 2); 7539 filter_mb_v_edge(u, uv_stride, edge_limit + 2, 7540 interior_limit, hev_threshold, 1); 7541 filter_mb_v_edge(v, uv_stride, edge_limit + 2, 7542 interior_limit, hev_threshold, 1); 7543 } 7545 /* NOTE: This conditional is actually dependent on the 7546 * number of coefficients decoded, not the skip flag as 7547 * coded in the bitstream. The tokens task is expected to 7548 * set 31 if there is *any* non-zero data. 7549 */ 7550 if (mbi->base.eob_mask 7551 || mbi->base.y_mode == SPLITMV 7552 || mbi->base.y_mode == B_PRED) 7553 { 7554 filter_subblock_v_edge(y + 4, stride, edge_limit, 7555 interior_limit, hev_threshold, 7556 2); 7557 filter_subblock_v_edge(y + 8, stride, edge_limit, 7558 interior_limit, hev_threshold, 7559 2); 7560 filter_subblock_v_edge(y + 12, stride, edge_limit, 7561 interior_limit, hev_threshold, 7562 2); 7563 filter_subblock_v_edge(u + 4, uv_stride, edge_limit, 7564 interior_limit, hev_threshold, 7565 1); 7566 filter_subblock_v_edge(v + 4, uv_stride, edge_limit, 7567 interior_limit, hev_threshold, 7568 1); 7569 } 7571 if (row) 7572 { 7573 filter_mb_h_edge(y, stride, edge_limit + 2, 7574 interior_limit, hev_threshold, 2); 7575 filter_mb_h_edge(u, uv_stride, edge_limit + 2, 7576 interior_limit, hev_threshold, 1); 7577 filter_mb_h_edge(v, uv_stride, edge_limit + 2, 7578 interior_limit, hev_threshold, 1); 7579 } 7581 if (mbi->base.eob_mask 7582 || mbi->base.y_mode == SPLITMV 7583 || mbi->base.y_mode == B_PRED) 7584 { 7585 filter_subblock_h_edge(y + 4 * stride, stride, 7586 edge_limit, interior_limit, 7587 hev_threshold, 2); 7588 filter_subblock_h_edge(y + 8 * stride, stride, 7589 edge_limit, interior_limit, 7590 hev_threshold, 2); 7591 filter_subblock_h_edge(y + 12 * stride, stride, 7592 edge_limit, interior_limit, 7593 hev_threshold, 2); 7594 filter_subblock_h_edge(u + 4 * uv_stride, uv_stride, 7595 edge_limit, interior_limit, 7596 hev_threshold, 1); 7597 filter_subblock_h_edge(v + 4 * uv_stride, uv_stride, 7598 edge_limit, interior_limit, 7599 hev_threshold, 1); 7600 } 7601 } 7603 y += 16; 7604 u += 8; 7605 v += 8; 7606 mbi++; 7607 } 7608 } 7609 static void 7610 filter_row_simple(struct vp8_decoder_ctx *ctx, 7611 unsigned int row, 7612 unsigned int start_col, 7613 unsigned int num_cols) 7614 { 7615 unsigned char *y; 7616 int stride; 7617 struct mb_info *mbi; 7618 unsigned int col; 7620 /* Adjust pointers based on row, start_col */ 7621 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7622 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7623 y += (stride * row + start_col) * 16; 7624 mbi = ctx->mb_info_rows[row] + start_col; 7626 for (col = start_col; col < start_col + num_cols; col++) 7627 { 7628 int edge_limit, interior_limit, hev_threshold; 7630 /* TODO: only need to recalculate every MB if segmentation is 7631 * enabled. 7632 */ 7633 calculate_filter_parameters(ctx, mbi, &edge_limit, 7634 &interior_limit, &hev_threshold); 7636 if (edge_limit) 7637 { 7639 /* NOTE: This conditional is actually dependent on the 7640 * number of coefficients decoded, not the skip flag as 7641 * coded in the bitstream. The tokens task is expected to 7642 * set 31 if there is *any* non-zero data. 7643 */ 7644 int filter_subblocks = (mbi->base.eob_mask 7645 || mbi->base.y_mode == SPLITMV 7646 || mbi->base.y_mode == B_PRED); 7647 int mb_limit = (edge_limit + 2) * 2 + interior_limit; 7648 int b_limit = edge_limit * 2 + interior_limit; 7650 if (col) 7651 filter_v_edge_simple(y, stride, mb_limit); 7653 if (filter_subblocks) 7654 { 7655 filter_v_edge_simple(y + 4, stride, b_limit); 7656 filter_v_edge_simple(y + 8, stride, b_limit); 7657 filter_v_edge_simple(y + 12, stride, b_limit); 7658 } 7660 if (row) 7661 filter_h_edge_simple(y, stride, mb_limit); 7663 if (filter_subblocks) 7664 { 7665 filter_h_edge_simple(y + 4 * stride, stride, 7666 b_limit); 7667 filter_h_edge_simple(y + 8 * stride, stride, 7668 b_limit); 7669 filter_h_edge_simple(y + 12 * stride, stride, 7670 b_limit); 7671 } 7672 } 7674 y += 16; 7675 mbi++; 7676 } 7677 } 7679 void 7680 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7681 unsigned int row, 7682 unsigned int start_col, 7683 unsigned int num_cols) 7684 { 7685 if (ctx->loopfilter_hdr.use_simple) 7686 filter_row_simple(ctx, row, start_col, num_cols); 7687 else 7688 filter_row_normal(ctx, row, start_col, num_cols); 7689 } 7691 ---- End code block ---------------------------------------- 7693 20.7. dixie_loopfilter.h 7695 ---- Begin code block -------------------------------------- 7697 /* 7698 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7699 * 7700 * Use of this source code is governed by a BSD-style license 7701 * that can be found in the LICENSE file in the root of the source 7702 * tree. An additional intellectual property rights grant can be 7703 * found in the file PATENTS. All contributing project authors may 7704 * be found in the AUTHORS file in the root of the source tree. 7705 */ 7706 #ifndef DIXIE_LOOPFILTER_H 7707 #define DIXIE_LOOPFILTER_H 7709 void 7710 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7711 unsigned int row, 7712 unsigned int start_col, 7713 unsigned int num_cols); 7715 #endif 7717 ---- End code block ---------------------------------------- 7719 20.8. idct_add.c 7721 ---- Begin code block -------------------------------------- 7723 /* 7724 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7725 * 7726 * Use of this source code is governed by a BSD-style license 7727 * that can be found in the LICENSE file in the root of the source 7728 * tree. An additional intellectual property rights grant can be 7729 * found in the file PATENTS. All contributing project authors may 7730 * be found in the AUTHORS file in the root of the source tree. 7731 */ 7732 #include "dixie.h" 7733 #include "idct_add.h" 7734 #include 7736 void 7737 vp8_dixie_walsh(const short *input, short *output) 7738 { 7739 int i; 7740 int a1, b1, c1, d1; 7741 int a2, b2, c2, d2; 7742 const short *ip = input; 7743 short *op = output; 7745 for (i = 0; i < 4; i++) 7746 { 7747 a1 = ip[0] + ip[12]; 7748 b1 = ip[4] + ip[8]; 7749 c1 = ip[4] - ip[8]; 7750 d1 = ip[0] - ip[12]; 7752 op[0] = a1 + b1; 7753 op[4] = c1 + d1; 7754 op[8] = a1 - b1; 7755 op[12] = d1 - c1; 7756 ip++; 7757 op++; 7758 } 7760 ip = output; 7761 op = output; 7763 for (i = 0; i < 4; i++) 7764 { 7765 a1 = ip[0] + ip[3]; 7766 b1 = ip[1] + ip[2]; 7767 c1 = ip[1] - ip[2]; 7768 d1 = ip[0] - ip[3]; 7770 a2 = a1 + b1; 7771 b2 = c1 + d1; 7772 c2 = a1 - b1; 7773 d2 = d1 - c1; 7775 op[0] = (a2 + 3) >> 3; 7776 op[1] = (b2 + 3) >> 3; 7777 op[2] = (c2 + 3) >> 3; 7778 op[3] = (d2 + 3) >> 3; 7780 ip += 4; 7781 op += 4; 7782 } 7783 } 7785 #define cospi8sqrt2minus1 20091 7786 #define sinpi8sqrt2 35468 7787 #define rounding 0 7788 static void 7789 idct_columns(const short *input, short *output) 7790 { 7791 int i; 7792 int a1, b1, c1, d1; 7794 const short *ip = input; 7795 short *op = output; 7796 int temp1, temp2; 7797 int shortpitch = 4; 7799 for (i = 0; i < 4; i++) 7800 { 7801 a1 = ip[0] + ip[8]; 7802 b1 = ip[0] - ip[8]; 7804 temp1 = (ip[4] * sinpi8sqrt2 + rounding) >> 16; 7805 temp2 = ip[12] + 7806 ((ip[12] * cospi8sqrt2minus1 + rounding) >> 16); 7807 c1 = temp1 - temp2; 7809 temp1 = ip[4] + 7810 ((ip[4] * cospi8sqrt2minus1 + rounding) >> 16); 7811 temp2 = (ip[12] * sinpi8sqrt2 + rounding) >> 16; 7812 d1 = temp1 + temp2; 7814 op[shortpitch*0] = a1 + d1; 7815 op[shortpitch*3] = a1 - d1; 7817 op[shortpitch*1] = b1 + c1; 7818 op[shortpitch*2] = b1 - c1; 7820 ip++; 7821 op++; 7822 } 7823 } 7825 void 7826 vp8_dixie_idct_add(unsigned char *recon, 7827 const unsigned char *predict, 7828 int stride, 7829 const short *coeffs) 7830 { 7831 int i; 7832 int a1, b1, c1, d1, temp1, temp2; 7833 short tmp[16]; 7834 idct_columns(coeffs, tmp); 7835 coeffs = tmp; 7837 for (i = 0; i < 4; i++) 7838 { 7839 a1 = coeffs[0] + coeffs[2]; 7840 b1 = coeffs[0] - coeffs[2]; 7842 temp1 = (coeffs[1] * sinpi8sqrt2 + rounding) >> 16; 7843 temp2 = coeffs[3] + 7844 ((coeffs[3] * cospi8sqrt2minus1 + rounding) >> 16); 7845 c1 = temp1 - temp2; 7847 temp1 = coeffs[1] + 7848 ((coeffs[1] * cospi8sqrt2minus1 + rounding) >> 16); 7849 temp2 = (coeffs[3] * sinpi8sqrt2 + rounding) >> 16; 7850 d1 = temp1 + temp2; 7852 recon[0] = CLAMP_255(predict[0] + ((a1 + d1 + 4) >> 3)); 7853 recon[3] = CLAMP_255(predict[3] + ((a1 - d1 + 4) >> 3)); 7854 recon[1] = CLAMP_255(predict[1] + ((b1 + c1 + 4) >> 3)); 7855 recon[2] = CLAMP_255(predict[2] + ((b1 - c1 + 4) >> 3)); 7857 coeffs += 4; 7858 recon += stride; 7859 predict += stride; 7860 } 7861 } 7863 ---- End code block ---------------------------------------- 7865 20.9. idct_add.h 7867 ---- Begin code block -------------------------------------- 7869 /* 7870 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7871 * 7872 * Use of this source code is governed by a BSD-style license 7873 * that can be found in the LICENSE file in the root of the source 7874 * tree. An additional intellectual property rights grant can be 7875 * found in the file PATENTS. All contributing project authors may 7876 * be found in the AUTHORS file in the root of the source tree. 7877 */ 7878 #ifndef IDCT_ADD_H 7879 #define IDCT_ADD_H 7881 void 7882 vp8_dixie_idct_add_init(struct vp8_decoder_ctx *ctx); 7884 void 7885 vp8_dixie_idct_add(unsigned char *recon, 7886 const unsigned char *predict, 7887 int stride, 7888 const short *coeffs); 7890 void 7891 vp8_dixie_walsh(const short *in, short *out); 7893 void 7894 vp8_dixie_idct_add_process_row(struct vp8_decoder_ctx *ctx, 7895 short *coeffs, 7896 unsigned int row, 7897 unsigned int start_col, 7898 unsigned int num_cols); 7900 #endif 7902 ---- End code block ---------------------------------------- 7904 20.10. mem.h 7906 ---- Begin code block -------------------------------------- 7908 /* 7909 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7910 * 7911 * Use of this source code is governed by a BSD-style license 7912 * that can be found in the LICENSE file in the root of the source 7913 * tree. An additional intellectual property rights grant can be 7914 * found in the file PATENTS. All contributing project authors may 7915 * be found in the AUTHORS file in the root of the source tree. 7916 */ 7918 #ifndef VPX_PORTS_MEM_H 7919 #define VPX_PORTS_MEM_H 7920 #include "vpx_config.h" 7921 #include "vpx_integer.h" 7923 #if defined(__GNUC__) && __GNUC__ 7924 #define DECLARE_ALIGNED(n,typ,val) typ val __attribute__ \ 7925 ((aligned (n))) 7926 #elif defined(_MSC_VER) 7927 #define DECLARE_ALIGNED(n,typ,val) __declspec(align(n)) typ val 7928 #else 7929 #warning No alignment directives known for this compiler. 7930 #define DECLARE_ALIGNED(n,typ,val) typ val 7931 #endif 7932 #endif 7934 /* Declare an aligned array on the stack, for situations where the 7935 * stack pointer may not have the alignment we expect. Creates an 7936 * array with a modified name, then defines val to be a pointer, and 7937 * aligns that pointer within the array. 7938 */ 7939 #define DECLARE_ALIGNED_ARRAY(a,typ,val,n)\ 7940 typ val##_[(n)+(a)/sizeof(typ)+1];\ 7941 typ *val = (typ*)((((intptr_t)val##_)+(a)-1)&((intptr_t)-(a))) 7943 /* Indicates that the usage of the specified variable has been 7944 * audited to assure that it's safe to use uninitialized. Silences 7945 * 'may be used uninitialized' warnings on gcc. 7946 */ 7947 #if defined(__GNUC__) && __GNUC__ 7948 #define UNINITIALIZED_IS_SAFE(x) x=x 7949 #else 7950 #define UNINITIALIZED_IS_SAFE(x) x 7951 #endif 7952 ---- End code block ---------------------------------------- 7954 20.11. modemv.c 7956 ---- Begin code block -------------------------------------- 7958 /* 7959 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7960 * 7961 * Use of this source code is governed by a BSD-style license 7962 * that can be found in the LICENSE file in the root of the source 7963 * tree. An additional intellectual property rights grant can be 7964 * found in the file PATENTS. All contributing project authors may 7965 * be found in the AUTHORS file in the root of the source tree. 7966 */ 7967 #include "dixie.h" 7968 #include "modemv_data.h" 7969 #include 7970 #include 7972 struct mv_clamp_rect 7973 { 7974 int to_left, to_right, to_top, to_bottom; 7975 }; 7977 static union mv 7978 clamp_mv(union mv raw, const struct mv_clamp_rect *bounds) 7979 { 7980 union mv newmv; 7982 newmv.d.x = (raw.d.x < bounds->to_left) 7983 ? bounds->to_left : raw.d.x; 7984 newmv.d.x = (raw.d.x > bounds->to_right) 7985 ? bounds->to_right : newmv.d.x; 7986 newmv.d.y = (raw.d.y < bounds->to_top) 7987 ? bounds->to_top : raw.d.y; 7988 newmv.d.y = (raw.d.y > bounds->to_bottom) 7989 ? bounds->to_bottom : newmv.d.y; 7990 return newmv; 7991 } 7993 static int 7994 read_segment_id(struct bool_decoder *bool, 7995 struct vp8_segment_hdr *seg) 7996 { 7997 return bool_get(bool, seg->tree_probs[0]) 7998 ? 2 + bool_get(bool, seg->tree_probs[2]) 7999 : bool_get(bool, seg->tree_probs[1]); 8000 } 8002 static enum prediction_mode 8003 above_block_mode(const struct mb_info *this, 8004 const struct mb_info *above, 8005 unsigned int b) 8006 { 8007 if (b < 4) 8008 { 8009 switch (above->base.y_mode) 8010 { 8011 case DC_PRED: 8012 return B_DC_PRED; 8013 case V_PRED: 8014 return B_VE_PRED; 8015 case H_PRED: 8016 return B_HE_PRED; 8017 case TM_PRED: 8018 return B_TM_PRED; 8019 case B_PRED: 8020 return above->split.modes[b+12]; 8021 default: 8022 assert(0); 8023 } 8024 } 8026 return this->split.modes[b-4]; 8027 } 8029 static enum prediction_mode 8030 left_block_mode(const struct mb_info *this, 8031 const struct mb_info *left, 8032 unsigned int b) 8033 { 8034 if (!(b & 3)) 8035 { 8036 switch (left->base.y_mode) 8037 { 8038 case DC_PRED: 8039 return B_DC_PRED; 8040 case V_PRED: 8041 return B_VE_PRED; 8042 case H_PRED: 8044 return B_HE_PRED; 8045 case TM_PRED: 8046 return B_TM_PRED; 8047 case B_PRED: 8048 return left->split.modes[b+3]; 8049 default: 8050 assert(0); 8051 } 8052 } 8054 return this->split.modes[b-1]; 8055 } 8057 static void 8058 decode_kf_mb_mode(struct mb_info *this, 8059 struct mb_info *left, 8060 struct mb_info *above, 8061 struct bool_decoder *bool) 8062 { 8063 int y_mode, uv_mode; 8065 y_mode = bool_read_tree(bool, kf_y_mode_tree, kf_y_mode_probs); 8067 if (y_mode == B_PRED) 8068 { 8069 unsigned int i; 8071 for (i = 0; i < 16; i++) 8072 { 8073 enum prediction_mode a = above_block_mode(this, above, 8074 i); 8075 enum prediction_mode l = left_block_mode(this, left, i); 8076 enum prediction_mode b; 8078 b = bool_read_tree(bool, b_mode_tree, 8079 kf_b_mode_probs[a][l]); 8080 this->split.modes[i] = b; 8081 } 8082 } 8084 uv_mode = bool_read_tree(bool, uv_mode_tree, kf_uv_mode_probs); 8086 this->base.y_mode = y_mode; 8087 this->base.uv_mode = uv_mode; 8088 this->base.mv.raw = 0; 8089 this->base.ref_frame = 0; 8090 } 8091 static void 8092 decode_intra_mb_mode(struct mb_info *this, 8093 struct vp8_entropy_hdr *hdr, 8094 struct bool_decoder *bool) 8095 { 8096 /* Like decode_kf_mb_mode, but with probabilities transmitted in 8097 * the bitstream and no context on the above/left block mode. 8098 */ 8099 int y_mode, uv_mode; 8101 y_mode = bool_read_tree(bool, y_mode_tree, hdr->y_mode_probs); 8103 if (y_mode == B_PRED) 8104 { 8105 unsigned int i; 8107 for (i = 0; i < 16; i++) 8108 { 8109 enum prediction_mode b; 8111 b = bool_read_tree(bool, b_mode_tree, 8112 default_b_mode_probs); 8113 this->split.modes[i] = b; 8114 } 8115 } 8117 uv_mode = bool_read_tree(bool, uv_mode_tree, hdr->uv_mode_probs); 8119 this->base.y_mode = y_mode; 8120 this->base.uv_mode = uv_mode; 8121 this->base.mv.raw = 0; 8122 this->base.ref_frame = CURRENT_FRAME; 8123 } 8125 static int 8126 read_mv_component(struct bool_decoder *bool, 8127 const unsigned char mvc[MV_PROB_CNT]) 8128 { 8129 enum {IS_SHORT, SIGN, SHORT, BITS = SHORT + 8 - 1, 8130 LONG_WIDTH = 10}; 8131 int x = 0; 8133 if (bool_get(bool, mvc[IS_SHORT])) /* Large */ 8134 { 8135 int i = 0; 8137 for (i = 0; i < 3; i++) 8138 x += bool_get(bool, mvc[BITS + i]) << i; 8140 /* Skip bit 3, which is sometimes implicit */ 8141 for (i = LONG_WIDTH - 1; i > 3; i--) 8142 x += bool_get(bool, mvc[BITS + i]) << i; 8144 if (!(x & 0xFFF0) || bool_get(bool, mvc[BITS + 3])) 8145 x += 8; 8146 } 8147 else /* small */ 8148 x = bool_read_tree(bool, small_mv_tree, mvc + SHORT); 8150 if (x && bool_get(bool, mvc[SIGN])) 8151 x = -x; 8153 return x << 1; 8154 } 8156 static mv_t 8157 above_block_mv(const struct mb_info *this, 8158 const struct mb_info *above, 8159 unsigned int b) 8160 { 8161 if (b < 4) 8162 { 8163 if (above->base.y_mode == SPLITMV) 8164 return above->split.mvs[b+12]; 8166 return above->base.mv; 8167 } 8169 return this->split.mvs[b-4]; 8170 } 8172 static mv_t 8173 left_block_mv(const struct mb_info *this, 8174 const struct mb_info *left, 8175 unsigned int b) 8176 { 8177 if (!(b & 3)) 8178 { 8179 if (left->base.y_mode == SPLITMV) 8180 return left->split.mvs[b+3]; 8182 return left->base.mv; 8183 } 8184 return this->split.mvs[b-1]; 8185 } 8187 static enum prediction_mode 8188 submv_ref(struct bool_decoder *bool, union mv l, union mv a) 8189 { 8190 enum subblock_mv_ref 8191 { 8192 SUBMVREF_NORMAL, 8193 SUBMVREF_LEFT_ZED, 8194 SUBMVREF_ABOVE_ZED, 8195 SUBMVREF_LEFT_ABOVE_SAME, 8196 SUBMVREF_LEFT_ABOVE_ZED 8197 }; 8199 int lez = !(l.raw); 8200 int aez = !(a.raw); 8201 int lea = l.raw == a.raw; 8202 enum subblock_mv_ref ctx = SUBMVREF_NORMAL; 8204 if (lea && lez) 8205 ctx = SUBMVREF_LEFT_ABOVE_ZED; 8206 else if (lea) 8207 ctx = SUBMVREF_LEFT_ABOVE_SAME; 8208 else if (aez) 8209 ctx = SUBMVREF_ABOVE_ZED; 8210 else if (lez) 8211 ctx = SUBMVREF_LEFT_ZED; 8213 return bool_read_tree(bool, submv_ref_tree, 8214 submv_ref_probs2[ctx]); 8215 } 8217 static void 8218 read_mv(struct bool_decoder *bool, 8219 union mv *mv, 8220 mv_component_probs_t mvc[2]) 8221 { 8222 mv->d.y = read_mv_component(bool, mvc[0]); 8223 mv->d.x = read_mv_component(bool, mvc[1]); 8224 } 8226 static void 8227 mv_bias(const struct mb_info *mb, 8228 const unsigned int sign_bias[3], 8229 enum reference_frame ref_frame, 8230 union mv *mv) 8231 { 8232 if (sign_bias[mb->base.ref_frame] ^ sign_bias[ref_frame]) 8233 { 8234 mv->d.x *= -1; 8235 mv->d.y *= -1; 8236 } 8237 } 8239 enum near_mv_v 8240 { 8241 CNT_BEST = 0, 8242 CNT_ZEROZERO = 0, 8243 CNT_NEAREST, 8244 CNT_NEAR, 8245 CNT_SPLITMV 8246 }; 8248 static void 8249 find_near_mvs(const struct mb_info *this, 8250 const struct mb_info *left, 8251 const struct mb_info *above, 8252 const unsigned int sign_bias[3], 8253 union mv near_mvs[4], 8254 int cnt[4]) 8255 { 8256 const struct mb_info *aboveleft = above - 1; 8257 union mv *mv = near_mvs; 8258 int *cntx = cnt; 8260 /* Zero accumulators */ 8261 mv[0].raw = mv[1].raw = mv[2].raw = 0; 8262 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 8264 /* Process above */ 8265 if (above->base.ref_frame != CURRENT_FRAME) 8266 { 8267 if (above->base.mv.raw) 8268 { 8269 (++mv)->raw = above->base.mv.raw; 8270 mv_bias(above, sign_bias, this->base.ref_frame, mv); 8271 ++cntx; 8272 } 8274 *cntx += 2; 8276 } 8278 /* Process left */ 8279 if (left->base.ref_frame != CURRENT_FRAME) 8280 { 8281 if (left->base.mv.raw) 8282 { 8283 union mv this_mv; 8285 this_mv.raw = left->base.mv.raw; 8286 mv_bias(left, sign_bias, this->base.ref_frame, &this_mv); 8288 if (this_mv.raw != mv->raw) 8289 { 8290 (++mv)->raw = this_mv.raw; 8291 ++cntx; 8292 } 8294 *cntx += 2; 8295 } 8296 else 8297 cnt[CNT_ZEROZERO] += 2; 8298 } 8300 /* Process above left */ 8301 if (aboveleft->base.ref_frame != CURRENT_FRAME) 8302 { 8303 if (aboveleft->base.mv.raw) 8304 { 8305 union mv this_mv; 8307 this_mv.raw = aboveleft->base.mv.raw; 8308 mv_bias(aboveleft, sign_bias, this->base.ref_frame, 8309 &this_mv); 8311 if (this_mv.raw != mv->raw) 8312 { 8313 (++mv)->raw = this_mv.raw; 8314 ++cntx; 8315 } 8317 *cntx += 1; 8318 } 8319 else 8320 cnt[CNT_ZEROZERO] += 1; 8321 } 8323 /* If we have three distinct MV's ... */ 8324 if (cnt[CNT_SPLITMV]) 8325 { 8326 /* See if above-left MV can be merged with NEAREST */ 8327 if (mv->raw == near_mvs[CNT_NEAREST].raw) 8328 cnt[CNT_NEAREST] += 1; 8329 } 8331 cnt[CNT_SPLITMV] = ((above->base.y_mode == SPLITMV) 8332 + (left->base.y_mode == SPLITMV)) * 2 8333 + (aboveleft->base.y_mode == SPLITMV); 8335 /* Swap near and nearest if necessary */ 8336 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) 8337 { 8338 int tmp; 8339 tmp = cnt[CNT_NEAREST]; 8340 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 8341 cnt[CNT_NEAR] = tmp; 8342 tmp = near_mvs[CNT_NEAREST].raw; 8343 near_mvs[CNT_NEAREST].raw = near_mvs[CNT_NEAR].raw; 8344 near_mvs[CNT_NEAR].raw = tmp; 8345 } 8347 /* Use near_mvs[CNT_BEST] to store the "best" MV. Note that this 8348 * storage shares the same address as near_mvs[CNT_ZEROZERO]. 8349 */ 8350 if (cnt[CNT_NEAREST] >= cnt[CNT_BEST]) 8351 near_mvs[CNT_BEST] = near_mvs[CNT_NEAREST]; 8352 } 8354 static void 8355 decode_split_mv(struct mb_info *this, 8356 const struct mb_info *left, 8357 const struct mb_info *above, 8358 struct vp8_entropy_hdr *hdr, 8359 union mv *best_mv, 8360 struct bool_decoder *bool) 8361 { 8362 const int *partition; 8363 int j, k, mask, partition_id; 8365 partition_id = bool_read_tree(bool, split_mv_tree, 8366 split_mv_probs); 8367 partition = mv_partitions[partition_id]; 8368 this->base.partitioning = partition_id; 8370 for (j = 0, mask = 0; mask < 65535; j++) 8371 { 8372 union mv mv, left_mv, above_mv; 8373 enum prediction_mode subblock_mode; 8375 /* Find the first subblock in this partition. */ 8376 for (k = 0; j != partition[k]; k++); 8378 /* Decode the next MV */ 8379 left_mv = left_block_mv(this, left, k); 8380 above_mv = above_block_mv(this, above, k); 8381 subblock_mode = submv_ref(bool, left_mv, above_mv); 8383 switch (subblock_mode) 8384 { 8385 case LEFT4X4: 8386 mv = left_mv; 8387 break; 8388 case ABOVE4X4: 8389 mv = above_mv; 8390 break; 8391 case ZERO4X4: 8392 mv.raw = 0; 8393 break; 8394 case NEW4X4: 8395 read_mv(bool, &mv, hdr->mv_probs); 8396 mv.d.x += best_mv->d.x; 8397 mv.d.y += best_mv->d.y; 8398 break; 8399 default: 8400 assert(0); 8401 } 8403 /* Fill the MV's for this partition */ 8404 for (; k < 16; k++) 8405 if (j == partition[k]) 8406 { 8407 this->split.mvs[k] = mv; 8408 mask |= 1 << k; 8409 } 8410 } 8411 } 8413 static int 8414 need_mc_border(union mv mv, int l, int t, int b_w, int w, int h) 8415 { 8416 int b, r; 8417 /* Get distance to edge for top-left pixel */ 8418 l += (mv.d.x >> 3); 8419 t += (mv.d.y >> 3); 8421 /* Get distance to edge for bottom-right pixel */ 8422 r = w - (l + b_w); 8423 b = h - (t + b_w); 8425 return (l >> 1 < 2 || r >> 1 < 3 || t >> 1 < 2 || b >> 1 < 3); 8426 } 8428 static void 8429 decode_mvs(struct vp8_decoder_ctx *ctx, 8430 struct mb_info *this, 8431 const struct mb_info *left, 8432 const struct mb_info *above, 8433 const struct mv_clamp_rect *bounds, 8434 struct bool_decoder *bool) 8435 { 8436 struct vp8_entropy_hdr *hdr = &ctx->entropy_hdr; 8437 union mv near_mvs[4]; 8438 union mv clamped_best_mv; 8439 int mv_cnts[4]; 8440 unsigned char probs[4]; 8441 enum {BEST, NEAREST, NEAR}; 8442 int x, y, w, h, b; 8444 this->base.ref_frame = bool_get(bool, hdr->prob_last) 8445 ? 2 + bool_get(bool, hdr->prob_gf) 8446 : 1; 8448 find_near_mvs(this, this - 1, above, 8449 ctx->reference_hdr.sign_bias, near_mvs, mv_cnts); 8450 probs[0] = mv_counts_to_probs[mv_cnts[0]][0]; 8451 probs[1] = mv_counts_to_probs[mv_cnts[1]][1]; 8452 probs[2] = mv_counts_to_probs[mv_cnts[2]][2]; 8453 probs[3] = mv_counts_to_probs[mv_cnts[3]][3]; 8455 this->base.y_mode = bool_read_tree(bool, mv_ref_tree, probs); 8456 this->base.uv_mode = this->base.y_mode; 8458 this->base.need_mc_border = 0; 8459 x = (-bounds->to_left - 128) >> 3; 8460 y = (-bounds->to_top - 128) >> 3; 8461 w = ctx->mb_cols * 16; 8462 h = ctx->mb_rows * 16; 8464 switch (this->base.y_mode) 8465 { 8466 case NEARESTMV: 8467 this->base.mv = clamp_mv(near_mvs[NEAREST], bounds); 8468 break; 8469 case NEARMV: 8470 this->base.mv = clamp_mv(near_mvs[NEAR], bounds); 8471 break; 8472 case ZEROMV: 8473 this->base.mv.raw = 0; 8474 return; //skip need_mc_border check 8475 case NEWMV: 8476 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8477 read_mv(bool, &this->base.mv, hdr->mv_probs); 8478 this->base.mv.d.x += clamped_best_mv.d.x; 8479 this->base.mv.d.y += clamped_best_mv.d.y; 8480 break; 8481 case SPLITMV: 8482 { 8483 union mv chroma_mv[4] = {{{0}}}; 8485 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8486 decode_split_mv(this, left, above, hdr, &clamped_best_mv, 8487 bool); 8488 this->base.mv = this->split.mvs[15]; 8490 for (b = 0; b < 16; b++) 8491 { 8492 chroma_mv[(b>>1&1) + (b>>2&2)].d.x += 8493 this->split.mvs[b].d.x; 8494 chroma_mv[(b>>1&1) + (b>>2&2)].d.y += 8495 this->split.mvs[b].d.y; 8497 if (need_mc_border(this->split.mvs[b], 8498 x + (b & 3) * 4, y + (b & ~3), 4, w, h)) 8499 { 8500 this->base.need_mc_border = 1; 8501 break; 8502 } 8503 } 8505 for (b = 0; b < 4; b++) 8506 { 8507 chroma_mv[b].d.x += 4 + 8 * (chroma_mv[b].d.x >> 31); 8508 chroma_mv[b].d.y += 4 + 8 * (chroma_mv[b].d.y >> 31); 8509 chroma_mv[b].d.x /= 4; 8510 chroma_mv[b].d.y /= 4; 8512 //note we're passing in non-subsampled coordinates 8513 if (need_mc_border(chroma_mv[b], 8514 x + (b & 1) * 8, y + (b >> 1) * 8, 16, w, h)) 8515 { 8516 this->base.need_mc_border = 1; 8517 break; 8518 } 8519 } 8521 return; //skip need_mc_border check 8522 } 8523 default: 8524 assert(0); 8525 } 8527 if (need_mc_border(this->base.mv, x, y, 16, w, h)) 8528 this->base.need_mc_border = 1; 8529 } 8531 void 8532 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8533 struct bool_decoder *bool, 8534 int row, 8535 int start_col, 8536 int num_cols) 8537 { 8538 struct mb_info *above, *this; 8539 unsigned int col; 8540 struct mv_clamp_rect bounds; 8542 this = ctx->mb_info_rows[row] + start_col; 8543 above = ctx->mb_info_rows[row - 1] + start_col; 8545 /* Calculate the eighth-pel MV bounds using a 1 MB border. */ 8546 bounds.to_left = -((start_col + 1) << 7); 8547 bounds.to_right = (ctx->mb_cols - start_col) << 7; 8548 bounds.to_top = -((row + 1) << 7); 8549 bounds.to_bottom = (ctx->mb_rows - row) << 7; 8551 for (col = start_col; col < start_col + num_cols; col++) 8552 { 8553 if (ctx->segment_hdr.update_map) 8554 this->base.segment_id = read_segment_id(bool, 8555 &ctx->segment_hdr); 8557 if (ctx->entropy_hdr.coeff_skip_enabled) 8558 this->base.skip_coeff = bool_get(bool, 8559 ctx->entropy_hdr.coeff_skip_prob); 8561 if (ctx->frame_hdr.is_keyframe) 8562 { 8563 if (!ctx->segment_hdr.update_map) 8564 this->base.segment_id = 0; 8566 decode_kf_mb_mode(this, this - 1, above, bool); 8567 } 8568 else 8569 { 8570 if (bool_get(bool, ctx->entropy_hdr.prob_inter)) 8571 decode_mvs(ctx, this, this - 1, above, &bounds, 8572 bool); 8573 else 8574 decode_intra_mb_mode(this, &ctx->entropy_hdr, bool); 8576 bounds.to_left -= 16 << 3; 8577 bounds.to_right -= 16 << 3; 8578 } 8580 /* Advance to next mb */ 8581 this++; 8582 above++; 8583 } 8584 } 8586 void 8587 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx) 8588 { 8589 unsigned int mbi_w, mbi_h, i; 8590 struct mb_info *mbi; 8592 mbi_w = ctx->mb_cols + 1; /* For left border col */ 8593 mbi_h = ctx->mb_rows + 1; /* For above border row */ 8595 if (ctx->frame_hdr.frame_size_updated) 8596 { 8597 free(ctx->mb_info_storage); 8598 ctx->mb_info_storage = NULL; 8599 free(ctx->mb_info_rows_storage); 8600 ctx->mb_info_rows_storage = NULL; 8601 } 8603 if (!ctx->mb_info_storage) 8604 ctx->mb_info_storage = calloc(mbi_w * mbi_h, 8605 sizeof(*ctx->mb_info_storage)); 8607 if (!ctx->mb_info_rows_storage) 8608 ctx->mb_info_rows_storage = calloc(mbi_h, 8609 sizeof(*ctx->mb_info_rows_storage)); 8611 /* Set up row pointers */ 8612 mbi = ctx->mb_info_storage + 1; 8614 for (i = 0; i < mbi_h; i++) 8615 { 8616 ctx->mb_info_rows_storage[i] = mbi; 8617 mbi += mbi_w; 8618 } 8620 ctx->mb_info_rows = ctx->mb_info_rows_storage + 1; 8621 } 8623 void 8624 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx) 8625 { 8626 free(ctx->mb_info_storage); 8627 ctx->mb_info_storage = NULL; 8628 free(ctx->mb_info_rows_storage); 8629 ctx->mb_info_rows_storage = NULL; 8630 } 8632 ---- End code block ---------------------------------------- 8634 20.12. modemv.h 8636 ---- Begin code block -------------------------------------- 8638 /* 8639 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8640 * 8641 * Use of this source code is governed by a BSD-style license 8642 * that can be found in the LICENSE file in the root of the source 8643 * tree. An additional intellectual property rights grant can be 8644 * found in the file PATENTS. All contributing project authors may 8645 * be found in the AUTHORS file in the root of the source tree. 8646 */ 8647 #ifndef MODEMV_H 8648 #define MODEMV_H 8650 void 8651 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx); 8653 void 8654 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx); 8656 void 8657 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8658 struct bool_decoder *bool, 8659 int row, 8660 int start_col, 8661 int num_cols); 8663 #endif 8665 ---- End code block ---------------------------------------- 8667 20.13. modemv_data.h 8669 ---- Begin code block -------------------------------------- 8671 static const unsigned char kf_y_mode_probs[] = { 145, 156, 163, 128}; 8672 static const unsigned char kf_uv_mode_probs[] = { 142, 114, 183}; 8673 static const unsigned char kf_b_mode_probs[10][10][9] = 8674 { 8675 { /* above mode 0 */ 8676 { /* left mode 0 */ 231, 120, 48, 89, 115, 113, 120, 152, 112}, 8677 { /* left mode 1 */ 152, 179, 64, 126, 170, 118, 46, 70, 95}, 8678 { /* left mode 2 */ 175, 69, 143, 80, 85, 82, 72, 155, 103}, 8679 { /* left mode 3 */ 56, 58, 10, 171, 218, 189, 17, 13, 152}, 8680 { /* left mode 4 */ 144, 71, 10, 38, 171, 213, 144, 34, 26}, 8681 { /* left mode 5 */ 114, 26, 17, 163, 44, 195, 21, 10, 173}, 8682 { /* left mode 6 */ 121, 24, 80, 195, 26, 62, 44, 64, 85}, 8683 { /* left mode 7 */ 170, 46, 55, 19, 136, 160, 33, 206, 71}, 8684 { /* left mode 8 */ 63, 20, 8, 114, 114, 208, 12, 9, 226}, 8685 { /* left mode 9 */ 81, 40, 11, 96, 182, 84, 29, 16, 36} 8686 }, 8687 { /* above mode 1 */ 8688 { /* left mode 0 */ 134, 183, 89, 137, 98, 101, 106, 165, 148}, 8689 { /* left mode 1 */ 72, 187, 100, 130, 157, 111, 32, 75, 80}, 8690 { /* left mode 2 */ 66, 102, 167, 99, 74, 62, 40, 234, 128}, 8691 { /* left mode 3 */ 41, 53, 9, 178, 241, 141, 26, 8, 107}, 8692 { /* left mode 4 */ 104, 79, 12, 27, 217, 255, 87, 17, 7}, 8693 { /* left mode 5 */ 74, 43, 26, 146, 73, 166, 49, 23, 157}, 8694 { /* left mode 6 */ 65, 38, 105, 160, 51, 52, 31, 115, 128}, 8695 { /* left mode 7 */ 87, 68, 71, 44, 114, 51, 15, 186, 23}, 8696 { /* left mode 8 */ 47, 41, 14, 110, 182, 183, 21, 17, 194}, 8697 { /* left mode 9 */ 66, 45, 25, 102, 197, 189, 23, 18, 22} 8698 }, 8699 { /* above mode 2 */ 8700 { /* left mode 0 */ 88, 88, 147, 150, 42, 46, 45, 196, 205}, 8701 { /* left mode 1 */ 43, 97, 183, 117, 85, 38, 35, 179, 61}, 8702 { /* left mode 2 */ 39, 53, 200, 87, 26, 21, 43, 232, 171}, 8703 { /* left mode 3 */ 56, 34, 51, 104, 114, 102, 29, 93, 77}, 8704 { /* left mode 4 */ 107, 54, 32, 26, 51, 1, 81, 43, 31}, 8705 { /* left mode 5 */ 39, 28, 85, 171, 58, 165, 90, 98, 64}, 8706 { /* left mode 6 */ 34, 22, 116, 206, 23, 34, 43, 166, 73}, 8707 { /* left mode 7 */ 68, 25, 106, 22, 64, 171, 36, 225, 114}, 8708 { /* left mode 8 */ 34, 19, 21, 102, 132, 188, 16, 76, 124}, 8709 { /* left mode 9 */ 62, 18, 78, 95, 85, 57, 50, 48, 51} 8710 }, 8711 { /* above mode 3 */ 8712 { /* left mode 0 */ 193, 101, 35, 159, 215, 111, 89, 46, 111}, 8713 { /* left mode 1 */ 60, 148, 31, 172, 219, 228, 21, 18, 111}, 8714 { /* left mode 2 */ 112, 113, 77, 85, 179, 255, 38, 120, 114}, 8715 { /* left mode 3 */ 40, 42, 1, 196, 245, 209, 10, 25, 109}, 8716 { /* left mode 4 */ 100, 80, 8, 43, 154, 1, 51, 26, 71}, 8717 { /* left mode 5 */ 88, 43, 29, 140, 166, 213, 37, 43, 154}, 8718 { /* left mode 6 */ 61, 63, 30, 155, 67, 45, 68, 1, 209}, 8719 { /* left mode 7 */ 142, 78, 78, 16, 255, 128, 34, 197, 171}, 8720 { /* left mode 8 */ 41, 40, 5, 102, 211, 183, 4, 1, 221}, 8721 { /* left mode 9 */ 51, 50, 17, 168, 209, 192, 23, 25, 82} 8722 }, 8723 { /* above mode 4 */ 8724 { /* left mode 0 */ 125, 98, 42, 88, 104, 85, 117, 175, 82}, 8725 { /* left mode 1 */ 95, 84, 53, 89, 128, 100, 113, 101, 45}, 8726 { /* left mode 2 */ 75, 79, 123, 47, 51, 128, 81, 171, 1}, 8727 { /* left mode 3 */ 57, 17, 5, 71, 102, 57, 53, 41, 49}, 8728 { /* left mode 4 */ 115, 21, 2, 10, 102, 255, 166, 23, 6}, 8729 { /* left mode 5 */ 38, 33, 13, 121, 57, 73, 26, 1, 85}, 8730 { /* left mode 6 */ 41, 10, 67, 138, 77, 110, 90, 47, 114}, 8731 { /* left mode 7 */ 101, 29, 16, 10, 85, 128, 101, 196, 26}, 8732 { /* left mode 8 */ 57, 18, 10, 102, 102, 213, 34, 20, 43}, 8733 { /* left mode 9 */ 117, 20, 15, 36, 163, 128, 68, 1, 26} 8734 }, 8735 { /* above mode 5 */ 8736 { /* left mode 0 */ 138, 31, 36, 171, 27, 166, 38, 44, 229}, 8737 { /* left mode 1 */ 67, 87, 58, 169, 82, 115, 26, 59, 179}, 8738 { /* left mode 2 */ 63, 59, 90, 180, 59, 166, 93, 73, 154}, 8739 { /* left mode 3 */ 40, 40, 21, 116, 143, 209, 34, 39, 175}, 8740 { /* left mode 4 */ 57, 46, 22, 24, 128, 1, 54, 17, 37}, 8741 { /* left mode 5 */ 47, 15, 16, 183, 34, 223, 49, 45, 183}, 8742 { /* left mode 6 */ 46, 17, 33, 183, 6, 98, 15, 32, 183}, 8743 { /* left mode 7 */ 65, 32, 73, 115, 28, 128, 23, 128, 205}, 8744 { /* left mode 8 */ 40, 3, 9, 115, 51, 192, 18, 6, 223}, 8745 { /* left mode 9 */ 87, 37, 9, 115, 59, 77, 64, 21, 47} 8746 }, 8747 { /* above mode 6 */ 8748 { /* left mode 0 */ 104, 55, 44, 218, 9, 54, 53, 130, 226}, 8749 { /* left mode 1 */ 64, 90, 70, 205, 40, 41, 23, 26, 57}, 8750 { /* left mode 2 */ 54, 57, 112, 184, 5, 41, 38, 166, 213}, 8751 { /* left mode 3 */ 30, 34, 26, 133, 152, 116, 10, 32, 134}, 8752 { /* left mode 4 */ 75, 32, 12, 51, 192, 255, 160, 43, 51}, 8753 { /* left mode 5 */ 39, 19, 53, 221, 26, 114, 32, 73, 255}, 8754 { /* left mode 6 */ 31, 9, 65, 234, 2, 15, 1, 118, 73}, 8755 { /* left mode 7 */ 88, 31, 35, 67, 102, 85, 55, 186, 85}, 8756 { /* left mode 8 */ 56, 21, 23, 111, 59, 205, 45, 37, 192}, 8757 { /* left mode 9 */ 55, 38, 70, 124, 73, 102, 1, 34, 98} 8758 }, 8759 { /* above mode 7 */ 8760 { /* left mode 0 */ 102, 61, 71, 37, 34, 53, 31, 243, 192}, 8761 { /* left mode 1 */ 69, 60, 71, 38, 73, 119, 28, 222, 37}, 8762 { /* left mode 2 */ 68, 45, 128, 34, 1, 47, 11, 245, 171}, 8763 { /* left mode 3 */ 62, 17, 19, 70, 146, 85, 55, 62, 70}, 8764 { /* left mode 4 */ 75, 15, 9, 9, 64, 255, 184, 119, 16}, 8765 { /* left mode 5 */ 37, 43, 37, 154, 100, 163, 85, 160, 1}, 8766 { /* left mode 6 */ 63, 9, 92, 136, 28, 64, 32, 201, 85}, 8767 { /* left mode 7 */ 86, 6, 28, 5, 64, 255, 25, 248, 1}, 8768 { /* left mode 8 */ 56, 8, 17, 132, 137, 255, 55, 116, 128}, 8769 { /* left mode 9 */ 58, 15, 20, 82, 135, 57, 26, 121, 40} 8770 }, 8771 { /* above mode 8 */ 8772 { /* left mode 0 */ 164, 50, 31, 137, 154, 133, 25, 35, 218}, 8773 { /* left mode 1 */ 51, 103, 44, 131, 131, 123, 31, 6, 158}, 8774 { /* left mode 2 */ 86, 40, 64, 135, 148, 224, 45, 183, 128}, 8775 { /* left mode 3 */ 22, 26, 17, 131, 240, 154, 14, 1, 209}, 8776 { /* left mode 4 */ 83, 12, 13, 54, 192, 255, 68, 47, 28}, 8777 { /* left mode 5 */ 45, 16, 21, 91, 64, 222, 7, 1, 197}, 8778 { /* left mode 6 */ 56, 21, 39, 155, 60, 138, 23, 102, 213}, 8779 { /* left mode 7 */ 85, 26, 85, 85, 128, 128, 32, 146, 171}, 8780 { /* left mode 8 */ 18, 11, 7, 63, 144, 171, 4, 4, 246}, 8781 { /* left mode 9 */ 35, 27, 10, 146, 174, 171, 12, 26, 128} 8782 }, 8783 { /* above mode 9 */ 8784 { /* left mode 0 */ 190, 80, 35, 99, 180, 80, 126, 54, 45}, 8785 { /* left mode 1 */ 85, 126, 47, 87, 176, 51, 41, 20, 32}, 8786 { /* left mode 2 */ 101, 75, 128, 139, 118, 146, 116, 128, 85}, 8787 { /* left mode 3 */ 56, 41, 15, 176, 236, 85, 37, 9, 62}, 8788 { /* left mode 4 */ 146, 36, 19, 30, 171, 255, 97, 27, 20}, 8789 { /* left mode 5 */ 71, 30, 17, 119, 118, 255, 17, 18, 138}, 8790 { /* left mode 6 */ 101, 38, 60, 138, 55, 70, 43, 26, 142}, 8791 { /* left mode 7 */ 138, 45, 61, 62, 219, 1, 81, 188, 64}, 8792 { /* left mode 8 */ 32, 41, 20, 117, 151, 142, 20, 21, 163}, 8793 { /* left mode 9 */ 112, 19, 12, 61, 195, 128, 48, 4, 24} 8794 } 8795 }; 8796 static const int kf_y_mode_tree[] = 8797 { 8798 -B_PRED, 2, 8799 4, 6, 8800 -DC_PRED, -V_PRED, 8801 -H_PRED, -TM_PRED 8802 }; 8803 static const int y_mode_tree[] = 8804 { 8805 -DC_PRED, 2, 8806 4, 6, 8807 -V_PRED, -H_PRED, 8808 -TM_PRED, -B_PRED 8809 }; 8810 static const int uv_mode_tree[6] = 8811 { 8812 -DC_PRED, 2, 8813 -V_PRED, 4, 8814 -H_PRED, -TM_PRED 8815 }; 8816 static const int b_mode_tree[18] = 8817 { 8818 -B_DC_PRED, 2, /* 0 = DC_NODE */ 8819 -B_TM_PRED, 4, /* 1 = TM_NODE */ 8820 -B_VE_PRED, 6, /* 2 = VE_NODE */ 8821 8, 12, /* 3 = COM_NODE */ 8822 -B_HE_PRED, 10, /* 4 = HE_NODE */ 8823 -B_RD_PRED, -B_VR_PRED, /* 5 = RD_NODE */ 8824 -B_LD_PRED, 14, /* 6 = LD_NODE */ 8825 -B_VL_PRED, 16, /* 7 = VL_NODE */ 8826 -B_HD_PRED, -B_HU_PRED /* 8 = HD_NODE */ 8827 }; 8828 static const int small_mv_tree[14] = 8829 { 8830 2, 8, 8831 4, 6, 8832 -0, -1, 8833 -2, -3, 8834 10, 12, 8835 -4, -5, 8836 -6, -7 8837 }; 8838 static const int mv_ref_tree[8] = 8839 { 8840 -ZEROMV, 2, 8841 -NEARESTMV, 4, 8842 -NEARMV, 6, 8843 -NEWMV, -SPLITMV 8844 }; 8845 static const int submv_ref_tree[6] = 8846 { 8847 -LEFT4X4, 2, 8848 -ABOVE4X4, 4, 8849 -ZERO4X4, -NEW4X4 8850 }; 8851 static const int split_mv_tree[6] = 8852 { 8853 -3, 2, 8854 -2, 4, 8855 -0, -1 8856 }; 8857 static const unsigned char default_b_mode_probs[] = 8858 { 120, 90, 79, 133, 87, 85, 80, 111, 151}; 8859 static const unsigned char mv_counts_to_probs[6][4] = 8860 { 8861 { 7, 1, 1, 143 }, 8862 { 14, 18, 14, 107 }, 8863 { 135, 64, 57, 68 }, 8864 { 60, 56, 128, 65 }, 8865 { 159, 134, 128, 34 }, 8866 { 234, 188, 128, 28 } 8868 }; 8869 static const unsigned char split_mv_probs[3] = 8870 { 110, 111, 150}; 8871 static const unsigned char submv_ref_probs2[5][3] = 8872 { 8873 { 147, 136, 18 }, 8874 { 106, 145, 1 }, 8875 { 179, 121, 1 }, 8876 { 223, 1, 34 }, 8877 { 208, 1, 1 } 8878 }; 8880 const static int mv_partitions[4][16] = 8881 { 8882 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, 8883 {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1 }, 8884 {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3 }, 8885 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } 8886 }; 8888 ---- End code block ---------------------------------------- 8890 20.14. predict.c 8892 ---- Begin code block -------------------------------------- 8894 /* 8895 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8896 * 8897 * Use of this source code is governed by a BSD-style license 8898 * that can be found in the LICENSE file in the root of the source 8899 * tree. An additional intellectual property rights grant can be 8900 * found in the file PATENTS. All contributing project authors may 8901 * be found in the AUTHORS file in the root of the source tree. 8902 */ 8903 #include "dixie.h" 8904 #include "predict.h" 8905 #include "idct_add.h" 8906 #include "mem.h" 8907 #include 8908 #include 8910 enum 8911 { 8912 BORDER_PIXELS = 16, 8913 }; 8915 static const filter_t sixtap_filters[8] = 8916 { 8917 { 0, 0, 128, 0, 0, 0 }, 8918 { 0, -6, 123, 12, -1, 0 }, 8919 { 2, -11, 108, 36, -8, 1 }, 8920 { 0, -9, 93, 50, -6, 0 }, 8921 { 3, -16, 77, 77, -16, 3 }, 8922 { 0, -6, 50, 93, -9, 0 }, 8923 { 1, -8, 36, 108, -11, 2 }, 8924 { 0, -1, 12, 123, -6, 0 }, 8925 }; 8927 static const filter_t bilinear_filters[8] = 8928 { 8930 { 0, 0, 128, 0, 0, 0 }, 8931 { 0, 0, 112, 16, 0, 0 }, 8932 { 0, 0, 96, 32, 0, 0 }, 8933 { 0, 0, 80, 48, 0, 0 }, 8934 { 0, 0, 64, 64, 0, 0 }, 8935 { 0, 0, 48, 80, 0, 0 }, 8936 { 0, 0, 32, 96, 0, 0 }, 8937 { 0, 0, 16, 112, 0, 0 } 8938 }; 8940 static void 8941 predict_h_nxn(unsigned char *predict, 8942 int stride, 8943 int n) 8944 { 8945 unsigned char *left = predict - 1; 8946 int i, j; 8948 for (i = 0; i < n; i++) 8949 for (j = 0; j < n; j++) 8950 predict[i *stride + j] = left[i * stride]; 8951 } 8953 static void 8954 predict_v_nxn(unsigned char *predict, 8955 int stride, 8956 int n) 8957 { 8958 unsigned char *above = predict - stride; 8959 int i, j; 8961 for (i = 0; i < n; i++) 8962 for (j = 0; j < n; j++) 8963 predict[i *stride + j] = above[j]; 8964 } 8966 static void 8967 predict_tm_nxn(unsigned char *predict, 8968 int stride, 8969 int n) 8970 { 8971 /* Transposes the left column to the top row for later 8972 * consumption by the idct/recon stage 8973 */ 8974 unsigned char *left = predict - 1; 8975 unsigned char *above = predict - stride; 8976 unsigned char p = above[-1]; 8977 int i, j; 8979 for (j = 0; j < n; j++) 8980 { 8981 for (i = 0; i < n; i++) 8982 predict[i] = CLAMP_255(*left + above[i] - p); 8984 predict += stride; 8985 left += stride; 8986 } 8987 } 8989 static void 8990 predict_dc_nxn(unsigned char *predict, 8991 int stride, 8992 int n) 8993 { 8994 unsigned char *left = predict - 1; 8995 unsigned char *above = predict - stride; 8996 int i, j, dc = 0; 8998 for (i = 0; i < n; i++) 8999 { 9000 dc += *left + above[i]; 9001 left += stride; 9002 } 9004 switch (n) 9005 { 9006 case 16: 9007 dc = (dc + 16) >> 5; 9008 break; 9010 case 8: 9011 dc = (dc + 8) >> 4; 9012 break; 9013 case 4: 9014 dc = (dc + 4) >> 3; 9015 break; 9016 } 9018 for (i = 0; i < n; i++) 9019 for (j = 0; j < n; j++) 9020 predict[i *stride + j] = dc; 9021 } 9023 static void 9024 predict_ve_4x4(unsigned char *predict, 9025 int stride) 9026 { 9027 unsigned char *above = predict - stride; 9028 int i, j; 9030 predict[0] = (above[-1] + 2 * above[0] + above[1] + 2) >> 2; 9031 predict[1] = (above[ 0] + 2 * above[1] + above[2] + 2) >> 2; 9032 predict[2] = (above[ 1] + 2 * above[2] + above[3] + 2) >> 2; 9033 predict[3] = (above[ 2] + 2 * above[3] + above[4] + 2) >> 2; 9035 for (i = 1; i < 4; i++) 9036 for (j = 0; j < 4; j++) 9037 predict[i *stride + j] = predict[j]; 9038 } 9040 static void 9041 predict_he_4x4(unsigned char *predict, 9042 int stride) 9043 { 9044 unsigned char *left = predict - 1; 9046 predict[0] = 9047 predict[1] = 9048 predict[2] = 9049 predict[3] = (left[-stride] + 2 * left[0] + 9050 left[stride] + 2) >> 2; 9051 predict += stride; 9052 left += stride; 9054 predict[0] = 9055 predict[1] = 9056 predict[2] = 9057 predict[3] = (left[-stride] + 2 * left[0] + 9058 left[stride] + 2) >> 2; 9059 predict += stride; 9060 left += stride; 9062 predict[0] = 9063 predict[1] = 9064 predict[2] = 9065 predict[3] = (left[-stride] + 2 * left[0] + 9066 left[stride] + 2) >> 2; 9067 predict += stride; 9068 left += stride; 9070 predict[0] = 9071 predict[1] = 9072 predict[2] = 9073 predict[3] = (left[-stride] + 2 * left[0] + left[0] + 2) >> 2; 9074 } 9076 static void 9077 predict_ld_4x4(unsigned char *predict, 9078 int stride) 9079 { 9080 unsigned char *above = predict - stride; 9081 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9083 predict[0] = pred0 = (above[0] + 2 * above[1] + 9084 above[2] + 2) >> 2; 9085 predict[1] = pred1 = (above[1] + 2 * above[2] + 9086 above[3] + 2) >> 2; 9087 predict[2] = pred2 = (above[2] + 2 * above[3] + 9088 above[4] + 2) >> 2; 9089 predict[3] = pred3 = (above[3] + 2 * above[4] + 9090 above[5] + 2) >> 2; 9091 predict += stride; 9093 predict[0] = pred1; 9094 predict[1] = pred2; 9095 predict[2] = pred3; 9096 predict[3] = pred4 = (above[4] + 2 * above[5] + 9097 above[6] + 2) >> 2; 9098 predict += stride; 9100 predict[0] = pred2; 9101 predict[1] = pred3; 9102 predict[2] = pred4; 9103 predict[3] = pred5 = (above[5] + 2 * above[6] + 9104 above[7] + 2) >> 2; 9105 predict += stride; 9107 predict[0] = pred3; 9108 predict[1] = pred4; 9109 predict[2] = pred5; 9110 predict[3] = pred6 = (above[6] + 2 * above[7] + 9111 above[7] + 2) >> 2; 9112 } 9114 static void 9115 predict_rd_4x4(unsigned char *predict, 9116 int stride) 9117 { 9118 unsigned char *left = predict - 1; 9119 unsigned char *above = predict - stride; 9120 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9122 predict[0] = pred0 = 9123 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9124 predict[1] = pred1 = 9125 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9126 predict[2] = pred2 = 9127 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9128 predict[3] = pred3 = 9129 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9130 predict += stride; 9132 predict[0] = pred4 = 9133 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9134 predict[1] = pred0; 9135 predict[2] = pred1; 9136 predict[3] = pred2; 9137 predict += stride; 9139 predict[0] = pred5 = 9140 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9141 predict[1] = pred4; 9142 predict[2] = pred0; 9143 predict[3] = pred1; 9144 predict += stride; 9146 predict[0] = pred6 = (left[stride*3] + 2 * left[stride*2] + 9147 left[stride] + 2) >> 2; 9148 predict[1] = pred5; 9149 predict[2] = pred4; 9150 predict[3] = pred0; 9151 } 9153 static void 9154 predict_vr_4x4(unsigned char *predict, 9155 int stride) 9156 { 9157 unsigned char *left = predict - 1; 9158 unsigned char *above = predict - stride; 9159 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9160 pred7, pred8, pred9; 9162 predict[0] = pred0 = (above[-1] + above[0] + 1) >> 1; 9163 predict[1] = pred1 = (above[ 0] + above[1] + 1) >> 1; 9164 predict[2] = pred2 = (above[ 1] + above[2] + 1) >> 1; 9165 predict[3] = pred3 = (above[ 2] + above[3] + 1) >> 1; 9166 predict += stride; 9168 predict[0] = pred4 = (left[ 0] + 2 * above[-1] + 9169 above[0] + 2) >> 2; 9170 predict[1] = pred5 = (above[-1] + 2 * above[ 0] + 9171 above[1] + 2) >> 2; 9172 predict[2] = pred6 = (above[ 0] + 2 * above[ 1] + 9173 above[2] + 2) >> 2; 9174 predict[3] = pred7 = (above[ 1] + 2 * above[ 2] + 9175 above[3] + 2) >> 2; 9176 predict += stride; 9178 predict[0] = pred8 = 9179 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9180 predict[1] = pred0; 9181 predict[2] = pred1; 9182 predict[3] = pred2; 9183 predict += stride; 9185 predict[0] = pred9 = 9186 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9187 predict[1] = pred4; 9188 predict[2] = pred5; 9189 predict[3] = pred6; 9190 } 9192 static void 9193 predict_vl_4x4(unsigned char *predict, 9194 int stride) 9195 { 9196 unsigned char *above = predict - stride; 9197 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9198 pred7, pred8, pred9; 9200 predict[0] = pred0 = (above[0] + above[1] + 1) >> 1; 9201 predict[1] = pred1 = (above[1] + above[2] + 1) >> 1; 9202 predict[2] = pred2 = (above[2] + above[3] + 1) >> 1; 9203 predict[3] = pred3 = (above[3] + above[4] + 1) >> 1; 9204 predict += stride; 9206 predict[0] = pred4 = (above[0] + 2 * above[1] + 9207 above[2] + 2) >> 2; 9208 predict[1] = pred5 = (above[1] + 2 * above[2] + 9209 above[3] + 2) >> 2; 9210 predict[2] = pred6 = (above[2] + 2 * above[3] + 9211 above[4] + 2) >> 2; 9212 predict[3] = pred7 = (above[3] + 2 * above[4] + 9213 above[5] + 2) >> 2; 9214 predict += stride; 9216 predict[0] = pred1; 9217 predict[1] = pred2; 9218 predict[2] = pred3; 9219 predict[3] = pred8 = (above[4] + 2 * above[5] + 9220 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] + 9227 above[7] + 2) >> 2; 9228 } 9230 static void 9231 predict_hd_4x4(unsigned char *predict, 9232 int stride) 9233 { 9234 unsigned char *left = predict - 1; 9235 unsigned char *above = predict - stride; 9236 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9237 pred7, pred8, pred9; 9239 predict[0] = pred0 = (left[ 0] + above[-1] + 1) >> 1; 9240 predict[1] = pred1 = (left[ 0] + 2 * above[-1] + 9241 above[0] + 2) >> 2; 9242 predict[2] = pred2 = (above[-1] + 2 * above[ 0] + 9243 above[1] + 2) >> 2; 9244 predict[3] = pred3 = (above[ 0] + 2 * above[ 1] + 9245 above[2] + 2) >> 2; 9246 predict += stride; 9248 predict[0] = pred4 = (left[stride] + left[0] + 1) >> 1; 9249 predict[1] = pred5 = (left[stride] + 2 * left[0] + 9250 above[-1] + 2) >> 2; 9251 predict[2] = pred0; 9252 predict[3] = pred1; 9253 predict += stride; 9255 predict[0] = pred6 = (left[stride*2] + left[stride] + 1) >> 1; 9256 predict[1] = pred7 = (left[stride*2] + 2 * left[stride] + 9257 left[0] + 2) >> 2; 9258 predict[2] = pred4; 9259 predict[3] = pred5; 9260 predict += stride; 9262 predict[0] = pred8 = (left[stride*3] + left[stride*2] + 1) >> 1; 9263 predict[1] = pred9 = (left[stride*3] + 2 * left[stride*2] + 9264 left[stride] + 2) >> 2; 9265 predict[2] = pred6; 9266 predict[3] = pred7; 9267 } 9269 static void 9270 predict_hu_4x4(unsigned char *predict, 9271 int stride) 9272 { 9273 unsigned char *left = predict - 1; 9274 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9276 predict[0] = pred0 = (left[stride*0] + 9277 left[stride*1] + 1) >> 1; 9278 predict[1] = pred1 = (left[stride*0] + 2 * left[stride*1] + 9279 left[stride*2] + 2) >> 2; 9280 predict[2] = pred2 = (left[stride*1] + left[stride*2] + 1) >> 1; 9281 predict[3] = pred3 = (left[stride*1] + 2 * left[stride*2] + 9282 left[stride*3] + 2) >> 2; 9283 predict += stride; 9285 predict[0] = pred2; 9286 predict[1] = pred3; 9287 predict[2] = pred4 = (left[stride*2] + left[stride*3] + 1) >> 1; 9288 predict[3] = pred5 = (left[stride*2] + 2 * left[stride*3] + 9289 left[stride*3] + 2) >> 2; 9291 predict += stride; 9293 predict[0] = pred4; 9294 predict[1] = pred5; 9295 predict[2] = pred6 = left[stride*3]; 9296 predict[3] = pred6; 9297 predict += stride; 9299 predict[0] = pred6; 9300 predict[1] = pred6; 9301 predict[2] = pred6; 9302 predict[3] = pred6; 9303 } 9305 static void 9306 predict_h_16x16(unsigned char *predict, int stride) 9307 { 9308 predict_h_nxn(predict, stride, 16); 9309 } 9311 static void 9312 predict_v_16x16(unsigned char *predict, int stride) 9313 { 9314 predict_v_nxn(predict, stride, 16); 9315 } 9317 static void 9318 predict_tm_16x16(unsigned char *predict, int stride) 9319 { 9320 predict_tm_nxn(predict, stride, 16); 9321 } 9323 static void 9324 predict_h_8x8(unsigned char *predict, int stride) 9325 { 9326 predict_h_nxn(predict, stride, 8); 9327 } 9329 static void 9330 predict_v_8x8(unsigned char *predict, int stride) 9331 { 9332 predict_v_nxn(predict, stride, 8); 9333 } 9334 static void 9335 predict_tm_8x8(unsigned char *predict, int stride) 9336 { 9337 predict_tm_nxn(predict, stride, 8); 9338 } 9340 static void 9341 predict_tm_4x4(unsigned char *predict, int stride) 9342 { 9343 predict_tm_nxn(predict, stride, 4); 9344 } 9346 static void 9347 copy_down(unsigned char *recon, 9348 int stride) 9349 { 9350 /* Copy the four pixels above-right of subblock 3 to 9351 * above-right of subblocks 7, 11, and 15 9352 */ 9353 uint32_t tmp, *copy = (void *)(recon + 16 - stride); 9355 stride = stride / sizeof(unsigned int); 9356 tmp = *copy; 9357 copy += stride * 4; 9358 *copy = tmp; 9359 copy += stride * 4; 9360 *copy = tmp; 9361 copy += stride * 4; 9362 *copy = tmp; 9363 } 9365 static void 9366 b_pred(unsigned char *predict, 9367 int stride, 9368 struct mb_info *mbi, 9369 short *coeffs) 9370 { 9371 int i; 9373 copy_down(predict, stride); 9375 for (i = 0; i < 16; i++) 9376 { 9377 unsigned char *b_predict = predict + (i & 3) * 4; 9378 switch (mbi->split.modes[i]) 9379 { 9380 case B_DC_PRED: 9381 predict_dc_nxn(b_predict, stride, 4); 9382 break; 9383 case B_TM_PRED: 9384 predict_tm_4x4(b_predict, stride); 9385 break; 9386 case B_VE_PRED: 9387 predict_ve_4x4(b_predict, stride); 9388 break; 9389 case B_HE_PRED: 9390 predict_he_4x4(b_predict, stride); 9391 break; 9392 case B_LD_PRED: 9393 predict_ld_4x4(b_predict, stride); 9394 break; 9395 case B_RD_PRED: 9396 predict_rd_4x4(b_predict, stride); 9397 break; 9398 case B_VR_PRED: 9399 predict_vr_4x4(b_predict, stride); 9400 break; 9401 case B_VL_PRED: 9402 predict_vl_4x4(b_predict, stride); 9403 break; 9404 case B_HD_PRED: 9405 predict_hd_4x4(b_predict, stride); 9406 break; 9407 case B_HU_PRED: 9408 predict_hu_4x4(b_predict, stride); 9409 break; 9410 default: 9411 assert(0); 9412 } 9414 vp8_dixie_idct_add(b_predict, b_predict, stride, coeffs); 9415 coeffs += 16; 9417 if ((i & 3) == 3) 9418 { 9419 predict += stride * 4; 9420 } 9421 } 9422 } 9424 static void 9425 fixup_dc_coeffs(struct mb_info *mbi, 9426 short *coeffs) 9427 { 9428 short y2[16]; 9429 int i; 9431 vp8_dixie_walsh(coeffs + 24 * 16, y2); 9433 for (i = 0; i < 16; i++) 9434 coeffs[i*16] = y2[i]; 9435 } 9437 static void 9438 predict_intra_luma(unsigned char *predict, 9439 int stride, 9440 struct mb_info *mbi, 9441 short *coeffs) 9442 { 9443 if (mbi->base.y_mode == B_PRED) 9444 b_pred(predict, stride, mbi, coeffs); 9445 else 9446 { 9447 int i; 9449 switch (mbi->base.y_mode) 9450 { 9451 case DC_PRED: 9452 predict_dc_nxn(predict, stride, 16); 9453 break; 9454 case V_PRED: 9455 predict_v_16x16(predict, stride); 9456 break; 9457 case H_PRED: 9458 predict_h_16x16(predict, stride); 9459 break; 9460 case TM_PRED: 9461 predict_tm_16x16(predict, stride); 9462 break; 9463 default: 9464 assert(0); 9465 } 9467 fixup_dc_coeffs(mbi, coeffs); 9469 for (i = 0; i < 16; i++) 9470 { 9471 vp8_dixie_idct_add(predict, predict, stride, coeffs); 9472 coeffs += 16; 9473 predict += 4; 9475 if ((i & 3) == 3) 9476 predict += stride * 4 - 16; 9477 } 9479 } 9480 } 9482 static void 9483 predict_intra_chroma(unsigned char *predict_u, 9484 unsigned char *predict_v, 9485 int stride, 9486 struct mb_info *mbi, 9487 short *coeffs) 9488 { 9489 int i; 9491 switch (mbi->base.uv_mode) 9492 { 9493 case DC_PRED: 9494 predict_dc_nxn(predict_u, stride, 8); 9495 predict_dc_nxn(predict_v, stride, 8); 9496 break; 9497 case V_PRED: 9498 predict_v_8x8(predict_u, stride); 9499 predict_v_8x8(predict_v, stride); 9500 break; 9501 case H_PRED: 9502 predict_h_8x8(predict_u, stride); 9503 predict_h_8x8(predict_v, stride); 9504 break; 9505 case TM_PRED: 9506 predict_tm_8x8(predict_u, stride); 9507 predict_tm_8x8(predict_v, stride); 9508 break; 9509 default: 9510 assert(0); 9511 } 9513 coeffs += 16 * 16; 9515 for (i = 16; i < 20; i++) 9516 { 9517 vp8_dixie_idct_add(predict_u, predict_u, stride, coeffs); 9518 coeffs += 16; 9519 predict_u += 4; 9521 if (i & 1) 9522 predict_u += stride * 4 - 8; 9523 } 9525 for (i = 20; i < 24; i++) 9526 { 9527 vp8_dixie_idct_add(predict_v, predict_v, stride, coeffs); 9528 coeffs += 16; 9529 predict_v += 4; 9531 if (i & 1) 9532 predict_v += stride * 4 - 8; 9533 } 9535 } 9537 static void 9538 sixtap_horiz(unsigned char *output, 9539 int output_stride, 9540 const unsigned char *reference, 9541 int reference_stride, 9542 int cols, 9543 int rows, 9544 const filter_t filter 9545 ) 9546 { 9547 int r, c, temp; 9549 for (r = 0; r < rows; r++) 9550 { 9551 for (c = 0; c < cols; c++) 9552 { 9553 temp = (reference[-2] * filter[0]) + 9554 (reference[-1] * filter[1]) + 9555 (reference[ 0] * filter[2]) + 9556 (reference[ 1] * filter[3]) + 9557 (reference[ 2] * filter[4]) + 9558 (reference[ 3] * filter[5]) + 9559 64; 9560 temp >>= 7; 9561 output[c] = CLAMP_255(temp); 9562 reference++; 9563 } 9565 reference += reference_stride - cols; 9566 output += output_stride; 9567 } 9568 } 9570 static void 9571 sixtap_vert(unsigned char *output, 9572 int output_stride, 9573 const unsigned char *reference, 9574 int reference_stride, 9575 int cols, 9576 int rows, 9577 const filter_t filter 9578 ) 9579 { 9580 int r, c, temp; 9582 for (r = 0; r < rows; r++) 9583 { 9584 for (c = 0; c < cols; c++) 9585 { 9586 temp = (reference[-2*reference_stride] * filter[0]) + 9587 (reference[-1*reference_stride] * filter[1]) + 9588 (reference[ 0*reference_stride] * filter[2]) + 9589 (reference[ 1*reference_stride] * filter[3]) + 9590 (reference[ 2*reference_stride] * filter[4]) + 9591 (reference[ 3*reference_stride] * filter[5]) + 9592 64; 9593 temp >>= 7; 9594 output[c] = CLAMP_255(temp); 9595 reference++; 9596 } 9598 reference += reference_stride - cols; 9599 output += output_stride; 9600 } 9601 } 9603 static void 9604 sixtap_2d(unsigned char *output, 9605 int output_stride, 9606 const unsigned char *reference, 9607 int reference_stride, 9608 int cols, 9609 int rows, 9610 int mx, 9611 int my, 9612 const filter_t filters[8] 9613 ) 9614 { 9615 DECLARE_ALIGNED(16, unsigned char, temp[16*(16+5)]); 9617 sixtap_horiz(temp, 16, 9618 reference - 2 * reference_stride, reference_stride, 9619 cols, rows + 5, filters[mx]); 9620 sixtap_vert(output, output_stride, 9621 temp + 2 * 16, 16, 9622 cols, rows, filters[my]); 9623 } 9625 struct img_index 9626 { 9627 unsigned char *y, *u, *v; 9628 int stride, uv_stride; 9629 }; 9631 static const unsigned char * 9632 filter_block(unsigned char *output, 9633 const unsigned char *reference, 9634 int stride, 9635 const union mv *mv, 9636 const filter_t filters[8]) 9637 { 9638 int mx, my; 9640 /* Handle 0,0 as a special case. TODO: does this make it any 9641 * faster? 9642 */ 9643 if (!mv->raw) 9644 return reference; 9646 mx = mv->d.x & 7; 9647 my = mv->d.y & 7; 9648 reference += ((mv->d.y >> 3) * stride) + (mv->d.x >> 3); 9650 if (mx | my) 9651 { 9652 sixtap_2d(output, stride, reference, stride, 4, 4, mx, my, 9653 filters); 9654 reference = output; 9655 } 9657 return reference; 9659 } 9661 static void 9662 recon_1_block(unsigned char *output, 9663 const unsigned char *reference, 9664 int stride, 9665 const union mv *mv, 9666 const filter_t filters[8], 9667 short *coeffs, 9668 struct mb_info *mbi, 9669 int b 9670 ) 9671 { 9672 const unsigned char *predict; 9674 predict = filter_block(output, reference, stride, mv, filters); 9675 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9676 } 9678 static mv_t 9679 calculate_chroma_splitmv(struct mb_info *mbi, 9680 int b, 9681 int full_pixel) 9682 { 9683 int temp; 9684 union mv mv; 9686 temp = mbi->split.mvs[b].d.x + 9687 mbi->split.mvs[b+1].d.x + 9688 mbi->split.mvs[b+4].d.x + 9689 mbi->split.mvs[b+5].d.x; 9691 if (temp < 0) 9692 temp -= 4; 9693 else 9694 temp += 4; 9696 mv.d.x = temp / 8; 9698 temp = mbi->split.mvs[b].d.y + 9699 mbi->split.mvs[b+1].d.y + 9700 mbi->split.mvs[b+4].d.y + 9701 mbi->split.mvs[b+5].d.y; 9703 if (temp < 0) 9704 temp -= 4; 9706 else 9707 temp += 4; 9709 mv.d.y = temp / 8; 9711 if (full_pixel) 9712 { 9713 mv.d.x &= ~7; 9714 mv.d.y &= ~7; 9715 } 9717 return mv; 9718 } 9720 /* Note: We rely on the reconstructed border having the same stride 9721 * as the reference buffer because the filter_block can't adjust the 9722 * stride with its return value, only the reference pointer. 9723 */ 9724 static void 9725 build_mc_border(unsigned char *dst, 9726 const unsigned char *src, 9727 int stride, 9728 int x, 9729 int y, 9730 int b_w, 9731 int b_h, 9732 int w, 9733 int h 9734 ) 9735 { 9736 const unsigned char *ref_row; 9738 /* Get a pointer to the start of the real data for this row */ 9739 ref_row = src - x - y * stride; 9741 if (y >= h) 9742 ref_row += (h - 1) * stride; 9743 else if (y > 0) 9744 ref_row += y * stride; 9746 do 9747 { 9748 int left, right = 0, copy; 9750 left = x < 0 ? -x : 0; 9751 if (left > b_w) 9752 left = b_w; 9754 if (x + b_w > w) 9755 right = x + b_w - w; 9757 if (right > b_w) 9758 right = b_w; 9760 copy = b_w - left - right; 9762 if (left) 9763 memset(dst, ref_row[0], left); 9765 if (copy) 9766 memcpy(dst + left, ref_row + x + left, copy); 9768 if (right) 9769 memset(dst + left + copy, ref_row[w-1], right); 9771 dst += stride; 9772 y++; 9774 if (y < h && y > 0) 9775 ref_row += stride; 9776 } 9777 while (--b_h); 9778 } 9780 static void 9781 recon_1_edge_block(unsigned char *output, 9782 unsigned char *emul_block, 9783 const unsigned char *reference, 9784 int stride, 9785 const union mv *mv, 9786 const filter_t filters[8], 9787 short *coeffs, 9788 struct mb_info *mbi, 9789 int x, 9790 int y, 9791 int w, 9792 int h, 9793 int start_b 9794 ) 9795 { 9796 const unsigned char *predict; 9797 int b = start_b; 9798 const int b_w = 4; 9799 const int b_h = 4; 9801 x += mv->d.x >> 3; 9802 y += mv->d.y >> 3; 9804 /* Need two pixels left/above, 3 right/below for 6-tap */ 9805 if (x < 2 || x + b_w - 1 + 3 >= w || y < 2 || 9806 y + b_h - 1 + 3 >= h) 9807 { 9808 reference += (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9809 build_mc_border(emul_block, 9810 reference - 2 - 2 * stride, stride, 9811 x - 2, y - 2, b_w + 5, b_h + 5, w, h); 9812 reference = emul_block + 2 * stride + 2; 9813 reference -= (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9814 } 9816 predict = filter_block(output, reference, stride, mv, filters); 9817 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9818 } 9820 static void 9821 predict_inter_emulated_edge(struct vp8_decoder_ctx *ctx, 9822 struct img_index *img, 9823 short *coeffs, 9824 struct mb_info *mbi, 9825 int mb_col, 9826 int mb_row) 9827 { 9828 /* TODO: move this into its own buffer. This only works because 9829 * we still have a border allocated. 9830 */ 9831 unsigned char *emul_block = ctx->frame_strg[0].img.img_data; 9832 unsigned char *reference; 9833 unsigned char *output; 9834 ptrdiff_t reference_offset; 9835 int w, h, x, y, b; 9836 union mv chroma_mv[4]; 9837 unsigned char *u = img->u, *v = img->v; 9838 int full_pixel = ctx->frame_hdr.version == 3; 9840 x = mb_col * 16; 9841 y = mb_row * 16; 9842 w = ctx->mb_cols * 16; 9843 h = ctx->mb_rows * 16; 9844 output = img->y; 9845 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9846 reference = output + reference_offset; 9848 if (mbi->base.y_mode != SPLITMV) 9849 { 9850 union mv uvmv; 9852 uvmv = mbi->base.mv; 9853 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9854 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9856 if (full_pixel) 9857 { 9858 uvmv.d.x &= ~7; 9859 uvmv.d.y &= ~7; 9860 } 9862 chroma_mv[0] = uvmv; 9863 chroma_mv[1] = uvmv; 9864 chroma_mv[2] = uvmv; 9865 chroma_mv[3] = uvmv; 9866 } 9867 else 9868 { 9869 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9870 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9871 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9872 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9873 } 9875 /* Luma */ 9876 for (b = 0; b < 16; b++) 9877 { 9878 union mv *ymv; 9880 if (mbi->base.y_mode != SPLITMV) 9881 ymv = &mbi->base.mv; 9882 else 9883 ymv = mbi->split.mvs + b; 9885 recon_1_edge_block(output, emul_block, reference, 9886 img->stride, ymv, ctx->subpixel_filters, coeffs, 9887 mbi, x, y, w, h, b); 9889 x += 4; 9890 output += 4; 9891 reference += 4; 9893 if ((b & 3) == 3) 9894 { 9895 x -= 16; 9896 y += 4; 9897 output += 4 * img->stride - 16; 9898 reference += 4 * img->stride - 16; 9899 } 9900 } 9902 x = mb_col * 16; 9903 y = mb_row * 16; 9905 /* Chroma */ 9906 x >>= 1; 9907 y >>= 1; 9908 w >>= 1; 9909 h >>= 1; 9911 for (b = 0; b < 4; b++) 9912 { 9913 recon_1_edge_block(u, emul_block, u + reference_offset, 9914 img->uv_stride, 9915 &chroma_mv[b], ctx->subpixel_filters, 9916 coeffs, mbi, x, y, w, h, b + 16); 9917 recon_1_edge_block(v, emul_block, v + reference_offset, 9918 img->uv_stride, 9919 &chroma_mv[b], ctx->subpixel_filters, 9920 coeffs, mbi, x, y, w, h, b + 20); 9921 u += 4; 9922 v += 4; 9923 x += 4; 9925 if (b & 1) 9926 { 9927 x -= 8; 9928 y += 4; 9929 u += 4 * img->uv_stride - 8; 9930 v += 4 * img->uv_stride - 8; 9931 } 9932 } 9934 } 9936 static void 9937 predict_inter(struct vp8_decoder_ctx *ctx, 9938 struct img_index *img, 9939 short *coeffs, 9940 struct mb_info *mbi) 9941 { 9942 unsigned char *y = img->y; 9943 unsigned char *u = img->u; 9944 unsigned char *v = img->v; 9945 ptrdiff_t reference_offset; 9946 union mv chroma_mv[4]; 9947 int full_pixel = ctx->frame_hdr.version == 3; 9948 int b; 9950 if (mbi->base.y_mode != SPLITMV) 9951 { 9952 union mv uvmv; 9954 uvmv = mbi->base.mv; 9955 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9956 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9958 if (full_pixel) 9959 { 9960 uvmv.d.x &= ~7; 9961 uvmv.d.y &= ~7; 9962 } 9964 chroma_mv[0] = 9965 chroma_mv[1] = 9966 chroma_mv[2] = 9967 chroma_mv[3] = uvmv; 9968 } 9969 else 9970 { 9971 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9972 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9973 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9974 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9975 } 9977 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9979 for (b = 0; b < 16; b++) 9980 { 9981 union mv *ymv; 9983 if (mbi->base.y_mode != SPLITMV) 9984 ymv = &mbi->base.mv; 9985 else 9986 ymv = mbi->split.mvs + b; 9988 recon_1_block(y, y + reference_offset, img->stride, 9989 ymv, ctx->subpixel_filters, coeffs, mbi, b); 9990 y += 4; 9992 if ((b & 3) == 3) 9993 y += 4 * img->stride - 16; 9994 } 9996 for (b = 0; b < 4; b++) 9997 { 9998 recon_1_block(u, u + reference_offset, 9999 img->uv_stride, &chroma_mv[b], 10000 ctx->subpixel_filters, coeffs, mbi, b + 16); 10001 recon_1_block(v, v + reference_offset, 10002 img->uv_stride, &chroma_mv[b], 10003 ctx->subpixel_filters, coeffs, mbi, b + 20); 10004 u += 4; 10005 v += 4; 10007 if (b & 1) 10008 { 10009 u += 4 * img->uv_stride - 8; 10010 v += 4 * img->uv_stride - 8; 10011 } 10012 } 10013 } 10015 void 10016 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg) 10017 { 10018 if (rcimg) 10019 { 10020 assert(rcimg->ref_cnt); 10021 rcimg->ref_cnt--; 10022 } 10023 } 10025 struct ref_cnt_img * 10026 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg) 10027 { 10028 rcimg->ref_cnt++; 10029 return rcimg; 10030 } 10032 struct ref_cnt_img * 10033 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames) 10034 { 10035 int i; 10037 for (i = 0; i < NUM_REF_FRAMES; i++) 10038 if (frames[i].ref_cnt == 0) 10039 { 10040 frames[i].ref_cnt = 1; 10041 return &frames[i]; 10042 } 10044 assert(0); 10045 return NULL; 10046 } 10048 static void 10049 fixup_left(unsigned char *predict, 10050 int width, 10051 int stride, 10052 unsigned int row, 10053 enum prediction_mode mode) 10054 { 10055 /* The left column of out-of-frame pixels is taken to be 129, 10056 * unless we're doing DC_PRED, in which case we duplicate the 10057 * above row, unless this is also row 0, in which case we use 10058 * 129. 10059 */ 10060 unsigned char *left = predict - 1; 10061 int i; 10063 if (mode == DC_PRED && row) 10064 { 10065 unsigned char *above = predict - stride; 10067 for (i = 0; i < width; i++) 10068 { 10069 *left = above[i]; 10070 left += stride; 10071 } 10072 } 10073 else 10074 { 10075 /* Need to re-set the above row, in case the above MB was 10076 * DC_PRED. 10077 */ 10078 left -= stride; 10079 for (i = -1; i < width; i++) 10080 { 10081 *left = 129; 10082 left += stride; 10083 } 10084 } 10085 } 10087 static void 10088 fixup_above(unsigned char *predict, 10089 int width, 10090 int stride, 10091 unsigned int col, 10092 enum prediction_mode mode) 10093 { 10094 /* The above row of out-of-frame pixels is taken to be 127, 10095 * unless we're doing DC_PRED, in which case we duplicate the 10096 * left col, unless this is also col 0, in which case we use 10097 * 127. 10098 */ 10099 unsigned char *above = predict - stride; 10100 int i; 10102 if (mode == DC_PRED && col) 10103 { 10104 unsigned char *left = predict - 1; 10106 for (i = 0; i < width; i++) 10107 { 10108 above[i] = *left; 10109 left += stride; 10110 } 10111 } 10112 else 10113 /* Need to re-set the left col, in case the last MB was 10114 * DC_PRED. 10115 */ 10116 memset(above - 1, 127, width + 1); 10118 memset(above + width, 127, 4); // for above-right subblock modes 10119 } 10121 void 10122 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx) 10123 { 10124 int i; 10125 unsigned char *this_frame_base; 10127 if (ctx->frame_hdr.frame_size_updated) 10128 { 10129 for (i = 0; i < NUM_REF_FRAMES; i++) 10130 { 10131 unsigned int w = ctx->mb_cols * 16 + BORDER_PIXELS * 2; 10132 unsigned int h = ctx->mb_rows * 16 + BORDER_PIXELS * 2; 10134 vpx_img_free(&ctx->frame_strg[i].img); 10135 ctx->frame_strg[i].ref_cnt = 0; 10136 ctx->ref_frames[i] = NULL; 10138 if (!vpx_img_alloc(&ctx->frame_strg[i].img, 10139 IMG_FMT_I420, w, h, 16)) 10140 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10141 "Failed to allocate %dx%d" 10142 " framebuffer", 10143 w, h); 10145 vpx_img_set_rect(&ctx->frame_strg[i].img, BORDER_PIXELS, 10146 BORDER_PIXELS, ctx->frame_hdr.kf.w, 10147 ctx->frame_hdr.kf.h); 10149 } 10151 if (ctx->frame_hdr.version) 10152 ctx->subpixel_filters = bilinear_filters; 10153 else 10154 ctx->subpixel_filters = sixtap_filters; 10155 } 10157 /* Find a free framebuffer to predict into */ 10158 if (ctx->ref_frames[CURRENT_FRAME]) 10159 vp8_dixie_release_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 10161 ctx->ref_frames[CURRENT_FRAME] = 10162 vp8_dixie_find_free_ref_frame(ctx->frame_strg); 10163 this_frame_base = ctx->ref_frames[CURRENT_FRAME]->img.img_data; 10165 /* Calculate offsets to the other reference frames */ 10166 for (i = 0; i < NUM_REF_FRAMES; i++) 10167 { 10168 struct ref_cnt_img *ref = ctx->ref_frames[i]; 10170 ctx->ref_frame_offsets[i] = 10171 ref ? ref->img.img_data - this_frame_base : 0; 10173 } 10175 /* TODO: No need to do this on every frame... */ 10176 } 10178 void 10179 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx) 10180 { 10181 int i; 10183 for (i = 0; i < NUM_REF_FRAMES; i++) 10184 { 10185 vpx_img_free(&ctx->frame_strg[i].img); 10186 ctx->frame_strg[i].ref_cnt = 0; 10187 ctx->ref_frames[i] = NULL; 10188 } 10189 } 10191 void 10192 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10193 unsigned int row, 10194 unsigned int start_col, 10195 unsigned int num_cols) 10196 { 10197 struct img_index img; 10198 struct mb_info *mbi; 10199 unsigned int col; 10200 short *coeffs; 10202 /* Adjust pointers based on row, start_col */ 10203 img.stride = 10204 ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 10205 img.uv_stride = 10206 ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 10207 img.y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 10208 img.u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 10209 img.v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 10210 img.y += (img.stride * row + start_col) * 16; 10211 img.u += (img.uv_stride * row + start_col) * 8; 10212 img.v += (img.uv_stride * row + start_col) * 8; 10213 mbi = ctx->mb_info_rows[row] + start_col; 10214 coeffs = ctx->tokens[row & 10215 (ctx->token_hdr.partitions - 1)].coeffs + 10216 25 * 16 * start_col; 10218 /* Fix up the out-of-frame pixels */ 10219 if (start_col == 0) 10220 { 10221 fixup_left(img.y, 16, img.stride, row, mbi->base.y_mode); 10222 fixup_left(img.u, 8, img.uv_stride, row, mbi->base.uv_mode); 10223 fixup_left(img.v, 8, img.uv_stride, row, mbi->base.uv_mode); 10225 if (row == 0) 10226 *(img.y - img.stride - 1) = 127; 10227 } 10229 for (col = start_col; col < start_col + num_cols; col++) 10230 { 10231 if (row == 0) 10232 { 10233 fixup_above(img.y, 16, img.stride, col, 10234 mbi->base.y_mode); 10235 fixup_above(img.u, 8, img.uv_stride, col, 10236 mbi->base.uv_mode); 10237 fixup_above(img.v, 8, img.uv_stride, col, 10238 mbi->base.uv_mode); 10239 } 10241 if (mbi->base.y_mode <= B_PRED) 10242 { 10243 predict_intra_luma(img.y, img.stride, mbi, coeffs); 10244 predict_intra_chroma(img.u, img.v, img.uv_stride, mbi, 10245 coeffs); 10246 } 10247 else 10248 { 10249 if (mbi->base.y_mode != SPLITMV) // && != BPRED 10250 fixup_dc_coeffs(mbi, coeffs); 10252 if (mbi->base.need_mc_border) 10253 predict_inter_emulated_edge(ctx, &img, coeffs, mbi, 10254 col, row); 10255 else 10256 predict_inter(ctx, &img, coeffs, mbi); 10257 } 10259 /* Advance to the next macroblock */ 10260 mbi++; 10261 img.y += 16; 10262 img.u += 8; 10263 img.v += 8; 10264 coeffs += 25 * 16; 10265 } 10266 if (col == ctx->mb_cols) 10267 { 10268 /* Extend the last row by four pixels for intra prediction. 10269 * This will be propagated later by copy_down. 10270 */ 10271 uint32_t *extend = (uint32_t *)(img.y + 15 * img.stride); 10272 uint32_t val = 0x01010101 * img.y[-1 + 15 * img.stride]; 10273 *extend = val; 10274 } 10275 } 10277 ---- End code block ---------------------------------------- 10279 20.15. predict.h 10281 ---- Begin code block -------------------------------------- 10283 /* 10284 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10285 * 10286 * Use of this source code is governed by a BSD-style license 10287 * that can be found in the LICENSE file in the root of the source 10288 * tree. An additional intellectual property rights grant can be 10289 * found in the file PATENTS. All contributing project authors may 10290 * be found in the AUTHORS file in the root of the source tree. 10291 */ 10292 #ifndef PREDICT_H 10293 #define PREDICT_H 10295 void 10296 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx); 10298 void 10299 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx); 10301 void 10302 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10303 unsigned int row, 10304 unsigned int start_col, 10305 unsigned int num_cols); 10307 void 10308 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg); 10310 struct ref_cnt_img * 10311 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg); 10313 struct ref_cnt_img * 10314 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames); 10316 #endif 10318 ---- End code block ---------------------------------------- 10320 20.16. tokens.c 10322 ---- Begin code block -------------------------------------- 10323 /* 10324 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10325 * 10326 * Use of this source code is governed by a BSD-style license 10327 * that can be found in the LICENSE file in the root of the source 10328 * tree. An additional intellectual property rights grant can be 10329 * found in the file PATENTS. All contributing project authors may 10330 * be found in the AUTHORS file in the root of the source tree. 10331 */ 10332 #include "vpx_codec_internal.h" 10333 #include "dixie.h" 10334 #include "tokens.h" 10335 #include 10336 #include 10337 #include 10339 enum 10340 { 10341 EOB_CONTEXT_NODE, 10342 ZERO_CONTEXT_NODE, 10343 ONE_CONTEXT_NODE, 10344 LOW_VAL_CONTEXT_NODE, 10345 TWO_CONTEXT_NODE, 10346 THREE_CONTEXT_NODE, 10347 HIGH_LOW_CONTEXT_NODE, 10348 CAT_ONE_CONTEXT_NODE, 10349 CAT_THREEFOUR_CONTEXT_NODE, 10350 CAT_THREE_CONTEXT_NODE, 10351 CAT_FIVE_CONTEXT_NODE 10352 }; 10353 enum 10354 { 10355 ZERO_TOKEN, 10356 ONE_TOKEN, 10357 TWO_TOKEN, 10358 THREE_TOKEN, 10359 FOUR_TOKEN, 10360 DCT_VAL_CATEGORY1, 10361 DCT_VAL_CATEGORY2, 10362 DCT_VAL_CATEGORY3, 10363 DCT_VAL_CATEGORY4, 10364 DCT_VAL_CATEGORY5, 10365 DCT_VAL_CATEGORY6, 10366 DCT_EOB_TOKEN, 10367 MAX_ENTROPY_TOKENS 10368 }; 10369 struct extrabits 10370 { 10371 short min_val; 10372 short length; 10373 unsigned char probs[12]; 10374 }; 10375 static const unsigned int left_context_index[25] = 10376 { 10377 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10378 4, 4, 5, 5, 6, 6, 7, 7, 8 10379 }; 10380 static const unsigned int above_context_index[25] = 10381 { 10382 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10383 4, 5, 4, 5, 6, 7, 6, 7, 8 10384 }; 10385 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10386 static const unsigned int bands_x[16] = 10387 { 10388 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10389 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10390 }; 10391 #undef X 10392 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10393 { 10394 { 0, -1, { 0, 0, 0, 0, 0, 0, 10395 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10396 { 1, 0, { 0, 0, 0, 0, 0, 0, 10397 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10398 { 2, 0, { 0, 0, 0, 0, 0, 0, 10399 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10400 { 3, 0, { 0, 0, 0, 0, 0, 0, 10401 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10402 { 4, 0, { 0, 0, 0, 0, 0, 0, 10403 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10404 { 5, 0, {159, 0, 0, 0, 0, 0, 10405 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10406 { 7, 1, {145, 165, 0, 0, 0, 0, 10407 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10408 {11, 2, {140, 148, 173, 0, 0, 0, 10409 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10410 {19, 3, {135, 140, 155, 176, 0, 0, 10411 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10412 {35, 4, {130, 134, 141, 157, 180, 0, 10413 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10414 {67, 10, {129, 130, 133, 140, 153, 177, 10415 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10416 { 0, -1, { 0, 0, 0, 0, 0, 0, 10417 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10419 }; 10420 static const unsigned int zigzag[16] = 10421 { 10422 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10423 }; 10425 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10426 v = (bool_get_bit(bool) ? -value_to_sign \ 10427 : value_to_sign) * dqf[!!c]; 10429 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10430 if (!bool_get(bool, probability)) goto branch; 10432 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10433 if (!bool_get(bool, probability)) \ 10434 { \ 10435 prob = type_probs; \ 10436 if(c<15) {\ 10437 ++c; \ 10438 prob += bands_x[c]; \ 10439 goto branch; \ 10440 }\ 10441 else \ 10442 goto BLOCK_FINISHED; /* for malformed input */\ 10443 } 10445 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10446 DECODE_AND_APPLYSIGN(val) \ 10447 prob = type_probs + (ENTROPY_NODES*2); \ 10448 if(c < 15){\ 10449 b_tokens[zigzag[c]] = v; \ 10450 ++c; \ 10451 goto DO_WHILE; }\ 10452 b_tokens[zigzag[15]] = v; \ 10453 goto BLOCK_FINISHED; 10455 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10456 val += bool_get(bool, extrabits[t].probs[bits_count]) << \ 10457 bits_count; 10459 static int 10460 decode_mb_tokens(struct bool_decoder *bool, 10461 token_entropy_ctx_t left, 10462 token_entropy_ctx_t above, 10463 short *tokens, 10464 enum prediction_mode mode, 10465 coeff_probs_table_t probs, 10466 short factor[TOKEN_BLOCK_TYPES][2]) 10467 { 10468 int i, stop, type; 10469 int c, t, v; 10470 int val, bits_count; 10471 int eob_mask; 10472 short *b_tokens; // tokens for this block 10473 unsigned char *type_probs; // probabilities for this block type 10474 unsigned char *prob; 10475 short *dqf; 10477 eob_mask = 0; 10479 if (mode != B_PRED && mode != SPLITMV) 10480 { 10481 i = 24; 10482 stop = 24; 10483 type = 1; 10484 b_tokens = tokens + 24 * 16; 10485 dqf = factor[TOKEN_BLOCK_Y2]; 10486 } 10487 else 10488 { 10489 i = 0; 10490 stop = 16; 10491 type = 3; 10492 b_tokens = tokens; 10493 dqf = factor[TOKEN_BLOCK_Y1]; 10494 } 10496 /* Save a pointer to the coefficient probs for the current type. 10497 * Need to repeat this whenever type changes. 10498 */ 10499 type_probs = probs[type][0][0]; 10501 BLOCK_LOOP: 10502 t = left[left_context_index[i]] + above[above_context_index[i]]; 10503 c = !type; /* all blocks start at 0 except type 0, which starts 10504 * at 1. */ 10506 prob = type_probs; 10507 prob += t * ENTROPY_NODES; 10509 DO_WHILE: 10510 prob += bands_x[c]; 10511 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], 10512 BLOCK_FINISHED); 10514 CHECK_0_: 10515 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10516 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10517 ONE_CONTEXT_NODE_0_); 10518 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10519 LOW_VAL_CONTEXT_NODE_0_); 10520 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10521 HIGH_LOW_CONTEXT_NODE_0_); 10522 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10523 CAT_THREEFOUR_CONTEXT_NODE_0_); 10524 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10525 CAT_FIVE_CONTEXT_NODE_0_); 10526 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10527 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10529 do 10530 { 10531 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, 10532 bits_count); 10533 bits_count -- ; 10534 } 10535 while (bits_count >= 0); 10537 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10539 CAT_FIVE_CONTEXT_NODE_0_: 10540 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10541 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10542 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10543 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10544 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10545 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10546 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10548 CAT_THREEFOUR_CONTEXT_NODE_0_: 10549 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10550 CAT_THREE_CONTEXT_NODE_0_); 10551 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10552 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10553 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10554 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10555 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10556 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10558 CAT_THREE_CONTEXT_NODE_0_: 10559 val = extrabits[DCT_VAL_CATEGORY3].min_val; 10560 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 10561 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 10562 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 10563 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10565 HIGH_LOW_CONTEXT_NODE_0_: 10566 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 10567 CAT_ONE_CONTEXT_NODE_0_); 10569 val = extrabits[DCT_VAL_CATEGORY2].min_val; 10570 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 10571 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 10572 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10574 CAT_ONE_CONTEXT_NODE_0_: 10575 val = extrabits[DCT_VAL_CATEGORY1].min_val; 10576 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 10577 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10579 LOW_VAL_CONTEXT_NODE_0_: 10580 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 10581 TWO_CONTEXT_NODE_0_); 10582 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 10583 THREE_CONTEXT_NODE_0_); 10584 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 10586 THREE_CONTEXT_NODE_0_: 10587 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 10589 TWO_CONTEXT_NODE_0_: 10590 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 10592 ONE_CONTEXT_NODE_0_: 10593 DECODE_AND_APPLYSIGN(1); 10594 prob = type_probs + ENTROPY_NODES; 10596 if (c < 15) 10597 { 10598 b_tokens[zigzag[c]] = v; 10599 ++c; 10600 goto DO_WHILE; 10601 } 10603 b_tokens[zigzag[15]] = v; 10604 BLOCK_FINISHED: 10605 eob_mask |= (c > 1) << i; 10606 t = (c != !type); // any nonzero data? 10607 eob_mask |= t << 31; 10609 left[left_context_index[i]] = above[above_context_index[i]] = t; 10610 b_tokens += 16; 10612 i++; 10614 if (i < stop) 10615 goto BLOCK_LOOP; 10617 if (i == 25) 10618 { 10619 type = 0; 10620 i = 0; 10621 stop = 16; 10622 type_probs = probs[type][0][0]; 10623 b_tokens = tokens; 10624 dqf = factor[TOKEN_BLOCK_Y1]; 10625 goto BLOCK_LOOP; 10626 } 10628 if (i == 16) 10629 { 10630 type = 2; 10631 type_probs = probs[type][0][0]; 10632 stop = 24; 10633 dqf = factor[TOKEN_BLOCK_UV]; 10634 goto BLOCK_LOOP; 10635 } 10637 return eob_mask; 10638 } 10640 static void 10641 reset_row_context(token_entropy_ctx_t *left) 10642 { 10643 memset(left, 0, sizeof(*left)); 10644 } 10646 static void 10647 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 10648 { 10649 memset(above, 0, cols * sizeof(*above)); 10650 } 10652 static void 10653 reset_mb_context(token_entropy_ctx_t *left, 10654 token_entropy_ctx_t *above, 10655 enum prediction_mode mode) 10656 { 10657 /* Reset the macroblock context on the left and right. We have to 10658 * preserve the context of the second order block if this mode 10659 * would not have updated it. 10660 */ 10661 memset(left, 0, sizeof((*left)[0]) * 8); 10662 memset(above, 0, sizeof((*above)[0]) * 8); 10664 if (mode != B_PRED && mode != SPLITMV) 10665 { 10666 (*left)[8] = 0; 10667 (*above)[8] = 0; 10668 } 10669 } 10671 void 10672 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 10673 unsigned int partition, 10674 unsigned int row, 10675 unsigned int start_col, 10676 unsigned int num_cols) 10677 { 10678 struct token_decoder *tokens = &ctx->tokens[partition]; 10679 short coeffs = tokens->coeffs + 25 * 16 * start_col; 10680 unsigned int col; 10681 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 10682 + start_col; 10683 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 10684 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 10686 if (row == 0) 10687 reset_above_context(above, num_cols); 10689 if (start_col == 0) 10690 reset_row_context(left); 10692 for (col = start_col; col < start_col + num_cols; col++) 10693 { 10694 memset(coeffs, 0, 25 * 16 * sizeof(short)); 10696 if (mbi->base.skip_coeff) 10697 { 10698 reset_mb_context(left, above, mbi->base.y_mode); 10699 mbi->base.eob_mask = 0; 10700 } 10701 else 10702 { 10703 struct dequant_factors *dqf; 10705 dqf = ctx->dequant_factors + mbi->base.segment_id; 10706 mbi->base.eob_mask = 10707 decode_mb_tokens(&tokens->bool, 10708 *left, *above, 10709 coeffs, 10710 mbi->base.y_mode, 10711 ctx->entropy_hdr.coeff_probs, 10712 dqf->factor); 10713 } 10715 above++; 10716 mbi++; 10717 coeffs += 25 * 16; 10718 } 10719 } 10721 void 10722 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 10723 { 10724 unsigned int partitions = ctx->token_hdr.partitions; 10726 if (ctx->frame_hdr.frame_size_updated) 10727 { 10728 unsigned int i; 10729 unsigned int coeff_row_sz = 10730 ctx->mb_cols * 25 * 16 * sizeof(short); 10732 for (i = 0; i < partitions; i++) 10733 { 10734 free(ctx->tokens[i].coeffs); 10735 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 10737 if (!ctx->tokens[i].coeffs) 10738 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10739 NULL); 10740 } 10742 free(ctx->above_token_entropy_ctx); 10743 ctx->above_token_entropy_ctx = 10744 calloc(ctx->mb_cols, 10745 sizeof(*ctx->above_token_entropy_ctx)); 10747 if (!ctx->above_token_entropy_ctx) 10748 vpx_internal_error(&ctx->error, 10749 VPX_CODEC_MEM_ERROR, NULL); 10750 } 10751 } 10753 void 10754 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 10755 { 10756 int i; 10758 for (i = 0; i < MAX_PARTITIONS; i++) 10759 free(ctx->tokens[i].coeffs); 10761 free(ctx->above_token_entropy_ctx); 10762 } 10764 ---- End code block ---------------------------------------- 10766 20.17. tokens.h 10768 ---- Begin code block -------------------------------------- 10770 /* 10771 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10772 * 10773 * Use of this source code is governed by a BSD-style license 10774 * that can be found in the LICENSE file in the root of the source 10775 * tree. An additional intellectual property rights grant can be 10776 * found in the file PATENTS. All contributing project authors may 10777 * be found in the AUTHORS file in the root of the source tree. 10778 */ 10779 #ifndef TOKENS_H 10780 #define TOKENS_H 10782 void 10783 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx); 10785 void 10786 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx); 10788 void 10789 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 10790 unsigned int partition, 10791 unsigned int row, 10792 unsigned int start_col, 10793 unsigned int num_cols); 10795 #endif 10797 ---- End code block ---------------------------------------- 10799 20.18. vp8_prob_data.h 10801 ---- Begin code block -------------------------------------- 10803 static const 10804 unsigned char k_coeff_entropy_update_probs[BLOCK_TYPES][COEF_BANDS] 10805 [PREV_COEF_CONTEXTS] 10806 [ENTROPY_NODES] = 10807 { 10808 { 10809 { 10810 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10811 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10812 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10813 }, 10814 { 10815 {176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10816 {223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 10817 {249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10818 }, 10819 { 10820 {255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 10821 {234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10822 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10823 }, 10824 { 10825 {255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10826 {239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10827 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10828 }, 10829 { 10830 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10831 {251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10832 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10833 }, 10834 { 10835 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10836 {251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10837 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10838 }, 10839 { 10840 {255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255}, 10841 {250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255}, 10842 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10843 }, 10844 { 10845 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10846 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10847 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10848 }, 10849 }, 10850 { 10851 { 10852 {217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10853 {225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255}, 10854 {234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255}, 10855 }, 10856 { 10857 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10858 {223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10859 {238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255}, 10860 }, 10861 { 10862 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10863 {249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10864 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10865 }, 10866 { 10867 {255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10868 {247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10869 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10870 }, 10871 { 10872 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10873 {252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10874 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10875 }, 10876 { 10877 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10878 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10879 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10880 }, 10881 { 10882 {255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10883 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10884 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10885 }, 10886 { 10887 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10888 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10889 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10890 }, 10891 }, 10892 { 10893 { 10894 {186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255}, 10895 {234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255}, 10896 {251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255}, 10897 }, 10898 { 10899 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10900 {236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10901 {251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255}, 10902 }, 10903 { 10904 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10905 {254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10906 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10908 }, 10909 { 10910 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10911 {254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10912 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10913 }, 10914 { 10915 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10916 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10917 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10918 }, 10919 { 10920 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10921 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10922 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10923 }, 10924 { 10925 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10926 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10927 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10928 }, 10929 { 10930 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10931 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10932 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10933 }, 10934 }, 10935 { 10936 { 10937 {248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10938 {250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255}, 10939 {248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255}, 10940 }, 10941 { 10942 {255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10943 {246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10944 {252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255}, 10945 }, 10946 { 10947 {255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 10948 {248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10949 {253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255}, 10950 }, 10951 { 10952 {255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10953 {245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10954 {253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10955 }, 10956 { 10957 {255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10958 {252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10959 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10960 }, 10961 { 10962 {255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10963 {249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10964 {255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10965 }, 10966 { 10967 {255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10968 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10969 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10970 }, 10971 { 10972 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10973 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10974 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10975 }, 10976 }, 10977 }; 10979 static const 10980 unsigned char k_default_y_mode_probs [] = 10981 { 112, 86, 140, 37}; 10983 static const 10984 unsigned char k_default_uv_mode_probs [] = 10985 { 162, 101, 204}; 10987 static const 10988 unsigned char k_default_coeff_probs [BLOCK_TYPES][COEF_BANDS] 10989 [PREV_COEF_CONTEXTS][ENTROPY_NODES] = 10990 { 10991 { /* block type 0 */ 10992 { /* coeff band 0 */ 10993 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 10994 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 10995 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 10996 }, 10997 { /* coeff band 1 */ 10998 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 10999 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 11000 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 11002 }, 11003 { /* coeff band 2 */ 11004 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 11005 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 11006 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 11007 }, 11008 { /* coeff band 3 */ 11009 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 11010 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 11011 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 11012 }, 11013 { /* coeff band 4 */ 11014 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 11015 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 11016 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 11017 }, 11018 { /* coeff band 5 */ 11019 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 11020 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 11021 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 11022 }, 11023 { /* coeff band 6 */ 11024 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 11025 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 11026 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 11027 }, 11028 { /* coeff band 7 */ 11029 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11030 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11031 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11032 } 11033 }, 11034 { /* block type 1 */ 11035 { /* coeff band 0 */ 11036 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 11037 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 11038 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 11039 }, 11040 { /* coeff band 1 */ 11041 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 11042 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 11043 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 11044 }, 11045 { /* coeff band 2 */ 11046 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 11047 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 11048 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 11049 }, 11050 { /* coeff band 3 */ 11051 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 11052 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 11053 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 11054 }, 11055 { /* coeff band 4 */ 11056 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 11057 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 11058 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 11059 }, 11060 { /* coeff band 5 */ 11061 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 11062 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 11063 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 11064 }, 11065 { /* coeff band 6 */ 11066 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 11067 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 11068 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 11069 }, 11070 { /* coeff band 7 */ 11071 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 11072 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11073 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 11074 } 11075 }, 11076 { /* block type 2 */ 11077 { /* coeff band 0 */ 11078 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 11079 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 11080 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 11081 }, 11082 { /* coeff band 1 */ 11083 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 11084 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 11085 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 11086 }, 11087 { /* coeff band 2 */ 11088 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 11089 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 11090 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 11091 }, 11092 { /* coeff band 3 */ 11093 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 11094 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 11095 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 11096 }, 11097 { /* coeff band 4 */ 11098 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11099 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 11100 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11101 }, 11102 { /* coeff band 5 */ 11103 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11104 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11105 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11106 }, 11107 { /* coeff band 6 */ 11108 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 11109 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 11110 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11111 }, 11112 { /* coeff band 7 */ 11113 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11114 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11115 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11116 } 11117 }, 11118 { /* block type 3 */ 11119 { /* coeff band 0 */ 11120 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 11121 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 11122 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 11123 }, 11124 { /* coeff band 1 */ 11125 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 11126 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 11127 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 11128 }, 11129 { /* coeff band 2 */ 11130 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 11131 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 11132 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 11133 }, 11134 { /* coeff band 3 */ 11135 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 11136 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 11137 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 11138 }, 11139 { /* coeff band 4 */ 11140 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 11141 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 11142 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 11143 }, 11144 { /* coeff band 5 */ 11145 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 11146 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 11147 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 11148 }, 11149 { /* coeff band 6 */ 11150 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 11151 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 11152 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 11153 }, 11154 { /* coeff band 7 */ 11155 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11156 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11157 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11158 } 11159 } 11160 }; 11162 static const 11163 unsigned char k_mv_entropy_update_probs[2][MV_PROB_CNT] = 11164 { 11165 { 11166 237, 11167 246, 11168 253, 253, 254, 254, 254, 254, 254, 11169 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 11170 }, 11171 { 11172 231, 11173 243, 11174 245, 253, 254, 254, 254, 254, 254, 11175 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 11176 } 11177 }; 11179 static const 11180 unsigned char k_default_mv_probs[2][MV_PROB_CNT] = 11181 { 11182 { // row 11183 162, // is short 11184 128, // sign 11185 225, 146, 172, 147, 214, 39, 156, // short tree 11186 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 // long bits 11187 }, 11188 { 11189 164, 11190 128, 11191 204, 170, 119, 235, 140, 230, 228, 11192 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 11194 } 11195 }; 11197 ---- End code block ---------------------------------------- 11199 20.19. vpx_codec_internal.h 11201 ---- Begin code block -------------------------------------- 11203 /* 11204 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 11205 * 11206 * Use of this source code is governed by a BSD-style license 11207 * that can be found in the LICENSE file in the root of the source 11208 * tree. An additional intellectual property rights grant can be 11209 * found in the file PATENTS. All contributing project authors may 11210 * be found in the AUTHORS file in the root of the source tree. 11211 */ 11213 /*!\file vpx_codec_internal.h 11214 * \brief Describes the decoder algorithm interface for algorithm 11215 * implementations. 11216 * 11217 * This file defines the private structures and data types that are 11218 * only relevant to implementing an algorithm, as opposed to using 11219 * it. 11220 * 11221 * To create a decoder algorithm class, an interface structure is put 11222 * into the global namespace: 11223 *
11224	    *     my_codec.c:
11225	    *       vpx_codec_iface_t my_codec = {
11226	    *           "My Codec v1.0",
11227	    *           VPX_CODEC_ALG_ABI_VERSION,
11228	    *           ...
11229	    *       };
11230	    *     
11231 * 11232 * An application instantiates a specific decoder instance by using 11233 * vpx_codec_init() and a pointer to the algorithm's interface 11234 * structure: 11235 *
11236	    *     my_app.c:
11237	    *       extern vpx_codec_iface_t my_codec;
11238	    *       {
11239	    *           vpx_codec_ctx_t algo;
11240	    *           res = vpx_codec_init(&algo, &my_codec);
11241	    *       }
11242	    *     
11243 * 11244 * Once initialized, the instance is manged using other functions 11245 * from the vpx_codec_* family. 11246 */ 11247 #ifndef VPX_CODEC_INTERNAL_H 11248 #define VPX_CODEC_INTERNAL_H 11249 #include "vpx_decoder.h" 11250 #include 11252 /*!\brief Current ABI version number 11253 * 11254 * \internal 11255 * If this file is altered in any way that changes the ABI, this 11256 * value must be bumped. Examples include, but are not limited to, 11257 * changing types, removing or reassigning enums, 11258 * adding/removing/rearranging fields to structures 11259 */ 11260 #define VPX_CODEC_INTERNAL_ABI_VERSION (3) 11262 typedef struct vpx_codec_alg_priv vpx_codec_alg_priv_t; 11264 /*!\brief init function pointer prototype 11265 * 11266 * Performs algorithm-specific initialization of the decoder context. 11267 * This function is called by the generic vpx_codec_init() wrapper 11268 * function, so plugins implementing this interface may trust the 11269 * input parameters to be properly initialized. 11270 * 11271 * \param[in] ctx Pointer to this instance's context 11272 * \retval #VPX_CODEC_OK 11273 * The input stream was recognized and decoder initialized. 11274 * \retval #VPX_CODEC_MEM_ERROR 11275 * Memory operation failed. 11276 */ 11277 typedef vpx_codec_err_t (*vpx_codec_init_fn_t)(vpx_codec_ctx_t *ctx); 11279 /*!\brief destroy function pointer prototype 11280 * 11281 * Performs algorithm-specific destruction of the decoder context. 11282 * This function is called by the generic vpx_codec_destroy() wrapper 11283 * function, so plugins implementing this interface may trust the 11284 * input parameters to be properly initialized. 11286 * 11287 * \param[in] ctx Pointer to this instance's context 11288 * \retval #VPX_CODEC_OK 11289 * The input stream was recognized and decoder initialized. 11290 * \retval #VPX_CODEC_MEM_ERROR 11291 * Memory operation failed. 11292 */ 11293 typedef vpx_codec_err_t (*vpx_codec_destroy_fn_t)( 11294 vpx_codec_alg_priv_t *ctx); 11296 /*!\brief parse stream info function pointer prototype 11297 * 11298 * Performs high level parsing of the bitstream. This function is 11299 * called by the generic vpx_codec_parse_stream() wrapper function, 11300 * so plugins implementing this interface may trust the input 11301 * parameters to be properly initialized. 11302 * 11303 * \param[in] data Pointer to a block of data to parse 11304 * \param[in] data_sz Size of the data buffer 11305 * \param[in,out] si Pointer to stream info to update. The size 11306 * member \ref MUST be properly initialized, 11307 * but \ref MAY be clobbered by the 11308 * algorithm. This parameter \ref MAY be 11309 * NULL. 11310 * 11311 * \retval #VPX_CODEC_OK 11312 * Bitstream is parsable and stream information updated 11313 */ 11314 typedef vpx_codec_err_t (*vpx_codec_peek_si_fn_t)( 11315 const uint8_t *data, 11316 unsigned int data_sz, 11317 vpx_codec_stream_info_t *si); 11319 /*!\brief Return information about the current stream. 11320 * 11321 * Returns information about the stream that has been parsed during 11322 * decoding. 11323 * 11324 * \param[in] ctx Pointer to this instance's context 11325 * \param[in,out] si Pointer to stream info to update. The size 11326 * member \ref MUST be properly initialized, 11327 * but \ref MAY be clobbered by the 11328 * algorithm. This parameter \ref MAY be 11329 * NULL. 11330 * 11331 * \retval #VPX_CODEC_OK 11332 * Bitstream is parsable and stream information updated 11333 */ 11335 typedef vpx_codec_err_t (*vpx_codec_get_si_fn_t)( 11336 vpx_codec_alg_priv_t *ctx, 11337 vpx_codec_stream_info_t *si); 11339 /*!\brief control function pointer prototype 11340 * 11341 * This function is used to exchange algorithm specific data with the 11342 * decoder instance. This can be used to implement features specific 11343 * to a particular algorithm. 11344 * 11345 * This function is called by the generic vpx_codec_control() wrapper 11346 * function, so plugins implementing this interface may trust the 11347 * input parameters to be properly initialized. However, this 11348 * interface does not provide type safety for the exchanged data or 11349 * assign meanings to the control codes. Those details should be 11350 * specified in the algorithm's header file. In particular, the 11351 * ctrl_id parameter is guaranteed to exist in the algorithm's 11352 * control mapping table, and the data paramter may be NULL. 11353 * 11354 * 11355 * \param[in] ctx Pointer to this instance's context 11356 * \param[in] ctrl_id Algorithm specific control identifier 11357 * \param[in,out] data Data to exchange with algorithm instance. 11358 * 11359 * \retval #VPX_CODEC_OK 11360 * The internal state data was deserialized. 11361 */ 11362 typedef vpx_codec_err_t (*vpx_codec_control_fn_t)( 11363 vpx_codec_alg_priv_t *ctx, 11364 int ctrl_id, 11365 va_list ap); 11367 /*!\brief control function pointer mapping 11368 * 11369 * This structure stores the mapping between control identifiers and 11370 * implementing functions. Each algorithm provides a list of these 11371 * mappings. This list is searched by the vpx_codec_control() wrapper 11372 * function to determine which function to invoke. The special 11373 * value {0, NULL} is used to indicate end-of-list, and must be 11374 * present. The special value {0, } can be used as a 11375 * catch-all mapping. This implies that ctrl_id values chosen by the 11376 * algorithm \ref MUST be non-zero. 11377 */ 11378 typedef const struct 11379 { 11380 int ctrl_id; 11381 vpx_codec_control_fn_t fn; 11382 } vpx_codec_ctrl_fn_map_t; 11383 /*!\brief decode data function pointer prototype 11384 * 11385 * Processes a buffer of coded data. If the processing results in a 11386 * new decoded frame becoming available, #VPX_CODEC_CB_PUT_SLICE and 11387 * #VPX_CODEC_CB_PUT_FRAME events are generated as appropriate. This 11388 * function is called by the generic vpx_codec_decode() wrapper 11389 * function, so plugins implementing this interface may trust the 11390 * input parameters to be properly initialized. 11391 * 11392 * \param[in] ctx Pointer to this instance's context 11393 * \param[in] data Pointer to this block of new coded data. If 11394 * NULL, a #VPX_CODEC_CB_PUT_FRAME event is 11395 * posted for the previously decoded frame. 11396 * \param[in] data_sz Size of the coded data, in bytes. 11397 * 11398 * \return Returns #VPX_CODEC_OK if the coded data was processed 11399 * completely and future pictures can be decoded without 11400 * error. Otherwise, see the descriptions of the other error 11401 * codes in ::vpx_codec_err_t for recoverability 11402 * capabilities. 11403 */ 11404 typedef vpx_codec_err_t (*vpx_codec_decode_fn_t)( 11405 vpx_codec_alg_priv_t *ctx, 11406 const uint8_t *data, 11407 unsigned int data_sz, 11408 void *user_priv, 11409 long deadline); 11411 /*!\brief Decoded frames iterator 11412 * 11413 * Iterates over a list of the frames available for display. The 11414 * iterator storage should be initialized to NULL to start the 11415 * iteration. Iteration is complete when this function returns NULL. 11416 * 11417 * The list of available frames becomes valid upon completion of the 11418 * vpx_codec_decode call, and remains valid until the next call to 11419 * vpx_codec_decode. 11420 * 11421 * \param[in] ctx Pointer to this instance's context 11422 * \param[in out] iter Iterator storage, initialized to NULL 11423 * 11424 * \return Returns a pointer to an image, if one is ready for 11425 * display. Frames produced will always be in PTS 11426 * (presentation time stamp) order. 11427 */ 11428 typedef vpx_image_t*(*vpx_codec_get_frame_fn_t)( 11429 vpx_codec_alg_priv_t *ctx, 11430 vpx_codec_iter_t *iter); 11432 /*\brief e_xternal Memory Allocation memory map get iterator 11433 * 11434 * Iterates over a list of the memory maps requested by the decoder. 11435 * The iterator storage should be initialized to NULL to start the 11436 * iteration. Iteration is complete when this function returns NULL. 11437 * 11438 * \param[in out] iter Iterator storage, initialized to NULL 11439 * 11440 * \return Returns a pointer to an memory segment descriptor, or NULL 11441 * to indicate end-of-list. 11442 */ 11443 typedef vpx_codec_err_t (*vpx_codec_get_mmap_fn_t)( 11444 const vpx_codec_ctx_t *ctx, 11445 vpx_codec_mmap_t *mmap, 11446 vpx_codec_iter_t *iter); 11448 /*\brief e_xternal Memory Allocation memory map set iterator 11449 * 11450 * Sets a memory descriptor inside the decoder instance. 11451 * 11452 * \param[in] ctx Pointer to this instance's context 11453 * \param[in] mmap Memory map to store. 11454 * 11455 * \retval #VPX_CODEC_OK 11456 * The memory map was accepted and stored. 11457 * \retval #VPX_CODEC_MEM_ERROR 11458 * The memory map was rejected. 11459 */ 11460 typedef vpx_codec_err_t (*vpx_codec_set_mmap_fn_t)( 11461 vpx_codec_ctx_t *ctx, 11462 const vpx_codec_mmap_t *mmap); 11464 typedef vpx_codec_err_t (*vpx_codec_encode_fn_t)( 11465 vpx_codec_alg_priv_t *ctx, 11466 const vpx_image_t *img, 11467 vpx_codec_pts_t pts, 11468 unsigned long duration, 11469 vpx_enc_frame_flags_t flags, 11470 unsigned long deadline); 11471 typedef const vpx_codec_cx_pkt_t*(*vpx_codec_get_cx_data_fn_t)( 11472 vpx_codec_alg_priv_t *ctx, 11473 vpx_codec_iter_t *iter); 11475 typedef vpx_codec_err_t 11476 (*vpx_codec_enc_config_set_fn_t)( 11477 vpx_codec_alg_priv_t *ctx, 11478 const vpx_codec_enc_cfg_t *cfg); 11479 typedef vpx_fixed_buf_t * 11480 (*vpx_codec_get_global_headers_fn_t)(vpx_codec_alg_priv_t *ctx); 11482 typedef vpx_image_t * 11483 (*vpx_codec_get_preview_frame_fn_t)(vpx_codec_alg_priv_t *ctx); 11485 /*!\brief usage configuration mapping 11486 * 11487 * This structure stores the mapping between usage identifiers and 11488 * configuration structures. Each algorithm provides a list of these 11489 * mappings. This list is searched by the 11490 * vpx_codec_enc_config_default() wrapper function to determine which 11491 * config to return. The special value {-1, {0}} is used to indicate 11492 * end-of-list, and must be present. At least one mapping must be 11493 * present, in addition to the end-of-list. 11494 * 11495 */ 11496 typedef const struct 11497 { 11498 int usage; 11499 vpx_codec_enc_cfg_t cfg; 11500 } vpx_codec_enc_cfg_map_t; 11502 #define NOT_IMPLEMENTED 0 11504 /*!\brief Decoder algorithm interface interface 11505 * 11506 * All decoders \ref MUST expose a variable of this type. 11507 */ 11508 struct vpx_codec_iface 11509 { 11510 const char *name; 11511 int abi_version; 11512 vpx_codec_caps_t caps; 11513 vpx_codec_init_fn_t init; 11514 vpx_codec_destroy_fn_t destroy; 11515 vpx_codec_ctrl_fn_map_t *ctrl_maps; 11516 vpx_codec_get_mmap_fn_t get_mmap; 11517 vpx_codec_set_mmap_fn_t set_mmap; 11518 struct 11519 { 11520 vpx_codec_peek_si_fn_t peek_si; 11521 vpx_codec_get_si_fn_t get_si; 11522 vpx_codec_decode_fn_t decode; 11523 vpx_codec_get_frame_fn_t get_frame; 11524 } dec; 11525 struct 11526 { 11527 vpx_codec_enc_cfg_map_t *cfg_maps; 11528 vpx_codec_encode_fn_t encode; 11529 vpx_codec_get_cx_data_fn_t get_cx_data; 11530 vpx_codec_enc_config_set_fn_t cfg_set; 11531 vpx_codec_get_global_headers_fn_t get_glob_hdrs; 11532 vpx_codec_get_preview_frame_fn_t get_preview; 11533 } enc; 11534 }; 11536 /*!\brief Callback function pointer / user data pair storage */ 11537 typedef struct vpx_codec_priv_cb_pair 11538 { 11539 union 11540 { 11541 vpx_codec_put_frame_cb_fn_t put_frame; 11542 vpx_codec_put_slice_cb_fn_t put_slice; 11543 }; 11544 void *user_priv; 11545 } vpx_codec_priv_cb_pair_t; 11547 /*!\brief Instance private storage 11548 * 11549 * This structure is allocated by the algorithm's init function. It 11550 * can be extended in one of two ways. First, a second, algorithm 11551 * specific structure can be allocated and the priv member pointed to 11552 * it. Alternatively, this structure can be made the first member of 11553 * the algorithm specific structure, and the pointer casted to the 11554 * proper type. 11555 */ 11556 struct vpx_codec_priv 11557 { 11558 unsigned int sz; 11559 vpx_codec_iface_t *iface; 11560 struct vpx_codec_alg_priv *alg_priv; 11561 const char *err_detail; 11562 vpx_codec_flags_t init_flags; 11563 struct 11564 { 11565 vpx_codec_priv_cb_pair_t put_frame_cb; 11566 vpx_codec_priv_cb_pair_t put_slice_cb; 11567 } dec; 11568 struct 11569 { 11570 int tbd; 11571 struct vpx_fixed_buf cx_data_dst_buf; 11572 unsigned int cx_data_pad_before; 11573 unsigned int cx_data_pad_after; 11574 vpx_codec_cx_pkt_t cx_data_pkt; 11575 } enc; 11576 }; 11578 #undef VPX_CTRL_USE_TYPE 11579 #define VPX_CTRL_USE_TYPE(id, typ) \ 11580 static typ id##__value(va_list args) \ 11581 {return va_arg(args, typ);} \ 11582 static typ id##__convert(void *x)\ 11583 {\ 11584 union\ 11585 {\ 11586 void *x;\ 11587 typ d;\ 11588 } u;\ 11589 u.x = x;\ 11590 return u.d;\ 11591 } 11593 #undef VPX_CTRL_USE_TYPE_DEPRECATED 11594 #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \ 11595 static typ id##__value(va_list args) \ 11596 {return va_arg(args, typ);} \ 11597 static typ id##__convert(void *x)\ 11598 {\ 11599 union\ 11600 {\ 11601 void *x;\ 11602 typ d;\ 11603 } u;\ 11604 u.x = x;\ 11605 return u.d;\ 11606 } 11608 #define CAST(id, arg) id##__value(arg) 11609 #define RECAST(id, x) id##__convert(x) 11611 /* Internal Utility Functions 11612 * 11613 * The following functions are indended to be used inside algorithms 11614 * as utilities for manipulating vpx_codec_* data structures. 11615 */ 11616 struct vpx_codec_pkt_list 11617 { 11618 unsigned int cnt; 11619 unsigned int max; 11620 struct vpx_codec_cx_pkt pkts[1]; 11621 }; 11623 #define vpx_codec_pkt_list_decl(n)\ 11624 union {struct vpx_codec_pkt_list head;\ 11625 struct {struct vpx_codec_pkt_list head;\ 11626 struct vpx_codec_cx_pkt pkts[n];} alloc;} 11628 #define vpx_codec_pkt_list_init(m)\ 11629 (m)->alloc.head.cnt = 0,\ 11630 (m)->alloc.head.max = \ 11631 sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0]) 11633 int 11634 vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *, 11635 const struct vpx_codec_cx_pkt *); 11637 const vpx_codec_cx_pkt_t* 11638 vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list, 11639 vpx_codec_iter_t *iter); 11641 #include 11642 #include 11643 struct vpx_internal_error_info 11644 { 11645 vpx_codec_err_t error_code; 11646 int has_detail; 11647 char detail[80]; 11648 int setjmp; 11649 jmp_buf jmp; 11650 }; 11652 static void vpx_internal_error(struct vpx_internal_error_info *info, 11653 vpx_codec_err_t error, 11654 const char *fmt, 11655 ...) 11656 { 11657 va_list ap; 11659 info->error_code = error; 11660 info->has_detail = 0; 11662 if (fmt) 11663 { 11664 size_t sz = sizeof(info->detail); 11665 info->has_detail = 1; 11666 va_start(ap, fmt); 11667 vsnprintf(info->detail, sz - 1, fmt, ap); 11668 va_end(ap); 11669 info->detail[sz-1] = '\0'; 11670 } 11672 if (info->setjmp) 11673 longjmp(info->jmp, info->error_code); 11674 } 11675 #endif 11677 ---- End code block ---------------------------------------- 11679 20.20. vpx_decoder.h 11681 ---- Begin code block -------------------------------------- 11683 /* 11684 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 11685 * 11686 * Use of this source code is governed by a BSD-style license 11687 * that can be found in the LICENSE file in the root of the source 11688 * tree. An additional intellectual property rights grant can be 11689 * found in the file PATENTS. All contributing project authors may 11690 * be found in the AUTHORS file in the root of the source tree. 11691 */ 11693 /*!\defgroup decoder Decoder Algorithm Interface 11694 * \ingroup codec 11695 * This abstraction allows applications using this decoder to easily 11696 * support multiple video formats with minimal code duplication. This 11697 * section describes the interface common to all decoders. 11698 * @{ 11699 */ 11701 /*!\file vpx_decoder.h 11702 * \brief Describes the decoder algorithm interface to applications. 11703 * 11704 * This file describes the interface between an application and a 11705 * video decoder algorithm. 11706 * 11707 */ 11708 #ifdef __cplusplus 11709 extern "C" { 11710 #endif 11711 #ifndef VPX_DECODER_H 11712 #define VPX_DECODER_H 11713 #include "vpx_codec.h" 11715 /*!\brief Current ABI version number 11716 * 11717 * \internal 11718 * If this file is altered in any way that changes the ABI, this 11719 * value must be bumped. Examples include, but are not limited 11720 * to, changing types, removing or reassigning enums, 11721 * adding/removing/rearranging fields to structures 11722 */ 11723 #define VPX_DECODER_ABI_VERSION (2 + VPX_CODEC_ABI_VERSION) 11725 /*! \brief Decoder capabilities bitfield 11726 * 11727 * Each decoder advertises the capabilities it supports as part 11728 * of its ::vpx_codec_iface_t interface structure. Capabilities 11729 * are extra interfaces or functionality, and are not required 11730 * to be supported by a decoder. 11731 * 11732 * The available flags are specifiedby VPX_CODEC_CAP_* defines. 11733 */ 11734 #define VPX_CODEC_CAP_PUT_SLICE 0x10000 /**< Will issue put_slice 11735 callbacks */ 11736 #define VPX_CODEC_CAP_PUT_FRAME 0x20000 /**< Will issue put_frame 11737 callbacks */ 11738 #define VPX_CODEC_CAP_POSTPROC 0x40000 /**< Can postprocess decoded 11739 frame */ 11741 /*! \brief Initialization-time Feature Enabling 11742 * 11743 * Certain codec features must be known at initialization time, 11744 * to allow for proper memory allocation. 11745 * 11746 * The available flags are specified by VPX_CODEC_USE_* defines. 11747 */ 11748 #define VPX_CODEC_USE_POSTPROC 0x10000 /**< Postprocess decoded 11749 frame */ 11751 /*!\brief Stream properties 11752 * 11753 * This structure is used to query or set properties of the 11754 * decoded stream. Algorithms may extend this structure with data 11755 * specific to their bitstream by setting the sz member 11756 * appropriately. 11757 */ 11758 typedef struct vpx_codec_stream_info 11759 { 11760 unsigned int sz; /**< Size of this structure */ 11761 unsigned int w; /**< Width (or 0 for unknown/default) */ 11762 unsigned int h; /**< Height (or 0 for unknown/default) */ 11763 unsigned int is_kf; /**< Current frame is a keyframe */ 11764 } vpx_codec_stream_info_t; 11766 /* REQUIRED FUNCTIONS 11767 * 11768 * The following functions are required to be implemented for all 11769 * decoders. They represent the base case functionality expected 11770 * of all decoders. 11771 */ 11773 /*!\brief Initialization Configurations 11774 * 11775 * This structure is used to pass init time configuration options 11776 * to the decoder. 11777 */ 11778 typedef struct vpx_codec_dec_cfg 11779 { 11780 unsigned int threads; /**< Maximum number of threads to use, 11781 default 1 */ 11782 unsigned int w; /**< Width */ 11783 unsigned int h; /**< Height */ 11784 } vpx_codec_dec_cfg_t; /**< alias for struct vpx_codec_dec_cfg */ 11786 /*!\brief Initialize a decoder instance 11787 * 11788 * Initializes a decoder context using the given interface. 11789 * Applications should call the vpx_codec_dec_init convenience 11790 * macro instead of this function directly, to ensure that the 11791 * ABI version number parameter is properly initialized. 11792 * 11793 * In XMA mode (activated by setting VPX_CODEC_USE_XMA in the 11794 * flags parameter), the storage pointed to by the cfg parameter 11795 * must be kept readable and stable until all memory maps have 11796 * been set. 11797 * 11798 * \param[in] ctx Pointer to this instance's context. 11799 * \param[in] iface Pointer to the alogrithm interface to 11800 * use. 11801 * \param[in] cfg Configuration to use, if known. May be 11802 * NULL. 11803 * \param[in] flags Bitfield of VPX_CODEC_USE_* flags 11804 * \param[in] ver ABI version number. Must be set to 11805 * VPX_DECODER_ABI_VERSION 11806 * \retval #VPX_CODEC_OK 11807 * The decoder algorithm initialized. 11808 * \retval #VPX_CODEC_MEM_ERROR 11809 * Memory allocation failed. 11810 */ 11811 vpx_codec_err_t vpx_codec_dec_init_ver( 11812 vpx_codec_ctx_t *ctx, 11813 vpx_codec_iface_t *iface, 11814 vpx_codec_dec_cfg_t *cfg, 11815 vpx_codec_flags_t flags, 11816 int ver); 11818 /*!\brief Convenience macro for vpx_codec_dec_init_ver() 11819 * 11820 * Ensures the ABI version parameter is properly set. 11821 */ 11822 #define vpx_codec_dec_init(ctx, iface, cfg, flags) \ 11823 vpx_codec_dec_init_ver(ctx, iface, cfg, flags, \ 11824 VPX_DECODER_ABI_VERSION) 11826 /*!\brief Parse stream info from a buffer 11827 * 11828 * Performs high level parsing of the bitstream. Construction of 11829 * a decoder context is not necessary. Can be used to determine 11830 * if the bitstream is of the proper format, and to extract 11831 * information from the stream. 11832 * 11833 * \param[in] iface Pointer to the alogrithm interface 11834 * \param[in] data Pointer to a block of data to parse 11835 * \param[in] data_sz Size of the data buffer 11836 * \param[in,out] si Pointer to stream info to update. The 11837 * size member 11838 * \ref MUST be properly initialized, but 11839 * \ref MAY be clobbered by the 11840 * algorithm. This parameter \ref MAY be 11841 * NULL. 11842 * 11843 * \retval #VPX_CODEC_OK 11844 * Bitstream is parsable and stream information updated 11845 */ 11846 vpx_codec_err_t vpx_codec_peek_stream_info( 11847 vpx_codec_iface_t *iface, 11848 const uint8_t *data, 11849 unsigned int data_sz, 11850 vpx_codec_stream_info_t *si); 11852 /*!\brief Return information about the current stream. 11853 * 11854 * Returns information about the stream that has been parsed 11855 * during decoding. 11856 * 11857 * \param[in] ctx Pointer to this instance's context 11858 * \param[in,out] si Pointer to stream info to update. The 11859 * size member \ref MUST be properly 11860 * initialized, but \ref MAY be clobbered 11861 * by the algorithm. This parameter \ref 11862 * MAY be NULL. 11863 * 11864 * \retval #VPX_CODEC_OK 11865 * Bitstream is parsable and stream information updated 11866 */ 11867 vpx_codec_err_t vpx_codec_get_stream_info( 11868 vpx_codec_ctx_t *ctx, 11869 vpx_codec_stream_info_t *si); 11871 /*!\brief Decode data 11872 * 11873 * Processes a buffer of coded data. If the processing results in 11874 * a new decoded frame becoming available, PUT_SLICE and 11875 * PUT_FRAME events may be generated, as appropriate. Encoded 11876 * data \ref MUST be passed in DTS (decode time stamp) order. 11877 * Frames produced will always be in PTS (presentation time 11878 * stamp) order. 11879 * 11880 * \param[in] ctx Pointer to this instance's context 11881 * \param[in] data Pointer to this block of new coded 11882 * data. If NULL, a 11883 * VPX_CODEC_CB_PUT_FRAME event is posted 11884 * for the previously decoded frame. 11885 * \param[in] data_sz Size of the coded data, in bytes. 11886 * \param[in] user_priv Application specific data to associate 11887 * with this frame. 11888 * \param[in] deadline Soft deadline the decoder should 11889 * attempt to meet, in us. Set to zero 11890 * for unlimited. 11891 * 11892 * \return Returns #VPX_CODEC_OK if the coded data was processed 11893 * completely and future pictures can be decoded without 11894 * error. Otherwise, see the descriptions of the other 11895 * error codes in ::vpx_codec_err_t for recoverability 11896 * capabilities. 11897 */ 11898 vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, 11899 const uint8_t *data, 11900 unsigned int data_sz, 11901 void *user_priv, 11902 long deadline); 11904 /*!\brief Decoded frames iterator 11905 * 11906 * Iterates over a list of the frames available for display. The 11907 * iterator storage should be initialized to NULL to start the 11908 * iteration. Iteration is complete when this function returns 11909 * NULL. 11910 * 11911 * The list of available frames becomes valid upon completion of 11912 * the vpx_codec_decode call, and remains valid until the next 11913 * call to vpx_codec_decode. 11914 * 11915 * \param[in] ctx Pointer to this instance's context 11916 * \param[in,out] iter Iterator storage, initialized to NULL 11917 * 11918 * \return Returns a pointer to an image, if one is ready for 11919 * display. Frames produced will always be in PTS 11920 * (presentation time stamp) order. 11921 */ 11922 vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, 11923 vpx_codec_iter_t *iter); 11925 /*!\defgroup cap_put_frame Frame-Based Decoding Functions 11926 * 11927 * The following functions are required to be implemented for all 11928 * decoders that advertise the VPX_CODEC_CAP_PUT_FRAME 11929 * capability. Calling these functions for codecs that don't 11930 * advertise this capability will result in an error code being 11931 * returned, usually VPX_CODEC_ERROR 11932 * @{ 11933 */ 11935 /*!\brief put frame callback prototype 11936 * 11937 * This callback is invoked by the decoder to notify the 11938 * application of the availability of decoded image data. 11939 */ 11940 typedef void (*vpx_codec_put_frame_cb_fn_t)( 11941 void *user_priv, 11942 const vpx_image_t *img); 11944 /*!\brief Register for notification of frame completion. 11945 * 11946 * Registers a given function to be called when a decoded frame 11947 * is available. 11948 * 11949 * \param[in] ctx Pointer to this instance's context 11950 * \param[in] cb Pointer to the callback function 11951 * \param[in] user_priv User's private data 11952 * 11953 * \retval #VPX_CODEC_OK 11954 * Callback successfully registered. 11955 * \retval #VPX_CODEC_ERROR 11956 * Decoder context not initialized, or algorithm not capable 11957 * of posting slice completion. 11958 */ 11959 vpx_codec_err_t vpx_codec_register_put_frame_cb( 11960 vpx_codec_ctx_t *ctx, 11961 vpx_codec_put_frame_cb_fn_t cb, 11962 void *user_priv); 11964 /*!@} - end defgroup cap_put_frame */ 11966 /*!\defgroup cap_put_slice Slice-Based Decoding Functions 11967 * 11968 * The following functions are required to be implemented for all 11969 * decoders that advertise the VPX_CODEC_CAP_PUT_SLICE 11970 * capability. Calling these functions for codecs that don't 11971 * advertise this capability will result in an error code being 11972 * returned, usually VPX_CODEC_ERROR 11973 * @{ 11974 */ 11976 /*!\brief put slice callback prototype 11977 * 11978 * This callback is invoked by the decoder to notify the 11979 * application of the availability of partially decoded image 11980 * data. 11981 */ 11982 typedef void (*vpx_codec_put_slice_cb_fn_t)( 11983 void *user_priv, 11984 const vpx_image_t *img, 11985 const vpx_image_rect_t *valid, 11986 const vpx_image_rect_t *update); 11988 /*!\brief Register for notification of slice completion. 11989 * 11990 * Registers a given function to be called when a decoded slice 11991 * is available. 11992 * 11993 * \param[in] ctx Pointer to this instance's context 11994 * \param[in] cb Pointer to the callback function 11995 * \param[in] user_priv User's private data 11996 * 11997 * \retval #VPX_CODEC_OK 11998 * Callback successfully registered. 11999 * \retval #VPX_CODEC_ERROR 12000 * Decoder context not initialized, or algorithm not capable 12001 * of posting slice completion. 12002 */ 12003 vpx_codec_err_t vpx_codec_register_put_slice_cb( 12004 vpx_codec_ctx_t *ctx, 12005 vpx_codec_put_slice_cb_fn_t cb, 12006 void *user_priv); 12008 /*!@} - end defgroup cap_put_slice*/ 12010 /*!@} - end defgroup decoder*/ 12012 #endif 12014 #ifdef __cplusplus 12015 } 12016 #endif 12018 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 12019 #include "vpx_decoder_compat.h" 12020 #endif 12022 ---- End code block ---------------------------------------- 12024 20.21. vpx_decoder_compat.h 12026 ---- Begin code block -------------------------------------- 12028 /* 12029 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 12030 * 12031 * Use of this source code is governed by a BSD-style license 12032 * that can be found in the LICENSE file in the root of the source 12033 * tree. An additional intellectual property rights grant can be 12034 * found in the file PATENTS. All contributing project authors may 12035 * be found in the AUTHORS file in the root of the source tree. 12037 */ 12039 /*!\defgroup decoder Common Decoder Algorithm Interface 12040 * This abstraction allows applications using this decoder to easily 12041 * support multiple video formats with minimal code duplication. This 12042 * section describes the interface common to all codecs. 12043 * @{ 12044 */ 12046 /*!\file 12047 * \brief Provides a compatibility layer between version 1 and 2 of 12048 * this API. 12049 * 12050 * This interface has been deprecated. Only existing code should make 12051 * use of this interface, and therefore, it is only thinly 12052 * documented. Existing code should be ported to the vpx_codec_* API. 12053 */ 12054 #ifdef __cplusplus 12055 extern "C" { 12056 #endif 12058 #ifndef VPX_DECODER_COMPAT_H 12059 #define VPX_DECODER_COMPAT_H 12061 /*!\brief Decoder algorithm return codes */ 12062 typedef enum { 12063 /*!\brief Operation completed without error */ 12064 VPX_DEC_OK = VPX_CODEC_OK, 12066 /*!\brief Unspecified error */ 12067 VPX_DEC_ERROR = VPX_CODEC_ERROR, 12069 /*!\brief Memory operation failed */ 12070 VPX_DEC_MEM_ERROR = VPX_CODEC_MEM_ERROR, 12072 /*!\brief ABI version mismatch */ 12073 VPX_DEC_ABI_MISMATCH = VPX_CODEC_ABI_MISMATCH, 12075 /*!\brief The given bitstream is not supported. 12076 * 12077 * The bitstream was unable to be parsed at the highest 12078 * level. The decoder is unable to proceed. This error \ref 12079 * SHOULD be treated as fatal to the stream. 12080 */ 12081 VPX_DEC_UNSUP_BITSTREAM = VPX_CODEC_UNSUP_BITSTREAM, 12083 /*!\brief Encoded bitstream uses an unsupported feature 12084 * 12085 * The decoder does not implement a feature required by the 12086 * encoder. This return code should only be used for features 12087 * that prevent future pictures from being properly decoded. 12088 * This error \ref MAY be treated as fatal to the stream or 12089 * \ref MAY be treated as fatal to the current GOP. 12090 */ 12091 VPX_DEC_UNSUP_FEATURE = VPX_CODEC_UNSUP_FEATURE, 12093 /*!\brief The coded data for this stream is corrupt or 12094 * incomplete 12095 * 12096 * There was a problem decoding the current frame. This 12097 * return code should only be used for failures that prevent 12098 * future pictures from being properly decoded. This error 12099 * \ref MAY be treated as fatal to the stream or \ref MAY be 12100 * treated as fatal to the current GOP. If decoding is 12101 * continued for the current GOP, artifacts may be present. 12102 */ 12103 VPX_DEC_CORRUPT_FRAME = VPX_CODEC_CORRUPT_FRAME, 12105 /*!\brief An application-supplied parameter is not valid. 12106 * 12107 */ 12108 VPX_DEC_INVALID_PARAM = VPX_CODEC_INVALID_PARAM, 12110 /*!\brief An iterator reached the end of list. 12111 * 12112 */ 12113 VPX_DEC_LIST_END = VPX_CODEC_LIST_END 12115 } 12116 vpx_dec_err_t; 12118 /*! \brief Decoder capabilities bitfield 12119 * 12120 * Each decoder advertises the capabilities it supports as part 12121 * of its ::vpx_dec_iface_t interface structure. Capabilities 12122 * are extra interfaces or functionality, and are not required 12123 * to be supported by a decoder. 12124 * 12125 * The available flags are specified by VPX_DEC_CAP_* defines. 12126 */ 12127 typedef int vpx_dec_caps_t; 12128 #define VPX_DEC_CAP_PUT_SLICE 0x0001 /**< Will issue put_slice 12129 callbacks */ 12130 #define VPX_DEC_CAP_PUT_FRAME 0x0002 /**< Will issue put_frame 12131 callbacks */ 12133 #define VPX_DEC_CAP_XMA 0x0004 /**< Supports eXternal Memory 12134 Allocation */ 12136 /*!\brief Stream properties 12137 * 12138 * This structure is used to query or set properties of the 12139 * decoded stream. Algorithms may extend this structure with 12140 * data specific to their bitstream by setting the sz member 12141 * appropriately. 12142 */ 12143 #if 1 12144 typedef vpx_codec_stream_info_t vpx_dec_stream_info_t; 12145 #else 12146 typedef struct 12147 { 12148 unsigned int sz; /**< Size of this structure */ 12149 unsigned int w; /**< Width (or 0 for unknown/default) */ 12150 unsigned int h; /**< Height (or 0 for unknown/default) */ 12151 unsigned int is_kf; /**< Current frame is a keyframe */ 12152 } vpx_dec_stream_info_t; 12153 #endif 12155 /*!\brief Decoder interface structure. 12156 * 12157 * Contains function pointers and other data private to the 12158 * decoder implementation. This structure is opaque to the 12159 * application. 12160 */ 12161 typedef const struct vpx_codec_iface vpx_dec_iface_t; 12162 typedef struct vpx_codec_priv vpx_dec_priv_t; 12164 /*!\brief Iterator 12165 * 12166 * Opaque storage used for iterating over lists. 12167 */ 12168 typedef vpx_codec_iter_t vpx_dec_iter_t; 12170 /*!\brief Decoder context structure 12171 * 12172 * All decoders \ref MUST support this context structure fully. 12173 * In general, this data should be considered private to the 12174 * decoder algorithm, and not be manipulated or examined by the 12175 * calling application. Applications may reference the 'name' 12176 * member to get a printable description of the algorithm. 12177 */ 12178 #if 1 12179 typedef vpx_codec_ctx_t vpx_dec_ctx_t; 12181 #else 12182 typedef struct 12183 { 12184 const char *name; /**< Printable interface name */ 12185 vpx_dec_iface_t *iface; /**< Interface pointers */ 12186 vpx_dec_err_t err; /**< Last returned error */ 12187 vpx_dec_priv_t *priv; /**< Algorithm private storage */ 12188 } vpx_dec_ctx_t; 12189 #endif 12191 /*!\brief Return the build configuration 12192 * 12193 * Returns a printable string containing an encoded version of 12194 * the build configuration. This may be useful to vpx support. 12195 * 12196 */ 12197 const char *vpx_dec_build_config(void) DEPRECATED; 12199 /*!\brief Return the name for a given interface 12200 * 12201 * Returns a human readable string for name of the given decoder 12202 * interface. 12203 * 12204 * \param[in] iface Interface pointer 12205 * 12206 */ 12207 const char *vpx_dec_iface_name( 12208 vpx_dec_iface_t *iface) DEPRECATED; 12210 /*!\brief Convert error number to printable string 12211 * 12212 * Returns a human readable string for the last error returned 12213 * by the algorithm. The returned error will be one line and will 12214 * not contain any newline characters. 12215 * 12216 * 12217 * \param[in] err Error number. 12218 * 12219 */ 12220 const char *vpx_dec_err_to_string(vpx_dec_err_t err) DEPRECATED; 12222 /*!\brief Retrieve error synopsis for decoder context 12223 * 12224 * Returns a human readable string for the last error returned by 12225 * the algorithm. The returned error will be one line and will 12226 * not contain any newline characters. 12227 * 12228 * 12229 * \param[in] ctx Pointer to this instance's context. 12230 * 12231 */ 12232 const char *vpx_dec_error(vpx_dec_ctx_t *ctx) DEPRECATED; 12234 /*!\brief Retrieve detailed error information for decoder context 12235 * 12236 * Returns a human readable string providing detailed information 12237 * about the last error. 12238 * 12239 * \param[in] ctx Pointer to this instance's context. 12240 * 12241 * \retval NULL 12242 * No detailed information is available. 12243 */ 12244 const char *vpx_dec_error_detail(vpx_dec_ctx_t *ctx) DEPRECATED; 12246 /* REQUIRED FUNCTIONS 12247 * 12248 * The following functions are required to be implemented for all 12249 * decoders. They represent the base case functionality expected 12250 * of all decoders. 12251 */ 12253 /*!\brief Initialize a decoder instance 12254 * 12255 * Initializes a decoder context using the given interface. 12256 * Applications should call the vpx_dec_init convenience macro 12257 * instead of this function directly, to ensure that the ABI 12258 * version number parameter is properly initialized. 12259 * 12260 * \param[in] ctx Pointer to this instance's context. 12261 * \param[in] iface Pointer to the algorithm interface to use. 12262 * \param[in] ver ABI version number. Must be set to 12263 * VPX_DECODER_ABI_VERSION 12264 * \retval #VPX_DEC_OK 12265 * The decoder algorithm initialized. 12266 * \retval #VPX_DEC_MEM_ERROR 12267 * Memory allocation failed. 12268 */ 12269 vpx_dec_err_t vpx_dec_init_ver( 12270 vpx_dec_ctx_t *ctx, 12271 vpx_dec_iface_t *iface, 12272 int ver) DEPRECATED; 12274 #define vpx_dec_init(ctx, iface) \ 12275 vpx_dec_init_ver(ctx, iface, VPX_DECODER_ABI_VERSION) 12277 /*!\brief Destroy a decoder instance 12278 * 12279 * Destroys a decoder context, freeing any associated memory 12280 * buffers. 12281 * 12282 * \param[in] ctx Pointer to this instance's context 12283 * 12284 * \retval #VPX_DEC_OK 12285 * The decoder algorithm initialized. 12286 * \retval #VPX_DEC_MEM_ERROR 12287 * Memory allocation failed. 12288 */ 12289 vpx_dec_err_t vpx_dec_destroy(vpx_dec_ctx_t *ctx) DEPRECATED; 12291 /*!\brief Get the capabilities of an algorithm. 12292 * 12293 * Retrieves the capabilities bitfield from the algorithm's 12294 * interface. 12295 * 12296 * \param[in] iface Pointer to the algorithm interface 12297 * 12298 */ 12299 vpx_dec_caps_t vpx_dec_get_caps( 12300 vpx_dec_iface_t *iface) DEPRECATED; 12302 /*!\brief Parse stream info from a buffer 12303 * 12304 * Performs high level parsing of the bitstream. Construction of 12305 * a decoder context is not necessary. Can be used to determine 12306 * if the bitstream is of the proper format, and to extract 12307 * information from the stream. 12308 * 12309 * \param[in] iface Pointer to the algorithm interface 12310 * \param[in] data Pointer to a block of data to parse 12311 * \param[in] data_sz Size of the data buffer 12312 * \param[in,out] si Pointer to stream info to update. The 12313 * size member \ref MUST be properly 12314 * initialized, but \ref MAY be 12315 * clobbered by the algorithm. This 12316 * parameter \ref MAY be NULL. 12317 * 12318 * \retval #VPX_DEC_OK 12319 * Bitstream is parsable and stream information updated 12320 */ 12321 vpx_dec_err_t vpx_dec_peek_stream_info( 12322 vpx_dec_iface_t *iface, 12323 const uint8_t *data, 12324 unsigned int data_sz, 12325 vpx_dec_stream_info_t *si) DEPRECATED; 12327 /*!\brief Return information about the current stream. 12328 * 12329 * Returns information about the stream that has been parsed 12330 * during decoding. 12331 * 12332 * \param[in] ctx Pointer to this instance's context 12333 * \param[in,out] si Pointer to stream info to update. 12334 * The size member \ref MUST be properly 12335 * initialized, but \ref MAY be clobbered 12336 * by the algorithm. This parameter \ref 12337 * MAY be NULL. 12338 * 12339 * \retval #VPX_DEC_OK 12340 * Bitstream is parsable and stream information updated 12341 */ 12342 vpx_dec_err_t vpx_dec_get_stream_info( 12343 vpx_dec_ctx_t *ctx, 12344 vpx_dec_stream_info_t *si) DEPRECATED; 12346 /*!\brief Control algorithm 12347 * 12348 * This function is used to exchange algorithm specific data with 12349 * the decoder instance. This can be used to implement features 12350 * specific to a particular algorithm. 12351 * 12352 * This wrapper function dispatches the request to the helper 12353 * function associated with the given ctrl_id. It tries to call 12354 * this function transparently, but will return #VPX_DEC_ERROR if 12355 * the request could not be dispatched. 12356 * 12357 * \param[in] ctx Pointer to this instance's context 12358 * \param[in] ctrl_id Algorithm specific control 12359 * identifier 12360 * \param[in,out] data Data to exchange with algorithm 12361 * instance. 12363 * 12364 * \retval #VPX_DEC_OK 12365 * The control request was processed. 12366 * \retval #VPX_DEC_ERROR 12367 * The control request was not processed. 12368 * \retval #VPX_DEC_INVALID_PARAM 12369 * The data was not valid. 12370 */ 12371 vpx_dec_err_t vpx_dec_control(vpx_dec_ctx_t *ctx, 12372 int ctrl_id, 12373 void *data) DEPRECATED; 12375 /*!\brief Decode data 12376 * 12377 * Processes a buffer of coded data. If the processing results in 12378 * a new decoded frame becoming available, #VPX_DEC_CB_PUT_SLICE 12379 * and #VPX_DEC_CB_PUT_FRAME events may be generated, as 12380 * appropriate. Encoded data \ref MUST be passed in DTS (decode 12381 * time stamp) order. Frames produced will always be in PTS 12382 * (presentation time stamp) order. 12383 * 12384 * \param[in] ctx Pointer to this instance's context 12385 * \param[in] data Pointer to this block of new coded 12386 * data. If NULL, a VPX_DEC_CB_PUT_FRAME 12387 * event is posted for the previously 12388 * decoded frame. 12389 * \param[in] data_sz Size of the coded data, in bytes. 12390 * \param[in] user_priv Application specific data to associate 12391 * with this frame. 12392 * \param[in] rel_pts PTS relative to the previous frame, in 12393 * us. If unknown or unavailable, set to 12394 * zero. 12395 * 12396 * \return Returns #VPX_DEC_OK if the coded data was processed 12397 * completely and future pictures can be decoded without 12398 * error. Otherwise, see the descriptions of the other 12399 * error codes in ::vpx_dec_err_t for recoverability 12400 * capabilities. 12401 */ 12402 vpx_dec_err_t vpx_dec_decode( 12403 vpx_dec_ctx_t *ctx, 12404 uint8_t *data, 12405 unsigned int data_sz, 12406 void *user_priv, 12407 int rel_pts) DEPRECATED; 12409 /*!\brief Decoded frames iterator 12410 * 12411 * Iterates over a list of the frames available for display. The 12412 * iterator storage should be initialized to NULL to start the 12413 * iteration. Iteration is complete when this function returns 12414 * NULL. 12415 * 12416 * The list of available frames becomes valid upon completion of 12417 * the vpx_dec_decode call, and remains valid until the next call 12418 * to vpx_dec_decode. 12419 * 12420 * \param[in] ctx Pointer to this instance's context 12421 * \param[in out] iter Iterator storage, initialized to NULL 12422 * 12423 * \return Returns a pointer to an image, if one is ready for 12424 * display. Frames produced will always be in PTS 12425 * (presentation time stamp) order. 12426 */ 12427 vpx_image_t *vpx_dec_get_frame(vpx_dec_ctx_t *ctx, 12428 vpx_dec_iter_t *iter) DEPRECATED; 12430 /*!\defgroup cap_put_frame Frame-Based Decoding Functions 12431 * 12432 * The following functions are required to be implemented for all 12433 * decoders that advertise the VPX_DEC_CAP_PUT_FRAME capability. 12434 * Calling these functions for codecs that don't advertise this 12435 * capability will result in an error code being returned, 12436 * usually VPX_DEC_ERROR @{ 12437 */ 12439 /*!\brief put frame callback prototype 12440 * 12441 * This callback is invoked by the decoder to notify the 12442 * application of the availability of decoded image data. 12443 */ 12444 typedef void (*vpx_dec_put_frame_cb_fn_t)( 12445 void *user_priv, 12446 const vpx_image_t *img); 12448 /*!\brief Register for notification of frame completion. 12449 * 12450 * Registers a given function to be called when a decoded frame 12451 * is available. 12452 * 12453 * \param[in] ctx Pointer to this instance's context 12454 * \param[in] cb Pointer to the callback function 12455 * \param[in] user_priv User's private data 12456 * 12457 * \retval #VPX_DEC_OK 12458 * Callback successfully registered. 12459 * \retval #VPX_DEC_ERROR 12460 * Decoder context not initialized, or algorithm not capable 12461 * of posting slice completion. 12462 */ 12463 vpx_dec_err_t vpx_dec_register_put_frame_cb( 12464 vpx_dec_ctx_t *ctx, 12465 vpx_dec_put_frame_cb_fn_t cb, 12466 void *user_priv) DEPRECATED; 12468 /*!@} - end defgroup cap_put_frame */ 12470 /*!\defgroup cap_put_slice Slice-Based Decoding Functions 12471 * 12472 * The following functions are required to be implemented for all 12473 * decoders that advertise the VPX_DEC_CAP_PUT_SLICE capability. 12474 * Calling these functions for codecs that don't advertise this 12475 * capability will result in an error code being returned, 12476 * usually VPX_DEC_ERROR 12477 * @{ 12478 */ 12480 /*!\brief put slice callback prototype 12481 * 12482 * This callback is invoked by the decoder to notify the 12483 * application of the availability of partially decoded image 12484 * data. The 12485 */ 12486 typedef void (*vpx_dec_put_slice_cb_fn_t)(void *user_priv, 12487 const vpx_image_t *img, 12488 const vpx_image_rect_t *valid, 12489 const vpx_image_rect_t *update); 12491 /*!\brief Register for notification of slice completion. 12492 * 12493 * Registers a given function to be called when a decoded slice 12494 * is available. 12495 * 12496 * \param[in] ctx Pointer to this instance's context 12497 * \param[in] cb Pointer to the callback function 12498 * \param[in] user_priv User's private data 12499 * 12500 * \retval #VPX_DEC_OK 12501 * Callback successfully registered. 12503 * \retval #VPX_DEC_ERROR 12504 * Decoder context not initialized, or algorithm not capable 12505 * of posting slice completion. 12506 */ 12507 vpx_dec_err_t vpx_dec_register_put_slice_cb(vpx_dec_ctx_t *ctx, 12508 vpx_dec_put_slice_cb_fn_t cb, 12509 void *user_priv) DEPRECATED; 12511 /*!@} - end defgroup cap_put_slice*/ 12513 /*!\defgroup cap_xma External Memory Allocation Functions 12514 * 12515 * The following functions are required to be implemented for all 12516 * decoders that advertise the VPX_DEC_CAP_XMA capability. 12517 * Calling these functions for codecs that don't advertise this 12518 * capability will result in an error code being returned, 12519 * usually VPX_DEC_ERROR 12520 * @{ 12521 */ 12523 /*!\brief Memory Map Entry 12524 * 12525 * This structure is used to contain the properties of a memory 12526 * segment. It is populated by the decoder in the request phase, 12527 * and by the calling application once the requested allocation 12528 * has been performed. 12529 */ 12530 #if 1 12531 #define VPX_DEC_MEM_ZERO 0x1 /**< Segment must be zeroed by 12532 allocation */ 12533 #define VPX_DEC_MEM_WRONLY 0x2 /**< Segment need not be 12534 readable */ 12535 #define VPX_DEC_MEM_FAST 0x4 /**< Place in fast memory, if 12536 available */ 12537 typedef struct vpx_codec_mmap vpx_dec_mmap_t; 12538 #else 12539 typedef struct vpx_dec_mmap 12540 { 12541 /* 12542 * The following members are set by the codec when requesting 12543 * a segment 12544 */ 12545 unsigned int id; /**< identifier for the segment's 12546 contents */ 12547 unsigned long sz; /**< size of the segment, in bytes */ 12548 unsigned int align; /**< required alignment of the 12549 segment, in bytes */ 12551 unsigned int flags; /**< bitfield containing segment 12552 properties */ 12553 #define VPX_DEC_MEM_ZERO 0x1 /**< Segment must be zeroed by 12554 allocation */ 12555 #define VPX_DEC_MEM_WRONLY 0x2 /**< Segment need not be 12556 readable */ 12557 #define VPX_DEC_MEM_FAST 0x4 /**< Place in fast memory, if 12558 available */ 12560 /* The following members are to be filled in by the 12561 * allocation function */ 12562 void *base; /**< pointer to the allocated 12563 segment */ 12564 void (*dtor)(struct vpx_dec_mmap *map); /**< destructor to 12565 call */ 12566 void *priv; /**< allocator private storage */ 12567 } vpx_dec_mmap_t; 12568 #endif 12570 /*!\brief Initialize a decoder instance in external allocation 12571 * mode 12572 * 12573 * Initializes a decoder context using the given interface. 12574 * Applications should call the vpx_dec_xma_init convenience 12575 * macro instead of this function directly, to ensure that the 12576 * ABI version number parameter is properly initialized. 12577 * 12578 * \param[in] ctx Pointer to this instance's context. 12579 * \param[in] iface Pointer to the algorithm interface to 12580 * use. 12581 * \param[in] ver ABI version number. Must be set to 12582 * VPX_DECODER_ABI_VERSION 12583 * \retval #VPX_DEC_OK 12584 * The decoder algorithm initialized. 12585 * \retval #VPX_DEC_ERROR 12586 * Decoder does not support XMA mode. 12587 */ 12588 vpx_dec_err_t vpx_dec_xma_init_ver(vpx_dec_ctx_t *ctx, 12589 vpx_dec_iface_t *iface, 12590 int ver) DEPRECATED; 12591 #define vpx_dec_xma_init(ctx, iface) \ 12592 vpx_dec_xma_init_ver(ctx, iface, VPX_DECODER_ABI_VERSION) 12594 /*!\brief Iterate over the list of segments to allocate. 12595 * 12596 * Iterates over a list of the segments to allocate. The iterator 12597 * storage should be initialized to NULL to start the iteration. 12599 * Iteration is complete when this function returns 12600 * VPX_DEC_LIST_END. The amount of memory needed to allocate is 12601 * dependent upon the size of the encoded stream. This means that 12602 * the stream info structure must be known at allocation time. It 12603 * can be populated with the vpx_dec_peek_stream_info() function. 12604 * In cases where the stream to be decoded is not available at 12605 * allocation time, a fixed size must be requested. The decoder 12606 * will not be able to decode streams larger than the size used 12607 * at allocation time. 12608 * 12609 * \param[in] ctx Pointer to this instance's context. 12610 * \param[out] mmap Pointer to the memory map entry to 12611 * populate. 12612 * \param[in] si Pointer to the stream info. 12613 * \param[in out] iter Iterator storage, initialized to NULL 12614 * 12615 * \retval #VPX_DEC_OK 12616 * The memory map entry was populated. 12617 * \retval #VPX_DEC_ERROR 12618 * Decoder does not support XMA mode. 12619 * \retval #VPX_DEC_MEM_ERROR 12620 * Unable to determine segment size from stream info. 12621 */ 12622 vpx_dec_err_t vpx_dec_get_mem_map( 12623 vpx_dec_ctx_t *ctx, 12624 vpx_dec_mmap_t *mmap, 12625 const vpx_dec_stream_info_t *si, 12626 vpx_dec_iter_t *iter) DEPRECATED; 12628 /*!\brief Identify allocated segments to decoder instance 12629 * 12630 * Stores a list of allocated segments in the decoder. Segments 12631 * \ref MUST be passed in the order they are read from 12632 * vpx_dec_get_mem_map(), but may be passed in groups of any 12633 * size. Segments \ref MUST be set only once. The allocation 12634 * function \ref MUST ensure that the vpx_dec_mmap_t::base member 12635 * is non-NULL. If the segment requires cleanup handling (e.g., 12636 * calling free() or close()) then the vpx_dec_mmap_t::dtor 12637 * member \ref MUST be populated. 12638 * 12639 * \param[in] ctx Pointer to this instance's context. 12640 * \param[in] mmaps Pointer to the first memory map 12641 * entry in the list. 12642 * \param[in] num_maps Number of entries being set at this 12643 * time 12644 * 12645 * \retval #VPX_DEC_OK 12646 * The segment was stored in the decoder context. 12647 * \retval #VPX_DEC_ERROR 12648 * Decoder does not support XMA mode. 12649 * \retval #VPX_DEC_MEM_ERROR 12650 * Segment base address was not set, or segment was already 12651 * stored. 12653 */ 12654 vpx_dec_err_t vpx_dec_set_mem_map( 12655 vpx_dec_ctx_t *ctx, 12656 vpx_dec_mmap_t *mmaps, 12657 unsigned int num_maps) DEPRECATED; 12659 /*!@} - end defgroup cap_xma*/ 12660 /*!@} - end defgroup decoder*/ 12662 #endif 12663 #ifdef __cplusplus 12664 } 12665 #endif 12667 ---- End code block ---------------------------------------- 12669 20.22. vpx_image.c 12671 ---- Begin code block -------------------------------------- 12673 /* 12674 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 12675 * 12676 * Use of this source code is governed by a BSD-style license 12677 * that can be found in the LICENSE file in the root of the source 12678 * tree. An additional intellectual property rights grant can be 12679 * found in the file PATENTS. All contributing project authors may 12680 * be found in the AUTHORS file in the root of the source tree. 12681 */ 12683 #include 12684 #include 12685 #include "vpx/vpx_image.h" 12687 static vpx_image_t *img_alloc_helper(vpx_image_t *img, 12688 vpx_img_fmt_t fmt, 12689 unsigned int d_w, 12690 unsigned int d_h, 12691 unsigned int stride_align, 12692 unsigned char *img_data) 12693 { 12695 unsigned int h, w, s, xcs, ycs, bps; 12696 int align; 12698 /* Treat align==0 like align==1 */ 12699 if (!stride_align) 12700 stride_align = 1; 12702 /* Validate alignment (must be power of 2) */ 12703 if (stride_align & (stride_align - 1)) 12704 goto fail; 12706 /* Get sample size for this format */ 12707 switch (fmt) 12708 { 12709 case VPX_IMG_FMT_RGB32: 12710 case VPX_IMG_FMT_RGB32_LE: 12711 case VPX_IMG_FMT_ARGB: 12712 case VPX_IMG_FMT_ARGB_LE: 12713 bps = 32; 12714 break; 12715 case VPX_IMG_FMT_RGB24: 12716 case VPX_IMG_FMT_BGR24: 12717 bps = 24; 12718 break; 12719 case VPX_IMG_FMT_RGB565: 12720 case VPX_IMG_FMT_RGB565_LE: 12721 case VPX_IMG_FMT_RGB555: 12722 case VPX_IMG_FMT_RGB555_LE: 12723 case VPX_IMG_FMT_UYVY: 12724 case VPX_IMG_FMT_YUY2: 12725 case VPX_IMG_FMT_YVYU: 12726 bps = 16; 12727 break; 12728 case VPX_IMG_FMT_I420: 12729 case VPX_IMG_FMT_YV12: 12730 case VPX_IMG_FMT_VPXI420: 12731 case VPX_IMG_FMT_VPXYV12: 12732 bps = 12; 12733 break; 12734 default: 12735 bps = 16; 12736 break; 12737 } 12738 /* Get chroma shift values for this format */ 12739 switch (fmt) 12740 { 12741 case VPX_IMG_FMT_I420: 12742 case VPX_IMG_FMT_YV12: 12743 case VPX_IMG_FMT_VPXI420: 12744 case VPX_IMG_FMT_VPXYV12: 12745 xcs = 1; 12746 break; 12747 default: 12748 xcs = 0; 12749 break; 12750 } 12752 switch (fmt) 12753 { 12754 case VPX_IMG_FMT_I420: 12755 case VPX_IMG_FMT_YV12: 12756 case VPX_IMG_FMT_VPXI420: 12757 case VPX_IMG_FMT_VPXYV12: 12758 ycs = 1; 12759 break; 12760 default: 12761 ycs = 0; 12762 break; 12763 } 12765 /* Calculate storage sizes given the chroma subsampling */ 12766 align = (1 << xcs) - 1; 12767 w = (d_w + align) & ~align; 12768 align = (1 << ycs) - 1; 12769 h = (d_h + align) & ~align; 12770 s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8; 12771 s = (s + stride_align - 1) & ~(stride_align - 1); 12773 /* Allocate the new image */ 12774 if (!img) 12775 { 12776 img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t)); 12778 if (!img) 12779 goto fail; 12781 img->self_allocd = 1; 12782 } 12783 else 12784 { 12785 memset(img, 0, sizeof(vpx_image_t)); 12787 } 12789 img->img_data = img_data; 12791 if (!img_data) 12792 { 12793 img->img_data = malloc((fmt & VPX_IMG_FMT_PLANAR) ? 12794 h * w * bps / 8 : h * s); 12795 img->img_data_owner = 1; 12796 } 12798 if (!img->img_data) 12799 goto fail; 12801 img->fmt = fmt; 12802 img->w = w; 12803 img->h = h; 12804 img->x_chroma_shift = xcs; 12805 img->y_chroma_shift = ycs; 12806 img->bps = bps; 12808 /* Calculate strides */ 12809 img->stride[VPX_PLANE_Y] = img->stride[VPX_PLANE_ALPHA] = s; 12810 img->stride[VPX_PLANE_U] = img->stride[VPX_PLANE_V] = s >> xcs; 12812 /* Default viewport to entire image */ 12813 if (!vpx_img_set_rect(img, 0, 0, d_w, d_h)) 12814 return img; 12816 fail: 12817 vpx_img_free(img); 12818 return NULL; 12819 } 12821 vpx_image_t *vpx_img_alloc(vpx_image_t *img, 12822 vpx_img_fmt_t fmt, 12823 unsigned int d_w, 12824 unsigned int d_h, 12825 unsigned int stride_align) 12826 { 12827 return img_alloc_helper(img, fmt, d_w, d_h, stride_align, NULL); 12828 } 12830 vpx_image_t *vpx_img_wrap(vpx_image_t *img, 12831 vpx_img_fmt_t fmt, 12832 unsigned int d_w, 12833 unsigned int d_h, 12834 unsigned int stride_align, 12835 unsigned char *img_data) 12836 { 12837 return img_alloc_helper(img, fmt, d_w, d_h, stride_align, 12838 img_data); 12839 } 12841 int vpx_img_set_rect(vpx_image_t *img, 12842 unsigned int x, 12843 unsigned int y, 12844 unsigned int w, 12845 unsigned int h) 12846 { 12847 unsigned char *data; 12849 if (x + w <= img->w && y + h <= img->h) 12850 { 12851 img->d_w = w; 12852 img->d_h = h; 12854 /* Calculate plane pointers */ 12855 if (!(img->fmt & VPX_IMG_FMT_PLANAR)) 12856 { 12857 img->planes[VPX_PLANE_PACKED] = 12858 img->img_data + x * img->bps / 8 + y * 12859 img->stride[VPX_PLANE_PACKED]; 12860 } 12861 else 12862 { 12863 data = img->img_data; 12865 if (img->fmt & VPX_IMG_FMT_HAS_ALPHA) 12866 { 12867 img->planes[VPX_PLANE_ALPHA] = 12868 data + x + y * img->stride[VPX_PLANE_ALPHA]; 12869 data += img->h * img->stride[VPX_PLANE_ALPHA]; 12870 } 12872 img->planes[VPX_PLANE_Y] = 12873 data + x + y * img->stride[VPX_PLANE_Y]; 12874 data += img->h * img->stride[VPX_PLANE_Y]; 12876 if (!(img->fmt & VPX_IMG_FMT_UV_FLIP)) 12877 { 12878 img->planes[VPX_PLANE_U] = data 12879 + (x >> img->x_chroma_shift) 12880 + (y >> img->y_chroma_shift) * 12881 img->stride[VPX_PLANE_U]; 12882 data += (img->h >> img->y_chroma_shift) * 12883 img->stride[VPX_PLANE_U]; 12884 img->planes[VPX_PLANE_V] = data 12885 + (x >> img->x_chroma_shift) 12886 + (y >> img->y_chroma_shift) * 12887 img->stride[VPX_PLANE_V]; 12888 } 12889 else 12890 { 12891 img->planes[VPX_PLANE_V] = data 12892 + (x >> img->x_chroma_shift) 12893 + (y >> img->y_chroma_shift) * 12894 img->stride[VPX_PLANE_V]; 12895 data += (img->h >> img->y_chroma_shift) * 12896 img->stride[VPX_PLANE_V]; 12897 img->planes[VPX_PLANE_U] = data 12898 + (x >> img->x_chroma_shift) 12899 + (y >> img->y_chroma_shift) * 12900 img->stride[VPX_PLANE_U]; 12901 } 12902 } 12904 return 0; 12905 } 12907 return -1; 12908 } 12910 void vpx_img_flip(vpx_image_t *img) 12911 { 12912 /* Note: In the calculation pointer adjustment calculation, we 12913 * want the rhs to be promoted to a signed type. Section 6.3.1.8 12914 * of the ISO C99 standard indicates that if the adjustment 12915 * parameter is unsigned, the stride parameter will be promoted 12916 * to unsigned, causing errors when the lhs is a larger type than 12917 * the rhs. 12918 */ 12919 img->planes[VPX_PLANE_Y] += (signed) 12920 (img->d_h - 1) * img->stride[VPX_PLANE_Y]; 12921 img->stride[VPX_PLANE_Y] = -img->stride[VPX_PLANE_Y]; 12923 img->planes[VPX_PLANE_U] += (signed) 12924 ((img->d_h >> img->y_chroma_shift) - 1) 12925 * img->stride[VPX_PLANE_U]; 12926 img->stride[VPX_PLANE_U] = -img->stride[VPX_PLANE_U]; 12928 img->planes[VPX_PLANE_V] += (signed) 12929 ((img->d_h >> img->y_chroma_shift) - 1) * 12930 img->stride[VPX_PLANE_V]; 12932 img->stride[VPX_PLANE_V] = -img->stride[VPX_PLANE_V]; 12934 img->planes[VPX_PLANE_ALPHA] += (signed) 12935 (img->d_h - 1) * img->stride[VPX_PLANE_ALPHA]; 12936 img->stride[VPX_PLANE_ALPHA] = -img->stride[VPX_PLANE_ALPHA]; 12937 } 12939 void vpx_img_free(vpx_image_t *img) 12940 { 12941 if (img) 12942 { 12943 if (img->img_data && img->img_data_owner) 12944 free(img->img_data); 12946 if (img->self_allocd) 12947 free(img); 12948 } 12949 } 12951 ---- End code block ---------------------------------------- 12953 20.23. vpx_image.h 12955 ---- Begin code block -------------------------------------- 12957 /* 12958 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 12959 * 12960 * Use of this source code is governed by a BSD-style license 12961 * that can be found in the LICENSE file in the root of the source 12962 * tree. An additional intellectual property rights grant can be 12963 * found in the file PATENTS. All contributing project authors may 12964 * be found in the AUTHORS file in the root of the source tree. 12965 */ 12967 /*!\file 12968 * \brief Describes the vpx image descriptor and associated 12969 * operations 12970 * 12971 */ 12972 #ifdef __cplusplus 12973 extern "C" { 12974 #endif 12976 #ifndef VPX_IMAGE_H 12977 #define VPX_IMAGE_H 12978 /*!\brief Current ABI version number 12979 * 12980 * \internal 12981 * If this file is altered in any way that changes the ABI, this 12982 * value must be bumped. Examples include, but are not limited 12983 * to, changing types, removing or reassigning enums, 12984 * adding/removing/rearranging fields to structures 12985 */ 12986 #define VPX_IMAGE_ABI_VERSION (1) /**<\hideinitializer*/ 12988 #define VPX_IMG_FMT_PLANAR 0x100 /**< Image is a planar 12989 format */ 12990 #define VPX_IMG_FMT_UV_FLIP 0x200 /**< V plane precedes U plane 12991 in memory */ 12992 #define VPX_IMG_FMT_HAS_ALPHA 0x400 /**< Image has an alpha channel 12993 component */ 12995 /*!\brief List of supported image formats */ 12996 typedef enum vpx_img_fmt { 12997 VPX_IMG_FMT_NONE, 12998 VPX_IMG_FMT_RGB24, /**< 24 bit per pixel packed RGB */ 12999 VPX_IMG_FMT_RGB32, /**< 32 bit per pixel packed 0RGB */ 13000 VPX_IMG_FMT_RGB565, /**< 16 bit per pixel, 565 */ 13001 VPX_IMGFMT_RGB555, /**< 16 bit per pixel, 555 */ 13002 VPX_IMG_FMT_UYVY, /**< UYVY packed YUV */ 13003 VPX_IMG_FMT_YUY2, /**< YUYV packed YUV */ 13004 VPX_IMG_FMT_YVYU, /**< YVYU packed YUV */ 13005 VPX_IMG_FMT_BGR24, /**< 24 bit per pixel packed BGR */ 13006 VPX_IMG_FMT_RGB32_LE, /**< 32 bit packed BGR0 */ 13007 VPX_IMG_FMT_ARGB, /**< 32 bit packed ARGB, alpha=255 */ 13008 VPX_IMG_FMT_ARGB_LE, /**< 32 bit packed BGRA, alpha=255 */ 13009 VPX_IMG_FMT_RGB565_LE, /**< 16 bit per pixel, 13010 gggbbbbb rrrrrggg */ 13011 VPX_IMG_FMT_RGB555_LE, /**< 16 bit per pixel, 13012 gggbbbbb 0rrrrrgg */ 13013 VPX_IMG_FMT_YV12 = VPX_IMG_FMT_PLANAR | 13014 VPX_IMG_FMT_UV_FLIP | 1, /**< planar YVU */ 13015 VPX_IMG_FMT_I420 = VPX_IMG_FMT_PLANAR | 2, 13016 VPX_IMG_FMT_VPXYV12 = VPX_IMG_FMT_PLANAR | 13017 VPX_IMG_FMT_UV_FLIP | 3, /** < planar 4:2:0 format with 13018 vpx color space */ 13019 VPX_IMG_FMT_VPXI420 = VPX_IMG_FMT_PLANAR | 4 /** < planar 13020 4:2:0 format with vpx color space */ 13021 } 13022 vpx_img_fmt_t; /**< alias for enum vpx_img_fmt */ 13024 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 13025 /** \deprecated Use #VPX_IMG_FMT_PLANAR */ 13026 #define IMG_FMT_PLANAR VPX_IMG_FMT_PLANAR 13027 /** \deprecated Use #VPX_IMG_FMT_UV_FLIP */ 13028 #define IMG_FMT_UV_FLIP VPX_IMG_FMT_UV_FLIP 13029 /** \deprecated Use #VPX_IMG_FMT_HAS_ALPHA */ 13030 #define IMG_FMT_HAS_ALPHA VPX_IMG_FMT_HAS_ALPHA 13032 /*!\brief Deprecated list of supported image formats 13033 * \deprecated New code should use #vpx_img_fmt 13034 */ 13035 #define img_fmt vpx_img_fmt 13036 /*!\brief alias for enum img_fmt. 13037 * \deprecated New code should use #vpx_img_fmt_t 13038 */ 13039 #define img_fmt_t vpx_img_fmt_t 13041 /** \deprecated Use #VPX_IMG_FMT_NONE */ 13042 #define IMG_FMT_NONE VPX_IMG_FMT_NONE 13043 /** \deprecated Use #VPX_IMG_FMT_RGB24 */ 13044 #define IMG_FMT_RGB24 VPX_IMG_FMT_RGB24 13045 /** \deprecated Use #VPX_IMG_FMT_RGB32 */ 13046 #define IMG_FMT_RGB32 VPX_IMG_FMT_RGB32 13047 /** \deprecated Use #VPX_IMG_FMT_RGB565 */ 13048 #define IMG_FMT_RGB565 VPX_IMG_FMT_RGB565 13049 /** \deprecated Use #VPX_IMG_FMT_RGB555 */ 13050 #define IMG_FMT_RGB555 VPX_IMG_FMT_RGB555 13051 /** \deprecated Use #VPX_IMG_FMT_UYVY */ 13052 #define IMG_FMT_UYVY VPX_IMG_FMT_UYVY 13053 /** \deprecated Use #VPX_IMG_FMT_YUY2 */ 13054 #define IMG_FMT_YUY2 VPX_IMG_FMT_YUY2 13055 /** \deprecated Use #VPX_IMG_FMT_YVYU */ 13056 #define IMG_FMT_YVYU VPX_IMG_FMT_YVYU 13057 /** \deprecated Use #VPX_IMG_FMT_BGR24 */ 13058 #define IMG_FMT_BGR24 VPX_IMG_FMT_BGR24 13059 /**< \deprecated Use #VPX_IMG_FMT_RGB32_LE */ 13060 #define IMG_FMT_RGB32_LE VPX_IMG_FMT_RGB32_LE 13061 /** \deprecated Use #VPX_IMG_FMT_ARGB */ 13062 #define IMG_FMT_ARGB VPX_IMG_FMT_ARGB 13063 /** \deprecated Use #VPX_IMG_FMT_ARGB_LE */ 13064 #define IMG_FMT_ARGB_LE VPX_IMG_FMT_ARGB_LE 13065 /** \deprecated Use #VPX_IMG_FMT_RGB565_LE */ 13066 #define IMG_FMT_RGB565_LE VPX_IMG_FMT_RGB565_LE 13067 /** \deprecated Use #VPX_IMG_FMT_RGB555_LE */ 13068 #define IMG_FMT_RGB555_LE VPX_IMG_FMT_RGB555_LE 13069 /** \deprecated Use #VPX_IMG_FMT_YV12 */ 13070 #define IMG_FMT_YV12 VPX_IMG_FMT_YV12 13071 /** \deprecated Use #VPX_IMG_FMT_I420 */ 13072 #define IMG_FMT_I420 VPX_IMG_FMT_I420 13073 /** \deprecated Use #VPX_IMG_FMT_VPXYV12 */ 13074 #define IMG_FMT_VPXYV12 VPX_IMG_FMT_VPXYV12 13075 /** \deprecated Use #VPX_IMG_FMT_VPXI420 */ 13076 #define IMG_FMT_VPXI420 VPX_IMG_FMT_VPXI420 13077 #endif /* VPX_CODEC_DISABLE_COMPAT */ 13079 /**\brief Image Descriptor */ 13080 typedef struct vpx_image 13081 { 13082 vpx_img_fmt_t fmt; /**< Image Format */ 13084 /* Image storage dimensions */ 13085 unsigned int w; /**< Stored image width */ 13086 unsigned int h; /**< Stored image height */ 13088 /* Image display dimensions */ 13089 unsigned int d_w; /**< Displayed image width */ 13090 unsigned int d_h; /**< Displayed image height */ 13092 /* Chroma subsampling info */ 13093 unsigned int x_chroma_shift; /**< subsampling order, X */ 13094 unsigned int y_chroma_shift; /**< subsampling order, Y */ 13096 /* Image data pointers. */ 13097 #define VPX_PLANE_PACKED 0 /**< To be used for all packed formats */ 13098 #define VPX_PLANE_Y 0 /**< Y (Luminance) plane */ 13099 #define VPX_PLANE_U 1 /**< U (Chroma) plane */ 13100 #define VPX_PLANE_V 2 /**< V (Chroma) plane */ 13101 #define VPX_PLANE_ALPHA 3 /**< A (Transparency) plane */ 13102 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 13103 #define PLANE_PACKED VPX_PLANE_PACKED 13104 #define PLANE_Y VPX_PLANE_Y 13105 #define PLANE_U VPX_PLANE_U 13106 #define PLANE_V VPX_PLANE_V 13107 #define PLANE_ALPHA VPX_PLANE_ALPHA 13108 #endif 13109 unsigned char *planes[4]; /**< pointer to the top left pixel 13110 q for each plane */ 13111 int stride[4]; /**< stride between rows for each plane */ 13113 int bps; /**< bits per sample (for packed formats) */ 13115 /* The following member may be set by the application to 13116 * associate data with this image. 13117 */ 13118 void *user_priv; /**< may be set by the application to 13119 associate data with this image. */ 13121 /* The following members should be treated as private. */ 13122 unsigned char *img_data; /**< private */ 13123 int img_data_owner; /**< private */ 13124 int self_allocd; /**< private */ 13125 } vpx_image_t; /**< alias for struct vpx_image */ 13127 /**\brief Representation of a rectangle on a surface */ 13128 typedef struct vpx_image_rect 13129 { 13130 unsigned int x; /**< leftmost column */ 13131 unsigned int y; /**< topmost row */ 13132 unsigned int w; /**< width */ 13133 unsigned int h; /**< height */ 13134 } vpx_image_rect_t; /**< alias for struct vpx_image_rect */ 13136 /*!\brief Open a descriptor, allocating storage for the 13137 * underlying image 13138 * 13139 * Returns a descriptor for storing an image of the given format. 13140 * The storage for the descriptor is allocated on the heap. 13141 * 13142 * \param[in] img Pointer to storage for descriptor. If 13143 * this parameter is NULL, the storage 13144 * for the descriptor will be allocated 13145 * on the heap. 13146 * \param[in] fmt Format for the image 13147 * \param[in] d_w Width of the image 13148 * \param[in] d_h Height of the image 13149 * \param[in] align Alignment, in bytes, of each row in 13150 * the image. 13151 * 13152 * \return Returns a pointer to the initialized image descriptor. 13153 * If the img parameter is non-null, the value of the img 13154 * parameter will be returned. 13155 */ 13156 vpx_image_t *vpx_img_alloc(vpx_image_t *img, 13157 vpx_img_fmt_t fmt, 13158 unsigned int d_w, 13159 unsigned int d_h, 13160 unsigned int align); 13162 /*!\brief Open a descriptor, using existing storage for the 13163 * underlying image 13164 * 13165 * Returns a descriptor for storing an image of the given format. 13166 * The storage for descriptor has been allocated elsewhere, and a 13167 * descriptor is desired to "wrap" that storage. 13168 * 13169 * \param[in] img Pointer to storage for descriptor. If 13170 * this parameter is NULL, the storage 13171 * for the descriptor will be 13172 * allocated on the heap. 13173 * \param[in] fmt Format for the image 13174 * \param[in] d_w Width of the image 13175 * \param[in] d_h Height of the image 13176 * \param[in] align Alignment, in bytes, of each row in 13177 * the image. 13178 * \param[in] img_data Storage to use for the image 13179 * 13180 * \return Returns a pointer to the initialized image descriptor. 13181 * If the img parameter is non-null, the value of the img 13182 * parameter will be returned. 13183 */ 13184 vpx_image_t *vpx_img_wrap(vpx_image_t *img, 13185 vpx_img_fmt_t fmt, 13186 unsigned int d_w, 13187 unsigned int d_h, 13188 unsigned int align, 13189 unsigned char *img_data); 13191 /*!\brief Set the rectangle identifying the displayed portion of 13192 * the image 13193 * 13194 * Updates the displayed rectangle (aka viewport) on the image 13195 * surface to match the specified coordinates and size. 13196 * 13197 * \param[in] img Image descriptor 13198 * \param[in] x leftmost column 13199 * \param[in] y topmost row 13200 * \param[in] w width 13201 * \param[in] h height 13202 * 13203 * \return 0 if the requested rectangle is valid, nonzero 13204 * otherwise. 13205 */ 13206 int vpx_img_set_rect(vpx_image_t *img, 13207 unsigned int x, 13208 unsigned int y, 13209 unsigned int w, 13210 unsigned int h); 13212 /*!\brief Flip the image vertically (top for bottom) 13213 * 13214 * Adjusts the image descriptor's pointers and strides to make 13215 * the image be referenced upside-down. 13216 * 13217 * \param[in] img Image descriptor 13218 */ 13219 void vpx_img_flip(vpx_image_t *img); 13221 /*!\brief Close an image descriptor 13222 * 13223 * Frees all allocated storage associated with an image 13224 * descriptor. 13225 * 13226 * \param[in] img Image descriptor 13227 */ 13228 void vpx_img_free(vpx_image_t *img); 13230 #endif 13231 #ifdef __cplusplus 13232 } 13233 #endif 13235 ---- End code block ---------------------------------------- 13237 20.24. vpx_integer.h 13239 ---- Begin code block -------------------------------------- 13241 /* 13242 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 13243 * 13244 * Use of this source code is governed by a BSD-style license 13245 * that can be found in the LICENSE file in the root of the source 13246 * tree. An additional intellectual property rights grant can be 13247 * found in the file PATENTS. All contributing project authors may 13248 * be found in the AUTHORS file in the root of the source tree. 13249 */ 13251 #ifndef VPX_INTEGER_H 13252 #define VPX_INTEGER_H 13254 /* get ptrdiff_t, size_t, wchar_t, NULL */ 13255 #include 13257 #if defined(_MSC_VER) || defined(VPX_EMULATE_INTTYPES) 13258 typedef signed char int8_t; 13259 typedef signed short int16_t; 13260 typedef signed int int32_t; 13261 typedef unsigned char uint8_t; 13262 typedef unsigned short uint16_t; 13263 typedef unsigned int uint32_t; 13265 #if defined(_MSC_VER) 13266 typedef signed __int64 int64_t; 13267 typedef unsigned __int64 uint64_t; 13268 #define PRId64 "I64d" 13269 #endif 13271 #ifdef HAVE_ARMV6 13272 typedef unsigned int int_fast16_t; 13273 #else 13274 typedef signed short int_fast16_t; 13275 #endif 13276 typedef signed char int_fast8_t; 13277 typedef unsigned char uint_fast8_t; 13279 #ifndef _UINTPTR_T_DEFINED 13280 typedef unsigned int uintptr_t; 13281 #endif 13283 #else 13285 /* Most platforms have the C99 standard integer types. */ 13287 #if defined(__cplusplus) && !defined(__STDC_FORMAT_MACROS) 13288 #define __STDC_FORMAT_MACROS 13289 #endif 13290 #include 13291 #include 13293 #endif 13295 #endif 13297 ---- End code block ---------------------------------------- 13299 20.25. AUTHORS 13301 Aaron Watry 13303 Adrian Grange 13305 Alex Converse 13307 Andoni Morales Alastruey 13308 Andres Mejia 13310 Attila Nagy 13312 Fabio Pedretti 13314 Frank Galligan 13316 Fredrik Soederquist 13318 Fritz Koenig 13320 Gaute Strokkenes 13322 Giuseppe Scrivano 13324 Guillermo Ballester Valor 13326 Henrik Lundin 13328 James Berry 13330 James Zern 13332 Jan Kratochvil 13334 Jeff Muizelaar 13336 Jim Bankoski 13338 Johann Koenig 13340 John Koleszar 13342 Justin Clift 13344 Justin Lebar 13346 Luca Barbato 13348 Makoto Kato 13350 Martin Ettl 13352 Michael Kohler 13354 Mikhal Shemer 13355 Pascal Massimino 13357 Patrik Westin 13359 Paul Wilkins 13361 Pavol Rusnak 13363 Philip Jaegenstedt 13365 Scott LaVarnway 13367 Tero Rintaluoma 13369 Timothy B. Terriberry 13371 Tom Finegan 13373 Yaowu Xu 13375 Yunqing Wang 13377 Google Inc. 13379 The Mozilla Foundation 13381 The Xiph.Org Foundation 13383 20.26. LICENSE 13385 Copyright (c) 2010, Google Inc. All rights reserved. 13387 Redistribution and use in source and binary forms, with or without 13388 modification, are permitted provided that the following conditions 13389 are met: 13391 o Redistributions of source code must retain the above copyright 13392 notice, this list of conditions and the following disclaimer. 13394 o Redistributions in binary form must reproduce the above copyright 13395 notice, this list of conditions and the following disclaimer in 13396 the documentation and/or other materials provided with the 13397 distribution. 13399 o Neither the name of Google nor the names of its contributors may 13400 be used to endorse or promote products derived from this software 13401 without specific prior written permission. 13403 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 13404 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 13405 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 13406 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 13407 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 13408 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 13409 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 13410 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 13411 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 13412 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 13413 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 13414 POSSIBILITY OF SUCH DAMAGE. 13416 20.27. PATENTS 13418 Additional IP Rights Grant (Patents) 13420 "This implementation" means the copyrightable works distributed by 13421 Google as part of the WebM Project. 13423 Google hereby grants to you a perpetual, worldwide, non-exclusive, 13424 no-charge, royalty-free, irrevocable (except as stated in this 13425 section) patent license to make, have made, use, offer to sell, sell, 13426 import, transfer, and otherwise run, modify and propagate the 13427 contents of this implementation of VP8, where such license applies 13428 only to those patent claims, both currently owned by Google and 13429 acquired in the future, licensable by Google that are necessarily 13430 infringed by this implementation of VP8. This grant does not include 13431 claims that would be infringed only as a consequence of further 13432 modification of this implementation. If you or your agent or 13433 exclusive licensee institute or order or agree to the institution of 13434 patent litigation against any entity (including a cross-claim or 13435 counterclaim in a lawsuit) alleging that this implementation of VP8 13436 or any code incorporated within this implementation of VP8 13437 constitutes direct or contributory patent infringement, or inducement 13438 of patent infringement, then any patent rights granted to you under 13439 this License for this implementation of VP8 shall terminate as of the 13440 date such litigation is filed. 13442 21. Security Considerations 13444 A VP8 decoder should take appropriate security considerations into 13445 account, as outlined in [RFC4732] and [RFC3552]. It is extremely 13446 important that a decoder be robust against malicious payloads. 13447 Malicious payloads must not cause the decoder to overrun its 13448 allocated memory or to consume inordinate resources. Although 13449 encoder issues are typically rarer, the same applies to an encoder. 13450 Malicious stream data must not cause the encoder to misbehave, as 13451 this might allow an attacker access to transcoding gateways. 13453 22. IANA Considerations 13455 This document has no actions for IANA. 13457 23. Informative References 13459 [Bell] Bell, T., Cleary, J., and I. Witten, "Text Compression", 13460 1990. 13462 [ITU-R_BT.601] 13463 International Telecommunication Union, "ITU BT.601: Studio 13464 encoding parameters of digital television for standard 4:3 13465 and wide screen 16:9 aspect ratios", January 2007. 13467 [Kernighan] 13468 Kernighan, B. and D. Ritchie, "The C Programming Language 13469 (2nd edition)", April 1988. 13471 [Loeffler] 13472 Loeffler, C., Ligtenberg , A., and G. Moschytz, "Practical 13473 Fast 1-D DCT Algorithms with 11 Multiplications", 13474 May 1989. 13476 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 13477 Requirement Levels", BCP 14, RFC 2119, March 1997. 13479 [RFC3552] Rescorla, E. and B. Korver, "Guidelines for Writing RFC 13480 Text on Security Considerations", BCP 72, RFC 3552, 13481 July 2003. 13483 [RFC3979] Bradner, S., "Intellectual Property Rights in IETF 13484 Technology", BCP 79, RFC 3979, March 2005. 13486 [RFC4732] Handley, M., Rescorla, E., and IAB, "Internet Denial-of- 13487 Service Considerations", RFC 4732, December 2006. 13489 [RFC5378] Bradner, S. and J. Contreras, "Rights Contributors Provide 13490 to the IETF Trust", BCP 78, RFC 5378, November 2008. 13492 [Shannon] Shannon, C., "A Mathematical Theory of Communication", 13493 Bell System Technical Journal Vol. 27, pp. 379-423, 623- 13494 656, July, October 1948. 13496 Authors' Addresses 13498 James Bankoski 13499 Google Inc. 13501 Email: jimbankoski@google.com 13503 John Koleszar 13504 Google Inc. 13506 Email: jkoleszar@google.com 13508 Lou Quillio 13509 Google Inc. 13511 Email: louquillio@google.com 13513 Janne Salonen 13514 Google Inc. 13516 Email: jsalonen@google.com 13518 Paul Wilkins 13519 Google Inc. 13521 Email: paulwilkins@google.com 13523 Yaowu Xu 13524 Google Inc. 13526 Email: yaowu@google.com