Line data Source code
1 : /*
2 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 : *
4 : * This source code is subject to the terms of the BSD 2 Clause License and
5 : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 : * was not distributed with this source code in the LICENSE file, you can
7 : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 : * Media Patent License 1.0 was not distributed with this source code in the
9 : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 : */
11 :
12 : #ifndef AV1_ENCODER_RD_H_
13 : #define AV1_ENCODER_RD_H_
14 :
15 : #include <limits.h>
16 :
17 : #if CONFIG_ANS
18 : #include "aom_dsp/ans.h"
19 : #endif // CONFIG_ANS
20 : #include "av1/common/blockd.h"
21 :
22 : #include "av1/encoder/block.h"
23 : #include "av1/encoder/context_tree.h"
24 : #include "av1/encoder/cost.h"
25 :
26 : #ifdef __cplusplus
27 : extern "C" {
28 : #endif
29 :
30 : #define RDDIV_BITS 7
31 : #define RD_EPB_SHIFT 6
32 :
33 : #define RDCOST(RM, DM, R, D) \
34 : (ROUND_POWER_OF_TWO(((int64_t)R) * (RM), AV1_PROB_COST_SHIFT) + (D << DM))
35 :
36 : #define RDCOST_DBL(RM, DM, R, D) \
37 : (((((double)(R)) * (RM)) / (double)(1 << AV1_PROB_COST_SHIFT)) + \
38 : ((double)(D) * (1 << (DM))))
39 :
40 : #define QIDX_SKIP_THRESH 115
41 :
42 : #define MV_COST_WEIGHT 108
43 : #define MV_COST_WEIGHT_SUB 120
44 :
45 : #define INVALID_MV 0x80008000
46 :
47 : #if CONFIG_EXT_REFS
48 : #define MAX_REFS 15
49 : #else
50 : #define MAX_REFS 6
51 : #endif // CONFIG_EXT_REFS
52 :
53 : #define RD_THRESH_MAX_FACT 64
54 : #define RD_THRESH_INC 1
55 :
56 : // This enumerator type needs to be kept aligned with the mode order in
57 : // const MODE_DEFINITION av1_mode_order[MAX_MODES] used in the rd code.
58 : typedef enum {
59 : THR_NEARESTMV,
60 : #if CONFIG_EXT_REFS
61 : THR_NEARESTL2,
62 : THR_NEARESTL3,
63 : THR_NEARESTB,
64 : #endif // CONFIG_EXT_REFS
65 : THR_NEARESTA,
66 : THR_NEARESTG,
67 :
68 : THR_DC,
69 :
70 : THR_NEWMV,
71 : #if CONFIG_EXT_REFS
72 : THR_NEWL2,
73 : THR_NEWL3,
74 : THR_NEWB,
75 : #endif // CONFIG_EXT_REFS
76 : THR_NEWA,
77 : THR_NEWG,
78 :
79 : THR_NEARMV,
80 : #if CONFIG_EXT_REFS
81 : THR_NEARL2,
82 : THR_NEARL3,
83 : THR_NEARB,
84 : #endif // CONFIG_EXT_REFS
85 : THR_NEARA,
86 : THR_NEARG,
87 :
88 : THR_ZEROMV,
89 : #if CONFIG_EXT_REFS
90 : THR_ZEROL2,
91 : THR_ZEROL3,
92 : THR_ZEROB,
93 : #endif // CONFIG_EXT_REFS
94 : THR_ZEROG,
95 : THR_ZEROA,
96 :
97 : #if CONFIG_EXT_INTER
98 :
99 : THR_COMP_NEAREST_NEARESTLA,
100 : #if CONFIG_EXT_REFS
101 : THR_COMP_NEAREST_NEARESTL2A,
102 : THR_COMP_NEAREST_NEARESTL3A,
103 : #endif // CONFIG_EXT_REFS
104 : THR_COMP_NEAREST_NEARESTGA,
105 : #if CONFIG_EXT_REFS
106 : THR_COMP_NEAREST_NEARESTLB,
107 : THR_COMP_NEAREST_NEARESTL2B,
108 : THR_COMP_NEAREST_NEARESTL3B,
109 : THR_COMP_NEAREST_NEARESTGB,
110 : #endif // CONFIG_EXT_REFS
111 :
112 : #else // CONFIG_EXT_INTER
113 :
114 : THR_COMP_NEARESTLA,
115 : #if CONFIG_EXT_REFS
116 : THR_COMP_NEARESTL2A,
117 : THR_COMP_NEARESTL3A,
118 : #endif // CONFIG_EXT_REFS
119 : THR_COMP_NEARESTGA,
120 : #if CONFIG_EXT_REFS
121 : THR_COMP_NEARESTLB,
122 : THR_COMP_NEARESTL2B,
123 : THR_COMP_NEARESTL3B,
124 : THR_COMP_NEARESTGB,
125 : #endif // CONFIG_EXT_REFS
126 :
127 : #endif // CONFIG_EXT_INTER
128 :
129 : THR_TM,
130 :
131 : #if CONFIG_ALT_INTRA
132 : THR_SMOOTH,
133 : #if CONFIG_SMOOTH_HV
134 : THR_SMOOTH_V,
135 : THR_SMOOTH_H,
136 : #endif // CONFIG_SMOOTH_HV
137 : #endif // CONFIG_ALT_INTRA
138 :
139 : #if CONFIG_EXT_INTER
140 :
141 : THR_COMP_NEAR_NEARESTLA,
142 : THR_COMP_NEAREST_NEARLA,
143 : THR_COMP_NEAR_NEARLA,
144 : THR_COMP_NEW_NEARESTLA,
145 : THR_COMP_NEAREST_NEWLA,
146 : THR_COMP_NEW_NEARLA,
147 : THR_COMP_NEAR_NEWLA,
148 : THR_COMP_NEW_NEWLA,
149 : THR_COMP_ZERO_ZEROLA,
150 :
151 : #if CONFIG_EXT_REFS
152 : THR_COMP_NEAR_NEARESTL2A,
153 : THR_COMP_NEAREST_NEARL2A,
154 : THR_COMP_NEAR_NEARL2A,
155 : THR_COMP_NEW_NEARESTL2A,
156 : THR_COMP_NEAREST_NEWL2A,
157 : THR_COMP_NEW_NEARL2A,
158 : THR_COMP_NEAR_NEWL2A,
159 : THR_COMP_NEW_NEWL2A,
160 : THR_COMP_ZERO_ZEROL2A,
161 :
162 : THR_COMP_NEAR_NEARESTL3A,
163 : THR_COMP_NEAREST_NEARL3A,
164 : THR_COMP_NEAR_NEARL3A,
165 : THR_COMP_NEW_NEARESTL3A,
166 : THR_COMP_NEAREST_NEWL3A,
167 : THR_COMP_NEW_NEARL3A,
168 : THR_COMP_NEAR_NEWL3A,
169 : THR_COMP_NEW_NEWL3A,
170 : THR_COMP_ZERO_ZEROL3A,
171 : #endif // CONFIG_EXT_REFS
172 :
173 : THR_COMP_NEAR_NEARESTGA,
174 : THR_COMP_NEAREST_NEARGA,
175 : THR_COMP_NEAR_NEARGA,
176 : THR_COMP_NEW_NEARESTGA,
177 : THR_COMP_NEAREST_NEWGA,
178 : THR_COMP_NEW_NEARGA,
179 : THR_COMP_NEAR_NEWGA,
180 : THR_COMP_NEW_NEWGA,
181 : THR_COMP_ZERO_ZEROGA,
182 :
183 : #if CONFIG_EXT_REFS
184 : THR_COMP_NEAR_NEARESTLB,
185 : THR_COMP_NEAREST_NEARLB,
186 : THR_COMP_NEAR_NEARLB,
187 : THR_COMP_NEW_NEARESTLB,
188 : THR_COMP_NEAREST_NEWLB,
189 : THR_COMP_NEW_NEARLB,
190 : THR_COMP_NEAR_NEWLB,
191 : THR_COMP_NEW_NEWLB,
192 : THR_COMP_ZERO_ZEROLB,
193 :
194 : THR_COMP_NEAR_NEARESTL2B,
195 : THR_COMP_NEAREST_NEARL2B,
196 : THR_COMP_NEAR_NEARL2B,
197 : THR_COMP_NEW_NEARESTL2B,
198 : THR_COMP_NEAREST_NEWL2B,
199 : THR_COMP_NEW_NEARL2B,
200 : THR_COMP_NEAR_NEWL2B,
201 : THR_COMP_NEW_NEWL2B,
202 : THR_COMP_ZERO_ZEROL2B,
203 :
204 : THR_COMP_NEAR_NEARESTL3B,
205 : THR_COMP_NEAREST_NEARL3B,
206 : THR_COMP_NEAR_NEARL3B,
207 : THR_COMP_NEW_NEARESTL3B,
208 : THR_COMP_NEAREST_NEWL3B,
209 : THR_COMP_NEW_NEARL3B,
210 : THR_COMP_NEAR_NEWL3B,
211 : THR_COMP_NEW_NEWL3B,
212 : THR_COMP_ZERO_ZEROL3B,
213 :
214 : THR_COMP_NEAR_NEARESTGB,
215 : THR_COMP_NEAREST_NEARGB,
216 : THR_COMP_NEAR_NEARGB,
217 : THR_COMP_NEW_NEARESTGB,
218 : THR_COMP_NEAREST_NEWGB,
219 : THR_COMP_NEW_NEARGB,
220 : THR_COMP_NEAR_NEWGB,
221 : THR_COMP_NEW_NEWGB,
222 : THR_COMP_ZERO_ZEROGB,
223 : #endif // CONFIG_EXT_REFS
224 :
225 : #else // CONFIG_EXT_INTER
226 :
227 : THR_COMP_NEARLA,
228 : THR_COMP_NEWLA,
229 : #if CONFIG_EXT_REFS
230 : THR_COMP_NEARL2A,
231 : THR_COMP_NEWL2A,
232 : THR_COMP_NEARL3A,
233 : THR_COMP_NEWL3A,
234 : #endif // CONFIG_EXT_REFS
235 : THR_COMP_NEARGA,
236 : THR_COMP_NEWGA,
237 :
238 : #if CONFIG_EXT_REFS
239 : THR_COMP_NEARLB,
240 : THR_COMP_NEWLB,
241 : THR_COMP_NEARL2B,
242 : THR_COMP_NEWL2B,
243 : THR_COMP_NEARL3B,
244 : THR_COMP_NEWL3B,
245 : THR_COMP_NEARGB,
246 : THR_COMP_NEWGB,
247 : #endif // CONFIG_EXT_REFS
248 :
249 : THR_COMP_ZEROLA,
250 : #if CONFIG_EXT_REFS
251 : THR_COMP_ZEROL2A,
252 : THR_COMP_ZEROL3A,
253 : #endif // CONFIG_EXT_REFS
254 : THR_COMP_ZEROGA,
255 :
256 : #if CONFIG_EXT_REFS
257 : THR_COMP_ZEROLB,
258 : THR_COMP_ZEROL2B,
259 : THR_COMP_ZEROL3B,
260 : THR_COMP_ZEROGB,
261 : #endif // CONFIG_EXT_REFS
262 :
263 : #endif // CONFIG_EXT_INTER
264 :
265 : THR_H_PRED,
266 : THR_V_PRED,
267 : THR_D135_PRED,
268 : THR_D207_PRED,
269 : THR_D153_PRED,
270 : THR_D63_PRED,
271 : THR_D117_PRED,
272 : THR_D45_PRED,
273 :
274 : #if CONFIG_EXT_INTER
275 : THR_COMP_INTERINTRA_ZEROL,
276 : THR_COMP_INTERINTRA_NEARESTL,
277 : THR_COMP_INTERINTRA_NEARL,
278 : THR_COMP_INTERINTRA_NEWL,
279 :
280 : #if CONFIG_EXT_REFS
281 : THR_COMP_INTERINTRA_ZEROL2,
282 : THR_COMP_INTERINTRA_NEARESTL2,
283 : THR_COMP_INTERINTRA_NEARL2,
284 : THR_COMP_INTERINTRA_NEWL2,
285 :
286 : THR_COMP_INTERINTRA_ZEROL3,
287 : THR_COMP_INTERINTRA_NEARESTL3,
288 : THR_COMP_INTERINTRA_NEARL3,
289 : THR_COMP_INTERINTRA_NEWL3,
290 : #endif // CONFIG_EXT_REFS
291 :
292 : THR_COMP_INTERINTRA_ZEROG,
293 : THR_COMP_INTERINTRA_NEARESTG,
294 : THR_COMP_INTERINTRA_NEARG,
295 : THR_COMP_INTERINTRA_NEWG,
296 :
297 : #if CONFIG_EXT_REFS
298 : THR_COMP_INTERINTRA_ZEROB,
299 : THR_COMP_INTERINTRA_NEARESTB,
300 : THR_COMP_INTERINTRA_NEARB,
301 : THR_COMP_INTERINTRA_NEWB,
302 : #endif // CONFIG_EXT_REFS
303 :
304 : THR_COMP_INTERINTRA_ZEROA,
305 : THR_COMP_INTERINTRA_NEARESTA,
306 : THR_COMP_INTERINTRA_NEARA,
307 : THR_COMP_INTERINTRA_NEWA,
308 : #endif // CONFIG_EXT_INTER
309 : MAX_MODES
310 : } THR_MODES;
311 :
312 : typedef enum {
313 : THR_LAST,
314 : #if CONFIG_EXT_REFS
315 : THR_LAST2,
316 : THR_LAST3,
317 : THR_BWDR,
318 : #endif // CONFIG_EXT_REFS
319 : THR_GOLD,
320 : THR_ALTR,
321 :
322 : THR_COMP_LA,
323 : #if CONFIG_EXT_REFS
324 : THR_COMP_L2A,
325 : THR_COMP_L3A,
326 : #endif // CONFIG_EXT_REFS
327 : THR_COMP_GA,
328 :
329 : #if CONFIG_EXT_REFS
330 : THR_COMP_LB,
331 : THR_COMP_L2B,
332 : THR_COMP_L3B,
333 : THR_COMP_GB,
334 : #endif // CONFIG_EXT_REFS
335 :
336 : THR_INTRA,
337 : } THR_MODES_SUB8X8;
338 :
339 : typedef struct RD_OPT {
340 : // Thresh_mult is used to set a threshold for the rd score. A higher value
341 : // means that we will accept the best mode so far more often. This number
342 : // is used in combination with the current block size, and thresh_freq_fact
343 : // to pick a threshold.
344 : int thresh_mult[MAX_MODES];
345 : int thresh_mult_sub8x8[MAX_REFS];
346 :
347 : int threshes[MAX_SEGMENTS][BLOCK_SIZES][MAX_MODES];
348 :
349 : int64_t prediction_type_threshes[TOTAL_REFS_PER_FRAME][REFERENCE_MODES];
350 :
351 : int RDMULT;
352 : int RDDIV;
353 : } RD_OPT;
354 :
355 0 : static INLINE void av1_init_rd_stats(RD_STATS *rd_stats) {
356 : #if CONFIG_RD_DEBUG
357 : int plane;
358 : #endif
359 0 : rd_stats->rate = 0;
360 0 : rd_stats->dist = 0;
361 0 : rd_stats->rdcost = 0;
362 0 : rd_stats->sse = 0;
363 0 : rd_stats->skip = 1;
364 : #if CONFIG_DAALA_DIST && CONFIG_CB4X4
365 : rd_stats->dist_y = 0;
366 : #endif
367 : #if CONFIG_RD_DEBUG
368 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
369 : rd_stats->txb_coeff_cost[plane] = 0;
370 : #if CONFIG_VAR_TX
371 : {
372 : int r, c;
373 : for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r)
374 : for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c)
375 : rd_stats->txb_coeff_cost_map[plane][r][c] = 0;
376 : }
377 : #endif
378 : }
379 : #endif
380 0 : }
381 :
382 0 : static INLINE void av1_invalid_rd_stats(RD_STATS *rd_stats) {
383 : #if CONFIG_RD_DEBUG
384 : int plane;
385 : #endif
386 0 : rd_stats->rate = INT_MAX;
387 0 : rd_stats->dist = INT64_MAX;
388 0 : rd_stats->rdcost = INT64_MAX;
389 0 : rd_stats->sse = INT64_MAX;
390 0 : rd_stats->skip = 0;
391 : #if CONFIG_DAALA_DIST && CONFIG_CB4X4
392 : rd_stats->dist_y = INT64_MAX;
393 : #endif
394 : #if CONFIG_RD_DEBUG
395 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
396 : rd_stats->txb_coeff_cost[plane] = INT_MAX;
397 : #if CONFIG_VAR_TX
398 : {
399 : int r, c;
400 : for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r)
401 : for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c)
402 : rd_stats->txb_coeff_cost_map[plane][r][c] = INT_MAX;
403 : }
404 : #endif
405 : }
406 : #endif
407 0 : }
408 :
409 0 : static INLINE void av1_merge_rd_stats(RD_STATS *rd_stats_dst,
410 : const RD_STATS *rd_stats_src) {
411 : #if CONFIG_RD_DEBUG
412 : int plane;
413 : #endif
414 0 : rd_stats_dst->rate += rd_stats_src->rate;
415 0 : rd_stats_dst->dist += rd_stats_src->dist;
416 0 : rd_stats_dst->sse += rd_stats_src->sse;
417 0 : rd_stats_dst->skip &= rd_stats_src->skip;
418 : #if CONFIG_DAALA_DIST && CONFIG_CB4X4
419 : rd_stats_dst->dist_y += rd_stats_src->dist_y;
420 : #endif
421 : #if CONFIG_RD_DEBUG
422 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
423 : rd_stats_dst->txb_coeff_cost[plane] += rd_stats_src->txb_coeff_cost[plane];
424 : #if CONFIG_VAR_TX
425 : {
426 : // TODO(angiebird): optimize this part
427 : int r, c;
428 : int ref_txb_coeff_cost = 0;
429 : for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r)
430 : for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) {
431 : rd_stats_dst->txb_coeff_cost_map[plane][r][c] +=
432 : rd_stats_src->txb_coeff_cost_map[plane][r][c];
433 : ref_txb_coeff_cost += rd_stats_dst->txb_coeff_cost_map[plane][r][c];
434 : }
435 : assert(ref_txb_coeff_cost == rd_stats_dst->txb_coeff_cost[plane]);
436 : }
437 : #endif
438 : }
439 : #endif
440 0 : }
441 :
442 : struct TileInfo;
443 : struct TileDataEnc;
444 : struct AV1_COMP;
445 : struct macroblock;
446 :
447 : int av1_compute_rd_mult(const struct AV1_COMP *cpi, int qindex);
448 :
449 : void av1_initialize_rd_consts(struct AV1_COMP *cpi);
450 :
451 : void av1_initialize_me_consts(const struct AV1_COMP *cpi, MACROBLOCK *x,
452 : int qindex);
453 :
454 : void av1_model_rd_from_var_lapndz(int64_t var, unsigned int n,
455 : unsigned int qstep, int *rate, int64_t *dist);
456 :
457 : int av1_get_switchable_rate(const struct AV1_COMP *cpi, const MACROBLOCKD *xd);
458 :
459 : int av1_raster_block_offset(BLOCK_SIZE plane_bsize, int raster_block,
460 : int stride);
461 :
462 : int16_t *av1_raster_block_offset_int16(BLOCK_SIZE plane_bsize, int raster_block,
463 : int16_t *base);
464 :
465 : YV12_BUFFER_CONFIG *av1_get_scaled_ref_frame(const struct AV1_COMP *cpi,
466 : int ref_frame);
467 :
468 : void av1_init_me_luts(void);
469 :
470 : void av1_set_mvcost(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame, int ref,
471 : int ref_mv_idx);
472 :
473 : void av1_get_entropy_contexts(BLOCK_SIZE bsize, TX_SIZE tx_size,
474 : const struct macroblockd_plane *pd,
475 : ENTROPY_CONTEXT t_above[2 * MAX_MIB_SIZE],
476 : ENTROPY_CONTEXT t_left[2 * MAX_MIB_SIZE]);
477 :
478 : void av1_set_rd_speed_thresholds(struct AV1_COMP *cpi);
479 :
480 : void av1_set_rd_speed_thresholds_sub8x8(struct AV1_COMP *cpi);
481 :
482 : void av1_update_rd_thresh_fact(const AV1_COMMON *const cm,
483 : int (*fact)[MAX_MODES], int rd_thresh, int bsize,
484 : int best_mode_index);
485 :
486 : void av1_fill_token_costs(av1_coeff_cost *c,
487 : av1_coeff_probs_model (*p)[PLANE_TYPES]);
488 :
489 : static INLINE int rd_less_than_thresh(int64_t best_rd, int thresh,
490 : int thresh_fact) {
491 : return best_rd < ((int64_t)thresh * thresh_fact >> 5) || thresh == INT_MAX;
492 : }
493 :
494 : void av1_mv_pred(const struct AV1_COMP *cpi, MACROBLOCK *x,
495 : uint8_t *ref_y_buffer, int ref_y_stride, int ref_frame,
496 : BLOCK_SIZE block_size);
497 :
498 0 : static INLINE void set_error_per_bit(MACROBLOCK *x, int rdmult) {
499 0 : x->errorperbit = rdmult >> RD_EPB_SHIFT;
500 0 : x->errorperbit += (x->errorperbit == 0);
501 0 : }
502 :
503 : void av1_setup_pred_block(const MACROBLOCKD *xd,
504 : struct buf_2d dst[MAX_MB_PLANE],
505 : const YV12_BUFFER_CONFIG *src, int mi_row, int mi_col,
506 : const struct scale_factors *scale,
507 : const struct scale_factors *scale_uv);
508 :
509 : int av1_get_intra_cost_penalty(int qindex, int qdelta,
510 : aom_bit_depth_t bit_depth);
511 :
512 : #ifdef __cplusplus
513 : } // extern "C"
514 : #endif
515 :
516 : #endif // AV1_ENCODER_RD_H_
|