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

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

Issue 181493009: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 9 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_onyx_int.h ('k') | source/libvpx/vp9/encoder/vp9_psnr.h » ('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) 2014 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2014 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>
12 #include <limits.h>
13 #include <math.h>
11 #include <stdio.h> 14 #include <stdio.h>
12 #include <math.h>
13 #include <limits.h>
14 #include <assert.h>
15 15
16 #include "vp9/common/vp9_pragmas.h" 16 #include "./vp9_rtcd.h"
17 #include "vp9/encoder/vp9_tokenize.h" 17
18 #include "vp9/encoder/vp9_treewriter.h" 18 #include "vpx_mem/vpx_mem.h"
19 #include "vp9/encoder/vp9_onyx_int.h" 19
20 #include "vp9/common/vp9_entropymode.h" 20 #include "vp9/common/vp9_common.h"
21 #include "vp9/common/vp9_mvref_common.h"
21 #include "vp9/common/vp9_reconinter.h" 22 #include "vp9/common/vp9_reconinter.h"
22 #include "vp9/common/vp9_reconintra.h" 23 #include "vp9/common/vp9_reconintra.h"
23 #include "vp9/common/vp9_quant_common.h" 24
24 #include "vp9/encoder/vp9_encodemb.h" 25 #include "vp9/encoder/vp9_onyx_int.h"
25 #include "vp9/encoder/vp9_quantize.h" 26 #include "vp9/encoder/vp9_ratectrl.h"
26 #include "vp9/encoder/vp9_variance.h"
27 #include "vp9/encoder/vp9_mcomp.h"
28 #include "vp9/encoder/vp9_rdopt.h" 27 #include "vp9/encoder/vp9_rdopt.h"
29 #include "vp9/encoder/vp9_ratectrl.h"
30 #include "vpx_mem/vpx_mem.h"
31 #include "vp9/common/vp9_systemdependent.h"
32 #include "vp9/encoder/vp9_encodemv.h"
33 #include "vp9/common/vp9_seg_common.h"
34 #include "vp9/common/vp9_pred_common.h"
35 #include "vp9/common/vp9_entropy.h"
36 #include "./vp9_rtcd.h"
37 #include "vp9/common/vp9_mvref_common.h"
38 #include "vp9/common/vp9_common.h"
39 28
40 static int full_pixel_motion_search(VP9_COMP *cpi, MACROBLOCK *x, 29 static int full_pixel_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
41 const TileInfo *const tile, 30 const TileInfo *const tile,
42 BLOCK_SIZE bsize, int mi_row, int mi_col, 31 BLOCK_SIZE bsize, int mi_row, int mi_col,
43 int_mv *tmp_mv, int *rate_mv) { 32 int_mv *tmp_mv, int *rate_mv) {
44 MACROBLOCKD *xd = &x->e_mbd; 33 MACROBLOCKD *xd = &x->e_mbd;
45 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; 34 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
46 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0}}; 35 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0}};
47 int bestsme = INT_MAX; 36 int bestsme = INT_MAX;
48 int further_steps, step_param; 37 int further_steps, step_param;
49 int sadpb = x->sadperbit16; 38 int sadpb = x->sadperbit16;
50 MV mvp_full; 39 MV mvp_full;
51 int ref = mbmi->ref_frame[0]; 40 int ref = mbmi->ref_frame[0];
52 int_mv ref_mv = mbmi->ref_mvs[ref][0]; 41 int_mv ref_mv = mbmi->ref_mvs[ref][0];
53 int i; 42 int i;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 } 80 }
92 return INT_MAX; 81 return INT_MAX;
93 } 82 }
94 } 83 }
95 84
96 mvp_full = mbmi->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv; 85 mvp_full = mbmi->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
97 86
98 mvp_full.col >>= 3; 87 mvp_full.col >>= 3;
99 mvp_full.row >>= 3; 88 mvp_full.row >>= 3;
100 89
101 vp9_full_pixel_diamond(cpi, x, &mvp_full, step_param, sadpb, further_steps, 1, 90 if (cpi->sf.search_method == FAST_HEX) {
102 &cpi->fn_ptr[bsize], &ref_mv.as_mv, &tmp_mv->as_mv); 91 bestsme = vp9_fast_hex_search(x, &mvp_full, step_param, sadpb,
92 &cpi->fn_ptr[bsize], 1,
93 &ref_mv.as_mv, &tmp_mv->as_mv);
94 } else if (cpi->sf.search_method == HEX) {
95 bestsme = vp9_hex_search(x, &mvp_full, step_param, sadpb, 1,
96 &cpi->fn_ptr[bsize], 1,
97 &ref_mv.as_mv, &tmp_mv->as_mv);
98 } else if (cpi->sf.search_method == SQUARE) {
99 bestsme = vp9_square_search(x, &mvp_full, step_param, sadpb, 1,
100 &cpi->fn_ptr[bsize], 1,
101 &ref_mv.as_mv, &tmp_mv->as_mv);
102 } else if (cpi->sf.search_method == BIGDIA) {
103 bestsme = vp9_bigdia_search(x, &mvp_full, step_param, sadpb, 1,
104 &cpi->fn_ptr[bsize], 1,
105 &ref_mv.as_mv, &tmp_mv->as_mv);
106 } else {
107 bestsme = vp9_full_pixel_diamond(cpi, x, &mvp_full, step_param,
108 sadpb, further_steps, 1,
109 &cpi->fn_ptr[bsize],
110 &ref_mv.as_mv, &tmp_mv->as_mv);
111 }
103 x->mv_col_min = tmp_col_min; 112 x->mv_col_min = tmp_col_min;
104 x->mv_col_max = tmp_col_max; 113 x->mv_col_max = tmp_col_max;
105 x->mv_row_min = tmp_row_min; 114 x->mv_row_min = tmp_row_min;
106 x->mv_row_max = tmp_row_max; 115 x->mv_row_max = tmp_row_max;
107 116
108 if (scaled_ref_frame) { 117 if (scaled_ref_frame) {
109 int i; 118 int i;
110 for (i = 0; i < MAX_MB_PLANE; i++) 119 for (i = 0; i < MAX_MB_PLANE; i++)
111 xd->plane[i].pre[0] = backup_yv12[i]; 120 xd->plane[i].pre[0] = backup_yv12[i];
112 } 121 }
113 122
114 // TODO(jingning) This step can be merged into full pixel search step in the 123 // TODO(jingning) This step can be merged into full pixel search step in the
115 // re-designed log-diamond search 124 // re-designed log-diamond search
116 buf_offset = tmp_mv->as_mv.row * stride + tmp_mv->as_mv.col; 125 buf_offset = tmp_mv->as_mv.row * stride + tmp_mv->as_mv.col;
117 126
118 // Find sad for current vector. 127 // Find sad for current vector.
119 bestsme = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, x->plane[0].src.stride, 128 bestsme = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, x->plane[0].src.stride,
120 xd->plane[0].pre[0].buf + buf_offset, 129 xd->plane[0].pre[0].buf + buf_offset,
121 stride, 0x7fffffff); 130 stride, 0x7fffffff);
122 131
123 // scale to 1/8 pixel resolution 132 // scale to 1/8 pixel resolution
124 tmp_mv->as_mv.row = tmp_mv->as_mv.row * 8; 133 tmp_mv->as_mv.row = tmp_mv->as_mv.row * 8;
125 tmp_mv->as_mv.col = tmp_mv->as_mv.col * 8; 134 tmp_mv->as_mv.col = tmp_mv->as_mv.col * 8;
126 135
127 // calculate the bit cost on motion vector 136 // calculate the bit cost on motion vector
128 *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv.as_mv, 137 *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv.as_mv,
129 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); 138 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
130
131
132 return bestsme; 139 return bestsme;
133 } 140 }
134 141
142 static void sub_pixel_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
143 const TileInfo *const tile,
144 BLOCK_SIZE bsize, int mi_row, int mi_col,
145 MV *tmp_mv) {
146 MACROBLOCKD *xd = &x->e_mbd;
147 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
148 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0}};
149 int ref = mbmi->ref_frame[0];
150 MV ref_mv = mbmi->ref_mvs[ref][0].as_mv;
151 int dis;
152
153 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
154 ref);
155 if (scaled_ref_frame) {
156 int i;
157 // Swap out the reference frame for a version that's been scaled to
158 // match the resolution of the current frame, allowing the existing
159 // motion search code to be used without additional modifications.
160 for (i = 0; i < MAX_MB_PLANE; i++)
161 backup_yv12[i] = xd->plane[i].pre[0];
162
163 setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
164 }
165
166 tmp_mv->col >>= 3;
167 tmp_mv->row >>= 3;
168
169 cpi->find_fractional_mv_step(x, tmp_mv, &ref_mv,
170 cpi->common.allow_high_precision_mv,
171 x->errorperbit,
172 &cpi->fn_ptr[bsize],
173 cpi->sf.subpel_force_stop,
174 cpi->sf.subpel_iters_per_step,
175 x->nmvjointcost, x->mvcost,
176 &dis, &x->pred_sse[ref]);
177
178 if (scaled_ref_frame) {
179 int i;
180 for (i = 0; i < MAX_MB_PLANE; i++)
181 xd->plane[i].pre[0] = backup_yv12[i];
182 }
183 }
184
135 // TODO(jingning) placeholder for inter-frame non-RD mode decision. 185 // TODO(jingning) placeholder for inter-frame non-RD mode decision.
136 // this needs various further optimizations. to be continued.. 186 // this needs various further optimizations. to be continued..
137 int64_t vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, 187 int64_t vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x,
138 const TileInfo *const tile, 188 const TileInfo *const tile,
139 int mi_row, int mi_col, 189 int mi_row, int mi_col,
140 int *returnrate, 190 int *returnrate,
141 int64_t *returndistortion, 191 int64_t *returndistortion,
142 BLOCK_SIZE bsize) { 192 BLOCK_SIZE bsize) {
143 MACROBLOCKD *xd = &x->e_mbd; 193 MACROBLOCKD *xd = &x->e_mbd;
144 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; 194 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
195 struct macroblock_plane *const p = &x->plane[0];
196 struct macroblockd_plane *const pd = &xd->plane[0];
145 const BLOCK_SIZE block_size = get_plane_block_size(bsize, &xd->plane[0]); 197 const BLOCK_SIZE block_size = get_plane_block_size(bsize, &xd->plane[0]);
146 MB_PREDICTION_MODE this_mode; 198 MB_PREDICTION_MODE this_mode, best_mode = ZEROMV;
147 MV_REFERENCE_FRAME ref_frame; 199 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME;
148 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES]; 200 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
149 struct buf_2d yv12_mb[4][MAX_MB_PLANE]; 201 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
150 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG, 202 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
151 VP9_ALT_FLAG }; 203 VP9_ALT_FLAG };
152 int64_t best_rd = INT64_MAX; 204 int64_t best_rd = INT64_MAX;
153 int64_t this_rd; 205 int64_t this_rd = INT64_MAX;
154 int64_t cost[4]= { 0, 100, 150, 205 }; 206 static const int cost[4]= { 0, 2, 4, 6 };
207
208 const int64_t inter_mode_thresh = 300;
209 const int64_t intra_mode_cost = 50;
155 210
156 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH; 211 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
157 212
158 x->skip = 0; 213 x->skip = 0;
159 if (cpi->active_map_enabled && x->active_ptr[0] == 0) 214 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
160 x->skip = 1; 215 x->skip = 1;
161 216
162 // initialize mode decisions 217 // initialize mode decisions
163 *returnrate = INT_MAX; 218 *returnrate = INT_MAX;
219 *returndistortion = INT64_MAX;
164 vpx_memset(mbmi, 0, sizeof(MB_MODE_INFO)); 220 vpx_memset(mbmi, 0, sizeof(MB_MODE_INFO));
165 mbmi->sb_type = bsize; 221 mbmi->sb_type = bsize;
166 mbmi->ref_frame[0] = NONE; 222 mbmi->ref_frame[0] = NONE;
167 mbmi->ref_frame[1] = NONE; 223 mbmi->ref_frame[1] = NONE;
168 mbmi->tx_size = MIN(max_txsize_lookup[bsize], 224 mbmi->tx_size = MIN(max_txsize_lookup[bsize],
169 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]); 225 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
170 mbmi->interp_filter = cpi->common.interp_filter == SWITCHABLE ? 226 mbmi->interp_filter = cpi->common.interp_filter == SWITCHABLE ?
171 EIGHTTAP : cpi->common.interp_filter; 227 EIGHTTAP : cpi->common.interp_filter;
172 mbmi->skip = 0; 228 mbmi->skip = 0;
173 mbmi->segment_id = 0; 229 mbmi->segment_id = 0;
174 230
175 for (ref_frame = LAST_FRAME; ref_frame <= LAST_FRAME ; ++ref_frame) { 231 for (ref_frame = LAST_FRAME; ref_frame <= LAST_FRAME ; ++ref_frame) {
176 x->pred_mv_sad[ref_frame] = INT_MAX; 232 x->pred_mv_sad[ref_frame] = INT_MAX;
177 if (cpi->ref_frame_flags & flag_list[ref_frame]) { 233 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
178 vp9_setup_buffer_inter(cpi, x, tile, 234 vp9_setup_buffer_inter(cpi, x, tile,
179 ref_frame, block_size, mi_row, mi_col, 235 ref_frame, block_size, mi_row, mi_col,
180 frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb); 236 frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
181 } 237 }
182 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; 238 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
183 frame_mv[ZEROMV][ref_frame].as_int = 0; 239 frame_mv[ZEROMV][ref_frame].as_int = 0;
184 } 240 }
185 241
186 for (ref_frame = LAST_FRAME; ref_frame <= LAST_FRAME ; ++ref_frame) { 242 for (ref_frame = LAST_FRAME; ref_frame <= LAST_FRAME ; ++ref_frame) {
187 int rate_mv = 0; 243 int rate_mv = 0;
188
189 if (!(cpi->ref_frame_flags & flag_list[ref_frame])) 244 if (!(cpi->ref_frame_flags & flag_list[ref_frame]))
190 continue; 245 continue;
191 246
192 // Select prediction reference frames. 247 // Select prediction reference frames.
193 xd->plane[0].pre[0] = yv12_mb[ref_frame][0]; 248 xd->plane[0].pre[0] = yv12_mb[ref_frame][0];
194 249
195 clamp_mv2(&frame_mv[NEARESTMV][ref_frame].as_mv, xd); 250 clamp_mv2(&frame_mv[NEARESTMV][ref_frame].as_mv, xd);
196 clamp_mv2(&frame_mv[NEARMV][ref_frame].as_mv, xd); 251 clamp_mv2(&frame_mv[NEARMV][ref_frame].as_mv, xd);
197 252
253 mbmi->ref_frame[0] = ref_frame;
254
198 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) { 255 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
199 int rate = cost[INTER_OFFSET(this_mode)]; 256 int rate = cost[INTER_OFFSET(this_mode)]
257 << (num_pels_log2_lookup[bsize] - 4);
200 int64_t dist; 258 int64_t dist;
259 if (cpi->sf.disable_inter_mode_mask[bsize] &
260 (1 << INTER_OFFSET(this_mode)))
261 continue;
201 262
202 if (this_mode == NEWMV) { 263 if (this_mode == NEWMV) {
203 if (this_rd < 300) 264 if (this_rd < (int64_t)(1 << num_pels_log2_lookup[bsize]))
204 continue; 265 continue;
205 266
206 x->mode_sad[ref_frame][INTER_OFFSET(NEWMV)] = 267 x->mode_sad[ref_frame][INTER_OFFSET(NEWMV)] =
207 full_pixel_motion_search(cpi, x, tile, bsize, mi_row, mi_col, 268 full_pixel_motion_search(cpi, x, tile, bsize, mi_row, mi_col,
208 &frame_mv[NEWMV][ref_frame], &rate_mv); 269 &frame_mv[NEWMV][ref_frame], &rate_mv);
209 270
210 if (frame_mv[NEWMV][ref_frame].as_int == INVALID_MV) 271 if (frame_mv[NEWMV][ref_frame].as_int == INVALID_MV)
211 continue; 272 continue;
273
274 sub_pixel_motion_search(cpi, x, tile, bsize, mi_row, mi_col,
275 &frame_mv[NEWMV][ref_frame].as_mv);
212 } 276 }
213 277
214 dist = x->mode_sad[ref_frame][INTER_OFFSET(this_mode)]; 278 if (frame_mv[this_mode][ref_frame].as_int == 0) {
279 dist = x->mode_sad[ref_frame][INTER_OFFSET(ZEROMV)];
280 } else if (this_mode != NEARESTMV &&
281 frame_mv[NEARESTMV][ref_frame].as_int ==
282 frame_mv[this_mode][ref_frame].as_int) {
283 dist = x->mode_sad[ref_frame][INTER_OFFSET(NEARESTMV)];
284 } else {
285 mbmi->mode = this_mode;
286 mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
287 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
288 dist = x->mode_sad[ref_frame][INTER_OFFSET(this_mode)] =
289 cpi->fn_ptr[bsize].sdf(p->src.buf, p->src.stride,
290 pd->dst.buf, pd->dst.stride, INT_MAX);
291 }
292
215 this_rd = rate + dist; 293 this_rd = rate + dist;
216 294
217 if (this_rd < best_rd) { 295 if (this_rd < best_rd) {
218 best_rd = this_rd; 296 best_rd = this_rd;
219 mbmi->mode = this_mode; 297 best_mode = this_mode;
220 mbmi->ref_frame[0] = ref_frame; 298 best_ref_frame = ref_frame;
221 mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
222 xd->mi_8x8[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
223 mbmi->uv_mode = this_mode;
224 } 299 }
225 } 300 }
226 } 301 }
227 302
228 // TODO(jingning) sub-pixel motion search, if NEWMV is chosen 303 mbmi->mode = best_mode;
304 mbmi->ref_frame[0] = best_ref_frame;
305 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int;
306 xd->mi_8x8[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
229 307
230 // TODO(jingning) intra prediction search, if the best SAD is above a certain 308 // Perform intra prediction search, if the best SAD is above a certain
231 // threshold. 309 // threshold.
310 if (best_rd > inter_mode_thresh) {
311 for (this_mode = DC_PRED; this_mode <= DC_PRED; ++this_mode) {
312 vp9_predict_intra_block(xd, 0, b_width_log2(bsize),
313 mbmi->tx_size, this_mode,
314 &p->src.buf[0], p->src.stride,
315 &pd->dst.buf[0], pd->dst.stride, 0, 0, 0);
232 316
317 this_rd = cpi->fn_ptr[bsize].sdf(p->src.buf,
318 p->src.stride,
319 pd->dst.buf,
320 pd->dst.stride, INT_MAX);
321
322 if (this_rd + intra_mode_cost < best_rd) {
323 best_rd = this_rd;
324 mbmi->mode = this_mode;
325 mbmi->ref_frame[0] = INTRA_FRAME;
326 mbmi->uv_mode = this_mode;
327 mbmi->mv[0].as_int = INVALID_MV;
328 }
329 }
330 }
233 return INT64_MAX; 331 return INT64_MAX;
234 } 332 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_onyx_int.h ('k') | source/libvpx/vp9/encoder/vp9_psnr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698