Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1286)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_rdopt.c

Issue 375983002: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_rdopt.h ('k') | source/libvpx/vp9/encoder/vp9_segmentation.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <assert.h> 11 #include <assert.h>
12 #include <math.h> 12 #include <math.h>
13 #include <stdio.h>
14 13
15 #include "./vp9_rtcd.h" 14 #include "./vp9_rtcd.h"
16 15
17 #include "vpx_mem/vpx_mem.h" 16 #include "vpx_mem/vpx_mem.h"
18 17
19 #include "vp9/common/vp9_common.h" 18 #include "vp9/common/vp9_common.h"
20 #include "vp9/common/vp9_entropy.h" 19 #include "vp9/common/vp9_entropy.h"
21 #include "vp9/common/vp9_entropymode.h" 20 #include "vp9/common/vp9_entropymode.h"
22 #include "vp9/common/vp9_idct.h" 21 #include "vp9/common/vp9_idct.h"
23 #include "vp9/common/vp9_mvref_common.h" 22 #include "vp9/common/vp9_mvref_common.h"
24 #include "vp9/common/vp9_pred_common.h" 23 #include "vp9/common/vp9_pred_common.h"
25 #include "vp9/common/vp9_quant_common.h" 24 #include "vp9/common/vp9_quant_common.h"
26 #include "vp9/common/vp9_reconinter.h" 25 #include "vp9/common/vp9_reconinter.h"
27 #include "vp9/common/vp9_reconintra.h" 26 #include "vp9/common/vp9_reconintra.h"
28 #include "vp9/common/vp9_seg_common.h" 27 #include "vp9/common/vp9_seg_common.h"
29 #include "vp9/common/vp9_systemdependent.h" 28 #include "vp9/common/vp9_systemdependent.h"
30 29
31 #include "vp9/encoder/vp9_cost.h" 30 #include "vp9/encoder/vp9_cost.h"
32 #include "vp9/encoder/vp9_encodemb.h" 31 #include "vp9/encoder/vp9_encodemb.h"
33 #include "vp9/encoder/vp9_encodemv.h" 32 #include "vp9/encoder/vp9_encodemv.h"
34 #include "vp9/encoder/vp9_encoder.h" 33 #include "vp9/encoder/vp9_encoder.h"
35 #include "vp9/encoder/vp9_mcomp.h" 34 #include "vp9/encoder/vp9_mcomp.h"
36 #include "vp9/encoder/vp9_quantize.h" 35 #include "vp9/encoder/vp9_quantize.h"
37 #include "vp9/encoder/vp9_ratectrl.h" 36 #include "vp9/encoder/vp9_ratectrl.h"
37 #include "vp9/encoder/vp9_rd.h"
38 #include "vp9/encoder/vp9_rdopt.h" 38 #include "vp9/encoder/vp9_rdopt.h"
39 #include "vp9/encoder/vp9_tokenize.h"
40 #include "vp9/encoder/vp9_variance.h" 39 #include "vp9/encoder/vp9_variance.h"
41 40
42 #define RD_THRESH_MAX_FACT 64 41 #define RD_THRESH_MAX_FACT 64
43 #define RD_THRESH_INC 1 42 #define RD_THRESH_INC 1
44 #define RD_THRESH_POW 1.25
45 #define RD_MULT_EPB_RATIO 64
46
47 /* Factor to weigh the rate for switchable interp filters */
48 #define SWITCHABLE_INTERP_RATE_FACTOR 1
49 43
50 #define LAST_FRAME_MODE_MASK 0xFFEDCD60 44 #define LAST_FRAME_MODE_MASK 0xFFEDCD60
51 #define GOLDEN_FRAME_MODE_MASK 0xFFDA3BB0 45 #define GOLDEN_FRAME_MODE_MASK 0xFFDA3BB0
52 #define ALT_REF_MODE_MASK 0xFFC648D0 46 #define ALT_REF_MODE_MASK 0xFFC648D0
53 47
54 #define MIN_EARLY_TERM_INDEX 3 48 #define MIN_EARLY_TERM_INDEX 3
55 49
56 typedef struct { 50 typedef struct {
57 PREDICTION_MODE mode; 51 PREDICTION_MODE mode;
58 MV_REFERENCE_FRAME ref_frame[2]; 52 MV_REFERENCE_FRAME ref_frame[2];
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 115
122 static const REF_DEFINITION vp9_ref_order[MAX_REFS] = { 116 static const REF_DEFINITION vp9_ref_order[MAX_REFS] = {
123 {{LAST_FRAME, NONE}}, 117 {{LAST_FRAME, NONE}},
124 {{GOLDEN_FRAME, NONE}}, 118 {{GOLDEN_FRAME, NONE}},
125 {{ALTREF_FRAME, NONE}}, 119 {{ALTREF_FRAME, NONE}},
126 {{LAST_FRAME, ALTREF_FRAME}}, 120 {{LAST_FRAME, ALTREF_FRAME}},
127 {{GOLDEN_FRAME, ALTREF_FRAME}}, 121 {{GOLDEN_FRAME, ALTREF_FRAME}},
128 {{INTRA_FRAME, NONE}}, 122 {{INTRA_FRAME, NONE}},
129 }; 123 };
130 124
131 // The baseline rd thresholds for breaking out of the rd loop for
132 // certain modes are assumed to be based on 8x8 blocks.
133 // This table is used to correct for blocks size.
134 // The factors here are << 2 (2 = x0.5, 32 = x8 etc).
135 static const uint8_t rd_thresh_block_size_factor[BLOCK_SIZES] = {
136 2, 3, 3, 4, 6, 6, 8, 12, 12, 16, 24, 24, 32
137 };
138
139 static int raster_block_offset(BLOCK_SIZE plane_bsize, 125 static int raster_block_offset(BLOCK_SIZE plane_bsize,
140 int raster_block, int stride) { 126 int raster_block, int stride) {
141 const int bw = b_width_log2(plane_bsize); 127 const int bw = b_width_log2(plane_bsize);
142 const int y = 4 * (raster_block >> bw); 128 const int y = 4 * (raster_block >> bw);
143 const int x = 4 * (raster_block & ((1 << bw) - 1)); 129 const int x = 4 * (raster_block & ((1 << bw) - 1));
144 return y * stride + x; 130 return y * stride + x;
145 } 131 }
146 static int16_t* raster_block_offset_int16(BLOCK_SIZE plane_bsize, 132 static int16_t* raster_block_offset_int16(BLOCK_SIZE plane_bsize,
147 int raster_block, int16_t *base) { 133 int raster_block, int16_t *base) {
148 const int stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize]; 134 const int stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
149 return base + raster_block_offset(plane_bsize, raster_block, stride); 135 return base + raster_block_offset(plane_bsize, raster_block, stride);
150 } 136 }
151 137
152 static void fill_mode_costs(VP9_COMP *cpi) {
153 const FRAME_CONTEXT *const fc = &cpi->common.fc;
154 int i, j;
155
156 for (i = 0; i < INTRA_MODES; i++)
157 for (j = 0; j < INTRA_MODES; j++)
158 vp9_cost_tokens(cpi->y_mode_costs[i][j], vp9_kf_y_mode_prob[i][j],
159 vp9_intra_mode_tree);
160
161 // TODO(rbultje) separate tables for superblock costing?
162 vp9_cost_tokens(cpi->mbmode_cost, fc->y_mode_prob[1], vp9_intra_mode_tree);
163 vp9_cost_tokens(cpi->intra_uv_mode_cost[KEY_FRAME],
164 vp9_kf_uv_mode_prob[TM_PRED], vp9_intra_mode_tree);
165 vp9_cost_tokens(cpi->intra_uv_mode_cost[INTER_FRAME],
166 fc->uv_mode_prob[TM_PRED], vp9_intra_mode_tree);
167
168 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
169 vp9_cost_tokens(cpi->switchable_interp_costs[i],
170 fc->switchable_interp_prob[i], vp9_switchable_interp_tree);
171 }
172
173 static void fill_token_costs(vp9_coeff_cost *c,
174 vp9_coeff_probs_model (*p)[PLANE_TYPES]) {
175 int i, j, k, l;
176 TX_SIZE t;
177 for (t = TX_4X4; t <= TX_32X32; ++t)
178 for (i = 0; i < PLANE_TYPES; ++i)
179 for (j = 0; j < REF_TYPES; ++j)
180 for (k = 0; k < COEF_BANDS; ++k)
181 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
182 vp9_prob probs[ENTROPY_NODES];
183 vp9_model_to_full_probs(p[t][i][j][k][l], probs);
184 vp9_cost_tokens((int *)c[t][i][j][k][0][l], probs,
185 vp9_coef_tree);
186 vp9_cost_tokens_skip((int *)c[t][i][j][k][1][l], probs,
187 vp9_coef_tree);
188 assert(c[t][i][j][k][0][l][EOB_TOKEN] ==
189 c[t][i][j][k][1][l][EOB_TOKEN]);
190 }
191 }
192
193 static const uint8_t rd_iifactor[32] = {
194 4, 4, 3, 2, 1, 0, 0, 0,
195 0, 0, 0, 0, 0, 0, 0, 0,
196 0, 0, 0, 0, 0, 0, 0, 0,
197 0, 0, 0, 0, 0, 0, 0, 0,
198 };
199
200 // 3* dc_qlookup[Q]*dc_qlookup[Q];
201
202 /* values are now correlated to quantizer */
203 static int sad_per_bit16lut[QINDEX_RANGE];
204 static int sad_per_bit4lut[QINDEX_RANGE];
205
206 void vp9_init_me_luts() {
207 int i;
208
209 // Initialize the sad lut tables using a formulaic calculation for now
210 // This is to make it easier to resolve the impact of experimental changes
211 // to the quantizer tables.
212 for (i = 0; i < QINDEX_RANGE; i++) {
213 const double q = vp9_convert_qindex_to_q(i);
214 sad_per_bit16lut[i] = (int)(0.0418 * q + 2.4107);
215 sad_per_bit4lut[i] = (int)(0.063 * q + 2.742);
216 }
217 }
218
219 int vp9_compute_rd_mult(const VP9_COMP *cpi, int qindex) {
220 const int q = vp9_dc_quant(qindex, 0);
221 // TODO(debargha): Adjust the function below
222 int rdmult = 88 * q * q / 25;
223 if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME)) {
224 if (cpi->twopass.next_iiratio > 31)
225 rdmult += (rdmult * rd_iifactor[31]) >> 4;
226 else
227 rdmult += (rdmult * rd_iifactor[cpi->twopass.next_iiratio]) >> 4;
228 }
229 return rdmult;
230 }
231
232 static int compute_rd_thresh_factor(int qindex) {
233 // TODO(debargha): Adjust the function below
234 const int q = (int)(pow(vp9_dc_quant(qindex, 0) / 4.0, RD_THRESH_POW) * 5.12);
235 return MAX(q, 8);
236 }
237
238 void vp9_initialize_me_consts(VP9_COMP *cpi, int qindex) {
239 cpi->mb.sadperbit16 = sad_per_bit16lut[qindex];
240 cpi->mb.sadperbit4 = sad_per_bit4lut[qindex];
241 }
242
243 static void swap_block_ptr(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, 138 static void swap_block_ptr(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx,
244 int m, int n, int min_plane, int max_plane) { 139 int m, int n, int min_plane, int max_plane) {
245 int i; 140 int i;
246 141
247 for (i = min_plane; i < max_plane; ++i) { 142 for (i = min_plane; i < max_plane; ++i) {
248 struct macroblock_plane *const p = &x->plane[i]; 143 struct macroblock_plane *const p = &x->plane[i];
249 struct macroblockd_plane *const pd = &x->e_mbd.plane[i]; 144 struct macroblockd_plane *const pd = &x->e_mbd.plane[i];
250 145
251 p->coeff = ctx->coeff_pbuf[i][m]; 146 p->coeff = ctx->coeff_pbuf[i][m];
252 p->qcoeff = ctx->qcoeff_pbuf[i][m]; 147 p->qcoeff = ctx->qcoeff_pbuf[i][m];
253 pd->dqcoeff = ctx->dqcoeff_pbuf[i][m]; 148 pd->dqcoeff = ctx->dqcoeff_pbuf[i][m];
254 p->eobs = ctx->eobs_pbuf[i][m]; 149 p->eobs = ctx->eobs_pbuf[i][m];
255 150
256 ctx->coeff_pbuf[i][m] = ctx->coeff_pbuf[i][n]; 151 ctx->coeff_pbuf[i][m] = ctx->coeff_pbuf[i][n];
257 ctx->qcoeff_pbuf[i][m] = ctx->qcoeff_pbuf[i][n]; 152 ctx->qcoeff_pbuf[i][m] = ctx->qcoeff_pbuf[i][n];
258 ctx->dqcoeff_pbuf[i][m] = ctx->dqcoeff_pbuf[i][n]; 153 ctx->dqcoeff_pbuf[i][m] = ctx->dqcoeff_pbuf[i][n];
259 ctx->eobs_pbuf[i][m] = ctx->eobs_pbuf[i][n]; 154 ctx->eobs_pbuf[i][m] = ctx->eobs_pbuf[i][n];
260 155
261 ctx->coeff_pbuf[i][n] = p->coeff; 156 ctx->coeff_pbuf[i][n] = p->coeff;
262 ctx->qcoeff_pbuf[i][n] = p->qcoeff; 157 ctx->qcoeff_pbuf[i][n] = p->qcoeff;
263 ctx->dqcoeff_pbuf[i][n] = pd->dqcoeff; 158 ctx->dqcoeff_pbuf[i][n] = pd->dqcoeff;
264 ctx->eobs_pbuf[i][n] = p->eobs; 159 ctx->eobs_pbuf[i][n] = p->eobs;
265 } 160 }
266 } 161 }
267 162
268 static void set_block_thresholds(const VP9_COMMON *cm, RD_OPT *rd) {
269 int i, bsize, segment_id;
270
271 for (segment_id = 0; segment_id < MAX_SEGMENTS; ++segment_id) {
272 const int qindex = clamp(vp9_get_qindex(&cm->seg, segment_id,
273 cm->base_qindex) + cm->y_dc_delta_q,
274 0, MAXQ);
275 const int q = compute_rd_thresh_factor(qindex);
276
277 for (bsize = 0; bsize < BLOCK_SIZES; ++bsize) {
278 // Threshold here seems unnecessarily harsh but fine given actual
279 // range of values used for cpi->sf.thresh_mult[].
280 const int t = q * rd_thresh_block_size_factor[bsize];
281 const int thresh_max = INT_MAX / t;
282
283 if (bsize >= BLOCK_8X8) {
284 for (i = 0; i < MAX_MODES; ++i)
285 rd->threshes[segment_id][bsize][i] =
286 rd->thresh_mult[i] < thresh_max
287 ? rd->thresh_mult[i] * t / 4
288 : INT_MAX;
289 } else {
290 for (i = 0; i < MAX_REFS; ++i)
291 rd->threshes[segment_id][bsize][i] =
292 rd->thresh_mult_sub8x8[i] < thresh_max
293 ? rd->thresh_mult_sub8x8[i] * t / 4
294 : INT_MAX;
295 }
296 }
297 }
298 }
299
300 void vp9_initialize_rd_consts(VP9_COMP *cpi) {
301 VP9_COMMON *const cm = &cpi->common;
302 MACROBLOCK *const x = &cpi->mb;
303 RD_OPT *const rd = &cpi->rd;
304 int i;
305
306 vp9_clear_system_state();
307
308 rd->RDDIV = RDDIV_BITS; // in bits (to multiply D by 128)
309 rd->RDMULT = vp9_compute_rd_mult(cpi, cm->base_qindex + cm->y_dc_delta_q);
310
311 x->errorperbit = rd->RDMULT / RD_MULT_EPB_RATIO;
312 x->errorperbit += (x->errorperbit == 0);
313
314 x->select_tx_size = (cpi->sf.tx_size_search_method == USE_LARGESTALL &&
315 cm->frame_type != KEY_FRAME) ? 0 : 1;
316
317 set_block_thresholds(cm, rd);
318
319 if (!cpi->sf.use_nonrd_pick_mode || cm->frame_type == KEY_FRAME) {
320 fill_token_costs(x->token_costs, cm->fc.coef_probs);
321
322 for (i = 0; i < PARTITION_CONTEXTS; i++)
323 vp9_cost_tokens(cpi->partition_cost[i], get_partition_probs(cm, i),
324 vp9_partition_tree);
325 }
326
327 if (!cpi->sf.use_nonrd_pick_mode || (cm->current_video_frame & 0x07) == 1 ||
328 cm->frame_type == KEY_FRAME) {
329 fill_mode_costs(cpi);
330
331 if (!frame_is_intra_only(cm)) {
332 vp9_build_nmv_cost_table(x->nmvjointcost,
333 cm->allow_high_precision_mv ? x->nmvcost_hp
334 : x->nmvcost,
335 &cm->fc.nmvc, cm->allow_high_precision_mv);
336
337 for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
338 vp9_cost_tokens((int *)cpi->inter_mode_cost[i],
339 cm->fc.inter_mode_probs[i], vp9_inter_mode_tree);
340 }
341 }
342 }
343
344 static const int MAX_XSQ_Q10 = 245727;
345
346 static void model_rd_norm(int xsq_q10, int *r_q10, int *d_q10) {
347 // NOTE: The tables below must be of the same size
348
349 // The functions described below are sampled at the four most significant
350 // bits of x^2 + 8 / 256
351
352 // Normalized rate
353 // This table models the rate for a Laplacian source
354 // source with given variance when quantized with a uniform quantizer
355 // with given stepsize. The closed form expression is:
356 // Rn(x) = H(sqrt(r)) + sqrt(r)*[1 + H(r)/(1 - r)],
357 // where r = exp(-sqrt(2) * x) and x = qpstep / sqrt(variance),
358 // and H(x) is the binary entropy function.
359 static const int rate_tab_q10[] = {
360 65536, 6086, 5574, 5275, 5063, 4899, 4764, 4651,
361 4553, 4389, 4255, 4142, 4044, 3958, 3881, 3811,
362 3748, 3635, 3538, 3453, 3376, 3307, 3244, 3186,
363 3133, 3037, 2952, 2877, 2809, 2747, 2690, 2638,
364 2589, 2501, 2423, 2353, 2290, 2232, 2179, 2130,
365 2084, 2001, 1928, 1862, 1802, 1748, 1698, 1651,
366 1608, 1530, 1460, 1398, 1342, 1290, 1243, 1199,
367 1159, 1086, 1021, 963, 911, 864, 821, 781,
368 745, 680, 623, 574, 530, 490, 455, 424,
369 395, 345, 304, 269, 239, 213, 190, 171,
370 154, 126, 104, 87, 73, 61, 52, 44,
371 38, 28, 21, 16, 12, 10, 8, 6,
372 5, 3, 2, 1, 1, 1, 0, 0,
373 };
374 // Normalized distortion
375 // This table models the normalized distortion for a Laplacian source
376 // source with given variance when quantized with a uniform quantizer
377 // with given stepsize. The closed form expression is:
378 // Dn(x) = 1 - 1/sqrt(2) * x / sinh(x/sqrt(2))
379 // where x = qpstep / sqrt(variance)
380 // Note the actual distortion is Dn * variance.
381 static const int dist_tab_q10[] = {
382 0, 0, 1, 1, 1, 2, 2, 2,
383 3, 3, 4, 5, 5, 6, 7, 7,
384 8, 9, 11, 12, 13, 15, 16, 17,
385 18, 21, 24, 26, 29, 31, 34, 36,
386 39, 44, 49, 54, 59, 64, 69, 73,
387 78, 88, 97, 106, 115, 124, 133, 142,
388 151, 167, 184, 200, 215, 231, 245, 260,
389 274, 301, 327, 351, 375, 397, 418, 439,
390 458, 495, 528, 559, 587, 613, 637, 659,
391 680, 717, 749, 777, 801, 823, 842, 859,
392 874, 899, 919, 936, 949, 960, 969, 977,
393 983, 994, 1001, 1006, 1010, 1013, 1015, 1017,
394 1018, 1020, 1022, 1022, 1023, 1023, 1023, 1024,
395 };
396 static const int xsq_iq_q10[] = {
397 0, 4, 8, 12, 16, 20, 24, 28,
398 32, 40, 48, 56, 64, 72, 80, 88,
399 96, 112, 128, 144, 160, 176, 192, 208,
400 224, 256, 288, 320, 352, 384, 416, 448,
401 480, 544, 608, 672, 736, 800, 864, 928,
402 992, 1120, 1248, 1376, 1504, 1632, 1760, 1888,
403 2016, 2272, 2528, 2784, 3040, 3296, 3552, 3808,
404 4064, 4576, 5088, 5600, 6112, 6624, 7136, 7648,
405 8160, 9184, 10208, 11232, 12256, 13280, 14304, 15328,
406 16352, 18400, 20448, 22496, 24544, 26592, 28640, 30688,
407 32736, 36832, 40928, 45024, 49120, 53216, 57312, 61408,
408 65504, 73696, 81888, 90080, 98272, 106464, 114656, 122848,
409 131040, 147424, 163808, 180192, 196576, 212960, 229344, 245728,
410 };
411 /*
412 static const int tab_size = sizeof(rate_tab_q10) / sizeof(rate_tab_q10[0]);
413 assert(sizeof(dist_tab_q10) / sizeof(dist_tab_q10[0]) == tab_size);
414 assert(sizeof(xsq_iq_q10) / sizeof(xsq_iq_q10[0]) == tab_size);
415 assert(MAX_XSQ_Q10 + 1 == xsq_iq_q10[tab_size - 1]);
416 */
417 int tmp = (xsq_q10 >> 2) + 8;
418 int k = get_msb(tmp) - 3;
419 int xq = (k << 3) + ((tmp >> k) & 0x7);
420 const int one_q10 = 1 << 10;
421 const int a_q10 = ((xsq_q10 - xsq_iq_q10[xq]) << 10) >> (2 + k);
422 const int b_q10 = one_q10 - a_q10;
423 *r_q10 = (rate_tab_q10[xq] * b_q10 + rate_tab_q10[xq + 1] * a_q10) >> 10;
424 *d_q10 = (dist_tab_q10[xq] * b_q10 + dist_tab_q10[xq + 1] * a_q10) >> 10;
425 }
426
427 void vp9_model_rd_from_var_lapndz(unsigned int var, unsigned int n,
428 unsigned int qstep, int *rate,
429 int64_t *dist) {
430 // This function models the rate and distortion for a Laplacian
431 // source with given variance when quantized with a uniform quantizer
432 // with given stepsize. The closed form expressions are in:
433 // Hang and Chen, "Source Model for transform video coder and its
434 // application - Part I: Fundamental Theory", IEEE Trans. Circ.
435 // Sys. for Video Tech., April 1997.
436 if (var == 0) {
437 *rate = 0;
438 *dist = 0;
439 } else {
440 int d_q10, r_q10;
441 const uint64_t xsq_q10_64 =
442 ((((uint64_t)qstep * qstep * n) << 10) + (var >> 1)) / var;
443 const int xsq_q10 = xsq_q10_64 > MAX_XSQ_Q10 ?
444 MAX_XSQ_Q10 : (int)xsq_q10_64;
445 model_rd_norm(xsq_q10, &r_q10, &d_q10);
446 *rate = (n * r_q10 + 2) >> 2;
447 *dist = (var * (int64_t)d_q10 + 512) >> 10;
448 }
449 }
450
451 static void model_rd_for_sb(VP9_COMP *cpi, BLOCK_SIZE bsize, 163 static void model_rd_for_sb(VP9_COMP *cpi, BLOCK_SIZE bsize,
452 MACROBLOCK *x, MACROBLOCKD *xd, 164 MACROBLOCK *x, MACROBLOCKD *xd,
453 int *out_rate_sum, int64_t *out_dist_sum) { 165 int *out_rate_sum, int64_t *out_dist_sum) {
454 // Note our transform coeffs are 8 times an orthogonal transform. 166 // Note our transform coeffs are 8 times an orthogonal transform.
455 // Hence quantizer step is also 8 times. To get effective quantizer 167 // Hence quantizer step is also 8 times. To get effective quantizer
456 // we need to divide by 8 before sending to modeling function. 168 // we need to divide by 8 before sending to modeling function.
457 int i; 169 int i;
458 int64_t rate_sum = 0; 170 int64_t rate_sum = 0;
459 int64_t dist_sum = 0; 171 int64_t dist_sum = 0;
460 const int ref = xd->mi[0]->mbmi.ref_frame[0]; 172 const int ref = xd->mi[0]->mbmi.ref_frame[0];
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 pd->dequant[1] >> 3, &rate, &dist); 204 pd->dequant[1] >> 3, &rate, &dist);
493 rate_sum += rate; 205 rate_sum += rate;
494 dist_sum += dist; 206 dist_sum += dist;
495 } 207 }
496 } 208 }
497 209
498 *out_rate_sum = (int)rate_sum; 210 *out_rate_sum = (int)rate_sum;
499 *out_dist_sum = dist_sum << 4; 211 *out_dist_sum = dist_sum << 4;
500 } 212 }
501 213
502 static void model_rd_for_sb_y_tx(VP9_COMP *cpi, BLOCK_SIZE bsize,
503 TX_SIZE tx_size,
504 MACROBLOCK *x, MACROBLOCKD *xd,
505 int *out_rate_sum, int64_t *out_dist_sum,
506 int *out_skip) {
507 int j, k;
508 BLOCK_SIZE bs;
509 const struct macroblock_plane *const p = &x->plane[0];
510 const struct macroblockd_plane *const pd = &xd->plane[0];
511 const int width = 4 * num_4x4_blocks_wide_lookup[bsize];
512 const int height = 4 * num_4x4_blocks_high_lookup[bsize];
513 int rate_sum = 0;
514 int64_t dist_sum = 0;
515 const int t = 4 << tx_size;
516
517 if (tx_size == TX_4X4) {
518 bs = BLOCK_4X4;
519 } else if (tx_size == TX_8X8) {
520 bs = BLOCK_8X8;
521 } else if (tx_size == TX_16X16) {
522 bs = BLOCK_16X16;
523 } else if (tx_size == TX_32X32) {
524 bs = BLOCK_32X32;
525 } else {
526 assert(0);
527 }
528
529 *out_skip = 1;
530 for (j = 0; j < height; j += t) {
531 for (k = 0; k < width; k += t) {
532 int rate;
533 int64_t dist;
534 unsigned int sse;
535 cpi->fn_ptr[bs].vf(&p->src.buf[j * p->src.stride + k], p->src.stride,
536 &pd->dst.buf[j * pd->dst.stride + k], pd->dst.stride,
537 &sse);
538 // sse works better than var, since there is no dc prediction used
539 vp9_model_rd_from_var_lapndz(sse, t * t, pd->dequant[1] >> 3,
540 &rate, &dist);
541 rate_sum += rate;
542 dist_sum += dist;
543 *out_skip &= (rate < 1024);
544 }
545 }
546
547 *out_rate_sum = rate_sum;
548 *out_dist_sum = dist_sum << 4;
549 }
550
551 int64_t vp9_block_error_c(const int16_t *coeff, const int16_t *dqcoeff, 214 int64_t vp9_block_error_c(const int16_t *coeff, const int16_t *dqcoeff,
552 intptr_t block_size, int64_t *ssz) { 215 intptr_t block_size, int64_t *ssz) {
553 int i; 216 int i;
554 int64_t error = 0, sqcoeff = 0; 217 int64_t error = 0, sqcoeff = 0;
555 218
556 for (i = 0; i < block_size; i++) { 219 for (i = 0; i < block_size; i++) {
557 const int diff = coeff[i] - dqcoeff[i]; 220 const int diff = coeff[i] - dqcoeff[i];
558 error += diff * diff; 221 error += diff * diff;
559 sqcoeff += coeff[i] * coeff[i]; 222 sqcoeff += coeff[i] * coeff[i];
560 } 223 }
(...skipping 27 matching lines...) Expand all
588 const int16_t *band_count = &band_counts[tx_size][1]; 251 const int16_t *band_count = &band_counts[tx_size][1];
589 const int eob = p->eobs[block]; 252 const int eob = p->eobs[block];
590 const int16_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block); 253 const int16_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
591 unsigned int (*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] = 254 unsigned int (*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
592 x->token_costs[tx_size][type][is_inter_block(mbmi)]; 255 x->token_costs[tx_size][type][is_inter_block(mbmi)];
593 uint8_t token_cache[32 * 32]; 256 uint8_t token_cache[32 * 32];
594 int pt = combine_entropy_contexts(*A, *L); 257 int pt = combine_entropy_contexts(*A, *L);
595 int c, cost; 258 int c, cost;
596 // Check for consistency of tx_size with mode info 259 // Check for consistency of tx_size with mode info
597 assert(type == PLANE_TYPE_Y ? mbmi->tx_size == tx_size 260 assert(type == PLANE_TYPE_Y ? mbmi->tx_size == tx_size
598 : get_uv_tx_size(mbmi) == tx_size); 261 : get_uv_tx_size(mbmi, pd) == tx_size);
599 262
600 if (eob == 0) { 263 if (eob == 0) {
601 // single eob token 264 // single eob token
602 cost = token_costs[0][0][pt][EOB_TOKEN]; 265 cost = token_costs[0][0][pt][EOB_TOKEN];
603 c = 0; 266 c = 0;
604 } else { 267 } else {
605 int band_left = *band_count++; 268 int band_left = *band_count++;
606 269
607 // dc token 270 // dc token
608 int v = qcoeff[0]; 271 int v = qcoeff[0];
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 args->this_dist += args->dist; 377 args->this_dist += args->dist;
715 args->this_sse += args->sse; 378 args->this_sse += args->sse;
716 args->this_rd += rd; 379 args->this_rd += rd;
717 380
718 if (args->this_rd > args->best_rd) { 381 if (args->this_rd > args->best_rd) {
719 args->skip = 1; 382 args->skip = 1;
720 return; 383 return;
721 } 384 }
722 } 385 }
723 386
724 void vp9_get_entropy_contexts(BLOCK_SIZE bsize, TX_SIZE tx_size,
725 const struct macroblockd_plane *pd,
726 ENTROPY_CONTEXT t_above[16],
727 ENTROPY_CONTEXT t_left[16]) {
728 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
729 const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
730 const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
731 const ENTROPY_CONTEXT *const above = pd->above_context;
732 const ENTROPY_CONTEXT *const left = pd->left_context;
733
734 int i;
735 switch (tx_size) {
736 case TX_4X4:
737 vpx_memcpy(t_above, above, sizeof(ENTROPY_CONTEXT) * num_4x4_w);
738 vpx_memcpy(t_left, left, sizeof(ENTROPY_CONTEXT) * num_4x4_h);
739 break;
740 case TX_8X8:
741 for (i = 0; i < num_4x4_w; i += 2)
742 t_above[i] = !!*(const uint16_t *)&above[i];
743 for (i = 0; i < num_4x4_h; i += 2)
744 t_left[i] = !!*(const uint16_t *)&left[i];
745 break;
746 case TX_16X16:
747 for (i = 0; i < num_4x4_w; i += 4)
748 t_above[i] = !!*(const uint32_t *)&above[i];
749 for (i = 0; i < num_4x4_h; i += 4)
750 t_left[i] = !!*(const uint32_t *)&left[i];
751 break;
752 case TX_32X32:
753 for (i = 0; i < num_4x4_w; i += 8)
754 t_above[i] = !!*(const uint64_t *)&above[i];
755 for (i = 0; i < num_4x4_h; i += 8)
756 t_left[i] = !!*(const uint64_t *)&left[i];
757 break;
758 default:
759 assert(0 && "Invalid transform size.");
760 }
761 }
762
763 static void txfm_rd_in_plane(MACROBLOCK *x, 387 static void txfm_rd_in_plane(MACROBLOCK *x,
764 int *rate, int64_t *distortion, 388 int *rate, int64_t *distortion,
765 int *skippable, int64_t *sse, 389 int *skippable, int64_t *sse,
766 int64_t ref_best_rd, int plane, 390 int64_t ref_best_rd, int plane,
767 BLOCK_SIZE bsize, TX_SIZE tx_size, 391 BLOCK_SIZE bsize, TX_SIZE tx_size,
768 int use_fast_coef_casting) { 392 int use_fast_coef_casting) {
769 MACROBLOCKD *const xd = &x->e_mbd; 393 MACROBLOCKD *const xd = &x->e_mbd;
770 const struct macroblockd_plane *const pd = &xd->plane[plane]; 394 const struct macroblockd_plane *const pd = &xd->plane[plane];
771 struct rdcost_block_args args; 395 struct rdcost_block_args args;
772 vp9_zero(args); 396 vp9_zero(args);
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 cpi->tx_stepdown_count[max_tx_size - TX_16X16]++; 510 cpi->tx_stepdown_count[max_tx_size - TX_16X16]++;
887 } else if (rd[TX_8X8][1] < rd[TX_4X4][1]) { 511 } else if (rd[TX_8X8][1] < rd[TX_4X4][1]) {
888 tx_cache[TX_MODE_SELECT] = rd[TX_8X8][1]; 512 tx_cache[TX_MODE_SELECT] = rd[TX_8X8][1];
889 cpi->tx_stepdown_count[max_tx_size - TX_8X8]++; 513 cpi->tx_stepdown_count[max_tx_size - TX_8X8]++;
890 } else { 514 } else {
891 tx_cache[TX_MODE_SELECT] = rd[TX_4X4][1]; 515 tx_cache[TX_MODE_SELECT] = rd[TX_4X4][1];
892 cpi->tx_stepdown_count[max_tx_size - TX_4X4]++; 516 cpi->tx_stepdown_count[max_tx_size - TX_4X4]++;
893 } 517 }
894 } 518 }
895 519
896 static int64_t scaled_rd_cost(int rdmult, int rddiv,
897 int rate, int64_t dist, double scale) {
898 return (int64_t) (RDCOST(rdmult, rddiv, rate, dist) * scale);
899 }
900
901 static void choose_tx_size_from_modelrd(VP9_COMP *cpi, MACROBLOCK *x,
902 int (*r)[2], int *rate,
903 int64_t *d, int64_t *distortion,
904 int *s, int *skip, int64_t *sse,
905 int64_t ref_best_rd,
906 BLOCK_SIZE bs) {
907 const TX_SIZE max_tx_size = max_txsize_lookup[bs];
908 VP9_COMMON *const cm = &cpi->common;
909 MACROBLOCKD *const xd = &x->e_mbd;
910 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
911 vp9_prob skip_prob = vp9_get_skip_prob(cm, xd);
912 int64_t rd[TX_SIZES][2] = {{INT64_MAX, INT64_MAX},
913 {INT64_MAX, INT64_MAX},
914 {INT64_MAX, INT64_MAX},
915 {INT64_MAX, INT64_MAX}};
916 TX_SIZE n, m;
917 int s0, s1;
918 double scale_rd[TX_SIZES] = {1.73, 1.44, 1.20, 1.00};
919 const TX_SIZE max_mode_tx_size = tx_mode_to_biggest_tx_size[cm->tx_mode];
920 int64_t best_rd = INT64_MAX;
921 TX_SIZE best_tx = TX_4X4;
922
923 const vp9_prob *tx_probs = get_tx_probs2(max_tx_size, xd, &cm->fc.tx_probs);
924 assert(skip_prob > 0);
925 s0 = vp9_cost_bit(skip_prob, 0);
926 s1 = vp9_cost_bit(skip_prob, 1);
927
928 for (n = TX_4X4; n <= max_tx_size; n++) {
929 double scale = scale_rd[n];
930 r[n][1] = r[n][0];
931 for (m = 0; m <= n - (n == max_tx_size); m++) {
932 if (m == n)
933 r[n][1] += vp9_cost_zero(tx_probs[m]);
934 else
935 r[n][1] += vp9_cost_one(tx_probs[m]);
936 }
937 if (s[n]) {
938 rd[n][0] = rd[n][1] = scaled_rd_cost(x->rdmult, x->rddiv, s1, d[n],
939 scale);
940 } else {
941 rd[n][0] = scaled_rd_cost(x->rdmult, x->rddiv, r[n][0] + s0, d[n],
942 scale);
943 rd[n][1] = scaled_rd_cost(x->rdmult, x->rddiv, r[n][1] + s0, d[n],
944 scale);
945 }
946 if (rd[n][1] < best_rd) {
947 best_rd = rd[n][1];
948 best_tx = n;
949 }
950 }
951
952 mbmi->tx_size = cm->tx_mode == TX_MODE_SELECT ?
953 best_tx : MIN(max_tx_size, max_mode_tx_size);
954
955 // Actually encode using the chosen mode if a model was used, but do not
956 // update the r, d costs
957 txfm_rd_in_plane(x, rate, distortion, skip,
958 &sse[mbmi->tx_size], ref_best_rd, 0, bs, mbmi->tx_size,
959 cpi->sf.use_fast_coef_costing);
960
961 if (max_tx_size == TX_32X32 && best_tx == TX_32X32) {
962 cpi->tx_stepdown_count[0]++;
963 } else if (max_tx_size >= TX_16X16 && best_tx == TX_16X16) {
964 cpi->tx_stepdown_count[max_tx_size - TX_16X16]++;
965 } else if (rd[TX_8X8][1] <= rd[TX_4X4][1]) {
966 cpi->tx_stepdown_count[max_tx_size - TX_8X8]++;
967 } else {
968 cpi->tx_stepdown_count[max_tx_size - TX_4X4]++;
969 }
970 }
971
972 static void inter_super_block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate, 520 static void inter_super_block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
973 int64_t *distortion, int *skip, 521 int64_t *distortion, int *skip,
974 int64_t *psse, BLOCK_SIZE bs, 522 int64_t *psse, BLOCK_SIZE bs,
975 int64_t txfm_cache[TX_MODES], 523 int64_t txfm_cache[TX_MODES],
976 int64_t ref_best_rd) { 524 int64_t ref_best_rd) {
977 int r[TX_SIZES][2], s[TX_SIZES]; 525 int r[TX_SIZES][2], s[TX_SIZES];
978 int64_t d[TX_SIZES], sse[TX_SIZES]; 526 int64_t d[TX_SIZES], sse[TX_SIZES];
979 MACROBLOCKD *xd = &x->e_mbd; 527 MACROBLOCKD *xd = &x->e_mbd;
980 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; 528 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
981 const TX_SIZE max_tx_size = max_txsize_lookup[bs]; 529 const TX_SIZE max_tx_size = max_txsize_lookup[bs];
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1320 868
1321 return best_rd; 869 return best_rd;
1322 } 870 }
1323 871
1324 static void super_block_uvrd(const VP9_COMP *cpi, MACROBLOCK *x, 872 static void super_block_uvrd(const VP9_COMP *cpi, MACROBLOCK *x,
1325 int *rate, int64_t *distortion, int *skippable, 873 int *rate, int64_t *distortion, int *skippable,
1326 int64_t *sse, BLOCK_SIZE bsize, 874 int64_t *sse, BLOCK_SIZE bsize,
1327 int64_t ref_best_rd) { 875 int64_t ref_best_rd) {
1328 MACROBLOCKD *const xd = &x->e_mbd; 876 MACROBLOCKD *const xd = &x->e_mbd;
1329 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; 877 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
1330 const TX_SIZE uv_tx_size = get_uv_tx_size(mbmi); 878 const TX_SIZE uv_tx_size = get_uv_tx_size(mbmi, &xd->plane[1]);
1331 int plane; 879 int plane;
1332 int pnrate = 0, pnskip = 1; 880 int pnrate = 0, pnskip = 1;
1333 int64_t pndist = 0, pnsse = 0; 881 int64_t pndist = 0, pnsse = 0;
1334 882
1335 if (ref_best_rd < 0) 883 if (ref_best_rd < 0)
1336 goto term; 884 goto term;
1337 885
1338 if (is_inter_block(mbmi)) { 886 if (is_inter_block(mbmi)) {
1339 int plane; 887 int plane;
1340 for (plane = 1; plane < MAX_MB_PLANE; ++plane) 888 for (plane = 1; plane < MAX_MB_PLANE; ++plane)
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1440 } else { 988 } else {
1441 rd_pick_intra_sbuv_mode(cpi, x, ctx, 989 rd_pick_intra_sbuv_mode(cpi, x, ctx,
1442 rate_uv, rate_uv_tokenonly, dist_uv, skip_uv, 990 rate_uv, rate_uv_tokenonly, dist_uv, skip_uv,
1443 bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize, max_tx_size); 991 bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize, max_tx_size);
1444 } 992 }
1445 *mode_uv = x->e_mbd.mi[0]->mbmi.uv_mode; 993 *mode_uv = x->e_mbd.mi[0]->mbmi.uv_mode;
1446 } 994 }
1447 995
1448 static int cost_mv_ref(const VP9_COMP *cpi, PREDICTION_MODE mode, 996 static int cost_mv_ref(const VP9_COMP *cpi, PREDICTION_MODE mode,
1449 int mode_context) { 997 int mode_context) {
1450 const MACROBLOCK *const x = &cpi->mb; 998 assert(is_inter_mode(mode));
1451 const int segment_id = x->e_mbd.mi[0]->mbmi.segment_id; 999 return cpi->inter_mode_cost[mode_context][INTER_OFFSET(mode)];
1452
1453 // Don't account for mode here if segment skip is enabled.
1454 if (!vp9_segfeature_active(&cpi->common.seg, segment_id, SEG_LVL_SKIP)) {
1455 assert(is_inter_mode(mode));
1456 return cpi->inter_mode_cost[mode_context][INTER_OFFSET(mode)];
1457 } else {
1458 return 0;
1459 }
1460 } 1000 }
1461 1001
1462 static void joint_motion_search(VP9_COMP *cpi, MACROBLOCK *x, 1002 static void joint_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
1463 BLOCK_SIZE bsize, 1003 BLOCK_SIZE bsize,
1464 int_mv *frame_mv, 1004 int_mv *frame_mv,
1465 int mi_row, int mi_col, 1005 int mi_row, int mi_col,
1466 int_mv single_newmv[MAX_REF_FRAMES], 1006 int_mv single_newmv[MAX_REF_FRAMES],
1467 int *rate_mv); 1007 int *rate_mv);
1468 1008
1469 static int set_and_cost_bmi_mvs(VP9_COMP *cpi, MACROBLOCKD *xd, int i, 1009 static int set_and_cost_bmi_mvs(VP9_COMP *cpi, MACROBLOCKD *xd, int i,
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after
2067 *returntotrate = bsi->r; 1607 *returntotrate = bsi->r;
2068 *returndistortion = bsi->d; 1608 *returndistortion = bsi->d;
2069 *returnyrate = bsi->segment_yrate; 1609 *returnyrate = bsi->segment_yrate;
2070 *skippable = vp9_is_skippable_in_plane(x, BLOCK_8X8, 0); 1610 *skippable = vp9_is_skippable_in_plane(x, BLOCK_8X8, 0);
2071 *psse = bsi->sse; 1611 *psse = bsi->sse;
2072 mbmi->mode = bsi->modes[3]; 1612 mbmi->mode = bsi->modes[3];
2073 1613
2074 return bsi->segment_rd; 1614 return bsi->segment_rd;
2075 } 1615 }
2076 1616
2077 static void mv_pred(VP9_COMP *cpi, MACROBLOCK *x,
2078 uint8_t *ref_y_buffer, int ref_y_stride,
2079 int ref_frame, BLOCK_SIZE block_size ) {
2080 MACROBLOCKD *xd = &x->e_mbd;
2081 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
2082 int_mv this_mv;
2083 int i;
2084 int zero_seen = 0;
2085 int best_index = 0;
2086 int best_sad = INT_MAX;
2087 int this_sad = INT_MAX;
2088 int max_mv = 0;
2089
2090 uint8_t *src_y_ptr = x->plane[0].src.buf;
2091 uint8_t *ref_y_ptr;
2092 int row_offset, col_offset;
2093 int num_mv_refs = MAX_MV_REF_CANDIDATES +
2094 (cpi->sf.adaptive_motion_search &&
2095 cpi->common.show_frame &&
2096 block_size < cpi->sf.max_partition_size);
2097
2098 MV pred_mv[3];
2099 pred_mv[0] = mbmi->ref_mvs[ref_frame][0].as_mv;
2100 pred_mv[1] = mbmi->ref_mvs[ref_frame][1].as_mv;
2101 pred_mv[2] = x->pred_mv[ref_frame];
2102
2103 // Get the sad for each candidate reference mv
2104 for (i = 0; i < num_mv_refs; i++) {
2105 this_mv.as_mv = pred_mv[i];
2106
2107 max_mv = MAX(max_mv,
2108 MAX(abs(this_mv.as_mv.row), abs(this_mv.as_mv.col)) >> 3);
2109 // only need to check zero mv once
2110 if (!this_mv.as_int && zero_seen)
2111 continue;
2112
2113 zero_seen = zero_seen || !this_mv.as_int;
2114
2115 row_offset = this_mv.as_mv.row >> 3;
2116 col_offset = this_mv.as_mv.col >> 3;
2117 ref_y_ptr = ref_y_buffer + (ref_y_stride * row_offset) + col_offset;
2118
2119 // Find sad for current vector.
2120 this_sad = cpi->fn_ptr[block_size].sdf(src_y_ptr, x->plane[0].src.stride,
2121 ref_y_ptr, ref_y_stride);
2122
2123 // Note if it is the best so far.
2124 if (this_sad < best_sad) {
2125 best_sad = this_sad;
2126 best_index = i;
2127 }
2128 }
2129
2130 // Note the index of the mv that worked best in the reference list.
2131 x->mv_best_ref_index[ref_frame] = best_index;
2132 x->max_mv_context[ref_frame] = max_mv;
2133 x->pred_mv_sad[ref_frame] = best_sad;
2134 }
2135
2136 static void estimate_ref_frame_costs(const VP9_COMMON *cm, 1617 static void estimate_ref_frame_costs(const VP9_COMMON *cm,
2137 const MACROBLOCKD *xd, 1618 const MACROBLOCKD *xd,
2138 int segment_id, 1619 int segment_id,
2139 unsigned int *ref_costs_single, 1620 unsigned int *ref_costs_single,
2140 unsigned int *ref_costs_comp, 1621 unsigned int *ref_costs_comp,
2141 vp9_prob *comp_mode_p) { 1622 vp9_prob *comp_mode_p) {
2142 int seg_ref_active = vp9_segfeature_active(&cm->seg, segment_id, 1623 int seg_ref_active = vp9_segfeature_active(&cm->seg, segment_id,
2143 SEG_LVL_REF_FRAME); 1624 SEG_LVL_REF_FRAME);
2144 if (seg_ref_active) { 1625 if (seg_ref_active) {
2145 vpx_memset(ref_costs_single, 0, MAX_REF_FRAMES * sizeof(*ref_costs_single)); 1626 vpx_memset(ref_costs_single, 0, MAX_REF_FRAMES * sizeof(*ref_costs_single));
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
2208 ctx->mic = *xd->mi[0]; 1689 ctx->mic = *xd->mi[0];
2209 ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE]; 1690 ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE];
2210 ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE]; 1691 ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE];
2211 ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT]; 1692 ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT];
2212 1693
2213 vpx_memcpy(ctx->tx_rd_diff, tx_size_diff, sizeof(ctx->tx_rd_diff)); 1694 vpx_memcpy(ctx->tx_rd_diff, tx_size_diff, sizeof(ctx->tx_rd_diff));
2214 vpx_memcpy(ctx->best_filter_diff, best_filter_diff, 1695 vpx_memcpy(ctx->best_filter_diff, best_filter_diff,
2215 sizeof(*best_filter_diff) * SWITCHABLE_FILTER_CONTEXTS); 1696 sizeof(*best_filter_diff) * SWITCHABLE_FILTER_CONTEXTS);
2216 } 1697 }
2217 1698
2218 static void setup_pred_block(const MACROBLOCKD *xd, 1699 static void setup_buffer_inter(VP9_COMP *cpi, MACROBLOCK *x,
2219 struct buf_2d dst[MAX_MB_PLANE], 1700 const TileInfo *const tile,
2220 const YV12_BUFFER_CONFIG *src, 1701 MV_REFERENCE_FRAME ref_frame,
2221 int mi_row, int mi_col, 1702 BLOCK_SIZE block_size,
2222 const struct scale_factors *scale, 1703 int mi_row, int mi_col,
2223 const struct scale_factors *scale_uv) { 1704 int_mv frame_nearest_mv[MAX_REF_FRAMES],
2224 int i; 1705 int_mv frame_near_mv[MAX_REF_FRAMES],
2225 1706 struct buf_2d yv12_mb[4][MAX_MB_PLANE]) {
2226 dst[0].buf = src->y_buffer;
2227 dst[0].stride = src->y_stride;
2228 dst[1].buf = src->u_buffer;
2229 dst[2].buf = src->v_buffer;
2230 dst[1].stride = dst[2].stride = src->uv_stride;
2231 #if CONFIG_ALPHA
2232 dst[3].buf = src->alpha_buffer;
2233 dst[3].stride = src->alpha_stride;
2234 #endif
2235
2236 // TODO(jkoleszar): Make scale factors per-plane data
2237 for (i = 0; i < MAX_MB_PLANE; i++) {
2238 setup_pred_plane(dst + i, dst[i].buf, dst[i].stride, mi_row, mi_col,
2239 i ? scale_uv : scale,
2240 xd->plane[i].subsampling_x, xd->plane[i].subsampling_y);
2241 }
2242 }
2243
2244 void vp9_setup_buffer_inter(VP9_COMP *cpi, MACROBLOCK *x,
2245 const TileInfo *const tile,
2246 MV_REFERENCE_FRAME ref_frame,
2247 BLOCK_SIZE block_size,
2248 int mi_row, int mi_col,
2249 int_mv frame_nearest_mv[MAX_REF_FRAMES],
2250 int_mv frame_near_mv[MAX_REF_FRAMES],
2251 struct buf_2d yv12_mb[4][MAX_MB_PLANE]) {
2252 const VP9_COMMON *cm = &cpi->common; 1707 const VP9_COMMON *cm = &cpi->common;
2253 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame); 1708 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
2254 MACROBLOCKD *const xd = &x->e_mbd; 1709 MACROBLOCKD *const xd = &x->e_mbd;
2255 MODE_INFO *const mi = xd->mi[0]; 1710 MODE_INFO *const mi = xd->mi[0];
2256 int_mv *const candidates = mi->mbmi.ref_mvs[ref_frame]; 1711 int_mv *const candidates = mi->mbmi.ref_mvs[ref_frame];
2257 const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf; 1712 const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
2258 1713
2259 // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this 1714 // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this
2260 // use the UV scaling factors. 1715 // use the UV scaling factors.
2261 setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf); 1716 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf);
2262 1717
2263 // Gets an initial list of candidate vectors from neighbours and orders them 1718 // Gets an initial list of candidate vectors from neighbours and orders them
2264 vp9_find_mv_refs(cm, xd, tile, mi, ref_frame, candidates, mi_row, mi_col); 1719 vp9_find_mv_refs(cm, xd, tile, mi, ref_frame, candidates, mi_row, mi_col);
2265 1720
2266 // Candidate refinement carried out at encoder and decoder 1721 // Candidate refinement carried out at encoder and decoder
2267 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates, 1722 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
2268 &frame_nearest_mv[ref_frame], 1723 &frame_nearest_mv[ref_frame],
2269 &frame_near_mv[ref_frame]); 1724 &frame_near_mv[ref_frame]);
2270 1725
2271 // Further refinement that is encode side only to test the top few candidates 1726 // Further refinement that is encode side only to test the top few candidates
2272 // in full and choose the best as the centre point for subsequent searches. 1727 // in full and choose the best as the centre point for subsequent searches.
2273 // The current implementation doesn't support scaling. 1728 // The current implementation doesn't support scaling.
2274 if (!vp9_is_scaled(sf) && block_size >= BLOCK_8X8) 1729 if (!vp9_is_scaled(sf) && block_size >= BLOCK_8X8)
2275 mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, 1730 vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride,
2276 ref_frame, block_size); 1731 ref_frame, block_size);
2277 }
2278
2279 const YV12_BUFFER_CONFIG *vp9_get_scaled_ref_frame(const VP9_COMP *cpi,
2280 int ref_frame) {
2281 const VP9_COMMON *const cm = &cpi->common;
2282 const int ref_idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2283 const int scaled_idx = cpi->scaled_ref_idx[ref_frame - 1];
2284 return (scaled_idx != ref_idx) ? &cm->frame_bufs[scaled_idx].buf : NULL;
2285 }
2286
2287 int vp9_get_switchable_rate(const VP9_COMP *cpi) {
2288 const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
2289 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
2290 const int ctx = vp9_get_pred_context_switchable_interp(xd);
2291 return SWITCHABLE_INTERP_RATE_FACTOR *
2292 cpi->switchable_interp_costs[ctx][mbmi->interp_filter];
2293 } 1732 }
2294 1733
2295 static void single_motion_search(VP9_COMP *cpi, MACROBLOCK *x, 1734 static void single_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
2296 BLOCK_SIZE bsize, 1735 BLOCK_SIZE bsize,
2297 int mi_row, int mi_col, 1736 int mi_row, int mi_col,
2298 int_mv *tmp_mv, int *rate_mv) { 1737 int_mv *tmp_mv, int *rate_mv) {
2299 MACROBLOCKD *xd = &x->e_mbd; 1738 MACROBLOCKD *xd = &x->e_mbd;
2300 const VP9_COMMON *cm = &cpi->common; 1739 const VP9_COMMON *cm = &cpi->common;
2301 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; 1740 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
2302 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}}; 1741 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}};
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after
2792 if (rd / 2 > ref_best_rd) { 2231 if (rd / 2 > ref_best_rd) {
2793 restore_dst_buf(xd, orig_dst, orig_dst_stride); 2232 restore_dst_buf(xd, orig_dst, orig_dst_stride);
2794 return INT64_MAX; 2233 return INT64_MAX;
2795 } 2234 }
2796 } 2235 }
2797 2236
2798 if (cm->interp_filter == SWITCHABLE) 2237 if (cm->interp_filter == SWITCHABLE)
2799 *rate2 += vp9_get_switchable_rate(cpi); 2238 *rate2 += vp9_get_switchable_rate(cpi);
2800 2239
2801 if (!is_comp_pred) { 2240 if (!is_comp_pred) {
2802 if (!x->in_active_map || 2241 if (cpi->allow_encode_breakout) {
2803 vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
2804 if (psse)
2805 *psse = 0;
2806 *distortion = 0;
2807 x->skip = 1;
2808 } else if (cpi->allow_encode_breakout && x->encode_breakout) {
2809 const BLOCK_SIZE y_size = get_plane_block_size(bsize, &xd->plane[0]); 2242 const BLOCK_SIZE y_size = get_plane_block_size(bsize, &xd->plane[0]);
2810 const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]); 2243 const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]);
2811 unsigned int var, sse; 2244 unsigned int var, sse;
2812 // Skipping threshold for ac. 2245 // Skipping threshold for ac.
2813 unsigned int thresh_ac; 2246 unsigned int thresh_ac;
2814 // Set a maximum for threshold to avoid big PSNR loss in low bitrate case. 2247 // Skipping threshold for dc
2815 // Use extreme low threshold for static frames to limit skipping. 2248 unsigned int thresh_dc;
2816 const unsigned int max_thresh = (cpi->allow_encode_breakout ==
2817 ENCODE_BREAKOUT_LIMITED) ? 128 : 36000;
2818 // The encode_breakout input
2819 const unsigned int min_thresh =
2820 MIN(((unsigned int)x->encode_breakout << 4), max_thresh);
2821
2822 // Calculate threshold according to dequant value.
2823 thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) / 9;
2824 thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
2825 2249
2826 var = cpi->fn_ptr[y_size].vf(x->plane[0].src.buf, x->plane[0].src.stride, 2250 var = cpi->fn_ptr[y_size].vf(x->plane[0].src.buf, x->plane[0].src.stride,
2827 xd->plane[0].dst.buf, 2251 xd->plane[0].dst.buf,
2828 xd->plane[0].dst.stride, &sse); 2252 xd->plane[0].dst.stride, &sse);
2829 2253
2830 // Adjust threshold according to partition size. 2254 if (x->encode_breakout > 0) {
2831 thresh_ac >>= 8 - (b_width_log2_lookup[bsize] + 2255 // Set a maximum for threshold to avoid big PSNR loss in low bitrate
2832 b_height_log2_lookup[bsize]); 2256 // case. Use extreme low threshold for static frames to limit skipping.
2257 const unsigned int max_thresh = (cpi->allow_encode_breakout ==
2258 ENCODE_BREAKOUT_LIMITED) ? 128 : 36000;
2259 // The encode_breakout input
2260 const unsigned int min_thresh =
2261 MIN(((unsigned int)x->encode_breakout << 4), max_thresh);
2262
2263 // Calculate threshold according to dequant value.
2264 thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) / 9;
2265 thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
2266
2267 // Adjust threshold according to partition size.
2268 thresh_ac >>= 8 - (b_width_log2_lookup[bsize] +
2269 b_height_log2_lookup[bsize]);
2270 thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
2271 } else {
2272 thresh_ac = 0;
2273 thresh_dc = 0;
2274 }
2833 2275
2834 // Y skipping condition checking 2276 // Y skipping condition checking
2835 if (sse < thresh_ac || sse == 0) { 2277 if (sse < thresh_ac || sse == 0) {
2836 // Skipping threshold for dc
2837 unsigned int thresh_dc;
2838
2839 thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
2840
2841 // dc skipping checking 2278 // dc skipping checking
2842 if ((sse - var) < thresh_dc || sse == var) { 2279 if ((sse - var) < thresh_dc || sse == var) {
2843 unsigned int sse_u, sse_v; 2280 unsigned int sse_u, sse_v;
2844 unsigned int var_u, var_v; 2281 unsigned int var_u, var_v;
2845 2282
2846 var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf, 2283 var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf,
2847 x->plane[1].src.stride, 2284 x->plane[1].src.stride,
2848 xd->plane[1].dst.buf, 2285 xd->plane[1].dst.buf,
2849 xd->plane[1].dst.stride, &sse_u); 2286 xd->plane[1].dst.stride, &sse_u);
2850 2287
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
2918 restore_dst_buf(xd, orig_dst, orig_dst_stride); 2355 restore_dst_buf(xd, orig_dst, orig_dst_stride);
2919 return this_rd; // if 0, this will be re-calculated by caller 2356 return this_rd; // if 0, this will be re-calculated by caller
2920 } 2357 }
2921 2358
2922 void vp9_rd_pick_intra_mode_sb(VP9_COMP *cpi, MACROBLOCK *x, 2359 void vp9_rd_pick_intra_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
2923 int *returnrate, int64_t *returndist, 2360 int *returnrate, int64_t *returndist,
2924 BLOCK_SIZE bsize, 2361 BLOCK_SIZE bsize,
2925 PICK_MODE_CONTEXT *ctx, int64_t best_rd) { 2362 PICK_MODE_CONTEXT *ctx, int64_t best_rd) {
2926 VP9_COMMON *const cm = &cpi->common; 2363 VP9_COMMON *const cm = &cpi->common;
2927 MACROBLOCKD *const xd = &x->e_mbd; 2364 MACROBLOCKD *const xd = &x->e_mbd;
2365 struct macroblockd_plane *const pd = xd->plane;
2928 int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0; 2366 int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0;
2929 int y_skip = 0, uv_skip = 0; 2367 int y_skip = 0, uv_skip = 0;
2930 int64_t dist_y = 0, dist_uv = 0, tx_cache[TX_MODES] = { 0 }; 2368 int64_t dist_y = 0, dist_uv = 0, tx_cache[TX_MODES] = { 0 };
2931 TX_SIZE max_uv_tx_size; 2369 TX_SIZE max_uv_tx_size;
2932 x->skip_encode = 0; 2370 x->skip_encode = 0;
2933 ctx->skip = 0; 2371 ctx->skip = 0;
2934 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME; 2372 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME;
2935 2373
2936 if (bsize >= BLOCK_8X8) { 2374 if (bsize >= BLOCK_8X8) {
2937 if (rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly, 2375 if (rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly,
2938 &dist_y, &y_skip, bsize, tx_cache, 2376 &dist_y, &y_skip, bsize, tx_cache,
2939 best_rd) >= best_rd) { 2377 best_rd) >= best_rd) {
2940 *returnrate = INT_MAX; 2378 *returnrate = INT_MAX;
2941 return; 2379 return;
2942 } 2380 }
2943 max_uv_tx_size = get_uv_tx_size_impl(xd->mi[0]->mbmi.tx_size, bsize); 2381 max_uv_tx_size = get_uv_tx_size_impl(xd->mi[0]->mbmi.tx_size, bsize,
2382 pd[1].subsampling_x,
2383 pd[1].subsampling_y);
2944 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly, 2384 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly,
2945 &dist_uv, &uv_skip, bsize, max_uv_tx_size); 2385 &dist_uv, &uv_skip, bsize, max_uv_tx_size);
2946 } else { 2386 } else {
2947 y_skip = 0; 2387 y_skip = 0;
2948 if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly, 2388 if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly,
2949 &dist_y, best_rd) >= best_rd) { 2389 &dist_y, best_rd) >= best_rd) {
2950 *returnrate = INT_MAX; 2390 *returnrate = INT_MAX;
2951 return; 2391 return;
2952 } 2392 }
2953 max_uv_tx_size = get_uv_tx_size_impl(xd->mi[0]->mbmi.tx_size, bsize); 2393 max_uv_tx_size = get_uv_tx_size_impl(xd->mi[0]->mbmi.tx_size, bsize,
2394 pd[1].subsampling_x,
2395 pd[1].subsampling_y);
2954 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly, 2396 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly,
2955 &dist_uv, &uv_skip, BLOCK_8X8, max_uv_tx_size); 2397 &dist_uv, &uv_skip, BLOCK_8X8, max_uv_tx_size);
2956 } 2398 }
2957 2399
2958 if (y_skip && uv_skip) { 2400 if (y_skip && uv_skip) {
2959 *returnrate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly + 2401 *returnrate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly +
2960 vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1); 2402 vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
2961 *returndist = dist_y + dist_uv; 2403 *returndist = dist_y + dist_uv;
2962 vp9_zero(ctx->tx_rd_diff); 2404 vp9_zero(ctx->tx_rd_diff);
2963 } else { 2405 } else {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
3005 int *returnrate, 2447 int *returnrate,
3006 int64_t *returndistortion, 2448 int64_t *returndistortion,
3007 BLOCK_SIZE bsize, 2449 BLOCK_SIZE bsize,
3008 PICK_MODE_CONTEXT *ctx, 2450 PICK_MODE_CONTEXT *ctx,
3009 int64_t best_rd_so_far) { 2451 int64_t best_rd_so_far) {
3010 VP9_COMMON *const cm = &cpi->common; 2452 VP9_COMMON *const cm = &cpi->common;
3011 RD_OPT *const rd_opt = &cpi->rd; 2453 RD_OPT *const rd_opt = &cpi->rd;
3012 MACROBLOCKD *const xd = &x->e_mbd; 2454 MACROBLOCKD *const xd = &x->e_mbd;
3013 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; 2455 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
3014 const struct segmentation *const seg = &cm->seg; 2456 const struct segmentation *const seg = &cm->seg;
2457 struct macroblockd_plane *const pd = xd->plane;
3015 PREDICTION_MODE this_mode; 2458 PREDICTION_MODE this_mode;
3016 MV_REFERENCE_FRAME ref_frame, second_ref_frame; 2459 MV_REFERENCE_FRAME ref_frame, second_ref_frame;
3017 unsigned char segment_id = mbmi->segment_id; 2460 unsigned char segment_id = mbmi->segment_id;
3018 int comp_pred, i; 2461 int comp_pred, i;
3019 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES]; 2462 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
3020 struct buf_2d yv12_mb[4][MAX_MB_PLANE]; 2463 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
3021 int_mv single_newmv[MAX_REF_FRAMES] = { { 0 } }; 2464 int_mv single_newmv[MAX_REF_FRAMES] = { { 0 } };
3022 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG, 2465 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
3023 VP9_ALT_FLAG }; 2466 VP9_ALT_FLAG };
3024 int64_t best_rd = best_rd_so_far; 2467 int64_t best_rd = best_rd_so_far;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
3069 for (i = 0; i < TX_SIZES; i++) 2512 for (i = 0; i < TX_SIZES; i++)
3070 rate_uv_intra[i] = INT_MAX; 2513 rate_uv_intra[i] = INT_MAX;
3071 for (i = 0; i < MAX_REF_FRAMES; ++i) 2514 for (i = 0; i < MAX_REF_FRAMES; ++i)
3072 x->pred_sse[i] = INT_MAX; 2515 x->pred_sse[i] = INT_MAX;
3073 2516
3074 *returnrate = INT_MAX; 2517 *returnrate = INT_MAX;
3075 2518
3076 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { 2519 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3077 x->pred_mv_sad[ref_frame] = INT_MAX; 2520 x->pred_mv_sad[ref_frame] = INT_MAX;
3078 if (cpi->ref_frame_flags & flag_list[ref_frame]) { 2521 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
3079 vp9_setup_buffer_inter(cpi, x, tile, 2522 setup_buffer_inter(cpi, x, tile,
3080 ref_frame, bsize, mi_row, mi_col, 2523 ref_frame, bsize, mi_row, mi_col,
3081 frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb); 2524 frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
3082 } 2525 }
3083 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; 2526 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
3084 frame_mv[ZEROMV][ref_frame].as_int = 0; 2527 frame_mv[ZEROMV][ref_frame].as_int = 0;
3085 } 2528 }
3086 2529
3087 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { 2530 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3088 // All modes from vp9_mode_order that use this frame as any ref 2531 // All modes from vp9_mode_order that use this frame as any ref
3089 static const int ref_frame_mask_all[] = { 2532 static const int ref_frame_mask_all[] = {
(...skipping 17 matching lines...) Expand all
3107 } 2550 }
3108 } 2551 }
3109 // If the segment reference frame feature is enabled.... 2552 // If the segment reference frame feature is enabled....
3110 // then do nothing if the current ref frame is not allowed.. 2553 // then do nothing if the current ref frame is not allowed..
3111 if (vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && 2554 if (vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
3112 vp9_get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) { 2555 vp9_get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) {
3113 mode_skip_mask |= ref_frame_mask_all[ref_frame]; 2556 mode_skip_mask |= ref_frame_mask_all[ref_frame];
3114 } 2557 }
3115 } 2558 }
3116 2559
3117 // If the segment skip feature is enabled....
3118 // then do nothing if the current mode is not allowed..
3119 if (vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)) {
3120 mode_skip_mask = ~(1 << THR_ZEROMV);
3121 inter_mode_mask = (1 << ZEROMV);
3122 }
3123
3124 // Disable this drop out case if the ref frame 2560 // Disable this drop out case if the ref frame
3125 // segment level feature is enabled for this segment. This is to 2561 // segment level feature is enabled for this segment. This is to
3126 // prevent the possibility that we end up unable to pick any mode. 2562 // prevent the possibility that we end up unable to pick any mode.
3127 if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) { 2563 if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
3128 // Only consider ZEROMV/ALTREF_FRAME for alt ref frame, 2564 // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
3129 // unless ARNR filtering is enabled in which case we want 2565 // unless ARNR filtering is enabled in which case we want
3130 // an unfiltered alternative. We allow near/nearest as well 2566 // an unfiltered alternative. We allow near/nearest as well
3131 // because they may result in zero-zero MVs but be cheaper. 2567 // because they may result in zero-zero MVs but be cheaper.
3132 if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) { 2568 if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) {
3133 mode_skip_mask = 2569 mode_skip_mask =
(...skipping 18 matching lines...) Expand all
3152 } 2588 }
3153 2589
3154 if (bsize > cpi->sf.max_intra_bsize) { 2590 if (bsize > cpi->sf.max_intra_bsize) {
3155 const int all_intra_modes = (1 << THR_DC) | (1 << THR_TM) | 2591 const int all_intra_modes = (1 << THR_DC) | (1 << THR_TM) |
3156 (1 << THR_H_PRED) | (1 << THR_V_PRED) | (1 << THR_D135_PRED) | 2592 (1 << THR_H_PRED) | (1 << THR_V_PRED) | (1 << THR_D135_PRED) |
3157 (1 << THR_D207_PRED) | (1 << THR_D153_PRED) | (1 << THR_D63_PRED) | 2593 (1 << THR_D207_PRED) | (1 << THR_D153_PRED) | (1 << THR_D63_PRED) |
3158 (1 << THR_D117_PRED) | (1 << THR_D45_PRED); 2594 (1 << THR_D117_PRED) | (1 << THR_D45_PRED);
3159 mode_skip_mask |= all_intra_modes; 2595 mode_skip_mask |= all_intra_modes;
3160 } 2596 }
3161 2597
3162 if (!x->in_active_map) {
3163 int mode_index;
3164 assert(cpi->ref_frame_flags & VP9_LAST_FLAG);
3165 if (frame_mv[NEARESTMV][LAST_FRAME].as_int == 0)
3166 mode_index = THR_NEARESTMV;
3167 else if (frame_mv[NEARMV][LAST_FRAME].as_int == 0)
3168 mode_index = THR_NEARMV;
3169 else
3170 mode_index = THR_ZEROMV;
3171 mode_skip_mask = ~(1 << mode_index);
3172 mode_skip_start = MAX_MODES;
3173 inter_mode_mask = (1 << NEARESTMV) | (1 << NEARMV) | (1 << ZEROMV) |
3174 (1 << NEWMV);
3175 }
3176
3177 for (mode_index = 0; mode_index < MAX_MODES; ++mode_index) { 2598 for (mode_index = 0; mode_index < MAX_MODES; ++mode_index) {
3178 int mode_excluded = 0; 2599 int mode_excluded = 0;
3179 int64_t this_rd = INT64_MAX; 2600 int64_t this_rd = INT64_MAX;
3180 int disable_skip = 0; 2601 int disable_skip = 0;
3181 int compmode_cost = 0; 2602 int compmode_cost = 0;
3182 int rate2 = 0, rate_y = 0, rate_uv = 0; 2603 int rate2 = 0, rate_y = 0, rate_uv = 0;
3183 int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0; 2604 int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0;
3184 int skippable = 0; 2605 int skippable = 0;
3185 int64_t tx_cache[TX_MODES]; 2606 int64_t tx_cache[TX_MODES];
3186 int i; 2607 int i;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
3256 if (best_mode_index >= 0 && 2677 if (best_mode_index >= 0 &&
3257 vp9_mode_order[best_mode_index].ref_frame[0] > INTRA_FRAME) 2678 vp9_mode_order[best_mode_index].ref_frame[0] > INTRA_FRAME)
3258 continue; 2679 continue;
3259 } 2680 }
3260 if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { 2681 if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
3261 if (conditional_skipintra(this_mode, best_intra_mode)) 2682 if (conditional_skipintra(this_mode, best_intra_mode))
3262 continue; 2683 continue;
3263 } 2684 }
3264 } 2685 }
3265 } else { 2686 } else {
3266 if (x->in_active_map && 2687 const MV_REFERENCE_FRAME ref_frames[2] = {ref_frame, second_ref_frame};
3267 !vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) { 2688 if (!check_best_zero_mv(cpi, mbmi->mode_context, frame_mv,
3268 const MV_REFERENCE_FRAME ref_frames[2] = {ref_frame, second_ref_frame}; 2689 inter_mode_mask, this_mode, ref_frames))
3269 if (!check_best_zero_mv(cpi, mbmi->mode_context, frame_mv, 2690 continue;
3270 inter_mode_mask, this_mode, ref_frames))
3271 continue;
3272 }
3273 } 2691 }
3274 2692
3275 mbmi->mode = this_mode; 2693 mbmi->mode = this_mode;
3276 mbmi->uv_mode = x->in_active_map ? DC_PRED : this_mode; 2694 mbmi->uv_mode = DC_PRED;
3277 mbmi->ref_frame[0] = ref_frame; 2695 mbmi->ref_frame[0] = ref_frame;
3278 mbmi->ref_frame[1] = second_ref_frame; 2696 mbmi->ref_frame[1] = second_ref_frame;
3279 // Evaluate all sub-pel filters irrespective of whether we can use 2697 // Evaluate all sub-pel filters irrespective of whether we can use
3280 // them for this frame. 2698 // them for this frame.
3281 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP 2699 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP
3282 : cm->interp_filter; 2700 : cm->interp_filter;
3283 x->skip = 0; 2701 x->skip = 0;
3284 set_ref_ptrs(cm, xd, ref_frame, second_ref_frame); 2702 set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
3285 2703
3286 // Select prediction reference frames. 2704 // Select prediction reference frames.
3287 for (i = 0; i < MAX_MB_PLANE; i++) { 2705 for (i = 0; i < MAX_MB_PLANE; i++) {
3288 xd->plane[i].pre[0] = yv12_mb[ref_frame][i]; 2706 xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
3289 if (comp_pred) 2707 if (comp_pred)
3290 xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i]; 2708 xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
3291 } 2709 }
3292 2710
3293 for (i = 0; i < TX_MODES; ++i) 2711 for (i = 0; i < TX_MODES; ++i)
3294 tx_cache[i] = INT64_MAX; 2712 tx_cache[i] = INT64_MAX;
3295 2713
3296 if (ref_frame == INTRA_FRAME) { 2714 if (ref_frame == INTRA_FRAME) {
3297 TX_SIZE uv_tx; 2715 TX_SIZE uv_tx;
3298 intra_super_block_yrd(cpi, x, &rate_y, &distortion_y, &skippable, NULL, 2716 intra_super_block_yrd(cpi, x, &rate_y, &distortion_y, &skippable, NULL,
3299 bsize, tx_cache, best_rd); 2717 bsize, tx_cache, best_rd);
3300 2718
3301 if (rate_y == INT_MAX) 2719 if (rate_y == INT_MAX)
3302 continue; 2720 continue;
3303 2721
3304 uv_tx = get_uv_tx_size_impl(mbmi->tx_size, bsize); 2722 uv_tx = get_uv_tx_size_impl(mbmi->tx_size, bsize, pd[1].subsampling_x,
2723 pd[1].subsampling_y);
3305 if (rate_uv_intra[uv_tx] == INT_MAX) { 2724 if (rate_uv_intra[uv_tx] == INT_MAX) {
3306 choose_intra_uv_mode(cpi, ctx, bsize, uv_tx, 2725 choose_intra_uv_mode(cpi, ctx, bsize, uv_tx,
3307 &rate_uv_intra[uv_tx], &rate_uv_tokenonly[uv_tx], 2726 &rate_uv_intra[uv_tx], &rate_uv_tokenonly[uv_tx],
3308 &dist_uv[uv_tx], &skip_uv[uv_tx], &mode_uv[uv_tx]); 2727 &dist_uv[uv_tx], &skip_uv[uv_tx], &mode_uv[uv_tx]);
3309 } 2728 }
3310 2729
3311 rate_uv = rate_uv_tokenonly[uv_tx]; 2730 rate_uv = rate_uv_tokenonly[uv_tx];
3312 distortion_uv = dist_uv[uv_tx]; 2731 distortion_uv = dist_uv[uv_tx];
3313 skippable = skippable && skip_uv[uv_tx]; 2732 skippable = skippable && skip_uv[uv_tx];
3314 mbmi->uv_mode = mode_uv[uv_tx]; 2733 mbmi->uv_mode = mode_uv[uv_tx];
(...skipping 23 matching lines...) Expand all
3338 2757
3339 // Estimate the reference frame signaling cost and add it 2758 // Estimate the reference frame signaling cost and add it
3340 // to the rolling cost variable. 2759 // to the rolling cost variable.
3341 if (comp_pred) { 2760 if (comp_pred) {
3342 rate2 += ref_costs_comp[ref_frame]; 2761 rate2 += ref_costs_comp[ref_frame];
3343 } else { 2762 } else {
3344 rate2 += ref_costs_single[ref_frame]; 2763 rate2 += ref_costs_single[ref_frame];
3345 } 2764 }
3346 2765
3347 if (!disable_skip) { 2766 if (!disable_skip) {
3348 // Test for the condition where skip block will be activated 2767 if (skippable) {
3349 // because there are no non zero coefficients and make any 2768 vp9_prob skip_prob = vp9_get_skip_prob(cm, xd);
3350 // necessary adjustment for rate. Ignore if skip is coded at
3351 // segment level as the cost wont have been added in.
3352 // Is Mb level skip allowed (i.e. not coded at segment level).
3353 const int mb_skip_allowed = !vp9_segfeature_active(seg, segment_id,
3354 SEG_LVL_SKIP);
3355 2769
3356 if (skippable) {
3357 // Back out the coefficient coding costs 2770 // Back out the coefficient coding costs
3358 rate2 -= (rate_y + rate_uv); 2771 rate2 -= (rate_y + rate_uv);
3359 // for best yrd calculation 2772 // for best yrd calculation
3360 rate_uv = 0; 2773 rate_uv = 0;
3361 2774
3362 if (mb_skip_allowed) { 2775 // Cost the skip mb case
3363 int prob_skip_cost; 2776 if (skip_prob) {
3364 2777 int prob_skip_cost = vp9_cost_bit(skip_prob, 1);
3365 // Cost the skip mb case 2778 rate2 += prob_skip_cost;
3366 vp9_prob skip_prob = vp9_get_skip_prob(cm, xd);
3367 if (skip_prob) {
3368 prob_skip_cost = vp9_cost_bit(skip_prob, 1);
3369 rate2 += prob_skip_cost;
3370 }
3371 } 2779 }
3372 } else if (mb_skip_allowed && ref_frame != INTRA_FRAME && !xd->lossless) { 2780 } else if (ref_frame != INTRA_FRAME && !xd->lossless) {
3373 if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) < 2781 if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) <
3374 RDCOST(x->rdmult, x->rddiv, 0, total_sse)) { 2782 RDCOST(x->rdmult, x->rddiv, 0, total_sse)) {
3375 // Add in the cost of the no skip flag. 2783 // Add in the cost of the no skip flag.
3376 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0); 2784 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
3377 } else { 2785 } else {
3378 // FIXME(rbultje) make this work for splitmv also 2786 // FIXME(rbultje) make this work for splitmv also
3379 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1); 2787 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
3380 distortion2 = total_sse; 2788 distortion2 = total_sse;
3381 assert(total_sse >= 0); 2789 assert(total_sse >= 0);
3382 rate2 -= (rate_y + rate_uv); 2790 rate2 -= (rate_y + rate_uv);
3383 rate_y = 0; 2791 rate_y = 0;
3384 rate_uv = 0; 2792 rate_uv = 0;
3385 this_skip2 = 1; 2793 this_skip2 = 1;
3386 } 2794 }
3387 } else if (mb_skip_allowed) { 2795 } else {
3388 // Add in the cost of the no skip flag. 2796 // Add in the cost of the no skip flag.
3389 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0); 2797 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
3390 } 2798 }
3391 2799
3392 // Calculate the final RD estimate for this mode. 2800 // Calculate the final RD estimate for this mode.
3393 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); 2801 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
3394 } 2802 }
3395 2803
3396 if (ref_frame == INTRA_FRAME) { 2804 if (ref_frame == INTRA_FRAME) {
3397 // Keep record of best intra rd 2805 // Keep record of best intra rd
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
3539 2947
3540 if (best_mode_index < 0 || best_rd >= best_rd_so_far) 2948 if (best_mode_index < 0 || best_rd >= best_rd_so_far)
3541 return INT64_MAX; 2949 return INT64_MAX;
3542 2950
3543 // If we used an estimate for the uv intra rd in the loop above... 2951 // If we used an estimate for the uv intra rd in the loop above...
3544 if (cpi->sf.use_uv_intra_rd_estimate) { 2952 if (cpi->sf.use_uv_intra_rd_estimate) {
3545 // Do Intra UV best rd mode selection if best mode choice above was intra. 2953 // Do Intra UV best rd mode selection if best mode choice above was intra.
3546 if (vp9_mode_order[best_mode_index].ref_frame[0] == INTRA_FRAME) { 2954 if (vp9_mode_order[best_mode_index].ref_frame[0] == INTRA_FRAME) {
3547 TX_SIZE uv_tx_size; 2955 TX_SIZE uv_tx_size;
3548 *mbmi = best_mbmode; 2956 *mbmi = best_mbmode;
3549 uv_tx_size = get_uv_tx_size(mbmi); 2957 uv_tx_size = get_uv_tx_size(mbmi, &xd->plane[1]);
3550 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra[uv_tx_size], 2958 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra[uv_tx_size],
3551 &rate_uv_tokenonly[uv_tx_size], 2959 &rate_uv_tokenonly[uv_tx_size],
3552 &dist_uv[uv_tx_size], 2960 &dist_uv[uv_tx_size],
3553 &skip_uv[uv_tx_size], 2961 &skip_uv[uv_tx_size],
3554 bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize, 2962 bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize,
3555 uv_tx_size); 2963 uv_tx_size);
3556 } 2964 }
3557 } 2965 }
3558 2966
3559 assert((cm->interp_filter == SWITCHABLE) || 2967 assert((cm->interp_filter == SWITCHABLE) ||
(...skipping 26 matching lines...) Expand all
3586 if (best_tx_rd[i] == INT64_MAX) 2994 if (best_tx_rd[i] == INT64_MAX)
3587 best_tx_diff[i] = 0; 2995 best_tx_diff[i] = 0;
3588 else 2996 else
3589 best_tx_diff[i] = best_rd - best_tx_rd[i]; 2997 best_tx_diff[i] = best_rd - best_tx_rd[i];
3590 } 2998 }
3591 } else { 2999 } else {
3592 vp9_zero(best_filter_diff); 3000 vp9_zero(best_filter_diff);
3593 vp9_zero(best_tx_diff); 3001 vp9_zero(best_tx_diff);
3594 } 3002 }
3595 3003
3596 if (!x->in_active_map) {
3597 assert(mbmi->ref_frame[0] == LAST_FRAME);
3598 assert(mbmi->ref_frame[1] == NONE);
3599 assert(mbmi->mode == NEARESTMV ||
3600 mbmi->mode == NEARMV ||
3601 mbmi->mode == ZEROMV);
3602 assert(frame_mv[mbmi->mode][LAST_FRAME].as_int == 0);
3603 assert(mbmi->mode == mbmi->uv_mode);
3604 }
3605
3606 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); 3004 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
3607 store_coding_context(x, ctx, best_mode_index, 3005 store_coding_context(x, ctx, best_mode_index,
3608 best_pred_diff, best_tx_diff, best_filter_diff); 3006 best_pred_diff, best_tx_diff, best_filter_diff);
3609 3007
3610 return best_rd; 3008 return best_rd;
3611 } 3009 }
3612 3010
3011 int64_t vp9_rd_pick_inter_mode_sb_seg_skip(VP9_COMP *cpi, MACROBLOCK *x,
3012 int *returnrate,
3013 int64_t *returndistortion,
3014 BLOCK_SIZE bsize,
3015 PICK_MODE_CONTEXT *ctx,
3016 int64_t best_rd_so_far) {
3017 VP9_COMMON *const cm = &cpi->common;
3018 RD_OPT *const rd_opt = &cpi->rd;
3019 MACROBLOCKD *const xd = &x->e_mbd;
3020 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
3021 const struct segmentation *const seg = &cm->seg;
3022 unsigned char segment_id = mbmi->segment_id;
3023 const int comp_pred = 0;
3024 int i;
3025 int64_t best_tx_diff[TX_MODES];
3026 int64_t best_pred_diff[REFERENCE_MODES];
3027 int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
3028 unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
3029 vp9_prob comp_mode_p;
3030 INTERP_FILTER best_filter = SWITCHABLE;
3031 int64_t this_rd = INT64_MAX;
3032 int rate2 = 0;
3033 const int64_t distortion2 = 0;
3034
3035 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
3036
3037 estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
3038 &comp_mode_p);
3039
3040 for (i = 0; i < MAX_REF_FRAMES; ++i)
3041 x->pred_sse[i] = INT_MAX;
3042 for (i = LAST_FRAME; i < MAX_REF_FRAMES; ++i)
3043 x->pred_mv_sad[i] = INT_MAX;
3044
3045 *returnrate = INT_MAX;
3046
3047 assert(vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP));
3048
3049 mbmi->mode = ZEROMV;
3050 mbmi->uv_mode = DC_PRED;
3051 mbmi->ref_frame[0] = LAST_FRAME;
3052 mbmi->ref_frame[1] = NONE;
3053 mbmi->mv[0].as_int = 0;
3054 x->skip = 1;
3055
3056 // Search for best switchable filter by checking the variance of
3057 // pred error irrespective of whether the filter will be used
3058 rd_opt->mask_filter = 0;
3059 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
3060 rd_opt->filter_cache[i] = INT64_MAX;
3061
3062 if (cm->interp_filter != BILINEAR) {
3063 best_filter = EIGHTTAP;
3064 if (cm->interp_filter == SWITCHABLE &&
3065 x->source_variance >= cpi->sf.disable_filter_search_var_thresh) {
3066 int rs;
3067 int best_rs = INT_MAX;
3068 for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
3069 mbmi->interp_filter = i;
3070 rs = vp9_get_switchable_rate(cpi);
3071 if (rs < best_rs) {
3072 best_rs = rs;
3073 best_filter = mbmi->interp_filter;
3074 }
3075 }
3076 }
3077 }
3078 // Set the appropriate filter
3079 if (cm->interp_filter == SWITCHABLE) {
3080 mbmi->interp_filter = best_filter;
3081 rate2 += vp9_get_switchable_rate(cpi);
3082 } else {
3083 mbmi->interp_filter = cm->interp_filter;
3084 }
3085
3086 if (cm->reference_mode == REFERENCE_MODE_SELECT)
3087 rate2 += vp9_cost_bit(comp_mode_p, comp_pred);
3088
3089 // Estimate the reference frame signaling cost and add it
3090 // to the rolling cost variable.
3091 rate2 += ref_costs_single[LAST_FRAME];
3092 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
3093
3094 *returnrate = rate2;
3095 *returndistortion = distortion2;
3096
3097 if (this_rd >= best_rd_so_far)
3098 return INT64_MAX;
3099
3100 assert((cm->interp_filter == SWITCHABLE) ||
3101 (cm->interp_filter == mbmi->interp_filter));
3102
3103 update_rd_thresh_fact(cpi, bsize, THR_ZEROMV);
3104
3105 vp9_zero(best_pred_diff);
3106 vp9_zero(best_filter_diff);
3107 vp9_zero(best_tx_diff);
3108
3109 if (!x->select_tx_size)
3110 swap_block_ptr(x, ctx, 1, 0, 0, MAX_MB_PLANE);
3111 store_coding_context(x, ctx, THR_ZEROMV,
3112 best_pred_diff, best_tx_diff, best_filter_diff);
3113
3114 return this_rd;
3115 }
3613 3116
3614 int64_t vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, MACROBLOCK *x, 3117 int64_t vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, MACROBLOCK *x,
3615 const TileInfo *const tile, 3118 const TileInfo *const tile,
3616 int mi_row, int mi_col, 3119 int mi_row, int mi_col,
3617 int *returnrate, 3120 int *returnrate,
3618 int64_t *returndistortion, 3121 int64_t *returndistortion,
3619 BLOCK_SIZE bsize, 3122 BLOCK_SIZE bsize,
3620 PICK_MODE_CONTEXT *ctx, 3123 PICK_MODE_CONTEXT *ctx,
3621 int64_t best_rd_so_far) { 3124 int64_t best_rd_so_far) {
3622 VP9_COMMON *const cm = &cpi->common; 3125 VP9_COMMON *const cm = &cpi->common;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
3671 for (i = 0; i < REFERENCE_MODES; ++i) 3174 for (i = 0; i < REFERENCE_MODES; ++i)
3672 best_pred_rd[i] = INT64_MAX; 3175 best_pred_rd[i] = INT64_MAX;
3673 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) 3176 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
3674 best_filter_rd[i] = INT64_MAX; 3177 best_filter_rd[i] = INT64_MAX;
3675 rate_uv_intra = INT_MAX; 3178 rate_uv_intra = INT_MAX;
3676 3179
3677 *returnrate = INT_MAX; 3180 *returnrate = INT_MAX;
3678 3181
3679 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { 3182 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
3680 if (cpi->ref_frame_flags & flag_list[ref_frame]) { 3183 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
3681 vp9_setup_buffer_inter(cpi, x, tile, 3184 setup_buffer_inter(cpi, x, tile,
3682 ref_frame, bsize, mi_row, mi_col, 3185 ref_frame, bsize, mi_row, mi_col,
3683 frame_mv[NEARESTMV], frame_mv[NEARMV], 3186 frame_mv[NEARESTMV], frame_mv[NEARMV],
3684 yv12_mb); 3187 yv12_mb);
3685 } 3188 }
3686 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; 3189 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
3687 frame_mv[ZEROMV][ref_frame].as_int = 0; 3190 frame_mv[ZEROMV][ref_frame].as_int = 0;
3688 } 3191 }
3689 3192
3690 for (ref_index = 0; ref_index < MAX_REFS; ++ref_index) { 3193 for (ref_index = 0; ref_index < MAX_REFS; ++ref_index) {
3691 int mode_excluded = 0; 3194 int mode_excluded = 0;
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after
4210 } else { 3713 } else {
4211 vp9_zero(best_filter_diff); 3714 vp9_zero(best_filter_diff);
4212 } 3715 }
4213 3716
4214 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); 3717 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
4215 store_coding_context(x, ctx, best_ref_index, 3718 store_coding_context(x, ctx, best_ref_index,
4216 best_pred_diff, best_tx_diff, best_filter_diff); 3719 best_pred_diff, best_tx_diff, best_filter_diff);
4217 3720
4218 return best_rd; 3721 return best_rd;
4219 } 3722 }
4220
4221 void vp9_set_rd_speed_thresholds(VP9_COMP *cpi) {
4222 int i;
4223 RD_OPT *const rd = &cpi->rd;
4224
4225 // Set baseline threshold values
4226 for (i = 0; i < MAX_MODES; ++i)
4227 rd->thresh_mult[i] = is_best_mode(cpi->oxcf.mode) ? -500 : 0;
4228
4229 rd->thresh_mult[THR_NEARESTMV] = 0;
4230 rd->thresh_mult[THR_NEARESTG] = 0;
4231 rd->thresh_mult[THR_NEARESTA] = 0;
4232
4233 rd->thresh_mult[THR_DC] += 1000;
4234
4235 rd->thresh_mult[THR_NEWMV] += 1000;
4236 rd->thresh_mult[THR_NEWA] += 1000;
4237 rd->thresh_mult[THR_NEWG] += 1000;
4238
4239 rd->thresh_mult[THR_NEARMV] += 1000;
4240 rd->thresh_mult[THR_NEARA] += 1000;
4241 rd->thresh_mult[THR_COMP_NEARESTLA] += 1000;
4242 rd->thresh_mult[THR_COMP_NEARESTGA] += 1000;
4243
4244 rd->thresh_mult[THR_TM] += 1000;
4245
4246 rd->thresh_mult[THR_COMP_NEARLA] += 1500;
4247 rd->thresh_mult[THR_COMP_NEWLA] += 2000;
4248 rd->thresh_mult[THR_NEARG] += 1000;
4249 rd->thresh_mult[THR_COMP_NEARGA] += 1500;
4250 rd->thresh_mult[THR_COMP_NEWGA] += 2000;
4251
4252 rd->thresh_mult[THR_ZEROMV] += 2000;
4253 rd->thresh_mult[THR_ZEROG] += 2000;
4254 rd->thresh_mult[THR_ZEROA] += 2000;
4255 rd->thresh_mult[THR_COMP_ZEROLA] += 2500;
4256 rd->thresh_mult[THR_COMP_ZEROGA] += 2500;
4257
4258 rd->thresh_mult[THR_H_PRED] += 2000;
4259 rd->thresh_mult[THR_V_PRED] += 2000;
4260 rd->thresh_mult[THR_D45_PRED ] += 2500;
4261 rd->thresh_mult[THR_D135_PRED] += 2500;
4262 rd->thresh_mult[THR_D117_PRED] += 2500;
4263 rd->thresh_mult[THR_D153_PRED] += 2500;
4264 rd->thresh_mult[THR_D207_PRED] += 2500;
4265 rd->thresh_mult[THR_D63_PRED] += 2500;
4266
4267 /* disable frame modes if flags not set */
4268 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
4269 rd->thresh_mult[THR_NEWMV ] = INT_MAX;
4270 rd->thresh_mult[THR_NEARESTMV] = INT_MAX;
4271 rd->thresh_mult[THR_ZEROMV ] = INT_MAX;
4272 rd->thresh_mult[THR_NEARMV ] = INT_MAX;
4273 }
4274 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
4275 rd->thresh_mult[THR_NEARESTG ] = INT_MAX;
4276 rd->thresh_mult[THR_ZEROG ] = INT_MAX;
4277 rd->thresh_mult[THR_NEARG ] = INT_MAX;
4278 rd->thresh_mult[THR_NEWG ] = INT_MAX;
4279 }
4280 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
4281 rd->thresh_mult[THR_NEARESTA ] = INT_MAX;
4282 rd->thresh_mult[THR_ZEROA ] = INT_MAX;
4283 rd->thresh_mult[THR_NEARA ] = INT_MAX;
4284 rd->thresh_mult[THR_NEWA ] = INT_MAX;
4285 }
4286
4287 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
4288 (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
4289 rd->thresh_mult[THR_COMP_ZEROLA ] = INT_MAX;
4290 rd->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
4291 rd->thresh_mult[THR_COMP_NEARLA ] = INT_MAX;
4292 rd->thresh_mult[THR_COMP_NEWLA ] = INT_MAX;
4293 }
4294 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
4295 (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
4296 rd->thresh_mult[THR_COMP_ZEROGA ] = INT_MAX;
4297 rd->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
4298 rd->thresh_mult[THR_COMP_NEARGA ] = INT_MAX;
4299 rd->thresh_mult[THR_COMP_NEWGA ] = INT_MAX;
4300 }
4301 }
4302
4303 void vp9_set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) {
4304 const SPEED_FEATURES *const sf = &cpi->sf;
4305 RD_OPT *const rd = &cpi->rd;
4306 int i;
4307
4308 for (i = 0; i < MAX_REFS; ++i)
4309 rd->thresh_mult_sub8x8[i] = is_best_mode(cpi->oxcf.mode) ? -500 : 0;
4310
4311 rd->thresh_mult_sub8x8[THR_LAST] += 2500;
4312 rd->thresh_mult_sub8x8[THR_GOLD] += 2500;
4313 rd->thresh_mult_sub8x8[THR_ALTR] += 2500;
4314 rd->thresh_mult_sub8x8[THR_INTRA] += 2500;
4315 rd->thresh_mult_sub8x8[THR_COMP_LA] += 4500;
4316 rd->thresh_mult_sub8x8[THR_COMP_GA] += 4500;
4317
4318 // Check for masked out split cases.
4319 for (i = 0; i < MAX_REFS; i++)
4320 if (sf->disable_split_mask & (1 << i))
4321 rd->thresh_mult_sub8x8[i] = INT_MAX;
4322
4323 // disable mode test if frame flag is not set
4324 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
4325 rd->thresh_mult_sub8x8[THR_LAST] = INT_MAX;
4326 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
4327 rd->thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
4328 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
4329 rd->thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
4330 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
4331 (VP9_LAST_FLAG | VP9_ALT_FLAG))
4332 rd->thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
4333 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
4334 (VP9_GOLD_FLAG | VP9_ALT_FLAG))
4335 rd->thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
4336 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_rdopt.h ('k') | source/libvpx/vp9/encoder/vp9_segmentation.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698