media/libtheora/lib/decode.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /********************************************************************
michael@0 2 * *
michael@0 3 * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
michael@0 4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
michael@0 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
michael@0 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
michael@0 7 * *
michael@0 8 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
michael@0 9 * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
michael@0 10 * *
michael@0 11 ********************************************************************
michael@0 12
michael@0 13 function:
michael@0 14 last mod: $Id: decode.c 17576 2010-10-29 01:07:51Z tterribe $
michael@0 15
michael@0 16 ********************************************************************/
michael@0 17
michael@0 18 #include <stdlib.h>
michael@0 19 #include <string.h>
michael@0 20 #include <ogg/ogg.h>
michael@0 21 #include "decint.h"
michael@0 22 #if defined(OC_DUMP_IMAGES)
michael@0 23 # include <stdio.h>
michael@0 24 # include "png.h"
michael@0 25 #endif
michael@0 26 #if defined(HAVE_CAIRO)
michael@0 27 # include <cairo.h>
michael@0 28 #endif
michael@0 29
michael@0 30
michael@0 31 /*No post-processing.*/
michael@0 32 #define OC_PP_LEVEL_DISABLED (0)
michael@0 33 /*Keep track of DC qi for each block only.*/
michael@0 34 #define OC_PP_LEVEL_TRACKDCQI (1)
michael@0 35 /*Deblock the luma plane.*/
michael@0 36 #define OC_PP_LEVEL_DEBLOCKY (2)
michael@0 37 /*Dering the luma plane.*/
michael@0 38 #define OC_PP_LEVEL_DERINGY (3)
michael@0 39 /*Stronger luma plane deringing.*/
michael@0 40 #define OC_PP_LEVEL_SDERINGY (4)
michael@0 41 /*Deblock the chroma planes.*/
michael@0 42 #define OC_PP_LEVEL_DEBLOCKC (5)
michael@0 43 /*Dering the chroma planes.*/
michael@0 44 #define OC_PP_LEVEL_DERINGC (6)
michael@0 45 /*Stronger chroma plane deringing.*/
michael@0 46 #define OC_PP_LEVEL_SDERINGC (7)
michael@0 47 /*Maximum valid post-processing level.*/
michael@0 48 #define OC_PP_LEVEL_MAX (7)
michael@0 49
michael@0 50
michael@0 51
michael@0 52 /*The mode alphabets for the various mode coding schemes.
michael@0 53 Scheme 0 uses a custom alphabet, which is not stored in this table.*/
michael@0 54 static const unsigned char OC_MODE_ALPHABETS[7][OC_NMODES]={
michael@0 55 /*Last MV dominates */
michael@0 56 {
michael@0 57 OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV_LAST2,OC_MODE_INTER_MV,
michael@0 58 OC_MODE_INTER_NOMV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
michael@0 59 OC_MODE_INTER_MV_FOUR
michael@0 60 },
michael@0 61 {
michael@0 62 OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV_LAST2,OC_MODE_INTER_NOMV,
michael@0 63 OC_MODE_INTER_MV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
michael@0 64 OC_MODE_INTER_MV_FOUR
michael@0 65 },
michael@0 66 {
michael@0 67 OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV,OC_MODE_INTER_MV_LAST2,
michael@0 68 OC_MODE_INTER_NOMV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
michael@0 69 OC_MODE_INTER_MV_FOUR
michael@0 70 },
michael@0 71 {
michael@0 72 OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV,OC_MODE_INTER_NOMV,
michael@0 73 OC_MODE_INTER_MV_LAST2,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,
michael@0 74 OC_MODE_GOLDEN_MV,OC_MODE_INTER_MV_FOUR
michael@0 75 },
michael@0 76 /*No MV dominates.*/
michael@0 77 {
michael@0 78 OC_MODE_INTER_NOMV,OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV_LAST2,
michael@0 79 OC_MODE_INTER_MV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
michael@0 80 OC_MODE_INTER_MV_FOUR
michael@0 81 },
michael@0 82 {
michael@0 83 OC_MODE_INTER_NOMV,OC_MODE_GOLDEN_NOMV,OC_MODE_INTER_MV_LAST,
michael@0 84 OC_MODE_INTER_MV_LAST2,OC_MODE_INTER_MV,OC_MODE_INTRA,OC_MODE_GOLDEN_MV,
michael@0 85 OC_MODE_INTER_MV_FOUR
michael@0 86 },
michael@0 87 /*Default ordering.*/
michael@0 88 {
michael@0 89 OC_MODE_INTER_NOMV,OC_MODE_INTRA,OC_MODE_INTER_MV,OC_MODE_INTER_MV_LAST,
michael@0 90 OC_MODE_INTER_MV_LAST2,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
michael@0 91 OC_MODE_INTER_MV_FOUR
michael@0 92 }
michael@0 93 };
michael@0 94
michael@0 95
michael@0 96 /*The original DCT tokens are extended and reordered during the construction of
michael@0 97 the Huffman tables.
michael@0 98 The extension means more bits can be read with fewer calls to the bitpacker
michael@0 99 during the Huffman decoding process (at the cost of larger Huffman tables),
michael@0 100 and fewer tokens require additional extra bits (reducing the average storage
michael@0 101 per decoded token).
michael@0 102 The revised ordering reveals essential information in the token value
michael@0 103 itself; specifically, whether or not there are additional extra bits to read
michael@0 104 and the parameter to which those extra bits are applied.
michael@0 105 The token is used to fetch a code word from the OC_DCT_CODE_WORD table below.
michael@0 106 The extra bits are added into code word at the bit position inferred from the
michael@0 107 token value, giving the final code word from which all required parameters
michael@0 108 are derived.
michael@0 109 The number of EOBs and the leading zero run length can be extracted directly.
michael@0 110 The coefficient magnitude is optionally negated before extraction, according
michael@0 111 to a 'flip' bit.*/
michael@0 112
michael@0 113 /*The number of additional extra bits that are decoded with each of the
michael@0 114 internal DCT tokens.*/
michael@0 115 static const unsigned char OC_INTERNAL_DCT_TOKEN_EXTRA_BITS[15]={
michael@0 116 12,4,3,3,4,4,5,5,8,8,8,8,3,3,6
michael@0 117 };
michael@0 118
michael@0 119 /*Whether or not an internal token needs any additional extra bits.*/
michael@0 120 #define OC_DCT_TOKEN_NEEDS_MORE(token) \
michael@0 121 (token<(int)(sizeof(OC_INTERNAL_DCT_TOKEN_EXTRA_BITS)/ \
michael@0 122 sizeof(*OC_INTERNAL_DCT_TOKEN_EXTRA_BITS)))
michael@0 123
michael@0 124 /*This token (OC_DCT_REPEAT_RUN3_TOKEN) requires more than 8 extra bits.*/
michael@0 125 #define OC_DCT_TOKEN_FAT_EOB (0)
michael@0 126
michael@0 127 /*The number of EOBs to use for an end-of-frame token.
michael@0 128 Note: We want to set eobs to PTRDIFF_MAX here, but that requires C99, which
michael@0 129 is not yet available everywhere; this should be equivalent.*/
michael@0 130 #define OC_DCT_EOB_FINISH (~(size_t)0>>1)
michael@0 131
michael@0 132 /*The location of the (6) run length bits in the code word.
michael@0 133 These are placed at index 0 and given 8 bits (even though 6 would suffice)
michael@0 134 because it may be faster to extract the lower byte on some platforms.*/
michael@0 135 #define OC_DCT_CW_RLEN_SHIFT (0)
michael@0 136 /*The location of the (12) EOB bits in the code word.*/
michael@0 137 #define OC_DCT_CW_EOB_SHIFT (8)
michael@0 138 /*The location of the (1) flip bit in the code word.
michael@0 139 This must be right under the magnitude bits.*/
michael@0 140 #define OC_DCT_CW_FLIP_BIT (20)
michael@0 141 /*The location of the (11) token magnitude bits in the code word.
michael@0 142 These must be last, and rely on a sign-extending right shift.*/
michael@0 143 #define OC_DCT_CW_MAG_SHIFT (21)
michael@0 144
michael@0 145 /*Pack the given fields into a code word.*/
michael@0 146 #define OC_DCT_CW_PACK(_eobs,_rlen,_mag,_flip) \
michael@0 147 ((_eobs)<<OC_DCT_CW_EOB_SHIFT| \
michael@0 148 (_rlen)<<OC_DCT_CW_RLEN_SHIFT| \
michael@0 149 (_flip)<<OC_DCT_CW_FLIP_BIT| \
michael@0 150 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
michael@0 151
michael@0 152 /*A special code word value that signals the end of the frame (a long EOB run
michael@0 153 of zero).*/
michael@0 154 #define OC_DCT_CW_FINISH (0)
michael@0 155
michael@0 156 /*The position at which to insert the extra bits in the code word.
michael@0 157 We use this formulation because Intel has no useful cmov.
michael@0 158 A real architecture would probably do better with two of those.
michael@0 159 This translates to 11 instructions(!), and is _still_ faster than either a
michael@0 160 table lookup (just barely) or the naive double-ternary implementation (which
michael@0 161 gcc translates to a jump and a cmov).
michael@0 162 This assumes OC_DCT_CW_RLEN_SHIFT is zero, but could easily be reworked if
michael@0 163 you want to make one of the other shifts zero.*/
michael@0 164 #define OC_DCT_TOKEN_EB_POS(_token) \
michael@0 165 ((OC_DCT_CW_EOB_SHIFT-OC_DCT_CW_MAG_SHIFT&-((_token)<2)) \
michael@0 166 +(OC_DCT_CW_MAG_SHIFT&-((_token)<12)))
michael@0 167
michael@0 168 /*The code words for each internal token.
michael@0 169 See the notes at OC_DCT_TOKEN_MAP for the reasons why things are out of
michael@0 170 order.*/
michael@0 171 static const ogg_int32_t OC_DCT_CODE_WORD[92]={
michael@0 172 /*These tokens require additional extra bits for the EOB count.*/
michael@0 173 /*OC_DCT_REPEAT_RUN3_TOKEN (12 extra bits)*/
michael@0 174 OC_DCT_CW_FINISH,
michael@0 175 /*OC_DCT_REPEAT_RUN2_TOKEN (4 extra bits)*/
michael@0 176 OC_DCT_CW_PACK(16, 0, 0,0),
michael@0 177 /*These tokens require additional extra bits for the magnitude.*/
michael@0 178 /*OC_DCT_VAL_CAT5 (4 extra bits-1 already read)*/
michael@0 179 OC_DCT_CW_PACK( 0, 0, 13,0),
michael@0 180 OC_DCT_CW_PACK( 0, 0, 13,1),
michael@0 181 /*OC_DCT_VAL_CAT6 (5 extra bits-1 already read)*/
michael@0 182 OC_DCT_CW_PACK( 0, 0, 21,0),
michael@0 183 OC_DCT_CW_PACK( 0, 0, 21,1),
michael@0 184 /*OC_DCT_VAL_CAT7 (6 extra bits-1 already read)*/
michael@0 185 OC_DCT_CW_PACK( 0, 0, 37,0),
michael@0 186 OC_DCT_CW_PACK( 0, 0, 37,1),
michael@0 187 /*OC_DCT_VAL_CAT8 (10 extra bits-2 already read)*/
michael@0 188 OC_DCT_CW_PACK( 0, 0, 69,0),
michael@0 189 OC_DCT_CW_PACK( 0, 0,325,0),
michael@0 190 OC_DCT_CW_PACK( 0, 0, 69,1),
michael@0 191 OC_DCT_CW_PACK( 0, 0,325,1),
michael@0 192 /*These tokens require additional extra bits for the run length.*/
michael@0 193 /*OC_DCT_RUN_CAT1C (4 extra bits-1 already read)*/
michael@0 194 OC_DCT_CW_PACK( 0,10, +1,0),
michael@0 195 OC_DCT_CW_PACK( 0,10, -1,0),
michael@0 196 /*OC_DCT_ZRL_TOKEN (6 extra bits)
michael@0 197 Flip is set to distinguish this from OC_DCT_CW_FINISH.*/
michael@0 198 OC_DCT_CW_PACK( 0, 0, 0,1),
michael@0 199 /*The remaining tokens require no additional extra bits.*/
michael@0 200 /*OC_DCT_EOB1_TOKEN (0 extra bits)*/
michael@0 201 OC_DCT_CW_PACK( 1, 0, 0,0),
michael@0 202 /*OC_DCT_EOB2_TOKEN (0 extra bits)*/
michael@0 203 OC_DCT_CW_PACK( 2, 0, 0,0),
michael@0 204 /*OC_DCT_EOB3_TOKEN (0 extra bits)*/
michael@0 205 OC_DCT_CW_PACK( 3, 0, 0,0),
michael@0 206 /*OC_DCT_RUN_CAT1A (1 extra bit-1 already read)x5*/
michael@0 207 OC_DCT_CW_PACK( 0, 1, +1,0),
michael@0 208 OC_DCT_CW_PACK( 0, 1, -1,0),
michael@0 209 OC_DCT_CW_PACK( 0, 2, +1,0),
michael@0 210 OC_DCT_CW_PACK( 0, 2, -1,0),
michael@0 211 OC_DCT_CW_PACK( 0, 3, +1,0),
michael@0 212 OC_DCT_CW_PACK( 0, 3, -1,0),
michael@0 213 OC_DCT_CW_PACK( 0, 4, +1,0),
michael@0 214 OC_DCT_CW_PACK( 0, 4, -1,0),
michael@0 215 OC_DCT_CW_PACK( 0, 5, +1,0),
michael@0 216 OC_DCT_CW_PACK( 0, 5, -1,0),
michael@0 217 /*OC_DCT_RUN_CAT2A (2 extra bits-2 already read)*/
michael@0 218 OC_DCT_CW_PACK( 0, 1, +2,0),
michael@0 219 OC_DCT_CW_PACK( 0, 1, +3,0),
michael@0 220 OC_DCT_CW_PACK( 0, 1, -2,0),
michael@0 221 OC_DCT_CW_PACK( 0, 1, -3,0),
michael@0 222 /*OC_DCT_RUN_CAT1B (3 extra bits-3 already read)*/
michael@0 223 OC_DCT_CW_PACK( 0, 6, +1,0),
michael@0 224 OC_DCT_CW_PACK( 0, 7, +1,0),
michael@0 225 OC_DCT_CW_PACK( 0, 8, +1,0),
michael@0 226 OC_DCT_CW_PACK( 0, 9, +1,0),
michael@0 227 OC_DCT_CW_PACK( 0, 6, -1,0),
michael@0 228 OC_DCT_CW_PACK( 0, 7, -1,0),
michael@0 229 OC_DCT_CW_PACK( 0, 8, -1,0),
michael@0 230 OC_DCT_CW_PACK( 0, 9, -1,0),
michael@0 231 /*OC_DCT_RUN_CAT2B (3 extra bits-3 already read)*/
michael@0 232 OC_DCT_CW_PACK( 0, 2, +2,0),
michael@0 233 OC_DCT_CW_PACK( 0, 3, +2,0),
michael@0 234 OC_DCT_CW_PACK( 0, 2, +3,0),
michael@0 235 OC_DCT_CW_PACK( 0, 3, +3,0),
michael@0 236 OC_DCT_CW_PACK( 0, 2, -2,0),
michael@0 237 OC_DCT_CW_PACK( 0, 3, -2,0),
michael@0 238 OC_DCT_CW_PACK( 0, 2, -3,0),
michael@0 239 OC_DCT_CW_PACK( 0, 3, -3,0),
michael@0 240 /*OC_DCT_SHORT_ZRL_TOKEN (3 extra bits-3 already read)
michael@0 241 Flip is set on the first one to distinguish it from OC_DCT_CW_FINISH.*/
michael@0 242 OC_DCT_CW_PACK( 0, 0, 0,1),
michael@0 243 OC_DCT_CW_PACK( 0, 1, 0,0),
michael@0 244 OC_DCT_CW_PACK( 0, 2, 0,0),
michael@0 245 OC_DCT_CW_PACK( 0, 3, 0,0),
michael@0 246 OC_DCT_CW_PACK( 0, 4, 0,0),
michael@0 247 OC_DCT_CW_PACK( 0, 5, 0,0),
michael@0 248 OC_DCT_CW_PACK( 0, 6, 0,0),
michael@0 249 OC_DCT_CW_PACK( 0, 7, 0,0),
michael@0 250 /*OC_ONE_TOKEN (0 extra bits)*/
michael@0 251 OC_DCT_CW_PACK( 0, 0, +1,0),
michael@0 252 /*OC_MINUS_ONE_TOKEN (0 extra bits)*/
michael@0 253 OC_DCT_CW_PACK( 0, 0, -1,0),
michael@0 254 /*OC_TWO_TOKEN (0 extra bits)*/
michael@0 255 OC_DCT_CW_PACK( 0, 0, +2,0),
michael@0 256 /*OC_MINUS_TWO_TOKEN (0 extra bits)*/
michael@0 257 OC_DCT_CW_PACK( 0, 0, -2,0),
michael@0 258 /*OC_DCT_VAL_CAT2 (1 extra bit-1 already read)x4*/
michael@0 259 OC_DCT_CW_PACK( 0, 0, +3,0),
michael@0 260 OC_DCT_CW_PACK( 0, 0, -3,0),
michael@0 261 OC_DCT_CW_PACK( 0, 0, +4,0),
michael@0 262 OC_DCT_CW_PACK( 0, 0, -4,0),
michael@0 263 OC_DCT_CW_PACK( 0, 0, +5,0),
michael@0 264 OC_DCT_CW_PACK( 0, 0, -5,0),
michael@0 265 OC_DCT_CW_PACK( 0, 0, +6,0),
michael@0 266 OC_DCT_CW_PACK( 0, 0, -6,0),
michael@0 267 /*OC_DCT_VAL_CAT3 (2 extra bits-2 already read)*/
michael@0 268 OC_DCT_CW_PACK( 0, 0, +7,0),
michael@0 269 OC_DCT_CW_PACK( 0, 0, +8,0),
michael@0 270 OC_DCT_CW_PACK( 0, 0, -7,0),
michael@0 271 OC_DCT_CW_PACK( 0, 0, -8,0),
michael@0 272 /*OC_DCT_VAL_CAT4 (3 extra bits-3 already read)*/
michael@0 273 OC_DCT_CW_PACK( 0, 0, +9,0),
michael@0 274 OC_DCT_CW_PACK( 0, 0,+10,0),
michael@0 275 OC_DCT_CW_PACK( 0, 0,+11,0),
michael@0 276 OC_DCT_CW_PACK( 0, 0,+12,0),
michael@0 277 OC_DCT_CW_PACK( 0, 0, -9,0),
michael@0 278 OC_DCT_CW_PACK( 0, 0,-10,0),
michael@0 279 OC_DCT_CW_PACK( 0, 0,-11,0),
michael@0 280 OC_DCT_CW_PACK( 0, 0,-12,0),
michael@0 281 /*OC_DCT_REPEAT_RUN1_TOKEN (3 extra bits-3 already read)*/
michael@0 282 OC_DCT_CW_PACK( 8, 0, 0,0),
michael@0 283 OC_DCT_CW_PACK( 9, 0, 0,0),
michael@0 284 OC_DCT_CW_PACK(10, 0, 0,0),
michael@0 285 OC_DCT_CW_PACK(11, 0, 0,0),
michael@0 286 OC_DCT_CW_PACK(12, 0, 0,0),
michael@0 287 OC_DCT_CW_PACK(13, 0, 0,0),
michael@0 288 OC_DCT_CW_PACK(14, 0, 0,0),
michael@0 289 OC_DCT_CW_PACK(15, 0, 0,0),
michael@0 290 /*OC_DCT_REPEAT_RUN0_TOKEN (2 extra bits-2 already read)*/
michael@0 291 OC_DCT_CW_PACK( 4, 0, 0,0),
michael@0 292 OC_DCT_CW_PACK( 5, 0, 0,0),
michael@0 293 OC_DCT_CW_PACK( 6, 0, 0,0),
michael@0 294 OC_DCT_CW_PACK( 7, 0, 0,0),
michael@0 295 };
michael@0 296
michael@0 297
michael@0 298
michael@0 299 static int oc_sb_run_unpack(oc_pack_buf *_opb){
michael@0 300 /*Coding scheme:
michael@0 301 Codeword Run Length
michael@0 302 0 1
michael@0 303 10x 2-3
michael@0 304 110x 4-5
michael@0 305 1110xx 6-9
michael@0 306 11110xxx 10-17
michael@0 307 111110xxxx 18-33
michael@0 308 111111xxxxxxxxxxxx 34-4129*/
michael@0 309 static const ogg_int16_t OC_SB_RUN_TREE[22]={
michael@0 310 4,
michael@0 311 -(1<<8|1),-(1<<8|1),-(1<<8|1),-(1<<8|1),
michael@0 312 -(1<<8|1),-(1<<8|1),-(1<<8|1),-(1<<8|1),
michael@0 313 -(3<<8|2),-(3<<8|2),-(3<<8|3),-(3<<8|3),
michael@0 314 -(4<<8|4),-(4<<8|5),-(4<<8|2<<4|6-6),17,
michael@0 315 2,
michael@0 316 -(2<<8|2<<4|10-6),-(2<<8|2<<4|14-6),-(2<<8|4<<4|18-6),-(2<<8|12<<4|34-6)
michael@0 317 };
michael@0 318 int ret;
michael@0 319 ret=oc_huff_token_decode(_opb,OC_SB_RUN_TREE);
michael@0 320 if(ret>=0x10){
michael@0 321 int offs;
michael@0 322 offs=ret&0x1F;
michael@0 323 ret=6+offs+(int)oc_pack_read(_opb,ret-offs>>4);
michael@0 324 }
michael@0 325 return ret;
michael@0 326 }
michael@0 327
michael@0 328 static int oc_block_run_unpack(oc_pack_buf *_opb){
michael@0 329 /*Coding scheme:
michael@0 330 Codeword Run Length
michael@0 331 0x 1-2
michael@0 332 10x 3-4
michael@0 333 110x 5-6
michael@0 334 1110xx 7-10
michael@0 335 11110xx 11-14
michael@0 336 11111xxxx 15-30*/
michael@0 337 static const ogg_int16_t OC_BLOCK_RUN_TREE[61]={
michael@0 338 5,
michael@0 339 -(2<<8|1),-(2<<8|1),-(2<<8|1),-(2<<8|1),
michael@0 340 -(2<<8|1),-(2<<8|1),-(2<<8|1),-(2<<8|1),
michael@0 341 -(2<<8|2),-(2<<8|2),-(2<<8|2),-(2<<8|2),
michael@0 342 -(2<<8|2),-(2<<8|2),-(2<<8|2),-(2<<8|2),
michael@0 343 -(3<<8|3),-(3<<8|3),-(3<<8|3),-(3<<8|3),
michael@0 344 -(3<<8|4),-(3<<8|4),-(3<<8|4),-(3<<8|4),
michael@0 345 -(4<<8|5),-(4<<8|5),-(4<<8|6),-(4<<8|6),
michael@0 346 33, 36, 39, 44,
michael@0 347 1,-(1<<8|7),-(1<<8|8),
michael@0 348 1,-(1<<8|9),-(1<<8|10),
michael@0 349 2,-(2<<8|11),-(2<<8|12),-(2<<8|13),-(2<<8|14),
michael@0 350 4,
michael@0 351 -(4<<8|15),-(4<<8|16),-(4<<8|17),-(4<<8|18),
michael@0 352 -(4<<8|19),-(4<<8|20),-(4<<8|21),-(4<<8|22),
michael@0 353 -(4<<8|23),-(4<<8|24),-(4<<8|25),-(4<<8|26),
michael@0 354 -(4<<8|27),-(4<<8|28),-(4<<8|29),-(4<<8|30)
michael@0 355 };
michael@0 356 return oc_huff_token_decode(_opb,OC_BLOCK_RUN_TREE);
michael@0 357 }
michael@0 358
michael@0 359
michael@0 360
michael@0 361 void oc_dec_accel_init_c(oc_dec_ctx *_dec){
michael@0 362 # if defined(OC_DEC_USE_VTABLE)
michael@0 363 _dec->opt_vtable.dc_unpredict_mcu_plane=
michael@0 364 oc_dec_dc_unpredict_mcu_plane_c;
michael@0 365 # endif
michael@0 366 }
michael@0 367
michael@0 368 static int oc_dec_init(oc_dec_ctx *_dec,const th_info *_info,
michael@0 369 const th_setup_info *_setup){
michael@0 370 int qti;
michael@0 371 int pli;
michael@0 372 int qi;
michael@0 373 int ret;
michael@0 374 ret=oc_state_init(&_dec->state,_info,3);
michael@0 375 if(ret<0)return ret;
michael@0 376 ret=oc_huff_trees_copy(_dec->huff_tables,
michael@0 377 (const ogg_int16_t *const *)_setup->huff_tables);
michael@0 378 if(ret<0){
michael@0 379 oc_state_clear(&_dec->state);
michael@0 380 return ret;
michael@0 381 }
michael@0 382 /*For each fragment, allocate one byte for every DCT coefficient token, plus
michael@0 383 one byte for extra-bits for each token, plus one more byte for the long
michael@0 384 EOB run, just in case it's the very last token and has a run length of
michael@0 385 one.*/
michael@0 386 _dec->dct_tokens=(unsigned char *)_ogg_malloc((64+64+1)*
michael@0 387 _dec->state.nfrags*sizeof(_dec->dct_tokens[0]));
michael@0 388 if(_dec->dct_tokens==NULL){
michael@0 389 oc_huff_trees_clear(_dec->huff_tables);
michael@0 390 oc_state_clear(&_dec->state);
michael@0 391 return TH_EFAULT;
michael@0 392 }
michael@0 393 for(qi=0;qi<64;qi++)for(pli=0;pli<3;pli++)for(qti=0;qti<2;qti++){
michael@0 394 _dec->state.dequant_tables[qi][pli][qti]=
michael@0 395 _dec->state.dequant_table_data[qi][pli][qti];
michael@0 396 }
michael@0 397 oc_dequant_tables_init(_dec->state.dequant_tables,_dec->pp_dc_scale,
michael@0 398 &_setup->qinfo);
michael@0 399 for(qi=0;qi<64;qi++){
michael@0 400 int qsum;
michael@0 401 qsum=0;
michael@0 402 for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
michael@0 403 qsum+=_dec->state.dequant_tables[qi][pli][qti][12]+
michael@0 404 _dec->state.dequant_tables[qi][pli][qti][17]+
michael@0 405 _dec->state.dequant_tables[qi][pli][qti][18]+
michael@0 406 _dec->state.dequant_tables[qi][pli][qti][24]<<(pli==0);
michael@0 407 }
michael@0 408 _dec->pp_sharp_mod[qi]=-(qsum>>11);
michael@0 409 }
michael@0 410 memcpy(_dec->state.loop_filter_limits,_setup->qinfo.loop_filter_limits,
michael@0 411 sizeof(_dec->state.loop_filter_limits));
michael@0 412 oc_dec_accel_init(_dec);
michael@0 413 _dec->pp_level=OC_PP_LEVEL_DISABLED;
michael@0 414 _dec->dc_qis=NULL;
michael@0 415 _dec->variances=NULL;
michael@0 416 _dec->pp_frame_data=NULL;
michael@0 417 _dec->stripe_cb.ctx=NULL;
michael@0 418 _dec->stripe_cb.stripe_decoded=NULL;
michael@0 419 #if defined(HAVE_CAIRO)
michael@0 420 _dec->telemetry=0;
michael@0 421 _dec->telemetry_bits=0;
michael@0 422 _dec->telemetry_qi=0;
michael@0 423 _dec->telemetry_mbmode=0;
michael@0 424 _dec->telemetry_mv=0;
michael@0 425 _dec->telemetry_frame_data=NULL;
michael@0 426 #endif
michael@0 427 return 0;
michael@0 428 }
michael@0 429
michael@0 430 static void oc_dec_clear(oc_dec_ctx *_dec){
michael@0 431 #if defined(HAVE_CAIRO)
michael@0 432 _ogg_free(_dec->telemetry_frame_data);
michael@0 433 #endif
michael@0 434 _ogg_free(_dec->pp_frame_data);
michael@0 435 _ogg_free(_dec->variances);
michael@0 436 _ogg_free(_dec->dc_qis);
michael@0 437 _ogg_free(_dec->dct_tokens);
michael@0 438 oc_huff_trees_clear(_dec->huff_tables);
michael@0 439 oc_state_clear(&_dec->state);
michael@0 440 }
michael@0 441
michael@0 442
michael@0 443 static int oc_dec_frame_header_unpack(oc_dec_ctx *_dec){
michael@0 444 long val;
michael@0 445 /*Check to make sure this is a data packet.*/
michael@0 446 val=oc_pack_read1(&_dec->opb);
michael@0 447 if(val!=0)return TH_EBADPACKET;
michael@0 448 /*Read in the frame type (I or P).*/
michael@0 449 val=oc_pack_read1(&_dec->opb);
michael@0 450 _dec->state.frame_type=(int)val;
michael@0 451 /*Read in the qi list.*/
michael@0 452 val=oc_pack_read(&_dec->opb,6);
michael@0 453 _dec->state.qis[0]=(unsigned char)val;
michael@0 454 val=oc_pack_read1(&_dec->opb);
michael@0 455 if(!val)_dec->state.nqis=1;
michael@0 456 else{
michael@0 457 val=oc_pack_read(&_dec->opb,6);
michael@0 458 _dec->state.qis[1]=(unsigned char)val;
michael@0 459 val=oc_pack_read1(&_dec->opb);
michael@0 460 if(!val)_dec->state.nqis=2;
michael@0 461 else{
michael@0 462 val=oc_pack_read(&_dec->opb,6);
michael@0 463 _dec->state.qis[2]=(unsigned char)val;
michael@0 464 _dec->state.nqis=3;
michael@0 465 }
michael@0 466 }
michael@0 467 if(_dec->state.frame_type==OC_INTRA_FRAME){
michael@0 468 /*Keyframes have 3 unused configuration bits, holdovers from VP3 days.
michael@0 469 Most of the other unused bits in the VP3 headers were eliminated.
michael@0 470 I don't know why these remain.*/
michael@0 471 /*I wanted to eliminate wasted bits, but not all config wiggle room
michael@0 472 --Monty.*/
michael@0 473 val=oc_pack_read(&_dec->opb,3);
michael@0 474 if(val!=0)return TH_EIMPL;
michael@0 475 }
michael@0 476 return 0;
michael@0 477 }
michael@0 478
michael@0 479 /*Mark all fragments as coded and in OC_MODE_INTRA.
michael@0 480 This also builds up the coded fragment list (in coded order), and clears the
michael@0 481 uncoded fragment list.
michael@0 482 It does not update the coded macro block list nor the super block flags, as
michael@0 483 those are not used when decoding INTRA frames.*/
michael@0 484 static void oc_dec_mark_all_intra(oc_dec_ctx *_dec){
michael@0 485 const oc_sb_map *sb_maps;
michael@0 486 const oc_sb_flags *sb_flags;
michael@0 487 oc_fragment *frags;
michael@0 488 ptrdiff_t *coded_fragis;
michael@0 489 ptrdiff_t ncoded_fragis;
michael@0 490 ptrdiff_t prev_ncoded_fragis;
michael@0 491 unsigned nsbs;
michael@0 492 unsigned sbi;
michael@0 493 int pli;
michael@0 494 coded_fragis=_dec->state.coded_fragis;
michael@0 495 prev_ncoded_fragis=ncoded_fragis=0;
michael@0 496 sb_maps=(const oc_sb_map *)_dec->state.sb_maps;
michael@0 497 sb_flags=_dec->state.sb_flags;
michael@0 498 frags=_dec->state.frags;
michael@0 499 sbi=nsbs=0;
michael@0 500 for(pli=0;pli<3;pli++){
michael@0 501 nsbs+=_dec->state.fplanes[pli].nsbs;
michael@0 502 for(;sbi<nsbs;sbi++){
michael@0 503 int quadi;
michael@0 504 for(quadi=0;quadi<4;quadi++)if(sb_flags[sbi].quad_valid&1<<quadi){
michael@0 505 int bi;
michael@0 506 for(bi=0;bi<4;bi++){
michael@0 507 ptrdiff_t fragi;
michael@0 508 fragi=sb_maps[sbi][quadi][bi];
michael@0 509 if(fragi>=0){
michael@0 510 frags[fragi].coded=1;
michael@0 511 frags[fragi].refi=OC_FRAME_SELF;
michael@0 512 frags[fragi].mb_mode=OC_MODE_INTRA;
michael@0 513 coded_fragis[ncoded_fragis++]=fragi;
michael@0 514 }
michael@0 515 }
michael@0 516 }
michael@0 517 }
michael@0 518 _dec->state.ncoded_fragis[pli]=ncoded_fragis-prev_ncoded_fragis;
michael@0 519 prev_ncoded_fragis=ncoded_fragis;
michael@0 520 }
michael@0 521 _dec->state.ntotal_coded_fragis=ncoded_fragis;
michael@0 522 }
michael@0 523
michael@0 524 /*Decodes the bit flags indicating whether each super block is partially coded
michael@0 525 or not.
michael@0 526 Return: The number of partially coded super blocks.*/
michael@0 527 static unsigned oc_dec_partial_sb_flags_unpack(oc_dec_ctx *_dec){
michael@0 528 oc_sb_flags *sb_flags;
michael@0 529 unsigned nsbs;
michael@0 530 unsigned sbi;
michael@0 531 unsigned npartial;
michael@0 532 unsigned run_count;
michael@0 533 long val;
michael@0 534 int flag;
michael@0 535 val=oc_pack_read1(&_dec->opb);
michael@0 536 flag=(int)val;
michael@0 537 sb_flags=_dec->state.sb_flags;
michael@0 538 nsbs=_dec->state.nsbs;
michael@0 539 sbi=npartial=0;
michael@0 540 while(sbi<nsbs){
michael@0 541 int full_run;
michael@0 542 run_count=oc_sb_run_unpack(&_dec->opb);
michael@0 543 full_run=run_count>=4129;
michael@0 544 do{
michael@0 545 sb_flags[sbi].coded_partially=flag;
michael@0 546 sb_flags[sbi].coded_fully=0;
michael@0 547 npartial+=flag;
michael@0 548 sbi++;
michael@0 549 }
michael@0 550 while(--run_count>0&&sbi<nsbs);
michael@0 551 if(full_run&&sbi<nsbs){
michael@0 552 val=oc_pack_read1(&_dec->opb);
michael@0 553 flag=(int)val;
michael@0 554 }
michael@0 555 else flag=!flag;
michael@0 556 }
michael@0 557 /*TODO: run_count should be 0 here.
michael@0 558 If it's not, we should issue a warning of some kind.*/
michael@0 559 return npartial;
michael@0 560 }
michael@0 561
michael@0 562 /*Decodes the bit flags for whether or not each non-partially-coded super
michael@0 563 block is fully coded or not.
michael@0 564 This function should only be called if there is at least one
michael@0 565 non-partially-coded super block.
michael@0 566 Return: The number of partially coded super blocks.*/
michael@0 567 static void oc_dec_coded_sb_flags_unpack(oc_dec_ctx *_dec){
michael@0 568 oc_sb_flags *sb_flags;
michael@0 569 unsigned nsbs;
michael@0 570 unsigned sbi;
michael@0 571 unsigned run_count;
michael@0 572 long val;
michael@0 573 int flag;
michael@0 574 sb_flags=_dec->state.sb_flags;
michael@0 575 nsbs=_dec->state.nsbs;
michael@0 576 /*Skip partially coded super blocks.*/
michael@0 577 for(sbi=0;sb_flags[sbi].coded_partially;sbi++);
michael@0 578 val=oc_pack_read1(&_dec->opb);
michael@0 579 flag=(int)val;
michael@0 580 do{
michael@0 581 int full_run;
michael@0 582 run_count=oc_sb_run_unpack(&_dec->opb);
michael@0 583 full_run=run_count>=4129;
michael@0 584 for(;sbi<nsbs;sbi++){
michael@0 585 if(sb_flags[sbi].coded_partially)continue;
michael@0 586 if(run_count--<=0)break;
michael@0 587 sb_flags[sbi].coded_fully=flag;
michael@0 588 }
michael@0 589 if(full_run&&sbi<nsbs){
michael@0 590 val=oc_pack_read1(&_dec->opb);
michael@0 591 flag=(int)val;
michael@0 592 }
michael@0 593 else flag=!flag;
michael@0 594 }
michael@0 595 while(sbi<nsbs);
michael@0 596 /*TODO: run_count should be 0 here.
michael@0 597 If it's not, we should issue a warning of some kind.*/
michael@0 598 }
michael@0 599
michael@0 600 static void oc_dec_coded_flags_unpack(oc_dec_ctx *_dec){
michael@0 601 const oc_sb_map *sb_maps;
michael@0 602 const oc_sb_flags *sb_flags;
michael@0 603 signed char *mb_modes;
michael@0 604 oc_fragment *frags;
michael@0 605 unsigned nsbs;
michael@0 606 unsigned sbi;
michael@0 607 unsigned npartial;
michael@0 608 long val;
michael@0 609 int pli;
michael@0 610 int flag;
michael@0 611 int run_count;
michael@0 612 ptrdiff_t *coded_fragis;
michael@0 613 ptrdiff_t *uncoded_fragis;
michael@0 614 ptrdiff_t ncoded_fragis;
michael@0 615 ptrdiff_t nuncoded_fragis;
michael@0 616 ptrdiff_t prev_ncoded_fragis;
michael@0 617 npartial=oc_dec_partial_sb_flags_unpack(_dec);
michael@0 618 if(npartial<_dec->state.nsbs)oc_dec_coded_sb_flags_unpack(_dec);
michael@0 619 if(npartial>0){
michael@0 620 val=oc_pack_read1(&_dec->opb);
michael@0 621 flag=!(int)val;
michael@0 622 }
michael@0 623 else flag=0;
michael@0 624 sb_maps=(const oc_sb_map *)_dec->state.sb_maps;
michael@0 625 sb_flags=_dec->state.sb_flags;
michael@0 626 mb_modes=_dec->state.mb_modes;
michael@0 627 frags=_dec->state.frags;
michael@0 628 sbi=nsbs=run_count=0;
michael@0 629 coded_fragis=_dec->state.coded_fragis;
michael@0 630 uncoded_fragis=coded_fragis+_dec->state.nfrags;
michael@0 631 prev_ncoded_fragis=ncoded_fragis=nuncoded_fragis=0;
michael@0 632 for(pli=0;pli<3;pli++){
michael@0 633 nsbs+=_dec->state.fplanes[pli].nsbs;
michael@0 634 for(;sbi<nsbs;sbi++){
michael@0 635 int quadi;
michael@0 636 for(quadi=0;quadi<4;quadi++)if(sb_flags[sbi].quad_valid&1<<quadi){
michael@0 637 int quad_coded;
michael@0 638 int bi;
michael@0 639 quad_coded=0;
michael@0 640 for(bi=0;bi<4;bi++){
michael@0 641 ptrdiff_t fragi;
michael@0 642 fragi=sb_maps[sbi][quadi][bi];
michael@0 643 if(fragi>=0){
michael@0 644 int coded;
michael@0 645 if(sb_flags[sbi].coded_fully)coded=1;
michael@0 646 else if(!sb_flags[sbi].coded_partially)coded=0;
michael@0 647 else{
michael@0 648 if(run_count<=0){
michael@0 649 run_count=oc_block_run_unpack(&_dec->opb);
michael@0 650 flag=!flag;
michael@0 651 }
michael@0 652 run_count--;
michael@0 653 coded=flag;
michael@0 654 }
michael@0 655 if(coded)coded_fragis[ncoded_fragis++]=fragi;
michael@0 656 else *(uncoded_fragis-++nuncoded_fragis)=fragi;
michael@0 657 quad_coded|=coded;
michael@0 658 frags[fragi].coded=coded;
michael@0 659 frags[fragi].refi=OC_FRAME_NONE;
michael@0 660 }
michael@0 661 }
michael@0 662 /*Remember if there's a coded luma block in this macro block.*/
michael@0 663 if(!pli)mb_modes[sbi<<2|quadi]=quad_coded;
michael@0 664 }
michael@0 665 }
michael@0 666 _dec->state.ncoded_fragis[pli]=ncoded_fragis-prev_ncoded_fragis;
michael@0 667 prev_ncoded_fragis=ncoded_fragis;
michael@0 668 }
michael@0 669 _dec->state.ntotal_coded_fragis=ncoded_fragis;
michael@0 670 /*TODO: run_count should be 0 here.
michael@0 671 If it's not, we should issue a warning of some kind.*/
michael@0 672 }
michael@0 673
michael@0 674
michael@0 675 /*Coding scheme:
michael@0 676 Codeword Mode Index
michael@0 677 0 0
michael@0 678 10 1
michael@0 679 110 2
michael@0 680 1110 3
michael@0 681 11110 4
michael@0 682 111110 5
michael@0 683 1111110 6
michael@0 684 1111111 7*/
michael@0 685 static const ogg_int16_t OC_VLC_MODE_TREE[26]={
michael@0 686 4,
michael@0 687 -(1<<8|0),-(1<<8|0),-(1<<8|0),-(1<<8|0),
michael@0 688 -(1<<8|0),-(1<<8|0),-(1<<8|0),-(1<<8|0),
michael@0 689 -(2<<8|1),-(2<<8|1),-(2<<8|1),-(2<<8|1),
michael@0 690 -(3<<8|2),-(3<<8|2),-(4<<8|3),17,
michael@0 691 3,
michael@0 692 -(1<<8|4),-(1<<8|4),-(1<<8|4),-(1<<8|4),
michael@0 693 -(2<<8|5),-(2<<8|5),-(3<<8|6),-(3<<8|7)
michael@0 694 };
michael@0 695
michael@0 696 static const ogg_int16_t OC_CLC_MODE_TREE[9]={
michael@0 697 3,
michael@0 698 -(3<<8|0),-(3<<8|1),-(3<<8|2),-(3<<8|3),
michael@0 699 -(3<<8|4),-(3<<8|5),-(3<<8|6),-(3<<8|7)
michael@0 700 };
michael@0 701
michael@0 702 /*Unpacks the list of macro block modes for INTER frames.*/
michael@0 703 static void oc_dec_mb_modes_unpack(oc_dec_ctx *_dec){
michael@0 704 signed char *mb_modes;
michael@0 705 const unsigned char *alphabet;
michael@0 706 unsigned char scheme0_alphabet[8];
michael@0 707 const ogg_int16_t *mode_tree;
michael@0 708 size_t nmbs;
michael@0 709 size_t mbi;
michael@0 710 long val;
michael@0 711 int mode_scheme;
michael@0 712 val=oc_pack_read(&_dec->opb,3);
michael@0 713 mode_scheme=(int)val;
michael@0 714 if(mode_scheme==0){
michael@0 715 int mi;
michael@0 716 /*Just in case, initialize the modes to something.
michael@0 717 If the bitstream doesn't contain each index exactly once, it's likely
michael@0 718 corrupt and the rest of the packet is garbage anyway, but this way we
michael@0 719 won't crash, and we'll decode SOMETHING.*/
michael@0 720 /*LOOP VECTORIZES*/
michael@0 721 for(mi=0;mi<OC_NMODES;mi++)scheme0_alphabet[mi]=OC_MODE_INTER_NOMV;
michael@0 722 for(mi=0;mi<OC_NMODES;mi++){
michael@0 723 val=oc_pack_read(&_dec->opb,3);
michael@0 724 scheme0_alphabet[val]=OC_MODE_ALPHABETS[6][mi];
michael@0 725 }
michael@0 726 alphabet=scheme0_alphabet;
michael@0 727 }
michael@0 728 else alphabet=OC_MODE_ALPHABETS[mode_scheme-1];
michael@0 729 mode_tree=mode_scheme==7?OC_CLC_MODE_TREE:OC_VLC_MODE_TREE;
michael@0 730 mb_modes=_dec->state.mb_modes;
michael@0 731 nmbs=_dec->state.nmbs;
michael@0 732 for(mbi=0;mbi<nmbs;mbi++){
michael@0 733 if(mb_modes[mbi]>0){
michael@0 734 /*We have a coded luma block; decode a mode.*/
michael@0 735 mb_modes[mbi]=alphabet[oc_huff_token_decode(&_dec->opb,mode_tree)];
michael@0 736 }
michael@0 737 /*For other valid macro blocks, INTER_NOMV is forced, but we rely on the
michael@0 738 fact that OC_MODE_INTER_NOMV is already 0.*/
michael@0 739 }
michael@0 740 }
michael@0 741
michael@0 742
michael@0 743
michael@0 744 static const ogg_int16_t OC_VLC_MV_COMP_TREE[101]={
michael@0 745 5,
michael@0 746 -(3<<8|32+0),-(3<<8|32+0),-(3<<8|32+0),-(3<<8|32+0),
michael@0 747 -(3<<8|32+1),-(3<<8|32+1),-(3<<8|32+1),-(3<<8|32+1),
michael@0 748 -(3<<8|32-1),-(3<<8|32-1),-(3<<8|32-1),-(3<<8|32-1),
michael@0 749 -(4<<8|32+2),-(4<<8|32+2),-(4<<8|32-2),-(4<<8|32-2),
michael@0 750 -(4<<8|32+3),-(4<<8|32+3),-(4<<8|32-3),-(4<<8|32-3),
michael@0 751 33, 36, 39, 42,
michael@0 752 45, 50, 55, 60,
michael@0 753 65, 74, 83, 92,
michael@0 754 1,-(1<<8|32+4),-(1<<8|32-4),
michael@0 755 1,-(1<<8|32+5),-(1<<8|32-5),
michael@0 756 1,-(1<<8|32+6),-(1<<8|32-6),
michael@0 757 1,-(1<<8|32+7),-(1<<8|32-7),
michael@0 758 2,-(2<<8|32+8),-(2<<8|32-8),-(2<<8|32+9),-(2<<8|32-9),
michael@0 759 2,-(2<<8|32+10),-(2<<8|32-10),-(2<<8|32+11),-(2<<8|32-11),
michael@0 760 2,-(2<<8|32+12),-(2<<8|32-12),-(2<<8|32+13),-(2<<8|32-13),
michael@0 761 2,-(2<<8|32+14),-(2<<8|32-14),-(2<<8|32+15),-(2<<8|32-15),
michael@0 762 3,
michael@0 763 -(3<<8|32+16),-(3<<8|32-16),-(3<<8|32+17),-(3<<8|32-17),
michael@0 764 -(3<<8|32+18),-(3<<8|32-18),-(3<<8|32+19),-(3<<8|32-19),
michael@0 765 3,
michael@0 766 -(3<<8|32+20),-(3<<8|32-20),-(3<<8|32+21),-(3<<8|32-21),
michael@0 767 -(3<<8|32+22),-(3<<8|32-22),-(3<<8|32+23),-(3<<8|32-23),
michael@0 768 3,
michael@0 769 -(3<<8|32+24),-(3<<8|32-24),-(3<<8|32+25),-(3<<8|32-25),
michael@0 770 -(3<<8|32+26),-(3<<8|32-26),-(3<<8|32+27),-(3<<8|32-27),
michael@0 771 3,
michael@0 772 -(3<<8|32+28),-(3<<8|32-28),-(3<<8|32+29),-(3<<8|32-29),
michael@0 773 -(3<<8|32+30),-(3<<8|32-30),-(3<<8|32+31),-(3<<8|32-31)
michael@0 774 };
michael@0 775
michael@0 776 static const ogg_int16_t OC_CLC_MV_COMP_TREE[65]={
michael@0 777 6,
michael@0 778 -(6<<8|32 +0),-(6<<8|32 -0),-(6<<8|32 +1),-(6<<8|32 -1),
michael@0 779 -(6<<8|32 +2),-(6<<8|32 -2),-(6<<8|32 +3),-(6<<8|32 -3),
michael@0 780 -(6<<8|32 +4),-(6<<8|32 -4),-(6<<8|32 +5),-(6<<8|32 -5),
michael@0 781 -(6<<8|32 +6),-(6<<8|32 -6),-(6<<8|32 +7),-(6<<8|32 -7),
michael@0 782 -(6<<8|32 +8),-(6<<8|32 -8),-(6<<8|32 +9),-(6<<8|32 -9),
michael@0 783 -(6<<8|32+10),-(6<<8|32-10),-(6<<8|32+11),-(6<<8|32-11),
michael@0 784 -(6<<8|32+12),-(6<<8|32-12),-(6<<8|32+13),-(6<<8|32-13),
michael@0 785 -(6<<8|32+14),-(6<<8|32-14),-(6<<8|32+15),-(6<<8|32-15),
michael@0 786 -(6<<8|32+16),-(6<<8|32-16),-(6<<8|32+17),-(6<<8|32-17),
michael@0 787 -(6<<8|32+18),-(6<<8|32-18),-(6<<8|32+19),-(6<<8|32-19),
michael@0 788 -(6<<8|32+20),-(6<<8|32-20),-(6<<8|32+21),-(6<<8|32-21),
michael@0 789 -(6<<8|32+22),-(6<<8|32-22),-(6<<8|32+23),-(6<<8|32-23),
michael@0 790 -(6<<8|32+24),-(6<<8|32-24),-(6<<8|32+25),-(6<<8|32-25),
michael@0 791 -(6<<8|32+26),-(6<<8|32-26),-(6<<8|32+27),-(6<<8|32-27),
michael@0 792 -(6<<8|32+28),-(6<<8|32-28),-(6<<8|32+29),-(6<<8|32-29),
michael@0 793 -(6<<8|32+30),-(6<<8|32-30),-(6<<8|32+31),-(6<<8|32-31)
michael@0 794 };
michael@0 795
michael@0 796
michael@0 797 static oc_mv oc_mv_unpack(oc_pack_buf *_opb,const ogg_int16_t *_tree){
michael@0 798 int dx;
michael@0 799 int dy;
michael@0 800 dx=oc_huff_token_decode(_opb,_tree)-32;
michael@0 801 dy=oc_huff_token_decode(_opb,_tree)-32;
michael@0 802 return OC_MV(dx,dy);
michael@0 803 }
michael@0 804
michael@0 805 /*Unpacks the list of motion vectors for INTER frames, and propagtes the macro
michael@0 806 block modes and motion vectors to the individual fragments.*/
michael@0 807 static void oc_dec_mv_unpack_and_frag_modes_fill(oc_dec_ctx *_dec){
michael@0 808 const oc_mb_map *mb_maps;
michael@0 809 const signed char *mb_modes;
michael@0 810 oc_set_chroma_mvs_func set_chroma_mvs;
michael@0 811 const ogg_int16_t *mv_comp_tree;
michael@0 812 oc_fragment *frags;
michael@0 813 oc_mv *frag_mvs;
michael@0 814 const unsigned char *map_idxs;
michael@0 815 int map_nidxs;
michael@0 816 oc_mv last_mv;
michael@0 817 oc_mv prior_mv;
michael@0 818 oc_mv cbmvs[4];
michael@0 819 size_t nmbs;
michael@0 820 size_t mbi;
michael@0 821 long val;
michael@0 822 set_chroma_mvs=OC_SET_CHROMA_MVS_TABLE[_dec->state.info.pixel_fmt];
michael@0 823 val=oc_pack_read1(&_dec->opb);
michael@0 824 mv_comp_tree=val?OC_CLC_MV_COMP_TREE:OC_VLC_MV_COMP_TREE;
michael@0 825 map_idxs=OC_MB_MAP_IDXS[_dec->state.info.pixel_fmt];
michael@0 826 map_nidxs=OC_MB_MAP_NIDXS[_dec->state.info.pixel_fmt];
michael@0 827 prior_mv=last_mv=0;
michael@0 828 frags=_dec->state.frags;
michael@0 829 frag_mvs=_dec->state.frag_mvs;
michael@0 830 mb_maps=(const oc_mb_map *)_dec->state.mb_maps;
michael@0 831 mb_modes=_dec->state.mb_modes;
michael@0 832 nmbs=_dec->state.nmbs;
michael@0 833 for(mbi=0;mbi<nmbs;mbi++){
michael@0 834 int mb_mode;
michael@0 835 mb_mode=mb_modes[mbi];
michael@0 836 if(mb_mode!=OC_MODE_INVALID){
michael@0 837 oc_mv mbmv;
michael@0 838 ptrdiff_t fragi;
michael@0 839 int mapi;
michael@0 840 int mapii;
michael@0 841 int refi;
michael@0 842 if(mb_mode==OC_MODE_INTER_MV_FOUR){
michael@0 843 oc_mv lbmvs[4];
michael@0 844 int bi;
michael@0 845 prior_mv=last_mv;
michael@0 846 for(bi=0;bi<4;bi++){
michael@0 847 fragi=mb_maps[mbi][0][bi];
michael@0 848 if(frags[fragi].coded){
michael@0 849 frags[fragi].refi=OC_FRAME_PREV;
michael@0 850 frags[fragi].mb_mode=OC_MODE_INTER_MV_FOUR;
michael@0 851 lbmvs[bi]=last_mv=oc_mv_unpack(&_dec->opb,mv_comp_tree);
michael@0 852 frag_mvs[fragi]=lbmvs[bi];
michael@0 853 }
michael@0 854 else lbmvs[bi]=0;
michael@0 855 }
michael@0 856 (*set_chroma_mvs)(cbmvs,lbmvs);
michael@0 857 for(mapii=4;mapii<map_nidxs;mapii++){
michael@0 858 mapi=map_idxs[mapii];
michael@0 859 bi=mapi&3;
michael@0 860 fragi=mb_maps[mbi][mapi>>2][bi];
michael@0 861 if(frags[fragi].coded){
michael@0 862 frags[fragi].refi=OC_FRAME_PREV;
michael@0 863 frags[fragi].mb_mode=OC_MODE_INTER_MV_FOUR;
michael@0 864 frag_mvs[fragi]=cbmvs[bi];
michael@0 865 }
michael@0 866 }
michael@0 867 }
michael@0 868 else{
michael@0 869 switch(mb_mode){
michael@0 870 case OC_MODE_INTER_MV:{
michael@0 871 prior_mv=last_mv;
michael@0 872 last_mv=mbmv=oc_mv_unpack(&_dec->opb,mv_comp_tree);
michael@0 873 }break;
michael@0 874 case OC_MODE_INTER_MV_LAST:mbmv=last_mv;break;
michael@0 875 case OC_MODE_INTER_MV_LAST2:{
michael@0 876 mbmv=prior_mv;
michael@0 877 prior_mv=last_mv;
michael@0 878 last_mv=mbmv;
michael@0 879 }break;
michael@0 880 case OC_MODE_GOLDEN_MV:{
michael@0 881 mbmv=oc_mv_unpack(&_dec->opb,mv_comp_tree);
michael@0 882 }break;
michael@0 883 default:mbmv=0;break;
michael@0 884 }
michael@0 885 /*Fill in the MVs for the fragments.*/
michael@0 886 refi=OC_FRAME_FOR_MODE(mb_mode);
michael@0 887 mapii=0;
michael@0 888 do{
michael@0 889 mapi=map_idxs[mapii];
michael@0 890 fragi=mb_maps[mbi][mapi>>2][mapi&3];
michael@0 891 if(frags[fragi].coded){
michael@0 892 frags[fragi].refi=refi;
michael@0 893 frags[fragi].mb_mode=mb_mode;
michael@0 894 frag_mvs[fragi]=mbmv;
michael@0 895 }
michael@0 896 }
michael@0 897 while(++mapii<map_nidxs);
michael@0 898 }
michael@0 899 }
michael@0 900 }
michael@0 901 }
michael@0 902
michael@0 903 static void oc_dec_block_qis_unpack(oc_dec_ctx *_dec){
michael@0 904 oc_fragment *frags;
michael@0 905 const ptrdiff_t *coded_fragis;
michael@0 906 ptrdiff_t ncoded_fragis;
michael@0 907 ptrdiff_t fragii;
michael@0 908 ptrdiff_t fragi;
michael@0 909 ncoded_fragis=_dec->state.ntotal_coded_fragis;
michael@0 910 if(ncoded_fragis<=0)return;
michael@0 911 frags=_dec->state.frags;
michael@0 912 coded_fragis=_dec->state.coded_fragis;
michael@0 913 if(_dec->state.nqis==1){
michael@0 914 /*If this frame has only a single qi value, then just use it for all coded
michael@0 915 fragments.*/
michael@0 916 for(fragii=0;fragii<ncoded_fragis;fragii++){
michael@0 917 frags[coded_fragis[fragii]].qii=0;
michael@0 918 }
michael@0 919 }
michael@0 920 else{
michael@0 921 long val;
michael@0 922 int flag;
michael@0 923 int nqi1;
michael@0 924 int run_count;
michael@0 925 /*Otherwise, we decode a qi index for each fragment, using two passes of
michael@0 926 the same binary RLE scheme used for super-block coded bits.
michael@0 927 The first pass marks each fragment as having a qii of 0 or greater than
michael@0 928 0, and the second pass (if necessary), distinguishes between a qii of
michael@0 929 1 and 2.
michael@0 930 At first we just store the qii in the fragment.
michael@0 931 After all the qii's are decoded, we make a final pass to replace them
michael@0 932 with the corresponding qi's for this frame.*/
michael@0 933 val=oc_pack_read1(&_dec->opb);
michael@0 934 flag=(int)val;
michael@0 935 nqi1=0;
michael@0 936 fragii=0;
michael@0 937 while(fragii<ncoded_fragis){
michael@0 938 int full_run;
michael@0 939 run_count=oc_sb_run_unpack(&_dec->opb);
michael@0 940 full_run=run_count>=4129;
michael@0 941 do{
michael@0 942 frags[coded_fragis[fragii++]].qii=flag;
michael@0 943 nqi1+=flag;
michael@0 944 }
michael@0 945 while(--run_count>0&&fragii<ncoded_fragis);
michael@0 946 if(full_run&&fragii<ncoded_fragis){
michael@0 947 val=oc_pack_read1(&_dec->opb);
michael@0 948 flag=(int)val;
michael@0 949 }
michael@0 950 else flag=!flag;
michael@0 951 }
michael@0 952 /*TODO: run_count should be 0 here.
michael@0 953 If it's not, we should issue a warning of some kind.*/
michael@0 954 /*If we have 3 different qi's for this frame, and there was at least one
michael@0 955 fragment with a non-zero qi, make the second pass.*/
michael@0 956 if(_dec->state.nqis==3&&nqi1>0){
michael@0 957 /*Skip qii==0 fragments.*/
michael@0 958 for(fragii=0;frags[coded_fragis[fragii]].qii==0;fragii++);
michael@0 959 val=oc_pack_read1(&_dec->opb);
michael@0 960 flag=(int)val;
michael@0 961 do{
michael@0 962 int full_run;
michael@0 963 run_count=oc_sb_run_unpack(&_dec->opb);
michael@0 964 full_run=run_count>=4129;
michael@0 965 for(;fragii<ncoded_fragis;fragii++){
michael@0 966 fragi=coded_fragis[fragii];
michael@0 967 if(frags[fragi].qii==0)continue;
michael@0 968 if(run_count--<=0)break;
michael@0 969 frags[fragi].qii+=flag;
michael@0 970 }
michael@0 971 if(full_run&&fragii<ncoded_fragis){
michael@0 972 val=oc_pack_read1(&_dec->opb);
michael@0 973 flag=(int)val;
michael@0 974 }
michael@0 975 else flag=!flag;
michael@0 976 }
michael@0 977 while(fragii<ncoded_fragis);
michael@0 978 /*TODO: run_count should be 0 here.
michael@0 979 If it's not, we should issue a warning of some kind.*/
michael@0 980 }
michael@0 981 }
michael@0 982 }
michael@0 983
michael@0 984
michael@0 985
michael@0 986 /*Unpacks the DC coefficient tokens.
michael@0 987 Unlike when unpacking the AC coefficient tokens, we actually need to decode
michael@0 988 the DC coefficient values now so that we can do DC prediction.
michael@0 989 _huff_idx: The index of the Huffman table to use for each color plane.
michael@0 990 _ntoks_left: The number of tokens left to be decoded in each color plane for
michael@0 991 each coefficient.
michael@0 992 This is updated as EOB tokens and zero run tokens are decoded.
michael@0 993 Return: The length of any outstanding EOB run.*/
michael@0 994 static ptrdiff_t oc_dec_dc_coeff_unpack(oc_dec_ctx *_dec,int _huff_idxs[2],
michael@0 995 ptrdiff_t _ntoks_left[3][64]){
michael@0 996 unsigned char *dct_tokens;
michael@0 997 oc_fragment *frags;
michael@0 998 const ptrdiff_t *coded_fragis;
michael@0 999 ptrdiff_t ncoded_fragis;
michael@0 1000 ptrdiff_t fragii;
michael@0 1001 ptrdiff_t eobs;
michael@0 1002 ptrdiff_t ti;
michael@0 1003 int pli;
michael@0 1004 dct_tokens=_dec->dct_tokens;
michael@0 1005 frags=_dec->state.frags;
michael@0 1006 coded_fragis=_dec->state.coded_fragis;
michael@0 1007 ncoded_fragis=fragii=eobs=ti=0;
michael@0 1008 for(pli=0;pli<3;pli++){
michael@0 1009 ptrdiff_t run_counts[64];
michael@0 1010 ptrdiff_t eob_count;
michael@0 1011 ptrdiff_t eobi;
michael@0 1012 int rli;
michael@0 1013 ncoded_fragis+=_dec->state.ncoded_fragis[pli];
michael@0 1014 memset(run_counts,0,sizeof(run_counts));
michael@0 1015 _dec->eob_runs[pli][0]=eobs;
michael@0 1016 _dec->ti0[pli][0]=ti;
michael@0 1017 /*Continue any previous EOB run, if there was one.*/
michael@0 1018 eobi=eobs;
michael@0 1019 if(ncoded_fragis-fragii<eobi)eobi=ncoded_fragis-fragii;
michael@0 1020 eob_count=eobi;
michael@0 1021 eobs-=eobi;
michael@0 1022 while(eobi-->0)frags[coded_fragis[fragii++]].dc=0;
michael@0 1023 while(fragii<ncoded_fragis){
michael@0 1024 int token;
michael@0 1025 int cw;
michael@0 1026 int eb;
michael@0 1027 int skip;
michael@0 1028 token=oc_huff_token_decode(&_dec->opb,
michael@0 1029 _dec->huff_tables[_huff_idxs[pli+1>>1]]);
michael@0 1030 dct_tokens[ti++]=(unsigned char)token;
michael@0 1031 if(OC_DCT_TOKEN_NEEDS_MORE(token)){
michael@0 1032 eb=(int)oc_pack_read(&_dec->opb,
michael@0 1033 OC_INTERNAL_DCT_TOKEN_EXTRA_BITS[token]);
michael@0 1034 dct_tokens[ti++]=(unsigned char)eb;
michael@0 1035 if(token==OC_DCT_TOKEN_FAT_EOB)dct_tokens[ti++]=(unsigned char)(eb>>8);
michael@0 1036 eb<<=OC_DCT_TOKEN_EB_POS(token);
michael@0 1037 }
michael@0 1038 else eb=0;
michael@0 1039 cw=OC_DCT_CODE_WORD[token]+eb;
michael@0 1040 eobs=cw>>OC_DCT_CW_EOB_SHIFT&0xFFF;
michael@0 1041 if(cw==OC_DCT_CW_FINISH)eobs=OC_DCT_EOB_FINISH;
michael@0 1042 if(eobs){
michael@0 1043 eobi=OC_MINI(eobs,ncoded_fragis-fragii);
michael@0 1044 eob_count+=eobi;
michael@0 1045 eobs-=eobi;
michael@0 1046 while(eobi-->0)frags[coded_fragis[fragii++]].dc=0;
michael@0 1047 }
michael@0 1048 else{
michael@0 1049 int coeff;
michael@0 1050 skip=(unsigned char)(cw>>OC_DCT_CW_RLEN_SHIFT);
michael@0 1051 cw^=-(cw&1<<OC_DCT_CW_FLIP_BIT);
michael@0 1052 coeff=cw>>OC_DCT_CW_MAG_SHIFT;
michael@0 1053 if(skip)coeff=0;
michael@0 1054 run_counts[skip]++;
michael@0 1055 frags[coded_fragis[fragii++]].dc=coeff;
michael@0 1056 }
michael@0 1057 }
michael@0 1058 /*Add the total EOB count to the longest run length.*/
michael@0 1059 run_counts[63]+=eob_count;
michael@0 1060 /*And convert the run_counts array to a moment table.*/
michael@0 1061 for(rli=63;rli-->0;)run_counts[rli]+=run_counts[rli+1];
michael@0 1062 /*Finally, subtract off the number of coefficients that have been
michael@0 1063 accounted for by runs started in this coefficient.*/
michael@0 1064 for(rli=64;rli-->0;)_ntoks_left[pli][rli]-=run_counts[rli];
michael@0 1065 }
michael@0 1066 _dec->dct_tokens_count=ti;
michael@0 1067 return eobs;
michael@0 1068 }
michael@0 1069
michael@0 1070 /*Unpacks the AC coefficient tokens.
michael@0 1071 This can completely discard coefficient values while unpacking, and so is
michael@0 1072 somewhat simpler than unpacking the DC coefficient tokens.
michael@0 1073 _huff_idx: The index of the Huffman table to use for each color plane.
michael@0 1074 _ntoks_left: The number of tokens left to be decoded in each color plane for
michael@0 1075 each coefficient.
michael@0 1076 This is updated as EOB tokens and zero run tokens are decoded.
michael@0 1077 _eobs: The length of any outstanding EOB run from previous
michael@0 1078 coefficients.
michael@0 1079 Return: The length of any outstanding EOB run.*/
michael@0 1080 static int oc_dec_ac_coeff_unpack(oc_dec_ctx *_dec,int _zzi,int _huff_idxs[2],
michael@0 1081 ptrdiff_t _ntoks_left[3][64],ptrdiff_t _eobs){
michael@0 1082 unsigned char *dct_tokens;
michael@0 1083 ptrdiff_t ti;
michael@0 1084 int pli;
michael@0 1085 dct_tokens=_dec->dct_tokens;
michael@0 1086 ti=_dec->dct_tokens_count;
michael@0 1087 for(pli=0;pli<3;pli++){
michael@0 1088 ptrdiff_t run_counts[64];
michael@0 1089 ptrdiff_t eob_count;
michael@0 1090 size_t ntoks_left;
michael@0 1091 size_t ntoks;
michael@0 1092 int rli;
michael@0 1093 _dec->eob_runs[pli][_zzi]=_eobs;
michael@0 1094 _dec->ti0[pli][_zzi]=ti;
michael@0 1095 ntoks_left=_ntoks_left[pli][_zzi];
michael@0 1096 memset(run_counts,0,sizeof(run_counts));
michael@0 1097 eob_count=0;
michael@0 1098 ntoks=0;
michael@0 1099 while(ntoks+_eobs<ntoks_left){
michael@0 1100 int token;
michael@0 1101 int cw;
michael@0 1102 int eb;
michael@0 1103 int skip;
michael@0 1104 ntoks+=_eobs;
michael@0 1105 eob_count+=_eobs;
michael@0 1106 token=oc_huff_token_decode(&_dec->opb,
michael@0 1107 _dec->huff_tables[_huff_idxs[pli+1>>1]]);
michael@0 1108 dct_tokens[ti++]=(unsigned char)token;
michael@0 1109 if(OC_DCT_TOKEN_NEEDS_MORE(token)){
michael@0 1110 eb=(int)oc_pack_read(&_dec->opb,
michael@0 1111 OC_INTERNAL_DCT_TOKEN_EXTRA_BITS[token]);
michael@0 1112 dct_tokens[ti++]=(unsigned char)eb;
michael@0 1113 if(token==OC_DCT_TOKEN_FAT_EOB)dct_tokens[ti++]=(unsigned char)(eb>>8);
michael@0 1114 eb<<=OC_DCT_TOKEN_EB_POS(token);
michael@0 1115 }
michael@0 1116 else eb=0;
michael@0 1117 cw=OC_DCT_CODE_WORD[token]+eb;
michael@0 1118 skip=(unsigned char)(cw>>OC_DCT_CW_RLEN_SHIFT);
michael@0 1119 _eobs=cw>>OC_DCT_CW_EOB_SHIFT&0xFFF;
michael@0 1120 if(cw==OC_DCT_CW_FINISH)_eobs=OC_DCT_EOB_FINISH;
michael@0 1121 if(_eobs==0){
michael@0 1122 run_counts[skip]++;
michael@0 1123 ntoks++;
michael@0 1124 }
michael@0 1125 }
michael@0 1126 /*Add the portion of the last EOB run actually used by this coefficient.*/
michael@0 1127 eob_count+=ntoks_left-ntoks;
michael@0 1128 /*And remove it from the remaining EOB count.*/
michael@0 1129 _eobs-=ntoks_left-ntoks;
michael@0 1130 /*Add the total EOB count to the longest run length.*/
michael@0 1131 run_counts[63]+=eob_count;
michael@0 1132 /*And convert the run_counts array to a moment table.*/
michael@0 1133 for(rli=63;rli-->0;)run_counts[rli]+=run_counts[rli+1];
michael@0 1134 /*Finally, subtract off the number of coefficients that have been
michael@0 1135 accounted for by runs started in this coefficient.*/
michael@0 1136 for(rli=64-_zzi;rli-->0;)_ntoks_left[pli][_zzi+rli]-=run_counts[rli];
michael@0 1137 }
michael@0 1138 _dec->dct_tokens_count=ti;
michael@0 1139 return _eobs;
michael@0 1140 }
michael@0 1141
michael@0 1142 /*Tokens describing the DCT coefficients that belong to each fragment are
michael@0 1143 stored in the bitstream grouped by coefficient, not by fragment.
michael@0 1144
michael@0 1145 This means that we either decode all the tokens in order, building up a
michael@0 1146 separate coefficient list for each fragment as we go, and then go back and
michael@0 1147 do the iDCT on each fragment, or we have to create separate lists of tokens
michael@0 1148 for each coefficient, so that we can pull the next token required off the
michael@0 1149 head of the appropriate list when decoding a specific fragment.
michael@0 1150
michael@0 1151 The former was VP3's choice, and it meant 2*w*h extra storage for all the
michael@0 1152 decoded coefficient values.
michael@0 1153
michael@0 1154 We take the second option, which lets us store just one to three bytes per
michael@0 1155 token (generally far fewer than the number of coefficients, due to EOB
michael@0 1156 tokens and zero runs), and which requires us to only maintain a counter for
michael@0 1157 each of the 64 coefficients, instead of a counter for every fragment to
michael@0 1158 determine where the next token goes.
michael@0 1159
michael@0 1160 We actually use 3 counters per coefficient, one for each color plane, so we
michael@0 1161 can decode all color planes simultaneously.
michael@0 1162 This lets color conversion, etc., be done as soon as a full MCU (one or
michael@0 1163 two super block rows) is decoded, while the image data is still in cache.*/
michael@0 1164
michael@0 1165 static void oc_dec_residual_tokens_unpack(oc_dec_ctx *_dec){
michael@0 1166 static const unsigned char OC_HUFF_LIST_MAX[5]={1,6,15,28,64};
michael@0 1167 ptrdiff_t ntoks_left[3][64];
michael@0 1168 int huff_idxs[2];
michael@0 1169 ptrdiff_t eobs;
michael@0 1170 long val;
michael@0 1171 int pli;
michael@0 1172 int zzi;
michael@0 1173 int hgi;
michael@0 1174 for(pli=0;pli<3;pli++)for(zzi=0;zzi<64;zzi++){
michael@0 1175 ntoks_left[pli][zzi]=_dec->state.ncoded_fragis[pli];
michael@0 1176 }
michael@0 1177 val=oc_pack_read(&_dec->opb,4);
michael@0 1178 huff_idxs[0]=(int)val;
michael@0 1179 val=oc_pack_read(&_dec->opb,4);
michael@0 1180 huff_idxs[1]=(int)val;
michael@0 1181 _dec->eob_runs[0][0]=0;
michael@0 1182 eobs=oc_dec_dc_coeff_unpack(_dec,huff_idxs,ntoks_left);
michael@0 1183 #if defined(HAVE_CAIRO)
michael@0 1184 _dec->telemetry_dc_bytes=oc_pack_bytes_left(&_dec->opb);
michael@0 1185 #endif
michael@0 1186 val=oc_pack_read(&_dec->opb,4);
michael@0 1187 huff_idxs[0]=(int)val;
michael@0 1188 val=oc_pack_read(&_dec->opb,4);
michael@0 1189 huff_idxs[1]=(int)val;
michael@0 1190 zzi=1;
michael@0 1191 for(hgi=1;hgi<5;hgi++){
michael@0 1192 huff_idxs[0]+=16;
michael@0 1193 huff_idxs[1]+=16;
michael@0 1194 for(;zzi<OC_HUFF_LIST_MAX[hgi];zzi++){
michael@0 1195 eobs=oc_dec_ac_coeff_unpack(_dec,zzi,huff_idxs,ntoks_left,eobs);
michael@0 1196 }
michael@0 1197 }
michael@0 1198 /*TODO: eobs should be exactly zero, or 4096 or greater.
michael@0 1199 The second case occurs when an EOB run of size zero is encountered, which
michael@0 1200 gets treated as an infinite EOB run (where infinity is PTRDIFF_MAX).
michael@0 1201 If neither of these conditions holds, then a warning should be issued.*/
michael@0 1202 }
michael@0 1203
michael@0 1204
michael@0 1205 static int oc_dec_postprocess_init(oc_dec_ctx *_dec){
michael@0 1206 /*pp_level 0: disabled; free any memory used and return*/
michael@0 1207 if(_dec->pp_level<=OC_PP_LEVEL_DISABLED){
michael@0 1208 if(_dec->dc_qis!=NULL){
michael@0 1209 _ogg_free(_dec->dc_qis);
michael@0 1210 _dec->dc_qis=NULL;
michael@0 1211 _ogg_free(_dec->variances);
michael@0 1212 _dec->variances=NULL;
michael@0 1213 _ogg_free(_dec->pp_frame_data);
michael@0 1214 _dec->pp_frame_data=NULL;
michael@0 1215 }
michael@0 1216 return 1;
michael@0 1217 }
michael@0 1218 if(_dec->dc_qis==NULL){
michael@0 1219 /*If we haven't been tracking DC quantization indices, there's no point in
michael@0 1220 starting now.*/
michael@0 1221 if(_dec->state.frame_type!=OC_INTRA_FRAME)return 1;
michael@0 1222 _dec->dc_qis=(unsigned char *)_ogg_malloc(
michael@0 1223 _dec->state.nfrags*sizeof(_dec->dc_qis[0]));
michael@0 1224 if(_dec->dc_qis==NULL)return 1;
michael@0 1225 memset(_dec->dc_qis,_dec->state.qis[0],_dec->state.nfrags);
michael@0 1226 }
michael@0 1227 else{
michael@0 1228 unsigned char *dc_qis;
michael@0 1229 const ptrdiff_t *coded_fragis;
michael@0 1230 ptrdiff_t ncoded_fragis;
michael@0 1231 ptrdiff_t fragii;
michael@0 1232 unsigned char qi0;
michael@0 1233 /*Update the DC quantization index of each coded block.*/
michael@0 1234 dc_qis=_dec->dc_qis;
michael@0 1235 coded_fragis=_dec->state.coded_fragis;
michael@0 1236 ncoded_fragis=_dec->state.ncoded_fragis[0]+
michael@0 1237 _dec->state.ncoded_fragis[1]+_dec->state.ncoded_fragis[2];
michael@0 1238 qi0=(unsigned char)_dec->state.qis[0];
michael@0 1239 for(fragii=0;fragii<ncoded_fragis;fragii++){
michael@0 1240 dc_qis[coded_fragis[fragii]]=qi0;
michael@0 1241 }
michael@0 1242 }
michael@0 1243 /*pp_level 1: Stop after updating DC quantization indices.*/
michael@0 1244 if(_dec->pp_level<=OC_PP_LEVEL_TRACKDCQI){
michael@0 1245 if(_dec->variances!=NULL){
michael@0 1246 _ogg_free(_dec->variances);
michael@0 1247 _dec->variances=NULL;
michael@0 1248 _ogg_free(_dec->pp_frame_data);
michael@0 1249 _dec->pp_frame_data=NULL;
michael@0 1250 }
michael@0 1251 return 1;
michael@0 1252 }
michael@0 1253 if(_dec->variances==NULL){
michael@0 1254 size_t frame_sz;
michael@0 1255 size_t c_sz;
michael@0 1256 int c_w;
michael@0 1257 int c_h;
michael@0 1258 frame_sz=_dec->state.info.frame_width*(size_t)_dec->state.info.frame_height;
michael@0 1259 c_w=_dec->state.info.frame_width>>!(_dec->state.info.pixel_fmt&1);
michael@0 1260 c_h=_dec->state.info.frame_height>>!(_dec->state.info.pixel_fmt&2);
michael@0 1261 c_sz=c_w*(size_t)c_h;
michael@0 1262 /*Allocate space for the chroma planes, even if we're not going to use
michael@0 1263 them; this simplifies allocation state management, though it may waste
michael@0 1264 memory on the few systems that don't overcommit pages.*/
michael@0 1265 frame_sz+=c_sz<<1;
michael@0 1266 _dec->pp_frame_data=(unsigned char *)_ogg_malloc(
michael@0 1267 frame_sz*sizeof(_dec->pp_frame_data[0]));
michael@0 1268 _dec->variances=(int *)_ogg_malloc(
michael@0 1269 _dec->state.nfrags*sizeof(_dec->variances[0]));
michael@0 1270 if(_dec->variances==NULL||_dec->pp_frame_data==NULL){
michael@0 1271 _ogg_free(_dec->pp_frame_data);
michael@0 1272 _dec->pp_frame_data=NULL;
michael@0 1273 _ogg_free(_dec->variances);
michael@0 1274 _dec->variances=NULL;
michael@0 1275 return 1;
michael@0 1276 }
michael@0 1277 /*Force an update of the PP buffer pointers.*/
michael@0 1278 _dec->pp_frame_state=0;
michael@0 1279 }
michael@0 1280 /*Update the PP buffer pointers if necessary.*/
michael@0 1281 if(_dec->pp_frame_state!=1+(_dec->pp_level>=OC_PP_LEVEL_DEBLOCKC)){
michael@0 1282 if(_dec->pp_level<OC_PP_LEVEL_DEBLOCKC){
michael@0 1283 /*If chroma processing is disabled, just use the PP luma plane.*/
michael@0 1284 _dec->pp_frame_buf[0].width=_dec->state.info.frame_width;
michael@0 1285 _dec->pp_frame_buf[0].height=_dec->state.info.frame_height;
michael@0 1286 _dec->pp_frame_buf[0].stride=-_dec->pp_frame_buf[0].width;
michael@0 1287 _dec->pp_frame_buf[0].data=_dec->pp_frame_data+
michael@0 1288 (1-_dec->pp_frame_buf[0].height)*(ptrdiff_t)_dec->pp_frame_buf[0].stride;
michael@0 1289 }
michael@0 1290 else{
michael@0 1291 size_t y_sz;
michael@0 1292 size_t c_sz;
michael@0 1293 int c_w;
michael@0 1294 int c_h;
michael@0 1295 /*Otherwise, set up pointers to all three PP planes.*/
michael@0 1296 y_sz=_dec->state.info.frame_width*(size_t)_dec->state.info.frame_height;
michael@0 1297 c_w=_dec->state.info.frame_width>>!(_dec->state.info.pixel_fmt&1);
michael@0 1298 c_h=_dec->state.info.frame_height>>!(_dec->state.info.pixel_fmt&2);
michael@0 1299 c_sz=c_w*(size_t)c_h;
michael@0 1300 _dec->pp_frame_buf[0].width=_dec->state.info.frame_width;
michael@0 1301 _dec->pp_frame_buf[0].height=_dec->state.info.frame_height;
michael@0 1302 _dec->pp_frame_buf[0].stride=_dec->pp_frame_buf[0].width;
michael@0 1303 _dec->pp_frame_buf[0].data=_dec->pp_frame_data;
michael@0 1304 _dec->pp_frame_buf[1].width=c_w;
michael@0 1305 _dec->pp_frame_buf[1].height=c_h;
michael@0 1306 _dec->pp_frame_buf[1].stride=_dec->pp_frame_buf[1].width;
michael@0 1307 _dec->pp_frame_buf[1].data=_dec->pp_frame_buf[0].data+y_sz;
michael@0 1308 _dec->pp_frame_buf[2].width=c_w;
michael@0 1309 _dec->pp_frame_buf[2].height=c_h;
michael@0 1310 _dec->pp_frame_buf[2].stride=_dec->pp_frame_buf[2].width;
michael@0 1311 _dec->pp_frame_buf[2].data=_dec->pp_frame_buf[1].data+c_sz;
michael@0 1312 oc_ycbcr_buffer_flip(_dec->pp_frame_buf,_dec->pp_frame_buf);
michael@0 1313 }
michael@0 1314 _dec->pp_frame_state=1+(_dec->pp_level>=OC_PP_LEVEL_DEBLOCKC);
michael@0 1315 }
michael@0 1316 /*If we're not processing chroma, copy the reference frame's chroma planes.*/
michael@0 1317 if(_dec->pp_level<OC_PP_LEVEL_DEBLOCKC){
michael@0 1318 memcpy(_dec->pp_frame_buf+1,
michael@0 1319 _dec->state.ref_frame_bufs[_dec->state.ref_frame_idx[OC_FRAME_SELF]]+1,
michael@0 1320 sizeof(_dec->pp_frame_buf[1])*2);
michael@0 1321 }
michael@0 1322 return 0;
michael@0 1323 }
michael@0 1324
michael@0 1325
michael@0 1326 /*Initialize the main decoding pipeline.*/
michael@0 1327 static void oc_dec_pipeline_init(oc_dec_ctx *_dec,
michael@0 1328 oc_dec_pipeline_state *_pipe){
michael@0 1329 const ptrdiff_t *coded_fragis;
michael@0 1330 const ptrdiff_t *uncoded_fragis;
michael@0 1331 int flimit;
michael@0 1332 int pli;
michael@0 1333 int qii;
michael@0 1334 int qti;
michael@0 1335 int zzi;
michael@0 1336 /*If chroma is sub-sampled in the vertical direction, we have to decode two
michael@0 1337 super block rows of Y' for each super block row of Cb and Cr.*/
michael@0 1338 _pipe->mcu_nvfrags=4<<!(_dec->state.info.pixel_fmt&2);
michael@0 1339 /*Initialize the token and extra bits indices for each plane and
michael@0 1340 coefficient.*/
michael@0 1341 memcpy(_pipe->ti,_dec->ti0,sizeof(_pipe->ti));
michael@0 1342 /*Also copy over the initial the EOB run counts.*/
michael@0 1343 memcpy(_pipe->eob_runs,_dec->eob_runs,sizeof(_pipe->eob_runs));
michael@0 1344 /*Set up per-plane pointers to the coded and uncoded fragments lists.*/
michael@0 1345 coded_fragis=_dec->state.coded_fragis;
michael@0 1346 uncoded_fragis=coded_fragis+_dec->state.nfrags;
michael@0 1347 for(pli=0;pli<3;pli++){
michael@0 1348 ptrdiff_t ncoded_fragis;
michael@0 1349 _pipe->coded_fragis[pli]=coded_fragis;
michael@0 1350 _pipe->uncoded_fragis[pli]=uncoded_fragis;
michael@0 1351 ncoded_fragis=_dec->state.ncoded_fragis[pli];
michael@0 1352 coded_fragis+=ncoded_fragis;
michael@0 1353 uncoded_fragis+=ncoded_fragis-_dec->state.fplanes[pli].nfrags;
michael@0 1354 }
michael@0 1355 /*Set up condensed quantizer tables.*/
michael@0 1356 for(pli=0;pli<3;pli++){
michael@0 1357 for(qii=0;qii<_dec->state.nqis;qii++){
michael@0 1358 for(qti=0;qti<2;qti++){
michael@0 1359 _pipe->dequant[pli][qii][qti]=
michael@0 1360 _dec->state.dequant_tables[_dec->state.qis[qii]][pli][qti];
michael@0 1361 }
michael@0 1362 }
michael@0 1363 }
michael@0 1364 /*Set the previous DC predictor to 0 for all color planes and frame types.*/
michael@0 1365 memset(_pipe->pred_last,0,sizeof(_pipe->pred_last));
michael@0 1366 /*Initialize the bounding value array for the loop filter.*/
michael@0 1367 flimit=_dec->state.loop_filter_limits[_dec->state.qis[0]];
michael@0 1368 _pipe->loop_filter=flimit!=0;
michael@0 1369 if(flimit!=0)oc_loop_filter_init(&_dec->state,_pipe->bounding_values,flimit);
michael@0 1370 /*Initialize any buffers needed for post-processing.
michael@0 1371 We also save the current post-processing level, to guard against the user
michael@0 1372 changing it from a callback.*/
michael@0 1373 if(!oc_dec_postprocess_init(_dec))_pipe->pp_level=_dec->pp_level;
michael@0 1374 /*If we don't have enough information to post-process, disable it, regardless
michael@0 1375 of the user-requested level.*/
michael@0 1376 else{
michael@0 1377 _pipe->pp_level=OC_PP_LEVEL_DISABLED;
michael@0 1378 memcpy(_dec->pp_frame_buf,
michael@0 1379 _dec->state.ref_frame_bufs[_dec->state.ref_frame_idx[OC_FRAME_SELF]],
michael@0 1380 sizeof(_dec->pp_frame_buf[0])*3);
michael@0 1381 }
michael@0 1382 /*Clear down the DCT coefficient buffer for the first block.*/
michael@0 1383 for(zzi=0;zzi<64;zzi++)_pipe->dct_coeffs[zzi]=0;
michael@0 1384 }
michael@0 1385
michael@0 1386 /*Undo the DC prediction in a single plane of an MCU (one or two super block
michael@0 1387 rows).
michael@0 1388 As a side effect, the number of coded and uncoded fragments in this plane of
michael@0 1389 the MCU is also computed.*/
michael@0 1390 void oc_dec_dc_unpredict_mcu_plane_c(oc_dec_ctx *_dec,
michael@0 1391 oc_dec_pipeline_state *_pipe,int _pli){
michael@0 1392 const oc_fragment_plane *fplane;
michael@0 1393 oc_fragment *frags;
michael@0 1394 int *pred_last;
michael@0 1395 ptrdiff_t ncoded_fragis;
michael@0 1396 ptrdiff_t fragi;
michael@0 1397 int fragx;
michael@0 1398 int fragy;
michael@0 1399 int fragy0;
michael@0 1400 int fragy_end;
michael@0 1401 int nhfrags;
michael@0 1402 /*Compute the first and last fragment row of the current MCU for this
michael@0 1403 plane.*/
michael@0 1404 fplane=_dec->state.fplanes+_pli;
michael@0 1405 fragy0=_pipe->fragy0[_pli];
michael@0 1406 fragy_end=_pipe->fragy_end[_pli];
michael@0 1407 nhfrags=fplane->nhfrags;
michael@0 1408 pred_last=_pipe->pred_last[_pli];
michael@0 1409 frags=_dec->state.frags;
michael@0 1410 ncoded_fragis=0;
michael@0 1411 fragi=fplane->froffset+fragy0*(ptrdiff_t)nhfrags;
michael@0 1412 for(fragy=fragy0;fragy<fragy_end;fragy++){
michael@0 1413 if(fragy==0){
michael@0 1414 /*For the first row, all of the cases reduce to just using the previous
michael@0 1415 predictor for the same reference frame.*/
michael@0 1416 for(fragx=0;fragx<nhfrags;fragx++,fragi++){
michael@0 1417 if(frags[fragi].coded){
michael@0 1418 int refi;
michael@0 1419 refi=frags[fragi].refi;
michael@0 1420 pred_last[refi]=frags[fragi].dc+=pred_last[refi];
michael@0 1421 ncoded_fragis++;
michael@0 1422 }
michael@0 1423 }
michael@0 1424 }
michael@0 1425 else{
michael@0 1426 oc_fragment *u_frags;
michael@0 1427 int l_ref;
michael@0 1428 int ul_ref;
michael@0 1429 int u_ref;
michael@0 1430 u_frags=frags-nhfrags;
michael@0 1431 l_ref=-1;
michael@0 1432 ul_ref=-1;
michael@0 1433 u_ref=u_frags[fragi].refi;
michael@0 1434 for(fragx=0;fragx<nhfrags;fragx++,fragi++){
michael@0 1435 int ur_ref;
michael@0 1436 if(fragx+1>=nhfrags)ur_ref=-1;
michael@0 1437 else ur_ref=u_frags[fragi+1].refi;
michael@0 1438 if(frags[fragi].coded){
michael@0 1439 int pred;
michael@0 1440 int refi;
michael@0 1441 refi=frags[fragi].refi;
michael@0 1442 /*We break out a separate case based on which of our neighbors use
michael@0 1443 the same reference frames.
michael@0 1444 This is somewhat faster than trying to make a generic case which
michael@0 1445 handles all of them, since it reduces lots of poorly predicted
michael@0 1446 jumps to one switch statement, and also lets a number of the
michael@0 1447 multiplications be optimized out by strength reduction.*/
michael@0 1448 switch((l_ref==refi)|(ul_ref==refi)<<1|
michael@0 1449 (u_ref==refi)<<2|(ur_ref==refi)<<3){
michael@0 1450 default:pred=pred_last[refi];break;
michael@0 1451 case 1:
michael@0 1452 case 3:pred=frags[fragi-1].dc;break;
michael@0 1453 case 2:pred=u_frags[fragi-1].dc;break;
michael@0 1454 case 4:
michael@0 1455 case 6:
michael@0 1456 case 12:pred=u_frags[fragi].dc;break;
michael@0 1457 case 5:pred=(frags[fragi-1].dc+u_frags[fragi].dc)/2;break;
michael@0 1458 case 8:pred=u_frags[fragi+1].dc;break;
michael@0 1459 case 9:
michael@0 1460 case 11:
michael@0 1461 case 13:{
michael@0 1462 /*The TI compiler mis-compiles this line.*/
michael@0 1463 pred=(75*frags[fragi-1].dc+53*u_frags[fragi+1].dc)/128;
michael@0 1464 }break;
michael@0 1465 case 10:pred=(u_frags[fragi-1].dc+u_frags[fragi+1].dc)/2;break;
michael@0 1466 case 14:{
michael@0 1467 pred=(3*(u_frags[fragi-1].dc+u_frags[fragi+1].dc)
michael@0 1468 +10*u_frags[fragi].dc)/16;
michael@0 1469 }break;
michael@0 1470 case 7:
michael@0 1471 case 15:{
michael@0 1472 int p0;
michael@0 1473 int p1;
michael@0 1474 int p2;
michael@0 1475 p0=frags[fragi-1].dc;
michael@0 1476 p1=u_frags[fragi-1].dc;
michael@0 1477 p2=u_frags[fragi].dc;
michael@0 1478 pred=(29*(p0+p2)-26*p1)/32;
michael@0 1479 if(abs(pred-p2)>128)pred=p2;
michael@0 1480 else if(abs(pred-p0)>128)pred=p0;
michael@0 1481 else if(abs(pred-p1)>128)pred=p1;
michael@0 1482 }break;
michael@0 1483 }
michael@0 1484 pred_last[refi]=frags[fragi].dc+=pred;
michael@0 1485 ncoded_fragis++;
michael@0 1486 l_ref=refi;
michael@0 1487 }
michael@0 1488 else l_ref=-1;
michael@0 1489 ul_ref=u_ref;
michael@0 1490 u_ref=ur_ref;
michael@0 1491 }
michael@0 1492 }
michael@0 1493 }
michael@0 1494 _pipe->ncoded_fragis[_pli]=ncoded_fragis;
michael@0 1495 /*Also save the number of uncoded fragments so we know how many to copy.*/
michael@0 1496 _pipe->nuncoded_fragis[_pli]=
michael@0 1497 (fragy_end-fragy0)*(ptrdiff_t)nhfrags-ncoded_fragis;
michael@0 1498 }
michael@0 1499
michael@0 1500 /*Reconstructs all coded fragments in a single MCU (one or two super block
michael@0 1501 rows).
michael@0 1502 This requires that each coded fragment have a proper macro block mode and
michael@0 1503 motion vector (if not in INTRA mode), and have its DC value decoded, with
michael@0 1504 the DC prediction process reversed, and the number of coded and uncoded
michael@0 1505 fragments in this plane of the MCU be counted.
michael@0 1506 The token lists for each color plane and coefficient should also be filled
michael@0 1507 in, along with initial token offsets, extra bits offsets, and EOB run
michael@0 1508 counts.*/
michael@0 1509 static void oc_dec_frags_recon_mcu_plane(oc_dec_ctx *_dec,
michael@0 1510 oc_dec_pipeline_state *_pipe,int _pli){
michael@0 1511 unsigned char *dct_tokens;
michael@0 1512 const unsigned char *dct_fzig_zag;
michael@0 1513 ogg_uint16_t dc_quant[2];
michael@0 1514 const oc_fragment *frags;
michael@0 1515 const ptrdiff_t *coded_fragis;
michael@0 1516 ptrdiff_t ncoded_fragis;
michael@0 1517 ptrdiff_t fragii;
michael@0 1518 ptrdiff_t *ti;
michael@0 1519 ptrdiff_t *eob_runs;
michael@0 1520 int qti;
michael@0 1521 dct_tokens=_dec->dct_tokens;
michael@0 1522 dct_fzig_zag=_dec->state.opt_data.dct_fzig_zag;
michael@0 1523 frags=_dec->state.frags;
michael@0 1524 coded_fragis=_pipe->coded_fragis[_pli];
michael@0 1525 ncoded_fragis=_pipe->ncoded_fragis[_pli];
michael@0 1526 ti=_pipe->ti[_pli];
michael@0 1527 eob_runs=_pipe->eob_runs[_pli];
michael@0 1528 for(qti=0;qti<2;qti++)dc_quant[qti]=_pipe->dequant[_pli][0][qti][0];
michael@0 1529 for(fragii=0;fragii<ncoded_fragis;fragii++){
michael@0 1530 const ogg_uint16_t *ac_quant;
michael@0 1531 ptrdiff_t fragi;
michael@0 1532 int last_zzi;
michael@0 1533 int zzi;
michael@0 1534 fragi=coded_fragis[fragii];
michael@0 1535 qti=frags[fragi].mb_mode!=OC_MODE_INTRA;
michael@0 1536 ac_quant=_pipe->dequant[_pli][frags[fragi].qii][qti];
michael@0 1537 /*Decode the AC coefficients.*/
michael@0 1538 for(zzi=0;zzi<64;){
michael@0 1539 int token;
michael@0 1540 last_zzi=zzi;
michael@0 1541 if(eob_runs[zzi]){
michael@0 1542 eob_runs[zzi]--;
michael@0 1543 break;
michael@0 1544 }
michael@0 1545 else{
michael@0 1546 ptrdiff_t eob;
michael@0 1547 int cw;
michael@0 1548 int rlen;
michael@0 1549 int coeff;
michael@0 1550 int lti;
michael@0 1551 lti=ti[zzi];
michael@0 1552 token=dct_tokens[lti++];
michael@0 1553 cw=OC_DCT_CODE_WORD[token];
michael@0 1554 /*These parts could be done branchless, but the branches are fairly
michael@0 1555 predictable and the C code translates into more than a few
michael@0 1556 instructions, so it's worth it to avoid them.*/
michael@0 1557 if(OC_DCT_TOKEN_NEEDS_MORE(token)){
michael@0 1558 cw+=dct_tokens[lti++]<<OC_DCT_TOKEN_EB_POS(token);
michael@0 1559 }
michael@0 1560 eob=cw>>OC_DCT_CW_EOB_SHIFT&0xFFF;
michael@0 1561 if(token==OC_DCT_TOKEN_FAT_EOB){
michael@0 1562 eob+=dct_tokens[lti++]<<8;
michael@0 1563 if(eob==0)eob=OC_DCT_EOB_FINISH;
michael@0 1564 }
michael@0 1565 rlen=(unsigned char)(cw>>OC_DCT_CW_RLEN_SHIFT);
michael@0 1566 cw^=-(cw&1<<OC_DCT_CW_FLIP_BIT);
michael@0 1567 coeff=cw>>OC_DCT_CW_MAG_SHIFT;
michael@0 1568 eob_runs[zzi]=eob;
michael@0 1569 ti[zzi]=lti;
michael@0 1570 zzi+=rlen;
michael@0 1571 _pipe->dct_coeffs[dct_fzig_zag[zzi]]=
michael@0 1572 (ogg_int16_t)(coeff*(int)ac_quant[zzi]);
michael@0 1573 zzi+=!eob;
michael@0 1574 }
michael@0 1575 }
michael@0 1576 /*TODO: zzi should be exactly 64 here.
michael@0 1577 If it's not, we should report some kind of warning.*/
michael@0 1578 zzi=OC_MINI(zzi,64);
michael@0 1579 _pipe->dct_coeffs[0]=(ogg_int16_t)frags[fragi].dc;
michael@0 1580 /*last_zzi is always initialized.
michael@0 1581 If your compiler thinks otherwise, it is dumb.*/
michael@0 1582 oc_state_frag_recon(&_dec->state,fragi,_pli,
michael@0 1583 _pipe->dct_coeffs,last_zzi,dc_quant[qti]);
michael@0 1584 }
michael@0 1585 _pipe->coded_fragis[_pli]+=ncoded_fragis;
michael@0 1586 /*Right now the reconstructed MCU has only the coded blocks in it.*/
michael@0 1587 /*TODO: We make the decision here to always copy the uncoded blocks into it
michael@0 1588 from the reference frame.
michael@0 1589 We could also copy the coded blocks back over the reference frame, if we
michael@0 1590 wait for an additional MCU to be decoded, which might be faster if only a
michael@0 1591 small number of blocks are coded.
michael@0 1592 However, this introduces more latency, creating a larger cache footprint.
michael@0 1593 It's unknown which decision is better, but this one results in simpler
michael@0 1594 code, and the hard case (high bitrate, high resolution) is handled
michael@0 1595 correctly.*/
michael@0 1596 /*Copy the uncoded blocks from the previous reference frame.*/
michael@0 1597 if(_pipe->nuncoded_fragis[_pli]>0){
michael@0 1598 _pipe->uncoded_fragis[_pli]-=_pipe->nuncoded_fragis[_pli];
michael@0 1599 oc_frag_copy_list(&_dec->state,
michael@0 1600 _dec->state.ref_frame_data[OC_FRAME_SELF],
michael@0 1601 _dec->state.ref_frame_data[OC_FRAME_PREV],
michael@0 1602 _dec->state.ref_ystride[_pli],_pipe->uncoded_fragis[_pli],
michael@0 1603 _pipe->nuncoded_fragis[_pli],_dec->state.frag_buf_offs);
michael@0 1604 }
michael@0 1605 }
michael@0 1606
michael@0 1607 /*Filter a horizontal block edge.*/
michael@0 1608 static void oc_filter_hedge(unsigned char *_dst,int _dst_ystride,
michael@0 1609 const unsigned char *_src,int _src_ystride,int _qstep,int _flimit,
michael@0 1610 int *_variance0,int *_variance1){
michael@0 1611 unsigned char *rdst;
michael@0 1612 const unsigned char *rsrc;
michael@0 1613 unsigned char *cdst;
michael@0 1614 const unsigned char *csrc;
michael@0 1615 int r[10];
michael@0 1616 int sum0;
michael@0 1617 int sum1;
michael@0 1618 int bx;
michael@0 1619 int by;
michael@0 1620 rdst=_dst;
michael@0 1621 rsrc=_src;
michael@0 1622 for(bx=0;bx<8;bx++){
michael@0 1623 cdst=rdst;
michael@0 1624 csrc=rsrc;
michael@0 1625 for(by=0;by<10;by++){
michael@0 1626 r[by]=*csrc;
michael@0 1627 csrc+=_src_ystride;
michael@0 1628 }
michael@0 1629 sum0=sum1=0;
michael@0 1630 for(by=0;by<4;by++){
michael@0 1631 sum0+=abs(r[by+1]-r[by]);
michael@0 1632 sum1+=abs(r[by+5]-r[by+6]);
michael@0 1633 }
michael@0 1634 *_variance0+=OC_MINI(255,sum0);
michael@0 1635 *_variance1+=OC_MINI(255,sum1);
michael@0 1636 if(sum0<_flimit&&sum1<_flimit&&r[5]-r[4]<_qstep&&r[4]-r[5]<_qstep){
michael@0 1637 *cdst=(unsigned char)(r[0]*3+r[1]*2+r[2]+r[3]+r[4]+4>>3);
michael@0 1638 cdst+=_dst_ystride;
michael@0 1639 *cdst=(unsigned char)(r[0]*2+r[1]+r[2]*2+r[3]+r[4]+r[5]+4>>3);
michael@0 1640 cdst+=_dst_ystride;
michael@0 1641 for(by=0;by<4;by++){
michael@0 1642 *cdst=(unsigned char)(r[by]+r[by+1]+r[by+2]+r[by+3]*2+
michael@0 1643 r[by+4]+r[by+5]+r[by+6]+4>>3);
michael@0 1644 cdst+=_dst_ystride;
michael@0 1645 }
michael@0 1646 *cdst=(unsigned char)(r[4]+r[5]+r[6]+r[7]*2+r[8]+r[9]*2+4>>3);
michael@0 1647 cdst+=_dst_ystride;
michael@0 1648 *cdst=(unsigned char)(r[5]+r[6]+r[7]+r[8]*2+r[9]*3+4>>3);
michael@0 1649 }
michael@0 1650 else{
michael@0 1651 for(by=1;by<=8;by++){
michael@0 1652 *cdst=(unsigned char)r[by];
michael@0 1653 cdst+=_dst_ystride;
michael@0 1654 }
michael@0 1655 }
michael@0 1656 rdst++;
michael@0 1657 rsrc++;
michael@0 1658 }
michael@0 1659 }
michael@0 1660
michael@0 1661 /*Filter a vertical block edge.*/
michael@0 1662 static void oc_filter_vedge(unsigned char *_dst,int _dst_ystride,
michael@0 1663 int _qstep,int _flimit,int *_variances){
michael@0 1664 unsigned char *rdst;
michael@0 1665 const unsigned char *rsrc;
michael@0 1666 unsigned char *cdst;
michael@0 1667 int r[10];
michael@0 1668 int sum0;
michael@0 1669 int sum1;
michael@0 1670 int bx;
michael@0 1671 int by;
michael@0 1672 cdst=_dst;
michael@0 1673 for(by=0;by<8;by++){
michael@0 1674 rsrc=cdst-1;
michael@0 1675 rdst=cdst;
michael@0 1676 for(bx=0;bx<10;bx++)r[bx]=*rsrc++;
michael@0 1677 sum0=sum1=0;
michael@0 1678 for(bx=0;bx<4;bx++){
michael@0 1679 sum0+=abs(r[bx+1]-r[bx]);
michael@0 1680 sum1+=abs(r[bx+5]-r[bx+6]);
michael@0 1681 }
michael@0 1682 _variances[0]+=OC_MINI(255,sum0);
michael@0 1683 _variances[1]+=OC_MINI(255,sum1);
michael@0 1684 if(sum0<_flimit&&sum1<_flimit&&r[5]-r[4]<_qstep&&r[4]-r[5]<_qstep){
michael@0 1685 *rdst++=(unsigned char)(r[0]*3+r[1]*2+r[2]+r[3]+r[4]+4>>3);
michael@0 1686 *rdst++=(unsigned char)(r[0]*2+r[1]+r[2]*2+r[3]+r[4]+r[5]+4>>3);
michael@0 1687 for(bx=0;bx<4;bx++){
michael@0 1688 *rdst++=(unsigned char)(r[bx]+r[bx+1]+r[bx+2]+r[bx+3]*2+
michael@0 1689 r[bx+4]+r[bx+5]+r[bx+6]+4>>3);
michael@0 1690 }
michael@0 1691 *rdst++=(unsigned char)(r[4]+r[5]+r[6]+r[7]*2+r[8]+r[9]*2+4>>3);
michael@0 1692 *rdst=(unsigned char)(r[5]+r[6]+r[7]+r[8]*2+r[9]*3+4>>3);
michael@0 1693 }
michael@0 1694 cdst+=_dst_ystride;
michael@0 1695 }
michael@0 1696 }
michael@0 1697
michael@0 1698 static void oc_dec_deblock_frag_rows(oc_dec_ctx *_dec,
michael@0 1699 th_img_plane *_dst,th_img_plane *_src,int _pli,int _fragy0,
michael@0 1700 int _fragy_end){
michael@0 1701 oc_fragment_plane *fplane;
michael@0 1702 int *variance;
michael@0 1703 unsigned char *dc_qi;
michael@0 1704 unsigned char *dst;
michael@0 1705 const unsigned char *src;
michael@0 1706 ptrdiff_t froffset;
michael@0 1707 int dst_ystride;
michael@0 1708 int src_ystride;
michael@0 1709 int nhfrags;
michael@0 1710 int width;
michael@0 1711 int notstart;
michael@0 1712 int notdone;
michael@0 1713 int flimit;
michael@0 1714 int qstep;
michael@0 1715 int y_end;
michael@0 1716 int y;
michael@0 1717 int x;
michael@0 1718 _dst+=_pli;
michael@0 1719 _src+=_pli;
michael@0 1720 fplane=_dec->state.fplanes+_pli;
michael@0 1721 nhfrags=fplane->nhfrags;
michael@0 1722 froffset=fplane->froffset+_fragy0*(ptrdiff_t)nhfrags;
michael@0 1723 variance=_dec->variances+froffset;
michael@0 1724 dc_qi=_dec->dc_qis+froffset;
michael@0 1725 notstart=_fragy0>0;
michael@0 1726 notdone=_fragy_end<fplane->nvfrags;
michael@0 1727 /*We want to clear an extra row of variances, except at the end.*/
michael@0 1728 memset(variance+(nhfrags&-notstart),0,
michael@0 1729 (_fragy_end+notdone-_fragy0-notstart)*(nhfrags*sizeof(variance[0])));
michael@0 1730 /*Except for the first time, we want to point to the middle of the row.*/
michael@0 1731 y=(_fragy0<<3)+(notstart<<2);
michael@0 1732 dst_ystride=_dst->stride;
michael@0 1733 src_ystride=_src->stride;
michael@0 1734 dst=_dst->data+y*(ptrdiff_t)dst_ystride;
michael@0 1735 src=_src->data+y*(ptrdiff_t)src_ystride;
michael@0 1736 width=_dst->width;
michael@0 1737 for(;y<4;y++){
michael@0 1738 memcpy(dst,src,width*sizeof(dst[0]));
michael@0 1739 dst+=dst_ystride;
michael@0 1740 src+=src_ystride;
michael@0 1741 }
michael@0 1742 /*We also want to skip the last row in the frame for this loop.*/
michael@0 1743 y_end=_fragy_end-!notdone<<3;
michael@0 1744 for(;y<y_end;y+=8){
michael@0 1745 qstep=_dec->pp_dc_scale[*dc_qi];
michael@0 1746 flimit=(qstep*3)>>2;
michael@0 1747 oc_filter_hedge(dst,dst_ystride,src-src_ystride,src_ystride,
michael@0 1748 qstep,flimit,variance,variance+nhfrags);
michael@0 1749 variance++;
michael@0 1750 dc_qi++;
michael@0 1751 for(x=8;x<width;x+=8){
michael@0 1752 qstep=_dec->pp_dc_scale[*dc_qi];
michael@0 1753 flimit=(qstep*3)>>2;
michael@0 1754 oc_filter_hedge(dst+x,dst_ystride,src+x-src_ystride,src_ystride,
michael@0 1755 qstep,flimit,variance,variance+nhfrags);
michael@0 1756 oc_filter_vedge(dst+x-(dst_ystride<<2)-4,dst_ystride,
michael@0 1757 qstep,flimit,variance-1);
michael@0 1758 variance++;
michael@0 1759 dc_qi++;
michael@0 1760 }
michael@0 1761 dst+=dst_ystride<<3;
michael@0 1762 src+=src_ystride<<3;
michael@0 1763 }
michael@0 1764 /*And finally, handle the last row in the frame, if it's in the range.*/
michael@0 1765 if(!notdone){
michael@0 1766 int height;
michael@0 1767 height=_dst->height;
michael@0 1768 for(;y<height;y++){
michael@0 1769 memcpy(dst,src,width*sizeof(dst[0]));
michael@0 1770 dst+=dst_ystride;
michael@0 1771 src+=src_ystride;
michael@0 1772 }
michael@0 1773 /*Filter the last row of vertical block edges.*/
michael@0 1774 dc_qi++;
michael@0 1775 for(x=8;x<width;x+=8){
michael@0 1776 qstep=_dec->pp_dc_scale[*dc_qi++];
michael@0 1777 flimit=(qstep*3)>>2;
michael@0 1778 oc_filter_vedge(dst+x-(dst_ystride<<3)-4,dst_ystride,
michael@0 1779 qstep,flimit,variance++);
michael@0 1780 }
michael@0 1781 }
michael@0 1782 }
michael@0 1783
michael@0 1784 static void oc_dering_block(unsigned char *_idata,int _ystride,int _b,
michael@0 1785 int _dc_scale,int _sharp_mod,int _strong){
michael@0 1786 static const unsigned char OC_MOD_MAX[2]={24,32};
michael@0 1787 static const unsigned char OC_MOD_SHIFT[2]={1,0};
michael@0 1788 const unsigned char *psrc;
michael@0 1789 const unsigned char *src;
michael@0 1790 const unsigned char *nsrc;
michael@0 1791 unsigned char *dst;
michael@0 1792 int vmod[72];
michael@0 1793 int hmod[72];
michael@0 1794 int mod_hi;
michael@0 1795 int by;
michael@0 1796 int bx;
michael@0 1797 mod_hi=OC_MINI(3*_dc_scale,OC_MOD_MAX[_strong]);
michael@0 1798 dst=_idata;
michael@0 1799 src=dst;
michael@0 1800 psrc=src-(_ystride&-!(_b&4));
michael@0 1801 for(by=0;by<9;by++){
michael@0 1802 for(bx=0;bx<8;bx++){
michael@0 1803 int mod;
michael@0 1804 mod=32+_dc_scale-(abs(src[bx]-psrc[bx])<<OC_MOD_SHIFT[_strong]);
michael@0 1805 vmod[(by<<3)+bx]=mod<-64?_sharp_mod:OC_CLAMPI(0,mod,mod_hi);
michael@0 1806 }
michael@0 1807 psrc=src;
michael@0 1808 src+=_ystride&-(!(_b&8)|by<7);
michael@0 1809 }
michael@0 1810 nsrc=dst;
michael@0 1811 psrc=dst-!(_b&1);
michael@0 1812 for(bx=0;bx<9;bx++){
michael@0 1813 src=nsrc;
michael@0 1814 for(by=0;by<8;by++){
michael@0 1815 int mod;
michael@0 1816 mod=32+_dc_scale-(abs(*src-*psrc)<<OC_MOD_SHIFT[_strong]);
michael@0 1817 hmod[(bx<<3)+by]=mod<-64?_sharp_mod:OC_CLAMPI(0,mod,mod_hi);
michael@0 1818 psrc+=_ystride;
michael@0 1819 src+=_ystride;
michael@0 1820 }
michael@0 1821 psrc=nsrc;
michael@0 1822 nsrc+=!(_b&2)|bx<7;
michael@0 1823 }
michael@0 1824 src=dst;
michael@0 1825 psrc=src-(_ystride&-!(_b&4));
michael@0 1826 nsrc=src+_ystride;
michael@0 1827 for(by=0;by<8;by++){
michael@0 1828 int a;
michael@0 1829 int b;
michael@0 1830 int w;
michael@0 1831 a=128;
michael@0 1832 b=64;
michael@0 1833 w=hmod[by];
michael@0 1834 a-=w;
michael@0 1835 b+=w**(src-!(_b&1));
michael@0 1836 w=vmod[by<<3];
michael@0 1837 a-=w;
michael@0 1838 b+=w*psrc[0];
michael@0 1839 w=vmod[by+1<<3];
michael@0 1840 a-=w;
michael@0 1841 b+=w*nsrc[0];
michael@0 1842 w=hmod[(1<<3)+by];
michael@0 1843 a-=w;
michael@0 1844 b+=w*src[1];
michael@0 1845 dst[0]=OC_CLAMP255(a*src[0]+b>>7);
michael@0 1846 for(bx=1;bx<7;bx++){
michael@0 1847 a=128;
michael@0 1848 b=64;
michael@0 1849 w=hmod[(bx<<3)+by];
michael@0 1850 a-=w;
michael@0 1851 b+=w*src[bx-1];
michael@0 1852 w=vmod[(by<<3)+bx];
michael@0 1853 a-=w;
michael@0 1854 b+=w*psrc[bx];
michael@0 1855 w=vmod[(by+1<<3)+bx];
michael@0 1856 a-=w;
michael@0 1857 b+=w*nsrc[bx];
michael@0 1858 w=hmod[(bx+1<<3)+by];
michael@0 1859 a-=w;
michael@0 1860 b+=w*src[bx+1];
michael@0 1861 dst[bx]=OC_CLAMP255(a*src[bx]+b>>7);
michael@0 1862 }
michael@0 1863 a=128;
michael@0 1864 b=64;
michael@0 1865 w=hmod[(7<<3)+by];
michael@0 1866 a-=w;
michael@0 1867 b+=w*src[6];
michael@0 1868 w=vmod[(by<<3)+7];
michael@0 1869 a-=w;
michael@0 1870 b+=w*psrc[7];
michael@0 1871 w=vmod[(by+1<<3)+7];
michael@0 1872 a-=w;
michael@0 1873 b+=w*nsrc[7];
michael@0 1874 w=hmod[(8<<3)+by];
michael@0 1875 a-=w;
michael@0 1876 b+=w*src[7+!(_b&2)];
michael@0 1877 dst[7]=OC_CLAMP255(a*src[7]+b>>7);
michael@0 1878 dst+=_ystride;
michael@0 1879 psrc=src;
michael@0 1880 src=nsrc;
michael@0 1881 nsrc+=_ystride&-(!(_b&8)|by<6);
michael@0 1882 }
michael@0 1883 }
michael@0 1884
michael@0 1885 #define OC_DERING_THRESH1 (384)
michael@0 1886 #define OC_DERING_THRESH2 (4*OC_DERING_THRESH1)
michael@0 1887 #define OC_DERING_THRESH3 (5*OC_DERING_THRESH1)
michael@0 1888 #define OC_DERING_THRESH4 (10*OC_DERING_THRESH1)
michael@0 1889
michael@0 1890 static void oc_dec_dering_frag_rows(oc_dec_ctx *_dec,th_img_plane *_img,
michael@0 1891 int _pli,int _fragy0,int _fragy_end){
michael@0 1892 th_img_plane *iplane;
michael@0 1893 oc_fragment_plane *fplane;
michael@0 1894 oc_fragment *frag;
michael@0 1895 int *variance;
michael@0 1896 unsigned char *idata;
michael@0 1897 ptrdiff_t froffset;
michael@0 1898 int ystride;
michael@0 1899 int nhfrags;
michael@0 1900 int sthresh;
michael@0 1901 int strong;
michael@0 1902 int y_end;
michael@0 1903 int width;
michael@0 1904 int height;
michael@0 1905 int y;
michael@0 1906 int x;
michael@0 1907 iplane=_img+_pli;
michael@0 1908 fplane=_dec->state.fplanes+_pli;
michael@0 1909 nhfrags=fplane->nhfrags;
michael@0 1910 froffset=fplane->froffset+_fragy0*(ptrdiff_t)nhfrags;
michael@0 1911 variance=_dec->variances+froffset;
michael@0 1912 frag=_dec->state.frags+froffset;
michael@0 1913 strong=_dec->pp_level>=(_pli?OC_PP_LEVEL_SDERINGC:OC_PP_LEVEL_SDERINGY);
michael@0 1914 sthresh=_pli?OC_DERING_THRESH4:OC_DERING_THRESH3;
michael@0 1915 y=_fragy0<<3;
michael@0 1916 ystride=iplane->stride;
michael@0 1917 idata=iplane->data+y*(ptrdiff_t)ystride;
michael@0 1918 y_end=_fragy_end<<3;
michael@0 1919 width=iplane->width;
michael@0 1920 height=iplane->height;
michael@0 1921 for(;y<y_end;y+=8){
michael@0 1922 for(x=0;x<width;x+=8){
michael@0 1923 int b;
michael@0 1924 int qi;
michael@0 1925 int var;
michael@0 1926 qi=_dec->state.qis[frag->qii];
michael@0 1927 var=*variance;
michael@0 1928 b=(x<=0)|(x+8>=width)<<1|(y<=0)<<2|(y+8>=height)<<3;
michael@0 1929 if(strong&&var>sthresh){
michael@0 1930 oc_dering_block(idata+x,ystride,b,
michael@0 1931 _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],1);
michael@0 1932 if(_pli||!(b&1)&&*(variance-1)>OC_DERING_THRESH4||
michael@0 1933 !(b&2)&&variance[1]>OC_DERING_THRESH4||
michael@0 1934 !(b&4)&&*(variance-nhfrags)>OC_DERING_THRESH4||
michael@0 1935 !(b&8)&&variance[nhfrags]>OC_DERING_THRESH4){
michael@0 1936 oc_dering_block(idata+x,ystride,b,
michael@0 1937 _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],1);
michael@0 1938 oc_dering_block(idata+x,ystride,b,
michael@0 1939 _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],1);
michael@0 1940 }
michael@0 1941 }
michael@0 1942 else if(var>OC_DERING_THRESH2){
michael@0 1943 oc_dering_block(idata+x,ystride,b,
michael@0 1944 _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],1);
michael@0 1945 }
michael@0 1946 else if(var>OC_DERING_THRESH1){
michael@0 1947 oc_dering_block(idata+x,ystride,b,
michael@0 1948 _dec->pp_dc_scale[qi],_dec->pp_sharp_mod[qi],0);
michael@0 1949 }
michael@0 1950 frag++;
michael@0 1951 variance++;
michael@0 1952 }
michael@0 1953 idata+=ystride<<3;
michael@0 1954 }
michael@0 1955 }
michael@0 1956
michael@0 1957
michael@0 1958
michael@0 1959 th_dec_ctx *th_decode_alloc(const th_info *_info,const th_setup_info *_setup){
michael@0 1960 oc_dec_ctx *dec;
michael@0 1961 if(_info==NULL||_setup==NULL)return NULL;
michael@0 1962 dec=oc_aligned_malloc(sizeof(*dec),16);
michael@0 1963 if(dec==NULL||oc_dec_init(dec,_info,_setup)<0){
michael@0 1964 oc_aligned_free(dec);
michael@0 1965 return NULL;
michael@0 1966 }
michael@0 1967 dec->state.curframe_num=0;
michael@0 1968 return dec;
michael@0 1969 }
michael@0 1970
michael@0 1971 void th_decode_free(th_dec_ctx *_dec){
michael@0 1972 if(_dec!=NULL){
michael@0 1973 oc_dec_clear(_dec);
michael@0 1974 oc_aligned_free(_dec);
michael@0 1975 }
michael@0 1976 }
michael@0 1977
michael@0 1978 int th_decode_ctl(th_dec_ctx *_dec,int _req,void *_buf,
michael@0 1979 size_t _buf_sz){
michael@0 1980 switch(_req){
michael@0 1981 case TH_DECCTL_GET_PPLEVEL_MAX:{
michael@0 1982 if(_dec==NULL||_buf==NULL)return TH_EFAULT;
michael@0 1983 if(_buf_sz!=sizeof(int))return TH_EINVAL;
michael@0 1984 (*(int *)_buf)=OC_PP_LEVEL_MAX;
michael@0 1985 return 0;
michael@0 1986 }break;
michael@0 1987 case TH_DECCTL_SET_PPLEVEL:{
michael@0 1988 int pp_level;
michael@0 1989 if(_dec==NULL||_buf==NULL)return TH_EFAULT;
michael@0 1990 if(_buf_sz!=sizeof(int))return TH_EINVAL;
michael@0 1991 pp_level=*(int *)_buf;
michael@0 1992 if(pp_level<0||pp_level>OC_PP_LEVEL_MAX)return TH_EINVAL;
michael@0 1993 _dec->pp_level=pp_level;
michael@0 1994 return 0;
michael@0 1995 }break;
michael@0 1996 case TH_DECCTL_SET_GRANPOS:{
michael@0 1997 ogg_int64_t granpos;
michael@0 1998 if(_dec==NULL||_buf==NULL)return TH_EFAULT;
michael@0 1999 if(_buf_sz!=sizeof(ogg_int64_t))return TH_EINVAL;
michael@0 2000 granpos=*(ogg_int64_t *)_buf;
michael@0 2001 if(granpos<0)return TH_EINVAL;
michael@0 2002 _dec->state.granpos=granpos;
michael@0 2003 _dec->state.keyframe_num=(granpos>>_dec->state.info.keyframe_granule_shift)
michael@0 2004 -_dec->state.granpos_bias;
michael@0 2005 _dec->state.curframe_num=_dec->state.keyframe_num
michael@0 2006 +(granpos&(1<<_dec->state.info.keyframe_granule_shift)-1);
michael@0 2007 return 0;
michael@0 2008 }break;
michael@0 2009 case TH_DECCTL_SET_STRIPE_CB:{
michael@0 2010 th_stripe_callback *cb;
michael@0 2011 if(_dec==NULL||_buf==NULL)return TH_EFAULT;
michael@0 2012 if(_buf_sz!=sizeof(th_stripe_callback))return TH_EINVAL;
michael@0 2013 cb=(th_stripe_callback *)_buf;
michael@0 2014 _dec->stripe_cb.ctx=cb->ctx;
michael@0 2015 _dec->stripe_cb.stripe_decoded=cb->stripe_decoded;
michael@0 2016 return 0;
michael@0 2017 }break;
michael@0 2018 #ifdef HAVE_CAIRO
michael@0 2019 case TH_DECCTL_SET_TELEMETRY_MBMODE:{
michael@0 2020 if(_dec==NULL||_buf==NULL)return TH_EFAULT;
michael@0 2021 if(_buf_sz!=sizeof(int))return TH_EINVAL;
michael@0 2022 _dec->telemetry=1;
michael@0 2023 _dec->telemetry_mbmode=*(int *)_buf;
michael@0 2024 return 0;
michael@0 2025 }break;
michael@0 2026 case TH_DECCTL_SET_TELEMETRY_MV:{
michael@0 2027 if(_dec==NULL||_buf==NULL)return TH_EFAULT;
michael@0 2028 if(_buf_sz!=sizeof(int))return TH_EINVAL;
michael@0 2029 _dec->telemetry=1;
michael@0 2030 _dec->telemetry_mv=*(int *)_buf;
michael@0 2031 return 0;
michael@0 2032 }break;
michael@0 2033 case TH_DECCTL_SET_TELEMETRY_QI:{
michael@0 2034 if(_dec==NULL||_buf==NULL)return TH_EFAULT;
michael@0 2035 if(_buf_sz!=sizeof(int))return TH_EINVAL;
michael@0 2036 _dec->telemetry=1;
michael@0 2037 _dec->telemetry_qi=*(int *)_buf;
michael@0 2038 return 0;
michael@0 2039 }break;
michael@0 2040 case TH_DECCTL_SET_TELEMETRY_BITS:{
michael@0 2041 if(_dec==NULL||_buf==NULL)return TH_EFAULT;
michael@0 2042 if(_buf_sz!=sizeof(int))return TH_EINVAL;
michael@0 2043 _dec->telemetry=1;
michael@0 2044 _dec->telemetry_bits=*(int *)_buf;
michael@0 2045 return 0;
michael@0 2046 }break;
michael@0 2047 #endif
michael@0 2048 default:return TH_EIMPL;
michael@0 2049 }
michael@0 2050 }
michael@0 2051
michael@0 2052 /*We're decoding an INTER frame, but have no initialized reference
michael@0 2053 buffers (i.e., decoding did not start on a key frame).
michael@0 2054 We initialize them to a solid gray here.*/
michael@0 2055 static void oc_dec_init_dummy_frame(th_dec_ctx *_dec){
michael@0 2056 th_info *info;
michael@0 2057 size_t yplane_sz;
michael@0 2058 size_t cplane_sz;
michael@0 2059 ptrdiff_t yoffset;
michael@0 2060 int yhstride;
michael@0 2061 int yheight;
michael@0 2062 int chstride;
michael@0 2063 int cheight;
michael@0 2064 _dec->state.ref_frame_idx[OC_FRAME_GOLD]=0;
michael@0 2065 _dec->state.ref_frame_idx[OC_FRAME_PREV]=0;
michael@0 2066 _dec->state.ref_frame_idx[OC_FRAME_SELF]=0;
michael@0 2067 _dec->state.ref_frame_data[OC_FRAME_GOLD]=
michael@0 2068 _dec->state.ref_frame_data[OC_FRAME_PREV]=
michael@0 2069 _dec->state.ref_frame_data[OC_FRAME_SELF]=
michael@0 2070 _dec->state.ref_frame_bufs[0][0].data;
michael@0 2071 memcpy(_dec->pp_frame_buf,_dec->state.ref_frame_bufs[0],
michael@0 2072 sizeof(_dec->pp_frame_buf[0])*3);
michael@0 2073 info=&_dec->state.info;
michael@0 2074 yhstride=abs(_dec->state.ref_ystride[0]);
michael@0 2075 yheight=info->frame_height+2*OC_UMV_PADDING;
michael@0 2076 chstride=abs(_dec->state.ref_ystride[1]);
michael@0 2077 cheight=yheight>>!(info->pixel_fmt&2);
michael@0 2078 yplane_sz=yhstride*(size_t)yheight+16;
michael@0 2079 cplane_sz=chstride*(size_t)cheight;
michael@0 2080 yoffset=yhstride*(ptrdiff_t)(yheight-OC_UMV_PADDING-1)+OC_UMV_PADDING;
michael@0 2081 memset(_dec->state.ref_frame_data[0]-yoffset,0x80,yplane_sz+2*cplane_sz);
michael@0 2082 }
michael@0 2083
michael@0 2084 int th_decode_packetin(th_dec_ctx *_dec,const ogg_packet *_op,
michael@0 2085 ogg_int64_t *_granpos){
michael@0 2086 int ret;
michael@0 2087 if(_dec==NULL||_op==NULL)return TH_EFAULT;
michael@0 2088 /*A completely empty packet indicates a dropped frame and is treated exactly
michael@0 2089 like an inter frame with no coded blocks.*/
michael@0 2090 if(_op->bytes==0){
michael@0 2091 _dec->state.frame_type=OC_INTER_FRAME;
michael@0 2092 _dec->state.ntotal_coded_fragis=0;
michael@0 2093 }
michael@0 2094 else{
michael@0 2095 oc_pack_readinit(&_dec->opb,_op->packet,_op->bytes);
michael@0 2096 ret=oc_dec_frame_header_unpack(_dec);
michael@0 2097 if(ret<0)return ret;
michael@0 2098 if(_dec->state.frame_type==OC_INTRA_FRAME)oc_dec_mark_all_intra(_dec);
michael@0 2099 else oc_dec_coded_flags_unpack(_dec);
michael@0 2100 }
michael@0 2101 /*If there have been no reference frames, and we need one, initialize one.*/
michael@0 2102 if(_dec->state.frame_type!=OC_INTRA_FRAME&&
michael@0 2103 (_dec->state.ref_frame_idx[OC_FRAME_GOLD]<0||
michael@0 2104 _dec->state.ref_frame_idx[OC_FRAME_PREV]<0)){
michael@0 2105 oc_dec_init_dummy_frame(_dec);
michael@0 2106 }
michael@0 2107 /*If this was an inter frame with no coded blocks...*/
michael@0 2108 if(_dec->state.ntotal_coded_fragis<=0){
michael@0 2109 /*Just update the granule position and return.*/
michael@0 2110 _dec->state.granpos=(_dec->state.keyframe_num+_dec->state.granpos_bias<<
michael@0 2111 _dec->state.info.keyframe_granule_shift)
michael@0 2112 +(_dec->state.curframe_num-_dec->state.keyframe_num);
michael@0 2113 _dec->state.curframe_num++;
michael@0 2114 if(_granpos!=NULL)*_granpos=_dec->state.granpos;
michael@0 2115 return TH_DUPFRAME;
michael@0 2116 }
michael@0 2117 else{
michael@0 2118 th_ycbcr_buffer stripe_buf;
michael@0 2119 int stripe_fragy;
michael@0 2120 int refi;
michael@0 2121 int pli;
michael@0 2122 int notstart;
michael@0 2123 int notdone;
michael@0 2124 /*Select a free buffer to use for the reconstructed version of this frame.*/
michael@0 2125 for(refi=0;refi==_dec->state.ref_frame_idx[OC_FRAME_GOLD]||
michael@0 2126 refi==_dec->state.ref_frame_idx[OC_FRAME_PREV];refi++);
michael@0 2127 _dec->state.ref_frame_idx[OC_FRAME_SELF]=refi;
michael@0 2128 _dec->state.ref_frame_data[OC_FRAME_SELF]=
michael@0 2129 _dec->state.ref_frame_bufs[refi][0].data;
michael@0 2130 #if defined(HAVE_CAIRO)
michael@0 2131 _dec->telemetry_frame_bytes=_op->bytes;
michael@0 2132 #endif
michael@0 2133 if(_dec->state.frame_type==OC_INTRA_FRAME){
michael@0 2134 _dec->state.keyframe_num=_dec->state.curframe_num;
michael@0 2135 #if defined(HAVE_CAIRO)
michael@0 2136 _dec->telemetry_coding_bytes=
michael@0 2137 _dec->telemetry_mode_bytes=
michael@0 2138 _dec->telemetry_mv_bytes=oc_pack_bytes_left(&_dec->opb);
michael@0 2139 #endif
michael@0 2140 }
michael@0 2141 else{
michael@0 2142 #if defined(HAVE_CAIRO)
michael@0 2143 _dec->telemetry_coding_bytes=oc_pack_bytes_left(&_dec->opb);
michael@0 2144 #endif
michael@0 2145 oc_dec_mb_modes_unpack(_dec);
michael@0 2146 #if defined(HAVE_CAIRO)
michael@0 2147 _dec->telemetry_mode_bytes=oc_pack_bytes_left(&_dec->opb);
michael@0 2148 #endif
michael@0 2149 oc_dec_mv_unpack_and_frag_modes_fill(_dec);
michael@0 2150 #if defined(HAVE_CAIRO)
michael@0 2151 _dec->telemetry_mv_bytes=oc_pack_bytes_left(&_dec->opb);
michael@0 2152 #endif
michael@0 2153 }
michael@0 2154 oc_dec_block_qis_unpack(_dec);
michael@0 2155 #if defined(HAVE_CAIRO)
michael@0 2156 _dec->telemetry_qi_bytes=oc_pack_bytes_left(&_dec->opb);
michael@0 2157 #endif
michael@0 2158 oc_dec_residual_tokens_unpack(_dec);
michael@0 2159 /*Update granule position.
michael@0 2160 This must be done before the striped decode callbacks so that the
michael@0 2161 application knows what to do with the frame data.*/
michael@0 2162 _dec->state.granpos=(_dec->state.keyframe_num+_dec->state.granpos_bias<<
michael@0 2163 _dec->state.info.keyframe_granule_shift)
michael@0 2164 +(_dec->state.curframe_num-_dec->state.keyframe_num);
michael@0 2165 _dec->state.curframe_num++;
michael@0 2166 if(_granpos!=NULL)*_granpos=_dec->state.granpos;
michael@0 2167 /*All of the rest of the operations -- DC prediction reversal,
michael@0 2168 reconstructing coded fragments, copying uncoded fragments, loop
michael@0 2169 filtering, extending borders, and out-of-loop post-processing -- should
michael@0 2170 be pipelined.
michael@0 2171 I.e., DC prediction reversal, reconstruction, and uncoded fragment
michael@0 2172 copying are done for one or two super block rows, then loop filtering is
michael@0 2173 run as far as it can, then bordering copying, then post-processing.
michael@0 2174 For 4:2:0 video a Minimum Codable Unit or MCU contains two luma super
michael@0 2175 block rows, and one chroma.
michael@0 2176 Otherwise, an MCU consists of one super block row from each plane.
michael@0 2177 Inside each MCU, we perform all of the steps on one color plane before
michael@0 2178 moving on to the next.
michael@0 2179 After reconstruction, the additional filtering stages introduce a delay
michael@0 2180 since they need some pixels from the next fragment row.
michael@0 2181 Thus the actual number of decoded rows available is slightly smaller for
michael@0 2182 the first MCU, and slightly larger for the last.
michael@0 2183
michael@0 2184 This entire process allows us to operate on the data while it is still in
michael@0 2185 cache, resulting in big performance improvements.
michael@0 2186 An application callback allows further application processing (blitting
michael@0 2187 to video memory, color conversion, etc.) to also use the data while it's
michael@0 2188 in cache.*/
michael@0 2189 oc_dec_pipeline_init(_dec,&_dec->pipe);
michael@0 2190 oc_ycbcr_buffer_flip(stripe_buf,_dec->pp_frame_buf);
michael@0 2191 notstart=0;
michael@0 2192 notdone=1;
michael@0 2193 for(stripe_fragy=0;notdone;stripe_fragy+=_dec->pipe.mcu_nvfrags){
michael@0 2194 int avail_fragy0;
michael@0 2195 int avail_fragy_end;
michael@0 2196 avail_fragy0=avail_fragy_end=_dec->state.fplanes[0].nvfrags;
michael@0 2197 notdone=stripe_fragy+_dec->pipe.mcu_nvfrags<avail_fragy_end;
michael@0 2198 for(pli=0;pli<3;pli++){
michael@0 2199 oc_fragment_plane *fplane;
michael@0 2200 int frag_shift;
michael@0 2201 int pp_offset;
michael@0 2202 int sdelay;
michael@0 2203 int edelay;
michael@0 2204 fplane=_dec->state.fplanes+pli;
michael@0 2205 /*Compute the first and last fragment row of the current MCU for this
michael@0 2206 plane.*/
michael@0 2207 frag_shift=pli!=0&&!(_dec->state.info.pixel_fmt&2);
michael@0 2208 _dec->pipe.fragy0[pli]=stripe_fragy>>frag_shift;
michael@0 2209 _dec->pipe.fragy_end[pli]=OC_MINI(fplane->nvfrags,
michael@0 2210 _dec->pipe.fragy0[pli]+(_dec->pipe.mcu_nvfrags>>frag_shift));
michael@0 2211 oc_dec_dc_unpredict_mcu_plane(_dec,&_dec->pipe,pli);
michael@0 2212 oc_dec_frags_recon_mcu_plane(_dec,&_dec->pipe,pli);
michael@0 2213 sdelay=edelay=0;
michael@0 2214 if(_dec->pipe.loop_filter){
michael@0 2215 sdelay+=notstart;
michael@0 2216 edelay+=notdone;
michael@0 2217 oc_state_loop_filter_frag_rows(&_dec->state,
michael@0 2218 _dec->pipe.bounding_values,OC_FRAME_SELF,pli,
michael@0 2219 _dec->pipe.fragy0[pli]-sdelay,_dec->pipe.fragy_end[pli]-edelay);
michael@0 2220 }
michael@0 2221 /*To fill the borders, we have an additional two pixel delay, since a
michael@0 2222 fragment in the next row could filter its top edge, using two pixels
michael@0 2223 from a fragment in this row.
michael@0 2224 But there's no reason to delay a full fragment between the two.*/
michael@0 2225 oc_state_borders_fill_rows(&_dec->state,refi,pli,
michael@0 2226 (_dec->pipe.fragy0[pli]-sdelay<<3)-(sdelay<<1),
michael@0 2227 (_dec->pipe.fragy_end[pli]-edelay<<3)-(edelay<<1));
michael@0 2228 /*Out-of-loop post-processing.*/
michael@0 2229 pp_offset=3*(pli!=0);
michael@0 2230 if(_dec->pipe.pp_level>=OC_PP_LEVEL_DEBLOCKY+pp_offset){
michael@0 2231 /*Perform de-blocking in one plane.*/
michael@0 2232 sdelay+=notstart;
michael@0 2233 edelay+=notdone;
michael@0 2234 oc_dec_deblock_frag_rows(_dec,_dec->pp_frame_buf,
michael@0 2235 _dec->state.ref_frame_bufs[refi],pli,
michael@0 2236 _dec->pipe.fragy0[pli]-sdelay,_dec->pipe.fragy_end[pli]-edelay);
michael@0 2237 if(_dec->pipe.pp_level>=OC_PP_LEVEL_DERINGY+pp_offset){
michael@0 2238 /*Perform de-ringing in one plane.*/
michael@0 2239 sdelay+=notstart;
michael@0 2240 edelay+=notdone;
michael@0 2241 oc_dec_dering_frag_rows(_dec,_dec->pp_frame_buf,pli,
michael@0 2242 _dec->pipe.fragy0[pli]-sdelay,_dec->pipe.fragy_end[pli]-edelay);
michael@0 2243 }
michael@0 2244 }
michael@0 2245 /*If no post-processing is done, we still need to delay a row for the
michael@0 2246 loop filter, thanks to the strange filtering order VP3 chose.*/
michael@0 2247 else if(_dec->pipe.loop_filter){
michael@0 2248 sdelay+=notstart;
michael@0 2249 edelay+=notdone;
michael@0 2250 }
michael@0 2251 /*Compute the intersection of the available rows in all planes.
michael@0 2252 If chroma is sub-sampled, the effect of each of its delays is
michael@0 2253 doubled, but luma might have more post-processing filters enabled
michael@0 2254 than chroma, so we don't know up front which one is the limiting
michael@0 2255 factor.*/
michael@0 2256 avail_fragy0=OC_MINI(avail_fragy0,
michael@0 2257 _dec->pipe.fragy0[pli]-sdelay<<frag_shift);
michael@0 2258 avail_fragy_end=OC_MINI(avail_fragy_end,
michael@0 2259 _dec->pipe.fragy_end[pli]-edelay<<frag_shift);
michael@0 2260 }
michael@0 2261 if(_dec->stripe_cb.stripe_decoded!=NULL){
michael@0 2262 /*The callback might want to use the FPU, so let's make sure they can.
michael@0 2263 We violate all kinds of ABI restrictions by not doing this until
michael@0 2264 now, but none of them actually matter since we don't use floating
michael@0 2265 point ourselves.*/
michael@0 2266 oc_restore_fpu(&_dec->state);
michael@0 2267 /*Make the callback, ensuring we flip the sense of the "start" and
michael@0 2268 "end" of the available region upside down.*/
michael@0 2269 (*_dec->stripe_cb.stripe_decoded)(_dec->stripe_cb.ctx,stripe_buf,
michael@0 2270 _dec->state.fplanes[0].nvfrags-avail_fragy_end,
michael@0 2271 _dec->state.fplanes[0].nvfrags-avail_fragy0);
michael@0 2272 }
michael@0 2273 notstart=1;
michael@0 2274 }
michael@0 2275 /*Finish filling in the reference frame borders.*/
michael@0 2276 for(pli=0;pli<3;pli++)oc_state_borders_fill_caps(&_dec->state,refi,pli);
michael@0 2277 /*Update the reference frame indices.*/
michael@0 2278 if(_dec->state.frame_type==OC_INTRA_FRAME){
michael@0 2279 /*The new frame becomes both the previous and gold reference frames.*/
michael@0 2280 _dec->state.ref_frame_idx[OC_FRAME_GOLD]=
michael@0 2281 _dec->state.ref_frame_idx[OC_FRAME_PREV]=
michael@0 2282 _dec->state.ref_frame_idx[OC_FRAME_SELF];
michael@0 2283 _dec->state.ref_frame_data[OC_FRAME_GOLD]=
michael@0 2284 _dec->state.ref_frame_data[OC_FRAME_PREV]=
michael@0 2285 _dec->state.ref_frame_data[OC_FRAME_SELF];
michael@0 2286 }
michael@0 2287 else{
michael@0 2288 /*Otherwise, just replace the previous reference frame.*/
michael@0 2289 _dec->state.ref_frame_idx[OC_FRAME_PREV]=
michael@0 2290 _dec->state.ref_frame_idx[OC_FRAME_SELF];
michael@0 2291 _dec->state.ref_frame_data[OC_FRAME_PREV]=
michael@0 2292 _dec->state.ref_frame_data[OC_FRAME_SELF];
michael@0 2293 }
michael@0 2294 /*Restore the FPU before dump_frame, since that _does_ use the FPU (for PNG
michael@0 2295 gamma values, if nothing else).*/
michael@0 2296 oc_restore_fpu(&_dec->state);
michael@0 2297 #if defined(OC_DUMP_IMAGES)
michael@0 2298 /*We only dump images if there were some coded blocks.*/
michael@0 2299 oc_state_dump_frame(&_dec->state,OC_FRAME_SELF,"dec");
michael@0 2300 #endif
michael@0 2301 return 0;
michael@0 2302 }
michael@0 2303 }
michael@0 2304
michael@0 2305 int th_decode_ycbcr_out(th_dec_ctx *_dec,th_ycbcr_buffer _ycbcr){
michael@0 2306 if(_dec==NULL||_ycbcr==NULL)return TH_EFAULT;
michael@0 2307 oc_ycbcr_buffer_flip(_ycbcr,_dec->pp_frame_buf);
michael@0 2308 #if defined(HAVE_CAIRO)
michael@0 2309 /*If telemetry ioctls are active, we need to draw to the output buffer.
michael@0 2310 Stuff the plane into cairo.*/
michael@0 2311 if(_dec->telemetry){
michael@0 2312 cairo_surface_t *cs;
michael@0 2313 unsigned char *data;
michael@0 2314 unsigned char *y_row;
michael@0 2315 unsigned char *u_row;
michael@0 2316 unsigned char *v_row;
michael@0 2317 unsigned char *rgb_row;
michael@0 2318 int cstride;
michael@0 2319 int w;
michael@0 2320 int h;
michael@0 2321 int x;
michael@0 2322 int y;
michael@0 2323 int hdec;
michael@0 2324 int vdec;
michael@0 2325 w=_ycbcr[0].width;
michael@0 2326 h=_ycbcr[0].height;
michael@0 2327 hdec=!(_dec->state.info.pixel_fmt&1);
michael@0 2328 vdec=!(_dec->state.info.pixel_fmt&2);
michael@0 2329 /*Lazy data buffer init.
michael@0 2330 We could try to re-use the post-processing buffer, which would save
michael@0 2331 memory, but complicate the allocation logic there.
michael@0 2332 I don't think anyone cares about memory usage when using telemetry; it is
michael@0 2333 not meant for embedded devices.*/
michael@0 2334 if(_dec->telemetry_frame_data==NULL){
michael@0 2335 _dec->telemetry_frame_data=_ogg_malloc(
michael@0 2336 (w*h+2*(w>>hdec)*(h>>vdec))*sizeof(*_dec->telemetry_frame_data));
michael@0 2337 if(_dec->telemetry_frame_data==NULL)return 0;
michael@0 2338 }
michael@0 2339 cs=cairo_image_surface_create(CAIRO_FORMAT_RGB24,w,h);
michael@0 2340 /*Sadly, no YUV support in Cairo (yet); convert into the RGB buffer.*/
michael@0 2341 data=cairo_image_surface_get_data(cs);
michael@0 2342 if(data==NULL){
michael@0 2343 cairo_surface_destroy(cs);
michael@0 2344 return 0;
michael@0 2345 }
michael@0 2346 cstride=cairo_image_surface_get_stride(cs);
michael@0 2347 y_row=_ycbcr[0].data;
michael@0 2348 u_row=_ycbcr[1].data;
michael@0 2349 v_row=_ycbcr[2].data;
michael@0 2350 rgb_row=data;
michael@0 2351 for(y=0;y<h;y++){
michael@0 2352 for(x=0;x<w;x++){
michael@0 2353 int r;
michael@0 2354 int g;
michael@0 2355 int b;
michael@0 2356 r=(1904000*y_row[x]+2609823*v_row[x>>hdec]-363703744)/1635200;
michael@0 2357 g=(3827562*y_row[x]-1287801*u_row[x>>hdec]
michael@0 2358 -2672387*v_row[x>>hdec]+447306710)/3287200;
michael@0 2359 b=(952000*y_row[x]+1649289*u_row[x>>hdec]-225932192)/817600;
michael@0 2360 rgb_row[4*x+0]=OC_CLAMP255(b);
michael@0 2361 rgb_row[4*x+1]=OC_CLAMP255(g);
michael@0 2362 rgb_row[4*x+2]=OC_CLAMP255(r);
michael@0 2363 }
michael@0 2364 y_row+=_ycbcr[0].stride;
michael@0 2365 u_row+=_ycbcr[1].stride&-((y&1)|!vdec);
michael@0 2366 v_row+=_ycbcr[2].stride&-((y&1)|!vdec);
michael@0 2367 rgb_row+=cstride;
michael@0 2368 }
michael@0 2369 /*Draw coded identifier for each macroblock (stored in Hilbert order).*/
michael@0 2370 {
michael@0 2371 cairo_t *c;
michael@0 2372 const oc_fragment *frags;
michael@0 2373 oc_mv *frag_mvs;
michael@0 2374 const signed char *mb_modes;
michael@0 2375 oc_mb_map *mb_maps;
michael@0 2376 size_t nmbs;
michael@0 2377 size_t mbi;
michael@0 2378 int row2;
michael@0 2379 int col2;
michael@0 2380 int qim[3]={0,0,0};
michael@0 2381 if(_dec->state.nqis==2){
michael@0 2382 int bqi;
michael@0 2383 bqi=_dec->state.qis[0];
michael@0 2384 if(_dec->state.qis[1]>bqi)qim[1]=1;
michael@0 2385 if(_dec->state.qis[1]<bqi)qim[1]=-1;
michael@0 2386 }
michael@0 2387 if(_dec->state.nqis==3){
michael@0 2388 int bqi;
michael@0 2389 int cqi;
michael@0 2390 int dqi;
michael@0 2391 bqi=_dec->state.qis[0];
michael@0 2392 cqi=_dec->state.qis[1];
michael@0 2393 dqi=_dec->state.qis[2];
michael@0 2394 if(cqi>bqi&&dqi>bqi){
michael@0 2395 if(dqi>cqi){
michael@0 2396 qim[1]=1;
michael@0 2397 qim[2]=2;
michael@0 2398 }
michael@0 2399 else{
michael@0 2400 qim[1]=2;
michael@0 2401 qim[2]=1;
michael@0 2402 }
michael@0 2403 }
michael@0 2404 else if(cqi<bqi&&dqi<bqi){
michael@0 2405 if(dqi<cqi){
michael@0 2406 qim[1]=-1;
michael@0 2407 qim[2]=-2;
michael@0 2408 }
michael@0 2409 else{
michael@0 2410 qim[1]=-2;
michael@0 2411 qim[2]=-1;
michael@0 2412 }
michael@0 2413 }
michael@0 2414 else{
michael@0 2415 if(cqi<bqi)qim[1]=-1;
michael@0 2416 else qim[1]=1;
michael@0 2417 if(dqi<bqi)qim[2]=-1;
michael@0 2418 else qim[2]=1;
michael@0 2419 }
michael@0 2420 }
michael@0 2421 c=cairo_create(cs);
michael@0 2422 frags=_dec->state.frags;
michael@0 2423 frag_mvs=_dec->state.frag_mvs;
michael@0 2424 mb_modes=_dec->state.mb_modes;
michael@0 2425 mb_maps=_dec->state.mb_maps;
michael@0 2426 nmbs=_dec->state.nmbs;
michael@0 2427 row2=0;
michael@0 2428 col2=0;
michael@0 2429 for(mbi=0;mbi<nmbs;mbi++){
michael@0 2430 float x;
michael@0 2431 float y;
michael@0 2432 int bi;
michael@0 2433 y=h-(row2+((col2+1>>1)&1))*16-16;
michael@0 2434 x=(col2>>1)*16;
michael@0 2435 cairo_set_line_width(c,1.);
michael@0 2436 /*Keyframe (all intra) red box.*/
michael@0 2437 if(_dec->state.frame_type==OC_INTRA_FRAME){
michael@0 2438 if(_dec->telemetry_mbmode&0x02){
michael@0 2439 cairo_set_source_rgba(c,1.,0,0,.5);
michael@0 2440 cairo_rectangle(c,x+2.5,y+2.5,11,11);
michael@0 2441 cairo_stroke_preserve(c);
michael@0 2442 cairo_set_source_rgba(c,1.,0,0,.25);
michael@0 2443 cairo_fill(c);
michael@0 2444 }
michael@0 2445 }
michael@0 2446 else{
michael@0 2447 ptrdiff_t fragi;
michael@0 2448 int frag_mvx;
michael@0 2449 int frag_mvy;
michael@0 2450 for(bi=0;bi<4;bi++){
michael@0 2451 fragi=mb_maps[mbi][0][bi];
michael@0 2452 if(fragi>=0&&frags[fragi].coded){
michael@0 2453 frag_mvx=OC_MV_X(frag_mvs[fragi]);
michael@0 2454 frag_mvy=OC_MV_Y(frag_mvs[fragi]);
michael@0 2455 break;
michael@0 2456 }
michael@0 2457 }
michael@0 2458 if(bi<4){
michael@0 2459 switch(mb_modes[mbi]){
michael@0 2460 case OC_MODE_INTRA:{
michael@0 2461 if(_dec->telemetry_mbmode&0x02){
michael@0 2462 cairo_set_source_rgba(c,1.,0,0,.5);
michael@0 2463 cairo_rectangle(c,x+2.5,y+2.5,11,11);
michael@0 2464 cairo_stroke_preserve(c);
michael@0 2465 cairo_set_source_rgba(c,1.,0,0,.25);
michael@0 2466 cairo_fill(c);
michael@0 2467 }
michael@0 2468 }break;
michael@0 2469 case OC_MODE_INTER_NOMV:{
michael@0 2470 if(_dec->telemetry_mbmode&0x01){
michael@0 2471 cairo_set_source_rgba(c,0,0,1.,.5);
michael@0 2472 cairo_rectangle(c,x+2.5,y+2.5,11,11);
michael@0 2473 cairo_stroke_preserve(c);
michael@0 2474 cairo_set_source_rgba(c,0,0,1.,.25);
michael@0 2475 cairo_fill(c);
michael@0 2476 }
michael@0 2477 }break;
michael@0 2478 case OC_MODE_INTER_MV:{
michael@0 2479 if(_dec->telemetry_mbmode&0x04){
michael@0 2480 cairo_rectangle(c,x+2.5,y+2.5,11,11);
michael@0 2481 cairo_set_source_rgba(c,0,1.,0,.5);
michael@0 2482 cairo_stroke(c);
michael@0 2483 }
michael@0 2484 if(_dec->telemetry_mv&0x04){
michael@0 2485 cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
michael@0 2486 cairo_set_source_rgba(c,1.,1.,1.,.9);
michael@0 2487 cairo_set_line_width(c,3.);
michael@0 2488 cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
michael@0 2489 cairo_stroke_preserve(c);
michael@0 2490 cairo_set_line_width(c,2.);
michael@0 2491 cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
michael@0 2492 cairo_stroke_preserve(c);
michael@0 2493 cairo_set_line_width(c,1.);
michael@0 2494 cairo_line_to(c,x+8,y+8);
michael@0 2495 cairo_stroke(c);
michael@0 2496 }
michael@0 2497 }break;
michael@0 2498 case OC_MODE_INTER_MV_LAST:{
michael@0 2499 if(_dec->telemetry_mbmode&0x08){
michael@0 2500 cairo_rectangle(c,x+2.5,y+2.5,11,11);
michael@0 2501 cairo_set_source_rgba(c,0,1.,0,.5);
michael@0 2502 cairo_move_to(c,x+13.5,y+2.5);
michael@0 2503 cairo_line_to(c,x+2.5,y+8);
michael@0 2504 cairo_line_to(c,x+13.5,y+13.5);
michael@0 2505 cairo_stroke(c);
michael@0 2506 }
michael@0 2507 if(_dec->telemetry_mv&0x08){
michael@0 2508 cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
michael@0 2509 cairo_set_source_rgba(c,1.,1.,1.,.9);
michael@0 2510 cairo_set_line_width(c,3.);
michael@0 2511 cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
michael@0 2512 cairo_stroke_preserve(c);
michael@0 2513 cairo_set_line_width(c,2.);
michael@0 2514 cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
michael@0 2515 cairo_stroke_preserve(c);
michael@0 2516 cairo_set_line_width(c,1.);
michael@0 2517 cairo_line_to(c,x+8,y+8);
michael@0 2518 cairo_stroke(c);
michael@0 2519 }
michael@0 2520 }break;
michael@0 2521 case OC_MODE_INTER_MV_LAST2:{
michael@0 2522 if(_dec->telemetry_mbmode&0x10){
michael@0 2523 cairo_rectangle(c,x+2.5,y+2.5,11,11);
michael@0 2524 cairo_set_source_rgba(c,0,1.,0,.5);
michael@0 2525 cairo_move_to(c,x+8,y+2.5);
michael@0 2526 cairo_line_to(c,x+2.5,y+8);
michael@0 2527 cairo_line_to(c,x+8,y+13.5);
michael@0 2528 cairo_move_to(c,x+13.5,y+2.5);
michael@0 2529 cairo_line_to(c,x+8,y+8);
michael@0 2530 cairo_line_to(c,x+13.5,y+13.5);
michael@0 2531 cairo_stroke(c);
michael@0 2532 }
michael@0 2533 if(_dec->telemetry_mv&0x10){
michael@0 2534 cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
michael@0 2535 cairo_set_source_rgba(c,1.,1.,1.,.9);
michael@0 2536 cairo_set_line_width(c,3.);
michael@0 2537 cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
michael@0 2538 cairo_stroke_preserve(c);
michael@0 2539 cairo_set_line_width(c,2.);
michael@0 2540 cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
michael@0 2541 cairo_stroke_preserve(c);
michael@0 2542 cairo_set_line_width(c,1.);
michael@0 2543 cairo_line_to(c,x+8,y+8);
michael@0 2544 cairo_stroke(c);
michael@0 2545 }
michael@0 2546 }break;
michael@0 2547 case OC_MODE_GOLDEN_NOMV:{
michael@0 2548 if(_dec->telemetry_mbmode&0x20){
michael@0 2549 cairo_set_source_rgba(c,1.,1.,0,.5);
michael@0 2550 cairo_rectangle(c,x+2.5,y+2.5,11,11);
michael@0 2551 cairo_stroke_preserve(c);
michael@0 2552 cairo_set_source_rgba(c,1.,1.,0,.25);
michael@0 2553 cairo_fill(c);
michael@0 2554 }
michael@0 2555 }break;
michael@0 2556 case OC_MODE_GOLDEN_MV:{
michael@0 2557 if(_dec->telemetry_mbmode&0x40){
michael@0 2558 cairo_rectangle(c,x+2.5,y+2.5,11,11);
michael@0 2559 cairo_set_source_rgba(c,1.,1.,0,.5);
michael@0 2560 cairo_stroke(c);
michael@0 2561 }
michael@0 2562 if(_dec->telemetry_mv&0x40){
michael@0 2563 cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
michael@0 2564 cairo_set_source_rgba(c,1.,1.,1.,.9);
michael@0 2565 cairo_set_line_width(c,3.);
michael@0 2566 cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
michael@0 2567 cairo_stroke_preserve(c);
michael@0 2568 cairo_set_line_width(c,2.);
michael@0 2569 cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
michael@0 2570 cairo_stroke_preserve(c);
michael@0 2571 cairo_set_line_width(c,1.);
michael@0 2572 cairo_line_to(c,x+8,y+8);
michael@0 2573 cairo_stroke(c);
michael@0 2574 }
michael@0 2575 }break;
michael@0 2576 case OC_MODE_INTER_MV_FOUR:{
michael@0 2577 if(_dec->telemetry_mbmode&0x80){
michael@0 2578 cairo_rectangle(c,x+2.5,y+2.5,4,4);
michael@0 2579 cairo_rectangle(c,x+9.5,y+2.5,4,4);
michael@0 2580 cairo_rectangle(c,x+2.5,y+9.5,4,4);
michael@0 2581 cairo_rectangle(c,x+9.5,y+9.5,4,4);
michael@0 2582 cairo_set_source_rgba(c,0,1.,0,.5);
michael@0 2583 cairo_stroke(c);
michael@0 2584 }
michael@0 2585 /*4mv is odd, coded in raster order.*/
michael@0 2586 fragi=mb_maps[mbi][0][0];
michael@0 2587 if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
michael@0 2588 frag_mvx=OC_MV_X(frag_mvs[fragi]);
michael@0 2589 frag_mvx=OC_MV_Y(frag_mvs[fragi]);
michael@0 2590 cairo_move_to(c,x+4+frag_mvx,y+12-frag_mvy);
michael@0 2591 cairo_set_source_rgba(c,1.,1.,1.,.9);
michael@0 2592 cairo_set_line_width(c,3.);
michael@0 2593 cairo_line_to(c,x+4+frag_mvx*.66,y+12-frag_mvy*.66);
michael@0 2594 cairo_stroke_preserve(c);
michael@0 2595 cairo_set_line_width(c,2.);
michael@0 2596 cairo_line_to(c,x+4+frag_mvx*.33,y+12-frag_mvy*.33);
michael@0 2597 cairo_stroke_preserve(c);
michael@0 2598 cairo_set_line_width(c,1.);
michael@0 2599 cairo_line_to(c,x+4,y+12);
michael@0 2600 cairo_stroke(c);
michael@0 2601 }
michael@0 2602 fragi=mb_maps[mbi][0][1];
michael@0 2603 if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
michael@0 2604 frag_mvx=OC_MV_X(frag_mvs[fragi]);
michael@0 2605 frag_mvx=OC_MV_Y(frag_mvs[fragi]);
michael@0 2606 cairo_move_to(c,x+12+frag_mvx,y+12-frag_mvy);
michael@0 2607 cairo_set_source_rgba(c,1.,1.,1.,.9);
michael@0 2608 cairo_set_line_width(c,3.);
michael@0 2609 cairo_line_to(c,x+12+frag_mvx*.66,y+12-frag_mvy*.66);
michael@0 2610 cairo_stroke_preserve(c);
michael@0 2611 cairo_set_line_width(c,2.);
michael@0 2612 cairo_line_to(c,x+12+frag_mvx*.33,y+12-frag_mvy*.33);
michael@0 2613 cairo_stroke_preserve(c);
michael@0 2614 cairo_set_line_width(c,1.);
michael@0 2615 cairo_line_to(c,x+12,y+12);
michael@0 2616 cairo_stroke(c);
michael@0 2617 }
michael@0 2618 fragi=mb_maps[mbi][0][2];
michael@0 2619 if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
michael@0 2620 frag_mvx=OC_MV_X(frag_mvs[fragi]);
michael@0 2621 frag_mvx=OC_MV_Y(frag_mvs[fragi]);
michael@0 2622 cairo_move_to(c,x+4+frag_mvx,y+4-frag_mvy);
michael@0 2623 cairo_set_source_rgba(c,1.,1.,1.,.9);
michael@0 2624 cairo_set_line_width(c,3.);
michael@0 2625 cairo_line_to(c,x+4+frag_mvx*.66,y+4-frag_mvy*.66);
michael@0 2626 cairo_stroke_preserve(c);
michael@0 2627 cairo_set_line_width(c,2.);
michael@0 2628 cairo_line_to(c,x+4+frag_mvx*.33,y+4-frag_mvy*.33);
michael@0 2629 cairo_stroke_preserve(c);
michael@0 2630 cairo_set_line_width(c,1.);
michael@0 2631 cairo_line_to(c,x+4,y+4);
michael@0 2632 cairo_stroke(c);
michael@0 2633 }
michael@0 2634 fragi=mb_maps[mbi][0][3];
michael@0 2635 if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
michael@0 2636 frag_mvx=OC_MV_X(frag_mvs[fragi]);
michael@0 2637 frag_mvx=OC_MV_Y(frag_mvs[fragi]);
michael@0 2638 cairo_move_to(c,x+12+frag_mvx,y+4-frag_mvy);
michael@0 2639 cairo_set_source_rgba(c,1.,1.,1.,.9);
michael@0 2640 cairo_set_line_width(c,3.);
michael@0 2641 cairo_line_to(c,x+12+frag_mvx*.66,y+4-frag_mvy*.66);
michael@0 2642 cairo_stroke_preserve(c);
michael@0 2643 cairo_set_line_width(c,2.);
michael@0 2644 cairo_line_to(c,x+12+frag_mvx*.33,y+4-frag_mvy*.33);
michael@0 2645 cairo_stroke_preserve(c);
michael@0 2646 cairo_set_line_width(c,1.);
michael@0 2647 cairo_line_to(c,x+12,y+4);
michael@0 2648 cairo_stroke(c);
michael@0 2649 }
michael@0 2650 }break;
michael@0 2651 }
michael@0 2652 }
michael@0 2653 }
michael@0 2654 /*qii illustration.*/
michael@0 2655 if(_dec->telemetry_qi&0x2){
michael@0 2656 cairo_set_line_cap(c,CAIRO_LINE_CAP_SQUARE);
michael@0 2657 for(bi=0;bi<4;bi++){
michael@0 2658 ptrdiff_t fragi;
michael@0 2659 int qiv;
michael@0 2660 int xp;
michael@0 2661 int yp;
michael@0 2662 xp=x+(bi&1)*8;
michael@0 2663 yp=y+8-(bi&2)*4;
michael@0 2664 fragi=mb_maps[mbi][0][bi];
michael@0 2665 if(fragi>=0&&frags[fragi].coded){
michael@0 2666 qiv=qim[frags[fragi].qii];
michael@0 2667 cairo_set_line_width(c,3.);
michael@0 2668 cairo_set_source_rgba(c,0.,0.,0.,.5);
michael@0 2669 switch(qiv){
michael@0 2670 /*Double plus:*/
michael@0 2671 case 2:{
michael@0 2672 if((bi&1)^((bi&2)>>1)){
michael@0 2673 cairo_move_to(c,xp+2.5,yp+1.5);
michael@0 2674 cairo_line_to(c,xp+2.5,yp+3.5);
michael@0 2675 cairo_move_to(c,xp+1.5,yp+2.5);
michael@0 2676 cairo_line_to(c,xp+3.5,yp+2.5);
michael@0 2677 cairo_move_to(c,xp+5.5,yp+4.5);
michael@0 2678 cairo_line_to(c,xp+5.5,yp+6.5);
michael@0 2679 cairo_move_to(c,xp+4.5,yp+5.5);
michael@0 2680 cairo_line_to(c,xp+6.5,yp+5.5);
michael@0 2681 cairo_stroke_preserve(c);
michael@0 2682 cairo_set_source_rgba(c,0.,1.,1.,1.);
michael@0 2683 }
michael@0 2684 else{
michael@0 2685 cairo_move_to(c,xp+5.5,yp+1.5);
michael@0 2686 cairo_line_to(c,xp+5.5,yp+3.5);
michael@0 2687 cairo_move_to(c,xp+4.5,yp+2.5);
michael@0 2688 cairo_line_to(c,xp+6.5,yp+2.5);
michael@0 2689 cairo_move_to(c,xp+2.5,yp+4.5);
michael@0 2690 cairo_line_to(c,xp+2.5,yp+6.5);
michael@0 2691 cairo_move_to(c,xp+1.5,yp+5.5);
michael@0 2692 cairo_line_to(c,xp+3.5,yp+5.5);
michael@0 2693 cairo_stroke_preserve(c);
michael@0 2694 cairo_set_source_rgba(c,0.,1.,1.,1.);
michael@0 2695 }
michael@0 2696 }break;
michael@0 2697 /*Double minus:*/
michael@0 2698 case -2:{
michael@0 2699 cairo_move_to(c,xp+2.5,yp+2.5);
michael@0 2700 cairo_line_to(c,xp+5.5,yp+2.5);
michael@0 2701 cairo_move_to(c,xp+2.5,yp+5.5);
michael@0 2702 cairo_line_to(c,xp+5.5,yp+5.5);
michael@0 2703 cairo_stroke_preserve(c);
michael@0 2704 cairo_set_source_rgba(c,1.,1.,1.,1.);
michael@0 2705 }break;
michael@0 2706 /*Plus:*/
michael@0 2707 case 1:{
michael@0 2708 if(bi&2==0)yp-=2;
michael@0 2709 if(bi&1==0)xp-=2;
michael@0 2710 cairo_move_to(c,xp+4.5,yp+2.5);
michael@0 2711 cairo_line_to(c,xp+4.5,yp+6.5);
michael@0 2712 cairo_move_to(c,xp+2.5,yp+4.5);
michael@0 2713 cairo_line_to(c,xp+6.5,yp+4.5);
michael@0 2714 cairo_stroke_preserve(c);
michael@0 2715 cairo_set_source_rgba(c,.1,1.,.3,1.);
michael@0 2716 break;
michael@0 2717 }
michael@0 2718 /*Fall through.*/
michael@0 2719 /*Minus:*/
michael@0 2720 case -1:{
michael@0 2721 cairo_move_to(c,xp+2.5,yp+4.5);
michael@0 2722 cairo_line_to(c,xp+6.5,yp+4.5);
michael@0 2723 cairo_stroke_preserve(c);
michael@0 2724 cairo_set_source_rgba(c,1.,.3,.1,1.);
michael@0 2725 }break;
michael@0 2726 default:continue;
michael@0 2727 }
michael@0 2728 cairo_set_line_width(c,1.);
michael@0 2729 cairo_stroke(c);
michael@0 2730 }
michael@0 2731 }
michael@0 2732 }
michael@0 2733 col2++;
michael@0 2734 if((col2>>1)>=_dec->state.nhmbs){
michael@0 2735 col2=0;
michael@0 2736 row2+=2;
michael@0 2737 }
michael@0 2738 }
michael@0 2739 /*Bit usage indicator[s]:*/
michael@0 2740 if(_dec->telemetry_bits){
michael@0 2741 int widths[6];
michael@0 2742 int fpsn;
michael@0 2743 int fpsd;
michael@0 2744 int mult;
michael@0 2745 int fullw;
michael@0 2746 int padw;
michael@0 2747 int i;
michael@0 2748 fpsn=_dec->state.info.fps_numerator;
michael@0 2749 fpsd=_dec->state.info.fps_denominator;
michael@0 2750 mult=(_dec->telemetry_bits>=0xFF?1:_dec->telemetry_bits);
michael@0 2751 fullw=250.f*h*fpsd*mult/fpsn;
michael@0 2752 padw=w-24;
michael@0 2753 /*Header and coded block bits.*/
michael@0 2754 if(_dec->telemetry_frame_bytes<0||
michael@0 2755 _dec->telemetry_frame_bytes==OC_LOTS_OF_BITS){
michael@0 2756 _dec->telemetry_frame_bytes=0;
michael@0 2757 }
michael@0 2758 if(_dec->telemetry_coding_bytes<0||
michael@0 2759 _dec->telemetry_coding_bytes>_dec->telemetry_frame_bytes){
michael@0 2760 _dec->telemetry_coding_bytes=0;
michael@0 2761 }
michael@0 2762 if(_dec->telemetry_mode_bytes<0||
michael@0 2763 _dec->telemetry_mode_bytes>_dec->telemetry_frame_bytes){
michael@0 2764 _dec->telemetry_mode_bytes=0;
michael@0 2765 }
michael@0 2766 if(_dec->telemetry_mv_bytes<0||
michael@0 2767 _dec->telemetry_mv_bytes>_dec->telemetry_frame_bytes){
michael@0 2768 _dec->telemetry_mv_bytes=0;
michael@0 2769 }
michael@0 2770 if(_dec->telemetry_qi_bytes<0||
michael@0 2771 _dec->telemetry_qi_bytes>_dec->telemetry_frame_bytes){
michael@0 2772 _dec->telemetry_qi_bytes=0;
michael@0 2773 }
michael@0 2774 if(_dec->telemetry_dc_bytes<0||
michael@0 2775 _dec->telemetry_dc_bytes>_dec->telemetry_frame_bytes){
michael@0 2776 _dec->telemetry_dc_bytes=0;
michael@0 2777 }
michael@0 2778 widths[0]=padw*(_dec->telemetry_frame_bytes-_dec->telemetry_coding_bytes)/fullw;
michael@0 2779 widths[1]=padw*(_dec->telemetry_coding_bytes-_dec->telemetry_mode_bytes)/fullw;
michael@0 2780 widths[2]=padw*(_dec->telemetry_mode_bytes-_dec->telemetry_mv_bytes)/fullw;
michael@0 2781 widths[3]=padw*(_dec->telemetry_mv_bytes-_dec->telemetry_qi_bytes)/fullw;
michael@0 2782 widths[4]=padw*(_dec->telemetry_qi_bytes-_dec->telemetry_dc_bytes)/fullw;
michael@0 2783 widths[5]=padw*(_dec->telemetry_dc_bytes)/fullw;
michael@0 2784 for(i=0;i<6;i++)if(widths[i]>w)widths[i]=w;
michael@0 2785 cairo_set_source_rgba(c,.0,.0,.0,.6);
michael@0 2786 cairo_rectangle(c,10,h-33,widths[0]+1,5);
michael@0 2787 cairo_rectangle(c,10,h-29,widths[1]+1,5);
michael@0 2788 cairo_rectangle(c,10,h-25,widths[2]+1,5);
michael@0 2789 cairo_rectangle(c,10,h-21,widths[3]+1,5);
michael@0 2790 cairo_rectangle(c,10,h-17,widths[4]+1,5);
michael@0 2791 cairo_rectangle(c,10,h-13,widths[5]+1,5);
michael@0 2792 cairo_fill(c);
michael@0 2793 cairo_set_source_rgb(c,1,0,0);
michael@0 2794 cairo_rectangle(c,10.5,h-32.5,widths[0],4);
michael@0 2795 cairo_fill(c);
michael@0 2796 cairo_set_source_rgb(c,0,1,0);
michael@0 2797 cairo_rectangle(c,10.5,h-28.5,widths[1],4);
michael@0 2798 cairo_fill(c);
michael@0 2799 cairo_set_source_rgb(c,0,0,1);
michael@0 2800 cairo_rectangle(c,10.5,h-24.5,widths[2],4);
michael@0 2801 cairo_fill(c);
michael@0 2802 cairo_set_source_rgb(c,.6,.4,.0);
michael@0 2803 cairo_rectangle(c,10.5,h-20.5,widths[3],4);
michael@0 2804 cairo_fill(c);
michael@0 2805 cairo_set_source_rgb(c,.3,.3,.3);
michael@0 2806 cairo_rectangle(c,10.5,h-16.5,widths[4],4);
michael@0 2807 cairo_fill(c);
michael@0 2808 cairo_set_source_rgb(c,.5,.5,.8);
michael@0 2809 cairo_rectangle(c,10.5,h-12.5,widths[5],4);
michael@0 2810 cairo_fill(c);
michael@0 2811 }
michael@0 2812 /*Master qi indicator[s]:*/
michael@0 2813 if(_dec->telemetry_qi&0x1){
michael@0 2814 cairo_text_extents_t extents;
michael@0 2815 char buffer[10];
michael@0 2816 int p;
michael@0 2817 int y;
michael@0 2818 p=0;
michael@0 2819 y=h-7.5;
michael@0 2820 if(_dec->state.qis[0]>=10)buffer[p++]=48+_dec->state.qis[0]/10;
michael@0 2821 buffer[p++]=48+_dec->state.qis[0]%10;
michael@0 2822 if(_dec->state.nqis>=2){
michael@0 2823 buffer[p++]=' ';
michael@0 2824 if(_dec->state.qis[1]>=10)buffer[p++]=48+_dec->state.qis[1]/10;
michael@0 2825 buffer[p++]=48+_dec->state.qis[1]%10;
michael@0 2826 }
michael@0 2827 if(_dec->state.nqis==3){
michael@0 2828 buffer[p++]=' ';
michael@0 2829 if(_dec->state.qis[2]>=10)buffer[p++]=48+_dec->state.qis[2]/10;
michael@0 2830 buffer[p++]=48+_dec->state.qis[2]%10;
michael@0 2831 }
michael@0 2832 buffer[p++]='\0';
michael@0 2833 cairo_select_font_face(c,"sans",
michael@0 2834 CAIRO_FONT_SLANT_NORMAL,CAIRO_FONT_WEIGHT_BOLD);
michael@0 2835 cairo_set_font_size(c,18);
michael@0 2836 cairo_text_extents(c,buffer,&extents);
michael@0 2837 cairo_set_source_rgb(c,1,1,1);
michael@0 2838 cairo_move_to(c,w-extents.x_advance-10,y);
michael@0 2839 cairo_show_text(c,buffer);
michael@0 2840 cairo_set_source_rgb(c,0,0,0);
michael@0 2841 cairo_move_to(c,w-extents.x_advance-10,y);
michael@0 2842 cairo_text_path(c,buffer);
michael@0 2843 cairo_set_line_width(c,.8);
michael@0 2844 cairo_set_line_join(c,CAIRO_LINE_JOIN_ROUND);
michael@0 2845 cairo_stroke(c);
michael@0 2846 }
michael@0 2847 cairo_destroy(c);
michael@0 2848 }
michael@0 2849 /*Out of the Cairo plane into the telemetry YUV buffer.*/
michael@0 2850 _ycbcr[0].data=_dec->telemetry_frame_data;
michael@0 2851 _ycbcr[0].stride=_ycbcr[0].width;
michael@0 2852 _ycbcr[1].data=_ycbcr[0].data+h*_ycbcr[0].stride;
michael@0 2853 _ycbcr[1].stride=_ycbcr[1].width;
michael@0 2854 _ycbcr[2].data=_ycbcr[1].data+(h>>vdec)*_ycbcr[1].stride;
michael@0 2855 _ycbcr[2].stride=_ycbcr[2].width;
michael@0 2856 y_row=_ycbcr[0].data;
michael@0 2857 u_row=_ycbcr[1].data;
michael@0 2858 v_row=_ycbcr[2].data;
michael@0 2859 rgb_row=data;
michael@0 2860 /*This is one of the few places it's worth handling chroma on a
michael@0 2861 case-by-case basis.*/
michael@0 2862 switch(_dec->state.info.pixel_fmt){
michael@0 2863 case TH_PF_420:{
michael@0 2864 for(y=0;y<h;y+=2){
michael@0 2865 unsigned char *y_row2;
michael@0 2866 unsigned char *rgb_row2;
michael@0 2867 y_row2=y_row+_ycbcr[0].stride;
michael@0 2868 rgb_row2=rgb_row+cstride;
michael@0 2869 for(x=0;x<w;x+=2){
michael@0 2870 int y;
michael@0 2871 int u;
michael@0 2872 int v;
michael@0 2873 y=(65481*rgb_row[4*x+2]+128553*rgb_row[4*x+1]
michael@0 2874 +24966*rgb_row[4*x+0]+4207500)/255000;
michael@0 2875 y_row[x]=OC_CLAMP255(y);
michael@0 2876 y=(65481*rgb_row[4*x+6]+128553*rgb_row[4*x+5]
michael@0 2877 +24966*rgb_row[4*x+4]+4207500)/255000;
michael@0 2878 y_row[x+1]=OC_CLAMP255(y);
michael@0 2879 y=(65481*rgb_row2[4*x+2]+128553*rgb_row2[4*x+1]
michael@0 2880 +24966*rgb_row2[4*x+0]+4207500)/255000;
michael@0 2881 y_row2[x]=OC_CLAMP255(y);
michael@0 2882 y=(65481*rgb_row2[4*x+6]+128553*rgb_row2[4*x+5]
michael@0 2883 +24966*rgb_row2[4*x+4]+4207500)/255000;
michael@0 2884 y_row2[x+1]=OC_CLAMP255(y);
michael@0 2885 u=(-8372*(rgb_row[4*x+2]+rgb_row[4*x+6]
michael@0 2886 +rgb_row2[4*x+2]+rgb_row2[4*x+6])
michael@0 2887 -16436*(rgb_row[4*x+1]+rgb_row[4*x+5]
michael@0 2888 +rgb_row2[4*x+1]+rgb_row2[4*x+5])
michael@0 2889 +24808*(rgb_row[4*x+0]+rgb_row[4*x+4]
michael@0 2890 +rgb_row2[4*x+0]+rgb_row2[4*x+4])+29032005)/225930;
michael@0 2891 v=(39256*(rgb_row[4*x+2]+rgb_row[4*x+6]
michael@0 2892 +rgb_row2[4*x+2]+rgb_row2[4*x+6])
michael@0 2893 -32872*(rgb_row[4*x+1]+rgb_row[4*x+5]
michael@0 2894 +rgb_row2[4*x+1]+rgb_row2[4*x+5])
michael@0 2895 -6384*(rgb_row[4*x+0]+rgb_row[4*x+4]
michael@0 2896 +rgb_row2[4*x+0]+rgb_row2[4*x+4])+45940035)/357510;
michael@0 2897 u_row[x>>1]=OC_CLAMP255(u);
michael@0 2898 v_row[x>>1]=OC_CLAMP255(v);
michael@0 2899 }
michael@0 2900 y_row+=_ycbcr[0].stride<<1;
michael@0 2901 u_row+=_ycbcr[1].stride;
michael@0 2902 v_row+=_ycbcr[2].stride;
michael@0 2903 rgb_row+=cstride<<1;
michael@0 2904 }
michael@0 2905 }break;
michael@0 2906 case TH_PF_422:{
michael@0 2907 for(y=0;y<h;y++){
michael@0 2908 for(x=0;x<w;x+=2){
michael@0 2909 int y;
michael@0 2910 int u;
michael@0 2911 int v;
michael@0 2912 y=(65481*rgb_row[4*x+2]+128553*rgb_row[4*x+1]
michael@0 2913 +24966*rgb_row[4*x+0]+4207500)/255000;
michael@0 2914 y_row[x]=OC_CLAMP255(y);
michael@0 2915 y=(65481*rgb_row[4*x+6]+128553*rgb_row[4*x+5]
michael@0 2916 +24966*rgb_row[4*x+4]+4207500)/255000;
michael@0 2917 y_row[x+1]=OC_CLAMP255(y);
michael@0 2918 u=(-16744*(rgb_row[4*x+2]+rgb_row[4*x+6])
michael@0 2919 -32872*(rgb_row[4*x+1]+rgb_row[4*x+5])
michael@0 2920 +49616*(rgb_row[4*x+0]+rgb_row[4*x+4])+29032005)/225930;
michael@0 2921 v=(78512*(rgb_row[4*x+2]+rgb_row[4*x+6])
michael@0 2922 -65744*(rgb_row[4*x+1]+rgb_row[4*x+5])
michael@0 2923 -12768*(rgb_row[4*x+0]+rgb_row[4*x+4])+45940035)/357510;
michael@0 2924 u_row[x>>1]=OC_CLAMP255(u);
michael@0 2925 v_row[x>>1]=OC_CLAMP255(v);
michael@0 2926 }
michael@0 2927 y_row+=_ycbcr[0].stride;
michael@0 2928 u_row+=_ycbcr[1].stride;
michael@0 2929 v_row+=_ycbcr[2].stride;
michael@0 2930 rgb_row+=cstride;
michael@0 2931 }
michael@0 2932 }break;
michael@0 2933 /*case TH_PF_444:*/
michael@0 2934 default:{
michael@0 2935 for(y=0;y<h;y++){
michael@0 2936 for(x=0;x<w;x++){
michael@0 2937 int y;
michael@0 2938 int u;
michael@0 2939 int v;
michael@0 2940 y=(65481*rgb_row[4*x+2]+128553*rgb_row[4*x+1]
michael@0 2941 +24966*rgb_row[4*x+0]+4207500)/255000;
michael@0 2942 u=(-33488*rgb_row[4*x+2]-65744*rgb_row[4*x+1]
michael@0 2943 +99232*rgb_row[4*x+0]+29032005)/225930;
michael@0 2944 v=(157024*rgb_row[4*x+2]-131488*rgb_row[4*x+1]
michael@0 2945 -25536*rgb_row[4*x+0]+45940035)/357510;
michael@0 2946 y_row[x]=OC_CLAMP255(y);
michael@0 2947 u_row[x]=OC_CLAMP255(u);
michael@0 2948 v_row[x]=OC_CLAMP255(v);
michael@0 2949 }
michael@0 2950 y_row+=_ycbcr[0].stride;
michael@0 2951 u_row+=_ycbcr[1].stride;
michael@0 2952 v_row+=_ycbcr[2].stride;
michael@0 2953 rgb_row+=cstride;
michael@0 2954 }
michael@0 2955 }break;
michael@0 2956 }
michael@0 2957 /*Finished.
michael@0 2958 Destroy the surface.*/
michael@0 2959 cairo_surface_destroy(cs);
michael@0 2960 }
michael@0 2961 #endif
michael@0 2962 return 0;
michael@0 2963 }

mercurial