Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | |
michael@0 | 12 | #include "./vp9_rtcd.h" |
michael@0 | 13 | #include "./vpx_config.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "vpx_mem/vpx_mem.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "vp9/common/vp9_idct.h" |
michael@0 | 18 | #include "vp9/common/vp9_reconinter.h" |
michael@0 | 19 | #include "vp9/common/vp9_reconintra.h" |
michael@0 | 20 | #include "vp9/common/vp9_systemdependent.h" |
michael@0 | 21 | |
michael@0 | 22 | #include "vp9/encoder/vp9_dct.h" |
michael@0 | 23 | #include "vp9/encoder/vp9_encodemb.h" |
michael@0 | 24 | #include "vp9/encoder/vp9_quantize.h" |
michael@0 | 25 | #include "vp9/encoder/vp9_rdopt.h" |
michael@0 | 26 | #include "vp9/encoder/vp9_tokenize.h" |
michael@0 | 27 | |
michael@0 | 28 | void vp9_subtract_block_c(int rows, int cols, |
michael@0 | 29 | int16_t *diff_ptr, ptrdiff_t diff_stride, |
michael@0 | 30 | const uint8_t *src_ptr, ptrdiff_t src_stride, |
michael@0 | 31 | const uint8_t *pred_ptr, ptrdiff_t pred_stride) { |
michael@0 | 32 | int r, c; |
michael@0 | 33 | |
michael@0 | 34 | for (r = 0; r < rows; r++) { |
michael@0 | 35 | for (c = 0; c < cols; c++) |
michael@0 | 36 | diff_ptr[c] = src_ptr[c] - pred_ptr[c]; |
michael@0 | 37 | |
michael@0 | 38 | diff_ptr += diff_stride; |
michael@0 | 39 | pred_ptr += pred_stride; |
michael@0 | 40 | src_ptr += src_stride; |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | static void subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) { |
michael@0 | 45 | struct macroblock_plane *const p = &x->plane[plane]; |
michael@0 | 46 | const MACROBLOCKD *const xd = &x->e_mbd; |
michael@0 | 47 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
michael@0 | 48 | const int bw = plane_block_width(bsize, pd); |
michael@0 | 49 | const int bh = plane_block_height(bsize, pd); |
michael@0 | 50 | |
michael@0 | 51 | vp9_subtract_block(bh, bw, p->src_diff, bw, |
michael@0 | 52 | p->src.buf, p->src.stride, |
michael@0 | 53 | pd->dst.buf, pd->dst.stride); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | void vp9_subtract_sby(MACROBLOCK *x, BLOCK_SIZE bsize) { |
michael@0 | 57 | subtract_plane(x, bsize, 0); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | void vp9_subtract_sbuv(MACROBLOCK *x, BLOCK_SIZE bsize) { |
michael@0 | 61 | int i; |
michael@0 | 62 | |
michael@0 | 63 | for (i = 1; i < MAX_MB_PLANE; i++) |
michael@0 | 64 | subtract_plane(x, bsize, i); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | void vp9_subtract_sb(MACROBLOCK *x, BLOCK_SIZE bsize) { |
michael@0 | 68 | vp9_subtract_sby(x, bsize); |
michael@0 | 69 | vp9_subtract_sbuv(x, bsize); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | #define RDTRUNC(RM, DM, R, D) ((128 + (R) * (RM)) & 0xFF) |
michael@0 | 73 | typedef struct vp9_token_state vp9_token_state; |
michael@0 | 74 | |
michael@0 | 75 | struct vp9_token_state { |
michael@0 | 76 | int rate; |
michael@0 | 77 | int error; |
michael@0 | 78 | int next; |
michael@0 | 79 | signed char token; |
michael@0 | 80 | short qc; |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | // TODO(jimbankoski): experiment to find optimal RD numbers. |
michael@0 | 84 | #define Y1_RD_MULT 4 |
michael@0 | 85 | #define UV_RD_MULT 2 |
michael@0 | 86 | |
michael@0 | 87 | static const int plane_rd_mult[4] = { |
michael@0 | 88 | Y1_RD_MULT, |
michael@0 | 89 | UV_RD_MULT, |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | #define UPDATE_RD_COST()\ |
michael@0 | 93 | {\ |
michael@0 | 94 | rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0);\ |
michael@0 | 95 | rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1);\ |
michael@0 | 96 | if (rd_cost0 == rd_cost1) {\ |
michael@0 | 97 | rd_cost0 = RDTRUNC(rdmult, rddiv, rate0, error0);\ |
michael@0 | 98 | rd_cost1 = RDTRUNC(rdmult, rddiv, rate1, error1);\ |
michael@0 | 99 | }\ |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | // This function is a place holder for now but may ultimately need |
michael@0 | 103 | // to scan previous tokens to work out the correct context. |
michael@0 | 104 | static int trellis_get_coeff_context(const int16_t *scan, |
michael@0 | 105 | const int16_t *nb, |
michael@0 | 106 | int idx, int token, |
michael@0 | 107 | uint8_t *token_cache) { |
michael@0 | 108 | int bak = token_cache[scan[idx]], pt; |
michael@0 | 109 | token_cache[scan[idx]] = vp9_pt_energy_class[token]; |
michael@0 | 110 | pt = get_coef_context(nb, token_cache, idx + 1); |
michael@0 | 111 | token_cache[scan[idx]] = bak; |
michael@0 | 112 | return pt; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | static void optimize_b(MACROBLOCK *mb, |
michael@0 | 116 | int plane, int block, BLOCK_SIZE plane_bsize, |
michael@0 | 117 | ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l, |
michael@0 | 118 | TX_SIZE tx_size) { |
michael@0 | 119 | MACROBLOCKD *const xd = &mb->e_mbd; |
michael@0 | 120 | struct macroblockd_plane *pd = &xd->plane[plane]; |
michael@0 | 121 | const int ref = is_inter_block(&xd->mi_8x8[0]->mbmi); |
michael@0 | 122 | vp9_token_state tokens[1025][2]; |
michael@0 | 123 | unsigned best_index[1025][2]; |
michael@0 | 124 | const int16_t *coeff_ptr = BLOCK_OFFSET(mb->plane[plane].coeff, block); |
michael@0 | 125 | int16_t *qcoeff_ptr; |
michael@0 | 126 | int16_t *dqcoeff_ptr; |
michael@0 | 127 | int eob = pd->eobs[block], final_eob, sz = 0; |
michael@0 | 128 | const int i0 = 0; |
michael@0 | 129 | int rc, x, next, i; |
michael@0 | 130 | int64_t rdmult, rddiv, rd_cost0, rd_cost1; |
michael@0 | 131 | int rate0, rate1, error0, error1, t0, t1; |
michael@0 | 132 | int best, band, pt; |
michael@0 | 133 | PLANE_TYPE type = pd->plane_type; |
michael@0 | 134 | int err_mult = plane_rd_mult[type]; |
michael@0 | 135 | const int default_eob = 16 << (tx_size << 1); |
michael@0 | 136 | const int16_t *scan, *nb; |
michael@0 | 137 | const int mul = 1 + (tx_size == TX_32X32); |
michael@0 | 138 | uint8_t token_cache[1024]; |
michael@0 | 139 | const int16_t *dequant_ptr = pd->dequant; |
michael@0 | 140 | const uint8_t *const band_translate = get_band_translate(tx_size); |
michael@0 | 141 | |
michael@0 | 142 | assert((!type && !plane) || (type && plane)); |
michael@0 | 143 | dqcoeff_ptr = BLOCK_OFFSET(pd->dqcoeff, block); |
michael@0 | 144 | qcoeff_ptr = BLOCK_OFFSET(pd->qcoeff, block); |
michael@0 | 145 | get_scan(xd, tx_size, type, block, &scan, &nb); |
michael@0 | 146 | assert(eob <= default_eob); |
michael@0 | 147 | |
michael@0 | 148 | /* Now set up a Viterbi trellis to evaluate alternative roundings. */ |
michael@0 | 149 | rdmult = mb->rdmult * err_mult; |
michael@0 | 150 | if (mb->e_mbd.mi_8x8[0]->mbmi.ref_frame[0] == INTRA_FRAME) |
michael@0 | 151 | rdmult = (rdmult * 9) >> 4; |
michael@0 | 152 | rddiv = mb->rddiv; |
michael@0 | 153 | /* Initialize the sentinel node of the trellis. */ |
michael@0 | 154 | tokens[eob][0].rate = 0; |
michael@0 | 155 | tokens[eob][0].error = 0; |
michael@0 | 156 | tokens[eob][0].next = default_eob; |
michael@0 | 157 | tokens[eob][0].token = DCT_EOB_TOKEN; |
michael@0 | 158 | tokens[eob][0].qc = 0; |
michael@0 | 159 | *(tokens[eob] + 1) = *(tokens[eob] + 0); |
michael@0 | 160 | next = eob; |
michael@0 | 161 | for (i = 0; i < eob; i++) |
michael@0 | 162 | token_cache[scan[i]] = vp9_pt_energy_class[vp9_dct_value_tokens_ptr[ |
michael@0 | 163 | qcoeff_ptr[scan[i]]].token]; |
michael@0 | 164 | |
michael@0 | 165 | for (i = eob; i-- > i0;) { |
michael@0 | 166 | int base_bits, d2, dx; |
michael@0 | 167 | |
michael@0 | 168 | rc = scan[i]; |
michael@0 | 169 | x = qcoeff_ptr[rc]; |
michael@0 | 170 | /* Only add a trellis state for non-zero coefficients. */ |
michael@0 | 171 | if (x) { |
michael@0 | 172 | int shortcut = 0; |
michael@0 | 173 | error0 = tokens[next][0].error; |
michael@0 | 174 | error1 = tokens[next][1].error; |
michael@0 | 175 | /* Evaluate the first possibility for this state. */ |
michael@0 | 176 | rate0 = tokens[next][0].rate; |
michael@0 | 177 | rate1 = tokens[next][1].rate; |
michael@0 | 178 | t0 = (vp9_dct_value_tokens_ptr + x)->token; |
michael@0 | 179 | /* Consider both possible successor states. */ |
michael@0 | 180 | if (next < default_eob) { |
michael@0 | 181 | band = band_translate[i + 1]; |
michael@0 | 182 | pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache); |
michael@0 | 183 | rate0 += |
michael@0 | 184 | mb->token_costs[tx_size][type][ref][band][0][pt] |
michael@0 | 185 | [tokens[next][0].token]; |
michael@0 | 186 | rate1 += |
michael@0 | 187 | mb->token_costs[tx_size][type][ref][band][0][pt] |
michael@0 | 188 | [tokens[next][1].token]; |
michael@0 | 189 | } |
michael@0 | 190 | UPDATE_RD_COST(); |
michael@0 | 191 | /* And pick the best. */ |
michael@0 | 192 | best = rd_cost1 < rd_cost0; |
michael@0 | 193 | base_bits = *(vp9_dct_value_cost_ptr + x); |
michael@0 | 194 | dx = mul * (dqcoeff_ptr[rc] - coeff_ptr[rc]); |
michael@0 | 195 | d2 = dx * dx; |
michael@0 | 196 | tokens[i][0].rate = base_bits + (best ? rate1 : rate0); |
michael@0 | 197 | tokens[i][0].error = d2 + (best ? error1 : error0); |
michael@0 | 198 | tokens[i][0].next = next; |
michael@0 | 199 | tokens[i][0].token = t0; |
michael@0 | 200 | tokens[i][0].qc = x; |
michael@0 | 201 | best_index[i][0] = best; |
michael@0 | 202 | |
michael@0 | 203 | /* Evaluate the second possibility for this state. */ |
michael@0 | 204 | rate0 = tokens[next][0].rate; |
michael@0 | 205 | rate1 = tokens[next][1].rate; |
michael@0 | 206 | |
michael@0 | 207 | if ((abs(x)*dequant_ptr[rc != 0] > abs(coeff_ptr[rc]) * mul) && |
michael@0 | 208 | (abs(x)*dequant_ptr[rc != 0] < abs(coeff_ptr[rc]) * mul + |
michael@0 | 209 | dequant_ptr[rc != 0])) |
michael@0 | 210 | shortcut = 1; |
michael@0 | 211 | else |
michael@0 | 212 | shortcut = 0; |
michael@0 | 213 | |
michael@0 | 214 | if (shortcut) { |
michael@0 | 215 | sz = -(x < 0); |
michael@0 | 216 | x -= 2 * sz + 1; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | /* Consider both possible successor states. */ |
michael@0 | 220 | if (!x) { |
michael@0 | 221 | /* If we reduced this coefficient to zero, check to see if |
michael@0 | 222 | * we need to move the EOB back here. |
michael@0 | 223 | */ |
michael@0 | 224 | t0 = tokens[next][0].token == DCT_EOB_TOKEN ? |
michael@0 | 225 | DCT_EOB_TOKEN : ZERO_TOKEN; |
michael@0 | 226 | t1 = tokens[next][1].token == DCT_EOB_TOKEN ? |
michael@0 | 227 | DCT_EOB_TOKEN : ZERO_TOKEN; |
michael@0 | 228 | } else { |
michael@0 | 229 | t0 = t1 = (vp9_dct_value_tokens_ptr + x)->token; |
michael@0 | 230 | } |
michael@0 | 231 | if (next < default_eob) { |
michael@0 | 232 | band = band_translate[i + 1]; |
michael@0 | 233 | if (t0 != DCT_EOB_TOKEN) { |
michael@0 | 234 | pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache); |
michael@0 | 235 | rate0 += mb->token_costs[tx_size][type][ref][band][!x][pt] |
michael@0 | 236 | [tokens[next][0].token]; |
michael@0 | 237 | } |
michael@0 | 238 | if (t1 != DCT_EOB_TOKEN) { |
michael@0 | 239 | pt = trellis_get_coeff_context(scan, nb, i, t1, token_cache); |
michael@0 | 240 | rate1 += mb->token_costs[tx_size][type][ref][band][!x][pt] |
michael@0 | 241 | [tokens[next][1].token]; |
michael@0 | 242 | } |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | UPDATE_RD_COST(); |
michael@0 | 246 | /* And pick the best. */ |
michael@0 | 247 | best = rd_cost1 < rd_cost0; |
michael@0 | 248 | base_bits = *(vp9_dct_value_cost_ptr + x); |
michael@0 | 249 | |
michael@0 | 250 | if (shortcut) { |
michael@0 | 251 | dx -= (dequant_ptr[rc != 0] + sz) ^ sz; |
michael@0 | 252 | d2 = dx * dx; |
michael@0 | 253 | } |
michael@0 | 254 | tokens[i][1].rate = base_bits + (best ? rate1 : rate0); |
michael@0 | 255 | tokens[i][1].error = d2 + (best ? error1 : error0); |
michael@0 | 256 | tokens[i][1].next = next; |
michael@0 | 257 | tokens[i][1].token = best ? t1 : t0; |
michael@0 | 258 | tokens[i][1].qc = x; |
michael@0 | 259 | best_index[i][1] = best; |
michael@0 | 260 | /* Finally, make this the new head of the trellis. */ |
michael@0 | 261 | next = i; |
michael@0 | 262 | } else { |
michael@0 | 263 | /* There's no choice to make for a zero coefficient, so we don't |
michael@0 | 264 | * add a new trellis node, but we do need to update the costs. |
michael@0 | 265 | */ |
michael@0 | 266 | band = band_translate[i + 1]; |
michael@0 | 267 | t0 = tokens[next][0].token; |
michael@0 | 268 | t1 = tokens[next][1].token; |
michael@0 | 269 | /* Update the cost of each path if we're past the EOB token. */ |
michael@0 | 270 | if (t0 != DCT_EOB_TOKEN) { |
michael@0 | 271 | tokens[next][0].rate += |
michael@0 | 272 | mb->token_costs[tx_size][type][ref][band][1][0][t0]; |
michael@0 | 273 | tokens[next][0].token = ZERO_TOKEN; |
michael@0 | 274 | } |
michael@0 | 275 | if (t1 != DCT_EOB_TOKEN) { |
michael@0 | 276 | tokens[next][1].rate += |
michael@0 | 277 | mb->token_costs[tx_size][type][ref][band][1][0][t1]; |
michael@0 | 278 | tokens[next][1].token = ZERO_TOKEN; |
michael@0 | 279 | } |
michael@0 | 280 | best_index[i][0] = best_index[i][1] = 0; |
michael@0 | 281 | /* Don't update next, because we didn't add a new node. */ |
michael@0 | 282 | } |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | /* Now pick the best path through the whole trellis. */ |
michael@0 | 286 | band = band_translate[i + 1]; |
michael@0 | 287 | pt = combine_entropy_contexts(*a, *l); |
michael@0 | 288 | rate0 = tokens[next][0].rate; |
michael@0 | 289 | rate1 = tokens[next][1].rate; |
michael@0 | 290 | error0 = tokens[next][0].error; |
michael@0 | 291 | error1 = tokens[next][1].error; |
michael@0 | 292 | t0 = tokens[next][0].token; |
michael@0 | 293 | t1 = tokens[next][1].token; |
michael@0 | 294 | rate0 += mb->token_costs[tx_size][type][ref][band][0][pt][t0]; |
michael@0 | 295 | rate1 += mb->token_costs[tx_size][type][ref][band][0][pt][t1]; |
michael@0 | 296 | UPDATE_RD_COST(); |
michael@0 | 297 | best = rd_cost1 < rd_cost0; |
michael@0 | 298 | final_eob = i0 - 1; |
michael@0 | 299 | vpx_memset(qcoeff_ptr, 0, sizeof(*qcoeff_ptr) * (16 << (tx_size * 2))); |
michael@0 | 300 | vpx_memset(dqcoeff_ptr, 0, sizeof(*dqcoeff_ptr) * (16 << (tx_size * 2))); |
michael@0 | 301 | for (i = next; i < eob; i = next) { |
michael@0 | 302 | x = tokens[i][best].qc; |
michael@0 | 303 | if (x) { |
michael@0 | 304 | final_eob = i; |
michael@0 | 305 | } |
michael@0 | 306 | rc = scan[i]; |
michael@0 | 307 | qcoeff_ptr[rc] = x; |
michael@0 | 308 | dqcoeff_ptr[rc] = (x * dequant_ptr[rc != 0]) / mul; |
michael@0 | 309 | |
michael@0 | 310 | next = tokens[i][best].next; |
michael@0 | 311 | best = best_index[i][best]; |
michael@0 | 312 | } |
michael@0 | 313 | final_eob++; |
michael@0 | 314 | |
michael@0 | 315 | xd->plane[plane].eobs[block] = final_eob; |
michael@0 | 316 | *a = *l = (final_eob > 0); |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | void vp9_optimize_b(int plane, int block, BLOCK_SIZE plane_bsize, |
michael@0 | 320 | TX_SIZE tx_size, MACROBLOCK *mb, struct optimize_ctx *ctx) { |
michael@0 | 321 | int x, y; |
michael@0 | 322 | txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &x, &y); |
michael@0 | 323 | optimize_b(mb, plane, block, plane_bsize, |
michael@0 | 324 | &ctx->ta[plane][x], &ctx->tl[plane][y], tx_size); |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | static void optimize_init_b(int plane, BLOCK_SIZE bsize, |
michael@0 | 328 | struct encode_b_args *args) { |
michael@0 | 329 | const MACROBLOCKD *xd = &args->x->e_mbd; |
michael@0 | 330 | const struct macroblockd_plane* const pd = &xd->plane[plane]; |
michael@0 | 331 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
michael@0 | 332 | const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize]; |
michael@0 | 333 | const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize]; |
michael@0 | 334 | const MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; |
michael@0 | 335 | const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi) : mbmi->tx_size; |
michael@0 | 336 | |
michael@0 | 337 | vp9_get_entropy_contexts(tx_size, args->ctx->ta[plane], args->ctx->tl[plane], |
michael@0 | 338 | pd->above_context, pd->left_context, |
michael@0 | 339 | num_4x4_w, num_4x4_h); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | void vp9_xform_quant(int plane, int block, BLOCK_SIZE plane_bsize, |
michael@0 | 343 | TX_SIZE tx_size, void *arg) { |
michael@0 | 344 | struct encode_b_args* const args = arg; |
michael@0 | 345 | MACROBLOCK* const x = args->x; |
michael@0 | 346 | MACROBLOCKD* const xd = &x->e_mbd; |
michael@0 | 347 | struct macroblock_plane *const p = &x->plane[plane]; |
michael@0 | 348 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
michael@0 | 349 | int16_t *coeff = BLOCK_OFFSET(p->coeff, block); |
michael@0 | 350 | int16_t *qcoeff = BLOCK_OFFSET(pd->qcoeff, block); |
michael@0 | 351 | int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
michael@0 | 352 | const int16_t *scan, *iscan; |
michael@0 | 353 | uint16_t *eob = &pd->eobs[block]; |
michael@0 | 354 | const int bwl = b_width_log2(plane_bsize), bw = 1 << bwl; |
michael@0 | 355 | const int twl = bwl - tx_size, twmask = (1 << twl) - 1; |
michael@0 | 356 | int xoff, yoff; |
michael@0 | 357 | int16_t *src_diff; |
michael@0 | 358 | |
michael@0 | 359 | switch (tx_size) { |
michael@0 | 360 | case TX_32X32: |
michael@0 | 361 | scan = vp9_default_scan_32x32; |
michael@0 | 362 | iscan = vp9_default_iscan_32x32; |
michael@0 | 363 | block >>= 6; |
michael@0 | 364 | xoff = 32 * (block & twmask); |
michael@0 | 365 | yoff = 32 * (block >> twl); |
michael@0 | 366 | src_diff = p->src_diff + 4 * bw * yoff + xoff; |
michael@0 | 367 | if (x->use_lp32x32fdct) |
michael@0 | 368 | vp9_fdct32x32_rd(src_diff, coeff, bw * 4); |
michael@0 | 369 | else |
michael@0 | 370 | vp9_fdct32x32(src_diff, coeff, bw * 4); |
michael@0 | 371 | vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round, |
michael@0 | 372 | p->quant, p->quant_shift, qcoeff, dqcoeff, |
michael@0 | 373 | pd->dequant, p->zbin_extra, eob, scan, iscan); |
michael@0 | 374 | break; |
michael@0 | 375 | case TX_16X16: |
michael@0 | 376 | scan = vp9_default_scan_16x16; |
michael@0 | 377 | iscan = vp9_default_iscan_16x16; |
michael@0 | 378 | block >>= 4; |
michael@0 | 379 | xoff = 16 * (block & twmask); |
michael@0 | 380 | yoff = 16 * (block >> twl); |
michael@0 | 381 | src_diff = p->src_diff + 4 * bw * yoff + xoff; |
michael@0 | 382 | vp9_fdct16x16(src_diff, coeff, bw * 4); |
michael@0 | 383 | vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round, |
michael@0 | 384 | p->quant, p->quant_shift, qcoeff, dqcoeff, |
michael@0 | 385 | pd->dequant, p->zbin_extra, eob, scan, iscan); |
michael@0 | 386 | break; |
michael@0 | 387 | case TX_8X8: |
michael@0 | 388 | scan = vp9_default_scan_8x8; |
michael@0 | 389 | iscan = vp9_default_iscan_8x8; |
michael@0 | 390 | block >>= 2; |
michael@0 | 391 | xoff = 8 * (block & twmask); |
michael@0 | 392 | yoff = 8 * (block >> twl); |
michael@0 | 393 | src_diff = p->src_diff + 4 * bw * yoff + xoff; |
michael@0 | 394 | vp9_fdct8x8(src_diff, coeff, bw * 4); |
michael@0 | 395 | vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, |
michael@0 | 396 | p->quant, p->quant_shift, qcoeff, dqcoeff, |
michael@0 | 397 | pd->dequant, p->zbin_extra, eob, scan, iscan); |
michael@0 | 398 | break; |
michael@0 | 399 | case TX_4X4: |
michael@0 | 400 | scan = vp9_default_scan_4x4; |
michael@0 | 401 | iscan = vp9_default_iscan_4x4; |
michael@0 | 402 | xoff = 4 * (block & twmask); |
michael@0 | 403 | yoff = 4 * (block >> twl); |
michael@0 | 404 | src_diff = p->src_diff + 4 * bw * yoff + xoff; |
michael@0 | 405 | x->fwd_txm4x4(src_diff, coeff, bw * 4); |
michael@0 | 406 | vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, |
michael@0 | 407 | p->quant, p->quant_shift, qcoeff, dqcoeff, |
michael@0 | 408 | pd->dequant, p->zbin_extra, eob, scan, iscan); |
michael@0 | 409 | break; |
michael@0 | 410 | default: |
michael@0 | 411 | assert(0); |
michael@0 | 412 | } |
michael@0 | 413 | } |
michael@0 | 414 | |
michael@0 | 415 | static void encode_block(int plane, int block, BLOCK_SIZE plane_bsize, |
michael@0 | 416 | TX_SIZE tx_size, void *arg) { |
michael@0 | 417 | struct encode_b_args *const args = arg; |
michael@0 | 418 | MACROBLOCK *const x = args->x; |
michael@0 | 419 | MACROBLOCKD *const xd = &x->e_mbd; |
michael@0 | 420 | struct optimize_ctx *const ctx = args->ctx; |
michael@0 | 421 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
michael@0 | 422 | int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
michael@0 | 423 | int i, j; |
michael@0 | 424 | uint8_t *dst; |
michael@0 | 425 | txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j); |
michael@0 | 426 | dst = &pd->dst.buf[4 * j * pd->dst.stride + 4 * i]; |
michael@0 | 427 | |
michael@0 | 428 | // TODO(jingning): per transformed block zero forcing only enabled for |
michael@0 | 429 | // luma component. will integrate chroma components as well. |
michael@0 | 430 | if (x->zcoeff_blk[tx_size][block] && plane == 0) { |
michael@0 | 431 | pd->eobs[block] = 0; |
michael@0 | 432 | ctx->ta[plane][i] = 0; |
michael@0 | 433 | ctx->tl[plane][j] = 0; |
michael@0 | 434 | return; |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | if (!x->skip_recode) |
michael@0 | 438 | vp9_xform_quant(plane, block, plane_bsize, tx_size, arg); |
michael@0 | 439 | |
michael@0 | 440 | if (x->optimize && (!x->skip_recode || !x->skip_optimize)) { |
michael@0 | 441 | vp9_optimize_b(plane, block, plane_bsize, tx_size, x, ctx); |
michael@0 | 442 | } else { |
michael@0 | 443 | ctx->ta[plane][i] = pd->eobs[block] > 0; |
michael@0 | 444 | ctx->tl[plane][j] = pd->eobs[block] > 0; |
michael@0 | 445 | } |
michael@0 | 446 | |
michael@0 | 447 | if (x->skip_encode || pd->eobs[block] == 0) |
michael@0 | 448 | return; |
michael@0 | 449 | |
michael@0 | 450 | switch (tx_size) { |
michael@0 | 451 | case TX_32X32: |
michael@0 | 452 | vp9_idct32x32_add(dqcoeff, dst, pd->dst.stride, pd->eobs[block]); |
michael@0 | 453 | break; |
michael@0 | 454 | case TX_16X16: |
michael@0 | 455 | vp9_idct16x16_add(dqcoeff, dst, pd->dst.stride, pd->eobs[block]); |
michael@0 | 456 | break; |
michael@0 | 457 | case TX_8X8: |
michael@0 | 458 | vp9_idct8x8_add(dqcoeff, dst, pd->dst.stride, pd->eobs[block]); |
michael@0 | 459 | break; |
michael@0 | 460 | case TX_4X4: |
michael@0 | 461 | // this is like vp9_short_idct4x4 but has a special case around eob<=1 |
michael@0 | 462 | // which is significant (not just an optimization) for the lossless |
michael@0 | 463 | // case. |
michael@0 | 464 | xd->itxm_add(dqcoeff, dst, pd->dst.stride, pd->eobs[block]); |
michael@0 | 465 | break; |
michael@0 | 466 | default: |
michael@0 | 467 | assert(!"Invalid transform size"); |
michael@0 | 468 | } |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | static void encode_block_pass1(int plane, int block, BLOCK_SIZE plane_bsize, |
michael@0 | 472 | TX_SIZE tx_size, void *arg) { |
michael@0 | 473 | struct encode_b_args *const args = arg; |
michael@0 | 474 | MACROBLOCK *const x = args->x; |
michael@0 | 475 | MACROBLOCKD *const xd = &x->e_mbd; |
michael@0 | 476 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
michael@0 | 477 | const int raster_block = txfrm_block_to_raster_block(plane_bsize, tx_size, |
michael@0 | 478 | block); |
michael@0 | 479 | |
michael@0 | 480 | int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
michael@0 | 481 | uint8_t *const dst = raster_block_offset_uint8(plane_bsize, raster_block, |
michael@0 | 482 | pd->dst.buf, pd->dst.stride); |
michael@0 | 483 | |
michael@0 | 484 | vp9_xform_quant(plane, block, plane_bsize, tx_size, arg); |
michael@0 | 485 | |
michael@0 | 486 | if (pd->eobs[block] == 0) |
michael@0 | 487 | return; |
michael@0 | 488 | |
michael@0 | 489 | xd->itxm_add(dqcoeff, dst, pd->dst.stride, pd->eobs[block]); |
michael@0 | 490 | } |
michael@0 | 491 | |
michael@0 | 492 | void vp9_encode_sby(MACROBLOCK *x, BLOCK_SIZE bsize) { |
michael@0 | 493 | MACROBLOCKD *const xd = &x->e_mbd; |
michael@0 | 494 | struct optimize_ctx ctx; |
michael@0 | 495 | struct encode_b_args arg = {x, &ctx}; |
michael@0 | 496 | |
michael@0 | 497 | vp9_subtract_sby(x, bsize); |
michael@0 | 498 | if (x->optimize) |
michael@0 | 499 | optimize_init_b(0, bsize, &arg); |
michael@0 | 500 | |
michael@0 | 501 | foreach_transformed_block_in_plane(xd, bsize, 0, encode_block_pass1, &arg); |
michael@0 | 502 | } |
michael@0 | 503 | |
michael@0 | 504 | void vp9_encode_sb(MACROBLOCK *x, BLOCK_SIZE bsize) { |
michael@0 | 505 | MACROBLOCKD *const xd = &x->e_mbd; |
michael@0 | 506 | struct optimize_ctx ctx; |
michael@0 | 507 | struct encode_b_args arg = {x, &ctx}; |
michael@0 | 508 | |
michael@0 | 509 | if (!x->skip_recode) |
michael@0 | 510 | vp9_subtract_sb(x, bsize); |
michael@0 | 511 | |
michael@0 | 512 | if (x->optimize && (!x->skip_recode || !x->skip_optimize)) { |
michael@0 | 513 | int i; |
michael@0 | 514 | for (i = 0; i < MAX_MB_PLANE; ++i) |
michael@0 | 515 | optimize_init_b(i, bsize, &arg); |
michael@0 | 516 | } |
michael@0 | 517 | |
michael@0 | 518 | foreach_transformed_block(xd, bsize, encode_block, &arg); |
michael@0 | 519 | } |
michael@0 | 520 | |
michael@0 | 521 | void vp9_encode_block_intra(int plane, int block, BLOCK_SIZE plane_bsize, |
michael@0 | 522 | TX_SIZE tx_size, void *arg) { |
michael@0 | 523 | struct encode_b_args* const args = arg; |
michael@0 | 524 | MACROBLOCK *const x = args->x; |
michael@0 | 525 | MACROBLOCKD *const xd = &x->e_mbd; |
michael@0 | 526 | MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; |
michael@0 | 527 | struct macroblock_plane *const p = &x->plane[plane]; |
michael@0 | 528 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
michael@0 | 529 | int16_t *coeff = BLOCK_OFFSET(p->coeff, block); |
michael@0 | 530 | int16_t *qcoeff = BLOCK_OFFSET(pd->qcoeff, block); |
michael@0 | 531 | int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
michael@0 | 532 | const int16_t *scan, *iscan; |
michael@0 | 533 | TX_TYPE tx_type; |
michael@0 | 534 | MB_PREDICTION_MODE mode; |
michael@0 | 535 | const int bwl = b_width_log2(plane_bsize), bw = 1 << bwl; |
michael@0 | 536 | const int twl = bwl - tx_size, twmask = (1 << twl) - 1; |
michael@0 | 537 | int xoff, yoff; |
michael@0 | 538 | uint8_t *src, *dst; |
michael@0 | 539 | int16_t *src_diff; |
michael@0 | 540 | uint16_t *eob = &pd->eobs[block]; |
michael@0 | 541 | |
michael@0 | 542 | if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0) |
michael@0 | 543 | extend_for_intra(xd, plane_bsize, plane, block, tx_size); |
michael@0 | 544 | |
michael@0 | 545 | // if (x->optimize) |
michael@0 | 546 | // vp9_optimize_b(plane, block, plane_bsize, tx_size, x, args->ctx); |
michael@0 | 547 | |
michael@0 | 548 | switch (tx_size) { |
michael@0 | 549 | case TX_32X32: |
michael@0 | 550 | scan = vp9_default_scan_32x32; |
michael@0 | 551 | iscan = vp9_default_iscan_32x32; |
michael@0 | 552 | mode = plane == 0 ? mbmi->mode : mbmi->uv_mode; |
michael@0 | 553 | block >>= 6; |
michael@0 | 554 | xoff = 32 * (block & twmask); |
michael@0 | 555 | yoff = 32 * (block >> twl); |
michael@0 | 556 | dst = pd->dst.buf + yoff * pd->dst.stride + xoff; |
michael@0 | 557 | vp9_predict_intra_block(xd, block, bwl, TX_32X32, mode, |
michael@0 | 558 | dst, pd->dst.stride, dst, pd->dst.stride); |
michael@0 | 559 | |
michael@0 | 560 | if (!x->skip_recode) { |
michael@0 | 561 | src = p->src.buf + yoff * p->src.stride + xoff; |
michael@0 | 562 | src_diff = p->src_diff + 4 * bw * yoff + xoff; |
michael@0 | 563 | vp9_subtract_block(32, 32, src_diff, bw * 4, |
michael@0 | 564 | src, p->src.stride, dst, pd->dst.stride); |
michael@0 | 565 | if (x->use_lp32x32fdct) |
michael@0 | 566 | vp9_fdct32x32_rd(src_diff, coeff, bw * 4); |
michael@0 | 567 | else |
michael@0 | 568 | vp9_fdct32x32(src_diff, coeff, bw * 4); |
michael@0 | 569 | vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round, |
michael@0 | 570 | p->quant, p->quant_shift, qcoeff, dqcoeff, |
michael@0 | 571 | pd->dequant, p->zbin_extra, eob, scan, iscan); |
michael@0 | 572 | } |
michael@0 | 573 | if (!x->skip_encode && *eob) |
michael@0 | 574 | vp9_idct32x32_add(dqcoeff, dst, pd->dst.stride, *eob); |
michael@0 | 575 | break; |
michael@0 | 576 | case TX_16X16: |
michael@0 | 577 | tx_type = get_tx_type_16x16(pd->plane_type, xd); |
michael@0 | 578 | scan = get_scan_16x16(tx_type); |
michael@0 | 579 | iscan = get_iscan_16x16(tx_type); |
michael@0 | 580 | mode = plane == 0 ? mbmi->mode : mbmi->uv_mode; |
michael@0 | 581 | block >>= 4; |
michael@0 | 582 | xoff = 16 * (block & twmask); |
michael@0 | 583 | yoff = 16 * (block >> twl); |
michael@0 | 584 | dst = pd->dst.buf + yoff * pd->dst.stride + xoff; |
michael@0 | 585 | vp9_predict_intra_block(xd, block, bwl, TX_16X16, mode, |
michael@0 | 586 | dst, pd->dst.stride, dst, pd->dst.stride); |
michael@0 | 587 | if (!x->skip_recode) { |
michael@0 | 588 | src = p->src.buf + yoff * p->src.stride + xoff; |
michael@0 | 589 | src_diff = p->src_diff + 4 * bw * yoff + xoff; |
michael@0 | 590 | vp9_subtract_block(16, 16, src_diff, bw * 4, |
michael@0 | 591 | src, p->src.stride, dst, pd->dst.stride); |
michael@0 | 592 | vp9_fht16x16(tx_type, src_diff, coeff, bw * 4); |
michael@0 | 593 | vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round, |
michael@0 | 594 | p->quant, p->quant_shift, qcoeff, dqcoeff, |
michael@0 | 595 | pd->dequant, p->zbin_extra, eob, scan, iscan); |
michael@0 | 596 | } |
michael@0 | 597 | if (!x->skip_encode && *eob) |
michael@0 | 598 | vp9_iht16x16_add(tx_type, dqcoeff, dst, pd->dst.stride, *eob); |
michael@0 | 599 | break; |
michael@0 | 600 | case TX_8X8: |
michael@0 | 601 | tx_type = get_tx_type_8x8(pd->plane_type, xd); |
michael@0 | 602 | scan = get_scan_8x8(tx_type); |
michael@0 | 603 | iscan = get_iscan_8x8(tx_type); |
michael@0 | 604 | mode = plane == 0 ? mbmi->mode : mbmi->uv_mode; |
michael@0 | 605 | block >>= 2; |
michael@0 | 606 | xoff = 8 * (block & twmask); |
michael@0 | 607 | yoff = 8 * (block >> twl); |
michael@0 | 608 | dst = pd->dst.buf + yoff * pd->dst.stride + xoff; |
michael@0 | 609 | vp9_predict_intra_block(xd, block, bwl, TX_8X8, mode, |
michael@0 | 610 | dst, pd->dst.stride, dst, pd->dst.stride); |
michael@0 | 611 | if (!x->skip_recode) { |
michael@0 | 612 | src = p->src.buf + yoff * p->src.stride + xoff; |
michael@0 | 613 | src_diff = p->src_diff + 4 * bw * yoff + xoff; |
michael@0 | 614 | vp9_subtract_block(8, 8, src_diff, bw * 4, |
michael@0 | 615 | src, p->src.stride, dst, pd->dst.stride); |
michael@0 | 616 | vp9_fht8x8(tx_type, src_diff, coeff, bw * 4); |
michael@0 | 617 | vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, p->quant, |
michael@0 | 618 | p->quant_shift, qcoeff, dqcoeff, |
michael@0 | 619 | pd->dequant, p->zbin_extra, eob, scan, iscan); |
michael@0 | 620 | } |
michael@0 | 621 | if (!x->skip_encode && *eob) |
michael@0 | 622 | vp9_iht8x8_add(tx_type, dqcoeff, dst, pd->dst.stride, *eob); |
michael@0 | 623 | break; |
michael@0 | 624 | case TX_4X4: |
michael@0 | 625 | tx_type = get_tx_type_4x4(pd->plane_type, xd, block); |
michael@0 | 626 | scan = get_scan_4x4(tx_type); |
michael@0 | 627 | iscan = get_iscan_4x4(tx_type); |
michael@0 | 628 | if (mbmi->sb_type < BLOCK_8X8 && plane == 0) |
michael@0 | 629 | mode = xd->mi_8x8[0]->bmi[block].as_mode; |
michael@0 | 630 | else |
michael@0 | 631 | mode = plane == 0 ? mbmi->mode : mbmi->uv_mode; |
michael@0 | 632 | |
michael@0 | 633 | xoff = 4 * (block & twmask); |
michael@0 | 634 | yoff = 4 * (block >> twl); |
michael@0 | 635 | dst = pd->dst.buf + yoff * pd->dst.stride + xoff; |
michael@0 | 636 | vp9_predict_intra_block(xd, block, bwl, TX_4X4, mode, |
michael@0 | 637 | dst, pd->dst.stride, dst, pd->dst.stride); |
michael@0 | 638 | |
michael@0 | 639 | if (!x->skip_recode) { |
michael@0 | 640 | src = p->src.buf + yoff * p->src.stride + xoff; |
michael@0 | 641 | src_diff = p->src_diff + 4 * bw * yoff + xoff; |
michael@0 | 642 | vp9_subtract_block(4, 4, src_diff, bw * 4, |
michael@0 | 643 | src, p->src.stride, dst, pd->dst.stride); |
michael@0 | 644 | if (tx_type != DCT_DCT) |
michael@0 | 645 | vp9_short_fht4x4(src_diff, coeff, bw * 4, tx_type); |
michael@0 | 646 | else |
michael@0 | 647 | x->fwd_txm4x4(src_diff, coeff, bw * 4); |
michael@0 | 648 | vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant, |
michael@0 | 649 | p->quant_shift, qcoeff, dqcoeff, |
michael@0 | 650 | pd->dequant, p->zbin_extra, eob, scan, iscan); |
michael@0 | 651 | } |
michael@0 | 652 | |
michael@0 | 653 | if (!x->skip_encode && *eob) { |
michael@0 | 654 | if (tx_type == DCT_DCT) |
michael@0 | 655 | // this is like vp9_short_idct4x4 but has a special case around eob<=1 |
michael@0 | 656 | // which is significant (not just an optimization) for the lossless |
michael@0 | 657 | // case. |
michael@0 | 658 | xd->itxm_add(dqcoeff, dst, pd->dst.stride, *eob); |
michael@0 | 659 | else |
michael@0 | 660 | vp9_iht4x4_16_add(dqcoeff, dst, pd->dst.stride, tx_type); |
michael@0 | 661 | } |
michael@0 | 662 | break; |
michael@0 | 663 | default: |
michael@0 | 664 | assert(0); |
michael@0 | 665 | } |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | void vp9_encode_intra_block_y(MACROBLOCK *x, BLOCK_SIZE bsize) { |
michael@0 | 669 | MACROBLOCKD* const xd = &x->e_mbd; |
michael@0 | 670 | struct optimize_ctx ctx; |
michael@0 | 671 | struct encode_b_args arg = {x, &ctx}; |
michael@0 | 672 | |
michael@0 | 673 | foreach_transformed_block_in_plane(xd, bsize, 0, vp9_encode_block_intra, |
michael@0 | 674 | &arg); |
michael@0 | 675 | } |
michael@0 | 676 | void vp9_encode_intra_block_uv(MACROBLOCK *x, BLOCK_SIZE bsize) { |
michael@0 | 677 | MACROBLOCKD* const xd = &x->e_mbd; |
michael@0 | 678 | struct optimize_ctx ctx; |
michael@0 | 679 | struct encode_b_args arg = {x, &ctx}; |
michael@0 | 680 | foreach_transformed_block_uv(xd, bsize, vp9_encode_block_intra, &arg); |
michael@0 | 681 | } |
michael@0 | 682 |