idnits 2.17.00 (12 Aug 2021) /tmp/idnits26017/draft-bankoski-vp8-bitstream-05.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 6217 has weird spacing: '...ned int split...' == Line 6658 has weird spacing: '...decoder bool;...' == Line 7093 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 2, 2011) is 3944 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 10006 == 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 10699 == Missing Reference: '-1' is mentioned on line 9586, but not defined -- Looks like a reference, but probably isn't: '0' on line 11663 -- Looks like a reference, but probably isn't: '1' on line 11652 -- Looks like a reference, but probably isn't: '4' on line 13143 -- Looks like a reference, but probably isn't: '7' on line 9259 -- Looks like a reference, but probably isn't: '9' on line 8705 -- Looks like a reference, but probably isn't: '2' on line 11212 -- Looks like a reference, but probably isn't: '5' on line 9623 -- Looks like a reference, but probably isn't: '6' on line 9258 -- Looks like a reference, but probably isn't: '16' on line 10452 -- Looks like a reference, but probably isn't: '12' on line 10405 == Missing Reference: 'MVPcount' is mentioned on line 4898, but not defined == Missing Reference: 'MVPsign' is mentioned on line 4947, but not defined -- Looks like a reference, but probably isn't: '128' on line 6328 == Missing Reference: 'SIGN' is mentioned on line 8182, but not defined == Missing Reference: 'NEAREST' is mentioned on line 8499, but not defined == Missing Reference: 'NEAR' is mentioned on line 8502, but not defined == Missing Reference: 'BEST' is mentioned on line 8517, but not defined -- Looks like a reference, but probably isn't: '15' on line 8520 -- Looks like a reference, but probably isn't: '10' on line 8705 -- Looks like a reference, but probably isn't: '18' on line 8848 -- Looks like a reference, but probably isn't: '14' on line 8860 == Missing Reference: '-stride' is mentioned on line 9105, but not defined == Missing Reference: '-2' is mentioned on line 9585, but not defined -- Looks like a reference, but probably isn't: '25' on line 10412 -- Looks like a reference, but probably isn't: '80' on line 11679 == Unused Reference: 'RFC2119' is defined on line 13508, but no explicit reference was found in the text == Unused Reference: 'RFC3979' is defined on line 13515, but no explicit reference was found in the text == Unused Reference: 'RFC5378' is defined on line 13521, 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 3, 2012 J. Salonen 6 P. Wilkins 7 Y. Xu 8 Google Inc. 9 August 2, 2011 11 VP8 Data Format and Decoding Guide 12 draft-bankoski-vp8-bitstream-05 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 3, 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 . . . . . . . . . 92 100 15.2. Simple Filter . . . . . . . . . . . . . . . . . . . . . 94 101 15.3. Normal Filter . . . . . . . . . . . . . . . . . . . . . 98 102 15.4. Calculation of Control Parameters . . . . . . . . . . . 103 103 16. Interframe Macroblock Prediction Records . . . . . . . . . . 105 104 16.1. Intra-Predicted Macroblocks . . . . . . . . . . . . . . 105 105 16.2. Inter-Predicted Macroblocks . . . . . . . . . . . . . . 106 106 16.3. Mode and Motion Vector Contexts . . . . . . . . . . . . 107 107 16.4. Split Prediction . . . . . . . . . . . . . . . . . . . . 113 108 17. Motion Vector Decoding . . . . . . . . . . . . . . . . . . . 117 109 17.1. Coding of Each Component . . . . . . . . . . . . . . . . 117 110 17.2. Probability Updates . . . . . . . . . . . . . . . . . . 119 111 18. Interframe Prediction . . . . . . . . . . . . . . . . . . . . 122 112 18.1. Bounds on and Adjustment of Motion Vectors . . . . . . . 122 113 18.2. Prediction Subblocks . . . . . . . . . . . . . . . . . . 123 114 18.3. Sub-pixel Interpolation . . . . . . . . . . . . . . . . 124 115 18.4. Filter Properties . . . . . . . . . . . . . . . . . . . 127 116 19. Annex A: Bitstream Syntax . . . . . . . . . . . . . . . . . . 130 117 19.1. Uncompressed Data Chunk . . . . . . . . . . . . . . . . 130 118 19.2. Frame Header . . . . . . . . . . . . . . . . . . . . . . 132 119 19.3. Macroblock Data . . . . . . . . . . . . . . . . . . . . 143 120 20. Attachment One: Reference Decoder Source Code . . . . . . . . 147 121 20.1. bit_ops.h . . . . . . . . . . . . . . . . . . . . . . . 147 122 20.2. bool_decoder.h . . . . . . . . . . . . . . . . . . . . . 147 123 20.3. dequant_data.h . . . . . . . . . . . . . . . . . . . . . 151 124 20.4. dixie.c . . . . . . . . . . . . . . . . . . . . . . . . 151 125 20.5. dixie.h . . . . . . . . . . . . . . . . . . . . . . . . 163 126 20.6. dixie_loopfilter.c . . . . . . . . . . . . . . . . . . . 170 127 20.7. dixie_loopfilter.h . . . . . . . . . . . . . . . . . . . 182 128 20.8. idct_add.c . . . . . . . . . . . . . . . . . . . . . . . 182 129 20.9. idct_add.h . . . . . . . . . . . . . . . . . . . . . . . 186 130 20.10. mem.h . . . . . . . . . . . . . . . . . . . . . . . . . 186 131 20.11. modemv.c . . . . . . . . . . . . . . . . . . . . . . . . 188 132 20.12. modemv.h . . . . . . . . . . . . . . . . . . . . . . . . 203 133 20.13. modemv_data.h . . . . . . . . . . . . . . . . . . . . . 203 134 20.14. predict.c . . . . . . . . . . . . . . . . . . . . . . . 208 135 20.15. predict.h . . . . . . . . . . . . . . . . . . . . . . . 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 To facilitate efficient implementation, the VP8 decoding algorithms 3763 generally, and the loop filter especially, were designed with SIMD 3764 ("Single Instruction Multiple Datum" or "integer vector") processors 3765 in mind. The reference decoder implementation of loop filtering 3766 (found in loopfilter.c) is, in effect, a portable SIMD specification 3767 of the loop filtering algorithms intended to simplify a realization 3768 on an actual SIMD processor. 3770 Unfortunately, the approach taken there does not lead to maximal 3771 efficency (restricted to the C language, that is) and, as far as a 3772 pure algorithm specification is concerned, is in places obscure. For 3773 example, various aspects of filtering are conditioned on absolute 3774 differences lying below certain thresholds. An ordinary C 3775 implementation would simply discriminate amongst these behaviors 3776 using if statements. The reference decoder instead effects this by 3777 "masking arithmetic", that is, using "and" operations to 3778 (conditionally) zero-out values to be added or subtracted to pixels. 3779 Furthermore, the structure holding the various threshold values is 3780 artificially parallelized. While this mimics closely the approach 3781 taken in vector-processor machine language, it is not how one usually 3782 programs in C. 3784 In this document, we take a different approach and present the 3785 algorithms in a more straightforward, idiomatic, and terse C style. 3786 Together with the reference version, we hope to provide the "best of 3787 both worlds", that is, a pure algorithm specification here and a 3788 strong suggestion as to an optimal actual implementation in 3789 loopfilter.c. 3791 We begin by discussing the aspects of loop filtering that are 3792 independent of the controlling parameters and type of filter chosen. 3794 15.1. Filter Geometry and Overall Procedure 3796 The Y, U, and V planes are processed independently and, except for 3797 the values of certain control parameters (derived from the 3798 loop_filter_level and sharpness_level), identically. 3800 The loop filter acts on the edges between adjacent macroblocks and on 3801 the edges between adjacent subblocks of a macroblock. All such edges 3802 are horizontal or vertical. For each pixel position on an edge, a 3803 small number (two or three) of pixels adjacent to either side of the 3804 position are examined and possibly modified. The displacements of 3805 these pixels are at a right angle to the edge orientation, that is, 3806 for a horizontal edge, we treat the pixels immediately above and 3807 below the edge position, for a vertical edge, we treat the pixels 3808 immediately to the left and right of the edge. 3810 We call this collection of pixels associated to an edge position a 3811 segment; the length of a segment is 2, 4, 6, or 8. Excepting that 3812 the normal filter uses slightly different algorithms for, and that 3813 either filter may apply different control parameters to, the edges 3814 between macroblocks and those between subblocks, the treatment of 3815 edges is quite uniform: All segments straddling an edge are treated 3816 identically, there is no distinction between the treatment of 3817 horizontal and vertical edges, whether between macroblocks or between 3818 subblocks. 3820 As a consequence, adjacent subblock edges within a macroblock may be 3821 concatenated and processed in their entirety. There is a single 3822 8-pixel long vertical edge horizontally centered in each of the U and 3823 V blocks (the concatenation of upper and lower 4-pixel edges between 3824 chroma subblocks), and three 16-pixel long vertical edges at 3825 horizontal positions 1/4, 1/2, and 3/4 the width of the luma 3826 macroblock, each representing the concatenation of four 4-pixel sub- 3827 edges between pairs of Y subblocks. 3829 The macroblocks comprising the frame are processed in the usual 3830 raster-scan order. Each macroblock is "responsible for" the inter- 3831 macroblock edges immediately above and left of it (but not the edges 3832 below and right of it), as well as the edges between its subblocks. 3834 For each macroblock M, there are four filtering steps, which are, 3835 (almost) in order: 3837 1. If M is not on the leftmost column of macroblocks, filter across 3838 the left (vertical) inter-macroblock edge of M. 3840 2. Filter across the vertical subblock edges within M. 3842 3. If M is not on the topmost row of macroblocks, filter across the 3843 top (horizontal) inter-macroblock edge of M. 3845 4. Filter across the horizontal subblock edges within M. 3847 We write MY, MU, and MV for the planar constituents of M, that is, 3848 the 16x16 luma block, 8x8 U block, and 8x8 V block comprising M. 3850 In step 1, for each of the three blocks MY, MU, and MV, we filter 3851 each of the (16 luma or 8 chroma) segments straddling the column 3852 separating the block from the block immediately to the left of it, 3853 using the inter-macroblock filter and controls associated to the 3854 loop_filter_level and sharpness_level. 3856 In step 4, we filter across the (three luma and one each for U and V) 3857 vertical subblock edges described above, this time using the inter- 3858 subblock filter and controls. 3860 Step 2 and 4 are skipped for macroblocks that satisfy both of the 3861 following two conditions: 3863 1. Macroblock coding mode is neither B_PRED nor SPLTMV; and 3865 2. There is no DCT coefficient coded for the whole macroblock. 3867 For these macroblocks, loop filtering for edges between subblocks 3868 internal to a macroblock is effectively skipped. This skip strategy 3869 significantly reduces VP8 loop-filtering complexity. 3871 Edges between macroblocks and those between subblocks are treated 3872 with different control parameters (and, in the case of the normal 3873 filter, with different algorithms); luma and chroma edges are also 3874 treated with different control parameters. Except for pixel 3875 addressing, there is no distinction between the treatment of vertical 3876 and horizontal edges. Luma edges are always 16 pixels long, chroma 3877 edges are always 8 pixels long, and the segments straddling an edge 3878 are treated identically; this of course facilitates vector 3879 processing. 3881 Because many pixels belong to segments straddling two or more edges, 3882 and so will be filtered more than once, the order in which edges are 3883 processed given above must be respected by any implementation. 3884 Within a single edge, however, the segments straddling that edge are 3885 disjoint and the order in which these segments are processed is 3886 immaterial. 3888 Before taking up the filtering algorithms themselves, we should 3889 emphasize a point already made: Even though the pixel segments 3890 associated to a macroblock are antecedent to the macroblock (that is, 3891 lie within the macroblock or in already-constructed macroblocks), a 3892 macroblock must not be filtered immediately after its 3893 "reconstruction" (described in Chapter 14). Rather, the loop filter 3894 applies after all the macroblocks have been "reconstructed" (i.e., 3895 had their predictor summed with their residue); correct decoding is 3896 predicated on the fact that already-constructed portions of the 3897 current frame referenced via intra-prediction (described in Chapter 3898 12) are not yet filtered. 3900 15.2. Simple Filter 3902 Having described the overall procedure of, and pixels affected by, 3903 the loop filter, we turn our attention to the treatment of individual 3904 segments straddling edges. We begin by describing the simple filter, 3905 which, as the reader might guess, is somewhat simpler than the normal 3906 filter. 3908 Note that the simple filter only applies to luma edges. Chroma edges 3909 are left unfiltered. 3911 Roughly speaking, the idea of loop filtering is, within limits, to 3912 reduce the difference between pixels straddling an edge. Differences 3913 in excess of a threshold (associated to the loop_filter_level) are 3914 assumed to be "natural" and are unmodified; differences below the 3915 threshold are assumed to be artifacts of quantization and the 3916 (partially) separate coding of blocks, and are reduced via the 3917 procedures described below. While the loop_filter_level is in 3918 principle arbitrary, the levels chosen by a VP8 compressor tend to be 3919 correlated to quantization levels. 3921 Most of the filtering arithmetic is done using 8-bit signed operands 3922 (having a range -128 to +127, inclusive), supplemented by 16-bit 3923 temporaries holding results of multiplies. 3925 Sums and other temporaries need to be "clamped" to a valid signed 3926 8-bit range: 3928 ---- Begin code block -------------------------------------- 3930 int8 c( int v) 3931 { 3932 return (int8) (v < -128 ? -128 : (v < 128 ? v : 127)); 3933 } 3935 ---- End code block ---------------------------------------- 3937 Since pixel values themselves are unsigned 8-bit numbers, we need to 3938 convert between signed and unsigned values: 3940 ---- Begin code block -------------------------------------- 3942 /* Convert pixel value (0 <= v <= 255) to an 8-bit signed 3943 number. */ 3944 int8 u2s( Pixel v) { return (int8) (v - 128);} 3946 /* Clamp, then convert signed number back to pixel value. */ 3947 Pixel s2u( int v) { return (Pixel) ( c(v) + 128);} 3949 ---- End code block ---------------------------------------- 3951 Filtering is often predicated on absolute-value thresholds. The 3952 following function is the equivalent of the standard library function 3953 abs, whose prototype is found in the standard header file stdlib.h. 3954 For us, the argument v is always the difference between two pixels 3955 and lies in the range -255 <= v <= +255. 3957 ---- Begin code block -------------------------------------- 3959 int abs( int v) { return v < 0? -v : v;} 3961 ---- End code block ---------------------------------------- 3963 An actual implementation would of course use inline functions or 3964 macros to accomplish these trivial procedures (which are used by both 3965 the normal and simple loop filters). An optimal implementation would 3966 probably express them in machine language, perhaps using SIMD vector 3967 instructions. On many SIMD processors, the saturation accomplished 3968 by the above clamping function is often folded into the arithmetic 3969 instructions themselves, obviating the explicit step taken here. 3971 To simplify the specification of relative pixel positions, we use the 3972 word before to mean "immediately above" (for a vertical segment 3973 straddling a horizontal edge) or "immediately to the left of" (for a 3974 horizontal segment straddling a vertical edge) and the word after to 3975 mean "immediately below" or "immediately to the right of". 3977 Given an edge, a segment, and a limit value, the simple loop filter 3978 computes a value based on the four pixels that straddle the edge (two 3979 either side). If that value is below a supplied limit, then, very 3980 roughly speaking, the two pixel values are brought closer to each 3981 other, "shaving off" something like a quarter of the difference. The 3982 same procedure is used for all segments straddling any type of edge, 3983 regardless of the nature (inter-macroblock, inter-subblock, luma, or 3984 chroma) of the edge; only the limit value depends on the edge-type. 3986 The exact procedure (for a single segment) is as follows; the 3987 subroutine common_adjust is used by both the simple filter presented 3988 here and the normal filters discussed in Section 15.3. 3990 ---- Begin code block -------------------------------------- 3992 int8 common_adjust( 3993 int use_outer_taps, /* filter is 2 or 4 taps wide */ 3994 const Pixel *P1, /* pixel before P0 */ 3995 Pixel *P0, /* pixel before edge */ 3996 Pixel *Q0, /* pixel after edge */ 3997 const Pixel *Q1 /* pixel after Q0 */ 3998 ) { 3999 cint8 p1 = u2s( *P1); /* retrieve and convert all 4 pixels */ 4000 cint8 p0 = u2s( *P0); 4001 cint8 q0 = u2s( *Q0); 4002 cint8 q1 = u2s( *Q1); 4004 /* Disregarding clamping, when "use_outer_taps" is false, 4005 "a" is 3*(q0-p0). Since we are about to divide "a" by 4006 8, in this case we end up multiplying the edge 4007 difference by 5/8. 4009 When "use_outer_taps" is true (as for the simple filter), 4010 "a" is p1 - 3*p0 + 3*q0 - q1, which can be thought of as 4011 a refinement of 2*(q0 - p0) and the adjustment is 4012 something like (q0 - p0)/4. */ 4014 int8 a = c( ( use_outer_taps? c(p1 - q1) : 0 ) + 3*(q0 - p0) ); 4016 /* b is used to balance the rounding of a/8 in the case where 4017 the "fractional" part "f" of a/8 is exactly 1/2. */ 4019 cint8 b = (c(a + 3)) >> 3; 4021 /* Divide a by 8, rounding up when f >= 1/2. 4022 Although not strictly part of the "C" language, 4023 the right-shift is assumed to propagate the sign bit. */ 4025 a = c( a + 4) >> 3; 4027 /* Subtract "a" from q0, "bringing it closer" to p0. */ 4029 *Q0 = s2u( q0 - a); 4031 /* Add "a" (with adjustment "b") to p0, "bringing it closer" 4032 to q0. 4034 The clamp of "a+b", while present in the reference decoder, 4035 is superfluous; we have -16 <= a <= 15 at this point. */ 4037 *P0 = s2u( p0 + b); 4039 return a; 4040 } 4042 ---- End code block ---------------------------------------- 4044 ---- Begin code block -------------------------------------- 4046 void simple_segment( 4047 uint8 edge_limit, /* do nothing if edge difference 4048 exceeds limit */ 4049 const Pixel *P1, /* pixel before P0 */ 4050 Pixel *P0, /* pixel before edge */ 4051 Pixel *Q0, /* pixel after edge */ 4052 const Pixel *Q1 /* pixel after Q0 */ 4053 ) { 4054 if( (abs(*P0 - *Q0)*2 + abs(*P1-*Q1)/2) <= edge_limit)) 4055 common_adjust( 1, P1, P0, Q0, Q1); /* use outer taps */ 4056 } 4058 ---- End code block ---------------------------------------- 4059 We make a couple of remarks about the rounding procedure above. When 4060 b is zero (that is, when the "fractional part" of a is not 1/2 ), we 4061 are (except for clamping) adding the same number to p0 as we are 4062 subtracting from q0. This preserves the average value of p0 and q0 4063 but the resulting difference between p0 and q0 is always even; in 4064 particular, the smallest non-zero gradation +-1 is not possible here. 4066 When b is one, the value we add to p0 (again except for clamping) is 4067 one less than the value we are subtracting from q0. In this case, 4068 the resulting difference is always odd (and the small gradation +-1 4069 is possible) but the average value is reduced by 1/2, yielding, for 4070 instance, a very slight darkening in the luma plane. (In the very 4071 unlikely event of appreciable darkening after a large number of 4072 interframes, a compressor would of course eventually compensate for 4073 this in the selection of predictor and/or residue.) 4075 The derivation of the edge_limit value used above, which depends on 4076 the loop_filter_level and sharpness_level, as well as the type of 4077 edge being processed, will be taken up after we describe the normal 4078 loop filtering algorithm below. 4080 15.3. Normal Filter 4082 The normal loop filter is a refinement of the simple loop filter; all 4083 of the general discussion above applies here as well. In particular, 4084 the functions c, u2s, s2u, abs, and common_adjust are used by both 4085 the normal and simple filters. 4087 As mentioned above, the normal algorithms for inter-macroblock and 4088 inter-subblock edges differ. Nonetheless, they have a great deal in 4089 common: They use similar threshold algorithms to disable the filter 4090 and to detect high internal edge variance (which influences the 4091 filtering algorithm). Both algorithms also use, at least 4092 conditionally, the simple filter adjustment procedure described 4093 above. 4095 The common thresholding algorithms are as follows. 4097 ---- Begin code block -------------------------------------- 4099 /* All functions take (among other things) a segment (of length 4100 at most 4 + 4 = 8) symmetrically straddling an edge. 4102 The pixel values (or pointers) are always given in order, 4103 from the "beforemost" to the "aftermost". So, for a 4104 horizontal edge (written "|"), an 8-pixel segment would be 4105 ordered p3 p2 p1 p0 | q0 q1 q2 q3. */ 4107 /* Filtering is disabled if the difference between any two 4108 adjacent "interior" pixels in the 8-pixel segment exceeds 4109 the relevant threshold (I). A more complex thresholding 4110 calculation is done for the group of four pixels that 4111 straddle the edge, in line with the calculation in 4112 simple_segment() above. */ 4114 int filter_yes( 4115 uint8 I, /* limit on interior differences */ 4116 uint8 E, /* limit at the edge */ 4118 cint8 p3, cint8 p2, cint8 p1, cint8 p0, /* pixels before 4119 edge */ 4120 cint8 q0, cint8 q1, cint8 q2, cint8 q3 /* pixels after 4121 edge */ 4122 ) { 4123 return (abs(p0 - q0)*2 + abs(p1-q1)/2) <= E 4124 && abs(p3 - p2) <= I && abs(p2 - p1) <= I && 4125 abs(p1 - p0) <= I 4126 && abs(q3 - q2) <= I && abs(q2 - q1) <= I && 4127 abs(q1 - q0) <= I; 4128 } 4130 ---- End code block ---------------------------------------- 4131 ---- Begin code block -------------------------------------- 4133 /* Filtering is altered if (at least) one of the differences 4134 on either side of the edge exceeds a threshold (we have 4135 "high edge variance"). */ 4137 int hev( 4138 uint8 threshold, 4139 cint8 p1, cint8 p0, /* pixels before edge */ 4140 cint8 q0, cint8 q1 /* pixels after edge */ 4141 ) { 4142 return abs(p1 - p0) > threshold || abs(q1 - q0) > threshold; 4143 } 4145 ---- End code block ---------------------------------------- 4147 The subblock filter is a variant of the simple filter. In fact, if 4148 we have high edge variance, the adjustment is exactly as for the 4149 simple filter. Otherwise, the simple adjustment (without outer taps) 4150 is applied and the two pixels one step in from the edge pixels are 4151 adjusted by roughly half the amount by which the two edge pixels are 4152 adjusted; since the edge adjustment here is essentially 3/8 the edge 4153 difference, the inner adjustment is approximately 3/16 the edge 4154 difference. 4156 ---- Begin code block -------------------------------------- 4158 void subblock_filter( 4159 uint8 hev_threshold, /* detect high edge variance */ 4160 uint8 interior_limit, /* possibly disable filter */ 4161 uint8 edge_limit, 4162 cint8 *P3, cint8 *P2, int8 *P1, int8 *P0, /* pixels before 4163 edge */ 4164 int8 *Q0, int8 *Q1, cint8 *Q2, cint8 *Q3 /* pixels after 4165 edge */ 4166 ) { 4167 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4168 p0 = u2s(*P0); 4169 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4170 q3 = u2s(*Q3); 4172 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4173 p0, p1, p2, p3)) 4174 { 4175 const int hv = hev( hev_threshold, p1, p0, q0, q1); 4177 cint8 a = ( common_adjust( hv, P1, P0, Q0, Q1) + 1) >> 1; 4179 if( !hv) { 4180 *Q1 = s2u( q1 - a); 4181 *P1 = s2u( p1 + a); 4182 } 4183 } 4184 } 4186 ---- End code block ---------------------------------------- 4188 The inter-macroblock filter has potentially wider scope. If the edge 4189 variance is high, it performs the simple adjustment (using the outer 4190 taps, just like the simple filter and the corresponding case of the 4191 normal subblock filter). If the edge variance is low, we begin with 4192 the same basic filter calculation and apply multiples of it to pixel 4193 pairs symmetric about the edge; the magnitude of adjustment decays as 4194 we move away from the edge and six of the pixels in the segment are 4195 affected. 4197 ---- Begin code block -------------------------------------- 4199 void MBfilter( 4200 uint8 hev_threshold, /* detect high edge variance */ 4201 uint8 interior_limit, /* possibly disable filter */ 4202 uint8 edge_limit, 4203 cint8 *P3, int8 *P2, int8 *P1, int8 *P0, /* pixels before 4204 edge */ 4205 int8 *Q0, int8 *Q1, int8 *Q2, cint8 *Q3 /* pixels after 4206 edge */ 4207 ) { 4208 cint8 p3 = u2s(*P3), p2 = u2s(*P2), p1 = u2s(*P1), 4209 p0 = u2s(*P0); 4210 cint8 q0 = u2s(*Q0), q1 = u2s(*Q1), q2 = u2s(*Q2), 4211 q3 = u2s(*Q3); 4213 if( filter_yes( interior_limit, edge_limit, q3, q2, q1, q0, 4214 p0, p1, p2, p3)) 4215 { 4216 if( !hev( hev_threshold, p1, p0, q0, q1)) 4217 { 4218 /* Same as the initial calculation in "common_adjust", 4219 w is something like twice the edge difference */ 4221 const int8 w = c( c(p1 - q1) + 3*(q0 - p0) ); 4223 /* 9/64 is approximately 9/63 = 1/7 and 1<<7 = 128 = 4224 2*64. So this a, used to adjust the pixels adjacent 4225 to the edge, is something like 3/7 the edge 4226 difference. */ 4228 int8 a = c( (27*w + 63) >> 7); 4230 *Q0 = s2u( q0 - a); *P0 = s2u( p0 + a); 4232 /* Next two are adjusted by 2/7 the edge difference */ 4234 a = c( (18*w + 63) >> 7); 4236 *Q1 = s2u( q1 - a); *P1 = s2u( p1 + a); 4238 /* Last two are adjusted by 1/7 the edge difference */ 4240 a = c( (9*w + 63) >> 7); 4242 *Q2 = s2u( q2 - a); *P2 = s2u( p2 + a); 4244 } else /* if hev, do simple filter */ 4245 common_adjust( 1, P1, P0, Q0, Q1); /* using outer 4246 taps */ 4247 } 4249 } 4251 ---- End code block ---------------------------------------- 4253 15.4. Calculation of Control Parameters 4255 We conclude the discussion of loop filtering by showing how the 4256 thresholds supplied to the procedures above are derived from the two 4257 control parameters sharpness_level (an unsigned 3-bit number having 4258 maximum value 7) and loop_filter_level (an unsigned 6-bit number 4259 having maximum value 63). 4261 While the sharpness_level is constant over the frame, individual 4262 macroblocks may override the loop_filter_level with one of four 4263 possibilities supplied in the frame header (as described in Chapter 4264 10). 4266 Both the simple and normal filters disable filtering if a value 4267 derived from the four pixels that straddle the edge (2 either side) 4268 exceeds a threshold / limit value. 4270 ---- Begin code block -------------------------------------- 4272 /* Luma and Chroma use the same inter-macroblock edge limit */ 4273 uint8 mbedge_limit = ((loop_filter_level + 2) * 2) + 4274 interior_limit; 4276 /* Luma and Chroma use the same inter-subblock edge limit */ 4277 uint8 sub_bedge_limit = (loop_filter_level * 2) + interior_limit; 4279 ---- End code block ---------------------------------------- 4281 The remaining thresholds are used only by the normal filters. The 4282 filter-disabling interior difference limit is the same for all edges 4283 (luma, chroma, inter-subblock, inter-macroblock) and is given by the 4284 following. 4286 ---- Begin code block -------------------------------------- 4288 uint8 interior_limit = loop_filter_level; 4290 if( sharpness_level) 4291 { 4292 interior_limit >>= sharpness_level > 4 ? 2 : 1; 4293 if( interior_limit > 9 - sharpness_level) 4294 interior_limit = 9 - sharpness_level; 4295 } 4296 if( !interior_limit) 4297 interior_limit = 1; 4299 ---- End code block ---------------------------------------- 4300 Finally, we give the derivation of the high edge-variance threshold, 4301 which is also the same for all edge types. 4303 ---- Begin code block -------------------------------------- 4305 uint8 hev_threshold = 0; 4307 if( we_are_decoding_akey_frame) /* current frame is a key frame */ 4308 { 4309 if( loop_filter_level >= 40) 4310 hev_threshold = 2; 4311 else if( loop_filter_level >= 15) 4312 hev_threshold = 1; 4313 } 4314 else /* current frame is an interframe */ 4315 { 4316 if( loop_filter_level >= 40) 4317 hev_threshold = 3; 4318 else if( loop_filter_level >= 20) 4319 hev_threshold = 2; 4320 else if( loop_filter_level >= 15) 4321 hev_threshold = 1; 4322 } 4324 ---- End code block ---------------------------------------- 4326 16. Interframe Macroblock Prediction Records 4328 We describe the layout and semantics of the prediction records for 4329 macroblocks in an interframe. 4331 After the feature specification (which is described in Chapter 10 and 4332 is identical for intraframes and interframes), there comes a Bool( 4333 prob_intra), which indicates inter-prediction (i.e., prediction from 4334 prior frames) when true and intra-prediction (i.e., prediction from 4335 already-coded portions of the current frame) when false. The zero- 4336 probability prob_intra is set by field J of the frame header. 4338 16.1. Intra-Predicted Macroblocks 4340 For intra-prediction, the layout of the prediction data is 4341 essentially the same as the layout for key frames, although the 4342 contexts used by the decoding process are slightly different. 4344 As discussed in Chapter 8, the "outer" Y mode here uses a different 4345 tree from that used in key frames, repeated here for convenience. 4347 ---- Begin code block -------------------------------------- 4349 const tree_index ymode_tree [2 * (num_ymodes - 1)] = 4350 { 4351 -DC_PRED, 2, /* root: DC_PRED = "0", "1" subtree */ 4352 4, 6, /* "1" subtree has 2 descendant subtrees */ 4353 -V_PRED, -H_PRED, /* "10" subtree: V_PRED = "100", 4354 H_PRED = "101" */ 4355 -TM_PRED, -B_PRED /* "11" subtree: TM_PRED = "110", 4356 B_PRED = "111" */ 4357 }; 4359 ---- End code block ---------------------------------------- 4361 The probability table used to decode this tree is variable. As 4362 described in Section 9, it (along with the similarly-treated UV 4363 table) can be updated by field J of the frame header. Similar to the 4364 coefficient-decoding probabilities, such updates are cumulative and 4365 affect all ensuing frames until the next key frame or explicit 4366 update. The default probabilities for the Y and UV tables are 4368 ---- Begin code block -------------------------------------- 4370 Prob ymode_prob [num_ymodes - 1] = { 112, 86, 140, 37}; 4371 Prob uv_mode_prob [num_uv_modes - 1] = { 162, 101, 204}; 4373 ---- End code block ---------------------------------------- 4374 These defaults must be restored after detection of a key frame. 4376 Just as for key frames, if the Y mode is B_PRED, there next comes an 4377 encoding of the intra_bpred mode used by each of the sixteen Y 4378 subblocks. These encodings use the same tree as does that for key 4379 frames but, in place of the contexts used in key frames, use the 4380 single fixed probability table 4382 ---- Begin code block -------------------------------------- 4384 const Prob bmode_prob [num_intra_bmodes - 1] = { 4385 120, 90, 79, 133, 87, 85, 80, 111, 151 4386 }; 4388 ---- End code block ---------------------------------------- 4390 Last comes the chroma mode, again coded using the same tree as that 4391 for key frames, this time using the dynamic uv_mode_prob table 4392 described above. 4394 The calculation of the intra-prediction buffer is identical to that 4395 described for key frames in Chapter 12. 4397 16.2. Inter-Predicted Macroblocks 4399 Otherwise (when the above bool is true), we are using inter- 4400 prediction (which of course only happens for interframes), to which 4401 we now restrict our attention. 4403 The next datum is then another bool, B( prob_last), selecting the 4404 reference frame. If 0, the reference frame is previous frame (last 4405 frame); if 1, another bool (prob_gf) selects the reference frame 4406 between golden frame (0) or altref frame (1). The probabilities 4407 prob_last and prob_gf are set in field J of the frame header. 4409 Together with setting the reference frame, the purpose of inter-mode 4410 decoding is to set a motion vector for each of the sixteen Y 4411 subblocks of the current macroblock. This then defines the 4412 calculation of the inter-prediction buffer (detailed in Chapter 18). 4413 While the net effect of inter-mode decoding is straightforward, the 4414 implementation is somewhat complex; the (lossless) compression 4415 achieved by this method justifies the complexity. 4417 After the reference frame selector comes the mode (or motion vector 4418 reference) applied to the macroblock as a whole, coded using the 4419 following enumeration and tree. Setting mv_nearest = num_ymodes is a 4420 convenience that allows a single variable to unambiguously hold an 4421 inter- or intraprediction mode. 4423 ---- Begin code block -------------------------------------- 4425 typedef enum 4426 { 4427 mv_nearest = num_ymodes, /* use "nearest" motion vector 4428 for entire MB */ 4429 mv_near, /* use "next nearest" "" */ 4430 mv_zero, /* use zero "" */ 4431 mv_new, /* use explicit offset from 4432 implicit "" */ 4433 mv_split, /* use multiple motion vectors */ 4435 num_mv_refs = mv_split + 1 - mv_nearest 4436 } 4437 mv_ref; 4439 const tree_index mv_ref_tree [2 * (num_mv_refs - 1)] = 4440 { 4441 -mv_zero, 2, /* zero = "0" */ 4442 -mv_nearest, 4, /* nearest = "10" */ 4443 -mv_near, 6, /* near = "110" */ 4444 -mv_new, -mv_split /* new = "1110", split = "1111" */ 4445 }; 4447 ---- End code block ---------------------------------------- 4449 16.3. Mode and Motion Vector Contexts 4451 The probability table used to decode the mv_ref, along with three 4452 reference motion vectors used by the selected mode, is calculated via 4453 a survey of the already-decoded motion vectors in (up to) 3 nearby 4454 macroblocks. 4456 The algorithm generates a sorted list of distinct motion vectors 4457 adjacent to the search site. The best_mv is the vector with the 4458 highest score. The nearest_mv is the non-zero vector with the 4459 highest score. The near_mv is the non-zero vector with the next 4460 highest score. The number of motion vectors coded using the SPLITMV 4461 mode is scored using the same weighting and is returned with the 4462 scores of the best, nearest, and near vectors. 4464 The three adjacent macroblocks above, left, and above-left are 4465 considered in order. If the macroblock is intra-coded, no action is 4466 taken. Otherwise, the motion vector is compared to other previously 4467 found motion vectors to determine if it has been seen before, and if 4468 so contributes its weight to that vector, otherwise enters a new 4469 vector in the list. The above and left vectors have twice the weight 4470 of the above-left vector. 4472 As is the case with many contexts used by VP8, it is possible for 4473 macroblocks near the top or left edges of the image to reference 4474 blocks that are outside the visible image. VP8 provides a border of 4475 1 macroblock filled with 0x0 motion vectors left of the left edge, 4476 and a border filled with 0,0 motion vectors of 1 macroblocks above 4477 the top edge. 4479 Much of the process is more easily described in C than in English. 4480 The reference code for this can be found in findnearmv.c. The 4481 calculation of reference vectors, probability table, and, finally, 4482 the inter-prediction mode itself is implemented as follows. 4484 ---- Begin code block -------------------------------------- 4486 typedef union 4487 { 4488 unsigned int as_int; 4489 MV as_mv; 4490 } int_mv; /* facilitates rapid equality tests */ 4492 static void mv_bias(MODE_INFO *x,int refframe, int_mv *mvp, 4493 int * ref_frame_sign_bias ) 4494 { 4495 MV xmv; 4496 xmv = x->mbmi.mv.as_mv; 4497 if ( ref_frame_sign_bias[x->mbmi.ref_frame] != 4498 ref_frame_sign_bias[refframe] ) 4499 { 4500 xmv.row*=-1; 4501 xmv.col*=-1; 4502 } 4503 mvp->as_mv = xmv; 4504 } 4506 ---- End code block ---------------------------------------- 4507 ---- Begin code block -------------------------------------- 4509 void vp8_clamp_mv(MV *mv, const MACROBLOCKD *xd) 4510 { 4511 if ( mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN) ) 4512 mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN; 4513 else if ( mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN ) 4514 mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN; 4516 if ( mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN) ) 4517 mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN; 4518 else if ( mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN ) 4519 mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN; 4520 } 4522 ---- End code block ---------------------------------------- 4524 In the function vp8_find_near_mvs(), the vectors "nearest" and "near" 4525 are used by the corresponding modes. 4527 The vector best_mv is used as a base for explicitly-coded motion 4528 vectors. 4530 The first three entries in the return value cnt are (in order) 4531 weighted census values for "zero", "nearest", and "near" vectors. 4532 The final value indicates the extent to which SPLIT_MV was used by 4533 the neighboring macroblocks. The largest possible "weight" value in 4534 each case is 5. 4536 ---- Begin code block -------------------------------------- 4538 void vp8_find_near_mvs 4539 ( 4540 MACROBLOCKD *xd, 4541 const MODE_INFO *here, 4542 MV *nearest, 4543 MV *near, 4544 MV *best_mv, 4545 int cnt[4], 4546 int refframe, 4547 int * ref_frame_sign_bias 4548 ) 4549 { 4550 const MODE_INFO *above = here - xd->mode_info_stride; 4551 const MODE_INFO *left = here - 1; 4552 const MODE_INFO *aboveleft = above - 1; 4553 int_mv near_mvs[4]; 4554 int_mv *mv = near_mvs; 4555 int *cntx = cnt; 4556 enum {CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV}; 4558 /* Zero accumulators */ 4559 mv[0].as_int = mv[1].as_int = mv[2].as_int = 0; 4560 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 4562 /* Process above */ 4563 if(above->mbmi.ref_frame != INTRA_FRAME) { 4564 if(above->mbmi.mv.as_int) { 4565 (++mv)->as_int = above->mbmi.mv.as_int; 4566 mv_bias(above, refframe, mv, ref_frame_sign_bias); 4567 ++cntx; 4568 } 4569 *cntx += 2; 4570 } 4572 /* Process left */ 4573 if(left->mbmi.ref_frame != INTRA_FRAME) { 4574 if(left->mbmi.mv.as_int) { 4575 int_mv this_mv; 4577 this_mv.as_int = left->mbmi.mv.as_int; 4578 mv_bias(left, refframe, &this_mv, ref_frame_sign_bias); 4580 if(this_mv.as_int != mv->as_int) { 4581 (++mv)->as_int = this_mv.as_int; 4582 ++cntx; 4583 } 4584 *cntx += 2; 4585 } else 4586 cnt[CNT_ZERO] += 2; 4587 } 4589 /* Process above left */ 4590 if(aboveleft->mbmi.ref_frame != INTRA_FRAME) { 4591 if(aboveleft->mbmi.mv.as_int) { 4592 int_mv this_mv; 4594 this_mv.as_int = aboveleft->mbmi.mv.as_int; 4595 mv_bias(aboveleft, refframe, &this_mv, 4596 ref_frame_sign_bias); 4598 if(this_mv.as_int != mv->as_int) { 4599 (++mv)->as_int = this_mv.as_int; 4600 ++cntx; 4601 } 4602 *cntx += 1; 4604 } else 4605 cnt[CNT_ZERO] += 1; 4606 } 4608 /* If we have three distinct MV's ... */ 4609 if(cnt[CNT_SPLITMV]) { 4610 /* See if above-left MV can be merged with NEAREST */ 4611 if(mv->as_int == near_mvs[CNT_NEAREST].as_int) 4612 cnt[CNT_NEAREST] += 1; 4613 } 4615 cnt[CNT_SPLITMV] = ((above->mbmi.mode == SPLITMV) 4616 + (left->mbmi.mode == SPLITMV)) * 2 4617 + (aboveleft->mbmi.mode == SPLITMV); 4619 /* Swap near and nearest if necessary */ 4620 if(cnt[CNT_NEAR] > cnt[CNT_NEAREST]) { 4621 int tmp; 4622 tmp = cnt[CNT_NEAREST]; 4623 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 4624 cnt[CNT_NEAR] = tmp; 4625 tmp = near_mvs[CNT_NEAREST].as_int; 4626 near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int; 4627 near_mvs[CNT_NEAR].as_int = tmp; 4628 } 4630 /* Use near_mvs[0] to store the "best" MV */ 4631 if(cnt[CNT_NEAREST] >= cnt[CNT_ZERO]) 4632 near_mvs[CNT_ZERO] = near_mvs[CNT_NEAREST]; 4634 /* Set up return values */ 4635 *best_mv = near_mvs[0].as_mv; 4636 *nearest = near_mvs[CNT_NEAREST].as_mv; 4637 *near = near_mvs[CNT_NEAR].as_mv; 4639 vp8_clamp_mv(nearest, xd); 4640 vp8_clamp_mv(near, xd); 4641 vp8_clamp_mv(best_mv, xd); //TODO: move this up before 4642 the copy 4643 } 4645 ---- End code block ---------------------------------------- 4647 The mv_ref probability table (mv_ref_p) is then derived from the 4648 census as follows. 4650 ---- Begin code block -------------------------------------- 4652 const int vp8_mode_contexts[6][4] = 4653 { 4654 { 7, 1, 1, 143, }, 4655 { 14, 18, 14, 107, }, 4656 { 135, 64, 57, 68, }, 4657 { 60, 56, 128, 65, }, 4658 { 159, 134, 128, 34, }, 4659 { 234, 188, 128, 28, }, 4660 } 4662 ---- End code block ---------------------------------------- 4664 ---- Begin code block -------------------------------------- 4666 vp8_prob *vp8_mv_ref_probs(vp8_prob mv_ref_p[VP8_MVREFS-1], 4667 int cnt[4]) 4668 { 4669 mv_ref_p[0] = vp8_mode_contexts [cnt[0]] [0]; 4670 mv_ref_p[1] = vp8_mode_contexts [cnt[1]] [1]; 4671 mv_ref_p[2] = vp8_mode_contexts [cnt[2]] [2]; 4672 mv_ref_p[3] = vp8_mode_contexts [cnt[3]] [3]; 4673 return p; 4674 } 4676 ---- End code block ---------------------------------------- 4678 Once mv_ref_p is established, the mv_ref is decoded as usual. 4680 ---- Begin code block -------------------------------------- 4682 mvr = (mv_ref) treed_read( d, mv_ref_tree, mv_ref_p); 4684 ---- End code block ---------------------------------------- 4686 For the first four inter-coding modes, the same motion vector is used 4687 for all the Y subblocks. The first three modes use an implicit 4688 motion vector. 4690 +------------+------------------------------------------------------+ 4691 | Mode | Instruction | 4692 +------------+------------------------------------------------------+ 4693 | mv_nearest | Use the nearest vector returned by | 4694 | | vp8_find_near_mvs. | 4695 | | | 4696 | mv_near | Use the near vector returned by vp8_find_near_mvs. | 4697 | | | 4698 | mv_zero | Use a zero vector, that is, predict the current | 4699 | | macroblock from the corresponding macroblock in the | 4700 | | prediction frame. | 4701 | | | 4702 | NEWMV | This mode is followed by an explicitly-coded motion | 4703 | | vector (the format of which is described in the next | 4704 | | chapter) that is added (component-wise) to the | 4705 | | best_mv reference vector returned by find_near_mvs | 4706 | | and applied to all 16 subblocks. | 4707 +------------+------------------------------------------------------+ 4709 16.4. Split Prediction 4711 The remaining mode (SPLITMV) causes multiple vectors to be applied to 4712 the Y subblocks. It is immediately followed by a partition 4713 specification that determines how many vectors will be specified and 4714 how they will be assigned to the subblocks. The possible partitions, 4715 with indicated subdivisions and coding tree, are as follows. 4717 ---- Begin code block -------------------------------------- 4719 typedef enum 4720 { 4721 mv_top_bottom, /* two pieces {0...7} and {8...15} */ 4722 mv_left_right, /* {0,1,4,5,8,9,12,13} and 4723 {2,3,6,7,10,11,14,15} */ 4724 mv_quarters, /* {0,1,4,5}, {2,3,6,7}, {8,9,12,13}, 4725 {10,11,14,15} */ 4726 MV_16, /* every subblock gets its own vector 4727 {0} ... {15} */ 4729 mv_num_partitions 4730 } 4731 MVpartition; 4733 const tree_index mvpartition_tree [2 * (mvnum_partition - 1)] = 4734 { 4735 -MV_16, 2, /* MV_16 = "0" */ 4736 -mv_quarters, 4, /* mv_quarters = "10" */ 4737 -mv_top_bottom, -mv_left_right /* top_bottom = "110", 4738 left_right = "111" */ 4739 }; 4741 ---- End code block ---------------------------------------- 4743 The partition is decoded using a fixed, constant probability table: 4745 ---- Begin code block -------------------------------------- 4747 const Prob mvpartition_probs [mvnum_partition - 1] = 4748 { 110, 111, 150}; 4749 part = (MVpartition) treed_read( d, mvpartition_tree, 4750 mvpartition_probs); 4752 ---- End code block ---------------------------------------- 4754 After the partition come two (for mv_top_bottom or mv_left_right), 4755 four (for mv_quarters), or sixteen (for MV_16) subblock inter- 4756 prediction modes. These modes occur in the order indicated by the 4757 partition layouts (given as comments to the MVpartition enum) and are 4758 coded as follows. (As was done for the macroblock-level modes, we 4759 offset the mode enumeration so that a single variable may 4760 unambiguously hold either an intra- or inter-subblock mode.) 4762 Prior to decoding each subblock, a decoding tree context is chosen as 4763 illustrated in the code snippet below. The context is based on the 4764 immediate left and above subblock neighbors, and whether they are 4765 equal, are zero, or a combination of those. 4767 ---- Begin code block -------------------------------------- 4769 typedef enum 4770 { 4771 LEFT4x4 = num_intra_bmodes, /* use already-coded MV to 4772 my left */ 4773 ABOVE4x4, /* use already-coded MV above me */ 4774 ZERO4x4, /* use zero MV */ 4775 NEW4x4, /* explicit offset from "best" */ 4777 num_sub_mv_ref 4778 }; 4779 sub_mv_ref; 4781 const tree_index sub_mv_ref_tree [2 * (num_sub_mv_ref - 1)] = 4782 { 4783 -LEFT4X4, 2, /* LEFT = "0" */ 4784 -ABOVE4X4, 4, /* ABOVE = "10" */ 4785 -ZERO4X4, -NEW4X4 /* ZERO = "110", NEW = "111" */ 4786 }; 4788 /* Choose correct decoding tree context 4789 * Function parameters are left subblock neighbor MV and above 4790 * subblock neighbor MV */ 4791 int vp8_mvCont(MV *l, MV*a) 4792 { 4793 int lez = (l->row == 0 && l->col == 0); /* left neighbour 4794 is zero */ 4795 int aez = (a->row == 0 && a->col == 0); /* above neighbour 4796 is zero */ 4797 int lea = (l->row == a->row && l->col == a->col); /* left 4798 neighbour equals above neighbour */ 4800 if(lea && lez) 4801 return SUBMVREF_LEFT_ABOVE_ZED; /* =4 */ 4803 if(lea) 4804 return SUBMVREF_LEFT_ABOVE_SAME; /* =3 */ 4806 if(aez) 4807 return SUBMVREF_ABOVE_ZED; /* =2 */ 4809 if(lez) 4810 return SUBMVREF_LEFT_ZED; /* =1*/ 4812 return SUBMVREF_NORMAL; /* =0 */ 4814 } 4816 /* Constant probabilities and decoding procedure. */ 4818 const Prob sub_mv_ref_prob [5][num_sub_mv_ref - 1] = { 4819 { 147,136,18 }, 4820 { 106,145,1 }, 4821 { 179,121,1 }, 4822 { 223,1 ,34 }, 4823 { 208,1 ,1 } }; 4825 sub_ref = (sub_mv_ref) treed_read( d, sub_mv_ref_tree, 4826 sub_mv_ref_prob[context]); 4828 ---- End code block ---------------------------------------- 4830 The first two sub-prediction modes simply copy the already-coded 4831 motion vectors used by the blocks above and to-the-left of the 4832 subblock at the upper left corner of the current subset (i.e., 4833 collection of subblocks being predicted). These prediction blocks 4834 need not lie in the current macroblock and, if the current subset 4835 lies at the top or left edges of the frame, need not lie in the 4836 frame. In this latter case, their motion vectors are taken to be 4837 zero, as are subblock motion vectors within an intra-predicted 4838 macroblock. Also, to ensure the correctness of prediction within 4839 this macroblock, all subblocks lying in an already-decoded subset of 4840 the current macroblock must have their motion vectors set. 4842 ZERO4x4 uses a zero motion vector and predicts the current subset 4843 using the corresponding subset from the prediction frame. 4845 NEW4x4 is exactly like NEWMV except applied only to the current 4846 subset. It is followed by a 2-dimensional motion vector offset 4847 (described in the next chapter) that is added to the best vector 4848 returned by the earlier call to find_near_mvs to form the motion 4849 vector in effect for the subset. 4851 Parsing of both inter-prediction modes and motion vectors (described 4852 next) can be found in the reference decoder file modemv.c. 4854 17. Motion Vector Decoding 4856 As discussed above, motion vectors appear in two places in the VP8 4857 datastream: applied to whole macroblocks in NEWMV mode and applied to 4858 subsets of macroblocks in NEW4x4 mode. The format of the vectors is 4859 identical in both cases. 4861 Each vector has two pieces: A vertical component (row) followed by a 4862 horizontal component (column). The row and column use separate 4863 coding probabilities but are otherwise represented identically. 4865 17.1. Coding of Each Component 4867 Each component is a signed integer V representing a vertical or 4868 horizontal luma displacement of V quarter-pixels (and a chroma 4869 displacement of V eighth-pixels). The absolute value of V, if non- 4870 zero, is followed by a boolean sign. V may take any value between 4871 -1023 and +1023, inclusive. 4873 The absolute value A is coded in one of two different ways according 4874 to its size. For 0 <= A <= 7, A is tree-coded, and for 8 <= A <= 4875 1023, the bits in the binary expansion of A are coded using 4876 independent boolean probabilities. The coding of A begins with a 4877 bool specifying which range is in effect. 4879 Decoding a motion vector component then requires a 19-position 4880 probability table, whose offsets, along with the procedure used to 4881 decode components, are as follows: 4883 ---- Begin code block -------------------------------------- 4885 typedef enum 4886 { 4887 mvpis_short, /* short (<= 7) vs long (>= 8) */ 4888 MVPsign, /* sign for non-zero */ 4889 MVPshort, /* 8 short values = 7-position tree */ 4891 MVPbits = MVPshort + 7, /* 8 long value bits 4892 w/independent probs */ 4894 MVPcount = MVPbits + 10 /* 19 probabilities in total */ 4895 } 4896 MVPindices; 4898 typedef Prob MV_CONTEXT [MVPcount]; /* Decoding spec for 4899 a single component */ 4901 /* Tree used for small absolute values (has expected 4902 correspondence). */ 4904 const tree_index small_mvtree [2 * (8 - 1)] = 4905 { 4906 2, 8, /* "0" subtree, "1" subtree */ 4907 4, 6, /* "00" subtree", "01" subtree */ 4908 -0, -1, /* 0 = "000", 1 = "001" */ 4909 -2, -3, /* 2 = "010", 3 = "011" */ 4910 10, 12, /* "10" subtree, "11" subtree */ 4911 -4, -5, /* 4 = "100", 5 = "101" */ 4912 -6, -7 /* 6 = "110", 7 = "111" */ 4913 }; 4915 /* Read MV component at current decoder position, using 4916 supplied probs. */ 4918 int read_mvcomponent( bool_decoder *d, const MV_CONTEXT *mvc) 4919 { 4920 const Prob * const p = (const Prob *) mvc; 4921 int A = 0; 4923 if( read_bool( d, p [mvpis_short])) /* 8 <= A <= 1023 */ 4924 { 4925 /* Read bits 0, 1, 2 */ 4927 int i = 0; 4928 do { A += read_bool( d, p [MVPbits + i]) << i;} 4929 while( ++i < 3); 4931 /* Read bits 9, 8, 7, 6, 5, 4 */ 4933 i = 9; 4934 do { A += read_bool( d, p [MVPbits + i]) << i;} 4935 while( --i > 3); 4937 /* We know that A >= 8 because it is coded long, 4938 so if A <= 15, bit 3 is one and is not 4939 explicitly coded. */ 4941 if( !(A & 0xfff0) || read_bool( d, p [MVPbits + 3])) 4942 A += 8; 4943 } 4944 else /* 0 <= A <= 7 */ 4945 A = treed_read( d, small_mvtree, p + MVPshort); 4947 return A && read_bool( r, p [MVPsign]) ? -A : A; 4948 } 4949 ---- End code block ---------------------------------------- 4951 17.2. Probability Updates 4953 The decoder should maintain an array of two MV_CONTEXTs for decoding 4954 row and column components, respectively. These MV_CONTEXTs should be 4955 set to their defaults every key frame. Each individual probability 4956 may be updated every interframe (by field J of the frame header) 4957 using a constant table of update probabilities. Each optional update 4958 is of the form B? P(7), that is, a bool followed by a 7-bit 4959 probability specification if true. 4961 As with other dynamic probabilities used by VP8, the updates remain 4962 in effect until the next key frame or until replaced via another 4963 update. 4965 In detail, the probabilities should then be managed as follows. 4967 ---- Begin code block -------------------------------------- 4969 /* Never-changing table of update probabilities for each 4970 individual probability used in decoding motion vectors. */ 4972 const MV_CONTEXT vp8_mv_update_probs[2] = 4973 { 4974 { 4975 237, 4976 246, 4977 253, 253, 254, 254, 254, 254, 254, 4978 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 4979 }, 4980 { 4981 231, 4982 243, 4983 245, 253, 254, 254, 254, 254, 254, 4984 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 4985 } 4986 }; 4988 /* Default MV decoding probabilities. */ 4990 const MV_CONTEXT default_mv_context[2] = 4991 { 4992 { // row 4993 162, // is short 4994 128, // sign 4995 225, 146, 172, 147, 214, 39, 156, // short tree 4996 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 // long bits 4998 }, 5000 { // same for column 5001 164, // is short 5002 128, 5003 204, 170, 119, 235, 140, 230, 228, 5004 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 // long bits 5006 } 5007 }; 5009 /* Current MV decoding probabilities, set to above defaults 5010 every key frame. */ 5012 MV_CONTEXT mvc [2]; /* always row, then column */ 5014 /* Procedure for decoding a complete motion vector. */ 5016 typedef struct { int16 row, col;} MV; /* as in previous chapter */ 5018 MV read_mv( bool_decoder *d) 5019 { 5020 MV v; 5021 v.row = (int16) read_mvcomponent( d, mvc); 5022 v.col = (int16) read_mvcomponent( d, mvc + 1); 5023 return v; 5024 } 5026 /* Procedure for updating MV decoding probabilities, called 5027 every interframe with "d" at the appropriate position in 5028 the frame header. */ 5030 void update_mvcontexts( bool_decoder *d) 5031 { 5032 int i = 0; 5033 do { /* component = row, then column */ 5034 const Prob *up = mv_update_probs[i]; /* update probs 5035 for component */ 5036 Prob *p = mvc[i]; /* start decode tbl "" */ 5037 Prob * const pstop = p + MVPcount; /* end decode tbl "" */ 5038 do { 5039 if( read_bool( d, *up++)) /* update this position */ 5040 { 5041 const Prob x = read_literal( d, 7); 5043 *p = x? x<<1 : 1; 5044 } 5045 } while( ++p < pstop); /* next position */ 5047 } while( ++i < 2); /* next component */ 5048 } 5050 ---- End code block ---------------------------------------- 5052 This completes the description of the motion-vector decoding 5053 procedure and, with it, the procedure for decoding interframe 5054 macroblock prediction records. 5056 18. Interframe Prediction 5058 Given an inter-prediction specification for the current macroblock, 5059 that is, a reference frame together with a motion vector for each of 5060 the sixteen Y subblocks, we describe the calculation of the 5061 prediction buffer for the macroblock. Frame reconstruction is then 5062 completed via the previously-described processes of residue summation 5063 (Section 14) and loop filtering (Section 15). 5065 The management of inter-predicted subblocks and sub-pixel 5066 interpolation may be found in the reference decoder file predict.c. 5068 18.1. Bounds on and Adjustment of Motion Vectors 5070 Since each motion vector is differentially encoded from a neighboring 5071 block or macroblock and the only clamp is to ensure that the 5072 referenced motion vector represents a valid location inside a 5073 reference frame buffer, it is technically possible within the VP8 5074 format for a block or macroblock to have arbitrarily large motion 5075 vectors, up to the size of the input image plus the extended border 5076 areas. For practical reasons, VP8 imposes a motion vector size range 5077 limit of -4096 to 4095 full pixels, regardless of image size (VP8 5078 defines 14 raw bits for width and height; 16383x16383 is the maximum 5079 possible image size). Bitstream-compliant encoders and decoders 5080 shall enforce this limit. 5082 Because the motion vectors applied to the chroma subblocks have 1/8 5083 pixel resolution, the synthetic pixel calculation, outlined in 5084 Chapter 5 and detailed below, uses this resolution for the luma 5085 subblocks as well. In accordance, the stored luma motion vectors are 5086 all doubled, each component of each luma vector becoming an even 5087 integer in the range -2046 to +2046, inclusive. 5089 The vector applied to each chroma subblock is calculated by averaging 5090 the vectors for the 4 luma subblocks occupying the same visible area 5091 as the chroma subblock in the usual correspondence, that is, the 5092 vector for U and V block 0 is the average of the vectors for the Y 5093 subblocks { 0, 1, 4, 5}, chroma block 1 corresponds to Y blocks { 2, 5094 3, 6, 7}, chroma block 2 to Y blocks { 8, 9, 12, 13}, and chroma 5095 block 3 to Y blocks { 10, 11, 14, 15}. 5097 In detail, each of the two components of the vectors for each of the 5098 chroma subblocks is calculated from the corresponding luma vector 5099 components as follows: 5101 ---- Begin code block -------------------------------------- 5103 int avg( int c1, int c2, int c3, int c4) 5104 { 5105 int s = c1 + c2 + c3 + c4; 5107 /* The shift divides by 8 (not 4) because chroma pixels 5108 have twice the diameter of luma pixels. The handling 5109 of negative motion vector components is slightly 5110 cumbersome because, strictly speaking, right shifts 5111 of negative numbers are not well-defined in C. */ 5113 return s >= 0 ? (s + 4) >> 3 : -( (-s + 4) >> 3); 5114 } 5116 ---- End code block ---------------------------------------- 5118 Furthermore, if the version number in the frame tag specifies only 5119 full-pel chroma motion vectors, then the fractional parts of both 5120 components of the vector are truncated to zero, as illustrated in the 5121 following pseudo-code (assuming 3 bits of fraction for both luma and 5122 chroma vectors): 5124 ---- Begin code block -------------------------------------- 5126 x = x & (~7); 5127 y = y & (~7); 5129 ---- End code block ---------------------------------------- 5131 Earlier in this document we described the vp8_clamp_mv() function to 5132 limit "nearest" and "near" motion vector predictors inside specified 5133 margins within the frame boundaries. Additional clamping is 5134 performed for NEW_MV macroblocks, for which the final motion vector 5135 is clamped again after combining the "best" predictor and the 5136 differential vector decoded from the stream. 5138 However, the secondary clamping is not performed for SPLIT_MV 5139 macroblocks, meaning that any subblock's motion vector within the 5140 SPLIT_MV macroblock may point outside the clamping zone. These non- 5141 clamped vectors are also used when determining the decoding tree 5142 context for subsequent subblocks' modes in the vp8_mvCont() function. 5144 18.2. Prediction Subblocks 5146 The prediction calculation for each subblock is then as follows. 5147 Temporarily disregarding the fractional part of the motion vector 5148 (that is, rounding "up" or "left" by right-shifting each component 3 5149 bits with sign propagation) and adding the origin (upper left 5150 position) of the (16x16 luma or 8x8 chroma) current macroblock gives 5151 us an origin in the Y, U, or V plane of the predictor frame (either 5152 the golden frame or previous frame). 5154 Considering that origin to be the upper left corner of a (luma or 5155 chroma) macroblock, we need to specify the relative positions of the 5156 pixels associated to that subblock, that is, any pixels that might be 5157 involved in the sub-pixel interpolation processes for the subblock. 5159 18.3. Sub-pixel Interpolation 5161 The sub-pixel interpolation is effected via two one-dimensional 5162 convolutions. These convolutions may be thought of as operating on a 5163 two-dimensional array of pixels whose origin is the subblock origin, 5164 that is the origin of the prediction macroblock described above plus 5165 the offset to the subblock. Because motion vectors are arbitrary, so 5166 are these "prediction subblock origins". 5168 The integer part of the motion vector is subsumed in the origin of 5169 the prediction subblock, the 16 (synthetic) pixels we need to 5170 construct are given by 16 offsets from the origin. The integer part 5171 of each of these offsets is the offset of the corresponding pixel 5172 from the subblock origin (using the vertical stride). To these 5173 integer parts is added a constant fractional part, which is simply 5174 the difference between the actual motion vector and its integer 5175 truncation used to calculate the origins of the prediction macroblock 5176 and subblock. Each component of this fractional part is an integer 5177 between 0 and 7, representing a forward displacement in eighths of a 5178 pixel. 5180 It is these fractional displacements that determine the filtering 5181 process. If they both happen to be zero (that is, we had a "whole 5182 pixel" motion vector), the prediction subblock is simply copied into 5183 the corresponding piece of the current macroblock's prediction 5184 buffer. As discussed in Chapter 14, the layout of the macroblock's 5185 prediction buffer can depend on the specifics of the reconstruction 5186 implementation chosen. Of course, the vertical displacement between 5187 lines of the prediction subblock is given by the stride, as are all 5188 vertical displacements used here. 5190 Otherwise, at least one of the fractional displacements is non-zero. 5191 We then synthesize the missing pixels via a horizontal, followed by a 5192 vertical, one-dimensional interpolation. 5194 The two interpolations are essentially identical. Each uses an (at 5195 most) six-tap filter (the choice of which of course depends on the 5196 one-dimensional offset). Thus, every calculated pixel references at 5197 most three pixels before (above or to-the-left of) it and at most 5198 three pixels after (below or to-the-right of) it. The horizontal 5199 interpolation must calculate two extra rows above and three extra 5200 rows below the 4x4 block, to provide enough samples for the vertical 5201 interpolation to proceed. 5203 Depending on the reconstruction filter type given in the field 5204 Version Number in the frame tag, either a bicubic or a bilinear tap 5205 set is used. 5207 The exact implementation of subsampling is as follows. 5209 ---- Begin code block -------------------------------------- 5211 /* Filter taps taken to 7-bit precision. 5212 Because DC is always passed, taps always sum to 128. */ 5214 const int BilinearFilters[8][6] = 5215 { 5216 { 0, 0, 128, 0, 0, 0 }, 5217 { 0, 0, 112, 16, 0, 0 }, 5218 { 0, 0, 96, 32, 0, 0 }, 5219 { 0, 0, 80, 48, 0, 0 }, 5220 { 0, 0, 64, 64, 0, 0 }, 5221 { 0, 0, 48, 80, 0, 0 }, 5222 { 0, 0, 32, 96, 0, 0 }, 5223 { 0, 0, 16, 112, 0, 0 } 5224 }; 5226 const int filters [8] [6] = { /* indexed by displacement */ 5227 { 0, 0, 128, 0, 0, 0 }, /* degenerate whole-pixel */ 5228 { 0, -6, 123, 12, -1, 0 }, /* 1/8 */ 5229 { 2, -11, 108, 36, -8, 1 }, /* 1/4 */ 5230 { 0, -9, 93, 50, -6, 0 }, /* 3/8 */ 5231 { 3, -16, 77, 77, -16, 3 }, /* 1/2 is symmetric */ 5232 { 0, -6, 50, 93, -9, 0 }, /* 5/8 = reverse of 3/8 */ 5233 { 1, -8, 36, 108, -11, 2 }, /* 3/4 = reverse of 1/4 */ 5234 { 0, -1, 12, 123, -6, 0 } /* 7/8 = reverse of 1/8 */ 5235 }; 5237 /* One-dimensional synthesis of a single sample. 5238 Filter is determined by fractional displacement */ 5240 Pixel interp( 5241 const int fil[6], /* filter to apply */ 5242 const Pixel *p, /* origin (rounded "before") in 5243 prediction area */ 5245 const int s /* size of one forward step "" */ 5246 ) { 5247 int32 a = 0; 5248 int i = 0; 5249 p -= s + s; /* move back two positions */ 5251 do { 5252 a += *p * fil[i]; 5253 p += s; 5254 } while( ++i < 6); 5256 return clamp255( (a + 64) >> 7); /* round to nearest 5257 8-bit value */ 5258 } 5260 /* First do horizontal interpolation, producing intermediate 5261 buffer. */ 5263 void Hinterp( 5264 Pixel temp[9][4], /* 9 rows of 4 (intermediate) 5265 destination values */ 5266 const Pixel *p, /* subblock origin in prediction 5267 frame */ 5268 int s, /* vertical stride to be used in 5269 prediction frame */ 5270 uint hfrac, /* 0 <= horizontal displacement <= 7 */ 5271 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5272 ) { 5273 const int * const fil = bicubic ? filters [hfrac] : 5274 BilinearFilters[hfrac]; 5276 int r = 0; do /* for each row */ 5277 { 5278 int c = 0; do /* for each destination sample */ 5279 { 5280 /* Pixel separation = one horizontal step = 1 */ 5282 temp[r][c] = interp( fil, p + c, 1); 5283 } 5284 while( ++c < 4); 5285 } 5286 while( p += s, ++r < 9); /* advance p to next row */ 5287 } 5289 /* Finish with vertical interpolation, producing final results. 5290 Input array "temp" is of course that computed above. */ 5292 void Vinterp( 5293 Pixel final[4][4], /* 4 rows of 4 (final) destination values */ 5294 const Pixel temp[9][4], 5295 uint vfrac, /* 0 <= vertical displacement <= 7 */ 5296 uint bicubic /* 1=bicubic filter, 0=bilinear */ 5297 ) { 5298 const int * const fil = bicubic ? filters [vfrac] : 5299 BilinearFilters[vfrac]; 5301 int r = 0; do /* for each row */ 5302 { 5303 int c = 0; do /* for each destination sample */ 5304 { 5305 /* Pixel separation = one vertical step = width 5306 of array = 4 */ 5308 final[r][c] = interp( fil, temp[r] + c, 4); 5309 } 5310 while( ++c < 4); 5311 } 5312 while( ++r < 4); 5313 } 5315 ---- End code block ---------------------------------------- 5317 18.4. Filter Properties 5319 We discuss briefly the rationale behind the choice of filters. Our 5320 approach is necessarily cursory; a genuinely accurate discussion 5321 would require a couple of books. Readers unfamiliar with signal 5322 processing may or may not wish to skip this. 5324 All digital signals are of course sampled in some fashion. The case 5325 where the inter-sample spacing (say in time for audio samples, or 5326 space for pixels) is uniform, that is, the same at all positions, is 5327 particularly common and amenable to analysis. Many aspects of the 5328 treatment of such signals are best-understood in the frequency domain 5329 via Fourier Analysis, particularly those aspects of the signal that 5330 are not changed by shifts in position, especially when those 5331 positional shifts are not given by a whole number of samples. 5333 Non-integral translates of a sampled signal are a textbook example of 5334 the foregoing. In our case of non-integral motion vectors, we wish 5335 to say what the underlying image "really is" at these pixels we don't 5336 have values for but feel that it makes sense to talk about. The 5337 correctness of this feeling is predicated on the underlying signal 5338 being band-limited, that is, not containing any energy in spatial 5339 frequencies that cannot be faithfully rendered at the pixel 5340 resolution at our disposal. In one dimension, this range of "OK" 5341 frequencies is called the Nyquist band; in our two-dimensional case 5342 of integer-grid samples, this range might be termed a Nyquist 5343 rectangle. The finer the grid, the more we know about the image, and 5344 the wider the Nyquist rectangle. 5346 It turns out that, for such band-limited signals, there is indeed an 5347 exact mathematical formula to produce the correct sample value at an 5348 arbitrary point. Unfortunately, this calculation requires the 5349 consideration of every single sample in the image, as well as needing 5350 to operate at infinite precision. Also, strictly speaking, all band- 5351 limited signals have infinite spatial (or temporal) extent, so 5352 everything we are discussing is really some sort of approximation. 5354 It is true that the theoretically correct subsampling procedure, as 5355 well as any approximation thereof, is always given by a translation- 5356 invariant weighted sum (or filter) similar to that used by VP8. It 5357 is also true that the reconstruction error made by such a filter can 5358 be simply represented as a multiplier in the frequency domain, that 5359 is, such filters simply multiply the Fourier transform of any signal 5360 to which they are applied by a fixed function associated to the 5361 filter. This fixed function is usually called the frequency response 5362 (or transfer function); the ideal subsampling filter has a frequency 5363 response equal to one in the Nyquist rectangle and zero everywhere 5364 else. 5366 Another basic fact about approximations to "truly correct" 5367 subsampling is that, the wider the subrectangle (within the Nyquist 5368 rectangle) of spatial frequencies one wishes to "pass" (that is, 5369 correctly render) or, put more accurately, the closer one wishes to 5370 approximate the ideal transfer function, the more samples of the 5371 original signal must be considered by the subsampling, and the wider 5372 the calculation precision necessitated. 5374 The filters chosen by VP8 were chosen, within the constraints of 4 or 5375 6 taps and 7-bit precision, to do the best possible job of handling 5376 the low spatial frequencies near the zeroth DC frequency along with 5377 introducing no resonances (places where the absolute value of the 5378 frequency response exceeds one). 5380 The justification for the foregoing has two parts. First, resonances 5381 can produce extremely objectionable visible artifacts when, as often 5382 happens in actual compressed video streams, filters are applied 5383 repeatedly. Second, the vast majority of energy in real-world images 5384 lies near DC and not at the high-end. 5386 To get slightly more specific, the filters chosen by VP8 are the best 5387 resonance-free 4- or 6-tap filters possible, where "best" describes 5388 the frequency response near the origin: the response at 0 is required 5389 to be 1 and the graph of the response at 0 is as flat as possible. 5391 To provide an intuitively more obvious point of reference, the "best" 5392 2-tap filter is given by simple linear interpolation between the 5393 surrounding actual pixels. 5395 Finally, it should be noted that, because of the way motion vectors 5396 are calculated, the (shorter) 4-tap filters (used for odd fractional 5397 displacements) are applied in the chroma plane only. Human color 5398 perception is notoriously poor, especially where higher spatial 5399 frequencies are involved. The shorter filters are easier to 5400 understand mathematically, and the difference between them and a 5401 theoretically slightly better 6-tap filter is negligible where chroma 5402 is concerned. 5404 19. Annex A: Bitstream Syntax 5406 This annex presents the bitstream syntax in a tabular form. All the 5407 information elements have been introduced and explained in the 5408 previous chapters but are collected here for a quick reference. Each 5409 syntax element is shortly described after the tabular representation 5410 along with a reference to the corresponding paragraph in the main 5411 document. The meaning of each syntax element value is not repeated 5412 here. 5414 The top-level hierarchy of the bitstream is introduced in Section 4. 5416 Definition of syntax element coding types can be found in Section 8. 5417 The types used in the representation in this annex are: 5419 o f(n), n-bit value from stream (n successive bits, not boolean 5420 encoded) 5422 o L(n), n-bit number encoded as n booleans (with equal probability 5423 of being 0 or 1) 5425 o B(p), bool with probability p of being 0 5427 o T, tree-encoded value 5429 19.1. Uncompressed Data Chunk 5431 +----------------------+-------+ 5432 | Frame Tag | Type | 5433 +----------------------+-------+ 5434 | frame_tag | f(24) | 5435 | | | 5436 | if (key_frame) { | | 5437 | | | 5438 | start_code | f(24) | 5439 | | | 5440 | horizontal_size_code | f(16) | 5441 | | | 5442 | vertical_size_code | f(16) | 5443 | | | 5444 | } | | 5445 +----------------------+-------+ 5447 The 3-byte frame tag can be parsed as follows: 5449 ---- Begin code block -------------------------------------- 5451 unsigned char *c = pbi->Source; 5452 unsigned int tmp; 5454 tmp = (c[2] << 16) | (c[1] << 8) | c[0]; 5456 key_frame = tmp & 0x1; 5457 version = (tmp >> 1) & 0x7; 5458 show_frame = (tmp >> 4) & 0x1; 5459 first_part_size = (tmp >> 5) & 0x7FFFF; 5461 ---- End code block ---------------------------------------- 5463 Where: 5465 o key_frame indicates if the current frame is a key frame or not. 5467 o version determines the bitstream version. 5469 o show_frame indicates if the current frame is meant to be displayed 5470 or not. 5472 o first_part_size determines the size of the first partition 5473 (control partition), excluding the uncompressed data chunk. 5475 The start_code is a constant 3-byte pattern having value 0x9d012a. 5476 The latter part of the uncompressed chunk (after the start_code) can 5477 be parsed as follows: 5479 ---- Begin code block -------------------------------------- 5481 unsigned char *c = pbi->Source + 6; 5482 unsigned int tmp; 5484 tmp = (c[1] << 8) | c[0]; 5486 width = tmp & 0x3FFF; 5487 horizontal_scale = tmp >> 14; 5489 tmp = (c[3] << 8) | c[2]; 5491 height = tmp & 0x3FFF; 5492 vertical_scale = tmp >> 14; 5494 ---- End code block ---------------------------------------- 5496 19.2. Frame Header 5498 +-------------------------------+------+ 5499 | Frame Header | Type | 5500 +-------------------------------+------+ 5501 | if (key_frame) { | | 5502 | | | 5503 | color_space | L(1) | 5504 | | | 5505 | clamping_type | L(1) | 5506 | | | 5507 | } | | 5508 | | | 5509 | segmentation_enabled | L(1) | 5510 | | | 5511 | if (segmentation_enabled) { | | 5512 | | | 5513 | update_segmentation() | | 5514 | | | 5515 | } | | 5516 | | | 5517 | filter_type | L(1) | 5518 | | | 5519 | loop_filter_level | L(6) | 5520 | | | 5521 | sharpness_level | L(3) | 5522 | | | 5523 | mb_lf_adjustments() | | 5524 | | | 5525 | log2_nbr_of_dct_partitions | L(2) | 5526 | | | 5527 | quant_indices() | | 5528 | | | 5529 | if (key_frame) { | | 5530 | | | 5531 | refresh_entropy_probs | L(1) | 5532 | | | 5533 | } else { | | 5534 | | | 5535 | refresh_golden_frame | L(1) | 5536 | | | 5537 | refresh_alternate_frame | L(1) | 5538 | | | 5539 | if (!refresh_golden_frame) | | 5540 | | | 5541 | copy_buffer_to_golden | L(2) | 5542 | | | 5543 | if (!refresh_alternate_frame) | | 5544 | copy_buffer_to_alternate | L(2) | 5545 | | | 5546 | sign_bias_golden | L(1) | 5547 | | | 5548 | sign_bias_alternate | L(1) | 5549 | | | 5550 | refresh_entropy_probs | L(1) | 5551 | | | 5552 | refresh_last | L(1) | 5553 | | | 5554 | } | | 5555 | | | 5556 | token_prob_update() | | 5557 | | | 5558 | mb_no_coeff_skip | L(1) | 5559 +-------------------------------+------+ 5561 +--------------------------------------+------+ 5562 | Frame Header | Type | 5563 +--------------------------------------+------+ 5564 | prob_skip_false | L(8) | 5565 | | | 5566 | if (!key_frame) { | | 5567 | | | 5568 | prob_intra | L(8) | 5569 | | | 5570 | prob_last | L(8) | 5571 | | | 5572 | prob_golden | L(8) | 5573 | | | 5574 | intra_16x16_prob_update_flag | L(1) | 5575 | | | 5576 | if (intra_16x16_prob_update_flag) { | | 5577 | | | 5578 | for (i = 0; i < 4; i++) | | 5579 | | | 5580 | intra_16x16_prob | L(8) | 5581 | | | 5582 | } | | 5583 | | | 5584 | intra_chroma prob_update_flag | L(1) | 5585 | | | 5586 | if (intra_chroma_prob_update_flag) { | | 5587 | | | 5588 | for (i = 0; i < 3; i++) | | 5589 | | | 5590 | intra_chroma_prob | L(8) | 5591 | | | 5592 | } | | 5593 | | | 5594 | mv_prob_update() | | 5595 | | | 5596 | } | | 5597 +--------------------------------------+------+ 5599 o color_space defines the YUV color space of the sequence 5600 (Section 9.2) 5602 o clamping_type specifies if the decoder is required to clamp the 5603 reconstructed pixel values (Section 9.2) 5605 o segmentation_enabled enables the segmentation feature for the 5606 current frame (Section 9.3) 5608 o filter_type determines whether the normal or the simple loop 5609 filter is used (Section 9.4, Section 15) 5611 o loop_filter_level controls the deblocking filter (Section 9.4, 5612 Section 15) 5614 o sharpness_level controls the deblocking filter (Section 9.4, 5615 Section 15) 5617 o log2_nbr_of_dct_partitions determines the number of separate 5618 partitions containing the DCT coefficients of the macroblocks 5619 (Section 9.5) 5621 o refresh_entropy_probs determines whether updated token 5622 probabilities are used only for this frame or until further update 5624 o refresh_golden_frame determines if the current decoded frame 5625 refreshes the golden frame (Section 9.7) 5627 o refresh_alternate_frame determines if the current decoded frame 5628 refreshes the alternate reference frame (Section 9.7) 5630 o copy_buffer_to_golden determines if the golden reference is 5631 replaced by another reference (Section 9.7) 5633 o copy_buffer_to_alternate determines if the alternate reference is 5634 replaced by another reference (Section 9.7) 5636 o sign_bias_golden controls the sign of motion vectors when the 5637 golden frame is referenced (Section 9.7) 5639 o sign_bias_alternate controls the sign of motion vectors when the 5640 alternate frame is referenced (Section 9.7) 5642 o refresh_last determines if the current decoded frame refreshes the 5643 last frame reference buffer (Section 9.8) 5645 o mb_no_coeff_skip enables or disables the skipping of macroblocks 5646 containing no non-zero coefficients (Section 9.10) 5648 o prob_skip_false the probability that the macroblock is not skipped 5649 (flag indicating skipped macroblock is false) (Section 9.10) 5651 o prob_intra the probability of an intra macroblock (Section 9.10) 5653 o prob_last the probability that the last reference frame is used 5654 for inter prediction (Section 9.10) 5656 o prob_golden the probability that the golden reference frame is 5657 used for inter prediction (Section 9.10) 5659 o intra_16x16_prob_update_flag indicates if the branch probabilies 5660 used in the decoding of luma intra prediction mode are updated 5661 (Section 9.10) 5663 o intra_16x16_prob the branch probabilities of the luma intra 5664 prediction mode decoding tree 5666 o intra_chroma_prob_update_flag indicates if the branch probabilies 5667 used in the decoding of chroma intra prediction mode are updated 5668 (Section 9.10) 5670 o intra_chroma_prob the branch probabilities of the chroma intra 5671 prediction mode decoding tree 5673 +------------------------------------+------+ 5674 | update_segmentation() | Type | 5675 +------------------------------------+------+ 5676 | update_mb_segmentation_map | L(1) | 5677 | | | 5678 | update_segment_feature_data | L(1) | 5679 | | | 5680 | if (update_segment_feature_data) { | | 5681 | | | 5682 | segment_feature_mode | L(1) | 5683 | | | 5684 | for (i = 0; i < 4; i++) { | | 5685 | | | 5686 | quantizer_update | L(1) | 5687 | | | 5688 | if (quantizer_update) { | | 5689 | | | 5690 | quantizer_update_value | L(7) | 5691 | | | 5692 | quantizer_update_sign | L(1) | 5693 | | | 5694 | } | | 5695 | | | 5696 | } | | 5697 | | | 5698 | for (i = 0; i < 4; i++) { | | 5699 | | | 5700 | loop_filter_update | L(1) | 5701 | | | 5702 | if (loop_filter_update) { | | 5703 | | | 5704 | lf_update_value | L(6) | 5705 | | | 5706 | lf_update_sign | L(1) | 5707 | | | 5708 | } | | 5709 | | | 5710 | } | | 5711 | | | 5712 | } | | 5713 | | | 5714 | if (update_mb_segmentation_map) { | | 5715 | | | 5716 | for (i = 0; i < 3; i++) { | | 5717 | | | 5718 | segment_prob_update | L(1) | 5719 | | | 5720 | if (segment_prob_update) { | | 5721 | | | 5722 | segment_prob | L(8) | 5723 | | | 5724 | } | | 5725 | | | 5726 | } | | 5727 | | | 5728 | } | | 5729 +------------------------------------+------+ 5731 o update_mb_segmentation_map determines if the MB segmentation map 5732 is updated in the current frame (Section 9.3) 5734 o update_segment_feature_data indicates if the segment feature data 5735 is updated in the current frame (Section 9.3) 5737 o segment_feature_mode indicates the feature data update mode, 0 for 5738 delta and 1 for the absolute value (Section 9.3) 5740 o quantizer_update indicates if the quantizer value is updated for 5741 the i^(th) segment (Section 9.3) 5743 o quantizer_update_value indicates the update value for the segment 5744 quantizer (Section 9.3) 5746 o quantizer_update_sign indicates the update sign for the segment 5747 quantizer (Section 9.3) 5749 o loop_filter_update indicates if the loop filter level value is 5750 updated for the i^(th) segment (Section 9.3) 5752 o lf_update_value indicates the update value for the loop filter 5753 level (Section 9.3) 5755 o lf_update_sign indicates the update sign for the loop filter level 5756 (Section 9.3) 5758 o segment_prob_update indicates if the branch probabilities used to 5759 decode the segment_id in the MB header are decoded from the stream 5760 or use the default value of 255 (Section 9.3) 5762 o segment_prob the branch probabilities of the segment_id decoding 5763 tree (Section 9.3) 5764 +------------------------------------+------+ 5765 | mb_lf_adjustments() | Type | 5766 +------------------------------------+------+ 5767 | loop_filter_adj_enable | L(1) | 5768 | | | 5769 | if (loop_filter_adj_enable) { | | 5770 | | | 5771 | mode_ref_lf_delta_update | L(1) | 5772 | | | 5773 | if (mode_ref_lf_delta_update) { | | 5774 | | | 5775 | for (i = 0; i < 4; i++) { | | 5776 | | | 5777 | ref_frame_delta_update_flag | L(1) | 5778 | | | 5779 | if (ref_frame_delta_update_flag) { | | 5780 | | | 5781 | delta_magnitude | L(6) | 5782 | | | 5783 | delta_sign | L(1) | 5784 | | | 5785 | } | | 5786 | | | 5787 | } | | 5788 | | | 5789 | for (i = 0; i < 4; i++) { | | 5790 | | | 5791 | mb_mode_delta_update_flag | L(1) | 5792 | | | 5793 | if (mb_mode_delta_update_flag) { | | 5794 | | | 5795 | delta_magnitude | L(6) | 5796 | | | 5797 | delta_sign | L(1) | 5798 | | | 5799 | } | | 5800 | | | 5801 | } | | 5802 | | | 5803 | } | | 5804 | | | 5805 | } | | 5806 +------------------------------------+------+ 5808 o loop_filter_adj_enable indicates if the MB-level loop filter 5809 adjustment (based on the used reference frame and coding mode) is 5810 on for the current frame (Section 9.4) 5812 o mode_ref_lf_delta_update indicates if the delta values used in 5813 adjustment are updated in the current frame (Section 9.4) 5815 o ref_frame_delta_update_flag indicates if the adjustment delta 5816 value corresponding to a certain used reference frame is updated 5817 (Section 9.4) 5819 o delta_magnitude is the absolute value of the delta value 5821 o delta_sign is the sign of the delta value 5823 o mb_mode_delta_update_flag indicates if the adjustment delta value 5824 corresponding to certain MB prediction mode is updated 5825 (Section 9.4) 5827 +----------------------------+------+ 5828 | quant_indices() | Type | 5829 +----------------------------+------+ 5830 | y_ac_qi | L(7) | 5831 | | | 5832 | y_dc_delta_present | L(1) | 5833 | | | 5834 | if (y_dc_delta_present) { | | 5835 | | | 5836 | y_dc_delta_magnitude | L(4) | 5837 | | | 5838 | y_dc_delta_sign | L(1) | 5839 | | | 5840 | } | | 5841 | | | 5842 | y2_dc_delta_present | L(1) | 5843 | | | 5844 | if (y2_dc_delta_present) { | | 5845 | | | 5846 | y2_dc_delta_magnitude | L(4) | 5847 | | | 5848 | y2_dc_delta_sign | L(1) | 5849 | | | 5850 | } | | 5851 | | | 5852 | y2_ac_delta_present | L(1) | 5853 | | | 5854 | if (y2_ac_delta_present) { | | 5855 | | | 5856 | y2_ac_delta_magnitude | L(4) | 5857 | | | 5858 | y2_ac_delta_sign | L(1) | 5859 | | | 5860 | } | | 5861 | | | 5862 | uv_dc_delta_present | L(1) | 5863 | | | 5864 | if (uv_dc_delta_present) { | | 5865 | | | 5866 | uv_dc_delta_magnitude | L(4) | 5867 | | | 5868 | uv_dc_delta_sign | L(1) | 5869 | | | 5870 | } | | 5871 | | | 5872 | uv_ac_delta_present | L(1) | 5873 | | | 5874 | if (uv_ac_delta_present) { | | 5875 | | | 5876 | uv_ac_delta_magnitude | L(4) | 5877 | | | 5878 | uv_ac_delta_sign | L(1) | 5879 | | | 5880 | } | | 5881 +----------------------------+------+ 5883 o y_ac_qi is the dequantization table index used for the luma AC 5884 coefficients (and other coefficient groups if no delta value is 5885 present) (Section 9.6) 5887 o y_dc_delta_present indicates if the stream contains a delta value 5888 that is added to the baseline index to obtain the luma DC 5889 coefficient dequantization index (Section 9.6) 5891 o y_dc_delta_magnitude the magnitude of the delta value 5892 (Section 9.6) 5894 o y_dc_delta_sign the sign of the delta value (Section 9.6) 5896 o y2_dc_delta_present indicates if the stream contains a delta value 5897 that is added to the baseline index to obtain the Y2 block DC 5898 coefficient dequantization index (Section 9.6) 5900 o y2_ac_delta_present indicates if the stream contains a delta value 5901 that is added to the baseline index to obtain the Y2 block AC 5902 coefficient dequantization index (Section 9.6) 5904 o uv_dc_delta_present indicates if the stream contains a delta value 5905 that is added to the baseline index to obtain the chroma DC 5906 coefficient dequantization index (Section 9.6) 5908 o uv_ac_delta_present indicates if the stream contains a delta value 5909 that is added to the baseline index to obtain the chroma AC 5910 coefficient dequantization index (Section 9.6) 5912 +-------------------------------+------+ 5913 | token_prob_update() | Type | 5914 +-------------------------------+------+ 5915 | for (i = 0; i < 4; i++) { | | 5916 | | | 5917 | for (j = 0; j < 8; j++) { | | 5918 | | | 5919 | for (k = 0; k < 3; k++) { | | 5920 | | | 5921 | for (l = 0; l < 11; l++) { | | 5922 | | | 5923 | coeff_prob_update_flag | L(1) | 5924 | | | 5925 | if (coeff_prob_update_flag) { | | 5926 | | | 5927 | coeff_prob | L(8) | 5928 | | | 5929 | } | | 5930 | | | 5931 | } | | 5932 | | | 5933 | } | | 5934 | | | 5935 | } | | 5936 | | | 5937 | } | | 5938 +-------------------------------+------+ 5940 o coeff_prob_update_flag indicates if the corresponding branch 5941 probability is updated in the current frame (Section 13.4) 5943 o coeff_prob is the new branch probability (Section 13.4) 5944 +----------------------------+------+ 5945 | mv_prob_update() | Type | 5946 +----------------------------+------+ 5947 | for (i = 0; i < 2; i++) { | | 5948 | | | 5949 | for (j = 0; j < 19; j++) { | | 5950 | | | 5951 | mv_prob_update_flag | L(1) | 5952 | | | 5953 | if (mv_prob_update_flag) { | | 5954 | | | 5955 | prob | L(7) | 5956 | | | 5957 | } | | 5958 | | | 5959 | } | | 5960 | | | 5961 | } | | 5962 +----------------------------+------+ 5964 o mv_prob_update_flag indicates if the corresponding MV decoding 5965 probability is updated in the current frame (Section 17.2) 5967 o prob is the updated probability (Section 17.2) 5969 19.3. Macroblock Data 5971 +---------------------+------+ 5972 | Macroblock Data | Type | 5973 +---------------------+------+ 5974 | macroblock_header() | | 5975 | | | 5976 | residual_data() | | 5977 +---------------------+------+ 5979 +--------------------------------+------+ 5980 | macroblock_header() | Type | 5981 +--------------------------------+------+ 5982 | if (segmentation_map_update) { | | 5983 | | | 5984 | segment_id | T | 5985 | | | 5986 | if (mb_no_coeff_skip) { | | 5987 | | | 5988 | mb_coeff_skip | B(p) | 5989 | | | 5990 | } | | 5991 | | | 5992 | if (!key_frame) { | | 5993 | | | 5994 | is_inter_mb | B(p) | 5995 | | | 5996 | if (is_inter_mb) { | | 5997 | | | 5998 | mb_ref_frame_sel1 | B(p) | 5999 | | | 6000 | if (mb_ref_frame_sel1) | | 6001 | | | 6002 | mb_ref_frame_sel2 | B(p) | 6003 | | | 6004 | mv_mode | T | 6005 | | | 6006 | if (mv_mode == SPLITMV) { | | 6007 | | | 6008 | mv_split_mode | T | 6009 | | | 6010 | for (i = 0; i < numMvs; i++) { | | 6011 | | | 6012 | sub_mv_mode | T | 6013 | | | 6014 | if (sub_mv_mode == NEWMV4x4) { | | 6015 | | | 6016 | read_mvcomponent() | | 6017 | | | 6018 | read_mvcomponent() | | 6019 | | | 6020 | } | | 6021 | | | 6022 | } | | 6023 | | | 6024 | } else if (mv_mode == NEWMV) { | | 6025 | | | 6026 | read_mvcomponent() | | 6027 | | | 6028 | read_mvcomponent() | | 6029 | | | 6030 | } | | 6031 | | | 6032 | } else { /* intra mb */ | | 6033 | | | 6034 | intra_y_mode | T | 6035 +--------------------------------+------+ 6036 +-------------------------------+------+ 6037 | macroblock_header() | Type | 6038 +-------------------------------+------+ 6039 | if (intra_y_mode == B_PRED) { | | 6040 | | | 6041 | for (i = 0; i < 16; i++) | | 6042 | | | 6043 | intra_b_mode | T | 6044 | | | 6045 | } | | 6046 | | | 6047 | intra_uv_mode | T | 6048 | | | 6049 | } | | 6050 +-------------------------------+------+ 6052 o segment_id indicates to which segment the macroblock belongs 6053 (Section 10) 6055 o mb_coeff_skip indicates if the macroblock contains any coded 6056 coefficients or not (Section 11.1) 6058 o is_inter_mb indicates if the macroblock is intra or inter coded 6059 (Section 16) 6061 o mb_ref_frame_sel1 selects the reference frame to be used; last 6062 frame (0), golden/alternate (1) (Section 16.2) 6064 o mb_ref_frame_sel2 selects whether the golden (0) or alternate 6065 reference frame (1) is used (Section 16.2) 6067 o mv_mode determines the macroblock motion vector mode 6068 (Section 16.2) 6070 o mv_split_mode gives macroblock partitioning specification and 6071 determines number of motion vectors used (numMvs)(Section 16.2) 6073 o sub_mv_mode determines the sub-macroblock motion vector mode for 6074 macroblocks coded using SPLITMV motion vector mode (Section 16.2) 6076 o intra_y_mode selects the luminance intra prediction mode 6077 (Section 16.1) 6079 o intra_b_mode selects the sub-macroblock luminance prediction mode 6080 for macroblocks coded using B_PRED mode (Section 16.1) 6082 o intra_uv_mode selects the chrominance intra prediction mode 6083 (Section 16.1) 6084 +----------------------------------------------+------+ 6085 | residual_data() | Type | 6086 +----------------------------------------------+------+ 6087 | if (!mb_coeff_skip) { | | 6088 | | | 6089 | if ( (is_inter_mb && mv_mode != SPLITMV) || | | 6090 | | | 6091 | (!is_inter_mb && intra_y_mode != B_PRED) ) { | | 6092 | | | 6093 | residual_block() /* Y2 */ | | 6094 | | | 6095 | } | | 6096 | | | 6097 | for (i = 0; i < 24; i++) | | 6098 | | | 6099 | residual_block() /* 16 Y, 4 U, 4 V */ | | 6100 | | | 6101 | } | | 6102 +----------------------------------------------+------+ 6104 +-------------------------------------+------+ 6105 | residual_block() | Type | 6106 +-------------------------------------+------+ 6107 | for (i = firstCoeff; i < 16; i++) { | | 6108 | | | 6109 | token | T | 6110 | | | 6111 | if (token == EOB) break; | | 6112 | | | 6113 | if (token_has_extra_bits) { | | 6114 | | | 6115 | extra_bits | L(n) | 6116 | | | 6117 | sign | L(1) | 6118 +-------------------------------------+------+ 6120 o firstCoeff is 1 for luma blocks of macroblocks containing Y2 6121 subblock, otherwise 0 6123 o token defines the value of the coefficient, the value range of the 6124 coefficient or the end of block (Section 13.2) 6126 o extra_bits determine the value of the coefficient within the value 6127 range defined by token (Section 13.2) 6129 o sign indicates the sign of the coefficient (Section 13.2) 6131 20. Attachment One: Reference Decoder Source Code 6133 20.1. bit_ops.h 6135 ---- Begin code block -------------------------------------- 6137 /* 6138 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6139 * 6140 * Use of this source code is governed by a BSD-style license 6141 * that can be found in the LICENSE file in the root of the source 6142 * tree. An additional intellectual property rights grant can be 6143 * found in the file PATENTS. All contributing project authors may 6144 * be found in the AUTHORS file in the root of the source tree. 6145 */ 6146 #ifndef BIT_OPS_H 6147 #define BIT_OPS_H 6149 /* Evaluates to a mask with n bits set */ 6150 #define BITS_MASK(n) ((1<<(n))-1) 6152 /* Returns len bits, with the LSB at position bit */ 6153 #define BITS_GET(val, bit, len) (((val)>>(bit))&BITS_MASK(len)) 6155 #endif 6157 ---- End code block ---------------------------------------- 6159 20.2. bool_decoder.h 6161 ---- Begin code block -------------------------------------- 6163 /* 6164 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6165 * 6166 * Use of this source code is governed by a BSD-style license and 6167 * patent grant that can be found in the LICENSE file in the root of 6168 * the source tree. All contributing project authors may be found in 6169 * the AUTHORS file in the root of the source tree. 6170 */ 6172 #ifndef BOOL_DECODER_H 6173 #define BOOL_DECODER_H 6174 #include 6176 struct bool_decoder 6177 { 6178 const unsigned char *input; /* next compressed data byte */ 6179 size_t input_len; /* length of the input buffer */ 6180 unsigned int range; /* identical to encoder's 6181 * range */ 6182 unsigned int value; /* contains at least 8 6183 * significant bits */ 6184 int bit_count; /* # of bits shifted out of 6185 * value, max 7 */ 6186 }; 6188 static void 6189 init_bool_decoder(struct bool_decoder *d, 6190 const unsigned char *start_partition, 6191 size_t sz) 6192 { 6193 if (sz >= 2) 6194 { 6195 d->value = (start_partition[0] << 8) /* first 2 input 6196 * bytes */ 6197 | start_partition[1]; 6198 d->input = start_partition + 2; /* ptr to next byte */ 6199 d->input_len = sz - 2; 6200 } 6201 else 6202 { 6203 d->value = 0; 6204 d->input = NULL; 6205 d->input_len = 0; 6206 } 6208 d->range = 255; /* initial range is full */ 6209 d->bit_count = 0; /* have not yet shifted out any bits */ 6210 } 6212 static int bool_get(struct bool_decoder *d, int probability) 6213 { 6214 /* range and split are identical to the corresponding values 6215 used by the encoder when this bool was written */ 6217 unsigned int split = 1 + (((d->range - 1) * probability) >> 8); 6218 unsigned int SPLIT = split << 8; 6219 int retval; /* will be 0 or 1 */ 6221 if (d->value >= SPLIT) /* encoded a one */ 6222 { 6223 retval = 1; 6224 d->range -= split; /* reduce range */ 6225 d->value -= SPLIT; /* subtract off left endpoint of 6226 * interval */ 6227 } 6228 else /* encoded a zero */ 6229 { 6230 retval = 0; 6231 d->range = split; /* reduce range, no change in left 6232 * endpoint */ 6233 } 6235 while (d->range < 128) /* shift out irrelevant value bits */ 6236 { 6237 d->value <<= 1; 6238 d->range <<= 1; 6240 if (++d->bit_count == 8) /* shift in new bits 8 at a time */ 6241 { 6242 d->bit_count = 0; 6244 if (d->input_len) 6245 { 6246 d->value |= *d->input++; 6247 d->input_len--; 6248 } 6249 } 6250 } 6252 return retval; 6253 } 6255 static int bool_get_bit(struct bool_decoder *br) 6256 { 6257 return bool_get(br, 128); 6258 } 6260 static int bool_get_uint(struct bool_decoder *br, int bits) 6261 { 6262 int z = 0; 6263 int bit; 6265 for (bit = bits - 1; bit >= 0; bit--) 6266 { 6267 z |= (bool_get_bit(br) << bit); 6268 } 6269 return z; 6270 } 6272 static int bool_get_int(struct bool_decoder *br, int bits) 6273 { 6274 int z = 0; 6275 int bit; 6277 for (bit = bits - 1; bit >= 0; bit--) 6278 { 6279 z |= (bool_get_bit(br) << bit); 6280 } 6282 return bool_get_bit(br) ? -z : z; 6283 } 6285 static int bool_maybe_get_int(struct bool_decoder *br, int bits) 6286 { 6287 return bool_get_bit(br) ? bool_get_int(br, bits) : 0; 6288 } 6290 static int 6291 bool_read_tree(struct bool_decoder *bool, 6292 const int *t, 6293 const unsigned char *p) 6294 { 6295 int i = 0; 6297 while ((i = t[ i + bool_get(bool, p[i>>1])]) > 0) ; 6299 return -i; 6300 } 6301 #endif 6303 ---- End code block ---------------------------------------- 6305 20.3. dequant_data.h 6307 ---- Begin code block -------------------------------------- 6309 static const int dc_q_lookup[128] = 6310 { 6311 4, 5, 6, 7, 8, 9, 10, 10, 6312 11, 12, 13, 14, 15, 16, 17, 17, 6313 18, 19, 20, 20, 21, 21, 22, 22, 6314 23, 23, 24, 25, 25, 26, 27, 28, 6315 29, 30, 31, 32, 33, 34, 35, 36, 6316 37, 37, 38, 39, 40, 41, 42, 43, 6317 44, 45, 46, 46, 47, 48, 49, 50, 6318 51, 52, 53, 54, 55, 56, 57, 58, 6319 59, 60, 61, 62, 63, 64, 65, 66, 6320 67, 68, 69, 70, 71, 72, 73, 74, 6321 75, 76, 76, 77, 78, 79, 80, 81, 6322 82, 83, 84, 85, 86, 87, 88, 89, 6323 91, 93, 95, 96, 98, 100, 101, 102, 6324 104, 106, 108, 110, 112, 114, 116, 118, 6325 122, 124, 126, 128, 130, 132, 134, 136, 6326 138, 140, 143, 145, 148, 151, 154, 157 6327 }; 6328 static const int ac_q_lookup[128] = 6329 { 6330 4, 5, 6, 7, 8, 9, 10, 11, 6331 12, 13, 14, 15, 16, 17, 18, 19, 6332 20, 21, 22, 23, 24, 25, 26, 27, 6333 28, 29, 30, 31, 32, 33, 34, 35, 6334 36, 37, 38, 39, 40, 41, 42, 43, 6335 44, 45, 46, 47, 48, 49, 50, 51, 6336 52, 53, 54, 55, 56, 57, 58, 60, 6337 62, 64, 66, 68, 70, 72, 74, 76, 6338 78, 80, 82, 84, 86, 88, 90, 92, 6339 94, 96, 98, 100, 102, 104, 106, 108, 6340 110, 112, 114, 116, 119, 122, 125, 128, 6341 131, 134, 137, 140, 143, 146, 149, 152, 6342 155, 158, 161, 164, 167, 170, 173, 177, 6343 181, 185, 189, 193, 197, 201, 205, 209, 6344 213, 217, 221, 225, 229, 234, 239, 245, 6345 249, 254, 259, 264, 269, 274, 279, 284 6346 }; 6348 ---- End code block ---------------------------------------- 6350 20.4. dixie.c 6351 ---- Begin code block -------------------------------------- 6353 /* 6354 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6355 * 6356 * Use of this source code is governed by a BSD-style license 6357 * that can be found in the LICENSE file in the root of the source 6358 * tree. An additional intellectual property rights grant can be 6359 * found in the file PATENTS. All contributing project authors may 6360 * be found in the AUTHORS file in the root of the source tree. 6361 */ 6362 #include "vpx_codec_internal.h" 6363 #include "bit_ops.h" 6364 #include "dixie.h" 6365 #include "vp8_prob_data.h" 6366 #include "dequant_data.h" 6367 #include "modemv.h" 6368 #include "tokens.h" 6369 #include "predict.h" 6370 #include "dixie_loopfilter.h" 6371 #include 6372 #include 6374 enum 6375 { 6376 FRAME_HEADER_SZ = 3, 6377 KEYFRAME_HEADER_SZ = 7 6378 }; 6380 #define ARRAY_COPY(a,b) {\ 6381 assert(sizeof(a)==sizeof(b));memcpy(a,b,sizeof(a));} 6382 static void 6383 decode_entropy_header(struct vp8_decoder_ctx *ctx, 6384 struct bool_decoder *bool, 6385 struct vp8_entropy_hdr *hdr) 6386 { 6387 int i, j, k, l; 6389 /* Read coefficient probability updates */ 6390 for (i = 0; i < BLOCK_TYPES; i++) 6391 for (j = 0; j < COEF_BANDS; j++) 6392 for (k = 0; k < PREV_COEF_CONTEXTS; k++) 6393 for (l = 0; l < ENTROPY_NODES; l++) 6394 if (bool_get(bool, 6395 k_coeff_entropy_update_probs 6396 [i][j][k][l])) 6397 hdr->coeff_probs[i][j][k][l] = 6398 bool_get_uint(bool, 8); 6400 /* Read coefficient skip mode probability */ 6401 hdr->coeff_skip_enabled = bool_get_bit(bool); 6403 if (hdr->coeff_skip_enabled) 6404 hdr->coeff_skip_prob = bool_get_uint(bool, 8); 6406 /* Parse interframe probability updates */ 6407 if (!ctx->frame_hdr.is_keyframe) 6408 { 6409 hdr->prob_inter = bool_get_uint(bool, 8); 6410 hdr->prob_last = bool_get_uint(bool, 8); 6411 hdr->prob_gf = bool_get_uint(bool, 8); 6413 if (bool_get_bit(bool)) 6414 for (i = 0; i < 4; i++) 6415 hdr->y_mode_probs[i] = bool_get_uint(bool, 8); 6417 if (bool_get_bit(bool)) 6418 for (i = 0; i < 3; i++) 6419 hdr->uv_mode_probs[i] = bool_get_uint(bool, 8); 6421 for (i = 0; i < 2; i++) 6422 for (j = 0; j < MV_PROB_CNT; j++) 6423 if (bool_get(bool, k_mv_entropy_update_probs[i][j])) 6424 { 6425 int x = bool_get_uint(bool, 7); 6426 hdr->mv_probs[i][j] = x ? x << 1 : 1; 6427 } 6428 } 6429 } 6431 static void 6432 decode_reference_header(struct vp8_decoder_ctx *ctx, 6433 struct bool_decoder *bool, 6434 struct vp8_reference_hdr *hdr) 6435 { 6436 unsigned int key = ctx->frame_hdr.is_keyframe; 6438 hdr->refresh_gf = key ? 1 : bool_get_bit(bool); 6439 hdr->refresh_arf = key ? 1 : bool_get_bit(bool); 6440 hdr->copy_gf = key ? 0 : !hdr->refresh_gf 6441 ? bool_get_uint(bool, 2) : 0; 6442 hdr->copy_arf = key ? 0 : !hdr->refresh_arf 6443 ? bool_get_uint(bool, 2) : 0; 6444 hdr->sign_bias[GOLDEN_FRAME] = key ? 0 : bool_get_bit(bool); 6445 hdr->sign_bias[ALTREF_FRAME] = key ? 0 : bool_get_bit(bool); 6446 hdr->refresh_entropy = bool_get_bit(bool); 6447 hdr->refresh_last = key ? 1 : bool_get_bit(bool); 6448 } 6450 static void 6451 decode_quantizer_header(struct vp8_decoder_ctx *ctx, 6452 struct bool_decoder *bool, 6453 struct vp8_quant_hdr *hdr) 6454 { 6455 int update; 6456 int last_q = hdr->q_index; 6458 hdr->q_index = bool_get_uint(bool, 7); 6459 update = last_q != hdr->q_index; 6460 update |= (hdr->y1_dc_delta_q = bool_maybe_get_int(bool, 4)); 6461 update |= (hdr->y2_dc_delta_q = bool_maybe_get_int(bool, 4)); 6462 update |= (hdr->y2_ac_delta_q = bool_maybe_get_int(bool, 4)); 6463 update |= (hdr->uv_dc_delta_q = bool_maybe_get_int(bool, 4)); 6464 update |= (hdr->uv_ac_delta_q = bool_maybe_get_int(bool, 4)); 6465 hdr->delta_update = update; 6466 } 6468 static void 6469 decode_and_init_token_partitions(struct vp8_decoder_ctx *ctx, 6470 struct bool_decoder *bool, 6471 const unsigned char *data, 6472 unsigned int sz, 6473 struct vp8_token_hdr *hdr) 6474 { 6475 int i; 6477 hdr->partitions = 1 << bool_get_uint(bool, 2); 6479 if (sz < 3 *(hdr->partitions - 1)) 6480 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6481 "Truncated packet found parsing partition" 6482 " lengths."); 6484 sz -= 3 * (hdr->partitions - 1); 6486 for (i = 0; i < hdr->partitions; i++) 6487 { 6488 if (i < hdr->partitions - 1) 6489 { 6490 hdr->partition_sz[i] = (data[2] << 16) 6491 | (data[1] << 8) | data[0]; 6492 data += 3; 6493 } 6494 else 6495 hdr->partition_sz[i] = sz; 6497 if (sz < hdr->partition_sz[i]) 6498 vpx_internal_error(&ctx->error, VPX_CODEC_CORRUPT_FRAME, 6499 "Truncated partition %d", i); 6501 sz -= hdr->partition_sz[i]; 6502 } 6504 for (i = 0; i < ctx->token_hdr.partitions; i++) 6505 { 6506 init_bool_decoder(&ctx->tokens[i].bool, data, 6507 ctx->token_hdr.partition_sz[i]); 6508 data += ctx->token_hdr.partition_sz[i]; 6509 } 6510 } 6512 static void 6513 decode_loopfilter_header(struct vp8_decoder_ctx *ctx, 6514 struct bool_decoder *bool, 6515 struct vp8_loopfilter_hdr *hdr) 6516 { 6517 if (ctx->frame_hdr.is_keyframe) 6518 memset(hdr, 0, sizeof(*hdr)); 6520 hdr->use_simple = bool_get_bit(bool); 6521 hdr->level = bool_get_uint(bool, 6); 6522 hdr->sharpness = bool_get_uint(bool, 3); 6523 hdr->delta_enabled = bool_get_bit(bool); 6525 if (hdr->delta_enabled && bool_get_bit(bool)) 6526 { 6527 int i; 6529 for (i = 0; i < BLOCK_CONTEXTS; i++) 6530 hdr->ref_delta[i] = bool_maybe_get_int(bool, 6); 6532 for (i = 0; i < BLOCK_CONTEXTS; i++) 6533 hdr->mode_delta[i] = bool_maybe_get_int(bool, 6); 6534 } 6535 } 6536 static void 6537 decode_segmentation_header(struct vp8_decoder_ctx *ctx, 6538 struct bool_decoder *bool, 6539 struct vp8_segment_hdr *hdr) 6540 { 6541 if (ctx->frame_hdr.is_keyframe) 6542 memset(hdr, 0, sizeof(*hdr)); 6544 hdr->enabled = bool_get_bit(bool); 6546 if (hdr->enabled) 6547 { 6548 int i; 6550 hdr->update_map = bool_get_bit(bool); 6551 hdr->update_data = bool_get_bit(bool); 6553 if (hdr->update_data) 6554 { 6555 hdr->abs = bool_get_bit(bool); 6557 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6558 hdr->quant_idx[i] = bool_maybe_get_int(bool, 7); 6560 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6561 hdr->lf_level[i] = bool_maybe_get_int(bool, 6); 6562 } 6564 if (hdr->update_map) 6565 { 6566 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) 6567 hdr->tree_probs[i] = bool_get_bit(bool) 6568 ? bool_get_uint(bool, 8) 6569 : 255; 6570 } 6571 } 6572 else 6573 { 6574 hdr->update_map = 0; 6575 hdr->update_data = 0; 6576 } 6577 } 6579 static void 6580 dequant_global_init(struct dequant_factors dqf[MAX_MB_SEGMENTS]) 6581 { 6582 int i; 6583 for (i = 0; i < MAX_MB_SEGMENTS; i++) 6584 dqf[i].quant_idx = -1; 6585 } 6587 static int 6588 clamp_q(int q) 6589 { 6590 if (q < 0) return 0; 6591 else if (q > 127) return 127; 6593 return q; 6594 } 6596 static int 6597 dc_q(int q) 6598 { 6599 return dc_q_lookup[clamp_q(q)]; 6600 } 6602 static int 6603 ac_q(int q) 6604 { 6605 return ac_q_lookup[clamp_q(q)]; 6606 } 6608 static void 6609 dequant_init(struct dequant_factors factors[MAX_MB_SEGMENTS], 6610 const struct vp8_segment_hdr *seg, 6611 const struct vp8_quant_hdr *quant_hdr) 6612 { 6613 int i, q; 6614 struct dequant_factors *dqf = factors; 6616 for (i = 0; i < (seg->enabled ? MAX_MB_SEGMENTS : 1); i++) 6617 { 6618 q = quant_hdr->q_index; 6620 if (seg->enabled) 6621 q = (!seg->abs) ? q + seg->quant_idx[i] 6622 : seg->quant_idx[i]; 6624 if (dqf->quant_idx != q || quant_hdr->delta_update) 6625 { 6626 dqf->factor[TOKEN_BLOCK_Y1][0] = 6627 dc_q(q + quant_hdr->y1_dc_delta_q); 6628 dqf->factor[TOKEN_BLOCK_Y1][1] = 6629 ac_q(q); 6630 dqf->factor[TOKEN_BLOCK_UV][0] = 6631 dc_q(q + quant_hdr->uv_dc_delta_q); 6632 dqf->factor[TOKEN_BLOCK_UV][1] = 6633 ac_q(q + quant_hdr->uv_ac_delta_q); 6634 dqf->factor[TOKEN_BLOCK_Y2][0] = 6635 dc_q(q + quant_hdr->y2_dc_delta_q) * 2; 6636 dqf->factor[TOKEN_BLOCK_Y2][1] = 6637 ac_q(q + quant_hdr->y2_ac_delta_q) * 155 / 100; 6639 if (dqf->factor[TOKEN_BLOCK_Y2][1] < 8) 6640 dqf->factor[TOKEN_BLOCK_Y2][1] = 8; 6642 if (dqf->factor[TOKEN_BLOCK_UV][0] > 132) 6643 dqf->factor[TOKEN_BLOCK_UV][0] = 132; 6645 dqf->quant_idx = q; 6646 } 6648 dqf++; 6649 } 6650 } 6652 static void 6653 decode_frame(struct vp8_decoder_ctx *ctx, 6654 const unsigned char *data, 6655 unsigned int sz) 6656 { 6657 vpx_codec_err_t res; 6658 struct bool_decoder bool; 6659 int i, row, partition; 6661 ctx->saved_entropy_valid = 0; 6663 if ((res = vp8_parse_frame_header(data, sz, &ctx->frame_hdr))) 6664 vpx_internal_error(&ctx->error, res, 6665 "Failed to parse frame header"); 6667 if (ctx->frame_hdr.is_experimental) 6668 vpx_internal_error(&ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6669 "Experimental bitstreams not supported."); 6671 data += FRAME_HEADER_SZ; 6672 sz -= FRAME_HEADER_SZ; 6673 if (ctx->frame_hdr.is_keyframe) 6674 { 6675 data += KEYFRAME_HEADER_SZ; 6676 sz -= KEYFRAME_HEADER_SZ; 6677 ctx->mb_cols = (ctx->frame_hdr.kf.w + 15) / 16; 6678 ctx->mb_rows = (ctx->frame_hdr.kf.h + 15) / 16; 6679 } 6681 /* Start the bitreader for the header/entropy partition */ 6682 init_bool_decoder(&bool, data, ctx->frame_hdr.part0_sz); 6684 /* Skip the colorspace and clamping bits */ 6685 if (ctx->frame_hdr.is_keyframe) 6686 if (bool_get_uint(&bool, 2)) 6687 vpx_internal_error( 6688 &ctx->error, VPX_CODEC_UNSUP_BITSTREAM, 6689 "Reserved bits not supported."); 6691 decode_segmentation_header(ctx, &bool, &ctx->segment_hdr); 6692 decode_loopfilter_header(ctx, &bool, &ctx->loopfilter_hdr); 6693 decode_and_init_token_partitions(ctx, 6694 &bool, 6695 data + ctx->frame_hdr.part0_sz, 6696 sz - ctx->frame_hdr.part0_sz, 6697 &ctx->token_hdr); 6698 decode_quantizer_header(ctx, &bool, &ctx->quant_hdr); 6699 decode_reference_header(ctx, &bool, &ctx->reference_hdr); 6701 /* Set keyframe entropy defaults. These get updated on keyframes 6702 * regardless of the refresh_entropy setting. 6703 */ 6704 if (ctx->frame_hdr.is_keyframe) 6705 { 6706 ARRAY_COPY(ctx->entropy_hdr.coeff_probs, 6707 k_default_coeff_probs); 6708 ARRAY_COPY(ctx->entropy_hdr.mv_probs, 6709 k_default_mv_probs); 6710 ARRAY_COPY(ctx->entropy_hdr.y_mode_probs, 6711 k_default_y_mode_probs); 6712 ARRAY_COPY(ctx->entropy_hdr.uv_mode_probs, 6713 k_default_uv_mode_probs); 6714 } 6716 if (!ctx->reference_hdr.refresh_entropy) 6717 { 6718 ctx->saved_entropy = ctx->entropy_hdr; 6719 ctx->saved_entropy_valid = 1; 6720 } 6721 decode_entropy_header(ctx, &bool, &ctx->entropy_hdr); 6723 vp8_dixie_modemv_init(ctx); 6724 vp8_dixie_tokens_init(ctx); 6725 vp8_dixie_predict_init(ctx); 6726 dequant_init(ctx->dequant_factors, &ctx->segment_hdr, 6727 &ctx->quant_hdr); 6729 for (row = 0, partition = 0; row < ctx->mb_rows; row++) 6730 { 6731 vp8_dixie_modemv_process_row( 6732 ctx, &bool, row, 0, ctx->mb_cols); 6733 vp8_dixie_tokens_process_row(ctx, partition, row, 0, 6734 ctx->mb_cols); 6735 vp8_dixie_predict_process_row(ctx, row, 0, ctx->mb_cols); 6737 if (ctx->loopfilter_hdr.level && row) 6738 vp8_dixie_loopfilter_process_row(ctx, row - 1, 0, 6739 ctx->mb_cols); 6741 if (++partition == ctx->token_hdr.partitions) 6742 partition = 0; 6743 } 6745 if (ctx->loopfilter_hdr.level) 6746 vp8_dixie_loopfilter_process_row( 6747 ctx, row - 1, 0, ctx->mb_cols); 6749 ctx->frame_cnt++; 6751 if (!ctx->reference_hdr.refresh_entropy) 6752 { 6753 ctx->entropy_hdr = ctx->saved_entropy; 6754 ctx->saved_entropy_valid = 0; 6755 } 6757 /* Handle reference frame updates */ 6758 if (ctx->reference_hdr.copy_arf == 1) 6759 { 6760 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6761 ctx->ref_frames[ALTREF_FRAME] = 6762 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6763 } 6764 else if (ctx->reference_hdr.copy_arf == 2) 6765 { 6766 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6767 ctx->ref_frames[ALTREF_FRAME] = 6768 vp8_dixie_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6770 } 6772 if (ctx->reference_hdr.copy_gf == 1) 6773 { 6774 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6775 ctx->ref_frames[GOLDEN_FRAME] = 6776 vp8_dixie_ref_frame(ctx->ref_frames[LAST_FRAME]); 6777 } 6778 else if (ctx->reference_hdr.copy_gf == 2) 6779 { 6780 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6781 ctx->ref_frames[GOLDEN_FRAME] = 6782 vp8_dixie_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6783 } 6785 if (ctx->reference_hdr.refresh_gf) 6786 { 6787 vp8_dixie_release_ref_frame(ctx->ref_frames[GOLDEN_FRAME]); 6788 ctx->ref_frames[GOLDEN_FRAME] = 6789 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6790 } 6792 if (ctx->reference_hdr.refresh_arf) 6793 { 6794 vp8_dixie_release_ref_frame(ctx->ref_frames[ALTREF_FRAME]); 6795 ctx->ref_frames[ALTREF_FRAME] = 6796 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6797 } 6799 if (ctx->reference_hdr.refresh_last) 6800 { 6801 vp8_dixie_release_ref_frame(ctx->ref_frames[LAST_FRAME]); 6802 ctx->ref_frames[LAST_FRAME] = 6803 vp8_dixie_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 6804 } 6806 } 6808 void 6809 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx) 6810 { 6811 dequant_global_init(ctx->dequant_factors); 6812 } 6814 #define CHECK_FOR_UPDATE(lval,rval,update_flag) do {\ 6815 unsigned int old = lval; \ 6816 update_flag |= (old != (lval = rval)); \ 6817 } while(0) 6819 vpx_codec_err_t 6820 vp8_parse_frame_header(const unsigned char *data, 6821 unsigned int sz, 6822 struct vp8_frame_hdr *hdr) 6823 { 6824 unsigned long raw; 6826 if (sz < 10) 6827 return VPX_CODEC_CORRUPT_FRAME; 6829 /* The frame header is defined as a three byte little endian 6830 * value 6831 */ 6832 raw = data[0] | (data[1] << 8) | (data[2] << 16); 6833 hdr->is_keyframe = !BITS_GET(raw, 0, 1); 6834 hdr->version = BITS_GET(raw, 1, 2); 6835 hdr->is_experimental = BITS_GET(raw, 3, 1); 6836 hdr->is_shown = BITS_GET(raw, 4, 1); 6837 hdr->part0_sz = BITS_GET(raw, 5, 19); 6839 if (sz <= hdr->part0_sz + (hdr->is_keyframe ? 10 : 3)) 6840 return VPX_CODEC_CORRUPT_FRAME; 6842 hdr->frame_size_updated = 0; 6844 if (hdr->is_keyframe) 6845 { 6846 unsigned int update = 0; 6848 /* Keyframe header consists of a three byte sync code 6849 * followed by the width and height and associated scaling 6850 * factors. 6851 */ 6852 if (data[3] != 0x9d || data[4] != 0x01 || data[5] != 0x2a) 6853 return VPX_CODEC_UNSUP_BITSTREAM; 6855 raw = data[6] | (data[7] << 8) 6856 | (data[8] << 16) | (data[9] << 24); 6857 CHECK_FOR_UPDATE(hdr->kf.w, BITS_GET(raw, 0, 14), 6858 update); 6859 CHECK_FOR_UPDATE(hdr->kf.scale_w, BITS_GET(raw, 14, 2), 6860 update); 6861 CHECK_FOR_UPDATE(hdr->kf.h, BITS_GET(raw, 16, 14), 6862 update); 6863 CHECK_FOR_UPDATE(hdr->kf.scale_h, BITS_GET(raw, 30, 2), 6864 update); 6866 hdr->frame_size_updated = update; 6868 if (!hdr->kf.w || !hdr->kf.h) 6869 return VPX_CODEC_UNSUP_BITSTREAM; 6870 } 6872 return VPX_CODEC_OK; 6873 } 6875 vpx_codec_err_t 6876 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 6877 const unsigned char *data, 6878 unsigned int sz) 6879 { 6880 volatile struct vp8_decoder_ctx *ctx_ = ctx; 6882 ctx->error.error_code = VPX_CODEC_OK; 6883 ctx->error.has_detail = 0; 6885 if (!setjmp(ctx->error.jmp)) 6886 decode_frame(ctx, data, sz); 6888 return ctx_->error.error_code; 6889 } 6891 void 6892 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx) 6893 { 6894 vp8_dixie_predict_destroy(ctx); 6895 vp8_dixie_tokens_destroy(ctx); 6896 vp8_dixie_modemv_destroy(ctx); 6897 } 6899 ---- End code block ---------------------------------------- 6901 20.5. dixie.h 6903 ---- Begin code block -------------------------------------- 6905 /* 6906 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 6907 * 6908 * Use of this source code is governed by a BSD-style license 6909 * that can be found in the LICENSE file in the root of the source 6910 * tree. An additional intellectual property rights grant can be 6911 * found in the file PATENTS. All contributing project authors may 6912 * be found in the AUTHORS file in the root of the source tree. 6913 */ 6914 #ifndef DIXIE_H 6915 #define DIXIE_H 6916 #include "vpx_codec_internal.h" 6917 #include "bool_decoder.h" 6919 struct vp8_frame_hdr 6920 { 6921 unsigned int is_keyframe; /* Frame is a keyframe */ 6922 unsigned int is_experimental; /* Frame is a keyframe */ 6923 unsigned int version; /* Bitstream version */ 6924 unsigned int is_shown; /* Frame is to be displayed. */ 6925 unsigned int part0_sz; /* Partition 0 length, in bytes */ 6927 struct vp8_kf_hdr 6928 { 6929 unsigned int w; /* Width */ 6930 unsigned int h; /* Height */ 6931 unsigned int scale_w; /* Scaling factor, Width */ 6932 unsigned int scale_h; /* Scaling factor, Height */ 6933 } kf; 6935 unsigned int frame_size_updated; /* Flag to indicate a resolution 6936 * update. 6937 */ 6938 }; 6940 enum 6941 { 6942 MB_FEATURE_TREE_PROBS = 3, 6943 MAX_MB_SEGMENTS = 4 6944 }; 6946 struct vp8_segment_hdr 6947 { 6948 unsigned int enabled; 6949 unsigned int update_data; 6950 unsigned int update_map; 6951 unsigned int abs; /* 0=deltas, 1=absolute values */ 6952 unsigned int tree_probs[MB_FEATURE_TREE_PROBS]; 6953 int lf_level[MAX_MB_SEGMENTS]; 6954 int quant_idx[MAX_MB_SEGMENTS]; 6956 }; 6958 enum 6959 { 6960 BLOCK_CONTEXTS = 4 6961 }; 6963 struct vp8_loopfilter_hdr 6964 { 6965 unsigned int use_simple; 6966 unsigned int level; 6967 unsigned int sharpness; 6968 unsigned int delta_enabled; 6969 int ref_delta[BLOCK_CONTEXTS]; 6970 int mode_delta[BLOCK_CONTEXTS]; 6971 }; 6973 enum 6974 { 6975 MAX_PARTITIONS = 8 6976 }; 6978 struct vp8_token_hdr 6979 { 6980 unsigned int partitions; 6981 unsigned int partition_sz[MAX_PARTITIONS]; 6982 }; 6984 struct vp8_quant_hdr 6985 { 6986 unsigned int q_index; 6987 int delta_update; 6988 int y1_dc_delta_q; 6989 int y2_dc_delta_q; 6990 int y2_ac_delta_q; 6991 int uv_dc_delta_q; 6992 int uv_ac_delta_q; 6993 }; 6995 struct vp8_reference_hdr 6996 { 6997 unsigned int refresh_last; 6998 unsigned int refresh_gf; 6999 unsigned int refresh_arf; 7000 unsigned int copy_gf; 7001 unsigned int copy_arf; 7002 unsigned int sign_bias[4]; 7003 unsigned int refresh_entropy; 7004 }; 7006 enum 7007 { 7008 BLOCK_TYPES = 4, 7009 PREV_COEF_CONTEXTS = 3, 7010 COEF_BANDS = 8, 7011 ENTROPY_NODES = 11, 7012 }; 7013 typedef unsigned char coeff_probs_table_t[BLOCK_TYPES][COEF_BANDS] 7014 [PREV_COEF_CONTEXTS] 7015 [ENTROPY_NODES]; 7017 enum 7018 { 7019 MV_PROB_CNT = 2 + 8 - 1 + 10 /* from entropymv.h */ 7020 }; 7021 typedef unsigned char mv_component_probs_t[MV_PROB_CNT]; 7023 struct vp8_entropy_hdr 7024 { 7025 coeff_probs_table_t coeff_probs; 7026 mv_component_probs_t mv_probs[2]; 7027 unsigned int coeff_skip_enabled; 7028 unsigned char coeff_skip_prob; 7029 unsigned char y_mode_probs[4]; 7030 unsigned char uv_mode_probs[3]; 7031 unsigned char prob_inter; 7032 unsigned char prob_last; 7033 unsigned char prob_gf; 7034 }; 7036 enum reference_frame 7037 { 7038 CURRENT_FRAME, 7039 LAST_FRAME, 7040 GOLDEN_FRAME, 7041 ALTREF_FRAME, 7042 NUM_REF_FRAMES 7044 }; 7046 enum prediction_mode 7047 { 7048 /* 16x16 intra modes */ 7049 DC_PRED, V_PRED, H_PRED, TM_PRED, B_PRED, 7051 /* 16x16 inter modes */ 7052 NEARESTMV, NEARMV, ZEROMV, NEWMV, SPLITMV, 7054 MB_MODE_COUNT, 7056 /* 4x4 intra modes */ 7057 B_DC_PRED = 0, B_TM_PRED, B_VE_PRED, B_HE_PRED, B_LD_PRED, 7058 B_RD_PRED, B_VR_PRED, B_VL_PRED, B_HD_PRED, B_HU_PRED, 7060 /* 4x4 inter modes */ 7061 LEFT4X4, ABOVE4X4, ZERO4X4, NEW4X4, 7063 B_MODE_COUNT 7064 }; 7066 enum splitmv_partitioning 7067 { 7068 SPLITMV_16X8, 7069 SPLITMV_8X16, 7070 SPLITMV_8X8, 7071 SPLITMV_4X4 7072 }; 7074 typedef short filter_t[6]; 7076 typedef union mv 7077 { 7078 struct 7079 { 7080 int16_t x, y; 7081 } d; 7082 uint32_t raw; 7083 } mv_t; 7085 struct mb_base_info 7086 { 7087 unsigned char y_mode : 4; 7088 unsigned char uv_mode : 4; 7089 unsigned char segment_id : 2; 7090 unsigned char ref_frame : 2; 7091 unsigned char skip_coeff : 1; 7092 unsigned char need_mc_border : 1; 7093 enum splitmv_partitioning partitioning : 2; 7094 union mv mv; 7095 unsigned int eob_mask; 7096 }; 7098 struct mb_info 7099 { 7100 struct mb_base_info base; 7101 union 7102 { 7103 union mv mvs[16]; 7104 enum prediction_mode modes[16]; 7105 } split; 7106 }; 7108 /* A "token entropy context" has 4 Y values, 2 U, 2 V, and 1 Y2 */ 7109 typedef int token_entropy_ctx_t[4 + 2 + 2 + 1]; 7111 struct token_decoder 7112 { 7113 struct bool_decoder bool; 7114 token_entropy_ctx_t left_token_entropy_ctx; 7115 short *coeffs; 7116 }; 7118 enum token_block_type 7119 { 7120 TOKEN_BLOCK_Y1, 7121 TOKEN_BLOCK_UV, 7122 TOKEN_BLOCK_Y2, 7123 TOKEN_BLOCK_TYPES, 7124 }; 7126 struct dequant_factors 7127 { 7128 int quant_idx; 7129 short factor[TOKEN_BLOCK_TYPES][2]; /* [ Y1, UV, Y2 ] 7130 * [ DC, AC ] */ 7131 }; 7132 struct ref_cnt_img 7133 { 7134 vpx_image_t img; 7135 unsigned int ref_cnt; 7136 }; 7138 struct vp8_decoder_ctx 7139 { 7140 struct vpx_internal_error_info error; 7141 unsigned int frame_cnt; 7143 struct vp8_frame_hdr frame_hdr; 7144 struct vp8_segment_hdr segment_hdr; 7145 struct vp8_loopfilter_hdr loopfilter_hdr; 7146 struct vp8_token_hdr token_hdr; 7147 struct vp8_quant_hdr quant_hdr; 7148 struct vp8_reference_hdr reference_hdr; 7149 struct vp8_entropy_hdr entropy_hdr; 7151 struct vp8_entropy_hdr saved_entropy; 7152 unsigned int saved_entropy_valid; 7154 unsigned int mb_rows; 7155 unsigned int mb_cols; 7156 struct mb_info *mb_info_storage; 7157 struct mb_info **mb_info_rows_storage; 7158 struct mb_info **mb_info_rows; 7160 token_entropy_ctx_t *above_token_entropy_ctx; 7161 struct token_decoder tokens[MAX_PARTITIONS]; 7162 struct dequant_factors dequant_factors[MAX_MB_SEGMENTS]; 7164 struct ref_cnt_img frame_strg[NUM_REF_FRAMES]; 7165 struct ref_cnt_img *ref_frames[NUM_REF_FRAMES]; 7166 ptrdiff_t ref_frame_offsets[4]; 7168 const filter_t *subpixel_filters; 7169 }; 7171 void 7172 vp8_dixie_decode_init(struct vp8_decoder_ctx *ctx); 7174 void 7175 vp8_dixie_decode_destroy(struct vp8_decoder_ctx *ctx); 7176 vpx_codec_err_t 7177 vp8_parse_frame_header(const unsigned char *data, 7178 unsigned int sz, 7179 struct vp8_frame_hdr *hdr); 7181 vpx_codec_err_t 7182 vp8_dixie_decode_frame(struct vp8_decoder_ctx *ctx, 7183 const unsigned char *data, 7184 unsigned int sz); 7186 #define CLAMP_255(x) ((x)<0?0:((x)>255?255:(x))) 7188 #endif 7190 ---- End code block ---------------------------------------- 7192 20.6. dixie_loopfilter.c 7194 ---- Begin code block -------------------------------------- 7196 /* 7197 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7198 * 7199 * Use of this source code is governed by a BSD-style license 7200 * that can be found in the LICENSE file in the root of the source 7201 * tree. An additional intellectual property rights grant can be 7202 * found in the file PATENTS. All contributing project authors may 7203 * be found in the AUTHORS file in the root of the source tree. 7204 */ 7205 #include "dixie.h" 7206 #include "dixie_loopfilter.h" 7208 #define ABS(x) ((x) >= 0 ? (x) : -(x)) 7210 #define p3 pixels[-4*stride] 7211 #define p2 pixels[-3*stride] 7212 #define p1 pixels[-2*stride] 7213 #define p0 pixels[-1*stride] 7214 #define q0 pixels[ 0*stride] 7215 #define q1 pixels[ 1*stride] 7216 #define q2 pixels[ 2*stride] 7217 #define q3 pixels[ 3*stride] 7219 #define static 7220 static int 7221 saturate_int8(int x) 7222 { 7223 if (x < -128) 7224 return -128; 7226 if (x > 127) 7227 return 127; 7229 return x; 7230 } 7232 static int 7233 saturate_uint8(int x) 7234 { 7235 if (x < 0) 7236 return 0; 7238 if (x > 255) 7239 return 255; 7241 return x; 7242 } 7244 static int 7245 high_edge_variance(unsigned char *pixels, 7246 int stride, 7247 int hev_threshold) 7248 { 7249 return ABS(p1 - p0) > hev_threshold || 7250 ABS(q1 - q0) > hev_threshold; 7251 } 7253 static int 7254 simple_threshold(unsigned char *pixels, 7255 int stride, 7256 int filter_limit) 7257 { 7258 return (ABS(p0 - q0) * 2 + (ABS(p1 - q1) >> 1)) <= filter_limit; 7259 } 7261 static int 7262 normal_threshold(unsigned char *pixels, 7263 int stride, 7264 int edge_limit, 7265 int interior_limit) 7266 { 7267 int E = edge_limit; 7268 int I = interior_limit; 7270 return simple_threshold(pixels, stride, 2 * E + I) 7271 && ABS(p3 - p2) <= I && ABS(p2 - p1) <= I 7272 && ABS(p1 - p0) <= I && ABS(q3 - q2) <= I 7273 && ABS(q2 - q1) <= I && ABS(q1 - q0) <= I; 7274 } 7276 static void 7277 filter_common(unsigned char *pixels, 7278 int stride, 7279 int use_outer_taps) 7280 { 7281 int a, f1, f2; 7283 a = 3 * (q0 - p0); 7285 if (use_outer_taps) 7286 a += saturate_int8(p1 - q1); 7288 a = saturate_int8(a); 7290 f1 = ((a + 4 > 127) ? 127 : a + 4) >> 3; 7291 f2 = ((a + 3 > 127) ? 127 : a + 3) >> 3; 7293 p0 = saturate_uint8(p0 + f2); 7294 q0 = saturate_uint8(q0 - f1); 7296 if (!use_outer_taps) 7297 { 7298 /* This handles the case of subblock_filter() 7299 * (from the bitstream guide. 7300 */ 7301 a = (f1 + 1) >> 1; 7302 p1 = saturate_uint8(p1 + a); 7303 q1 = saturate_uint8(q1 - a); 7304 } 7305 } 7307 static void 7308 filter_mb_edge(unsigned char *pixels, 7309 int stride) 7310 { 7311 int w, a; 7313 w = saturate_int8(saturate_int8(p1 - q1) + 3 * (q0 - p0)); 7315 a = (27 * w + 63) >> 7; 7316 p0 = saturate_uint8(p0 + a); 7317 q0 = saturate_uint8(q0 - a); 7319 a = (18 * w + 63) >> 7; 7320 p1 = saturate_uint8(p1 + a); 7321 q1 = saturate_uint8(q1 - a); 7323 a = (9 * w + 63) >> 7; 7324 p2 = saturate_uint8(p2 + a); 7325 q2 = saturate_uint8(q2 - a); 7327 } 7329 static void 7330 filter_mb_v_edge(unsigned char *src, 7331 int stride, 7332 int edge_limit, 7333 int interior_limit, 7334 int hev_threshold, 7335 int size) 7336 { 7337 int i; 7339 for (i = 0; i < 8 * size; i++) 7340 { 7341 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7342 { 7343 if (high_edge_variance(src, 1, hev_threshold)) 7344 filter_common(src, 1, 1); 7345 else 7346 filter_mb_edge(src, 1); 7347 } 7349 src += stride; 7350 } 7351 } 7353 static void 7354 filter_subblock_v_edge(unsigned char *src, 7355 int stride, 7356 int edge_limit, 7357 int interior_limit, 7358 int hev_threshold, 7359 int size) 7360 { 7361 int i; 7363 for (i = 0; i < 8 * size; i++) 7364 { 7365 if (normal_threshold(src, 1, edge_limit, interior_limit)) 7366 filter_common(src, 1, 7367 high_edge_variance(src, 1, hev_threshold)); 7369 src += stride; 7370 } 7371 } 7373 static void 7374 filter_mb_h_edge(unsigned char *src, 7375 int stride, 7376 int edge_limit, 7377 int interior_limit, 7378 int hev_threshold, 7379 int size) 7380 { 7381 int i; 7383 for (i = 0; i < 8 * size; i++) 7384 { 7385 if (normal_threshold(src, stride, edge_limit, 7386 interior_limit)) 7387 { 7388 if (high_edge_variance(src, stride, hev_threshold)) 7389 filter_common(src, stride, 1); 7390 else 7391 filter_mb_edge(src, stride); 7392 } 7394 src += 1; 7395 } 7396 } 7398 static void 7399 filter_subblock_h_edge(unsigned char *src, 7400 int stride, 7401 int edge_limit, 7402 int interior_limit, 7403 int hev_threshold, 7404 int size) 7405 { 7406 int i; 7408 for (i = 0; i < 8 * size; i++) 7409 { 7410 if (normal_threshold(src, stride, edge_limit, 7411 interior_limit)) 7412 filter_common(src, stride, 7413 high_edge_variance(src, stride, 7414 hev_threshold)); 7416 src += 1; 7417 } 7418 } 7420 static void 7421 filter_v_edge_simple(unsigned char *src, 7422 int stride, 7423 int filter_limit) 7424 { 7425 int i; 7427 for (i = 0; i < 16; i++) 7428 { 7429 if (simple_threshold(src, 1, filter_limit)) 7430 filter_common(src, 1, 1); 7432 src += stride; 7433 } 7434 } 7436 static void 7437 filter_h_edge_simple(unsigned char *src, 7438 int stride, 7439 int filter_limit) 7440 { 7441 int i; 7443 for (i = 0; i < 16; i++) 7444 { 7445 if (simple_threshold(src, stride, filter_limit)) 7446 filter_common(src, stride, 1); 7448 src += 1; 7450 } 7451 } 7453 static void 7454 calculate_filter_parameters(struct vp8_decoder_ctx *ctx, 7455 struct mb_info *mbi, 7456 int *edge_limit_, 7457 int *interior_limit_, 7458 int *hev_threshold_) 7459 { 7460 int filter_level, interior_limit, hev_threshold; 7462 /* Reference code/spec seems to conflate filter_level and 7463 * edge_limit 7464 */ 7466 filter_level = ctx->loopfilter_hdr.level; 7468 if (ctx->segment_hdr.enabled) 7469 { 7470 if (!ctx->segment_hdr.abs) 7471 filter_level += 7472 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7473 else 7474 filter_level = 7475 ctx->segment_hdr.lf_level[mbi->base.segment_id]; 7476 } 7478 if (filter_level > 63) 7479 filter_level = 63; 7480 else if (filter_level < 0) 7481 filter_level = 0; 7483 if (ctx->loopfilter_hdr.delta_enabled) 7484 { 7485 filter_level += 7486 ctx->loopfilter_hdr.ref_delta[mbi->base.ref_frame]; 7488 if (mbi->base.ref_frame == CURRENT_FRAME) 7489 { 7490 if (mbi->base.y_mode == B_PRED) 7491 filter_level += ctx->loopfilter_hdr.mode_delta[0]; 7492 } 7493 else if (mbi->base.y_mode == ZEROMV) 7494 filter_level += ctx->loopfilter_hdr.mode_delta[1]; 7495 else if (mbi->base.y_mode == SPLITMV) 7496 filter_level += ctx->loopfilter_hdr.mode_delta[3]; 7498 else 7499 filter_level += ctx->loopfilter_hdr.mode_delta[2]; 7500 } 7502 if (filter_level > 63) 7503 filter_level = 63; 7504 else if (filter_level < 0) 7505 filter_level = 0; 7507 interior_limit = filter_level; 7509 if (ctx->loopfilter_hdr.sharpness) 7510 { 7511 interior_limit >>= ctx->loopfilter_hdr.sharpness > 4 ? 2 : 1; 7513 if (interior_limit > 9 - ctx->loopfilter_hdr.sharpness) 7514 interior_limit = 9 - ctx->loopfilter_hdr.sharpness; 7515 } 7517 if (interior_limit < 1) 7518 interior_limit = 1; 7520 hev_threshold = (filter_level >= 15); 7522 if (filter_level >= 40) 7523 hev_threshold++; 7525 if (filter_level >= 20 && !ctx->frame_hdr.is_keyframe) 7526 hev_threshold++; 7528 *edge_limit_ = filter_level; 7529 *interior_limit_ = interior_limit; 7530 *hev_threshold_ = hev_threshold; 7531 } 7533 static void 7534 filter_row_normal(struct vp8_decoder_ctx *ctx, 7535 unsigned int row, 7536 unsigned int start_col, 7537 unsigned int num_cols) 7538 { 7539 unsigned char *y, *u, *v; 7540 int stride, uv_stride; 7541 struct mb_info *mbi; 7542 unsigned int col; 7544 /* Adjust pointers based on row, start_col */ 7545 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7546 uv_stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 7547 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7548 u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 7549 v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 7550 y += (stride * row + start_col) * 16; 7551 u += (uv_stride * row + start_col) * 8; 7552 v += (uv_stride * row + start_col) * 8; 7553 mbi = ctx->mb_info_rows[row] + start_col; 7555 for (col = start_col; col < start_col + num_cols; col++) 7556 { 7557 int edge_limit, interior_limit, hev_threshold; 7559 /* TODO: only need to recalculate every MB if segmentation is 7560 * enabled. 7561 */ 7562 calculate_filter_parameters(ctx, mbi, &edge_limit, 7563 &interior_limit, &hev_threshold); 7565 if (edge_limit) 7566 { 7567 if (col) 7568 { 7569 filter_mb_v_edge(y, stride, edge_limit + 2, 7570 interior_limit, hev_threshold, 2); 7571 filter_mb_v_edge(u, uv_stride, edge_limit + 2, 7572 interior_limit, hev_threshold, 1); 7573 filter_mb_v_edge(v, uv_stride, edge_limit + 2, 7574 interior_limit, hev_threshold, 1); 7575 } 7577 /* NOTE: This conditional is actually dependent on the 7578 * number of coefficients decoded, not the skip flag as 7579 * coded in the bitstream. The tokens task is expected to 7580 * set 31 if there is *any* non-zero data. 7581 */ 7582 if (mbi->base.eob_mask 7583 || mbi->base.y_mode == SPLITMV 7584 || mbi->base.y_mode == B_PRED) 7585 { 7586 filter_subblock_v_edge(y + 4, stride, edge_limit, 7587 interior_limit, hev_threshold, 7588 2); 7589 filter_subblock_v_edge(y + 8, stride, edge_limit, 7590 interior_limit, hev_threshold, 7591 2); 7592 filter_subblock_v_edge(y + 12, stride, edge_limit, 7593 interior_limit, hev_threshold, 7594 2); 7595 filter_subblock_v_edge(u + 4, uv_stride, edge_limit, 7596 interior_limit, hev_threshold, 7597 1); 7598 filter_subblock_v_edge(v + 4, uv_stride, edge_limit, 7599 interior_limit, hev_threshold, 7600 1); 7601 } 7603 if (row) 7604 { 7605 filter_mb_h_edge(y, stride, edge_limit + 2, 7606 interior_limit, hev_threshold, 2); 7607 filter_mb_h_edge(u, uv_stride, edge_limit + 2, 7608 interior_limit, hev_threshold, 1); 7609 filter_mb_h_edge(v, uv_stride, edge_limit + 2, 7610 interior_limit, hev_threshold, 1); 7611 } 7613 if (mbi->base.eob_mask 7614 || mbi->base.y_mode == SPLITMV 7615 || mbi->base.y_mode == B_PRED) 7616 { 7617 filter_subblock_h_edge(y + 4 * stride, stride, 7618 edge_limit, interior_limit, 7619 hev_threshold, 2); 7620 filter_subblock_h_edge(y + 8 * stride, stride, 7621 edge_limit, interior_limit, 7622 hev_threshold, 2); 7623 filter_subblock_h_edge(y + 12 * stride, stride, 7624 edge_limit, interior_limit, 7625 hev_threshold, 2); 7626 filter_subblock_h_edge(u + 4 * uv_stride, uv_stride, 7627 edge_limit, interior_limit, 7628 hev_threshold, 1); 7629 filter_subblock_h_edge(v + 4 * uv_stride, uv_stride, 7630 edge_limit, interior_limit, 7631 hev_threshold, 1); 7632 } 7633 } 7635 y += 16; 7636 u += 8; 7637 v += 8; 7638 mbi++; 7639 } 7640 } 7641 static void 7642 filter_row_simple(struct vp8_decoder_ctx *ctx, 7643 unsigned int row, 7644 unsigned int start_col, 7645 unsigned int num_cols) 7646 { 7647 unsigned char *y; 7648 int stride; 7649 struct mb_info *mbi; 7650 unsigned int col; 7652 /* Adjust pointers based on row, start_col */ 7653 stride = ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 7654 y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 7655 y += (stride * row + start_col) * 16; 7656 mbi = ctx->mb_info_rows[row] + start_col; 7658 for (col = start_col; col < start_col + num_cols; col++) 7659 { 7660 int edge_limit, interior_limit, hev_threshold; 7662 /* TODO: only need to recalculate every MB if segmentation is 7663 * enabled. 7664 */ 7665 calculate_filter_parameters(ctx, mbi, &edge_limit, 7666 &interior_limit, &hev_threshold); 7668 if (edge_limit) 7669 { 7671 /* NOTE: This conditional is actually dependent on the 7672 * number of coefficients decoded, not the skip flag as 7673 * coded in the bitstream. The tokens task is expected to 7674 * set 31 if there is *any* non-zero data. 7675 */ 7676 int filter_subblocks = (mbi->base.eob_mask 7677 || mbi->base.y_mode == SPLITMV 7678 || mbi->base.y_mode == B_PRED); 7679 int mb_limit = (edge_limit + 2) * 2 + interior_limit; 7680 int b_limit = edge_limit * 2 + interior_limit; 7682 if (col) 7683 filter_v_edge_simple(y, stride, mb_limit); 7685 if (filter_subblocks) 7686 { 7687 filter_v_edge_simple(y + 4, stride, b_limit); 7688 filter_v_edge_simple(y + 8, stride, b_limit); 7689 filter_v_edge_simple(y + 12, stride, b_limit); 7690 } 7692 if (row) 7693 filter_h_edge_simple(y, stride, mb_limit); 7695 if (filter_subblocks) 7696 { 7697 filter_h_edge_simple(y + 4 * stride, stride, 7698 b_limit); 7699 filter_h_edge_simple(y + 8 * stride, stride, 7700 b_limit); 7701 filter_h_edge_simple(y + 12 * stride, stride, 7702 b_limit); 7703 } 7704 } 7706 y += 16; 7707 mbi++; 7708 } 7709 } 7711 void 7712 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7713 unsigned int row, 7714 unsigned int start_col, 7715 unsigned int num_cols) 7716 { 7717 if (ctx->loopfilter_hdr.use_simple) 7718 filter_row_simple(ctx, row, start_col, num_cols); 7719 else 7720 filter_row_normal(ctx, row, start_col, num_cols); 7721 } 7723 ---- End code block ---------------------------------------- 7725 20.7. dixie_loopfilter.h 7727 ---- Begin code block -------------------------------------- 7729 /* 7730 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7731 * 7732 * Use of this source code is governed by a BSD-style license 7733 * that can be found in the LICENSE file in the root of the source 7734 * tree. An additional intellectual property rights grant can be 7735 * found in the file PATENTS. All contributing project authors may 7736 * be found in the AUTHORS file in the root of the source tree. 7737 */ 7738 #ifndef DIXIE_LOOPFILTER_H 7739 #define DIXIE_LOOPFILTER_H 7741 void 7742 vp8_dixie_loopfilter_process_row(struct vp8_decoder_ctx *ctx, 7743 unsigned int row, 7744 unsigned int start_col, 7745 unsigned int num_cols); 7747 #endif 7749 ---- End code block ---------------------------------------- 7751 20.8. idct_add.c 7753 ---- Begin code block -------------------------------------- 7755 /* 7756 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7757 * 7758 * Use of this source code is governed by a BSD-style license 7759 * that can be found in the LICENSE file in the root of the source 7760 * tree. An additional intellectual property rights grant can be 7761 * found in the file PATENTS. All contributing project authors may 7762 * be found in the AUTHORS file in the root of the source tree. 7763 */ 7764 #include "dixie.h" 7765 #include "idct_add.h" 7766 #include 7768 void 7769 vp8_dixie_walsh(const short *input, short *output) 7770 { 7771 int i; 7772 int a1, b1, c1, d1; 7773 int a2, b2, c2, d2; 7774 const short *ip = input; 7775 short *op = output; 7777 for (i = 0; i < 4; i++) 7778 { 7779 a1 = ip[0] + ip[12]; 7780 b1 = ip[4] + ip[8]; 7781 c1 = ip[4] - ip[8]; 7782 d1 = ip[0] - ip[12]; 7784 op[0] = a1 + b1; 7785 op[4] = c1 + d1; 7786 op[8] = a1 - b1; 7787 op[12] = d1 - c1; 7788 ip++; 7789 op++; 7790 } 7792 ip = output; 7793 op = output; 7795 for (i = 0; i < 4; i++) 7796 { 7797 a1 = ip[0] + ip[3]; 7798 b1 = ip[1] + ip[2]; 7799 c1 = ip[1] - ip[2]; 7800 d1 = ip[0] - ip[3]; 7802 a2 = a1 + b1; 7803 b2 = c1 + d1; 7804 c2 = a1 - b1; 7805 d2 = d1 - c1; 7807 op[0] = (a2 + 3) >> 3; 7808 op[1] = (b2 + 3) >> 3; 7809 op[2] = (c2 + 3) >> 3; 7810 op[3] = (d2 + 3) >> 3; 7812 ip += 4; 7813 op += 4; 7814 } 7815 } 7817 #define cospi8sqrt2minus1 20091 7818 #define sinpi8sqrt2 35468 7819 #define rounding 0 7820 static void 7821 idct_columns(const short *input, short *output) 7822 { 7823 int i; 7824 int a1, b1, c1, d1; 7826 const short *ip = input; 7827 short *op = output; 7828 int temp1, temp2; 7829 int shortpitch = 4; 7831 for (i = 0; i < 4; i++) 7832 { 7833 a1 = ip[0] + ip[8]; 7834 b1 = ip[0] - ip[8]; 7836 temp1 = (ip[4] * sinpi8sqrt2 + rounding) >> 16; 7837 temp2 = ip[12] + 7838 ((ip[12] * cospi8sqrt2minus1 + rounding) >> 16); 7839 c1 = temp1 - temp2; 7841 temp1 = ip[4] + 7842 ((ip[4] * cospi8sqrt2minus1 + rounding) >> 16); 7843 temp2 = (ip[12] * sinpi8sqrt2 + rounding) >> 16; 7844 d1 = temp1 + temp2; 7846 op[shortpitch*0] = a1 + d1; 7847 op[shortpitch*3] = a1 - d1; 7849 op[shortpitch*1] = b1 + c1; 7850 op[shortpitch*2] = b1 - c1; 7852 ip++; 7853 op++; 7854 } 7855 } 7857 void 7858 vp8_dixie_idct_add(unsigned char *recon, 7859 const unsigned char *predict, 7860 int stride, 7861 const short *coeffs) 7862 { 7863 int i; 7864 int a1, b1, c1, d1, temp1, temp2; 7865 short tmp[16]; 7866 idct_columns(coeffs, tmp); 7867 coeffs = tmp; 7869 for (i = 0; i < 4; i++) 7870 { 7871 a1 = coeffs[0] + coeffs[2]; 7872 b1 = coeffs[0] - coeffs[2]; 7874 temp1 = (coeffs[1] * sinpi8sqrt2 + rounding) >> 16; 7875 temp2 = coeffs[3] + 7876 ((coeffs[3] * cospi8sqrt2minus1 + rounding) >> 16); 7877 c1 = temp1 - temp2; 7879 temp1 = coeffs[1] + 7880 ((coeffs[1] * cospi8sqrt2minus1 + rounding) >> 16); 7881 temp2 = (coeffs[3] * sinpi8sqrt2 + rounding) >> 16; 7882 d1 = temp1 + temp2; 7884 recon[0] = CLAMP_255(predict[0] + ((a1 + d1 + 4) >> 3)); 7885 recon[3] = CLAMP_255(predict[3] + ((a1 - d1 + 4) >> 3)); 7886 recon[1] = CLAMP_255(predict[1] + ((b1 + c1 + 4) >> 3)); 7887 recon[2] = CLAMP_255(predict[2] + ((b1 - c1 + 4) >> 3)); 7889 coeffs += 4; 7890 recon += stride; 7891 predict += stride; 7892 } 7893 } 7895 ---- End code block ---------------------------------------- 7897 20.9. idct_add.h 7899 ---- Begin code block -------------------------------------- 7901 /* 7902 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7903 * 7904 * Use of this source code is governed by a BSD-style license 7905 * that can be found in the LICENSE file in the root of the source 7906 * tree. An additional intellectual property rights grant can be 7907 * found in the file PATENTS. All contributing project authors may 7908 * be found in the AUTHORS file in the root of the source tree. 7909 */ 7910 #ifndef IDCT_ADD_H 7911 #define IDCT_ADD_H 7913 void 7914 vp8_dixie_idct_add_init(struct vp8_decoder_ctx *ctx); 7916 void 7917 vp8_dixie_idct_add(unsigned char *recon, 7918 const unsigned char *predict, 7919 int stride, 7920 const short *coeffs); 7922 void 7923 vp8_dixie_walsh(const short *in, short *out); 7925 void 7926 vp8_dixie_idct_add_process_row(struct vp8_decoder_ctx *ctx, 7927 short *coeffs, 7928 unsigned int row, 7929 unsigned int start_col, 7930 unsigned int num_cols); 7932 #endif 7934 ---- End code block ---------------------------------------- 7936 20.10. mem.h 7938 ---- Begin code block -------------------------------------- 7940 /* 7941 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7942 * 7943 * Use of this source code is governed by a BSD-style license 7944 * that can be found in the LICENSE file in the root of the source 7945 * tree. An additional intellectual property rights grant can be 7946 * found in the file PATENTS. All contributing project authors may 7947 * be found in the AUTHORS file in the root of the source tree. 7948 */ 7950 #ifndef VPX_PORTS_MEM_H 7951 #define VPX_PORTS_MEM_H 7952 #include "vpx_config.h" 7953 #include "vpx_integer.h" 7955 #if defined(__GNUC__) && __GNUC__ 7956 #define DECLARE_ALIGNED(n,typ,val) typ val __attribute__ \ 7957 ((aligned (n))) 7958 #elif defined(_MSC_VER) 7959 #define DECLARE_ALIGNED(n,typ,val) __declspec(align(n)) typ val 7960 #else 7961 #warning No alignment directives known for this compiler. 7962 #define DECLARE_ALIGNED(n,typ,val) typ val 7963 #endif 7964 #endif 7966 /* Declare an aligned array on the stack, for situations where the 7967 * stack pointer may not have the alignment we expect. Creates an 7968 * array with a modified name, then defines val to be a pointer, and 7969 * aligns that pointer within the array. 7970 */ 7971 #define DECLARE_ALIGNED_ARRAY(a,typ,val,n)\ 7972 typ val##_[(n)+(a)/sizeof(typ)+1];\ 7973 typ *val = (typ*)((((intptr_t)val##_)+(a)-1)&((intptr_t)-(a))) 7975 /* Indicates that the usage of the specified variable has been 7976 * audited to assure that it's safe to use uninitialized. Silences 7977 * 'may be used uninitialized' warnings on gcc. 7978 */ 7979 #if defined(__GNUC__) && __GNUC__ 7980 #define UNINITIALIZED_IS_SAFE(x) x=x 7981 #else 7982 #define UNINITIALIZED_IS_SAFE(x) x 7983 #endif 7984 ---- End code block ---------------------------------------- 7986 20.11. modemv.c 7988 ---- Begin code block -------------------------------------- 7990 /* 7991 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 7992 * 7993 * Use of this source code is governed by a BSD-style license 7994 * that can be found in the LICENSE file in the root of the source 7995 * tree. An additional intellectual property rights grant can be 7996 * found in the file PATENTS. All contributing project authors may 7997 * be found in the AUTHORS file in the root of the source tree. 7998 */ 7999 #include "dixie.h" 8000 #include "modemv_data.h" 8001 #include 8002 #include 8004 struct mv_clamp_rect 8005 { 8006 int to_left, to_right, to_top, to_bottom; 8007 }; 8009 static union mv 8010 clamp_mv(union mv raw, const struct mv_clamp_rect *bounds) 8011 { 8012 union mv newmv; 8014 newmv.d.x = (raw.d.x < bounds->to_left) 8015 ? bounds->to_left : raw.d.x; 8016 newmv.d.x = (raw.d.x > bounds->to_right) 8017 ? bounds->to_right : newmv.d.x; 8018 newmv.d.y = (raw.d.y < bounds->to_top) 8019 ? bounds->to_top : raw.d.y; 8020 newmv.d.y = (raw.d.y > bounds->to_bottom) 8021 ? bounds->to_bottom : newmv.d.y; 8022 return newmv; 8023 } 8025 static int 8026 read_segment_id(struct bool_decoder *bool, 8027 struct vp8_segment_hdr *seg) 8028 { 8029 return bool_get(bool, seg->tree_probs[0]) 8030 ? 2 + bool_get(bool, seg->tree_probs[2]) 8031 : bool_get(bool, seg->tree_probs[1]); 8032 } 8034 static enum prediction_mode 8035 above_block_mode(const struct mb_info *this, 8036 const struct mb_info *above, 8037 unsigned int b) 8038 { 8039 if (b < 4) 8040 { 8041 switch (above->base.y_mode) 8042 { 8043 case DC_PRED: 8044 return B_DC_PRED; 8045 case V_PRED: 8046 return B_VE_PRED; 8047 case H_PRED: 8048 return B_HE_PRED; 8049 case TM_PRED: 8050 return B_TM_PRED; 8051 case B_PRED: 8052 return above->split.modes[b+12]; 8053 default: 8054 assert(0); 8055 } 8056 } 8058 return this->split.modes[b-4]; 8059 } 8061 static enum prediction_mode 8062 left_block_mode(const struct mb_info *this, 8063 const struct mb_info *left, 8064 unsigned int b) 8065 { 8066 if (!(b & 3)) 8067 { 8068 switch (left->base.y_mode) 8069 { 8070 case DC_PRED: 8071 return B_DC_PRED; 8072 case V_PRED: 8073 return B_VE_PRED; 8074 case H_PRED: 8076 return B_HE_PRED; 8077 case TM_PRED: 8078 return B_TM_PRED; 8079 case B_PRED: 8080 return left->split.modes[b+3]; 8081 default: 8082 assert(0); 8083 } 8084 } 8086 return this->split.modes[b-1]; 8087 } 8089 static void 8090 decode_kf_mb_mode(struct mb_info *this, 8091 struct mb_info *left, 8092 struct mb_info *above, 8093 struct bool_decoder *bool) 8094 { 8095 int y_mode, uv_mode; 8097 y_mode = bool_read_tree(bool, kf_y_mode_tree, kf_y_mode_probs); 8099 if (y_mode == B_PRED) 8100 { 8101 unsigned int i; 8103 for (i = 0; i < 16; i++) 8104 { 8105 enum prediction_mode a = above_block_mode(this, above, 8106 i); 8107 enum prediction_mode l = left_block_mode(this, left, i); 8108 enum prediction_mode b; 8110 b = bool_read_tree(bool, b_mode_tree, 8111 kf_b_mode_probs[a][l]); 8112 this->split.modes[i] = b; 8113 } 8114 } 8116 uv_mode = bool_read_tree(bool, uv_mode_tree, kf_uv_mode_probs); 8118 this->base.y_mode = y_mode; 8119 this->base.uv_mode = uv_mode; 8120 this->base.mv.raw = 0; 8121 this->base.ref_frame = 0; 8122 } 8123 static void 8124 decode_intra_mb_mode(struct mb_info *this, 8125 struct vp8_entropy_hdr *hdr, 8126 struct bool_decoder *bool) 8127 { 8128 /* Like decode_kf_mb_mode, but with probabilities transmitted in 8129 * the bitstream and no context on the above/left block mode. 8130 */ 8131 int y_mode, uv_mode; 8133 y_mode = bool_read_tree(bool, y_mode_tree, hdr->y_mode_probs); 8135 if (y_mode == B_PRED) 8136 { 8137 unsigned int i; 8139 for (i = 0; i < 16; i++) 8140 { 8141 enum prediction_mode b; 8143 b = bool_read_tree(bool, b_mode_tree, 8144 default_b_mode_probs); 8145 this->split.modes[i] = b; 8146 } 8147 } 8149 uv_mode = bool_read_tree(bool, uv_mode_tree, hdr->uv_mode_probs); 8151 this->base.y_mode = y_mode; 8152 this->base.uv_mode = uv_mode; 8153 this->base.mv.raw = 0; 8154 this->base.ref_frame = CURRENT_FRAME; 8155 } 8157 static int 8158 read_mv_component(struct bool_decoder *bool, 8159 const unsigned char mvc[MV_PROB_CNT]) 8160 { 8161 enum {IS_SHORT, SIGN, SHORT, BITS = SHORT + 8 - 1, 8162 LONG_WIDTH = 10}; 8163 int x = 0; 8165 if (bool_get(bool, mvc[IS_SHORT])) /* Large */ 8166 { 8167 int i = 0; 8169 for (i = 0; i < 3; i++) 8170 x += bool_get(bool, mvc[BITS + i]) << i; 8172 /* Skip bit 3, which is sometimes implicit */ 8173 for (i = LONG_WIDTH - 1; i > 3; i--) 8174 x += bool_get(bool, mvc[BITS + i]) << i; 8176 if (!(x & 0xFFF0) || bool_get(bool, mvc[BITS + 3])) 8177 x += 8; 8178 } 8179 else /* small */ 8180 x = bool_read_tree(bool, small_mv_tree, mvc + SHORT); 8182 if (x && bool_get(bool, mvc[SIGN])) 8183 x = -x; 8185 return x << 1; 8186 } 8188 static mv_t 8189 above_block_mv(const struct mb_info *this, 8190 const struct mb_info *above, 8191 unsigned int b) 8192 { 8193 if (b < 4) 8194 { 8195 if (above->base.y_mode == SPLITMV) 8196 return above->split.mvs[b+12]; 8198 return above->base.mv; 8199 } 8201 return this->split.mvs[b-4]; 8202 } 8204 static mv_t 8205 left_block_mv(const struct mb_info *this, 8206 const struct mb_info *left, 8207 unsigned int b) 8208 { 8209 if (!(b & 3)) 8210 { 8211 if (left->base.y_mode == SPLITMV) 8212 return left->split.mvs[b+3]; 8214 return left->base.mv; 8215 } 8216 return this->split.mvs[b-1]; 8217 } 8219 static enum prediction_mode 8220 submv_ref(struct bool_decoder *bool, union mv l, union mv a) 8221 { 8222 enum subblock_mv_ref 8223 { 8224 SUBMVREF_NORMAL, 8225 SUBMVREF_LEFT_ZED, 8226 SUBMVREF_ABOVE_ZED, 8227 SUBMVREF_LEFT_ABOVE_SAME, 8228 SUBMVREF_LEFT_ABOVE_ZED 8229 }; 8231 int lez = !(l.raw); 8232 int aez = !(a.raw); 8233 int lea = l.raw == a.raw; 8234 enum subblock_mv_ref ctx = SUBMVREF_NORMAL; 8236 if (lea && lez) 8237 ctx = SUBMVREF_LEFT_ABOVE_ZED; 8238 else if (lea) 8239 ctx = SUBMVREF_LEFT_ABOVE_SAME; 8240 else if (aez) 8241 ctx = SUBMVREF_ABOVE_ZED; 8242 else if (lez) 8243 ctx = SUBMVREF_LEFT_ZED; 8245 return bool_read_tree(bool, submv_ref_tree, 8246 submv_ref_probs2[ctx]); 8247 } 8249 static void 8250 read_mv(struct bool_decoder *bool, 8251 union mv *mv, 8252 mv_component_probs_t mvc[2]) 8253 { 8254 mv->d.y = read_mv_component(bool, mvc[0]); 8255 mv->d.x = read_mv_component(bool, mvc[1]); 8256 } 8258 static void 8259 mv_bias(const struct mb_info *mb, 8260 const unsigned int sign_bias[3], 8261 enum reference_frame ref_frame, 8262 union mv *mv) 8263 { 8264 if (sign_bias[mb->base.ref_frame] ^ sign_bias[ref_frame]) 8265 { 8266 mv->d.x *= -1; 8267 mv->d.y *= -1; 8268 } 8269 } 8271 enum near_mv_v 8272 { 8273 CNT_BEST = 0, 8274 CNT_ZEROZERO = 0, 8275 CNT_NEAREST, 8276 CNT_NEAR, 8277 CNT_SPLITMV 8278 }; 8280 static void 8281 find_near_mvs(const struct mb_info *this, 8282 const struct mb_info *left, 8283 const struct mb_info *above, 8284 const unsigned int sign_bias[3], 8285 union mv near_mvs[4], 8286 int cnt[4]) 8287 { 8288 const struct mb_info *aboveleft = above - 1; 8289 union mv *mv = near_mvs; 8290 int *cntx = cnt; 8292 /* Zero accumulators */ 8293 mv[0].raw = mv[1].raw = mv[2].raw = 0; 8294 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0; 8296 /* Process above */ 8297 if (above->base.ref_frame != CURRENT_FRAME) 8298 { 8299 if (above->base.mv.raw) 8300 { 8301 (++mv)->raw = above->base.mv.raw; 8302 mv_bias(above, sign_bias, this->base.ref_frame, mv); 8303 ++cntx; 8304 } 8306 *cntx += 2; 8308 } 8310 /* Process left */ 8311 if (left->base.ref_frame != CURRENT_FRAME) 8312 { 8313 if (left->base.mv.raw) 8314 { 8315 union mv this_mv; 8317 this_mv.raw = left->base.mv.raw; 8318 mv_bias(left, sign_bias, this->base.ref_frame, &this_mv); 8320 if (this_mv.raw != mv->raw) 8321 { 8322 (++mv)->raw = this_mv.raw; 8323 ++cntx; 8324 } 8326 *cntx += 2; 8327 } 8328 else 8329 cnt[CNT_ZEROZERO] += 2; 8330 } 8332 /* Process above left */ 8333 if (aboveleft->base.ref_frame != CURRENT_FRAME) 8334 { 8335 if (aboveleft->base.mv.raw) 8336 { 8337 union mv this_mv; 8339 this_mv.raw = aboveleft->base.mv.raw; 8340 mv_bias(aboveleft, sign_bias, this->base.ref_frame, 8341 &this_mv); 8343 if (this_mv.raw != mv->raw) 8344 { 8345 (++mv)->raw = this_mv.raw; 8346 ++cntx; 8347 } 8349 *cntx += 1; 8350 } 8351 else 8352 cnt[CNT_ZEROZERO] += 1; 8353 } 8355 /* If we have three distinct MV's ... */ 8356 if (cnt[CNT_SPLITMV]) 8357 { 8358 /* See if above-left MV can be merged with NEAREST */ 8359 if (mv->raw == near_mvs[CNT_NEAREST].raw) 8360 cnt[CNT_NEAREST] += 1; 8361 } 8363 cnt[CNT_SPLITMV] = ((above->base.y_mode == SPLITMV) 8364 + (left->base.y_mode == SPLITMV)) * 2 8365 + (aboveleft->base.y_mode == SPLITMV); 8367 /* Swap near and nearest if necessary */ 8368 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) 8369 { 8370 int tmp; 8371 tmp = cnt[CNT_NEAREST]; 8372 cnt[CNT_NEAREST] = cnt[CNT_NEAR]; 8373 cnt[CNT_NEAR] = tmp; 8374 tmp = near_mvs[CNT_NEAREST].raw; 8375 near_mvs[CNT_NEAREST].raw = near_mvs[CNT_NEAR].raw; 8376 near_mvs[CNT_NEAR].raw = tmp; 8377 } 8379 /* Use near_mvs[CNT_BEST] to store the "best" MV. Note that this 8380 * storage shares the same address as near_mvs[CNT_ZEROZERO]. 8381 */ 8382 if (cnt[CNT_NEAREST] >= cnt[CNT_BEST]) 8383 near_mvs[CNT_BEST] = near_mvs[CNT_NEAREST]; 8384 } 8386 static void 8387 decode_split_mv(struct mb_info *this, 8388 const struct mb_info *left, 8389 const struct mb_info *above, 8390 struct vp8_entropy_hdr *hdr, 8391 union mv *best_mv, 8392 struct bool_decoder *bool) 8393 { 8394 const int *partition; 8395 int j, k, mask, partition_id; 8397 partition_id = bool_read_tree(bool, split_mv_tree, 8398 split_mv_probs); 8399 partition = mv_partitions[partition_id]; 8400 this->base.partitioning = partition_id; 8402 for (j = 0, mask = 0; mask < 65535; j++) 8403 { 8404 union mv mv, left_mv, above_mv; 8405 enum prediction_mode subblock_mode; 8407 /* Find the first subblock in this partition. */ 8408 for (k = 0; j != partition[k]; k++); 8410 /* Decode the next MV */ 8411 left_mv = left_block_mv(this, left, k); 8412 above_mv = above_block_mv(this, above, k); 8413 subblock_mode = submv_ref(bool, left_mv, above_mv); 8415 switch (subblock_mode) 8416 { 8417 case LEFT4X4: 8418 mv = left_mv; 8419 break; 8420 case ABOVE4X4: 8421 mv = above_mv; 8422 break; 8423 case ZERO4X4: 8424 mv.raw = 0; 8425 break; 8426 case NEW4X4: 8427 read_mv(bool, &mv, hdr->mv_probs); 8428 mv.d.x += best_mv->d.x; 8429 mv.d.y += best_mv->d.y; 8430 break; 8431 default: 8432 assert(0); 8433 } 8435 /* Fill the MV's for this partition */ 8436 for (; k < 16; k++) 8437 if (j == partition[k]) 8438 { 8439 this->split.mvs[k] = mv; 8440 mask |= 1 << k; 8441 } 8442 } 8443 } 8445 static int 8446 need_mc_border(union mv mv, int l, int t, int b_w, int w, int h) 8447 { 8448 int b, r; 8449 /* Get distance to edge for top-left pixel */ 8450 l += (mv.d.x >> 3); 8451 t += (mv.d.y >> 3); 8453 /* Get distance to edge for bottom-right pixel */ 8454 r = w - (l + b_w); 8455 b = h - (t + b_w); 8457 return (l >> 1 < 2 || r >> 1 < 3 || t >> 1 < 2 || b >> 1 < 3); 8458 } 8460 static void 8461 decode_mvs(struct vp8_decoder_ctx *ctx, 8462 struct mb_info *this, 8463 const struct mb_info *left, 8464 const struct mb_info *above, 8465 const struct mv_clamp_rect *bounds, 8466 struct bool_decoder *bool) 8467 { 8468 struct vp8_entropy_hdr *hdr = &ctx->entropy_hdr; 8469 union mv near_mvs[4]; 8470 union mv clamped_best_mv; 8471 int mv_cnts[4]; 8472 unsigned char probs[4]; 8473 enum {BEST, NEAREST, NEAR}; 8474 int x, y, w, h, b; 8476 this->base.ref_frame = bool_get(bool, hdr->prob_last) 8477 ? 2 + bool_get(bool, hdr->prob_gf) 8478 : 1; 8480 find_near_mvs(this, this - 1, above, 8481 ctx->reference_hdr.sign_bias, near_mvs, mv_cnts); 8482 probs[0] = mv_counts_to_probs[mv_cnts[0]][0]; 8483 probs[1] = mv_counts_to_probs[mv_cnts[1]][1]; 8484 probs[2] = mv_counts_to_probs[mv_cnts[2]][2]; 8485 probs[3] = mv_counts_to_probs[mv_cnts[3]][3]; 8487 this->base.y_mode = bool_read_tree(bool, mv_ref_tree, probs); 8488 this->base.uv_mode = this->base.y_mode; 8490 this->base.need_mc_border = 0; 8491 x = (-bounds->to_left - 128) >> 3; 8492 y = (-bounds->to_top - 128) >> 3; 8493 w = ctx->mb_cols * 16; 8494 h = ctx->mb_rows * 16; 8496 switch (this->base.y_mode) 8497 { 8498 case NEARESTMV: 8499 this->base.mv = clamp_mv(near_mvs[NEAREST], bounds); 8500 break; 8501 case NEARMV: 8502 this->base.mv = clamp_mv(near_mvs[NEAR], bounds); 8503 break; 8504 case ZEROMV: 8505 this->base.mv.raw = 0; 8506 return; //skip need_mc_border check 8507 case NEWMV: 8508 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8509 read_mv(bool, &this->base.mv, hdr->mv_probs); 8510 this->base.mv.d.x += clamped_best_mv.d.x; 8511 this->base.mv.d.y += clamped_best_mv.d.y; 8512 break; 8513 case SPLITMV: 8514 { 8515 union mv chroma_mv[4] = {{{0}}}; 8517 clamped_best_mv = clamp_mv(near_mvs[BEST], bounds); 8518 decode_split_mv(this, left, above, hdr, &clamped_best_mv, 8519 bool); 8520 this->base.mv = this->split.mvs[15]; 8522 for (b = 0; b < 16; b++) 8523 { 8524 chroma_mv[(b>>1&1) + (b>>2&2)].d.x += 8525 this->split.mvs[b].d.x; 8526 chroma_mv[(b>>1&1) + (b>>2&2)].d.y += 8527 this->split.mvs[b].d.y; 8529 if (need_mc_border(this->split.mvs[b], 8530 x + (b & 3) * 4, y + (b & ~3), 4, w, h)) 8531 { 8532 this->base.need_mc_border = 1; 8533 break; 8534 } 8535 } 8537 for (b = 0; b < 4; b++) 8538 { 8539 chroma_mv[b].d.x += 4 + 8 * (chroma_mv[b].d.x >> 31); 8540 chroma_mv[b].d.y += 4 + 8 * (chroma_mv[b].d.y >> 31); 8541 chroma_mv[b].d.x /= 4; 8542 chroma_mv[b].d.y /= 4; 8544 //note we're passing in non-subsampled coordinates 8545 if (need_mc_border(chroma_mv[b], 8546 x + (b & 1) * 8, y + (b >> 1) * 8, 16, w, h)) 8547 { 8548 this->base.need_mc_border = 1; 8549 break; 8550 } 8551 } 8553 return; //skip need_mc_border check 8554 } 8555 default: 8556 assert(0); 8557 } 8559 if (need_mc_border(this->base.mv, x, y, 16, w, h)) 8560 this->base.need_mc_border = 1; 8561 } 8563 void 8564 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8565 struct bool_decoder *bool, 8566 int row, 8567 int start_col, 8568 int num_cols) 8569 { 8570 struct mb_info *above, *this; 8571 unsigned int col; 8572 struct mv_clamp_rect bounds; 8574 this = ctx->mb_info_rows[row] + start_col; 8575 above = ctx->mb_info_rows[row - 1] + start_col; 8577 /* Calculate the eighth-pel MV bounds using a 1 MB border. */ 8578 bounds.to_left = -((start_col + 1) << 7); 8579 bounds.to_right = (ctx->mb_cols - start_col) << 7; 8580 bounds.to_top = -((row + 1) << 7); 8581 bounds.to_bottom = (ctx->mb_rows - row) << 7; 8583 for (col = start_col; col < start_col + num_cols; col++) 8584 { 8585 if (ctx->segment_hdr.update_map) 8586 this->base.segment_id = read_segment_id(bool, 8587 &ctx->segment_hdr); 8589 if (ctx->entropy_hdr.coeff_skip_enabled) 8590 this->base.skip_coeff = bool_get(bool, 8591 ctx->entropy_hdr.coeff_skip_prob); 8593 if (ctx->frame_hdr.is_keyframe) 8594 { 8595 if (!ctx->segment_hdr.update_map) 8596 this->base.segment_id = 0; 8598 decode_kf_mb_mode(this, this - 1, above, bool); 8599 } 8600 else 8601 { 8602 if (bool_get(bool, ctx->entropy_hdr.prob_inter)) 8603 decode_mvs(ctx, this, this - 1, above, &bounds, 8604 bool); 8605 else 8606 decode_intra_mb_mode(this, &ctx->entropy_hdr, bool); 8608 bounds.to_left -= 16 << 3; 8609 bounds.to_right -= 16 << 3; 8610 } 8612 /* Advance to next mb */ 8613 this++; 8614 above++; 8615 } 8616 } 8618 void 8619 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx) 8620 { 8621 unsigned int mbi_w, mbi_h, i; 8622 struct mb_info *mbi; 8624 mbi_w = ctx->mb_cols + 1; /* For left border col */ 8625 mbi_h = ctx->mb_rows + 1; /* For above border row */ 8627 if (ctx->frame_hdr.frame_size_updated) 8628 { 8629 free(ctx->mb_info_storage); 8630 ctx->mb_info_storage = NULL; 8631 free(ctx->mb_info_rows_storage); 8632 ctx->mb_info_rows_storage = NULL; 8633 } 8635 if (!ctx->mb_info_storage) 8636 ctx->mb_info_storage = calloc(mbi_w * mbi_h, 8637 sizeof(*ctx->mb_info_storage)); 8639 if (!ctx->mb_info_rows_storage) 8640 ctx->mb_info_rows_storage = calloc(mbi_h, 8641 sizeof(*ctx->mb_info_rows_storage)); 8643 /* Set up row pointers */ 8644 mbi = ctx->mb_info_storage + 1; 8646 for (i = 0; i < mbi_h; i++) 8647 { 8648 ctx->mb_info_rows_storage[i] = mbi; 8649 mbi += mbi_w; 8650 } 8652 ctx->mb_info_rows = ctx->mb_info_rows_storage + 1; 8653 } 8655 void 8656 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx) 8657 { 8658 free(ctx->mb_info_storage); 8659 ctx->mb_info_storage = NULL; 8660 free(ctx->mb_info_rows_storage); 8661 ctx->mb_info_rows_storage = NULL; 8662 } 8664 ---- End code block ---------------------------------------- 8666 20.12. modemv.h 8668 ---- Begin code block -------------------------------------- 8670 /* 8671 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8672 * 8673 * Use of this source code is governed by a BSD-style license 8674 * that can be found in the LICENSE file in the root of the source 8675 * tree. An additional intellectual property rights grant can be 8676 * found in the file PATENTS. All contributing project authors may 8677 * be found in the AUTHORS file in the root of the source tree. 8678 */ 8679 #ifndef MODEMV_H 8680 #define MODEMV_H 8682 void 8683 vp8_dixie_modemv_init(struct vp8_decoder_ctx *ctx); 8685 void 8686 vp8_dixie_modemv_destroy(struct vp8_decoder_ctx *ctx); 8688 void 8689 vp8_dixie_modemv_process_row(struct vp8_decoder_ctx *ctx, 8690 struct bool_decoder *bool, 8691 int row, 8692 int start_col, 8693 int num_cols); 8695 #endif 8697 ---- End code block ---------------------------------------- 8699 20.13. modemv_data.h 8701 ---- Begin code block -------------------------------------- 8703 static const unsigned char kf_y_mode_probs[] = { 145, 156, 163, 128}; 8704 static const unsigned char kf_uv_mode_probs[] = { 142, 114, 183}; 8705 static const unsigned char kf_b_mode_probs[10][10][9] = 8706 { 8707 { /* above mode 0 */ 8708 { /* left mode 0 */ 231, 120, 48, 89, 115, 113, 120, 152, 112}, 8709 { /* left mode 1 */ 152, 179, 64, 126, 170, 118, 46, 70, 95}, 8710 { /* left mode 2 */ 175, 69, 143, 80, 85, 82, 72, 155, 103}, 8711 { /* left mode 3 */ 56, 58, 10, 171, 218, 189, 17, 13, 152}, 8712 { /* left mode 4 */ 144, 71, 10, 38, 171, 213, 144, 34, 26}, 8713 { /* left mode 5 */ 114, 26, 17, 163, 44, 195, 21, 10, 173}, 8714 { /* left mode 6 */ 121, 24, 80, 195, 26, 62, 44, 64, 85}, 8715 { /* left mode 7 */ 170, 46, 55, 19, 136, 160, 33, 206, 71}, 8716 { /* left mode 8 */ 63, 20, 8, 114, 114, 208, 12, 9, 226}, 8717 { /* left mode 9 */ 81, 40, 11, 96, 182, 84, 29, 16, 36} 8718 }, 8719 { /* above mode 1 */ 8720 { /* left mode 0 */ 134, 183, 89, 137, 98, 101, 106, 165, 148}, 8721 { /* left mode 1 */ 72, 187, 100, 130, 157, 111, 32, 75, 80}, 8722 { /* left mode 2 */ 66, 102, 167, 99, 74, 62, 40, 234, 128}, 8723 { /* left mode 3 */ 41, 53, 9, 178, 241, 141, 26, 8, 107}, 8724 { /* left mode 4 */ 104, 79, 12, 27, 217, 255, 87, 17, 7}, 8725 { /* left mode 5 */ 74, 43, 26, 146, 73, 166, 49, 23, 157}, 8726 { /* left mode 6 */ 65, 38, 105, 160, 51, 52, 31, 115, 128}, 8727 { /* left mode 7 */ 87, 68, 71, 44, 114, 51, 15, 186, 23}, 8728 { /* left mode 8 */ 47, 41, 14, 110, 182, 183, 21, 17, 194}, 8729 { /* left mode 9 */ 66, 45, 25, 102, 197, 189, 23, 18, 22} 8730 }, 8731 { /* above mode 2 */ 8732 { /* left mode 0 */ 88, 88, 147, 150, 42, 46, 45, 196, 205}, 8733 { /* left mode 1 */ 43, 97, 183, 117, 85, 38, 35, 179, 61}, 8734 { /* left mode 2 */ 39, 53, 200, 87, 26, 21, 43, 232, 171}, 8735 { /* left mode 3 */ 56, 34, 51, 104, 114, 102, 29, 93, 77}, 8736 { /* left mode 4 */ 107, 54, 32, 26, 51, 1, 81, 43, 31}, 8737 { /* left mode 5 */ 39, 28, 85, 171, 58, 165, 90, 98, 64}, 8738 { /* left mode 6 */ 34, 22, 116, 206, 23, 34, 43, 166, 73}, 8739 { /* left mode 7 */ 68, 25, 106, 22, 64, 171, 36, 225, 114}, 8740 { /* left mode 8 */ 34, 19, 21, 102, 132, 188, 16, 76, 124}, 8741 { /* left mode 9 */ 62, 18, 78, 95, 85, 57, 50, 48, 51} 8742 }, 8743 { /* above mode 3 */ 8744 { /* left mode 0 */ 193, 101, 35, 159, 215, 111, 89, 46, 111}, 8745 { /* left mode 1 */ 60, 148, 31, 172, 219, 228, 21, 18, 111}, 8746 { /* left mode 2 */ 112, 113, 77, 85, 179, 255, 38, 120, 114}, 8747 { /* left mode 3 */ 40, 42, 1, 196, 245, 209, 10, 25, 109}, 8748 { /* left mode 4 */ 100, 80, 8, 43, 154, 1, 51, 26, 71}, 8749 { /* left mode 5 */ 88, 43, 29, 140, 166, 213, 37, 43, 154}, 8750 { /* left mode 6 */ 61, 63, 30, 155, 67, 45, 68, 1, 209}, 8751 { /* left mode 7 */ 142, 78, 78, 16, 255, 128, 34, 197, 171}, 8752 { /* left mode 8 */ 41, 40, 5, 102, 211, 183, 4, 1, 221}, 8753 { /* left mode 9 */ 51, 50, 17, 168, 209, 192, 23, 25, 82} 8754 }, 8755 { /* above mode 4 */ 8756 { /* left mode 0 */ 125, 98, 42, 88, 104, 85, 117, 175, 82}, 8757 { /* left mode 1 */ 95, 84, 53, 89, 128, 100, 113, 101, 45}, 8758 { /* left mode 2 */ 75, 79, 123, 47, 51, 128, 81, 171, 1}, 8759 { /* left mode 3 */ 57, 17, 5, 71, 102, 57, 53, 41, 49}, 8760 { /* left mode 4 */ 115, 21, 2, 10, 102, 255, 166, 23, 6}, 8761 { /* left mode 5 */ 38, 33, 13, 121, 57, 73, 26, 1, 85}, 8762 { /* left mode 6 */ 41, 10, 67, 138, 77, 110, 90, 47, 114}, 8763 { /* left mode 7 */ 101, 29, 16, 10, 85, 128, 101, 196, 26}, 8764 { /* left mode 8 */ 57, 18, 10, 102, 102, 213, 34, 20, 43}, 8765 { /* left mode 9 */ 117, 20, 15, 36, 163, 128, 68, 1, 26} 8766 }, 8767 { /* above mode 5 */ 8768 { /* left mode 0 */ 138, 31, 36, 171, 27, 166, 38, 44, 229}, 8769 { /* left mode 1 */ 67, 87, 58, 169, 82, 115, 26, 59, 179}, 8770 { /* left mode 2 */ 63, 59, 90, 180, 59, 166, 93, 73, 154}, 8771 { /* left mode 3 */ 40, 40, 21, 116, 143, 209, 34, 39, 175}, 8772 { /* left mode 4 */ 57, 46, 22, 24, 128, 1, 54, 17, 37}, 8773 { /* left mode 5 */ 47, 15, 16, 183, 34, 223, 49, 45, 183}, 8774 { /* left mode 6 */ 46, 17, 33, 183, 6, 98, 15, 32, 183}, 8775 { /* left mode 7 */ 65, 32, 73, 115, 28, 128, 23, 128, 205}, 8776 { /* left mode 8 */ 40, 3, 9, 115, 51, 192, 18, 6, 223}, 8777 { /* left mode 9 */ 87, 37, 9, 115, 59, 77, 64, 21, 47} 8778 }, 8779 { /* above mode 6 */ 8780 { /* left mode 0 */ 104, 55, 44, 218, 9, 54, 53, 130, 226}, 8781 { /* left mode 1 */ 64, 90, 70, 205, 40, 41, 23, 26, 57}, 8782 { /* left mode 2 */ 54, 57, 112, 184, 5, 41, 38, 166, 213}, 8783 { /* left mode 3 */ 30, 34, 26, 133, 152, 116, 10, 32, 134}, 8784 { /* left mode 4 */ 75, 32, 12, 51, 192, 255, 160, 43, 51}, 8785 { /* left mode 5 */ 39, 19, 53, 221, 26, 114, 32, 73, 255}, 8786 { /* left mode 6 */ 31, 9, 65, 234, 2, 15, 1, 118, 73}, 8787 { /* left mode 7 */ 88, 31, 35, 67, 102, 85, 55, 186, 85}, 8788 { /* left mode 8 */ 56, 21, 23, 111, 59, 205, 45, 37, 192}, 8789 { /* left mode 9 */ 55, 38, 70, 124, 73, 102, 1, 34, 98} 8790 }, 8791 { /* above mode 7 */ 8792 { /* left mode 0 */ 102, 61, 71, 37, 34, 53, 31, 243, 192}, 8793 { /* left mode 1 */ 69, 60, 71, 38, 73, 119, 28, 222, 37}, 8794 { /* left mode 2 */ 68, 45, 128, 34, 1, 47, 11, 245, 171}, 8795 { /* left mode 3 */ 62, 17, 19, 70, 146, 85, 55, 62, 70}, 8796 { /* left mode 4 */ 75, 15, 9, 9, 64, 255, 184, 119, 16}, 8797 { /* left mode 5 */ 37, 43, 37, 154, 100, 163, 85, 160, 1}, 8798 { /* left mode 6 */ 63, 9, 92, 136, 28, 64, 32, 201, 85}, 8799 { /* left mode 7 */ 86, 6, 28, 5, 64, 255, 25, 248, 1}, 8800 { /* left mode 8 */ 56, 8, 17, 132, 137, 255, 55, 116, 128}, 8801 { /* left mode 9 */ 58, 15, 20, 82, 135, 57, 26, 121, 40} 8802 }, 8803 { /* above mode 8 */ 8804 { /* left mode 0 */ 164, 50, 31, 137, 154, 133, 25, 35, 218}, 8805 { /* left mode 1 */ 51, 103, 44, 131, 131, 123, 31, 6, 158}, 8806 { /* left mode 2 */ 86, 40, 64, 135, 148, 224, 45, 183, 128}, 8807 { /* left mode 3 */ 22, 26, 17, 131, 240, 154, 14, 1, 209}, 8808 { /* left mode 4 */ 83, 12, 13, 54, 192, 255, 68, 47, 28}, 8809 { /* left mode 5 */ 45, 16, 21, 91, 64, 222, 7, 1, 197}, 8810 { /* left mode 6 */ 56, 21, 39, 155, 60, 138, 23, 102, 213}, 8811 { /* left mode 7 */ 85, 26, 85, 85, 128, 128, 32, 146, 171}, 8812 { /* left mode 8 */ 18, 11, 7, 63, 144, 171, 4, 4, 246}, 8813 { /* left mode 9 */ 35, 27, 10, 146, 174, 171, 12, 26, 128} 8814 }, 8815 { /* above mode 9 */ 8816 { /* left mode 0 */ 190, 80, 35, 99, 180, 80, 126, 54, 45}, 8817 { /* left mode 1 */ 85, 126, 47, 87, 176, 51, 41, 20, 32}, 8818 { /* left mode 2 */ 101, 75, 128, 139, 118, 146, 116, 128, 85}, 8819 { /* left mode 3 */ 56, 41, 15, 176, 236, 85, 37, 9, 62}, 8820 { /* left mode 4 */ 146, 36, 19, 30, 171, 255, 97, 27, 20}, 8821 { /* left mode 5 */ 71, 30, 17, 119, 118, 255, 17, 18, 138}, 8822 { /* left mode 6 */ 101, 38, 60, 138, 55, 70, 43, 26, 142}, 8823 { /* left mode 7 */ 138, 45, 61, 62, 219, 1, 81, 188, 64}, 8824 { /* left mode 8 */ 32, 41, 20, 117, 151, 142, 20, 21, 163}, 8825 { /* left mode 9 */ 112, 19, 12, 61, 195, 128, 48, 4, 24} 8826 } 8827 }; 8828 static const int kf_y_mode_tree[] = 8829 { 8830 -B_PRED, 2, 8831 4, 6, 8832 -DC_PRED, -V_PRED, 8833 -H_PRED, -TM_PRED 8834 }; 8835 static const int y_mode_tree[] = 8836 { 8837 -DC_PRED, 2, 8838 4, 6, 8839 -V_PRED, -H_PRED, 8840 -TM_PRED, -B_PRED 8841 }; 8842 static const int uv_mode_tree[6] = 8843 { 8844 -DC_PRED, 2, 8845 -V_PRED, 4, 8846 -H_PRED, -TM_PRED 8847 }; 8848 static const int b_mode_tree[18] = 8849 { 8850 -B_DC_PRED, 2, /* 0 = DC_NODE */ 8851 -B_TM_PRED, 4, /* 1 = TM_NODE */ 8852 -B_VE_PRED, 6, /* 2 = VE_NODE */ 8853 8, 12, /* 3 = COM_NODE */ 8854 -B_HE_PRED, 10, /* 4 = HE_NODE */ 8855 -B_RD_PRED, -B_VR_PRED, /* 5 = RD_NODE */ 8856 -B_LD_PRED, 14, /* 6 = LD_NODE */ 8857 -B_VL_PRED, 16, /* 7 = VL_NODE */ 8858 -B_HD_PRED, -B_HU_PRED /* 8 = HD_NODE */ 8859 }; 8860 static const int small_mv_tree[14] = 8861 { 8862 2, 8, 8863 4, 6, 8864 -0, -1, 8865 -2, -3, 8866 10, 12, 8867 -4, -5, 8868 -6, -7 8869 }; 8870 static const int mv_ref_tree[8] = 8871 { 8872 -ZEROMV, 2, 8873 -NEARESTMV, 4, 8874 -NEARMV, 6, 8875 -NEWMV, -SPLITMV 8876 }; 8877 static const int submv_ref_tree[6] = 8878 { 8879 -LEFT4X4, 2, 8880 -ABOVE4X4, 4, 8881 -ZERO4X4, -NEW4X4 8882 }; 8883 static const int split_mv_tree[6] = 8884 { 8885 -3, 2, 8886 -2, 4, 8887 -0, -1 8888 }; 8889 static const unsigned char default_b_mode_probs[] = 8890 { 120, 90, 79, 133, 87, 85, 80, 111, 151}; 8891 static const unsigned char mv_counts_to_probs[6][4] = 8892 { 8893 { 7, 1, 1, 143 }, 8894 { 14, 18, 14, 107 }, 8895 { 135, 64, 57, 68 }, 8896 { 60, 56, 128, 65 }, 8897 { 159, 134, 128, 34 }, 8898 { 234, 188, 128, 28 } 8900 }; 8901 static const unsigned char split_mv_probs[3] = 8902 { 110, 111, 150}; 8903 static const unsigned char submv_ref_probs2[5][3] = 8904 { 8905 { 147, 136, 18 }, 8906 { 106, 145, 1 }, 8907 { 179, 121, 1 }, 8908 { 223, 1, 34 }, 8909 { 208, 1, 1 } 8910 }; 8912 const static int mv_partitions[4][16] = 8913 { 8914 {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, 8915 {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1 }, 8916 {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3 }, 8917 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } 8918 }; 8920 ---- End code block ---------------------------------------- 8922 20.14. predict.c 8924 ---- Begin code block -------------------------------------- 8926 /* 8927 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 8928 * 8929 * Use of this source code is governed by a BSD-style license 8930 * that can be found in the LICENSE file in the root of the source 8931 * tree. An additional intellectual property rights grant can be 8932 * found in the file PATENTS. All contributing project authors may 8933 * be found in the AUTHORS file in the root of the source tree. 8934 */ 8935 #include "dixie.h" 8936 #include "predict.h" 8937 #include "idct_add.h" 8938 #include "mem.h" 8939 #include 8940 #include 8942 enum 8943 { 8944 BORDER_PIXELS = 16, 8945 }; 8947 static const filter_t sixtap_filters[8] = 8948 { 8949 { 0, 0, 128, 0, 0, 0 }, 8950 { 0, -6, 123, 12, -1, 0 }, 8951 { 2, -11, 108, 36, -8, 1 }, 8952 { 0, -9, 93, 50, -6, 0 }, 8953 { 3, -16, 77, 77, -16, 3 }, 8954 { 0, -6, 50, 93, -9, 0 }, 8955 { 1, -8, 36, 108, -11, 2 }, 8956 { 0, -1, 12, 123, -6, 0 }, 8957 }; 8959 static const filter_t bilinear_filters[8] = 8960 { 8962 { 0, 0, 128, 0, 0, 0 }, 8963 { 0, 0, 112, 16, 0, 0 }, 8964 { 0, 0, 96, 32, 0, 0 }, 8965 { 0, 0, 80, 48, 0, 0 }, 8966 { 0, 0, 64, 64, 0, 0 }, 8967 { 0, 0, 48, 80, 0, 0 }, 8968 { 0, 0, 32, 96, 0, 0 }, 8969 { 0, 0, 16, 112, 0, 0 } 8970 }; 8972 static void 8973 predict_h_nxn(unsigned char *predict, 8974 int stride, 8975 int n) 8976 { 8977 unsigned char *left = predict - 1; 8978 int i, j; 8980 for (i = 0; i < n; i++) 8981 for (j = 0; j < n; j++) 8982 predict[i *stride + j] = left[i * stride]; 8983 } 8985 static void 8986 predict_v_nxn(unsigned char *predict, 8987 int stride, 8988 int n) 8989 { 8990 unsigned char *above = predict - stride; 8991 int i, j; 8993 for (i = 0; i < n; i++) 8994 for (j = 0; j < n; j++) 8995 predict[i *stride + j] = above[j]; 8996 } 8998 static void 8999 predict_tm_nxn(unsigned char *predict, 9000 int stride, 9001 int n) 9002 { 9003 /* Transposes the left column to the top row for later 9004 * consumption by the idct/recon stage 9005 */ 9006 unsigned char *left = predict - 1; 9007 unsigned char *above = predict - stride; 9008 unsigned char p = above[-1]; 9009 int i, j; 9011 for (j = 0; j < n; j++) 9012 { 9013 for (i = 0; i < n; i++) 9014 predict[i] = CLAMP_255(*left + above[i] - p); 9016 predict += stride; 9017 left += stride; 9018 } 9019 } 9021 static void 9022 predict_dc_nxn(unsigned char *predict, 9023 int stride, 9024 int n) 9025 { 9026 unsigned char *left = predict - 1; 9027 unsigned char *above = predict - stride; 9028 int i, j, dc = 0; 9030 for (i = 0; i < n; i++) 9031 { 9032 dc += *left + above[i]; 9033 left += stride; 9034 } 9036 switch (n) 9037 { 9038 case 16: 9039 dc = (dc + 16) >> 5; 9040 break; 9042 case 8: 9043 dc = (dc + 8) >> 4; 9044 break; 9045 case 4: 9046 dc = (dc + 4) >> 3; 9047 break; 9048 } 9050 for (i = 0; i < n; i++) 9051 for (j = 0; j < n; j++) 9052 predict[i *stride + j] = dc; 9053 } 9055 static void 9056 predict_ve_4x4(unsigned char *predict, 9057 int stride) 9058 { 9059 unsigned char *above = predict - stride; 9060 int i, j; 9062 predict[0] = (above[-1] + 2 * above[0] + above[1] + 2) >> 2; 9063 predict[1] = (above[ 0] + 2 * above[1] + above[2] + 2) >> 2; 9064 predict[2] = (above[ 1] + 2 * above[2] + above[3] + 2) >> 2; 9065 predict[3] = (above[ 2] + 2 * above[3] + above[4] + 2) >> 2; 9067 for (i = 1; i < 4; i++) 9068 for (j = 0; j < 4; j++) 9069 predict[i *stride + j] = predict[j]; 9070 } 9072 static void 9073 predict_he_4x4(unsigned char *predict, 9074 int stride) 9075 { 9076 unsigned char *left = predict - 1; 9078 predict[0] = 9079 predict[1] = 9080 predict[2] = 9081 predict[3] = (left[-stride] + 2 * left[0] + 9082 left[stride] + 2) >> 2; 9083 predict += stride; 9084 left += stride; 9086 predict[0] = 9087 predict[1] = 9088 predict[2] = 9089 predict[3] = (left[-stride] + 2 * left[0] + 9090 left[stride] + 2) >> 2; 9091 predict += stride; 9092 left += stride; 9094 predict[0] = 9095 predict[1] = 9096 predict[2] = 9097 predict[3] = (left[-stride] + 2 * left[0] + 9098 left[stride] + 2) >> 2; 9099 predict += stride; 9100 left += stride; 9102 predict[0] = 9103 predict[1] = 9104 predict[2] = 9105 predict[3] = (left[-stride] + 2 * left[0] + left[0] + 2) >> 2; 9106 } 9108 static void 9109 predict_ld_4x4(unsigned char *predict, 9110 int stride) 9111 { 9112 unsigned char *above = predict - stride; 9113 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9115 predict[0] = pred0 = (above[0] + 2 * above[1] + 9116 above[2] + 2) >> 2; 9117 predict[1] = pred1 = (above[1] + 2 * above[2] + 9118 above[3] + 2) >> 2; 9119 predict[2] = pred2 = (above[2] + 2 * above[3] + 9120 above[4] + 2) >> 2; 9121 predict[3] = pred3 = (above[3] + 2 * above[4] + 9122 above[5] + 2) >> 2; 9123 predict += stride; 9125 predict[0] = pred1; 9126 predict[1] = pred2; 9127 predict[2] = pred3; 9128 predict[3] = pred4 = (above[4] + 2 * above[5] + 9129 above[6] + 2) >> 2; 9130 predict += stride; 9132 predict[0] = pred2; 9133 predict[1] = pred3; 9134 predict[2] = pred4; 9135 predict[3] = pred5 = (above[5] + 2 * above[6] + 9136 above[7] + 2) >> 2; 9137 predict += stride; 9139 predict[0] = pred3; 9140 predict[1] = pred4; 9141 predict[2] = pred5; 9142 predict[3] = pred6 = (above[6] + 2 * above[7] + 9143 above[7] + 2) >> 2; 9144 } 9146 static void 9147 predict_rd_4x4(unsigned char *predict, 9148 int stride) 9149 { 9150 unsigned char *left = predict - 1; 9151 unsigned char *above = predict - stride; 9152 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9154 predict[0] = pred0 = 9155 (left[ 0] + 2 * above[-1] + above[0] + 2) >> 2; 9156 predict[1] = pred1 = 9157 (above[-1] + 2 * above[ 0] + above[1] + 2) >> 2; 9158 predict[2] = pred2 = 9159 (above[ 0] + 2 * above[ 1] + above[2] + 2) >> 2; 9160 predict[3] = pred3 = 9161 (above[ 1] + 2 * above[ 2] + above[3] + 2) >> 2; 9162 predict += stride; 9164 predict[0] = pred4 = 9165 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9166 predict[1] = pred0; 9167 predict[2] = pred1; 9168 predict[3] = pred2; 9169 predict += stride; 9171 predict[0] = pred5 = 9172 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9173 predict[1] = pred4; 9174 predict[2] = pred0; 9175 predict[3] = pred1; 9176 predict += stride; 9178 predict[0] = pred6 = (left[stride*3] + 2 * left[stride*2] + 9179 left[stride] + 2) >> 2; 9180 predict[1] = pred5; 9181 predict[2] = pred4; 9182 predict[3] = pred0; 9183 } 9185 static void 9186 predict_vr_4x4(unsigned char *predict, 9187 int stride) 9188 { 9189 unsigned char *left = predict - 1; 9190 unsigned char *above = predict - stride; 9191 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9192 pred7, pred8, pred9; 9194 predict[0] = pred0 = (above[-1] + above[0] + 1) >> 1; 9195 predict[1] = pred1 = (above[ 0] + above[1] + 1) >> 1; 9196 predict[2] = pred2 = (above[ 1] + above[2] + 1) >> 1; 9197 predict[3] = pred3 = (above[ 2] + above[3] + 1) >> 1; 9198 predict += stride; 9200 predict[0] = pred4 = (left[ 0] + 2 * above[-1] + 9201 above[0] + 2) >> 2; 9202 predict[1] = pred5 = (above[-1] + 2 * above[ 0] + 9203 above[1] + 2) >> 2; 9204 predict[2] = pred6 = (above[ 0] + 2 * above[ 1] + 9205 above[2] + 2) >> 2; 9206 predict[3] = pred7 = (above[ 1] + 2 * above[ 2] + 9207 above[3] + 2) >> 2; 9208 predict += stride; 9210 predict[0] = pred8 = 9211 (left[stride] + 2 * left[0] + above[-1] + 2) >> 2; 9212 predict[1] = pred0; 9213 predict[2] = pred1; 9214 predict[3] = pred2; 9215 predict += stride; 9217 predict[0] = pred9 = 9218 (left[stride*2] + 2 * left[stride] + left[0] + 2) >> 2; 9219 predict[1] = pred4; 9220 predict[2] = pred5; 9221 predict[3] = pred6; 9222 } 9224 static void 9225 predict_vl_4x4(unsigned char *predict, 9226 int stride) 9227 { 9228 unsigned char *above = predict - stride; 9229 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9230 pred7, pred8, pred9; 9232 predict[0] = pred0 = (above[0] + above[1] + 1) >> 1; 9233 predict[1] = pred1 = (above[1] + above[2] + 1) >> 1; 9234 predict[2] = pred2 = (above[2] + above[3] + 1) >> 1; 9235 predict[3] = pred3 = (above[3] + above[4] + 1) >> 1; 9236 predict += stride; 9238 predict[0] = pred4 = (above[0] + 2 * above[1] + 9239 above[2] + 2) >> 2; 9240 predict[1] = pred5 = (above[1] + 2 * above[2] + 9241 above[3] + 2) >> 2; 9242 predict[2] = pred6 = (above[2] + 2 * above[3] + 9243 above[4] + 2) >> 2; 9244 predict[3] = pred7 = (above[3] + 2 * above[4] + 9245 above[5] + 2) >> 2; 9246 predict += stride; 9248 predict[0] = pred1; 9249 predict[1] = pred2; 9250 predict[2] = pred3; 9251 predict[3] = pred8 = (above[4] + 2 * above[5] + 9252 above[6] + 2) >> 2; 9253 predict += stride; 9255 predict[0] = pred5; 9256 predict[1] = pred6; 9257 predict[2] = pred7; 9258 predict[3] = pred9 = (above[5] + 2 * above[6] + 9259 above[7] + 2) >> 2; 9260 } 9262 static void 9263 predict_hd_4x4(unsigned char *predict, 9264 int stride) 9265 { 9266 unsigned char *left = predict - 1; 9267 unsigned char *above = predict - stride; 9268 int pred0, pred1, pred2, pred3, pred4, pred5, pred6, 9269 pred7, pred8, pred9; 9271 predict[0] = pred0 = (left[ 0] + above[-1] + 1) >> 1; 9272 predict[1] = pred1 = (left[ 0] + 2 * above[-1] + 9273 above[0] + 2) >> 2; 9274 predict[2] = pred2 = (above[-1] + 2 * above[ 0] + 9275 above[1] + 2) >> 2; 9276 predict[3] = pred3 = (above[ 0] + 2 * above[ 1] + 9277 above[2] + 2) >> 2; 9278 predict += stride; 9280 predict[0] = pred4 = (left[stride] + left[0] + 1) >> 1; 9281 predict[1] = pred5 = (left[stride] + 2 * left[0] + 9282 above[-1] + 2) >> 2; 9283 predict[2] = pred0; 9284 predict[3] = pred1; 9285 predict += stride; 9287 predict[0] = pred6 = (left[stride*2] + left[stride] + 1) >> 1; 9288 predict[1] = pred7 = (left[stride*2] + 2 * left[stride] + 9289 left[0] + 2) >> 2; 9290 predict[2] = pred4; 9291 predict[3] = pred5; 9292 predict += stride; 9294 predict[0] = pred8 = (left[stride*3] + left[stride*2] + 1) >> 1; 9295 predict[1] = pred9 = (left[stride*3] + 2 * left[stride*2] + 9296 left[stride] + 2) >> 2; 9297 predict[2] = pred6; 9298 predict[3] = pred7; 9299 } 9301 static void 9302 predict_hu_4x4(unsigned char *predict, 9303 int stride) 9304 { 9305 unsigned char *left = predict - 1; 9306 int pred0, pred1, pred2, pred3, pred4, pred5, pred6; 9308 predict[0] = pred0 = (left[stride*0] + 9309 left[stride*1] + 1) >> 1; 9310 predict[1] = pred1 = (left[stride*0] + 2 * left[stride*1] + 9311 left[stride*2] + 2) >> 2; 9312 predict[2] = pred2 = (left[stride*1] + left[stride*2] + 1) >> 1; 9313 predict[3] = pred3 = (left[stride*1] + 2 * left[stride*2] + 9314 left[stride*3] + 2) >> 2; 9315 predict += stride; 9317 predict[0] = pred2; 9318 predict[1] = pred3; 9319 predict[2] = pred4 = (left[stride*2] + left[stride*3] + 1) >> 1; 9320 predict[3] = pred5 = (left[stride*2] + 2 * left[stride*3] + 9321 left[stride*3] + 2) >> 2; 9323 predict += stride; 9325 predict[0] = pred4; 9326 predict[1] = pred5; 9327 predict[2] = pred6 = left[stride*3]; 9328 predict[3] = pred6; 9329 predict += stride; 9331 predict[0] = pred6; 9332 predict[1] = pred6; 9333 predict[2] = pred6; 9334 predict[3] = pred6; 9335 } 9337 static void 9338 predict_h_16x16(unsigned char *predict, int stride) 9339 { 9340 predict_h_nxn(predict, stride, 16); 9341 } 9343 static void 9344 predict_v_16x16(unsigned char *predict, int stride) 9345 { 9346 predict_v_nxn(predict, stride, 16); 9347 } 9349 static void 9350 predict_tm_16x16(unsigned char *predict, int stride) 9351 { 9352 predict_tm_nxn(predict, stride, 16); 9353 } 9355 static void 9356 predict_h_8x8(unsigned char *predict, int stride) 9357 { 9358 predict_h_nxn(predict, stride, 8); 9359 } 9361 static void 9362 predict_v_8x8(unsigned char *predict, int stride) 9363 { 9364 predict_v_nxn(predict, stride, 8); 9365 } 9366 static void 9367 predict_tm_8x8(unsigned char *predict, int stride) 9368 { 9369 predict_tm_nxn(predict, stride, 8); 9370 } 9372 static void 9373 predict_tm_4x4(unsigned char *predict, int stride) 9374 { 9375 predict_tm_nxn(predict, stride, 4); 9376 } 9378 static void 9379 copy_down(unsigned char *recon, 9380 int stride) 9381 { 9382 /* Copy the four pixels above-right of subblock 3 to 9383 * above-right of subblocks 7, 11, and 15 9384 */ 9385 uint32_t tmp, *copy = (void *)(recon + 16 - stride); 9387 stride = stride / sizeof(unsigned int); 9388 tmp = *copy; 9389 copy += stride * 4; 9390 *copy = tmp; 9391 copy += stride * 4; 9392 *copy = tmp; 9393 copy += stride * 4; 9394 *copy = tmp; 9395 } 9397 static void 9398 b_pred(unsigned char *predict, 9399 int stride, 9400 struct mb_info *mbi, 9401 short *coeffs) 9402 { 9403 int i; 9405 copy_down(predict, stride); 9407 for (i = 0; i < 16; i++) 9408 { 9409 unsigned char *b_predict = predict + (i & 3) * 4; 9410 switch (mbi->split.modes[i]) 9411 { 9412 case B_DC_PRED: 9413 predict_dc_nxn(b_predict, stride, 4); 9414 break; 9415 case B_TM_PRED: 9416 predict_tm_4x4(b_predict, stride); 9417 break; 9418 case B_VE_PRED: 9419 predict_ve_4x4(b_predict, stride); 9420 break; 9421 case B_HE_PRED: 9422 predict_he_4x4(b_predict, stride); 9423 break; 9424 case B_LD_PRED: 9425 predict_ld_4x4(b_predict, stride); 9426 break; 9427 case B_RD_PRED: 9428 predict_rd_4x4(b_predict, stride); 9429 break; 9430 case B_VR_PRED: 9431 predict_vr_4x4(b_predict, stride); 9432 break; 9433 case B_VL_PRED: 9434 predict_vl_4x4(b_predict, stride); 9435 break; 9436 case B_HD_PRED: 9437 predict_hd_4x4(b_predict, stride); 9438 break; 9439 case B_HU_PRED: 9440 predict_hu_4x4(b_predict, stride); 9441 break; 9442 default: 9443 assert(0); 9444 } 9446 vp8_dixie_idct_add(b_predict, b_predict, stride, coeffs); 9447 coeffs += 16; 9449 if ((i & 3) == 3) 9450 { 9451 predict += stride * 4; 9452 } 9453 } 9454 } 9456 static void 9457 fixup_dc_coeffs(struct mb_info *mbi, 9458 short *coeffs) 9459 { 9460 short y2[16]; 9461 int i; 9463 vp8_dixie_walsh(coeffs + 24 * 16, y2); 9465 for (i = 0; i < 16; i++) 9466 coeffs[i*16] = y2[i]; 9467 } 9469 static void 9470 predict_intra_luma(unsigned char *predict, 9471 int stride, 9472 struct mb_info *mbi, 9473 short *coeffs) 9474 { 9475 if (mbi->base.y_mode == B_PRED) 9476 b_pred(predict, stride, mbi, coeffs); 9477 else 9478 { 9479 int i; 9481 switch (mbi->base.y_mode) 9482 { 9483 case DC_PRED: 9484 predict_dc_nxn(predict, stride, 16); 9485 break; 9486 case V_PRED: 9487 predict_v_16x16(predict, stride); 9488 break; 9489 case H_PRED: 9490 predict_h_16x16(predict, stride); 9491 break; 9492 case TM_PRED: 9493 predict_tm_16x16(predict, stride); 9494 break; 9495 default: 9496 assert(0); 9497 } 9499 fixup_dc_coeffs(mbi, coeffs); 9501 for (i = 0; i < 16; i++) 9502 { 9503 vp8_dixie_idct_add(predict, predict, stride, coeffs); 9504 coeffs += 16; 9505 predict += 4; 9507 if ((i & 3) == 3) 9508 predict += stride * 4 - 16; 9509 } 9511 } 9512 } 9514 static void 9515 predict_intra_chroma(unsigned char *predict_u, 9516 unsigned char *predict_v, 9517 int stride, 9518 struct mb_info *mbi, 9519 short *coeffs) 9520 { 9521 int i; 9523 switch (mbi->base.uv_mode) 9524 { 9525 case DC_PRED: 9526 predict_dc_nxn(predict_u, stride, 8); 9527 predict_dc_nxn(predict_v, stride, 8); 9528 break; 9529 case V_PRED: 9530 predict_v_8x8(predict_u, stride); 9531 predict_v_8x8(predict_v, stride); 9532 break; 9533 case H_PRED: 9534 predict_h_8x8(predict_u, stride); 9535 predict_h_8x8(predict_v, stride); 9536 break; 9537 case TM_PRED: 9538 predict_tm_8x8(predict_u, stride); 9539 predict_tm_8x8(predict_v, stride); 9540 break; 9541 default: 9542 assert(0); 9543 } 9545 coeffs += 16 * 16; 9547 for (i = 16; i < 20; i++) 9548 { 9549 vp8_dixie_idct_add(predict_u, predict_u, stride, coeffs); 9550 coeffs += 16; 9551 predict_u += 4; 9553 if (i & 1) 9554 predict_u += stride * 4 - 8; 9555 } 9557 for (i = 20; i < 24; i++) 9558 { 9559 vp8_dixie_idct_add(predict_v, predict_v, stride, coeffs); 9560 coeffs += 16; 9561 predict_v += 4; 9563 if (i & 1) 9564 predict_v += stride * 4 - 8; 9565 } 9567 } 9569 static void 9570 sixtap_horiz(unsigned char *output, 9571 int output_stride, 9572 const unsigned char *reference, 9573 int reference_stride, 9574 int cols, 9575 int rows, 9576 const filter_t filter 9577 ) 9578 { 9579 int r, c, temp; 9581 for (r = 0; r < rows; r++) 9582 { 9583 for (c = 0; c < cols; c++) 9584 { 9585 temp = (reference[-2] * filter[0]) + 9586 (reference[-1] * filter[1]) + 9587 (reference[ 0] * filter[2]) + 9588 (reference[ 1] * filter[3]) + 9589 (reference[ 2] * filter[4]) + 9590 (reference[ 3] * filter[5]) + 9591 64; 9592 temp >>= 7; 9593 output[c] = CLAMP_255(temp); 9594 reference++; 9595 } 9597 reference += reference_stride - cols; 9598 output += output_stride; 9599 } 9600 } 9602 static void 9603 sixtap_vert(unsigned char *output, 9604 int output_stride, 9605 const unsigned char *reference, 9606 int reference_stride, 9607 int cols, 9608 int rows, 9609 const filter_t filter 9610 ) 9611 { 9612 int r, c, temp; 9614 for (r = 0; r < rows; r++) 9615 { 9616 for (c = 0; c < cols; c++) 9617 { 9618 temp = (reference[-2*reference_stride] * filter[0]) + 9619 (reference[-1*reference_stride] * filter[1]) + 9620 (reference[ 0*reference_stride] * filter[2]) + 9621 (reference[ 1*reference_stride] * filter[3]) + 9622 (reference[ 2*reference_stride] * filter[4]) + 9623 (reference[ 3*reference_stride] * filter[5]) + 9624 64; 9625 temp >>= 7; 9626 output[c] = CLAMP_255(temp); 9627 reference++; 9628 } 9630 reference += reference_stride - cols; 9631 output += output_stride; 9632 } 9633 } 9635 static void 9636 sixtap_2d(unsigned char *output, 9637 int output_stride, 9638 const unsigned char *reference, 9639 int reference_stride, 9640 int cols, 9641 int rows, 9642 int mx, 9643 int my, 9644 const filter_t filters[8] 9645 ) 9646 { 9647 DECLARE_ALIGNED(16, unsigned char, temp[16*(16+5)]); 9649 sixtap_horiz(temp, 16, 9650 reference - 2 * reference_stride, reference_stride, 9651 cols, rows + 5, filters[mx]); 9652 sixtap_vert(output, output_stride, 9653 temp + 2 * 16, 16, 9654 cols, rows, filters[my]); 9655 } 9657 struct img_index 9658 { 9659 unsigned char *y, *u, *v; 9660 int stride, uv_stride; 9661 }; 9663 static const unsigned char * 9664 filter_block(unsigned char *output, 9665 const unsigned char *reference, 9666 int stride, 9667 const union mv *mv, 9668 const filter_t filters[8]) 9669 { 9670 int mx, my; 9672 /* Handle 0,0 as a special case. TODO: does this make it any 9673 * faster? 9674 */ 9675 if (!mv->raw) 9676 return reference; 9678 mx = mv->d.x & 7; 9679 my = mv->d.y & 7; 9680 reference += ((mv->d.y >> 3) * stride) + (mv->d.x >> 3); 9682 if (mx | my) 9683 { 9684 sixtap_2d(output, stride, reference, stride, 4, 4, mx, my, 9685 filters); 9686 reference = output; 9687 } 9689 return reference; 9691 } 9693 static void 9694 recon_1_block(unsigned char *output, 9695 const unsigned char *reference, 9696 int stride, 9697 const union mv *mv, 9698 const filter_t filters[8], 9699 short *coeffs, 9700 struct mb_info *mbi, 9701 int b 9702 ) 9703 { 9704 const unsigned char *predict; 9706 predict = filter_block(output, reference, stride, mv, filters); 9707 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9708 } 9710 static mv_t 9711 calculate_chroma_splitmv(struct mb_info *mbi, 9712 int b, 9713 int full_pixel) 9714 { 9715 int temp; 9716 union mv mv; 9718 temp = mbi->split.mvs[b].d.x + 9719 mbi->split.mvs[b+1].d.x + 9720 mbi->split.mvs[b+4].d.x + 9721 mbi->split.mvs[b+5].d.x; 9723 if (temp < 0) 9724 temp -= 4; 9725 else 9726 temp += 4; 9728 mv.d.x = temp / 8; 9730 temp = mbi->split.mvs[b].d.y + 9731 mbi->split.mvs[b+1].d.y + 9732 mbi->split.mvs[b+4].d.y + 9733 mbi->split.mvs[b+5].d.y; 9735 if (temp < 0) 9736 temp -= 4; 9738 else 9739 temp += 4; 9741 mv.d.y = temp / 8; 9743 if (full_pixel) 9744 { 9745 mv.d.x &= ~7; 9746 mv.d.y &= ~7; 9747 } 9749 return mv; 9750 } 9752 /* Note: We rely on the reconstructed border having the same stride 9753 * as the reference buffer because the filter_block can't adjust the 9754 * stride with its return value, only the reference pointer. 9755 */ 9756 static void 9757 build_mc_border(unsigned char *dst, 9758 const unsigned char *src, 9759 int stride, 9760 int x, 9761 int y, 9762 int b_w, 9763 int b_h, 9764 int w, 9765 int h 9766 ) 9767 { 9768 const unsigned char *ref_row; 9770 /* Get a pointer to the start of the real data for this row */ 9771 ref_row = src - x - y * stride; 9773 if (y >= h) 9774 ref_row += (h - 1) * stride; 9775 else if (y > 0) 9776 ref_row += y * stride; 9778 do 9779 { 9780 int left, right = 0, copy; 9782 left = x < 0 ? -x : 0; 9783 if (left > b_w) 9784 left = b_w; 9786 if (x + b_w > w) 9787 right = x + b_w - w; 9789 if (right > b_w) 9790 right = b_w; 9792 copy = b_w - left - right; 9794 if (left) 9795 memset(dst, ref_row[0], left); 9797 if (copy) 9798 memcpy(dst + left, ref_row + x + left, copy); 9800 if (right) 9801 memset(dst + left + copy, ref_row[w-1], right); 9803 dst += stride; 9804 y++; 9806 if (y < h && y > 0) 9807 ref_row += stride; 9808 } 9809 while (--b_h); 9810 } 9812 static void 9813 recon_1_edge_block(unsigned char *output, 9814 unsigned char *emul_block, 9815 const unsigned char *reference, 9816 int stride, 9817 const union mv *mv, 9818 const filter_t filters[8], 9819 short *coeffs, 9820 struct mb_info *mbi, 9821 int x, 9822 int y, 9823 int w, 9824 int h, 9825 int start_b 9826 ) 9827 { 9828 const unsigned char *predict; 9829 int b = start_b; 9830 const int b_w = 4; 9831 const int b_h = 4; 9833 x += mv->d.x >> 3; 9834 y += mv->d.y >> 3; 9836 /* Need two pixels left/above, 3 right/below for 6-tap */ 9837 if (x < 2 || x + b_w - 1 + 3 >= w || y < 2 || 9838 y + b_h - 1 + 3 >= h) 9839 { 9840 reference += (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9841 build_mc_border(emul_block, 9842 reference - 2 - 2 * stride, stride, 9843 x - 2, y - 2, b_w + 5, b_h + 5, w, h); 9844 reference = emul_block + 2 * stride + 2; 9845 reference -= (mv->d.x >> 3) + (mv->d.y >> 3) * stride; 9846 } 9848 predict = filter_block(output, reference, stride, mv, filters); 9849 vp8_dixie_idct_add(output, predict, stride, coeffs + 16 * b); 9850 } 9852 static void 9853 predict_inter_emulated_edge(struct vp8_decoder_ctx *ctx, 9854 struct img_index *img, 9855 short *coeffs, 9856 struct mb_info *mbi, 9857 int mb_col, 9858 int mb_row) 9859 { 9860 /* TODO: move this into its own buffer. This only works because 9861 * we still have a border allocated. 9862 */ 9863 unsigned char *emul_block = ctx->frame_strg[0].img.img_data; 9864 unsigned char *reference; 9865 unsigned char *output; 9866 ptrdiff_t reference_offset; 9867 int w, h, x, y, b; 9868 union mv chroma_mv[4]; 9869 unsigned char *u = img->u, *v = img->v; 9870 int full_pixel = ctx->frame_hdr.version == 3; 9872 x = mb_col * 16; 9873 y = mb_row * 16; 9874 w = ctx->mb_cols * 16; 9875 h = ctx->mb_rows * 16; 9876 output = img->y; 9877 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 9878 reference = output + reference_offset; 9880 if (mbi->base.y_mode != SPLITMV) 9881 { 9882 union mv uvmv; 9884 uvmv = mbi->base.mv; 9885 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9886 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9888 if (full_pixel) 9889 { 9890 uvmv.d.x &= ~7; 9891 uvmv.d.y &= ~7; 9892 } 9894 chroma_mv[0] = uvmv; 9895 chroma_mv[1] = uvmv; 9896 chroma_mv[2] = uvmv; 9897 chroma_mv[3] = uvmv; 9898 } 9899 else 9900 { 9901 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 9902 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 9903 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 9904 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 9905 } 9907 /* Luma */ 9908 for (b = 0; b < 16; b++) 9909 { 9910 union mv *ymv; 9912 if (mbi->base.y_mode != SPLITMV) 9913 ymv = &mbi->base.mv; 9914 else 9915 ymv = mbi->split.mvs + b; 9917 recon_1_edge_block(output, emul_block, reference, 9918 img->stride, ymv, ctx->subpixel_filters, coeffs, 9919 mbi, x, y, w, h, b); 9921 x += 4; 9922 output += 4; 9923 reference += 4; 9925 if ((b & 3) == 3) 9926 { 9927 x -= 16; 9928 y += 4; 9929 output += 4 * img->stride - 16; 9930 reference += 4 * img->stride - 16; 9931 } 9932 } 9934 x = mb_col * 16; 9935 y = mb_row * 16; 9937 /* Chroma */ 9938 x >>= 1; 9939 y >>= 1; 9940 w >>= 1; 9941 h >>= 1; 9943 for (b = 0; b < 4; b++) 9944 { 9945 recon_1_edge_block(u, emul_block, u + reference_offset, 9946 img->uv_stride, 9947 &chroma_mv[b], ctx->subpixel_filters, 9948 coeffs, mbi, x, y, w, h, b + 16); 9949 recon_1_edge_block(v, emul_block, v + reference_offset, 9950 img->uv_stride, 9951 &chroma_mv[b], ctx->subpixel_filters, 9952 coeffs, mbi, x, y, w, h, b + 20); 9953 u += 4; 9954 v += 4; 9955 x += 4; 9957 if (b & 1) 9958 { 9959 x -= 8; 9960 y += 4; 9961 u += 4 * img->uv_stride - 8; 9962 v += 4 * img->uv_stride - 8; 9963 } 9964 } 9966 } 9968 static void 9969 predict_inter(struct vp8_decoder_ctx *ctx, 9970 struct img_index *img, 9971 short *coeffs, 9972 struct mb_info *mbi) 9973 { 9974 unsigned char *y = img->y; 9975 unsigned char *u = img->u; 9976 unsigned char *v = img->v; 9977 ptrdiff_t reference_offset; 9978 union mv chroma_mv[4]; 9979 int full_pixel = ctx->frame_hdr.version == 3; 9980 int b; 9982 if (mbi->base.y_mode != SPLITMV) 9983 { 9984 union mv uvmv; 9986 uvmv = mbi->base.mv; 9987 uvmv.d.x = (uvmv.d.x + 1 + (uvmv.d.x >> 31) * 2) / 2; 9988 uvmv.d.y = (uvmv.d.y + 1 + (uvmv.d.y >> 31) * 2) / 2; 9990 if (full_pixel) 9991 { 9992 uvmv.d.x &= ~7; 9993 uvmv.d.y &= ~7; 9994 } 9996 chroma_mv[0] = 9997 chroma_mv[1] = 9998 chroma_mv[2] = 9999 chroma_mv[3] = uvmv; 10000 } 10001 else 10002 { 10003 chroma_mv[0] = calculate_chroma_splitmv(mbi, 0, full_pixel); 10004 chroma_mv[1] = calculate_chroma_splitmv(mbi, 2, full_pixel); 10005 chroma_mv[2] = calculate_chroma_splitmv(mbi, 8, full_pixel); 10006 chroma_mv[3] = calculate_chroma_splitmv(mbi, 10, full_pixel); 10007 } 10009 reference_offset = ctx->ref_frame_offsets[mbi->base.ref_frame]; 10011 for (b = 0; b < 16; b++) 10012 { 10013 union mv *ymv; 10015 if (mbi->base.y_mode != SPLITMV) 10016 ymv = &mbi->base.mv; 10017 else 10018 ymv = mbi->split.mvs + b; 10020 recon_1_block(y, y + reference_offset, img->stride, 10021 ymv, ctx->subpixel_filters, coeffs, mbi, b); 10022 y += 4; 10024 if ((b & 3) == 3) 10025 y += 4 * img->stride - 16; 10026 } 10028 for (b = 0; b < 4; b++) 10029 { 10030 recon_1_block(u, u + reference_offset, 10031 img->uv_stride, &chroma_mv[b], 10032 ctx->subpixel_filters, coeffs, mbi, b + 16); 10033 recon_1_block(v, v + reference_offset, 10034 img->uv_stride, &chroma_mv[b], 10035 ctx->subpixel_filters, coeffs, mbi, b + 20); 10036 u += 4; 10037 v += 4; 10039 if (b & 1) 10040 { 10041 u += 4 * img->uv_stride - 8; 10042 v += 4 * img->uv_stride - 8; 10043 } 10044 } 10045 } 10047 void 10048 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg) 10049 { 10050 if (rcimg) 10051 { 10052 assert(rcimg->ref_cnt); 10053 rcimg->ref_cnt--; 10054 } 10055 } 10057 struct ref_cnt_img * 10058 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg) 10059 { 10060 rcimg->ref_cnt++; 10061 return rcimg; 10062 } 10064 struct ref_cnt_img * 10065 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames) 10066 { 10067 int i; 10069 for (i = 0; i < NUM_REF_FRAMES; i++) 10070 if (frames[i].ref_cnt == 0) 10071 { 10072 frames[i].ref_cnt = 1; 10073 return &frames[i]; 10074 } 10076 assert(0); 10077 return NULL; 10078 } 10080 static void 10081 fixup_left(unsigned char *predict, 10082 int width, 10083 int stride, 10084 unsigned int row, 10085 enum prediction_mode mode) 10086 { 10087 /* The left column of out-of-frame pixels is taken to be 129, 10088 * unless we're doing DC_PRED, in which case we duplicate the 10089 * above row, unless this is also row 0, in which case we use 10090 * 129. 10091 */ 10092 unsigned char *left = predict - 1; 10093 int i; 10095 if (mode == DC_PRED && row) 10096 { 10097 unsigned char *above = predict - stride; 10099 for (i = 0; i < width; i++) 10100 { 10101 *left = above[i]; 10102 left += stride; 10103 } 10104 } 10105 else 10106 { 10107 /* Need to re-set the above row, in case the above MB was 10108 * DC_PRED. 10109 */ 10110 left -= stride; 10111 for (i = -1; i < width; i++) 10112 { 10113 *left = 129; 10114 left += stride; 10115 } 10116 } 10117 } 10119 static void 10120 fixup_above(unsigned char *predict, 10121 int width, 10122 int stride, 10123 unsigned int col, 10124 enum prediction_mode mode) 10125 { 10126 /* The above row of out-of-frame pixels is taken to be 127, 10127 * unless we're doing DC_PRED, in which case we duplicate the 10128 * left col, unless this is also col 0, in which case we use 10129 * 127. 10130 */ 10131 unsigned char *above = predict - stride; 10132 int i; 10134 if (mode == DC_PRED && col) 10135 { 10136 unsigned char *left = predict - 1; 10138 for (i = 0; i < width; i++) 10139 { 10140 above[i] = *left; 10141 left += stride; 10142 } 10143 } 10144 else 10145 /* Need to re-set the left col, in case the last MB was 10146 * DC_PRED. 10147 */ 10148 memset(above - 1, 127, width + 1); 10150 memset(above + width, 127, 4); // for above-right subblock modes 10151 } 10153 void 10154 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx) 10155 { 10156 int i; 10157 unsigned char *this_frame_base; 10159 if (ctx->frame_hdr.frame_size_updated) 10160 { 10161 for (i = 0; i < NUM_REF_FRAMES; i++) 10162 { 10163 unsigned int w = ctx->mb_cols * 16 + BORDER_PIXELS * 2; 10164 unsigned int h = ctx->mb_rows * 16 + BORDER_PIXELS * 2; 10166 vpx_img_free(&ctx->frame_strg[i].img); 10167 ctx->frame_strg[i].ref_cnt = 0; 10168 ctx->ref_frames[i] = NULL; 10170 if (!vpx_img_alloc(&ctx->frame_strg[i].img, 10171 IMG_FMT_I420, w, h, 16)) 10172 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10173 "Failed to allocate %dx%d" 10174 " framebuffer", 10175 w, h); 10177 vpx_img_set_rect(&ctx->frame_strg[i].img, BORDER_PIXELS, 10178 BORDER_PIXELS, ctx->frame_hdr.kf.w, 10179 ctx->frame_hdr.kf.h); 10181 } 10183 if (ctx->frame_hdr.version) 10184 ctx->subpixel_filters = bilinear_filters; 10185 else 10186 ctx->subpixel_filters = sixtap_filters; 10187 } 10189 /* Find a free framebuffer to predict into */ 10190 if (ctx->ref_frames[CURRENT_FRAME]) 10191 vp8_dixie_release_ref_frame(ctx->ref_frames[CURRENT_FRAME]); 10193 ctx->ref_frames[CURRENT_FRAME] = 10194 vp8_dixie_find_free_ref_frame(ctx->frame_strg); 10195 this_frame_base = ctx->ref_frames[CURRENT_FRAME]->img.img_data; 10197 /* Calculate offsets to the other reference frames */ 10198 for (i = 0; i < NUM_REF_FRAMES; i++) 10199 { 10200 struct ref_cnt_img *ref = ctx->ref_frames[i]; 10202 ctx->ref_frame_offsets[i] = 10203 ref ? ref->img.img_data - this_frame_base : 0; 10205 } 10207 /* TODO: No need to do this on every frame... */ 10208 } 10210 void 10211 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx) 10212 { 10213 int i; 10215 for (i = 0; i < NUM_REF_FRAMES; i++) 10216 { 10217 vpx_img_free(&ctx->frame_strg[i].img); 10218 ctx->frame_strg[i].ref_cnt = 0; 10219 ctx->ref_frames[i] = NULL; 10220 } 10221 } 10223 void 10224 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10225 unsigned int row, 10226 unsigned int start_col, 10227 unsigned int num_cols) 10228 { 10229 struct img_index img; 10230 struct mb_info *mbi; 10231 unsigned int col; 10232 short *coeffs; 10234 /* Adjust pointers based on row, start_col */ 10235 img.stride = 10236 ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_Y]; 10237 img.uv_stride = 10238 ctx->ref_frames[CURRENT_FRAME]->img.stride[PLANE_U]; 10239 img.y = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_Y]; 10240 img.u = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_U]; 10241 img.v = ctx->ref_frames[CURRENT_FRAME]->img.planes[PLANE_V]; 10242 img.y += (img.stride * row + start_col) * 16; 10243 img.u += (img.uv_stride * row + start_col) * 8; 10244 img.v += (img.uv_stride * row + start_col) * 8; 10245 mbi = ctx->mb_info_rows[row] + start_col; 10246 coeffs = ctx->tokens[row & 10247 (ctx->token_hdr.partitions - 1)].coeffs + 10248 25 * 16 * start_col; 10250 /* Fix up the out-of-frame pixels */ 10251 if (start_col == 0) 10252 { 10253 fixup_left(img.y, 16, img.stride, row, mbi->base.y_mode); 10254 fixup_left(img.u, 8, img.uv_stride, row, mbi->base.uv_mode); 10255 fixup_left(img.v, 8, img.uv_stride, row, mbi->base.uv_mode); 10257 if (row == 0) 10258 *(img.y - img.stride - 1) = 127; 10259 } 10261 for (col = start_col; col < start_col + num_cols; col++) 10262 { 10263 if (row == 0) 10264 { 10265 fixup_above(img.y, 16, img.stride, col, 10266 mbi->base.y_mode); 10267 fixup_above(img.u, 8, img.uv_stride, col, 10268 mbi->base.uv_mode); 10269 fixup_above(img.v, 8, img.uv_stride, col, 10270 mbi->base.uv_mode); 10271 } 10273 if (mbi->base.y_mode <= B_PRED) 10274 { 10275 predict_intra_luma(img.y, img.stride, mbi, coeffs); 10276 predict_intra_chroma(img.u, img.v, img.uv_stride, mbi, 10277 coeffs); 10278 } 10279 else 10280 { 10281 if (mbi->base.y_mode != SPLITMV) // && != BPRED 10282 fixup_dc_coeffs(mbi, coeffs); 10284 if (mbi->base.need_mc_border) 10285 predict_inter_emulated_edge(ctx, &img, coeffs, mbi, 10286 col, row); 10287 else 10288 predict_inter(ctx, &img, coeffs, mbi); 10289 } 10291 /* Advance to the next macroblock */ 10292 mbi++; 10293 img.y += 16; 10294 img.u += 8; 10295 img.v += 8; 10296 coeffs += 25 * 16; 10297 } 10298 if (col == ctx->mb_cols) 10299 { 10300 /* Extend the last row by four pixels for intra prediction. 10301 * This will be propagated later by copy_down. 10302 */ 10303 uint32_t *extend = (uint32_t *)(img.y + 15 * img.stride); 10304 uint32_t val = 0x01010101 * img.y[-1 + 15 * img.stride]; 10305 *extend = val; 10306 } 10307 } 10309 ---- End code block ---------------------------------------- 10311 20.15. predict.h 10313 ---- Begin code block -------------------------------------- 10315 /* 10316 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10317 * 10318 * Use of this source code is governed by a BSD-style license 10319 * that can be found in the LICENSE file in the root of the source 10320 * tree. An additional intellectual property rights grant can be 10321 * found in the file PATENTS. All contributing project authors may 10322 * be found in the AUTHORS file in the root of the source tree. 10323 */ 10324 #ifndef PREDICT_H 10325 #define PREDICT_H 10327 void 10328 vp8_dixie_predict_init(struct vp8_decoder_ctx *ctx); 10330 void 10331 vp8_dixie_predict_destroy(struct vp8_decoder_ctx *ctx); 10333 void 10334 vp8_dixie_predict_process_row(struct vp8_decoder_ctx *ctx, 10335 unsigned int row, 10336 unsigned int start_col, 10337 unsigned int num_cols); 10339 void 10340 vp8_dixie_release_ref_frame(struct ref_cnt_img *rcimg); 10342 struct ref_cnt_img * 10343 vp8_dixie_ref_frame(struct ref_cnt_img *rcimg); 10345 struct ref_cnt_img * 10346 vp8_dixie_find_free_ref_frame(struct ref_cnt_img *frames); 10348 #endif 10350 ---- End code block ---------------------------------------- 10352 20.16. tokens.c 10354 ---- Begin code block -------------------------------------- 10355 /* 10356 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10357 * 10358 * Use of this source code is governed by a BSD-style license 10359 * that can be found in the LICENSE file in the root of the source 10360 * tree. An additional intellectual property rights grant can be 10361 * found in the file PATENTS. All contributing project authors may 10362 * be found in the AUTHORS file in the root of the source tree. 10363 */ 10364 #include "vpx_codec_internal.h" 10365 #include "dixie.h" 10366 #include "tokens.h" 10367 #include 10368 #include 10369 #include 10371 enum 10372 { 10373 EOB_CONTEXT_NODE, 10374 ZERO_CONTEXT_NODE, 10375 ONE_CONTEXT_NODE, 10376 LOW_VAL_CONTEXT_NODE, 10377 TWO_CONTEXT_NODE, 10378 THREE_CONTEXT_NODE, 10379 HIGH_LOW_CONTEXT_NODE, 10380 CAT_ONE_CONTEXT_NODE, 10381 CAT_THREEFOUR_CONTEXT_NODE, 10382 CAT_THREE_CONTEXT_NODE, 10383 CAT_FIVE_CONTEXT_NODE 10384 }; 10385 enum 10386 { 10387 ZERO_TOKEN, 10388 ONE_TOKEN, 10389 TWO_TOKEN, 10390 THREE_TOKEN, 10391 FOUR_TOKEN, 10392 DCT_VAL_CATEGORY1, 10393 DCT_VAL_CATEGORY2, 10394 DCT_VAL_CATEGORY3, 10395 DCT_VAL_CATEGORY4, 10396 DCT_VAL_CATEGORY5, 10397 DCT_VAL_CATEGORY6, 10398 DCT_EOB_TOKEN, 10399 MAX_ENTROPY_TOKENS 10400 }; 10401 struct extrabits 10402 { 10403 short min_val; 10404 short length; 10405 unsigned char probs[12]; 10406 }; 10407 static const unsigned int left_context_index[25] = 10408 { 10409 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 10410 4, 4, 5, 5, 6, 6, 7, 7, 8 10411 }; 10412 static const unsigned int above_context_index[25] = 10413 { 10414 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 10415 4, 5, 4, 5, 6, 7, 6, 7, 8 10416 }; 10417 #define X(n) ((n) * PREV_COEF_CONTEXTS * ENTROPY_NODES) 10418 static const unsigned int bands_x[16] = 10419 { 10420 X(0), X(1), X(2), X(3), X(6), X(4), X(5), X(6), 10421 X(6), X(6), X(6), X(6), X(6), X(6), X(6), X(7) 10422 }; 10423 #undef X 10424 static const struct extrabits extrabits[MAX_ENTROPY_TOKENS] = 10425 { 10426 { 0, -1, { 0, 0, 0, 0, 0, 0, 10427 0, 0, 0, 0, 0, 0 } }, //ZERO_TOKEN 10428 { 1, 0, { 0, 0, 0, 0, 0, 0, 10429 0, 0, 0, 0, 0, 0 } }, //ONE_TOKEN 10430 { 2, 0, { 0, 0, 0, 0, 0, 0, 10431 0, 0, 0, 0, 0, 0 } }, //TWO_TOKEN 10432 { 3, 0, { 0, 0, 0, 0, 0, 0, 10433 0, 0, 0, 0, 0, 0 } }, //THREE_TOKEN 10434 { 4, 0, { 0, 0, 0, 0, 0, 0, 10435 0, 0, 0, 0, 0, 0 } }, //FOUR_TOKEN 10436 { 5, 0, {159, 0, 0, 0, 0, 0, 10437 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY1 10438 { 7, 1, {145, 165, 0, 0, 0, 0, 10439 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY2 10440 {11, 2, {140, 148, 173, 0, 0, 0, 10441 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY3 10442 {19, 3, {135, 140, 155, 176, 0, 0, 10443 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY4 10444 {35, 4, {130, 134, 141, 157, 180, 0, 10445 0, 0, 0, 0, 0, 0 } }, //DCT_VAL_CATEGORY5 10446 {67, 10, {129, 130, 133, 140, 153, 177, 10447 196, 230, 243, 254, 254, 0 } }, //DCT_VAL_CATEGORY6 10448 { 0, -1, { 0, 0, 0, 0, 0, 0, 10449 0, 0, 0, 0, 0, 0 } }, // EOB TOKEN 10451 }; 10452 static const unsigned int zigzag[16] = 10453 { 10454 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 10455 }; 10457 #define DECODE_AND_APPLYSIGN(value_to_sign) \ 10458 v = (bool_get_bit(bool) ? -value_to_sign \ 10459 : value_to_sign) * dqf[!!c]; 10461 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \ 10462 if (!bool_get(bool, probability)) goto branch; 10464 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \ 10465 if (!bool_get(bool, probability)) \ 10466 { \ 10467 prob = type_probs; \ 10468 if(c<15) {\ 10469 ++c; \ 10470 prob += bands_x[c]; \ 10471 goto branch; \ 10472 }\ 10473 else \ 10474 goto BLOCK_FINISHED; /* for malformed input */\ 10475 } 10477 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \ 10478 DECODE_AND_APPLYSIGN(val) \ 10479 prob = type_probs + (ENTROPY_NODES*2); \ 10480 if(c < 15){\ 10481 b_tokens[zigzag[c]] = v; \ 10482 ++c; \ 10483 goto DO_WHILE; }\ 10484 b_tokens[zigzag[15]] = v; \ 10485 goto BLOCK_FINISHED; 10487 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\ 10488 val += bool_get(bool, extrabits[t].probs[bits_count]) << \ 10489 bits_count; 10491 static int 10492 decode_mb_tokens(struct bool_decoder *bool, 10493 token_entropy_ctx_t left, 10494 token_entropy_ctx_t above, 10495 short *tokens, 10496 enum prediction_mode mode, 10497 coeff_probs_table_t probs, 10498 short factor[TOKEN_BLOCK_TYPES][2]) 10499 { 10500 int i, stop, type; 10501 int c, t, v; 10502 int val, bits_count; 10503 int eob_mask; 10504 short *b_tokens; // tokens for this block 10505 unsigned char *type_probs; // probabilities for this block type 10506 unsigned char *prob; 10507 short *dqf; 10509 eob_mask = 0; 10511 if (mode != B_PRED && mode != SPLITMV) 10512 { 10513 i = 24; 10514 stop = 24; 10515 type = 1; 10516 b_tokens = tokens + 24 * 16; 10517 dqf = factor[TOKEN_BLOCK_Y2]; 10518 } 10519 else 10520 { 10521 i = 0; 10522 stop = 16; 10523 type = 3; 10524 b_tokens = tokens; 10525 dqf = factor[TOKEN_BLOCK_Y1]; 10526 } 10528 /* Save a pointer to the coefficient probs for the current type. 10529 * Need to repeat this whenever type changes. 10530 */ 10531 type_probs = probs[type][0][0]; 10533 BLOCK_LOOP: 10534 t = left[left_context_index[i]] + above[above_context_index[i]]; 10535 c = !type; /* all blocks start at 0 except type 0, which starts 10536 * at 1. */ 10538 prob = type_probs; 10539 prob += t * ENTROPY_NODES; 10541 DO_WHILE: 10542 prob += bands_x[c]; 10543 DECODE_AND_BRANCH_IF_ZERO(prob[EOB_CONTEXT_NODE], 10544 BLOCK_FINISHED); 10546 CHECK_0_: 10547 DECODE_AND_LOOP_IF_ZERO(prob[ZERO_CONTEXT_NODE], CHECK_0_); 10548 DECODE_AND_BRANCH_IF_ZERO(prob[ONE_CONTEXT_NODE], 10549 ONE_CONTEXT_NODE_0_); 10550 DECODE_AND_BRANCH_IF_ZERO(prob[LOW_VAL_CONTEXT_NODE], 10551 LOW_VAL_CONTEXT_NODE_0_); 10552 DECODE_AND_BRANCH_IF_ZERO(prob[HIGH_LOW_CONTEXT_NODE], 10553 HIGH_LOW_CONTEXT_NODE_0_); 10554 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREEFOUR_CONTEXT_NODE], 10555 CAT_THREEFOUR_CONTEXT_NODE_0_); 10556 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_FIVE_CONTEXT_NODE], 10557 CAT_FIVE_CONTEXT_NODE_0_); 10558 val = extrabits[DCT_VAL_CATEGORY6].min_val; 10559 bits_count = extrabits[DCT_VAL_CATEGORY6].length; 10561 do 10562 { 10563 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, 10564 bits_count); 10565 bits_count -- ; 10566 } 10567 while (bits_count >= 0); 10569 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10571 CAT_FIVE_CONTEXT_NODE_0_: 10572 val = extrabits[DCT_VAL_CATEGORY5].min_val; 10573 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4); 10574 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3); 10575 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2); 10576 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1); 10577 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0); 10578 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10580 CAT_THREEFOUR_CONTEXT_NODE_0_: 10581 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_THREE_CONTEXT_NODE], 10582 CAT_THREE_CONTEXT_NODE_0_); 10583 val = extrabits[DCT_VAL_CATEGORY4].min_val; 10584 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3); 10585 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2); 10586 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1); 10587 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0); 10588 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10590 CAT_THREE_CONTEXT_NODE_0_: 10591 val = extrabits[DCT_VAL_CATEGORY3].min_val; 10592 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2); 10593 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1); 10594 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0); 10595 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10597 HIGH_LOW_CONTEXT_NODE_0_: 10598 DECODE_AND_BRANCH_IF_ZERO(prob[CAT_ONE_CONTEXT_NODE], 10599 CAT_ONE_CONTEXT_NODE_0_); 10601 val = extrabits[DCT_VAL_CATEGORY2].min_val; 10602 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1); 10603 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0); 10604 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10606 CAT_ONE_CONTEXT_NODE_0_: 10607 val = extrabits[DCT_VAL_CATEGORY1].min_val; 10608 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0); 10609 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val); 10611 LOW_VAL_CONTEXT_NODE_0_: 10612 DECODE_AND_BRANCH_IF_ZERO(prob[TWO_CONTEXT_NODE], 10613 TWO_CONTEXT_NODE_0_); 10614 DECODE_AND_BRANCH_IF_ZERO(prob[THREE_CONTEXT_NODE], 10615 THREE_CONTEXT_NODE_0_); 10616 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4); 10618 THREE_CONTEXT_NODE_0_: 10619 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3); 10621 TWO_CONTEXT_NODE_0_: 10622 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2); 10624 ONE_CONTEXT_NODE_0_: 10625 DECODE_AND_APPLYSIGN(1); 10626 prob = type_probs + ENTROPY_NODES; 10628 if (c < 15) 10629 { 10630 b_tokens[zigzag[c]] = v; 10631 ++c; 10632 goto DO_WHILE; 10633 } 10635 b_tokens[zigzag[15]] = v; 10636 BLOCK_FINISHED: 10637 eob_mask |= (c > 1) << i; 10638 t = (c != !type); // any nonzero data? 10639 eob_mask |= t << 31; 10641 left[left_context_index[i]] = above[above_context_index[i]] = t; 10642 b_tokens += 16; 10644 i++; 10646 if (i < stop) 10647 goto BLOCK_LOOP; 10649 if (i == 25) 10650 { 10651 type = 0; 10652 i = 0; 10653 stop = 16; 10654 type_probs = probs[type][0][0]; 10655 b_tokens = tokens; 10656 dqf = factor[TOKEN_BLOCK_Y1]; 10657 goto BLOCK_LOOP; 10658 } 10660 if (i == 16) 10661 { 10662 type = 2; 10663 type_probs = probs[type][0][0]; 10664 stop = 24; 10665 dqf = factor[TOKEN_BLOCK_UV]; 10666 goto BLOCK_LOOP; 10667 } 10669 return eob_mask; 10670 } 10672 static void 10673 reset_row_context(token_entropy_ctx_t *left) 10674 { 10675 memset(left, 0, sizeof(*left)); 10676 } 10678 static void 10679 reset_above_context(token_entropy_ctx_t *above, unsigned int cols) 10680 { 10681 memset(above, 0, cols * sizeof(*above)); 10682 } 10684 static void 10685 reset_mb_context(token_entropy_ctx_t *left, 10686 token_entropy_ctx_t *above, 10687 enum prediction_mode mode) 10688 { 10689 /* Reset the macroblock context on the left and right. We have to 10690 * preserve the context of the second order block if this mode 10691 * would not have updated it. 10692 */ 10693 memset(left, 0, sizeof((*left)[0]) * 8); 10694 memset(above, 0, sizeof((*above)[0]) * 8); 10696 if (mode != B_PRED && mode != SPLITMV) 10697 { 10698 (*left)[8] = 0; 10699 (*above)[8] = 0; 10700 } 10701 } 10703 void 10704 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 10705 unsigned int partition, 10706 unsigned int row, 10707 unsigned int start_col, 10708 unsigned int num_cols) 10709 { 10710 struct token_decoder *tokens = &ctx->tokens[partition]; 10711 short coeffs = tokens->coeffs + 25 * 16 * start_col; 10712 unsigned int col; 10713 token_entropy_ctx_t *above = ctx->above_token_entropy_ctx 10714 + start_col; 10715 token_entropy_ctx_t *left = &tokens->left_token_entropy_ctx; 10716 struct mb_info *mbi = ctx->mb_info_rows[row] + start_col; 10718 if (row == 0) 10719 reset_above_context(above, num_cols); 10721 if (start_col == 0) 10722 reset_row_context(left); 10724 for (col = start_col; col < start_col + num_cols; col++) 10725 { 10726 memset(coeffs, 0, 25 * 16 * sizeof(short)); 10728 if (mbi->base.skip_coeff) 10729 { 10730 reset_mb_context(left, above, mbi->base.y_mode); 10731 mbi->base.eob_mask = 0; 10732 } 10733 else 10734 { 10735 struct dequant_factors *dqf; 10737 dqf = ctx->dequant_factors + mbi->base.segment_id; 10738 mbi->base.eob_mask = 10739 decode_mb_tokens(&tokens->bool, 10740 *left, *above, 10741 coeffs, 10742 mbi->base.y_mode, 10743 ctx->entropy_hdr.coeff_probs, 10744 dqf->factor); 10745 } 10747 above++; 10748 mbi++; 10749 coeffs += 25 * 16; 10750 } 10751 } 10753 void 10754 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx) 10755 { 10756 unsigned int partitions = ctx->token_hdr.partitions; 10758 if (ctx->frame_hdr.frame_size_updated) 10759 { 10760 unsigned int i; 10761 unsigned int coeff_row_sz = 10762 ctx->mb_cols * 25 * 16 * sizeof(short); 10764 for (i = 0; i < partitions; i++) 10765 { 10766 free(ctx->tokens[i].coeffs); 10767 ctx->tokens[i].coeffs = memalign(16, coeff_row_sz); 10769 if (!ctx->tokens[i].coeffs) 10770 vpx_internal_error(&ctx->error, VPX_CODEC_MEM_ERROR, 10771 NULL); 10772 } 10774 free(ctx->above_token_entropy_ctx); 10775 ctx->above_token_entropy_ctx = 10776 calloc(ctx->mb_cols, 10777 sizeof(*ctx->above_token_entropy_ctx)); 10779 if (!ctx->above_token_entropy_ctx) 10780 vpx_internal_error(&ctx->error, 10781 VPX_CODEC_MEM_ERROR, NULL); 10782 } 10783 } 10785 void 10786 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx) 10787 { 10788 int i; 10790 for (i = 0; i < MAX_PARTITIONS; i++) 10791 free(ctx->tokens[i].coeffs); 10793 free(ctx->above_token_entropy_ctx); 10794 } 10796 ---- End code block ---------------------------------------- 10798 20.17. tokens.h 10800 ---- Begin code block -------------------------------------- 10802 /* 10803 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 10804 * 10805 * Use of this source code is governed by a BSD-style license 10806 * that can be found in the LICENSE file in the root of the source 10807 * tree. An additional intellectual property rights grant can be 10808 * found in the file PATENTS. All contributing project authors may 10809 * be found in the AUTHORS file in the root of the source tree. 10810 */ 10811 #ifndef TOKENS_H 10812 #define TOKENS_H 10814 void 10815 vp8_dixie_tokens_init(struct vp8_decoder_ctx *ctx); 10817 void 10818 vp8_dixie_tokens_destroy(struct vp8_decoder_ctx *ctx); 10820 void 10821 vp8_dixie_tokens_process_row(struct vp8_decoder_ctx *ctx, 10822 unsigned int partition, 10823 unsigned int row, 10824 unsigned int start_col, 10825 unsigned int num_cols); 10827 #endif 10829 ---- End code block ---------------------------------------- 10831 20.18. vp8_prob_data.h 10833 ---- Begin code block -------------------------------------- 10835 static const 10836 unsigned char k_coeff_entropy_update_probs[BLOCK_TYPES][COEF_BANDS] 10837 [PREV_COEF_CONTEXTS] 10838 [ENTROPY_NODES] = 10839 { 10840 { 10841 { 10842 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10843 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10844 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10845 }, 10846 { 10847 {176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10848 {223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 10849 {249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10850 }, 10851 { 10852 {255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 10853 {234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10854 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10855 }, 10856 { 10857 {255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10858 {239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10859 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10860 }, 10861 { 10862 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10863 {251, 255, 254, 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, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10868 {251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10869 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10870 }, 10871 { 10872 {255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255}, 10873 {250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255}, 10874 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10875 }, 10876 { 10877 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10878 {255, 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 { 10883 { 10884 {217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10885 {225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255}, 10886 {234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255}, 10887 }, 10888 { 10889 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10890 {223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10891 {238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255}, 10892 }, 10893 { 10894 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10895 {249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10896 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10897 }, 10898 { 10899 {255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10900 {247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10901 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10902 }, 10903 { 10904 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10905 {252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10906 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10907 }, 10908 { 10909 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10910 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10911 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10912 }, 10913 { 10914 {255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10915 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10916 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10917 }, 10918 { 10919 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 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 }, 10923 }, 10924 { 10925 { 10926 {186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255}, 10927 {234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255}, 10928 {251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255}, 10929 }, 10930 { 10931 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10932 {236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10933 {251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255}, 10934 }, 10935 { 10936 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10937 {254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10938 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10940 }, 10941 { 10942 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10943 {254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10944 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10945 }, 10946 { 10947 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10948 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10949 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10950 }, 10951 { 10952 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10953 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10954 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10955 }, 10956 { 10957 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10958 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10959 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10960 }, 10961 { 10962 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10963 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10964 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10965 }, 10966 }, 10967 { 10968 { 10969 {248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10970 {250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255}, 10971 {248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255}, 10972 }, 10973 { 10974 {255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10975 {246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10976 {252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255}, 10977 }, 10978 { 10979 {255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255}, 10980 {248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10981 {253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255}, 10982 }, 10983 { 10984 {255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10985 {245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10986 {253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10987 }, 10988 { 10989 {255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 10990 {252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10991 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10992 }, 10993 { 10994 {255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 10995 {249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10996 {255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255}, 10997 }, 10998 { 10999 {255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255}, 11000 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 11001 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 11002 }, 11003 { 11004 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 11005 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 11006 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 11007 }, 11008 }, 11009 }; 11011 static const 11012 unsigned char k_default_y_mode_probs [] = 11013 { 112, 86, 140, 37}; 11015 static const 11016 unsigned char k_default_uv_mode_probs [] = 11017 { 162, 101, 204}; 11019 static const 11020 unsigned char k_default_coeff_probs [BLOCK_TYPES][COEF_BANDS] 11021 [PREV_COEF_CONTEXTS][ENTROPY_NODES] = 11022 { 11023 { /* block type 0 */ 11024 { /* coeff band 0 */ 11025 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11026 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11027 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11028 }, 11029 { /* coeff band 1 */ 11030 { 253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128}, 11031 { 189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128}, 11032 { 106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128} 11034 }, 11035 { /* coeff band 2 */ 11036 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128}, 11037 { 181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128}, 11038 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128} 11039 }, 11040 { /* coeff band 3 */ 11041 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128}, 11042 { 184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128}, 11043 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128} 11044 }, 11045 { /* coeff band 4 */ 11046 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128}, 11047 { 170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128}, 11048 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128} 11049 }, 11050 { /* coeff band 5 */ 11051 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128}, 11052 { 207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128}, 11053 { 102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128} 11054 }, 11055 { /* coeff band 6 */ 11056 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128}, 11057 { 177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128}, 11058 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128} 11059 }, 11060 { /* coeff band 7 */ 11061 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11062 { 246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11063 { 255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11064 } 11065 }, 11066 { /* block type 1 */ 11067 { /* coeff band 0 */ 11068 { 198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62}, 11069 { 131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1}, 11070 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128} 11071 }, 11072 { /* coeff band 1 */ 11073 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128}, 11074 { 184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128}, 11075 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128} 11076 }, 11077 { /* coeff band 2 */ 11078 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128}, 11079 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128}, 11080 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128} 11081 }, 11082 { /* coeff band 3 */ 11083 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128}, 11084 { 109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128}, 11085 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128} 11086 }, 11087 { /* coeff band 4 */ 11088 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128}, 11089 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128}, 11090 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128} 11091 }, 11092 { /* coeff band 5 */ 11093 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128}, 11094 { 124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128}, 11095 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128} 11096 }, 11097 { /* coeff band 6 */ 11098 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128}, 11099 { 121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128}, 11100 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128} 11101 }, 11102 { /* coeff band 7 */ 11103 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128}, 11104 { 203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11105 { 137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128} 11106 } 11107 }, 11108 { /* block type 2 */ 11109 { /* coeff band 0 */ 11110 { 253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128}, 11111 { 175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128}, 11112 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128} 11113 }, 11114 { /* coeff band 1 */ 11115 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128}, 11116 { 239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128}, 11117 { 155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128} 11118 }, 11119 { /* coeff band 2 */ 11120 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128}, 11121 { 201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128}, 11122 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128} 11123 }, 11124 { /* coeff band 3 */ 11125 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128}, 11126 { 223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128}, 11127 { 141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128} 11128 }, 11129 { /* coeff band 4 */ 11130 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128}, 11131 { 190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128}, 11132 { 149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11133 }, 11134 { /* coeff band 5 */ 11135 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11136 { 247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11137 { 240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11138 }, 11139 { /* coeff band 6 */ 11140 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128}, 11141 { 213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128}, 11142 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11143 }, 11144 { /* coeff band 7 */ 11145 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11146 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, 11147 { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128} 11148 } 11149 }, 11150 { /* block type 3 */ 11151 { /* coeff band 0 */ 11152 { 202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255}, 11153 { 126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128}, 11154 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128} 11155 }, 11156 { /* coeff band 1 */ 11157 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128}, 11158 { 166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128}, 11159 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128} 11160 }, 11161 { /* coeff band 2 */ 11162 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128}, 11163 { 124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128}, 11164 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128} 11165 }, 11166 { /* coeff band 3 */ 11167 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128}, 11168 { 149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128}, 11169 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128} 11170 }, 11171 { /* coeff band 4 */ 11172 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128}, 11173 { 123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128}, 11174 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128} 11175 }, 11176 { /* coeff band 5 */ 11177 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128}, 11178 { 168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128}, 11179 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128} 11180 }, 11181 { /* coeff band 6 */ 11182 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128}, 11183 { 141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128}, 11184 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128} 11185 }, 11186 { /* coeff band 7 */ 11187 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11188 { 244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128}, 11189 { 238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128} 11190 } 11191 } 11192 }; 11194 static const 11195 unsigned char k_mv_entropy_update_probs[2][MV_PROB_CNT] = 11196 { 11197 { 11198 237, 11199 246, 11200 253, 253, 254, 254, 254, 254, 254, 11201 254, 254, 254, 254, 254, 250, 250, 252, 254, 254 11202 }, 11203 { 11204 231, 11205 243, 11206 245, 253, 254, 254, 254, 254, 254, 11207 254, 254, 254, 254, 254, 251, 251, 254, 254, 254 11208 } 11209 }; 11211 static const 11212 unsigned char k_default_mv_probs[2][MV_PROB_CNT] = 11213 { 11214 { // row 11215 162, // is short 11216 128, // sign 11217 225, 146, 172, 147, 214, 39, 156, // short tree 11218 128, 129, 132, 75, 145, 178, 206, 239, 254, 254 // long bits 11219 }, 11220 { 11221 164, 11222 128, 11223 204, 170, 119, 235, 140, 230, 228, 11224 128, 130, 130, 74, 148, 180, 203, 236, 254, 254 11226 } 11227 }; 11229 ---- End code block ---------------------------------------- 11231 20.19. vpx_codec_internal.h 11233 ---- Begin code block -------------------------------------- 11235 /* 11236 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 11237 * 11238 * Use of this source code is governed by a BSD-style license 11239 * that can be found in the LICENSE file in the root of the source 11240 * tree. An additional intellectual property rights grant can be 11241 * found in the file PATENTS. All contributing project authors may 11242 * be found in the AUTHORS file in the root of the source tree. 11243 */ 11245 /*!\file vpx_codec_internal.h 11246 * \brief Describes the decoder algorithm interface for algorithm 11247 * implementations. 11248 * 11249 * This file defines the private structures and data types that are 11250 * only relevant to implementing an algorithm, as opposed to using 11251 * it. 11252 * 11253 * To create a decoder algorithm class, an interface structure is put 11254 * into the global namespace: 11255 *
11256	    *     my_codec.c:
11257	    *       vpx_codec_iface_t my_codec = {
11258	    *           "My Codec v1.0",
11259	    *           VPX_CODEC_ALG_ABI_VERSION,
11260	    *           ...
11261	    *       };
11262	    *     
11263 * 11264 * An application instantiates a specific decoder instance by using 11265 * vpx_codec_init() and a pointer to the algorithm's interface 11266 * structure: 11267 *
11268	    *     my_app.c:
11269	    *       extern vpx_codec_iface_t my_codec;
11270	    *       {
11271	    *           vpx_codec_ctx_t algo;
11272	    *           res = vpx_codec_init(&algo, &my_codec);
11273	    *       }
11274	    *     
11275 * 11276 * Once initialized, the instance is manged using other functions 11277 * from the vpx_codec_* family. 11278 */ 11279 #ifndef VPX_CODEC_INTERNAL_H 11280 #define VPX_CODEC_INTERNAL_H 11281 #include "vpx_decoder.h" 11282 #include 11284 /*!\brief Current ABI version number 11285 * 11286 * \internal 11287 * If this file is altered in any way that changes the ABI, this 11288 * value must be bumped. Examples include, but are not limited to, 11289 * changing types, removing or reassigning enums, 11290 * adding/removing/rearranging fields to structures 11291 */ 11292 #define VPX_CODEC_INTERNAL_ABI_VERSION (3) 11294 typedef struct vpx_codec_alg_priv vpx_codec_alg_priv_t; 11296 /*!\brief init function pointer prototype 11297 * 11298 * Performs algorithm-specific initialization of the decoder context. 11299 * This function is called by the generic vpx_codec_init() wrapper 11300 * function, so plugins implementing this interface may trust the 11301 * input parameters to be properly initialized. 11302 * 11303 * \param[in] ctx Pointer to this instance's context 11304 * \retval #VPX_CODEC_OK 11305 * The input stream was recognized and decoder initialized. 11306 * \retval #VPX_CODEC_MEM_ERROR 11307 * Memory operation failed. 11308 */ 11309 typedef vpx_codec_err_t (*vpx_codec_init_fn_t)(vpx_codec_ctx_t *ctx); 11311 /*!\brief destroy function pointer prototype 11312 * 11313 * Performs algorithm-specific destruction of the decoder context. 11314 * This function is called by the generic vpx_codec_destroy() wrapper 11315 * function, so plugins implementing this interface may trust the 11316 * input parameters to be properly initialized. 11318 * 11319 * \param[in] ctx Pointer to this instance's context 11320 * \retval #VPX_CODEC_OK 11321 * The input stream was recognized and decoder initialized. 11322 * \retval #VPX_CODEC_MEM_ERROR 11323 * Memory operation failed. 11324 */ 11325 typedef vpx_codec_err_t (*vpx_codec_destroy_fn_t)( 11326 vpx_codec_alg_priv_t *ctx); 11328 /*!\brief parse stream info function pointer prototype 11329 * 11330 * Performs high level parsing of the bitstream. This function is 11331 * called by the generic vpx_codec_parse_stream() wrapper function, 11332 * so plugins implementing this interface may trust the input 11333 * parameters to be properly initialized. 11334 * 11335 * \param[in] data Pointer to a block of data to parse 11336 * \param[in] data_sz Size of the data buffer 11337 * \param[in,out] si Pointer to stream info to update. The size 11338 * member \ref MUST be properly initialized, 11339 * but \ref MAY be clobbered by the 11340 * algorithm. This parameter \ref MAY be 11341 * NULL. 11342 * 11343 * \retval #VPX_CODEC_OK 11344 * Bitstream is parsable and stream information updated 11345 */ 11346 typedef vpx_codec_err_t (*vpx_codec_peek_si_fn_t)( 11347 const uint8_t *data, 11348 unsigned int data_sz, 11349 vpx_codec_stream_info_t *si); 11351 /*!\brief Return information about the current stream. 11352 * 11353 * Returns information about the stream that has been parsed during 11354 * decoding. 11355 * 11356 * \param[in] ctx Pointer to this instance's context 11357 * \param[in,out] si Pointer to stream info to update. The size 11358 * member \ref MUST be properly initialized, 11359 * but \ref MAY be clobbered by the 11360 * algorithm. This parameter \ref MAY be 11361 * NULL. 11362 * 11363 * \retval #VPX_CODEC_OK 11364 * Bitstream is parsable and stream information updated 11365 */ 11367 typedef vpx_codec_err_t (*vpx_codec_get_si_fn_t)( 11368 vpx_codec_alg_priv_t *ctx, 11369 vpx_codec_stream_info_t *si); 11371 /*!\brief control function pointer prototype 11372 * 11373 * This function is used to exchange algorithm specific data with the 11374 * decoder instance. This can be used to implement features specific 11375 * to a particular algorithm. 11376 * 11377 * This function is called by the generic vpx_codec_control() wrapper 11378 * function, so plugins implementing this interface may trust the 11379 * input parameters to be properly initialized. However, this 11380 * interface does not provide type safety for the exchanged data or 11381 * assign meanings to the control codes. Those details should be 11382 * specified in the algorithm's header file. In particular, the 11383 * ctrl_id parameter is guaranteed to exist in the algorithm's 11384 * control mapping table, and the data paramter may be NULL. 11385 * 11386 * 11387 * \param[in] ctx Pointer to this instance's context 11388 * \param[in] ctrl_id Algorithm specific control identifier 11389 * \param[in,out] data Data to exchange with algorithm instance. 11390 * 11391 * \retval #VPX_CODEC_OK 11392 * The internal state data was deserialized. 11393 */ 11394 typedef vpx_codec_err_t (*vpx_codec_control_fn_t)( 11395 vpx_codec_alg_priv_t *ctx, 11396 int ctrl_id, 11397 va_list ap); 11399 /*!\brief control function pointer mapping 11400 * 11401 * This structure stores the mapping between control identifiers and 11402 * implementing functions. Each algorithm provides a list of these 11403 * mappings. This list is searched by the vpx_codec_control() wrapper 11404 * function to determine which function to invoke. The special 11405 * value {0, NULL} is used to indicate end-of-list, and must be 11406 * present. The special value {0, } can be used as a 11407 * catch-all mapping. This implies that ctrl_id values chosen by the 11408 * algorithm \ref MUST be non-zero. 11409 */ 11410 typedef const struct 11411 { 11412 int ctrl_id; 11413 vpx_codec_control_fn_t fn; 11414 } vpx_codec_ctrl_fn_map_t; 11415 /*!\brief decode data function pointer prototype 11416 * 11417 * Processes a buffer of coded data. If the processing results in a 11418 * new decoded frame becoming available, #VPX_CODEC_CB_PUT_SLICE and 11419 * #VPX_CODEC_CB_PUT_FRAME events are generated as appropriate. This 11420 * function is called by the generic vpx_codec_decode() wrapper 11421 * function, so plugins implementing this interface may trust the 11422 * input parameters to be properly initialized. 11423 * 11424 * \param[in] ctx Pointer to this instance's context 11425 * \param[in] data Pointer to this block of new coded data. If 11426 * NULL, a #VPX_CODEC_CB_PUT_FRAME event is 11427 * posted for the previously decoded frame. 11428 * \param[in] data_sz Size of the coded data, in bytes. 11429 * 11430 * \return Returns #VPX_CODEC_OK if the coded data was processed 11431 * completely and future pictures can be decoded without 11432 * error. Otherwise, see the descriptions of the other error 11433 * codes in ::vpx_codec_err_t for recoverability 11434 * capabilities. 11435 */ 11436 typedef vpx_codec_err_t (*vpx_codec_decode_fn_t)( 11437 vpx_codec_alg_priv_t *ctx, 11438 const uint8_t *data, 11439 unsigned int data_sz, 11440 void *user_priv, 11441 long deadline); 11443 /*!\brief Decoded frames iterator 11444 * 11445 * Iterates over a list of the frames available for display. The 11446 * iterator storage should be initialized to NULL to start the 11447 * iteration. Iteration is complete when this function returns NULL. 11448 * 11449 * The list of available frames becomes valid upon completion of the 11450 * vpx_codec_decode call, and remains valid until the next call to 11451 * vpx_codec_decode. 11452 * 11453 * \param[in] ctx Pointer to this instance's context 11454 * \param[in out] iter Iterator storage, initialized to NULL 11455 * 11456 * \return Returns a pointer to an image, if one is ready for 11457 * display. Frames produced will always be in PTS 11458 * (presentation time stamp) order. 11459 */ 11460 typedef vpx_image_t*(*vpx_codec_get_frame_fn_t)( 11461 vpx_codec_alg_priv_t *ctx, 11462 vpx_codec_iter_t *iter); 11464 /*\brief e_xternal Memory Allocation memory map get iterator 11465 * 11466 * Iterates over a list of the memory maps requested by the decoder. 11467 * The iterator storage should be initialized to NULL to start the 11468 * iteration. Iteration is complete when this function returns NULL. 11469 * 11470 * \param[in out] iter Iterator storage, initialized to NULL 11471 * 11472 * \return Returns a pointer to an memory segment descriptor, or NULL 11473 * to indicate end-of-list. 11474 */ 11475 typedef vpx_codec_err_t (*vpx_codec_get_mmap_fn_t)( 11476 const vpx_codec_ctx_t *ctx, 11477 vpx_codec_mmap_t *mmap, 11478 vpx_codec_iter_t *iter); 11480 /*\brief e_xternal Memory Allocation memory map set iterator 11481 * 11482 * Sets a memory descriptor inside the decoder instance. 11483 * 11484 * \param[in] ctx Pointer to this instance's context 11485 * \param[in] mmap Memory map to store. 11486 * 11487 * \retval #VPX_CODEC_OK 11488 * The memory map was accepted and stored. 11489 * \retval #VPX_CODEC_MEM_ERROR 11490 * The memory map was rejected. 11491 */ 11492 typedef vpx_codec_err_t (*vpx_codec_set_mmap_fn_t)( 11493 vpx_codec_ctx_t *ctx, 11494 const vpx_codec_mmap_t *mmap); 11496 typedef vpx_codec_err_t (*vpx_codec_encode_fn_t)( 11497 vpx_codec_alg_priv_t *ctx, 11498 const vpx_image_t *img, 11499 vpx_codec_pts_t pts, 11500 unsigned long duration, 11501 vpx_enc_frame_flags_t flags, 11502 unsigned long deadline); 11503 typedef const vpx_codec_cx_pkt_t*(*vpx_codec_get_cx_data_fn_t)( 11504 vpx_codec_alg_priv_t *ctx, 11505 vpx_codec_iter_t *iter); 11507 typedef vpx_codec_err_t 11508 (*vpx_codec_enc_config_set_fn_t)( 11509 vpx_codec_alg_priv_t *ctx, 11510 const vpx_codec_enc_cfg_t *cfg); 11511 typedef vpx_fixed_buf_t * 11512 (*vpx_codec_get_global_headers_fn_t)(vpx_codec_alg_priv_t *ctx); 11514 typedef vpx_image_t * 11515 (*vpx_codec_get_preview_frame_fn_t)(vpx_codec_alg_priv_t *ctx); 11517 /*!\brief usage configuration mapping 11518 * 11519 * This structure stores the mapping between usage identifiers and 11520 * configuration structures. Each algorithm provides a list of these 11521 * mappings. This list is searched by the 11522 * vpx_codec_enc_config_default() wrapper function to determine which 11523 * config to return. The special value {-1, {0}} is used to indicate 11524 * end-of-list, and must be present. At least one mapping must be 11525 * present, in addition to the end-of-list. 11526 * 11527 */ 11528 typedef const struct 11529 { 11530 int usage; 11531 vpx_codec_enc_cfg_t cfg; 11532 } vpx_codec_enc_cfg_map_t; 11534 #define NOT_IMPLEMENTED 0 11536 /*!\brief Decoder algorithm interface interface 11537 * 11538 * All decoders \ref MUST expose a variable of this type. 11539 */ 11540 struct vpx_codec_iface 11541 { 11542 const char *name; 11543 int abi_version; 11544 vpx_codec_caps_t caps; 11545 vpx_codec_init_fn_t init; 11546 vpx_codec_destroy_fn_t destroy; 11547 vpx_codec_ctrl_fn_map_t *ctrl_maps; 11548 vpx_codec_get_mmap_fn_t get_mmap; 11549 vpx_codec_set_mmap_fn_t set_mmap; 11550 struct 11551 { 11552 vpx_codec_peek_si_fn_t peek_si; 11553 vpx_codec_get_si_fn_t get_si; 11554 vpx_codec_decode_fn_t decode; 11555 vpx_codec_get_frame_fn_t get_frame; 11556 } dec; 11557 struct 11558 { 11559 vpx_codec_enc_cfg_map_t *cfg_maps; 11560 vpx_codec_encode_fn_t encode; 11561 vpx_codec_get_cx_data_fn_t get_cx_data; 11562 vpx_codec_enc_config_set_fn_t cfg_set; 11563 vpx_codec_get_global_headers_fn_t get_glob_hdrs; 11564 vpx_codec_get_preview_frame_fn_t get_preview; 11565 } enc; 11566 }; 11568 /*!\brief Callback function pointer / user data pair storage */ 11569 typedef struct vpx_codec_priv_cb_pair 11570 { 11571 union 11572 { 11573 vpx_codec_put_frame_cb_fn_t put_frame; 11574 vpx_codec_put_slice_cb_fn_t put_slice; 11575 }; 11576 void *user_priv; 11577 } vpx_codec_priv_cb_pair_t; 11579 /*!\brief Instance private storage 11580 * 11581 * This structure is allocated by the algorithm's init function. It 11582 * can be extended in one of two ways. First, a second, algorithm 11583 * specific structure can be allocated and the priv member pointed to 11584 * it. Alternatively, this structure can be made the first member of 11585 * the algorithm specific structure, and the pointer casted to the 11586 * proper type. 11587 */ 11588 struct vpx_codec_priv 11589 { 11590 unsigned int sz; 11591 vpx_codec_iface_t *iface; 11592 struct vpx_codec_alg_priv *alg_priv; 11593 const char *err_detail; 11594 vpx_codec_flags_t init_flags; 11595 struct 11596 { 11597 vpx_codec_priv_cb_pair_t put_frame_cb; 11598 vpx_codec_priv_cb_pair_t put_slice_cb; 11599 } dec; 11600 struct 11601 { 11602 int tbd; 11603 struct vpx_fixed_buf cx_data_dst_buf; 11604 unsigned int cx_data_pad_before; 11605 unsigned int cx_data_pad_after; 11606 vpx_codec_cx_pkt_t cx_data_pkt; 11607 } enc; 11608 }; 11610 #undef VPX_CTRL_USE_TYPE 11611 #define VPX_CTRL_USE_TYPE(id, typ) \ 11612 static typ id##__value(va_list args) \ 11613 {return va_arg(args, typ);} \ 11614 static typ id##__convert(void *x)\ 11615 {\ 11616 union\ 11617 {\ 11618 void *x;\ 11619 typ d;\ 11620 } u;\ 11621 u.x = x;\ 11622 return u.d;\ 11623 } 11625 #undef VPX_CTRL_USE_TYPE_DEPRECATED 11626 #define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \ 11627 static typ id##__value(va_list args) \ 11628 {return va_arg(args, typ);} \ 11629 static typ id##__convert(void *x)\ 11630 {\ 11631 union\ 11632 {\ 11633 void *x;\ 11634 typ d;\ 11635 } u;\ 11636 u.x = x;\ 11637 return u.d;\ 11638 } 11640 #define CAST(id, arg) id##__value(arg) 11641 #define RECAST(id, x) id##__convert(x) 11643 /* Internal Utility Functions 11644 * 11645 * The following functions are indended to be used inside algorithms 11646 * as utilities for manipulating vpx_codec_* data structures. 11647 */ 11648 struct vpx_codec_pkt_list 11649 { 11650 unsigned int cnt; 11651 unsigned int max; 11652 struct vpx_codec_cx_pkt pkts[1]; 11653 }; 11655 #define vpx_codec_pkt_list_decl(n)\ 11656 union {struct vpx_codec_pkt_list head;\ 11657 struct {struct vpx_codec_pkt_list head;\ 11658 struct vpx_codec_cx_pkt pkts[n];} alloc;} 11660 #define vpx_codec_pkt_list_init(m)\ 11661 (m)->alloc.head.cnt = 0,\ 11662 (m)->alloc.head.max = \ 11663 sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0]) 11665 int 11666 vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *, 11667 const struct vpx_codec_cx_pkt *); 11669 const vpx_codec_cx_pkt_t* 11670 vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list, 11671 vpx_codec_iter_t *iter); 11673 #include 11674 #include 11675 struct vpx_internal_error_info 11676 { 11677 vpx_codec_err_t error_code; 11678 int has_detail; 11679 char detail[80]; 11680 int setjmp; 11681 jmp_buf jmp; 11682 }; 11684 static void vpx_internal_error(struct vpx_internal_error_info *info, 11685 vpx_codec_err_t error, 11686 const char *fmt, 11687 ...) 11688 { 11689 va_list ap; 11691 info->error_code = error; 11692 info->has_detail = 0; 11694 if (fmt) 11695 { 11696 size_t sz = sizeof(info->detail); 11697 info->has_detail = 1; 11698 va_start(ap, fmt); 11699 vsnprintf(info->detail, sz - 1, fmt, ap); 11700 va_end(ap); 11701 info->detail[sz-1] = '\0'; 11702 } 11704 if (info->setjmp) 11705 longjmp(info->jmp, info->error_code); 11706 } 11707 #endif 11709 ---- End code block ---------------------------------------- 11711 20.20. vpx_decoder.h 11713 ---- Begin code block -------------------------------------- 11715 /* 11716 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 11717 * 11718 * Use of this source code is governed by a BSD-style license 11719 * that can be found in the LICENSE file in the root of the source 11720 * tree. An additional intellectual property rights grant can be 11721 * found in the file PATENTS. All contributing project authors may 11722 * be found in the AUTHORS file in the root of the source tree. 11723 */ 11725 /*!\defgroup decoder Decoder Algorithm Interface 11726 * \ingroup codec 11727 * This abstraction allows applications using this decoder to easily 11728 * support multiple video formats with minimal code duplication. This 11729 * section describes the interface common to all decoders. 11730 * @{ 11731 */ 11733 /*!\file vpx_decoder.h 11734 * \brief Describes the decoder algorithm interface to applications. 11735 * 11736 * This file describes the interface between an application and a 11737 * video decoder algorithm. 11738 * 11739 */ 11740 #ifdef __cplusplus 11741 extern "C" { 11742 #endif 11743 #ifndef VPX_DECODER_H 11744 #define VPX_DECODER_H 11745 #include "vpx_codec.h" 11747 /*!\brief Current ABI version number 11748 * 11749 * \internal 11750 * If this file is altered in any way that changes the ABI, this 11751 * value must be bumped. Examples include, but are not limited 11752 * to, changing types, removing or reassigning enums, 11753 * adding/removing/rearranging fields to structures 11754 */ 11755 #define VPX_DECODER_ABI_VERSION (2 + VPX_CODEC_ABI_VERSION) 11757 /*! \brief Decoder capabilities bitfield 11758 * 11759 * Each decoder advertises the capabilities it supports as part 11760 * of its ::vpx_codec_iface_t interface structure. Capabilities 11761 * are extra interfaces or functionality, and are not required 11762 * to be supported by a decoder. 11763 * 11764 * The available flags are specifiedby VPX_CODEC_CAP_* defines. 11765 */ 11766 #define VPX_CODEC_CAP_PUT_SLICE 0x10000 /**< Will issue put_slice 11767 callbacks */ 11768 #define VPX_CODEC_CAP_PUT_FRAME 0x20000 /**< Will issue put_frame 11769 callbacks */ 11770 #define VPX_CODEC_CAP_POSTPROC 0x40000 /**< Can postprocess decoded 11771 frame */ 11773 /*! \brief Initialization-time Feature Enabling 11774 * 11775 * Certain codec features must be known at initialization time, 11776 * to allow for proper memory allocation. 11777 * 11778 * The available flags are specified by VPX_CODEC_USE_* defines. 11779 */ 11780 #define VPX_CODEC_USE_POSTPROC 0x10000 /**< Postprocess decoded 11781 frame */ 11783 /*!\brief Stream properties 11784 * 11785 * This structure is used to query or set properties of the 11786 * decoded stream. Algorithms may extend this structure with data 11787 * specific to their bitstream by setting the sz member 11788 * appropriately. 11789 */ 11790 typedef struct vpx_codec_stream_info 11791 { 11792 unsigned int sz; /**< Size of this structure */ 11793 unsigned int w; /**< Width (or 0 for unknown/default) */ 11794 unsigned int h; /**< Height (or 0 for unknown/default) */ 11795 unsigned int is_kf; /**< Current frame is a keyframe */ 11796 } vpx_codec_stream_info_t; 11798 /* REQUIRED FUNCTIONS 11799 * 11800 * The following functions are required to be implemented for all 11801 * decoders. They represent the base case functionality expected 11802 * of all decoders. 11803 */ 11805 /*!\brief Initialization Configurations 11806 * 11807 * This structure is used to pass init time configuration options 11808 * to the decoder. 11809 */ 11810 typedef struct vpx_codec_dec_cfg 11811 { 11812 unsigned int threads; /**< Maximum number of threads to use, 11813 default 1 */ 11814 unsigned int w; /**< Width */ 11815 unsigned int h; /**< Height */ 11816 } vpx_codec_dec_cfg_t; /**< alias for struct vpx_codec_dec_cfg */ 11818 /*!\brief Initialize a decoder instance 11819 * 11820 * Initializes a decoder context using the given interface. 11821 * Applications should call the vpx_codec_dec_init convenience 11822 * macro instead of this function directly, to ensure that the 11823 * ABI version number parameter is properly initialized. 11824 * 11825 * In XMA mode (activated by setting VPX_CODEC_USE_XMA in the 11826 * flags parameter), the storage pointed to by the cfg parameter 11827 * must be kept readable and stable until all memory maps have 11828 * been set. 11829 * 11830 * \param[in] ctx Pointer to this instance's context. 11831 * \param[in] iface Pointer to the alogrithm interface to 11832 * use. 11833 * \param[in] cfg Configuration to use, if known. May be 11834 * NULL. 11835 * \param[in] flags Bitfield of VPX_CODEC_USE_* flags 11836 * \param[in] ver ABI version number. Must be set to 11837 * VPX_DECODER_ABI_VERSION 11838 * \retval #VPX_CODEC_OK 11839 * The decoder algorithm initialized. 11840 * \retval #VPX_CODEC_MEM_ERROR 11841 * Memory allocation failed. 11842 */ 11843 vpx_codec_err_t vpx_codec_dec_init_ver( 11844 vpx_codec_ctx_t *ctx, 11845 vpx_codec_iface_t *iface, 11846 vpx_codec_dec_cfg_t *cfg, 11847 vpx_codec_flags_t flags, 11848 int ver); 11850 /*!\brief Convenience macro for vpx_codec_dec_init_ver() 11851 * 11852 * Ensures the ABI version parameter is properly set. 11853 */ 11854 #define vpx_codec_dec_init(ctx, iface, cfg, flags) \ 11855 vpx_codec_dec_init_ver(ctx, iface, cfg, flags, \ 11856 VPX_DECODER_ABI_VERSION) 11858 /*!\brief Parse stream info from a buffer 11859 * 11860 * Performs high level parsing of the bitstream. Construction of 11861 * a decoder context is not necessary. Can be used to determine 11862 * if the bitstream is of the proper format, and to extract 11863 * information from the stream. 11864 * 11865 * \param[in] iface Pointer to the alogrithm interface 11866 * \param[in] data Pointer to a block of data to parse 11867 * \param[in] data_sz Size of the data buffer 11868 * \param[in,out] si Pointer to stream info to update. The 11869 * size member 11870 * \ref MUST be properly initialized, but 11871 * \ref MAY be clobbered by the 11872 * algorithm. This parameter \ref MAY be 11873 * NULL. 11874 * 11875 * \retval #VPX_CODEC_OK 11876 * Bitstream is parsable and stream information updated 11877 */ 11878 vpx_codec_err_t vpx_codec_peek_stream_info( 11879 vpx_codec_iface_t *iface, 11880 const uint8_t *data, 11881 unsigned int data_sz, 11882 vpx_codec_stream_info_t *si); 11884 /*!\brief Return information about the current stream. 11885 * 11886 * Returns information about the stream that has been parsed 11887 * during decoding. 11888 * 11889 * \param[in] ctx Pointer to this instance's context 11890 * \param[in,out] si Pointer to stream info to update. The 11891 * size member \ref MUST be properly 11892 * initialized, but \ref MAY be clobbered 11893 * by the algorithm. This parameter \ref 11894 * MAY be NULL. 11895 * 11896 * \retval #VPX_CODEC_OK 11897 * Bitstream is parsable and stream information updated 11898 */ 11899 vpx_codec_err_t vpx_codec_get_stream_info( 11900 vpx_codec_ctx_t *ctx, 11901 vpx_codec_stream_info_t *si); 11903 /*!\brief Decode data 11904 * 11905 * Processes a buffer of coded data. If the processing results in 11906 * a new decoded frame becoming available, PUT_SLICE and 11907 * PUT_FRAME events may be generated, as appropriate. Encoded 11908 * data \ref MUST be passed in DTS (decode time stamp) order. 11909 * Frames produced will always be in PTS (presentation time 11910 * stamp) order. 11911 * 11912 * \param[in] ctx Pointer to this instance's context 11913 * \param[in] data Pointer to this block of new coded 11914 * data. If NULL, a 11915 * VPX_CODEC_CB_PUT_FRAME event is posted 11916 * for the previously decoded frame. 11917 * \param[in] data_sz Size of the coded data, in bytes. 11918 * \param[in] user_priv Application specific data to associate 11919 * with this frame. 11920 * \param[in] deadline Soft deadline the decoder should 11921 * attempt to meet, in us. Set to zero 11922 * for unlimited. 11923 * 11924 * \return Returns #VPX_CODEC_OK if the coded data was processed 11925 * completely and future pictures can be decoded without 11926 * error. Otherwise, see the descriptions of the other 11927 * error codes in ::vpx_codec_err_t for recoverability 11928 * capabilities. 11929 */ 11930 vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, 11931 const uint8_t *data, 11932 unsigned int data_sz, 11933 void *user_priv, 11934 long deadline); 11936 /*!\brief Decoded frames iterator 11937 * 11938 * Iterates over a list of the frames available for display. The 11939 * iterator storage should be initialized to NULL to start the 11940 * iteration. Iteration is complete when this function returns 11941 * NULL. 11942 * 11943 * The list of available frames becomes valid upon completion of 11944 * the vpx_codec_decode call, and remains valid until the next 11945 * call to vpx_codec_decode. 11946 * 11947 * \param[in] ctx Pointer to this instance's context 11948 * \param[in,out] iter Iterator storage, initialized to NULL 11949 * 11950 * \return Returns a pointer to an image, if one is ready for 11951 * display. Frames produced will always be in PTS 11952 * (presentation time stamp) order. 11953 */ 11954 vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, 11955 vpx_codec_iter_t *iter); 11957 /*!\defgroup cap_put_frame Frame-Based Decoding Functions 11958 * 11959 * The following functions are required to be implemented for all 11960 * decoders that advertise the VPX_CODEC_CAP_PUT_FRAME 11961 * capability. Calling these functions for codecs that don't 11962 * advertise this capability will result in an error code being 11963 * returned, usually VPX_CODEC_ERROR 11964 * @{ 11965 */ 11967 /*!\brief put frame callback prototype 11968 * 11969 * This callback is invoked by the decoder to notify the 11970 * application of the availability of decoded image data. 11971 */ 11972 typedef void (*vpx_codec_put_frame_cb_fn_t)( 11973 void *user_priv, 11974 const vpx_image_t *img); 11976 /*!\brief Register for notification of frame completion. 11977 * 11978 * Registers a given function to be called when a decoded frame 11979 * is available. 11980 * 11981 * \param[in] ctx Pointer to this instance's context 11982 * \param[in] cb Pointer to the callback function 11983 * \param[in] user_priv User's private data 11984 * 11985 * \retval #VPX_CODEC_OK 11986 * Callback successfully registered. 11987 * \retval #VPX_CODEC_ERROR 11988 * Decoder context not initialized, or algorithm not capable 11989 * of posting slice completion. 11990 */ 11991 vpx_codec_err_t vpx_codec_register_put_frame_cb( 11992 vpx_codec_ctx_t *ctx, 11993 vpx_codec_put_frame_cb_fn_t cb, 11994 void *user_priv); 11996 /*!@} - end defgroup cap_put_frame */ 11998 /*!\defgroup cap_put_slice Slice-Based Decoding Functions 11999 * 12000 * The following functions are required to be implemented for all 12001 * decoders that advertise the VPX_CODEC_CAP_PUT_SLICE 12002 * capability. Calling these functions for codecs that don't 12003 * advertise this capability will result in an error code being 12004 * returned, usually VPX_CODEC_ERROR 12005 * @{ 12006 */ 12008 /*!\brief put slice callback prototype 12009 * 12010 * This callback is invoked by the decoder to notify the 12011 * application of the availability of partially decoded image 12012 * data. 12013 */ 12014 typedef void (*vpx_codec_put_slice_cb_fn_t)( 12015 void *user_priv, 12016 const vpx_image_t *img, 12017 const vpx_image_rect_t *valid, 12018 const vpx_image_rect_t *update); 12020 /*!\brief Register for notification of slice completion. 12021 * 12022 * Registers a given function to be called when a decoded slice 12023 * is available. 12024 * 12025 * \param[in] ctx Pointer to this instance's context 12026 * \param[in] cb Pointer to the callback function 12027 * \param[in] user_priv User's private data 12028 * 12029 * \retval #VPX_CODEC_OK 12030 * Callback successfully registered. 12031 * \retval #VPX_CODEC_ERROR 12032 * Decoder context not initialized, or algorithm not capable 12033 * of posting slice completion. 12034 */ 12035 vpx_codec_err_t vpx_codec_register_put_slice_cb( 12036 vpx_codec_ctx_t *ctx, 12037 vpx_codec_put_slice_cb_fn_t cb, 12038 void *user_priv); 12040 /*!@} - end defgroup cap_put_slice*/ 12042 /*!@} - end defgroup decoder*/ 12044 #endif 12046 #ifdef __cplusplus 12047 } 12048 #endif 12050 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 12051 #include "vpx_decoder_compat.h" 12052 #endif 12054 ---- End code block ---------------------------------------- 12056 20.21. vpx_decoder_compat.h 12058 ---- Begin code block -------------------------------------- 12060 /* 12061 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 12062 * 12063 * Use of this source code is governed by a BSD-style license 12064 * that can be found in the LICENSE file in the root of the source 12065 * tree. An additional intellectual property rights grant can be 12066 * found in the file PATENTS. All contributing project authors may 12067 * be found in the AUTHORS file in the root of the source tree. 12069 */ 12071 /*!\defgroup decoder Common Decoder Algorithm Interface 12072 * This abstraction allows applications using this decoder to easily 12073 * support multiple video formats with minimal code duplication. This 12074 * section describes the interface common to all codecs. 12075 * @{ 12076 */ 12078 /*!\file 12079 * \brief Provides a compatibility layer between version 1 and 2 of 12080 * this API. 12081 * 12082 * This interface has been deprecated. Only existing code should make 12083 * use of this interface, and therefore, it is only thinly 12084 * documented. Existing code should be ported to the vpx_codec_* API. 12085 */ 12086 #ifdef __cplusplus 12087 extern "C" { 12088 #endif 12090 #ifndef VPX_DECODER_COMPAT_H 12091 #define VPX_DECODER_COMPAT_H 12093 /*!\brief Decoder algorithm return codes */ 12094 typedef enum { 12095 /*!\brief Operation completed without error */ 12096 VPX_DEC_OK = VPX_CODEC_OK, 12098 /*!\brief Unspecified error */ 12099 VPX_DEC_ERROR = VPX_CODEC_ERROR, 12101 /*!\brief Memory operation failed */ 12102 VPX_DEC_MEM_ERROR = VPX_CODEC_MEM_ERROR, 12104 /*!\brief ABI version mismatch */ 12105 VPX_DEC_ABI_MISMATCH = VPX_CODEC_ABI_MISMATCH, 12107 /*!\brief The given bitstream is not supported. 12108 * 12109 * The bitstream was unable to be parsed at the highest 12110 * level. The decoder is unable to proceed. This error \ref 12111 * SHOULD be treated as fatal to the stream. 12112 */ 12113 VPX_DEC_UNSUP_BITSTREAM = VPX_CODEC_UNSUP_BITSTREAM, 12115 /*!\brief Encoded bitstream uses an unsupported feature 12116 * 12117 * The decoder does not implement a feature required by the 12118 * encoder. This return code should only be used for features 12119 * that prevent future pictures from being properly decoded. 12120 * This error \ref MAY be treated as fatal to the stream or 12121 * \ref MAY be treated as fatal to the current GOP. 12122 */ 12123 VPX_DEC_UNSUP_FEATURE = VPX_CODEC_UNSUP_FEATURE, 12125 /*!\brief The coded data for this stream is corrupt or 12126 * incomplete 12127 * 12128 * There was a problem decoding the current frame. This 12129 * return code should only be used for failures that prevent 12130 * future pictures from being properly decoded. This error 12131 * \ref MAY be treated as fatal to the stream or \ref MAY be 12132 * treated as fatal to the current GOP. If decoding is 12133 * continued for the current GOP, artifacts may be present. 12134 */ 12135 VPX_DEC_CORRUPT_FRAME = VPX_CODEC_CORRUPT_FRAME, 12137 /*!\brief An application-supplied parameter is not valid. 12138 * 12139 */ 12140 VPX_DEC_INVALID_PARAM = VPX_CODEC_INVALID_PARAM, 12142 /*!\brief An iterator reached the end of list. 12143 * 12144 */ 12145 VPX_DEC_LIST_END = VPX_CODEC_LIST_END 12147 } 12148 vpx_dec_err_t; 12150 /*! \brief Decoder capabilities bitfield 12151 * 12152 * Each decoder advertises the capabilities it supports as part 12153 * of its ::vpx_dec_iface_t interface structure. Capabilities 12154 * are extra interfaces or functionality, and are not required 12155 * to be supported by a decoder. 12156 * 12157 * The available flags are specified by VPX_DEC_CAP_* defines. 12158 */ 12159 typedef int vpx_dec_caps_t; 12160 #define VPX_DEC_CAP_PUT_SLICE 0x0001 /**< Will issue put_slice 12161 callbacks */ 12162 #define VPX_DEC_CAP_PUT_FRAME 0x0002 /**< Will issue put_frame 12163 callbacks */ 12165 #define VPX_DEC_CAP_XMA 0x0004 /**< Supports eXternal Memory 12166 Allocation */ 12168 /*!\brief Stream properties 12169 * 12170 * This structure is used to query or set properties of the 12171 * decoded stream. Algorithms may extend this structure with 12172 * data specific to their bitstream by setting the sz member 12173 * appropriately. 12174 */ 12175 #if 1 12176 typedef vpx_codec_stream_info_t vpx_dec_stream_info_t; 12177 #else 12178 typedef struct 12179 { 12180 unsigned int sz; /**< Size of this structure */ 12181 unsigned int w; /**< Width (or 0 for unknown/default) */ 12182 unsigned int h; /**< Height (or 0 for unknown/default) */ 12183 unsigned int is_kf; /**< Current frame is a keyframe */ 12184 } vpx_dec_stream_info_t; 12185 #endif 12187 /*!\brief Decoder interface structure. 12188 * 12189 * Contains function pointers and other data private to the 12190 * decoder implementation. This structure is opaque to the 12191 * application. 12192 */ 12193 typedef const struct vpx_codec_iface vpx_dec_iface_t; 12194 typedef struct vpx_codec_priv vpx_dec_priv_t; 12196 /*!\brief Iterator 12197 * 12198 * Opaque storage used for iterating over lists. 12199 */ 12200 typedef vpx_codec_iter_t vpx_dec_iter_t; 12202 /*!\brief Decoder context structure 12203 * 12204 * All decoders \ref MUST support this context structure fully. 12205 * In general, this data should be considered private to the 12206 * decoder algorithm, and not be manipulated or examined by the 12207 * calling application. Applications may reference the 'name' 12208 * member to get a printable description of the algorithm. 12209 */ 12210 #if 1 12211 typedef vpx_codec_ctx_t vpx_dec_ctx_t; 12213 #else 12214 typedef struct 12215 { 12216 const char *name; /**< Printable interface name */ 12217 vpx_dec_iface_t *iface; /**< Interface pointers */ 12218 vpx_dec_err_t err; /**< Last returned error */ 12219 vpx_dec_priv_t *priv; /**< Algorithm private storage */ 12220 } vpx_dec_ctx_t; 12221 #endif 12223 /*!\brief Return the build configuration 12224 * 12225 * Returns a printable string containing an encoded version of 12226 * the build configuration. This may be useful to vpx support. 12227 * 12228 */ 12229 const char *vpx_dec_build_config(void) DEPRECATED; 12231 /*!\brief Return the name for a given interface 12232 * 12233 * Returns a human readable string for name of the given decoder 12234 * interface. 12235 * 12236 * \param[in] iface Interface pointer 12237 * 12238 */ 12239 const char *vpx_dec_iface_name( 12240 vpx_dec_iface_t *iface) DEPRECATED; 12242 /*!\brief Convert error number to printable string 12243 * 12244 * Returns a human readable string for the last error returned 12245 * by the algorithm. The returned error will be one line and will 12246 * not contain any newline characters. 12247 * 12248 * 12249 * \param[in] err Error number. 12250 * 12251 */ 12252 const char *vpx_dec_err_to_string(vpx_dec_err_t err) DEPRECATED; 12254 /*!\brief Retrieve error synopsis for decoder context 12255 * 12256 * Returns a human readable string for the last error returned by 12257 * the algorithm. The returned error will be one line and will 12258 * not contain any newline characters. 12259 * 12260 * 12261 * \param[in] ctx Pointer to this instance's context. 12262 * 12263 */ 12264 const char *vpx_dec_error(vpx_dec_ctx_t *ctx) DEPRECATED; 12266 /*!\brief Retrieve detailed error information for decoder context 12267 * 12268 * Returns a human readable string providing detailed information 12269 * about the last error. 12270 * 12271 * \param[in] ctx Pointer to this instance's context. 12272 * 12273 * \retval NULL 12274 * No detailed information is available. 12275 */ 12276 const char *vpx_dec_error_detail(vpx_dec_ctx_t *ctx) DEPRECATED; 12278 /* REQUIRED FUNCTIONS 12279 * 12280 * The following functions are required to be implemented for all 12281 * decoders. They represent the base case functionality expected 12282 * of all decoders. 12283 */ 12285 /*!\brief Initialize a decoder instance 12286 * 12287 * Initializes a decoder context using the given interface. 12288 * Applications should call the vpx_dec_init convenience macro 12289 * instead of this function directly, to ensure that the ABI 12290 * version number parameter is properly initialized. 12291 * 12292 * \param[in] ctx Pointer to this instance's context. 12293 * \param[in] iface Pointer to the algorithm interface to use. 12294 * \param[in] ver ABI version number. Must be set to 12295 * VPX_DECODER_ABI_VERSION 12296 * \retval #VPX_DEC_OK 12297 * The decoder algorithm initialized. 12298 * \retval #VPX_DEC_MEM_ERROR 12299 * Memory allocation failed. 12300 */ 12301 vpx_dec_err_t vpx_dec_init_ver( 12302 vpx_dec_ctx_t *ctx, 12303 vpx_dec_iface_t *iface, 12304 int ver) DEPRECATED; 12306 #define vpx_dec_init(ctx, iface) \ 12307 vpx_dec_init_ver(ctx, iface, VPX_DECODER_ABI_VERSION) 12309 /*!\brief Destroy a decoder instance 12310 * 12311 * Destroys a decoder context, freeing any associated memory 12312 * buffers. 12313 * 12314 * \param[in] ctx Pointer to this instance's context 12315 * 12316 * \retval #VPX_DEC_OK 12317 * The decoder algorithm initialized. 12318 * \retval #VPX_DEC_MEM_ERROR 12319 * Memory allocation failed. 12320 */ 12321 vpx_dec_err_t vpx_dec_destroy(vpx_dec_ctx_t *ctx) DEPRECATED; 12323 /*!\brief Get the capabilities of an algorithm. 12324 * 12325 * Retrieves the capabilities bitfield from the algorithm's 12326 * interface. 12327 * 12328 * \param[in] iface Pointer to the algorithm interface 12329 * 12330 */ 12331 vpx_dec_caps_t vpx_dec_get_caps( 12332 vpx_dec_iface_t *iface) DEPRECATED; 12334 /*!\brief Parse stream info from a buffer 12335 * 12336 * Performs high level parsing of the bitstream. Construction of 12337 * a decoder context is not necessary. Can be used to determine 12338 * if the bitstream is of the proper format, and to extract 12339 * information from the stream. 12340 * 12341 * \param[in] iface Pointer to the algorithm interface 12342 * \param[in] data Pointer to a block of data to parse 12343 * \param[in] data_sz Size of the data buffer 12344 * \param[in,out] si Pointer to stream info to update. The 12345 * size member \ref MUST be properly 12346 * initialized, but \ref MAY be 12347 * clobbered by the algorithm. This 12348 * parameter \ref MAY be NULL. 12349 * 12350 * \retval #VPX_DEC_OK 12351 * Bitstream is parsable and stream information updated 12352 */ 12353 vpx_dec_err_t vpx_dec_peek_stream_info( 12354 vpx_dec_iface_t *iface, 12355 const uint8_t *data, 12356 unsigned int data_sz, 12357 vpx_dec_stream_info_t *si) DEPRECATED; 12359 /*!\brief Return information about the current stream. 12360 * 12361 * Returns information about the stream that has been parsed 12362 * during decoding. 12363 * 12364 * \param[in] ctx Pointer to this instance's context 12365 * \param[in,out] si Pointer to stream info to update. 12366 * The size member \ref MUST be properly 12367 * initialized, but \ref MAY be clobbered 12368 * by the algorithm. This parameter \ref 12369 * MAY be NULL. 12370 * 12371 * \retval #VPX_DEC_OK 12372 * Bitstream is parsable and stream information updated 12373 */ 12374 vpx_dec_err_t vpx_dec_get_stream_info( 12375 vpx_dec_ctx_t *ctx, 12376 vpx_dec_stream_info_t *si) DEPRECATED; 12378 /*!\brief Control algorithm 12379 * 12380 * This function is used to exchange algorithm specific data with 12381 * the decoder instance. This can be used to implement features 12382 * specific to a particular algorithm. 12383 * 12384 * This wrapper function dispatches the request to the helper 12385 * function associated with the given ctrl_id. It tries to call 12386 * this function transparently, but will return #VPX_DEC_ERROR if 12387 * the request could not be dispatched. 12388 * 12389 * \param[in] ctx Pointer to this instance's context 12390 * \param[in] ctrl_id Algorithm specific control 12391 * identifier 12392 * \param[in,out] data Data to exchange with algorithm 12393 * instance. 12395 * 12396 * \retval #VPX_DEC_OK 12397 * The control request was processed. 12398 * \retval #VPX_DEC_ERROR 12399 * The control request was not processed. 12400 * \retval #VPX_DEC_INVALID_PARAM 12401 * The data was not valid. 12402 */ 12403 vpx_dec_err_t vpx_dec_control(vpx_dec_ctx_t *ctx, 12404 int ctrl_id, 12405 void *data) DEPRECATED; 12407 /*!\brief Decode data 12408 * 12409 * Processes a buffer of coded data. If the processing results in 12410 * a new decoded frame becoming available, #VPX_DEC_CB_PUT_SLICE 12411 * and #VPX_DEC_CB_PUT_FRAME events may be generated, as 12412 * appropriate. Encoded data \ref MUST be passed in DTS (decode 12413 * time stamp) order. Frames produced will always be in PTS 12414 * (presentation time stamp) order. 12415 * 12416 * \param[in] ctx Pointer to this instance's context 12417 * \param[in] data Pointer to this block of new coded 12418 * data. If NULL, a VPX_DEC_CB_PUT_FRAME 12419 * event is posted for the previously 12420 * decoded frame. 12421 * \param[in] data_sz Size of the coded data, in bytes. 12422 * \param[in] user_priv Application specific data to associate 12423 * with this frame. 12424 * \param[in] rel_pts PTS relative to the previous frame, in 12425 * us. If unknown or unavailable, set to 12426 * zero. 12427 * 12428 * \return Returns #VPX_DEC_OK if the coded data was processed 12429 * completely and future pictures can be decoded without 12430 * error. Otherwise, see the descriptions of the other 12431 * error codes in ::vpx_dec_err_t for recoverability 12432 * capabilities. 12433 */ 12434 vpx_dec_err_t vpx_dec_decode( 12435 vpx_dec_ctx_t *ctx, 12436 uint8_t *data, 12437 unsigned int data_sz, 12438 void *user_priv, 12439 int rel_pts) DEPRECATED; 12441 /*!\brief Decoded frames iterator 12442 * 12443 * Iterates over a list of the frames available for display. The 12444 * iterator storage should be initialized to NULL to start the 12445 * iteration. Iteration is complete when this function returns 12446 * NULL. 12447 * 12448 * The list of available frames becomes valid upon completion of 12449 * the vpx_dec_decode call, and remains valid until the next call 12450 * to vpx_dec_decode. 12451 * 12452 * \param[in] ctx Pointer to this instance's context 12453 * \param[in out] iter Iterator storage, initialized to NULL 12454 * 12455 * \return Returns a pointer to an image, if one is ready for 12456 * display. Frames produced will always be in PTS 12457 * (presentation time stamp) order. 12458 */ 12459 vpx_image_t *vpx_dec_get_frame(vpx_dec_ctx_t *ctx, 12460 vpx_dec_iter_t *iter) DEPRECATED; 12462 /*!\defgroup cap_put_frame Frame-Based Decoding Functions 12463 * 12464 * The following functions are required to be implemented for all 12465 * decoders that advertise the VPX_DEC_CAP_PUT_FRAME capability. 12466 * Calling these functions for codecs that don't advertise this 12467 * capability will result in an error code being returned, 12468 * usually VPX_DEC_ERROR @{ 12469 */ 12471 /*!\brief put frame callback prototype 12472 * 12473 * This callback is invoked by the decoder to notify the 12474 * application of the availability of decoded image data. 12475 */ 12476 typedef void (*vpx_dec_put_frame_cb_fn_t)( 12477 void *user_priv, 12478 const vpx_image_t *img); 12480 /*!\brief Register for notification of frame completion. 12481 * 12482 * Registers a given function to be called when a decoded frame 12483 * is available. 12484 * 12485 * \param[in] ctx Pointer to this instance's context 12486 * \param[in] cb Pointer to the callback function 12487 * \param[in] user_priv User's private data 12488 * 12489 * \retval #VPX_DEC_OK 12490 * Callback successfully registered. 12491 * \retval #VPX_DEC_ERROR 12492 * Decoder context not initialized, or algorithm not capable 12493 * of posting slice completion. 12494 */ 12495 vpx_dec_err_t vpx_dec_register_put_frame_cb( 12496 vpx_dec_ctx_t *ctx, 12497 vpx_dec_put_frame_cb_fn_t cb, 12498 void *user_priv) DEPRECATED; 12500 /*!@} - end defgroup cap_put_frame */ 12502 /*!\defgroup cap_put_slice Slice-Based Decoding Functions 12503 * 12504 * The following functions are required to be implemented for all 12505 * decoders that advertise the VPX_DEC_CAP_PUT_SLICE capability. 12506 * Calling these functions for codecs that don't advertise this 12507 * capability will result in an error code being returned, 12508 * usually VPX_DEC_ERROR 12509 * @{ 12510 */ 12512 /*!\brief put slice callback prototype 12513 * 12514 * This callback is invoked by the decoder to notify the 12515 * application of the availability of partially decoded image 12516 * data. The 12517 */ 12518 typedef void (*vpx_dec_put_slice_cb_fn_t)(void *user_priv, 12519 const vpx_image_t *img, 12520 const vpx_image_rect_t *valid, 12521 const vpx_image_rect_t *update); 12523 /*!\brief Register for notification of slice completion. 12524 * 12525 * Registers a given function to be called when a decoded slice 12526 * is available. 12527 * 12528 * \param[in] ctx Pointer to this instance's context 12529 * \param[in] cb Pointer to the callback function 12530 * \param[in] user_priv User's private data 12531 * 12532 * \retval #VPX_DEC_OK 12533 * Callback successfully registered. 12535 * \retval #VPX_DEC_ERROR 12536 * Decoder context not initialized, or algorithm not capable 12537 * of posting slice completion. 12538 */ 12539 vpx_dec_err_t vpx_dec_register_put_slice_cb(vpx_dec_ctx_t *ctx, 12540 vpx_dec_put_slice_cb_fn_t cb, 12541 void *user_priv) DEPRECATED; 12543 /*!@} - end defgroup cap_put_slice*/ 12545 /*!\defgroup cap_xma External Memory Allocation Functions 12546 * 12547 * The following functions are required to be implemented for all 12548 * decoders that advertise the VPX_DEC_CAP_XMA capability. 12549 * Calling these functions for codecs that don't advertise this 12550 * capability will result in an error code being returned, 12551 * usually VPX_DEC_ERROR 12552 * @{ 12553 */ 12555 /*!\brief Memory Map Entry 12556 * 12557 * This structure is used to contain the properties of a memory 12558 * segment. It is populated by the decoder in the request phase, 12559 * and by the calling application once the requested allocation 12560 * has been performed. 12561 */ 12562 #if 1 12563 #define VPX_DEC_MEM_ZERO 0x1 /**< Segment must be zeroed by 12564 allocation */ 12565 #define VPX_DEC_MEM_WRONLY 0x2 /**< Segment need not be 12566 readable */ 12567 #define VPX_DEC_MEM_FAST 0x4 /**< Place in fast memory, if 12568 available */ 12569 typedef struct vpx_codec_mmap vpx_dec_mmap_t; 12570 #else 12571 typedef struct vpx_dec_mmap 12572 { 12573 /* 12574 * The following members are set by the codec when requesting 12575 * a segment 12576 */ 12577 unsigned int id; /**< identifier for the segment's 12578 contents */ 12579 unsigned long sz; /**< size of the segment, in bytes */ 12580 unsigned int align; /**< required alignment of the 12581 segment, in bytes */ 12583 unsigned int flags; /**< bitfield containing segment 12584 properties */ 12585 #define VPX_DEC_MEM_ZERO 0x1 /**< Segment must be zeroed by 12586 allocation */ 12587 #define VPX_DEC_MEM_WRONLY 0x2 /**< Segment need not be 12588 readable */ 12589 #define VPX_DEC_MEM_FAST 0x4 /**< Place in fast memory, if 12590 available */ 12592 /* The following members are to be filled in by the 12593 * allocation function */ 12594 void *base; /**< pointer to the allocated 12595 segment */ 12596 void (*dtor)(struct vpx_dec_mmap *map); /**< destructor to 12597 call */ 12598 void *priv; /**< allocator private storage */ 12599 } vpx_dec_mmap_t; 12600 #endif 12602 /*!\brief Initialize a decoder instance in external allocation 12603 * mode 12604 * 12605 * Initializes a decoder context using the given interface. 12606 * Applications should call the vpx_dec_xma_init convenience 12607 * macro instead of this function directly, to ensure that the 12608 * ABI version number parameter is properly initialized. 12609 * 12610 * \param[in] ctx Pointer to this instance's context. 12611 * \param[in] iface Pointer to the algorithm interface to 12612 * use. 12613 * \param[in] ver ABI version number. Must be set to 12614 * VPX_DECODER_ABI_VERSION 12615 * \retval #VPX_DEC_OK 12616 * The decoder algorithm initialized. 12617 * \retval #VPX_DEC_ERROR 12618 * Decoder does not support XMA mode. 12619 */ 12620 vpx_dec_err_t vpx_dec_xma_init_ver(vpx_dec_ctx_t *ctx, 12621 vpx_dec_iface_t *iface, 12622 int ver) DEPRECATED; 12623 #define vpx_dec_xma_init(ctx, iface) \ 12624 vpx_dec_xma_init_ver(ctx, iface, VPX_DECODER_ABI_VERSION) 12626 /*!\brief Iterate over the list of segments to allocate. 12627 * 12628 * Iterates over a list of the segments to allocate. The iterator 12629 * storage should be initialized to NULL to start the iteration. 12631 * Iteration is complete when this function returns 12632 * VPX_DEC_LIST_END. The amount of memory needed to allocate is 12633 * dependent upon the size of the encoded stream. This means that 12634 * the stream info structure must be known at allocation time. It 12635 * can be populated with the vpx_dec_peek_stream_info() function. 12636 * In cases where the stream to be decoded is not available at 12637 * allocation time, a fixed size must be requested. The decoder 12638 * will not be able to decode streams larger than the size used 12639 * at allocation time. 12640 * 12641 * \param[in] ctx Pointer to this instance's context. 12642 * \param[out] mmap Pointer to the memory map entry to 12643 * populate. 12644 * \param[in] si Pointer to the stream info. 12645 * \param[in out] iter Iterator storage, initialized to NULL 12646 * 12647 * \retval #VPX_DEC_OK 12648 * The memory map entry was populated. 12649 * \retval #VPX_DEC_ERROR 12650 * Decoder does not support XMA mode. 12651 * \retval #VPX_DEC_MEM_ERROR 12652 * Unable to determine segment size from stream info. 12653 */ 12654 vpx_dec_err_t vpx_dec_get_mem_map( 12655 vpx_dec_ctx_t *ctx, 12656 vpx_dec_mmap_t *mmap, 12657 const vpx_dec_stream_info_t *si, 12658 vpx_dec_iter_t *iter) DEPRECATED; 12660 /*!\brief Identify allocated segments to decoder instance 12661 * 12662 * Stores a list of allocated segments in the decoder. Segments 12663 * \ref MUST be passed in the order they are read from 12664 * vpx_dec_get_mem_map(), but may be passed in groups of any 12665 * size. Segments \ref MUST be set only once. The allocation 12666 * function \ref MUST ensure that the vpx_dec_mmap_t::base member 12667 * is non-NULL. If the segment requires cleanup handling (e.g., 12668 * calling free() or close()) then the vpx_dec_mmap_t::dtor 12669 * member \ref MUST be populated. 12670 * 12671 * \param[in] ctx Pointer to this instance's context. 12672 * \param[in] mmaps Pointer to the first memory map 12673 * entry in the list. 12674 * \param[in] num_maps Number of entries being set at this 12675 * time 12676 * 12677 * \retval #VPX_DEC_OK 12678 * The segment was stored in the decoder context. 12679 * \retval #VPX_DEC_ERROR 12680 * Decoder does not support XMA mode. 12681 * \retval #VPX_DEC_MEM_ERROR 12682 * Segment base address was not set, or segment was already 12683 * stored. 12685 */ 12686 vpx_dec_err_t vpx_dec_set_mem_map( 12687 vpx_dec_ctx_t *ctx, 12688 vpx_dec_mmap_t *mmaps, 12689 unsigned int num_maps) DEPRECATED; 12691 /*!@} - end defgroup cap_xma*/ 12692 /*!@} - end defgroup decoder*/ 12694 #endif 12695 #ifdef __cplusplus 12696 } 12697 #endif 12699 ---- End code block ---------------------------------------- 12701 20.22. vpx_image.c 12703 ---- Begin code block -------------------------------------- 12705 /* 12706 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 12707 * 12708 * Use of this source code is governed by a BSD-style license 12709 * that can be found in the LICENSE file in the root of the source 12710 * tree. An additional intellectual property rights grant can be 12711 * found in the file PATENTS. All contributing project authors may 12712 * be found in the AUTHORS file in the root of the source tree. 12713 */ 12715 #include 12716 #include 12717 #include "vpx/vpx_image.h" 12719 static vpx_image_t *img_alloc_helper(vpx_image_t *img, 12720 vpx_img_fmt_t fmt, 12721 unsigned int d_w, 12722 unsigned int d_h, 12723 unsigned int stride_align, 12724 unsigned char *img_data) 12725 { 12727 unsigned int h, w, s, xcs, ycs, bps; 12728 int align; 12730 /* Treat align==0 like align==1 */ 12731 if (!stride_align) 12732 stride_align = 1; 12734 /* Validate alignment (must be power of 2) */ 12735 if (stride_align & (stride_align - 1)) 12736 goto fail; 12738 /* Get sample size for this format */ 12739 switch (fmt) 12740 { 12741 case VPX_IMG_FMT_RGB32: 12742 case VPX_IMG_FMT_RGB32_LE: 12743 case VPX_IMG_FMT_ARGB: 12744 case VPX_IMG_FMT_ARGB_LE: 12745 bps = 32; 12746 break; 12747 case VPX_IMG_FMT_RGB24: 12748 case VPX_IMG_FMT_BGR24: 12749 bps = 24; 12750 break; 12751 case VPX_IMG_FMT_RGB565: 12752 case VPX_IMG_FMT_RGB565_LE: 12753 case VPX_IMG_FMT_RGB555: 12754 case VPX_IMG_FMT_RGB555_LE: 12755 case VPX_IMG_FMT_UYVY: 12756 case VPX_IMG_FMT_YUY2: 12757 case VPX_IMG_FMT_YVYU: 12758 bps = 16; 12759 break; 12760 case VPX_IMG_FMT_I420: 12761 case VPX_IMG_FMT_YV12: 12762 case VPX_IMG_FMT_VPXI420: 12763 case VPX_IMG_FMT_VPXYV12: 12764 bps = 12; 12765 break; 12766 default: 12767 bps = 16; 12768 break; 12769 } 12770 /* Get chroma shift values for this format */ 12771 switch (fmt) 12772 { 12773 case VPX_IMG_FMT_I420: 12774 case VPX_IMG_FMT_YV12: 12775 case VPX_IMG_FMT_VPXI420: 12776 case VPX_IMG_FMT_VPXYV12: 12777 xcs = 1; 12778 break; 12779 default: 12780 xcs = 0; 12781 break; 12782 } 12784 switch (fmt) 12785 { 12786 case VPX_IMG_FMT_I420: 12787 case VPX_IMG_FMT_YV12: 12788 case VPX_IMG_FMT_VPXI420: 12789 case VPX_IMG_FMT_VPXYV12: 12790 ycs = 1; 12791 break; 12792 default: 12793 ycs = 0; 12794 break; 12795 } 12797 /* Calculate storage sizes given the chroma subsampling */ 12798 align = (1 << xcs) - 1; 12799 w = (d_w + align) & ~align; 12800 align = (1 << ycs) - 1; 12801 h = (d_h + align) & ~align; 12802 s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8; 12803 s = (s + stride_align - 1) & ~(stride_align - 1); 12805 /* Allocate the new image */ 12806 if (!img) 12807 { 12808 img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t)); 12810 if (!img) 12811 goto fail; 12813 img->self_allocd = 1; 12814 } 12815 else 12816 { 12817 memset(img, 0, sizeof(vpx_image_t)); 12819 } 12821 img->img_data = img_data; 12823 if (!img_data) 12824 { 12825 img->img_data = malloc((fmt & VPX_IMG_FMT_PLANAR) ? 12826 h * w * bps / 8 : h * s); 12827 img->img_data_owner = 1; 12828 } 12830 if (!img->img_data) 12831 goto fail; 12833 img->fmt = fmt; 12834 img->w = w; 12835 img->h = h; 12836 img->x_chroma_shift = xcs; 12837 img->y_chroma_shift = ycs; 12838 img->bps = bps; 12840 /* Calculate strides */ 12841 img->stride[VPX_PLANE_Y] = img->stride[VPX_PLANE_ALPHA] = s; 12842 img->stride[VPX_PLANE_U] = img->stride[VPX_PLANE_V] = s >> xcs; 12844 /* Default viewport to entire image */ 12845 if (!vpx_img_set_rect(img, 0, 0, d_w, d_h)) 12846 return img; 12848 fail: 12849 vpx_img_free(img); 12850 return NULL; 12851 } 12853 vpx_image_t *vpx_img_alloc(vpx_image_t *img, 12854 vpx_img_fmt_t fmt, 12855 unsigned int d_w, 12856 unsigned int d_h, 12857 unsigned int stride_align) 12858 { 12859 return img_alloc_helper(img, fmt, d_w, d_h, stride_align, NULL); 12860 } 12862 vpx_image_t *vpx_img_wrap(vpx_image_t *img, 12863 vpx_img_fmt_t fmt, 12864 unsigned int d_w, 12865 unsigned int d_h, 12866 unsigned int stride_align, 12867 unsigned char *img_data) 12868 { 12869 return img_alloc_helper(img, fmt, d_w, d_h, stride_align, 12870 img_data); 12871 } 12873 int vpx_img_set_rect(vpx_image_t *img, 12874 unsigned int x, 12875 unsigned int y, 12876 unsigned int w, 12877 unsigned int h) 12878 { 12879 unsigned char *data; 12881 if (x + w <= img->w && y + h <= img->h) 12882 { 12883 img->d_w = w; 12884 img->d_h = h; 12886 /* Calculate plane pointers */ 12887 if (!(img->fmt & VPX_IMG_FMT_PLANAR)) 12888 { 12889 img->planes[VPX_PLANE_PACKED] = 12890 img->img_data + x * img->bps / 8 + y * 12891 img->stride[VPX_PLANE_PACKED]; 12892 } 12893 else 12894 { 12895 data = img->img_data; 12897 if (img->fmt & VPX_IMG_FMT_HAS_ALPHA) 12898 { 12899 img->planes[VPX_PLANE_ALPHA] = 12900 data + x + y * img->stride[VPX_PLANE_ALPHA]; 12901 data += img->h * img->stride[VPX_PLANE_ALPHA]; 12902 } 12904 img->planes[VPX_PLANE_Y] = 12905 data + x + y * img->stride[VPX_PLANE_Y]; 12906 data += img->h * img->stride[VPX_PLANE_Y]; 12908 if (!(img->fmt & VPX_IMG_FMT_UV_FLIP)) 12909 { 12910 img->planes[VPX_PLANE_U] = data 12911 + (x >> img->x_chroma_shift) 12912 + (y >> img->y_chroma_shift) * 12913 img->stride[VPX_PLANE_U]; 12914 data += (img->h >> img->y_chroma_shift) * 12915 img->stride[VPX_PLANE_U]; 12916 img->planes[VPX_PLANE_V] = data 12917 + (x >> img->x_chroma_shift) 12918 + (y >> img->y_chroma_shift) * 12919 img->stride[VPX_PLANE_V]; 12920 } 12921 else 12922 { 12923 img->planes[VPX_PLANE_V] = data 12924 + (x >> img->x_chroma_shift) 12925 + (y >> img->y_chroma_shift) * 12926 img->stride[VPX_PLANE_V]; 12927 data += (img->h >> img->y_chroma_shift) * 12928 img->stride[VPX_PLANE_V]; 12929 img->planes[VPX_PLANE_U] = data 12930 + (x >> img->x_chroma_shift) 12931 + (y >> img->y_chroma_shift) * 12932 img->stride[VPX_PLANE_U]; 12933 } 12934 } 12936 return 0; 12937 } 12939 return -1; 12940 } 12942 void vpx_img_flip(vpx_image_t *img) 12943 { 12944 /* Note: In the calculation pointer adjustment calculation, we 12945 * want the rhs to be promoted to a signed type. Section 6.3.1.8 12946 * of the ISO C99 standard indicates that if the adjustment 12947 * parameter is unsigned, the stride parameter will be promoted 12948 * to unsigned, causing errors when the lhs is a larger type than 12949 * the rhs. 12950 */ 12951 img->planes[VPX_PLANE_Y] += (signed) 12952 (img->d_h - 1) * img->stride[VPX_PLANE_Y]; 12953 img->stride[VPX_PLANE_Y] = -img->stride[VPX_PLANE_Y]; 12955 img->planes[VPX_PLANE_U] += (signed) 12956 ((img->d_h >> img->y_chroma_shift) - 1) 12957 * img->stride[VPX_PLANE_U]; 12958 img->stride[VPX_PLANE_U] = -img->stride[VPX_PLANE_U]; 12960 img->planes[VPX_PLANE_V] += (signed) 12961 ((img->d_h >> img->y_chroma_shift) - 1) * 12962 img->stride[VPX_PLANE_V]; 12964 img->stride[VPX_PLANE_V] = -img->stride[VPX_PLANE_V]; 12966 img->planes[VPX_PLANE_ALPHA] += (signed) 12967 (img->d_h - 1) * img->stride[VPX_PLANE_ALPHA]; 12968 img->stride[VPX_PLANE_ALPHA] = -img->stride[VPX_PLANE_ALPHA]; 12969 } 12971 void vpx_img_free(vpx_image_t *img) 12972 { 12973 if (img) 12974 { 12975 if (img->img_data && img->img_data_owner) 12976 free(img->img_data); 12978 if (img->self_allocd) 12979 free(img); 12980 } 12981 } 12983 ---- End code block ---------------------------------------- 12985 20.23. vpx_image.h 12987 ---- Begin code block -------------------------------------- 12989 /* 12990 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 12991 * 12992 * Use of this source code is governed by a BSD-style license 12993 * that can be found in the LICENSE file in the root of the source 12994 * tree. An additional intellectual property rights grant can be 12995 * found in the file PATENTS. All contributing project authors may 12996 * be found in the AUTHORS file in the root of the source tree. 12997 */ 12999 /*!\file 13000 * \brief Describes the vpx image descriptor and associated 13001 * operations 13002 * 13003 */ 13004 #ifdef __cplusplus 13005 extern "C" { 13006 #endif 13008 #ifndef VPX_IMAGE_H 13009 #define VPX_IMAGE_H 13010 /*!\brief Current ABI version number 13011 * 13012 * \internal 13013 * If this file is altered in any way that changes the ABI, this 13014 * value must be bumped. Examples include, but are not limited 13015 * to, changing types, removing or reassigning enums, 13016 * adding/removing/rearranging fields to structures 13017 */ 13018 #define VPX_IMAGE_ABI_VERSION (1) /**<\hideinitializer*/ 13020 #define VPX_IMG_FMT_PLANAR 0x100 /**< Image is a planar 13021 format */ 13022 #define VPX_IMG_FMT_UV_FLIP 0x200 /**< V plane precedes U plane 13023 in memory */ 13024 #define VPX_IMG_FMT_HAS_ALPHA 0x400 /**< Image has an alpha channel 13025 component */ 13027 /*!\brief List of supported image formats */ 13028 typedef enum vpx_img_fmt { 13029 VPX_IMG_FMT_NONE, 13030 VPX_IMG_FMT_RGB24, /**< 24 bit per pixel packed RGB */ 13031 VPX_IMG_FMT_RGB32, /**< 32 bit per pixel packed 0RGB */ 13032 VPX_IMG_FMT_RGB565, /**< 16 bit per pixel, 565 */ 13033 VPX_IMGFMT_RGB555, /**< 16 bit per pixel, 555 */ 13034 VPX_IMG_FMT_UYVY, /**< UYVY packed YUV */ 13035 VPX_IMG_FMT_YUY2, /**< YUYV packed YUV */ 13036 VPX_IMG_FMT_YVYU, /**< YVYU packed YUV */ 13037 VPX_IMG_FMT_BGR24, /**< 24 bit per pixel packed BGR */ 13038 VPX_IMG_FMT_RGB32_LE, /**< 32 bit packed BGR0 */ 13039 VPX_IMG_FMT_ARGB, /**< 32 bit packed ARGB, alpha=255 */ 13040 VPX_IMG_FMT_ARGB_LE, /**< 32 bit packed BGRA, alpha=255 */ 13041 VPX_IMG_FMT_RGB565_LE, /**< 16 bit per pixel, 13042 gggbbbbb rrrrrggg */ 13043 VPX_IMG_FMT_RGB555_LE, /**< 16 bit per pixel, 13044 gggbbbbb 0rrrrrgg */ 13045 VPX_IMG_FMT_YV12 = VPX_IMG_FMT_PLANAR | 13046 VPX_IMG_FMT_UV_FLIP | 1, /**< planar YVU */ 13047 VPX_IMG_FMT_I420 = VPX_IMG_FMT_PLANAR | 2, 13048 VPX_IMG_FMT_VPXYV12 = VPX_IMG_FMT_PLANAR | 13049 VPX_IMG_FMT_UV_FLIP | 3, /** < planar 4:2:0 format with 13050 vpx color space */ 13051 VPX_IMG_FMT_VPXI420 = VPX_IMG_FMT_PLANAR | 4 /** < planar 13052 4:2:0 format with vpx color space */ 13053 } 13054 vpx_img_fmt_t; /**< alias for enum vpx_img_fmt */ 13056 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 13057 /** \deprecated Use #VPX_IMG_FMT_PLANAR */ 13058 #define IMG_FMT_PLANAR VPX_IMG_FMT_PLANAR 13059 /** \deprecated Use #VPX_IMG_FMT_UV_FLIP */ 13060 #define IMG_FMT_UV_FLIP VPX_IMG_FMT_UV_FLIP 13061 /** \deprecated Use #VPX_IMG_FMT_HAS_ALPHA */ 13062 #define IMG_FMT_HAS_ALPHA VPX_IMG_FMT_HAS_ALPHA 13064 /*!\brief Deprecated list of supported image formats 13065 * \deprecated New code should use #vpx_img_fmt 13066 */ 13067 #define img_fmt vpx_img_fmt 13068 /*!\brief alias for enum img_fmt. 13069 * \deprecated New code should use #vpx_img_fmt_t 13070 */ 13071 #define img_fmt_t vpx_img_fmt_t 13073 /** \deprecated Use #VPX_IMG_FMT_NONE */ 13074 #define IMG_FMT_NONE VPX_IMG_FMT_NONE 13075 /** \deprecated Use #VPX_IMG_FMT_RGB24 */ 13076 #define IMG_FMT_RGB24 VPX_IMG_FMT_RGB24 13077 /** \deprecated Use #VPX_IMG_FMT_RGB32 */ 13078 #define IMG_FMT_RGB32 VPX_IMG_FMT_RGB32 13079 /** \deprecated Use #VPX_IMG_FMT_RGB565 */ 13080 #define IMG_FMT_RGB565 VPX_IMG_FMT_RGB565 13081 /** \deprecated Use #VPX_IMG_FMT_RGB555 */ 13082 #define IMG_FMT_RGB555 VPX_IMG_FMT_RGB555 13083 /** \deprecated Use #VPX_IMG_FMT_UYVY */ 13084 #define IMG_FMT_UYVY VPX_IMG_FMT_UYVY 13085 /** \deprecated Use #VPX_IMG_FMT_YUY2 */ 13086 #define IMG_FMT_YUY2 VPX_IMG_FMT_YUY2 13087 /** \deprecated Use #VPX_IMG_FMT_YVYU */ 13088 #define IMG_FMT_YVYU VPX_IMG_FMT_YVYU 13089 /** \deprecated Use #VPX_IMG_FMT_BGR24 */ 13090 #define IMG_FMT_BGR24 VPX_IMG_FMT_BGR24 13091 /**< \deprecated Use #VPX_IMG_FMT_RGB32_LE */ 13092 #define IMG_FMT_RGB32_LE VPX_IMG_FMT_RGB32_LE 13093 /** \deprecated Use #VPX_IMG_FMT_ARGB */ 13094 #define IMG_FMT_ARGB VPX_IMG_FMT_ARGB 13095 /** \deprecated Use #VPX_IMG_FMT_ARGB_LE */ 13096 #define IMG_FMT_ARGB_LE VPX_IMG_FMT_ARGB_LE 13097 /** \deprecated Use #VPX_IMG_FMT_RGB565_LE */ 13098 #define IMG_FMT_RGB565_LE VPX_IMG_FMT_RGB565_LE 13099 /** \deprecated Use #VPX_IMG_FMT_RGB555_LE */ 13100 #define IMG_FMT_RGB555_LE VPX_IMG_FMT_RGB555_LE 13101 /** \deprecated Use #VPX_IMG_FMT_YV12 */ 13102 #define IMG_FMT_YV12 VPX_IMG_FMT_YV12 13103 /** \deprecated Use #VPX_IMG_FMT_I420 */ 13104 #define IMG_FMT_I420 VPX_IMG_FMT_I420 13105 /** \deprecated Use #VPX_IMG_FMT_VPXYV12 */ 13106 #define IMG_FMT_VPXYV12 VPX_IMG_FMT_VPXYV12 13107 /** \deprecated Use #VPX_IMG_FMT_VPXI420 */ 13108 #define IMG_FMT_VPXI420 VPX_IMG_FMT_VPXI420 13109 #endif /* VPX_CODEC_DISABLE_COMPAT */ 13111 /**\brief Image Descriptor */ 13112 typedef struct vpx_image 13113 { 13114 vpx_img_fmt_t fmt; /**< Image Format */ 13116 /* Image storage dimensions */ 13117 unsigned int w; /**< Stored image width */ 13118 unsigned int h; /**< Stored image height */ 13120 /* Image display dimensions */ 13121 unsigned int d_w; /**< Displayed image width */ 13122 unsigned int d_h; /**< Displayed image height */ 13124 /* Chroma subsampling info */ 13125 unsigned int x_chroma_shift; /**< subsampling order, X */ 13126 unsigned int y_chroma_shift; /**< subsampling order, Y */ 13128 /* Image data pointers. */ 13129 #define VPX_PLANE_PACKED 0 /**< To be used for all packed formats */ 13130 #define VPX_PLANE_Y 0 /**< Y (Luminance) plane */ 13131 #define VPX_PLANE_U 1 /**< U (Chroma) plane */ 13132 #define VPX_PLANE_V 2 /**< V (Chroma) plane */ 13133 #define VPX_PLANE_ALPHA 3 /**< A (Transparency) plane */ 13134 #if !defined(VPX_CODEC_DISABLE_COMPAT) || !VPX_CODEC_DISABLE_COMPAT 13135 #define PLANE_PACKED VPX_PLANE_PACKED 13136 #define PLANE_Y VPX_PLANE_Y 13137 #define PLANE_U VPX_PLANE_U 13138 #define PLANE_V VPX_PLANE_V 13139 #define PLANE_ALPHA VPX_PLANE_ALPHA 13140 #endif 13141 unsigned char *planes[4]; /**< pointer to the top left pixel 13142 q for each plane */ 13143 int stride[4]; /**< stride between rows for each plane */ 13145 int bps; /**< bits per sample (for packed formats) */ 13147 /* The following member may be set by the application to 13148 * associate data with this image. 13149 */ 13150 void *user_priv; /**< may be set by the application to 13151 associate data with this image. */ 13153 /* The following members should be treated as private. */ 13154 unsigned char *img_data; /**< private */ 13155 int img_data_owner; /**< private */ 13156 int self_allocd; /**< private */ 13157 } vpx_image_t; /**< alias for struct vpx_image */ 13159 /**\brief Representation of a rectangle on a surface */ 13160 typedef struct vpx_image_rect 13161 { 13162 unsigned int x; /**< leftmost column */ 13163 unsigned int y; /**< topmost row */ 13164 unsigned int w; /**< width */ 13165 unsigned int h; /**< height */ 13166 } vpx_image_rect_t; /**< alias for struct vpx_image_rect */ 13168 /*!\brief Open a descriptor, allocating storage for the 13169 * underlying image 13170 * 13171 * Returns a descriptor for storing an image of the given format. 13172 * The storage for the descriptor is allocated on the heap. 13173 * 13174 * \param[in] img Pointer to storage for descriptor. If 13175 * this parameter is NULL, the storage 13176 * for the descriptor will be allocated 13177 * on the heap. 13178 * \param[in] fmt Format for the image 13179 * \param[in] d_w Width of the image 13180 * \param[in] d_h Height of the image 13181 * \param[in] align Alignment, in bytes, of each row in 13182 * the image. 13183 * 13184 * \return Returns a pointer to the initialized image descriptor. 13185 * If the img parameter is non-null, the value of the img 13186 * parameter will be returned. 13187 */ 13188 vpx_image_t *vpx_img_alloc(vpx_image_t *img, 13189 vpx_img_fmt_t fmt, 13190 unsigned int d_w, 13191 unsigned int d_h, 13192 unsigned int align); 13194 /*!\brief Open a descriptor, using existing storage for the 13195 * underlying image 13196 * 13197 * Returns a descriptor for storing an image of the given format. 13198 * The storage for descriptor has been allocated elsewhere, and a 13199 * descriptor is desired to "wrap" that storage. 13200 * 13201 * \param[in] img Pointer to storage for descriptor. If 13202 * this parameter is NULL, the storage 13203 * for the descriptor will be 13204 * allocated on the heap. 13205 * \param[in] fmt Format for the image 13206 * \param[in] d_w Width of the image 13207 * \param[in] d_h Height of the image 13208 * \param[in] align Alignment, in bytes, of each row in 13209 * the image. 13210 * \param[in] img_data Storage to use for the image 13211 * 13212 * \return Returns a pointer to the initialized image descriptor. 13213 * If the img parameter is non-null, the value of the img 13214 * parameter will be returned. 13215 */ 13216 vpx_image_t *vpx_img_wrap(vpx_image_t *img, 13217 vpx_img_fmt_t fmt, 13218 unsigned int d_w, 13219 unsigned int d_h, 13220 unsigned int align, 13221 unsigned char *img_data); 13223 /*!\brief Set the rectangle identifying the displayed portion of 13224 * the image 13225 * 13226 * Updates the displayed rectangle (aka viewport) on the image 13227 * surface to match the specified coordinates and size. 13228 * 13229 * \param[in] img Image descriptor 13230 * \param[in] x leftmost column 13231 * \param[in] y topmost row 13232 * \param[in] w width 13233 * \param[in] h height 13234 * 13235 * \return 0 if the requested rectangle is valid, nonzero 13236 * otherwise. 13237 */ 13238 int vpx_img_set_rect(vpx_image_t *img, 13239 unsigned int x, 13240 unsigned int y, 13241 unsigned int w, 13242 unsigned int h); 13244 /*!\brief Flip the image vertically (top for bottom) 13245 * 13246 * Adjusts the image descriptor's pointers and strides to make 13247 * the image be referenced upside-down. 13248 * 13249 * \param[in] img Image descriptor 13250 */ 13251 void vpx_img_flip(vpx_image_t *img); 13253 /*!\brief Close an image descriptor 13254 * 13255 * Frees all allocated storage associated with an image 13256 * descriptor. 13257 * 13258 * \param[in] img Image descriptor 13259 */ 13260 void vpx_img_free(vpx_image_t *img); 13262 #endif 13263 #ifdef __cplusplus 13264 } 13265 #endif 13267 ---- End code block ---------------------------------------- 13269 20.24. vpx_integer.h 13271 ---- Begin code block -------------------------------------- 13273 /* 13274 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. 13275 * 13276 * Use of this source code is governed by a BSD-style license 13277 * that can be found in the LICENSE file in the root of the source 13278 * tree. An additional intellectual property rights grant can be 13279 * found in the file PATENTS. All contributing project authors may 13280 * be found in the AUTHORS file in the root of the source tree. 13281 */ 13283 #ifndef VPX_INTEGER_H 13284 #define VPX_INTEGER_H 13286 /* get ptrdiff_t, size_t, wchar_t, NULL */ 13287 #include 13289 #if defined(_MSC_VER) || defined(VPX_EMULATE_INTTYPES) 13290 typedef signed char int8_t; 13291 typedef signed short int16_t; 13292 typedef signed int int32_t; 13293 typedef unsigned char uint8_t; 13294 typedef unsigned short uint16_t; 13295 typedef unsigned int uint32_t; 13297 #if defined(_MSC_VER) 13298 typedef signed __int64 int64_t; 13299 typedef unsigned __int64 uint64_t; 13300 #define PRId64 "I64d" 13301 #endif 13303 #ifdef HAVE_ARMV6 13304 typedef unsigned int int_fast16_t; 13305 #else 13306 typedef signed short int_fast16_t; 13307 #endif 13308 typedef signed char int_fast8_t; 13309 typedef unsigned char uint_fast8_t; 13311 #ifndef _UINTPTR_T_DEFINED 13312 typedef unsigned int uintptr_t; 13313 #endif 13315 #else 13317 /* Most platforms have the C99 standard integer types. */ 13319 #if defined(__cplusplus) && !defined(__STDC_FORMAT_MACROS) 13320 #define __STDC_FORMAT_MACROS 13321 #endif 13322 #include 13323 #include 13325 #endif 13327 #endif 13329 ---- End code block ---------------------------------------- 13331 20.25. AUTHORS 13333 Aaron Watry 13335 Adrian Grange 13337 Alex Converse 13339 Andoni Morales Alastruey 13340 Andres Mejia 13342 Attila Nagy 13344 Fabio Pedretti 13346 Frank Galligan 13348 Fredrik Soederquist 13350 Fritz Koenig 13352 Gaute Strokkenes 13354 Giuseppe Scrivano 13356 Guillermo Ballester Valor 13358 Henrik Lundin 13360 James Berry 13362 James Zern 13364 Jan Kratochvil 13366 Jeff Muizelaar 13368 Jim Bankoski 13370 Johann Koenig 13372 John Koleszar 13374 Justin Clift 13376 Justin Lebar 13378 Luca Barbato 13380 Makoto Kato 13382 Martin Ettl 13384 Michael Kohler 13386 Mikhal Shemer 13387 Pascal Massimino 13389 Patrik Westin 13391 Paul Wilkins 13393 Pavol Rusnak 13395 Philip Jaegenstedt 13397 Scott LaVarnway 13399 Tero Rintaluoma 13401 Timothy B. Terriberry 13403 Tom Finegan 13405 Yaowu Xu 13407 Yunqing Wang 13409 Google Inc. 13411 The Mozilla Foundation 13413 The Xiph.Org Foundation 13415 20.26. LICENSE 13417 Copyright (c) 2010, Google Inc. All rights reserved. 13419 Redistribution and use in source and binary forms, with or without 13420 modification, are permitted provided that the following conditions 13421 are met: 13423 o Redistributions of source code must retain the above copyright 13424 notice, this list of conditions and the following disclaimer. 13426 o Redistributions in binary form must reproduce the above copyright 13427 notice, this list of conditions and the following disclaimer in 13428 the documentation and/or other materials provided with the 13429 distribution. 13431 o Neither the name of Google nor the names of its contributors may 13432 be used to endorse or promote products derived from this software 13433 without specific prior written permission. 13435 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 13436 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 13437 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 13438 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 13439 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 13440 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 13441 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 13442 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 13443 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 13444 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 13445 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 13446 POSSIBILITY OF SUCH DAMAGE. 13448 20.27. PATENTS 13450 Additional IP Rights Grant (Patents) 13452 "This implementation" means the copyrightable works distributed by 13453 Google as part of the WebM Project. 13455 Google hereby grants to you a perpetual, worldwide, non-exclusive, 13456 no-charge, royalty-free, irrevocable (except as stated in this 13457 section) patent license to make, have made, use, offer to sell, sell, 13458 import, transfer, and otherwise run, modify and propagate the 13459 contents of this implementation of VP8, where such license applies 13460 only to those patent claims, both currently owned by Google and 13461 acquired in the future, licensable by Google that are necessarily 13462 infringed by this implementation of VP8. This grant does not include 13463 claims that would be infringed only as a consequence of further 13464 modification of this implementation. If you or your agent or 13465 exclusive licensee institute or order or agree to the institution of 13466 patent litigation against any entity (including a cross-claim or 13467 counterclaim in a lawsuit) alleging that this implementation of VP8 13468 or any code incorporated within this implementation of VP8 13469 constitutes direct or contributory patent infringement, or inducement 13470 of patent infringement, then any patent rights granted to you under 13471 this License for this implementation of VP8 shall terminate as of the 13472 date such litigation is filed. 13474 21. Security Considerations 13476 A VP8 decoder should take appropriate security considerations into 13477 account, as outlined in [RFC4732] and [RFC3552]. It is extremely 13478 important that a decoder be robust against malicious payloads. 13479 Malicious payloads must not cause the decoder to overrun its 13480 allocated memory or to consume inordinate resources. Although 13481 encoder issues are typically rarer, the same applies to an encoder. 13482 Malicious stream data must not cause the encoder to misbehave, as 13483 this might allow an attacker access to transcoding gateways. 13485 22. IANA Considerations 13487 This document has no actions for IANA. 13489 23. Informative References 13491 [Bell] Bell, T., Cleary, J., and I. Witten, "Text Compression", 13492 1990. 13494 [ITU-R_BT.601] 13495 International Telecommunication Union, "ITU BT.601: Studio 13496 encoding parameters of digital television for standard 4:3 13497 and wide screen 16:9 aspect ratios", January 2007. 13499 [Kernighan] 13500 Kernighan, B. and D. Ritchie, "The C Programming Language 13501 (2nd edition)", April 1988. 13503 [Loeffler] 13504 Loeffler, C., Ligtenberg , A., and G. Moschytz, "Practical 13505 Fast 1-D DCT Algorithms with 11 Multiplications", 13506 May 1989. 13508 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 13509 Requirement Levels", BCP 14, RFC 2119, March 1997. 13511 [RFC3552] Rescorla, E. and B. Korver, "Guidelines for Writing RFC 13512 Text on Security Considerations", BCP 72, RFC 3552, 13513 July 2003. 13515 [RFC3979] Bradner, S., "Intellectual Property Rights in IETF 13516 Technology", BCP 79, RFC 3979, March 2005. 13518 [RFC4732] Handley, M., Rescorla, E., and IAB, "Internet Denial-of- 13519 Service Considerations", RFC 4732, December 2006. 13521 [RFC5378] Bradner, S. and J. Contreras, "Rights Contributors Provide 13522 to the IETF Trust", BCP 78, RFC 5378, November 2008. 13524 [Shannon] Shannon, C., "A Mathematical Theory of Communication", 13525 Bell System Technical Journal Vol. 27, pp. 379-423, 623- 13526 656, July, October 1948. 13528 Authors' Addresses 13530 James Bankoski 13531 Google Inc. 13533 Email: jimbankoski@google.com 13535 John Koleszar 13536 Google Inc. 13538 Email: jkoleszar@google.com 13540 Lou Quillio 13541 Google Inc. 13543 Email: louquillio@google.com 13545 Janne Salonen 13546 Google Inc. 13548 Email: jsalonen@google.com 13550 Paul Wilkins 13551 Google Inc. 13553 Email: paulwilkins@google.com 13555 Yaowu Xu 13556 Google Inc. 13558 Email: yaowu@google.com