2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 #include "onyxc_int.h"
14 #include "systemdependent.h"
16 #include "alloccommon.h"
18 #include "firstpass.h"
20 #include "vpx_scale/vpxscale.h"
23 #include "quant_common.h"
24 #include "segmentation_common.h"
26 #include "vpx_scale/yv12extend.h"
28 #include "vpx_mem/vpx_mem.h"
29 #include "swapyv12buffer.h"
30 #include "threading.h"
31 #include "vpx_ports/vpx_timer.h"
36 #if CONFIG_RUNTIME_CPU_DETECT
37 #define IF_RTCD(x) (x)
38 #define RTCD(x) &cpi->common.rtcd.x
40 #define IF_RTCD(x) NULL
44 extern void vp8cx_init_mv_bits_sadcost();
45 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG
*sd
, VP8_COMP
*cpi
);
46 extern void vp8cx_set_alt_lf_level(VP8_COMP
*cpi
, int filt_val
);
47 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG
*sd
, VP8_COMP
*cpi
);
49 extern void vp8_init_loop_filter(VP8_COMMON
*cm
);
50 extern void vp8_loop_filter_frame(VP8_COMMON
*cm
, MACROBLOCKD
*mbd
, int filt_val
);
51 extern void vp8_loop_filter_frame_yonly(VP8_COMMON
*cm
, MACROBLOCKD
*mbd
, int filt_val
, int sharpness_lvl
);
52 extern void vp8_dmachine_specific_config(VP8_COMP
*cpi
);
53 extern void vp8_cmachine_specific_config(VP8_COMP
*cpi
);
54 extern void vp8_calc_auto_iframe_target_size(VP8_COMP
*cpi
);
55 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*post
, int filt_lvl
, int low_var_thresh
, int flag
);
56 extern void print_parms(VP8_CONFIG
*ocf
, char *filenam
);
57 extern unsigned int vp8_get_processor_freq();
58 extern void print_tree_update_probs();
59 extern void vp8cx_create_encoder_threads(VP8_COMP
*cpi
);
60 extern void vp8cx_remove_encoder_threads(VP8_COMP
*cpi
);
62 extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG
*src_ybc
, YV12_BUFFER_CONFIG
*dst_ybc
);
63 extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG
*src_ybc
, YV12_BUFFER_CONFIG
*dst_ybc
);
66 int vp8_estimate_entropy_savings(VP8_COMP
*cpi
);
67 int vp8_calc_ss_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, const vp8_variance_rtcd_vtable_t
*rtcd
);
68 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, const vp8_variance_rtcd_vtable_t
*rtcd
);
71 static void mode_ref_lf_test_function(VP8_COMP
*cpi
);
73 extern const int vp8_gf_interval_table
[101];
78 extern double vp8_calc_ssim
80 YV12_BUFFER_CONFIG
*source
,
81 YV12_BUFFER_CONFIG
*dest
,
86 extern double vp8_calc_ssimg
88 YV12_BUFFER_CONFIG
*source
,
89 YV12_BUFFER_CONFIG
*dest
,
110 extern int skip_true_count
;
111 extern int skip_false_count
;
116 extern int intra_mode_stats
[10][10][10];
120 unsigned int frames_at_speed
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
121 unsigned int tot_pm
= 0;
122 unsigned int cnt_pm
= 0;
123 unsigned int tot_ef
= 0;
124 unsigned int cnt_ef
= 0;
128 extern unsigned __int64 Sectionbits
[50];
129 extern int y_modes
[5] ;
130 extern int uv_modes
[4] ;
131 extern int b_modes
[10] ;
133 extern int inter_y_modes
[10] ;
134 extern int inter_uv_modes
[4] ;
135 extern unsigned int inter_b_modes
[15];
138 extern void (*vp8_short_fdct4x4
)(short *input
, short *output
, int pitch
);
139 extern void (*vp8_short_fdct8x4
)(short *input
, short *output
, int pitch
);
140 extern void (*vp8_fast_fdct4x4
)(short *input
, short *output
, int pitch
);
141 extern void (*vp8_fast_fdct8x4
)(short *input
, short *output
, int pitch
);
143 extern const int vp8_bits_per_mb
[2][QINDEX_RANGE
];
145 extern const int qrounding_factors
[129];
146 extern const int qzbin_factors
[129];
147 extern void vp8cx_init_quantizer(VP8_COMP
*cpi
);
148 extern const int vp8cx_base_skip_false_prob
[128];
151 void vp8_initialize()
153 static int init_done
= 0;
157 vp8_scale_machine_specific_config();
158 vp8_initialize_common();
159 //vp8_dmachine_specific_config();
160 vp8_tokenize_initialize();
162 vp8cx_init_mv_bits_sadcost();
166 #ifdef PACKET_TESTING
167 extern FILE *vpxlogc
;
170 static void setup_features(VP8_COMP
*cpi
)
172 // Set up default state for MB feature flags
173 cpi
->mb
.e_mbd
.segmentation_enabled
= 0;
174 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 0;
175 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 0;
176 vpx_memset(cpi
->mb
.e_mbd
.mb_segment_tree_probs
, 255, sizeof(cpi
->mb
.e_mbd
.mb_segment_tree_probs
));
177 vpx_memset(cpi
->mb
.e_mbd
.segment_feature_data
, 0, sizeof(cpi
->mb
.e_mbd
.segment_feature_data
));
179 cpi
->mb
.e_mbd
.mode_ref_lf_delta_enabled
= 0;
180 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 0;
181 vpx_memset(cpi
->mb
.e_mbd
.ref_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.ref_lf_deltas
));
182 vpx_memset(cpi
->mb
.e_mbd
.mode_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.mode_lf_deltas
));
185 mode_ref_lf_test_function(cpi
);
190 void vp8_dealloc_compressor_data(VP8_COMP
*cpi
)
193 // Delete sementation map
194 if (cpi
->segmentation_map
!= 0)
195 vpx_free(cpi
->segmentation_map
);
197 cpi
->segmentation_map
= 0;
199 if (cpi
->active_map
!= 0)
200 vpx_free(cpi
->active_map
);
204 // Delete first pass motion map
205 if (cpi
->fp_motion_map
!= 0)
206 vpx_free(cpi
->fp_motion_map
);
208 cpi
->fp_motion_map
= 0;
210 vp8_de_alloc_frame_buffers(&cpi
->common
);
212 vp8_yv12_de_alloc_frame_buffer(&cpi
->last_frame_uf
);
213 vp8_yv12_de_alloc_frame_buffer(&cpi
->scaled_source
);
214 #if VP8_TEMPORAL_ALT_REF
215 vp8_yv12_de_alloc_frame_buffer(&cpi
->alt_ref_buffer
.source_buffer
);
220 for (i
= 0; i
< MAX_LAG_BUFFERS
; i
++)
221 vp8_yv12_de_alloc_frame_buffer(&cpi
->src_buffer
[i
].source_buffer
);
223 cpi
->source_buffer_count
= 0;
231 static void enable_segmentation(VP8_PTR ptr
)
233 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
235 // Set the appropriate feature bit
236 cpi
->mb
.e_mbd
.segmentation_enabled
= 1;
237 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 1;
238 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 1;
240 static void disable_segmentation(VP8_PTR ptr
)
242 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
244 // Clear the appropriate feature bit
245 cpi
->mb
.e_mbd
.segmentation_enabled
= 0;
248 // Valid values for a segment are 0 to 3
249 // Segmentation map is arrange as [Rows][Columns]
250 static void set_segmentation_map(VP8_PTR ptr
, unsigned char *segmentation_map
)
252 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
254 // Copy in the new segmentation map
255 vpx_memcpy(cpi
->segmentation_map
, segmentation_map
, (cpi
->common
.mb_rows
* cpi
->common
.mb_cols
));
257 // Signal that the map should be updated.
258 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 1;
259 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 1;
262 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
264 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
265 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
267 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
270 static void set_segment_data(VP8_PTR ptr
, signed char *feature_data
, unsigned char abs_delta
)
272 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
274 cpi
->mb
.e_mbd
.mb_segement_abs_delta
= abs_delta
;
275 vpx_memcpy(cpi
->segment_feature_data
, feature_data
, sizeof(cpi
->segment_feature_data
));
279 static void segmentation_test_function(VP8_PTR ptr
)
281 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
283 unsigned char *seg_map
;
284 signed char feature_data
[MB_LVL_MAX
][MAX_MB_SEGMENTS
];
286 // Create a temporary map for segmentation data.
287 CHECK_MEM_ERROR(seg_map
, vpx_calloc(cpi
->common
.mb_rows
* cpi
->common
.mb_cols
, 1));
289 // MB loop to set local segmentation map
290 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
292 for ( j = 0; j < cpi->common.mb_cols; j++ )
294 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
295 //if ( j < cpi->common.mb_cols/2 )
297 // Segment 1 around the edge else 0
298 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
299 seg_map[(i*cpi->common.mb_cols) + j] = 1;
300 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
301 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
302 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
303 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
305 seg_map[(i*cpi->common.mb_cols) + j] = 0;
309 // Set the segmentation Map
310 set_segmentation_map(ptr
, seg_map
);
312 // Activate segmentation.
313 enable_segmentation(ptr
);
315 // Set up the quant segment data
316 feature_data
[MB_LVL_ALT_Q
][0] = 0;
317 feature_data
[MB_LVL_ALT_Q
][1] = 4;
318 feature_data
[MB_LVL_ALT_Q
][2] = 0;
319 feature_data
[MB_LVL_ALT_Q
][3] = 0;
320 // Set up the loop segment data
321 feature_data
[MB_LVL_ALT_LF
][0] = 0;
322 feature_data
[MB_LVL_ALT_LF
][1] = 0;
323 feature_data
[MB_LVL_ALT_LF
][2] = 0;
324 feature_data
[MB_LVL_ALT_LF
][3] = 0;
326 // Initialise the feature data structure
327 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
328 set_segment_data(ptr
, &feature_data
[0][0], SEGMENT_DELTADATA
);
330 // Delete sementation map
338 // A simple function to cyclically refresh the background at a lower Q
339 static void cyclic_background_refresh(VP8_COMP
*cpi
, int Q
, int lf_adjustment
)
341 unsigned char *seg_map
;
342 signed char feature_data
[MB_LVL_MAX
][MAX_MB_SEGMENTS
];
344 int block_count
= cpi
->cyclic_refresh_mode_max_mbs_perframe
;
345 int mbs_in_frame
= cpi
->common
.mb_rows
* cpi
->common
.mb_cols
;
347 // Create a temporary map for segmentation data.
348 CHECK_MEM_ERROR(seg_map
, vpx_calloc(cpi
->common
.mb_rows
* cpi
->common
.mb_cols
, 1));
350 cpi
->cyclic_refresh_q
= Q
;
352 for (i
= Q
; i
> 0; i
--)
354 if (vp8_bits_per_mb
[cpi
->common
.frame_type
][i
] >= ((vp8_bits_per_mb
[cpi
->common
.frame_type
][Q
]*(Q
+ 128)) / 64))
355 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
361 cpi
->cyclic_refresh_q
= i
;
363 // Only update for inter frames
364 if (cpi
->common
.frame_type
!= KEY_FRAME
)
366 // Cycle through the macro_block rows
367 // MB loop to set local segmentation map
368 for (i
= cpi
->cyclic_refresh_mode_index
; i
< mbs_in_frame
; i
++)
370 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
371 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
372 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
373 if (cpi
->cyclic_refresh_map
[i
] == 0)
381 // Skip blocks that have been refreshed recently anyway.
382 if (cpi
->cyclic_refresh_map
[i
] < 0)
383 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
384 cpi
->cyclic_refresh_map
[i
]++;
395 // If we have gone through the frame reset to the start
396 cpi
->cyclic_refresh_mode_index
= i
;
398 if (cpi
->cyclic_refresh_mode_index
>= mbs_in_frame
)
399 cpi
->cyclic_refresh_mode_index
= 0;
402 // Set the segmentation Map
403 set_segmentation_map((VP8_PTR
)cpi
, seg_map
);
405 // Activate segmentation.
406 enable_segmentation((VP8_PTR
)cpi
);
408 // Set up the quant segment data
409 feature_data
[MB_LVL_ALT_Q
][0] = 0;
410 feature_data
[MB_LVL_ALT_Q
][1] = (cpi
->cyclic_refresh_q
- Q
);
411 feature_data
[MB_LVL_ALT_Q
][2] = 0;
412 feature_data
[MB_LVL_ALT_Q
][3] = 0;
414 // Set up the loop segment data
415 feature_data
[MB_LVL_ALT_LF
][0] = 0;
416 feature_data
[MB_LVL_ALT_LF
][1] = lf_adjustment
;
417 feature_data
[MB_LVL_ALT_LF
][2] = 0;
418 feature_data
[MB_LVL_ALT_LF
][3] = 0;
420 // Initialise the feature data structure
421 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
422 set_segment_data((VP8_PTR
)cpi
, &feature_data
[0][0], SEGMENT_DELTADATA
);
424 // Delete sementation map
432 static void mode_ref_lf_test_function(VP8_COMP
*cpi
)
434 cpi
->mb
.e_mbd
.mode_ref_lf_delta_enabled
= 1;
435 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 1;
437 vpx_memset(cpi
->mb
.e_mbd
.ref_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.ref_lf_deltas
));
438 vpx_memset(cpi
->mb
.e_mbd
.mode_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.mode_lf_deltas
));
440 // Test of ref frame deltas
441 cpi
->mb
.e_mbd
.ref_lf_deltas
[INTRA_FRAME
] = 2;
442 cpi
->mb
.e_mbd
.ref_lf_deltas
[LAST_FRAME
] = 0;
443 cpi
->mb
.e_mbd
.ref_lf_deltas
[GOLDEN_FRAME
] = -2;
444 cpi
->mb
.e_mbd
.ref_lf_deltas
[ALTREF_FRAME
] = -2;
446 cpi
->mb
.e_mbd
.mode_lf_deltas
[0] = 4; // BPRED
447 cpi
->mb
.e_mbd
.mode_lf_deltas
[1] = -2; // Zero
448 cpi
->mb
.e_mbd
.mode_lf_deltas
[2] = 2; // New mv
449 cpi
->mb
.e_mbd
.mode_lf_deltas
[3] = 4; // Split mv
452 void vp8_set_speed_features(VP8_COMP
*cpi
)
454 SPEED_FEATURES
*sf
= &cpi
->sf
;
455 int Mode
= cpi
->compressor_speed
;
456 int Speed
= cpi
->Speed
;
458 VP8_COMMON
*cm
= &cpi
->common
;
460 // Initialise default mode frequency sampling variables
461 for (i
= 0; i
< MAX_MODES
; i
++)
463 cpi
->mode_check_freq
[i
] = 0;
464 cpi
->mode_test_hit_counts
[i
] = 0;
465 cpi
->mode_chosen_counts
[i
] = 0;
468 cpi
->mbs_tested_so_far
= 0;
472 sf
->search_method
= NSTEP
;
473 sf
->improved_quant
= 1;
474 sf
->improved_dct
= 1;
477 sf
->quarter_pixel_search
= 1;
478 sf
->half_pixel_search
= 1;
479 sf
->full_freq
[0] = 7;
480 sf
->full_freq
[1] = 7;
481 sf
->min_fs_radius
= 8;
482 sf
->max_fs_radius
= 32;
483 sf
->iterative_sub_pixel
= 1;
484 sf
->optimize_coefficients
= 1;
487 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
492 // default thresholds to 0
493 for (i
= 0; i
< MAX_MODES
; i
++)
494 sf
->thresh_mult
[i
] = 0;
498 #if !(CONFIG_REALTIME_ONLY)
499 case 0: // best quality mode
500 sf
->thresh_mult
[THR_ZEROMV
] = 0;
501 sf
->thresh_mult
[THR_ZEROG
] = 0;
502 sf
->thresh_mult
[THR_ZEROA
] = 0;
503 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
504 sf
->thresh_mult
[THR_NEARESTG
] = 0;
505 sf
->thresh_mult
[THR_NEARESTA
] = 0;
506 sf
->thresh_mult
[THR_NEARMV
] = 0;
507 sf
->thresh_mult
[THR_NEARG
] = 0;
508 sf
->thresh_mult
[THR_NEARA
] = 0;
510 sf
->thresh_mult
[THR_DC
] = 0;
512 sf
->thresh_mult
[THR_V_PRED
] = 1000;
513 sf
->thresh_mult
[THR_H_PRED
] = 1000;
514 sf
->thresh_mult
[THR_B_PRED
] = 2000;
515 sf
->thresh_mult
[THR_TM
] = 1000;
517 sf
->thresh_mult
[THR_NEWMV
] = 1000;
518 sf
->thresh_mult
[THR_NEWG
] = 1000;
519 sf
->thresh_mult
[THR_NEWA
] = 1000;
521 sf
->thresh_mult
[THR_SPLITMV
] = 2500;
522 sf
->thresh_mult
[THR_SPLITG
] = 5000;
523 sf
->thresh_mult
[THR_SPLITA
] = 5000;
525 sf
->full_freq
[0] = 7;
526 sf
->full_freq
[1] = 15;
529 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
531 if (!(cpi
->ref_frame_flags
& VP8_LAST_FLAG
))
533 sf
->thresh_mult
[THR_NEWMV
] = INT_MAX
;
534 sf
->thresh_mult
[THR_NEARESTMV
] = INT_MAX
;
535 sf
->thresh_mult
[THR_ZEROMV
] = INT_MAX
;
536 sf
->thresh_mult
[THR_NEARMV
] = INT_MAX
;
537 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
540 if (!(cpi
->ref_frame_flags
& VP8_GOLD_FLAG
))
542 sf
->thresh_mult
[THR_NEARESTG
] = INT_MAX
;
543 sf
->thresh_mult
[THR_ZEROG
] = INT_MAX
;
544 sf
->thresh_mult
[THR_NEARG
] = INT_MAX
;
545 sf
->thresh_mult
[THR_NEWG
] = INT_MAX
;
546 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
548 else if (!(cpi
->ref_frame_flags
& VP8_ALT_FLAG
))
550 sf
->thresh_mult
[THR_NEARESTA
] = INT_MAX
;
551 sf
->thresh_mult
[THR_ZEROA
] = INT_MAX
;
552 sf
->thresh_mult
[THR_NEARA
] = INT_MAX
;
553 sf
->thresh_mult
[THR_NEWA
] = INT_MAX
;
554 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
560 sf
->optimize_coefficients
= 0;
561 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
562 sf
->thresh_mult
[THR_ZEROMV
] = 0;
563 sf
->thresh_mult
[THR_DC
] = 0;
564 sf
->thresh_mult
[THR_NEARMV
] = 0;
565 sf
->thresh_mult
[THR_V_PRED
] = 1000;
566 sf
->thresh_mult
[THR_H_PRED
] = 1000;
567 sf
->thresh_mult
[THR_B_PRED
] = 2500;
568 sf
->thresh_mult
[THR_TM
] = 1000;
570 sf
->thresh_mult
[THR_NEARESTG
] = 1000;
571 sf
->thresh_mult
[THR_NEARESTA
] = 1000;
573 sf
->thresh_mult
[THR_ZEROG
] = 1000;
574 sf
->thresh_mult
[THR_ZEROA
] = 1000;
575 sf
->thresh_mult
[THR_NEARG
] = 1000;
576 sf
->thresh_mult
[THR_NEARA
] = 1000;
578 sf
->thresh_mult
[THR_NEWMV
] = 1500;
579 sf
->thresh_mult
[THR_NEWG
] = 1500;
580 sf
->thresh_mult
[THR_NEWA
] = 1500;
582 sf
->thresh_mult
[THR_SPLITMV
] = 5000;
583 sf
->thresh_mult
[THR_SPLITG
] = 10000;
584 sf
->thresh_mult
[THR_SPLITA
] = 10000;
586 sf
->full_freq
[0] = 15;
587 sf
->full_freq
[1] = 31;
590 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
592 if (!(cpi
->ref_frame_flags
& VP8_LAST_FLAG
))
594 sf
->thresh_mult
[THR_NEWMV
] = INT_MAX
;
595 sf
->thresh_mult
[THR_NEARESTMV
] = INT_MAX
;
596 sf
->thresh_mult
[THR_ZEROMV
] = INT_MAX
;
597 sf
->thresh_mult
[THR_NEARMV
] = INT_MAX
;
598 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
600 else if (!(cpi
->ref_frame_flags
& VP8_GOLD_FLAG
))
602 sf
->thresh_mult
[THR_NEARESTG
] = INT_MAX
;
603 sf
->thresh_mult
[THR_ZEROG
] = INT_MAX
;
604 sf
->thresh_mult
[THR_NEARG
] = INT_MAX
;
605 sf
->thresh_mult
[THR_NEWG
] = INT_MAX
;
606 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
608 else if (!(cpi
->ref_frame_flags
& VP8_ALT_FLAG
))
610 sf
->thresh_mult
[THR_NEARESTA
] = INT_MAX
;
611 sf
->thresh_mult
[THR_ZEROA
] = INT_MAX
;
612 sf
->thresh_mult
[THR_NEARA
] = INT_MAX
;
613 sf
->thresh_mult
[THR_NEWA
] = INT_MAX
;
614 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
619 cpi
->mode_check_freq
[THR_SPLITG
] = 4;
620 cpi
->mode_check_freq
[THR_SPLITA
] = 4;
621 cpi
->mode_check_freq
[THR_SPLITMV
] = 2;
623 sf
->thresh_mult
[THR_TM
] = 1500;
624 sf
->thresh_mult
[THR_V_PRED
] = 1500;
625 sf
->thresh_mult
[THR_H_PRED
] = 1500;
626 sf
->thresh_mult
[THR_B_PRED
] = 5000;
628 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
630 sf
->thresh_mult
[THR_NEWMV
] = 2000;
631 sf
->thresh_mult
[THR_SPLITMV
] = 10000;
634 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
636 sf
->thresh_mult
[THR_NEARESTG
] = 1500;
637 sf
->thresh_mult
[THR_ZEROG
] = 1500;
638 sf
->thresh_mult
[THR_NEARG
] = 1500;
639 sf
->thresh_mult
[THR_NEWG
] = 2000;
640 sf
->thresh_mult
[THR_SPLITG
] = 20000;
643 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
645 sf
->thresh_mult
[THR_NEARESTA
] = 1500;
646 sf
->thresh_mult
[THR_ZEROA
] = 1500;
647 sf
->thresh_mult
[THR_NEARA
] = 1500;
648 sf
->thresh_mult
[THR_NEWA
] = 2000;
649 sf
->thresh_mult
[THR_SPLITA
] = 20000;
652 sf
->improved_quant
= 0;
653 sf
->improved_dct
= 0;
656 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
661 cpi
->mode_check_freq
[THR_SPLITG
] = 15;
662 cpi
->mode_check_freq
[THR_SPLITA
] = 15;
663 cpi
->mode_check_freq
[THR_SPLITMV
] = 7;
665 sf
->thresh_mult
[THR_TM
] = 2000;
666 sf
->thresh_mult
[THR_V_PRED
] = 2000;
667 sf
->thresh_mult
[THR_H_PRED
] = 2000;
668 sf
->thresh_mult
[THR_B_PRED
] = 7500;
670 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
672 sf
->thresh_mult
[THR_NEWMV
] = 2000;
673 sf
->thresh_mult
[THR_SPLITMV
] = 25000;
676 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
678 sf
->thresh_mult
[THR_NEARESTG
] = 2000;
679 sf
->thresh_mult
[THR_ZEROG
] = 2000;
680 sf
->thresh_mult
[THR_NEARG
] = 2000;
681 sf
->thresh_mult
[THR_NEWG
] = 2500;
682 sf
->thresh_mult
[THR_SPLITG
] = 50000;
685 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
687 sf
->thresh_mult
[THR_NEARESTA
] = 2000;
688 sf
->thresh_mult
[THR_ZEROA
] = 2000;
689 sf
->thresh_mult
[THR_NEARA
] = 2000;
690 sf
->thresh_mult
[THR_NEWA
] = 2500;
691 sf
->thresh_mult
[THR_SPLITA
] = 50000;
694 // Only do recode loop on key frames and golden frames
697 sf
->full_freq
[0] = 31;
698 sf
->full_freq
[1] = 63;
704 sf
->auto_filter
= 0; // Faster selection of loop filter
705 cpi
->mode_check_freq
[THR_V_PRED
] = 2;
706 cpi
->mode_check_freq
[THR_H_PRED
] = 2;
707 cpi
->mode_check_freq
[THR_B_PRED
] = 2;
709 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
711 cpi
->mode_check_freq
[THR_NEARG
] = 2;
712 cpi
->mode_check_freq
[THR_NEWG
] = 4;
715 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
717 cpi
->mode_check_freq
[THR_NEARA
] = 2;
718 cpi
->mode_check_freq
[THR_NEWA
] = 4;
721 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
722 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
723 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
725 sf
->full_freq
[0] = 63;
726 sf
->full_freq
[1] = 127;
731 cpi
->mode_check_freq
[THR_V_PRED
] = 0;
732 cpi
->mode_check_freq
[THR_H_PRED
] = 0;
733 cpi
->mode_check_freq
[THR_B_PRED
] = 0;
734 cpi
->mode_check_freq
[THR_NEARG
] = 0;
735 cpi
->mode_check_freq
[THR_NEWG
] = 0;
736 cpi
->mode_check_freq
[THR_NEARA
] = 0;
737 cpi
->mode_check_freq
[THR_NEWA
] = 0;
740 sf
->recode_loop
= 0; // recode loop off
741 sf
->RD
= 0; // Turn rd off
742 sf
->full_freq
[0] = INT_MAX
;
743 sf
->full_freq
[1] = INT_MAX
;
748 sf
->auto_filter
= 0; // Faster selection of loop filter
750 cpi
->mode_check_freq
[THR_V_PRED
] = 2;
751 cpi
->mode_check_freq
[THR_H_PRED
] = 2;
752 cpi
->mode_check_freq
[THR_B_PRED
] = 2;
754 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
756 cpi
->mode_check_freq
[THR_NEARG
] = 2;
757 cpi
->mode_check_freq
[THR_NEWG
] = 4;
760 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
762 cpi
->mode_check_freq
[THR_NEARA
] = 2;
763 cpi
->mode_check_freq
[THR_NEWA
] = 4;
766 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
& VP8_GOLD_FLAG
)
768 sf
->thresh_mult
[THR_NEARESTG
] = 2000;
769 sf
->thresh_mult
[THR_ZEROG
] = 2000;
770 sf
->thresh_mult
[THR_NEARG
] = 2000;
771 sf
->thresh_mult
[THR_NEWG
] = 4000;
774 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
& VP8_ALT_FLAG
)
776 sf
->thresh_mult
[THR_NEARESTA
] = 2000;
777 sf
->thresh_mult
[THR_ZEROA
] = 2000;
778 sf
->thresh_mult
[THR_NEARA
] = 2000;
779 sf
->thresh_mult
[THR_NEWA
] = 4000;
786 sf
->optimize_coefficients
= 0;
789 sf
->iterative_sub_pixel
= 1;
790 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
791 sf
->thresh_mult
[THR_ZEROMV
] = 0;
792 sf
->thresh_mult
[THR_DC
] = 0;
793 sf
->thresh_mult
[THR_TM
] = 0;
794 sf
->thresh_mult
[THR_NEARMV
] = 0;
795 sf
->thresh_mult
[THR_V_PRED
] = 1000;
796 sf
->thresh_mult
[THR_H_PRED
] = 1000;
797 sf
->thresh_mult
[THR_B_PRED
] = 2500;
798 sf
->thresh_mult
[THR_NEARESTG
] = 1000;
799 sf
->thresh_mult
[THR_ZEROG
] = 1000;
800 sf
->thresh_mult
[THR_NEARG
] = 1000;
801 sf
->thresh_mult
[THR_NEARESTA
] = 1000;
802 sf
->thresh_mult
[THR_ZEROA
] = 1000;
803 sf
->thresh_mult
[THR_NEARA
] = 1000;
804 sf
->thresh_mult
[THR_NEWMV
] = 2000;
805 sf
->thresh_mult
[THR_NEWG
] = 2000;
806 sf
->thresh_mult
[THR_NEWA
] = 2000;
807 sf
->thresh_mult
[THR_SPLITMV
] = 5000;
808 sf
->thresh_mult
[THR_SPLITG
] = 10000;
809 sf
->thresh_mult
[THR_SPLITA
] = 10000;
810 sf
->full_freq
[0] = 15;
811 sf
->full_freq
[1] = 31;
812 sf
->search_method
= NSTEP
;
814 if (!cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
816 sf
->thresh_mult
[THR_NEWMV
] = INT_MAX
;
817 sf
->thresh_mult
[THR_NEARESTMV
] = INT_MAX
;
818 sf
->thresh_mult
[THR_ZEROMV
] = INT_MAX
;
819 sf
->thresh_mult
[THR_NEARMV
] = INT_MAX
;
820 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
823 if (!cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
825 sf
->thresh_mult
[THR_NEARESTG
] = INT_MAX
;
826 sf
->thresh_mult
[THR_ZEROG
] = INT_MAX
;
827 sf
->thresh_mult
[THR_NEARG
] = INT_MAX
;
828 sf
->thresh_mult
[THR_NEWG
] = INT_MAX
;
829 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
832 if (!cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
834 sf
->thresh_mult
[THR_NEARESTA
] = INT_MAX
;
835 sf
->thresh_mult
[THR_ZEROA
] = INT_MAX
;
836 sf
->thresh_mult
[THR_NEARA
] = INT_MAX
;
837 sf
->thresh_mult
[THR_NEWA
] = INT_MAX
;
838 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
843 cpi
->mode_check_freq
[THR_SPLITG
] = 4;
844 cpi
->mode_check_freq
[THR_SPLITA
] = 4;
845 cpi
->mode_check_freq
[THR_SPLITMV
] = 2;
847 sf
->thresh_mult
[THR_DC
] = 0;
848 sf
->thresh_mult
[THR_TM
] = 1000;
849 sf
->thresh_mult
[THR_V_PRED
] = 2000;
850 sf
->thresh_mult
[THR_H_PRED
] = 2000;
851 sf
->thresh_mult
[THR_B_PRED
] = 5000;
853 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
855 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
856 sf
->thresh_mult
[THR_ZEROMV
] = 0;
857 sf
->thresh_mult
[THR_NEARMV
] = 0;
858 sf
->thresh_mult
[THR_NEWMV
] = 2000;
859 sf
->thresh_mult
[THR_SPLITMV
] = 10000;
862 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
864 sf
->thresh_mult
[THR_NEARESTG
] = 1000;
865 sf
->thresh_mult
[THR_ZEROG
] = 1000;
866 sf
->thresh_mult
[THR_NEARG
] = 1000;
867 sf
->thresh_mult
[THR_NEWG
] = 2000;
868 sf
->thresh_mult
[THR_SPLITG
] = 20000;
871 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
873 sf
->thresh_mult
[THR_NEARESTA
] = 1000;
874 sf
->thresh_mult
[THR_ZEROA
] = 1000;
875 sf
->thresh_mult
[THR_NEARA
] = 1000;
876 sf
->thresh_mult
[THR_NEWA
] = 2000;
877 sf
->thresh_mult
[THR_SPLITA
] = 20000;
880 sf
->improved_quant
= 0;
881 sf
->improved_dct
= 0;
886 cpi
->mode_check_freq
[THR_SPLITMV
] = 7;
887 cpi
->mode_check_freq
[THR_SPLITG
] = 15;
888 cpi
->mode_check_freq
[THR_SPLITA
] = 15;
890 sf
->thresh_mult
[THR_TM
] = 2000;
891 sf
->thresh_mult
[THR_V_PRED
] = 2000;
892 sf
->thresh_mult
[THR_H_PRED
] = 2000;
893 sf
->thresh_mult
[THR_B_PRED
] = 5000;
895 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
897 sf
->thresh_mult
[THR_NEWMV
] = 2000;
898 sf
->thresh_mult
[THR_SPLITMV
] = 25000;
901 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
903 sf
->thresh_mult
[THR_NEARESTG
] = 2000;
904 sf
->thresh_mult
[THR_ZEROG
] = 2000;
905 sf
->thresh_mult
[THR_NEARG
] = 2000;
906 sf
->thresh_mult
[THR_NEWG
] = 2500;
907 sf
->thresh_mult
[THR_SPLITG
] = 50000;
910 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
912 sf
->thresh_mult
[THR_NEARESTA
] = 2000;
913 sf
->thresh_mult
[THR_ZEROA
] = 2000;
914 sf
->thresh_mult
[THR_NEARA
] = 2000;
915 sf
->thresh_mult
[THR_NEWA
] = 2500;
916 sf
->thresh_mult
[THR_SPLITA
] = 50000;
919 sf
->full_freq
[0] = 31;
920 sf
->full_freq
[1] = 63;
925 sf
->auto_filter
= 0; // Faster selection of loop filter
927 cpi
->mode_check_freq
[THR_V_PRED
] = 2;
928 cpi
->mode_check_freq
[THR_H_PRED
] = 2;
929 cpi
->mode_check_freq
[THR_B_PRED
] = 2;
931 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
933 cpi
->mode_check_freq
[THR_NEARG
] = 2;
934 cpi
->mode_check_freq
[THR_NEWG
] = 4;
937 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
939 cpi
->mode_check_freq
[THR_NEARA
] = 2;
940 cpi
->mode_check_freq
[THR_NEWA
] = 4;
943 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
944 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
945 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
947 sf
->full_freq
[0] = 63;
948 sf
->full_freq
[1] = 127;
954 sf
->full_freq
[0] = INT_MAX
;
955 sf
->full_freq
[1] = INT_MAX
;
962 sf
->auto_filter
= 0; // Faster selection of loop filter
964 #if CONFIG_REALTIME_ONLY
965 sf
->search_method
= HEX
;
967 sf
->search_method
= DIAMOND
;
970 cpi
->mode_check_freq
[THR_V_PRED
] = 4;
971 cpi
->mode_check_freq
[THR_H_PRED
] = 4;
972 cpi
->mode_check_freq
[THR_B_PRED
] = 4;
974 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
976 cpi
->mode_check_freq
[THR_NEARG
] = 2;
977 cpi
->mode_check_freq
[THR_NEWG
] = 4;
980 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
982 cpi
->mode_check_freq
[THR_NEARA
] = 2;
983 cpi
->mode_check_freq
[THR_NEWA
] = 4;
986 sf
->thresh_mult
[THR_TM
] = 2000;
987 sf
->thresh_mult
[THR_B_PRED
] = 5000;
989 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
991 sf
->thresh_mult
[THR_NEARESTG
] = 2000;
992 sf
->thresh_mult
[THR_ZEROG
] = 2000;
993 sf
->thresh_mult
[THR_NEARG
] = 2000;
994 sf
->thresh_mult
[THR_NEWG
] = 4000;
997 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
999 sf
->thresh_mult
[THR_NEARESTA
] = 2000;
1000 sf
->thresh_mult
[THR_ZEROA
] = 2000;
1001 sf
->thresh_mult
[THR_NEARA
] = 2000;
1002 sf
->thresh_mult
[THR_NEWA
] = 4000;
1008 // Disable split MB intra prediction mode
1009 sf
->thresh_mult
[THR_B_PRED
] = INT_MAX
;
1014 unsigned int i
, sum
= 0;
1015 unsigned int total_mbs
= cm
->MBs
;
1020 sf
->iterative_sub_pixel
= 0;
1022 if (cpi
->oxcf
.encode_breakout
> 2000)
1023 min
= cpi
->oxcf
.encode_breakout
;
1027 for (i
= 0; i
< min
; i
++)
1029 sum
+= cpi
->error_bins
[i
];
1035 // i starts from 2 to make sure thresh started from 2048
1036 for (; i
< 1024; i
++)
1038 sum
+= cpi
->error_bins
[i
];
1040 if (10 * sum
>= (unsigned int)(cpi
->Speed
- 6)*(total_mbs
- total_skip
))
1050 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
1052 sf
->thresh_mult
[THR_NEWMV
] = thresh
;
1053 sf
->thresh_mult
[THR_NEARESTMV
] = thresh
>> 1;
1054 sf
->thresh_mult
[THR_NEARMV
] = thresh
>> 1;
1057 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
1059 sf
->thresh_mult
[THR_NEWG
] = thresh
<< 1;
1060 sf
->thresh_mult
[THR_NEARESTG
] = thresh
;
1061 sf
->thresh_mult
[THR_NEARG
] = thresh
;
1064 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
1066 sf
->thresh_mult
[THR_NEWA
] = thresh
<< 1;
1067 sf
->thresh_mult
[THR_NEARESTA
] = thresh
;
1068 sf
->thresh_mult
[THR_NEARA
] = thresh
;
1071 // Disable other intra prediction modes
1072 sf
->thresh_mult
[THR_TM
] = INT_MAX
;
1073 sf
->thresh_mult
[THR_V_PRED
] = INT_MAX
;
1074 sf
->thresh_mult
[THR_H_PRED
] = INT_MAX
;
1080 sf
->quarter_pixel_search
= 0;
1085 int Tmp
= cpi
->Speed
- 8;
1090 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
1092 cpi
->mode_check_freq
[THR_ZEROG
] = 1 << (Tmp
- 1);
1093 cpi
->mode_check_freq
[THR_NEARESTG
] = 1 << (Tmp
- 1);
1094 cpi
->mode_check_freq
[THR_NEARG
] = 1 << Tmp
;
1095 cpi
->mode_check_freq
[THR_NEWG
] = 1 << (Tmp
+ 1);
1098 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
1100 cpi
->mode_check_freq
[THR_ZEROA
] = 1 << (Tmp
- 1);
1101 cpi
->mode_check_freq
[THR_NEARESTA
] = 1 << (Tmp
- 1);
1102 cpi
->mode_check_freq
[THR_NEARA
] = 1 << Tmp
;
1103 cpi
->mode_check_freq
[THR_NEWA
] = 1 << (Tmp
+ 1);
1106 cpi
->mode_check_freq
[THR_NEWMV
] = 1 << (Tmp
- 1);
1109 cm
->filter_type
= NORMAL_LOOPFILTER
;
1112 cm
->filter_type
= SIMPLE_LOOPFILTER
;
1116 sf
->half_pixel_search
= 0; // This has a big hit on quality. Last resort
1119 vpx_memset(cpi
->error_bins
, 0, sizeof(cpi
->error_bins
));
1123 if (cpi
->sf
.search_method
== NSTEP
)
1125 vp8_init3smotion_compensation(&cpi
->mb
, cm
->last_frame
.y_stride
);
1127 else if (cpi
->sf
.search_method
== DIAMOND
)
1129 vp8_init_dsmotion_compensation(&cpi
->mb
, cm
->last_frame
.y_stride
);
1132 if (cpi
->sf
.improved_dct
)
1134 cpi
->mb
.vp8_short_fdct8x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, short8x4
);
1135 cpi
->mb
.vp8_short_fdct4x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, short4x4
);
1136 cpi
->mb
.short_fdct8x4rd
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, short8x4
);
1137 cpi
->mb
.short_fdct4x4rd
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, short4x4
);
1141 cpi
->mb
.vp8_short_fdct8x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, fast8x4
);
1142 cpi
->mb
.vp8_short_fdct4x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, fast4x4
);
1143 cpi
->mb
.short_fdct8x4rd
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, fast8x4
);
1144 cpi
->mb
.short_fdct4x4rd
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, fast4x4
);
1147 cpi
->mb
.vp8_short_fdct4x4_ptr
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, short4x4
);
1148 cpi
->mb
.short_walsh4x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, walsh_short4x4
);
1150 if (cpi
->sf
.improved_quant
)
1152 cpi
->mb
.quantize_b
= QUANTIZE_INVOKE(&cpi
->rtcd
.quantize
, quantb
);
1156 cpi
->mb
.quantize_b
= QUANTIZE_INVOKE(&cpi
->rtcd
.quantize
, fastquantb
);
1159 #if CONFIG_RUNTIME_CPU_DETECT
1160 cpi
->mb
.e_mbd
.rtcd
= &cpi
->common
.rtcd
;
1163 if (cpi
->sf
.iterative_sub_pixel
== 1)
1165 cpi
->find_fractional_mv_step
= vp8_find_best_sub_pixel_step_iteratively
;
1167 else if (cpi
->sf
.quarter_pixel_search
)
1169 cpi
->find_fractional_mv_step
= vp8_find_best_sub_pixel_step
;
1171 else if (cpi
->sf
.half_pixel_search
)
1173 cpi
->find_fractional_mv_step
= vp8_find_best_half_pixel_step
;
1177 cpi
->find_fractional_mv_step
= vp8_skip_fractional_mv_step
;
1180 if (cpi
->sf
.optimize_coefficients
== 1)
1181 cpi
->mb
.optimize
= 1;
1183 cpi
->mb
.optimize
= 0;
1185 if (cpi
->common
.full_pixel
)
1186 cpi
->find_fractional_mv_step
= vp8_skip_fractional_mv_step
;
1189 frames_at_speed
[cpi
->Speed
]++;
1192 static void alloc_raw_frame_buffers(VP8_COMP
*cpi
)
1196 buffers
= cpi
->oxcf
.lag_in_frames
;
1198 if (buffers
> MAX_LAG_BUFFERS
)
1199 buffers
= MAX_LAG_BUFFERS
;
1204 for (i
= 0; i
< buffers
; i
++)
1205 if (vp8_yv12_alloc_frame_buffer(&cpi
->src_buffer
[i
].source_buffer
,
1206 cpi
->oxcf
.Width
, cpi
->oxcf
.Height
,
1208 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1209 "Failed to allocate lag buffer");
1211 #if VP8_TEMPORAL_ALT_REF
1213 if (vp8_yv12_alloc_frame_buffer(&cpi
->alt_ref_buffer
.source_buffer
,
1214 cpi
->oxcf
.Width
, cpi
->oxcf
.Height
, 16))
1215 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1216 "Failed to allocate altref buffer");
1220 cpi
->source_buffer_count
= 0;
1222 void vp8_alloc_compressor_data(VP8_COMP
*cpi
)
1224 VP8_COMMON
*cm
= & cpi
->common
;
1226 int width
= cm
->Width
;
1227 int height
= cm
->Height
;
1229 if (vp8_alloc_frame_buffers(cm
, width
, height
))
1230 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1231 "Failed to allocate frame buffers");
1233 if ((width
& 0xf) != 0)
1234 width
+= 16 - (width
& 0xf);
1236 if ((height
& 0xf) != 0)
1237 height
+= 16 - (height
& 0xf);
1240 if (vp8_yv12_alloc_frame_buffer(&cpi
->last_frame_uf
,
1241 width
, height
, VP8BORDERINPIXELS
))
1242 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1243 "Failed to allocate last frame buffer");
1245 if (vp8_yv12_alloc_frame_buffer(&cpi
->scaled_source
, width
, height
, 16))
1246 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1247 "Failed to allocate scaled source buffer");
1254 unsigned int tokens
= cm
->mb_rows
* cm
->mb_cols
* 24 * 16;
1256 CHECK_MEM_ERROR(cpi
->tok
, vpx_calloc(tokens
, sizeof(*cpi
->tok
)));
1259 // Data used for real time vc mode to see if gf needs refreshing
1260 cpi
->inter_zz_count
= 0;
1261 cpi
->gf_bad_count
= 0;
1262 cpi
->gf_update_recommended
= 0;
1267 static const int q_trans
[] =
1269 0, 1, 2, 3, 4, 5, 7, 8,
1270 9, 10, 12, 13, 15, 17, 18, 19,
1271 20, 21, 23, 24, 25, 26, 27, 28,
1272 29, 30, 31, 33, 35, 37, 39, 41,
1273 43, 45, 47, 49, 51, 53, 55, 57,
1274 59, 61, 64, 67, 70, 73, 76, 79,
1275 82, 85, 88, 91, 94, 97, 100, 103,
1276 106, 109, 112, 115, 118, 121, 124, 127,
1279 int vp8_reverse_trans(int x
)
1283 for (i
= 0; i
< 64; i
++)
1284 if (q_trans
[i
] >= x
)
1289 void vp8_new_frame_rate(VP8_COMP
*cpi
, double framerate
)
1291 cpi
->oxcf
.frame_rate
= framerate
;
1292 cpi
->output_frame_rate
= cpi
->oxcf
.frame_rate
;
1293 cpi
->per_frame_bandwidth
= (int)(cpi
->oxcf
.target_bandwidth
/ cpi
->output_frame_rate
);
1294 cpi
->av_per_frame_bandwidth
= (int)(cpi
->oxcf
.target_bandwidth
/ cpi
->output_frame_rate
);
1295 cpi
->min_frame_bandwidth
= (int)(cpi
->av_per_frame_bandwidth
* cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
1296 cpi
->rolling_target_bits
= cpi
->av_per_frame_bandwidth
;
1297 cpi
->rolling_actual_bits
= cpi
->av_per_frame_bandwidth
;
1299 cpi
->long_rolling_target_bits
= cpi
->av_per_frame_bandwidth
;
1300 cpi
->long_rolling_actual_bits
= cpi
->av_per_frame_bandwidth
;
1301 cpi
->max_gf_interval
= (int)(cpi
->output_frame_rate
/ 2) + 2;
1303 //cpi->max_gf_interval = (int)(cpi->output_frame_rate * 2 / 3) + 1;
1304 //cpi->max_gf_interval = 24;
1306 if (cpi
->max_gf_interval
< 12)
1307 cpi
->max_gf_interval
= 12;
1310 // Special conditions when altr ref frame enabled
1311 if (cpi
->oxcf
.play_alternate
)
1313 if (cpi
->max_gf_interval
> cpi
->oxcf
.lag_in_frames
- 1)
1314 cpi
->max_gf_interval
= cpi
->oxcf
.lag_in_frames
- 1;
1318 void vp8_init_config(VP8_PTR ptr
, VP8_CONFIG
*oxcf
)
1320 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
1321 VP8_COMMON
*cm
= &cpi
->common
;
1327 cpi
->auto_adjust_gold_quantizer
= 1;
1328 cpi
->goldquantizer
= 1;
1330 cpi
->auto_adjust_key_quantizer
= 1;
1331 cpi
->keyquantizer
= 1;
1333 cm
->version
= oxcf
->Version
;
1334 vp8_setup_version(cm
);
1340 cpi
->auto_worst_q
= 0;
1341 cpi
->oxcf
.best_allowed_q
= MINQ
;
1342 cpi
->oxcf
.worst_allowed_q
= MAXQ
;
1344 cpi
->oxcf
.end_usage
= USAGE_STREAM_FROM_SERVER
;
1345 cpi
->oxcf
.starting_buffer_level
= 4;
1346 cpi
->oxcf
.optimal_buffer_level
= 5;
1347 cpi
->oxcf
.maximum_buffer_size
= 6;
1348 cpi
->oxcf
.under_shoot_pct
= 90;
1349 cpi
->oxcf
.allow_df
= 0;
1350 cpi
->oxcf
.drop_frames_water_mark
= 20;
1352 cpi
->oxcf
.allow_spatial_resampling
= 0;
1353 cpi
->oxcf
.resample_down_water_mark
= 40;
1354 cpi
->oxcf
.resample_up_water_mark
= 60;
1356 cpi
->oxcf
.fixed_q
= cpi
->interquantizer
;
1358 cpi
->filter_type
= NORMAL_LOOPFILTER
;
1360 if (cm
->simpler_lpf
)
1361 cpi
->filter_type
= SIMPLE_LOOPFILTER
;
1363 cpi
->compressor_speed
= 1;
1364 cpi
->horiz_scale
= 0;
1365 cpi
->vert_scale
= 0;
1366 cpi
->oxcf
.two_pass_vbrbias
= 50;
1367 cpi
->oxcf
.two_pass_vbrmax_section
= 400;
1368 cpi
->oxcf
.two_pass_vbrmin_section
= 0;
1370 cpi
->oxcf
.Sharpness
= 0;
1371 cpi
->oxcf
.noise_sensitivity
= 0;
1377 switch (cpi
->oxcf
.Mode
)
1382 cpi
->compressor_speed
= 2;
1384 if (cpi
->oxcf
.cpu_used
< -16)
1386 cpi
->oxcf
.cpu_used
= -16;
1389 if (cpi
->oxcf
.cpu_used
> 16)
1390 cpi
->oxcf
.cpu_used
= 16;
1394 #if !(CONFIG_REALTIME_ONLY)
1395 case MODE_GOODQUALITY
:
1397 cpi
->compressor_speed
= 1;
1399 if (cpi
->oxcf
.cpu_used
< -5)
1401 cpi
->oxcf
.cpu_used
= -5;
1404 if (cpi
->oxcf
.cpu_used
> 5)
1405 cpi
->oxcf
.cpu_used
= 5;
1409 case MODE_BESTQUALITY
:
1411 cpi
->compressor_speed
= 0;
1414 case MODE_FIRSTPASS
:
1416 cpi
->compressor_speed
= 1;
1418 case MODE_SECONDPASS
:
1420 cpi
->compressor_speed
= 1;
1422 if (cpi
->oxcf
.cpu_used
< -5)
1424 cpi
->oxcf
.cpu_used
= -5;
1427 if (cpi
->oxcf
.cpu_used
> 5)
1428 cpi
->oxcf
.cpu_used
= 5;
1431 case MODE_SECONDPASS_BEST
:
1433 cpi
->compressor_speed
= 0;
1439 cpi
->auto_worst_q
= 1;
1441 cpi
->oxcf
.worst_allowed_q
= q_trans
[oxcf
->worst_allowed_q
];
1442 cpi
->oxcf
.best_allowed_q
= q_trans
[oxcf
->best_allowed_q
];
1444 if (oxcf
->fixed_q
>= 0)
1446 if (oxcf
->worst_allowed_q
< 0)
1447 cpi
->oxcf
.fixed_q
= q_trans
[0];
1449 cpi
->oxcf
.fixed_q
= q_trans
[oxcf
->worst_allowed_q
];
1451 if (oxcf
->alt_q
< 0)
1452 cpi
->oxcf
.alt_q
= q_trans
[0];
1454 cpi
->oxcf
.alt_q
= q_trans
[oxcf
->alt_q
];
1456 if (oxcf
->key_q
< 0)
1457 cpi
->oxcf
.key_q
= q_trans
[0];
1459 cpi
->oxcf
.key_q
= q_trans
[oxcf
->key_q
];
1461 if (oxcf
->gold_q
< 0)
1462 cpi
->oxcf
.gold_q
= q_trans
[0];
1464 cpi
->oxcf
.gold_q
= q_trans
[oxcf
->gold_q
];
1468 cpi
->baseline_gf_interval
= cpi
->oxcf
.alt_freq
? cpi
->oxcf
.alt_freq
: DEFAULT_GF_INTERVAL
;
1469 cpi
->ref_frame_flags
= VP8_ALT_FLAG
| VP8_GOLD_FLAG
| VP8_LAST_FLAG
;
1471 //cpi->use_golden_frame_only = 0;
1472 //cpi->use_last_frame_only = 0;
1473 cm
->refresh_golden_frame
= 0;
1474 cm
->refresh_last_frame
= 1;
1475 cm
->refresh_entropy_probs
= 1;
1477 if (cpi
->oxcf
.token_partitions
>= 0 && cpi
->oxcf
.token_partitions
<= 3)
1478 cm
->multi_token_partition
= (TOKEN_PARTITION
) cpi
->oxcf
.token_partitions
;
1480 setup_features(cpi
);
1485 for (i
= 0; i
< MAX_MB_SEGMENTS
; i
++)
1486 cpi
->segment_encode_breakout
[i
] = cpi
->oxcf
.encode_breakout
;
1489 // At the moment the first order values may not be > MAXQ
1490 if (cpi
->oxcf
.fixed_q
> MAXQ
)
1491 cpi
->oxcf
.fixed_q
= MAXQ
;
1493 // local file playback mode == really big buffer
1494 if (cpi
->oxcf
.end_usage
== USAGE_LOCAL_FILE_PLAYBACK
)
1496 cpi
->oxcf
.starting_buffer_level
= 60;
1497 cpi
->oxcf
.optimal_buffer_level
= 60;
1498 cpi
->oxcf
.maximum_buffer_size
= 240;
1503 // Convert target bandwidth from Kbit/s to Bit/s
1504 cpi
->oxcf
.target_bandwidth
*= 1000;
1505 cpi
->oxcf
.starting_buffer_level
*= cpi
->oxcf
.target_bandwidth
;
1507 if (cpi
->oxcf
.optimal_buffer_level
== 0)
1508 cpi
->oxcf
.optimal_buffer_level
= cpi
->oxcf
.target_bandwidth
/ 8;
1510 cpi
->oxcf
.optimal_buffer_level
*= cpi
->oxcf
.target_bandwidth
;
1512 if (cpi
->oxcf
.maximum_buffer_size
== 0)
1513 cpi
->oxcf
.maximum_buffer_size
= cpi
->oxcf
.target_bandwidth
/ 8;
1515 cpi
->oxcf
.maximum_buffer_size
*= cpi
->oxcf
.target_bandwidth
;
1517 cpi
->buffer_level
= cpi
->oxcf
.starting_buffer_level
;
1518 cpi
->bits_off_target
= cpi
->oxcf
.starting_buffer_level
;
1520 vp8_new_frame_rate(cpi
, cpi
->oxcf
.frame_rate
);
1521 cpi
->worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1522 cpi
->active_worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1523 cpi
->avg_frame_qindex
= cpi
->oxcf
.worst_allowed_q
;
1524 cpi
->best_quality
= cpi
->oxcf
.best_allowed_q
;
1525 cpi
->active_best_quality
= cpi
->oxcf
.best_allowed_q
;
1526 cpi
->buffered_mode
= (cpi
->oxcf
.optimal_buffer_level
> 0) ? TRUE
: FALSE
;
1529 cpi
->total_actual_bits
= 0;
1530 cpi
->total_target_vs_actual
= 0;
1532 // Only allow dropped frames in buffered mode
1533 cpi
->drop_frames_allowed
= cpi
->oxcf
.allow_df
&& cpi
->buffered_mode
;
1535 cm
->filter_type
= (LOOPFILTERTYPE
) cpi
->filter_type
;
1537 if (!cm
->use_bilinear_mc_filter
)
1538 cm
->mcomp_filter_type
= SIXTAP
;
1540 cm
->mcomp_filter_type
= BILINEAR
;
1542 cpi
->target_bandwidth
= cpi
->oxcf
.target_bandwidth
;
1544 cm
->Width
= cpi
->oxcf
.Width
;
1545 cm
->Height
= cpi
->oxcf
.Height
;
1547 cpi
->intra_frame_target
= (4 * (cm
->Width
+ cm
->Height
) / 15) * 1000; // As per VP8
1549 cm
->horiz_scale
= cpi
->horiz_scale
;
1550 cm
->vert_scale
= cpi
->vert_scale
;
1552 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1553 if (cpi
->oxcf
.Sharpness
> 7)
1554 cpi
->oxcf
.Sharpness
= 7;
1556 cm
->sharpness_level
= cpi
->oxcf
.Sharpness
;
1558 if (cm
->horiz_scale
!= NORMAL
|| cm
->vert_scale
!= NORMAL
)
1560 int UNINITIALIZED_IS_SAFE(hr
), UNINITIALIZED_IS_SAFE(hs
);
1561 int UNINITIALIZED_IS_SAFE(vr
), UNINITIALIZED_IS_SAFE(vs
);
1563 Scale2Ratio(cm
->horiz_scale
, &hr
, &hs
);
1564 Scale2Ratio(cm
->vert_scale
, &vr
, &vs
);
1566 // always go to the next whole number
1567 cm
->Width
= (hs
- 1 + cpi
->oxcf
.Width
* hr
) / hs
;
1568 cm
->Height
= (vs
- 1 + cpi
->oxcf
.Height
* vr
) / vs
;
1571 if (((cm
->Width
+ 15) & 0xfffffff0) != cm
->last_frame
.y_width
||
1572 ((cm
->Height
+ 15) & 0xfffffff0) != cm
->last_frame
.y_height
||
1573 cm
->last_frame
.y_width
== 0)
1575 alloc_raw_frame_buffers(cpi
);
1576 vp8_alloc_compressor_data(cpi
);
1579 // Clamp KF frame size to quarter of data rate
1580 if (cpi
->intra_frame_target
> cpi
->target_bandwidth
>> 2)
1581 cpi
->intra_frame_target
= cpi
->target_bandwidth
>> 2;
1583 if (cpi
->oxcf
.fixed_q
>= 0)
1585 cpi
->last_q
[0] = cpi
->oxcf
.fixed_q
;
1586 cpi
->last_q
[1] = cpi
->oxcf
.fixed_q
;
1589 cpi
->Speed
= cpi
->oxcf
.cpu_used
;
1591 // force to allowlag to 0 if lag_in_frames is 0;
1592 if (cpi
->oxcf
.lag_in_frames
== 0)
1594 cpi
->oxcf
.allow_lag
= 0;
1596 // Limit on lag buffers as these are not currently dynamically allocated
1597 else if (cpi
->oxcf
.lag_in_frames
> MAX_LAG_BUFFERS
)
1598 cpi
->oxcf
.lag_in_frames
= MAX_LAG_BUFFERS
;
1600 // force play_alternate to 0 if allow_lag is 0, lag_in_frames is too small, Mode is real time or one pass compress enabled.
1601 if (cpi
->oxcf
.allow_lag
== 0 || cpi
->oxcf
.lag_in_frames
<= 5 || (cpi
->oxcf
.Mode
< MODE_SECONDPASS
))
1603 cpi
->oxcf
.play_alternate
= 0;
1604 cpi
->ref_frame_flags
= cpi
->ref_frame_flags
& ~VP8_ALT_FLAG
;
1608 cpi
->last_alt_ref_sei
= -1;
1609 cpi
->is_src_frame_alt_ref
= 0;
1612 // Experimental RD Code
1613 cpi
->frame_distortion
= 0;
1614 cpi
->last_frame_distortion
= 0;
1617 #if VP8_TEMPORAL_ALT_REF
1621 cpi
->fixed_divide
[0] = 0;
1623 for (i
= 1; i
< 255; i
++)
1624 cpi
->fixed_divide
[i
] = 0x10000 / i
;
1630 * This function needs more clean up, i.e. be more tuned torwards
1631 * change_config rather than init_config !!!!!!!!!!!!!!!!
1636 void vp8_change_config(VP8_PTR ptr
, VP8_CONFIG
*oxcf
)
1638 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
1639 VP8_COMMON
*cm
= &cpi
->common
;
1647 if (cm
->version
!= oxcf
->Version
)
1649 cm
->version
= oxcf
->Version
;
1650 vp8_setup_version(cm
);
1655 switch (cpi
->oxcf
.Mode
)
1660 cpi
->compressor_speed
= 2;
1662 if (cpi
->oxcf
.cpu_used
< -16)
1664 cpi
->oxcf
.cpu_used
= -16;
1667 if (cpi
->oxcf
.cpu_used
> 16)
1668 cpi
->oxcf
.cpu_used
= 16;
1672 #if !(CONFIG_REALTIME_ONLY)
1673 case MODE_GOODQUALITY
:
1675 cpi
->compressor_speed
= 1;
1677 if (cpi
->oxcf
.cpu_used
< -5)
1679 cpi
->oxcf
.cpu_used
= -5;
1682 if (cpi
->oxcf
.cpu_used
> 5)
1683 cpi
->oxcf
.cpu_used
= 5;
1687 case MODE_BESTQUALITY
:
1689 cpi
->compressor_speed
= 0;
1692 case MODE_FIRSTPASS
:
1694 cpi
->compressor_speed
= 1;
1696 case MODE_SECONDPASS
:
1698 cpi
->compressor_speed
= 1;
1700 if (cpi
->oxcf
.cpu_used
< -5)
1702 cpi
->oxcf
.cpu_used
= -5;
1705 if (cpi
->oxcf
.cpu_used
> 5)
1706 cpi
->oxcf
.cpu_used
= 5;
1709 case MODE_SECONDPASS_BEST
:
1711 cpi
->compressor_speed
= 0;
1717 cpi
->auto_worst_q
= 1;
1719 cpi
->oxcf
.worst_allowed_q
= q_trans
[oxcf
->worst_allowed_q
];
1720 cpi
->oxcf
.best_allowed_q
= q_trans
[oxcf
->best_allowed_q
];
1722 if (oxcf
->fixed_q
>= 0)
1724 if (oxcf
->worst_allowed_q
< 0)
1725 cpi
->oxcf
.fixed_q
= q_trans
[0];
1727 cpi
->oxcf
.fixed_q
= q_trans
[oxcf
->worst_allowed_q
];
1729 if (oxcf
->alt_q
< 0)
1730 cpi
->oxcf
.alt_q
= q_trans
[0];
1732 cpi
->oxcf
.alt_q
= q_trans
[oxcf
->alt_q
];
1734 if (oxcf
->key_q
< 0)
1735 cpi
->oxcf
.key_q
= q_trans
[0];
1737 cpi
->oxcf
.key_q
= q_trans
[oxcf
->key_q
];
1739 if (oxcf
->gold_q
< 0)
1740 cpi
->oxcf
.gold_q
= q_trans
[0];
1742 cpi
->oxcf
.gold_q
= q_trans
[oxcf
->gold_q
];
1746 cpi
->baseline_gf_interval
= cpi
->oxcf
.alt_freq
? cpi
->oxcf
.alt_freq
: DEFAULT_GF_INTERVAL
;
1748 cpi
->ref_frame_flags
= VP8_ALT_FLAG
| VP8_GOLD_FLAG
| VP8_LAST_FLAG
;
1750 //cpi->use_golden_frame_only = 0;
1751 //cpi->use_last_frame_only = 0;
1752 cm
->refresh_golden_frame
= 0;
1753 cm
->refresh_last_frame
= 1;
1754 cm
->refresh_entropy_probs
= 1;
1756 if (cpi
->oxcf
.token_partitions
>= 0 && cpi
->oxcf
.token_partitions
<= 3)
1757 cm
->multi_token_partition
= (TOKEN_PARTITION
) cpi
->oxcf
.token_partitions
;
1759 setup_features(cpi
);
1764 for (i
= 0; i
< MAX_MB_SEGMENTS
; i
++)
1765 cpi
->segment_encode_breakout
[i
] = cpi
->oxcf
.encode_breakout
;
1768 // At the moment the first order values may not be > MAXQ
1769 if (cpi
->oxcf
.fixed_q
> MAXQ
)
1770 cpi
->oxcf
.fixed_q
= MAXQ
;
1772 // local file playback mode == really big buffer
1773 if (cpi
->oxcf
.end_usage
== USAGE_LOCAL_FILE_PLAYBACK
)
1775 cpi
->oxcf
.starting_buffer_level
= 60;
1776 cpi
->oxcf
.optimal_buffer_level
= 60;
1777 cpi
->oxcf
.maximum_buffer_size
= 240;
1781 // Convert target bandwidth from Kbit/s to Bit/s
1782 cpi
->oxcf
.target_bandwidth
*= 1000;
1784 cpi
->oxcf
.starting_buffer_level
*= cpi
->oxcf
.target_bandwidth
;
1786 if (cpi
->oxcf
.optimal_buffer_level
== 0)
1787 cpi
->oxcf
.optimal_buffer_level
= cpi
->oxcf
.target_bandwidth
/ 8;
1789 cpi
->oxcf
.optimal_buffer_level
*= cpi
->oxcf
.target_bandwidth
;
1791 if (cpi
->oxcf
.maximum_buffer_size
== 0)
1792 cpi
->oxcf
.maximum_buffer_size
= cpi
->oxcf
.target_bandwidth
/ 8;
1794 cpi
->oxcf
.maximum_buffer_size
*= cpi
->oxcf
.target_bandwidth
;
1796 cpi
->buffer_level
= cpi
->oxcf
.starting_buffer_level
;
1797 cpi
->bits_off_target
= cpi
->oxcf
.starting_buffer_level
;
1799 vp8_new_frame_rate(cpi
, cpi
->oxcf
.frame_rate
);
1800 cpi
->worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1801 cpi
->active_worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1802 cpi
->avg_frame_qindex
= cpi
->oxcf
.worst_allowed_q
;
1803 cpi
->best_quality
= cpi
->oxcf
.best_allowed_q
;
1804 cpi
->active_best_quality
= cpi
->oxcf
.best_allowed_q
;
1805 cpi
->buffered_mode
= (cpi
->oxcf
.optimal_buffer_level
> 0) ? TRUE
: FALSE
;
1808 cpi
->total_actual_bits
= 0;
1809 cpi
->total_target_vs_actual
= 0;
1811 // Only allow dropped frames in buffered mode
1812 cpi
->drop_frames_allowed
= cpi
->oxcf
.allow_df
&& cpi
->buffered_mode
;
1814 cm
->filter_type
= (LOOPFILTERTYPE
) cpi
->filter_type
;
1816 if (!cm
->use_bilinear_mc_filter
)
1817 cm
->mcomp_filter_type
= SIXTAP
;
1819 cm
->mcomp_filter_type
= BILINEAR
;
1821 cpi
->target_bandwidth
= cpi
->oxcf
.target_bandwidth
;
1823 cm
->Width
= cpi
->oxcf
.Width
;
1824 cm
->Height
= cpi
->oxcf
.Height
;
1826 cm
->horiz_scale
= cpi
->horiz_scale
;
1827 cm
->vert_scale
= cpi
->vert_scale
;
1829 cpi
->intra_frame_target
= (4 * (cm
->Width
+ cm
->Height
) / 15) * 1000; // As per VP8
1831 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1832 if (cpi
->oxcf
.Sharpness
> 7)
1833 cpi
->oxcf
.Sharpness
= 7;
1835 cm
->sharpness_level
= cpi
->oxcf
.Sharpness
;
1837 if (cm
->horiz_scale
!= NORMAL
|| cm
->vert_scale
!= NORMAL
)
1839 int UNINITIALIZED_IS_SAFE(hr
), UNINITIALIZED_IS_SAFE(hs
);
1840 int UNINITIALIZED_IS_SAFE(vr
), UNINITIALIZED_IS_SAFE(vs
);
1842 Scale2Ratio(cm
->horiz_scale
, &hr
, &hs
);
1843 Scale2Ratio(cm
->vert_scale
, &vr
, &vs
);
1845 // always go to the next whole number
1846 cm
->Width
= (hs
- 1 + cpi
->oxcf
.Width
* hr
) / hs
;
1847 cm
->Height
= (vs
- 1 + cpi
->oxcf
.Height
* vr
) / vs
;
1850 if (((cm
->Width
+ 15) & 0xfffffff0) != cm
->last_frame
.y_width
||
1851 ((cm
->Height
+ 15) & 0xfffffff0) != cm
->last_frame
.y_height
||
1852 cm
->last_frame
.y_width
== 0)
1854 alloc_raw_frame_buffers(cpi
);
1855 vp8_alloc_compressor_data(cpi
);
1858 // Clamp KF frame size to quarter of data rate
1859 if (cpi
->intra_frame_target
> cpi
->target_bandwidth
>> 2)
1860 cpi
->intra_frame_target
= cpi
->target_bandwidth
>> 2;
1862 if (cpi
->oxcf
.fixed_q
>= 0)
1864 cpi
->last_q
[0] = cpi
->oxcf
.fixed_q
;
1865 cpi
->last_q
[1] = cpi
->oxcf
.fixed_q
;
1868 cpi
->Speed
= cpi
->oxcf
.cpu_used
;
1870 // force to allowlag to 0 if lag_in_frames is 0;
1871 if (cpi
->oxcf
.lag_in_frames
== 0)
1873 cpi
->oxcf
.allow_lag
= 0;
1875 // Limit on lag buffers as these are not currently dynamically allocated
1876 else if (cpi
->oxcf
.lag_in_frames
> MAX_LAG_BUFFERS
)
1877 cpi
->oxcf
.lag_in_frames
= MAX_LAG_BUFFERS
;
1879 // force play_alternate to 0 if allow_lag is 0, lag_in_frames is too small, Mode is real time or one pass compress enabled.
1880 if (cpi
->oxcf
.allow_lag
== 0 || cpi
->oxcf
.lag_in_frames
<= 5 || (cpi
->oxcf
.Mode
< MODE_SECONDPASS
))
1882 cpi
->oxcf
.play_alternate
= 0;
1883 cpi
->ref_frame_flags
= cpi
->ref_frame_flags
& ~VP8_ALT_FLAG
;
1887 cpi
->last_alt_ref_sei
= -1;
1888 cpi
->is_src_frame_alt_ref
= 0;
1891 // Experimental RD Code
1892 cpi
->frame_distortion
= 0;
1893 cpi
->last_frame_distortion
= 0;
1898 #define M_LOG2_E 0.693147180559945309417
1899 #define log2f(x) (log (x) / (float) M_LOG2_E)
1900 static void cal_mvsadcosts(int *mvsadcost
[2])
1904 mvsadcost
[0] [0] = 300;
1905 mvsadcost
[1] [0] = 300;
1909 double z
= 256 * (2 * (log2f(2 * i
) + .6));
1910 mvsadcost
[0][i
] = (int) z
;
1911 mvsadcost
[1][i
] = (int) z
;
1912 mvsadcost
[0][-i
] = (int) z
;
1913 mvsadcost
[1][-i
] = (int) z
;
1915 while (++i
<= mv_max
);
1918 VP8_PTR
vp8_create_compressor(VP8_CONFIG
*oxcf
)
1930 cpi
= ctx
.cpi
= vpx_memalign(32, sizeof(VP8_COMP
));
1931 // Check that the CPI instance is valid
1937 vpx_memset(cpi
, 0, sizeof(VP8_COMP
));
1939 if (setjmp(cm
->error
.jmp
))
1941 VP8_PTR ptr
= ctx
.ptr
;
1943 ctx
.cpi
->common
.error
.setjmp
= 0;
1944 vp8_remove_compressor(&ptr
);
1948 cpi
->common
.error
.setjmp
= 1;
1950 CHECK_MEM_ERROR(cpi
->rdtok
, vpx_calloc(256 * 3 / 2, sizeof(TOKENEXTRA
)));
1951 CHECK_MEM_ERROR(cpi
->mb
.ss
, vpx_calloc(sizeof(search_site
), (MAX_MVSEARCH_STEPS
* 8) + 1));
1953 vp8_cmachine_specific_config(cpi
);
1954 vp8_create_common(&cpi
->common
);
1956 vp8_init_config((VP8_PTR
)cpi
, oxcf
);
1958 memcpy(cpi
->base_skip_false_prob
, vp8cx_base_skip_false_prob
, sizeof(vp8cx_base_skip_false_prob
));
1959 cpi
->common
.current_video_frame
= 0;
1960 cpi
->kf_overspend_bits
= 0;
1961 cpi
->kf_bitrate_adjustment
= 0;
1962 cpi
->frames_till_gf_update_due
= 0;
1963 cpi
->gf_overspend_bits
= 0;
1964 cpi
->non_gf_bitrate_adjustment
= 0;
1965 cpi
->prob_last_coded
= 128;
1966 cpi
->prob_gf_coded
= 128;
1967 cpi
->prob_intra_coded
= 63;
1969 // Prime the recent reference frame useage counters.
1970 // Hereafter they will be maintained as a sort of moving average
1971 cpi
->recent_ref_frame_usage
[INTRA_FRAME
] = 1;
1972 cpi
->recent_ref_frame_usage
[LAST_FRAME
] = 1;
1973 cpi
->recent_ref_frame_usage
[GOLDEN_FRAME
] = 1;
1974 cpi
->recent_ref_frame_usage
[ALTREF_FRAME
] = 1;
1976 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1977 cpi
->common
.ref_frame_sign_bias
[ALTREF_FRAME
] = 1;
1979 cpi
->gf_decay_rate
= 0;
1980 cpi
->baseline_gf_interval
= DEFAULT_GF_INTERVAL
;
1982 cpi
->gold_is_last
= 0 ;
1983 cpi
->alt_is_last
= 0 ;
1984 cpi
->gold_is_alt
= 0 ;
1988 // Create the encoder segmentation map and set all entries to 0
1989 CHECK_MEM_ERROR(cpi
->segmentation_map
, vpx_calloc(cpi
->common
.mb_rows
* cpi
->common
.mb_cols
, 1));
1990 CHECK_MEM_ERROR(cpi
->active_map
, vpx_calloc(cpi
->common
.mb_rows
* cpi
->common
.mb_cols
, 1));
1991 vpx_memset(cpi
->active_map
, 1, (cpi
->common
.mb_rows
* cpi
->common
.mb_cols
));
1992 cpi
->active_map_enabled
= 0;
1994 // Create the first pass motion map structure and set to 0
1995 CHECK_MEM_ERROR(cpi
->fp_motion_map
, vpx_calloc(cpi
->common
.MBs
, 1));
1998 // Experimental code for lagged and one pass
1999 // Initialise one_pass GF frames stats
2000 // Update stats used for GF selection
2003 cpi
->one_pass_frame_index
= 0;
2005 for (i
= 0; i
< MAX_LAG_BUFFERS
; i
++)
2007 cpi
->one_pass_frame_stats
[i
].frames_so_far
= 0;
2008 cpi
->one_pass_frame_stats
[i
].frame_intra_error
= 0.0;
2009 cpi
->one_pass_frame_stats
[i
].frame_coded_error
= 0.0;
2010 cpi
->one_pass_frame_stats
[i
].frame_pcnt_inter
= 0.0;
2011 cpi
->one_pass_frame_stats
[i
].frame_pcnt_motion
= 0.0;
2012 cpi
->one_pass_frame_stats
[i
].frame_mvr
= 0.0;
2013 cpi
->one_pass_frame_stats
[i
].frame_mvr_abs
= 0.0;
2014 cpi
->one_pass_frame_stats
[i
].frame_mvc
= 0.0;
2015 cpi
->one_pass_frame_stats
[i
].frame_mvc_abs
= 0.0;
2020 // Should we use the cyclic refresh method.
2021 // Currently this is tied to error resilliant mode
2022 cpi
->cyclic_refresh_mode_enabled
= cpi
->oxcf
.error_resilient_mode
;
2023 cpi
->cyclic_refresh_mode_max_mbs_perframe
= (cpi
->common
.mb_rows
* cpi
->common
.mb_cols
) / 40;
2024 cpi
->cyclic_refresh_mode_index
= 0;
2025 cpi
->cyclic_refresh_q
= 32;
2027 if (cpi
->cyclic_refresh_mode_enabled
)
2029 CHECK_MEM_ERROR(cpi
->cyclic_refresh_map
, vpx_calloc((cpi
->common
.mb_rows
* cpi
->common
.mb_cols
), 1));
2032 cpi
->cyclic_refresh_map
= (signed char *) NULL
;
2034 // Test function for segmentation
2035 //segmentation_test_function((VP8_PTR) cpi);
2037 // Loop filter mode / ref deltas test function
2038 //mode_ref_lf_test_function(cpi);
2040 #ifdef ENTROPY_STATS
2041 init_context_counters();
2046 cpi
->intra_rd_opt
= 1;
2050 cpi
->frames_since_key
= 8; // Give a sensible default for the first frame.
2051 cpi
->key_frame_frequency
= cpi
->oxcf
.key_freq
;
2053 cpi
->source_alt_ref_pending
= FALSE
;
2054 cpi
->source_alt_ref_active
= FALSE
;
2055 cpi
->common
.refresh_alt_ref_frame
= 0;
2057 cpi
->b_calculate_psnr
= CONFIG_PSNR
;
2059 cpi
->b_calculate_ssimg
= 0;
2064 if (cpi
->b_calculate_psnr
)
2066 cpi
->total_sq_error
= 0.0;
2067 cpi
->total_sq_error2
= 0.0;
2072 cpi
->totalp_y
= 0.0;
2073 cpi
->totalp_u
= 0.0;
2074 cpi
->totalp_v
= 0.0;
2076 cpi
->tot_recode_hits
= 0;
2077 cpi
->summed_quality
= 0;
2078 cpi
->summed_weights
= 0;
2081 if (cpi
->b_calculate_ssimg
)
2083 cpi
->total_ssimg_y
= 0;
2084 cpi
->total_ssimg_u
= 0;
2085 cpi
->total_ssimg_v
= 0;
2086 cpi
->total_ssimg_all
= 0;
2090 #define LLONG_MAX 9223372036854775807LL
2092 cpi
->first_time_stamp_ever
= LLONG_MAX
;
2096 cpi
->frames_till_gf_update_due
= 0;
2097 cpi
->key_frame_count
= 1;
2098 cpi
->tot_key_frame_bits
= 0;
2100 cpi
->ni_av_qi
= cpi
->oxcf
.worst_allowed_q
;
2103 cpi
->total_byte_count
= 0;
2105 cpi
->drop_frame
= 0;
2106 cpi
->drop_count
= 0;
2107 cpi
->max_drop_count
= 0;
2108 cpi
->max_consec_dropped_frames
= 4;
2110 cpi
->rate_correction_factor
= 1.0;
2111 cpi
->key_frame_rate_correction_factor
= 1.0;
2112 cpi
->gf_rate_correction_factor
= 1.0;
2113 cpi
->est_max_qcorrection_factor
= 1.0;
2115 cpi
->mb
.mvcost
[0] = &cpi
->mb
.mvcosts
[0][mv_max
+1];
2116 cpi
->mb
.mvcost
[1] = &cpi
->mb
.mvcosts
[1][mv_max
+1];
2117 cpi
->mb
.mvsadcost
[0] = &cpi
->mb
.mvsadcosts
[0][mv_max
+1];
2118 cpi
->mb
.mvsadcost
[1] = &cpi
->mb
.mvsadcosts
[1][mv_max
+1];
2120 cal_mvsadcosts(cpi
->mb
.mvsadcost
);
2122 for (i
= 0; i
< KEY_FRAME_CONTEXT
; i
++)
2124 cpi
->prior_key_frame_size
[i
] = cpi
->intra_frame_target
;
2125 cpi
->prior_key_frame_distance
[i
] = (int)cpi
->output_frame_rate
;
2128 cpi
->check_freq
[0] = 15;
2129 cpi
->check_freq
[1] = 15;
2131 #ifdef OUTPUT_YUV_SRC
2132 yuv_file
= fopen("bd.yuv", "ab");
2136 framepsnr
= fopen("framepsnr.stt", "a");
2137 kf_list
= fopen("kf_list.stt", "w");
2140 cpi
->output_pkt_list
= oxcf
->output_pkt_list
;
2142 #if !(CONFIG_REALTIME_ONLY)
2146 vp8_init_first_pass(cpi
);
2148 else if (cpi
->pass
== 2)
2150 cpi
->stats_in
= oxcf
->two_pass_stats_in
.buf
;
2151 cpi
->stats_in_end
= cpi
->stats_in
2152 + oxcf
->two_pass_stats_in
.sz
/ sizeof(FIRSTPASS_STATS
)
2154 vp8_init_second_pass(cpi
);
2159 if (cpi
->compressor_speed
== 2)
2161 cpi
->cpu_freq
= 0; //vp8_get_processor_freq();
2162 cpi
->avg_encode_time
= 0;
2163 cpi
->avg_pick_mode_time
= 0;
2166 vp8_set_speed_features(cpi
);
2168 // Set starting values of RD threshold multipliers (128 = *1)
2169 for (i
= 0; i
< MAX_MODES
; i
++)
2171 cpi
->rd_thresh_mult
[i
] = 128;
2174 #ifdef ENTROPY_STATS
2175 init_mv_ref_counts();
2178 vp8cx_create_encoder_threads(cpi
);
2180 cpi
->fn_ptr
.sdf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x16
);
2181 cpi
->fn_ptr
.vf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, var16x16
);
2182 cpi
->fn_ptr
.svf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, subpixvar16x16
);
2183 cpi
->fn_ptr
.sdx3f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x16x3
);
2184 cpi
->fn_ptr
.sdx4df
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x16x4d
);
2186 #if !(CONFIG_REALTIME_ONLY)
2187 cpi
->full_search_sad
= SEARCH_INVOKE(&cpi
->rtcd
.search
, full_search
);
2189 cpi
->diamond_search_sad
= SEARCH_INVOKE(&cpi
->rtcd
.search
, diamond_search
);
2191 cpi
->ready_for_new_frame
= 1;
2193 cpi
->source_encode_index
= 0;
2195 // make sure frame 1 is okay
2196 cpi
->error_bins
[0] = cpi
->common
.MBs
;
2198 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2199 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2200 vp8cx_init_quantizer(cpi
);
2202 vp8_init_loop_filter(cm
);
2203 cm
->last_frame_type
= KEY_FRAME
;
2204 cm
->last_filter_type
= cm
->filter_type
;
2205 cm
->last_sharpness_level
= cm
->sharpness_level
;
2207 cpi
->common
.error
.setjmp
= 0;
2208 return (VP8_PTR
) cpi
;
2213 void vp8_remove_compressor(VP8_PTR
*ptr
)
2215 VP8_COMP
*cpi
= (VP8_COMP
*)(*ptr
);
2220 if (cpi
&& (cpi
->common
.current_video_frame
> 0))
2222 #if !(CONFIG_REALTIME_ONLY)
2226 vp8_end_second_pass(cpi
);
2231 #ifdef ENTROPY_STATS
2232 print_context_counters();
2233 print_tree_update_probs();
2234 print_mode_context();
2241 FILE *f
= fopen("opsnr.stt", "a");
2242 double time_encoded
= (cpi
->source_end_time_stamp
- cpi
->first_time_stamp_ever
) / 10000000.000;
2243 double total_encode_time
= (cpi
->time_receive_data
+ cpi
->time_compress_data
) / 1000.000;
2244 double dr
= (double)cpi
->bytes
* (double) 8 / (double)1000 / time_encoded
;
2246 if (cpi
->b_calculate_psnr
)
2248 double samples
= 3.0 / 2 * cpi
->count
* cpi
->common
.last_frame
.y_width
* cpi
->common
.last_frame
.y_height
;
2249 double total_psnr
= vp8_mse2psnr(samples
, 255.0, cpi
->total_sq_error
);
2250 double total_psnr2
= vp8_mse2psnr(samples
, 255.0, cpi
->total_sq_error2
);
2251 double total_ssim
= 100 * pow(cpi
->summed_quality
/ cpi
->summed_weights
, 8.0);
2253 fprintf(f
, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time(us)\n");
2254 fprintf(f
, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f %8.0f\n",
2255 dr
, cpi
->total
/ cpi
->count
, total_psnr
, cpi
->totalp
/ cpi
->count
, total_psnr2
, total_ssim
,
2259 if (cpi
->b_calculate_ssimg
)
2261 fprintf(f
, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(us)\n");
2262 fprintf(f
, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr
,
2263 cpi
->total_ssimg_y
/ cpi
->count
, cpi
->total_ssimg_u
/ cpi
->count
,
2264 cpi
->total_ssimg_v
/ cpi
->count
, cpi
->total_ssimg_all
/ cpi
->count
, total_encode_time
);
2269 f
= fopen("qskip.stt", "a");
2270 fprintf(f
, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi
->oxcf
.best_allowed_q
, cpi
->oxcf
.worst_allowed_q
, skiptruecount
, skipfalsecount
);
2281 if (cpi
->compressor_speed
== 2)
2284 FILE *f
= fopen("cxspeed.stt", "a");
2285 cnt_pm
/= cpi
->common
.MBs
;
2287 for (i
= 0; i
< 16; i
++)
2288 fprintf(f
, "%5d", frames_at_speed
[i
]);
2291 //fprintf(f, "%10d PM %10d %10d %10d EF %10d %10d %10d\n", cpi->Speed, cpi->avg_pick_mode_time, (tot_pm/cnt_pm), cnt_pm, cpi->avg_encode_time, 0, 0);
2300 extern int count_mb_seg
[4];
2301 FILE *f
= fopen("modes.stt", "a");
2302 double dr
= (double)cpi
->oxcf
.frame_rate
* (double)bytes
* (double)8 / (double)count
/ (double)1000 ;
2303 fprintf(f
, "intra_mode in Intra Frames:\n");
2304 fprintf(f
, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes
[0], y_modes
[1], y_modes
[2], y_modes
[3], y_modes
[4]);
2305 fprintf(f
, "UV:%8d, %8d, %8d, %8d\n", uv_modes
[0], uv_modes
[1], uv_modes
[2], uv_modes
[3]);
2310 for (i
= 0; i
< 10; i
++)
2311 fprintf(f
, "%8d, ", b_modes
[i
]);
2317 fprintf(f
, "Modes in Inter Frames:\n");
2318 fprintf(f
, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2319 inter_y_modes
[0], inter_y_modes
[1], inter_y_modes
[2], inter_y_modes
[3], inter_y_modes
[4],
2320 inter_y_modes
[5], inter_y_modes
[6], inter_y_modes
[7], inter_y_modes
[8], inter_y_modes
[9]);
2321 fprintf(f
, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes
[0], inter_uv_modes
[1], inter_uv_modes
[2], inter_uv_modes
[3]);
2326 for (i
= 0; i
< 15; i
++)
2327 fprintf(f
, "%8d, ", inter_b_modes
[i
]);
2332 fprintf(f
, "P:%8d, %8d, %8d, %8d\n", count_mb_seg
[0], count_mb_seg
[1], count_mb_seg
[2], count_mb_seg
[3]);
2333 fprintf(f
, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes
[LEFT4X4
], inter_b_modes
[ABOVE4X4
], inter_b_modes
[ZERO4X4
], inter_b_modes
[NEW4X4
]);
2341 #ifdef ENTROPY_STATS
2344 FILE *fmode
= fopen("modecontext.c", "w");
2346 fprintf(fmode
, "\n#include \"entropymode.h\"\n\n");
2347 fprintf(fmode
, "const unsigned int vp8_kf_default_bmode_counts ");
2348 fprintf(fmode
, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2350 for (i
= 0; i
< 10; i
++)
2353 fprintf(fmode
, " { //Above Mode : %d\n", i
);
2355 for (j
= 0; j
< 10; j
++)
2358 fprintf(fmode
, " {");
2360 for (k
= 0; k
< 10; k
++)
2362 if (!intra_mode_stats
[i
][j
][k
])
2363 fprintf(fmode
, " %5d, ", 1);
2365 fprintf(fmode
, " %5d, ", intra_mode_stats
[i
][j
][k
]);
2368 fprintf(fmode
, "}, // left_mode %d\n", j
);
2372 fprintf(fmode
, " },\n");
2376 fprintf(fmode
, "};\n");
2381 #if defined(SECTIONBITS_OUTPUT)
2386 FILE *f
= fopen("tokenbits.stt", "a");
2388 for (i
= 0; i
< 28; i
++)
2389 fprintf(f
, "%8d", (int)(Sectionbits
[i
] / 256));
2399 printf("\n_pick_loop_filter_level:%d\n", cpi
->time_pick_lpf
/ 1000);
2400 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2401 printf("%6d %10ld %10ld %10ld %10ld\n", cpi
->common
.current_video_frame
, cpi
->time_receive_data
/ 1000, cpi
->time_encode_mb_row
/ 1000, cpi
->time_compress_data
/ 1000, (cpi
->time_receive_data
+ cpi
->time_compress_data
) / 1000);
2407 vp8cx_remove_encoder_threads(cpi
);
2409 vp8_dealloc_compressor_data(cpi
);
2410 vpx_free(cpi
->mb
.ss
);
2412 vpx_free(cpi
->rdtok
);
2413 vpx_free(cpi
->cyclic_refresh_map
);
2415 vp8_remove_common(&cpi
->common
);
2419 #ifdef OUTPUT_YUV_SRC
2439 static uint64_t calc_plane_error(unsigned char *orig
, int orig_stride
,
2440 unsigned char *recon
, int recon_stride
,
2441 unsigned int cols
, unsigned int rows
,
2442 vp8_variance_rtcd_vtable_t
*rtcd
)
2444 unsigned int row
, col
;
2445 uint64_t total_sse
= 0;
2448 for (row
= 0; row
+ 16 <= rows
; row
+= 16)
2450 for (col
= 0; col
+ 16 <= cols
; col
+= 16)
2454 VARIANCE_INVOKE(rtcd
, mse16x16
)(orig
+ col
, orig_stride
,
2455 recon
+ col
, recon_stride
,
2460 /* Handle odd-sized width */
2463 unsigned int border_row
, border_col
;
2464 unsigned char *border_orig
= orig
;
2465 unsigned char *border_recon
= recon
;
2467 for (border_row
= 0; border_row
< 16; border_row
++)
2469 for (border_col
= col
; border_col
< cols
; border_col
++)
2471 diff
= border_orig
[border_col
] - border_recon
[border_col
];
2472 total_sse
+= diff
* diff
;
2475 border_orig
+= orig_stride
;
2476 border_recon
+= recon_stride
;
2480 orig
+= orig_stride
* 16;
2481 recon
+= recon_stride
* 16;
2484 /* Handle odd-sized height */
2485 for (; row
< rows
; row
++)
2487 for (col
= 0; col
< cols
; col
++)
2489 diff
= orig
[col
] - recon
[col
];
2490 total_sse
+= diff
* diff
;
2493 orig
+= orig_stride
;
2494 recon
+= recon_stride
;
2501 static void generate_psnr_packet(VP8_COMP
*cpi
)
2503 YV12_BUFFER_CONFIG
*orig
= cpi
->Source
;
2504 YV12_BUFFER_CONFIG
*recon
= cpi
->common
.frame_to_show
;
2505 struct vpx_codec_cx_pkt pkt
;
2508 unsigned int width
= cpi
->common
.Width
;
2509 unsigned int height
= cpi
->common
.Height
;
2511 pkt
.kind
= VPX_CODEC_PSNR_PKT
;
2512 sse
= calc_plane_error(orig
->y_buffer
, orig
->y_stride
,
2513 recon
->y_buffer
, recon
->y_stride
,
2515 IF_RTCD(&cpi
->rtcd
.variance
));
2516 pkt
.data
.psnr
.sse
[0] = sse
;
2517 pkt
.data
.psnr
.sse
[1] = sse
;
2518 pkt
.data
.psnr
.samples
[0] = width
* height
;
2519 pkt
.data
.psnr
.samples
[1] = width
* height
;
2521 width
= (width
+ 1) / 2;
2522 height
= (height
+ 1) / 2;
2524 sse
= calc_plane_error(orig
->u_buffer
, orig
->uv_stride
,
2525 recon
->u_buffer
, recon
->uv_stride
,
2527 IF_RTCD(&cpi
->rtcd
.variance
));
2528 pkt
.data
.psnr
.sse
[0] += sse
;
2529 pkt
.data
.psnr
.sse
[2] = sse
;
2530 pkt
.data
.psnr
.samples
[0] += width
* height
;
2531 pkt
.data
.psnr
.samples
[2] = width
* height
;
2533 sse
= calc_plane_error(orig
->v_buffer
, orig
->uv_stride
,
2534 recon
->v_buffer
, recon
->uv_stride
,
2536 IF_RTCD(&cpi
->rtcd
.variance
));
2537 pkt
.data
.psnr
.sse
[0] += sse
;
2538 pkt
.data
.psnr
.sse
[3] = sse
;
2539 pkt
.data
.psnr
.samples
[0] += width
* height
;
2540 pkt
.data
.psnr
.samples
[3] = width
* height
;
2542 for (i
= 0; i
< 4; i
++)
2543 pkt
.data
.psnr
.psnr
[i
] = vp8_mse2psnr(pkt
.data
.psnr
.samples
[i
], 255.0,
2544 pkt
.data
.psnr
.sse
[i
]);
2546 vpx_codec_pkt_list_add(cpi
->output_pkt_list
, &pkt
);
2550 int vp8_use_as_reference(VP8_PTR ptr
, int ref_frame_flags
)
2552 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
2554 if (ref_frame_flags
> 7)
2557 cpi
->ref_frame_flags
= ref_frame_flags
;
2560 int vp8_update_reference(VP8_PTR ptr
, int ref_frame_flags
)
2562 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
2564 if (ref_frame_flags
> 7)
2567 cpi
->common
.refresh_golden_frame
= 0;
2568 cpi
->common
.refresh_alt_ref_frame
= 0;
2569 cpi
->common
.refresh_last_frame
= 0;
2571 if (ref_frame_flags
& VP8_LAST_FLAG
)
2572 cpi
->common
.refresh_last_frame
= 1;
2574 if (ref_frame_flags
& VP8_GOLD_FLAG
)
2575 cpi
->common
.refresh_golden_frame
= 1;
2577 if (ref_frame_flags
& VP8_ALT_FLAG
)
2578 cpi
->common
.refresh_alt_ref_frame
= 1;
2583 int vp8_get_reference(VP8_PTR ptr
, VP8_REFFRAME ref_frame_flag
, YV12_BUFFER_CONFIG
*sd
)
2585 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
2586 VP8_COMMON
*cm
= &cpi
->common
;
2588 if (ref_frame_flag
== VP8_LAST_FLAG
)
2589 vp8_yv12_copy_frame_ptr(&cm
->last_frame
, sd
);
2591 else if (ref_frame_flag
== VP8_GOLD_FLAG
)
2592 vp8_yv12_copy_frame_ptr(&cm
->golden_frame
, sd
);
2594 else if (ref_frame_flag
== VP8_ALT_FLAG
)
2595 vp8_yv12_copy_frame_ptr(&cm
->alt_ref_frame
, sd
);
2602 int vp8_set_reference(VP8_PTR ptr
, VP8_REFFRAME ref_frame_flag
, YV12_BUFFER_CONFIG
*sd
)
2604 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
2605 VP8_COMMON
*cm
= &cpi
->common
;
2607 if (ref_frame_flag
== VP8_LAST_FLAG
)
2608 vp8_yv12_copy_frame_ptr(sd
, &cm
->last_frame
);
2610 else if (ref_frame_flag
== VP8_GOLD_FLAG
)
2611 vp8_yv12_copy_frame_ptr(sd
, &cm
->golden_frame
);
2613 else if (ref_frame_flag
== VP8_ALT_FLAG
)
2614 vp8_yv12_copy_frame_ptr(sd
, &cm
->alt_ref_frame
);
2621 int vp8_update_entropy(VP8_PTR comp
, int update
)
2623 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
2624 VP8_COMMON
*cm
= &cpi
->common
;
2625 cm
->refresh_entropy_probs
= update
;
2630 void vp8_write_yuv_frame(const char *name
, YV12_BUFFER_CONFIG
*s
)
2632 FILE *yuv_file
= fopen(name
, "ab");
2633 unsigned char *src
= s
->y_buffer
;
2634 int h
= s
->y_height
;
2638 fwrite(src
, s
->y_width
, 1, yuv_file
);
2648 fwrite(src
, s
->uv_width
, 1, yuv_file
);
2649 src
+= s
->uv_stride
;
2658 fwrite(src
, s
->uv_width
, 1, yuv_file
);
2659 src
+= s
->uv_stride
;
2666 static void scale_and_extend_source(YV12_BUFFER_CONFIG
*sd
, VP8_COMP
*cpi
)
2668 VP8_COMMON
*cm
= &cpi
->common
;
2670 // are we resizing the image
2671 if (cm
->horiz_scale
!= 0 || cm
->vert_scale
!= 0)
2673 #if CONFIG_SPATIAL_RESAMPLING
2674 int UNINITIALIZED_IS_SAFE(hr
), UNINITIALIZED_IS_SAFE(hs
);
2675 int UNINITIALIZED_IS_SAFE(vr
), UNINITIALIZED_IS_SAFE(vs
);
2678 if (cm
->vert_scale
== 3)
2683 Scale2Ratio(cm
->horiz_scale
, &hr
, &hs
);
2684 Scale2Ratio(cm
->vert_scale
, &vr
, &vs
);
2686 vp8_scale_frame(sd
, &cpi
->scaled_source
, cm
->temp_scale_frame
.y_buffer
,
2687 tmp_height
, hs
, hr
, vs
, vr
, 0);
2689 cpi
->Source
= &cpi
->scaled_source
;
2692 // we may need to copy to a buffer so we can extend the image...
2693 else if (cm
->Width
!= cm
->last_frame
.y_width
||
2694 cm
->Height
!= cm
->last_frame
.y_height
)
2696 //vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2698 vp8_yv12_copy_src_frame_func_neon(sd
, &cpi
->scaled_source
);
2700 vp8_yv12_copy_frame_ptr(sd
, &cpi
->scaled_source
);
2703 cpi
->Source
= &cpi
->scaled_source
;
2706 vp8_extend_to_multiple_of16(cpi
->Source
, cm
->Width
, cm
->Height
);
2709 static void resize_key_frame(VP8_COMP
*cpi
)
2711 #if CONFIG_SPATIAL_RESAMPLING
2712 VP8_COMMON
*cm
= &cpi
->common
;
2714 // Do we need to apply resampling for one pass cbr.
2715 // In one pass this is more limited than in two pass cbr
2716 // The test and any change is only made one per key frame sequence
2717 if (cpi
->oxcf
.allow_spatial_resampling
&& (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
))
2719 int UNINITIALIZED_IS_SAFE(hr
), UNINITIALIZED_IS_SAFE(hs
);
2720 int UNINITIALIZED_IS_SAFE(vr
), UNINITIALIZED_IS_SAFE(vs
);
2721 int new_width
, new_height
;
2723 // If we are below the resample DOWN watermark then scale down a notch.
2724 if (cpi
->buffer_level
< (cpi
->oxcf
.resample_down_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100))
2726 cm
->horiz_scale
= (cm
->horiz_scale
< ONETWO
) ? cm
->horiz_scale
+ 1 : ONETWO
;
2727 cm
->vert_scale
= (cm
->vert_scale
< ONETWO
) ? cm
->vert_scale
+ 1 : ONETWO
;
2729 // Should we now start scaling back up
2730 else if (cpi
->buffer_level
> (cpi
->oxcf
.resample_up_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100))
2732 cm
->horiz_scale
= (cm
->horiz_scale
> NORMAL
) ? cm
->horiz_scale
- 1 : NORMAL
;
2733 cm
->vert_scale
= (cm
->vert_scale
> NORMAL
) ? cm
->vert_scale
- 1 : NORMAL
;
2736 // Get the new hieght and width
2737 Scale2Ratio(cm
->horiz_scale
, &hr
, &hs
);
2738 Scale2Ratio(cm
->vert_scale
, &vr
, &vs
);
2739 new_width
= ((hs
- 1) + (cpi
->oxcf
.Width
* hr
)) / hs
;
2740 new_height
= ((vs
- 1) + (cpi
->oxcf
.Height
* vr
)) / vs
;
2742 // If the image size has changed we need to reallocate the buffers
2743 // and resample the source image
2744 if ((cm
->Width
!= new_width
) || (cm
->Height
!= new_height
))
2746 cm
->Width
= new_width
;
2747 cm
->Height
= new_height
;
2748 vp8_alloc_compressor_data(cpi
);
2749 scale_and_extend_source(cpi
->un_scaled_source
, cpi
);
2755 // return of 0 means drop frame
2756 static int pick_frame_size(VP8_COMP
*cpi
)
2758 VP8_COMMON
*cm
= &cpi
->common
;
2760 // First Frame is a special case
2761 if (cm
->current_video_frame
== 0)
2763 #if !(CONFIG_REALTIME_ONLY)
2766 vp8_calc_auto_iframe_target_size(cpi
);
2768 // 1 Pass there is no information on which to base size so use bandwidth per second * fixed fraction
2771 cpi
->this_frame_target
= cpi
->oxcf
.target_bandwidth
/ 2;
2773 // in error resilient mode the first frame is bigger since it likely contains
2774 // all the static background
2775 if (cpi
->oxcf
.error_resilient_mode
== 1 || (cpi
->compressor_speed
== 2))
2777 cpi
->this_frame_target
*= 3; // 5;
2780 // Key frame from VFW/auto-keyframe/first frame
2781 cm
->frame_type
= KEY_FRAME
;
2784 // Auto key frames (Only two pass will enter here)
2785 else if (cm
->frame_type
== KEY_FRAME
)
2787 vp8_calc_auto_iframe_target_size(cpi
);
2789 // Forced key frames (by interval or an external signal)
2790 else if ((cm
->frame_flags
& FRAMEFLAGS_KEY
) ||
2791 (cpi
->oxcf
.auto_key
&& (cpi
->frames_since_key
% cpi
->key_frame_frequency
== 0)))
2793 // Key frame from VFW/auto-keyframe/first frame
2794 cm
->frame_type
= KEY_FRAME
;
2796 resize_key_frame(cpi
);
2798 // Compute target frame size
2800 vp8_calc_iframe_target_size(cpi
);
2804 // INTER frame: compute target frame size
2805 cm
->frame_type
= INTER_FRAME
;
2806 vp8_calc_pframe_target_size(cpi
);
2808 // Check if we're dropping the frame:
2809 if (cpi
->drop_frame
)
2811 cpi
->drop_frame
= FALSE
;
2817 // Note target_size in bits * 256 per MB
2818 cpi
->target_bits_per_mb
= (cpi
->this_frame_target
* 256) / cpi
->common
.MBs
;
2822 static void set_quantizer(VP8_COMP
*cpi
, int Q
)
2824 VP8_COMMON
*cm
= &cpi
->common
;
2825 MACROBLOCKD
*mbd
= &cpi
->mb
.e_mbd
;
2827 cm
->base_qindex
= Q
;
2829 cm
->y1dc_delta_q
= 0;
2830 cm
->y2dc_delta_q
= 0;
2831 cm
->y2ac_delta_q
= 0;
2832 cm
->uvdc_delta_q
= 0;
2833 cm
->uvac_delta_q
= 0;
2835 // Set Segment specific quatizers
2836 mbd
->segment_feature_data
[MB_LVL_ALT_Q
][0] = cpi
->segment_feature_data
[MB_LVL_ALT_Q
][0];
2837 mbd
->segment_feature_data
[MB_LVL_ALT_Q
][1] = cpi
->segment_feature_data
[MB_LVL_ALT_Q
][1];
2838 mbd
->segment_feature_data
[MB_LVL_ALT_Q
][2] = cpi
->segment_feature_data
[MB_LVL_ALT_Q
][2];
2839 mbd
->segment_feature_data
[MB_LVL_ALT_Q
][3] = cpi
->segment_feature_data
[MB_LVL_ALT_Q
][3];
2842 static void update_alt_ref_frame_and_stats(VP8_COMP
*cpi
)
2844 VP8_COMMON
*cm
= &cpi
->common
;
2846 // Update the golden frame buffer
2847 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->alt_ref_frame
);
2849 // Select an interval before next GF or altref
2850 if (!cpi
->auto_gold
)
2851 cpi
->frames_till_gf_update_due
= cpi
->goldfreq
;
2853 if ((cpi
->pass
!= 2) && cpi
->frames_till_gf_update_due
)
2855 cpi
->current_gf_interval
= cpi
->frames_till_gf_update_due
;
2857 // Set the bits per frame that we should try and recover in subsequent inter frames
2858 // to account for the extra GF spend... note that his does not apply for GF updates
2859 // that occur coincident with a key frame as the extra cost of key frames is dealt
2862 cpi
->gf_overspend_bits
+= cpi
->projected_frame_size
;
2863 cpi
->non_gf_bitrate_adjustment
= cpi
->gf_overspend_bits
/ cpi
->frames_till_gf_update_due
;
2866 // Update data structure that monitors level of reference to last GF
2867 vpx_memset(cm
->gf_active_flags
, 1, (cm
->mb_rows
* cm
->mb_cols
));
2868 cm
->gf_active_count
= cm
->mb_rows
* cm
->mb_cols
;
2869 // this frame refreshes means next frames don't unless specified by user
2871 cpi
->common
.frames_since_golden
= 0;
2873 // Clear the alternate reference update pending flag.
2874 cpi
->source_alt_ref_pending
= FALSE
;
2876 // Set the alternate refernce frame active flag
2877 cpi
->source_alt_ref_active
= TRUE
;
2881 static void update_golden_frame_and_stats(VP8_COMP
*cpi
)
2883 VP8_COMMON
*cm
= &cpi
->common
;
2885 // Update the Golden frame reconstruction buffer if signalled and the GF usage counts.
2886 if (cm
->refresh_golden_frame
)
2888 // Update the golden frame buffer
2889 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->golden_frame
);
2891 // Select an interval before next GF
2892 if (!cpi
->auto_gold
)
2893 cpi
->frames_till_gf_update_due
= cpi
->goldfreq
;
2895 if ((cpi
->pass
!= 2) && (cpi
->frames_till_gf_update_due
> 0))
2897 cpi
->current_gf_interval
= cpi
->frames_till_gf_update_due
;
2899 // Set the bits per frame that we should try and recover in subsequent inter frames
2900 // to account for the extra GF spend... note that his does not apply for GF updates
2901 // that occur coincident with a key frame as the extra cost of key frames is dealt
2903 if ((cm
->frame_type
!= KEY_FRAME
) && !cpi
->source_alt_ref_active
)
2905 // Calcluate GF bits to be recovered
2906 // Projected size - av frame bits available for inter frames for clip as a whole
2907 cpi
->gf_overspend_bits
+= (cpi
->projected_frame_size
- cpi
->inter_frame_target
);
2910 cpi
->non_gf_bitrate_adjustment
= cpi
->gf_overspend_bits
/ cpi
->frames_till_gf_update_due
;
2914 // Update data structure that monitors level of reference to last GF
2915 vpx_memset(cm
->gf_active_flags
, 1, (cm
->mb_rows
* cm
->mb_cols
));
2916 cm
->gf_active_count
= cm
->mb_rows
* cm
->mb_cols
;
2918 // this frame refreshes means next frames don't unless specified by user
2919 cm
->refresh_golden_frame
= 0;
2920 cpi
->common
.frames_since_golden
= 0;
2922 //if ( cm->frame_type == KEY_FRAME )
2924 cpi
->recent_ref_frame_usage
[INTRA_FRAME
] = 1;
2925 cpi
->recent_ref_frame_usage
[LAST_FRAME
] = 1;
2926 cpi
->recent_ref_frame_usage
[GOLDEN_FRAME
] = 1;
2927 cpi
->recent_ref_frame_usage
[ALTREF_FRAME
] = 1;
2931 // // Carry a potrtion of count over to begining of next gf sequence
2932 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
2933 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
2934 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
2935 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
2938 // ******** Fixed Q test code only ************
2939 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
2940 if (cpi
->oxcf
.fixed_q
>= 0 &&
2941 cpi
->oxcf
.play_alternate
&& !cpi
->common
.refresh_alt_ref_frame
)
2943 cpi
->source_alt_ref_pending
= TRUE
;
2944 cpi
->frames_till_gf_update_due
= cpi
->baseline_gf_interval
;
2947 if (!cpi
->source_alt_ref_pending
)
2948 cpi
->source_alt_ref_active
= FALSE
;
2950 // Decrement count down till next gf
2951 if (cpi
->frames_till_gf_update_due
> 0)
2952 cpi
->frames_till_gf_update_due
--;
2955 else if (!cpi
->common
.refresh_alt_ref_frame
)
2957 // Decrement count down till next gf
2958 if (cpi
->frames_till_gf_update_due
> 0)
2959 cpi
->frames_till_gf_update_due
--;
2961 if (cpi
->common
.frames_till_alt_ref_frame
)
2962 cpi
->common
.frames_till_alt_ref_frame
--;
2964 cpi
->common
.frames_since_golden
++;
2966 if (cpi
->common
.frames_since_golden
> 1)
2968 cpi
->recent_ref_frame_usage
[INTRA_FRAME
] += cpi
->count_mb_ref_frame_usage
[INTRA_FRAME
];
2969 cpi
->recent_ref_frame_usage
[LAST_FRAME
] += cpi
->count_mb_ref_frame_usage
[LAST_FRAME
];
2970 cpi
->recent_ref_frame_usage
[GOLDEN_FRAME
] += cpi
->count_mb_ref_frame_usage
[GOLDEN_FRAME
];
2971 cpi
->recent_ref_frame_usage
[ALTREF_FRAME
] += cpi
->count_mb_ref_frame_usage
[ALTREF_FRAME
];
2976 // This function updates the reference frame probability estimates that
2977 // will be used during mode selection
2978 static void update_rd_ref_frame_probs(VP8_COMP
*cpi
)
2980 VP8_COMMON
*cm
= &cpi
->common
;
2983 const int *const rfct
= cpi
->recent_ref_frame_usage
;
2984 const int rf_intra
= rfct
[INTRA_FRAME
];
2985 const int rf_inter
= rfct
[LAST_FRAME
] + rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
];
2987 if (cm
->frame_type
== KEY_FRAME
)
2989 cpi
->prob_intra_coded
= 255;
2990 cpi
->prob_last_coded
= 128;
2991 cpi
->prob_gf_coded
= 128;
2993 else if (!(rf_intra
+ rf_inter
))
2995 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
2996 cpi
->prob_intra_coded
= 63;
2997 cpi
->prob_last_coded
= 128;
2998 cpi
->prob_gf_coded
= 128;
3002 cpi
->prob_intra_coded
= (rf_intra
* 255) / (rf_intra
+ rf_inter
);
3004 if (cpi
->prob_intra_coded
< 1)
3005 cpi
->prob_intra_coded
= 1;
3007 if ((cm
->frames_since_golden
> 0) || cpi
->source_alt_ref_active
)
3009 cpi
->prob_last_coded
= rf_inter
? (rfct
[LAST_FRAME
] * 255) / rf_inter
: 128;
3011 if (cpi
->prob_last_coded
< 1)
3012 cpi
->prob_last_coded
= 1;
3014 cpi
->prob_gf_coded
= (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
])
3015 ? (rfct
[GOLDEN_FRAME
] * 255) / (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
]) : 128;
3017 if (cpi
->prob_gf_coded
< 1)
3018 cpi
->prob_gf_coded
= 1;
3023 const int *const rfct
= cpi
->count_mb_ref_frame_usage
;
3024 const int rf_intra
= rfct
[INTRA_FRAME
];
3025 const int rf_inter
= rfct
[LAST_FRAME
] + rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
];
3027 if (cm
->frame_type
== KEY_FRAME
)
3029 cpi
->prob_intra_coded
= 255;
3030 cpi
->prob_last_coded
= 128;
3031 cpi
->prob_gf_coded
= 128;
3033 else if (!(rf_intra
+ rf_inter
))
3035 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3036 cpi
->prob_intra_coded
= 63;
3037 cpi
->prob_last_coded
= 128;
3038 cpi
->prob_gf_coded
= 128;
3042 cpi
->prob_intra_coded
= (rf_intra
* 255) / (rf_intra
+ rf_inter
);
3044 if (cpi
->prob_intra_coded
< 1)
3045 cpi
->prob_intra_coded
= 1;
3047 cpi
->prob_last_coded
= rf_inter
? (rfct
[LAST_FRAME
] * 255) / rf_inter
: 128;
3049 if (cpi
->prob_last_coded
< 1)
3050 cpi
->prob_last_coded
= 1;
3052 cpi
->prob_gf_coded
= (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
])
3053 ? (rfct
[GOLDEN_FRAME
] * 255) / (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
]) : 128;
3055 if (cpi
->prob_gf_coded
< 1)
3056 cpi
->prob_gf_coded
= 1;
3059 // update reference frame costs since we can do better than what we got last frame.
3061 if (cpi
->common
.refresh_alt_ref_frame
)
3063 cpi
->prob_intra_coded
+= 40;
3064 cpi
->prob_last_coded
= 200;
3065 cpi
->prob_gf_coded
= 1;
3067 else if (cpi
->common
.frames_since_golden
== 0)
3069 cpi
->prob_last_coded
= 214;
3070 cpi
->prob_gf_coded
= 1;
3072 else if (cpi
->common
.frames_since_golden
== 1)
3074 cpi
->prob_last_coded
= 192;
3075 cpi
->prob_gf_coded
= 220;
3077 else if (cpi
->source_alt_ref_active
)
3079 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden;
3080 cpi
->prob_gf_coded
-= 20;
3082 if (cpi
->prob_gf_coded
< 10)
3083 cpi
->prob_gf_coded
= 10;
3090 // 1 = key, 0 = inter
3091 static int decide_key_frame(VP8_COMP
*cpi
)
3093 VP8_COMMON
*cm
= &cpi
->common
;
3095 int code_key_frame
= FALSE
;
3099 if (cpi
->Speed
> 11)
3102 // Clear down mmx registers
3103 vp8_clear_system_state(); //__asm emms;
3105 if ((cpi
->compressor_speed
== 2) && (cpi
->Speed
>= 5) && (cpi
->sf
.RD
== 0))
3107 double change
= 1.0 * abs((int)(cpi
->intra_error
- cpi
->last_intra_error
)) / (1 + cpi
->last_intra_error
);
3108 double change2
= 1.0 * abs((int)(cpi
->prediction_error
- cpi
->last_prediction_error
)) / (1 + cpi
->last_prediction_error
);
3109 double minerror
= cm
->MBs
* 256;
3113 if (10 * cpi
->intra_error
/ (1 + cpi
->prediction_error
) < 15
3114 && cpi
->prediction_error
> minerror
3115 && (change
> .25 || change2
> .25))
3117 FILE *f
= fopen("intra_inter.stt", "a");
3119 if (cpi
->prediction_error
<= 0)
3120 cpi
->prediction_error
= 1;
3122 fprintf(f
, "%d %d %d %d %14.4f\n",
3123 cm
->current_video_frame
,
3124 (int) cpi
->prediction_error
,
3125 (int) cpi
->intra_error
,
3126 (int)((10 * cpi
->intra_error
) / cpi
->prediction_error
),
3134 cpi
->last_intra_error
= cpi
->intra_error
;
3135 cpi
->last_prediction_error
= cpi
->prediction_error
;
3137 if (10 * cpi
->intra_error
/ (1 + cpi
->prediction_error
) < 15
3138 && cpi
->prediction_error
> minerror
3139 && (change
> .25 || change2
> .25))
3141 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3149 // If the following are true we might as well code a key frame
3150 if (((cpi
->this_frame_percent_intra
== 100) &&
3151 (cpi
->this_frame_percent_intra
> (cpi
->last_frame_percent_intra
+ 2))) ||
3152 ((cpi
->this_frame_percent_intra
> 95) &&
3153 (cpi
->this_frame_percent_intra
>= (cpi
->last_frame_percent_intra
+ 5))))
3155 code_key_frame
= TRUE
;
3157 // in addition if the following are true and this is not a golden frame then code a key frame
3158 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
3159 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
3160 else if (((cpi
->this_frame_percent_intra
> 60) &&
3161 (cpi
->this_frame_percent_intra
> (cpi
->last_frame_percent_intra
* 2))) ||
3162 ((cpi
->this_frame_percent_intra
> 75) &&
3163 (cpi
->this_frame_percent_intra
> (cpi
->last_frame_percent_intra
* 3 / 2))) ||
3164 ((cpi
->this_frame_percent_intra
> 90) &&
3165 (cpi
->this_frame_percent_intra
> (cpi
->last_frame_percent_intra
+ 10))))
3167 if (!cm
->refresh_golden_frame
)
3168 code_key_frame
= TRUE
;
3171 return code_key_frame
;
3175 #if !(CONFIG_REALTIME_ONLY)
3176 static void Pass1Encode(VP8_COMP
*cpi
, unsigned long *size
, unsigned char *dest
, unsigned int *frame_flags
)
3181 set_quantizer(cpi
, 26);
3183 scale_and_extend_source(cpi
->un_scaled_source
, cpi
);
3184 vp8_first_pass(cpi
);
3189 void write_cx_frame_to_file(YV12_BUFFER_CONFIG
*frame
, int this_frame
)
3197 sprintf(filename
, "cx\\y%04d.raw", this_frame
);
3198 yframe
= fopen(filename
, "wb");
3200 for (i
= 0; i
< frame
->y_height
; i
++)
3201 fwrite(frame
->y_buffer
+ i
* frame
->y_stride
, frame
->y_width
, 1, yframe
);
3204 sprintf(filename
, "cx\\u%04d.raw", this_frame
);
3205 yframe
= fopen(filename
, "wb");
3207 for (i
= 0; i
< frame
->uv_height
; i
++)
3208 fwrite(frame
->u_buffer
+ i
* frame
->uv_stride
, frame
->uv_width
, 1, yframe
);
3211 sprintf(filename
, "cx\\v%04d.raw", this_frame
);
3212 yframe
= fopen(filename
, "wb");
3214 for (i
= 0; i
< frame
->uv_height
; i
++)
3215 fwrite(frame
->v_buffer
+ i
* frame
->uv_stride
, frame
->uv_width
, 1, yframe
);
3220 // return of 0 means drop frame
3222 #if VP8_TEMPORAL_ALT_REF
3223 static void vp8cx_temp_blur1_c
3225 unsigned char **frames
,
3234 unsigned char *motion_map_ptr
,
3235 unsigned char block_size
3238 int byte
= 0; // Buffer offset for the current pixel value being filtered
3244 unsigned char Shift
= (block_size
== 16) ? 4 : 3;
3246 Cols
= width
/ block_size
;
3247 Rows
= height
/ block_size
;
3249 for (i
= 0; i
< height
; i
++)
3251 block_ofset
= (i
>> Shift
) * Cols
;
3253 for (j
= 0; j
< Cols
; j
++)
3255 if (motion_map_ptr
[block_ofset
] > 2)
3257 vpx_memcpy(&dst
[byte
], &src
[byte
], block_size
);
3262 for (k
= 0; k
< block_size
; k
++)
3264 int accumulator
= 0;
3266 int src_byte
= src
[byte
];
3268 for (frame
= 0; frame
< frame_count
; frame
++)
3270 // get current frame pixel value
3271 int pixel_value
= frames
[frame
][byte
]; // int pixel_value = *frameptr;
3273 modifier
= src_byte
; // modifier = s[byte];
3274 modifier
-= pixel_value
;
3275 modifier
*= modifier
;
3276 modifier
>>= strength
;
3282 modifier
= 16 - modifier
;
3284 accumulator
+= modifier
* pixel_value
;
3289 accumulator
+= (count
>> 1);
3290 accumulator
*= fixed_divide
[count
]; // accumulator *= ppi->fixed_divide[count];
3293 dst
[byte
] = accumulator
; // d[byte] = accumulator;
3295 // move to next pixel
3303 // Step byte on over the UMV border to the start of the next line
3304 byte
+= stride
- width
;
3308 static void vp8cx_temp_filter_c
3313 YV12_BUFFER_CONFIG
*temp_source_buffer
;
3314 int *fixed_divide
= cpi
->fixed_divide
;
3317 int max_frames
= 11;
3319 int num_frames_backward
= 0;
3320 int num_frames_forward
= 0;
3321 int frames_to_blur_backward
= 0;
3322 int frames_to_blur_forward
= 0;
3323 int frames_to_blur
= 0;
3324 int start_frame
= 0;
3326 int strength
= cpi
->oxcf
.arnr_strength
;
3328 int blur_type
= cpi
->oxcf
.arnr_type
;
3330 int new_max_frames
= cpi
->oxcf
.arnr_max_frames
;
3332 if (new_max_frames
> 0)
3333 max_frames
= new_max_frames
;
3335 num_frames_backward
= cpi
->last_alt_ref_sei
- cpi
->source_encode_index
;
3337 if (num_frames_backward
< 0)
3338 num_frames_backward
+= cpi
->oxcf
.lag_in_frames
;
3340 num_frames_forward
= cpi
->oxcf
.lag_in_frames
- (num_frames_backward
+ 1);
3345 /////////////////////////////////////////
3348 frames_to_blur_backward
= num_frames_backward
;
3350 if (frames_to_blur_backward
>= max_frames
)
3351 frames_to_blur_backward
= max_frames
- 1;
3353 frames_to_blur
= frames_to_blur_backward
+ 1;
3357 /////////////////////////////////////////
3360 frames_to_blur_forward
= num_frames_forward
;
3362 if (frames_to_blur_forward
>= max_frames
)
3363 frames_to_blur_forward
= max_frames
- 1;
3365 frames_to_blur
= frames_to_blur_forward
+ 1;
3369 /////////////////////////////////////////
3371 frames_to_blur_forward
= num_frames_forward
;
3372 frames_to_blur_backward
= num_frames_backward
;
3374 if (frames_to_blur_forward
> frames_to_blur_backward
)
3375 frames_to_blur_forward
= frames_to_blur_backward
;
3377 if (frames_to_blur_backward
> frames_to_blur_forward
)
3378 frames_to_blur_backward
= frames_to_blur_forward
;
3380 if (frames_to_blur_forward
> (max_frames
/ 2))
3381 frames_to_blur_forward
= (max_frames
/ 2);
3383 if (frames_to_blur_backward
> (max_frames
/ 2))
3384 frames_to_blur_backward
= (max_frames
/ 2);
3386 frames_to_blur
= frames_to_blur_backward
+ frames_to_blur_forward
+ 1;
3390 /////////////////////////////////////////
3391 // At most 4 frames forward Blur
3392 frames_to_blur_forward
= 4;
3393 frames_to_blur_backward
= num_frames_backward
;
3397 if ((frames_to_blur_backward
+ frames_to_blur_forward
) >= max_frames
)
3399 frames_to_blur_backward
= max_frames
- frames_to_blur_forward
- 1;
3404 frames_to_blur_forward
= max_frames
- 1;
3405 frames_to_blur_backward
= 0;
3408 frames_to_blur
= frames_to_blur_backward
+ frames_to_blur_forward
+ 1;
3412 start_frame
= (cpi
->last_alt_ref_sei
+ frames_to_blur_forward
) % cpi
->oxcf
.lag_in_frames
;
3416 printf("max:%d FBCK:%d FFWD:%d ftb:%d ftbbck:%d ftbfwd:%d sei:%d lasei:%d start:%d"
3418 , num_frames_backward
3419 , num_frames_forward
3421 , frames_to_blur_backward
3422 , frames_to_blur_forward
3423 , cpi
->source_encode_index
3424 , cpi
->last_alt_ref_sei
3428 for (frame
= 0; frame
< frames_to_blur
; frame
++)
3430 int which_buffer
= start_frame
- frame
;
3432 if (which_buffer
< 0)
3433 which_buffer
+= cpi
->oxcf
.lag_in_frames
;
3435 cpi
->frames
[frame
] = cpi
->src_buffer
[which_buffer
].source_buffer
.y_buffer
;
3438 temp_source_buffer
= &cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_buffer
;
3444 temp_source_buffer
->y_buffer
, // cpi->Source->y_buffer,
3445 cpi
->alt_ref_buffer
.source_buffer
.y_buffer
, // cpi->Source->y_buffer,
3446 temp_source_buffer
->y_width
,
3447 temp_source_buffer
->y_stride
,
3448 temp_source_buffer
->y_height
,
3449 //temp_source_buffer->y_height * temp_source_buffer->y_stride,
3452 cpi
->fp_motion_map
, 16);
3454 for (frame
= 0; frame
< frames_to_blur
; frame
++)
3456 int which_buffer
= cpi
->last_alt_ref_sei
- frame
;
3458 if (which_buffer
< 0)
3459 which_buffer
+= cpi
->oxcf
.lag_in_frames
;
3461 cpi
->frames
[frame
] = cpi
->src_buffer
[which_buffer
].source_buffer
.u_buffer
;
3468 temp_source_buffer
->u_buffer
,
3469 cpi
->alt_ref_buffer
.source_buffer
.u_buffer
, // cpi->Source->u_buffer,
3470 temp_source_buffer
->uv_width
,
3471 temp_source_buffer
->uv_stride
,
3472 temp_source_buffer
->uv_height
,
3473 //temp_source_buffer->uv_height * temp_source_buffer->uv_stride,
3476 cpi
->fp_motion_map
, 8);
3478 for (frame
= 0; frame
< frames_to_blur
; frame
++)
3480 int which_buffer
= cpi
->last_alt_ref_sei
- frame
;
3482 if (which_buffer
< 0)
3483 which_buffer
+= cpi
->oxcf
.lag_in_frames
;
3485 cpi
->frames
[frame
] = cpi
->src_buffer
[which_buffer
].source_buffer
.v_buffer
;
3492 temp_source_buffer
->v_buffer
,
3493 cpi
->alt_ref_buffer
.source_buffer
.v_buffer
, // cpi->Source->v_buffer,
3494 temp_source_buffer
->uv_width
,
3495 temp_source_buffer
->uv_stride
,
3496 //temp_source_buffer->uv_height * temp_source_buffer->uv_stride,
3497 temp_source_buffer
->uv_height
,
3500 cpi
->fp_motion_map
, 8);
3505 static void encode_frame_to_data_rate(VP8_COMP
*cpi
, unsigned long *size
, unsigned char *dest
, unsigned int *frame_flags
)
3508 int frame_over_shoot_limit
;
3509 int frame_under_shoot_limit
;
3519 int zbin_oq_low
= 0;
3522 VP8_COMMON
*cm
= &cpi
->common
;
3523 int active_worst_qchanged
= FALSE
;
3525 int overshoot_seen
= FALSE
;
3526 int undershoot_seen
= FALSE
;
3527 int drop_mark
= cpi
->oxcf
.drop_frames_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100;
3528 int drop_mark75
= drop_mark
* 2 / 3;
3529 int drop_mark50
= drop_mark
/ 4;
3530 int drop_mark25
= drop_mark
/ 8;
3532 // Clear down mmx registers to allow floating point in what follows
3533 vp8_clear_system_state();
3535 // Test code for segmentation of gf/arf (0,0)
3536 //segmentation_test_function((VP8_PTR) cpi);
3538 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3539 #if !(CONFIG_REALTIME_ONLY)
3543 if (cpi
->common
.refresh_alt_ref_frame
)
3545 cpi
->per_frame_bandwidth
= cpi
->gf_bits
; // Per frame bit target for the alt ref frame
3546 cpi
->target_bandwidth
= cpi
->gf_bits
* cpi
->output_frame_rate
; // per second target bitrate
3551 cpi
->per_frame_bandwidth
= (int)(cpi
->target_bandwidth
/ cpi
->output_frame_rate
);
3553 // Default turn off buffer to buffer copying
3554 cm
->copy_buffer_to_gf
= 0;
3555 cm
->copy_buffer_to_arf
= 0;
3557 // Clear zbin over-quant value and mode boost values.
3558 cpi
->zbin_over_quant
= 0;
3559 cpi
->zbin_mode_boost
= 0;
3561 // Enable mode based tweaking of the zbin
3562 cpi
->zbin_mode_boost_enabled
= TRUE
;
3564 // Current default encoder behaviour for the altref sign bias
3565 if (cpi
->source_alt_ref_active
)
3566 cpi
->common
.ref_frame_sign_bias
[ALTREF_FRAME
] = 1;
3568 cpi
->common
.ref_frame_sign_bias
[ALTREF_FRAME
] = 0;
3570 // Check to see if a key frame is signalled
3571 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3572 if ((cm
->current_video_frame
== 0) ||
3573 (cm
->frame_flags
& FRAMEFLAGS_KEY
) ||
3574 (cpi
->oxcf
.auto_key
&& (cpi
->frames_since_key
% cpi
->key_frame_frequency
== 0)))
3576 // Key frame from VFW/auto-keyframe/first frame
3577 cm
->frame_type
= KEY_FRAME
;
3580 // Set default state for segment and mode based loop filter update flags
3581 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 0;
3582 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 0;
3583 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 0;
3585 // Set various flags etc to special state if it is a key frame
3586 if (cm
->frame_type
== KEY_FRAME
)
3590 // If segmentation is enabled force a map update for key frames
3591 if (cpi
->mb
.e_mbd
.segmentation_enabled
)
3593 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 1;
3594 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 1;
3597 // If mode or reference frame based loop filter deltas are enabled then force an update for key frames.
3598 if (cpi
->mb
.e_mbd
.mode_ref_lf_delta_enabled
)
3600 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 1;
3603 // The alternate reference frame cannot be active for a key frame
3604 cpi
->source_alt_ref_active
= FALSE
;
3606 // Reset the RD threshold multipliers to default of * 1 (128)
3607 for (i
= 0; i
< MAX_MODES
; i
++)
3609 cpi
->rd_thresh_mult
[i
] = 128;
3613 // Test code for segmentation
3614 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3615 //if ( (cm->current_video_frame % 2) == 0 )
3616 // enable_segmentation((VP8_PTR)cpi);
3618 // disable_segmentation((VP8_PTR)cpi);
3621 // Experimental code for lagged compress and one pass
3622 // Initialise one_pass GF frames stats
3623 // Update stats used for GF selection
3624 //if ( cpi->pass == 0 )
3626 cpi
->one_pass_frame_index
= cm
->current_video_frame
% MAX_LAG_BUFFERS
;
3628 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frames_so_far
= 0;
3629 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_intra_error
= 0.0;
3630 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_coded_error
= 0.0;
3631 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_pcnt_inter
= 0.0;
3632 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_pcnt_motion
= 0.0;
3633 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_mvr
= 0.0;
3634 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_mvr_abs
= 0.0;
3635 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_mvc
= 0.0;
3636 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_mvc_abs
= 0.0;
3640 update_rd_ref_frame_probs(cpi
);
3642 if (cpi
->drop_frames_allowed
)
3644 // The reset to decimation 0 is only done here for one pass.
3645 // Once it is set two pass leaves decimation on till the next kf.
3646 if ((cpi
->buffer_level
> drop_mark
) && (cpi
->decimation_factor
> 0))
3647 cpi
->decimation_factor
--;
3649 if (cpi
->buffer_level
> drop_mark75
&& cpi
->decimation_factor
> 0)
3650 cpi
->decimation_factor
= 1;
3652 else if (cpi
->buffer_level
< drop_mark25
&& (cpi
->decimation_factor
== 2 || cpi
->decimation_factor
== 3))
3654 cpi
->decimation_factor
= 3;
3656 else if (cpi
->buffer_level
< drop_mark50
&& (cpi
->decimation_factor
== 1 || cpi
->decimation_factor
== 2))
3658 cpi
->decimation_factor
= 2;
3660 else if (cpi
->buffer_level
< drop_mark75
&& (cpi
->decimation_factor
== 0 || cpi
->decimation_factor
== 1))
3662 cpi
->decimation_factor
= 1;
3665 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3668 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3669 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3670 // some situations to drop frame rate but throw more bits at each frame.
3672 // Note that dropping a key frame can be problematic if spatial resampling is also active
3673 if (cpi
->decimation_factor
> 0)
3675 switch (cpi
->decimation_factor
)
3678 cpi
->per_frame_bandwidth
= cpi
->per_frame_bandwidth
* 3 / 2;
3681 cpi
->per_frame_bandwidth
= cpi
->per_frame_bandwidth
* 5 / 4;
3684 cpi
->per_frame_bandwidth
= cpi
->per_frame_bandwidth
* 5 / 4;
3688 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3689 if ((cm
->frame_type
== KEY_FRAME
)) // && cpi->oxcf.allow_spatial_resampling )
3691 cpi
->decimation_count
= cpi
->decimation_factor
;
3693 else if (cpi
->decimation_count
> 0)
3695 cpi
->decimation_count
--;
3696 cpi
->bits_off_target
+= cpi
->av_per_frame_bandwidth
;
3697 cm
->current_video_frame
++;
3698 cpi
->frames_since_key
++;
3704 cpi
->buffer_level
= cpi
->bits_off_target
;
3709 cpi
->decimation_count
= cpi
->decimation_factor
;
3712 // Decide how big to make the frame
3713 if (!pick_frame_size(cpi
))
3715 cm
->current_video_frame
++;
3716 cpi
->frames_since_key
++;
3720 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3721 // This has a knock on effect on active best quality as well.
3722 // For CBR if the buffer reaches its maximum level then we can no longer
3723 // save up bits for later frames so we might as well use them up
3724 // on the current frame.
3725 if ((cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
) &&
3726 (cpi
->buffer_level
>= cpi
->oxcf
.optimal_buffer_level
) && cpi
->buffered_mode
)
3728 int Adjustment
= cpi
->active_worst_quality
/ 4; // Max adjustment is 1/4
3733 int tmp_lvl
= cpi
->buffer_level
;
3735 if (cpi
->buffer_level
< cpi
->oxcf
.maximum_buffer_size
)
3737 buff_lvl_step
= (cpi
->oxcf
.maximum_buffer_size
- cpi
->oxcf
.optimal_buffer_level
) / Adjustment
;
3741 Adjustment
= (cpi
->buffer_level
- cpi
->oxcf
.optimal_buffer_level
) / buff_lvl_step
;
3742 cpi
->active_worst_quality
-= Adjustment
;
3747 cpi
->active_worst_quality
-= Adjustment
;
3752 // Set an active best quality and if necessary active worst quality
3753 if (cpi
->pass
== 2 || (cm
->current_video_frame
> 150))
3755 //if ( (cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame )
3760 Q
= cpi
->active_worst_quality
;
3762 if ((cm
->frame_type
== KEY_FRAME
) || cm
->refresh_golden_frame
|| cpi
->common
.refresh_alt_ref_frame
)
3764 vp8_clear_system_state();
3766 if (cm
->frame_type
!= KEY_FRAME
)
3768 // Where a gf overlays an existing arf then allow active max Q to drift to highest allowed value.
3769 //if ( cpi->common.refresh_golden_frame && cpi->source_alt_ref_active )
3770 //cpi->active_worst_quality = cpi->worst_quality;
3772 if (cpi
->avg_frame_qindex
< cpi
->active_worst_quality
)
3773 Q
= cpi
->avg_frame_qindex
;
3775 if (cpi
->section_is_low_motion
)
3776 bpm_target
= (vp8_bits_per_mb
[cm
->frame_type
][Q
] * ((Q
* 3 / 2) + 128)) / 64;
3777 else if (cpi
->section_is_fast_motion
)
3778 bpm_target
= (vp8_bits_per_mb
[cm
->frame_type
][Q
] * (Q
+ 128)) / 64;
3780 bpm_target
= (vp8_bits_per_mb
[cm
->frame_type
][Q
] * ((Q
* 5 / 4) + 128)) / 64;
3785 if (cpi
->section_is_low_motion
)
3786 bpm_target
= (vp8_bits_per_mb
[cm
->frame_type
][Q
] * (Q
+ 240)) / 64; // Approx 2.5 to 4.5 where Q has the range 0-127
3788 bpm_target
= (vp8_bits_per_mb
[cm
->frame_type
][Q
] * (Q
+ 160)) / 64;
3791 for (i
= Q
; i
> 0; i
--)
3793 if (bpm_target
<= vp8_bits_per_mb
[cm
->frame_type
][i
])
3797 cpi
->active_best_quality
= i
;
3799 // this entire section could be replaced by a look up table
3804 for (Q
= 0; Q
< 128; Q
++)
3806 bpm_target
= (vp8_bits_per_mb
[cm
->frame_type
][Q
] * (Q
+ 160)) / 64; // Approx 2.5 to 4.5 where Q has the range 0-127
3808 for (i
= Q
; i
> 0; i
--)
3810 if (bpm_target
<= vp8_bits_per_mb
[cm
->frame_type
][i
])
3824 vp8_clear_system_state();
3826 //bpm_target = (vp8_bits_per_mb[cm->frame_type][Q]*(Q+128))/64; // Approx 2 to 4 where Q has the range 0-127
3827 bpm_target
= (vp8_bits_per_mb
[cm
->frame_type
][Q
] * (Q
+ 192)) / 128; // Approx * 1.5 to 2.5 where Q has range 0-127
3829 for (i
= Q
; i
> 0; i
--)
3831 if (bpm_target
<= vp8_bits_per_mb
[cm
->frame_type
][i
])
3835 cpi
->active_best_quality
= i
;
3838 // If CBR and the buffer is as full then it is reasonable to allow higher quality on the frames
3839 // to prevent bits just going to waste.
3840 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
3842 // Note that the use of >= here elliminates the risk of a devide by 0 error in the else if clause
3843 if (cpi
->buffer_level
>= cpi
->oxcf
.maximum_buffer_size
)
3844 cpi
->active_best_quality
= cpi
->best_quality
;
3846 else if (cpi
->buffer_level
> cpi
->oxcf
.optimal_buffer_level
)
3848 int Fraction
= ((cpi
->buffer_level
- cpi
->oxcf
.optimal_buffer_level
) * 128) / (cpi
->oxcf
.maximum_buffer_size
- cpi
->oxcf
.optimal_buffer_level
);
3849 int min_qadjustment
= ((cpi
->active_best_quality
- cpi
->best_quality
) * Fraction
) / 128;
3851 cpi
->active_best_quality
-= min_qadjustment
;
3857 // Clip the active best and worst quality values to limits
3858 if (cpi
->active_worst_quality
> cpi
->worst_quality
)
3859 cpi
->active_worst_quality
= cpi
->worst_quality
;
3861 if (cpi
->active_best_quality
< cpi
->best_quality
)
3862 cpi
->active_best_quality
= cpi
->best_quality
;
3863 else if (cpi
->active_best_quality
> cpi
->active_worst_quality
)
3864 cpi
->active_best_quality
= cpi
->active_worst_quality
;
3866 // Determine initial Q to try
3867 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
3868 last_zbin_oq
= cpi
->zbin_over_quant
;
3870 // Set highest allowed value for Zbin over quant
3871 if (cm
->frame_type
== KEY_FRAME
)
3872 zbin_oq_high
= 0; //ZBIN_OQ_MAX/16
3873 else if (cm
->refresh_alt_ref_frame
|| (cm
->refresh_golden_frame
&& !cpi
->source_alt_ref_active
))
3876 zbin_oq_high
= ZBIN_OQ_MAX
;
3878 // Setup background Q adjustment for error resilliant mode
3879 if (cpi
->cyclic_refresh_mode_enabled
)
3880 cyclic_background_refresh(cpi
, Q
, 0);
3882 vp8_compute_frame_size_bounds(cpi
, &frame_under_shoot_limit
, &frame_over_shoot_limit
);
3884 // Limit Q range for the adaptive loop (Values not clipped to range 20-60 as in VP8).
3885 bottom_index
= cpi
->active_best_quality
;
3886 top_index
= cpi
->active_worst_quality
;
3888 vp8_save_coding_context(cpi
);
3892 q_low
= cpi
->best_quality
;
3893 q_high
= cpi
->worst_quality
;
3896 scale_and_extend_source(cpi
->un_scaled_source
, cpi
);
3897 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC
3899 if (cpi
->oxcf
.noise_sensitivity
> 0)
3904 switch (cpi
->oxcf
.noise_sensitivity
)
3927 if (cm
->frame_type
== KEY_FRAME
)
3929 vp8_de_noise(cpi
->Source
, cpi
->Source
, l
, 1, 0, RTCD(postproc
));
3934 vp8_de_noise(cpi
->Source
, cpi
->Source
, l
, 1, 0, RTCD(postproc
));
3936 src
= cpi
->Source
->y_buffer
;
3938 if (cpi
->Source
->y_stride
< 0)
3940 src
+= cpi
->Source
->y_stride
* (cpi
->Source
->y_height
- 1);
3943 //temp_filter(&cpi->ppi,src,src,
3944 // cm->last_frame.y_width * cm->last_frame.y_height,
3945 // cpi->oxcf.noise_sensitivity);
3951 #ifdef OUTPUT_YUV_SRC
3952 vp8_write_yuv_frame(cpi
->Source
);
3957 vp8_clear_system_state(); //__asm emms;
3960 if(cpi->is_src_frame_alt_ref)
3964 set_quantizer(cpi
, Q
);
3967 // setup skip prob for costing in mode/mv decision
3968 if (cpi
->common
.mb_no_coeff_skip
)
3970 cpi
->prob_skip_false
= cpi
->base_skip_false_prob
[Q
];
3972 if (cm
->frame_type
!= KEY_FRAME
)
3974 if (cpi
->common
.refresh_alt_ref_frame
)
3976 if (cpi
->last_skip_false_probs
[2] != 0)
3977 cpi
->prob_skip_false
= cpi
->last_skip_false_probs
[2];
3980 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3981 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3982 else if (cpi->last_skip_false_probs[2]!=0)
3983 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3986 else if (cpi
->common
.refresh_golden_frame
)
3988 if (cpi
->last_skip_false_probs
[1] != 0)
3989 cpi
->prob_skip_false
= cpi
->last_skip_false_probs
[1];
3992 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3993 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3994 else if (cpi->last_skip_false_probs[1]!=0)
3995 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
4000 if (cpi
->last_skip_false_probs
[0] != 0)
4001 cpi
->prob_skip_false
= cpi
->last_skip_false_probs
[0];
4004 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
4005 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
4006 else if(cpi->last_skip_false_probs[0]!=0)
4007 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
4011 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
4012 if (cpi
->prob_skip_false
< 5)
4013 cpi
->prob_skip_false
= 5;
4015 if (cpi
->prob_skip_false
> 250)
4016 cpi
->prob_skip_false
= 250;
4018 if (cpi
->is_src_frame_alt_ref
)
4019 cpi
->prob_skip_false
= 1;
4028 FILE *f
= fopen("skip.stt", "a");
4029 fprintf(f
, "%d, %d, %4d ", cpi
->common
.refresh_golden_frame
, cpi
->common
.refresh_alt_ref_frame
, cpi
->prob_skip_false
);
4037 if (cm
->frame_type
== KEY_FRAME
)
4038 vp8_setup_key_frame(cpi
);
4040 // transform / motion compensation build reconstruction frame
4042 vp8_encode_frame(cpi
);
4043 cpi
->projected_frame_size
-= vp8_estimate_entropy_savings(cpi
);
4044 cpi
->projected_frame_size
= (cpi
->projected_frame_size
> 0) ? cpi
->projected_frame_size
: 0;
4046 vp8_clear_system_state(); //__asm emms;
4048 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
4049 // (assuming that we didn't)!
4050 if (cpi
->pass
!= 2 && cpi
->oxcf
.auto_key
&& cm
->frame_type
!= KEY_FRAME
)
4052 if (decide_key_frame(cpi
))
4054 vp8_calc_auto_iframe_target_size(cpi
);
4056 // Reset all our sizing numbers and recode
4057 cm
->frame_type
= KEY_FRAME
;
4059 // Clear the Alt reference frame active flag when we have a key frame
4060 cpi
->source_alt_ref_active
= FALSE
;
4062 // If segmentation is enabled force a map update for key frames
4063 if (cpi
->mb
.e_mbd
.segmentation_enabled
)
4065 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 1;
4066 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 1;
4069 // If mode or reference frame based loop filter deltas are enabled then force an update for key frames.
4070 if (cpi
->mb
.e_mbd
.mode_ref_lf_delta_enabled
)
4072 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 1;
4075 vp8_restore_coding_context(cpi
);
4077 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4079 q_low
= cpi
->best_quality
;
4080 q_high
= cpi
->worst_quality
;
4082 vp8_compute_frame_size_bounds(cpi
, &frame_under_shoot_limit
, &frame_over_shoot_limit
);
4084 // Limit Q range for the adaptive loop (Values not clipped to range 20-60 as in VP8).
4085 bottom_index
= cpi
->active_best_quality
;
4086 top_index
= cpi
->active_worst_quality
;
4092 resize_key_frame(cpi
);
4097 vp8_clear_system_state();
4099 if (frame_over_shoot_limit
== 0)
4100 frame_over_shoot_limit
= 1;
4102 // Are we are overshooting and up against the limit of active max Q.
4103 if (((cpi
->pass
!= 2) || (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)) &&
4104 (Q
== cpi
->active_worst_quality
) &&
4105 (cpi
->active_worst_quality
< cpi
->worst_quality
) &&
4106 (cpi
->projected_frame_size
> frame_over_shoot_limit
))
4108 int over_size_percent
= ((cpi
->projected_frame_size
- frame_over_shoot_limit
) * 100) / frame_over_shoot_limit
;
4110 // If so is there any scope for relaxing it
4111 while ((cpi
->active_worst_quality
< cpi
->worst_quality
) && (over_size_percent
> 0))
4113 cpi
->active_worst_quality
++;
4114 top_index
= cpi
->active_worst_quality
;
4115 over_size_percent
= (int)(over_size_percent
* 0.96); // Assume 1 qstep = about 4% on frame size.
4118 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
4119 active_worst_qchanged
= TRUE
;
4122 active_worst_qchanged
= FALSE
;
4124 #if !(CONFIG_REALTIME_ONLY)
4126 // Is the projected frame size out of range and are we allowed to attempt to recode.
4127 if (((cpi
->sf
.recode_loop
== 1) ||
4128 ((cpi
->sf
.recode_loop
== 2) && (cm
->refresh_golden_frame
|| (cm
->frame_type
== KEY_FRAME
)))) &&
4129 (((cpi
->projected_frame_size
> frame_over_shoot_limit
) && (Q
< top_index
)) ||
4130 //((cpi->projected_frame_size > frame_over_shoot_limit ) && (Q == top_index) && (cpi->zbin_over_quant < ZBIN_OQ_MAX)) ||
4131 ((cpi
->projected_frame_size
< frame_under_shoot_limit
) && (Q
> bottom_index
)))
4137 // Frame size out of permitted range:
4138 // Update correction factor & compute new Q to try...
4139 if (cpi
->projected_frame_size
> frame_over_shoot_limit
)
4141 //if ( cpi->zbin_over_quant == 0 )
4142 q_low
= (Q
< q_high
) ? (Q
+ 1) : q_high
; // Raise Qlow as to at least the current value
4144 if (cpi
->zbin_over_quant
> 0) // If we are using over quant do the same for zbin_oq_low
4145 zbin_oq_low
= (cpi
->zbin_over_quant
< zbin_oq_high
) ? (cpi
->zbin_over_quant
+ 1) : zbin_oq_high
;
4147 //if ( undershoot_seen || (Q == MAXQ) )
4148 if (undershoot_seen
)
4150 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4151 if (!active_worst_qchanged
)
4152 vp8_update_rate_correction_factors(cpi
, 1);
4154 Q
= (q_high
+ q_low
+ 1) / 2;
4156 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4158 cpi
->zbin_over_quant
= 0;
4161 zbin_oq_low
= (cpi
->zbin_over_quant
< zbin_oq_high
) ? (cpi
->zbin_over_quant
+ 1) : zbin_oq_high
;
4162 cpi
->zbin_over_quant
= (zbin_oq_high
+ zbin_oq_low
) / 2;
4167 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4168 if (!active_worst_qchanged
)
4169 vp8_update_rate_correction_factors(cpi
, 0);
4171 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4173 while (((Q
< q_low
) || (cpi
->zbin_over_quant
< zbin_oq_low
)) && (Retries
< 10))
4175 vp8_update_rate_correction_factors(cpi
, 0);
4176 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4181 overshoot_seen
= TRUE
;
4185 if (cpi
->zbin_over_quant
== 0)
4186 q_high
= (Q
> q_low
) ? (Q
- 1) : q_low
; // Lower q_high if not using over quant
4187 else // else lower zbin_oq_high
4188 zbin_oq_high
= (cpi
->zbin_over_quant
> zbin_oq_low
) ? (cpi
->zbin_over_quant
- 1) : zbin_oq_low
;
4192 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4193 if (!active_worst_qchanged
)
4194 vp8_update_rate_correction_factors(cpi
, 1);
4196 Q
= (q_high
+ q_low
) / 2;
4198 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4200 cpi
->zbin_over_quant
= 0;
4202 cpi
->zbin_over_quant
= (zbin_oq_high
+ zbin_oq_low
) / 2;
4206 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4207 if (!active_worst_qchanged
)
4208 vp8_update_rate_correction_factors(cpi
, 0);
4210 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4212 while (((Q
> q_high
) || (cpi
->zbin_over_quant
> zbin_oq_high
)) && (Retries
< 10))
4214 vp8_update_rate_correction_factors(cpi
, 0);
4215 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4220 undershoot_seen
= TRUE
;
4223 // Clamp Q to upper and lower limits:
4229 // Clamp cpi->zbin_over_quant
4230 cpi
->zbin_over_quant
= (cpi
->zbin_over_quant
< zbin_oq_low
) ? zbin_oq_low
: (cpi
->zbin_over_quant
> zbin_oq_high
) ? zbin_oq_high
: cpi
->zbin_over_quant
;
4232 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4233 Loop
= ((Q
!= last_q
)) ? TRUE
: FALSE
;
4234 last_zbin_oq
= cpi
->zbin_over_quant
;
4240 if (cpi
->is_src_frame_alt_ref
)
4245 vp8_restore_coding_context(cpi
);
4248 cpi
->tot_recode_hits
++;
4252 while (Loop
== TRUE
);
4255 // Experimental code for lagged and one pass
4256 // Update stats used for one pass GF selection
4260 double frame_intra_error;
4261 double frame_coded_error;
4262 double frame_pcnt_inter;
4263 double frame_pcnt_motion;
4265 double frame_mvr_abs;
4267 double frame_mvc_abs;
4270 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_coded_error
= (double)cpi
->prediction_error
;
4271 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_intra_error
= (double)cpi
->intra_error
;
4272 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_pcnt_inter
= (double)(100 - cpi
->this_frame_percent_intra
) / 100.0;
4276 // Update the GF useage maps.
4277 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4278 vp8_update_gf_useage_maps(cm
, &cpi
->mb
.e_mbd
);
4280 if (cm
->frame_type
== KEY_FRAME
)
4281 cm
->refresh_last_frame
= 1;
4285 FILE *f
= fopen("gfactive.stt", "a");
4286 fprintf(f
, "%8d %8d %8d %8d %8d\n", cm
->current_video_frame
, (100 * cpi
->common
.gf_active_count
) / (cpi
->common
.mb_rows
* cpi
->common
.mb_cols
), cpi
->this_iiratio
, cpi
->next_iiratio
, cm
->refresh_golden_frame
);
4290 // For inter frames the current default behaviour is that when cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4291 // This is purely an encoder descision at present.
4292 if (!cpi
->oxcf
.error_resilient_mode
&& cm
->refresh_golden_frame
)
4293 cm
->copy_buffer_to_arf
= 2;
4295 cm
->copy_buffer_to_arf
= 0;
4297 if (cm
->refresh_last_frame
)
4299 vp8_swap_yv12_buffer(&cm
->last_frame
, &cm
->new_frame
);
4300 cm
->frame_to_show
= &cm
->last_frame
;
4303 cm
->frame_to_show
= &cm
->new_frame
;
4307 //#pragma omp parallel sections
4310 //#pragma omp section
4313 struct vpx_usec_timer timer
;
4315 vpx_usec_timer_start(&timer
);
4317 if (cpi
->sf
.auto_filter
== 0)
4318 vp8cx_pick_filter_level_fast(cpi
->Source
, cpi
);
4320 vp8cx_pick_filter_level(cpi
->Source
, cpi
);
4322 vpx_usec_timer_mark(&timer
);
4324 cpi
->time_pick_lpf
+= vpx_usec_timer_elapsed(&timer
);
4327 cm
->filter_level
= 0;
4329 if (cm
->filter_level
> 0)
4331 vp8cx_set_alt_lf_level(cpi
, cm
->filter_level
);
4332 vp8_loop_filter_frame(cm
, &cpi
->mb
.e_mbd
, cm
->filter_level
);
4333 cm
->last_frame_type
= cm
->frame_type
;
4334 cm
->last_filter_type
= cm
->filter_type
;
4335 cm
->last_sharpness_level
= cm
->sharpness_level
;
4338 vp8_yv12_extend_frame_borders_ptr(cm
->frame_to_show
);
4340 if (cpi
->oxcf
.error_resilient_mode
== 1)
4342 cm
->refresh_entropy_probs
= 0;
4346 //#pragma omp section
4348 // build the bitstream
4349 vp8_pack_bitstream(cpi
, dest
, size
);
4354 // At this point the new frame has been encoded coded.
4355 // If any buffer copy / swaping is signalled it should be done here.
4356 if (cm
->frame_type
== KEY_FRAME
)
4358 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->golden_frame
);
4359 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->alt_ref_frame
);
4361 else // For non key frames
4363 // Code to copy between reference buffers
4364 if (cm
->copy_buffer_to_arf
)
4366 if (cm
->copy_buffer_to_arf
== 1)
4368 if (cm
->refresh_last_frame
)
4369 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4370 vp8_yv12_copy_frame_ptr(&cm
->new_frame
, &cm
->alt_ref_frame
);
4372 vp8_yv12_copy_frame_ptr(&cm
->last_frame
, &cm
->alt_ref_frame
);
4374 else if (cm
->copy_buffer_to_arf
== 2)
4375 vp8_yv12_copy_frame_ptr(&cm
->golden_frame
, &cm
->alt_ref_frame
);
4378 if (cm
->copy_buffer_to_gf
)
4380 if (cm
->copy_buffer_to_gf
== 1)
4382 if (cm
->refresh_last_frame
)
4383 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4384 vp8_yv12_copy_frame_ptr(&cm
->new_frame
, &cm
->golden_frame
);
4386 vp8_yv12_copy_frame_ptr(&cm
->last_frame
, &cm
->golden_frame
);
4388 else if (cm
->copy_buffer_to_gf
== 2)
4389 vp8_yv12_copy_frame_ptr(&cm
->alt_ref_frame
, &cm
->golden_frame
);
4393 // Update rate control heuristics
4394 cpi
->total_byte_count
+= (*size
);
4395 cpi
->projected_frame_size
= (*size
) << 3;
4397 if (!active_worst_qchanged
)
4398 vp8_update_rate_correction_factors(cpi
, 2);
4400 cpi
->last_q
[cm
->frame_type
] = cm
->base_qindex
;
4402 if (cm
->frame_type
== KEY_FRAME
)
4404 vp8_adjust_key_frame_context(cpi
);
4407 // Keep a record of ambient average Q.
4408 if (cm
->frame_type
== KEY_FRAME
)
4409 cpi
->avg_frame_qindex
= cm
->base_qindex
;
4411 cpi
->avg_frame_qindex
= (2 + 3 * cpi
->avg_frame_qindex
+ cm
->base_qindex
) >> 2;
4413 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4414 if ((cm
->frame_type
!= KEY_FRAME
) && !cm
->refresh_golden_frame
&& !cm
->refresh_alt_ref_frame
)
4418 // Calculate the average Q for normal inter frames (not key or GFU frames)
4419 // This is used as a basis for setting active worst quality.
4420 if (cpi
->ni_frames
> 150)
4422 cpi
->ni_tot_qi
+= Q
;
4423 cpi
->ni_av_qi
= (cpi
->ni_tot_qi
/ cpi
->ni_frames
);
4425 // Early in the clip ... average the current frame Q value with the default
4426 // entered by the user as a dampening measure
4429 cpi
->ni_tot_qi
+= Q
;
4430 cpi
->ni_av_qi
= ((cpi
->ni_tot_qi
/ cpi
->ni_frames
) + cpi
->worst_quality
+ 1) / 2;
4433 // If the average Q is higher than what was used in the last frame
4434 // (after going through the recode loop to keep the frame size within range)
4435 // then use the last frame value - 1.
4436 // The -1 is designed to stop Q and hence the data rate, from progressively
4437 // falling away during difficult sections, but at the same time reduce the number of
4438 // itterations around the recode loop.
4439 if (Q
> cpi
->ni_av_qi
)
4440 cpi
->ni_av_qi
= Q
- 1;
4446 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4447 if ((cpi
->drop_frames_allowed
) &&
4448 (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
) &&
4449 (cpi
->buffer_level
< cpi
->oxcf
.drop_frames_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100) &&
4450 (cpi
->projected_frame_size
> (4 * cpi
->this_frame_target
)))
4452 cpi
->drop_frame
= TRUE
;
4457 // Set the count for maximum consequative dropped frames based upon the ratio of
4458 // this frame size to the target average per frame bandwidth.
4459 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4460 if (cpi
->drop_frames_allowed
&& (cpi
->av_per_frame_bandwidth
> 0))
4462 cpi
->max_drop_count
= cpi
->projected_frame_size
/ cpi
->av_per_frame_bandwidth
;
4464 if (cpi
->max_drop_count
> cpi
->max_consec_dropped_frames
)
4465 cpi
->max_drop_count
= cpi
->max_consec_dropped_frames
;
4468 // Update the buffer level variable.
4469 if (cpi
->common
.refresh_alt_ref_frame
)
4470 cpi
->bits_off_target
-= cpi
->projected_frame_size
;
4472 cpi
->bits_off_target
+= cpi
->av_per_frame_bandwidth
- cpi
->projected_frame_size
;
4474 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4475 cpi
->rolling_target_bits
= ((cpi
->rolling_target_bits
* 3) + cpi
->this_frame_target
+ 2) / 4;
4476 cpi
->rolling_actual_bits
= ((cpi
->rolling_actual_bits
* 3) + cpi
->projected_frame_size
+ 2) / 4;
4477 cpi
->long_rolling_target_bits
= ((cpi
->long_rolling_target_bits
* 31) + cpi
->this_frame_target
+ 16) / 32;
4478 cpi
->long_rolling_actual_bits
= ((cpi
->long_rolling_actual_bits
* 31) + cpi
->projected_frame_size
+ 16) / 32;
4480 // Actual bits spent
4481 cpi
->total_actual_bits
+= cpi
->projected_frame_size
;
4484 cpi
->total_target_vs_actual
+= (cpi
->this_frame_target
- cpi
->projected_frame_size
);
4486 cpi
->buffer_level
= cpi
->bits_off_target
;
4488 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4489 if (cm
->frame_type
== KEY_FRAME
)
4491 cpi
->kf_group_bits
+= cpi
->this_frame_target
- cpi
->projected_frame_size
;
4493 if (cpi
->kf_group_bits
< 0)
4494 cpi
->kf_group_bits
= 0 ;
4496 else if (cm
->refresh_golden_frame
|| cm
->refresh_alt_ref_frame
)
4498 cpi
->gf_group_bits
+= cpi
->this_frame_target
- cpi
->projected_frame_size
;
4500 if (cpi
->gf_group_bits
< 0)
4501 cpi
->gf_group_bits
= 0 ;
4504 if (cm
->frame_type
!= KEY_FRAME
)
4506 if (cpi
->common
.refresh_alt_ref_frame
)
4508 cpi
->last_skip_false_probs
[2] = cpi
->prob_skip_false
;
4509 cpi
->last_skip_probs_q
[2] = cm
->base_qindex
;
4511 else if (cpi
->common
.refresh_golden_frame
)
4513 cpi
->last_skip_false_probs
[1] = cpi
->prob_skip_false
;
4514 cpi
->last_skip_probs_q
[1] = cm
->base_qindex
;
4518 cpi
->last_skip_false_probs
[0] = cpi
->prob_skip_false
;
4519 cpi
->last_skip_probs_q
[0] = cm
->base_qindex
;
4521 //update the baseline
4522 cpi
->base_skip_false_prob
[cm
->base_qindex
] = cpi
->prob_skip_false
;
4531 FILE *f
= fopen("tmp.stt", "a");
4533 vp8_clear_system_state(); //__asm emms;
4535 if (cpi
->total_coded_error_left
!= 0.0)
4536 fprintf(f
, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f %10.3f %8ld\n", cpi
->common
.current_video_frame
, cpi
->this_frame_target
, cpi
->projected_frame_size
, (cpi
->projected_frame_size
- cpi
->this_frame_target
), (int)cpi
->total_target_vs_actual
, (cpi
->oxcf
.starting_buffer_level
- cpi
->bits_off_target
), (int)cpi
->total_actual_bits
, cm
->base_qindex
, cpi
->active_best_quality
, cpi
->active_worst_quality
, cpi
->avg_frame_qindex
, cpi
->zbin_over_quant
, cm
->refresh_golden_frame
, cm
->refresh_alt_ref_frame
, cm
->frame_type
, cpi
->gfu_boost
, cpi
->est_max_qcorrection_factor
, (int)cpi
->bits_left
, cpi
->total_coded_error_left
, (double)cpi
->bits_left
/ cpi
->total_coded_error_left
, cpi
->tot_recode_hits
);
4538 fprintf(f
, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f %8ld\n", cpi
->common
.current_video_frame
, cpi
->this_frame_target
, cpi
->projected_frame_size
, (cpi
->projected_frame_size
- cpi
->this_frame_target
), (int)cpi
->total_target_vs_actual
, (cpi
->oxcf
.starting_buffer_level
- cpi
->bits_off_target
), (int)cpi
->total_actual_bits
, cm
->base_qindex
, cpi
->active_best_quality
, cpi
->active_worst_quality
, cpi
->avg_frame_qindex
, cpi
->zbin_over_quant
, cm
->refresh_golden_frame
, cm
->refresh_alt_ref_frame
, cm
->frame_type
, cpi
->gfu_boost
, cpi
->est_max_qcorrection_factor
, (int)cpi
->bits_left
, cpi
->total_coded_error_left
, cpi
->tot_recode_hits
);
4543 FILE *fmodes
= fopen("Modes.stt", "a");
4546 fprintf(fmodes
, "%6d:%1d:%1d:%1d ", cpi
->common
.current_video_frame
, cm
->frame_type
, cm
->refresh_golden_frame
, cm
->refresh_alt_ref_frame
);
4548 for (i
= 0; i
< MAX_MODES
; i
++)
4549 fprintf(fmodes
, "%5d ", cpi
->mode_chosen_counts
[i
]);
4551 fprintf(fmodes
, "\n");
4559 // If this was a kf or Gf note the Q
4560 if ((cm
->frame_type
== KEY_FRAME
) || cm
->refresh_golden_frame
|| cm
->refresh_alt_ref_frame
)
4561 cm
->last_kf_gf_q
= cm
->base_qindex
;
4563 if (cm
->refresh_golden_frame
== 1)
4564 cm
->frame_flags
= cm
->frame_flags
| FRAMEFLAGS_GOLDEN
;
4566 cm
->frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_GOLDEN
;
4568 if (cm
->refresh_alt_ref_frame
== 1)
4569 cm
->frame_flags
= cm
->frame_flags
| FRAMEFLAGS_ALTREF
;
4571 cm
->frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_ALTREF
;
4574 if (cm
->refresh_last_frame
& cm
->refresh_golden_frame
) // both refreshed
4575 cpi
->gold_is_last
= 1;
4576 else if (cm
->refresh_last_frame
^ cm
->refresh_golden_frame
) // 1 refreshed but not the other
4577 cpi
->gold_is_last
= 0;
4579 if (cm
->refresh_last_frame
& cm
->refresh_alt_ref_frame
) // both refreshed
4580 cpi
->alt_is_last
= 1;
4581 else if (cm
->refresh_last_frame
^ cm
->refresh_alt_ref_frame
) // 1 refreshed but not the other
4582 cpi
->alt_is_last
= 0;
4584 if (cm
->refresh_alt_ref_frame
& cm
->refresh_golden_frame
) // both refreshed
4585 cpi
->gold_is_alt
= 1;
4586 else if (cm
->refresh_alt_ref_frame
^ cm
->refresh_golden_frame
) // 1 refreshed but not the other
4587 cpi
->gold_is_alt
= 0;
4589 cpi
->ref_frame_flags
= VP8_ALT_FLAG
| VP8_GOLD_FLAG
| VP8_LAST_FLAG
;
4591 if (cpi
->gold_is_last
)
4592 cpi
->ref_frame_flags
&= !VP8_GOLD_FLAG
;
4594 if (cpi
->alt_is_last
)
4595 cpi
->ref_frame_flags
&= !VP8_ALT_FLAG
;
4597 if (cpi
->gold_is_alt
)
4598 cpi
->ref_frame_flags
&= !VP8_ALT_FLAG
;
4601 if (cpi
->oxcf
.error_resilient_mode
)
4603 // Is this an alternate reference update
4604 if (cpi
->common
.refresh_alt_ref_frame
)
4605 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->alt_ref_frame
);
4607 if (cpi
->common
.refresh_golden_frame
)
4608 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->golden_frame
);
4612 if (cpi
->oxcf
.play_alternate
&& cpi
->common
.refresh_alt_ref_frame
)
4613 // Update the alternate reference frame and stats as appropriate.
4614 update_alt_ref_frame_and_stats(cpi
);
4616 // Update the Golden frame and golden frame and stats as appropriate.
4617 update_golden_frame_and_stats(cpi
);
4620 if (cm
->frame_type
== KEY_FRAME
)
4622 // Tell the caller that the frame was coded as a key frame
4623 *frame_flags
= cm
->frame_flags
| FRAMEFLAGS_KEY
;
4625 // As this frame is a key frame the next defaults to an inter frame.
4626 cm
->frame_type
= INTER_FRAME
;
4628 cpi
->last_frame_percent_intra
= 100;
4632 *frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_KEY
;
4634 cpi
->last_frame_percent_intra
= cpi
->this_frame_percent_intra
;
4637 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4638 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 0;
4639 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 0;
4640 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 0;
4643 // Dont increment frame counters if this was an altref buffer update not a real frame
4646 cm
->current_video_frame
++;
4647 cpi
->frames_since_key
++;
4650 // reset to normal state now that we are done.
4658 sprintf(filename
, "enc%04d.yuv", (int) cm
->current_video_frame
);
4659 recon_file
= fopen(filename
, "wb");
4660 fwrite(cm
->last_frame
.buffer_alloc
, cm
->last_frame
.frame_size
, 1, recon_file
);
4665 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4670 int vp8_is_gf_update_needed(VP8_PTR ptr
)
4672 VP8_COMP
*cpi
= (VP8_COMP
*) ptr
;
4675 ret_val
= cpi
->gf_update_recommended
;
4676 cpi
->gf_update_recommended
= 0;
4681 void vp8_check_gf_quality(VP8_COMP
*cpi
)
4683 VP8_COMMON
*cm
= &cpi
->common
;
4684 int gf_active_pct
= (100 * cm
->gf_active_count
) / (cm
->mb_rows
* cm
->mb_cols
);
4685 int gf_ref_usage_pct
= (cpi
->count_mb_ref_frame_usage
[GOLDEN_FRAME
] * 100) / (cm
->mb_rows
* cm
->mb_cols
);
4686 int last_ref_zz_useage
= (cpi
->inter_zz_count
* 100) / (cm
->mb_rows
* cm
->mb_cols
);
4688 // Gf refresh is not currently being signalled
4689 if (cpi
->gf_update_recommended
== 0)
4691 if (cpi
->common
.frames_since_golden
> 7)
4694 if ((gf_active_pct
< 10) || ((gf_active_pct
+ gf_ref_usage_pct
) < 15))
4696 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4697 if (last_ref_zz_useage
>= 25)
4699 cpi
->gf_bad_count
++;
4701 if (cpi
->gf_bad_count
>= 8) // Check that the condition is stable
4703 cpi
->gf_update_recommended
= 1;
4704 cpi
->gf_bad_count
= 0;
4708 cpi
->gf_bad_count
= 0; // Restart count as the background is not stable enough
4711 cpi
->gf_bad_count
= 0; // Gf useage has picked up so reset count
4714 // If the signal is set but has not been read should we cancel it.
4715 else if (last_ref_zz_useage
< 15)
4717 cpi
->gf_update_recommended
= 0;
4718 cpi
->gf_bad_count
= 0;
4725 FILE *f
= fopen("gfneeded.stt", "a");
4726 fprintf(f
, "%10d %10d %10d %10d %10ld \n",
4727 cm
->current_video_frame
,
4728 cpi
->common
.frames_since_golden
,
4729 gf_active_pct
, gf_ref_usage_pct
,
4730 cpi
->gf_update_recommended
);
4737 #if !(CONFIG_REALTIME_ONLY)
4738 static void Pass2Encode(VP8_COMP
*cpi
, unsigned long *size
, unsigned char *dest
, unsigned int *frame_flags
)
4741 if (!cpi
->common
.refresh_alt_ref_frame
)
4742 vp8_second_pass(cpi
);
4744 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
4745 cpi
->bits_left
-= 8 * *size
;
4747 if (!cpi
->common
.refresh_alt_ref_frame
)
4749 double two_pass_min_rate
= (double)(cpi
->oxcf
.target_bandwidth
4750 *cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
4751 cpi
->bits_left
+= (long long)(two_pass_min_rate
/ cpi
->oxcf
.frame_rate
);
4756 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4758 extern void vp8_push_neon(INT64
*store
);
4759 extern void vp8_pop_neon(INT64
*store
);
4760 static INT64 store_reg
[8];
4762 int vp8_receive_raw_frame(VP8_PTR ptr
, unsigned int frame_flags
, YV12_BUFFER_CONFIG
*sd
, INT64 time_stamp
, INT64 end_time
)
4764 VP8_COMP
*cpi
= (VP8_COMP
*) ptr
;
4765 VP8_COMMON
*cm
= &cpi
->common
;
4766 struct vpx_usec_timer timer
;
4772 vp8_push_neon(store_reg
);
4775 vpx_usec_timer_start(&timer
);
4777 // no more room for frames;
4778 if (cpi
->source_buffer_count
!= 0 && cpi
->source_buffer_count
>= cpi
->oxcf
.lag_in_frames
)
4781 vp8_pop_neon(store_reg
);
4786 //printf("in-cpi->source_buffer_count: %d\n", cpi->source_buffer_count);
4788 cm
->clr_type
= sd
->clrtype
;
4790 // make a copy of the frame for use later...
4791 #if !(CONFIG_REALTIME_ONLY)
4793 if (cpi
->oxcf
.allow_lag
)
4795 int which_buffer
= cpi
->source_encode_index
- 1;
4798 if (which_buffer
== -1)
4799 which_buffer
= cpi
->oxcf
.lag_in_frames
- 1;
4801 if (cpi
->source_buffer_count
< cpi
->oxcf
.lag_in_frames
- 1)
4802 which_buffer
= cpi
->source_buffer_count
;
4804 s
= &cpi
->src_buffer
[which_buffer
];
4806 s
->source_time_stamp
= time_stamp
;
4807 s
->source_end_time_stamp
= end_time
;
4808 s
->source_frame_flags
= frame_flags
;
4809 vp8_yv12_copy_frame_ptr(sd
, &s
->source_buffer
);
4811 cpi
->source_buffer_count
++;
4817 s
= &cpi
->src_buffer
[0];
4818 s
->source_end_time_stamp
= end_time
;
4819 s
->source_time_stamp
= time_stamp
;
4820 s
->source_frame_flags
= frame_flags
;
4822 vp8_yv12_copy_src_frame_func_neon(sd
, &s
->source_buffer
);
4824 vp8_yv12_copy_frame_ptr(sd
, &s
->source_buffer
);
4826 cpi
->source_buffer_count
= 1;
4829 vpx_usec_timer_mark(&timer
);
4830 cpi
->time_receive_data
+= vpx_usec_timer_elapsed(&timer
);
4833 vp8_pop_neon(store_reg
);
4838 int vp8_get_compressed_data(VP8_PTR ptr
, unsigned int *frame_flags
, unsigned long *size
, unsigned char *dest
, INT64
*time_stamp
, INT64
*time_end
, int flush
)
4841 VP8_COMP
*cpi
= (VP8_COMP
*) ptr
;
4842 VP8_COMMON
*cm
= &cpi
->common
;
4843 struct vpx_usec_timer tsctimer
;
4844 struct vpx_usec_timer ticktimer
;
4845 struct vpx_usec_timer cmptimer
;
4851 vp8_push_neon(store_reg
);
4854 vpx_usec_timer_start(&cmptimer
);
4857 // flush variable tells us that even though we have less than 10 frames
4858 // in our buffer we need to start producing compressed frames.
4859 // Probably because we are at the end of a file....
4860 if ((cpi
->source_buffer_count
== cpi
->oxcf
.lag_in_frames
&& cpi
->oxcf
.lag_in_frames
> 0)
4861 || (!cpi
->oxcf
.allow_lag
&& cpi
->source_buffer_count
> 0)
4862 || (flush
&& cpi
->source_buffer_count
> 0))
4867 s
= &cpi
->src_buffer
[cpi
->source_encode_index
];
4868 cpi
->source_time_stamp
= s
->source_time_stamp
;
4869 cpi
->source_end_time_stamp
= s
->source_end_time_stamp
;
4871 #if !(CONFIG_REALTIME_ONLY)
4873 // Should we code an alternate reference frame
4874 if (cpi
->oxcf
.error_resilient_mode
== 0 &&
4875 cpi
->oxcf
.play_alternate
&&
4876 cpi
->source_alt_ref_pending
&&
4877 (cpi
->frames_till_gf_update_due
< cpi
->source_buffer_count
) &&
4878 cpi
->oxcf
.lag_in_frames
!= 0)
4880 cpi
->last_alt_ref_sei
= (cpi
->source_encode_index
+ cpi
->frames_till_gf_update_due
) % cpi
->oxcf
.lag_in_frames
;
4882 #if VP8_TEMPORAL_ALT_REF
4884 if (cpi
->oxcf
.arnr_max_frames
> 0)
4887 // my attempt at a loop that tests the results of strength filter.
4888 int start_frame
= cpi
->last_alt_ref_sei
- 3;
4890 int i
, besti
= -1, pastin
= cpi
->oxcf
.arnr_strength
;
4894 if (start_frame
< 0)
4895 start_frame
+= cpi
->oxcf
.lag_in_frames
;
4897 besterr
= vp8_calc_low_ss_err(&cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_buffer
,
4898 &cpi
->src_buffer
[start_frame
].source_buffer
, IF_RTCD(&cpi
->rtcd
.variance
));
4900 for (i
= 0; i
< 7; i
++)
4903 cpi
->oxcf
.arnr_strength
= i
;
4904 vp8cx_temp_filter_c(cpi
);
4906 thiserr
= vp8_calc_low_ss_err(&cpi
->alt_ref_buffer
.source_buffer
,
4907 &cpi
->src_buffer
[start_frame
].source_buffer
, IF_RTCD(&cpi
->rtcd
.variance
));
4909 if (10 * thiserr
< besterr
* 8)
4918 cpi
->oxcf
.arnr_strength
= besti
;
4919 vp8cx_temp_filter_c(cpi
);
4920 s
= &cpi
->alt_ref_buffer
;
4922 // FWG not sure if I need to copy this data for the Alt Ref frame
4923 s
->source_time_stamp
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_time_stamp
;
4924 s
->source_end_time_stamp
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_end_time_stamp
;
4925 s
->source_frame_flags
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_frame_flags
;
4928 s
= &cpi
->src_buffer
[cpi
->last_alt_ref_sei
];
4931 vp8cx_temp_filter_c(cpi
);
4932 s
= &cpi
->alt_ref_buffer
;
4934 // FWG not sure if I need to copy this data for the Alt Ref frame
4935 s
->source_time_stamp
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_time_stamp
;
4936 s
->source_end_time_stamp
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_end_time_stamp
;
4937 s
->source_frame_flags
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_frame_flags
;
4943 s
= &cpi
->src_buffer
[cpi
->last_alt_ref_sei
];
4945 cm
->frames_till_alt_ref_frame
= cpi
->frames_till_gf_update_due
;
4946 cm
->refresh_alt_ref_frame
= 1;
4947 cm
->refresh_golden_frame
= 0;
4948 cm
->refresh_last_frame
= 0;
4950 cpi
->source_alt_ref_pending
= FALSE
; // Clear Pending altf Ref flag.
4951 cpi
->is_src_frame_alt_ref
= 0;
4957 #if !(CONFIG_REALTIME_ONLY)
4959 if (cpi
->oxcf
.allow_lag
)
4961 if (cpi
->source_encode_index
== cpi
->last_alt_ref_sei
)
4963 #if VP8_TEMPORAL_ALT_REF
4965 if (cpi
->oxcf
.arnr_max_frames
== 0)
4967 cpi
->is_src_frame_alt_ref
= 1; // copy alt ref
4971 cpi
->is_src_frame_alt_ref
= 0;
4975 cpi
->is_src_frame_alt_ref
= 1;
4977 cpi
->last_alt_ref_sei
= -1;
4980 cpi
->is_src_frame_alt_ref
= 0;
4982 cpi
->source_encode_index
= (cpi
->source_encode_index
+ 1) % cpi
->oxcf
.lag_in_frames
;
4986 cpi
->source_buffer_count
--;
4989 cpi
->un_scaled_source
= &s
->source_buffer
;
4990 cpi
->Source
= &s
->source_buffer
;
4991 cpi
->source_frame_flags
= s
->source_frame_flags
;
4993 *time_stamp
= cpi
->source_time_stamp
;
4994 *time_end
= cpi
->source_end_time_stamp
;
4999 #if !(CONFIG_REALTIME_ONLY)
5001 if (flush
&& cpi
->pass
== 1 && !cpi
->first_pass_done
)
5003 vp8_end_first_pass(cpi
); /* get last stats packet */
5004 cpi
->first_pass_done
= 1;
5010 vp8_pop_neon(store_reg
);
5015 *frame_flags
= cpi
->source_frame_flags
;
5019 if (cpi
->source_time_stamp
< cpi
->first_time_stamp_ever
)
5020 cpi
->first_time_stamp_ever
= cpi
->source_time_stamp
;
5024 // adjust frame rates based on timestamps given
5025 if (!cm
->refresh_alt_ref_frame
)
5027 if (cpi
->last_time_stamp_seen
== 0)
5029 double this_fps
= 10000000.000 / (cpi
->source_end_time_stamp
- cpi
->source_time_stamp
);
5031 vp8_new_frame_rate(cpi
, this_fps
);
5035 long long nanosecs
= cpi
->source_time_stamp
- cpi
->last_time_stamp_seen
;
5036 double this_fps
= 10000000.000 / nanosecs
;
5038 vp8_new_frame_rate(cpi
, (7 * cpi
->oxcf
.frame_rate
+ this_fps
) / 8);
5042 cpi
->last_time_stamp_seen
= cpi
->source_time_stamp
;
5045 if (cpi
->compressor_speed
== 2)
5047 vp8_check_gf_quality(cpi
);
5053 vp8_pop_neon(store_reg
);
5058 if (cpi
->compressor_speed
== 2)
5060 vpx_usec_timer_start(&tsctimer
);
5061 vpx_usec_timer_start(&ticktimer
);
5064 // start with a 0 size frame
5067 // Clear down mmx registers
5068 vp8_clear_system_state(); //__asm emms;
5070 cm
->frame_type
= INTER_FRAME
;
5071 cm
->frame_flags
= *frame_flags
;
5075 if (cm
->refresh_alt_ref_frame
)
5077 //cm->refresh_golden_frame = 1;
5078 cm
->refresh_golden_frame
= 0;
5079 cm
->refresh_last_frame
= 0;
5083 cm
->refresh_golden_frame
= 0;
5084 cm
->refresh_last_frame
= 1;
5089 #if !(CONFIG_REALTIME_ONLY)
5093 Pass1Encode(cpi
, size
, dest
, frame_flags
);
5095 else if (cpi
->pass
== 2)
5097 Pass2Encode(cpi
, size
, dest
, frame_flags
);
5101 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
5103 if (cpi
->compressor_speed
== 2)
5105 unsigned int duration
, duration2
;
5106 vpx_usec_timer_mark(&tsctimer
);
5107 vpx_usec_timer_mark(&ticktimer
);
5109 duration
= vpx_usec_timer_elapsed(&ticktimer
);
5110 duration2
= (unsigned int)((double)duration
/ 2);
5112 if (cm
->frame_type
!= KEY_FRAME
)
5114 if (cpi
->avg_encode_time
== 0)
5115 cpi
->avg_encode_time
= duration
;
5117 cpi
->avg_encode_time
= (7 * cpi
->avg_encode_time
+ duration
) >> 3;
5122 //if(*frame_flags!=1)
5125 if (cpi
->avg_pick_mode_time
== 0)
5126 cpi
->avg_pick_mode_time
= duration2
;
5128 cpi
->avg_pick_mode_time
= (7 * cpi
->avg_pick_mode_time
+ duration2
) >> 3;
5134 if (cm
->refresh_entropy_probs
== 0)
5136 vpx_memcpy(&cm
->fc
, &cm
->lfc
, sizeof(cm
->fc
));
5139 // if its a dropped frame honor the requests on subsequent frames
5143 // return to normal state
5144 cpi
->ref_frame_flags
= VP8_ALT_FLAG
| VP8_GOLD_FLAG
| VP8_LAST_FLAG
;
5146 cm
->refresh_entropy_probs
= 1;
5147 cm
->refresh_alt_ref_frame
= 0;
5148 cm
->refresh_golden_frame
= 0;
5149 cm
->refresh_last_frame
= 1;
5150 cm
->frame_type
= INTER_FRAME
;
5154 cpi
->ready_for_new_frame
= 1;
5156 vpx_usec_timer_mark(&cmptimer
);
5157 cpi
->time_compress_data
+= vpx_usec_timer_elapsed(&cmptimer
);
5159 if (cpi
->b_calculate_psnr
&& cpi
->pass
!= 1 && cm
->show_frame
)
5160 generate_psnr_packet(cpi
);
5166 cpi
->bytes
+= *size
;
5173 if (cpi
->b_calculate_psnr
)
5177 double frame_psnr
= vp8_calc_psnr(cpi
->Source
, cm
->frame_to_show
, &y
, &u
, &v
, &sq_error
);
5182 cpi
->total_sq_error
+= sq_error
;
5183 cpi
->total
+= frame_psnr
;
5185 double y2
, u2
, v2
, frame_psnr2
, frame_ssim2
= 0;
5188 vp8_deblock(cm
->frame_to_show
, &cm
->post_proc_buffer
, cm
->filter_level
* 10 / 6, 1, 0, IF_RTCD(&cm
->rtcd
.postproc
));
5189 vp8_clear_system_state();
5190 frame_psnr2
= vp8_calc_psnr(cpi
->Source
, &cm
->post_proc_buffer
, &y2
, &u2
, &v2
, &sq_error
);
5191 frame_ssim2
= vp8_calc_ssim(cpi
->Source
, &cm
->post_proc_buffer
, 1, &weight
);
5193 cpi
->summed_quality
+= frame_ssim2
* weight
;
5194 cpi
->summed_weights
+= weight
;
5196 cpi
->totalp_y
+= y2
;
5197 cpi
->totalp_u
+= u2
;
5198 cpi
->totalp_v
+= v2
;
5199 cpi
->totalp
+= frame_psnr2
;
5200 cpi
->total_sq_error2
+= sq_error
;
5205 if (cpi
->b_calculate_ssimg
)
5207 double y
, u
, v
, frame_all
;
5208 frame_all
= vp8_calc_ssimg(cpi
->Source
, cm
->frame_to_show
, &y
, &u
, &v
);
5209 cpi
->total_ssimg_y
+= y
;
5210 cpi
->total_ssimg_u
+= u
;
5211 cpi
->total_ssimg_v
+= v
;
5212 cpi
->total_ssimg_all
+= frame_all
;
5220 if (cpi
->common
.frame_type
!= 0 && cpi
->common
.base_qindex
== cpi
->oxcf
.worst_allowed_q
)
5222 skiptruecount
+= cpi
->skip_true_count
;
5223 skipfalsecount
+= cpi
->skip_false_count
;
5231 FILE *f
= fopen("skip.stt", "a");
5232 fprintf(f
, "frame:%4d flags:%4x Q:%4d P:%4d Size:%5d\n", cpi
->common
.current_video_frame
, *frame_flags
, cpi
->common
.base_qindex
, cpi
->prob_skip_false
, *size
);
5234 if (cpi
->is_src_frame_alt_ref
== 1)
5235 fprintf(f
, "skipcount: %4d framesize: %d\n", cpi
->skip_true_count
, *size
);
5244 vp8_pop_neon(store_reg
);
5250 int vp8_get_preview_raw_frame(VP8_PTR comp
, YV12_BUFFER_CONFIG
*dest
, int deblock_level
, int noise_level
, int flags
)
5252 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
5254 if (cpi
->common
.refresh_alt_ref_frame
)
5260 ret
= vp8_post_proc_frame(&cpi
->common
, dest
, deblock_level
, noise_level
, flags
);
5263 if (cpi
->common
.frame_to_show
)
5265 *dest
= *cpi
->common
.frame_to_show
;
5266 dest
->y_width
= cpi
->common
.Width
;
5267 dest
->y_height
= cpi
->common
.Height
;
5268 dest
->uv_height
= cpi
->common
.Height
/ 2;
5276 #endif //!CONFIG_POSTPROC
5277 vp8_clear_system_state();
5282 int vp8_set_roimap(VP8_PTR comp
, unsigned char *map
, unsigned int rows
, unsigned int cols
, int delta_q
[4], int delta_lf
[4], unsigned int threshold
[4])
5284 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
5285 signed char feature_data
[MB_LVL_MAX
][MAX_MB_SEGMENTS
];
5287 if (cpi
->common
.mb_rows
!= rows
|| cpi
->common
.mb_cols
!= cols
)
5292 disable_segmentation((VP8_PTR
)cpi
);
5296 // Set the segmentation Map
5297 set_segmentation_map((VP8_PTR
)cpi
, map
);
5299 // Activate segmentation.
5300 enable_segmentation((VP8_PTR
)cpi
);
5302 // Set up the quant segment data
5303 feature_data
[MB_LVL_ALT_Q
][0] = delta_q
[0];
5304 feature_data
[MB_LVL_ALT_Q
][1] = delta_q
[1];
5305 feature_data
[MB_LVL_ALT_Q
][2] = delta_q
[2];
5306 feature_data
[MB_LVL_ALT_Q
][3] = delta_q
[3];
5308 // Set up the loop segment data s
5309 feature_data
[MB_LVL_ALT_LF
][0] = delta_lf
[0];
5310 feature_data
[MB_LVL_ALT_LF
][1] = delta_lf
[1];
5311 feature_data
[MB_LVL_ALT_LF
][2] = delta_lf
[2];
5312 feature_data
[MB_LVL_ALT_LF
][3] = delta_lf
[3];
5314 cpi
->segment_encode_breakout
[0] = threshold
[0];
5315 cpi
->segment_encode_breakout
[1] = threshold
[1];
5316 cpi
->segment_encode_breakout
[2] = threshold
[2];
5317 cpi
->segment_encode_breakout
[3] = threshold
[3];
5319 // Initialise the feature data structure
5320 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5321 set_segment_data((VP8_PTR
)cpi
, &feature_data
[0][0], SEGMENT_DELTADATA
);
5326 int vp8_set_active_map(VP8_PTR comp
, unsigned char *map
, unsigned int rows
, unsigned int cols
)
5328 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
5330 if (rows
== cpi
->common
.mb_rows
&& cols
== cpi
->common
.mb_cols
)
5334 vpx_memcpy(cpi
->active_map
, map
, rows
* cols
);
5335 cpi
->active_map_enabled
= 1;
5338 cpi
->active_map_enabled
= 0;
5344 //cpi->active_map_enabled = 0;
5349 int vp8_set_internal_size(VP8_PTR comp
, VPX_SCALING horiz_mode
, VPX_SCALING vert_mode
)
5351 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
5353 if (horiz_mode
>= NORMAL
&& horiz_mode
<= ONETWO
)
5354 cpi
->common
.horiz_scale
= horiz_mode
;
5358 if (vert_mode
>= NORMAL
&& vert_mode
<= ONETWO
)
5359 cpi
->common
.vert_scale
= vert_mode
;
5368 int vp8_calc_ss_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, const vp8_variance_rtcd_vtable_t
*rtcd
)
5373 unsigned char *src
= source
->y_buffer
;
5374 unsigned char *dst
= dest
->y_buffer
;
5377 // Loop through the Y plane raw and reconstruction data summing (square differences)
5378 for (i
= 0; i
< source
->y_height
; i
+= 16)
5380 for (j
= 0; j
< source
->y_width
; j
+= 16)
5383 Total
+= VARIANCE_INVOKE(rtcd
, mse16x16
)(src
+ j
, source
->y_stride
, dst
+ j
, dest
->y_stride
, &sse
);
5386 src
+= 16 * source
->y_stride
;
5387 dst
+= 16 * dest
->y_stride
;
5392 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, const vp8_variance_rtcd_vtable_t
*rtcd
)
5397 unsigned char *src
= source
->y_buffer
;
5398 unsigned char *dst
= dest
->y_buffer
;
5401 // Loop through the Y plane raw and reconstruction data summing (square differences)
5402 for (i
= 0; i
< source
->y_height
; i
+= 16)
5404 for (j
= 0; j
< source
->y_width
; j
+= 16)
5407 VARIANCE_INVOKE(rtcd
, mse16x16
)(src
+ j
, source
->y_stride
, dst
+ j
, dest
->y_stride
, &sse
);
5413 src
+= 16 * source
->y_stride
;
5414 dst
+= 16 * dest
->y_stride
;
5420 int vp8_get_speed(VP8_PTR c
)
5422 VP8_COMP
*cpi
= (VP8_COMP
*) c
;
5425 int vp8_get_quantizer(VP8_PTR c
)
5427 VP8_COMP
*cpi
= (VP8_COMP
*) c
;
5428 return cpi
->common
.base_qindex
;