OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <math.h> | 11 #include <math.h> |
12 #include <stdio.h> | 12 #include <stdio.h> |
13 #include <limits.h> | 13 #include <limits.h> |
14 | 14 |
15 #include "./vpx_config.h" | 15 #include "./vpx_config.h" |
16 #include "./vpx_scale_rtcd.h" | 16 #include "./vpx_scale_rtcd.h" |
17 | 17 |
18 #include "vp9/common/vp9_alloccommon.h" | 18 #include "vp9/common/vp9_alloccommon.h" |
19 #include "vp9/common/vp9_filter.h" | 19 #include "vp9/common/vp9_filter.h" |
20 #include "vp9/common/vp9_idct.h" | 20 #include "vp9/common/vp9_idct.h" |
21 #if CONFIG_VP9_POSTPROC | 21 #if CONFIG_VP9_POSTPROC |
22 #include "vp9/common/vp9_postproc.h" | 22 #include "vp9/common/vp9_postproc.h" |
23 #endif | 23 #endif |
24 #include "vp9/common/vp9_reconinter.h" | 24 #include "vp9/common/vp9_reconinter.h" |
25 #include "vp9/common/vp9_systemdependent.h" | 25 #include "vp9/common/vp9_systemdependent.h" |
26 #include "vp9/common/vp9_tile_common.h" | 26 #include "vp9/common/vp9_tile_common.h" |
| 27 |
| 28 #include "vp9/encoder/vp9_encodemv.h" |
27 #include "vp9/encoder/vp9_firstpass.h" | 29 #include "vp9/encoder/vp9_firstpass.h" |
28 #include "vp9/encoder/vp9_mbgraph.h" | 30 #include "vp9/encoder/vp9_mbgraph.h" |
29 #include "vp9/encoder/vp9_onyx_int.h" | 31 #include "vp9/encoder/vp9_onyx_int.h" |
30 #include "vp9/encoder/vp9_picklpf.h" | 32 #include "vp9/encoder/vp9_picklpf.h" |
31 #include "vp9/encoder/vp9_psnr.h" | 33 #include "vp9/encoder/vp9_psnr.h" |
32 #include "vp9/encoder/vp9_ratectrl.h" | 34 #include "vp9/encoder/vp9_ratectrl.h" |
33 #include "vp9/encoder/vp9_rdopt.h" | 35 #include "vp9/encoder/vp9_rdopt.h" |
34 #include "vp9/encoder/vp9_segmentation.h" | 36 #include "vp9/encoder/vp9_segmentation.h" |
35 #include "vp9/encoder/vp9_temporal_filter.h" | 37 #include "vp9/encoder/vp9_temporal_filter.h" |
36 #include "vp9/encoder/vp9_vaq.h" | 38 #include "vp9/encoder/vp9_vaq.h" |
37 | 39 |
38 #include "vpx_ports/vpx_timer.h" | 40 #include "vpx_ports/vpx_timer.h" |
39 | 41 |
40 | 42 void vp9_entropy_mode_init(); |
41 extern void print_tree_update_probs(); | 43 void vp9_coef_tree_initialize(); |
42 | 44 |
43 static void set_default_lf_deltas(struct loopfilter *lf); | 45 static void set_default_lf_deltas(struct loopfilter *lf); |
44 | 46 |
45 #define DEFAULT_INTERP_FILTER SWITCHABLE | 47 #define DEFAULT_INTERP_FILTER SWITCHABLE |
46 | 48 |
47 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */ | 49 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */ |
48 | 50 |
49 #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv | 51 #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv |
50 // for altref computation. | 52 // for altref computation. |
51 #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision | 53 #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 107 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
106 0, 0, 0}; | 108 0, 0, 0}; |
107 #endif | 109 #endif |
108 | 110 |
109 #if defined(SECTIONBITS_OUTPUT) | 111 #if defined(SECTIONBITS_OUTPUT) |
110 extern unsigned __int64 Sectionbits[500]; | 112 extern unsigned __int64 Sectionbits[500]; |
111 #endif | 113 #endif |
112 | 114 |
113 extern void vp9_init_quantizer(VP9_COMP *cpi); | 115 extern void vp9_init_quantizer(VP9_COMP *cpi); |
114 | 116 |
115 // Tables relating active max Q to active min Q | 117 static const double in_frame_q_adj_ratio[MAX_SEGMENTS] = |
116 static int kf_low_motion_minq[QINDEX_RANGE]; | 118 {1.0, 1.5, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0}; |
117 static int kf_high_motion_minq[QINDEX_RANGE]; | |
118 static int gf_low_motion_minq[QINDEX_RANGE]; | |
119 static int gf_high_motion_minq[QINDEX_RANGE]; | |
120 static int inter_minq[QINDEX_RANGE]; | |
121 static int afq_low_motion_minq[QINDEX_RANGE]; | |
122 static int afq_high_motion_minq[QINDEX_RANGE]; | |
123 | 119 |
124 static INLINE void Scale2Ratio(int mode, int *hr, int *hs) { | 120 static INLINE void Scale2Ratio(int mode, int *hr, int *hs) { |
125 switch (mode) { | 121 switch (mode) { |
126 case NORMAL: | 122 case NORMAL: |
127 *hr = 1; | 123 *hr = 1; |
128 *hs = 1; | 124 *hs = 1; |
129 break; | 125 break; |
130 case FOURFIVE: | 126 case FOURFIVE: |
131 *hr = 4; | 127 *hr = 4; |
132 *hs = 5; | 128 *hs = 5; |
133 break; | 129 break; |
134 case THREEFIVE: | 130 case THREEFIVE: |
135 *hr = 3; | 131 *hr = 3; |
136 *hs = 5; | 132 *hs = 5; |
137 break; | 133 break; |
138 case ONETWO: | 134 case ONETWO: |
139 *hr = 1; | 135 *hr = 1; |
140 *hs = 2; | 136 *hs = 2; |
141 break; | 137 break; |
142 default: | 138 default: |
143 *hr = 1; | 139 *hr = 1; |
144 *hs = 1; | 140 *hs = 1; |
145 assert(0); | 141 assert(0); |
146 break; | 142 break; |
147 } | 143 } |
148 } | 144 } |
149 | 145 |
150 // Functions to compute the active minq lookup table entries based on a | 146 static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) { |
151 // formulaic approach to facilitate easier adjustment of the Q tables. | |
152 // The formulae were derived from computing a 3rd order polynomial best | |
153 // fit to the original data (after plotting real maxq vs minq (not q index)) | |
154 static int calculate_minq_index(double maxq, | |
155 double x3, double x2, double x1, double c) { | |
156 int i; | |
157 const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c, | |
158 maxq); | |
159 | |
160 // Special case handling to deal with the step from q2.0 | |
161 // down to lossless mode represented by q 1.0. | |
162 if (minqtarget <= 2.0) | |
163 return 0; | |
164 | |
165 for (i = 0; i < QINDEX_RANGE; i++) { | |
166 if (minqtarget <= vp9_convert_qindex_to_q(i)) | |
167 return i; | |
168 } | |
169 | |
170 return QINDEX_RANGE - 1; | |
171 } | |
172 | |
173 static void init_minq_luts(void) { | |
174 int i; | |
175 | |
176 for (i = 0; i < QINDEX_RANGE; i++) { | |
177 const double maxq = vp9_convert_qindex_to_q(i); | |
178 | |
179 | |
180 kf_low_motion_minq[i] = calculate_minq_index(maxq, | |
181 0.000001, | |
182 -0.0004, | |
183 0.15, | |
184 0.0); | |
185 kf_high_motion_minq[i] = calculate_minq_index(maxq, | |
186 0.000002, | |
187 -0.0012, | |
188 0.5, | |
189 0.0); | |
190 | |
191 gf_low_motion_minq[i] = calculate_minq_index(maxq, | |
192 0.0000015, | |
193 -0.0009, | |
194 0.32, | |
195 0.0); | |
196 gf_high_motion_minq[i] = calculate_minq_index(maxq, | |
197 0.0000021, | |
198 -0.00125, | |
199 0.50, | |
200 0.0); | |
201 inter_minq[i] = calculate_minq_index(maxq, | |
202 0.00000271, | |
203 -0.00113, | |
204 0.75, | |
205 0.0); | |
206 afq_low_motion_minq[i] = calculate_minq_index(maxq, | |
207 0.0000015, | |
208 -0.0009, | |
209 0.33, | |
210 0.0); | |
211 afq_high_motion_minq[i] = calculate_minq_index(maxq, | |
212 0.0000021, | |
213 -0.00125, | |
214 0.55, | |
215 0.0); | |
216 } | |
217 } | |
218 | |
219 static int get_active_quality(int q, | |
220 int gfu_boost, | |
221 int low, | |
222 int high, | |
223 int *low_motion_minq, | |
224 int *high_motion_minq) { | |
225 int active_best_quality; | |
226 if (gfu_boost > high) { | |
227 active_best_quality = low_motion_minq[q]; | |
228 } else if (gfu_boost < low) { | |
229 active_best_quality = high_motion_minq[q]; | |
230 } else { | |
231 const int gap = high - low; | |
232 const int offset = high - gfu_boost; | |
233 const int qdiff = high_motion_minq[q] - low_motion_minq[q]; | |
234 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; | |
235 active_best_quality = low_motion_minq[q] + adjustment; | |
236 } | |
237 return active_best_quality; | |
238 } | |
239 | |
240 static void set_mvcost(VP9_COMP *cpi) { | |
241 MACROBLOCK *const mb = &cpi->mb; | 147 MACROBLOCK *const mb = &cpi->mb; |
| 148 cpi->common.allow_high_precision_mv = allow_high_precision_mv; |
242 if (cpi->common.allow_high_precision_mv) { | 149 if (cpi->common.allow_high_precision_mv) { |
243 mb->mvcost = mb->nmvcost_hp; | 150 mb->mvcost = mb->nmvcost_hp; |
244 mb->mvsadcost = mb->nmvsadcost_hp; | 151 mb->mvsadcost = mb->nmvsadcost_hp; |
245 } else { | 152 } else { |
246 mb->mvcost = mb->nmvcost; | 153 mb->mvcost = mb->nmvcost; |
247 mb->mvsadcost = mb->nmvsadcost; | 154 mb->mvsadcost = mb->nmvsadcost; |
248 } | 155 } |
249 } | 156 } |
250 | 157 |
251 void vp9_initialize_enc() { | 158 void vp9_initialize_enc() { |
252 static int init_done = 0; | 159 static int init_done = 0; |
253 | 160 |
254 if (!init_done) { | 161 if (!init_done) { |
255 vp9_initialize_common(); | 162 vp9_initialize_common(); |
| 163 vp9_coef_tree_initialize(); |
256 vp9_tokenize_initialize(); | 164 vp9_tokenize_initialize(); |
257 vp9_init_quant_tables(); | 165 vp9_init_quant_tables(); |
258 vp9_init_me_luts(); | 166 vp9_init_me_luts(); |
259 init_minq_luts(); | 167 vp9_rc_init_minq_luts(); |
260 // init_base_skip_probs(); | 168 // init_base_skip_probs(); |
| 169 vp9_entropy_mv_init(); |
| 170 vp9_entropy_mode_init(); |
261 init_done = 1; | 171 init_done = 1; |
262 } | 172 } |
263 } | 173 } |
264 | 174 |
265 static void setup_features(VP9_COMMON *cm) { | 175 static void setup_features(VP9_COMMON *cm) { |
266 struct loopfilter *const lf = &cm->lf; | 176 struct loopfilter *const lf = &cm->lf; |
267 struct segmentation *const seg = &cm->seg; | 177 struct segmentation *const seg = &cm->seg; |
268 | 178 |
269 // Set up default state for MB feature flags | 179 // Set up default state for MB feature flags |
270 seg->enabled = 0; | 180 seg->enabled = 0; |
(...skipping 16 matching lines...) Expand all Loading... |
287 | 197 |
288 static void dealloc_compressor_data(VP9_COMP *cpi) { | 198 static void dealloc_compressor_data(VP9_COMP *cpi) { |
289 // Delete sementation map | 199 // Delete sementation map |
290 vpx_free(cpi->segmentation_map); | 200 vpx_free(cpi->segmentation_map); |
291 cpi->segmentation_map = 0; | 201 cpi->segmentation_map = 0; |
292 vpx_free(cpi->common.last_frame_seg_map); | 202 vpx_free(cpi->common.last_frame_seg_map); |
293 cpi->common.last_frame_seg_map = 0; | 203 cpi->common.last_frame_seg_map = 0; |
294 vpx_free(cpi->coding_context.last_frame_seg_map_copy); | 204 vpx_free(cpi->coding_context.last_frame_seg_map_copy); |
295 cpi->coding_context.last_frame_seg_map_copy = 0; | 205 cpi->coding_context.last_frame_seg_map_copy = 0; |
296 | 206 |
| 207 vpx_free(cpi->complexity_map); |
| 208 cpi->complexity_map = 0; |
297 vpx_free(cpi->active_map); | 209 vpx_free(cpi->active_map); |
298 cpi->active_map = 0; | 210 cpi->active_map = 0; |
299 | 211 |
300 vp9_free_frame_buffers(&cpi->common); | 212 vp9_free_frame_buffers(&cpi->common); |
301 | 213 |
302 vp9_free_frame_buffer(&cpi->last_frame_uf); | 214 vp9_free_frame_buffer(&cpi->last_frame_uf); |
303 vp9_free_frame_buffer(&cpi->scaled_source); | 215 vp9_free_frame_buffer(&cpi->scaled_source); |
304 vp9_free_frame_buffer(&cpi->alt_ref_buffer); | 216 vp9_free_frame_buffer(&cpi->alt_ref_buffer); |
305 vp9_lookahead_destroy(cpi->lookahead); | 217 vp9_lookahead_destroy(cpi->lookahead); |
306 | 218 |
307 vpx_free(cpi->tok); | 219 vpx_free(cpi->tok); |
308 cpi->tok = 0; | 220 cpi->tok = 0; |
309 | 221 |
310 // Activity mask based per mb zbin adjustments | 222 // Activity mask based per mb zbin adjustments |
311 vpx_free(cpi->mb_activity_map); | 223 vpx_free(cpi->mb_activity_map); |
312 cpi->mb_activity_map = 0; | 224 cpi->mb_activity_map = 0; |
313 vpx_free(cpi->mb_norm_activity_map); | 225 vpx_free(cpi->mb_norm_activity_map); |
314 cpi->mb_norm_activity_map = 0; | 226 cpi->mb_norm_activity_map = 0; |
315 | 227 |
316 vpx_free(cpi->above_context[0]); | 228 vpx_free(cpi->above_context[0]); |
317 cpi->above_context[0] = NULL; | 229 cpi->above_context[0] = NULL; |
318 | 230 |
319 vpx_free(cpi->above_seg_context); | 231 vpx_free(cpi->above_seg_context); |
320 cpi->above_seg_context = NULL; | 232 cpi->above_seg_context = NULL; |
321 } | 233 } |
322 | 234 |
323 // Computes a q delta (in "q index" terms) to get from a starting q value | 235 // Computes a q delta (in "q index" terms) to get from a starting q value |
324 // to a target value | 236 // to a target value |
325 // target q value | 237 // target q value |
326 int vp9_compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) { | 238 int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget) { |
327 int i; | 239 int i; |
328 int start_index = cpi->worst_quality; | 240 int start_index = cpi->rc.worst_quality; |
329 int target_index = cpi->worst_quality; | 241 int target_index = cpi->rc.worst_quality; |
330 | 242 |
331 // Convert the average q value to an index. | 243 // Convert the average q value to an index. |
332 for (i = cpi->best_quality; i < cpi->worst_quality; i++) { | 244 for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) { |
333 start_index = i; | 245 start_index = i; |
334 if (vp9_convert_qindex_to_q(i) >= qstart) | 246 if (vp9_convert_qindex_to_q(i) >= qstart) |
335 break; | 247 break; |
336 } | 248 } |
337 | 249 |
338 // Convert the q target to an index | 250 // Convert the q target to an index |
339 for (i = cpi->best_quality; i < cpi->worst_quality; i++) { | 251 for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) { |
340 target_index = i; | 252 target_index = i; |
341 if (vp9_convert_qindex_to_q(i) >= qtarget) | 253 if (vp9_convert_qindex_to_q(i) >= qtarget) |
342 break; | 254 break; |
343 } | 255 } |
344 | 256 |
345 return target_index - start_index; | 257 return target_index - start_index; |
346 } | 258 } |
347 | 259 |
| 260 // Computes a q delta (in "q index" terms) to get from a starting q value |
| 261 // to a value that should equate to thegiven rate ratio. |
| 262 |
| 263 int vp9_compute_qdelta_by_rate(VP9_COMP *cpi, |
| 264 double base_q_index, double rate_target_ratio) { |
| 265 int i; |
| 266 int base_bits_per_mb; |
| 267 int target_bits_per_mb; |
| 268 int target_index = cpi->rc.worst_quality; |
| 269 |
| 270 // Make SURE use of floating point in this function is safe. |
| 271 vp9_clear_system_state(); |
| 272 |
| 273 // Look up the current projected bits per block for the base index |
| 274 base_bits_per_mb = vp9_rc_bits_per_mb(cpi->common.frame_type, |
| 275 base_q_index, 1.0); |
| 276 |
| 277 // Find the target bits per mb based on the base value and given ratio. |
| 278 target_bits_per_mb = rate_target_ratio * base_bits_per_mb; |
| 279 |
| 280 // Convert the q target to an index |
| 281 for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) { |
| 282 target_index = i; |
| 283 if (vp9_rc_bits_per_mb(cpi->common.frame_type, |
| 284 i, 1.0) <= target_bits_per_mb ) |
| 285 break; |
| 286 } |
| 287 |
| 288 return target_index - base_q_index; |
| 289 } |
| 290 |
| 291 // This function sets up a set of segments with delta Q values around |
| 292 // the baseline frame quantizer. |
| 293 static void setup_in_frame_q_adj(VP9_COMP *cpi) { |
| 294 VP9_COMMON *cm = &cpi->common; |
| 295 struct segmentation *seg = &cm->seg; |
| 296 // double q_ratio; |
| 297 int segment; |
| 298 int qindex_delta; |
| 299 |
| 300 // Make SURE use of floating point in this function is safe. |
| 301 vp9_clear_system_state(); |
| 302 |
| 303 if (cm->frame_type == KEY_FRAME || |
| 304 cpi->refresh_alt_ref_frame || |
| 305 (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)) { |
| 306 // Clear down the segment map |
| 307 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); |
| 308 |
| 309 // Clear down the complexity map used for rd |
| 310 vpx_memset(cpi->complexity_map, 0, cm->mi_rows * cm->mi_cols); |
| 311 |
| 312 // Enable segmentation |
| 313 vp9_enable_segmentation((VP9_PTR)cpi); |
| 314 vp9_clearall_segfeatures(seg); |
| 315 |
| 316 // Select delta coding method |
| 317 seg->abs_delta = SEGMENT_DELTADATA; |
| 318 |
| 319 // Segment 0 "Q" feature is disabled so it defaults to the baseline Q |
| 320 vp9_disable_segfeature(seg, 0, SEG_LVL_ALT_Q); |
| 321 |
| 322 // Use some of the segments for in frame Q adjustment |
| 323 for (segment = 1; segment < 3; segment++) { |
| 324 qindex_delta = |
| 325 vp9_compute_qdelta_by_rate(cpi, cm->base_qindex, |
| 326 in_frame_q_adj_ratio[segment]); |
| 327 vp9_enable_segfeature(seg, segment, SEG_LVL_ALT_Q); |
| 328 vp9_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta); |
| 329 } |
| 330 } |
| 331 } |
| 332 |
348 static void configure_static_seg_features(VP9_COMP *cpi) { | 333 static void configure_static_seg_features(VP9_COMP *cpi) { |
349 VP9_COMMON *cm = &cpi->common; | 334 VP9_COMMON *cm = &cpi->common; |
350 struct segmentation *seg = &cm->seg; | 335 struct segmentation *seg = &cm->seg; |
351 | 336 |
352 int high_q = (int)(cpi->avg_q > 48.0); | 337 int high_q = (int)(cpi->rc.avg_q > 48.0); |
353 int qi_delta; | 338 int qi_delta; |
354 | 339 |
355 // Disable and clear down for KF | 340 // Disable and clear down for KF |
356 if (cm->frame_type == KEY_FRAME) { | 341 if (cm->frame_type == KEY_FRAME) { |
357 // Clear down the global segmentation map | 342 // Clear down the global segmentation map |
358 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); | 343 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); |
359 seg->update_map = 0; | 344 seg->update_map = 0; |
360 seg->update_data = 0; | 345 seg->update_data = 0; |
361 cpi->static_mb_pct = 0; | 346 cpi->static_mb_pct = 0; |
362 | 347 |
(...skipping 17 matching lines...) Expand all Loading... |
380 // Scan frames from current to arf frame. | 365 // Scan frames from current to arf frame. |
381 // This function re-enables segmentation if appropriate. | 366 // This function re-enables segmentation if appropriate. |
382 vp9_update_mbgraph_stats(cpi); | 367 vp9_update_mbgraph_stats(cpi); |
383 | 368 |
384 // If segmentation was enabled set those features needed for the | 369 // If segmentation was enabled set those features needed for the |
385 // arf itself. | 370 // arf itself. |
386 if (seg->enabled) { | 371 if (seg->enabled) { |
387 seg->update_map = 1; | 372 seg->update_map = 1; |
388 seg->update_data = 1; | 373 seg->update_data = 1; |
389 | 374 |
390 qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875)); | 375 qi_delta = vp9_compute_qdelta( |
| 376 cpi, cpi->rc.avg_q, (cpi->rc.avg_q * 0.875)); |
391 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2)); | 377 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2)); |
392 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); | 378 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); |
393 | 379 |
394 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); | 380 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); |
395 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); | 381 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); |
396 | 382 |
397 // Where relevant assume segment data is delta data | 383 // Where relevant assume segment data is delta data |
398 seg->abs_delta = SEGMENT_DELTADATA; | 384 seg->abs_delta = SEGMENT_DELTADATA; |
399 } | 385 } |
400 } else if (seg->enabled) { | 386 } else if (seg->enabled) { |
401 // All other frames if segmentation has been enabled | 387 // All other frames if segmentation has been enabled |
402 | 388 |
403 // First normal frame in a valid gf or alt ref group | 389 // First normal frame in a valid gf or alt ref group |
404 if (cpi->frames_since_golden == 0) { | 390 if (cpi->rc.frames_since_golden == 0) { |
405 // Set up segment features for normal frames in an arf group | 391 // Set up segment features for normal frames in an arf group |
406 if (cpi->source_alt_ref_active) { | 392 if (cpi->source_alt_ref_active) { |
407 seg->update_map = 0; | 393 seg->update_map = 0; |
408 seg->update_data = 1; | 394 seg->update_data = 1; |
409 seg->abs_delta = SEGMENT_DELTADATA; | 395 seg->abs_delta = SEGMENT_DELTADATA; |
410 | 396 |
411 qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q, | 397 qi_delta = vp9_compute_qdelta(cpi, cpi->rc.avg_q, |
412 (cpi->avg_q * 1.125)); | 398 (cpi->rc.avg_q * 1.125)); |
413 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2)); | 399 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2)); |
414 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); | 400 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); |
415 | 401 |
416 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); | 402 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); |
417 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); | 403 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); |
418 | 404 |
419 // Segment coding disabled for compred testing | 405 // Segment coding disabled for compred testing |
420 if (high_q || (cpi->static_mb_pct == 100)) { | 406 if (high_q || (cpi->static_mb_pct == 100)) { |
421 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); | 407 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); |
422 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); | 408 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 } else { | 446 } else { |
461 // All other frames. | 447 // All other frames. |
462 | 448 |
463 // No updates.. leave things as they are. | 449 // No updates.. leave things as they are. |
464 seg->update_map = 0; | 450 seg->update_map = 0; |
465 seg->update_data = 0; | 451 seg->update_data = 0; |
466 } | 452 } |
467 } | 453 } |
468 } | 454 } |
469 | 455 |
470 #ifdef ENTROPY_STATS | |
471 void vp9_update_mode_context_stats(VP9_COMP *cpi) { | |
472 VP9_COMMON *cm = &cpi->common; | |
473 int i, j; | |
474 unsigned int (*inter_mode_counts)[INTER_MODES - 1][2] = | |
475 cm->fc.inter_mode_counts; | |
476 int64_t (*mv_ref_stats)[INTER_MODES - 1][2] = cpi->mv_ref_stats; | |
477 FILE *f; | |
478 | |
479 // Read the past stats counters | |
480 f = fopen("mode_context.bin", "rb"); | |
481 if (!f) { | |
482 vpx_memset(cpi->mv_ref_stats, 0, sizeof(cpi->mv_ref_stats)); | |
483 } else { | |
484 fread(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f); | |
485 fclose(f); | |
486 } | |
487 | |
488 // Add in the values for this frame | |
489 for (i = 0; i < INTER_MODE_CONTEXTS; i++) { | |
490 for (j = 0; j < INTER_MODES - 1; j++) { | |
491 mv_ref_stats[i][j][0] += (int64_t)inter_mode_counts[i][j][0]; | |
492 mv_ref_stats[i][j][1] += (int64_t)inter_mode_counts[i][j][1]; | |
493 } | |
494 } | |
495 | |
496 // Write back the accumulated stats | |
497 f = fopen("mode_context.bin", "wb"); | |
498 fwrite(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f); | |
499 fclose(f); | |
500 } | |
501 | |
502 void print_mode_context(VP9_COMP *cpi) { | |
503 FILE *f = fopen("vp9_modecont.c", "a"); | |
504 int i, j; | |
505 | |
506 fprintf(f, "#include \"vp9_entropy.h\"\n"); | |
507 fprintf( | |
508 f, | |
509 "const int inter_mode_probs[INTER_MODE_CONTEXTS][INTER_MODES - 1] ="); | |
510 fprintf(f, "{\n"); | |
511 for (j = 0; j < INTER_MODE_CONTEXTS; j++) { | |
512 fprintf(f, " {/* %d */ ", j); | |
513 fprintf(f, " "); | |
514 for (i = 0; i < INTER_MODES - 1; i++) { | |
515 int this_prob; | |
516 int64_t count = cpi->mv_ref_stats[j][i][0] + cpi->mv_ref_stats[j][i][1]; | |
517 if (count) | |
518 this_prob = ((cpi->mv_ref_stats[j][i][0] * 256) + (count >> 1)) / count; | |
519 else | |
520 this_prob = 128; | |
521 | |
522 // context probs | |
523 fprintf(f, "%5d, ", this_prob); | |
524 } | |
525 fprintf(f, " },\n"); | |
526 } | |
527 | |
528 fprintf(f, "};\n"); | |
529 fclose(f); | |
530 } | |
531 #endif // ENTROPY_STATS | |
532 | |
533 // DEBUG: Print out the segment id of each MB in the current frame. | 456 // DEBUG: Print out the segment id of each MB in the current frame. |
534 static void print_seg_map(VP9_COMP *cpi) { | 457 static void print_seg_map(VP9_COMP *cpi) { |
535 VP9_COMMON *cm = &cpi->common; | 458 VP9_COMMON *cm = &cpi->common; |
536 int row, col; | 459 int row, col; |
537 int map_index = 0; | 460 int map_index = 0; |
538 FILE *statsfile = fopen("segmap.stt", "a"); | 461 FILE *statsfile = fopen("segmap.stt", "a"); |
539 | 462 |
540 fprintf(statsfile, "%10d\n", cm->current_video_frame); | 463 fprintf(statsfile, "%10d\n", cm->current_video_frame); |
541 | 464 |
542 for (row = 0; row < cpi->common.mi_rows; row++) { | 465 for (row = 0; row < cpi->common.mi_rows; row++) { |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
724 sf->optimize_coefficients = !cpi->oxcf.lossless; | 647 sf->optimize_coefficients = !cpi->oxcf.lossless; |
725 sf->reduce_first_step_size = 0; | 648 sf->reduce_first_step_size = 0; |
726 sf->auto_mv_step_size = 0; | 649 sf->auto_mv_step_size = 0; |
727 sf->max_step_search_steps = MAX_MVSEARCH_STEPS; | 650 sf->max_step_search_steps = MAX_MVSEARCH_STEPS; |
728 sf->comp_inter_joint_search_thresh = BLOCK_4X4; | 651 sf->comp_inter_joint_search_thresh = BLOCK_4X4; |
729 sf->adaptive_rd_thresh = 0; | 652 sf->adaptive_rd_thresh = 0; |
730 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF; | 653 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF; |
731 sf->tx_size_search_method = USE_FULL_RD; | 654 sf->tx_size_search_method = USE_FULL_RD; |
732 sf->use_lp32x32fdct = 0; | 655 sf->use_lp32x32fdct = 0; |
733 sf->adaptive_motion_search = 0; | 656 sf->adaptive_motion_search = 0; |
| 657 sf->adaptive_pred_filter_type = 0; |
734 sf->use_avoid_tested_higherror = 0; | 658 sf->use_avoid_tested_higherror = 0; |
735 sf->reference_masking = 0; | 659 sf->reference_masking = 0; |
736 sf->use_one_partition_size_always = 0; | 660 sf->use_one_partition_size_always = 0; |
737 sf->less_rectangular_check = 0; | 661 sf->less_rectangular_check = 0; |
738 sf->use_square_partition_only = 0; | 662 sf->use_square_partition_only = 0; |
739 sf->auto_min_max_partition_size = 0; | 663 sf->auto_min_max_partition_size = 0; |
740 sf->max_partition_size = BLOCK_64X64; | 664 sf->max_partition_size = BLOCK_64X64; |
741 sf->min_partition_size = BLOCK_4X4; | 665 sf->min_partition_size = BLOCK_4X4; |
742 sf->adjust_partitioning_from_last_frame = 0; | 666 sf->adjust_partitioning_from_last_frame = 0; |
743 sf->last_partitioning_redo_frequency = 4; | 667 sf->last_partitioning_redo_frequency = 4; |
(...skipping 13 matching lines...) Expand all Loading... |
757 sf->using_small_partition_info = 0; | 681 sf->using_small_partition_info = 0; |
758 sf->mode_skip_start = MAX_MODES; // Mode index at which mode skip mask set | 682 sf->mode_skip_start = MAX_MODES; // Mode index at which mode skip mask set |
759 | 683 |
760 #if CONFIG_MULTIPLE_ARF | 684 #if CONFIG_MULTIPLE_ARF |
761 // Switch segmentation off. | 685 // Switch segmentation off. |
762 sf->static_segmentation = 0; | 686 sf->static_segmentation = 0; |
763 #else | 687 #else |
764 sf->static_segmentation = 0; | 688 sf->static_segmentation = 0; |
765 #endif | 689 #endif |
766 | 690 |
767 sf->variance_adaptive_quantization = 0; | |
768 | |
769 switch (mode) { | 691 switch (mode) { |
770 case 0: // This is the best quality mode. | 692 case 0: // This is the best quality mode. |
| 693 cpi->diamond_search_sad = vp9_full_range_search; |
771 break; | 694 break; |
772 | 695 |
773 case 1: | 696 case 1: |
774 #if CONFIG_MULTIPLE_ARF | 697 #if CONFIG_MULTIPLE_ARF |
775 // Switch segmentation off. | 698 // Switch segmentation off. |
776 sf->static_segmentation = 0; | 699 sf->static_segmentation = 0; |
777 #else | 700 #else |
778 sf->static_segmentation = 0; | 701 sf->static_segmentation = 0; |
779 #endif | 702 #endif |
780 sf->use_avoid_tested_higherror = 1; | 703 sf->use_avoid_tested_higherror = 1; |
781 sf->adaptive_rd_thresh = 1; | 704 sf->adaptive_rd_thresh = 1; |
782 sf->recode_loop = (speed < 1); | 705 sf->recode_loop = (speed < 1); |
783 | 706 |
784 if (speed == 1) { | 707 if (speed == 1) { |
785 sf->use_square_partition_only = !frame_is_intra_only(&cpi->common); | 708 sf->use_square_partition_only = !frame_is_intra_only(&cpi->common); |
786 sf->less_rectangular_check = 1; | 709 sf->less_rectangular_check = 1; |
787 sf->tx_size_search_method = frame_is_intra_only(&cpi->common) | 710 sf->tx_size_search_method = frame_is_intra_only(&cpi->common) |
788 ? USE_FULL_RD : USE_LARGESTALL; | 711 ? USE_FULL_RD : USE_LARGESTALL; |
789 | 712 |
790 if (MIN(cpi->common.width, cpi->common.height) >= 720) | 713 if (MIN(cpi->common.width, cpi->common.height) >= 720) |
791 sf->disable_split_mask = cpi->common.show_frame ? | 714 sf->disable_split_mask = cpi->common.show_frame ? |
792 DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT; | 715 DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT; |
793 else | 716 else |
794 sf->disable_split_mask = DISABLE_COMPOUND_SPLIT; | 717 sf->disable_split_mask = DISABLE_COMPOUND_SPLIT; |
795 | 718 |
796 sf->use_rd_breakout = 1; | 719 sf->use_rd_breakout = 1; |
797 sf->adaptive_motion_search = 1; | 720 sf->adaptive_motion_search = 1; |
| 721 sf->adaptive_pred_filter_type = 1; |
798 sf->auto_mv_step_size = 1; | 722 sf->auto_mv_step_size = 1; |
799 sf->adaptive_rd_thresh = 2; | 723 sf->adaptive_rd_thresh = 2; |
800 sf->recode_loop = 2; | 724 sf->recode_loop = 2; |
801 sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V; | 725 sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V; |
802 sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V; | 726 sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V; |
803 sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V; | 727 sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V; |
804 } | 728 } |
805 if (speed == 2) { | 729 if (speed == 2) { |
806 sf->use_square_partition_only = !frame_is_intra_only(&cpi->common); | 730 sf->use_square_partition_only = !frame_is_intra_only(&cpi->common); |
807 sf->less_rectangular_check = 1; | 731 sf->less_rectangular_check = 1; |
808 sf->tx_size_search_method = frame_is_intra_only(&cpi->common) | 732 sf->tx_size_search_method = frame_is_intra_only(&cpi->common) |
809 ? USE_FULL_RD : USE_LARGESTALL; | 733 ? USE_FULL_RD : USE_LARGESTALL; |
810 | 734 |
811 if (MIN(cpi->common.width, cpi->common.height) >= 720) | 735 if (MIN(cpi->common.width, cpi->common.height) >= 720) |
812 sf->disable_split_mask = cpi->common.show_frame ? | 736 sf->disable_split_mask = cpi->common.show_frame ? |
813 DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT; | 737 DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT; |
814 else | 738 else |
815 sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY; | 739 sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY; |
816 | 740 |
817 | 741 |
818 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | | 742 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | |
819 FLAG_SKIP_INTRA_BESTINTER | | 743 FLAG_SKIP_INTRA_BESTINTER | |
820 FLAG_SKIP_COMP_BESTINTRA | | 744 FLAG_SKIP_COMP_BESTINTRA | |
821 FLAG_SKIP_INTRA_LOWVAR; | 745 FLAG_SKIP_INTRA_LOWVAR; |
822 | 746 |
823 sf->use_rd_breakout = 1; | 747 sf->use_rd_breakout = 1; |
824 sf->adaptive_motion_search = 1; | 748 sf->adaptive_motion_search = 1; |
| 749 sf->adaptive_pred_filter_type = 2; |
825 sf->auto_mv_step_size = 1; | 750 sf->auto_mv_step_size = 1; |
826 | 751 |
827 sf->disable_filter_search_var_thresh = 16; | 752 sf->disable_filter_search_var_thresh = 50; |
828 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; | 753 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; |
829 | 754 |
830 sf->auto_min_max_partition_size = 1; | 755 sf->auto_min_max_partition_size = 1; |
831 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION; | 756 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION; |
832 sf->adjust_partitioning_from_last_frame = 1; | 757 sf->adjust_partitioning_from_last_frame = 1; |
833 sf->last_partitioning_redo_frequency = 3; | 758 sf->last_partitioning_redo_frequency = 3; |
834 | 759 |
835 sf->adaptive_rd_thresh = 2; | 760 sf->adaptive_rd_thresh = 2; |
836 sf->recode_loop = 2; | 761 sf->recode_loop = 2; |
| 762 sf->use_lp32x32fdct = 1; |
837 sf->mode_skip_start = 11; | 763 sf->mode_skip_start = 11; |
838 sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V; | 764 sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V; |
839 sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V; | 765 sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V; |
840 sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V; | 766 sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V; |
841 sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V; | 767 sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V; |
842 } | 768 } |
843 if (speed == 3) { | 769 if (speed == 3) { |
844 sf->use_square_partition_only = 1; | 770 sf->use_square_partition_only = 1; |
845 sf->tx_size_search_method = USE_LARGESTALL; | 771 sf->tx_size_search_method = USE_LARGESTALL; |
846 | 772 |
847 if (MIN(cpi->common.width, cpi->common.height) >= 720) | 773 if (MIN(cpi->common.width, cpi->common.height) >= 720) |
848 sf->disable_split_mask = DISABLE_ALL_SPLIT; | 774 sf->disable_split_mask = DISABLE_ALL_SPLIT; |
849 else | 775 else |
850 sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT; | 776 sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT; |
851 | 777 |
852 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | | 778 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | |
853 FLAG_SKIP_INTRA_BESTINTER | | 779 FLAG_SKIP_INTRA_BESTINTER | |
854 FLAG_SKIP_COMP_BESTINTRA | | 780 FLAG_SKIP_COMP_BESTINTRA | |
855 FLAG_SKIP_INTRA_LOWVAR; | 781 FLAG_SKIP_INTRA_LOWVAR; |
856 | 782 |
857 sf->use_rd_breakout = 1; | 783 sf->use_rd_breakout = 1; |
858 sf->adaptive_motion_search = 1; | 784 sf->adaptive_motion_search = 1; |
| 785 sf->adaptive_pred_filter_type = 2; |
859 sf->auto_mv_step_size = 1; | 786 sf->auto_mv_step_size = 1; |
860 | 787 |
861 sf->disable_filter_search_var_thresh = 16; | 788 sf->disable_filter_search_var_thresh = 100; |
862 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; | 789 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; |
863 | 790 |
864 sf->auto_min_max_partition_size = 1; | 791 sf->auto_min_max_partition_size = 1; |
865 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL; | 792 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL; |
866 sf->adjust_partitioning_from_last_frame = 1; | 793 sf->adjust_partitioning_from_last_frame = 1; |
867 sf->last_partitioning_redo_frequency = 3; | 794 sf->last_partitioning_redo_frequency = 3; |
868 | 795 |
869 sf->use_uv_intra_rd_estimate = 1; | 796 sf->use_uv_intra_rd_estimate = 1; |
870 sf->skip_encode_sb = 1; | 797 sf->skip_encode_sb = 1; |
871 sf->use_lp32x32fdct = 1; | 798 sf->use_lp32x32fdct = 1; |
(...skipping 10 matching lines...) Expand all Loading... |
882 | 809 |
883 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | | 810 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | |
884 FLAG_SKIP_INTRA_BESTINTER | | 811 FLAG_SKIP_INTRA_BESTINTER | |
885 FLAG_SKIP_COMP_BESTINTRA | | 812 FLAG_SKIP_COMP_BESTINTRA | |
886 FLAG_SKIP_COMP_REFMISMATCH | | 813 FLAG_SKIP_COMP_REFMISMATCH | |
887 FLAG_SKIP_INTRA_LOWVAR | | 814 FLAG_SKIP_INTRA_LOWVAR | |
888 FLAG_EARLY_TERMINATE; | 815 FLAG_EARLY_TERMINATE; |
889 | 816 |
890 sf->use_rd_breakout = 1; | 817 sf->use_rd_breakout = 1; |
891 sf->adaptive_motion_search = 1; | 818 sf->adaptive_motion_search = 1; |
| 819 sf->adaptive_pred_filter_type = 2; |
892 sf->auto_mv_step_size = 1; | 820 sf->auto_mv_step_size = 1; |
893 | 821 |
894 sf->disable_filter_search_var_thresh = 16; | 822 sf->disable_filter_search_var_thresh = 200; |
895 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; | 823 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; |
896 | 824 |
897 sf->auto_min_max_partition_size = 1; | 825 sf->auto_min_max_partition_size = 1; |
898 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL; | 826 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL; |
899 sf->adjust_partitioning_from_last_frame = 1; | 827 sf->adjust_partitioning_from_last_frame = 1; |
900 sf->last_partitioning_redo_frequency = 3; | 828 sf->last_partitioning_redo_frequency = 3; |
901 | 829 |
902 sf->use_uv_intra_rd_estimate = 1; | 830 sf->use_uv_intra_rd_estimate = 1; |
903 sf->skip_encode_sb = 1; | 831 sf->skip_encode_sb = 1; |
904 sf->use_lp32x32fdct = 1; | 832 sf->use_lp32x32fdct = 1; |
(...skipping 25 matching lines...) Expand all Loading... |
930 sf->use_lp32x32fdct = 1; | 858 sf->use_lp32x32fdct = 1; |
931 sf->optimize_coefficients = 0; | 859 sf->optimize_coefficients = 0; |
932 sf->auto_mv_step_size = 1; | 860 sf->auto_mv_step_size = 1; |
933 // sf->reduce_first_step_size = 1; | 861 // sf->reduce_first_step_size = 1; |
934 // sf->reference_masking = 1; | 862 // sf->reference_masking = 1; |
935 | 863 |
936 sf->disable_split_mask = DISABLE_ALL_SPLIT; | 864 sf->disable_split_mask = DISABLE_ALL_SPLIT; |
937 sf->search_method = HEX; | 865 sf->search_method = HEX; |
938 sf->subpel_iters_per_step = 1; | 866 sf->subpel_iters_per_step = 1; |
939 sf->disable_split_var_thresh = 64; | 867 sf->disable_split_var_thresh = 64; |
940 sf->disable_filter_search_var_thresh = 96; | 868 sf->disable_filter_search_var_thresh = 500; |
941 for (i = 0; i < TX_SIZES; i++) { | 869 for (i = 0; i < TX_SIZES; i++) { |
942 sf->intra_y_mode_mask[i] = INTRA_DC_ONLY; | 870 sf->intra_y_mode_mask[i] = INTRA_DC_ONLY; |
943 sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY; | 871 sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY; |
944 } | 872 } |
945 sf->use_fast_coef_updates = 2; | 873 sf->use_fast_coef_updates = 2; |
946 sf->adaptive_rd_thresh = 4; | 874 sf->adaptive_rd_thresh = 4; |
947 sf->mode_skip_start = 6; | 875 sf->mode_skip_start = 6; |
948 } | 876 } |
949 break; | 877 break; |
950 }; /* switch */ | 878 }; /* switch */ |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height, | 919 cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height, |
992 cm->subsampling_x, cm->subsampling_y, | 920 cm->subsampling_x, cm->subsampling_y, |
993 cpi->oxcf.lag_in_frames); | 921 cpi->oxcf.lag_in_frames); |
994 if (!cpi->lookahead) | 922 if (!cpi->lookahead) |
995 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, | 923 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
996 "Failed to allocate lag buffers"); | 924 "Failed to allocate lag buffers"); |
997 | 925 |
998 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer, | 926 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer, |
999 cpi->oxcf.width, cpi->oxcf.height, | 927 cpi->oxcf.width, cpi->oxcf.height, |
1000 cm->subsampling_x, cm->subsampling_y, | 928 cm->subsampling_x, cm->subsampling_y, |
1001 VP9BORDERINPIXELS)) | 929 VP9BORDERINPIXELS, NULL, NULL, NULL)) |
1002 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, | 930 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
1003 "Failed to allocate altref buffer"); | 931 "Failed to allocate altref buffer"); |
1004 } | 932 } |
1005 | 933 |
1006 void vp9_alloc_compressor_data(VP9_COMP *cpi) { | 934 void vp9_alloc_compressor_data(VP9_COMP *cpi) { |
1007 VP9_COMMON *cm = &cpi->common; | 935 VP9_COMMON *cm = &cpi->common; |
1008 | 936 |
1009 if (vp9_alloc_frame_buffers(cm, cm->width, cm->height)) | 937 if (vp9_alloc_frame_buffers(cm, cm->width, cm->height)) |
1010 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, | 938 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
1011 "Failed to allocate frame buffers"); | 939 "Failed to allocate frame buffers"); |
(...skipping 13 matching lines...) Expand all Loading... |
1025 "Failed to allocate scaled source buffer"); | 953 "Failed to allocate scaled source buffer"); |
1026 | 954 |
1027 vpx_free(cpi->tok); | 955 vpx_free(cpi->tok); |
1028 | 956 |
1029 { | 957 { |
1030 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols); | 958 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols); |
1031 | 959 |
1032 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok))); | 960 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok))); |
1033 } | 961 } |
1034 | 962 |
1035 // Data used for real time vc mode to see if gf needs refreshing | |
1036 cpi->inter_zz_count = 0; | |
1037 cpi->gf_bad_count = 0; | |
1038 cpi->gf_update_recommended = 0; | |
1039 | |
1040 vpx_free(cpi->mb_activity_map); | 963 vpx_free(cpi->mb_activity_map); |
1041 CHECK_MEM_ERROR(cm, cpi->mb_activity_map, | 964 CHECK_MEM_ERROR(cm, cpi->mb_activity_map, |
1042 vpx_calloc(sizeof(unsigned int), | 965 vpx_calloc(sizeof(unsigned int), |
1043 cm->mb_rows * cm->mb_cols)); | 966 cm->mb_rows * cm->mb_cols)); |
1044 | 967 |
1045 vpx_free(cpi->mb_norm_activity_map); | 968 vpx_free(cpi->mb_norm_activity_map); |
1046 CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map, | 969 CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map, |
1047 vpx_calloc(sizeof(unsigned int), | 970 vpx_calloc(sizeof(unsigned int), |
1048 cm->mb_rows * cm->mb_cols)); | 971 cm->mb_rows * cm->mb_cols)); |
1049 | 972 |
(...skipping 14 matching lines...) Expand all Loading... |
1064 | 987 |
1065 static void update_frame_size(VP9_COMP *cpi) { | 988 static void update_frame_size(VP9_COMP *cpi) { |
1066 VP9_COMMON *cm = &cpi->common; | 989 VP9_COMMON *cm = &cpi->common; |
1067 | 990 |
1068 vp9_update_frame_size(cm); | 991 vp9_update_frame_size(cm); |
1069 | 992 |
1070 // Update size of buffers local to this frame | 993 // Update size of buffers local to this frame |
1071 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf, | 994 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf, |
1072 cm->width, cm->height, | 995 cm->width, cm->height, |
1073 cm->subsampling_x, cm->subsampling_y, | 996 cm->subsampling_x, cm->subsampling_y, |
1074 VP9BORDERINPIXELS)) | 997 VP9BORDERINPIXELS, NULL, NULL, NULL)) |
1075 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, | 998 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
1076 "Failed to reallocate last frame buffer"); | 999 "Failed to reallocate last frame buffer"); |
1077 | 1000 |
1078 if (vp9_realloc_frame_buffer(&cpi->scaled_source, | 1001 if (vp9_realloc_frame_buffer(&cpi->scaled_source, |
1079 cm->width, cm->height, | 1002 cm->width, cm->height, |
1080 cm->subsampling_x, cm->subsampling_y, | 1003 cm->subsampling_x, cm->subsampling_y, |
1081 VP9BORDERINPIXELS)) | 1004 VP9BORDERINPIXELS, NULL, NULL, NULL)) |
1082 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, | 1005 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
1083 "Failed to reallocate scaled source buffer"); | 1006 "Failed to reallocate scaled source buffer"); |
1084 | 1007 |
1085 { | 1008 { |
1086 int y_stride = cpi->scaled_source.y_stride; | 1009 int y_stride = cpi->scaled_source.y_stride; |
1087 | 1010 |
1088 if (cpi->sf.search_method == NSTEP) { | 1011 if (cpi->sf.search_method == NSTEP) { |
1089 vp9_init3smotion_compensation(&cpi->mb, y_stride); | 1012 vp9_init3smotion_compensation(&cpi->mb, y_stride); |
1090 } else if (cpi->sf.search_method == DIAMOND) { | 1013 } else if (cpi->sf.search_method == DIAMOND) { |
1091 vp9_init_dsmotion_compensation(&cpi->mb, y_stride); | 1014 vp9_init_dsmotion_compensation(&cpi->mb, y_stride); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1124 return i; | 1047 return i; |
1125 | 1048 |
1126 return 63; | 1049 return 63; |
1127 }; | 1050 }; |
1128 void vp9_new_framerate(VP9_COMP *cpi, double framerate) { | 1051 void vp9_new_framerate(VP9_COMP *cpi, double framerate) { |
1129 if (framerate < 0.1) | 1052 if (framerate < 0.1) |
1130 framerate = 30; | 1053 framerate = 30; |
1131 | 1054 |
1132 cpi->oxcf.framerate = framerate; | 1055 cpi->oxcf.framerate = framerate; |
1133 cpi->output_framerate = cpi->oxcf.framerate; | 1056 cpi->output_framerate = cpi->oxcf.framerate; |
1134 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth | 1057 cpi->rc.per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth |
1135 / cpi->output_framerate); | 1058 / cpi->output_framerate); |
1136 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth | 1059 cpi->rc.av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth |
1137 / cpi->output_framerate); | 1060 / cpi->output_framerate); |
1138 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * | 1061 cpi->rc.min_frame_bandwidth = (int)(cpi->rc.av_per_frame_bandwidth * |
1139 cpi->oxcf.two_pass_vbrmin_section / 100); | 1062 cpi->oxcf.two_pass_vbrmin_section / 100); |
1140 | 1063 |
1141 | 1064 |
1142 cpi->min_frame_bandwidth = MAX(cpi->min_frame_bandwidth, FRAME_OVERHEAD_BITS); | 1065 cpi->rc.min_frame_bandwidth = MAX(cpi->rc.min_frame_bandwidth, |
| 1066 FRAME_OVERHEAD_BITS); |
1143 | 1067 |
1144 // Set Maximum gf/arf interval | 1068 // Set Maximum gf/arf interval |
1145 cpi->max_gf_interval = 16; | 1069 cpi->rc.max_gf_interval = 16; |
1146 | 1070 |
1147 // Extended interval for genuinely static scenes | 1071 // Extended interval for genuinely static scenes |
1148 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1; | 1072 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1; |
1149 | 1073 |
1150 // Special conditions when alt ref frame enabled in lagged compress mode | 1074 // Special conditions when alt ref frame enabled in lagged compress mode |
1151 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) { | 1075 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) { |
1152 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1) | 1076 if (cpi->rc.max_gf_interval > cpi->oxcf.lag_in_frames - 1) |
1153 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1; | 1077 cpi->rc.max_gf_interval = cpi->oxcf.lag_in_frames - 1; |
1154 | 1078 |
1155 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1) | 1079 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1) |
1156 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1; | 1080 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1; |
1157 } | 1081 } |
1158 | 1082 |
1159 if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval) | 1083 if (cpi->rc.max_gf_interval > cpi->twopass.static_scene_max_gf_interval) |
1160 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval; | 1084 cpi->rc.max_gf_interval = cpi->twopass.static_scene_max_gf_interval; |
1161 } | 1085 } |
1162 | 1086 |
1163 static int64_t rescale(int val, int64_t num, int denom) { | 1087 static int64_t rescale(int val, int64_t num, int denom) { |
1164 int64_t llnum = num; | 1088 int64_t llnum = num; |
1165 int64_t llden = denom; | 1089 int64_t llden = denom; |
1166 int64_t llval = val; | 1090 int64_t llval = val; |
1167 | 1091 |
1168 return (llval * llnum / llden); | 1092 return (llval * llnum / llden); |
1169 } | 1093 } |
1170 | 1094 |
1171 static void set_tile_limits(VP9_COMP *cpi) { | 1095 static void set_tile_limits(VP9_COMP *cpi) { |
1172 VP9_COMMON *const cm = &cpi->common; | 1096 VP9_COMMON *const cm = &cpi->common; |
1173 | 1097 |
1174 int min_log2_tile_cols, max_log2_tile_cols; | 1098 int min_log2_tile_cols, max_log2_tile_cols; |
1175 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); | 1099 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); |
1176 | 1100 |
1177 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns, | 1101 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns, |
1178 min_log2_tile_cols, max_log2_tile_cols); | 1102 min_log2_tile_cols, max_log2_tile_cols); |
1179 cm->log2_tile_rows = cpi->oxcf.tile_rows; | 1103 cm->log2_tile_rows = cpi->oxcf.tile_rows; |
1180 } | 1104 } |
1181 | 1105 |
1182 static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { | 1106 static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { |
1183 VP9_COMP *cpi = (VP9_COMP *)(ptr); | 1107 VP9_COMP *cpi = (VP9_COMP *)(ptr); |
1184 VP9_COMMON *const cm = &cpi->common; | 1108 VP9_COMMON *const cm = &cpi->common; |
1185 int i; | 1109 int i; |
1186 | 1110 |
1187 cpi->oxcf = *oxcf; | 1111 cpi->oxcf = *oxcf; |
1188 cpi->goldfreq = 7; | |
1189 | 1112 |
1190 cm->version = oxcf->version; | 1113 cm->version = oxcf->version; |
1191 | 1114 |
1192 cm->width = oxcf->width; | 1115 cm->width = oxcf->width; |
1193 cm->height = oxcf->height; | 1116 cm->height = oxcf->height; |
1194 cm->subsampling_x = 0; | 1117 cm->subsampling_x = 0; |
1195 cm->subsampling_y = 0; | 1118 cm->subsampling_y = 0; |
1196 vp9_alloc_compressor_data(cpi); | 1119 vp9_alloc_compressor_data(cpi); |
1197 | 1120 |
1198 // change includes all joint functionality | 1121 // change includes all joint functionality |
1199 vp9_change_config(ptr, oxcf); | 1122 vp9_change_config(ptr, oxcf); |
1200 | 1123 |
1201 // Initialize active best and worst q and average q values. | 1124 // Initialize active best and worst q and average q values. |
1202 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q; | 1125 cpi->rc.active_worst_quality = cpi->oxcf.worst_allowed_q; |
1203 cpi->active_best_quality = cpi->oxcf.best_allowed_q; | 1126 |
1204 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q; | 1127 cpi->rc.avg_frame_qindex = cpi->oxcf.worst_allowed_q; |
1205 | 1128 |
1206 // Initialise the starting buffer levels | 1129 // Initialise the starting buffer levels |
1207 cpi->buffer_level = cpi->oxcf.starting_buffer_level; | 1130 cpi->rc.buffer_level = cpi->oxcf.starting_buffer_level; |
1208 cpi->bits_off_target = cpi->oxcf.starting_buffer_level; | 1131 cpi->rc.bits_off_target = cpi->oxcf.starting_buffer_level; |
1209 | 1132 |
1210 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth; | 1133 cpi->rc.rolling_target_bits = cpi->rc.av_per_frame_bandwidth; |
1211 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth; | 1134 cpi->rc.rolling_actual_bits = cpi->rc.av_per_frame_bandwidth; |
1212 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth; | 1135 cpi->rc.long_rolling_target_bits = cpi->rc.av_per_frame_bandwidth; |
1213 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth; | 1136 cpi->rc.long_rolling_actual_bits = cpi->rc.av_per_frame_bandwidth; |
1214 | 1137 |
1215 cpi->total_actual_bits = 0; | 1138 cpi->rc.total_actual_bits = 0; |
1216 cpi->total_target_vs_actual = 0; | 1139 cpi->rc.total_target_vs_actual = 0; |
1217 | 1140 |
1218 cpi->static_mb_pct = 0; | 1141 cpi->static_mb_pct = 0; |
1219 | 1142 |
1220 cpi->lst_fb_idx = 0; | 1143 cpi->lst_fb_idx = 0; |
1221 cpi->gld_fb_idx = 1; | 1144 cpi->gld_fb_idx = 1; |
1222 cpi->alt_fb_idx = 2; | 1145 cpi->alt_fb_idx = 2; |
1223 | 1146 |
1224 cpi->current_layer = 0; | 1147 cpi->current_layer = 0; |
1225 cpi->use_svc = 0; | 1148 cpi->use_svc = 0; |
1226 | 1149 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1270 break; | 1193 break; |
1271 } | 1194 } |
1272 | 1195 |
1273 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q]; | 1196 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q]; |
1274 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q]; | 1197 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q]; |
1275 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level]; | 1198 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level]; |
1276 | 1199 |
1277 cpi->oxcf.lossless = oxcf->lossless; | 1200 cpi->oxcf.lossless = oxcf->lossless; |
1278 cpi->mb.e_mbd.itxm_add = cpi->oxcf.lossless ? vp9_iwht4x4_add | 1201 cpi->mb.e_mbd.itxm_add = cpi->oxcf.lossless ? vp9_iwht4x4_add |
1279 : vp9_idct4x4_add; | 1202 : vp9_idct4x4_add; |
1280 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; | 1203 cpi->rc.baseline_gf_interval = DEFAULT_GF_INTERVAL; |
1281 | 1204 |
1282 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; | 1205 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; |
1283 | 1206 |
1284 // cpi->use_golden_frame_only = 0; | 1207 // cpi->use_golden_frame_only = 0; |
1285 // cpi->use_last_frame_only = 0; | 1208 // cpi->use_last_frame_only = 0; |
1286 cpi->refresh_golden_frame = 0; | 1209 cpi->refresh_golden_frame = 0; |
1287 cpi->refresh_last_frame = 1; | 1210 cpi->refresh_last_frame = 1; |
1288 cm->refresh_frame_context = 1; | 1211 cm->refresh_frame_context = 1; |
1289 cm->reset_frame_context = 0; | 1212 cm->reset_frame_context = 0; |
1290 | 1213 |
1291 setup_features(cm); | 1214 setup_features(cm); |
1292 cpi->common.allow_high_precision_mv = 0; // Default mv precision | 1215 set_high_precision_mv(cpi, 0); |
1293 set_mvcost(cpi); | |
1294 | 1216 |
1295 { | 1217 { |
1296 int i; | 1218 int i; |
1297 | 1219 |
1298 for (i = 0; i < MAX_SEGMENTS; i++) | 1220 for (i = 0; i < MAX_SEGMENTS; i++) |
1299 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout; | 1221 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout; |
1300 } | 1222 } |
1301 | 1223 |
1302 // At the moment the first order values may not be > MAXQ | 1224 // At the moment the first order values may not be > MAXQ |
1303 cpi->oxcf.fixed_q = MIN(cpi->oxcf.fixed_q, MAXQ); | 1225 cpi->oxcf.fixed_q = MIN(cpi->oxcf.fixed_q, MAXQ); |
(...skipping 21 matching lines...) Expand all Loading... |
1325 if (cpi->oxcf.maximum_buffer_size == 0) | 1247 if (cpi->oxcf.maximum_buffer_size == 0) |
1326 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8; | 1248 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8; |
1327 else | 1249 else |
1328 cpi->oxcf.maximum_buffer_size = rescale(cpi->oxcf.maximum_buffer_size, | 1250 cpi->oxcf.maximum_buffer_size = rescale(cpi->oxcf.maximum_buffer_size, |
1329 cpi->oxcf.target_bandwidth, 1000); | 1251 cpi->oxcf.target_bandwidth, 1000); |
1330 | 1252 |
1331 // Set up frame rate and related parameters rate control values. | 1253 // Set up frame rate and related parameters rate control values. |
1332 vp9_new_framerate(cpi, cpi->oxcf.framerate); | 1254 vp9_new_framerate(cpi, cpi->oxcf.framerate); |
1333 | 1255 |
1334 // Set absolute upper and lower quality limits | 1256 // Set absolute upper and lower quality limits |
1335 cpi->worst_quality = cpi->oxcf.worst_allowed_q; | 1257 cpi->rc.worst_quality = cpi->oxcf.worst_allowed_q; |
1336 cpi->best_quality = cpi->oxcf.best_allowed_q; | 1258 cpi->rc.best_quality = cpi->oxcf.best_allowed_q; |
1337 | 1259 |
1338 // active values should only be modified if out of new range | 1260 // active values should only be modified if out of new range |
1339 cpi->active_worst_quality = clamp(cpi->active_worst_quality, | 1261 cpi->rc.active_worst_quality = clamp(cpi->rc.active_worst_quality, |
1340 cpi->oxcf.best_allowed_q, | 1262 cpi->rc.best_quality, |
1341 cpi->oxcf.worst_allowed_q); | 1263 cpi->rc.worst_quality); |
1342 | |
1343 cpi->active_best_quality = clamp(cpi->active_best_quality, | |
1344 cpi->oxcf.best_allowed_q, | |
1345 cpi->oxcf.worst_allowed_q); | |
1346 | |
1347 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0; | |
1348 | 1264 |
1349 cpi->cq_target_quality = cpi->oxcf.cq_level; | 1265 cpi->cq_target_quality = cpi->oxcf.cq_level; |
1350 | 1266 |
1351 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; | 1267 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; |
1352 | 1268 |
1353 cpi->target_bandwidth = cpi->oxcf.target_bandwidth; | 1269 cpi->target_bandwidth = cpi->oxcf.target_bandwidth; |
1354 | 1270 |
1355 cm->display_width = cpi->oxcf.width; | 1271 cm->display_width = cpi->oxcf.width; |
1356 cm->display_height = cpi->oxcf.height; | 1272 cm->display_height = cpi->oxcf.height; |
1357 | 1273 |
1358 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) | 1274 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) |
1359 cpi->oxcf.Sharpness = MIN(7, cpi->oxcf.Sharpness); | 1275 cpi->oxcf.Sharpness = MIN(7, cpi->oxcf.Sharpness); |
1360 | 1276 |
1361 cpi->common.lf.sharpness_level = cpi->oxcf.Sharpness; | 1277 cpi->common.lf.sharpness_level = cpi->oxcf.Sharpness; |
1362 | 1278 |
1363 if (cpi->initial_width) { | 1279 if (cpi->initial_width) { |
1364 // Increasing the size of the frame beyond the first seen frame, or some | 1280 // Increasing the size of the frame beyond the first seen frame, or some |
1365 // otherwise signalled maximum size, is not supported. | 1281 // otherwise signalled maximum size, is not supported. |
1366 // TODO(jkoleszar): exit gracefully. | 1282 // TODO(jkoleszar): exit gracefully. |
1367 assert(cm->width <= cpi->initial_width); | 1283 assert(cm->width <= cpi->initial_width); |
1368 assert(cm->height <= cpi->initial_height); | 1284 assert(cm->height <= cpi->initial_height); |
1369 } | 1285 } |
1370 update_frame_size(cpi); | 1286 update_frame_size(cpi); |
1371 | 1287 |
1372 if (cpi->oxcf.fixed_q >= 0) { | 1288 if (cpi->oxcf.fixed_q >= 0) { |
1373 cpi->last_q[0] = cpi->oxcf.fixed_q; | 1289 cpi->rc.last_q[0] = cpi->oxcf.fixed_q; |
1374 cpi->last_q[1] = cpi->oxcf.fixed_q; | 1290 cpi->rc.last_q[1] = cpi->oxcf.fixed_q; |
1375 cpi->last_boosted_qindex = cpi->oxcf.fixed_q; | 1291 cpi->rc.last_boosted_qindex = cpi->oxcf.fixed_q; |
1376 } | 1292 } |
1377 | 1293 |
1378 cpi->speed = cpi->oxcf.cpu_used; | 1294 cpi->speed = cpi->oxcf.cpu_used; |
1379 | 1295 |
1380 if (cpi->oxcf.lag_in_frames == 0) { | 1296 if (cpi->oxcf.lag_in_frames == 0) { |
1381 // force to allowlag to 0 if lag_in_frames is 0; | 1297 // force to allowlag to 0 if lag_in_frames is 0; |
1382 cpi->oxcf.allow_lag = 0; | 1298 cpi->oxcf.allow_lag = 0; |
1383 } else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) { | 1299 } else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) { |
1384 // Limit on lag buffers as these are not currently dynamically allocated | 1300 // Limit on lag buffers as these are not currently dynamically allocated |
1385 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS; | 1301 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1435 | 1351 |
1436 do { | 1352 do { |
1437 double z = 256 * (2 * (log2f(8 * i) + .6)); | 1353 double z = 256 * (2 * (log2f(8 * i) + .6)); |
1438 mvsadcost[0][i] = (int)z; | 1354 mvsadcost[0][i] = (int)z; |
1439 mvsadcost[1][i] = (int)z; | 1355 mvsadcost[1][i] = (int)z; |
1440 mvsadcost[0][-i] = (int)z; | 1356 mvsadcost[0][-i] = (int)z; |
1441 mvsadcost[1][-i] = (int)z; | 1357 mvsadcost[1][-i] = (int)z; |
1442 } while (++i <= MV_MAX); | 1358 } while (++i <= MV_MAX); |
1443 } | 1359 } |
1444 | 1360 |
| 1361 static void alloc_mode_context(VP9_COMMON *cm, int num_4x4_blk, |
| 1362 PICK_MODE_CONTEXT *ctx) { |
| 1363 int num_pix = num_4x4_blk << 4; |
| 1364 int i, k; |
| 1365 ctx->num_4x4_blk = num_4x4_blk; |
| 1366 CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, |
| 1367 vpx_calloc(num_4x4_blk, sizeof(uint8_t))); |
| 1368 for (i = 0; i < MAX_MB_PLANE; ++i) { |
| 1369 for (k = 0; k < 3; ++k) { |
| 1370 CHECK_MEM_ERROR(cm, ctx->coeff[i][k], |
| 1371 vpx_memalign(16, num_pix * sizeof(int16_t))); |
| 1372 CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k], |
| 1373 vpx_memalign(16, num_pix * sizeof(int16_t))); |
| 1374 CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k], |
| 1375 vpx_memalign(16, num_pix * sizeof(int16_t))); |
| 1376 CHECK_MEM_ERROR(cm, ctx->eobs[i][k], |
| 1377 vpx_memalign(16, num_pix * sizeof(uint16_t))); |
| 1378 ctx->coeff_pbuf[i][k] = ctx->coeff[i][k]; |
| 1379 ctx->qcoeff_pbuf[i][k] = ctx->qcoeff[i][k]; |
| 1380 ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k]; |
| 1381 ctx->eobs_pbuf[i][k] = ctx->eobs[i][k]; |
| 1382 } |
| 1383 } |
| 1384 } |
| 1385 |
| 1386 static void free_mode_context(PICK_MODE_CONTEXT *ctx) { |
| 1387 int i, k; |
| 1388 vpx_free(ctx->zcoeff_blk); |
| 1389 ctx->zcoeff_blk = 0; |
| 1390 for (i = 0; i < MAX_MB_PLANE; ++i) { |
| 1391 for (k = 0; k < 3; ++k) { |
| 1392 vpx_free(ctx->coeff[i][k]); |
| 1393 ctx->coeff[i][k] = 0; |
| 1394 vpx_free(ctx->qcoeff[i][k]); |
| 1395 ctx->qcoeff[i][k] = 0; |
| 1396 vpx_free(ctx->dqcoeff[i][k]); |
| 1397 ctx->dqcoeff[i][k] = 0; |
| 1398 vpx_free(ctx->eobs[i][k]); |
| 1399 ctx->eobs[i][k] = 0; |
| 1400 } |
| 1401 } |
| 1402 } |
| 1403 |
1445 static void init_pick_mode_context(VP9_COMP *cpi) { | 1404 static void init_pick_mode_context(VP9_COMP *cpi) { |
1446 int i; | 1405 int i; |
1447 MACROBLOCK *x = &cpi->mb; | 1406 VP9_COMMON *const cm = &cpi->common; |
1448 MACROBLOCKD *xd = &x->e_mbd; | 1407 MACROBLOCK *const x = &cpi->mb; |
1449 VP9_COMMON *cm = &cpi->common; | 1408 |
1450 | 1409 |
1451 for (i = 0; i < BLOCK_SIZES; ++i) { | 1410 for (i = 0; i < BLOCK_SIZES; ++i) { |
1452 const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; | 1411 const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; |
1453 const int num_4x4_h = num_4x4_blocks_high_lookup[i]; | 1412 const int num_4x4_h = num_4x4_blocks_high_lookup[i]; |
1454 const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); | 1413 const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); |
1455 if (i < BLOCK_16X16) { | 1414 if (i < BLOCK_16X16) { |
1456 for (xd->sb_index = 0; xd->sb_index < 4; ++xd->sb_index) { | 1415 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { |
1457 for (xd->mb_index = 0; xd->mb_index < 4; ++xd->mb_index) { | 1416 for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) { |
1458 for (xd->b_index = 0; xd->b_index < 16 / num_4x4_blk; ++xd->b_index) { | 1417 for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) { |
1459 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); | 1418 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
1460 ctx->num_4x4_blk = num_4x4_blk; | 1419 alloc_mode_context(cm, num_4x4_blk, ctx); |
1461 CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, | |
1462 vpx_calloc(num_4x4_blk, sizeof(uint8_t))); | |
1463 } | 1420 } |
1464 } | 1421 } |
1465 } | 1422 } |
1466 } else if (i < BLOCK_32X32) { | 1423 } else if (i < BLOCK_32X32) { |
1467 for (xd->sb_index = 0; xd->sb_index < 4; ++xd->sb_index) { | 1424 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { |
1468 for (xd->mb_index = 0; xd->mb_index < 64 / num_4x4_blk; | 1425 for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) { |
1469 ++xd->mb_index) { | |
1470 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); | 1426 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
1471 ctx->num_4x4_blk = num_4x4_blk; | 1427 ctx->num_4x4_blk = num_4x4_blk; |
1472 CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, | 1428 alloc_mode_context(cm, num_4x4_blk, ctx); |
1473 vpx_calloc(num_4x4_blk, sizeof(uint8_t))); | |
1474 } | 1429 } |
1475 } | 1430 } |
1476 } else if (i < BLOCK_64X64) { | 1431 } else if (i < BLOCK_64X64) { |
1477 for (xd->sb_index = 0; xd->sb_index < 256 / num_4x4_blk; ++xd->sb_index) { | 1432 for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) { |
1478 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); | 1433 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
1479 ctx->num_4x4_blk = num_4x4_blk; | 1434 ctx->num_4x4_blk = num_4x4_blk; |
1480 CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, | 1435 alloc_mode_context(cm, num_4x4_blk, ctx); |
1481 vpx_calloc(num_4x4_blk, sizeof(uint8_t))); | |
1482 } | 1436 } |
1483 } else { | 1437 } else { |
1484 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); | 1438 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
1485 ctx->num_4x4_blk = num_4x4_blk; | 1439 ctx->num_4x4_blk = num_4x4_blk; |
1486 CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, | 1440 alloc_mode_context(cm, num_4x4_blk, ctx); |
1487 vpx_calloc(num_4x4_blk, sizeof(uint8_t))); | |
1488 } | 1441 } |
1489 } | 1442 } |
1490 } | 1443 } |
1491 | 1444 |
1492 static void free_pick_mode_context(MACROBLOCK *x) { | 1445 static void free_pick_mode_context(MACROBLOCK *x) { |
1493 int i; | 1446 int i; |
1494 MACROBLOCKD *xd = &x->e_mbd; | |
1495 | 1447 |
1496 for (i = 0; i < BLOCK_SIZES; ++i) { | 1448 for (i = 0; i < BLOCK_SIZES; ++i) { |
1497 const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; | 1449 const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; |
1498 const int num_4x4_h = num_4x4_blocks_high_lookup[i]; | 1450 const int num_4x4_h = num_4x4_blocks_high_lookup[i]; |
1499 const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); | 1451 const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); |
1500 if (i < BLOCK_16X16) { | 1452 if (i < BLOCK_16X16) { |
1501 for (xd->sb_index = 0; xd->sb_index < 4; ++xd->sb_index) { | 1453 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { |
1502 for (xd->mb_index = 0; xd->mb_index < 4; ++xd->mb_index) { | 1454 for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) { |
1503 for (xd->b_index = 0; xd->b_index < 16 / num_4x4_blk; ++xd->b_index) { | 1455 for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) { |
1504 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); | 1456 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
1505 vpx_free(ctx->zcoeff_blk); | 1457 free_mode_context(ctx); |
1506 ctx->zcoeff_blk = 0; | |
1507 } | 1458 } |
1508 } | 1459 } |
1509 } | 1460 } |
1510 } else if (i < BLOCK_32X32) { | 1461 } else if (i < BLOCK_32X32) { |
1511 for (xd->sb_index = 0; xd->sb_index < 4; ++xd->sb_index) { | 1462 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { |
1512 for (xd->mb_index = 0; xd->mb_index < 64 / num_4x4_blk; | 1463 for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) { |
1513 ++xd->mb_index) { | |
1514 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); | 1464 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
1515 vpx_free(ctx->zcoeff_blk); | 1465 free_mode_context(ctx); |
1516 ctx->zcoeff_blk = 0; | |
1517 } | 1466 } |
1518 } | 1467 } |
1519 } else if (i < BLOCK_64X64) { | 1468 } else if (i < BLOCK_64X64) { |
1520 for (xd->sb_index = 0; xd->sb_index < 256 / num_4x4_blk; ++xd->sb_index) { | 1469 for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) { |
1521 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); | 1470 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
1522 vpx_free(ctx->zcoeff_blk); | 1471 free_mode_context(ctx); |
1523 ctx->zcoeff_blk = 0; | |
1524 } | 1472 } |
1525 } else { | 1473 } else { |
1526 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); | 1474 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
1527 vpx_free(ctx->zcoeff_blk); | 1475 free_mode_context(ctx); |
1528 ctx->zcoeff_blk = 0; | |
1529 } | 1476 } |
1530 } | 1477 } |
1531 } | 1478 } |
1532 | 1479 |
1533 VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) { | 1480 VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) { |
1534 int i, j; | 1481 int i, j; |
1535 volatile union { | 1482 volatile union { |
1536 VP9_COMP *cpi; | 1483 VP9_COMP *cpi; |
1537 VP9_PTR ptr; | 1484 VP9_PTR ptr; |
1538 } ctx; | 1485 } ctx; |
(...skipping 23 matching lines...) Expand all Loading... |
1562 CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site), | 1509 CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site), |
1563 (MAX_MVSEARCH_STEPS * 8) + 1)); | 1510 (MAX_MVSEARCH_STEPS * 8) + 1)); |
1564 | 1511 |
1565 vp9_create_common(cm); | 1512 vp9_create_common(cm); |
1566 | 1513 |
1567 init_config((VP9_PTR)cpi, oxcf); | 1514 init_config((VP9_PTR)cpi, oxcf); |
1568 | 1515 |
1569 init_pick_mode_context(cpi); | 1516 init_pick_mode_context(cpi); |
1570 | 1517 |
1571 cm->current_video_frame = 0; | 1518 cm->current_video_frame = 0; |
1572 cpi->kf_overspend_bits = 0; | 1519 cpi->rc.frames_till_gf_update_due = 0; |
1573 cpi->kf_bitrate_adjustment = 0; | |
1574 cpi->frames_till_gf_update_due = 0; | |
1575 cpi->gf_overspend_bits = 0; | |
1576 cpi->non_gf_bitrate_adjustment = 0; | |
1577 | 1520 |
1578 // Set reference frame sign bias for ALTREF frame to 1 (for now) | 1521 // Set reference frame sign bias for ALTREF frame to 1 (for now) |
1579 cm->ref_frame_sign_bias[ALTREF_FRAME] = 1; | 1522 cm->ref_frame_sign_bias[ALTREF_FRAME] = 1; |
1580 | 1523 |
1581 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; | 1524 cpi->rc.baseline_gf_interval = DEFAULT_GF_INTERVAL; |
1582 | 1525 |
1583 cpi->gold_is_last = 0; | 1526 cpi->gold_is_last = 0; |
1584 cpi->alt_is_last = 0; | 1527 cpi->alt_is_last = 0; |
1585 cpi->gold_is_alt = 0; | 1528 cpi->gold_is_alt = 0; |
1586 | 1529 |
1587 // Spatial scalability | 1530 // Spatial scalability |
1588 cpi->number_spatial_layers = oxcf->ss_number_layers; | 1531 cpi->number_spatial_layers = oxcf->ss_number_layers; |
1589 | 1532 |
1590 // Create the encoder segmentation map and set all entries to 0 | 1533 // Create the encoder segmentation map and set all entries to 0 |
1591 CHECK_MEM_ERROR(cm, cpi->segmentation_map, | 1534 CHECK_MEM_ERROR(cm, cpi->segmentation_map, |
1592 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); | 1535 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); |
1593 | 1536 |
| 1537 // Create a complexity map used for rd adjustment |
| 1538 CHECK_MEM_ERROR(cm, cpi->complexity_map, |
| 1539 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); |
| 1540 |
| 1541 |
1594 // And a place holder structure is the coding context | 1542 // And a place holder structure is the coding context |
1595 // for use if we want to save and restore it | 1543 // for use if we want to save and restore it |
1596 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy, | 1544 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy, |
1597 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); | 1545 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); |
1598 | 1546 |
1599 CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1)); | 1547 CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1)); |
1600 vpx_memset(cpi->active_map, 1, cm->MBs); | 1548 vpx_memset(cpi->active_map, 1, cm->MBs); |
1601 cpi->active_map_enabled = 0; | 1549 cpi->active_map_enabled = 0; |
1602 | 1550 |
1603 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / | 1551 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1671 cpi->total_ssimg_y = 0; | 1619 cpi->total_ssimg_y = 0; |
1672 cpi->total_ssimg_u = 0; | 1620 cpi->total_ssimg_u = 0; |
1673 cpi->total_ssimg_v = 0; | 1621 cpi->total_ssimg_v = 0; |
1674 cpi->total_ssimg_all = 0; | 1622 cpi->total_ssimg_all = 0; |
1675 } | 1623 } |
1676 | 1624 |
1677 #endif | 1625 #endif |
1678 | 1626 |
1679 cpi->first_time_stamp_ever = INT64_MAX; | 1627 cpi->first_time_stamp_ever = INT64_MAX; |
1680 | 1628 |
1681 cpi->frames_till_gf_update_due = 0; | 1629 cpi->rc.frames_till_gf_update_due = 0; |
1682 cpi->key_frame_count = 1; | 1630 cpi->rc.key_frame_count = 1; |
1683 | 1631 |
1684 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q; | 1632 cpi->rc.ni_av_qi = cpi->oxcf.worst_allowed_q; |
1685 cpi->ni_tot_qi = 0; | 1633 cpi->rc.ni_tot_qi = 0; |
1686 cpi->ni_frames = 0; | 1634 cpi->rc.ni_frames = 0; |
1687 cpi->tot_q = 0.0; | 1635 cpi->rc.tot_q = 0.0; |
1688 cpi->avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q); | 1636 cpi->rc.avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q); |
1689 cpi->total_byte_count = 0; | |
1690 | 1637 |
1691 cpi->rate_correction_factor = 1.0; | 1638 cpi->rc.rate_correction_factor = 1.0; |
1692 cpi->key_frame_rate_correction_factor = 1.0; | 1639 cpi->rc.key_frame_rate_correction_factor = 1.0; |
1693 cpi->gf_rate_correction_factor = 1.0; | 1640 cpi->rc.gf_rate_correction_factor = 1.0; |
1694 cpi->twopass.est_max_qcorrection_factor = 1.0; | |
1695 | 1641 |
1696 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost); | 1642 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost); |
1697 cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX]; | 1643 cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX]; |
1698 cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX]; | 1644 cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX]; |
1699 cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX]; | 1645 cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX]; |
1700 cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX]; | 1646 cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX]; |
1701 cal_nmvsadcosts(cpi->mb.nmvsadcost); | 1647 cal_nmvsadcosts(cpi->mb.nmvsadcost); |
1702 | 1648 |
1703 cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX]; | 1649 cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX]; |
1704 cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX]; | 1650 cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX]; |
1705 cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX]; | 1651 cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX]; |
1706 cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX]; | 1652 cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX]; |
1707 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp); | 1653 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp); |
1708 | 1654 |
1709 for (i = 0; i < KEY_FRAME_CONTEXT; i++) | 1655 for (i = 0; i < KEY_FRAME_CONTEXT; i++) |
1710 cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate; | 1656 cpi->rc.prior_key_frame_distance[i] = (int)cpi->output_framerate; |
1711 | 1657 |
1712 #ifdef OUTPUT_YUV_SRC | 1658 #ifdef OUTPUT_YUV_SRC |
1713 yuv_file = fopen("bd.yuv", "ab"); | 1659 yuv_file = fopen("bd.yuv", "ab"); |
1714 #endif | 1660 #endif |
1715 #ifdef OUTPUT_YUV_REC | 1661 #ifdef OUTPUT_YUV_REC |
1716 yuv_rec_file = fopen("rec.yuv", "wb"); | 1662 yuv_rec_file = fopen("rec.yuv", "wb"); |
1717 #endif | 1663 #endif |
1718 | 1664 |
1719 #if 0 | 1665 #if 0 |
1720 framepsnr = fopen("framepsnr.stt", "a"); | 1666 framepsnr = fopen("framepsnr.stt", "a"); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1871 int i; | 1817 int i; |
1872 | 1818 |
1873 if (!cpi) | 1819 if (!cpi) |
1874 return; | 1820 return; |
1875 | 1821 |
1876 if (cpi && (cpi->common.current_video_frame > 0)) { | 1822 if (cpi && (cpi->common.current_video_frame > 0)) { |
1877 if (cpi->pass == 2) { | 1823 if (cpi->pass == 2) { |
1878 vp9_end_second_pass(cpi); | 1824 vp9_end_second_pass(cpi); |
1879 } | 1825 } |
1880 | 1826 |
1881 #ifdef ENTROPY_STATS | |
1882 if (cpi->pass != 1) { | |
1883 print_context_counters(); | |
1884 print_tree_update_probs(); | |
1885 print_mode_context(cpi); | |
1886 } | |
1887 #endif | |
1888 | |
1889 #ifdef MODE_STATS | 1827 #ifdef MODE_STATS |
1890 if (cpi->pass != 1) { | 1828 if (cpi->pass != 1) { |
1891 write_tx_count_stats(); | 1829 write_tx_count_stats(); |
1892 write_switchable_interp_stats(); | 1830 write_switchable_interp_stats(); |
1893 } | 1831 } |
1894 #endif | 1832 #endif |
1895 | 1833 |
1896 #if CONFIG_INTERNAL_STATS | 1834 #if CONFIG_INTERNAL_STATS |
1897 | 1835 |
1898 vp9_clear_system_state(); | 1836 vp9_clear_system_state(); |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2211 | 2149 |
2212 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd); | 2150 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd); |
2213 | 2151 |
2214 return 0; | 2152 return 0; |
2215 } | 2153 } |
2216 | 2154 |
2217 int vp9_get_reference_enc(VP9_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) { | 2155 int vp9_get_reference_enc(VP9_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) { |
2218 VP9_COMP *cpi = (VP9_COMP *)(ptr); | 2156 VP9_COMP *cpi = (VP9_COMP *)(ptr); |
2219 VP9_COMMON *cm = &cpi->common; | 2157 VP9_COMMON *cm = &cpi->common; |
2220 | 2158 |
2221 if (index < 0 || index >= NUM_REF_FRAMES) | 2159 if (index < 0 || index >= REF_FRAMES) |
2222 return -1; | 2160 return -1; |
2223 | 2161 |
2224 *fb = &cm->yv12_fb[cm->ref_frame_map[index]]; | 2162 *fb = &cm->yv12_fb[cm->ref_frame_map[index]]; |
2225 return 0; | 2163 return 0; |
2226 } | 2164 } |
2227 | 2165 |
2228 int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag, | 2166 int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag, |
2229 YV12_BUFFER_CONFIG *sd) { | 2167 YV12_BUFFER_CONFIG *sd) { |
2230 VP9_COMP *cpi = (VP9_COMP *)(ptr); | 2168 VP9_COMP *cpi = (VP9_COMP *)(ptr); |
2231 VP9_COMMON *cm = &cpi->common; | 2169 VP9_COMMON *cm = &cpi->common; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2358 } | 2296 } |
2359 } | 2297 } |
2360 } | 2298 } |
2361 | 2299 |
2362 vp8_yv12_extend_frame_borders(dst_fb); | 2300 vp8_yv12_extend_frame_borders(dst_fb); |
2363 } | 2301 } |
2364 | 2302 |
2365 | 2303 |
2366 static void update_alt_ref_frame_stats(VP9_COMP *cpi) { | 2304 static void update_alt_ref_frame_stats(VP9_COMP *cpi) { |
2367 // this frame refreshes means next frames don't unless specified by user | 2305 // this frame refreshes means next frames don't unless specified by user |
2368 cpi->frames_since_golden = 0; | 2306 cpi->rc.frames_since_golden = 0; |
2369 | 2307 |
2370 #if CONFIG_MULTIPLE_ARF | 2308 #if CONFIG_MULTIPLE_ARF |
2371 if (!cpi->multi_arf_enabled) | 2309 if (!cpi->multi_arf_enabled) |
2372 #endif | 2310 #endif |
2373 // Clear the alternate reference update pending flag. | 2311 // Clear the alternate reference update pending flag. |
2374 cpi->source_alt_ref_pending = 0; | 2312 cpi->source_alt_ref_pending = 0; |
2375 | 2313 |
2376 // Set the alternate reference frame active flag | 2314 // Set the alternate reference frame active flag |
2377 cpi->source_alt_ref_active = 1; | 2315 cpi->source_alt_ref_active = 1; |
2378 } | 2316 } |
2379 static void update_golden_frame_stats(VP9_COMP *cpi) { | 2317 static void update_golden_frame_stats(VP9_COMP *cpi) { |
2380 // Update the Golden frame usage counts. | 2318 // Update the Golden frame usage counts. |
2381 if (cpi->refresh_golden_frame) { | 2319 if (cpi->refresh_golden_frame) { |
2382 // this frame refreshes means next frames don't unless specified by user | 2320 // this frame refreshes means next frames don't unless specified by user |
2383 cpi->refresh_golden_frame = 0; | 2321 cpi->refresh_golden_frame = 0; |
2384 cpi->frames_since_golden = 0; | 2322 cpi->rc.frames_since_golden = 0; |
2385 | 2323 |
2386 // ******** Fixed Q test code only ************ | 2324 // ******** Fixed Q test code only ************ |
2387 // If we are going to use the ALT reference for the next group of frames | 2325 // If we are going to use the ALT reference for the next group of frames |
2388 // set a flag to say so. | 2326 // set a flag to say so. |
2389 if (cpi->oxcf.fixed_q >= 0 && | 2327 if (cpi->oxcf.fixed_q >= 0 && |
2390 cpi->oxcf.play_alternate && !cpi->refresh_alt_ref_frame) { | 2328 cpi->oxcf.play_alternate && !cpi->refresh_alt_ref_frame) { |
2391 cpi->source_alt_ref_pending = 1; | 2329 cpi->source_alt_ref_pending = 1; |
2392 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; | 2330 cpi->rc.frames_till_gf_update_due = cpi->rc.baseline_gf_interval; |
2393 | 2331 |
2394 // TODO(ivan): For SVC encoder, GF automatic update is disabled by using | 2332 // TODO(ivan): For SVC encoder, GF automatic update is disabled by using |
2395 // a large GF_interval. | 2333 // a large GF_interval. |
2396 if (cpi->use_svc) { | 2334 if (cpi->use_svc) { |
2397 cpi->frames_till_gf_update_due = INT_MAX; | 2335 cpi->rc.frames_till_gf_update_due = INT_MAX; |
2398 } | 2336 } |
2399 } | 2337 } |
2400 | 2338 |
2401 if (!cpi->source_alt_ref_pending) | 2339 if (!cpi->source_alt_ref_pending) |
2402 cpi->source_alt_ref_active = 0; | 2340 cpi->source_alt_ref_active = 0; |
2403 | 2341 |
2404 // Decrement count down till next gf | 2342 // Decrement count down till next gf |
2405 if (cpi->frames_till_gf_update_due > 0) | 2343 if (cpi->rc.frames_till_gf_update_due > 0) |
2406 cpi->frames_till_gf_update_due--; | 2344 cpi->rc.frames_till_gf_update_due--; |
2407 | 2345 |
2408 } else if (!cpi->refresh_alt_ref_frame) { | 2346 } else if (!cpi->refresh_alt_ref_frame) { |
2409 // Decrement count down till next gf | 2347 // Decrement count down till next gf |
2410 if (cpi->frames_till_gf_update_due > 0) | 2348 if (cpi->rc.frames_till_gf_update_due > 0) |
2411 cpi->frames_till_gf_update_due--; | 2349 cpi->rc.frames_till_gf_update_due--; |
2412 | 2350 |
2413 if (cpi->frames_till_alt_ref_frame) | 2351 if (cpi->frames_till_alt_ref_frame) |
2414 cpi->frames_till_alt_ref_frame--; | 2352 cpi->frames_till_alt_ref_frame--; |
2415 | 2353 |
2416 cpi->frames_since_golden++; | 2354 cpi->rc.frames_since_golden++; |
2417 } | 2355 } |
2418 } | 2356 } |
2419 | 2357 |
2420 static int find_fp_qindex() { | 2358 static int find_fp_qindex() { |
2421 int i; | 2359 int i; |
2422 | 2360 |
2423 for (i = 0; i < QINDEX_RANGE; i++) { | 2361 for (i = 0; i < QINDEX_RANGE; i++) { |
2424 if (vp9_convert_qindex_to_q(i) >= 30.0) { | 2362 if (vp9_convert_qindex_to_q(i) >= 30.0) { |
2425 break; | 2363 break; |
2426 } | 2364 } |
2427 } | 2365 } |
2428 | 2366 |
2429 if (i == QINDEX_RANGE) | 2367 if (i == QINDEX_RANGE) |
2430 i--; | 2368 i--; |
2431 | 2369 |
2432 return i; | 2370 return i; |
2433 } | 2371 } |
2434 | 2372 |
2435 static void Pass1Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest, | |
2436 unsigned int *frame_flags) { | |
2437 (void) size; | |
2438 (void) dest; | |
2439 (void) frame_flags; | |
2440 | |
2441 vp9_set_quantizer(cpi, find_fp_qindex()); | |
2442 vp9_first_pass(cpi); | |
2443 } | |
2444 | |
2445 #define WRITE_RECON_BUFFER 0 | 2373 #define WRITE_RECON_BUFFER 0 |
2446 #if WRITE_RECON_BUFFER | 2374 #if WRITE_RECON_BUFFER |
2447 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) { | 2375 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) { |
2448 FILE *yframe; | 2376 FILE *yframe; |
2449 int i; | 2377 int i; |
2450 char filename[255]; | 2378 char filename[255]; |
2451 | 2379 |
2452 snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame); | 2380 snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame); |
2453 yframe = fopen(filename, "wb"); | 2381 yframe = fopen(filename, "wb"); |
2454 | 2382 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2516 | 2444 |
2517 // Is frame recode allowed at all | 2445 // Is frame recode allowed at all |
2518 // Yes if either recode mode 1 is selected or mode two is selected | 2446 // Yes if either recode mode 1 is selected or mode two is selected |
2519 // and the frame is a key frame. golden frame or alt_ref_frame | 2447 // and the frame is a key frame. golden frame or alt_ref_frame |
2520 if ((cpi->sf.recode_loop == 1) || | 2448 if ((cpi->sf.recode_loop == 1) || |
2521 ((cpi->sf.recode_loop == 2) && | 2449 ((cpi->sf.recode_loop == 2) && |
2522 ((cm->frame_type == KEY_FRAME) || | 2450 ((cm->frame_type == KEY_FRAME) || |
2523 cpi->refresh_golden_frame || | 2451 cpi->refresh_golden_frame || |
2524 cpi->refresh_alt_ref_frame))) { | 2452 cpi->refresh_alt_ref_frame))) { |
2525 // General over and under shoot tests | 2453 // General over and under shoot tests |
2526 if (((cpi->projected_frame_size > high_limit) && (q < maxq)) || | 2454 if (((cpi->rc.projected_frame_size > high_limit) && (q < maxq)) || |
2527 ((cpi->projected_frame_size < low_limit) && (q > minq))) { | 2455 ((cpi->rc.projected_frame_size < low_limit) && (q > minq))) { |
2528 force_recode = 1; | 2456 force_recode = 1; |
2529 } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { | 2457 } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { |
2530 // Deal with frame undershoot and whether or not we are | 2458 // Deal with frame undershoot and whether or not we are |
2531 // below the automatically set cq level. | 2459 // below the automatically set cq level. |
2532 if (q > cpi->cq_target_quality && | 2460 if (q > cpi->cq_target_quality && |
2533 cpi->projected_frame_size < ((cpi->this_frame_target * 7) >> 3)) { | 2461 cpi->rc.projected_frame_size < |
| 2462 ((cpi->rc.this_frame_target * 7) >> 3)) { |
2534 force_recode = 1; | 2463 force_recode = 1; |
2535 } else if (q > cpi->oxcf.cq_level && | |
2536 cpi->projected_frame_size < cpi->min_frame_bandwidth && | |
2537 cpi->active_best_quality > cpi->oxcf.cq_level) { | |
2538 // Severe undershoot and between auto and user cq level | |
2539 force_recode = 1; | |
2540 cpi->active_best_quality = cpi->oxcf.cq_level; | |
2541 } | 2464 } |
2542 } | 2465 } |
2543 } | 2466 } |
2544 | |
2545 return force_recode; | 2467 return force_recode; |
2546 } | 2468 } |
2547 | 2469 |
2548 static void update_reference_frames(VP9_COMP * const cpi) { | 2470 static void update_reference_frames(VP9_COMP * const cpi) { |
2549 VP9_COMMON * const cm = &cpi->common; | 2471 VP9_COMMON * const cm = &cpi->common; |
2550 | 2472 |
2551 // At this point the new frame has been encoded. | 2473 // At this point the new frame has been encoded. |
2552 // If any buffer copy / swapping is signaled it should be done here. | 2474 // If any buffer copy / swapping is signaled it should be done here. |
2553 if (cm->frame_type == KEY_FRAME) { | 2475 if (cm->frame_type == KEY_FRAME) { |
2554 ref_cnt_fb(cm->fb_idx_ref_cnt, | 2476 ref_cnt_fb(cm->fb_idx_ref_cnt, |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2627 vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0); | 2549 vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0); |
2628 } | 2550 } |
2629 | 2551 |
2630 vp9_extend_frame_inner_borders(cm->frame_to_show, | 2552 vp9_extend_frame_inner_borders(cm->frame_to_show, |
2631 cm->subsampling_x, cm->subsampling_y); | 2553 cm->subsampling_x, cm->subsampling_y); |
2632 } | 2554 } |
2633 | 2555 |
2634 static void scale_references(VP9_COMP *cpi) { | 2556 static void scale_references(VP9_COMP *cpi) { |
2635 VP9_COMMON *cm = &cpi->common; | 2557 VP9_COMMON *cm = &cpi->common; |
2636 int i; | 2558 int i; |
2637 int refs[ALLOWED_REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx, | 2559 int refs[REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx, |
2638 cpi->alt_fb_idx}; | 2560 cpi->alt_fb_idx}; |
2639 | 2561 |
2640 for (i = 0; i < 3; i++) { | 2562 for (i = 0; i < 3; i++) { |
2641 YV12_BUFFER_CONFIG *ref = &cm->yv12_fb[cm->ref_frame_map[refs[i]]]; | 2563 YV12_BUFFER_CONFIG *ref = &cm->yv12_fb[cm->ref_frame_map[refs[i]]]; |
2642 | 2564 |
2643 if (ref->y_crop_width != cm->width || | 2565 if (ref->y_crop_width != cm->width || |
2644 ref->y_crop_height != cm->height) { | 2566 ref->y_crop_height != cm->height) { |
2645 int new_fb = get_free_fb(cm); | 2567 int new_fb = get_free_fb(cm); |
2646 | 2568 |
2647 vp9_realloc_frame_buffer(&cm->yv12_fb[new_fb], | 2569 vp9_realloc_frame_buffer(&cm->yv12_fb[new_fb], |
2648 cm->width, cm->height, | 2570 cm->width, cm->height, |
2649 cm->subsampling_x, cm->subsampling_y, | 2571 cm->subsampling_x, cm->subsampling_y, |
2650 VP9BORDERINPIXELS); | 2572 VP9BORDERINPIXELS, NULL, NULL, NULL); |
2651 scale_and_extend_frame(ref, &cm->yv12_fb[new_fb]); | 2573 scale_and_extend_frame(ref, &cm->yv12_fb[new_fb]); |
2652 cpi->scaled_ref_idx[i] = new_fb; | 2574 cpi->scaled_ref_idx[i] = new_fb; |
2653 } else { | 2575 } else { |
2654 cpi->scaled_ref_idx[i] = cm->ref_frame_map[refs[i]]; | 2576 cpi->scaled_ref_idx[i] = cm->ref_frame_map[refs[i]]; |
2655 cm->fb_idx_ref_cnt[cm->ref_frame_map[refs[i]]]++; | 2577 cm->fb_idx_ref_cnt[cm->ref_frame_map[refs[i]]]++; |
2656 } | 2578 } |
2657 } | 2579 } |
2658 } | 2580 } |
2659 | 2581 |
2660 static void release_scaled_references(VP9_COMP *cpi) { | 2582 static void release_scaled_references(VP9_COMP *cpi) { |
2661 VP9_COMMON *cm = &cpi->common; | 2583 VP9_COMMON *cm = &cpi->common; |
2662 int i; | 2584 int i; |
2663 | 2585 |
2664 for (i = 0; i < 3; i++) | 2586 for (i = 0; i < 3; i++) |
2665 cm->fb_idx_ref_cnt[cpi->scaled_ref_idx[i]]--; | 2587 cm->fb_idx_ref_cnt[cpi->scaled_ref_idx[i]]--; |
2666 } | 2588 } |
2667 | 2589 |
2668 static void full_to_model_count(unsigned int *model_count, | 2590 static void full_to_model_count(unsigned int *model_count, |
2669 unsigned int *full_count) { | 2591 unsigned int *full_count) { |
2670 int n; | 2592 int n; |
2671 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN]; | 2593 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN]; |
2672 model_count[ONE_TOKEN] = full_count[ONE_TOKEN]; | 2594 model_count[ONE_TOKEN] = full_count[ONE_TOKEN]; |
2673 model_count[TWO_TOKEN] = full_count[TWO_TOKEN]; | 2595 model_count[TWO_TOKEN] = full_count[TWO_TOKEN]; |
2674 for (n = THREE_TOKEN; n < DCT_EOB_TOKEN; ++n) | 2596 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n) |
2675 model_count[TWO_TOKEN] += full_count[n]; | 2597 model_count[TWO_TOKEN] += full_count[n]; |
2676 model_count[DCT_EOB_MODEL_TOKEN] = full_count[DCT_EOB_TOKEN]; | 2598 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN]; |
2677 } | 2599 } |
2678 | 2600 |
2679 static void full_to_model_counts( | 2601 static void full_to_model_counts(vp9_coeff_count_model *model_count, |
2680 vp9_coeff_count_model *model_count, vp9_coeff_count *full_count) { | 2602 vp9_coeff_count *full_count) { |
2681 int i, j, k, l; | 2603 int i, j, k, l; |
2682 for (i = 0; i < BLOCK_TYPES; ++i) | 2604 |
| 2605 for (i = 0; i < PLANE_TYPES; ++i) |
2683 for (j = 0; j < REF_TYPES; ++j) | 2606 for (j = 0; j < REF_TYPES; ++j) |
2684 for (k = 0; k < COEF_BANDS; ++k) | 2607 for (k = 0; k < COEF_BANDS; ++k) |
2685 for (l = 0; l < PREV_COEF_CONTEXTS; ++l) { | 2608 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) |
2686 if (l >= 3 && k == 0) | |
2687 continue; | |
2688 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]); | 2609 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]); |
2689 } | |
2690 } | 2610 } |
2691 | 2611 |
2692 #if 0 && CONFIG_INTERNAL_STATS | 2612 #if 0 && CONFIG_INTERNAL_STATS |
2693 static void output_frame_level_debug_stats(VP9_COMP *cpi) { | 2613 static void output_frame_level_debug_stats(VP9_COMP *cpi) { |
2694 VP9_COMMON *const cm = &cpi->common; | 2614 VP9_COMMON *const cm = &cpi->common; |
2695 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w"); | 2615 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w"); |
2696 int recon_err; | 2616 int recon_err; |
2697 | 2617 |
2698 vp9_clear_system_state(); // __asm emms; | 2618 vp9_clear_system_state(); // __asm emms; |
2699 | 2619 |
2700 recon_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); | 2620 recon_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); |
2701 | 2621 |
2702 if (cpi->twopass.total_left_stats.coded_error != 0.0) | 2622 if (cpi->twopass.total_left_stats.coded_error != 0.0) |
2703 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d" | 2623 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d" |
2704 "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f" | 2624 "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f" |
2705 "%6d %6d %5d %5d %5d %8.2f %10d %10.3f" | 2625 "%6d %6d %5d %5d %5d %10d %10.3f" |
2706 "%10.3f %8d %10d %10d %10d\n", | 2626 "%10.3f %8d %10d %10d %10d\n", |
2707 cpi->common.current_video_frame, cpi->this_frame_target, | 2627 cpi->common.current_video_frame, cpi->rc.this_frame_target, |
2708 cpi->projected_frame_size, 0, | 2628 cpi->rc.projected_frame_size, 0, |
2709 (cpi->projected_frame_size - cpi->this_frame_target), | 2629 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target), |
2710 (int)cpi->total_target_vs_actual, | 2630 (int)cpi->rc.total_target_vs_actual, |
2711 (int)(cpi->oxcf.starting_buffer_level - cpi->bits_off_target), | 2631 (int)(cpi->oxcf.starting_buffer_level - cpi->rc.bits_off_target), |
2712 (int)cpi->total_actual_bits, cm->base_qindex, | 2632 (int)cpi->rc.total_actual_bits, cm->base_qindex, |
2713 vp9_convert_qindex_to_q(cm->base_qindex), | 2633 vp9_convert_qindex_to_q(cm->base_qindex), |
2714 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0, | 2634 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0, |
2715 vp9_convert_qindex_to_q(cpi->active_best_quality), | 2635 vp9_convert_qindex_to_q(cpi->rc.active_worst_quality), cpi->rc.avg_q, |
2716 vp9_convert_qindex_to_q(cpi->active_worst_quality), cpi->avg_q, | 2636 vp9_convert_qindex_to_q(cpi->rc.ni_av_qi), |
2717 vp9_convert_qindex_to_q(cpi->ni_av_qi), | |
2718 vp9_convert_qindex_to_q(cpi->cq_target_quality), | 2637 vp9_convert_qindex_to_q(cpi->cq_target_quality), |
2719 cpi->refresh_last_frame, cpi->refresh_golden_frame, | 2638 cpi->refresh_last_frame, cpi->refresh_golden_frame, |
2720 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->gfu_boost, | 2639 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost, |
2721 cpi->twopass.est_max_qcorrection_factor, (int)cpi->twopass.bits_left, | 2640 (int)cpi->twopass.bits_left, |
2722 cpi->twopass.total_left_stats.coded_error, | 2641 cpi->twopass.total_left_stats.coded_error, |
2723 (double)cpi->twopass.bits_left / | 2642 (double)cpi->twopass.bits_left / |
2724 (1 + cpi->twopass.total_left_stats.coded_error), | 2643 (1 + cpi->twopass.total_left_stats.coded_error), |
2725 cpi->tot_recode_hits, recon_err, cpi->kf_boost, cpi->kf_zeromotion_pct); | 2644 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost, |
| 2645 cpi->kf_zeromotion_pct); |
2726 | 2646 |
2727 fclose(f); | 2647 fclose(f); |
2728 | 2648 |
2729 if (0) { | 2649 if (0) { |
2730 FILE *const fmodes = fopen("Modes.stt", "a"); | 2650 FILE *const fmodes = fopen("Modes.stt", "a"); |
2731 int i; | 2651 int i; |
2732 | 2652 |
2733 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame, | 2653 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame, |
2734 cm->frame_type, cpi->refresh_golden_frame, | 2654 cm->frame_type, cpi->refresh_golden_frame, |
2735 cpi->refresh_alt_ref_frame); | 2655 cpi->refresh_alt_ref_frame); |
2736 | 2656 |
2737 for (i = 0; i < MAX_MODES; ++i) | 2657 for (i = 0; i < MAX_MODES; ++i) |
2738 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]); | 2658 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]); |
2739 for (i = 0; i < MAX_REFS; ++i) | 2659 for (i = 0; i < MAX_REFS; ++i) |
2740 fprintf(fmodes, "%5d ", cpi->sub8x8_mode_chosen_counts[i]); | 2660 fprintf(fmodes, "%5d ", cpi->sub8x8_mode_chosen_counts[i]); |
2741 | 2661 |
2742 fprintf(fmodes, "\n"); | 2662 fprintf(fmodes, "\n"); |
2743 | 2663 |
2744 fclose(fmodes); | 2664 fclose(fmodes); |
2745 } | 2665 } |
2746 } | 2666 } |
2747 #endif | 2667 #endif |
2748 | 2668 |
2749 static int pick_q_and_adjust_q_bounds(VP9_COMP *cpi, | 2669 static void encode_with_recode_loop(VP9_COMP *cpi, |
2750 int * bottom_index, int * top_index) { | 2670 size_t *size, |
2751 // Set an active best quality and if necessary active worst quality | 2671 uint8_t *dest, |
2752 int q = cpi->active_worst_quality; | 2672 int *q, |
| 2673 int bottom_index, |
| 2674 int top_index, |
| 2675 int frame_over_shoot_limit, |
| 2676 int frame_under_shoot_limit) { |
2753 VP9_COMMON *const cm = &cpi->common; | 2677 VP9_COMMON *const cm = &cpi->common; |
2754 | 2678 int loop_count = 0; |
2755 if (frame_is_intra_only(cm)) { | 2679 int loop = 0; |
2756 #if !CONFIG_MULTIPLE_ARF | 2680 int overshoot_seen = 0; |
2757 // Handle the special case for key frames forced when we have75 reached | 2681 int undershoot_seen = 0; |
2758 // the maximum key frame interval. Here force the Q to a range | 2682 int q_low = bottom_index, q_high = top_index; |
2759 // based on the ambient Q to reduce the risk of popping. | 2683 |
2760 if (cpi->this_key_frame_forced) { | 2684 do { |
2761 int delta_qindex; | 2685 vp9_clear_system_state(); // __asm emms; |
2762 int qindex = cpi->last_boosted_qindex; | 2686 |
2763 double last_boosted_q = vp9_convert_qindex_to_q(qindex); | 2687 vp9_set_quantizer(cpi, *q); |
2764 | 2688 |
2765 delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, | 2689 if (loop_count == 0) { |
2766 (last_boosted_q * 0.75)); | 2690 // Set up entropy context depending on frame type. The decoder mandates |
2767 | 2691 // the use of the default context, index 0, for keyframes and inter |
2768 cpi->active_best_quality = MAX(qindex + delta_qindex, | 2692 // frames where the error_resilient_mode or intra_only flag is set. For |
2769 cpi->best_quality); | 2693 // other inter-frames the encoder currently uses only two contexts; |
| 2694 // context 1 for ALTREF frames and context 0 for the others. |
| 2695 if (cm->frame_type == KEY_FRAME) { |
| 2696 vp9_setup_key_frame(cpi); |
| 2697 } else { |
| 2698 if (!cm->intra_only && !cm->error_resilient_mode) { |
| 2699 cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame; |
| 2700 } |
| 2701 vp9_setup_inter_frame(cpi); |
| 2702 } |
| 2703 } |
| 2704 |
| 2705 // Variance adaptive and in frame q adjustment experiments are mutually |
| 2706 // exclusive. |
| 2707 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 2708 vp9_vaq_frame_setup(cpi); |
| 2709 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) { |
| 2710 setup_in_frame_q_adj(cpi); |
| 2711 } |
| 2712 |
| 2713 // transform / motion compensation build reconstruction frame |
| 2714 |
| 2715 vp9_encode_frame(cpi); |
| 2716 |
| 2717 // Update the skip mb flag probabilities based on the distribution |
| 2718 // seen in the last encoder iteration. |
| 2719 // update_base_skip_probs(cpi); |
| 2720 |
| 2721 vp9_clear_system_state(); // __asm emms; |
| 2722 |
| 2723 // Dummy pack of the bitstream using up to date stats to get an |
| 2724 // accurate estimate of output frame size to determine if we need |
| 2725 // to recode. |
| 2726 vp9_save_coding_context(cpi); |
| 2727 cpi->dummy_packing = 1; |
| 2728 vp9_pack_bitstream(cpi, dest, size); |
| 2729 cpi->rc.projected_frame_size = (*size) << 3; |
| 2730 vp9_restore_coding_context(cpi); |
| 2731 |
| 2732 if (frame_over_shoot_limit == 0) |
| 2733 frame_over_shoot_limit = 1; |
| 2734 |
| 2735 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
| 2736 loop = 0; |
2770 } else { | 2737 } else { |
2771 int high = 5000; | 2738 // Special case handling for forced key frames |
2772 int low = 400; | 2739 if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { |
2773 double q_adj_factor = 1.0; | 2740 int last_q = *q; |
2774 double q_val; | 2741 int kf_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); |
2775 | 2742 |
2776 // Baseline value derived from cpi->active_worst_quality and kf boost | 2743 int high_err_target = cpi->ambient_err; |
2777 cpi->active_best_quality = get_active_quality(q, cpi->kf_boost, | 2744 int low_err_target = cpi->ambient_err >> 1; |
2778 low, high, | 2745 |
2779 kf_low_motion_minq, | 2746 // Prevent possible divide by zero error below for perfect KF |
2780 kf_high_motion_minq); | 2747 kf_err += !kf_err; |
2781 | 2748 |
2782 // Allow somewhat lower kf minq with small image formats. | 2749 // The key frame is not good enough or we can afford |
2783 if ((cm->width * cm->height) <= (352 * 288)) { | 2750 // to make it better without undue risk of popping. |
2784 q_adj_factor -= 0.25; | 2751 if ((kf_err > high_err_target && |
| 2752 cpi->rc.projected_frame_size <= frame_over_shoot_limit) || |
| 2753 (kf_err > low_err_target && |
| 2754 cpi->rc.projected_frame_size <= frame_under_shoot_limit)) { |
| 2755 // Lower q_high |
| 2756 q_high = *q > q_low ? *q - 1 : q_low; |
| 2757 |
| 2758 // Adjust Q |
| 2759 *q = ((*q) * high_err_target) / kf_err; |
| 2760 *q = MIN((*q), (q_high + q_low) >> 1); |
| 2761 } else if (kf_err < low_err_target && |
| 2762 cpi->rc.projected_frame_size >= frame_under_shoot_limit) { |
| 2763 // The key frame is much better than the previous frame |
| 2764 // Raise q_low |
| 2765 q_low = *q < q_high ? *q + 1 : q_high; |
| 2766 |
| 2767 // Adjust Q |
| 2768 *q = ((*q) * low_err_target) / kf_err; |
| 2769 *q = MIN((*q), (q_high + q_low + 1) >> 1); |
| 2770 } |
| 2771 |
| 2772 // Clamp Q to upper and lower limits: |
| 2773 *q = clamp(*q, q_low, q_high); |
| 2774 |
| 2775 loop = *q != last_q; |
| 2776 } else if (recode_loop_test( |
| 2777 cpi, frame_over_shoot_limit, frame_under_shoot_limit, |
| 2778 *q, top_index, bottom_index)) { |
| 2779 // Is the projected frame size out of range and are we allowed |
| 2780 // to attempt to recode. |
| 2781 int last_q = *q; |
| 2782 int retries = 0; |
| 2783 |
| 2784 // Frame size out of permitted range: |
| 2785 // Update correction factor & compute new Q to try... |
| 2786 |
| 2787 // Frame is too large |
| 2788 if (cpi->rc.projected_frame_size > cpi->rc.this_frame_target) { |
| 2789 // Raise Qlow as to at least the current value |
| 2790 q_low = *q < q_high ? *q + 1 : q_high; |
| 2791 |
| 2792 if (undershoot_seen || loop_count > 1) { |
| 2793 // Update rate_correction_factor unless |
| 2794 vp9_rc_update_rate_correction_factors(cpi, 1); |
| 2795 |
| 2796 *q = (q_high + q_low + 1) / 2; |
| 2797 } else { |
| 2798 // Update rate_correction_factor unless |
| 2799 vp9_rc_update_rate_correction_factors(cpi, 0); |
| 2800 |
| 2801 *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target, |
| 2802 bottom_index, top_index); |
| 2803 |
| 2804 while (*q < q_low && retries < 10) { |
| 2805 vp9_rc_update_rate_correction_factors(cpi, 0); |
| 2806 *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target, |
| 2807 bottom_index, top_index); |
| 2808 retries++; |
| 2809 } |
| 2810 } |
| 2811 |
| 2812 overshoot_seen = 1; |
| 2813 } else { |
| 2814 // Frame is too small |
| 2815 q_high = *q > q_low ? *q - 1 : q_low; |
| 2816 |
| 2817 if (overshoot_seen || loop_count > 1) { |
| 2818 vp9_rc_update_rate_correction_factors(cpi, 1); |
| 2819 *q = (q_high + q_low) / 2; |
| 2820 } else { |
| 2821 vp9_rc_update_rate_correction_factors(cpi, 0); |
| 2822 *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target, |
| 2823 bottom_index, top_index); |
| 2824 // Special case reset for qlow for constrained quality. |
| 2825 // This should only trigger where there is very substantial |
| 2826 // undershoot on a frame and the auto cq level is above |
| 2827 // the user passsed in value. |
| 2828 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && |
| 2829 *q < q_low) { |
| 2830 q_low = *q; |
| 2831 } |
| 2832 |
| 2833 while (*q > q_high && retries < 10) { |
| 2834 vp9_rc_update_rate_correction_factors(cpi, 0); |
| 2835 *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target, |
| 2836 bottom_index, top_index); |
| 2837 retries++; |
| 2838 } |
| 2839 } |
| 2840 |
| 2841 undershoot_seen = 1; |
| 2842 } |
| 2843 |
| 2844 // Clamp Q to upper and lower limits: |
| 2845 *q = clamp(*q, q_low, q_high); |
| 2846 |
| 2847 loop = *q != last_q; |
| 2848 } else { |
| 2849 loop = 0; |
2785 } | 2850 } |
2786 | 2851 } |
2787 // Make a further adjustment based on the kf zero motion measure. | 2852 |
2788 q_adj_factor += 0.05 - (0.001 * (double)cpi->kf_zeromotion_pct); | 2853 if (cpi->is_src_frame_alt_ref) |
2789 | 2854 loop = 0; |
2790 // Convert the adjustment factor to a qindex delta | 2855 |
2791 // on active_best_quality. | 2856 if (loop) { |
2792 q_val = vp9_convert_qindex_to_q(cpi->active_best_quality); | 2857 loop_count++; |
2793 cpi->active_best_quality += | 2858 |
2794 vp9_compute_qdelta(cpi, q_val, (q_val * q_adj_factor)); | 2859 #if CONFIG_INTERNAL_STATS |
2795 } | 2860 cpi->tot_recode_hits++; |
2796 #else | |
2797 double current_q; | |
2798 // Force the KF quantizer to be 30% of the active_worst_quality. | |
2799 current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality); | |
2800 cpi->active_best_quality = cpi->active_worst_quality | |
2801 + vp9_compute_qdelta(cpi, current_q, current_q * 0.3); | |
2802 #endif | 2861 #endif |
2803 } else if (!cpi->is_src_frame_alt_ref && | 2862 } |
2804 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { | 2863 } while (loop); |
2805 int high = 2000; | |
2806 int low = 400; | |
2807 | |
2808 // Use the lower of cpi->active_worst_quality and recent | |
2809 // average Q as basis for GF/ARF best Q limit unless last frame was | |
2810 // a key frame. | |
2811 if (cpi->frames_since_key > 1 && | |
2812 cpi->avg_frame_qindex < cpi->active_worst_quality) { | |
2813 q = cpi->avg_frame_qindex; | |
2814 } | |
2815 // For constrained quality dont allow Q less than the cq level | |
2816 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { | |
2817 if (q < cpi->cq_target_quality) | |
2818 q = cpi->cq_target_quality; | |
2819 if (cpi->frames_since_key > 1) { | |
2820 cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, | |
2821 low, high, | |
2822 afq_low_motion_minq, | |
2823 afq_high_motion_minq); | |
2824 } else { | |
2825 cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, | |
2826 low, high, | |
2827 gf_low_motion_minq, | |
2828 gf_high_motion_minq); | |
2829 } | |
2830 // Constrained quality use slightly lower active best. | |
2831 cpi->active_best_quality = cpi->active_best_quality * 15 / 16; | |
2832 | |
2833 } else if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { | |
2834 if (!cpi->refresh_alt_ref_frame) { | |
2835 cpi->active_best_quality = cpi->cq_target_quality; | |
2836 } else { | |
2837 if (cpi->frames_since_key > 1) { | |
2838 cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, | |
2839 low, high, | |
2840 afq_low_motion_minq, | |
2841 afq_high_motion_minq); | |
2842 } else { | |
2843 cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, | |
2844 low, high, | |
2845 gf_low_motion_minq, | |
2846 gf_high_motion_minq); | |
2847 } | |
2848 } | |
2849 } else { | |
2850 cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, | |
2851 low, high, | |
2852 gf_low_motion_minq, | |
2853 gf_high_motion_minq); | |
2854 } | |
2855 } else { | |
2856 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { | |
2857 cpi->active_best_quality = cpi->cq_target_quality; | |
2858 } else { | |
2859 #ifdef ONE_SHOT_Q_ESTIMATE | |
2860 #ifdef STRICT_ONE_SHOT_Q | |
2861 cpi->active_best_quality = q; | |
2862 #else | |
2863 cpi->active_best_quality = inter_minq[q]; | |
2864 #endif | |
2865 #else | |
2866 cpi->active_best_quality = inter_minq[q]; | |
2867 // 1-pass: for now, use the average Q for the active_best, if its lower | |
2868 // than active_worst. | |
2869 if (cpi->pass == 0 && (cpi->avg_frame_qindex < q)) | |
2870 cpi->active_best_quality = inter_minq[cpi->avg_frame_qindex]; | |
2871 #endif | |
2872 | |
2873 // For the constrained quality mode we don't want | |
2874 // q to fall below the cq level. | |
2875 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && | |
2876 (cpi->active_best_quality < cpi->cq_target_quality)) { | |
2877 // If we are strongly undershooting the target rate in the last | |
2878 // frames then use the user passed in cq value not the auto | |
2879 // cq value. | |
2880 if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth) | |
2881 cpi->active_best_quality = cpi->oxcf.cq_level; | |
2882 else | |
2883 cpi->active_best_quality = cpi->cq_target_quality; | |
2884 } | |
2885 } | |
2886 } | |
2887 | |
2888 // Clip the active best and worst quality values to limits | |
2889 if (cpi->active_worst_quality > cpi->worst_quality) | |
2890 cpi->active_worst_quality = cpi->worst_quality; | |
2891 | |
2892 if (cpi->active_best_quality < cpi->best_quality) | |
2893 cpi->active_best_quality = cpi->best_quality; | |
2894 | |
2895 if (cpi->active_best_quality > cpi->worst_quality) | |
2896 cpi->active_best_quality = cpi->worst_quality; | |
2897 | |
2898 if (cpi->active_worst_quality < cpi->active_best_quality) | |
2899 cpi->active_worst_quality = cpi->active_best_quality; | |
2900 | |
2901 // Limit Q range for the adaptive loop. | |
2902 if (cm->frame_type == KEY_FRAME && !cpi->this_key_frame_forced) { | |
2903 *top_index = | |
2904 (cpi->active_worst_quality + cpi->active_best_quality * 3) / 4; | |
2905 // If this is the first (key) frame in 1-pass, active best is the user | |
2906 // best-allowed, and leave the top_index to active_worst. | |
2907 if (cpi->pass == 0 && cpi->common.current_video_frame == 0) { | |
2908 cpi->active_best_quality = cpi->oxcf.best_allowed_q; | |
2909 *top_index = cpi->oxcf.worst_allowed_q; | |
2910 } | |
2911 } else if (!cpi->is_src_frame_alt_ref && | |
2912 (cpi->oxcf.end_usage != USAGE_STREAM_FROM_SERVER) && | |
2913 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { | |
2914 *top_index = | |
2915 (cpi->active_worst_quality + cpi->active_best_quality) / 2; | |
2916 } else { | |
2917 *top_index = cpi->active_worst_quality; | |
2918 } | |
2919 *bottom_index = cpi->active_best_quality; | |
2920 | |
2921 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { | |
2922 q = cpi->active_best_quality; | |
2923 // Special case code to try and match quality with forced key frames | |
2924 } else if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { | |
2925 q = cpi->last_boosted_qindex; | |
2926 } else { | |
2927 // Determine initial Q to try. | |
2928 if (cpi->pass == 0) { | |
2929 // 1-pass: for now, use per-frame-bw for target size of frame, scaled | |
2930 // by |x| for key frame. | |
2931 int scale = (cm->frame_type == KEY_FRAME) ? 5 : 1; | |
2932 q = vp9_regulate_q(cpi, scale * cpi->av_per_frame_bandwidth); | |
2933 } else { | |
2934 q = vp9_regulate_q(cpi, cpi->this_frame_target); | |
2935 } | |
2936 if (q > *top_index) | |
2937 q = *top_index; | |
2938 } | |
2939 | |
2940 return q; | |
2941 } | 2864 } |
| 2865 |
2942 static void encode_frame_to_data_rate(VP9_COMP *cpi, | 2866 static void encode_frame_to_data_rate(VP9_COMP *cpi, |
2943 unsigned long *size, | 2867 size_t *size, |
2944 unsigned char *dest, | 2868 uint8_t *dest, |
2945 unsigned int *frame_flags) { | 2869 unsigned int *frame_flags) { |
2946 VP9_COMMON *const cm = &cpi->common; | 2870 VP9_COMMON *const cm = &cpi->common; |
2947 TX_SIZE t; | 2871 TX_SIZE t; |
2948 int q; | 2872 int q; |
2949 int frame_over_shoot_limit; | 2873 int frame_over_shoot_limit; |
2950 int frame_under_shoot_limit; | 2874 int frame_under_shoot_limit; |
2951 | |
2952 int loop = 0; | |
2953 int loop_count; | |
2954 | |
2955 int q_low; | |
2956 int q_high; | |
2957 | |
2958 int top_index; | 2875 int top_index; |
| 2876 int top_index_prop; |
2959 int bottom_index; | 2877 int bottom_index; |
2960 int active_worst_qchanged = 0; | |
2961 | |
2962 int overshoot_seen = 0; | |
2963 int undershoot_seen = 0; | |
2964 | 2878 |
2965 SPEED_FEATURES *const sf = &cpi->sf; | 2879 SPEED_FEATURES *const sf = &cpi->sf; |
2966 unsigned int max_mv_def = MIN(cpi->common.width, cpi->common.height); | 2880 unsigned int max_mv_def = MIN(cpi->common.width, cpi->common.height); |
2967 struct segmentation *const seg = &cm->seg; | 2881 struct segmentation *const seg = &cm->seg; |
2968 | 2882 |
2969 /* Scale the source buffer, if required. */ | 2883 /* Scale the source buffer, if required. */ |
2970 if (cm->mi_cols * 8 != cpi->un_scaled_source->y_width || | 2884 if (cm->mi_cols * 8 != cpi->un_scaled_source->y_width || |
2971 cm->mi_rows * 8 != cpi->un_scaled_source->y_height) { | 2885 cm->mi_rows * 8 != cpi->un_scaled_source->y_height) { |
2972 scale_and_extend_frame(cpi->un_scaled_source, &cpi->scaled_source); | 2886 scale_and_extend_frame(cpi->un_scaled_source, &cpi->scaled_source); |
2973 cpi->Source = &cpi->scaled_source; | 2887 cpi->Source = &cpi->scaled_source; |
2974 } else { | 2888 } else { |
2975 cpi->Source = cpi->un_scaled_source; | 2889 cpi->Source = cpi->un_scaled_source; |
2976 } | 2890 } |
2977 scale_references(cpi); | 2891 scale_references(cpi); |
2978 | 2892 |
2979 // Clear down mmx registers to allow floating point in what follows. | 2893 // Clear down mmx registers to allow floating point in what follows. |
2980 vp9_clear_system_state(); | 2894 vp9_clear_system_state(); |
2981 | 2895 |
2982 // For an alt ref frame in 2 pass we skip the call to the second | 2896 // For an alt ref frame in 2 pass we skip the call to the second |
2983 // pass function that sets the target bandwidth so we must set it here. | 2897 // pass function that sets the target bandwidth so we must set it here. |
2984 if (cpi->refresh_alt_ref_frame) { | 2898 if (cpi->refresh_alt_ref_frame) { |
2985 // Set a per frame bit target for the alt ref frame. | 2899 // Set a per frame bit target for the alt ref frame. |
2986 cpi->per_frame_bandwidth = cpi->twopass.gf_bits; | 2900 cpi->rc.per_frame_bandwidth = cpi->twopass.gf_bits; |
2987 // Set a per second target bitrate. | 2901 // Set a per second target bitrate. |
2988 cpi->target_bandwidth = (int)(cpi->twopass.gf_bits * cpi->output_framerate); | 2902 cpi->target_bandwidth = (int)(cpi->twopass.gf_bits * cpi->output_framerate); |
2989 } | 2903 } |
2990 | 2904 |
2991 // Clear zbin over-quant value and mode boost values. | 2905 // Clear zbin over-quant value and mode boost values. |
2992 cpi->zbin_mode_boost = 0; | 2906 cpi->zbin_mode_boost = 0; |
2993 | 2907 |
2994 // Enable or disable mode based tweaking of the zbin. | 2908 // Enable or disable mode based tweaking of the zbin. |
2995 // For 2 pass only used where GF/ARF prediction quality | 2909 // For 2 pass only used where GF/ARF prediction quality |
2996 // is above a threshold. | 2910 // is above a threshold. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3064 } | 2978 } |
3065 | 2979 |
3066 // Configure experimental use of segmentation for enhanced coding of | 2980 // Configure experimental use of segmentation for enhanced coding of |
3067 // static regions if indicated. | 2981 // static regions if indicated. |
3068 // Only allowed in second pass of two pass (as requires lagged coding) | 2982 // Only allowed in second pass of two pass (as requires lagged coding) |
3069 // and if the relevant speed feature flag is set. | 2983 // and if the relevant speed feature flag is set. |
3070 if ((cpi->pass == 2) && (cpi->sf.static_segmentation)) { | 2984 if ((cpi->pass == 2) && (cpi->sf.static_segmentation)) { |
3071 configure_static_seg_features(cpi); | 2985 configure_static_seg_features(cpi); |
3072 } | 2986 } |
3073 | 2987 |
3074 // Decide how big to make the frame. | |
3075 vp9_pick_frame_size(cpi); | |
3076 | |
3077 vp9_clear_system_state(); | 2988 vp9_clear_system_state(); |
3078 | 2989 |
3079 q = pick_q_and_adjust_q_bounds(cpi, &bottom_index, &top_index); | |
3080 | |
3081 q_high = top_index; | |
3082 q_low = bottom_index; | |
3083 | |
3084 vp9_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, | |
3085 &frame_over_shoot_limit); | |
3086 | |
3087 #if CONFIG_MULTIPLE_ARF | |
3088 // Force the quantizer determined by the coding order pattern. | |
3089 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && | |
3090 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { | |
3091 double new_q; | |
3092 double current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality); | |
3093 int level = cpi->this_frame_weight; | |
3094 assert(level >= 0); | |
3095 | |
3096 // Set quantizer steps at 10% increments. | |
3097 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); | |
3098 q = cpi->active_worst_quality + vp9_compute_qdelta(cpi, current_q, new_q); | |
3099 | |
3100 bottom_index = q; | |
3101 top_index = q; | |
3102 q_low = q; | |
3103 q_high = q; | |
3104 | |
3105 printf("frame:%d q:%d\n", cm->current_video_frame, q); | |
3106 } | |
3107 #endif | |
3108 | |
3109 loop_count = 0; | |
3110 vp9_zero(cpi->rd_tx_select_threshes); | 2990 vp9_zero(cpi->rd_tx_select_threshes); |
3111 | 2991 |
3112 if (!frame_is_intra_only(cm)) { | |
3113 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; | |
3114 /* TODO: Decide this more intelligently */ | |
3115 cm->allow_high_precision_mv = q < HIGH_PRECISION_MV_QTHRESH; | |
3116 set_mvcost(cpi); | |
3117 } | |
3118 | |
3119 #if CONFIG_VP9_POSTPROC | 2992 #if CONFIG_VP9_POSTPROC |
3120 | |
3121 if (cpi->oxcf.noise_sensitivity > 0) { | 2993 if (cpi->oxcf.noise_sensitivity > 0) { |
3122 int l = 0; | 2994 int l = 0; |
3123 | |
3124 switch (cpi->oxcf.noise_sensitivity) { | 2995 switch (cpi->oxcf.noise_sensitivity) { |
3125 case 1: | 2996 case 1: |
3126 l = 20; | 2997 l = 20; |
3127 break; | 2998 break; |
3128 case 2: | 2999 case 2: |
3129 l = 40; | 3000 l = 40; |
3130 break; | 3001 break; |
3131 case 3: | 3002 case 3: |
3132 l = 60; | 3003 l = 60; |
3133 break; | 3004 break; |
3134 case 4: | 3005 case 4: |
3135 case 5: | 3006 case 5: |
3136 l = 100; | 3007 l = 100; |
3137 break; | 3008 break; |
3138 case 6: | 3009 case 6: |
3139 l = 150; | 3010 l = 150; |
3140 break; | 3011 break; |
3141 } | 3012 } |
3142 | |
3143 vp9_denoise(cpi->Source, cpi->Source, l); | 3013 vp9_denoise(cpi->Source, cpi->Source, l); |
3144 } | 3014 } |
3145 | |
3146 #endif | 3015 #endif |
3147 | 3016 |
3148 #ifdef OUTPUT_YUV_SRC | 3017 #ifdef OUTPUT_YUV_SRC |
3149 vp9_write_yuv_frame(cpi->Source); | 3018 vp9_write_yuv_frame(cpi->Source); |
3150 #endif | 3019 #endif |
3151 | 3020 |
3152 do { | 3021 // Decide how big to make the frame. |
3153 vp9_clear_system_state(); // __asm emms; | 3022 vp9_rc_pick_frame_size_target(cpi); |
3154 | 3023 |
3155 vp9_set_quantizer(cpi, q); | 3024 // Decide frame size bounds |
| 3025 vp9_rc_compute_frame_size_bounds(cpi, cpi->rc.this_frame_target, |
| 3026 &frame_under_shoot_limit, |
| 3027 &frame_over_shoot_limit); |
3156 | 3028 |
3157 if (loop_count == 0) { | 3029 // Decide q and q bounds |
3158 // Set up entropy context depending on frame type. The decoder mandates | 3030 q = vp9_rc_pick_q_and_adjust_q_bounds(cpi, |
3159 // the use of the default context, index 0, for keyframes and inter | 3031 &bottom_index, |
3160 // frames where the error_resilient_mode or intra_only flag is set. For | 3032 &top_index, |
3161 // other inter-frames the encoder currently uses only two contexts; | 3033 &top_index_prop); |
3162 // context 1 for ALTREF frames and context 0 for the others. | |
3163 if (cm->frame_type == KEY_FRAME) { | |
3164 vp9_setup_key_frame(cpi); | |
3165 } else { | |
3166 if (!cm->intra_only && !cm->error_resilient_mode) { | |
3167 cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame; | |
3168 } | |
3169 vp9_setup_inter_frame(cpi); | |
3170 } | |
3171 } | |
3172 | 3034 |
3173 if (cpi->sf.variance_adaptive_quantization) { | 3035 if (!frame_is_intra_only(cm)) { |
3174 vp9_vaq_frame_setup(cpi); | 3036 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; |
3175 } | 3037 /* TODO: Decide this more intelligently */ |
| 3038 set_high_precision_mv(cpi, (q < HIGH_PRECISION_MV_QTHRESH)); |
| 3039 } |
3176 | 3040 |
3177 // transform / motion compensation build reconstruction frame | 3041 encode_with_recode_loop(cpi, |
3178 | 3042 size, |
3179 vp9_encode_frame(cpi); | 3043 dest, |
3180 | 3044 &q, |
3181 // Update the skip mb flag probabilities based on the distribution | 3045 bottom_index, |
3182 // seen in the last encoder iteration. | 3046 top_index, |
3183 // update_base_skip_probs(cpi); | 3047 frame_over_shoot_limit, |
3184 | 3048 frame_under_shoot_limit); |
3185 vp9_clear_system_state(); // __asm emms; | |
3186 | |
3187 // Dummy pack of the bitstream using up to date stats to get an | |
3188 // accurate estimate of output frame size to determine if we need | |
3189 // to recode. | |
3190 vp9_save_coding_context(cpi); | |
3191 cpi->dummy_packing = 1; | |
3192 vp9_pack_bitstream(cpi, dest, size); | |
3193 cpi->projected_frame_size = (*size) << 3; | |
3194 vp9_restore_coding_context(cpi); | |
3195 | |
3196 if (frame_over_shoot_limit == 0) | |
3197 frame_over_shoot_limit = 1; | |
3198 active_worst_qchanged = 0; | |
3199 | |
3200 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { | |
3201 loop = 0; | |
3202 } else { | |
3203 // Special case handling for forced key frames | |
3204 if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { | |
3205 int last_q = q; | |
3206 int kf_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); | |
3207 | |
3208 int high_err_target = cpi->ambient_err; | |
3209 int low_err_target = cpi->ambient_err >> 1; | |
3210 | |
3211 // Prevent possible divide by zero error below for perfect KF | |
3212 kf_err += !kf_err; | |
3213 | |
3214 // The key frame is not good enough or we can afford | |
3215 // to make it better without undue risk of popping. | |
3216 if ((kf_err > high_err_target && | |
3217 cpi->projected_frame_size <= frame_over_shoot_limit) || | |
3218 (kf_err > low_err_target && | |
3219 cpi->projected_frame_size <= frame_under_shoot_limit)) { | |
3220 // Lower q_high | |
3221 q_high = q > q_low ? q - 1 : q_low; | |
3222 | |
3223 // Adjust Q | |
3224 q = (q * high_err_target) / kf_err; | |
3225 q = MIN(q, (q_high + q_low) >> 1); | |
3226 } else if (kf_err < low_err_target && | |
3227 cpi->projected_frame_size >= frame_under_shoot_limit) { | |
3228 // The key frame is much better than the previous frame | |
3229 // Raise q_low | |
3230 q_low = q < q_high ? q + 1 : q_high; | |
3231 | |
3232 // Adjust Q | |
3233 q = (q * low_err_target) / kf_err; | |
3234 q = MIN(q, (q_high + q_low + 1) >> 1); | |
3235 } | |
3236 | |
3237 // Clamp Q to upper and lower limits: | |
3238 q = clamp(q, q_low, q_high); | |
3239 | |
3240 loop = q != last_q; | |
3241 } else if (recode_loop_test( | |
3242 cpi, frame_over_shoot_limit, frame_under_shoot_limit, | |
3243 q, top_index, bottom_index)) { | |
3244 // Is the projected frame size out of range and are we allowed | |
3245 // to attempt to recode. | |
3246 int last_q = q; | |
3247 int retries = 0; | |
3248 | |
3249 // Frame size out of permitted range: | |
3250 // Update correction factor & compute new Q to try... | |
3251 | |
3252 // Frame is too large | |
3253 if (cpi->projected_frame_size > cpi->this_frame_target) { | |
3254 // Raise Qlow as to at least the current value | |
3255 q_low = q < q_high ? q + 1 : q_high; | |
3256 | |
3257 if (undershoot_seen || loop_count > 1) { | |
3258 // Update rate_correction_factor unless | |
3259 // cpi->active_worst_quality has changed. | |
3260 if (!active_worst_qchanged) | |
3261 vp9_update_rate_correction_factors(cpi, 1); | |
3262 | |
3263 q = (q_high + q_low + 1) / 2; | |
3264 } else { | |
3265 // Update rate_correction_factor unless | |
3266 // cpi->active_worst_quality has changed. | |
3267 if (!active_worst_qchanged) | |
3268 vp9_update_rate_correction_factors(cpi, 0); | |
3269 | |
3270 q = vp9_regulate_q(cpi, cpi->this_frame_target); | |
3271 | |
3272 while (q < q_low && retries < 10) { | |
3273 vp9_update_rate_correction_factors(cpi, 0); | |
3274 q = vp9_regulate_q(cpi, cpi->this_frame_target); | |
3275 retries++; | |
3276 } | |
3277 } | |
3278 | |
3279 overshoot_seen = 1; | |
3280 } else { | |
3281 // Frame is too small | |
3282 q_high = q > q_low ? q - 1 : q_low; | |
3283 | |
3284 if (overshoot_seen || loop_count > 1) { | |
3285 // Update rate_correction_factor unless | |
3286 // cpi->active_worst_quality has changed. | |
3287 if (!active_worst_qchanged) | |
3288 vp9_update_rate_correction_factors(cpi, 1); | |
3289 | |
3290 q = (q_high + q_low) / 2; | |
3291 } else { | |
3292 // Update rate_correction_factor unless | |
3293 // cpi->active_worst_quality has changed. | |
3294 if (!active_worst_qchanged) | |
3295 vp9_update_rate_correction_factors(cpi, 0); | |
3296 | |
3297 q = vp9_regulate_q(cpi, cpi->this_frame_target); | |
3298 | |
3299 // Special case reset for qlow for constrained quality. | |
3300 // This should only trigger where there is very substantial | |
3301 // undershoot on a frame and the auto cq level is above | |
3302 // the user passsed in value. | |
3303 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && q < q_low) { | |
3304 q_low = q; | |
3305 } | |
3306 | |
3307 while (q > q_high && retries < 10) { | |
3308 vp9_update_rate_correction_factors(cpi, 0); | |
3309 q = vp9_regulate_q(cpi, cpi->this_frame_target); | |
3310 retries++; | |
3311 } | |
3312 } | |
3313 | |
3314 undershoot_seen = 1; | |
3315 } | |
3316 | |
3317 // Clamp Q to upper and lower limits: | |
3318 q = clamp(q, q_low, q_high); | |
3319 | |
3320 loop = q != last_q; | |
3321 } else { | |
3322 loop = 0; | |
3323 } | |
3324 } | |
3325 | |
3326 if (cpi->is_src_frame_alt_ref) | |
3327 loop = 0; | |
3328 | |
3329 if (loop) { | |
3330 loop_count++; | |
3331 | |
3332 #if CONFIG_INTERNAL_STATS | |
3333 cpi->tot_recode_hits++; | |
3334 #endif | |
3335 } | |
3336 } while (loop); | |
3337 | 3049 |
3338 // Special case code to reduce pulsing when key frames are forced at a | 3050 // Special case code to reduce pulsing when key frames are forced at a |
3339 // fixed interval. Note the reconstruction error if it is the frame before | 3051 // fixed interval. Note the reconstruction error if it is the frame before |
3340 // the force key frame | 3052 // the force key frame |
3341 if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) { | 3053 if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) { |
3342 cpi->ambient_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); | 3054 cpi->ambient_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); |
3343 } | 3055 } |
3344 | 3056 |
3345 if (cm->frame_type == KEY_FRAME) | 3057 if (cm->frame_type == KEY_FRAME) |
3346 cpi->refresh_last_frame = 1; | 3058 cpi->refresh_last_frame = 1; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3384 if (!cpi->common.error_resilient_mode && | 3096 if (!cpi->common.error_resilient_mode && |
3385 !cpi->common.frame_parallel_decoding_mode) { | 3097 !cpi->common.frame_parallel_decoding_mode) { |
3386 vp9_adapt_coef_probs(&cpi->common); | 3098 vp9_adapt_coef_probs(&cpi->common); |
3387 } | 3099 } |
3388 | 3100 |
3389 if (!frame_is_intra_only(&cpi->common)) { | 3101 if (!frame_is_intra_only(&cpi->common)) { |
3390 FRAME_COUNTS *counts = &cpi->common.counts; | 3102 FRAME_COUNTS *counts = &cpi->common.counts; |
3391 | 3103 |
3392 vp9_copy(counts->y_mode, cpi->y_mode_count); | 3104 vp9_copy(counts->y_mode, cpi->y_mode_count); |
3393 vp9_copy(counts->uv_mode, cpi->y_uv_mode_count); | 3105 vp9_copy(counts->uv_mode, cpi->y_uv_mode_count); |
3394 vp9_copy(counts->partition, cpi->partition_count); | |
3395 vp9_copy(counts->intra_inter, cpi->intra_inter_count); | 3106 vp9_copy(counts->intra_inter, cpi->intra_inter_count); |
3396 vp9_copy(counts->comp_inter, cpi->comp_inter_count); | 3107 vp9_copy(counts->comp_inter, cpi->comp_inter_count); |
3397 vp9_copy(counts->single_ref, cpi->single_ref_count); | 3108 vp9_copy(counts->single_ref, cpi->single_ref_count); |
3398 vp9_copy(counts->comp_ref, cpi->comp_ref_count); | 3109 vp9_copy(counts->comp_ref, cpi->comp_ref_count); |
3399 counts->mv = cpi->NMVcount; | 3110 counts->mv = cpi->NMVcount; |
3400 if (!cpi->common.error_resilient_mode && | 3111 if (!cpi->common.error_resilient_mode && |
3401 !cpi->common.frame_parallel_decoding_mode) { | 3112 !cpi->common.frame_parallel_decoding_mode) { |
3402 vp9_adapt_mode_probs(&cpi->common); | 3113 vp9_adapt_mode_probs(&cpi->common); |
3403 vp9_adapt_mv_probs(&cpi->common, cpi->common.allow_high_precision_mv); | 3114 vp9_adapt_mv_probs(&cpi->common, cpi->common.allow_high_precision_mv); |
3404 } | 3115 } |
3405 } | 3116 } |
3406 | 3117 |
3407 #ifdef ENTROPY_STATS | 3118 #ifdef ENTROPY_STATS |
3408 vp9_update_mode_context_stats(cpi); | 3119 vp9_update_mode_context_stats(cpi); |
3409 #endif | 3120 #endif |
3410 | 3121 |
3411 /* Move storing frame_type out of the above loop since it is also | 3122 /* Move storing frame_type out of the above loop since it is also |
3412 * needed in motion search besides loopfilter */ | 3123 * needed in motion search besides loopfilter */ |
3413 cm->last_frame_type = cm->frame_type; | 3124 cm->last_frame_type = cm->frame_type; |
3414 | 3125 |
3415 // Update rate control heuristics | 3126 vp9_rc_postencode_update(cpi, *size, top_index_prop); |
3416 cpi->total_byte_count += (*size); | |
3417 cpi->projected_frame_size = (*size) << 3; | |
3418 | |
3419 // Post encode loop adjustment of Q prediction. | |
3420 if (!active_worst_qchanged) | |
3421 vp9_update_rate_correction_factors(cpi, (cpi->sf.recode_loop) ? 2 : 0); | |
3422 | |
3423 cpi->last_q[cm->frame_type] = cm->base_qindex; | |
3424 | |
3425 // Keep record of last boosted (KF/KF/ARF) Q value. | |
3426 // If the current frame is coded at a lower Q then we also update it. | |
3427 // If all mbs in this group are skipped only update if the Q value is | |
3428 // better than that already stored. | |
3429 // This is used to help set quality in forced key frames to reduce popping | |
3430 if ((cm->base_qindex < cpi->last_boosted_qindex) || | |
3431 ((cpi->static_mb_pct < 100) && | |
3432 ((cm->frame_type == KEY_FRAME) || | |
3433 cpi->refresh_alt_ref_frame || | |
3434 (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)))) { | |
3435 cpi->last_boosted_qindex = cm->base_qindex; | |
3436 } | |
3437 | |
3438 if (cm->frame_type == KEY_FRAME) { | |
3439 vp9_adjust_key_frame_context(cpi); | |
3440 } | |
3441 | |
3442 // Keep a record of ambient average Q. | |
3443 if (cm->frame_type != KEY_FRAME) | |
3444 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + | |
3445 cm->base_qindex) >> 2; | |
3446 | |
3447 // Keep a record from which we can calculate the average Q excluding GF | |
3448 // updates and key frames. | |
3449 if (cm->frame_type != KEY_FRAME && | |
3450 !cpi->refresh_golden_frame && | |
3451 !cpi->refresh_alt_ref_frame) { | |
3452 cpi->ni_frames++; | |
3453 cpi->tot_q += vp9_convert_qindex_to_q(q); | |
3454 cpi->avg_q = cpi->tot_q / (double)cpi->ni_frames; | |
3455 | |
3456 // Calculate the average Q for normal inter frames (not key or GFU frames). | |
3457 cpi->ni_tot_qi += q; | |
3458 cpi->ni_av_qi = cpi->ni_tot_qi / cpi->ni_frames; | |
3459 } | |
3460 | |
3461 // Update the buffer level variable. | |
3462 // Non-viewable frames are a special case and are treated as pure overhead. | |
3463 if (!cm->show_frame) | |
3464 cpi->bits_off_target -= cpi->projected_frame_size; | |
3465 else | |
3466 cpi->bits_off_target += cpi->av_per_frame_bandwidth - | |
3467 cpi->projected_frame_size; | |
3468 | |
3469 // Clip the buffer level at the maximum buffer size | |
3470 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) | |
3471 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size; | |
3472 | |
3473 // Rolling monitors of whether we are over or underspending used to help | |
3474 // regulate min and Max Q in two pass. | |
3475 if (cm->frame_type != KEY_FRAME) { | |
3476 cpi->rolling_target_bits = | |
3477 ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4; | |
3478 cpi->rolling_actual_bits = | |
3479 ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4; | |
3480 cpi->long_rolling_target_bits = | |
3481 ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32; | |
3482 cpi->long_rolling_actual_bits = | |
3483 ((cpi->long_rolling_actual_bits * 31) + | |
3484 cpi->projected_frame_size + 16) / 32; | |
3485 } | |
3486 | |
3487 // Actual bits spent | |
3488 cpi->total_actual_bits += cpi->projected_frame_size; | |
3489 | |
3490 // Debug stats | |
3491 cpi->total_target_vs_actual += (cpi->this_frame_target - | |
3492 cpi->projected_frame_size); | |
3493 | |
3494 cpi->buffer_level = cpi->bits_off_target; | |
3495 | |
3496 #ifndef DISABLE_RC_LONG_TERM_MEM | |
3497 // Update bits left to the kf and gf groups to account for overshoot or | |
3498 // undershoot on these frames | |
3499 if (cm->frame_type == KEY_FRAME) { | |
3500 cpi->twopass.kf_group_bits += cpi->this_frame_target - | |
3501 cpi->projected_frame_size; | |
3502 | |
3503 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); | |
3504 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { | |
3505 cpi->twopass.gf_group_bits += cpi->this_frame_target - | |
3506 cpi->projected_frame_size; | |
3507 | |
3508 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); | |
3509 } | |
3510 #endif | |
3511 | 3127 |
3512 #if 0 | 3128 #if 0 |
3513 output_frame_level_debug_stats(cpi); | 3129 output_frame_level_debug_stats(cpi); |
3514 #endif | 3130 #endif |
3515 if (cpi->refresh_golden_frame == 1) | 3131 if (cpi->refresh_golden_frame == 1) |
3516 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN; | 3132 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN; |
3517 else | 3133 else |
3518 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN; | 3134 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN; |
3519 | 3135 |
3520 if (cpi->refresh_alt_ref_frame == 1) | 3136 if (cpi->refresh_alt_ref_frame == 1) |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3621 // Don't increment frame counters if this was an altref buffer | 3237 // Don't increment frame counters if this was an altref buffer |
3622 // update not a real frame | 3238 // update not a real frame |
3623 ++cm->current_video_frame; | 3239 ++cm->current_video_frame; |
3624 ++cpi->frames_since_key; | 3240 ++cpi->frames_since_key; |
3625 } | 3241 } |
3626 // restore prev_mi | 3242 // restore prev_mi |
3627 cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1; | 3243 cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1; |
3628 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1; | 3244 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1; |
3629 } | 3245 } |
3630 | 3246 |
3631 static void Pass2Encode(VP9_COMP *cpi, unsigned long *size, | 3247 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest, |
3632 unsigned char *dest, unsigned int *frame_flags) { | 3248 unsigned int *frame_flags) { |
| 3249 encode_frame_to_data_rate(cpi, size, dest, frame_flags); |
| 3250 } |
| 3251 |
| 3252 static void Pass1Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest, |
| 3253 unsigned int *frame_flags) { |
| 3254 (void) size; |
| 3255 (void) dest; |
| 3256 (void) frame_flags; |
| 3257 |
| 3258 vp9_set_quantizer(cpi, find_fp_qindex()); |
| 3259 vp9_first_pass(cpi); |
| 3260 } |
| 3261 |
| 3262 static void Pass2Encode(VP9_COMP *cpi, size_t *size, |
| 3263 uint8_t *dest, unsigned int *frame_flags) { |
3633 cpi->enable_encode_breakout = 1; | 3264 cpi->enable_encode_breakout = 1; |
3634 | 3265 |
3635 if (!cpi->refresh_alt_ref_frame) | 3266 if (!cpi->refresh_alt_ref_frame) |
3636 vp9_second_pass(cpi); | 3267 vp9_second_pass(cpi); |
3637 | 3268 |
3638 encode_frame_to_data_rate(cpi, size, dest, frame_flags); | 3269 encode_frame_to_data_rate(cpi, size, dest, frame_flags); |
3639 // vp9_print_modes_and_motion_vectors(&cpi->common, "encode.stt"); | 3270 // vp9_print_modes_and_motion_vectors(&cpi->common, "encode.stt"); |
3640 #ifdef DISABLE_RC_LONG_TERM_MEM | |
3641 cpi->twopass.bits_left -= cpi->this_frame_target; | |
3642 #else | |
3643 cpi->twopass.bits_left -= 8 * *size; | |
3644 #endif | |
3645 | 3271 |
3646 if (!cpi->refresh_alt_ref_frame) { | 3272 vp9_twopass_postencode_update(cpi, *size); |
3647 double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate; | |
3648 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth | |
3649 * cpi->oxcf.two_pass_vbrmin_section | |
3650 / 100); | |
3651 | |
3652 if (two_pass_min_rate < lower_bounds_min_rate) | |
3653 two_pass_min_rate = lower_bounds_min_rate; | |
3654 | |
3655 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate | |
3656 / cpi->oxcf.framerate); | |
3657 } | |
3658 } | 3273 } |
3659 | 3274 |
3660 static void check_initial_width(VP9_COMP *cpi, YV12_BUFFER_CONFIG *sd) { | 3275 static void check_initial_width(VP9_COMP *cpi, YV12_BUFFER_CONFIG *sd) { |
3661 VP9_COMMON *cm = &cpi->common; | 3276 VP9_COMMON *const cm = &cpi->common; |
3662 if (!cpi->initial_width) { | 3277 if (!cpi->initial_width) { |
3663 // TODO(jkoleszar): Support 1/4 subsampling? | 3278 // TODO(agrange) Subsampling defaults to assuming sampled chroma. |
3664 cm->subsampling_x = (sd != NULL) && sd->uv_width < sd->y_width; | 3279 cm->subsampling_x = sd != NULL ? (sd->uv_width < sd->y_width) : 1; |
3665 cm->subsampling_y = (sd != NULL) && sd->uv_height < sd->y_height; | 3280 cm->subsampling_y = sd != NULL ? (sd->uv_height < sd->y_height) : 1; |
3666 alloc_raw_frame_buffers(cpi); | 3281 alloc_raw_frame_buffers(cpi); |
3667 | |
3668 cpi->initial_width = cm->width; | 3282 cpi->initial_width = cm->width; |
3669 cpi->initial_height = cm->height; | 3283 cpi->initial_height = cm->height; |
3670 } | 3284 } |
3671 } | 3285 } |
3672 | 3286 |
3673 | 3287 |
3674 int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags, | 3288 int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags, |
3675 YV12_BUFFER_CONFIG *sd, int64_t time_stamp, | 3289 YV12_BUFFER_CONFIG *sd, int64_t time_stamp, |
3676 int64_t end_time) { | 3290 int64_t end_time) { |
3677 VP9_COMP *cpi = (VP9_COMP *) ptr; | 3291 VP9_COMP *cpi = (VP9_COMP *) ptr; |
(...skipping 26 matching lines...) Expand all Loading... |
3704 } | 3318 } |
3705 | 3319 |
3706 #if CONFIG_MULTIPLE_ARF | 3320 #if CONFIG_MULTIPLE_ARF |
3707 int is_next_frame_arf(VP9_COMP *cpi) { | 3321 int is_next_frame_arf(VP9_COMP *cpi) { |
3708 // Negative entry in frame_coding_order indicates an ARF at this position. | 3322 // Negative entry in frame_coding_order indicates an ARF at this position. |
3709 return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0; | 3323 return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0; |
3710 } | 3324 } |
3711 #endif | 3325 #endif |
3712 | 3326 |
3713 int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags, | 3327 int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags, |
3714 unsigned long *size, unsigned char *dest, | 3328 size_t *size, uint8_t *dest, |
3715 int64_t *time_stamp, int64_t *time_end, int flush) { | 3329 int64_t *time_stamp, int64_t *time_end, int flush) { |
3716 VP9_COMP *cpi = (VP9_COMP *) ptr; | 3330 VP9_COMP *cpi = (VP9_COMP *) ptr; |
3717 VP9_COMMON *cm = &cpi->common; | 3331 VP9_COMMON *cm = &cpi->common; |
3718 struct vpx_usec_timer cmptimer; | 3332 struct vpx_usec_timer cmptimer; |
3719 YV12_BUFFER_CONFIG *force_src_buffer = NULL; | 3333 YV12_BUFFER_CONFIG *force_src_buffer = NULL; |
3720 int i; | 3334 int i; |
3721 // FILE *fp_out = fopen("enc_frame_type.txt", "a"); | 3335 // FILE *fp_out = fopen("enc_frame_type.txt", "a"); |
3722 | 3336 |
3723 if (!cpi) | 3337 if (!cpi) |
3724 return -1; | 3338 return -1; |
3725 | 3339 |
3726 vpx_usec_timer_start(&cmptimer); | 3340 vpx_usec_timer_start(&cmptimer); |
3727 | 3341 |
3728 cpi->source = NULL; | 3342 cpi->source = NULL; |
3729 | 3343 |
3730 cpi->common.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV; | 3344 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV); |
3731 set_mvcost(cpi); | |
3732 | 3345 |
3733 // Should we code an alternate reference frame. | 3346 // Should we code an alternate reference frame. |
3734 if (cpi->oxcf.play_alternate && cpi->source_alt_ref_pending) { | 3347 if (cpi->oxcf.play_alternate && cpi->source_alt_ref_pending) { |
3735 int frames_to_arf; | 3348 int frames_to_arf; |
3736 | 3349 |
3737 #if CONFIG_MULTIPLE_ARF | 3350 #if CONFIG_MULTIPLE_ARF |
3738 assert(!cpi->multi_arf_enabled || | 3351 assert(!cpi->multi_arf_enabled || |
3739 cpi->frame_coding_order[cpi->sequence_number] < 0); | 3352 cpi->frame_coding_order[cpi->sequence_number] < 0); |
3740 | 3353 |
3741 if (cpi->multi_arf_enabled && (cpi->pass == 2)) | 3354 if (cpi->multi_arf_enabled && (cpi->pass == 2)) |
3742 frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number]) | 3355 frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number]) |
3743 - cpi->next_frame_in_order; | 3356 - cpi->next_frame_in_order; |
3744 else | 3357 else |
3745 #endif | 3358 #endif |
3746 frames_to_arf = cpi->frames_till_gf_update_due; | 3359 frames_to_arf = cpi->rc.frames_till_gf_update_due; |
3747 | 3360 |
3748 assert(frames_to_arf < cpi->twopass.frames_to_key); | 3361 assert(frames_to_arf < cpi->twopass.frames_to_key); |
3749 | 3362 |
3750 if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) { | 3363 if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) { |
3751 #if CONFIG_MULTIPLE_ARF | 3364 #if CONFIG_MULTIPLE_ARF |
3752 cpi->alt_ref_source[cpi->arf_buffered] = cpi->source; | 3365 cpi->alt_ref_source[cpi->arf_buffered] = cpi->source; |
3753 #else | 3366 #else |
3754 cpi->alt_ref_source = cpi->source; | 3367 cpi->alt_ref_source = cpi->source; |
3755 #endif | 3368 #endif |
3756 | 3369 |
3757 if (cpi->oxcf.arnr_max_frames > 0) { | 3370 if (cpi->oxcf.arnr_max_frames > 0) { |
3758 // Produce the filtered ARF frame. | 3371 // Produce the filtered ARF frame. |
3759 // TODO(agrange) merge these two functions. | 3372 // TODO(agrange) merge these two functions. |
3760 configure_arnr_filter(cpi, cm->current_video_frame + frames_to_arf, | 3373 configure_arnr_filter(cpi, cm->current_video_frame + frames_to_arf, |
3761 cpi->gfu_boost); | 3374 cpi->rc.gfu_boost); |
3762 vp9_temporal_filter_prepare(cpi, frames_to_arf); | 3375 vp9_temporal_filter_prepare(cpi, frames_to_arf); |
3763 vp9_extend_frame_borders(&cpi->alt_ref_buffer, | 3376 vp9_extend_frame_borders(&cpi->alt_ref_buffer, |
3764 cm->subsampling_x, cm->subsampling_y); | 3377 cm->subsampling_x, cm->subsampling_y); |
3765 force_src_buffer = &cpi->alt_ref_buffer; | 3378 force_src_buffer = &cpi->alt_ref_buffer; |
3766 } | 3379 } |
3767 | 3380 |
3768 cm->show_frame = 0; | 3381 cm->show_frame = 0; |
3769 cpi->refresh_alt_ref_frame = 1; | 3382 cpi->refresh_alt_ref_frame = 1; |
3770 cpi->refresh_golden_frame = 0; | 3383 cpi->refresh_golden_frame = 0; |
3771 cpi->refresh_last_frame = 0; | 3384 cpi->refresh_last_frame = 0; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3940 } | 3553 } |
3941 #endif | 3554 #endif |
3942 | 3555 |
3943 cm->frame_type = INTER_FRAME; | 3556 cm->frame_type = INTER_FRAME; |
3944 cm->frame_flags = *frame_flags; | 3557 cm->frame_flags = *frame_flags; |
3945 | 3558 |
3946 // Reset the frame pointers to the current frame size | 3559 // Reset the frame pointers to the current frame size |
3947 vp9_realloc_frame_buffer(get_frame_new_buffer(cm), | 3560 vp9_realloc_frame_buffer(get_frame_new_buffer(cm), |
3948 cm->width, cm->height, | 3561 cm->width, cm->height, |
3949 cm->subsampling_x, cm->subsampling_y, | 3562 cm->subsampling_x, cm->subsampling_y, |
3950 VP9BORDERINPIXELS); | 3563 VP9BORDERINPIXELS, NULL, NULL, NULL); |
3951 | 3564 |
3952 // Calculate scaling factors for each of the 3 available references | 3565 // Calculate scaling factors for each of the 3 available references |
3953 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) | 3566 for (i = 0; i < REFS_PER_FRAME; ++i) { |
3954 vp9_setup_scale_factors(cm, i); | 3567 vp9_setup_scale_factors(cm, i); |
| 3568 if (vp9_is_scaled(&cm->active_ref_scale_comm[i])) |
| 3569 vp9_extend_frame_borders(&cm->yv12_fb[cm->active_ref_idx[i]], |
| 3570 cm->subsampling_x, cm->subsampling_y); |
| 3571 } |
3955 | 3572 |
3956 vp9_setup_interp_filters(&cpi->mb.e_mbd, DEFAULT_INTERP_FILTER, cm); | 3573 vp9_setup_interp_filters(&cpi->mb.e_mbd, DEFAULT_INTERP_FILTER, cm); |
3957 | 3574 |
3958 if (cpi->sf.variance_adaptive_quantization) { | 3575 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
3959 vp9_vaq_init(); | 3576 vp9_vaq_init(); |
3960 } | 3577 } |
3961 | 3578 |
3962 if (cpi->pass == 1) { | 3579 if (cpi->pass == 1) { |
3963 Pass1Encode(cpi, size, dest, frame_flags); | 3580 Pass1Encode(cpi, size, dest, frame_flags); |
3964 } else if (cpi->pass == 2) { | 3581 } else if (cpi->pass == 2) { |
3965 Pass2Encode(cpi, size, dest, frame_flags); | 3582 Pass2Encode(cpi, size, dest, frame_flags); |
3966 } else { | 3583 } else { |
3967 encode_frame_to_data_rate(cpi, size, dest, frame_flags); | 3584 // One pass encode |
| 3585 Pass0Encode(cpi, size, dest, frame_flags); |
3968 } | 3586 } |
3969 | 3587 |
3970 if (cm->refresh_frame_context) | 3588 if (cm->refresh_frame_context) |
3971 cm->frame_contexts[cm->frame_context_idx] = cm->fc; | 3589 cm->frame_contexts[cm->frame_context_idx] = cm->fc; |
3972 | 3590 |
3973 if (*size > 0) { | 3591 if (*size > 0) { |
3974 // if its a dropped frame honor the requests on subsequent frames | 3592 // if its a dropped frame honor the requests on subsequent frames |
3975 cpi->droppable = !frame_is_reference(cpi); | 3593 cpi->droppable = !frame_is_reference(cpi); |
3976 | 3594 |
3977 // return to normal state | 3595 // return to normal state |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4249 printf("Warning: Desired height too large, changed to %d \n", cm->height); | 3867 printf("Warning: Desired height too large, changed to %d \n", cm->height); |
4250 } | 3868 } |
4251 } | 3869 } |
4252 | 3870 |
4253 assert(cm->width <= cpi->initial_width); | 3871 assert(cm->width <= cpi->initial_width); |
4254 assert(cm->height <= cpi->initial_height); | 3872 assert(cm->height <= cpi->initial_height); |
4255 update_frame_size(cpi); | 3873 update_frame_size(cpi); |
4256 return 0; | 3874 return 0; |
4257 } | 3875 } |
4258 | 3876 |
4259 int vp9_switch_layer(VP9_PTR comp, int layer) { | |
4260 VP9_COMP *cpi = (VP9_COMP *)comp; | |
4261 | |
4262 if (cpi->use_svc) { | |
4263 cpi->current_layer = layer; | |
4264 | |
4265 // Use buffer i for layer i LST | |
4266 cpi->lst_fb_idx = layer; | |
4267 | |
4268 // Use buffer i-1 for layer i Alt (Inter-layer prediction) | |
4269 if (layer != 0) cpi->alt_fb_idx = layer - 1; | |
4270 | |
4271 // Use the rest for Golden | |
4272 if (layer < 2 * cpi->number_spatial_layers - NUM_REF_FRAMES) | |
4273 cpi->gld_fb_idx = cpi->lst_fb_idx; | |
4274 else | |
4275 cpi->gld_fb_idx = 2 * cpi->number_spatial_layers - 1 - layer; | |
4276 | |
4277 printf("Switching to layer %d:\n", layer); | |
4278 printf("Using references: LST/GLD/ALT [%d|%d|%d]\n", cpi->lst_fb_idx, | |
4279 cpi->gld_fb_idx, cpi->alt_fb_idx); | |
4280 } else { | |
4281 printf("Switching layer not supported. Enable SVC first \n"); | |
4282 } | |
4283 return 0; | |
4284 } | |
4285 | |
4286 void vp9_set_svc(VP9_PTR comp, int use_svc) { | 3877 void vp9_set_svc(VP9_PTR comp, int use_svc) { |
4287 VP9_COMP *cpi = (VP9_COMP *)comp; | 3878 VP9_COMP *cpi = (VP9_COMP *)comp; |
4288 cpi->use_svc = use_svc; | 3879 cpi->use_svc = use_svc; |
4289 if (cpi->use_svc) printf("Enabled SVC encoder \n"); | |
4290 return; | 3880 return; |
4291 } | 3881 } |
4292 | 3882 |
4293 int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) { | 3883 int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) { |
4294 int i, j; | 3884 int i, j; |
4295 int total = 0; | 3885 int total = 0; |
4296 | 3886 |
4297 uint8_t *src = source->y_buffer; | 3887 uint8_t *src = source->y_buffer; |
4298 uint8_t *dst = dest->y_buffer; | 3888 uint8_t *dst = dest->y_buffer; |
4299 | 3889 |
(...skipping 10 matching lines...) Expand all Loading... |
4310 dst += 16 * dest->y_stride; | 3900 dst += 16 * dest->y_stride; |
4311 } | 3901 } |
4312 | 3902 |
4313 return total; | 3903 return total; |
4314 } | 3904 } |
4315 | 3905 |
4316 | 3906 |
4317 int vp9_get_quantizer(VP9_PTR c) { | 3907 int vp9_get_quantizer(VP9_PTR c) { |
4318 return ((VP9_COMP *)c)->common.base_qindex; | 3908 return ((VP9_COMP *)c)->common.base_qindex; |
4319 } | 3909 } |
OLD | NEW |