2 * Copyright (c) 2010 The WebM 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.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"
32 #include "vpxerrors.h"
33 #include "temporal_filter.h"
35 #include "vpx_ports/arm.h"
42 #if CONFIG_RUNTIME_CPU_DETECT
43 #define IF_RTCD(x) (x)
44 #define RTCD(x) &cpi->common.rtcd.x
46 #define IF_RTCD(x) NULL
50 extern void vp8cx_init_mv_bits_sadcost();
51 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG
*sd
, VP8_COMP
*cpi
);
52 extern void vp8cx_set_alt_lf_level(VP8_COMP
*cpi
, int filt_val
);
53 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG
*sd
, VP8_COMP
*cpi
);
55 extern void vp8_init_loop_filter(VP8_COMMON
*cm
);
56 extern void vp8_loop_filter_frame(VP8_COMMON
*cm
, MACROBLOCKD
*mbd
, int filt_val
);
57 extern void vp8_loop_filter_frame_yonly(VP8_COMMON
*cm
, MACROBLOCKD
*mbd
, int filt_val
, int sharpness_lvl
);
58 extern void vp8_dmachine_specific_config(VP8_COMP
*cpi
);
59 extern void vp8_cmachine_specific_config(VP8_COMP
*cpi
);
60 extern void vp8_calc_auto_iframe_target_size(VP8_COMP
*cpi
);
61 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*post
, int filt_lvl
, int low_var_thresh
, int flag
);
62 extern void print_parms(VP8_CONFIG
*ocf
, char *filenam
);
63 extern unsigned int vp8_get_processor_freq();
64 extern void print_tree_update_probs();
65 extern void vp8cx_create_encoder_threads(VP8_COMP
*cpi
);
66 extern void vp8cx_remove_encoder_threads(VP8_COMP
*cpi
);
68 extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG
*src_ybc
, YV12_BUFFER_CONFIG
*dst_ybc
);
69 extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG
*src_ybc
, YV12_BUFFER_CONFIG
*dst_ybc
);
72 int vp8_estimate_entropy_savings(VP8_COMP
*cpi
);
73 int vp8_calc_ss_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, const vp8_variance_rtcd_vtable_t
*rtcd
);
74 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, const vp8_variance_rtcd_vtable_t
*rtcd
);
76 extern void vp8_temporal_filter_prepare_c(VP8_COMP
*cpi
);
78 static void set_default_lf_deltas(VP8_COMP
*cpi
);
80 extern const int vp8_gf_interval_table
[101];
85 extern double vp8_calc_ssim
87 YV12_BUFFER_CONFIG
*source
,
88 YV12_BUFFER_CONFIG
*dest
,
93 extern double vp8_calc_ssimg
95 YV12_BUFFER_CONFIG
*source
,
96 YV12_BUFFER_CONFIG
*dest
,
106 #ifdef OUTPUT_YUV_SRC
117 extern int skip_true_count
;
118 extern int skip_false_count
;
123 extern int intra_mode_stats
[10][10][10];
127 unsigned int frames_at_speed
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
128 unsigned int tot_pm
= 0;
129 unsigned int cnt_pm
= 0;
130 unsigned int tot_ef
= 0;
131 unsigned int cnt_ef
= 0;
135 extern unsigned __int64 Sectionbits
[50];
136 extern int y_modes
[5] ;
137 extern int uv_modes
[4] ;
138 extern int b_modes
[10] ;
140 extern int inter_y_modes
[10] ;
141 extern int inter_uv_modes
[4] ;
142 extern unsigned int inter_b_modes
[15];
145 extern void (*vp8_short_fdct4x4
)(short *input
, short *output
, int pitch
);
146 extern void (*vp8_short_fdct8x4
)(short *input
, short *output
, int pitch
);
148 extern const int vp8_bits_per_mb
[2][QINDEX_RANGE
];
150 extern const int qrounding_factors
[129];
151 extern const int qzbin_factors
[129];
152 extern void vp8cx_init_quantizer(VP8_COMP
*cpi
);
153 extern const int vp8cx_base_skip_false_prob
[128];
155 // Tables relating active max Q to active min Q
156 static const int kf_low_motion_minq
[QINDEX_RANGE
] =
158 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
159 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
160 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
161 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
162 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
163 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
164 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
165 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
167 static const int kf_high_motion_minq
[QINDEX_RANGE
] =
169 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
170 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
171 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
172 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
173 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
174 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
175 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
176 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
178 static const int gf_low_motion_minq
[QINDEX_RANGE
] =
180 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
181 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
182 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
183 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
184 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
185 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
186 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
187 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
189 static const int gf_mid_motion_minq
[QINDEX_RANGE
] =
191 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
192 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
193 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
194 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
195 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
196 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
197 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
198 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
200 static const int gf_high_motion_minq
[QINDEX_RANGE
] =
202 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
203 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
204 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
205 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
206 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
207 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
208 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
209 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
211 static const int inter_minq
[QINDEX_RANGE
] =
213 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
214 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
215 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
216 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
217 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
218 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
219 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
220 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
223 void vp8_initialize()
225 static int init_done
= 0;
229 vp8_scale_machine_specific_config();
230 vp8_initialize_common();
231 //vp8_dmachine_specific_config();
232 vp8_tokenize_initialize();
234 vp8cx_init_mv_bits_sadcost();
238 #ifdef PACKET_TESTING
239 extern FILE *vpxlogc
;
242 static void setup_features(VP8_COMP
*cpi
)
244 // Set up default state for MB feature flags
245 cpi
->mb
.e_mbd
.segmentation_enabled
= 0;
246 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 0;
247 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 0;
248 vpx_memset(cpi
->mb
.e_mbd
.mb_segment_tree_probs
, 255, sizeof(cpi
->mb
.e_mbd
.mb_segment_tree_probs
));
249 vpx_memset(cpi
->mb
.e_mbd
.segment_feature_data
, 0, sizeof(cpi
->mb
.e_mbd
.segment_feature_data
));
251 cpi
->mb
.e_mbd
.mode_ref_lf_delta_enabled
= 0;
252 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 0;
253 vpx_memset(cpi
->mb
.e_mbd
.ref_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.ref_lf_deltas
));
254 vpx_memset(cpi
->mb
.e_mbd
.mode_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.mode_lf_deltas
));
255 vpx_memset(cpi
->mb
.e_mbd
.last_ref_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.ref_lf_deltas
));
256 vpx_memset(cpi
->mb
.e_mbd
.last_mode_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.mode_lf_deltas
));
258 set_default_lf_deltas(cpi
);
263 void vp8_dealloc_compressor_data(VP8_COMP
*cpi
)
265 // Delete last frame MV storage buffers
271 if (cpi
->lf_ref_frame_sign_bias
!= 0)
272 vpx_free(cpi
->lf_ref_frame_sign_bias
);
274 cpi
->lf_ref_frame_sign_bias
= 0;
276 if (cpi
->lf_ref_frame
!= 0)
277 vpx_free(cpi
->lf_ref_frame
);
279 cpi
->lf_ref_frame
= 0;
281 // Delete sementation map
282 if (cpi
->segmentation_map
!= 0)
283 vpx_free(cpi
->segmentation_map
);
285 cpi
->segmentation_map
= 0;
287 if (cpi
->active_map
!= 0)
288 vpx_free(cpi
->active_map
);
292 // Delete first pass motion map
293 if (cpi
->fp_motion_map
!= 0)
294 vpx_free(cpi
->fp_motion_map
);
296 cpi
->fp_motion_map
= 0;
298 vp8_de_alloc_frame_buffers(&cpi
->common
);
300 vp8_yv12_de_alloc_frame_buffer(&cpi
->last_frame_uf
);
301 vp8_yv12_de_alloc_frame_buffer(&cpi
->scaled_source
);
302 #if VP8_TEMPORAL_ALT_REF
303 vp8_yv12_de_alloc_frame_buffer(&cpi
->alt_ref_buffer
.source_buffer
);
308 for (i
= 0; i
< MAX_LAG_BUFFERS
; i
++)
309 vp8_yv12_de_alloc_frame_buffer(&cpi
->src_buffer
[i
].source_buffer
);
311 cpi
->source_buffer_count
= 0;
317 // Structure used to monitor GF usage
318 if (cpi
->gf_active_flags
!= 0)
319 vpx_free(cpi
->gf_active_flags
);
321 cpi
->gf_active_flags
= 0;
324 vpx_free(cpi
->mb
.pip
);
328 #if !(CONFIG_REALTIME_ONLY)
330 vpx_free(cpi
->total_stats
);
332 cpi
->total_stats
= 0;
334 if(cpi
->this_frame_stats
)
335 vpx_free(cpi
->this_frame_stats
);
337 cpi
->this_frame_stats
= 0;
341 static void enable_segmentation(VP8_PTR ptr
)
343 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
345 // Set the appropriate feature bit
346 cpi
->mb
.e_mbd
.segmentation_enabled
= 1;
347 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 1;
348 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 1;
350 static void disable_segmentation(VP8_PTR ptr
)
352 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
354 // Clear the appropriate feature bit
355 cpi
->mb
.e_mbd
.segmentation_enabled
= 0;
358 // Valid values for a segment are 0 to 3
359 // Segmentation map is arrange as [Rows][Columns]
360 static void set_segmentation_map(VP8_PTR ptr
, unsigned char *segmentation_map
)
362 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
364 // Copy in the new segmentation map
365 vpx_memcpy(cpi
->segmentation_map
, segmentation_map
, (cpi
->common
.mb_rows
* cpi
->common
.mb_cols
));
367 // Signal that the map should be updated.
368 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 1;
369 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 1;
372 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
374 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
375 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
377 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
380 static void set_segment_data(VP8_PTR ptr
, signed char *feature_data
, unsigned char abs_delta
)
382 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
384 cpi
->mb
.e_mbd
.mb_segement_abs_delta
= abs_delta
;
385 vpx_memcpy(cpi
->segment_feature_data
, feature_data
, sizeof(cpi
->segment_feature_data
));
389 static void segmentation_test_function(VP8_PTR ptr
)
391 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
393 unsigned char *seg_map
;
394 signed char feature_data
[MB_LVL_MAX
][MAX_MB_SEGMENTS
];
396 // Create a temporary map for segmentation data.
397 CHECK_MEM_ERROR(seg_map
, vpx_calloc(cpi
->common
.mb_rows
* cpi
->common
.mb_cols
, 1));
399 // MB loop to set local segmentation map
400 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
402 for ( j = 0; j < cpi->common.mb_cols; j++ )
404 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
405 //if ( j < cpi->common.mb_cols/2 )
407 // Segment 1 around the edge else 0
408 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
409 seg_map[(i*cpi->common.mb_cols) + j] = 1;
410 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
411 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
412 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
413 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
415 seg_map[(i*cpi->common.mb_cols) + j] = 0;
419 // Set the segmentation Map
420 set_segmentation_map(ptr
, seg_map
);
422 // Activate segmentation.
423 enable_segmentation(ptr
);
425 // Set up the quant segment data
426 feature_data
[MB_LVL_ALT_Q
][0] = 0;
427 feature_data
[MB_LVL_ALT_Q
][1] = 4;
428 feature_data
[MB_LVL_ALT_Q
][2] = 0;
429 feature_data
[MB_LVL_ALT_Q
][3] = 0;
430 // Set up the loop segment data
431 feature_data
[MB_LVL_ALT_LF
][0] = 0;
432 feature_data
[MB_LVL_ALT_LF
][1] = 0;
433 feature_data
[MB_LVL_ALT_LF
][2] = 0;
434 feature_data
[MB_LVL_ALT_LF
][3] = 0;
436 // Initialise the feature data structure
437 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
438 set_segment_data(ptr
, &feature_data
[0][0], SEGMENT_DELTADATA
);
440 // Delete sementation map
448 // A simple function to cyclically refresh the background at a lower Q
449 static void cyclic_background_refresh(VP8_COMP
*cpi
, int Q
, int lf_adjustment
)
451 unsigned char *seg_map
;
452 signed char feature_data
[MB_LVL_MAX
][MAX_MB_SEGMENTS
];
454 int block_count
= cpi
->cyclic_refresh_mode_max_mbs_perframe
;
455 int mbs_in_frame
= cpi
->common
.mb_rows
* cpi
->common
.mb_cols
;
457 // Create a temporary map for segmentation data.
458 CHECK_MEM_ERROR(seg_map
, vpx_calloc(cpi
->common
.mb_rows
* cpi
->common
.mb_cols
, 1));
460 cpi
->cyclic_refresh_q
= Q
;
462 for (i
= Q
; i
> 0; i
--)
464 if (vp8_bits_per_mb
[cpi
->common
.frame_type
][i
] >= ((vp8_bits_per_mb
[cpi
->common
.frame_type
][Q
]*(Q
+ 128)) / 64))
465 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
471 cpi
->cyclic_refresh_q
= i
;
473 // Only update for inter frames
474 if (cpi
->common
.frame_type
!= KEY_FRAME
)
476 // Cycle through the macro_block rows
477 // MB loop to set local segmentation map
478 for (i
= cpi
->cyclic_refresh_mode_index
; i
< mbs_in_frame
; i
++)
480 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
481 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
482 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
483 if (cpi
->cyclic_refresh_map
[i
] == 0)
491 // Skip blocks that have been refreshed recently anyway.
492 if (cpi
->cyclic_refresh_map
[i
] < 0)
493 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
494 cpi
->cyclic_refresh_map
[i
]++;
505 // If we have gone through the frame reset to the start
506 cpi
->cyclic_refresh_mode_index
= i
;
508 if (cpi
->cyclic_refresh_mode_index
>= mbs_in_frame
)
509 cpi
->cyclic_refresh_mode_index
= 0;
512 // Set the segmentation Map
513 set_segmentation_map((VP8_PTR
)cpi
, seg_map
);
515 // Activate segmentation.
516 enable_segmentation((VP8_PTR
)cpi
);
518 // Set up the quant segment data
519 feature_data
[MB_LVL_ALT_Q
][0] = 0;
520 feature_data
[MB_LVL_ALT_Q
][1] = (cpi
->cyclic_refresh_q
- Q
);
521 feature_data
[MB_LVL_ALT_Q
][2] = 0;
522 feature_data
[MB_LVL_ALT_Q
][3] = 0;
524 // Set up the loop segment data
525 feature_data
[MB_LVL_ALT_LF
][0] = 0;
526 feature_data
[MB_LVL_ALT_LF
][1] = lf_adjustment
;
527 feature_data
[MB_LVL_ALT_LF
][2] = 0;
528 feature_data
[MB_LVL_ALT_LF
][3] = 0;
530 // Initialise the feature data structure
531 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
532 set_segment_data((VP8_PTR
)cpi
, &feature_data
[0][0], SEGMENT_DELTADATA
);
534 // Delete sementation map
542 static void set_default_lf_deltas(VP8_COMP
*cpi
)
544 cpi
->mb
.e_mbd
.mode_ref_lf_delta_enabled
= 1;
545 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 1;
547 vpx_memset(cpi
->mb
.e_mbd
.ref_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.ref_lf_deltas
));
548 vpx_memset(cpi
->mb
.e_mbd
.mode_lf_deltas
, 0, sizeof(cpi
->mb
.e_mbd
.mode_lf_deltas
));
550 // Test of ref frame deltas
551 cpi
->mb
.e_mbd
.ref_lf_deltas
[INTRA_FRAME
] = 2;
552 cpi
->mb
.e_mbd
.ref_lf_deltas
[LAST_FRAME
] = 0;
553 cpi
->mb
.e_mbd
.ref_lf_deltas
[GOLDEN_FRAME
] = -2;
554 cpi
->mb
.e_mbd
.ref_lf_deltas
[ALTREF_FRAME
] = -2;
556 cpi
->mb
.e_mbd
.mode_lf_deltas
[0] = 4; // BPRED
557 cpi
->mb
.e_mbd
.mode_lf_deltas
[1] = -2; // Zero
558 cpi
->mb
.e_mbd
.mode_lf_deltas
[2] = 2; // New mv
559 cpi
->mb
.e_mbd
.mode_lf_deltas
[3] = 4; // Split mv
562 void vp8_set_speed_features(VP8_COMP
*cpi
)
564 SPEED_FEATURES
*sf
= &cpi
->sf
;
565 int Mode
= cpi
->compressor_speed
;
566 int Speed
= cpi
->Speed
;
568 VP8_COMMON
*cm
= &cpi
->common
;
569 int last_improved_quant
= sf
->improved_quant
;
571 // Initialise default mode frequency sampling variables
572 for (i
= 0; i
< MAX_MODES
; i
++)
574 cpi
->mode_check_freq
[i
] = 0;
575 cpi
->mode_test_hit_counts
[i
] = 0;
576 cpi
->mode_chosen_counts
[i
] = 0;
579 cpi
->mbs_tested_so_far
= 0;
581 // best quality defaults
583 sf
->search_method
= NSTEP
;
584 sf
->improved_quant
= 1;
585 sf
->improved_dct
= 1;
588 sf
->quarter_pixel_search
= 1;
589 sf
->half_pixel_search
= 1;
590 sf
->full_freq
[0] = 7;
591 sf
->full_freq
[1] = 7;
592 sf
->min_fs_radius
= 8;
593 sf
->max_fs_radius
= 32;
594 sf
->iterative_sub_pixel
= 1;
595 sf
->optimize_coefficients
= 1;
596 sf
->use_fastquant_for_pick
= 0;
597 sf
->no_skip_block4x4_search
= 1;
600 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
605 // default thresholds to 0
606 for (i
= 0; i
< MAX_MODES
; i
++)
607 sf
->thresh_mult
[i
] = 0;
611 #if !(CONFIG_REALTIME_ONLY)
612 case 0: // best quality mode
613 sf
->thresh_mult
[THR_ZEROMV
] = 0;
614 sf
->thresh_mult
[THR_ZEROG
] = 0;
615 sf
->thresh_mult
[THR_ZEROA
] = 0;
616 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
617 sf
->thresh_mult
[THR_NEARESTG
] = 0;
618 sf
->thresh_mult
[THR_NEARESTA
] = 0;
619 sf
->thresh_mult
[THR_NEARMV
] = 0;
620 sf
->thresh_mult
[THR_NEARG
] = 0;
621 sf
->thresh_mult
[THR_NEARA
] = 0;
623 sf
->thresh_mult
[THR_DC
] = 0;
625 sf
->thresh_mult
[THR_V_PRED
] = 1000;
626 sf
->thresh_mult
[THR_H_PRED
] = 1000;
627 sf
->thresh_mult
[THR_B_PRED
] = 2000;
628 sf
->thresh_mult
[THR_TM
] = 1000;
630 sf
->thresh_mult
[THR_NEWMV
] = 1000;
631 sf
->thresh_mult
[THR_NEWG
] = 1000;
632 sf
->thresh_mult
[THR_NEWA
] = 1000;
634 sf
->thresh_mult
[THR_SPLITMV
] = 2500;
635 sf
->thresh_mult
[THR_SPLITG
] = 5000;
636 sf
->thresh_mult
[THR_SPLITA
] = 5000;
638 sf
->full_freq
[0] = 7;
639 sf
->full_freq
[1] = 15;
642 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
644 if (!(cpi
->ref_frame_flags
& VP8_LAST_FLAG
))
646 sf
->thresh_mult
[THR_NEWMV
] = INT_MAX
;
647 sf
->thresh_mult
[THR_NEARESTMV
] = INT_MAX
;
648 sf
->thresh_mult
[THR_ZEROMV
] = INT_MAX
;
649 sf
->thresh_mult
[THR_NEARMV
] = INT_MAX
;
650 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
653 if (!(cpi
->ref_frame_flags
& VP8_GOLD_FLAG
))
655 sf
->thresh_mult
[THR_NEARESTG
] = INT_MAX
;
656 sf
->thresh_mult
[THR_ZEROG
] = INT_MAX
;
657 sf
->thresh_mult
[THR_NEARG
] = INT_MAX
;
658 sf
->thresh_mult
[THR_NEWG
] = INT_MAX
;
659 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
662 if (!(cpi
->ref_frame_flags
& VP8_ALT_FLAG
))
664 sf
->thresh_mult
[THR_NEARESTA
] = INT_MAX
;
665 sf
->thresh_mult
[THR_ZEROA
] = INT_MAX
;
666 sf
->thresh_mult
[THR_NEARA
] = INT_MAX
;
667 sf
->thresh_mult
[THR_NEWA
] = INT_MAX
;
668 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
674 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
675 sf
->thresh_mult
[THR_ZEROMV
] = 0;
676 sf
->thresh_mult
[THR_DC
] = 0;
677 sf
->thresh_mult
[THR_NEARMV
] = 0;
678 sf
->thresh_mult
[THR_V_PRED
] = 1000;
679 sf
->thresh_mult
[THR_H_PRED
] = 1000;
680 sf
->thresh_mult
[THR_B_PRED
] = 2500;
681 sf
->thresh_mult
[THR_TM
] = 1000;
683 sf
->thresh_mult
[THR_NEARESTG
] = 1000;
684 sf
->thresh_mult
[THR_NEARESTA
] = 1000;
686 sf
->thresh_mult
[THR_ZEROG
] = 1000;
687 sf
->thresh_mult
[THR_ZEROA
] = 1000;
688 sf
->thresh_mult
[THR_NEARG
] = 1000;
689 sf
->thresh_mult
[THR_NEARA
] = 1000;
692 sf
->thresh_mult
[THR_ZEROMV
] = 0;
693 sf
->thresh_mult
[THR_ZEROG
] = 0;
694 sf
->thresh_mult
[THR_ZEROA
] = 0;
695 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
696 sf
->thresh_mult
[THR_NEARESTG
] = 0;
697 sf
->thresh_mult
[THR_NEARESTA
] = 0;
698 sf
->thresh_mult
[THR_NEARMV
] = 0;
699 sf
->thresh_mult
[THR_NEARG
] = 0;
700 sf
->thresh_mult
[THR_NEARA
] = 0;
702 // sf->thresh_mult[THR_DC ] = 0;
704 // sf->thresh_mult[THR_V_PRED ] = 1000;
705 // sf->thresh_mult[THR_H_PRED ] = 1000;
706 // sf->thresh_mult[THR_B_PRED ] = 2000;
707 // sf->thresh_mult[THR_TM ] = 1000;
709 sf
->thresh_mult
[THR_NEWMV
] = 1000;
710 sf
->thresh_mult
[THR_NEWG
] = 1000;
711 sf
->thresh_mult
[THR_NEWA
] = 1000;
713 sf
->thresh_mult
[THR_SPLITMV
] = 1700;
714 sf
->thresh_mult
[THR_SPLITG
] = 4500;
715 sf
->thresh_mult
[THR_SPLITA
] = 4500;
717 sf
->thresh_mult
[THR_NEWMV
] = 1500;
718 sf
->thresh_mult
[THR_NEWG
] = 1500;
719 sf
->thresh_mult
[THR_NEWA
] = 1500;
721 sf
->thresh_mult
[THR_SPLITMV
] = 5000;
722 sf
->thresh_mult
[THR_SPLITG
] = 10000;
723 sf
->thresh_mult
[THR_SPLITA
] = 10000;
725 sf
->full_freq
[0] = 15;
726 sf
->full_freq
[1] = 31;
729 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
731 if (!(cpi
->ref_frame_flags
& VP8_LAST_FLAG
))
733 sf
->thresh_mult
[THR_NEWMV
] = INT_MAX
;
734 sf
->thresh_mult
[THR_NEARESTMV
] = INT_MAX
;
735 sf
->thresh_mult
[THR_ZEROMV
] = INT_MAX
;
736 sf
->thresh_mult
[THR_NEARMV
] = INT_MAX
;
737 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
740 if (!(cpi
->ref_frame_flags
& VP8_GOLD_FLAG
))
742 sf
->thresh_mult
[THR_NEARESTG
] = INT_MAX
;
743 sf
->thresh_mult
[THR_ZEROG
] = INT_MAX
;
744 sf
->thresh_mult
[THR_NEARG
] = INT_MAX
;
745 sf
->thresh_mult
[THR_NEWG
] = INT_MAX
;
746 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
749 if (!(cpi
->ref_frame_flags
& VP8_ALT_FLAG
))
751 sf
->thresh_mult
[THR_NEARESTA
] = INT_MAX
;
752 sf
->thresh_mult
[THR_ZEROA
] = INT_MAX
;
753 sf
->thresh_mult
[THR_NEARA
] = INT_MAX
;
754 sf
->thresh_mult
[THR_NEWA
] = INT_MAX
;
755 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
760 // Disable coefficient optimization above speed 0
761 sf
->optimize_coefficients
= 0;
763 cpi
->mode_check_freq
[THR_SPLITG
] = 4;
764 cpi
->mode_check_freq
[THR_SPLITA
] = 4;
765 cpi
->mode_check_freq
[THR_SPLITMV
] = 2;
767 sf
->thresh_mult
[THR_TM
] = 1500;
768 sf
->thresh_mult
[THR_V_PRED
] = 1500;
769 sf
->thresh_mult
[THR_H_PRED
] = 1500;
770 sf
->thresh_mult
[THR_B_PRED
] = 5000;
772 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
774 sf
->thresh_mult
[THR_NEWMV
] = 2000;
775 sf
->thresh_mult
[THR_SPLITMV
] = 10000;
778 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
780 sf
->thresh_mult
[THR_NEARESTG
] = 1500;
781 sf
->thresh_mult
[THR_ZEROG
] = 1500;
782 sf
->thresh_mult
[THR_NEARG
] = 1500;
783 sf
->thresh_mult
[THR_NEWG
] = 2000;
784 sf
->thresh_mult
[THR_SPLITG
] = 20000;
787 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
789 sf
->thresh_mult
[THR_NEARESTA
] = 1500;
790 sf
->thresh_mult
[THR_ZEROA
] = 1500;
791 sf
->thresh_mult
[THR_NEARA
] = 1500;
792 sf
->thresh_mult
[THR_NEWA
] = 2000;
793 sf
->thresh_mult
[THR_SPLITA
] = 20000;
796 sf
->use_fastquant_for_pick
= 1;
799 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
800 sf
->no_skip_block4x4_search
= 0;
805 sf
->use_fastquant_for_pick
= 0;
807 cpi
->mode_check_freq
[THR_SPLITG
] = 15;
808 cpi
->mode_check_freq
[THR_SPLITA
] = 15;
809 cpi
->mode_check_freq
[THR_SPLITMV
] = 7;
811 sf
->thresh_mult
[THR_TM
] = 2000;
812 sf
->thresh_mult
[THR_V_PRED
] = 2000;
813 sf
->thresh_mult
[THR_H_PRED
] = 2000;
814 sf
->thresh_mult
[THR_B_PRED
] = 7500;
816 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
818 sf
->thresh_mult
[THR_NEWMV
] = 2000;
819 sf
->thresh_mult
[THR_SPLITMV
] = 25000;
822 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
824 sf
->thresh_mult
[THR_NEARESTG
] = 2000;
825 sf
->thresh_mult
[THR_ZEROG
] = 2000;
826 sf
->thresh_mult
[THR_NEARG
] = 2000;
827 sf
->thresh_mult
[THR_NEWG
] = 2500;
828 sf
->thresh_mult
[THR_SPLITG
] = 50000;
831 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
833 sf
->thresh_mult
[THR_NEARESTA
] = 2000;
834 sf
->thresh_mult
[THR_ZEROA
] = 2000;
835 sf
->thresh_mult
[THR_NEARA
] = 2000;
836 sf
->thresh_mult
[THR_NEWA
] = 2500;
837 sf
->thresh_mult
[THR_SPLITA
] = 50000;
842 sf
->improved_quant
= 0;
843 sf
->improved_dct
= 0;
845 // Only do recode loop on key frames, golden frames and
849 sf
->full_freq
[0] = 31;
850 sf
->full_freq
[1] = 63;
856 sf
->auto_filter
= 0; // Faster selection of loop filter
857 cpi
->mode_check_freq
[THR_V_PRED
] = 2;
858 cpi
->mode_check_freq
[THR_H_PRED
] = 2;
859 cpi
->mode_check_freq
[THR_B_PRED
] = 2;
861 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
863 cpi
->mode_check_freq
[THR_NEARG
] = 2;
864 cpi
->mode_check_freq
[THR_NEWG
] = 4;
867 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
869 cpi
->mode_check_freq
[THR_NEARA
] = 2;
870 cpi
->mode_check_freq
[THR_NEWA
] = 4;
873 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
874 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
875 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
877 sf
->full_freq
[0] = 63;
878 sf
->full_freq
[1] = 127;
883 cpi
->mode_check_freq
[THR_V_PRED
] = 0;
884 cpi
->mode_check_freq
[THR_H_PRED
] = 0;
885 cpi
->mode_check_freq
[THR_B_PRED
] = 0;
886 cpi
->mode_check_freq
[THR_NEARG
] = 0;
887 cpi
->mode_check_freq
[THR_NEWG
] = 0;
888 cpi
->mode_check_freq
[THR_NEARA
] = 0;
889 cpi
->mode_check_freq
[THR_NEWA
] = 0;
892 sf
->recode_loop
= 0; // recode loop off
893 sf
->RD
= 0; // Turn rd off
894 sf
->full_freq
[0] = INT_MAX
;
895 sf
->full_freq
[1] = INT_MAX
;
900 sf
->auto_filter
= 0; // Faster selection of loop filter
902 cpi
->mode_check_freq
[THR_V_PRED
] = 2;
903 cpi
->mode_check_freq
[THR_H_PRED
] = 2;
904 cpi
->mode_check_freq
[THR_B_PRED
] = 2;
906 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
908 cpi
->mode_check_freq
[THR_NEARG
] = 2;
909 cpi
->mode_check_freq
[THR_NEWG
] = 4;
912 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
914 cpi
->mode_check_freq
[THR_NEARA
] = 2;
915 cpi
->mode_check_freq
[THR_NEWA
] = 4;
918 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
920 sf
->thresh_mult
[THR_NEARESTG
] = 2000;
921 sf
->thresh_mult
[THR_ZEROG
] = 2000;
922 sf
->thresh_mult
[THR_NEARG
] = 2000;
923 sf
->thresh_mult
[THR_NEWG
] = 4000;
926 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
928 sf
->thresh_mult
[THR_NEARESTA
] = 2000;
929 sf
->thresh_mult
[THR_ZEROA
] = 2000;
930 sf
->thresh_mult
[THR_NEARA
] = 2000;
931 sf
->thresh_mult
[THR_NEWA
] = 4000;
938 sf
->optimize_coefficients
= 0;
941 sf
->iterative_sub_pixel
= 1;
942 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
943 sf
->thresh_mult
[THR_ZEROMV
] = 0;
944 sf
->thresh_mult
[THR_DC
] = 0;
945 sf
->thresh_mult
[THR_TM
] = 0;
946 sf
->thresh_mult
[THR_NEARMV
] = 0;
947 sf
->thresh_mult
[THR_V_PRED
] = 1000;
948 sf
->thresh_mult
[THR_H_PRED
] = 1000;
949 sf
->thresh_mult
[THR_B_PRED
] = 2500;
950 sf
->thresh_mult
[THR_NEARESTG
] = 1000;
951 sf
->thresh_mult
[THR_ZEROG
] = 1000;
952 sf
->thresh_mult
[THR_NEARG
] = 1000;
953 sf
->thresh_mult
[THR_NEARESTA
] = 1000;
954 sf
->thresh_mult
[THR_ZEROA
] = 1000;
955 sf
->thresh_mult
[THR_NEARA
] = 1000;
956 sf
->thresh_mult
[THR_NEWMV
] = 2000;
957 sf
->thresh_mult
[THR_NEWG
] = 2000;
958 sf
->thresh_mult
[THR_NEWA
] = 2000;
959 sf
->thresh_mult
[THR_SPLITMV
] = 5000;
960 sf
->thresh_mult
[THR_SPLITG
] = 10000;
961 sf
->thresh_mult
[THR_SPLITA
] = 10000;
962 sf
->full_freq
[0] = 15;
963 sf
->full_freq
[1] = 31;
964 sf
->search_method
= NSTEP
;
966 if (!(cpi
->ref_frame_flags
& VP8_LAST_FLAG
))
968 sf
->thresh_mult
[THR_NEWMV
] = INT_MAX
;
969 sf
->thresh_mult
[THR_NEARESTMV
] = INT_MAX
;
970 sf
->thresh_mult
[THR_ZEROMV
] = INT_MAX
;
971 sf
->thresh_mult
[THR_NEARMV
] = INT_MAX
;
972 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
975 if (!(cpi
->ref_frame_flags
& VP8_GOLD_FLAG
))
977 sf
->thresh_mult
[THR_NEARESTG
] = INT_MAX
;
978 sf
->thresh_mult
[THR_ZEROG
] = INT_MAX
;
979 sf
->thresh_mult
[THR_NEARG
] = INT_MAX
;
980 sf
->thresh_mult
[THR_NEWG
] = INT_MAX
;
981 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
984 if (!(cpi
->ref_frame_flags
& VP8_ALT_FLAG
))
986 sf
->thresh_mult
[THR_NEARESTA
] = INT_MAX
;
987 sf
->thresh_mult
[THR_ZEROA
] = INT_MAX
;
988 sf
->thresh_mult
[THR_NEARA
] = INT_MAX
;
989 sf
->thresh_mult
[THR_NEWA
] = INT_MAX
;
990 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
995 cpi
->mode_check_freq
[THR_SPLITG
] = 4;
996 cpi
->mode_check_freq
[THR_SPLITA
] = 4;
997 cpi
->mode_check_freq
[THR_SPLITMV
] = 2;
999 sf
->thresh_mult
[THR_DC
] = 0;
1000 sf
->thresh_mult
[THR_TM
] = 1000;
1001 sf
->thresh_mult
[THR_V_PRED
] = 2000;
1002 sf
->thresh_mult
[THR_H_PRED
] = 2000;
1003 sf
->thresh_mult
[THR_B_PRED
] = 5000;
1005 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
1007 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
1008 sf
->thresh_mult
[THR_ZEROMV
] = 0;
1009 sf
->thresh_mult
[THR_NEARMV
] = 0;
1010 sf
->thresh_mult
[THR_NEWMV
] = 2000;
1011 sf
->thresh_mult
[THR_SPLITMV
] = 10000;
1014 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
1016 sf
->thresh_mult
[THR_NEARESTG
] = 1000;
1017 sf
->thresh_mult
[THR_ZEROG
] = 1000;
1018 sf
->thresh_mult
[THR_NEARG
] = 1000;
1019 sf
->thresh_mult
[THR_NEWG
] = 2000;
1020 sf
->thresh_mult
[THR_SPLITG
] = 20000;
1023 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
1025 sf
->thresh_mult
[THR_NEARESTA
] = 1000;
1026 sf
->thresh_mult
[THR_ZEROA
] = 1000;
1027 sf
->thresh_mult
[THR_NEARA
] = 1000;
1028 sf
->thresh_mult
[THR_NEWA
] = 2000;
1029 sf
->thresh_mult
[THR_SPLITA
] = 20000;
1032 sf
->improved_quant
= 0;
1033 sf
->improved_dct
= 0;
1038 cpi
->mode_check_freq
[THR_SPLITMV
] = 7;
1039 cpi
->mode_check_freq
[THR_SPLITG
] = 15;
1040 cpi
->mode_check_freq
[THR_SPLITA
] = 15;
1042 sf
->thresh_mult
[THR_TM
] = 2000;
1043 sf
->thresh_mult
[THR_V_PRED
] = 2000;
1044 sf
->thresh_mult
[THR_H_PRED
] = 2000;
1045 sf
->thresh_mult
[THR_B_PRED
] = 5000;
1047 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
1049 sf
->thresh_mult
[THR_NEWMV
] = 2000;
1050 sf
->thresh_mult
[THR_SPLITMV
] = 25000;
1053 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
1055 sf
->thresh_mult
[THR_NEARESTG
] = 2000;
1056 sf
->thresh_mult
[THR_ZEROG
] = 2000;
1057 sf
->thresh_mult
[THR_NEARG
] = 2000;
1058 sf
->thresh_mult
[THR_NEWG
] = 2500;
1059 sf
->thresh_mult
[THR_SPLITG
] = 50000;
1062 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
1064 sf
->thresh_mult
[THR_NEARESTA
] = 2000;
1065 sf
->thresh_mult
[THR_ZEROA
] = 2000;
1066 sf
->thresh_mult
[THR_NEARA
] = 2000;
1067 sf
->thresh_mult
[THR_NEWA
] = 2500;
1068 sf
->thresh_mult
[THR_SPLITA
] = 50000;
1071 sf
->full_freq
[0] = 31;
1072 sf
->full_freq
[1] = 63;
1077 sf
->auto_filter
= 0; // Faster selection of loop filter
1079 cpi
->mode_check_freq
[THR_V_PRED
] = 2;
1080 cpi
->mode_check_freq
[THR_H_PRED
] = 2;
1081 cpi
->mode_check_freq
[THR_B_PRED
] = 2;
1083 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
1085 cpi
->mode_check_freq
[THR_NEARG
] = 2;
1086 cpi
->mode_check_freq
[THR_NEWG
] = 4;
1089 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
1091 cpi
->mode_check_freq
[THR_NEARA
] = 2;
1092 cpi
->mode_check_freq
[THR_NEWA
] = 4;
1095 sf
->thresh_mult
[THR_SPLITMV
] = INT_MAX
;
1096 sf
->thresh_mult
[THR_SPLITG
] = INT_MAX
;
1097 sf
->thresh_mult
[THR_SPLITA
] = INT_MAX
;
1099 sf
->full_freq
[0] = 63;
1100 sf
->full_freq
[1] = 127;
1106 sf
->full_freq
[0] = INT_MAX
;
1107 sf
->full_freq
[1] = INT_MAX
;
1109 sf
->auto_filter
= 1;
1114 sf
->auto_filter
= 0; // Faster selection of loop filter
1116 #if CONFIG_REALTIME_ONLY
1117 sf
->search_method
= HEX
;
1119 sf
->search_method
= DIAMOND
;
1122 cpi
->mode_check_freq
[THR_V_PRED
] = 4;
1123 cpi
->mode_check_freq
[THR_H_PRED
] = 4;
1124 cpi
->mode_check_freq
[THR_B_PRED
] = 4;
1126 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
1128 cpi
->mode_check_freq
[THR_NEARG
] = 2;
1129 cpi
->mode_check_freq
[THR_NEWG
] = 4;
1132 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
1134 cpi
->mode_check_freq
[THR_NEARA
] = 2;
1135 cpi
->mode_check_freq
[THR_NEWA
] = 4;
1138 sf
->thresh_mult
[THR_TM
] = 2000;
1139 sf
->thresh_mult
[THR_B_PRED
] = 5000;
1141 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
1143 sf
->thresh_mult
[THR_NEARESTG
] = 2000;
1144 sf
->thresh_mult
[THR_ZEROG
] = 2000;
1145 sf
->thresh_mult
[THR_NEARG
] = 2000;
1146 sf
->thresh_mult
[THR_NEWG
] = 4000;
1149 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
1151 sf
->thresh_mult
[THR_NEARESTA
] = 2000;
1152 sf
->thresh_mult
[THR_ZEROA
] = 2000;
1153 sf
->thresh_mult
[THR_NEARA
] = 2000;
1154 sf
->thresh_mult
[THR_NEWA
] = 4000;
1160 // Disable split MB intra prediction mode
1161 sf
->thresh_mult
[THR_B_PRED
] = INT_MAX
;
1166 unsigned int i
, sum
= 0;
1167 unsigned int total_mbs
= cm
->MBs
;
1172 sf
->iterative_sub_pixel
= 0;
1174 if (cpi
->oxcf
.encode_breakout
> 2000)
1175 min
= cpi
->oxcf
.encode_breakout
;
1179 for (i
= 0; i
< min
; i
++)
1181 sum
+= cpi
->error_bins
[i
];
1187 // i starts from 2 to make sure thresh started from 2048
1188 for (; i
< 1024; i
++)
1190 sum
+= cpi
->error_bins
[i
];
1192 if (10 * sum
>= (unsigned int)(cpi
->Speed
- 6)*(total_mbs
- total_skip
))
1202 if (cpi
->ref_frame_flags
& VP8_LAST_FLAG
)
1204 sf
->thresh_mult
[THR_NEWMV
] = thresh
;
1205 sf
->thresh_mult
[THR_NEARESTMV
] = thresh
>> 1;
1206 sf
->thresh_mult
[THR_NEARMV
] = thresh
>> 1;
1209 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
1211 sf
->thresh_mult
[THR_NEWG
] = thresh
<< 1;
1212 sf
->thresh_mult
[THR_NEARESTG
] = thresh
;
1213 sf
->thresh_mult
[THR_NEARG
] = thresh
;
1216 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
1218 sf
->thresh_mult
[THR_NEWA
] = thresh
<< 1;
1219 sf
->thresh_mult
[THR_NEARESTA
] = thresh
;
1220 sf
->thresh_mult
[THR_NEARA
] = thresh
;
1223 // Disable other intra prediction modes
1224 sf
->thresh_mult
[THR_TM
] = INT_MAX
;
1225 sf
->thresh_mult
[THR_V_PRED
] = INT_MAX
;
1226 sf
->thresh_mult
[THR_H_PRED
] = INT_MAX
;
1232 sf
->quarter_pixel_search
= 0;
1237 int Tmp
= cpi
->Speed
- 8;
1242 if (cpi
->ref_frame_flags
& VP8_GOLD_FLAG
)
1244 cpi
->mode_check_freq
[THR_ZEROG
] = 1 << (Tmp
- 1);
1245 cpi
->mode_check_freq
[THR_NEARESTG
] = 1 << (Tmp
- 1);
1246 cpi
->mode_check_freq
[THR_NEARG
] = 1 << Tmp
;
1247 cpi
->mode_check_freq
[THR_NEWG
] = 1 << (Tmp
+ 1);
1250 if (cpi
->ref_frame_flags
& VP8_ALT_FLAG
)
1252 cpi
->mode_check_freq
[THR_ZEROA
] = 1 << (Tmp
- 1);
1253 cpi
->mode_check_freq
[THR_NEARESTA
] = 1 << (Tmp
- 1);
1254 cpi
->mode_check_freq
[THR_NEARA
] = 1 << Tmp
;
1255 cpi
->mode_check_freq
[THR_NEWA
] = 1 << (Tmp
+ 1);
1258 cpi
->mode_check_freq
[THR_NEWMV
] = 1 << (Tmp
- 1);
1261 cm
->filter_type
= NORMAL_LOOPFILTER
;
1264 cm
->filter_type
= SIMPLE_LOOPFILTER
;
1268 sf
->half_pixel_search
= 0; // This has a big hit on quality. Last resort
1271 vpx_memset(cpi
->error_bins
, 0, sizeof(cpi
->error_bins
));
1275 // Slow quant, dct and trellis not worthwhile for first pass
1276 // so make sure they are always turned off.
1277 if ( cpi
->pass
== 1 )
1279 sf
->improved_quant
= 0;
1280 sf
->optimize_coefficients
= 0;
1281 sf
->improved_dct
= 0;
1284 if (cpi
->sf
.search_method
== NSTEP
)
1286 vp8_init3smotion_compensation(&cpi
->mb
, cm
->yv12_fb
[cm
->lst_fb_idx
].y_stride
);
1288 else if (cpi
->sf
.search_method
== DIAMOND
)
1290 vp8_init_dsmotion_compensation(&cpi
->mb
, cm
->yv12_fb
[cm
->lst_fb_idx
].y_stride
);
1293 if (cpi
->sf
.improved_dct
)
1295 cpi
->mb
.vp8_short_fdct8x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, short8x4
);
1296 cpi
->mb
.vp8_short_fdct4x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, short4x4
);
1300 cpi
->mb
.vp8_short_fdct8x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, fast8x4
);
1301 cpi
->mb
.vp8_short_fdct4x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, fast4x4
);
1304 cpi
->mb
.short_walsh4x4
= FDCT_INVOKE(&cpi
->rtcd
.fdct
, walsh_short4x4
);
1306 if (cpi
->sf
.improved_quant
)
1308 cpi
->mb
.quantize_b
= QUANTIZE_INVOKE(&cpi
->rtcd
.quantize
, quantb
);
1312 cpi
->mb
.quantize_b
= QUANTIZE_INVOKE(&cpi
->rtcd
.quantize
, fastquantb
);
1314 if (cpi
->sf
.improved_quant
!= last_improved_quant
)
1315 vp8cx_init_quantizer(cpi
);
1317 #if CONFIG_RUNTIME_CPU_DETECT
1318 cpi
->mb
.e_mbd
.rtcd
= &cpi
->common
.rtcd
;
1321 if (cpi
->sf
.iterative_sub_pixel
== 1)
1323 cpi
->find_fractional_mv_step
= vp8_find_best_sub_pixel_step_iteratively
;
1325 else if (cpi
->sf
.quarter_pixel_search
)
1327 cpi
->find_fractional_mv_step
= vp8_find_best_sub_pixel_step
;
1329 else if (cpi
->sf
.half_pixel_search
)
1331 cpi
->find_fractional_mv_step
= vp8_find_best_half_pixel_step
;
1335 cpi
->find_fractional_mv_step
= vp8_skip_fractional_mv_step
;
1338 if (cpi
->sf
.optimize_coefficients
== 1)
1339 cpi
->mb
.optimize
= 1 + cpi
->is_next_src_alt_ref
;
1341 cpi
->mb
.optimize
= 0;
1343 if (cpi
->common
.full_pixel
)
1344 cpi
->find_fractional_mv_step
= vp8_skip_fractional_mv_step
;
1347 frames_at_speed
[cpi
->Speed
]++;
1350 static void alloc_raw_frame_buffers(VP8_COMP
*cpi
)
1354 buffers
= cpi
->oxcf
.lag_in_frames
;
1356 if (buffers
> MAX_LAG_BUFFERS
)
1357 buffers
= MAX_LAG_BUFFERS
;
1362 for (i
= 0; i
< buffers
; i
++)
1363 if (vp8_yv12_alloc_frame_buffer(&cpi
->src_buffer
[i
].source_buffer
,
1364 cpi
->oxcf
.Width
, cpi
->oxcf
.Height
,
1366 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1367 "Failed to allocate lag buffer");
1369 #if VP8_TEMPORAL_ALT_REF
1371 if (vp8_yv12_alloc_frame_buffer(&cpi
->alt_ref_buffer
.source_buffer
,
1372 cpi
->oxcf
.Width
, cpi
->oxcf
.Height
, 16))
1373 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1374 "Failed to allocate altref buffer");
1378 cpi
->source_buffer_count
= 0;
1381 static int vp8_alloc_partition_data(VP8_COMP
*cpi
)
1384 vpx_free(cpi
->mb
.pip
);
1386 cpi
->mb
.pip
= vpx_calloc((cpi
->common
.mb_cols
+ 1) *
1387 (cpi
->common
.mb_rows
+ 1),
1388 sizeof(PARTITION_INFO
));
1390 return ALLOC_FAILURE
;
1392 cpi
->mb
.pi
= cpi
->mb
.pip
+ cpi
->common
.mode_info_stride
+ 1;
1397 void vp8_alloc_compressor_data(VP8_COMP
*cpi
)
1399 VP8_COMMON
*cm
= & cpi
->common
;
1401 int width
= cm
->Width
;
1402 int height
= cm
->Height
;
1404 if (vp8_alloc_frame_buffers(cm
, width
, height
))
1405 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1406 "Failed to allocate frame buffers");
1408 if (vp8_alloc_partition_data(cpi
))
1409 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1410 "Failed to allocate partition data");
1413 if ((width
& 0xf) != 0)
1414 width
+= 16 - (width
& 0xf);
1416 if ((height
& 0xf) != 0)
1417 height
+= 16 - (height
& 0xf);
1420 if (vp8_yv12_alloc_frame_buffer(&cpi
->last_frame_uf
,
1421 width
, height
, VP8BORDERINPIXELS
))
1422 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1423 "Failed to allocate last frame buffer");
1425 if (vp8_yv12_alloc_frame_buffer(&cpi
->scaled_source
, width
, height
, 16))
1426 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1427 "Failed to allocate scaled source buffer");
1434 unsigned int tokens
= cm
->mb_rows
* cm
->mb_cols
* 24 * 16;
1436 CHECK_MEM_ERROR(cpi
->tok
, vpx_calloc(tokens
, sizeof(*cpi
->tok
)));
1439 // Data used for real time vc mode to see if gf needs refreshing
1440 cpi
->inter_zz_count
= 0;
1441 cpi
->gf_bad_count
= 0;
1442 cpi
->gf_update_recommended
= 0;
1445 // Structures used to minitor GF usage
1446 if (cpi
->gf_active_flags
!= 0)
1447 vpx_free(cpi
->gf_active_flags
);
1449 CHECK_MEM_ERROR(cpi
->gf_active_flags
, vpx_calloc(1, cm
->mb_rows
* cm
->mb_cols
));
1451 cpi
->gf_active_count
= cm
->mb_rows
* cm
->mb_cols
;
1453 #if !(CONFIG_REALTIME_ONLY)
1454 if(cpi
->total_stats
)
1455 vpx_free(cpi
->total_stats
);
1457 cpi
->total_stats
= vpx_calloc(1, vp8_firstpass_stats_sz(cpi
->common
.MBs
));
1459 if(cpi
->this_frame_stats
)
1460 vpx_free(cpi
->this_frame_stats
);
1462 cpi
->this_frame_stats
= vpx_calloc(1, vp8_firstpass_stats_sz(cpi
->common
.MBs
));
1464 if(!cpi
->total_stats
|| !cpi
->this_frame_stats
)
1465 vpx_internal_error(&cpi
->common
.error
, VPX_CODEC_MEM_ERROR
,
1466 "Failed to allocate firstpass stats");
1472 static const int q_trans
[] =
1474 0, 1, 2, 3, 4, 5, 7, 8,
1475 9, 10, 12, 13, 15, 17, 18, 19,
1476 20, 21, 23, 24, 25, 26, 27, 28,
1477 29, 30, 31, 33, 35, 37, 39, 41,
1478 43, 45, 47, 49, 51, 53, 55, 57,
1479 59, 61, 64, 67, 70, 73, 76, 79,
1480 82, 85, 88, 91, 94, 97, 100, 103,
1481 106, 109, 112, 115, 118, 121, 124, 127,
1484 int vp8_reverse_trans(int x
)
1488 for (i
= 0; i
< 64; i
++)
1489 if (q_trans
[i
] >= x
)
1494 void vp8_new_frame_rate(VP8_COMP
*cpi
, double framerate
)
1499 cpi
->oxcf
.frame_rate
= framerate
;
1500 cpi
->output_frame_rate
= cpi
->oxcf
.frame_rate
;
1501 cpi
->per_frame_bandwidth
= (int)(cpi
->oxcf
.target_bandwidth
/ cpi
->output_frame_rate
);
1502 cpi
->av_per_frame_bandwidth
= (int)(cpi
->oxcf
.target_bandwidth
/ cpi
->output_frame_rate
);
1503 cpi
->min_frame_bandwidth
= (int)(cpi
->av_per_frame_bandwidth
* cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
1505 // Set Maximum gf/arf interval
1506 cpi
->max_gf_interval
= ((int)(cpi
->output_frame_rate
/ 2.0) + 2);
1508 if(cpi
->max_gf_interval
< 12)
1509 cpi
->max_gf_interval
= 12;
1511 // Extended interval for genuinely static scenes
1512 cpi
->static_scene_max_gf_interval
= cpi
->key_frame_frequency
>> 1;
1514 // Special conditions when altr ref frame enabled in lagged compress mode
1515 if (cpi
->oxcf
.play_alternate
&& cpi
->oxcf
.lag_in_frames
)
1517 if (cpi
->max_gf_interval
> cpi
->oxcf
.lag_in_frames
- 1)
1518 cpi
->max_gf_interval
= cpi
->oxcf
.lag_in_frames
- 1;
1520 if (cpi
->static_scene_max_gf_interval
> cpi
->oxcf
.lag_in_frames
- 1)
1521 cpi
->static_scene_max_gf_interval
= cpi
->oxcf
.lag_in_frames
- 1;
1524 if ( cpi
->max_gf_interval
> cpi
->static_scene_max_gf_interval
)
1525 cpi
->max_gf_interval
= cpi
->static_scene_max_gf_interval
;
1530 rescale(int val
, int num
, int denom
)
1532 int64_t llnum
= num
;
1533 int64_t llden
= denom
;
1534 int64_t llval
= val
;
1536 return llval
* llnum
/ llden
;
1540 void vp8_init_config(VP8_PTR ptr
, VP8_CONFIG
*oxcf
)
1542 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
1543 VP8_COMMON
*cm
= &cpi
->common
;
1549 cpi
->auto_adjust_gold_quantizer
= 1;
1550 cpi
->goldquantizer
= 1;
1552 cpi
->auto_adjust_key_quantizer
= 1;
1553 cpi
->keyquantizer
= 1;
1555 cm
->version
= oxcf
->Version
;
1556 vp8_setup_version(cm
);
1562 cpi
->auto_worst_q
= 0;
1563 cpi
->oxcf
.best_allowed_q
= MINQ
;
1564 cpi
->oxcf
.worst_allowed_q
= MAXQ
;
1565 cpi
->oxcf
.cq_level
= MINQ
;
1567 cpi
->oxcf
.end_usage
= USAGE_STREAM_FROM_SERVER
;
1568 cpi
->oxcf
.starting_buffer_level
= 4000;
1569 cpi
->oxcf
.optimal_buffer_level
= 5000;
1570 cpi
->oxcf
.maximum_buffer_size
= 6000;
1571 cpi
->oxcf
.under_shoot_pct
= 90;
1572 cpi
->oxcf
.allow_df
= 0;
1573 cpi
->oxcf
.drop_frames_water_mark
= 20;
1575 cpi
->oxcf
.allow_spatial_resampling
= 0;
1576 cpi
->oxcf
.resample_down_water_mark
= 40;
1577 cpi
->oxcf
.resample_up_water_mark
= 60;
1579 cpi
->oxcf
.fixed_q
= cpi
->interquantizer
;
1581 cpi
->filter_type
= NORMAL_LOOPFILTER
;
1583 if (cm
->simpler_lpf
)
1584 cpi
->filter_type
= SIMPLE_LOOPFILTER
;
1586 cpi
->compressor_speed
= 1;
1587 cpi
->horiz_scale
= 0;
1588 cpi
->vert_scale
= 0;
1589 cpi
->oxcf
.two_pass_vbrbias
= 50;
1590 cpi
->oxcf
.two_pass_vbrmax_section
= 400;
1591 cpi
->oxcf
.two_pass_vbrmin_section
= 0;
1593 cpi
->oxcf
.Sharpness
= 0;
1594 cpi
->oxcf
.noise_sensitivity
= 0;
1600 switch (cpi
->oxcf
.Mode
)
1605 cpi
->compressor_speed
= 2;
1607 if (cpi
->oxcf
.cpu_used
< -16)
1609 cpi
->oxcf
.cpu_used
= -16;
1612 if (cpi
->oxcf
.cpu_used
> 16)
1613 cpi
->oxcf
.cpu_used
= 16;
1617 #if !(CONFIG_REALTIME_ONLY)
1618 case MODE_GOODQUALITY
:
1620 cpi
->compressor_speed
= 1;
1622 if (cpi
->oxcf
.cpu_used
< -5)
1624 cpi
->oxcf
.cpu_used
= -5;
1627 if (cpi
->oxcf
.cpu_used
> 5)
1628 cpi
->oxcf
.cpu_used
= 5;
1632 case MODE_BESTQUALITY
:
1634 cpi
->compressor_speed
= 0;
1637 case MODE_FIRSTPASS
:
1639 cpi
->compressor_speed
= 1;
1641 case MODE_SECONDPASS
:
1643 cpi
->compressor_speed
= 1;
1645 if (cpi
->oxcf
.cpu_used
< -5)
1647 cpi
->oxcf
.cpu_used
= -5;
1650 if (cpi
->oxcf
.cpu_used
> 5)
1651 cpi
->oxcf
.cpu_used
= 5;
1654 case MODE_SECONDPASS_BEST
:
1656 cpi
->compressor_speed
= 0;
1662 cpi
->auto_worst_q
= 1;
1664 cpi
->oxcf
.worst_allowed_q
= q_trans
[oxcf
->worst_allowed_q
];
1665 cpi
->oxcf
.best_allowed_q
= q_trans
[oxcf
->best_allowed_q
];
1666 cpi
->oxcf
.cq_level
= q_trans
[cpi
->oxcf
.cq_level
];
1668 if (oxcf
->fixed_q
>= 0)
1670 if (oxcf
->worst_allowed_q
< 0)
1671 cpi
->oxcf
.fixed_q
= q_trans
[0];
1673 cpi
->oxcf
.fixed_q
= q_trans
[oxcf
->worst_allowed_q
];
1675 if (oxcf
->alt_q
< 0)
1676 cpi
->oxcf
.alt_q
= q_trans
[0];
1678 cpi
->oxcf
.alt_q
= q_trans
[oxcf
->alt_q
];
1680 if (oxcf
->key_q
< 0)
1681 cpi
->oxcf
.key_q
= q_trans
[0];
1683 cpi
->oxcf
.key_q
= q_trans
[oxcf
->key_q
];
1685 if (oxcf
->gold_q
< 0)
1686 cpi
->oxcf
.gold_q
= q_trans
[0];
1688 cpi
->oxcf
.gold_q
= q_trans
[oxcf
->gold_q
];
1692 cpi
->baseline_gf_interval
= cpi
->oxcf
.alt_freq
? cpi
->oxcf
.alt_freq
: DEFAULT_GF_INTERVAL
;
1693 cpi
->ref_frame_flags
= VP8_ALT_FLAG
| VP8_GOLD_FLAG
| VP8_LAST_FLAG
;
1695 //cpi->use_golden_frame_only = 0;
1696 //cpi->use_last_frame_only = 0;
1697 cm
->refresh_golden_frame
= 0;
1698 cm
->refresh_last_frame
= 1;
1699 cm
->refresh_entropy_probs
= 1;
1701 if (cpi
->oxcf
.token_partitions
>= 0 && cpi
->oxcf
.token_partitions
<= 3)
1702 cm
->multi_token_partition
= (TOKEN_PARTITION
) cpi
->oxcf
.token_partitions
;
1704 setup_features(cpi
);
1709 for (i
= 0; i
< MAX_MB_SEGMENTS
; i
++)
1710 cpi
->segment_encode_breakout
[i
] = cpi
->oxcf
.encode_breakout
;
1713 // At the moment the first order values may not be > MAXQ
1714 if (cpi
->oxcf
.fixed_q
> MAXQ
)
1715 cpi
->oxcf
.fixed_q
= MAXQ
;
1717 // local file playback mode == really big buffer
1718 if (cpi
->oxcf
.end_usage
== USAGE_LOCAL_FILE_PLAYBACK
)
1720 cpi
->oxcf
.starting_buffer_level
= 60000;
1721 cpi
->oxcf
.optimal_buffer_level
= 60000;
1722 cpi
->oxcf
.maximum_buffer_size
= 240000;
1727 // Convert target bandwidth from Kbit/s to Bit/s
1728 cpi
->oxcf
.target_bandwidth
*= 1000;
1729 cpi
->oxcf
.starting_buffer_level
=
1730 rescale(cpi
->oxcf
.starting_buffer_level
,
1731 cpi
->oxcf
.target_bandwidth
, 1000);
1733 if (cpi
->oxcf
.optimal_buffer_level
== 0)
1734 cpi
->oxcf
.optimal_buffer_level
= cpi
->oxcf
.target_bandwidth
/ 8;
1736 cpi
->oxcf
.optimal_buffer_level
=
1737 rescale(cpi
->oxcf
.optimal_buffer_level
,
1738 cpi
->oxcf
.target_bandwidth
, 1000);
1740 if (cpi
->oxcf
.maximum_buffer_size
== 0)
1741 cpi
->oxcf
.maximum_buffer_size
= cpi
->oxcf
.target_bandwidth
/ 8;
1743 cpi
->oxcf
.maximum_buffer_size
=
1744 rescale(cpi
->oxcf
.maximum_buffer_size
,
1745 cpi
->oxcf
.target_bandwidth
, 1000);
1747 cpi
->buffer_level
= cpi
->oxcf
.starting_buffer_level
;
1748 cpi
->bits_off_target
= cpi
->oxcf
.starting_buffer_level
;
1750 vp8_new_frame_rate(cpi
, cpi
->oxcf
.frame_rate
);
1751 cpi
->worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1752 cpi
->active_worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1753 cpi
->avg_frame_qindex
= cpi
->oxcf
.worst_allowed_q
;
1754 cpi
->best_quality
= cpi
->oxcf
.best_allowed_q
;
1755 cpi
->active_best_quality
= cpi
->oxcf
.best_allowed_q
;
1756 cpi
->cq_target_quality
= cpi
->oxcf
.cq_level
;
1758 cpi
->buffered_mode
= (cpi
->oxcf
.optimal_buffer_level
> 0) ? TRUE
: FALSE
;
1760 cpi
->rolling_target_bits
= cpi
->av_per_frame_bandwidth
;
1761 cpi
->rolling_actual_bits
= cpi
->av_per_frame_bandwidth
;
1762 cpi
->long_rolling_target_bits
= cpi
->av_per_frame_bandwidth
;
1763 cpi
->long_rolling_actual_bits
= cpi
->av_per_frame_bandwidth
;
1765 cpi
->total_actual_bits
= 0;
1766 cpi
->total_target_vs_actual
= 0;
1768 // Only allow dropped frames in buffered mode
1769 cpi
->drop_frames_allowed
= cpi
->oxcf
.allow_df
&& cpi
->buffered_mode
;
1771 cm
->filter_type
= (LOOPFILTERTYPE
) cpi
->filter_type
;
1773 if (!cm
->use_bilinear_mc_filter
)
1774 cm
->mcomp_filter_type
= SIXTAP
;
1776 cm
->mcomp_filter_type
= BILINEAR
;
1778 cpi
->target_bandwidth
= cpi
->oxcf
.target_bandwidth
;
1780 cm
->Width
= cpi
->oxcf
.Width
;
1781 cm
->Height
= cpi
->oxcf
.Height
;
1783 cpi
->intra_frame_target
= (4 * (cm
->Width
+ cm
->Height
) / 15) * 1000; // As per VP8
1785 cm
->horiz_scale
= cpi
->horiz_scale
;
1786 cm
->vert_scale
= cpi
->vert_scale
;
1788 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1789 if (cpi
->oxcf
.Sharpness
> 7)
1790 cpi
->oxcf
.Sharpness
= 7;
1792 cm
->sharpness_level
= cpi
->oxcf
.Sharpness
;
1794 if (cm
->horiz_scale
!= NORMAL
|| cm
->vert_scale
!= NORMAL
)
1796 int UNINITIALIZED_IS_SAFE(hr
), UNINITIALIZED_IS_SAFE(hs
);
1797 int UNINITIALIZED_IS_SAFE(vr
), UNINITIALIZED_IS_SAFE(vs
);
1799 Scale2Ratio(cm
->horiz_scale
, &hr
, &hs
);
1800 Scale2Ratio(cm
->vert_scale
, &vr
, &vs
);
1802 // always go to the next whole number
1803 cm
->Width
= (hs
- 1 + cpi
->oxcf
.Width
* hr
) / hs
;
1804 cm
->Height
= (vs
- 1 + cpi
->oxcf
.Height
* vr
) / vs
;
1807 if (((cm
->Width
+ 15) & 0xfffffff0) != cm
->yv12_fb
[cm
->lst_fb_idx
].y_width
||
1808 ((cm
->Height
+ 15) & 0xfffffff0) != cm
->yv12_fb
[cm
->lst_fb_idx
].y_height
||
1809 cm
->yv12_fb
[cm
->lst_fb_idx
].y_width
== 0)
1811 alloc_raw_frame_buffers(cpi
);
1812 vp8_alloc_compressor_data(cpi
);
1815 // Clamp KF frame size to quarter of data rate
1816 if (cpi
->intra_frame_target
> cpi
->target_bandwidth
>> 2)
1817 cpi
->intra_frame_target
= cpi
->target_bandwidth
>> 2;
1819 if (cpi
->oxcf
.fixed_q
>= 0)
1821 cpi
->last_q
[0] = cpi
->oxcf
.fixed_q
;
1822 cpi
->last_q
[1] = cpi
->oxcf
.fixed_q
;
1825 cpi
->Speed
= cpi
->oxcf
.cpu_used
;
1827 // force to allowlag to 0 if lag_in_frames is 0;
1828 if (cpi
->oxcf
.lag_in_frames
== 0)
1830 cpi
->oxcf
.allow_lag
= 0;
1832 // Limit on lag buffers as these are not currently dynamically allocated
1833 else if (cpi
->oxcf
.lag_in_frames
> MAX_LAG_BUFFERS
)
1834 cpi
->oxcf
.lag_in_frames
= MAX_LAG_BUFFERS
;
1837 cpi
->last_alt_ref_sei
= -1;
1838 cpi
->is_src_frame_alt_ref
= 0;
1839 cpi
->is_next_src_alt_ref
= 0;
1842 // Experimental RD Code
1843 cpi
->frame_distortion
= 0;
1844 cpi
->last_frame_distortion
= 0;
1847 #if VP8_TEMPORAL_ALT_REF
1849 cpi
->use_weighted_temporal_filter
= 0;
1854 cpi
->fixed_divide
[0] = 0;
1856 for (i
= 1; i
< 512; i
++)
1857 cpi
->fixed_divide
[i
] = 0x80000 / i
;
1863 * This function needs more clean up, i.e. be more tuned torwards
1864 * change_config rather than init_config !!!!!!!!!!!!!!!!
1869 void vp8_change_config(VP8_PTR ptr
, VP8_CONFIG
*oxcf
)
1871 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
1872 VP8_COMMON
*cm
= &cpi
->common
;
1880 if (cm
->version
!= oxcf
->Version
)
1882 cm
->version
= oxcf
->Version
;
1883 vp8_setup_version(cm
);
1888 switch (cpi
->oxcf
.Mode
)
1893 cpi
->compressor_speed
= 2;
1895 if (cpi
->oxcf
.cpu_used
< -16)
1897 cpi
->oxcf
.cpu_used
= -16;
1900 if (cpi
->oxcf
.cpu_used
> 16)
1901 cpi
->oxcf
.cpu_used
= 16;
1905 #if !(CONFIG_REALTIME_ONLY)
1906 case MODE_GOODQUALITY
:
1908 cpi
->compressor_speed
= 1;
1910 if (cpi
->oxcf
.cpu_used
< -5)
1912 cpi
->oxcf
.cpu_used
= -5;
1915 if (cpi
->oxcf
.cpu_used
> 5)
1916 cpi
->oxcf
.cpu_used
= 5;
1920 case MODE_BESTQUALITY
:
1922 cpi
->compressor_speed
= 0;
1925 case MODE_FIRSTPASS
:
1927 cpi
->compressor_speed
= 1;
1929 case MODE_SECONDPASS
:
1931 cpi
->compressor_speed
= 1;
1933 if (cpi
->oxcf
.cpu_used
< -5)
1935 cpi
->oxcf
.cpu_used
= -5;
1938 if (cpi
->oxcf
.cpu_used
> 5)
1939 cpi
->oxcf
.cpu_used
= 5;
1942 case MODE_SECONDPASS_BEST
:
1944 cpi
->compressor_speed
= 0;
1950 cpi
->auto_worst_q
= 1;
1952 cpi
->oxcf
.worst_allowed_q
= q_trans
[oxcf
->worst_allowed_q
];
1953 cpi
->oxcf
.best_allowed_q
= q_trans
[oxcf
->best_allowed_q
];
1954 cpi
->oxcf
.cq_level
= q_trans
[cpi
->oxcf
.cq_level
];
1956 if (oxcf
->fixed_q
>= 0)
1958 if (oxcf
->worst_allowed_q
< 0)
1959 cpi
->oxcf
.fixed_q
= q_trans
[0];
1961 cpi
->oxcf
.fixed_q
= q_trans
[oxcf
->worst_allowed_q
];
1963 if (oxcf
->alt_q
< 0)
1964 cpi
->oxcf
.alt_q
= q_trans
[0];
1966 cpi
->oxcf
.alt_q
= q_trans
[oxcf
->alt_q
];
1968 if (oxcf
->key_q
< 0)
1969 cpi
->oxcf
.key_q
= q_trans
[0];
1971 cpi
->oxcf
.key_q
= q_trans
[oxcf
->key_q
];
1973 if (oxcf
->gold_q
< 0)
1974 cpi
->oxcf
.gold_q
= q_trans
[0];
1976 cpi
->oxcf
.gold_q
= q_trans
[oxcf
->gold_q
];
1980 cpi
->baseline_gf_interval
= cpi
->oxcf
.alt_freq
? cpi
->oxcf
.alt_freq
: DEFAULT_GF_INTERVAL
;
1982 cpi
->ref_frame_flags
= VP8_ALT_FLAG
| VP8_GOLD_FLAG
| VP8_LAST_FLAG
;
1984 //cpi->use_golden_frame_only = 0;
1985 //cpi->use_last_frame_only = 0;
1986 cm
->refresh_golden_frame
= 0;
1987 cm
->refresh_last_frame
= 1;
1988 cm
->refresh_entropy_probs
= 1;
1990 if (cpi
->oxcf
.token_partitions
>= 0 && cpi
->oxcf
.token_partitions
<= 3)
1991 cm
->multi_token_partition
= (TOKEN_PARTITION
) cpi
->oxcf
.token_partitions
;
1993 setup_features(cpi
);
1998 for (i
= 0; i
< MAX_MB_SEGMENTS
; i
++)
1999 cpi
->segment_encode_breakout
[i
] = cpi
->oxcf
.encode_breakout
;
2002 // At the moment the first order values may not be > MAXQ
2003 if (cpi
->oxcf
.fixed_q
> MAXQ
)
2004 cpi
->oxcf
.fixed_q
= MAXQ
;
2006 // local file playback mode == really big buffer
2007 if (cpi
->oxcf
.end_usage
== USAGE_LOCAL_FILE_PLAYBACK
)
2009 cpi
->oxcf
.starting_buffer_level
= 60000;
2010 cpi
->oxcf
.optimal_buffer_level
= 60000;
2011 cpi
->oxcf
.maximum_buffer_size
= 240000;
2015 // Convert target bandwidth from Kbit/s to Bit/s
2016 cpi
->oxcf
.target_bandwidth
*= 1000;
2018 cpi
->oxcf
.starting_buffer_level
=
2019 rescale(cpi
->oxcf
.starting_buffer_level
,
2020 cpi
->oxcf
.target_bandwidth
, 1000);
2022 if (cpi
->oxcf
.optimal_buffer_level
== 0)
2023 cpi
->oxcf
.optimal_buffer_level
= cpi
->oxcf
.target_bandwidth
/ 8;
2025 cpi
->oxcf
.optimal_buffer_level
=
2026 rescale(cpi
->oxcf
.optimal_buffer_level
,
2027 cpi
->oxcf
.target_bandwidth
, 1000);
2029 if (cpi
->oxcf
.maximum_buffer_size
== 0)
2030 cpi
->oxcf
.maximum_buffer_size
= cpi
->oxcf
.target_bandwidth
/ 8;
2032 cpi
->oxcf
.maximum_buffer_size
=
2033 rescale(cpi
->oxcf
.maximum_buffer_size
,
2034 cpi
->oxcf
.target_bandwidth
, 1000);
2036 cpi
->buffer_level
= cpi
->oxcf
.starting_buffer_level
;
2037 cpi
->bits_off_target
= cpi
->oxcf
.starting_buffer_level
;
2039 vp8_new_frame_rate(cpi
, cpi
->oxcf
.frame_rate
);
2040 cpi
->worst_quality
= cpi
->oxcf
.worst_allowed_q
;
2041 cpi
->active_worst_quality
= cpi
->oxcf
.worst_allowed_q
;
2042 cpi
->avg_frame_qindex
= cpi
->oxcf
.worst_allowed_q
;
2043 cpi
->best_quality
= cpi
->oxcf
.best_allowed_q
;
2044 cpi
->active_best_quality
= cpi
->oxcf
.best_allowed_q
;
2045 cpi
->buffered_mode
= (cpi
->oxcf
.optimal_buffer_level
> 0) ? TRUE
: FALSE
;
2047 cpi
->cq_target_quality
= cpi
->oxcf
.cq_level
;
2049 cpi
->rolling_target_bits
= cpi
->av_per_frame_bandwidth
;
2050 cpi
->rolling_actual_bits
= cpi
->av_per_frame_bandwidth
;
2051 cpi
->long_rolling_target_bits
= cpi
->av_per_frame_bandwidth
;
2052 cpi
->long_rolling_actual_bits
= cpi
->av_per_frame_bandwidth
;
2054 cpi
->total_actual_bits
= 0;
2055 cpi
->total_target_vs_actual
= 0;
2057 // Only allow dropped frames in buffered mode
2058 cpi
->drop_frames_allowed
= cpi
->oxcf
.allow_df
&& cpi
->buffered_mode
;
2060 cm
->filter_type
= (LOOPFILTERTYPE
) cpi
->filter_type
;
2062 if (!cm
->use_bilinear_mc_filter
)
2063 cm
->mcomp_filter_type
= SIXTAP
;
2065 cm
->mcomp_filter_type
= BILINEAR
;
2067 cpi
->target_bandwidth
= cpi
->oxcf
.target_bandwidth
;
2069 cm
->Width
= cpi
->oxcf
.Width
;
2070 cm
->Height
= cpi
->oxcf
.Height
;
2072 cm
->horiz_scale
= cpi
->horiz_scale
;
2073 cm
->vert_scale
= cpi
->vert_scale
;
2075 cpi
->intra_frame_target
= (4 * (cm
->Width
+ cm
->Height
) / 15) * 1000; // As per VP8
2077 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
2078 if (cpi
->oxcf
.Sharpness
> 7)
2079 cpi
->oxcf
.Sharpness
= 7;
2081 cm
->sharpness_level
= cpi
->oxcf
.Sharpness
;
2083 if (cm
->horiz_scale
!= NORMAL
|| cm
->vert_scale
!= NORMAL
)
2085 int UNINITIALIZED_IS_SAFE(hr
), UNINITIALIZED_IS_SAFE(hs
);
2086 int UNINITIALIZED_IS_SAFE(vr
), UNINITIALIZED_IS_SAFE(vs
);
2088 Scale2Ratio(cm
->horiz_scale
, &hr
, &hs
);
2089 Scale2Ratio(cm
->vert_scale
, &vr
, &vs
);
2091 // always go to the next whole number
2092 cm
->Width
= (hs
- 1 + cpi
->oxcf
.Width
* hr
) / hs
;
2093 cm
->Height
= (vs
- 1 + cpi
->oxcf
.Height
* vr
) / vs
;
2096 if (((cm
->Width
+ 15) & 0xfffffff0) != cm
->yv12_fb
[cm
->lst_fb_idx
].y_width
||
2097 ((cm
->Height
+ 15) & 0xfffffff0) != cm
->yv12_fb
[cm
->lst_fb_idx
].y_height
||
2098 cm
->yv12_fb
[cm
->lst_fb_idx
].y_width
== 0)
2100 alloc_raw_frame_buffers(cpi
);
2101 vp8_alloc_compressor_data(cpi
);
2104 // Clamp KF frame size to quarter of data rate
2105 if (cpi
->intra_frame_target
> cpi
->target_bandwidth
>> 2)
2106 cpi
->intra_frame_target
= cpi
->target_bandwidth
>> 2;
2108 if (cpi
->oxcf
.fixed_q
>= 0)
2110 cpi
->last_q
[0] = cpi
->oxcf
.fixed_q
;
2111 cpi
->last_q
[1] = cpi
->oxcf
.fixed_q
;
2114 cpi
->Speed
= cpi
->oxcf
.cpu_used
;
2116 // force to allowlag to 0 if lag_in_frames is 0;
2117 if (cpi
->oxcf
.lag_in_frames
== 0)
2119 cpi
->oxcf
.allow_lag
= 0;
2121 // Limit on lag buffers as these are not currently dynamically allocated
2122 else if (cpi
->oxcf
.lag_in_frames
> MAX_LAG_BUFFERS
)
2123 cpi
->oxcf
.lag_in_frames
= MAX_LAG_BUFFERS
;
2126 cpi
->last_alt_ref_sei
= -1;
2127 cpi
->is_src_frame_alt_ref
= 0;
2128 cpi
->is_next_src_alt_ref
= 0;
2131 // Experimental RD Code
2132 cpi
->frame_distortion
= 0;
2133 cpi
->last_frame_distortion
= 0;
2138 #define M_LOG2_E 0.693147180559945309417
2139 #define log2f(x) (log (x) / (float) M_LOG2_E)
2140 static void cal_mvsadcosts(int *mvsadcost
[2])
2144 mvsadcost
[0] [0] = 300;
2145 mvsadcost
[1] [0] = 300;
2149 double z
= 256 * (2 * (log2f(2 * i
) + .6));
2150 mvsadcost
[0][i
] = (int) z
;
2151 mvsadcost
[1][i
] = (int) z
;
2152 mvsadcost
[0][-i
] = (int) z
;
2153 mvsadcost
[1][-i
] = (int) z
;
2155 while (++i
<= mv_max
);
2158 VP8_PTR
vp8_create_compressor(VP8_CONFIG
*oxcf
)
2170 cpi
= ctx
.cpi
= vpx_memalign(32, sizeof(VP8_COMP
));
2171 // Check that the CPI instance is valid
2177 vpx_memset(cpi
, 0, sizeof(VP8_COMP
));
2179 if (setjmp(cm
->error
.jmp
))
2181 VP8_PTR ptr
= ctx
.ptr
;
2183 ctx
.cpi
->common
.error
.setjmp
= 0;
2184 vp8_remove_compressor(&ptr
);
2188 cpi
->common
.error
.setjmp
= 1;
2190 CHECK_MEM_ERROR(cpi
->rdtok
, vpx_calloc(256 * 3 / 2, sizeof(TOKENEXTRA
)));
2191 CHECK_MEM_ERROR(cpi
->mb
.ss
, vpx_calloc(sizeof(search_site
), (MAX_MVSEARCH_STEPS
* 8) + 1));
2193 vp8_create_common(&cpi
->common
);
2194 vp8_cmachine_specific_config(cpi
);
2196 vp8_init_config((VP8_PTR
)cpi
, oxcf
);
2198 memcpy(cpi
->base_skip_false_prob
, vp8cx_base_skip_false_prob
, sizeof(vp8cx_base_skip_false_prob
));
2199 cpi
->common
.current_video_frame
= 0;
2200 cpi
->kf_overspend_bits
= 0;
2201 cpi
->kf_bitrate_adjustment
= 0;
2202 cpi
->frames_till_gf_update_due
= 0;
2203 cpi
->gf_overspend_bits
= 0;
2204 cpi
->non_gf_bitrate_adjustment
= 0;
2205 cpi
->prob_last_coded
= 128;
2206 cpi
->prob_gf_coded
= 128;
2207 cpi
->prob_intra_coded
= 63;
2209 // Prime the recent reference frame useage counters.
2210 // Hereafter they will be maintained as a sort of moving average
2211 cpi
->recent_ref_frame_usage
[INTRA_FRAME
] = 1;
2212 cpi
->recent_ref_frame_usage
[LAST_FRAME
] = 1;
2213 cpi
->recent_ref_frame_usage
[GOLDEN_FRAME
] = 1;
2214 cpi
->recent_ref_frame_usage
[ALTREF_FRAME
] = 1;
2216 // Set reference frame sign bias for ALTREF frame to 1 (for now)
2217 cpi
->common
.ref_frame_sign_bias
[ALTREF_FRAME
] = 1;
2219 cpi
->gf_decay_rate
= 0;
2220 cpi
->baseline_gf_interval
= DEFAULT_GF_INTERVAL
;
2222 cpi
->gold_is_last
= 0 ;
2223 cpi
->alt_is_last
= 0 ;
2224 cpi
->gold_is_alt
= 0 ;
2226 // allocate memory for storing last frame's MVs for MV prediction.
2227 CHECK_MEM_ERROR(cpi
->lfmv
, vpx_calloc((cpi
->common
.mb_rows
+1) * (cpi
->common
.mb_cols
+1), sizeof(int_mv
)));
2228 CHECK_MEM_ERROR(cpi
->lf_ref_frame_sign_bias
, vpx_calloc((cpi
->common
.mb_rows
+1) * (cpi
->common
.mb_cols
+1), sizeof(int)));
2229 CHECK_MEM_ERROR(cpi
->lf_ref_frame
, vpx_calloc((cpi
->common
.mb_rows
+1) * (cpi
->common
.mb_cols
+1), sizeof(int)));
2231 // Create the encoder segmentation map and set all entries to 0
2232 CHECK_MEM_ERROR(cpi
->segmentation_map
, vpx_calloc(cpi
->common
.mb_rows
* cpi
->common
.mb_cols
, 1));
2233 CHECK_MEM_ERROR(cpi
->active_map
, vpx_calloc(cpi
->common
.mb_rows
* cpi
->common
.mb_cols
, 1));
2234 vpx_memset(cpi
->active_map
, 1, (cpi
->common
.mb_rows
* cpi
->common
.mb_cols
));
2235 cpi
->active_map_enabled
= 0;
2237 // Create the first pass motion map structure and set to 0
2238 // Allocate space for maximum of 15 buffers
2239 CHECK_MEM_ERROR(cpi
->fp_motion_map
, vpx_calloc(15*cpi
->common
.MBs
, 1));
2242 // Experimental code for lagged and one pass
2243 // Initialise one_pass GF frames stats
2244 // Update stats used for GF selection
2247 cpi
->one_pass_frame_index
= 0;
2249 for (i
= 0; i
< MAX_LAG_BUFFERS
; i
++)
2251 cpi
->one_pass_frame_stats
[i
].frames_so_far
= 0;
2252 cpi
->one_pass_frame_stats
[i
].frame_intra_error
= 0.0;
2253 cpi
->one_pass_frame_stats
[i
].frame_coded_error
= 0.0;
2254 cpi
->one_pass_frame_stats
[i
].frame_pcnt_inter
= 0.0;
2255 cpi
->one_pass_frame_stats
[i
].frame_pcnt_motion
= 0.0;
2256 cpi
->one_pass_frame_stats
[i
].frame_mvr
= 0.0;
2257 cpi
->one_pass_frame_stats
[i
].frame_mvr_abs
= 0.0;
2258 cpi
->one_pass_frame_stats
[i
].frame_mvc
= 0.0;
2259 cpi
->one_pass_frame_stats
[i
].frame_mvc_abs
= 0.0;
2264 // Should we use the cyclic refresh method.
2265 // Currently this is tied to error resilliant mode
2266 cpi
->cyclic_refresh_mode_enabled
= cpi
->oxcf
.error_resilient_mode
;
2267 cpi
->cyclic_refresh_mode_max_mbs_perframe
= (cpi
->common
.mb_rows
* cpi
->common
.mb_cols
) / 40;
2268 cpi
->cyclic_refresh_mode_index
= 0;
2269 cpi
->cyclic_refresh_q
= 32;
2271 if (cpi
->cyclic_refresh_mode_enabled
)
2273 CHECK_MEM_ERROR(cpi
->cyclic_refresh_map
, vpx_calloc((cpi
->common
.mb_rows
* cpi
->common
.mb_cols
), 1));
2276 cpi
->cyclic_refresh_map
= (signed char *) NULL
;
2278 // Test function for segmentation
2279 //segmentation_test_function((VP8_PTR) cpi);
2281 #ifdef ENTROPY_STATS
2282 init_context_counters();
2285 /*Initialize the feed-forward activity masking.*/
2286 cpi
->activity_avg
= 90<<12;
2288 cpi
->frames_since_key
= 8; // Give a sensible default for the first frame.
2289 cpi
->key_frame_frequency
= cpi
->oxcf
.key_freq
;
2290 cpi
->this_key_frame_forced
= FALSE
;
2291 cpi
->next_key_frame_forced
= FALSE
;
2293 cpi
->source_alt_ref_pending
= FALSE
;
2294 cpi
->source_alt_ref_active
= FALSE
;
2295 cpi
->common
.refresh_alt_ref_frame
= 0;
2297 cpi
->b_calculate_psnr
= CONFIG_PSNR
;
2299 cpi
->b_calculate_ssimg
= 0;
2304 if (cpi
->b_calculate_psnr
)
2306 cpi
->total_sq_error
= 0.0;
2307 cpi
->total_sq_error2
= 0.0;
2312 cpi
->totalp_y
= 0.0;
2313 cpi
->totalp_u
= 0.0;
2314 cpi
->totalp_v
= 0.0;
2316 cpi
->tot_recode_hits
= 0;
2317 cpi
->summed_quality
= 0;
2318 cpi
->summed_weights
= 0;
2321 if (cpi
->b_calculate_ssimg
)
2323 cpi
->total_ssimg_y
= 0;
2324 cpi
->total_ssimg_u
= 0;
2325 cpi
->total_ssimg_v
= 0;
2326 cpi
->total_ssimg_all
= 0;
2330 #define LLONG_MAX 9223372036854775807LL
2332 cpi
->first_time_stamp_ever
= LLONG_MAX
;
2336 cpi
->frames_till_gf_update_due
= 0;
2337 cpi
->key_frame_count
= 1;
2338 cpi
->tot_key_frame_bits
= 0;
2340 cpi
->ni_av_qi
= cpi
->oxcf
.worst_allowed_q
;
2343 cpi
->total_byte_count
= 0;
2345 cpi
->drop_frame
= 0;
2346 cpi
->drop_count
= 0;
2347 cpi
->max_drop_count
= 0;
2348 cpi
->max_consec_dropped_frames
= 4;
2350 cpi
->rate_correction_factor
= 1.0;
2351 cpi
->key_frame_rate_correction_factor
= 1.0;
2352 cpi
->gf_rate_correction_factor
= 1.0;
2353 cpi
->est_max_qcorrection_factor
= 1.0;
2355 cpi
->mb
.mvcost
[0] = &cpi
->mb
.mvcosts
[0][mv_max
+1];
2356 cpi
->mb
.mvcost
[1] = &cpi
->mb
.mvcosts
[1][mv_max
+1];
2357 cpi
->mb
.mvsadcost
[0] = &cpi
->mb
.mvsadcosts
[0][mv_max
+1];
2358 cpi
->mb
.mvsadcost
[1] = &cpi
->mb
.mvsadcosts
[1][mv_max
+1];
2360 cal_mvsadcosts(cpi
->mb
.mvsadcost
);
2362 for (i
= 0; i
< KEY_FRAME_CONTEXT
; i
++)
2364 cpi
->prior_key_frame_size
[i
] = cpi
->intra_frame_target
;
2365 cpi
->prior_key_frame_distance
[i
] = (int)cpi
->output_frame_rate
;
2368 cpi
->check_freq
[0] = 15;
2369 cpi
->check_freq
[1] = 15;
2371 #ifdef OUTPUT_YUV_SRC
2372 yuv_file
= fopen("bd.yuv", "ab");
2376 framepsnr
= fopen("framepsnr.stt", "a");
2377 kf_list
= fopen("kf_list.stt", "w");
2380 cpi
->output_pkt_list
= oxcf
->output_pkt_list
;
2382 #if !(CONFIG_REALTIME_ONLY)
2386 vp8_init_first_pass(cpi
);
2388 else if (cpi
->pass
== 2)
2390 size_t packet_sz
= vp8_firstpass_stats_sz(cpi
->common
.MBs
);
2391 int packets
= oxcf
->two_pass_stats_in
.sz
/ packet_sz
;
2393 cpi
->stats_in
= oxcf
->two_pass_stats_in
.buf
;
2394 cpi
->stats_in_end
= (void*)((char *)cpi
->stats_in
2395 + (packets
- 1) * packet_sz
);
2396 vp8_init_second_pass(cpi
);
2401 if (cpi
->compressor_speed
== 2)
2403 cpi
->cpu_freq
= 0; //vp8_get_processor_freq();
2404 cpi
->avg_encode_time
= 0;
2405 cpi
->avg_pick_mode_time
= 0;
2408 vp8_set_speed_features(cpi
);
2410 // Set starting values of RD threshold multipliers (128 = *1)
2411 for (i
= 0; i
< MAX_MODES
; i
++)
2413 cpi
->rd_thresh_mult
[i
] = 128;
2416 #ifdef ENTROPY_STATS
2417 init_mv_ref_counts();
2420 vp8cx_create_encoder_threads(cpi
);
2422 cpi
->fn_ptr
[BLOCK_16X16
].sdf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x16
);
2423 cpi
->fn_ptr
[BLOCK_16X16
].vf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, var16x16
);
2424 cpi
->fn_ptr
[BLOCK_16X16
].svf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, subpixvar16x16
);
2425 cpi
->fn_ptr
[BLOCK_16X16
].svf_halfpix_h
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, halfpixvar16x16_h
);
2426 cpi
->fn_ptr
[BLOCK_16X16
].svf_halfpix_v
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, halfpixvar16x16_v
);
2427 cpi
->fn_ptr
[BLOCK_16X16
].svf_halfpix_hv
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, halfpixvar16x16_hv
);
2428 cpi
->fn_ptr
[BLOCK_16X16
].sdx3f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x16x3
);
2429 cpi
->fn_ptr
[BLOCK_16X16
].sdx8f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x16x8
);
2430 cpi
->fn_ptr
[BLOCK_16X16
].sdx4df
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x16x4d
);
2432 cpi
->fn_ptr
[BLOCK_16X8
].sdf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x8
);
2433 cpi
->fn_ptr
[BLOCK_16X8
].vf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, var16x8
);
2434 cpi
->fn_ptr
[BLOCK_16X8
].svf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, subpixvar16x8
);
2435 cpi
->fn_ptr
[BLOCK_16X8
].svf_halfpix_h
= NULL
;
2436 cpi
->fn_ptr
[BLOCK_16X8
].svf_halfpix_v
= NULL
;
2437 cpi
->fn_ptr
[BLOCK_16X8
].svf_halfpix_hv
= NULL
;
2438 cpi
->fn_ptr
[BLOCK_16X8
].sdx3f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x8x3
);
2439 cpi
->fn_ptr
[BLOCK_16X8
].sdx8f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x8x8
);
2440 cpi
->fn_ptr
[BLOCK_16X8
].sdx4df
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad16x8x4d
);
2442 cpi
->fn_ptr
[BLOCK_8X16
].sdf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad8x16
);
2443 cpi
->fn_ptr
[BLOCK_8X16
].vf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, var8x16
);
2444 cpi
->fn_ptr
[BLOCK_8X16
].svf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, subpixvar8x16
);
2445 cpi
->fn_ptr
[BLOCK_8X16
].svf_halfpix_h
= NULL
;
2446 cpi
->fn_ptr
[BLOCK_8X16
].svf_halfpix_v
= NULL
;
2447 cpi
->fn_ptr
[BLOCK_8X16
].svf_halfpix_hv
= NULL
;
2448 cpi
->fn_ptr
[BLOCK_8X16
].sdx3f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad8x16x3
);
2449 cpi
->fn_ptr
[BLOCK_8X16
].sdx8f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad8x16x8
);
2450 cpi
->fn_ptr
[BLOCK_8X16
].sdx4df
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad8x16x4d
);
2452 cpi
->fn_ptr
[BLOCK_8X8
].sdf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad8x8
);
2453 cpi
->fn_ptr
[BLOCK_8X8
].vf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, var8x8
);
2454 cpi
->fn_ptr
[BLOCK_8X8
].svf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, subpixvar8x8
);
2455 cpi
->fn_ptr
[BLOCK_8X8
].svf_halfpix_h
= NULL
;
2456 cpi
->fn_ptr
[BLOCK_8X8
].svf_halfpix_v
= NULL
;
2457 cpi
->fn_ptr
[BLOCK_8X8
].svf_halfpix_hv
= NULL
;
2458 cpi
->fn_ptr
[BLOCK_8X8
].sdx3f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad8x8x3
);
2459 cpi
->fn_ptr
[BLOCK_8X8
].sdx8f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad8x8x8
);
2460 cpi
->fn_ptr
[BLOCK_8X8
].sdx4df
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad8x8x4d
);
2462 cpi
->fn_ptr
[BLOCK_4X4
].sdf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad4x4
);
2463 cpi
->fn_ptr
[BLOCK_4X4
].vf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, var4x4
);
2464 cpi
->fn_ptr
[BLOCK_4X4
].svf
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, subpixvar4x4
);
2465 cpi
->fn_ptr
[BLOCK_4X4
].svf_halfpix_h
= NULL
;
2466 cpi
->fn_ptr
[BLOCK_4X4
].svf_halfpix_v
= NULL
;
2467 cpi
->fn_ptr
[BLOCK_4X4
].svf_halfpix_hv
= NULL
;
2468 cpi
->fn_ptr
[BLOCK_4X4
].sdx3f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad4x4x3
);
2469 cpi
->fn_ptr
[BLOCK_4X4
].sdx8f
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad4x4x8
);
2470 cpi
->fn_ptr
[BLOCK_4X4
].sdx4df
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, sad4x4x4d
);
2472 #if !(CONFIG_REALTIME_ONLY)
2473 cpi
->full_search_sad
= SEARCH_INVOKE(&cpi
->rtcd
.search
, full_search
);
2475 cpi
->diamond_search_sad
= SEARCH_INVOKE(&cpi
->rtcd
.search
, diamond_search
);
2477 cpi
->ready_for_new_frame
= 1;
2479 cpi
->source_encode_index
= 0;
2481 // make sure frame 1 is okay
2482 cpi
->error_bins
[0] = cpi
->common
.MBs
;
2484 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2485 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2486 vp8cx_init_quantizer(cpi
);
2488 vp8_init_loop_filter(cm
);
2489 cm
->last_frame_type
= KEY_FRAME
;
2490 cm
->last_filter_type
= cm
->filter_type
;
2491 cm
->last_sharpness_level
= cm
->sharpness_level
;
2493 cpi
->common
.error
.setjmp
= 0;
2494 return (VP8_PTR
) cpi
;
2499 void vp8_remove_compressor(VP8_PTR
*ptr
)
2501 VP8_COMP
*cpi
= (VP8_COMP
*)(*ptr
);
2506 if (cpi
&& (cpi
->common
.current_video_frame
> 0))
2508 #if !(CONFIG_REALTIME_ONLY)
2512 vp8_end_second_pass(cpi
);
2517 #ifdef ENTROPY_STATS
2518 print_context_counters();
2519 print_tree_update_probs();
2520 print_mode_context();
2527 FILE *f
= fopen("opsnr.stt", "a");
2528 double time_encoded
= (cpi
->source_end_time_stamp
- cpi
->first_time_stamp_ever
) / 10000000.000;
2529 double total_encode_time
= (cpi
->time_receive_data
+ cpi
->time_compress_data
) / 1000.000;
2530 double dr
= (double)cpi
->bytes
* (double) 8 / (double)1000 / time_encoded
;
2532 if (cpi
->b_calculate_psnr
)
2534 YV12_BUFFER_CONFIG
*lst_yv12
= &cpi
->common
.yv12_fb
[cpi
->common
.lst_fb_idx
];
2535 double samples
= 3.0 / 2 * cpi
->count
* lst_yv12
->y_width
* lst_yv12
->y_height
;
2536 double total_psnr
= vp8_mse2psnr(samples
, 255.0, cpi
->total_sq_error
);
2537 double total_psnr2
= vp8_mse2psnr(samples
, 255.0, cpi
->total_sq_error2
);
2538 double total_ssim
= 100 * pow(cpi
->summed_quality
/ cpi
->summed_weights
, 8.0);
2540 fprintf(f
, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time(us)\n");
2541 fprintf(f
, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f %8.0f\n",
2542 dr
, cpi
->total
/ cpi
->count
, total_psnr
, cpi
->totalp
/ cpi
->count
, total_psnr2
, total_ssim
,
2546 if (cpi
->b_calculate_ssimg
)
2548 fprintf(f
, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(us)\n");
2549 fprintf(f
, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr
,
2550 cpi
->total_ssimg_y
/ cpi
->count
, cpi
->total_ssimg_u
/ cpi
->count
,
2551 cpi
->total_ssimg_v
/ cpi
->count
, cpi
->total_ssimg_all
/ cpi
->count
, total_encode_time
);
2556 f
= fopen("qskip.stt", "a");
2557 fprintf(f
, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi
->oxcf
.best_allowed_q
, cpi
->oxcf
.worst_allowed_q
, skiptruecount
, skipfalsecount
);
2568 if (cpi
->compressor_speed
== 2)
2571 FILE *f
= fopen("cxspeed.stt", "a");
2572 cnt_pm
/= cpi
->common
.MBs
;
2574 for (i
= 0; i
< 16; i
++)
2575 fprintf(f
, "%5d", frames_at_speed
[i
]);
2578 //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);
2587 extern int count_mb_seg
[4];
2588 FILE *f
= fopen("modes.stt", "a");
2589 double dr
= (double)cpi
->oxcf
.frame_rate
* (double)bytes
* (double)8 / (double)count
/ (double)1000 ;
2590 fprintf(f
, "intra_mode in Intra Frames:\n");
2591 fprintf(f
, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes
[0], y_modes
[1], y_modes
[2], y_modes
[3], y_modes
[4]);
2592 fprintf(f
, "UV:%8d, %8d, %8d, %8d\n", uv_modes
[0], uv_modes
[1], uv_modes
[2], uv_modes
[3]);
2597 for (i
= 0; i
< 10; i
++)
2598 fprintf(f
, "%8d, ", b_modes
[i
]);
2604 fprintf(f
, "Modes in Inter Frames:\n");
2605 fprintf(f
, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2606 inter_y_modes
[0], inter_y_modes
[1], inter_y_modes
[2], inter_y_modes
[3], inter_y_modes
[4],
2607 inter_y_modes
[5], inter_y_modes
[6], inter_y_modes
[7], inter_y_modes
[8], inter_y_modes
[9]);
2608 fprintf(f
, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes
[0], inter_uv_modes
[1], inter_uv_modes
[2], inter_uv_modes
[3]);
2613 for (i
= 0; i
< 15; i
++)
2614 fprintf(f
, "%8d, ", inter_b_modes
[i
]);
2619 fprintf(f
, "P:%8d, %8d, %8d, %8d\n", count_mb_seg
[0], count_mb_seg
[1], count_mb_seg
[2], count_mb_seg
[3]);
2620 fprintf(f
, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes
[LEFT4X4
], inter_b_modes
[ABOVE4X4
], inter_b_modes
[ZERO4X4
], inter_b_modes
[NEW4X4
]);
2628 #ifdef ENTROPY_STATS
2631 FILE *fmode
= fopen("modecontext.c", "w");
2633 fprintf(fmode
, "\n#include \"entropymode.h\"\n\n");
2634 fprintf(fmode
, "const unsigned int vp8_kf_default_bmode_counts ");
2635 fprintf(fmode
, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2637 for (i
= 0; i
< 10; i
++)
2640 fprintf(fmode
, " { //Above Mode : %d\n", i
);
2642 for (j
= 0; j
< 10; j
++)
2645 fprintf(fmode
, " {");
2647 for (k
= 0; k
< 10; k
++)
2649 if (!intra_mode_stats
[i
][j
][k
])
2650 fprintf(fmode
, " %5d, ", 1);
2652 fprintf(fmode
, " %5d, ", intra_mode_stats
[i
][j
][k
]);
2655 fprintf(fmode
, "}, // left_mode %d\n", j
);
2659 fprintf(fmode
, " },\n");
2663 fprintf(fmode
, "};\n");
2669 #if defined(SECTIONBITS_OUTPUT)
2674 FILE *f
= fopen("tokenbits.stt", "a");
2676 for (i
= 0; i
< 28; i
++)
2677 fprintf(f
, "%8d", (int)(Sectionbits
[i
] / 256));
2687 printf("\n_pick_loop_filter_level:%d\n", cpi
->time_pick_lpf
/ 1000);
2688 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2689 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);
2695 vp8cx_remove_encoder_threads(cpi
);
2697 vp8_dealloc_compressor_data(cpi
);
2698 vpx_free(cpi
->mb
.ss
);
2700 vpx_free(cpi
->rdtok
);
2701 vpx_free(cpi
->cyclic_refresh_map
);
2703 vp8_remove_common(&cpi
->common
);
2707 #ifdef OUTPUT_YUV_SRC
2727 static uint64_t calc_plane_error(unsigned char *orig
, int orig_stride
,
2728 unsigned char *recon
, int recon_stride
,
2729 unsigned int cols
, unsigned int rows
,
2730 vp8_variance_rtcd_vtable_t
*rtcd
)
2732 unsigned int row
, col
;
2733 uint64_t total_sse
= 0;
2736 for (row
= 0; row
+ 16 <= rows
; row
+= 16)
2738 for (col
= 0; col
+ 16 <= cols
; col
+= 16)
2742 VARIANCE_INVOKE(rtcd
, mse16x16
)(orig
+ col
, orig_stride
,
2743 recon
+ col
, recon_stride
,
2748 /* Handle odd-sized width */
2751 unsigned int border_row
, border_col
;
2752 unsigned char *border_orig
= orig
;
2753 unsigned char *border_recon
= recon
;
2755 for (border_row
= 0; border_row
< 16; border_row
++)
2757 for (border_col
= col
; border_col
< cols
; border_col
++)
2759 diff
= border_orig
[border_col
] - border_recon
[border_col
];
2760 total_sse
+= diff
* diff
;
2763 border_orig
+= orig_stride
;
2764 border_recon
+= recon_stride
;
2768 orig
+= orig_stride
* 16;
2769 recon
+= recon_stride
* 16;
2772 /* Handle odd-sized height */
2773 for (; row
< rows
; row
++)
2775 for (col
= 0; col
< cols
; col
++)
2777 diff
= orig
[col
] - recon
[col
];
2778 total_sse
+= diff
* diff
;
2781 orig
+= orig_stride
;
2782 recon
+= recon_stride
;
2789 static void generate_psnr_packet(VP8_COMP
*cpi
)
2791 YV12_BUFFER_CONFIG
*orig
= cpi
->Source
;
2792 YV12_BUFFER_CONFIG
*recon
= cpi
->common
.frame_to_show
;
2793 struct vpx_codec_cx_pkt pkt
;
2796 unsigned int width
= cpi
->common
.Width
;
2797 unsigned int height
= cpi
->common
.Height
;
2799 pkt
.kind
= VPX_CODEC_PSNR_PKT
;
2800 sse
= calc_plane_error(orig
->y_buffer
, orig
->y_stride
,
2801 recon
->y_buffer
, recon
->y_stride
,
2803 IF_RTCD(&cpi
->rtcd
.variance
));
2804 pkt
.data
.psnr
.sse
[0] = sse
;
2805 pkt
.data
.psnr
.sse
[1] = sse
;
2806 pkt
.data
.psnr
.samples
[0] = width
* height
;
2807 pkt
.data
.psnr
.samples
[1] = width
* height
;
2809 width
= (width
+ 1) / 2;
2810 height
= (height
+ 1) / 2;
2812 sse
= calc_plane_error(orig
->u_buffer
, orig
->uv_stride
,
2813 recon
->u_buffer
, recon
->uv_stride
,
2815 IF_RTCD(&cpi
->rtcd
.variance
));
2816 pkt
.data
.psnr
.sse
[0] += sse
;
2817 pkt
.data
.psnr
.sse
[2] = sse
;
2818 pkt
.data
.psnr
.samples
[0] += width
* height
;
2819 pkt
.data
.psnr
.samples
[2] = width
* height
;
2821 sse
= calc_plane_error(orig
->v_buffer
, orig
->uv_stride
,
2822 recon
->v_buffer
, recon
->uv_stride
,
2824 IF_RTCD(&cpi
->rtcd
.variance
));
2825 pkt
.data
.psnr
.sse
[0] += sse
;
2826 pkt
.data
.psnr
.sse
[3] = sse
;
2827 pkt
.data
.psnr
.samples
[0] += width
* height
;
2828 pkt
.data
.psnr
.samples
[3] = width
* height
;
2830 for (i
= 0; i
< 4; i
++)
2831 pkt
.data
.psnr
.psnr
[i
] = vp8_mse2psnr(pkt
.data
.psnr
.samples
[i
], 255.0,
2832 pkt
.data
.psnr
.sse
[i
]);
2834 vpx_codec_pkt_list_add(cpi
->output_pkt_list
, &pkt
);
2838 int vp8_use_as_reference(VP8_PTR ptr
, int ref_frame_flags
)
2840 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
2842 if (ref_frame_flags
> 7)
2845 cpi
->ref_frame_flags
= ref_frame_flags
;
2848 int vp8_update_reference(VP8_PTR ptr
, int ref_frame_flags
)
2850 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
2852 if (ref_frame_flags
> 7)
2855 cpi
->common
.refresh_golden_frame
= 0;
2856 cpi
->common
.refresh_alt_ref_frame
= 0;
2857 cpi
->common
.refresh_last_frame
= 0;
2859 if (ref_frame_flags
& VP8_LAST_FLAG
)
2860 cpi
->common
.refresh_last_frame
= 1;
2862 if (ref_frame_flags
& VP8_GOLD_FLAG
)
2863 cpi
->common
.refresh_golden_frame
= 1;
2865 if (ref_frame_flags
& VP8_ALT_FLAG
)
2866 cpi
->common
.refresh_alt_ref_frame
= 1;
2871 int vp8_get_reference(VP8_PTR ptr
, VP8_REFFRAME ref_frame_flag
, YV12_BUFFER_CONFIG
*sd
)
2873 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
2874 VP8_COMMON
*cm
= &cpi
->common
;
2877 if (ref_frame_flag
== VP8_LAST_FLAG
)
2878 ref_fb_idx
= cm
->lst_fb_idx
;
2879 else if (ref_frame_flag
== VP8_GOLD_FLAG
)
2880 ref_fb_idx
= cm
->gld_fb_idx
;
2881 else if (ref_frame_flag
== VP8_ALT_FLAG
)
2882 ref_fb_idx
= cm
->alt_fb_idx
;
2886 vp8_yv12_copy_frame_ptr(&cm
->yv12_fb
[ref_fb_idx
], sd
);
2890 int vp8_set_reference(VP8_PTR ptr
, VP8_REFFRAME ref_frame_flag
, YV12_BUFFER_CONFIG
*sd
)
2892 VP8_COMP
*cpi
= (VP8_COMP
*)(ptr
);
2893 VP8_COMMON
*cm
= &cpi
->common
;
2897 if (ref_frame_flag
== VP8_LAST_FLAG
)
2898 ref_fb_idx
= cm
->lst_fb_idx
;
2899 else if (ref_frame_flag
== VP8_GOLD_FLAG
)
2900 ref_fb_idx
= cm
->gld_fb_idx
;
2901 else if (ref_frame_flag
== VP8_ALT_FLAG
)
2902 ref_fb_idx
= cm
->alt_fb_idx
;
2906 vp8_yv12_copy_frame_ptr(sd
, &cm
->yv12_fb
[ref_fb_idx
]);
2910 int vp8_update_entropy(VP8_PTR comp
, int update
)
2912 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
2913 VP8_COMMON
*cm
= &cpi
->common
;
2914 cm
->refresh_entropy_probs
= update
;
2921 void vp8_write_yuv_frame(const char *name
, YV12_BUFFER_CONFIG
*s
)
2923 FILE *yuv_file
= fopen(name
, "ab");
2924 unsigned char *src
= s
->y_buffer
;
2925 int h
= s
->y_height
;
2929 fwrite(src
, s
->y_width
, 1, yuv_file
);
2939 fwrite(src
, s
->uv_width
, 1, yuv_file
);
2940 src
+= s
->uv_stride
;
2949 fwrite(src
, s
->uv_width
, 1, yuv_file
);
2950 src
+= s
->uv_stride
;
2959 static void scale_and_extend_source(YV12_BUFFER_CONFIG
*sd
, VP8_COMP
*cpi
)
2961 VP8_COMMON
*cm
= &cpi
->common
;
2963 // are we resizing the image
2964 if (cm
->horiz_scale
!= 0 || cm
->vert_scale
!= 0)
2966 #if CONFIG_SPATIAL_RESAMPLING
2967 int UNINITIALIZED_IS_SAFE(hr
), UNINITIALIZED_IS_SAFE(hs
);
2968 int UNINITIALIZED_IS_SAFE(vr
), UNINITIALIZED_IS_SAFE(vs
);
2971 if (cm
->vert_scale
== 3)
2976 Scale2Ratio(cm
->horiz_scale
, &hr
, &hs
);
2977 Scale2Ratio(cm
->vert_scale
, &vr
, &vs
);
2979 vp8_scale_frame(sd
, &cpi
->scaled_source
, cm
->temp_scale_frame
.y_buffer
,
2980 tmp_height
, hs
, hr
, vs
, vr
, 0);
2982 cpi
->Source
= &cpi
->scaled_source
;
2985 // we may need to copy to a buffer so we can extend the image...
2986 else if (cm
->Width
!= cm
->yv12_fb
[cm
->lst_fb_idx
].y_width
||
2987 cm
->Height
!= cm
->yv12_fb
[cm
->lst_fb_idx
].y_height
)
2989 //vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2991 #if CONFIG_RUNTIME_CPU_DETECT
2992 if (cm
->rtcd
.flags
& HAS_NEON
)
2995 vp8_yv12_copy_src_frame_func_neon(sd
, &cpi
->scaled_source
);
2997 #if CONFIG_RUNTIME_CPU_DETECT
3001 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
3003 vp8_yv12_copy_frame_ptr(sd
, &cpi
->scaled_source
);
3007 cpi
->Source
= &cpi
->scaled_source
;
3010 vp8_extend_to_multiple_of16(cpi
->Source
, cm
->Width
, cm
->Height
);
3013 static void resize_key_frame(VP8_COMP
*cpi
)
3015 #if CONFIG_SPATIAL_RESAMPLING
3016 VP8_COMMON
*cm
= &cpi
->common
;
3018 // Do we need to apply resampling for one pass cbr.
3019 // In one pass this is more limited than in two pass cbr
3020 // The test and any change is only made one per key frame sequence
3021 if (cpi
->oxcf
.allow_spatial_resampling
&& (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
))
3023 int UNINITIALIZED_IS_SAFE(hr
), UNINITIALIZED_IS_SAFE(hs
);
3024 int UNINITIALIZED_IS_SAFE(vr
), UNINITIALIZED_IS_SAFE(vs
);
3025 int new_width
, new_height
;
3027 // If we are below the resample DOWN watermark then scale down a notch.
3028 if (cpi
->buffer_level
< (cpi
->oxcf
.resample_down_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100))
3030 cm
->horiz_scale
= (cm
->horiz_scale
< ONETWO
) ? cm
->horiz_scale
+ 1 : ONETWO
;
3031 cm
->vert_scale
= (cm
->vert_scale
< ONETWO
) ? cm
->vert_scale
+ 1 : ONETWO
;
3033 // Should we now start scaling back up
3034 else if (cpi
->buffer_level
> (cpi
->oxcf
.resample_up_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100))
3036 cm
->horiz_scale
= (cm
->horiz_scale
> NORMAL
) ? cm
->horiz_scale
- 1 : NORMAL
;
3037 cm
->vert_scale
= (cm
->vert_scale
> NORMAL
) ? cm
->vert_scale
- 1 : NORMAL
;
3040 // Get the new hieght and width
3041 Scale2Ratio(cm
->horiz_scale
, &hr
, &hs
);
3042 Scale2Ratio(cm
->vert_scale
, &vr
, &vs
);
3043 new_width
= ((hs
- 1) + (cpi
->oxcf
.Width
* hr
)) / hs
;
3044 new_height
= ((vs
- 1) + (cpi
->oxcf
.Height
* vr
)) / vs
;
3046 // If the image size has changed we need to reallocate the buffers
3047 // and resample the source image
3048 if ((cm
->Width
!= new_width
) || (cm
->Height
!= new_height
))
3050 cm
->Width
= new_width
;
3051 cm
->Height
= new_height
;
3052 vp8_alloc_compressor_data(cpi
);
3053 scale_and_extend_source(cpi
->un_scaled_source
, cpi
);
3059 // return of 0 means drop frame
3060 static int pick_frame_size(VP8_COMP
*cpi
)
3062 VP8_COMMON
*cm
= &cpi
->common
;
3064 // First Frame is a special case
3065 if (cm
->current_video_frame
== 0)
3067 #if !(CONFIG_REALTIME_ONLY)
3070 vp8_calc_auto_iframe_target_size(cpi
);
3072 // 1 Pass there is no information on which to base size so use bandwidth per second * fixed fraction
3075 cpi
->this_frame_target
= cpi
->oxcf
.target_bandwidth
/ 2;
3077 // in error resilient mode the first frame is bigger since it likely contains
3078 // all the static background
3079 if (cpi
->oxcf
.error_resilient_mode
== 1 || (cpi
->compressor_speed
== 2))
3081 cpi
->this_frame_target
*= 3; // 5;
3084 // Key frame from VFW/auto-keyframe/first frame
3085 cm
->frame_type
= KEY_FRAME
;
3088 // Special case for forced key frames
3089 // The frame sizing here is still far from ideal for 2 pass.
3090 else if (cm
->frame_flags
& FRAMEFLAGS_KEY
)
3092 cm
->frame_type
= KEY_FRAME
;
3093 resize_key_frame(cpi
);
3094 vp8_calc_iframe_target_size(cpi
);
3096 else if (cm
->frame_type
== KEY_FRAME
)
3098 vp8_calc_auto_iframe_target_size(cpi
);
3102 // INTER frame: compute target frame size
3103 cm
->frame_type
= INTER_FRAME
;
3104 vp8_calc_pframe_target_size(cpi
);
3106 // Check if we're dropping the frame:
3107 if (cpi
->drop_frame
)
3109 cpi
->drop_frame
= FALSE
;
3117 static void set_quantizer(VP8_COMP
*cpi
, int Q
)
3119 VP8_COMMON
*cm
= &cpi
->common
;
3120 MACROBLOCKD
*mbd
= &cpi
->mb
.e_mbd
;
3122 cm
->base_qindex
= Q
;
3124 cm
->y1dc_delta_q
= 0;
3125 cm
->y2dc_delta_q
= 0;
3126 cm
->y2ac_delta_q
= 0;
3127 cm
->uvdc_delta_q
= 0;
3128 cm
->uvac_delta_q
= 0;
3130 // Set Segment specific quatizers
3131 mbd
->segment_feature_data
[MB_LVL_ALT_Q
][0] = cpi
->segment_feature_data
[MB_LVL_ALT_Q
][0];
3132 mbd
->segment_feature_data
[MB_LVL_ALT_Q
][1] = cpi
->segment_feature_data
[MB_LVL_ALT_Q
][1];
3133 mbd
->segment_feature_data
[MB_LVL_ALT_Q
][2] = cpi
->segment_feature_data
[MB_LVL_ALT_Q
][2];
3134 mbd
->segment_feature_data
[MB_LVL_ALT_Q
][3] = cpi
->segment_feature_data
[MB_LVL_ALT_Q
][3];
3137 static void update_alt_ref_frame_and_stats(VP8_COMP
*cpi
)
3139 VP8_COMMON
*cm
= &cpi
->common
;
3141 // Update the golden frame buffer
3142 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->yv12_fb
[cm
->alt_fb_idx
]);
3144 // Select an interval before next GF or altref
3145 if (!cpi
->auto_gold
)
3146 cpi
->frames_till_gf_update_due
= cpi
->goldfreq
;
3148 if ((cpi
->pass
!= 2) && cpi
->frames_till_gf_update_due
)
3150 cpi
->current_gf_interval
= cpi
->frames_till_gf_update_due
;
3152 // Set the bits per frame that we should try and recover in subsequent inter frames
3153 // to account for the extra GF spend... note that his does not apply for GF updates
3154 // that occur coincident with a key frame as the extra cost of key frames is dealt
3157 cpi
->gf_overspend_bits
+= cpi
->projected_frame_size
;
3158 cpi
->non_gf_bitrate_adjustment
= cpi
->gf_overspend_bits
/ cpi
->frames_till_gf_update_due
;
3161 // Update data structure that monitors level of reference to last GF
3162 vpx_memset(cpi
->gf_active_flags
, 1, (cm
->mb_rows
* cm
->mb_cols
));
3163 cpi
->gf_active_count
= cm
->mb_rows
* cm
->mb_cols
;
3165 // this frame refreshes means next frames don't unless specified by user
3166 cpi
->common
.frames_since_golden
= 0;
3168 // Clear the alternate reference update pending flag.
3169 cpi
->source_alt_ref_pending
= FALSE
;
3171 // Set the alternate refernce frame active flag
3172 cpi
->source_alt_ref_active
= TRUE
;
3176 static void update_golden_frame_and_stats(VP8_COMP
*cpi
)
3178 VP8_COMMON
*cm
= &cpi
->common
;
3180 // Update the Golden frame reconstruction buffer if signalled and the GF usage counts.
3181 if (cm
->refresh_golden_frame
)
3183 // Update the golden frame buffer
3184 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->yv12_fb
[cm
->gld_fb_idx
]);
3186 // Select an interval before next GF
3187 if (!cpi
->auto_gold
)
3188 cpi
->frames_till_gf_update_due
= cpi
->goldfreq
;
3190 if ((cpi
->pass
!= 2) && (cpi
->frames_till_gf_update_due
> 0))
3192 cpi
->current_gf_interval
= cpi
->frames_till_gf_update_due
;
3194 // Set the bits per frame that we should try and recover in subsequent inter frames
3195 // to account for the extra GF spend... note that his does not apply for GF updates
3196 // that occur coincident with a key frame as the extra cost of key frames is dealt
3198 if ((cm
->frame_type
!= KEY_FRAME
) && !cpi
->source_alt_ref_active
)
3200 // Calcluate GF bits to be recovered
3201 // Projected size - av frame bits available for inter frames for clip as a whole
3202 cpi
->gf_overspend_bits
+= (cpi
->projected_frame_size
- cpi
->inter_frame_target
);
3205 cpi
->non_gf_bitrate_adjustment
= cpi
->gf_overspend_bits
/ cpi
->frames_till_gf_update_due
;
3209 // Update data structure that monitors level of reference to last GF
3210 vpx_memset(cpi
->gf_active_flags
, 1, (cm
->mb_rows
* cm
->mb_cols
));
3211 cpi
->gf_active_count
= cm
->mb_rows
* cm
->mb_cols
;
3213 // this frame refreshes means next frames don't unless specified by user
3214 cm
->refresh_golden_frame
= 0;
3215 cpi
->common
.frames_since_golden
= 0;
3217 //if ( cm->frame_type == KEY_FRAME )
3219 cpi
->recent_ref_frame_usage
[INTRA_FRAME
] = 1;
3220 cpi
->recent_ref_frame_usage
[LAST_FRAME
] = 1;
3221 cpi
->recent_ref_frame_usage
[GOLDEN_FRAME
] = 1;
3222 cpi
->recent_ref_frame_usage
[ALTREF_FRAME
] = 1;
3226 // // Carry a potrtion of count over to begining of next gf sequence
3227 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
3228 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
3229 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
3230 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
3233 // ******** Fixed Q test code only ************
3234 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
3235 if (cpi
->oxcf
.fixed_q
>= 0 &&
3236 cpi
->oxcf
.play_alternate
&& !cpi
->common
.refresh_alt_ref_frame
)
3238 cpi
->source_alt_ref_pending
= TRUE
;
3239 cpi
->frames_till_gf_update_due
= cpi
->baseline_gf_interval
;
3242 if (!cpi
->source_alt_ref_pending
)
3243 cpi
->source_alt_ref_active
= FALSE
;
3245 // Decrement count down till next gf
3246 if (cpi
->frames_till_gf_update_due
> 0)
3247 cpi
->frames_till_gf_update_due
--;
3250 else if (!cpi
->common
.refresh_alt_ref_frame
)
3252 // Decrement count down till next gf
3253 if (cpi
->frames_till_gf_update_due
> 0)
3254 cpi
->frames_till_gf_update_due
--;
3256 if (cpi
->common
.frames_till_alt_ref_frame
)
3257 cpi
->common
.frames_till_alt_ref_frame
--;
3259 cpi
->common
.frames_since_golden
++;
3261 if (cpi
->common
.frames_since_golden
> 1)
3263 cpi
->recent_ref_frame_usage
[INTRA_FRAME
] += cpi
->count_mb_ref_frame_usage
[INTRA_FRAME
];
3264 cpi
->recent_ref_frame_usage
[LAST_FRAME
] += cpi
->count_mb_ref_frame_usage
[LAST_FRAME
];
3265 cpi
->recent_ref_frame_usage
[GOLDEN_FRAME
] += cpi
->count_mb_ref_frame_usage
[GOLDEN_FRAME
];
3266 cpi
->recent_ref_frame_usage
[ALTREF_FRAME
] += cpi
->count_mb_ref_frame_usage
[ALTREF_FRAME
];
3271 // This function updates the reference frame probability estimates that
3272 // will be used during mode selection
3273 static void update_rd_ref_frame_probs(VP8_COMP
*cpi
)
3275 VP8_COMMON
*cm
= &cpi
->common
;
3278 const int *const rfct
= cpi
->recent_ref_frame_usage
;
3279 const int rf_intra
= rfct
[INTRA_FRAME
];
3280 const int rf_inter
= rfct
[LAST_FRAME
] + rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
];
3282 if (cm
->frame_type
== KEY_FRAME
)
3284 cpi
->prob_intra_coded
= 255;
3285 cpi
->prob_last_coded
= 128;
3286 cpi
->prob_gf_coded
= 128;
3288 else if (!(rf_intra
+ rf_inter
))
3290 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3291 cpi
->prob_intra_coded
= 63;
3292 cpi
->prob_last_coded
= 128;
3293 cpi
->prob_gf_coded
= 128;
3297 cpi
->prob_intra_coded
= (rf_intra
* 255) / (rf_intra
+ rf_inter
);
3299 if (cpi
->prob_intra_coded
< 1)
3300 cpi
->prob_intra_coded
= 1;
3302 if ((cm
->frames_since_golden
> 0) || cpi
->source_alt_ref_active
)
3304 cpi
->prob_last_coded
= rf_inter
? (rfct
[LAST_FRAME
] * 255) / rf_inter
: 128;
3306 if (cpi
->prob_last_coded
< 1)
3307 cpi
->prob_last_coded
= 1;
3309 cpi
->prob_gf_coded
= (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
])
3310 ? (rfct
[GOLDEN_FRAME
] * 255) / (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
]) : 128;
3312 if (cpi
->prob_gf_coded
< 1)
3313 cpi
->prob_gf_coded
= 1;
3318 const int *const rfct
= cpi
->count_mb_ref_frame_usage
;
3319 const int rf_intra
= rfct
[INTRA_FRAME
];
3320 const int rf_inter
= rfct
[LAST_FRAME
] + rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
];
3322 if (cm
->frame_type
== KEY_FRAME
)
3324 cpi
->prob_intra_coded
= 255;
3325 cpi
->prob_last_coded
= 128;
3326 cpi
->prob_gf_coded
= 128;
3328 else if (!(rf_intra
+ rf_inter
))
3330 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3331 cpi
->prob_intra_coded
= 63;
3332 cpi
->prob_last_coded
= 128;
3333 cpi
->prob_gf_coded
= 128;
3337 cpi
->prob_intra_coded
= (rf_intra
* 255) / (rf_intra
+ rf_inter
);
3339 if (cpi
->prob_intra_coded
< 1)
3340 cpi
->prob_intra_coded
= 1;
3342 cpi
->prob_last_coded
= rf_inter
? (rfct
[LAST_FRAME
] * 255) / rf_inter
: 128;
3344 if (cpi
->prob_last_coded
< 1)
3345 cpi
->prob_last_coded
= 1;
3347 cpi
->prob_gf_coded
= (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
])
3348 ? (rfct
[GOLDEN_FRAME
] * 255) / (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
]) : 128;
3350 if (cpi
->prob_gf_coded
< 1)
3351 cpi
->prob_gf_coded
= 1;
3354 // update reference frame costs since we can do better than what we got last frame.
3356 if (cpi
->common
.refresh_alt_ref_frame
)
3358 cpi
->prob_intra_coded
+= 40;
3359 cpi
->prob_last_coded
= 200;
3360 cpi
->prob_gf_coded
= 1;
3362 else if (cpi
->common
.frames_since_golden
== 0)
3364 cpi
->prob_last_coded
= 214;
3365 cpi
->prob_gf_coded
= 1;
3367 else if (cpi
->common
.frames_since_golden
== 1)
3369 cpi
->prob_last_coded
= 192;
3370 cpi
->prob_gf_coded
= 220;
3372 else if (cpi
->source_alt_ref_active
)
3374 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden;
3375 cpi
->prob_gf_coded
-= 20;
3377 if (cpi
->prob_gf_coded
< 10)
3378 cpi
->prob_gf_coded
= 10;
3385 // 1 = key, 0 = inter
3386 static int decide_key_frame(VP8_COMP
*cpi
)
3388 VP8_COMMON
*cm
= &cpi
->common
;
3390 int code_key_frame
= FALSE
;
3394 if (cpi
->Speed
> 11)
3397 // Clear down mmx registers
3398 vp8_clear_system_state(); //__asm emms;
3400 if ((cpi
->compressor_speed
== 2) && (cpi
->Speed
>= 5) && (cpi
->sf
.RD
== 0))
3402 double change
= 1.0 * abs((int)(cpi
->intra_error
- cpi
->last_intra_error
)) / (1 + cpi
->last_intra_error
);
3403 double change2
= 1.0 * abs((int)(cpi
->prediction_error
- cpi
->last_prediction_error
)) / (1 + cpi
->last_prediction_error
);
3404 double minerror
= cm
->MBs
* 256;
3408 if (10 * cpi
->intra_error
/ (1 + cpi
->prediction_error
) < 15
3409 && cpi
->prediction_error
> minerror
3410 && (change
> .25 || change2
> .25))
3412 FILE *f
= fopen("intra_inter.stt", "a");
3414 if (cpi
->prediction_error
<= 0)
3415 cpi
->prediction_error
= 1;
3417 fprintf(f
, "%d %d %d %d %14.4f\n",
3418 cm
->current_video_frame
,
3419 (int) cpi
->prediction_error
,
3420 (int) cpi
->intra_error
,
3421 (int)((10 * cpi
->intra_error
) / cpi
->prediction_error
),
3429 cpi
->last_intra_error
= cpi
->intra_error
;
3430 cpi
->last_prediction_error
= cpi
->prediction_error
;
3432 if (10 * cpi
->intra_error
/ (1 + cpi
->prediction_error
) < 15
3433 && cpi
->prediction_error
> minerror
3434 && (change
> .25 || change2
> .25))
3436 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3444 // If the following are true we might as well code a key frame
3445 if (((cpi
->this_frame_percent_intra
== 100) &&
3446 (cpi
->this_frame_percent_intra
> (cpi
->last_frame_percent_intra
+ 2))) ||
3447 ((cpi
->this_frame_percent_intra
> 95) &&
3448 (cpi
->this_frame_percent_intra
>= (cpi
->last_frame_percent_intra
+ 5))))
3450 code_key_frame
= TRUE
;
3452 // in addition if the following are true and this is not a golden frame then code a key frame
3453 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
3454 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
3455 else if (((cpi
->this_frame_percent_intra
> 60) &&
3456 (cpi
->this_frame_percent_intra
> (cpi
->last_frame_percent_intra
* 2))) ||
3457 ((cpi
->this_frame_percent_intra
> 75) &&
3458 (cpi
->this_frame_percent_intra
> (cpi
->last_frame_percent_intra
* 3 / 2))) ||
3459 ((cpi
->this_frame_percent_intra
> 90) &&
3460 (cpi
->this_frame_percent_intra
> (cpi
->last_frame_percent_intra
+ 10))))
3462 if (!cm
->refresh_golden_frame
)
3463 code_key_frame
= TRUE
;
3466 return code_key_frame
;
3470 #if !(CONFIG_REALTIME_ONLY)
3471 static void Pass1Encode(VP8_COMP
*cpi
, unsigned long *size
, unsigned char *dest
, unsigned int *frame_flags
)
3476 set_quantizer(cpi
, 26);
3478 scale_and_extend_source(cpi
->un_scaled_source
, cpi
);
3479 vp8_first_pass(cpi
);
3484 void write_cx_frame_to_file(YV12_BUFFER_CONFIG
*frame
, int this_frame
)
3492 sprintf(filename
, "cx\\y%04d.raw", this_frame
);
3493 yframe
= fopen(filename
, "wb");
3495 for (i
= 0; i
< frame
->y_height
; i
++)
3496 fwrite(frame
->y_buffer
+ i
* frame
->y_stride
, frame
->y_width
, 1, yframe
);
3499 sprintf(filename
, "cx\\u%04d.raw", this_frame
);
3500 yframe
= fopen(filename
, "wb");
3502 for (i
= 0; i
< frame
->uv_height
; i
++)
3503 fwrite(frame
->u_buffer
+ i
* frame
->uv_stride
, frame
->uv_width
, 1, yframe
);
3506 sprintf(filename
, "cx\\v%04d.raw", this_frame
);
3507 yframe
= fopen(filename
, "wb");
3509 for (i
= 0; i
< frame
->uv_height
; i
++)
3510 fwrite(frame
->v_buffer
+ i
* frame
->uv_stride
, frame
->uv_width
, 1, yframe
);
3515 // return of 0 means drop frame
3517 // Function to test for conditions that indeicate we should loop
3518 // back and recode a frame.
3519 static BOOL
recode_loop_test( VP8_COMP
*cpi
,
3520 int high_limit
, int low_limit
,
3521 int q
, int maxq
, int minq
)
3523 BOOL force_recode
= FALSE
;
3524 VP8_COMMON
*cm
= &cpi
->common
;
3526 // Is frame recode allowed at all
3527 // Yes if either recode mode 1 is selected or mode two is selcted
3528 // and the frame is a key frame. golden frame or alt_ref_frame
3529 if ( (cpi
->sf
.recode_loop
== 1) ||
3530 ( (cpi
->sf
.recode_loop
== 2) &&
3531 ( (cm
->frame_type
== KEY_FRAME
) ||
3532 cm
->refresh_golden_frame
||
3533 cm
->refresh_alt_ref_frame
) ) )
3535 // General over and under shoot tests
3536 if ( ((cpi
->projected_frame_size
> high_limit
) && (q
< maxq
)) ||
3537 ((cpi
->projected_frame_size
< low_limit
) && (q
> minq
)) )
3539 force_recode
= TRUE
;
3541 // Special Constrained quality tests
3542 else if (cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
)
3544 // Undershoot and below auto cq level
3545 if ( (q
> cpi
->cq_target_quality
) &&
3546 (cpi
->projected_frame_size
<
3547 ((cpi
->this_frame_target
* 7) >> 3)))
3549 force_recode
= TRUE
;
3551 // Severe undershoot and between auto and user cq level
3552 else if ( (q
> cpi
->oxcf
.cq_level
) &&
3553 (cpi
->projected_frame_size
< cpi
->min_frame_bandwidth
) &&
3554 (cpi
->active_best_quality
> cpi
->oxcf
.cq_level
))
3556 force_recode
= TRUE
;
3557 cpi
->active_best_quality
= cpi
->oxcf
.cq_level
;
3562 return force_recode
;
3565 static void encode_frame_to_data_rate
3568 unsigned long *size
,
3569 unsigned char *dest
,
3570 unsigned int *frame_flags
3574 int frame_over_shoot_limit
;
3575 int frame_under_shoot_limit
;
3585 int zbin_oq_low
= 0;
3588 VP8_COMMON
*cm
= &cpi
->common
;
3589 int active_worst_qchanged
= FALSE
;
3591 int overshoot_seen
= FALSE
;
3592 int undershoot_seen
= FALSE
;
3593 int drop_mark
= cpi
->oxcf
.drop_frames_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100;
3594 int drop_mark75
= drop_mark
* 2 / 3;
3595 int drop_mark50
= drop_mark
/ 4;
3596 int drop_mark25
= drop_mark
/ 8;
3598 // Clear down mmx registers to allow floating point in what follows
3599 vp8_clear_system_state();
3601 // Test code for segmentation of gf/arf (0,0)
3602 //segmentation_test_function((VP8_PTR) cpi);
3604 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3605 #if !(CONFIG_REALTIME_ONLY)
3609 if (cpi
->common
.refresh_alt_ref_frame
)
3611 cpi
->per_frame_bandwidth
= cpi
->gf_bits
; // Per frame bit target for the alt ref frame
3612 cpi
->target_bandwidth
= cpi
->gf_bits
* cpi
->output_frame_rate
; // per second target bitrate
3617 cpi
->per_frame_bandwidth
= (int)(cpi
->target_bandwidth
/ cpi
->output_frame_rate
);
3619 // Default turn off buffer to buffer copying
3620 cm
->copy_buffer_to_gf
= 0;
3621 cm
->copy_buffer_to_arf
= 0;
3623 // Clear zbin over-quant value and mode boost values.
3624 cpi
->zbin_over_quant
= 0;
3625 cpi
->zbin_mode_boost
= 0;
3627 // Enable or disable mode based tweaking of the zbin
3628 // For 2 Pass Only used where GF/ARF prediction quality
3629 // is above a threshold
3630 cpi
->zbin_mode_boost
= 0;
3631 cpi
->zbin_mode_boost_enabled
= TRUE
;
3634 if ( cpi
->gfu_boost
<= 400 )
3636 cpi
->zbin_mode_boost_enabled
= FALSE
;
3640 // Current default encoder behaviour for the altref sign bias
3641 if (cpi
->source_alt_ref_active
)
3642 cpi
->common
.ref_frame_sign_bias
[ALTREF_FRAME
] = 1;
3644 cpi
->common
.ref_frame_sign_bias
[ALTREF_FRAME
] = 0;
3646 // Check to see if a key frame is signalled
3647 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3648 if ((cm
->current_video_frame
== 0) ||
3649 (cm
->frame_flags
& FRAMEFLAGS_KEY
) ||
3650 (cpi
->oxcf
.auto_key
&& (cpi
->frames_since_key
% cpi
->key_frame_frequency
== 0)))
3652 // Key frame from VFW/auto-keyframe/first frame
3653 cm
->frame_type
= KEY_FRAME
;
3656 // Set default state for segment and mode based loop filter update flags
3657 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 0;
3658 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 0;
3659 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 0;
3661 // Set various flags etc to special state if it is a key frame
3662 if (cm
->frame_type
== KEY_FRAME
)
3666 // Reset the loop filter deltas and segmentation map
3667 setup_features(cpi
);
3669 // If segmentation is enabled force a map update for key frames
3670 if (cpi
->mb
.e_mbd
.segmentation_enabled
)
3672 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 1;
3673 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 1;
3676 // The alternate reference frame cannot be active for a key frame
3677 cpi
->source_alt_ref_active
= FALSE
;
3679 // Reset the RD threshold multipliers to default of * 1 (128)
3680 for (i
= 0; i
< MAX_MODES
; i
++)
3682 cpi
->rd_thresh_mult
[i
] = 128;
3686 // Test code for segmentation
3687 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3688 //if ( (cm->current_video_frame % 2) == 0 )
3689 // enable_segmentation((VP8_PTR)cpi);
3691 // disable_segmentation((VP8_PTR)cpi);
3694 // Experimental code for lagged compress and one pass
3695 // Initialise one_pass GF frames stats
3696 // Update stats used for GF selection
3697 //if ( cpi->pass == 0 )
3699 cpi
->one_pass_frame_index
= cm
->current_video_frame
% MAX_LAG_BUFFERS
;
3701 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frames_so_far
= 0;
3702 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_intra_error
= 0.0;
3703 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_coded_error
= 0.0;
3704 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_pcnt_inter
= 0.0;
3705 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_pcnt_motion
= 0.0;
3706 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_mvr
= 0.0;
3707 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_mvr_abs
= 0.0;
3708 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_mvc
= 0.0;
3709 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_mvc_abs
= 0.0;
3713 update_rd_ref_frame_probs(cpi
);
3715 if (cpi
->drop_frames_allowed
)
3717 // The reset to decimation 0 is only done here for one pass.
3718 // Once it is set two pass leaves decimation on till the next kf.
3719 if ((cpi
->buffer_level
> drop_mark
) && (cpi
->decimation_factor
> 0))
3720 cpi
->decimation_factor
--;
3722 if (cpi
->buffer_level
> drop_mark75
&& cpi
->decimation_factor
> 0)
3723 cpi
->decimation_factor
= 1;
3725 else if (cpi
->buffer_level
< drop_mark25
&& (cpi
->decimation_factor
== 2 || cpi
->decimation_factor
== 3))
3727 cpi
->decimation_factor
= 3;
3729 else if (cpi
->buffer_level
< drop_mark50
&& (cpi
->decimation_factor
== 1 || cpi
->decimation_factor
== 2))
3731 cpi
->decimation_factor
= 2;
3733 else if (cpi
->buffer_level
< drop_mark75
&& (cpi
->decimation_factor
== 0 || cpi
->decimation_factor
== 1))
3735 cpi
->decimation_factor
= 1;
3738 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3741 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3742 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3743 // some situations to drop frame rate but throw more bits at each frame.
3745 // Note that dropping a key frame can be problematic if spatial resampling is also active
3746 if (cpi
->decimation_factor
> 0)
3748 switch (cpi
->decimation_factor
)
3751 cpi
->per_frame_bandwidth
= cpi
->per_frame_bandwidth
* 3 / 2;
3754 cpi
->per_frame_bandwidth
= cpi
->per_frame_bandwidth
* 5 / 4;
3757 cpi
->per_frame_bandwidth
= cpi
->per_frame_bandwidth
* 5 / 4;
3761 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3762 if ((cm
->frame_type
== KEY_FRAME
)) // && cpi->oxcf.allow_spatial_resampling )
3764 cpi
->decimation_count
= cpi
->decimation_factor
;
3766 else if (cpi
->decimation_count
> 0)
3768 cpi
->decimation_count
--;
3769 cpi
->bits_off_target
+= cpi
->av_per_frame_bandwidth
;
3770 cm
->current_video_frame
++;
3771 cpi
->frames_since_key
++;
3777 cpi
->buffer_level
= cpi
->bits_off_target
;
3782 cpi
->decimation_count
= cpi
->decimation_factor
;
3785 // Decide how big to make the frame
3786 if (!pick_frame_size(cpi
))
3788 cm
->current_video_frame
++;
3789 cpi
->frames_since_key
++;
3793 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3794 // This has a knock on effect on active best quality as well.
3795 // For CBR if the buffer reaches its maximum level then we can no longer
3796 // save up bits for later frames so we might as well use them up
3797 // on the current frame.
3798 if ((cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
) &&
3799 (cpi
->buffer_level
>= cpi
->oxcf
.optimal_buffer_level
) && cpi
->buffered_mode
)
3801 int Adjustment
= cpi
->active_worst_quality
/ 4; // Max adjustment is 1/4
3806 int tmp_lvl
= cpi
->buffer_level
;
3808 if (cpi
->buffer_level
< cpi
->oxcf
.maximum_buffer_size
)
3810 buff_lvl_step
= (cpi
->oxcf
.maximum_buffer_size
- cpi
->oxcf
.optimal_buffer_level
) / Adjustment
;
3814 Adjustment
= (cpi
->buffer_level
- cpi
->oxcf
.optimal_buffer_level
) / buff_lvl_step
;
3815 cpi
->active_worst_quality
-= Adjustment
;
3820 cpi
->active_worst_quality
-= Adjustment
;
3825 // Set an active best quality and if necessary active worst quality
3826 // There is some odd behaviour for one pass here that needs attention.
3827 if ( (cpi
->pass
== 2) || (cpi
->ni_frames
> 150))
3829 vp8_clear_system_state();
3831 Q
= cpi
->active_worst_quality
;
3833 if ( cm
->frame_type
== KEY_FRAME
)
3835 if ( cpi
->pass
== 2 )
3837 if (cpi
->gfu_boost
> 600)
3838 cpi
->active_best_quality
= kf_low_motion_minq
[Q
];
3840 cpi
->active_best_quality
= kf_high_motion_minq
[Q
];
3842 // Special case for key frames forced because we have reached
3843 // the maximum key frame interval. Here force the Q to a range
3844 // based on the ambient Q to reduce the risk of popping
3845 if ( cpi
->this_key_frame_forced
)
3847 if ( cpi
->active_best_quality
> cpi
->avg_frame_qindex
* 7/8)
3848 cpi
->active_best_quality
= cpi
->avg_frame_qindex
* 7/8;
3849 else if ( cpi
->active_best_quality
< cpi
->avg_frame_qindex
>> 2 )
3850 cpi
->active_best_quality
= cpi
->avg_frame_qindex
>> 2;
3853 // One pass more conservative
3855 cpi
->active_best_quality
= kf_high_motion_minq
[Q
];
3858 else if (cm
->refresh_golden_frame
|| cpi
->common
.refresh_alt_ref_frame
)
3860 // Use the lower of cpi->active_worst_quality and recent
3861 // average Q as basis for GF/ARF Q limit unless last frame was
3863 if ( (cpi
->frames_since_key
> 1) &&
3864 (cpi
->avg_frame_qindex
< cpi
->active_worst_quality
) )
3866 Q
= cpi
->avg_frame_qindex
;
3868 if ( (cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
) &&
3869 (Q
< cpi
->oxcf
.cq_level
) )
3871 Q
= cpi
->oxcf
.cq_level
;
3875 if ( cpi
->pass
== 2 )
3877 if ( cpi
->gfu_boost
> 1000 )
3878 cpi
->active_best_quality
= gf_low_motion_minq
[Q
];
3879 else if ( cpi
->gfu_boost
< 400 )
3880 cpi
->active_best_quality
= gf_high_motion_minq
[Q
];
3882 cpi
->active_best_quality
= gf_mid_motion_minq
[Q
];
3884 // One pass more conservative
3886 cpi
->active_best_quality
= gf_high_motion_minq
[Q
];
3890 cpi
->active_best_quality
= inter_minq
[Q
];
3892 // For the constant/constrained quality mode we dont want
3893 // the quality to rise above the cq level.
3894 if ((cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
) &&
3895 (cpi
->active_best_quality
< cpi
->cq_target_quality
) )
3897 // If we are strongly undershooting the target rate in the last
3898 // frames then use the user passed in cq value not the auto
3900 if ( cpi
->rolling_actual_bits
< cpi
->min_frame_bandwidth
)
3901 cpi
->active_best_quality
= cpi
->oxcf
.cq_level
;
3903 cpi
->active_best_quality
= cpi
->cq_target_quality
;
3907 // If CBR and the buffer is as full then it is reasonable to allow higher quality on the frames
3908 // to prevent bits just going to waste.
3909 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
3911 // Note that the use of >= here elliminates the risk of a devide by 0 error in the else if clause
3912 if (cpi
->buffer_level
>= cpi
->oxcf
.maximum_buffer_size
)
3913 cpi
->active_best_quality
= cpi
->best_quality
;
3915 else if (cpi
->buffer_level
> cpi
->oxcf
.optimal_buffer_level
)
3917 int Fraction
= ((cpi
->buffer_level
- cpi
->oxcf
.optimal_buffer_level
) * 128) / (cpi
->oxcf
.maximum_buffer_size
- cpi
->oxcf
.optimal_buffer_level
);
3918 int min_qadjustment
= ((cpi
->active_best_quality
- cpi
->best_quality
) * Fraction
) / 128;
3920 cpi
->active_best_quality
-= min_qadjustment
;
3925 // Clip the active best and worst quality values to limits
3926 if (cpi
->active_worst_quality
> cpi
->worst_quality
)
3927 cpi
->active_worst_quality
= cpi
->worst_quality
;
3929 if (cpi
->active_best_quality
< cpi
->best_quality
)
3930 cpi
->active_best_quality
= cpi
->best_quality
;
3931 else if (cpi
->active_best_quality
> cpi
->active_worst_quality
)
3932 cpi
->active_best_quality
= cpi
->active_worst_quality
;
3934 // Determine initial Q to try
3935 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
3936 last_zbin_oq
= cpi
->zbin_over_quant
;
3938 // Set highest allowed value for Zbin over quant
3939 if (cm
->frame_type
== KEY_FRAME
)
3940 zbin_oq_high
= 0; //ZBIN_OQ_MAX/16
3941 else if (cm
->refresh_alt_ref_frame
|| (cm
->refresh_golden_frame
&& !cpi
->source_alt_ref_active
))
3944 zbin_oq_high
= ZBIN_OQ_MAX
;
3946 // Setup background Q adjustment for error resilliant mode
3947 if (cpi
->cyclic_refresh_mode_enabled
)
3948 cyclic_background_refresh(cpi
, Q
, 0);
3950 vp8_compute_frame_size_bounds(cpi
, &frame_under_shoot_limit
, &frame_over_shoot_limit
);
3952 // Limit Q range for the adaptive loop.
3953 bottom_index
= cpi
->active_best_quality
;
3954 top_index
= cpi
->active_worst_quality
;
3955 q_low
= cpi
->active_best_quality
;
3956 q_high
= cpi
->active_worst_quality
;
3958 vp8_save_coding_context(cpi
);
3963 scale_and_extend_source(cpi
->un_scaled_source
, cpi
);
3964 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC
3966 if (cpi
->oxcf
.noise_sensitivity
> 0)
3971 switch (cpi
->oxcf
.noise_sensitivity
)
3994 if (cm
->frame_type
== KEY_FRAME
)
3996 vp8_de_noise(cpi
->Source
, cpi
->Source
, l
, 1, 0, RTCD(postproc
));
4000 vp8_de_noise(cpi
->Source
, cpi
->Source
, l
, 1, 0, RTCD(postproc
));
4002 src
= cpi
->Source
->y_buffer
;
4004 if (cpi
->Source
->y_stride
< 0)
4006 src
+= cpi
->Source
->y_stride
* (cpi
->Source
->y_height
- 1);
4013 #ifdef OUTPUT_YUV_SRC
4014 vp8_write_yuv_frame(cpi
->Source
);
4019 vp8_clear_system_state(); //__asm emms;
4022 if(cpi->is_src_frame_alt_ref)
4026 set_quantizer(cpi
, Q
);
4029 // setup skip prob for costing in mode/mv decision
4030 if (cpi
->common
.mb_no_coeff_skip
)
4032 cpi
->prob_skip_false
= cpi
->base_skip_false_prob
[Q
];
4034 if (cm
->frame_type
!= KEY_FRAME
)
4036 if (cpi
->common
.refresh_alt_ref_frame
)
4038 if (cpi
->last_skip_false_probs
[2] != 0)
4039 cpi
->prob_skip_false
= cpi
->last_skip_false_probs
[2];
4042 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
4043 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
4044 else if (cpi->last_skip_false_probs[2]!=0)
4045 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
4048 else if (cpi
->common
.refresh_golden_frame
)
4050 if (cpi
->last_skip_false_probs
[1] != 0)
4051 cpi
->prob_skip_false
= cpi
->last_skip_false_probs
[1];
4054 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
4055 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
4056 else if (cpi->last_skip_false_probs[1]!=0)
4057 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
4062 if (cpi
->last_skip_false_probs
[0] != 0)
4063 cpi
->prob_skip_false
= cpi
->last_skip_false_probs
[0];
4066 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
4067 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
4068 else if(cpi->last_skip_false_probs[0]!=0)
4069 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
4073 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
4074 if (cpi
->prob_skip_false
< 5)
4075 cpi
->prob_skip_false
= 5;
4077 if (cpi
->prob_skip_false
> 250)
4078 cpi
->prob_skip_false
= 250;
4080 if (cpi
->is_src_frame_alt_ref
)
4081 cpi
->prob_skip_false
= 1;
4090 FILE *f
= fopen("skip.stt", "a");
4091 fprintf(f
, "%d, %d, %4d ", cpi
->common
.refresh_golden_frame
, cpi
->common
.refresh_alt_ref_frame
, cpi
->prob_skip_false
);
4099 if (cm
->frame_type
== KEY_FRAME
)
4100 vp8_setup_key_frame(cpi
);
4102 // transform / motion compensation build reconstruction frame
4104 vp8_encode_frame(cpi
);
4105 cpi
->projected_frame_size
-= vp8_estimate_entropy_savings(cpi
);
4106 cpi
->projected_frame_size
= (cpi
->projected_frame_size
> 0) ? cpi
->projected_frame_size
: 0;
4108 vp8_clear_system_state(); //__asm emms;
4110 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
4111 // (assuming that we didn't)!
4112 if (cpi
->pass
!= 2 && cpi
->oxcf
.auto_key
&& cm
->frame_type
!= KEY_FRAME
)
4114 if (decide_key_frame(cpi
))
4116 vp8_calc_auto_iframe_target_size(cpi
);
4118 // Reset all our sizing numbers and recode
4119 cm
->frame_type
= KEY_FRAME
;
4121 // Clear the Alt reference frame active flag when we have a key frame
4122 cpi
->source_alt_ref_active
= FALSE
;
4124 // Reset the loop filter deltas and segmentation map
4125 setup_features(cpi
);
4127 // If segmentation is enabled force a map update for key frames
4128 if (cpi
->mb
.e_mbd
.segmentation_enabled
)
4130 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 1;
4131 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 1;
4134 vp8_restore_coding_context(cpi
);
4136 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4138 vp8_compute_frame_size_bounds(cpi
, &frame_under_shoot_limit
, &frame_over_shoot_limit
);
4140 // Limit Q range for the adaptive loop.
4141 bottom_index
= cpi
->active_best_quality
;
4142 top_index
= cpi
->active_worst_quality
;
4143 q_low
= cpi
->active_best_quality
;
4144 q_high
= cpi
->active_worst_quality
;
4149 resize_key_frame(cpi
);
4154 vp8_clear_system_state();
4156 if (frame_over_shoot_limit
== 0)
4157 frame_over_shoot_limit
= 1;
4159 // Are we are overshooting and up against the limit of active max Q.
4160 if (((cpi
->pass
!= 2) || (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)) &&
4161 (Q
== cpi
->active_worst_quality
) &&
4162 (cpi
->active_worst_quality
< cpi
->worst_quality
) &&
4163 (cpi
->projected_frame_size
> frame_over_shoot_limit
))
4165 int over_size_percent
= ((cpi
->projected_frame_size
- frame_over_shoot_limit
) * 100) / frame_over_shoot_limit
;
4167 // If so is there any scope for relaxing it
4168 while ((cpi
->active_worst_quality
< cpi
->worst_quality
) && (over_size_percent
> 0))
4170 cpi
->active_worst_quality
++;
4171 top_index
= cpi
->active_worst_quality
;
4172 over_size_percent
= (int)(over_size_percent
* 0.96); // Assume 1 qstep = about 4% on frame size.
4175 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
4176 active_worst_qchanged
= TRUE
;
4179 active_worst_qchanged
= FALSE
;
4181 #if !(CONFIG_REALTIME_ONLY)
4182 // Special case handling for forced key frames
4183 if ( (cm
->frame_type
== KEY_FRAME
) && cpi
->this_key_frame_forced
)
4186 int kf_err
= vp8_calc_ss_err(cpi
->Source
,
4187 &cm
->yv12_fb
[cm
->new_fb_idx
],
4188 IF_RTCD(&cpi
->rtcd
.variance
));
4190 // The key frame is not good enough
4191 if ( kf_err
> ((cpi
->ambient_err
* 7) >> 3) )
4194 q_high
= (Q
> q_low
) ? (Q
- 1) : q_low
;
4197 Q
= (q_high
+ q_low
) >> 1;
4199 // The key frame is much better than the previous frame
4200 else if ( kf_err
< (cpi
->ambient_err
>> 1) )
4203 q_low
= (Q
< q_high
) ? (Q
+ 1) : q_high
;
4206 Q
= (q_high
+ q_low
+ 1) >> 1;
4209 // Clamp Q to upper and lower limits:
4215 Loop
= ((Q
!= last_q
)) ? TRUE
: FALSE
;
4218 // Is the projected frame size out of range and are we allowed to attempt to recode.
4219 else if ( recode_loop_test( cpi
,
4220 frame_over_shoot_limit
, frame_under_shoot_limit
,
4221 Q
, top_index
, bottom_index
) )
4226 // Frame size out of permitted range:
4227 // Update correction factor & compute new Q to try...
4229 // Frame is too large
4230 if (cpi
->projected_frame_size
> cpi
->this_frame_target
)
4232 //if ( cpi->zbin_over_quant == 0 )
4233 q_low
= (Q
< q_high
) ? (Q
+ 1) : q_high
; // Raise Qlow as to at least the current value
4235 if (cpi
->zbin_over_quant
> 0) // If we are using over quant do the same for zbin_oq_low
4236 zbin_oq_low
= (cpi
->zbin_over_quant
< zbin_oq_high
) ? (cpi
->zbin_over_quant
+ 1) : zbin_oq_high
;
4238 //if ( undershoot_seen || (Q == MAXQ) )
4239 if (undershoot_seen
)
4241 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4242 if (!active_worst_qchanged
)
4243 vp8_update_rate_correction_factors(cpi
, 1);
4245 Q
= (q_high
+ q_low
+ 1) / 2;
4247 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4249 cpi
->zbin_over_quant
= 0;
4252 zbin_oq_low
= (cpi
->zbin_over_quant
< zbin_oq_high
) ? (cpi
->zbin_over_quant
+ 1) : zbin_oq_high
;
4253 cpi
->zbin_over_quant
= (zbin_oq_high
+ zbin_oq_low
) / 2;
4258 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4259 if (!active_worst_qchanged
)
4260 vp8_update_rate_correction_factors(cpi
, 0);
4262 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4264 while (((Q
< q_low
) || (cpi
->zbin_over_quant
< zbin_oq_low
)) && (Retries
< 10))
4266 vp8_update_rate_correction_factors(cpi
, 0);
4267 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4272 overshoot_seen
= TRUE
;
4274 // Frame is too small
4277 if (cpi
->zbin_over_quant
== 0)
4278 q_high
= (Q
> q_low
) ? (Q
- 1) : q_low
; // Lower q_high if not using over quant
4279 else // else lower zbin_oq_high
4280 zbin_oq_high
= (cpi
->zbin_over_quant
> zbin_oq_low
) ? (cpi
->zbin_over_quant
- 1) : zbin_oq_low
;
4284 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4285 if (!active_worst_qchanged
)
4286 vp8_update_rate_correction_factors(cpi
, 1);
4288 Q
= (q_high
+ q_low
) / 2;
4290 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4292 cpi
->zbin_over_quant
= 0;
4294 cpi
->zbin_over_quant
= (zbin_oq_high
+ zbin_oq_low
) / 2;
4298 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4299 if (!active_worst_qchanged
)
4300 vp8_update_rate_correction_factors(cpi
, 0);
4302 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4304 // Special case reset for qlow for constrained quality.
4305 // This should only trigger where there is very substantial
4306 // undershoot on a frame and the auto cq level is above
4307 // the user passsed in value.
4308 if ( (cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
) &&
4314 while (((Q
> q_high
) || (cpi
->zbin_over_quant
> zbin_oq_high
)) && (Retries
< 10))
4316 vp8_update_rate_correction_factors(cpi
, 0);
4317 Q
= vp8_regulate_q(cpi
, cpi
->this_frame_target
);
4322 undershoot_seen
= TRUE
;
4325 // Clamp Q to upper and lower limits:
4331 // Clamp cpi->zbin_over_quant
4332 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
;
4334 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4335 Loop
= ((Q
!= last_q
)) ? TRUE
: FALSE
;
4336 last_zbin_oq
= cpi
->zbin_over_quant
;
4342 if (cpi
->is_src_frame_alt_ref
)
4347 vp8_restore_coding_context(cpi
);
4350 cpi
->tot_recode_hits
++;
4354 while (Loop
== TRUE
);
4357 // Experimental code for lagged and one pass
4358 // Update stats used for one pass GF selection
4362 double frame_intra_error;
4363 double frame_coded_error;
4364 double frame_pcnt_inter;
4365 double frame_pcnt_motion;
4367 double frame_mvr_abs;
4369 double frame_mvc_abs;
4372 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_coded_error
= (double)cpi
->prediction_error
;
4373 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_intra_error
= (double)cpi
->intra_error
;
4374 cpi
->one_pass_frame_stats
[cpi
->one_pass_frame_index
].frame_pcnt_inter
= (double)(100 - cpi
->this_frame_percent_intra
) / 100.0;
4378 // Special case code to reduce pulsing when key frames are forced at a
4379 // fixed interval. Note the reconstruction error if it is the frame before
4380 // the force key frame
4381 if ( cpi
->next_key_frame_forced
&& (cpi
->frames_to_key
== 0) )
4383 cpi
->ambient_err
= vp8_calc_ss_err(cpi
->Source
,
4384 &cm
->yv12_fb
[cm
->new_fb_idx
],
4385 IF_RTCD(&cpi
->rtcd
.variance
));
4388 // This frame's MVs are saved and will be used in next frame's MV prediction.
4389 if(cm
->show_frame
) //do not save for altref frame
4393 MODE_INFO
*tmp
= cm
->mip
; //point to beginning of allocated MODE_INFO arrays.
4394 //static int last_video_frame = 0;
4396 if(cm
->frame_type
!= KEY_FRAME
)
4398 for (mb_row
= 0; mb_row
< cm
->mb_rows
+1; mb_row
++)
4400 for (mb_col
= 0; mb_col
< cm
->mb_cols
+1; mb_col
++)
4402 if(tmp
->mbmi
.ref_frame
!= INTRA_FRAME
)
4403 cpi
->lfmv
[mb_col
+ mb_row
*(cm
->mode_info_stride
)].as_int
= tmp
->mbmi
.mv
.as_int
;
4405 cpi
->lf_ref_frame_sign_bias
[mb_col
+ mb_row
*(cm
->mode_info_stride
)] = cm
->ref_frame_sign_bias
[tmp
->mbmi
.ref_frame
];
4406 cpi
->lf_ref_frame
[mb_col
+ mb_row
*(cm
->mode_info_stride
)] = tmp
->mbmi
.ref_frame
;
4413 // Update the GF useage maps.
4414 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4415 vp8_update_gf_useage_maps(cpi
, cm
, &cpi
->mb
);
4417 if (cm
->frame_type
== KEY_FRAME
)
4418 cm
->refresh_last_frame
= 1;
4422 FILE *f
= fopen("gfactive.stt", "a");
4423 fprintf(f
, "%8d %8d %8d %8d %8d\n", cm
->current_video_frame
, (100 * cpi
->gf_active_count
) / (cpi
->common
.mb_rows
* cpi
->common
.mb_cols
), cpi
->this_iiratio
, cpi
->next_iiratio
, cm
->refresh_golden_frame
);
4428 // 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
4429 // This is purely an encoder descision at present.
4430 if (!cpi
->oxcf
.error_resilient_mode
&& cm
->refresh_golden_frame
)
4431 cm
->copy_buffer_to_arf
= 2;
4433 cm
->copy_buffer_to_arf
= 0;
4435 if (cm
->refresh_last_frame
)
4437 vp8_swap_yv12_buffer(&cm
->yv12_fb
[cm
->lst_fb_idx
], &cm
->yv12_fb
[cm
->new_fb_idx
]);
4438 cm
->frame_to_show
= &cm
->yv12_fb
[cm
->lst_fb_idx
];
4441 cm
->frame_to_show
= &cm
->yv12_fb
[cm
->new_fb_idx
];
4443 //#pragma omp parallel sections
4446 //#pragma omp section
4449 struct vpx_usec_timer timer
;
4451 vpx_usec_timer_start(&timer
);
4453 if (cpi
->sf
.auto_filter
== 0)
4454 vp8cx_pick_filter_level_fast(cpi
->Source
, cpi
);
4456 vp8cx_pick_filter_level(cpi
->Source
, cpi
);
4458 vpx_usec_timer_mark(&timer
);
4460 cpi
->time_pick_lpf
+= vpx_usec_timer_elapsed(&timer
);
4463 cm
->filter_level
= 0;
4465 if (cm
->filter_level
> 0)
4467 vp8cx_set_alt_lf_level(cpi
, cm
->filter_level
);
4468 vp8_loop_filter_frame(cm
, &cpi
->mb
.e_mbd
, cm
->filter_level
);
4469 cm
->last_filter_type
= cm
->filter_type
;
4470 cm
->last_sharpness_level
= cm
->sharpness_level
;
4472 /* Move storing frame_type out of the above loop since it is also needed in motion search besides loopfilter */
4473 cm
->last_frame_type
= cm
->frame_type
;
4475 vp8_yv12_extend_frame_borders_ptr(cm
->frame_to_show
);
4477 if (cpi
->oxcf
.error_resilient_mode
== 1)
4479 cm
->refresh_entropy_probs
= 0;
4483 //#pragma omp section
4485 // build the bitstream
4486 vp8_pack_bitstream(cpi
, dest
, size
);
4491 YV12_BUFFER_CONFIG
*lst_yv12
= &cm
->yv12_fb
[cm
->lst_fb_idx
];
4492 YV12_BUFFER_CONFIG
*new_yv12
= &cm
->yv12_fb
[cm
->new_fb_idx
];
4493 YV12_BUFFER_CONFIG
*gld_yv12
= &cm
->yv12_fb
[cm
->gld_fb_idx
];
4494 YV12_BUFFER_CONFIG
*alt_yv12
= &cm
->yv12_fb
[cm
->alt_fb_idx
];
4495 // At this point the new frame has been encoded coded.
4496 // If any buffer copy / swaping is signalled it should be done here.
4497 if (cm
->frame_type
== KEY_FRAME
)
4499 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, gld_yv12
);
4500 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, alt_yv12
);
4502 else // For non key frames
4504 // Code to copy between reference buffers
4505 if (cm
->copy_buffer_to_arf
)
4507 if (cm
->copy_buffer_to_arf
== 1)
4509 if (cm
->refresh_last_frame
)
4510 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4511 vp8_yv12_copy_frame_ptr(new_yv12
, alt_yv12
);
4513 vp8_yv12_copy_frame_ptr(lst_yv12
, alt_yv12
);
4515 else if (cm
->copy_buffer_to_arf
== 2)
4516 vp8_yv12_copy_frame_ptr(gld_yv12
, alt_yv12
);
4519 if (cm
->copy_buffer_to_gf
)
4521 if (cm
->copy_buffer_to_gf
== 1)
4523 if (cm
->refresh_last_frame
)
4524 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4525 vp8_yv12_copy_frame_ptr(new_yv12
, gld_yv12
);
4527 vp8_yv12_copy_frame_ptr(lst_yv12
, gld_yv12
);
4529 else if (cm
->copy_buffer_to_gf
== 2)
4530 vp8_yv12_copy_frame_ptr(alt_yv12
, gld_yv12
);
4535 // Update rate control heuristics
4536 cpi
->total_byte_count
+= (*size
);
4537 cpi
->projected_frame_size
= (*size
) << 3;
4539 if (!active_worst_qchanged
)
4540 vp8_update_rate_correction_factors(cpi
, 2);
4542 cpi
->last_q
[cm
->frame_type
] = cm
->base_qindex
;
4544 if (cm
->frame_type
== KEY_FRAME
)
4546 vp8_adjust_key_frame_context(cpi
);
4549 // Keep a record of ambient average Q.
4550 if (cm
->frame_type
!= KEY_FRAME
)
4551 cpi
->avg_frame_qindex
= (2 + 3 * cpi
->avg_frame_qindex
+ cm
->base_qindex
) >> 2;
4553 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4554 if ((cm
->frame_type
!= KEY_FRAME
) && !cm
->refresh_golden_frame
&& !cm
->refresh_alt_ref_frame
)
4558 // Calculate the average Q for normal inter frames (not key or GFU
4560 if ( cpi
->pass
== 2 )
4562 cpi
->ni_tot_qi
+= Q
;
4563 cpi
->ni_av_qi
= (cpi
->ni_tot_qi
/ cpi
->ni_frames
);
4567 // Damp value for first few frames
4568 if (cpi
->ni_frames
> 150 )
4570 cpi
->ni_tot_qi
+= Q
;
4571 cpi
->ni_av_qi
= (cpi
->ni_tot_qi
/ cpi
->ni_frames
);
4573 // For one pass, early in the clip ... average the current frame Q
4574 // value with the worstq entered by the user as a dampening measure
4577 cpi
->ni_tot_qi
+= Q
;
4578 cpi
->ni_av_qi
= ((cpi
->ni_tot_qi
/ cpi
->ni_frames
) + cpi
->worst_quality
+ 1) / 2;
4581 // If the average Q is higher than what was used in the last frame
4582 // (after going through the recode loop to keep the frame size within range)
4583 // then use the last frame value - 1.
4584 // The -1 is designed to stop Q and hence the data rate, from progressively
4585 // falling away during difficult sections, but at the same time reduce the number of
4586 // itterations around the recode loop.
4587 if (Q
> cpi
->ni_av_qi
)
4588 cpi
->ni_av_qi
= Q
- 1;
4594 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4595 if ((cpi
->drop_frames_allowed
) &&
4596 (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
) &&
4597 (cpi
->buffer_level
< cpi
->oxcf
.drop_frames_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100) &&
4598 (cpi
->projected_frame_size
> (4 * cpi
->this_frame_target
)))
4600 cpi
->drop_frame
= TRUE
;
4605 // Set the count for maximum consequative dropped frames based upon the ratio of
4606 // this frame size to the target average per frame bandwidth.
4607 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4608 if (cpi
->drop_frames_allowed
&& (cpi
->av_per_frame_bandwidth
> 0))
4610 cpi
->max_drop_count
= cpi
->projected_frame_size
/ cpi
->av_per_frame_bandwidth
;
4612 if (cpi
->max_drop_count
> cpi
->max_consec_dropped_frames
)
4613 cpi
->max_drop_count
= cpi
->max_consec_dropped_frames
;
4616 // Update the buffer level variable.
4617 if (cpi
->common
.refresh_alt_ref_frame
)
4618 cpi
->bits_off_target
-= cpi
->projected_frame_size
;
4620 cpi
->bits_off_target
+= cpi
->av_per_frame_bandwidth
- cpi
->projected_frame_size
;
4622 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4623 cpi
->rolling_target_bits
= ((cpi
->rolling_target_bits
* 3) + cpi
->this_frame_target
+ 2) / 4;
4624 cpi
->rolling_actual_bits
= ((cpi
->rolling_actual_bits
* 3) + cpi
->projected_frame_size
+ 2) / 4;
4625 cpi
->long_rolling_target_bits
= ((cpi
->long_rolling_target_bits
* 31) + cpi
->this_frame_target
+ 16) / 32;
4626 cpi
->long_rolling_actual_bits
= ((cpi
->long_rolling_actual_bits
* 31) + cpi
->projected_frame_size
+ 16) / 32;
4628 // Actual bits spent
4629 cpi
->total_actual_bits
+= cpi
->projected_frame_size
;
4632 cpi
->total_target_vs_actual
+= (cpi
->this_frame_target
- cpi
->projected_frame_size
);
4634 cpi
->buffer_level
= cpi
->bits_off_target
;
4636 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4637 if (cm
->frame_type
== KEY_FRAME
)
4639 cpi
->kf_group_bits
+= cpi
->this_frame_target
- cpi
->projected_frame_size
;
4641 if (cpi
->kf_group_bits
< 0)
4642 cpi
->kf_group_bits
= 0 ;
4644 else if (cm
->refresh_golden_frame
|| cm
->refresh_alt_ref_frame
)
4646 cpi
->gf_group_bits
+= cpi
->this_frame_target
- cpi
->projected_frame_size
;
4648 if (cpi
->gf_group_bits
< 0)
4649 cpi
->gf_group_bits
= 0 ;
4652 if (cm
->frame_type
!= KEY_FRAME
)
4654 if (cpi
->common
.refresh_alt_ref_frame
)
4656 cpi
->last_skip_false_probs
[2] = cpi
->prob_skip_false
;
4657 cpi
->last_skip_probs_q
[2] = cm
->base_qindex
;
4659 else if (cpi
->common
.refresh_golden_frame
)
4661 cpi
->last_skip_false_probs
[1] = cpi
->prob_skip_false
;
4662 cpi
->last_skip_probs_q
[1] = cm
->base_qindex
;
4666 cpi
->last_skip_false_probs
[0] = cpi
->prob_skip_false
;
4667 cpi
->last_skip_probs_q
[0] = cm
->base_qindex
;
4669 //update the baseline
4670 cpi
->base_skip_false_prob
[cm
->base_qindex
] = cpi
->prob_skip_false
;
4675 #if 0 && CONFIG_PSNR
4677 FILE *f
= fopen("tmp.stt", "a");
4679 vp8_clear_system_state(); //__asm emms;
4681 if (cpi
->total_coded_error_left
!= 0.0)
4682 fprintf(f
, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4683 "%6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4685 cpi
->common
.current_video_frame
, cpi
->this_frame_target
,
4686 cpi
->projected_frame_size
,
4687 (cpi
->projected_frame_size
- cpi
->this_frame_target
),
4688 (int)cpi
->total_target_vs_actual
,
4689 (cpi
->oxcf
.starting_buffer_level
-cpi
->bits_off_target
),
4690 (int)cpi
->total_actual_bits
, cm
->base_qindex
,
4691 cpi
->active_best_quality
, cpi
->active_worst_quality
,
4692 cpi
->ni_av_qi
, cpi
->cq_target_quality
, cpi
->zbin_over_quant
,
4693 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4694 cm
->refresh_golden_frame
, cm
->refresh_alt_ref_frame
,
4695 cm
->frame_type
, cpi
->gfu_boost
,
4696 cpi
->est_max_qcorrection_factor
, (int)cpi
->bits_left
,
4697 cpi
->total_coded_error_left
,
4698 (double)cpi
->bits_left
/ cpi
->total_coded_error_left
,
4699 cpi
->tot_recode_hits
);
4701 fprintf(f
, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4702 "%6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4704 cpi
->common
.current_video_frame
,
4705 cpi
->this_frame_target
, cpi
->projected_frame_size
,
4706 (cpi
->projected_frame_size
- cpi
->this_frame_target
),
4707 (int)cpi
->total_target_vs_actual
,
4708 (cpi
->oxcf
.starting_buffer_level
-cpi
->bits_off_target
),
4709 (int)cpi
->total_actual_bits
, cm
->base_qindex
,
4710 cpi
->active_best_quality
, cpi
->active_worst_quality
,
4711 cpi
->ni_av_qi
, cpi
->cq_target_quality
, cpi
->zbin_over_quant
,
4712 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4713 cm
->refresh_golden_frame
, cm
->refresh_alt_ref_frame
,
4714 cm
->frame_type
, cpi
->gfu_boost
,
4715 cpi
->est_max_qcorrection_factor
, (int)cpi
->bits_left
,
4716 cpi
->total_coded_error_left
, cpi
->tot_recode_hits
);
4721 FILE *fmodes
= fopen("Modes.stt", "a");
4724 fprintf(fmodes
, "%6d:%1d:%1d:%1d ",
4725 cpi
->common
.current_video_frame
,
4726 cm
->frame_type
, cm
->refresh_golden_frame
,
4727 cm
->refresh_alt_ref_frame
);
4729 for (i
= 0; i
< MAX_MODES
; i
++)
4730 fprintf(fmodes
, "%5d ", cpi
->mode_chosen_counts
[i
]);
4732 fprintf(fmodes
, "\n");
4740 // If this was a kf or Gf note the Q
4741 if ((cm
->frame_type
== KEY_FRAME
) || cm
->refresh_golden_frame
|| cm
->refresh_alt_ref_frame
)
4742 cm
->last_kf_gf_q
= cm
->base_qindex
;
4744 if (cm
->refresh_golden_frame
== 1)
4745 cm
->frame_flags
= cm
->frame_flags
| FRAMEFLAGS_GOLDEN
;
4747 cm
->frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_GOLDEN
;
4749 if (cm
->refresh_alt_ref_frame
== 1)
4750 cm
->frame_flags
= cm
->frame_flags
| FRAMEFLAGS_ALTREF
;
4752 cm
->frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_ALTREF
;
4755 if (cm
->refresh_last_frame
& cm
->refresh_golden_frame
) // both refreshed
4756 cpi
->gold_is_last
= 1;
4757 else if (cm
->refresh_last_frame
^ cm
->refresh_golden_frame
) // 1 refreshed but not the other
4758 cpi
->gold_is_last
= 0;
4760 if (cm
->refresh_last_frame
& cm
->refresh_alt_ref_frame
) // both refreshed
4761 cpi
->alt_is_last
= 1;
4762 else if (cm
->refresh_last_frame
^ cm
->refresh_alt_ref_frame
) // 1 refreshed but not the other
4763 cpi
->alt_is_last
= 0;
4765 if (cm
->refresh_alt_ref_frame
& cm
->refresh_golden_frame
) // both refreshed
4766 cpi
->gold_is_alt
= 1;
4767 else if (cm
->refresh_alt_ref_frame
^ cm
->refresh_golden_frame
) // 1 refreshed but not the other
4768 cpi
->gold_is_alt
= 0;
4770 cpi
->ref_frame_flags
= VP8_ALT_FLAG
| VP8_GOLD_FLAG
| VP8_LAST_FLAG
;
4772 if (cpi
->gold_is_last
)
4773 cpi
->ref_frame_flags
&= ~VP8_GOLD_FLAG
;
4775 if (cpi
->alt_is_last
)
4776 cpi
->ref_frame_flags
&= ~VP8_ALT_FLAG
;
4778 if (cpi
->gold_is_alt
)
4779 cpi
->ref_frame_flags
&= ~VP8_ALT_FLAG
;
4782 if (cpi
->oxcf
.error_resilient_mode
)
4784 // Is this an alternate reference update
4785 if (cpi
->common
.refresh_alt_ref_frame
)
4786 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->yv12_fb
[cm
->alt_fb_idx
]);
4788 if (cpi
->common
.refresh_golden_frame
)
4789 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cm
->yv12_fb
[cm
->gld_fb_idx
]);
4793 if (cpi
->oxcf
.play_alternate
&& cpi
->common
.refresh_alt_ref_frame
&& (cpi
->common
.frame_type
!= KEY_FRAME
))
4794 // Update the alternate reference frame and stats as appropriate.
4795 update_alt_ref_frame_and_stats(cpi
);
4797 // Update the Golden frame and golden frame and stats as appropriate.
4798 update_golden_frame_and_stats(cpi
);
4801 if (cm
->frame_type
== KEY_FRAME
)
4803 // Tell the caller that the frame was coded as a key frame
4804 *frame_flags
= cm
->frame_flags
| FRAMEFLAGS_KEY
;
4806 // As this frame is a key frame the next defaults to an inter frame.
4807 cm
->frame_type
= INTER_FRAME
;
4809 cpi
->last_frame_percent_intra
= 100;
4813 *frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_KEY
;
4815 cpi
->last_frame_percent_intra
= cpi
->this_frame_percent_intra
;
4818 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4819 cpi
->mb
.e_mbd
.update_mb_segmentation_map
= 0;
4820 cpi
->mb
.e_mbd
.update_mb_segmentation_data
= 0;
4821 cpi
->mb
.e_mbd
.mode_ref_lf_delta_update
= 0;
4824 // Dont increment frame counters if this was an altref buffer update not a real frame
4827 cm
->current_video_frame
++;
4828 cpi
->frames_since_key
++;
4831 // reset to normal state now that we are done.
4839 sprintf(filename
, "enc%04d.yuv", (int) cm
->current_video_frame
);
4840 recon_file
= fopen(filename
, "wb");
4841 fwrite(cm
->yv12_fb
[cm
->lst_fb_idx
].buffer_alloc
,
4842 cm
->yv12_fb
[cm
->lst_fb_idx
].frame_size
, 1, recon_file
);
4848 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4853 int vp8_is_gf_update_needed(VP8_PTR ptr
)
4855 VP8_COMP
*cpi
= (VP8_COMP
*) ptr
;
4858 ret_val
= cpi
->gf_update_recommended
;
4859 cpi
->gf_update_recommended
= 0;
4864 void vp8_check_gf_quality(VP8_COMP
*cpi
)
4866 VP8_COMMON
*cm
= &cpi
->common
;
4867 int gf_active_pct
= (100 * cpi
->gf_active_count
) / (cm
->mb_rows
* cm
->mb_cols
);
4868 int gf_ref_usage_pct
= (cpi
->count_mb_ref_frame_usage
[GOLDEN_FRAME
] * 100) / (cm
->mb_rows
* cm
->mb_cols
);
4869 int last_ref_zz_useage
= (cpi
->inter_zz_count
* 100) / (cm
->mb_rows
* cm
->mb_cols
);
4871 // Gf refresh is not currently being signalled
4872 if (cpi
->gf_update_recommended
== 0)
4874 if (cpi
->common
.frames_since_golden
> 7)
4877 if ((gf_active_pct
< 10) || ((gf_active_pct
+ gf_ref_usage_pct
) < 15))
4879 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4880 if (last_ref_zz_useage
>= 25)
4882 cpi
->gf_bad_count
++;
4884 if (cpi
->gf_bad_count
>= 8) // Check that the condition is stable
4886 cpi
->gf_update_recommended
= 1;
4887 cpi
->gf_bad_count
= 0;
4891 cpi
->gf_bad_count
= 0; // Restart count as the background is not stable enough
4894 cpi
->gf_bad_count
= 0; // Gf useage has picked up so reset count
4897 // If the signal is set but has not been read should we cancel it.
4898 else if (last_ref_zz_useage
< 15)
4900 cpi
->gf_update_recommended
= 0;
4901 cpi
->gf_bad_count
= 0;
4906 FILE *f
= fopen("gfneeded.stt", "a");
4907 fprintf(f
, "%10d %10d %10d %10d %10ld \n",
4908 cm
->current_video_frame
,
4909 cpi
->common
.frames_since_golden
,
4910 gf_active_pct
, gf_ref_usage_pct
,
4911 cpi
->gf_update_recommended
);
4918 #if !(CONFIG_REALTIME_ONLY)
4919 static void Pass2Encode(VP8_COMP
*cpi
, unsigned long *size
, unsigned char *dest
, unsigned int *frame_flags
)
4922 if (!cpi
->common
.refresh_alt_ref_frame
)
4923 vp8_second_pass(cpi
);
4925 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
4926 cpi
->bits_left
-= 8 * *size
;
4928 if (!cpi
->common
.refresh_alt_ref_frame
)
4930 double two_pass_min_rate
= (double)(cpi
->oxcf
.target_bandwidth
4931 *cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
4932 cpi
->bits_left
+= (long long)(two_pass_min_rate
/ cpi
->oxcf
.frame_rate
);
4937 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4939 extern void vp8_push_neon(INT64
*store
);
4940 extern void vp8_pop_neon(INT64
*store
);
4942 int vp8_receive_raw_frame(VP8_PTR ptr
, unsigned int frame_flags
, YV12_BUFFER_CONFIG
*sd
, INT64 time_stamp
, INT64 end_time
)
4947 VP8_COMP
*cpi
= (VP8_COMP
*) ptr
;
4948 VP8_COMMON
*cm
= &cpi
->common
;
4949 struct vpx_usec_timer timer
;
4955 #if CONFIG_RUNTIME_CPU_DETECT
4956 if (cm
->rtcd
.flags
& HAS_NEON
)
4959 vp8_push_neon(store_reg
);
4963 vpx_usec_timer_start(&timer
);
4965 // no more room for frames;
4966 if (cpi
->source_buffer_count
!= 0 && cpi
->source_buffer_count
>= cpi
->oxcf
.lag_in_frames
)
4969 #if CONFIG_RUNTIME_CPU_DETECT
4970 if (cm
->rtcd
.flags
& HAS_NEON
)
4973 vp8_pop_neon(store_reg
);
4979 //printf("in-cpi->source_buffer_count: %d\n", cpi->source_buffer_count);
4981 cm
->clr_type
= sd
->clrtype
;
4983 // make a copy of the frame for use later...
4984 #if !(CONFIG_REALTIME_ONLY)
4986 if (cpi
->oxcf
.allow_lag
)
4988 int which_buffer
= cpi
->source_encode_index
- 1;
4991 if (which_buffer
== -1)
4992 which_buffer
= cpi
->oxcf
.lag_in_frames
- 1;
4994 if (cpi
->source_buffer_count
< cpi
->oxcf
.lag_in_frames
- 1)
4995 which_buffer
= cpi
->source_buffer_count
;
4997 s
= &cpi
->src_buffer
[which_buffer
];
4999 s
->source_time_stamp
= time_stamp
;
5000 s
->source_end_time_stamp
= end_time
;
5001 s
->source_frame_flags
= frame_flags
;
5002 vp8_yv12_copy_frame_ptr(sd
, &s
->source_buffer
);
5004 cpi
->source_buffer_count
++;
5010 s
= &cpi
->src_buffer
[0];
5011 s
->source_end_time_stamp
= end_time
;
5012 s
->source_time_stamp
= time_stamp
;
5013 s
->source_frame_flags
= frame_flags
;
5015 #if CONFIG_RUNTIME_CPU_DETECT
5016 if (cm
->rtcd
.flags
& HAS_NEON
)
5019 vp8_yv12_copy_src_frame_func_neon(sd
, &s
->source_buffer
);
5021 #if CONFIG_RUNTIME_CPU_DETECT
5025 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
5027 vp8_yv12_copy_frame_ptr(sd
, &s
->source_buffer
);
5030 cpi
->source_buffer_count
= 1;
5033 vpx_usec_timer_mark(&timer
);
5034 cpi
->time_receive_data
+= vpx_usec_timer_elapsed(&timer
);
5037 #if CONFIG_RUNTIME_CPU_DETECT
5038 if (cm
->rtcd
.flags
& HAS_NEON
)
5041 vp8_pop_neon(store_reg
);
5047 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
)
5052 VP8_COMP
*cpi
= (VP8_COMP
*) ptr
;
5053 VP8_COMMON
*cm
= &cpi
->common
;
5054 struct vpx_usec_timer tsctimer
;
5055 struct vpx_usec_timer ticktimer
;
5056 struct vpx_usec_timer cmptimer
;
5062 #if CONFIG_RUNTIME_CPU_DETECT
5063 if (cm
->rtcd
.flags
& HAS_NEON
)
5066 vp8_push_neon(store_reg
);
5070 vpx_usec_timer_start(&cmptimer
);
5073 // flush variable tells us that even though we have less than 10 frames
5074 // in our buffer we need to start producing compressed frames.
5075 // Probably because we are at the end of a file....
5076 if ((cpi
->source_buffer_count
== cpi
->oxcf
.lag_in_frames
&& cpi
->oxcf
.lag_in_frames
> 0)
5077 || (!cpi
->oxcf
.allow_lag
&& cpi
->source_buffer_count
> 0)
5078 || (flush
&& cpi
->source_buffer_count
> 0))
5083 s
= &cpi
->src_buffer
[cpi
->source_encode_index
];
5084 cpi
->source_time_stamp
= s
->source_time_stamp
;
5085 cpi
->source_end_time_stamp
= s
->source_end_time_stamp
;
5087 #if !(CONFIG_REALTIME_ONLY)
5089 // Should we code an alternate reference frame
5090 if (cpi
->oxcf
.error_resilient_mode
== 0 &&
5091 cpi
->oxcf
.play_alternate
&&
5092 cpi
->source_alt_ref_pending
&&
5093 (cpi
->frames_till_gf_update_due
< cpi
->source_buffer_count
) &&
5094 cpi
->oxcf
.lag_in_frames
!= 0)
5096 cpi
->last_alt_ref_sei
= (cpi
->source_encode_index
+ cpi
->frames_till_gf_update_due
) % cpi
->oxcf
.lag_in_frames
;
5098 #if VP8_TEMPORAL_ALT_REF
5100 if (cpi
->oxcf
.arnr_max_frames
> 0)
5103 // my attempt at a loop that tests the results of strength filter.
5104 int start_frame
= cpi
->last_alt_ref_sei
- 3;
5106 int i
, besti
= -1, pastin
= cpi
->oxcf
.arnr_strength
;
5110 if (start_frame
< 0)
5111 start_frame
+= cpi
->oxcf
.lag_in_frames
;
5113 besterr
= vp8_calc_low_ss_err(&cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_buffer
,
5114 &cpi
->src_buffer
[start_frame
].source_buffer
, IF_RTCD(&cpi
->rtcd
.variance
));
5116 for (i
= 0; i
< 7; i
++)
5119 cpi
->oxcf
.arnr_strength
= i
;
5120 vp8_temporal_filter_prepare_c(cpi
);
5122 thiserr
= vp8_calc_low_ss_err(&cpi
->alt_ref_buffer
.source_buffer
,
5123 &cpi
->src_buffer
[start_frame
].source_buffer
, IF_RTCD(&cpi
->rtcd
.variance
));
5125 if (10 * thiserr
< besterr
* 8)
5134 cpi
->oxcf
.arnr_strength
= besti
;
5135 vp8_temporal_filter_prepare_c(cpi
);
5136 s
= &cpi
->alt_ref_buffer
;
5138 // FWG not sure if I need to copy this data for the Alt Ref frame
5139 s
->source_time_stamp
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_time_stamp
;
5140 s
->source_end_time_stamp
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_end_time_stamp
;
5141 s
->source_frame_flags
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_frame_flags
;
5144 s
= &cpi
->src_buffer
[cpi
->last_alt_ref_sei
];
5147 vp8_temporal_filter_prepare_c(cpi
);
5148 s
= &cpi
->alt_ref_buffer
;
5150 // FWG not sure if I need to copy this data for the Alt Ref frame
5151 s
->source_time_stamp
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_time_stamp
;
5152 s
->source_end_time_stamp
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_end_time_stamp
;
5153 s
->source_frame_flags
= cpi
->src_buffer
[cpi
->last_alt_ref_sei
].source_frame_flags
;
5159 s
= &cpi
->src_buffer
[cpi
->last_alt_ref_sei
];
5161 cm
->frames_till_alt_ref_frame
= cpi
->frames_till_gf_update_due
;
5162 cm
->refresh_alt_ref_frame
= 1;
5163 cm
->refresh_golden_frame
= 0;
5164 cm
->refresh_last_frame
= 0;
5166 cpi
->source_alt_ref_pending
= FALSE
; // Clear Pending altf Ref flag.
5167 cpi
->is_src_frame_alt_ref
= 0;
5168 cpi
->is_next_src_alt_ref
= 0;
5174 #if !(CONFIG_REALTIME_ONLY)
5176 if (cpi
->oxcf
.allow_lag
)
5178 if (cpi
->source_encode_index
== cpi
->last_alt_ref_sei
)
5180 cpi
->is_src_frame_alt_ref
= 1;
5181 cpi
->last_alt_ref_sei
= -1;
5184 cpi
->is_src_frame_alt_ref
= 0;
5186 cpi
->source_encode_index
= (cpi
->source_encode_index
+ 1) % cpi
->oxcf
.lag_in_frames
;
5188 if(cpi
->source_encode_index
== cpi
->last_alt_ref_sei
)
5189 cpi
->is_next_src_alt_ref
= 1;
5191 cpi
->is_next_src_alt_ref
= 0;
5195 cpi
->source_buffer_count
--;
5198 cpi
->un_scaled_source
= &s
->source_buffer
;
5199 cpi
->Source
= &s
->source_buffer
;
5200 cpi
->source_frame_flags
= s
->source_frame_flags
;
5202 *time_stamp
= cpi
->source_time_stamp
;
5203 *time_end
= cpi
->source_end_time_stamp
;
5208 #if !(CONFIG_REALTIME_ONLY)
5210 if (flush
&& cpi
->pass
== 1 && !cpi
->first_pass_done
)
5212 vp8_end_first_pass(cpi
); /* get last stats packet */
5213 cpi
->first_pass_done
= 1;
5219 #if CONFIG_RUNTIME_CPU_DETECT
5220 if (cm
->rtcd
.flags
& HAS_NEON
)
5223 vp8_pop_neon(store_reg
);
5229 *frame_flags
= cpi
->source_frame_flags
;
5231 if (cpi
->source_time_stamp
< cpi
->first_time_stamp_ever
)
5233 cpi
->first_time_stamp_ever
= cpi
->source_time_stamp
;
5234 cpi
->last_end_time_stamp_seen
= cpi
->source_time_stamp
;
5237 // adjust frame rates based on timestamps given
5238 if (!cm
->refresh_alt_ref_frame
)
5240 if (cpi
->source_time_stamp
== cpi
->first_time_stamp_ever
)
5242 double this_fps
= 10000000.000 / (cpi
->source_end_time_stamp
- cpi
->source_time_stamp
);
5244 vp8_new_frame_rate(cpi
, this_fps
);
5248 long long nanosecs
= cpi
->source_end_time_stamp
5249 - cpi
->last_end_time_stamp_seen
;
5250 double this_fps
= 10000000.000 / nanosecs
;
5252 vp8_new_frame_rate(cpi
, (7 * cpi
->oxcf
.frame_rate
+ this_fps
) / 8);
5256 cpi
->last_time_stamp_seen
= cpi
->source_time_stamp
;
5257 cpi
->last_end_time_stamp_seen
= cpi
->source_end_time_stamp
;
5260 if (cpi
->compressor_speed
== 2)
5262 vp8_check_gf_quality(cpi
);
5268 #if CONFIG_RUNTIME_CPU_DETECT
5269 if (cm
->rtcd
.flags
& HAS_NEON
)
5272 vp8_pop_neon(store_reg
);
5278 if (cpi
->compressor_speed
== 2)
5280 vpx_usec_timer_start(&tsctimer
);
5281 vpx_usec_timer_start(&ticktimer
);
5284 // start with a 0 size frame
5287 // Clear down mmx registers
5288 vp8_clear_system_state(); //__asm emms;
5290 cm
->frame_type
= INTER_FRAME
;
5291 cm
->frame_flags
= *frame_flags
;
5295 if (cm
->refresh_alt_ref_frame
)
5297 //cm->refresh_golden_frame = 1;
5298 cm
->refresh_golden_frame
= 0;
5299 cm
->refresh_last_frame
= 0;
5303 cm
->refresh_golden_frame
= 0;
5304 cm
->refresh_last_frame
= 1;
5309 #if !(CONFIG_REALTIME_ONLY)
5313 Pass1Encode(cpi
, size
, dest
, frame_flags
);
5315 else if (cpi
->pass
== 2)
5317 Pass2Encode(cpi
, size
, dest
, frame_flags
);
5321 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
5323 if (cpi
->compressor_speed
== 2)
5325 unsigned int duration
, duration2
;
5326 vpx_usec_timer_mark(&tsctimer
);
5327 vpx_usec_timer_mark(&ticktimer
);
5329 duration
= vpx_usec_timer_elapsed(&ticktimer
);
5330 duration2
= (unsigned int)((double)duration
/ 2);
5332 if (cm
->frame_type
!= KEY_FRAME
)
5334 if (cpi
->avg_encode_time
== 0)
5335 cpi
->avg_encode_time
= duration
;
5337 cpi
->avg_encode_time
= (7 * cpi
->avg_encode_time
+ duration
) >> 3;
5342 //if(*frame_flags!=1)
5345 if (cpi
->avg_pick_mode_time
== 0)
5346 cpi
->avg_pick_mode_time
= duration2
;
5348 cpi
->avg_pick_mode_time
= (7 * cpi
->avg_pick_mode_time
+ duration2
) >> 3;
5354 if (cm
->refresh_entropy_probs
== 0)
5356 vpx_memcpy(&cm
->fc
, &cm
->lfc
, sizeof(cm
->fc
));
5359 // if its a dropped frame honor the requests on subsequent frames
5363 // return to normal state
5364 cm
->refresh_entropy_probs
= 1;
5365 cm
->refresh_alt_ref_frame
= 0;
5366 cm
->refresh_golden_frame
= 0;
5367 cm
->refresh_last_frame
= 1;
5368 cm
->frame_type
= INTER_FRAME
;
5372 cpi
->ready_for_new_frame
= 1;
5374 vpx_usec_timer_mark(&cmptimer
);
5375 cpi
->time_compress_data
+= vpx_usec_timer_elapsed(&cmptimer
);
5377 if (cpi
->b_calculate_psnr
&& cpi
->pass
!= 1 && cm
->show_frame
)
5378 generate_psnr_packet(cpi
);
5384 cpi
->bytes
+= *size
;
5391 if (cpi
->b_calculate_psnr
)
5395 double frame_psnr
= vp8_calc_psnr(cpi
->Source
, cm
->frame_to_show
, &y
, &u
, &v
, &sq_error
);
5400 cpi
->total_sq_error
+= sq_error
;
5401 cpi
->total
+= frame_psnr
;
5403 double y2
, u2
, v2
, frame_psnr2
, frame_ssim2
= 0;
5406 vp8_deblock(cm
->frame_to_show
, &cm
->post_proc_buffer
, cm
->filter_level
* 10 / 6, 1, 0, IF_RTCD(&cm
->rtcd
.postproc
));
5407 vp8_clear_system_state();
5408 frame_psnr2
= vp8_calc_psnr(cpi
->Source
, &cm
->post_proc_buffer
, &y2
, &u2
, &v2
, &sq_error
);
5409 frame_ssim2
= vp8_calc_ssim(cpi
->Source
, &cm
->post_proc_buffer
, 1, &weight
);
5411 cpi
->summed_quality
+= frame_ssim2
* weight
;
5412 cpi
->summed_weights
+= weight
;
5414 cpi
->totalp_y
+= y2
;
5415 cpi
->totalp_u
+= u2
;
5416 cpi
->totalp_v
+= v2
;
5417 cpi
->totalp
+= frame_psnr2
;
5418 cpi
->total_sq_error2
+= sq_error
;
5423 if (cpi
->b_calculate_ssimg
)
5425 double y
, u
, v
, frame_all
;
5426 frame_all
= vp8_calc_ssimg(cpi
->Source
, cm
->frame_to_show
, &y
, &u
, &v
);
5427 cpi
->total_ssimg_y
+= y
;
5428 cpi
->total_ssimg_u
+= u
;
5429 cpi
->total_ssimg_v
+= v
;
5430 cpi
->total_ssimg_all
+= frame_all
;
5438 if (cpi
->common
.frame_type
!= 0 && cpi
->common
.base_qindex
== cpi
->oxcf
.worst_allowed_q
)
5440 skiptruecount
+= cpi
->skip_true_count
;
5441 skipfalsecount
+= cpi
->skip_false_count
;
5449 FILE *f
= fopen("skip.stt", "a");
5450 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
);
5452 if (cpi
->is_src_frame_alt_ref
== 1)
5453 fprintf(f
, "skipcount: %4d framesize: %d\n", cpi
->skip_true_count
, *size
);
5462 #if CONFIG_RUNTIME_CPU_DETECT
5463 if (cm
->rtcd
.flags
& HAS_NEON
)
5466 vp8_pop_neon(store_reg
);
5473 int vp8_get_preview_raw_frame(VP8_PTR comp
, YV12_BUFFER_CONFIG
*dest
, vp8_ppflags_t
*flags
)
5475 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
5477 if (cpi
->common
.refresh_alt_ref_frame
)
5483 ret
= vp8_post_proc_frame(&cpi
->common
, dest
, flags
);
5486 if (cpi
->common
.frame_to_show
)
5488 *dest
= *cpi
->common
.frame_to_show
;
5489 dest
->y_width
= cpi
->common
.Width
;
5490 dest
->y_height
= cpi
->common
.Height
;
5491 dest
->uv_height
= cpi
->common
.Height
/ 2;
5499 #endif //!CONFIG_POSTPROC
5500 vp8_clear_system_state();
5505 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])
5507 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
5508 signed char feature_data
[MB_LVL_MAX
][MAX_MB_SEGMENTS
];
5510 if (cpi
->common
.mb_rows
!= rows
|| cpi
->common
.mb_cols
!= cols
)
5515 disable_segmentation((VP8_PTR
)cpi
);
5519 // Set the segmentation Map
5520 set_segmentation_map((VP8_PTR
)cpi
, map
);
5522 // Activate segmentation.
5523 enable_segmentation((VP8_PTR
)cpi
);
5525 // Set up the quant segment data
5526 feature_data
[MB_LVL_ALT_Q
][0] = delta_q
[0];
5527 feature_data
[MB_LVL_ALT_Q
][1] = delta_q
[1];
5528 feature_data
[MB_LVL_ALT_Q
][2] = delta_q
[2];
5529 feature_data
[MB_LVL_ALT_Q
][3] = delta_q
[3];
5531 // Set up the loop segment data s
5532 feature_data
[MB_LVL_ALT_LF
][0] = delta_lf
[0];
5533 feature_data
[MB_LVL_ALT_LF
][1] = delta_lf
[1];
5534 feature_data
[MB_LVL_ALT_LF
][2] = delta_lf
[2];
5535 feature_data
[MB_LVL_ALT_LF
][3] = delta_lf
[3];
5537 cpi
->segment_encode_breakout
[0] = threshold
[0];
5538 cpi
->segment_encode_breakout
[1] = threshold
[1];
5539 cpi
->segment_encode_breakout
[2] = threshold
[2];
5540 cpi
->segment_encode_breakout
[3] = threshold
[3];
5542 // Initialise the feature data structure
5543 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5544 set_segment_data((VP8_PTR
)cpi
, &feature_data
[0][0], SEGMENT_DELTADATA
);
5549 int vp8_set_active_map(VP8_PTR comp
, unsigned char *map
, unsigned int rows
, unsigned int cols
)
5551 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
5553 if (rows
== cpi
->common
.mb_rows
&& cols
== cpi
->common
.mb_cols
)
5557 vpx_memcpy(cpi
->active_map
, map
, rows
* cols
);
5558 cpi
->active_map_enabled
= 1;
5561 cpi
->active_map_enabled
= 0;
5567 //cpi->active_map_enabled = 0;
5572 int vp8_set_internal_size(VP8_PTR comp
, VPX_SCALING horiz_mode
, VPX_SCALING vert_mode
)
5574 VP8_COMP
*cpi
= (VP8_COMP
*) comp
;
5576 if (horiz_mode
<= ONETWO
)
5577 cpi
->common
.horiz_scale
= horiz_mode
;
5581 if (vert_mode
<= ONETWO
)
5582 cpi
->common
.vert_scale
= vert_mode
;
5591 int vp8_calc_ss_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, const vp8_variance_rtcd_vtable_t
*rtcd
)
5596 unsigned char *src
= source
->y_buffer
;
5597 unsigned char *dst
= dest
->y_buffer
;
5600 // Loop through the Y plane raw and reconstruction data summing (square differences)
5601 for (i
= 0; i
< source
->y_height
; i
+= 16)
5603 for (j
= 0; j
< source
->y_width
; j
+= 16)
5606 Total
+= VARIANCE_INVOKE(rtcd
, mse16x16
)(src
+ j
, source
->y_stride
, dst
+ j
, dest
->y_stride
, &sse
);
5609 src
+= 16 * source
->y_stride
;
5610 dst
+= 16 * dest
->y_stride
;
5615 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, const vp8_variance_rtcd_vtable_t
*rtcd
)
5620 unsigned char *src
= source
->y_buffer
;
5621 unsigned char *dst
= dest
->y_buffer
;
5624 // Loop through the Y plane raw and reconstruction data summing (square differences)
5625 for (i
= 0; i
< source
->y_height
; i
+= 16)
5627 for (j
= 0; j
< source
->y_width
; j
+= 16)
5630 VARIANCE_INVOKE(rtcd
, mse16x16
)(src
+ j
, source
->y_stride
, dst
+ j
, dest
->y_stride
, &sse
);
5636 src
+= 16 * source
->y_stride
;
5637 dst
+= 16 * dest
->y_stride
;
5643 int vp8_get_speed(VP8_PTR c
)
5645 VP8_COMP
*cpi
= (VP8_COMP
*) c
;
5648 int vp8_get_quantizer(VP8_PTR c
)
5650 VP8_COMP
*cpi
= (VP8_COMP
*) c
;
5651 return cpi
->common
.base_qindex
;