/src/aom/av1/encoder/firstpass.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include <limits.h> |
13 | | #include <math.h> |
14 | | #include <stdio.h> |
15 | | |
16 | | #include "config/aom_dsp_rtcd.h" |
17 | | #include "config/aom_scale_rtcd.h" |
18 | | |
19 | | #include "aom_dsp/aom_dsp_common.h" |
20 | | #include "aom_dsp/variance.h" |
21 | | #include "aom_mem/aom_mem.h" |
22 | | #include "aom_ports/mem.h" |
23 | | #include "aom_scale/aom_scale.h" |
24 | | #include "aom_scale/yv12config.h" |
25 | | |
26 | | #include "av1/common/entropymv.h" |
27 | | #include "av1/common/quant_common.h" |
28 | | #include "av1/common/reconinter.h" // av1_setup_dst_planes() |
29 | | #include "av1/common/reconintra.h" |
30 | | #include "av1/common/txb_common.h" |
31 | | #include "av1/encoder/aq_variance.h" |
32 | | #include "av1/encoder/av1_quantize.h" |
33 | | #include "av1/encoder/block.h" |
34 | | #include "av1/encoder/dwt.h" |
35 | | #include "av1/encoder/encodeframe.h" |
36 | | #include "av1/encoder/encodemb.h" |
37 | | #include "av1/encoder/encodemv.h" |
38 | | #include "av1/encoder/encoder.h" |
39 | | #include "av1/encoder/encode_strategy.h" |
40 | | #include "av1/encoder/ethread.h" |
41 | | #include "av1/encoder/extend.h" |
42 | | #include "av1/encoder/firstpass.h" |
43 | | #include "av1/encoder/mcomp.h" |
44 | | #include "av1/encoder/rd.h" |
45 | | #include "av1/encoder/reconinter_enc.h" |
46 | | |
47 | | #define OUTPUT_FPF 0 |
48 | | |
49 | 0 | #define FIRST_PASS_Q 10.0 |
50 | 0 | #define INTRA_MODE_PENALTY 1024 |
51 | 0 | #define NEW_MV_MODE_PENALTY 32 |
52 | 0 | #define DARK_THRESH 64 |
53 | | |
54 | 0 | #define NCOUNT_INTRA_THRESH 8192 |
55 | 0 | #define NCOUNT_INTRA_FACTOR 3 |
56 | | |
57 | 0 | #define INVALID_FP_STATS_TO_PREDICT_FLAT_GOP -1 |
58 | | |
59 | | static AOM_INLINE void output_stats(FIRSTPASS_STATS *stats, |
60 | 0 | struct aom_codec_pkt_list *pktlist) { |
61 | 0 | struct aom_codec_cx_pkt pkt; |
62 | 0 | pkt.kind = AOM_CODEC_STATS_PKT; |
63 | 0 | pkt.data.twopass_stats.buf = stats; |
64 | 0 | pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); |
65 | 0 | if (pktlist != NULL) aom_codec_pkt_list_add(pktlist, &pkt); |
66 | | |
67 | | // TEMP debug code |
68 | | #if OUTPUT_FPF |
69 | | { |
70 | | FILE *fpfile; |
71 | | fpfile = fopen("firstpass.stt", "a"); |
72 | | |
73 | | fprintf(fpfile, |
74 | | "%12.0lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf %12.4lf" |
75 | | "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf" |
76 | | "%12.4lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf %12.4lf\n", |
77 | | stats->frame, stats->weight, stats->intra_error, stats->coded_error, |
78 | | stats->sr_coded_error, stats->pcnt_inter, stats->pcnt_motion, |
79 | | stats->pcnt_second_ref, stats->pcnt_neutral, stats->intra_skip_pct, |
80 | | stats->inactive_zone_rows, stats->inactive_zone_cols, stats->MVr, |
81 | | stats->mvr_abs, stats->MVc, stats->mvc_abs, stats->MVrv, |
82 | | stats->MVcv, stats->mv_in_out_count, stats->new_mv_count, |
83 | | stats->count, stats->duration); |
84 | | fclose(fpfile); |
85 | | } |
86 | | #endif |
87 | 0 | } |
88 | | |
89 | 2.52k | void av1_twopass_zero_stats(FIRSTPASS_STATS *section) { |
90 | 2.52k | section->frame = 0.0; |
91 | 2.52k | section->weight = 0.0; |
92 | 2.52k | section->intra_error = 0.0; |
93 | 2.52k | section->frame_avg_wavelet_energy = 0.0; |
94 | 2.52k | section->coded_error = 0.0; |
95 | 2.52k | section->sr_coded_error = 0.0; |
96 | 2.52k | section->pcnt_inter = 0.0; |
97 | 2.52k | section->pcnt_motion = 0.0; |
98 | 2.52k | section->pcnt_second_ref = 0.0; |
99 | 2.52k | section->pcnt_neutral = 0.0; |
100 | 2.52k | section->intra_skip_pct = 0.0; |
101 | 2.52k | section->inactive_zone_rows = 0.0; |
102 | 2.52k | section->inactive_zone_cols = 0.0; |
103 | 2.52k | section->MVr = 0.0; |
104 | 2.52k | section->mvr_abs = 0.0; |
105 | 2.52k | section->MVc = 0.0; |
106 | 2.52k | section->mvc_abs = 0.0; |
107 | 2.52k | section->MVrv = 0.0; |
108 | 2.52k | section->MVcv = 0.0; |
109 | 2.52k | section->mv_in_out_count = 0.0; |
110 | 2.52k | section->new_mv_count = 0.0; |
111 | 2.52k | section->count = 0.0; |
112 | 2.52k | section->duration = 1.0; |
113 | 2.52k | section->is_flash = 0; |
114 | 2.52k | section->noise_var = 0; |
115 | 2.52k | section->cor_coeff = 1.0; |
116 | 2.52k | } |
117 | | |
118 | | void av1_accumulate_stats(FIRSTPASS_STATS *section, |
119 | 0 | const FIRSTPASS_STATS *frame) { |
120 | 0 | section->frame += frame->frame; |
121 | 0 | section->weight += frame->weight; |
122 | 0 | section->intra_error += frame->intra_error; |
123 | 0 | section->frame_avg_wavelet_energy += frame->frame_avg_wavelet_energy; |
124 | 0 | section->coded_error += frame->coded_error; |
125 | 0 | section->sr_coded_error += frame->sr_coded_error; |
126 | 0 | section->pcnt_inter += frame->pcnt_inter; |
127 | 0 | section->pcnt_motion += frame->pcnt_motion; |
128 | 0 | section->pcnt_second_ref += frame->pcnt_second_ref; |
129 | 0 | section->pcnt_neutral += frame->pcnt_neutral; |
130 | 0 | section->intra_skip_pct += frame->intra_skip_pct; |
131 | 0 | section->inactive_zone_rows += frame->inactive_zone_rows; |
132 | 0 | section->inactive_zone_cols += frame->inactive_zone_cols; |
133 | 0 | section->MVr += frame->MVr; |
134 | 0 | section->mvr_abs += frame->mvr_abs; |
135 | 0 | section->MVc += frame->MVc; |
136 | 0 | section->mvc_abs += frame->mvc_abs; |
137 | 0 | section->MVrv += frame->MVrv; |
138 | 0 | section->MVcv += frame->MVcv; |
139 | 0 | section->mv_in_out_count += frame->mv_in_out_count; |
140 | 0 | section->new_mv_count += frame->new_mv_count; |
141 | 0 | section->count += frame->count; |
142 | 0 | section->duration += frame->duration; |
143 | 0 | } |
144 | | |
145 | 0 | static int get_unit_rows(const BLOCK_SIZE fp_block_size, const int mb_rows) { |
146 | 0 | const int height_mi_log2 = mi_size_high_log2[fp_block_size]; |
147 | 0 | const int mb_height_mi_log2 = mi_size_high_log2[BLOCK_16X16]; |
148 | 0 | if (height_mi_log2 > mb_height_mi_log2) { |
149 | 0 | return mb_rows >> (height_mi_log2 - mb_height_mi_log2); |
150 | 0 | } |
151 | | |
152 | 0 | return mb_rows << (mb_height_mi_log2 - height_mi_log2); |
153 | 0 | } |
154 | | |
155 | 0 | static int get_unit_cols(const BLOCK_SIZE fp_block_size, const int mb_cols) { |
156 | 0 | const int width_mi_log2 = mi_size_wide_log2[fp_block_size]; |
157 | 0 | const int mb_width_mi_log2 = mi_size_wide_log2[BLOCK_16X16]; |
158 | 0 | if (width_mi_log2 > mb_width_mi_log2) { |
159 | 0 | return mb_cols >> (width_mi_log2 - mb_width_mi_log2); |
160 | 0 | } |
161 | | |
162 | 0 | return mb_cols << (mb_width_mi_log2 - width_mi_log2); |
163 | 0 | } |
164 | | |
165 | | // TODO(chengchen): can we simplify it even if resize has to be considered? |
166 | | static int get_num_mbs(const BLOCK_SIZE fp_block_size, |
167 | 0 | const int num_mbs_16X16) { |
168 | 0 | const int width_mi_log2 = mi_size_wide_log2[fp_block_size]; |
169 | 0 | const int height_mi_log2 = mi_size_high_log2[fp_block_size]; |
170 | 0 | const int mb_width_mi_log2 = mi_size_wide_log2[BLOCK_16X16]; |
171 | 0 | const int mb_height_mi_log2 = mi_size_high_log2[BLOCK_16X16]; |
172 | | // TODO(chengchen): Now this function assumes a square block is used. |
173 | | // It does not support rectangular block sizes. |
174 | 0 | assert(width_mi_log2 == height_mi_log2); |
175 | 0 | if (width_mi_log2 > mb_width_mi_log2) { |
176 | 0 | return num_mbs_16X16 >> ((width_mi_log2 - mb_width_mi_log2) + |
177 | 0 | (height_mi_log2 - mb_height_mi_log2)); |
178 | 0 | } |
179 | | |
180 | 0 | return num_mbs_16X16 << ((mb_width_mi_log2 - width_mi_log2) + |
181 | 0 | (mb_height_mi_log2 - height_mi_log2)); |
182 | 0 | } |
183 | | |
184 | 0 | void av1_end_first_pass(AV1_COMP *cpi) { |
185 | 0 | if (cpi->ppi->twopass.stats_buf_ctx->total_stats && !cpi->ppi->lap_enabled) |
186 | 0 | output_stats(cpi->ppi->twopass.stats_buf_ctx->total_stats, |
187 | 0 | cpi->ppi->output_pkt_list); |
188 | 0 | } |
189 | | |
190 | 0 | static aom_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) { |
191 | 0 | switch (bsize) { |
192 | 0 | case BLOCK_8X8: return aom_mse8x8; |
193 | 0 | case BLOCK_16X8: return aom_mse16x8; |
194 | 0 | case BLOCK_8X16: return aom_mse8x16; |
195 | 0 | default: return aom_mse16x16; |
196 | 0 | } |
197 | 0 | } |
198 | | |
199 | | static unsigned int get_prediction_error(BLOCK_SIZE bsize, |
200 | | const struct buf_2d *src, |
201 | 0 | const struct buf_2d *ref) { |
202 | 0 | unsigned int sse; |
203 | 0 | const aom_variance_fn_t fn = get_block_variance_fn(bsize); |
204 | 0 | fn(src->buf, src->stride, ref->buf, ref->stride, &sse); |
205 | 0 | return sse; |
206 | 0 | } |
207 | | |
208 | | #if CONFIG_AV1_HIGHBITDEPTH |
209 | | static aom_variance_fn_t highbd_get_block_variance_fn(BLOCK_SIZE bsize, |
210 | 0 | int bd) { |
211 | 0 | switch (bd) { |
212 | 0 | default: |
213 | 0 | switch (bsize) { |
214 | 0 | case BLOCK_8X8: return aom_highbd_8_mse8x8; |
215 | 0 | case BLOCK_16X8: return aom_highbd_8_mse16x8; |
216 | 0 | case BLOCK_8X16: return aom_highbd_8_mse8x16; |
217 | 0 | default: return aom_highbd_8_mse16x16; |
218 | 0 | } |
219 | 0 | break; |
220 | 0 | case 10: |
221 | 0 | switch (bsize) { |
222 | 0 | case BLOCK_8X8: return aom_highbd_10_mse8x8; |
223 | 0 | case BLOCK_16X8: return aom_highbd_10_mse16x8; |
224 | 0 | case BLOCK_8X16: return aom_highbd_10_mse8x16; |
225 | 0 | default: return aom_highbd_10_mse16x16; |
226 | 0 | } |
227 | 0 | break; |
228 | 0 | case 12: |
229 | 0 | switch (bsize) { |
230 | 0 | case BLOCK_8X8: return aom_highbd_12_mse8x8; |
231 | 0 | case BLOCK_16X8: return aom_highbd_12_mse16x8; |
232 | 0 | case BLOCK_8X16: return aom_highbd_12_mse8x16; |
233 | 0 | default: return aom_highbd_12_mse16x16; |
234 | 0 | } |
235 | 0 | break; |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | | static unsigned int highbd_get_prediction_error(BLOCK_SIZE bsize, |
240 | | const struct buf_2d *src, |
241 | | const struct buf_2d *ref, |
242 | 0 | int bd) { |
243 | 0 | unsigned int sse; |
244 | 0 | const aom_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd); |
245 | 0 | fn(src->buf, src->stride, ref->buf, ref->stride, &sse); |
246 | 0 | return sse; |
247 | 0 | } |
248 | | #endif // CONFIG_AV1_HIGHBITDEPTH |
249 | | |
250 | | // Refine the motion search range according to the frame dimension |
251 | | // for first pass test. |
252 | 0 | static int get_search_range(const InitialDimensions *initial_dimensions) { |
253 | 0 | int sr = 0; |
254 | 0 | const int dim = AOMMIN(initial_dimensions->width, initial_dimensions->height); |
255 | |
|
256 | 0 | while ((dim << sr) < MAX_FULL_PEL_VAL) ++sr; |
257 | 0 | return sr; |
258 | 0 | } |
259 | | |
260 | | static AOM_INLINE void first_pass_motion_search(AV1_COMP *cpi, MACROBLOCK *x, |
261 | | const MV *ref_mv, |
262 | | FULLPEL_MV *best_mv, |
263 | 0 | int *best_motion_err) { |
264 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
265 | 0 | FULLPEL_MV start_mv = get_fullmv_from_mv(ref_mv); |
266 | 0 | int tmp_err; |
267 | 0 | const BLOCK_SIZE bsize = xd->mi[0]->bsize; |
268 | 0 | const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY; |
269 | 0 | const int sr = get_search_range(&cpi->initial_dimensions); |
270 | 0 | const int step_param = cpi->sf.fp_sf.reduce_mv_step_param + sr; |
271 | |
|
272 | 0 | const search_site_config *first_pass_search_sites = |
273 | 0 | cpi->mv_search_params.search_site_cfg[SS_CFG_FPF]; |
274 | 0 | const int fine_search_interval = |
275 | 0 | cpi->is_screen_content_type && cpi->common.features.allow_intrabc; |
276 | 0 | FULLPEL_MOTION_SEARCH_PARAMS ms_params; |
277 | 0 | av1_make_default_fullpel_ms_params(&ms_params, cpi, x, bsize, ref_mv, |
278 | 0 | first_pass_search_sites, |
279 | 0 | fine_search_interval); |
280 | 0 | av1_set_mv_search_method(&ms_params, first_pass_search_sites, NSTEP); |
281 | |
|
282 | 0 | FULLPEL_MV this_best_mv; |
283 | 0 | tmp_err = av1_full_pixel_search(start_mv, &ms_params, step_param, NULL, |
284 | 0 | &this_best_mv, NULL); |
285 | |
|
286 | 0 | if (tmp_err < INT_MAX) { |
287 | 0 | aom_variance_fn_ptr_t v_fn_ptr = cpi->ppi->fn_ptr[bsize]; |
288 | 0 | const MSBuffers *ms_buffers = &ms_params.ms_buffers; |
289 | 0 | tmp_err = av1_get_mvpred_sse(&ms_params.mv_cost_params, this_best_mv, |
290 | 0 | &v_fn_ptr, ms_buffers->src, ms_buffers->ref) + |
291 | 0 | new_mv_mode_penalty; |
292 | 0 | } |
293 | |
|
294 | 0 | if (tmp_err < *best_motion_err) { |
295 | 0 | *best_motion_err = tmp_err; |
296 | 0 | *best_mv = this_best_mv; |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | | static BLOCK_SIZE get_bsize(const CommonModeInfoParams *const mi_params, |
301 | | const BLOCK_SIZE fp_block_size, const int unit_row, |
302 | 0 | const int unit_col) { |
303 | 0 | const int unit_width = mi_size_wide[fp_block_size]; |
304 | 0 | const int unit_height = mi_size_high[fp_block_size]; |
305 | 0 | const int is_half_width = |
306 | 0 | unit_width * unit_col + unit_width / 2 >= mi_params->mi_cols; |
307 | 0 | const int is_half_height = |
308 | 0 | unit_height * unit_row + unit_height / 2 >= mi_params->mi_rows; |
309 | 0 | const int max_dimension = |
310 | 0 | AOMMAX(block_size_wide[fp_block_size], block_size_high[fp_block_size]); |
311 | 0 | int square_block_size = 0; |
312 | | // 4X4, 8X8, 16X16, 32X32, 64X64, 128X128 |
313 | 0 | switch (max_dimension) { |
314 | 0 | case 4: square_block_size = 0; break; |
315 | 0 | case 8: square_block_size = 1; break; |
316 | 0 | case 16: square_block_size = 2; break; |
317 | 0 | case 32: square_block_size = 3; break; |
318 | 0 | case 64: square_block_size = 4; break; |
319 | 0 | case 128: square_block_size = 5; break; |
320 | 0 | default: assert(0 && "First pass block size is not supported!"); break; |
321 | 0 | } |
322 | 0 | if (is_half_width && is_half_height) { |
323 | 0 | return subsize_lookup[PARTITION_SPLIT][square_block_size]; |
324 | 0 | } else if (is_half_width) { |
325 | 0 | return subsize_lookup[PARTITION_VERT][square_block_size]; |
326 | 0 | } else if (is_half_height) { |
327 | 0 | return subsize_lookup[PARTITION_HORZ][square_block_size]; |
328 | 0 | } else { |
329 | 0 | return fp_block_size; |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | 0 | static int find_fp_qindex(aom_bit_depth_t bit_depth) { |
334 | 0 | return av1_find_qindex(FIRST_PASS_Q, bit_depth, 0, QINDEX_RANGE - 1); |
335 | 0 | } |
336 | | |
337 | | static double raw_motion_error_stdev(int *raw_motion_err_list, |
338 | 0 | int raw_motion_err_counts) { |
339 | 0 | int64_t sum_raw_err = 0; |
340 | 0 | double raw_err_avg = 0; |
341 | 0 | double raw_err_stdev = 0; |
342 | 0 | if (raw_motion_err_counts == 0) return 0; |
343 | | |
344 | 0 | int i; |
345 | 0 | for (i = 0; i < raw_motion_err_counts; i++) { |
346 | 0 | sum_raw_err += raw_motion_err_list[i]; |
347 | 0 | } |
348 | 0 | raw_err_avg = (double)sum_raw_err / raw_motion_err_counts; |
349 | 0 | for (i = 0; i < raw_motion_err_counts; i++) { |
350 | 0 | raw_err_stdev += (raw_motion_err_list[i] - raw_err_avg) * |
351 | 0 | (raw_motion_err_list[i] - raw_err_avg); |
352 | 0 | } |
353 | | // Calculate the standard deviation for the motion error of all the inter |
354 | | // blocks of the 0,0 motion using the last source |
355 | | // frame as the reference. |
356 | 0 | raw_err_stdev = sqrt(raw_err_stdev / raw_motion_err_counts); |
357 | 0 | return raw_err_stdev; |
358 | 0 | } |
359 | | |
360 | 0 | static AOM_INLINE int calc_wavelet_energy(const AV1EncoderConfig *oxcf) { |
361 | 0 | return oxcf->q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL; |
362 | 0 | } |
363 | | typedef struct intra_pred_block_pass1_args { |
364 | | const SequenceHeader *seq_params; |
365 | | MACROBLOCK *x; |
366 | | } intra_pred_block_pass1_args; |
367 | | |
368 | | static INLINE void copy_rect(uint8_t *dst, int dstride, const uint8_t *src, |
369 | 0 | int sstride, int width, int height, int use_hbd) { |
370 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
371 | 0 | if (use_hbd) { |
372 | 0 | aom_highbd_convolve_copy(CONVERT_TO_SHORTPTR(src), sstride, |
373 | 0 | CONVERT_TO_SHORTPTR(dst), dstride, width, height); |
374 | 0 | } else { |
375 | 0 | aom_convolve_copy(src, sstride, dst, dstride, width, height); |
376 | 0 | } |
377 | | #else |
378 | | (void)use_hbd; |
379 | | aom_convolve_copy(src, sstride, dst, dstride, width, height); |
380 | | #endif |
381 | 0 | } |
382 | | |
383 | | static void first_pass_intra_pred_and_calc_diff(int plane, int block, |
384 | | int blk_row, int blk_col, |
385 | | BLOCK_SIZE plane_bsize, |
386 | 0 | TX_SIZE tx_size, void *arg) { |
387 | 0 | (void)block; |
388 | 0 | struct intra_pred_block_pass1_args *const args = arg; |
389 | 0 | MACROBLOCK *const x = args->x; |
390 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
391 | 0 | MACROBLOCKD_PLANE *const pd = &xd->plane[plane]; |
392 | 0 | MACROBLOCK_PLANE *const p = &x->plane[plane]; |
393 | 0 | const int dst_stride = pd->dst.stride; |
394 | 0 | uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2]; |
395 | 0 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
396 | 0 | const SequenceHeader *seq_params = args->seq_params; |
397 | 0 | const int src_stride = p->src.stride; |
398 | 0 | uint8_t *src = &p->src.buf[(blk_row * src_stride + blk_col) << MI_SIZE_LOG2]; |
399 | |
|
400 | 0 | av1_predict_intra_block( |
401 | 0 | xd, seq_params->sb_size, seq_params->enable_intra_edge_filter, pd->width, |
402 | 0 | pd->height, tx_size, mbmi->mode, 0, 0, FILTER_INTRA_MODES, src, |
403 | 0 | src_stride, dst, dst_stride, blk_col, blk_row, plane); |
404 | |
|
405 | 0 | av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size); |
406 | 0 | } |
407 | | |
408 | | static void first_pass_predict_intra_block_for_luma_plane( |
409 | 0 | const SequenceHeader *seq_params, MACROBLOCK *x, BLOCK_SIZE bsize) { |
410 | 0 | assert(bsize < BLOCK_SIZES_ALL); |
411 | 0 | const MACROBLOCKD *const xd = &x->e_mbd; |
412 | 0 | const int plane = AOM_PLANE_Y; |
413 | 0 | const MACROBLOCKD_PLANE *const pd = &xd->plane[plane]; |
414 | 0 | const int ss_x = pd->subsampling_x; |
415 | 0 | const int ss_y = pd->subsampling_y; |
416 | 0 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y); |
417 | 0 | const int dst_stride = pd->dst.stride; |
418 | 0 | uint8_t *dst = pd->dst.buf; |
419 | 0 | const MACROBLOCK_PLANE *const p = &x->plane[plane]; |
420 | 0 | const int src_stride = p->src.stride; |
421 | 0 | const uint8_t *src = p->src.buf; |
422 | |
|
423 | 0 | intra_pred_block_pass1_args args = { seq_params, x }; |
424 | 0 | av1_foreach_transformed_block_in_plane( |
425 | 0 | xd, plane_bsize, plane, first_pass_intra_pred_and_calc_diff, &args); |
426 | | |
427 | | // copy source data to recon buffer, as the recon buffer will be used as a |
428 | | // reference frame subsequently. |
429 | 0 | copy_rect(dst, dst_stride, src, src_stride, block_size_wide[bsize], |
430 | 0 | block_size_high[bsize], seq_params->use_highbitdepth); |
431 | 0 | } |
432 | | |
433 | 0 | #define UL_INTRA_THRESH 50 |
434 | 0 | #define INVALID_ROW -1 |
435 | | // Computes and returns the intra pred error of a block. |
436 | | // intra pred error: sum of squared error of the intra predicted residual. |
437 | | // Inputs: |
438 | | // cpi: the encoder setting. Only a few params in it will be used. |
439 | | // this_frame: the current frame buffer. |
440 | | // tile: tile information (not used in first pass, already init to zero) |
441 | | // unit_row: row index in the unit of first pass block size. |
442 | | // unit_col: column index in the unit of first pass block size. |
443 | | // y_offset: the offset of y frame buffer, indicating the starting point of |
444 | | // the current block. |
445 | | // uv_offset: the offset of u and v frame buffer, indicating the starting |
446 | | // point of the current block. |
447 | | // fp_block_size: first pass block size. |
448 | | // qindex: quantization step size to encode the frame. |
449 | | // stats: frame encoding stats. |
450 | | // Modifies: |
451 | | // stats->intra_skip_count |
452 | | // stats->image_data_start_row |
453 | | // stats->intra_factor |
454 | | // stats->brightness_factor |
455 | | // stats->intra_error |
456 | | // stats->frame_avg_wavelet_energy |
457 | | // Returns: |
458 | | // this_intra_error. |
459 | | static int firstpass_intra_prediction( |
460 | | AV1_COMP *cpi, ThreadData *td, YV12_BUFFER_CONFIG *const this_frame, |
461 | | const TileInfo *const tile, const int unit_row, const int unit_col, |
462 | | const int y_offset, const int uv_offset, const BLOCK_SIZE fp_block_size, |
463 | 0 | const int qindex, FRAME_STATS *const stats) { |
464 | 0 | const AV1_COMMON *const cm = &cpi->common; |
465 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
466 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
467 | 0 | MACROBLOCK *const x = &td->mb; |
468 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
469 | 0 | const int unit_scale = mi_size_wide[fp_block_size]; |
470 | 0 | const int num_planes = av1_num_planes(cm); |
471 | 0 | const BLOCK_SIZE bsize = |
472 | 0 | get_bsize(mi_params, fp_block_size, unit_row, unit_col); |
473 | |
|
474 | 0 | set_mi_offsets(mi_params, xd, unit_row * unit_scale, unit_col * unit_scale); |
475 | 0 | xd->plane[0].dst.buf = this_frame->y_buffer + y_offset; |
476 | 0 | xd->plane[1].dst.buf = this_frame->u_buffer + uv_offset; |
477 | 0 | xd->plane[2].dst.buf = this_frame->v_buffer + uv_offset; |
478 | 0 | xd->left_available = (unit_col != 0); |
479 | 0 | xd->mi[0]->bsize = bsize; |
480 | 0 | xd->mi[0]->ref_frame[0] = INTRA_FRAME; |
481 | 0 | set_mi_row_col(xd, tile, unit_row * unit_scale, mi_size_high[bsize], |
482 | 0 | unit_col * unit_scale, mi_size_wide[bsize], mi_params->mi_rows, |
483 | 0 | mi_params->mi_cols); |
484 | 0 | set_plane_n4(xd, mi_size_wide[bsize], mi_size_high[bsize], num_planes); |
485 | 0 | xd->mi[0]->segment_id = 0; |
486 | 0 | xd->lossless[xd->mi[0]->segment_id] = (qindex == 0); |
487 | 0 | xd->mi[0]->mode = DC_PRED; |
488 | 0 | xd->mi[0]->tx_size = TX_4X4; |
489 | |
|
490 | 0 | if (cpi->sf.fp_sf.disable_recon) |
491 | 0 | first_pass_predict_intra_block_for_luma_plane(seq_params, x, bsize); |
492 | 0 | else |
493 | 0 | av1_encode_intra_block_plane(cpi, x, bsize, 0, DRY_RUN_NORMAL, 0); |
494 | 0 | int this_intra_error = aom_get_mb_ss(x->plane[0].src_diff); |
495 | 0 | if (seq_params->use_highbitdepth) { |
496 | 0 | switch (seq_params->bit_depth) { |
497 | 0 | case AOM_BITS_8: break; |
498 | 0 | case AOM_BITS_10: this_intra_error >>= 4; break; |
499 | 0 | case AOM_BITS_12: this_intra_error >>= 8; break; |
500 | 0 | default: |
501 | 0 | assert(0 && |
502 | 0 | "seq_params->bit_depth should be AOM_BITS_8, " |
503 | 0 | "AOM_BITS_10 or AOM_BITS_12"); |
504 | 0 | return -1; |
505 | 0 | } |
506 | 0 | } |
507 | | |
508 | 0 | if (this_intra_error < UL_INTRA_THRESH) { |
509 | 0 | ++stats->intra_skip_count; |
510 | 0 | } else if ((unit_col > 0) && (stats->image_data_start_row == INVALID_ROW)) { |
511 | 0 | stats->image_data_start_row = unit_row; |
512 | 0 | } |
513 | |
|
514 | 0 | double log_intra = log(this_intra_error + 1.0); |
515 | 0 | if (log_intra < 10.0) { |
516 | 0 | stats->intra_factor += 1.0 + ((10.0 - log_intra) * 0.05); |
517 | 0 | } else { |
518 | 0 | stats->intra_factor += 1.0; |
519 | 0 | } |
520 | |
|
521 | 0 | int level_sample; |
522 | 0 | if (seq_params->use_highbitdepth) { |
523 | 0 | level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0]; |
524 | 0 | } else { |
525 | 0 | level_sample = x->plane[0].src.buf[0]; |
526 | 0 | } |
527 | |
|
528 | 0 | if (seq_params->use_highbitdepth) { |
529 | 0 | switch (seq_params->bit_depth) { |
530 | 0 | case AOM_BITS_8: break; |
531 | 0 | case AOM_BITS_10: level_sample >>= 2; break; |
532 | 0 | case AOM_BITS_12: level_sample >>= 4; break; |
533 | 0 | default: |
534 | 0 | assert(0 && |
535 | 0 | "seq_params->bit_depth should be AOM_BITS_8, " |
536 | 0 | "AOM_BITS_10 or AOM_BITS_12"); |
537 | 0 | return -1; |
538 | 0 | } |
539 | 0 | } |
540 | 0 | if ((level_sample < DARK_THRESH) && (log_intra < 9.0)) { |
541 | 0 | stats->brightness_factor += 1.0 + (0.01 * (DARK_THRESH - level_sample)); |
542 | 0 | } else { |
543 | 0 | stats->brightness_factor += 1.0; |
544 | 0 | } |
545 | | |
546 | | // Intrapenalty below deals with situations where the intra and inter |
547 | | // error scores are very low (e.g. a plain black frame). |
548 | | // We do not have special cases in first pass for 0,0 and nearest etc so |
549 | | // all inter modes carry an overhead cost estimate for the mv. |
550 | | // When the error score is very low this causes us to pick all or lots of |
551 | | // INTRA modes and throw lots of key frames. |
552 | | // This penalty adds a cost matching that of a 0,0 mv to the intra case. |
553 | 0 | this_intra_error += INTRA_MODE_PENALTY; |
554 | | |
555 | | // Accumulate the intra error. |
556 | 0 | stats->intra_error += (int64_t)this_intra_error; |
557 | | |
558 | | // Stats based on wavelet energy is used in the following cases : |
559 | | // 1. ML model which predicts if a flat structure (golden-frame only structure |
560 | | // without ALT-REF and Internal-ARFs) is better. This ML model is enabled in |
561 | | // constant quality mode under certain conditions. |
562 | | // 2. Delta qindex mode is set as DELTA_Q_PERCEPTUAL. |
563 | | // Thus, wavelet energy calculation is enabled for the above cases. |
564 | 0 | if (calc_wavelet_energy(&cpi->oxcf)) { |
565 | 0 | const int hbd = is_cur_buf_hbd(xd); |
566 | 0 | const int stride = x->plane[0].src.stride; |
567 | 0 | const int num_8x8_rows = block_size_high[fp_block_size] / 8; |
568 | 0 | const int num_8x8_cols = block_size_wide[fp_block_size] / 8; |
569 | 0 | const uint8_t *buf = x->plane[0].src.buf; |
570 | 0 | stats->frame_avg_wavelet_energy += av1_haar_ac_sad_mxn_uint8_input( |
571 | 0 | buf, stride, hbd, num_8x8_rows, num_8x8_cols); |
572 | 0 | } else { |
573 | 0 | stats->frame_avg_wavelet_energy = INVALID_FP_STATS_TO_PREDICT_FLAT_GOP; |
574 | 0 | } |
575 | |
|
576 | 0 | return this_intra_error; |
577 | 0 | } |
578 | | |
579 | | // Returns the sum of square error between source and reference blocks. |
580 | | static int get_prediction_error_bitdepth(const int is_high_bitdepth, |
581 | | const int bitdepth, |
582 | | const BLOCK_SIZE block_size, |
583 | | const struct buf_2d *src, |
584 | 0 | const struct buf_2d *ref) { |
585 | 0 | (void)is_high_bitdepth; |
586 | 0 | (void)bitdepth; |
587 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
588 | 0 | if (is_high_bitdepth) { |
589 | 0 | return highbd_get_prediction_error(block_size, src, ref, bitdepth); |
590 | 0 | } |
591 | 0 | #endif // CONFIG_AV1_HIGHBITDEPTH |
592 | 0 | return get_prediction_error(block_size, src, ref); |
593 | 0 | } |
594 | | |
595 | | // Accumulates motion vector stats. |
596 | | // Modifies member variables of "stats". |
597 | | static void accumulate_mv_stats(const MV best_mv, const FULLPEL_MV mv, |
598 | | const int mb_row, const int mb_col, |
599 | | const int mb_rows, const int mb_cols, |
600 | 0 | MV *last_non_zero_mv, FRAME_STATS *stats) { |
601 | 0 | if (is_zero_mv(&best_mv)) return; |
602 | | |
603 | 0 | ++stats->mv_count; |
604 | | // Non-zero vector, was it different from the last non zero vector? |
605 | 0 | if (!is_equal_mv(&best_mv, last_non_zero_mv)) ++stats->new_mv_count; |
606 | 0 | *last_non_zero_mv = best_mv; |
607 | | |
608 | | // Does the row vector point inwards or outwards? |
609 | 0 | if (mb_row < mb_rows / 2) { |
610 | 0 | if (mv.row > 0) { |
611 | 0 | --stats->sum_in_vectors; |
612 | 0 | } else if (mv.row < 0) { |
613 | 0 | ++stats->sum_in_vectors; |
614 | 0 | } |
615 | 0 | } else if (mb_row > mb_rows / 2) { |
616 | 0 | if (mv.row > 0) { |
617 | 0 | ++stats->sum_in_vectors; |
618 | 0 | } else if (mv.row < 0) { |
619 | 0 | --stats->sum_in_vectors; |
620 | 0 | } |
621 | 0 | } |
622 | | |
623 | | // Does the col vector point inwards or outwards? |
624 | 0 | if (mb_col < mb_cols / 2) { |
625 | 0 | if (mv.col > 0) { |
626 | 0 | --stats->sum_in_vectors; |
627 | 0 | } else if (mv.col < 0) { |
628 | 0 | ++stats->sum_in_vectors; |
629 | 0 | } |
630 | 0 | } else if (mb_col > mb_cols / 2) { |
631 | 0 | if (mv.col > 0) { |
632 | 0 | ++stats->sum_in_vectors; |
633 | 0 | } else if (mv.col < 0) { |
634 | 0 | --stats->sum_in_vectors; |
635 | 0 | } |
636 | 0 | } |
637 | 0 | } |
638 | | |
639 | | // Computes and returns the inter prediction error from the last frame. |
640 | | // Computes inter prediction errors from the golden and alt ref frams and |
641 | | // Updates stats accordingly. |
642 | | // Inputs: |
643 | | // cpi: the encoder setting. Only a few params in it will be used. |
644 | | // last_frame: the frame buffer of the last frame. |
645 | | // golden_frame: the frame buffer of the golden frame. |
646 | | // unit_row: row index in the unit of first pass block size. |
647 | | // unit_col: column index in the unit of first pass block size. |
648 | | // recon_yoffset: the y offset of the reconstructed frame buffer, |
649 | | // indicating the starting point of the current block. |
650 | | // recont_uvoffset: the u/v offset of the reconstructed frame buffer, |
651 | | // indicating the starting point of the current block. |
652 | | // src_yoffset: the y offset of the source frame buffer. |
653 | | // fp_block_size: first pass block size. |
654 | | // this_intra_error: the intra prediction error of this block. |
655 | | // raw_motion_err_counts: the count of raw motion vectors. |
656 | | // raw_motion_err_list: the array that records the raw motion error. |
657 | | // ref_mv: the reference used to start the motion search |
658 | | // best_mv: the best mv found |
659 | | // last_non_zero_mv: the last non zero mv found in this tile row. |
660 | | // stats: frame encoding stats. |
661 | | // Modifies: |
662 | | // raw_motion_err_list |
663 | | // best_ref_mv |
664 | | // last_mv |
665 | | // stats: many member params in it. |
666 | | // Returns: |
667 | | // this_inter_error |
668 | | static int firstpass_inter_prediction( |
669 | | AV1_COMP *cpi, ThreadData *td, const YV12_BUFFER_CONFIG *const last_frame, |
670 | | const YV12_BUFFER_CONFIG *const golden_frame, const int unit_row, |
671 | | const int unit_col, const int recon_yoffset, const int recon_uvoffset, |
672 | | const int src_yoffset, const BLOCK_SIZE fp_block_size, |
673 | | const int this_intra_error, const int raw_motion_err_counts, |
674 | | int *raw_motion_err_list, const MV ref_mv, MV *best_mv, |
675 | 0 | MV *last_non_zero_mv, FRAME_STATS *stats) { |
676 | 0 | int this_inter_error = this_intra_error; |
677 | 0 | AV1_COMMON *const cm = &cpi->common; |
678 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
679 | 0 | CurrentFrame *const current_frame = &cm->current_frame; |
680 | 0 | MACROBLOCK *const x = &td->mb; |
681 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
682 | 0 | const int is_high_bitdepth = is_cur_buf_hbd(xd); |
683 | 0 | const int bitdepth = xd->bd; |
684 | 0 | const int unit_scale = mi_size_wide[fp_block_size]; |
685 | 0 | const BLOCK_SIZE bsize = |
686 | 0 | get_bsize(mi_params, fp_block_size, unit_row, unit_col); |
687 | 0 | const int fp_block_size_height = block_size_wide[fp_block_size]; |
688 | 0 | const int unit_width = mi_size_wide[fp_block_size]; |
689 | 0 | const int unit_rows = get_unit_rows(fp_block_size, mi_params->mb_rows); |
690 | 0 | const int unit_cols = get_unit_cols(fp_block_size, mi_params->mb_cols); |
691 | | // Assume 0,0 motion with no mv overhead. |
692 | 0 | FULLPEL_MV mv = kZeroFullMv; |
693 | 0 | xd->plane[0].pre[0].buf = last_frame->y_buffer + recon_yoffset; |
694 | | // Set up limit values for motion vectors to prevent them extending |
695 | | // outside the UMV borders. |
696 | 0 | av1_set_mv_col_limits(mi_params, &x->mv_limits, unit_col * unit_width, |
697 | 0 | fp_block_size_height >> MI_SIZE_LOG2, |
698 | 0 | cpi->oxcf.border_in_pixels); |
699 | |
|
700 | 0 | int motion_error = |
701 | 0 | get_prediction_error_bitdepth(is_high_bitdepth, bitdepth, bsize, |
702 | 0 | &x->plane[0].src, &xd->plane[0].pre[0]); |
703 | | |
704 | | // Compute the motion error of the 0,0 motion using the last source |
705 | | // frame as the reference. Skip the further motion search on |
706 | | // reconstructed frame if this error is small. |
707 | 0 | struct buf_2d unscaled_last_source_buf_2d; |
708 | 0 | unscaled_last_source_buf_2d.buf = |
709 | 0 | cpi->unscaled_last_source->y_buffer + src_yoffset; |
710 | 0 | unscaled_last_source_buf_2d.stride = cpi->unscaled_last_source->y_stride; |
711 | 0 | const int raw_motion_error = get_prediction_error_bitdepth( |
712 | 0 | is_high_bitdepth, bitdepth, bsize, &x->plane[0].src, |
713 | 0 | &unscaled_last_source_buf_2d); |
714 | 0 | raw_motion_err_list[raw_motion_err_counts] = raw_motion_error; |
715 | 0 | const FIRST_PASS_SPEED_FEATURES *const fp_sf = &cpi->sf.fp_sf; |
716 | |
|
717 | 0 | if (raw_motion_error > fp_sf->skip_motion_search_threshold) { |
718 | | // Test last reference frame using the previous best mv as the |
719 | | // starting point (best reference) for the search. |
720 | 0 | first_pass_motion_search(cpi, x, &ref_mv, &mv, &motion_error); |
721 | | |
722 | | // If the current best reference mv is not centered on 0,0 then do a |
723 | | // 0,0 based search as well. |
724 | 0 | if ((fp_sf->skip_zeromv_motion_search == 0) && !is_zero_mv(&ref_mv)) { |
725 | 0 | FULLPEL_MV tmp_mv = kZeroFullMv; |
726 | 0 | int tmp_err = INT_MAX; |
727 | 0 | first_pass_motion_search(cpi, x, &kZeroMv, &tmp_mv, &tmp_err); |
728 | |
|
729 | 0 | if (tmp_err < motion_error) { |
730 | 0 | motion_error = tmp_err; |
731 | 0 | mv = tmp_mv; |
732 | 0 | } |
733 | 0 | } |
734 | | |
735 | | // Motion search in 2nd reference frame. |
736 | 0 | int gf_motion_error = motion_error; |
737 | 0 | if ((current_frame->frame_number > 1) && golden_frame != NULL) { |
738 | 0 | FULLPEL_MV tmp_mv = kZeroFullMv; |
739 | | // Assume 0,0 motion with no mv overhead. |
740 | 0 | xd->plane[0].pre[0].buf = golden_frame->y_buffer + recon_yoffset; |
741 | 0 | xd->plane[0].pre[0].stride = golden_frame->y_stride; |
742 | 0 | gf_motion_error = |
743 | 0 | get_prediction_error_bitdepth(is_high_bitdepth, bitdepth, bsize, |
744 | 0 | &x->plane[0].src, &xd->plane[0].pre[0]); |
745 | 0 | first_pass_motion_search(cpi, x, &kZeroMv, &tmp_mv, &gf_motion_error); |
746 | 0 | } |
747 | 0 | if (gf_motion_error < motion_error && gf_motion_error < this_intra_error) { |
748 | 0 | ++stats->second_ref_count; |
749 | 0 | } |
750 | | // In accumulating a score for the 2nd reference frame take the |
751 | | // best of the motion predicted score and the intra coded error |
752 | | // (just as will be done for) accumulation of "coded_error" for |
753 | | // the last frame. |
754 | 0 | if ((current_frame->frame_number > 1) && golden_frame != NULL) { |
755 | 0 | stats->sr_coded_error += AOMMIN(gf_motion_error, this_intra_error); |
756 | 0 | } else { |
757 | | // TODO(chengchen): I believe logically this should also be changed to |
758 | | // stats->sr_coded_error += AOMMIN(gf_motion_error, this_intra_error). |
759 | 0 | stats->sr_coded_error += motion_error; |
760 | 0 | } |
761 | | |
762 | | // Reset to last frame as reference buffer. |
763 | 0 | xd->plane[0].pre[0].buf = last_frame->y_buffer + recon_yoffset; |
764 | 0 | xd->plane[1].pre[0].buf = last_frame->u_buffer + recon_uvoffset; |
765 | 0 | xd->plane[2].pre[0].buf = last_frame->v_buffer + recon_uvoffset; |
766 | 0 | } else { |
767 | 0 | stats->sr_coded_error += motion_error; |
768 | 0 | } |
769 | | |
770 | | // Start by assuming that intra mode is best. |
771 | 0 | *best_mv = kZeroMv; |
772 | |
|
773 | 0 | if (motion_error <= this_intra_error) { |
774 | | // Keep a count of cases where the inter and intra were very close |
775 | | // and very low. This helps with scene cut detection for example in |
776 | | // cropped clips with black bars at the sides or top and bottom. |
777 | 0 | if (((this_intra_error - INTRA_MODE_PENALTY) * 9 <= motion_error * 10) && |
778 | 0 | (this_intra_error < (2 * INTRA_MODE_PENALTY))) { |
779 | 0 | stats->neutral_count += 1.0; |
780 | | // Also track cases where the intra is not much worse than the inter |
781 | | // and use this in limiting the GF/arf group length. |
782 | 0 | } else if ((this_intra_error > NCOUNT_INTRA_THRESH) && |
783 | 0 | (this_intra_error < (NCOUNT_INTRA_FACTOR * motion_error))) { |
784 | 0 | stats->neutral_count += |
785 | 0 | (double)motion_error / DOUBLE_DIVIDE_CHECK((double)this_intra_error); |
786 | 0 | } |
787 | |
|
788 | 0 | *best_mv = get_mv_from_fullmv(&mv); |
789 | 0 | this_inter_error = motion_error; |
790 | 0 | xd->mi[0]->mode = NEWMV; |
791 | 0 | xd->mi[0]->mv[0].as_mv = *best_mv; |
792 | 0 | xd->mi[0]->tx_size = TX_4X4; |
793 | 0 | xd->mi[0]->ref_frame[0] = LAST_FRAME; |
794 | 0 | xd->mi[0]->ref_frame[1] = NONE_FRAME; |
795 | |
|
796 | 0 | if (fp_sf->disable_recon == 0) { |
797 | 0 | av1_enc_build_inter_predictor(cm, xd, unit_row * unit_scale, |
798 | 0 | unit_col * unit_scale, NULL, bsize, |
799 | 0 | AOM_PLANE_Y, AOM_PLANE_Y); |
800 | 0 | av1_encode_sby_pass1(cpi, x, bsize); |
801 | 0 | } |
802 | 0 | stats->sum_mvr += best_mv->row; |
803 | 0 | stats->sum_mvr_abs += abs(best_mv->row); |
804 | 0 | stats->sum_mvc += best_mv->col; |
805 | 0 | stats->sum_mvc_abs += abs(best_mv->col); |
806 | 0 | stats->sum_mvrs += best_mv->row * best_mv->row; |
807 | 0 | stats->sum_mvcs += best_mv->col * best_mv->col; |
808 | 0 | ++stats->inter_count; |
809 | |
|
810 | 0 | accumulate_mv_stats(*best_mv, mv, unit_row, unit_col, unit_rows, unit_cols, |
811 | 0 | last_non_zero_mv, stats); |
812 | 0 | } |
813 | |
|
814 | 0 | return this_inter_error; |
815 | 0 | } |
816 | | |
817 | | // Normalize the first pass stats. |
818 | | // Error / counters are normalized to each MB. |
819 | | // MVs are normalized to the width/height of the frame. |
820 | | static void normalize_firstpass_stats(FIRSTPASS_STATS *fps, |
821 | | double num_mbs_16x16, double f_w, |
822 | 0 | double f_h) { |
823 | 0 | fps->coded_error /= num_mbs_16x16; |
824 | 0 | fps->sr_coded_error /= num_mbs_16x16; |
825 | 0 | fps->intra_error /= num_mbs_16x16; |
826 | 0 | fps->frame_avg_wavelet_energy /= num_mbs_16x16; |
827 | |
|
828 | 0 | fps->MVr /= f_h; |
829 | 0 | fps->mvr_abs /= f_h; |
830 | 0 | fps->MVc /= f_w; |
831 | 0 | fps->mvc_abs /= f_w; |
832 | 0 | fps->MVrv /= (f_h * f_h); |
833 | 0 | fps->MVcv /= (f_w * f_w); |
834 | 0 | fps->new_mv_count /= num_mbs_16x16; |
835 | 0 | } |
836 | | |
837 | | // Updates the first pass stats of this frame. |
838 | | // Input: |
839 | | // cpi: the encoder setting. Only a few params in it will be used. |
840 | | // stats: stats accumulated for this frame. |
841 | | // raw_err_stdev: the statndard deviation for the motion error of all the |
842 | | // inter blocks of the (0,0) motion using the last source |
843 | | // frame as the reference. |
844 | | // frame_number: current frame number. |
845 | | // ts_duration: Duration of the frame / collection of frames. |
846 | | // Updates: |
847 | | // twopass->total_stats: the accumulated stats. |
848 | | // twopass->stats_buf_ctx->stats_in_end: the pointer to the current stats, |
849 | | // update its value and its position |
850 | | // in the buffer. |
851 | | static void update_firstpass_stats(AV1_COMP *cpi, |
852 | | const FRAME_STATS *const stats, |
853 | | const double raw_err_stdev, |
854 | | const int frame_number, |
855 | | const int64_t ts_duration, |
856 | 0 | const BLOCK_SIZE fp_block_size) { |
857 | 0 | TWO_PASS *twopass = &cpi->ppi->twopass; |
858 | 0 | AV1_COMMON *const cm = &cpi->common; |
859 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
860 | 0 | FIRSTPASS_STATS *this_frame_stats = twopass->stats_buf_ctx->stats_in_end; |
861 | 0 | FIRSTPASS_STATS fps; |
862 | | // The minimum error here insures some bit allocation to frames even |
863 | | // in static regions. The allocation per MB declines for larger formats |
864 | | // where the typical "real" energy per MB also falls. |
865 | | // Initial estimate here uses sqrt(mbs) to define the min_err, where the |
866 | | // number of mbs is proportional to the image area. |
867 | 0 | const int num_mbs_16X16 = (cpi->oxcf.resize_cfg.resize_mode != RESIZE_NONE) |
868 | 0 | ? cpi->initial_mbs |
869 | 0 | : mi_params->MBs; |
870 | | // Number of actual units used in the first pass, it can be other square |
871 | | // block sizes than 16X16. |
872 | 0 | const int num_mbs = get_num_mbs(fp_block_size, num_mbs_16X16); |
873 | 0 | const double min_err = 200 * sqrt(num_mbs); |
874 | |
|
875 | 0 | fps.weight = stats->intra_factor * stats->brightness_factor; |
876 | 0 | fps.frame = frame_number; |
877 | 0 | fps.coded_error = (double)(stats->coded_error >> 8) + min_err; |
878 | 0 | fps.sr_coded_error = (double)(stats->sr_coded_error >> 8) + min_err; |
879 | 0 | fps.intra_error = (double)(stats->intra_error >> 8) + min_err; |
880 | 0 | fps.frame_avg_wavelet_energy = (double)stats->frame_avg_wavelet_energy; |
881 | 0 | fps.count = 1.0; |
882 | 0 | fps.pcnt_inter = (double)stats->inter_count / num_mbs; |
883 | 0 | fps.pcnt_second_ref = (double)stats->second_ref_count / num_mbs; |
884 | 0 | fps.pcnt_neutral = (double)stats->neutral_count / num_mbs; |
885 | 0 | fps.intra_skip_pct = (double)stats->intra_skip_count / num_mbs; |
886 | 0 | fps.inactive_zone_rows = (double)stats->image_data_start_row; |
887 | 0 | fps.inactive_zone_cols = (double)0; // TODO(paulwilkins): fix |
888 | 0 | fps.raw_error_stdev = raw_err_stdev; |
889 | 0 | fps.is_flash = 0; |
890 | 0 | fps.noise_var = (double)0; |
891 | 0 | fps.cor_coeff = (double)1.0; |
892 | |
|
893 | 0 | if (stats->mv_count > 0) { |
894 | 0 | fps.MVr = (double)stats->sum_mvr / stats->mv_count; |
895 | 0 | fps.mvr_abs = (double)stats->sum_mvr_abs / stats->mv_count; |
896 | 0 | fps.MVc = (double)stats->sum_mvc / stats->mv_count; |
897 | 0 | fps.mvc_abs = (double)stats->sum_mvc_abs / stats->mv_count; |
898 | 0 | fps.MVrv = ((double)stats->sum_mvrs - |
899 | 0 | ((double)stats->sum_mvr * stats->sum_mvr / stats->mv_count)) / |
900 | 0 | stats->mv_count; |
901 | 0 | fps.MVcv = ((double)stats->sum_mvcs - |
902 | 0 | ((double)stats->sum_mvc * stats->sum_mvc / stats->mv_count)) / |
903 | 0 | stats->mv_count; |
904 | 0 | fps.mv_in_out_count = (double)stats->sum_in_vectors / (stats->mv_count * 2); |
905 | 0 | fps.new_mv_count = stats->new_mv_count; |
906 | 0 | fps.pcnt_motion = (double)stats->mv_count / num_mbs; |
907 | 0 | } else { |
908 | 0 | fps.MVr = 0.0; |
909 | 0 | fps.mvr_abs = 0.0; |
910 | 0 | fps.MVc = 0.0; |
911 | 0 | fps.mvc_abs = 0.0; |
912 | 0 | fps.MVrv = 0.0; |
913 | 0 | fps.MVcv = 0.0; |
914 | 0 | fps.mv_in_out_count = 0.0; |
915 | 0 | fps.new_mv_count = 0.0; |
916 | 0 | fps.pcnt_motion = 0.0; |
917 | 0 | } |
918 | | |
919 | | // TODO(paulwilkins): Handle the case when duration is set to 0, or |
920 | | // something less than the full time between subsequent values of |
921 | | // cpi->source_time_stamp. |
922 | 0 | fps.duration = (double)ts_duration; |
923 | |
|
924 | 0 | normalize_firstpass_stats(&fps, num_mbs_16X16, cm->width, cm->height); |
925 | | |
926 | | // We will store the stats inside the persistent twopass struct (and NOT the |
927 | | // local variable 'fps'), and then cpi->output_pkt_list will point to it. |
928 | 0 | *this_frame_stats = fps; |
929 | 0 | if (!cpi->ppi->lap_enabled) { |
930 | 0 | output_stats(this_frame_stats, cpi->ppi->output_pkt_list); |
931 | 0 | } else { |
932 | 0 | av1_firstpass_info_push(&twopass->firstpass_info, this_frame_stats); |
933 | 0 | } |
934 | 0 | if (cpi->ppi->twopass.stats_buf_ctx->total_stats != NULL) { |
935 | 0 | av1_accumulate_stats(cpi->ppi->twopass.stats_buf_ctx->total_stats, &fps); |
936 | 0 | } |
937 | | /*In the case of two pass, first pass uses it as a circular buffer, |
938 | | * when LAP is enabled it is used as a linear buffer*/ |
939 | 0 | twopass->stats_buf_ctx->stats_in_end++; |
940 | 0 | if ((cpi->oxcf.pass == AOM_RC_FIRST_PASS) && |
941 | 0 | (twopass->stats_buf_ctx->stats_in_end >= |
942 | 0 | twopass->stats_buf_ctx->stats_in_buf_end)) { |
943 | 0 | twopass->stats_buf_ctx->stats_in_end = |
944 | 0 | twopass->stats_buf_ctx->stats_in_start; |
945 | 0 | } |
946 | 0 | } |
947 | | |
948 | | static void print_reconstruction_frame( |
949 | | const YV12_BUFFER_CONFIG *const last_frame, int frame_number, |
950 | 0 | int do_print) { |
951 | 0 | if (!do_print) return; |
952 | | |
953 | 0 | char filename[512]; |
954 | 0 | FILE *recon_file; |
955 | 0 | snprintf(filename, sizeof(filename), "enc%04d.yuv", frame_number); |
956 | |
|
957 | 0 | if (frame_number == 0) { |
958 | 0 | recon_file = fopen(filename, "wb"); |
959 | 0 | } else { |
960 | 0 | recon_file = fopen(filename, "ab"); |
961 | 0 | } |
962 | |
|
963 | 0 | fwrite(last_frame->buffer_alloc, last_frame->frame_size, 1, recon_file); |
964 | 0 | fclose(recon_file); |
965 | 0 | } |
966 | | |
967 | | static FRAME_STATS accumulate_frame_stats(FRAME_STATS *mb_stats, int mb_rows, |
968 | 0 | int mb_cols) { |
969 | 0 | FRAME_STATS stats = { 0 }; |
970 | 0 | int i, j; |
971 | |
|
972 | 0 | stats.image_data_start_row = INVALID_ROW; |
973 | 0 | for (j = 0; j < mb_rows; j++) { |
974 | 0 | for (i = 0; i < mb_cols; i++) { |
975 | 0 | FRAME_STATS mb_stat = mb_stats[j * mb_cols + i]; |
976 | 0 | stats.brightness_factor += mb_stat.brightness_factor; |
977 | 0 | stats.coded_error += mb_stat.coded_error; |
978 | 0 | stats.frame_avg_wavelet_energy += mb_stat.frame_avg_wavelet_energy; |
979 | 0 | if (stats.image_data_start_row == INVALID_ROW && |
980 | 0 | mb_stat.image_data_start_row != INVALID_ROW) { |
981 | 0 | stats.image_data_start_row = mb_stat.image_data_start_row; |
982 | 0 | } |
983 | 0 | stats.inter_count += mb_stat.inter_count; |
984 | 0 | stats.intra_error += mb_stat.intra_error; |
985 | 0 | stats.intra_factor += mb_stat.intra_factor; |
986 | 0 | stats.intra_skip_count += mb_stat.intra_skip_count; |
987 | 0 | stats.mv_count += mb_stat.mv_count; |
988 | 0 | stats.neutral_count += mb_stat.neutral_count; |
989 | 0 | stats.new_mv_count += mb_stat.new_mv_count; |
990 | 0 | stats.second_ref_count += mb_stat.second_ref_count; |
991 | 0 | stats.sr_coded_error += mb_stat.sr_coded_error; |
992 | 0 | stats.sum_in_vectors += mb_stat.sum_in_vectors; |
993 | 0 | stats.sum_mvc += mb_stat.sum_mvc; |
994 | 0 | stats.sum_mvc_abs += mb_stat.sum_mvc_abs; |
995 | 0 | stats.sum_mvcs += mb_stat.sum_mvcs; |
996 | 0 | stats.sum_mvr += mb_stat.sum_mvr; |
997 | 0 | stats.sum_mvr_abs += mb_stat.sum_mvr_abs; |
998 | 0 | stats.sum_mvrs += mb_stat.sum_mvrs; |
999 | 0 | } |
1000 | 0 | } |
1001 | 0 | return stats; |
1002 | 0 | } |
1003 | | |
1004 | | static void setup_firstpass_data(AV1_COMMON *const cm, |
1005 | | FirstPassData *firstpass_data, |
1006 | 0 | const int unit_rows, const int unit_cols) { |
1007 | 0 | CHECK_MEM_ERROR(cm, firstpass_data->raw_motion_err_list, |
1008 | 0 | aom_calloc(unit_rows * unit_cols, |
1009 | 0 | sizeof(*firstpass_data->raw_motion_err_list))); |
1010 | 0 | CHECK_MEM_ERROR( |
1011 | 0 | cm, firstpass_data->mb_stats, |
1012 | 0 | aom_calloc(unit_rows * unit_cols, sizeof(*firstpass_data->mb_stats))); |
1013 | 0 | for (int j = 0; j < unit_rows; j++) { |
1014 | 0 | for (int i = 0; i < unit_cols; i++) { |
1015 | 0 | firstpass_data->mb_stats[j * unit_cols + i].image_data_start_row = |
1016 | 0 | INVALID_ROW; |
1017 | 0 | } |
1018 | 0 | } |
1019 | 0 | } |
1020 | | |
1021 | 0 | static void free_firstpass_data(FirstPassData *firstpass_data) { |
1022 | 0 | aom_free(firstpass_data->raw_motion_err_list); |
1023 | 0 | aom_free(firstpass_data->mb_stats); |
1024 | 0 | } |
1025 | | |
1026 | 0 | int av1_get_unit_rows_in_tile(TileInfo tile, const BLOCK_SIZE fp_block_size) { |
1027 | 0 | const int unit_height_log2 = mi_size_high_log2[fp_block_size]; |
1028 | 0 | const int mi_rows_aligned_to_unit = |
1029 | 0 | ALIGN_POWER_OF_TWO(tile.mi_row_end - tile.mi_row_start, unit_height_log2); |
1030 | 0 | const int unit_rows = mi_rows_aligned_to_unit >> unit_height_log2; |
1031 | |
|
1032 | 0 | return unit_rows; |
1033 | 0 | } |
1034 | | |
1035 | 0 | int av1_get_unit_cols_in_tile(TileInfo tile, const BLOCK_SIZE fp_block_size) { |
1036 | 0 | const int unit_width_log2 = mi_size_wide_log2[fp_block_size]; |
1037 | 0 | const int mi_cols_aligned_to_unit = |
1038 | 0 | ALIGN_POWER_OF_TWO(tile.mi_col_end - tile.mi_col_start, unit_width_log2); |
1039 | 0 | const int unit_cols = mi_cols_aligned_to_unit >> unit_width_log2; |
1040 | |
|
1041 | 0 | return unit_cols; |
1042 | 0 | } |
1043 | | |
1044 | | #define FIRST_PASS_ALT_REF_DISTANCE 16 |
1045 | | static void first_pass_tile(AV1_COMP *cpi, ThreadData *td, |
1046 | | TileDataEnc *tile_data, |
1047 | 0 | const BLOCK_SIZE fp_block_size) { |
1048 | 0 | TileInfo *tile = &tile_data->tile_info; |
1049 | 0 | const int unit_height = mi_size_high[fp_block_size]; |
1050 | 0 | const int unit_height_log2 = mi_size_high_log2[fp_block_size]; |
1051 | 0 | for (int mi_row = tile->mi_row_start; mi_row < tile->mi_row_end; |
1052 | 0 | mi_row += unit_height) { |
1053 | 0 | av1_first_pass_row(cpi, td, tile_data, mi_row >> unit_height_log2, |
1054 | 0 | fp_block_size); |
1055 | 0 | } |
1056 | 0 | } |
1057 | | |
1058 | 0 | static void first_pass_tiles(AV1_COMP *cpi, const BLOCK_SIZE fp_block_size) { |
1059 | 0 | AV1_COMMON *const cm = &cpi->common; |
1060 | 0 | const int tile_cols = cm->tiles.cols; |
1061 | 0 | const int tile_rows = cm->tiles.rows; |
1062 | 0 | const int num_planes = av1_num_planes(&cpi->common); |
1063 | 0 | for (int plane = 0; plane < num_planes; plane++) { |
1064 | 0 | const int subsampling_xy = |
1065 | 0 | plane ? cm->seq_params->subsampling_x + cm->seq_params->subsampling_y |
1066 | 0 | : 0; |
1067 | 0 | const int sb_size = MAX_SB_SQUARE >> subsampling_xy; |
1068 | 0 | CHECK_MEM_ERROR( |
1069 | 0 | cm, cpi->td.mb.plane[plane].src_diff, |
1070 | 0 | (int16_t *)aom_memalign( |
1071 | 0 | 32, sizeof(*cpi->td.mb.plane[plane].src_diff) * sb_size)); |
1072 | 0 | } |
1073 | 0 | for (int tile_row = 0; tile_row < tile_rows; ++tile_row) { |
1074 | 0 | for (int tile_col = 0; tile_col < tile_cols; ++tile_col) { |
1075 | 0 | TileDataEnc *const tile_data = |
1076 | 0 | &cpi->tile_data[tile_row * tile_cols + tile_col]; |
1077 | 0 | first_pass_tile(cpi, &cpi->td, tile_data, fp_block_size); |
1078 | 0 | } |
1079 | 0 | } |
1080 | 0 | for (int plane = 0; plane < num_planes; plane++) { |
1081 | 0 | if (cpi->td.mb.plane[plane].src_diff) { |
1082 | 0 | aom_free(cpi->td.mb.plane[plane].src_diff); |
1083 | 0 | cpi->td.mb.plane[plane].src_diff = NULL; |
1084 | 0 | } |
1085 | 0 | } |
1086 | 0 | } |
1087 | | |
1088 | | void av1_first_pass_row(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data, |
1089 | 0 | const int unit_row, const BLOCK_SIZE fp_block_size) { |
1090 | 0 | MACROBLOCK *const x = &td->mb; |
1091 | 0 | AV1_COMMON *const cm = &cpi->common; |
1092 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
1093 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
1094 | 0 | const int num_planes = av1_num_planes(cm); |
1095 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
1096 | 0 | TileInfo *tile = &tile_data->tile_info; |
1097 | 0 | const int qindex = find_fp_qindex(seq_params->bit_depth); |
1098 | 0 | const int fp_block_size_width = block_size_high[fp_block_size]; |
1099 | 0 | const int fp_block_size_height = block_size_wide[fp_block_size]; |
1100 | 0 | const int unit_width = mi_size_wide[fp_block_size]; |
1101 | 0 | const int unit_width_log2 = mi_size_wide_log2[fp_block_size]; |
1102 | 0 | const int unit_height_log2 = mi_size_high_log2[fp_block_size]; |
1103 | 0 | const int unit_cols = mi_params->mb_cols * 4 / unit_width; |
1104 | 0 | int raw_motion_err_counts = 0; |
1105 | 0 | int unit_row_in_tile = unit_row - (tile->mi_row_start >> unit_height_log2); |
1106 | 0 | int unit_col_start = tile->mi_col_start >> unit_width_log2; |
1107 | 0 | int unit_cols_in_tile = av1_get_unit_cols_in_tile(*tile, fp_block_size); |
1108 | 0 | MultiThreadInfo *const mt_info = &cpi->mt_info; |
1109 | 0 | AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt; |
1110 | 0 | AV1EncRowMultiThreadSync *const row_mt_sync = &tile_data->row_mt_sync; |
1111 | |
|
1112 | 0 | const YV12_BUFFER_CONFIG *const last_frame = |
1113 | 0 | get_ref_frame_yv12_buf(cm, LAST_FRAME); |
1114 | 0 | const YV12_BUFFER_CONFIG *golden_frame = |
1115 | 0 | get_ref_frame_yv12_buf(cm, GOLDEN_FRAME); |
1116 | 0 | YV12_BUFFER_CONFIG *const this_frame = &cm->cur_frame->buf; |
1117 | |
|
1118 | 0 | PICK_MODE_CONTEXT *ctx = td->firstpass_ctx; |
1119 | 0 | FRAME_STATS *mb_stats = |
1120 | 0 | cpi->firstpass_data.mb_stats + unit_row * unit_cols + unit_col_start; |
1121 | 0 | int *raw_motion_err_list = cpi->firstpass_data.raw_motion_err_list + |
1122 | 0 | unit_row * unit_cols + unit_col_start; |
1123 | 0 | MV *first_top_mv = &tile_data->firstpass_top_mv; |
1124 | |
|
1125 | 0 | for (int i = 0; i < num_planes; ++i) { |
1126 | 0 | x->plane[i].coeff = ctx->coeff[i]; |
1127 | 0 | x->plane[i].qcoeff = ctx->qcoeff[i]; |
1128 | 0 | x->plane[i].eobs = ctx->eobs[i]; |
1129 | 0 | x->plane[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i]; |
1130 | 0 | x->plane[i].dqcoeff = ctx->dqcoeff[i]; |
1131 | 0 | } |
1132 | |
|
1133 | 0 | const int src_y_stride = cpi->source->y_stride; |
1134 | 0 | const int recon_y_stride = this_frame->y_stride; |
1135 | 0 | const int recon_uv_stride = this_frame->uv_stride; |
1136 | 0 | const int uv_mb_height = |
1137 | 0 | fp_block_size_height >> (this_frame->y_height > this_frame->uv_height); |
1138 | |
|
1139 | 0 | MV best_ref_mv = kZeroMv; |
1140 | 0 | MV last_mv; |
1141 | | |
1142 | | // Reset above block coeffs. |
1143 | 0 | xd->up_available = (unit_row_in_tile != 0); |
1144 | 0 | int recon_yoffset = (unit_row * recon_y_stride * fp_block_size_height) + |
1145 | 0 | (unit_col_start * fp_block_size_width); |
1146 | 0 | int src_yoffset = (unit_row * src_y_stride * fp_block_size_height) + |
1147 | 0 | (unit_col_start * fp_block_size_width); |
1148 | 0 | int recon_uvoffset = (unit_row * recon_uv_stride * uv_mb_height) + |
1149 | 0 | (unit_col_start * uv_mb_height); |
1150 | | |
1151 | | // Set up limit values for motion vectors to prevent them extending |
1152 | | // outside the UMV borders. |
1153 | 0 | av1_set_mv_row_limits( |
1154 | 0 | mi_params, &x->mv_limits, (unit_row << unit_height_log2), |
1155 | 0 | (fp_block_size_height >> MI_SIZE_LOG2), cpi->oxcf.border_in_pixels); |
1156 | |
|
1157 | 0 | av1_setup_src_planes(x, cpi->source, unit_row << unit_height_log2, |
1158 | 0 | tile->mi_col_start, num_planes, fp_block_size); |
1159 | | |
1160 | | // Fix - zero the 16x16 block first. This ensures correct this_intra_error for |
1161 | | // block sizes smaller than 16x16. |
1162 | 0 | av1_zero_array(x->plane[0].src_diff, 256); |
1163 | |
|
1164 | 0 | for (int mi_col = tile->mi_col_start; mi_col < tile->mi_col_end; |
1165 | 0 | mi_col += unit_width) { |
1166 | 0 | const int unit_col = mi_col >> unit_width_log2; |
1167 | 0 | const int unit_col_in_tile = unit_col - unit_col_start; |
1168 | |
|
1169 | 0 | (*(enc_row_mt->sync_read_ptr))(row_mt_sync, unit_row_in_tile, |
1170 | 0 | unit_col_in_tile); |
1171 | |
|
1172 | 0 | if (unit_col_in_tile == 0) { |
1173 | 0 | last_mv = *first_top_mv; |
1174 | 0 | } |
1175 | 0 | int this_intra_error = firstpass_intra_prediction( |
1176 | 0 | cpi, td, this_frame, tile, unit_row, unit_col, recon_yoffset, |
1177 | 0 | recon_uvoffset, fp_block_size, qindex, mb_stats); |
1178 | |
|
1179 | 0 | if (!frame_is_intra_only(cm)) { |
1180 | 0 | const int this_inter_error = firstpass_inter_prediction( |
1181 | 0 | cpi, td, last_frame, golden_frame, unit_row, unit_col, recon_yoffset, |
1182 | 0 | recon_uvoffset, src_yoffset, fp_block_size, this_intra_error, |
1183 | 0 | raw_motion_err_counts, raw_motion_err_list, best_ref_mv, &best_ref_mv, |
1184 | 0 | &last_mv, mb_stats); |
1185 | 0 | if (unit_col_in_tile == 0) { |
1186 | 0 | *first_top_mv = last_mv; |
1187 | 0 | } |
1188 | 0 | mb_stats->coded_error += this_inter_error; |
1189 | 0 | ++raw_motion_err_counts; |
1190 | 0 | } else { |
1191 | 0 | mb_stats->sr_coded_error += this_intra_error; |
1192 | 0 | mb_stats->coded_error += this_intra_error; |
1193 | 0 | } |
1194 | | |
1195 | | // Adjust to the next column of MBs. |
1196 | 0 | x->plane[0].src.buf += fp_block_size_width; |
1197 | 0 | x->plane[1].src.buf += uv_mb_height; |
1198 | 0 | x->plane[2].src.buf += uv_mb_height; |
1199 | |
|
1200 | 0 | recon_yoffset += fp_block_size_width; |
1201 | 0 | src_yoffset += fp_block_size_width; |
1202 | 0 | recon_uvoffset += uv_mb_height; |
1203 | 0 | mb_stats++; |
1204 | |
|
1205 | 0 | (*(enc_row_mt->sync_write_ptr))(row_mt_sync, unit_row_in_tile, |
1206 | 0 | unit_col_in_tile, unit_cols_in_tile); |
1207 | 0 | } |
1208 | 0 | } |
1209 | | |
1210 | 0 | void av1_first_pass(AV1_COMP *cpi, const int64_t ts_duration) { |
1211 | 0 | MACROBLOCK *const x = &cpi->td.mb; |
1212 | 0 | AV1_COMMON *const cm = &cpi->common; |
1213 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
1214 | 0 | CurrentFrame *const current_frame = &cm->current_frame; |
1215 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
1216 | 0 | const int num_planes = av1_num_planes(cm); |
1217 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
1218 | 0 | const int qindex = find_fp_qindex(seq_params->bit_depth); |
1219 | | // Detect if the key frame is screen content type. |
1220 | 0 | if (frame_is_intra_only(cm)) { |
1221 | 0 | FeatureFlags *const features = &cm->features; |
1222 | 0 | av1_set_screen_content_options(cpi, features); |
1223 | 0 | } |
1224 | | |
1225 | | // Prepare the speed features |
1226 | 0 | av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed); |
1227 | | |
1228 | | // Unit size for the first pass encoding. |
1229 | 0 | const BLOCK_SIZE fp_block_size = |
1230 | 0 | get_fp_block_size(cpi->is_screen_content_type); |
1231 | | |
1232 | | // Number of rows in the unit size. |
1233 | | // Note mi_params->mb_rows and mi_params->mb_cols are in the unit of 16x16. |
1234 | 0 | const int unit_rows = get_unit_rows(fp_block_size, mi_params->mb_rows); |
1235 | 0 | const int unit_cols = get_unit_cols(fp_block_size, mi_params->mb_cols); |
1236 | | |
1237 | | // Set fp_block_size, for the convenience of multi-thread usage. |
1238 | 0 | cpi->fp_block_size = fp_block_size; |
1239 | |
|
1240 | 0 | setup_firstpass_data(cm, &cpi->firstpass_data, unit_rows, unit_cols); |
1241 | 0 | int *raw_motion_err_list = cpi->firstpass_data.raw_motion_err_list; |
1242 | 0 | FRAME_STATS *mb_stats = cpi->firstpass_data.mb_stats; |
1243 | | |
1244 | | // multi threading info |
1245 | 0 | MultiThreadInfo *const mt_info = &cpi->mt_info; |
1246 | 0 | AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt; |
1247 | |
|
1248 | 0 | const int tile_cols = cm->tiles.cols; |
1249 | 0 | const int tile_rows = cm->tiles.rows; |
1250 | 0 | if (cpi->allocated_tiles < tile_cols * tile_rows) { |
1251 | 0 | av1_row_mt_mem_dealloc(cpi); |
1252 | 0 | av1_alloc_tile_data(cpi); |
1253 | 0 | } |
1254 | |
|
1255 | 0 | av1_init_tile_data(cpi); |
1256 | |
|
1257 | 0 | const YV12_BUFFER_CONFIG *const last_frame = |
1258 | 0 | get_ref_frame_yv12_buf(cm, LAST_FRAME); |
1259 | 0 | const YV12_BUFFER_CONFIG *golden_frame = |
1260 | 0 | get_ref_frame_yv12_buf(cm, GOLDEN_FRAME); |
1261 | 0 | YV12_BUFFER_CONFIG *const this_frame = &cm->cur_frame->buf; |
1262 | | // First pass code requires valid last and new frame buffers. |
1263 | 0 | assert(this_frame != NULL); |
1264 | 0 | assert(frame_is_intra_only(cm) || (last_frame != NULL)); |
1265 | |
|
1266 | 0 | av1_setup_frame_size(cpi); |
1267 | 0 | av1_set_mv_search_params(cpi); |
1268 | |
|
1269 | 0 | set_mi_offsets(mi_params, xd, 0, 0); |
1270 | 0 | xd->mi[0]->bsize = fp_block_size; |
1271 | | |
1272 | | // Do not use periodic key frames. |
1273 | 0 | cpi->rc.frames_to_key = INT_MAX; |
1274 | |
|
1275 | 0 | av1_set_quantizer( |
1276 | 0 | cm, cpi->oxcf.q_cfg.qm_minlevel, cpi->oxcf.q_cfg.qm_maxlevel, qindex, |
1277 | 0 | cpi->oxcf.q_cfg.enable_chroma_deltaq, cpi->oxcf.q_cfg.enable_hdr_deltaq); |
1278 | |
|
1279 | 0 | av1_setup_block_planes(xd, seq_params->subsampling_x, |
1280 | 0 | seq_params->subsampling_y, num_planes); |
1281 | |
|
1282 | 0 | av1_setup_src_planes(x, cpi->source, 0, 0, num_planes, fp_block_size); |
1283 | 0 | av1_setup_dst_planes(xd->plane, seq_params->sb_size, this_frame, 0, 0, 0, |
1284 | 0 | num_planes); |
1285 | |
|
1286 | 0 | if (!frame_is_intra_only(cm)) { |
1287 | 0 | av1_setup_pre_planes(xd, 0, last_frame, 0, 0, NULL, num_planes); |
1288 | 0 | } |
1289 | |
|
1290 | 0 | set_mi_offsets(mi_params, xd, 0, 0); |
1291 | | |
1292 | | // Don't store luma on the fist pass since chroma is not computed |
1293 | 0 | xd->cfl.store_y = 0; |
1294 | 0 | av1_frame_init_quantizer(cpi); |
1295 | |
|
1296 | 0 | av1_default_coef_probs(cm); |
1297 | 0 | av1_init_mode_probs(cm->fc); |
1298 | 0 | av1_init_mv_probs(cm); |
1299 | 0 | av1_initialize_rd_consts(cpi); |
1300 | |
|
1301 | 0 | enc_row_mt->sync_read_ptr = av1_row_mt_sync_read_dummy; |
1302 | 0 | enc_row_mt->sync_write_ptr = av1_row_mt_sync_write_dummy; |
1303 | |
|
1304 | 0 | if (mt_info->num_workers > 1) { |
1305 | 0 | enc_row_mt->sync_read_ptr = av1_row_mt_sync_read; |
1306 | 0 | enc_row_mt->sync_write_ptr = av1_row_mt_sync_write; |
1307 | 0 | av1_fp_encode_tiles_row_mt(cpi); |
1308 | 0 | } else { |
1309 | 0 | first_pass_tiles(cpi, fp_block_size); |
1310 | 0 | } |
1311 | |
|
1312 | 0 | FRAME_STATS stats = accumulate_frame_stats(mb_stats, unit_rows, unit_cols); |
1313 | 0 | int total_raw_motion_err_count = |
1314 | 0 | frame_is_intra_only(cm) ? 0 : unit_rows * unit_cols; |
1315 | 0 | const double raw_err_stdev = |
1316 | 0 | raw_motion_error_stdev(raw_motion_err_list, total_raw_motion_err_count); |
1317 | 0 | free_firstpass_data(&cpi->firstpass_data); |
1318 | | |
1319 | | // Clamp the image start to rows/2. This number of rows is discarded top |
1320 | | // and bottom as dead data so rows / 2 means the frame is blank. |
1321 | 0 | if ((stats.image_data_start_row > unit_rows / 2) || |
1322 | 0 | (stats.image_data_start_row == INVALID_ROW)) { |
1323 | 0 | stats.image_data_start_row = unit_rows / 2; |
1324 | 0 | } |
1325 | | // Exclude any image dead zone |
1326 | 0 | if (stats.image_data_start_row > 0) { |
1327 | 0 | stats.intra_skip_count = |
1328 | 0 | AOMMAX(0, stats.intra_skip_count - |
1329 | 0 | (stats.image_data_start_row * unit_cols * 2)); |
1330 | 0 | } |
1331 | |
|
1332 | 0 | TWO_PASS *twopass = &cpi->ppi->twopass; |
1333 | 0 | const int num_mbs_16X16 = (cpi->oxcf.resize_cfg.resize_mode != RESIZE_NONE) |
1334 | 0 | ? cpi->initial_mbs |
1335 | 0 | : mi_params->MBs; |
1336 | | // Number of actual units used in the first pass, it can be other square |
1337 | | // block sizes than 16X16. |
1338 | 0 | const int num_mbs = get_num_mbs(fp_block_size, num_mbs_16X16); |
1339 | 0 | stats.intra_factor = stats.intra_factor / (double)num_mbs; |
1340 | 0 | stats.brightness_factor = stats.brightness_factor / (double)num_mbs; |
1341 | 0 | FIRSTPASS_STATS *this_frame_stats = twopass->stats_buf_ctx->stats_in_end; |
1342 | 0 | update_firstpass_stats(cpi, &stats, raw_err_stdev, |
1343 | 0 | current_frame->frame_number, ts_duration, |
1344 | 0 | fp_block_size); |
1345 | | |
1346 | | // Copy the previous Last Frame back into gf buffer if the prediction is good |
1347 | | // enough... but also don't allow it to lag too far. |
1348 | 0 | if ((twopass->sr_update_lag > 3) || |
1349 | 0 | ((current_frame->frame_number > 0) && |
1350 | 0 | (this_frame_stats->pcnt_inter > 0.20) && |
1351 | 0 | ((this_frame_stats->intra_error / |
1352 | 0 | DOUBLE_DIVIDE_CHECK(this_frame_stats->coded_error)) > 2.0))) { |
1353 | 0 | if (golden_frame != NULL) { |
1354 | 0 | assign_frame_buffer_p( |
1355 | 0 | &cm->ref_frame_map[get_ref_frame_map_idx(cm, GOLDEN_FRAME)], |
1356 | 0 | cm->ref_frame_map[get_ref_frame_map_idx(cm, LAST_FRAME)]); |
1357 | 0 | } |
1358 | 0 | twopass->sr_update_lag = 1; |
1359 | 0 | } else { |
1360 | 0 | ++twopass->sr_update_lag; |
1361 | 0 | } |
1362 | |
|
1363 | 0 | aom_extend_frame_borders(this_frame, num_planes); |
1364 | | |
1365 | | // The frame we just compressed now becomes the last frame. |
1366 | 0 | assign_frame_buffer_p( |
1367 | 0 | &cm->ref_frame_map[get_ref_frame_map_idx(cm, LAST_FRAME)], cm->cur_frame); |
1368 | | |
1369 | | // Special case for the first frame. Copy into the GF buffer as a second |
1370 | | // reference. |
1371 | 0 | if (current_frame->frame_number == 0 && |
1372 | 0 | get_ref_frame_map_idx(cm, GOLDEN_FRAME) != INVALID_IDX) { |
1373 | 0 | assign_frame_buffer_p( |
1374 | 0 | &cm->ref_frame_map[get_ref_frame_map_idx(cm, GOLDEN_FRAME)], |
1375 | 0 | cm->ref_frame_map[get_ref_frame_map_idx(cm, LAST_FRAME)]); |
1376 | 0 | } |
1377 | |
|
1378 | 0 | print_reconstruction_frame(last_frame, current_frame->frame_number, |
1379 | 0 | /*do_print=*/0); |
1380 | |
|
1381 | 0 | ++current_frame->frame_number; |
1382 | 0 | } |
1383 | | |
1384 | | aom_codec_err_t av1_firstpass_info_init(FIRSTPASS_INFO *firstpass_info, |
1385 | | FIRSTPASS_STATS *ext_stats_buf, |
1386 | 0 | int ext_stats_buf_size) { |
1387 | 0 | assert(IMPLIES(ext_stats_buf == NULL, ext_stats_buf_size == 0)); |
1388 | 0 | if (ext_stats_buf == NULL) { |
1389 | 0 | firstpass_info->stats_buf = firstpass_info->static_stats_buf; |
1390 | 0 | firstpass_info->stats_buf_size = |
1391 | 0 | sizeof(firstpass_info->static_stats_buf) / |
1392 | 0 | sizeof(firstpass_info->static_stats_buf[0]); |
1393 | 0 | firstpass_info->start_index = 0; |
1394 | 0 | firstpass_info->cur_index = 0; |
1395 | 0 | firstpass_info->stats_count = 0; |
1396 | 0 | firstpass_info->future_stats_count = 0; |
1397 | 0 | firstpass_info->past_stats_count = 0; |
1398 | 0 | av1_zero(firstpass_info->total_stats); |
1399 | 0 | if (ext_stats_buf_size == 0) { |
1400 | 0 | return AOM_CODEC_OK; |
1401 | 0 | } else { |
1402 | 0 | return AOM_CODEC_ERROR; |
1403 | 0 | } |
1404 | 0 | } else { |
1405 | 0 | firstpass_info->stats_buf = ext_stats_buf; |
1406 | 0 | firstpass_info->stats_buf_size = ext_stats_buf_size; |
1407 | 0 | firstpass_info->start_index = 0; |
1408 | 0 | firstpass_info->cur_index = 0; |
1409 | 0 | firstpass_info->stats_count = firstpass_info->stats_buf_size; |
1410 | 0 | firstpass_info->future_stats_count = firstpass_info->stats_count; |
1411 | 0 | firstpass_info->past_stats_count = 0; |
1412 | 0 | av1_zero(firstpass_info->total_stats); |
1413 | 0 | for (int i = 0; i < firstpass_info->stats_count; ++i) { |
1414 | 0 | av1_accumulate_stats(&firstpass_info->total_stats, |
1415 | 0 | &firstpass_info->stats_buf[i]); |
1416 | 0 | } |
1417 | 0 | } |
1418 | 0 | return AOM_CODEC_OK; |
1419 | 0 | } |
1420 | | |
1421 | | aom_codec_err_t av1_firstpass_info_move_cur_index( |
1422 | 1.26k | FIRSTPASS_INFO *firstpass_info) { |
1423 | 1.26k | assert(firstpass_info->future_stats_count + |
1424 | 1.26k | firstpass_info->past_stats_count == |
1425 | 1.26k | firstpass_info->stats_count); |
1426 | 1.26k | if (firstpass_info->future_stats_count > 1) { |
1427 | 0 | firstpass_info->cur_index = |
1428 | 0 | (firstpass_info->cur_index + 1) % firstpass_info->stats_buf_size; |
1429 | 0 | --firstpass_info->future_stats_count; |
1430 | 0 | ++firstpass_info->past_stats_count; |
1431 | 0 | return AOM_CODEC_OK; |
1432 | 1.26k | } else { |
1433 | 1.26k | return AOM_CODEC_ERROR; |
1434 | 1.26k | } |
1435 | 1.26k | } |
1436 | | |
1437 | 0 | aom_codec_err_t av1_firstpass_info_pop(FIRSTPASS_INFO *firstpass_info) { |
1438 | 0 | if (firstpass_info->stats_count > 0 && firstpass_info->past_stats_count > 0) { |
1439 | 0 | const int next_start = |
1440 | 0 | (firstpass_info->start_index + 1) % firstpass_info->stats_buf_size; |
1441 | 0 | firstpass_info->start_index = next_start; |
1442 | 0 | --firstpass_info->stats_count; |
1443 | 0 | --firstpass_info->past_stats_count; |
1444 | 0 | return AOM_CODEC_OK; |
1445 | 0 | } else { |
1446 | 0 | return AOM_CODEC_ERROR; |
1447 | 0 | } |
1448 | 0 | } |
1449 | | |
1450 | | aom_codec_err_t av1_firstpass_info_move_cur_index_and_pop( |
1451 | 0 | FIRSTPASS_INFO *firstpass_info) { |
1452 | 0 | aom_codec_err_t ret = av1_firstpass_info_move_cur_index(firstpass_info); |
1453 | 0 | if (ret != AOM_CODEC_OK) return ret; |
1454 | 0 | ret = av1_firstpass_info_pop(firstpass_info); |
1455 | 0 | return ret; |
1456 | 0 | } |
1457 | | |
1458 | | aom_codec_err_t av1_firstpass_info_push(FIRSTPASS_INFO *firstpass_info, |
1459 | 0 | const FIRSTPASS_STATS *input_stats) { |
1460 | 0 | if (firstpass_info->stats_count < firstpass_info->stats_buf_size) { |
1461 | 0 | const int next_index = |
1462 | 0 | (firstpass_info->start_index + firstpass_info->stats_count) % |
1463 | 0 | firstpass_info->stats_buf_size; |
1464 | 0 | firstpass_info->stats_buf[next_index] = *input_stats; |
1465 | 0 | ++firstpass_info->stats_count; |
1466 | 0 | ++firstpass_info->future_stats_count; |
1467 | 0 | av1_accumulate_stats(&firstpass_info->total_stats, input_stats); |
1468 | 0 | return AOM_CODEC_OK; |
1469 | 0 | } else { |
1470 | 0 | return AOM_CODEC_ERROR; |
1471 | 0 | } |
1472 | 0 | } |
1473 | | |
1474 | | const FIRSTPASS_STATS *av1_firstpass_info_peek( |
1475 | 0 | const FIRSTPASS_INFO *firstpass_info, int offset_from_cur) { |
1476 | 0 | if (offset_from_cur >= -firstpass_info->past_stats_count && |
1477 | 0 | offset_from_cur < firstpass_info->future_stats_count) { |
1478 | 0 | const int index = (firstpass_info->cur_index + offset_from_cur) % |
1479 | 0 | firstpass_info->stats_buf_size; |
1480 | 0 | return &firstpass_info->stats_buf[index]; |
1481 | 0 | } else { |
1482 | 0 | return NULL; |
1483 | 0 | } |
1484 | 0 | } |
1485 | | |
1486 | | int av1_firstpass_info_future_count(const FIRSTPASS_INFO *firstpass_info, |
1487 | 0 | int offset_from_cur) { |
1488 | 0 | if (offset_from_cur < firstpass_info->future_stats_count) { |
1489 | 0 | return firstpass_info->future_stats_count - offset_from_cur; |
1490 | 0 | } |
1491 | 0 | return 0; |
1492 | 0 | } |
1493 | | |
1494 | | int av1_firstpass_info_past_count(const FIRSTPASS_INFO *firstpass_info, |
1495 | 0 | int offset_from_cur) { |
1496 | 0 | if (offset_from_cur >= -firstpass_info->past_stats_count) { |
1497 | 0 | return offset_from_cur + firstpass_info->past_stats_count; |
1498 | 0 | } |
1499 | 0 | return 0; |
1500 | 0 | } |