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

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

Issue 11555023: libvpx: Add VP9 decoder. (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11
12 #include "vpx_config.h"
13 #include "vp9/common/vp9_onyxc_int.h"
14 #include "vp9/encoder/vp9_onyx_int.h"
15 #include "vp9/common/vp9_systemdependent.h"
16 #include "vp9/encoder/vp9_quantize.h"
17 #include "vp9/common/vp9_alloccommon.h"
18 #include "vp9/encoder/vp9_mcomp.h"
19 #include "vp9/encoder/vp9_firstpass.h"
20 #include "vp9/encoder/vp9_psnr.h"
21 #include "vpx_scale/vpxscale.h"
22 #include "vp9/common/vp9_extend.h"
23 #include "vp9/encoder/vp9_ratectrl.h"
24 #include "vp9/common/vp9_quant_common.h"
25 #include "vp9/encoder/vp9_segmentation.h"
26 #include "./vp9_rtcd.h"
27 #include "./vpx_scale_rtcd.h"
28 #if CONFIG_POSTPROC
29 #include "vp9/common/vp9_postproc.h"
30 #endif
31 #include "vpx_mem/vpx_mem.h"
32 #include "vp9/common/vp9_swapyv12buffer.h"
33 #include "vpx_ports/vpx_timer.h"
34
35 #include "vp9/common/vp9_seg_common.h"
36 #include "vp9/encoder/vp9_mbgraph.h"
37 #include "vp9/common/vp9_pred_common.h"
38 #include "vp9/encoder/vp9_rdopt.h"
39 #include "vp9/encoder/vp9_bitstream.h"
40 #include "vp9/encoder/vp9_picklpf.h"
41 #include "vp9/common/vp9_mvref_common.h"
42 #include "vp9/encoder/vp9_temporal_filter.h"
43
44 #include <math.h>
45 #include <stdio.h>
46 #include <limits.h>
47
48 extern void print_tree_update_probs();
49
50 static void set_default_lf_deltas(VP9_COMP *cpi);
51
52 #define DEFAULT_INTERP_FILTER EIGHTTAP /* SWITCHABLE for better performance */
53 #define SEARCH_BEST_FILTER 0 /* to search exhaustively for
54 best filter */
55 #define RESET_FOREACH_FILTER 0 /* whether to reset the encoder state
56 before trying each new filter */
57 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
58
59 #define ALTREF_HIGH_PRECISION_MV 1 /* whether to use high precision mv
60 for altref computation */
61 #define HIGH_PRECISION_MV_QTHRESH 200 /* Q threshold for use of high precision
62 mv. Choose a very high value for
63 now so that HIGH_PRECISION is always
64 chosen */
65
66 #if CONFIG_INTERNAL_STATS
67 #include "math.h"
68
69 extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
70 YV12_BUFFER_CONFIG *dest, int lumamask,
71 double *weight);
72
73
74 extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source,
75 YV12_BUFFER_CONFIG *dest, double *ssim_y,
76 double *ssim_u, double *ssim_v);
77
78
79 #endif
80
81 // #define OUTPUT_YUV_REC
82
83 #ifdef OUTPUT_YUV_SRC
84 FILE *yuv_file;
85 #endif
86 #ifdef OUTPUT_YUV_REC
87 FILE *yuv_rec_file;
88 #endif
89
90 #if 0
91 FILE *framepsnr;
92 FILE *kf_list;
93 FILE *keyfile;
94 #endif
95
96 #if 0
97 extern int skip_true_count;
98 extern int skip_false_count;
99 #endif
100
101
102 #ifdef ENTROPY_STATS
103 extern int intra_mode_stats[VP9_KF_BINTRAMODES]
104 [VP9_KF_BINTRAMODES]
105 [VP9_KF_BINTRAMODES];
106 #endif
107
108 #ifdef NMV_STATS
109 extern void init_nmvstats();
110 extern void print_nmvstats();
111 #endif
112
113 #ifdef SPEEDSTATS
114 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
115 #endif
116
117 #if defined(SECTIONBITS_OUTPUT)
118 extern unsigned __int64 Sectionbits[500];
119 #endif
120 #ifdef MODE_STATS
121 extern INT64 Sectionbits[500];
122 extern unsigned int y_modes[VP9_YMODES];
123 extern unsigned int i8x8_modes[VP9_I8X8_MODES];
124 extern unsigned int uv_modes[VP9_UV_MODES];
125 extern unsigned int uv_modes_y[VP9_YMODES][VP9_UV_MODES];
126 extern unsigned int b_modes[B_MODE_COUNT];
127 extern unsigned int inter_y_modes[MB_MODE_COUNT];
128 extern unsigned int inter_uv_modes[VP9_UV_MODES];
129 extern unsigned int inter_b_modes[B_MODE_COUNT];
130 #endif
131
132 extern void vp9_init_quantizer(VP9_COMP *cpi);
133
134 static int base_skip_false_prob[QINDEX_RANGE][3];
135
136 // Tables relating active max Q to active min Q
137 static int kf_low_motion_minq[QINDEX_RANGE];
138 static int kf_high_motion_minq[QINDEX_RANGE];
139 static int gf_low_motion_minq[QINDEX_RANGE];
140 static int gf_high_motion_minq[QINDEX_RANGE];
141 static int inter_minq[QINDEX_RANGE];
142
143 // Functions to compute the active minq lookup table entries based on a
144 // formulaic approach to facilitate easier adjustment of the Q tables.
145 // The formulae were derived from computing a 3rd order polynomial best
146 // fit to the original data (after plotting real maxq vs minq (not q index))
147 static int calculate_minq_index(double maxq,
148 double x3, double x2, double x, double c) {
149 int i;
150 double minqtarget;
151 double thisq;
152
153 minqtarget = ((x3 * maxq * maxq * maxq) +
154 (x2 * maxq * maxq) +
155 (x * maxq) +
156 c);
157
158 if (minqtarget > maxq)
159 minqtarget = maxq;
160
161 for (i = 0; i < QINDEX_RANGE; i++) {
162 thisq = vp9_convert_qindex_to_q(i);
163 if (minqtarget <= vp9_convert_qindex_to_q(i))
164 return i;
165 }
166 return QINDEX_RANGE - 1;
167 }
168
169 static void init_minq_luts(void) {
170 int i;
171 double maxq;
172
173 for (i = 0; i < QINDEX_RANGE; i++) {
174 maxq = vp9_convert_qindex_to_q(i);
175
176
177 kf_low_motion_minq[i] = calculate_minq_index(maxq,
178 0.0000003,
179 -0.000015,
180 0.074,
181 0.0);
182 kf_high_motion_minq[i] = calculate_minq_index(maxq,
183 0.0000004,
184 -0.000125,
185 0.14,
186 0.0);
187 gf_low_motion_minq[i] = calculate_minq_index(maxq,
188 0.0000015,
189 -0.0009,
190 0.33,
191 0.0);
192 gf_high_motion_minq[i] = calculate_minq_index(maxq,
193 0.0000021,
194 -0.00125,
195 0.45,
196 0.0);
197 inter_minq[i] = calculate_minq_index(maxq,
198 0.00000271,
199 -0.00113,
200 0.697,
201 0.0);
202
203 }
204 }
205
206 static void set_mvcost(MACROBLOCK *mb) {
207 if (mb->e_mbd.allow_high_precision_mv) {
208 mb->mvcost = mb->nmvcost_hp;
209 mb->mvsadcost = mb->nmvsadcost_hp;
210
211 } else {
212 mb->mvcost = mb->nmvcost;
213 mb->mvsadcost = mb->nmvsadcost;
214 }
215 }
216 static void init_base_skip_probs(void) {
217 int i;
218 double q;
219 int skip_prob, t;
220
221 for (i = 0; i < QINDEX_RANGE; i++) {
222 q = vp9_convert_qindex_to_q(i);
223
224 // Exponential decay caluclation of baseline skip prob with clamping
225 // Based on crude best fit of old table.
226 t = (int)(564.25 * pow(2.71828, (-0.012 * q)));
227
228 skip_prob = t;
229 if (skip_prob < 1)
230 skip_prob = 1;
231 else if (skip_prob > 255)
232 skip_prob = 255;
233 base_skip_false_prob[i][1] = skip_prob;
234
235 skip_prob = t * 3 / 4;
236 if (skip_prob < 1)
237 skip_prob = 1;
238 else if (skip_prob > 255)
239 skip_prob = 255;
240 base_skip_false_prob[i][2] = skip_prob;
241
242 skip_prob = t * 5 / 4;
243 if (skip_prob < 1)
244 skip_prob = 1;
245 else if (skip_prob > 255)
246 skip_prob = 255;
247 base_skip_false_prob[i][0] = skip_prob;
248 }
249 }
250
251 static void update_base_skip_probs(VP9_COMP *cpi) {
252 VP9_COMMON *cm = &cpi->common;
253
254 if (cm->frame_type != KEY_FRAME) {
255 vp9_update_skip_probs(cpi);
256
257 if (cm->refresh_alt_ref_frame) {
258 int k;
259 for (k = 0; k < MBSKIP_CONTEXTS; ++k)
260 cpi->last_skip_false_probs[2][k] = cm->mbskip_pred_probs[k];
261 cpi->last_skip_probs_q[2] = cm->base_qindex;
262 } else if (cpi->common.refresh_golden_frame) {
263 int k;
264 for (k = 0; k < MBSKIP_CONTEXTS; ++k)
265 cpi->last_skip_false_probs[1][k] = cm->mbskip_pred_probs[k];
266 cpi->last_skip_probs_q[1] = cm->base_qindex;
267 } else {
268 int k;
269 for (k = 0; k < MBSKIP_CONTEXTS; ++k)
270 cpi->last_skip_false_probs[0][k] = cm->mbskip_pred_probs[k];
271 cpi->last_skip_probs_q[0] = cm->base_qindex;
272
273 // update the baseline table for the current q
274 for (k = 0; k < MBSKIP_CONTEXTS; ++k)
275 cpi->base_skip_false_prob[cm->base_qindex][k] =
276 cm->mbskip_pred_probs[k];
277 }
278 }
279
280 }
281
282 void vp9_initialize_enc() {
283 static int init_done = 0;
284
285 if (!init_done) {
286 vp9_initialize_common();
287 vp9_tokenize_initialize();
288 vp9_init_quant_tables();
289 vp9_init_me_luts();
290 init_minq_luts();
291 init_base_skip_probs();
292 init_done = 1;
293 }
294 }
295 #ifdef PACKET_TESTING
296 extern FILE *vpxlogc;
297 #endif
298
299 static void setup_features(VP9_COMP *cpi) {
300 MACROBLOCKD *xd = &cpi->mb.e_mbd;
301
302 // Set up default state for MB feature flags
303
304 xd->segmentation_enabled = 0; // Default segmentation disabled
305
306 xd->update_mb_segmentation_map = 0;
307 xd->update_mb_segmentation_data = 0;
308 vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
309
310 vp9_clearall_segfeatures(xd);
311
312 xd->mode_ref_lf_delta_enabled = 0;
313 xd->mode_ref_lf_delta_update = 0;
314 vpx_memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
315 vpx_memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
316 vpx_memset(xd->last_ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
317 vpx_memset(xd->last_mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
318
319 set_default_lf_deltas(cpi);
320
321 }
322
323
324 static void dealloc_compressor_data(VP9_COMP *cpi) {
325 vpx_free(cpi->tplist);
326 cpi->tplist = NULL;
327
328 // Delete last frame MV storage buffers
329 vpx_free(cpi->lfmv);
330 cpi->lfmv = 0;
331
332 vpx_free(cpi->lf_ref_frame_sign_bias);
333 cpi->lf_ref_frame_sign_bias = 0;
334
335 vpx_free(cpi->lf_ref_frame);
336 cpi->lf_ref_frame = 0;
337
338 // Delete sementation map
339 vpx_free(cpi->segmentation_map);
340 cpi->segmentation_map = 0;
341 vpx_free(cpi->common.last_frame_seg_map);
342 cpi->common.last_frame_seg_map = 0;
343 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
344 cpi->coding_context.last_frame_seg_map_copy = 0;
345
346 vpx_free(cpi->active_map);
347 cpi->active_map = 0;
348
349 vp9_de_alloc_frame_buffers(&cpi->common);
350
351 vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
352 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
353 #if VP9_TEMPORAL_ALT_REF
354 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
355 #endif
356 vp9_lookahead_destroy(cpi->lookahead);
357
358 vpx_free(cpi->tok);
359 cpi->tok = 0;
360
361 // Structure used to monitor GF usage
362 vpx_free(cpi->gf_active_flags);
363 cpi->gf_active_flags = 0;
364
365 // Activity mask based per mb zbin adjustments
366 vpx_free(cpi->mb_activity_map);
367 cpi->mb_activity_map = 0;
368 vpx_free(cpi->mb_norm_activity_map);
369 cpi->mb_norm_activity_map = 0;
370
371 vpx_free(cpi->mb.pip);
372 cpi->mb.pip = 0;
373
374 vpx_free(cpi->twopass.total_stats);
375 cpi->twopass.total_stats = 0;
376
377 vpx_free(cpi->twopass.total_left_stats);
378 cpi->twopass.total_left_stats = 0;
379
380 vpx_free(cpi->twopass.this_frame_stats);
381 cpi->twopass.this_frame_stats = 0;
382 }
383
384 // Computes a q delta (in "q index" terms) to get from a starting q value
385 // to a target value
386 // target q value
387 static int compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
388 int i;
389 int start_index = cpi->worst_quality;
390 int target_index = cpi->worst_quality;
391
392 // Convert the average q value to an index.
393 for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
394 start_index = i;
395 if (vp9_convert_qindex_to_q(i) >= qstart)
396 break;
397 }
398
399 // Convert the q target to an index
400 for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
401 target_index = i;
402 if (vp9_convert_qindex_to_q(i) >= qtarget)
403 break;
404 }
405
406 return target_index - start_index;
407 }
408
409 static void init_seg_features(VP9_COMP *cpi) {
410 VP9_COMMON *cm = &cpi->common;
411 MACROBLOCKD *xd = &cpi->mb.e_mbd;
412
413 int high_q = (int)(cpi->avg_q > 48.0);
414 int qi_delta;
415
416 // Disable and clear down for KF
417 if (cm->frame_type == KEY_FRAME) {
418 // Clear down the global segmentation map
419 vpx_memset(cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));
420 xd->update_mb_segmentation_map = 0;
421 xd->update_mb_segmentation_data = 0;
422 cpi->static_mb_pct = 0;
423
424 // Disable segmentation
425 vp9_disable_segmentation((VP9_PTR)cpi);
426
427 // Clear down the segment features.
428 vp9_clearall_segfeatures(xd);
429 }
430
431 // If this is an alt ref frame
432 else if (cm->refresh_alt_ref_frame) {
433 // Clear down the global segmentation map
434 vpx_memset(cpi->segmentation_map, 0, (cm->mb_rows * cm->mb_cols));
435 xd->update_mb_segmentation_map = 0;
436 xd->update_mb_segmentation_data = 0;
437 cpi->static_mb_pct = 0;
438
439 // Disable segmentation and individual segment features by default
440 vp9_disable_segmentation((VP9_PTR)cpi);
441 vp9_clearall_segfeatures(xd);
442
443 // Scan frames from current to arf frame.
444 // This function re-enables segmentation if appropriate.
445 vp9_update_mbgraph_stats(cpi);
446
447 // If segmentation was enabled set those features needed for the
448 // arf itself.
449 if (xd->segmentation_enabled) {
450 xd->update_mb_segmentation_map = 1;
451 xd->update_mb_segmentation_data = 1;
452
453 qi_delta = compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
454 vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
455 vp9_set_segdata(xd, 1, SEG_LVL_ALT_LF, -2);
456
457 vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
458 vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_LF);
459
460 // Where relevant assume segment data is delta data
461 xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
462
463 }
464 }
465 // All other frames if segmentation has been enabled
466 else if (xd->segmentation_enabled) {
467 // First normal frame in a valid gf or alt ref group
468 if (cpi->common.frames_since_golden == 0) {
469 // Set up segment features for normal frames in an af group
470 if (cpi->source_alt_ref_active) {
471 xd->update_mb_segmentation_map = 0;
472 xd->update_mb_segmentation_data = 1;
473 xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
474
475 qi_delta = compute_qdelta(cpi, cpi->avg_q,
476 (cpi->avg_q * 1.125));
477 vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
478 vp9_set_segdata(xd, 1, SEG_LVL_ALT_Q, 0);
479 vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_Q);
480
481 vp9_set_segdata(xd, 1, SEG_LVL_ALT_LF, -2);
482 vp9_enable_segfeature(xd, 1, SEG_LVL_ALT_LF);
483
484 // Segment coding disabled for compred testing
485 if (high_q || (cpi->static_mb_pct == 100)) {
486 // set_segref(xd, 1, LAST_FRAME);
487 vp9_set_segref(xd, 1, ALTREF_FRAME);
488 vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
489
490 vp9_set_segdata(xd, 1, SEG_LVL_MODE, ZEROMV);
491 vp9_enable_segfeature(xd, 1, SEG_LVL_MODE);
492
493 // EOB segment coding not fixed for 8x8 yet
494 vp9_set_segdata(xd, 1, SEG_LVL_EOB, 0);
495 vp9_enable_segfeature(xd, 1, SEG_LVL_EOB);
496 }
497 }
498 // Disable segmentation and clear down features if alt ref
499 // is not active for this group
500 else {
501 vp9_disable_segmentation((VP9_PTR)cpi);
502
503 vpx_memset(cpi->segmentation_map, 0,
504 (cm->mb_rows * cm->mb_cols));
505
506 xd->update_mb_segmentation_map = 0;
507 xd->update_mb_segmentation_data = 0;
508
509 vp9_clearall_segfeatures(xd);
510 }
511 }
512
513 // Special case where we are coding over the top of a previous
514 // alt ref frame
515 // Segment coding disabled for compred testing
516 else if (cpi->is_src_frame_alt_ref) {
517 // Enable mode and ref frame features for segment 0 as well
518 vp9_enable_segfeature(xd, 0, SEG_LVL_REF_FRAME);
519 vp9_enable_segfeature(xd, 0, SEG_LVL_MODE);
520 vp9_enable_segfeature(xd, 1, SEG_LVL_REF_FRAME);
521 vp9_enable_segfeature(xd, 1, SEG_LVL_MODE);
522
523 // All mbs should use ALTREF_FRAME, ZEROMV exclusively
524 vp9_clear_segref(xd, 0);
525 vp9_set_segref(xd, 0, ALTREF_FRAME);
526 vp9_clear_segref(xd, 1);
527 vp9_set_segref(xd, 1, ALTREF_FRAME);
528 vp9_set_segdata(xd, 0, SEG_LVL_MODE, ZEROMV);
529 vp9_set_segdata(xd, 1, SEG_LVL_MODE, ZEROMV);
530
531 // Skip all MBs if high Q
532 if (high_q) {
533 vp9_enable_segfeature(xd, 0, SEG_LVL_EOB);
534 vp9_set_segdata(xd, 0, SEG_LVL_EOB, 0);
535 vp9_enable_segfeature(xd, 1, SEG_LVL_EOB);
536 vp9_set_segdata(xd, 1, SEG_LVL_EOB, 0);
537 }
538 // Enable data udpate
539 xd->update_mb_segmentation_data = 1;
540 }
541 // All other frames.
542 else {
543 // No updates.. leave things as they are.
544 xd->update_mb_segmentation_map = 0;
545 xd->update_mb_segmentation_data = 0;
546 }
547 }
548 }
549
550 // DEBUG: Print out the segment id of each MB in the current frame.
551 static void print_seg_map(VP9_COMP *cpi) {
552 VP9_COMMON *cm = &cpi->common;
553 int row, col;
554 int map_index = 0;
555 FILE *statsfile;
556
557 statsfile = fopen("segmap.stt", "a");
558
559 fprintf(statsfile, "%10d\n",
560 cm->current_video_frame);
561
562 for (row = 0; row < cpi->common.mb_rows; row++) {
563 for (col = 0; col < cpi->common.mb_cols; col++) {
564 fprintf(statsfile, "%10d",
565 cpi->segmentation_map[map_index]);
566 map_index++;
567 }
568 fprintf(statsfile, "\n");
569 }
570 fprintf(statsfile, "\n");
571
572 fclose(statsfile);
573 }
574
575 static void update_reference_segmentation_map(VP9_COMP *cpi) {
576 VP9_COMMON *cm = &cpi->common;
577 int row, col, sb_rows = (cm->mb_rows + 1) >> 1, sb_cols = (cm->mb_cols + 1) >> 1;
578 MODE_INFO *mi = cm->mi;
579 uint8_t *segmap = cpi->segmentation_map;
580 uint8_t *segcache = cm->last_frame_seg_map;
581
582 for (row = 0; row < sb_rows; row++) {
583 for (col = 0; col < sb_cols; col++) {
584 MODE_INFO *miptr = mi + col * 2;
585 uint8_t *cache = segcache + col * 2;
586 #if CONFIG_SUPERBLOCKS
587 if (miptr->mbmi.encoded_as_sb) {
588 cache[0] = miptr->mbmi.segment_id;
589 if (!(cm->mb_cols & 1) || col < sb_cols - 1)
590 cache[1] = miptr->mbmi.segment_id;
591 if (!(cm->mb_rows & 1) || row < sb_rows - 1) {
592 cache[cm->mb_cols] = miptr->mbmi.segment_id;
593 if (!(cm->mb_cols & 1) || col < sb_cols - 1)
594 cache[cm->mb_cols + 1] = miptr->mbmi.segment_id;
595 }
596 } else
597 #endif
598 {
599 cache[0] = miptr[0].mbmi.segment_id;
600 if (!(cm->mb_cols & 1) || col < sb_cols - 1)
601 cache[1] = miptr[1].mbmi.segment_id;
602 if (!(cm->mb_rows & 1) || row < sb_rows - 1) {
603 cache[cm->mb_cols] = miptr[cm->mode_info_stride].mbmi.segment_id;
604 if (!(cm->mb_cols & 1) || col < sb_cols - 1)
605 cache[1] = miptr[1].mbmi.segment_id;
606 cache[cm->mb_cols + 1] = miptr[cm->mode_info_stride + 1].mbmi.segment_ id;
607 }
608 }
609 }
610 segmap += 2 * cm->mb_cols;
611 segcache += 2 * cm->mb_cols;
612 mi += 2 * cm->mode_info_stride;
613 }
614 }
615
616 static void set_default_lf_deltas(VP9_COMP *cpi) {
617 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
618 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
619
620 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas) );
621 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_delta s));
622
623 // Test of ref frame deltas
624 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
625 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
626 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
627 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
628
629 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
630 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
631 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
632 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
633 }
634
635 void vp9_set_speed_features(VP9_COMP *cpi) {
636 SPEED_FEATURES *sf = &cpi->sf;
637 int Mode = cpi->compressor_speed;
638 int Speed = cpi->Speed;
639 int i;
640 VP9_COMMON *cm = &cpi->common;
641
642 // Only modes 0 and 1 supported for now in experimental code basae
643 if (Mode > 1)
644 Mode = 1;
645
646 // Initialise default mode frequency sampling variables
647 for (i = 0; i < MAX_MODES; i ++) {
648 cpi->mode_check_freq[i] = 0;
649 cpi->mode_test_hit_counts[i] = 0;
650 cpi->mode_chosen_counts[i] = 0;
651 }
652
653 // best quality defaults
654 sf->RD = 1;
655 sf->search_method = NSTEP;
656 sf->improved_dct = 1;
657 sf->auto_filter = 1;
658 sf->recode_loop = 1;
659 sf->quarter_pixel_search = 1;
660 sf->half_pixel_search = 1;
661 sf->iterative_sub_pixel = 1;
662 #if CONFIG_LOSSLESS
663 sf->optimize_coefficients = 0;
664 #else
665 sf->optimize_coefficients = 1;
666 #endif
667 sf->no_skip_block4x4_search = 1;
668
669 sf->first_step = 0;
670 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
671 sf->improved_mv_pred = 1;
672
673 // default thresholds to 0
674 for (i = 0; i < MAX_MODES; i++)
675 sf->thresh_mult[i] = 0;
676
677 switch (Mode) {
678 case 0: // best quality mode
679 #if CONFIG_PRED_FILTER
680 sf->thresh_mult[THR_ZEROMV ] = 0;
681 sf->thresh_mult[THR_ZEROMV_FILT ] = 0;
682 sf->thresh_mult[THR_ZEROG ] = 0;
683 sf->thresh_mult[THR_ZEROG_FILT ] = 0;
684 sf->thresh_mult[THR_ZEROA ] = 0;
685 sf->thresh_mult[THR_ZEROA_FILT ] = 0;
686 sf->thresh_mult[THR_NEARESTMV ] = 0;
687 sf->thresh_mult[THR_NEARESTMV_FILT] = 0;
688 sf->thresh_mult[THR_NEARESTG ] = 0;
689 sf->thresh_mult[THR_NEARESTG_FILT ] = 0;
690 sf->thresh_mult[THR_NEARESTA ] = 0;
691 sf->thresh_mult[THR_NEARESTA_FILT ] = 0;
692 sf->thresh_mult[THR_NEARMV ] = 0;
693 sf->thresh_mult[THR_NEARMV_FILT ] = 0;
694 sf->thresh_mult[THR_NEARG ] = 0;
695 sf->thresh_mult[THR_NEARG_FILT ] = 0;
696 sf->thresh_mult[THR_NEARA ] = 0;
697 sf->thresh_mult[THR_NEARA_FILT ] = 0;
698
699 sf->thresh_mult[THR_DC ] = 0;
700
701 sf->thresh_mult[THR_V_PRED ] = 1000;
702 sf->thresh_mult[THR_H_PRED ] = 1000;
703 sf->thresh_mult[THR_D45_PRED ] = 1000;
704 sf->thresh_mult[THR_D135_PRED] = 1000;
705 sf->thresh_mult[THR_D117_PRED] = 1000;
706 sf->thresh_mult[THR_D153_PRED] = 1000;
707 sf->thresh_mult[THR_D27_PRED ] = 1000;
708 sf->thresh_mult[THR_D63_PRED ] = 1000;
709 sf->thresh_mult[THR_B_PRED ] = 2000;
710 sf->thresh_mult[THR_I8X8_PRED] = 2000;
711 sf->thresh_mult[THR_TM ] = 1000;
712
713 sf->thresh_mult[THR_NEWMV ] = 1000;
714 sf->thresh_mult[THR_NEWG ] = 1000;
715 sf->thresh_mult[THR_NEWA ] = 1000;
716 sf->thresh_mult[THR_NEWMV_FILT ] = 1000;
717 sf->thresh_mult[THR_NEWG_FILT ] = 1000;
718 sf->thresh_mult[THR_NEWA_FILT ] = 1000;
719 #else
720 sf->thresh_mult[THR_ZEROMV ] = 0;
721 sf->thresh_mult[THR_ZEROG ] = 0;
722 sf->thresh_mult[THR_ZEROA ] = 0;
723 sf->thresh_mult[THR_NEARESTMV] = 0;
724 sf->thresh_mult[THR_NEARESTG ] = 0;
725 sf->thresh_mult[THR_NEARESTA ] = 0;
726 sf->thresh_mult[THR_NEARMV ] = 0;
727 sf->thresh_mult[THR_NEARG ] = 0;
728 sf->thresh_mult[THR_NEARA ] = 0;
729
730 sf->thresh_mult[THR_DC ] = 0;
731
732 sf->thresh_mult[THR_V_PRED ] = 1000;
733 sf->thresh_mult[THR_H_PRED ] = 1000;
734 sf->thresh_mult[THR_D45_PRED ] = 1000;
735 sf->thresh_mult[THR_D135_PRED] = 1000;
736 sf->thresh_mult[THR_D117_PRED] = 1000;
737 sf->thresh_mult[THR_D153_PRED] = 1000;
738 sf->thresh_mult[THR_D27_PRED ] = 1000;
739 sf->thresh_mult[THR_D63_PRED ] = 1000;
740 sf->thresh_mult[THR_B_PRED ] = 2000;
741 sf->thresh_mult[THR_I8X8_PRED] = 2000;
742 sf->thresh_mult[THR_TM ] = 1000;
743
744 sf->thresh_mult[THR_NEWMV ] = 1000;
745 sf->thresh_mult[THR_NEWG ] = 1000;
746 sf->thresh_mult[THR_NEWA ] = 1000;
747 #endif
748 sf->thresh_mult[THR_SPLITMV ] = 2500;
749 sf->thresh_mult[THR_SPLITG ] = 5000;
750 sf->thresh_mult[THR_SPLITA ] = 5000;
751
752 sf->thresh_mult[THR_COMP_ZEROLG ] = 0;
753 sf->thresh_mult[THR_COMP_NEARESTLG] = 0;
754 sf->thresh_mult[THR_COMP_NEARLG ] = 0;
755 sf->thresh_mult[THR_COMP_ZEROLA ] = 0;
756 sf->thresh_mult[THR_COMP_NEARESTLA] = 0;
757 sf->thresh_mult[THR_COMP_NEARLA ] = 0;
758 sf->thresh_mult[THR_COMP_ZEROGA ] = 0;
759 sf->thresh_mult[THR_COMP_NEARESTGA] = 0;
760 sf->thresh_mult[THR_COMP_NEARGA ] = 0;
761
762 sf->thresh_mult[THR_COMP_NEWLG ] = 1000;
763 sf->thresh_mult[THR_COMP_NEWLA ] = 1000;
764 sf->thresh_mult[THR_COMP_NEWGA ] = 1000;
765
766 sf->thresh_mult[THR_COMP_SPLITLA ] = 2500;
767 sf->thresh_mult[THR_COMP_SPLITGA ] = 5000;
768 sf->thresh_mult[THR_COMP_SPLITLG ] = 5000;
769
770 #if CONFIG_COMP_INTERINTRA_PRED
771 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL ] = 0;
772 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = 0;
773 sf->thresh_mult[THR_COMP_INTERINTRA_NEARL ] = 0;
774 sf->thresh_mult[THR_COMP_INTERINTRA_NEWL ] = 0;
775 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG ] = 0;
776 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = 0;
777 sf->thresh_mult[THR_COMP_INTERINTRA_NEARG ] = 0;
778 sf->thresh_mult[THR_COMP_INTERINTRA_NEWG ] = 0;
779 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA ] = 0;
780 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = 0;
781 sf->thresh_mult[THR_COMP_INTERINTRA_NEARA ] = 0;
782 sf->thresh_mult[THR_COMP_INTERINTRA_NEWA ] = 0;
783 #endif
784
785 sf->first_step = 0;
786 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
787 sf->search_best_filter = SEARCH_BEST_FILTER;
788 break;
789 case 1:
790 #if CONFIG_PRED_FILTER
791 sf->thresh_mult[THR_NEARESTMV] = 0;
792 sf->thresh_mult[THR_NEARESTMV_FILT] = 0;
793 sf->thresh_mult[THR_ZEROMV ] = 0;
794 sf->thresh_mult[THR_ZEROMV_FILT ] = 0;
795 sf->thresh_mult[THR_DC ] = 0;
796 sf->thresh_mult[THR_NEARMV ] = 0;
797 sf->thresh_mult[THR_NEARMV_FILT ] = 0;
798 sf->thresh_mult[THR_V_PRED ] = 1000;
799 sf->thresh_mult[THR_H_PRED ] = 1000;
800 sf->thresh_mult[THR_D45_PRED ] = 1000;
801 sf->thresh_mult[THR_D135_PRED] = 1000;
802 sf->thresh_mult[THR_D117_PRED] = 1000;
803 sf->thresh_mult[THR_D153_PRED] = 1000;
804 sf->thresh_mult[THR_D27_PRED ] = 1000;
805 sf->thresh_mult[THR_D63_PRED ] = 1000;
806 sf->thresh_mult[THR_B_PRED ] = 2500;
807 sf->thresh_mult[THR_I8X8_PRED] = 2500;
808 sf->thresh_mult[THR_TM ] = 1000;
809
810 sf->thresh_mult[THR_NEARESTG ] = 1000;
811 sf->thresh_mult[THR_NEARESTG_FILT ] = 1000;
812 sf->thresh_mult[THR_NEARESTA ] = 1000;
813 sf->thresh_mult[THR_NEARESTA_FILT ] = 1000;
814
815 sf->thresh_mult[THR_ZEROG ] = 1000;
816 sf->thresh_mult[THR_ZEROA ] = 1000;
817 sf->thresh_mult[THR_NEARG ] = 1000;
818 sf->thresh_mult[THR_NEARA ] = 1000;
819 sf->thresh_mult[THR_ZEROG_FILT ] = 1000;
820 sf->thresh_mult[THR_ZEROA_FILT ] = 1000;
821 sf->thresh_mult[THR_NEARG_FILT ] = 1000;
822 sf->thresh_mult[THR_NEARA_FILT ] = 1000;
823
824 sf->thresh_mult[THR_ZEROMV ] = 0;
825 sf->thresh_mult[THR_ZEROG ] = 0;
826 sf->thresh_mult[THR_ZEROA ] = 0;
827 sf->thresh_mult[THR_NEARESTMV] = 0;
828 sf->thresh_mult[THR_NEARESTG ] = 0;
829 sf->thresh_mult[THR_NEARESTA ] = 0;
830 sf->thresh_mult[THR_NEARMV ] = 0;
831 sf->thresh_mult[THR_NEARG ] = 0;
832 sf->thresh_mult[THR_NEARA ] = 0;
833 sf->thresh_mult[THR_ZEROMV_FILT ] = 0;
834 sf->thresh_mult[THR_ZEROG_FILT ] = 0;
835 sf->thresh_mult[THR_ZEROA_FILT ] = 0;
836 sf->thresh_mult[THR_NEARESTMV_FILT] = 0;
837 sf->thresh_mult[THR_NEARESTG_FILT ] = 0;
838 sf->thresh_mult[THR_NEARESTA_FILT ] = 0;
839 sf->thresh_mult[THR_NEARMV_FILT ] = 0;
840 sf->thresh_mult[THR_NEARG_FILT ] = 0;
841 sf->thresh_mult[THR_NEARA_FILT ] = 0;
842
843 sf->thresh_mult[THR_NEWMV ] = 1000;
844 sf->thresh_mult[THR_NEWG ] = 1000;
845 sf->thresh_mult[THR_NEWA ] = 1000;
846 sf->thresh_mult[THR_NEWMV_FILT ] = 1000;
847 sf->thresh_mult[THR_NEWG_FILT ] = 1000;
848 sf->thresh_mult[THR_NEWA_FILT ] = 1000;
849 #else
850 sf->thresh_mult[THR_NEARESTMV] = 0;
851 sf->thresh_mult[THR_ZEROMV ] = 0;
852 sf->thresh_mult[THR_DC ] = 0;
853 sf->thresh_mult[THR_NEARMV ] = 0;
854 sf->thresh_mult[THR_V_PRED ] = 1000;
855 sf->thresh_mult[THR_H_PRED ] = 1000;
856 sf->thresh_mult[THR_D45_PRED ] = 1000;
857 sf->thresh_mult[THR_D135_PRED] = 1000;
858 sf->thresh_mult[THR_D117_PRED] = 1000;
859 sf->thresh_mult[THR_D153_PRED] = 1000;
860 sf->thresh_mult[THR_D27_PRED ] = 1000;
861 sf->thresh_mult[THR_D63_PRED ] = 1000;
862 sf->thresh_mult[THR_B_PRED ] = 2500;
863 sf->thresh_mult[THR_I8X8_PRED] = 2500;
864 sf->thresh_mult[THR_TM ] = 1000;
865
866 sf->thresh_mult[THR_NEARESTG ] = 1000;
867 sf->thresh_mult[THR_NEARESTA ] = 1000;
868
869 sf->thresh_mult[THR_ZEROG ] = 1000;
870 sf->thresh_mult[THR_ZEROA ] = 1000;
871 sf->thresh_mult[THR_NEARG ] = 1000;
872 sf->thresh_mult[THR_NEARA ] = 1000;
873
874 sf->thresh_mult[THR_ZEROMV ] = 0;
875 sf->thresh_mult[THR_ZEROG ] = 0;
876 sf->thresh_mult[THR_ZEROA ] = 0;
877 sf->thresh_mult[THR_NEARESTMV] = 0;
878 sf->thresh_mult[THR_NEARESTG ] = 0;
879 sf->thresh_mult[THR_NEARESTA ] = 0;
880 sf->thresh_mult[THR_NEARMV ] = 0;
881 sf->thresh_mult[THR_NEARG ] = 0;
882 sf->thresh_mult[THR_NEARA ] = 0;
883
884 sf->thresh_mult[THR_NEWMV ] = 1000;
885 sf->thresh_mult[THR_NEWG ] = 1000;
886 sf->thresh_mult[THR_NEWA ] = 1000;
887 #endif
888 sf->thresh_mult[THR_SPLITMV ] = 1700;
889 sf->thresh_mult[THR_SPLITG ] = 4500;
890 sf->thresh_mult[THR_SPLITA ] = 4500;
891
892 sf->thresh_mult[THR_COMP_ZEROLG ] = 0;
893 sf->thresh_mult[THR_COMP_NEARESTLG] = 0;
894 sf->thresh_mult[THR_COMP_NEARLG ] = 0;
895 sf->thresh_mult[THR_COMP_ZEROLA ] = 0;
896 sf->thresh_mult[THR_COMP_NEARESTLA] = 0;
897 sf->thresh_mult[THR_COMP_NEARLA ] = 0;
898 sf->thresh_mult[THR_COMP_ZEROGA ] = 0;
899 sf->thresh_mult[THR_COMP_NEARESTGA] = 0;
900 sf->thresh_mult[THR_COMP_NEARGA ] = 0;
901
902 sf->thresh_mult[THR_COMP_NEWLG ] = 1000;
903 sf->thresh_mult[THR_COMP_NEWLA ] = 1000;
904 sf->thresh_mult[THR_COMP_NEWGA ] = 1000;
905
906 sf->thresh_mult[THR_COMP_SPLITLA ] = 1700;
907 sf->thresh_mult[THR_COMP_SPLITGA ] = 4500;
908 sf->thresh_mult[THR_COMP_SPLITLG ] = 4500;
909 #if CONFIG_COMP_INTERINTRA_PRED
910 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL ] = 0;
911 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = 0;
912 sf->thresh_mult[THR_COMP_INTERINTRA_NEARL ] = 0;
913 sf->thresh_mult[THR_COMP_INTERINTRA_NEWL ] = 0;
914 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG ] = 0;
915 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = 0;
916 sf->thresh_mult[THR_COMP_INTERINTRA_NEARG ] = 0;
917 sf->thresh_mult[THR_COMP_INTERINTRA_NEWG ] = 0;
918 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA ] = 0;
919 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = 0;
920 sf->thresh_mult[THR_COMP_INTERINTRA_NEARA ] = 0;
921 sf->thresh_mult[THR_COMP_INTERINTRA_NEWA ] = 0;
922 #endif
923
924 if (Speed > 0) {
925 /* Disable coefficient optimization above speed 0 */
926 sf->optimize_coefficients = 0;
927 sf->no_skip_block4x4_search = 0;
928
929 sf->first_step = 1;
930
931 cpi->mode_check_freq[THR_SPLITG] = 2;
932 cpi->mode_check_freq[THR_SPLITA] = 2;
933 cpi->mode_check_freq[THR_SPLITMV] = 0;
934
935 cpi->mode_check_freq[THR_COMP_SPLITGA] = 2;
936 cpi->mode_check_freq[THR_COMP_SPLITLG] = 2;
937 cpi->mode_check_freq[THR_COMP_SPLITLA] = 0;
938 }
939
940 if (Speed > 1) {
941 cpi->mode_check_freq[THR_SPLITG] = 4;
942 cpi->mode_check_freq[THR_SPLITA] = 4;
943 cpi->mode_check_freq[THR_SPLITMV] = 2;
944
945 cpi->mode_check_freq[THR_COMP_SPLITGA] = 4;
946 cpi->mode_check_freq[THR_COMP_SPLITLG] = 4;
947 cpi->mode_check_freq[THR_COMP_SPLITLA] = 2;
948
949 sf->thresh_mult[THR_TM ] = 1500;
950 sf->thresh_mult[THR_V_PRED ] = 1500;
951 sf->thresh_mult[THR_H_PRED ] = 1500;
952 sf->thresh_mult[THR_D45_PRED ] = 1500;
953 sf->thresh_mult[THR_D135_PRED] = 1500;
954 sf->thresh_mult[THR_D117_PRED] = 1500;
955 sf->thresh_mult[THR_D153_PRED] = 1500;
956 sf->thresh_mult[THR_D27_PRED ] = 1500;
957 sf->thresh_mult[THR_D63_PRED ] = 1500;
958 sf->thresh_mult[THR_B_PRED ] = 5000;
959 sf->thresh_mult[THR_I8X8_PRED] = 5000;
960
961 if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
962 sf->thresh_mult[THR_NEWMV ] = 2000;
963 #if CONFIG_PRED_FILTER
964 sf->thresh_mult[THR_NEWMV_FILT ] = 2000;
965 #endif
966 sf->thresh_mult[THR_SPLITMV ] = 10000;
967 sf->thresh_mult[THR_COMP_SPLITLG ] = 20000;
968 }
969
970 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
971 sf->thresh_mult[THR_NEARESTG ] = 1500;
972 sf->thresh_mult[THR_ZEROG ] = 1500;
973 sf->thresh_mult[THR_NEARG ] = 1500;
974 sf->thresh_mult[THR_NEWG ] = 2000;
975 #if CONFIG_PRED_FILTER
976 sf->thresh_mult[THR_NEARESTG_FILT ] = 1500;
977 sf->thresh_mult[THR_ZEROG_FILT ] = 1500;
978 sf->thresh_mult[THR_NEARG_FILT ] = 1500;
979 sf->thresh_mult[THR_NEWG_FILT ] = 2000;
980 #endif
981 sf->thresh_mult[THR_SPLITG ] = 20000;
982 sf->thresh_mult[THR_COMP_SPLITGA ] = 20000;
983 }
984
985 if (cpi->ref_frame_flags & VP9_ALT_FLAG) {
986 sf->thresh_mult[THR_NEARESTA ] = 1500;
987 sf->thresh_mult[THR_ZEROA ] = 1500;
988 sf->thresh_mult[THR_NEARA ] = 1500;
989 sf->thresh_mult[THR_NEWA ] = 2000;
990 #if CONFIG_PRED_FILTER
991 sf->thresh_mult[THR_NEARESTA_FILT ] = 1500;
992 sf->thresh_mult[THR_ZEROA_FILT ] = 1500;
993 sf->thresh_mult[THR_NEARA_FILT ] = 1500;
994 sf->thresh_mult[THR_NEWA_FILT ] = 2000;
995 #endif
996 sf->thresh_mult[THR_SPLITA ] = 20000;
997 sf->thresh_mult[THR_COMP_SPLITLA ] = 10000;
998 }
999
1000 sf->thresh_mult[THR_COMP_ZEROLG ] = 1500;
1001 sf->thresh_mult[THR_COMP_NEARESTLG] = 1500;
1002 sf->thresh_mult[THR_COMP_NEARLG ] = 1500;
1003 sf->thresh_mult[THR_COMP_ZEROLA ] = 1500;
1004 sf->thresh_mult[THR_COMP_NEARESTLA] = 1500;
1005 sf->thresh_mult[THR_COMP_NEARLA ] = 1500;
1006 sf->thresh_mult[THR_COMP_ZEROGA ] = 1500;
1007 sf->thresh_mult[THR_COMP_NEARESTGA] = 1500;
1008 sf->thresh_mult[THR_COMP_NEARGA ] = 1500;
1009
1010 sf->thresh_mult[THR_COMP_NEWLG ] = 2000;
1011 sf->thresh_mult[THR_COMP_NEWLA ] = 2000;
1012 sf->thresh_mult[THR_COMP_NEWGA ] = 2000;
1013 #if CONFIG_COMP_INTERINTRA_PRED
1014 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL ] = 0;
1015 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = 0;
1016 sf->thresh_mult[THR_COMP_INTERINTRA_NEARL ] = 0;
1017 sf->thresh_mult[THR_COMP_INTERINTRA_NEWL ] = 0;
1018 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG ] = 0;
1019 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = 0;
1020 sf->thresh_mult[THR_COMP_INTERINTRA_NEARG ] = 0;
1021 sf->thresh_mult[THR_COMP_INTERINTRA_NEWG ] = 0;
1022 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA ] = 0;
1023 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = 0;
1024 sf->thresh_mult[THR_COMP_INTERINTRA_NEARA ] = 0;
1025 sf->thresh_mult[THR_COMP_INTERINTRA_NEWA ] = 0;
1026 #endif
1027 }
1028
1029 if (Speed > 2) {
1030 cpi->mode_check_freq[THR_SPLITG] = 15;
1031 cpi->mode_check_freq[THR_SPLITA] = 15;
1032 cpi->mode_check_freq[THR_SPLITMV] = 7;
1033
1034 cpi->mode_check_freq[THR_COMP_SPLITGA] = 15;
1035 cpi->mode_check_freq[THR_COMP_SPLITLG] = 15;
1036 cpi->mode_check_freq[THR_COMP_SPLITLA] = 7;
1037
1038 sf->thresh_mult[THR_TM ] = 2000;
1039 sf->thresh_mult[THR_V_PRED ] = 2000;
1040 sf->thresh_mult[THR_H_PRED ] = 2000;
1041 sf->thresh_mult[THR_D45_PRED ] = 2000;
1042 sf->thresh_mult[THR_D135_PRED] = 2000;
1043 sf->thresh_mult[THR_D117_PRED] = 2000;
1044 sf->thresh_mult[THR_D153_PRED] = 2000;
1045 sf->thresh_mult[THR_D27_PRED ] = 2000;
1046 sf->thresh_mult[THR_D63_PRED ] = 2000;
1047 sf->thresh_mult[THR_B_PRED ] = 7500;
1048 sf->thresh_mult[THR_I8X8_PRED] = 7500;
1049
1050 if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
1051 sf->thresh_mult[THR_NEWMV ] = 2000;
1052 #if CONFIG_PRED_FILTER
1053 sf->thresh_mult[THR_NEWMV_FILT ] = 2000;
1054 #endif
1055 sf->thresh_mult[THR_SPLITMV ] = 25000;
1056 sf->thresh_mult[THR_COMP_SPLITLG ] = 50000;
1057 }
1058
1059 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
1060 sf->thresh_mult[THR_NEARESTG ] = 2000;
1061 sf->thresh_mult[THR_ZEROG ] = 2000;
1062 sf->thresh_mult[THR_NEARG ] = 2000;
1063 sf->thresh_mult[THR_NEWG ] = 2500;
1064 #if CONFIG_PRED_FILTER
1065 sf->thresh_mult[THR_NEARESTG_FILT ] = 2000;
1066 sf->thresh_mult[THR_ZEROG_FILT ] = 2000;
1067 sf->thresh_mult[THR_NEARG_FILT ] = 2000;
1068 sf->thresh_mult[THR_NEWG_FILT ] = 2500;
1069 #endif
1070 sf->thresh_mult[THR_SPLITG ] = 50000;
1071 sf->thresh_mult[THR_COMP_SPLITGA ] = 50000;
1072 }
1073
1074 if (cpi->ref_frame_flags & VP9_ALT_FLAG) {
1075 sf->thresh_mult[THR_NEARESTA ] = 2000;
1076 sf->thresh_mult[THR_ZEROA ] = 2000;
1077 sf->thresh_mult[THR_NEARA ] = 2000;
1078 sf->thresh_mult[THR_NEWA ] = 2500;
1079 #if CONFIG_PRED_FILTER
1080 sf->thresh_mult[THR_NEARESTA_FILT ] = 2000;
1081 sf->thresh_mult[THR_ZEROA_FILT ] = 2000;
1082 sf->thresh_mult[THR_NEARA_FILT ] = 2000;
1083 sf->thresh_mult[THR_NEWA_FILT ] = 2500;
1084 #endif
1085 sf->thresh_mult[THR_SPLITA ] = 50000;
1086 sf->thresh_mult[THR_COMP_SPLITLA ] = 25000;
1087 }
1088
1089 sf->thresh_mult[THR_COMP_ZEROLG ] = 2000;
1090 sf->thresh_mult[THR_COMP_NEARESTLG] = 2000;
1091 sf->thresh_mult[THR_COMP_NEARLG ] = 2000;
1092 sf->thresh_mult[THR_COMP_ZEROLA ] = 2000;
1093 sf->thresh_mult[THR_COMP_NEARESTLA] = 2000;
1094 sf->thresh_mult[THR_COMP_NEARLA ] = 2000;
1095 sf->thresh_mult[THR_COMP_ZEROGA ] = 2000;
1096 sf->thresh_mult[THR_COMP_NEARESTGA] = 2000;
1097 sf->thresh_mult[THR_COMP_NEARGA ] = 2000;
1098
1099 sf->thresh_mult[THR_COMP_NEWLG ] = 2500;
1100 sf->thresh_mult[THR_COMP_NEWLA ] = 2500;
1101 sf->thresh_mult[THR_COMP_NEWGA ] = 2500;
1102 #if CONFIG_COMP_INTERINTRA_PRED
1103 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL ] = 0;
1104 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = 0;
1105 sf->thresh_mult[THR_COMP_INTERINTRA_NEARL ] = 0;
1106 sf->thresh_mult[THR_COMP_INTERINTRA_NEWL ] = 0;
1107 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG ] = 0;
1108 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = 0;
1109 sf->thresh_mult[THR_COMP_INTERINTRA_NEARG ] = 0;
1110 sf->thresh_mult[THR_COMP_INTERINTRA_NEWG ] = 0;
1111 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA ] = 0;
1112 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = 0;
1113 sf->thresh_mult[THR_COMP_INTERINTRA_NEARA ] = 0;
1114 sf->thresh_mult[THR_COMP_INTERINTRA_NEWA ] = 0;
1115 #endif
1116
1117 sf->improved_dct = 0;
1118
1119 // Only do recode loop on key frames, golden frames and
1120 // alt ref frames
1121 sf->recode_loop = 2;
1122
1123 }
1124
1125 break;
1126
1127 }; /* switch */
1128
1129 /* disable frame modes if flags not set */
1130 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
1131 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
1132 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
1133 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
1134 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
1135 #if CONFIG_PRED_FILTER
1136 sf->thresh_mult[THR_NEWMV_FILT ] = INT_MAX;
1137 sf->thresh_mult[THR_NEARESTMV_FILT] = INT_MAX;
1138 sf->thresh_mult[THR_ZEROMV_FILT ] = INT_MAX;
1139 sf->thresh_mult[THR_NEARMV_FILT ] = INT_MAX;
1140 #endif
1141 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
1142 }
1143
1144 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
1145 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
1146 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
1147 sf->thresh_mult[THR_NEARG ] = INT_MAX;
1148 sf->thresh_mult[THR_NEWG ] = INT_MAX;
1149 #if CONFIG_PRED_FILTER
1150 sf->thresh_mult[THR_NEARESTG_FILT ] = INT_MAX;
1151 sf->thresh_mult[THR_ZEROG_FILT ] = INT_MAX;
1152 sf->thresh_mult[THR_NEARG_FILT ] = INT_MAX;
1153 sf->thresh_mult[THR_NEWG_FILT ] = INT_MAX;
1154 #endif
1155 #if CONFIG_COMP_INTERINTRA_PRED
1156 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROG ] = INT_MAX;
1157 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] = INT_MAX;
1158 sf->thresh_mult[THR_COMP_INTERINTRA_NEARG ] = INT_MAX;
1159 sf->thresh_mult[THR_COMP_INTERINTRA_NEWG ] = INT_MAX;
1160 #endif
1161 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
1162 }
1163
1164 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
1165 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
1166 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
1167 sf->thresh_mult[THR_NEARA ] = INT_MAX;
1168 sf->thresh_mult[THR_NEWA ] = INT_MAX;
1169 #if CONFIG_PRED_FILTER
1170 sf->thresh_mult[THR_NEARESTA_FILT ] = INT_MAX;
1171 sf->thresh_mult[THR_ZEROA_FILT ] = INT_MAX;
1172 sf->thresh_mult[THR_NEARA_FILT ] = INT_MAX;
1173 sf->thresh_mult[THR_NEWA_FILT ] = INT_MAX;
1174 #endif
1175 #if CONFIG_COMP_INTERINTRA_PRED
1176 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROA ] = INT_MAX;
1177 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] = INT_MAX;
1178 sf->thresh_mult[THR_COMP_INTERINTRA_NEARA ] = INT_MAX;
1179 sf->thresh_mult[THR_COMP_INTERINTRA_NEWA ] = INT_MAX;
1180 #endif
1181 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
1182 }
1183
1184 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_GOLD_FLAG)) != (VP9_LAST_FLAG | VP9_GOLD_FLAG)) {
1185 sf->thresh_mult[THR_COMP_ZEROLG ] = INT_MAX;
1186 sf->thresh_mult[THR_COMP_NEARESTLG] = INT_MAX;
1187 sf->thresh_mult[THR_COMP_NEARLG ] = INT_MAX;
1188 sf->thresh_mult[THR_COMP_NEWLG ] = INT_MAX;
1189 sf->thresh_mult[THR_COMP_SPLITLG ] = INT_MAX;
1190 }
1191
1192 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) != (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
1193 sf->thresh_mult[THR_COMP_ZEROLA ] = INT_MAX;
1194 sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
1195 sf->thresh_mult[THR_COMP_NEARLA ] = INT_MAX;
1196 sf->thresh_mult[THR_COMP_NEWLA ] = INT_MAX;
1197 sf->thresh_mult[THR_COMP_SPLITLA ] = INT_MAX;
1198 }
1199
1200 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) != (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
1201 sf->thresh_mult[THR_COMP_ZEROGA ] = INT_MAX;
1202 sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
1203 sf->thresh_mult[THR_COMP_NEARGA ] = INT_MAX;
1204 sf->thresh_mult[THR_COMP_NEWGA ] = INT_MAX;
1205 sf->thresh_mult[THR_COMP_SPLITGA ] = INT_MAX;
1206 }
1207 #if CONFIG_COMP_INTERINTRA_PRED
1208 if ((cpi->ref_frame_flags & VP9_LAST_FLAG) != VP9_LAST_FLAG) {
1209 sf->thresh_mult[THR_COMP_INTERINTRA_ZEROL ] = INT_MAX;
1210 sf->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] = INT_MAX;
1211 sf->thresh_mult[THR_COMP_INTERINTRA_NEARL ] = INT_MAX;
1212 sf->thresh_mult[THR_COMP_INTERINTRA_NEWL ] = INT_MAX;
1213 }
1214 #endif
1215
1216 // Slow quant, dct and trellis not worthwhile for first pass
1217 // so make sure they are always turned off.
1218 if (cpi->pass == 1) {
1219 sf->optimize_coefficients = 0;
1220 sf->improved_dct = 0;
1221 }
1222
1223 if (cpi->sf.search_method == NSTEP) {
1224 vp9_init3smotion_compensation(&cpi->mb,
1225 cm->yv12_fb[cm->lst_fb_idx].y_stride);
1226 } else if (cpi->sf.search_method == DIAMOND) {
1227 vp9_init_dsmotion_compensation(&cpi->mb,
1228 cm->yv12_fb[cm->lst_fb_idx].y_stride);
1229 }
1230
1231 cpi->mb.vp9_short_fdct16x16 = vp9_short_fdct16x16;
1232 cpi->mb.vp9_short_fdct8x8 = vp9_short_fdct8x8;
1233 cpi->mb.vp9_short_fdct8x4 = vp9_short_fdct8x4;
1234 cpi->mb.vp9_short_fdct4x4 = vp9_short_fdct4x4;
1235 cpi->mb.short_walsh4x4 = vp9_short_walsh4x4;
1236 cpi->mb.short_fhaar2x2 = vp9_short_fhaar2x2;
1237
1238 #if CONFIG_LOSSLESS
1239 if (cpi->oxcf.lossless) {
1240 cpi->mb.vp9_short_fdct8x4 = vp9_short_walsh8x4_x8;
1241 cpi->mb.vp9_short_fdct4x4 = vp9_short_walsh4x4_x8;
1242 cpi->mb.short_walsh4x4 = vp9_short_walsh4x4;
1243 cpi->mb.short_fhaar2x2 = vp9_short_fhaar2x2;
1244 cpi->mb.short_walsh4x4 = vp9_short_walsh4x4_lossless;
1245 }
1246 #endif
1247
1248 cpi->mb.quantize_b_4x4 = vp9_regular_quantize_b_4x4;
1249 cpi->mb.quantize_b_4x4_pair = vp9_regular_quantize_b_4x4_pair;
1250 cpi->mb.quantize_b_8x8 = vp9_regular_quantize_b_8x8;
1251 cpi->mb.quantize_b_16x16 = vp9_regular_quantize_b_16x16;
1252 cpi->mb.quantize_b_2x2 = vp9_regular_quantize_b_2x2;
1253
1254 vp9_init_quantizer(cpi);
1255
1256 if (cpi->sf.iterative_sub_pixel == 1) {
1257 cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step_iteratively;
1258 } else if (cpi->sf.quarter_pixel_search) {
1259 cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step;
1260 } else if (cpi->sf.half_pixel_search) {
1261 cpi->find_fractional_mv_step = vp9_find_best_half_pixel_step;
1262 }
1263
1264 if (cpi->sf.optimize_coefficients == 1 && cpi->pass != 1)
1265 cpi->mb.optimize = 1;
1266 else
1267 cpi->mb.optimize = 0;
1268
1269 #ifdef SPEEDSTATS
1270 frames_at_speed[cpi->Speed]++;
1271 #endif
1272 }
1273 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
1274 int width = (cpi->oxcf.Width + 15) & ~15;
1275 int height = (cpi->oxcf.Height + 15) & ~15;
1276
1277 cpi->lookahead = vp9_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1278 cpi->oxcf.lag_in_frames);
1279 if (!cpi->lookahead)
1280 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1281 "Failed to allocate lag buffers");
1282
1283 #if VP9_TEMPORAL_ALT_REF
1284
1285 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1286 width, height, VP9BORDERINPIXELS))
1287 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1288 "Failed to allocate altref buffer");
1289
1290 #endif
1291 }
1292
1293 static int alloc_partition_data(VP9_COMP *cpi) {
1294 vpx_free(cpi->mb.pip);
1295
1296 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1297 (cpi->common.mb_rows + 1),
1298 sizeof(PARTITION_INFO));
1299 if (!cpi->mb.pip)
1300 return 1;
1301
1302 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1303
1304 return 0;
1305 }
1306
1307 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
1308 VP9_COMMON *cm = &cpi->common;
1309
1310 int width = cm->Width;
1311 int height = cm->Height;
1312
1313 if (vp9_alloc_frame_buffers(cm, width, height))
1314 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1315 "Failed to allocate frame buffers");
1316
1317 if (alloc_partition_data(cpi))
1318 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1319 "Failed to allocate partition data");
1320
1321
1322 if ((width & 0xf) != 0)
1323 width += 16 - (width & 0xf);
1324
1325 if ((height & 0xf) != 0)
1326 height += 16 - (height & 0xf);
1327
1328
1329 if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
1330 width, height, VP9BORDERINPIXELS))
1331 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1332 "Failed to allocate last frame buffer");
1333
1334 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
1335 width, height, VP9BORDERINPIXELS))
1336 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1337 "Failed to allocate scaled source buffer");
1338
1339
1340 vpx_free(cpi->tok);
1341
1342 {
1343 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1344
1345 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1346 }
1347
1348 // Data used for real time vc mode to see if gf needs refreshing
1349 cpi->inter_zz_count = 0;
1350 cpi->gf_bad_count = 0;
1351 cpi->gf_update_recommended = 0;
1352
1353
1354 // Structures used to minitor GF usage
1355 vpx_free(cpi->gf_active_flags);
1356 CHECK_MEM_ERROR(cpi->gf_active_flags,
1357 vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1358 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1359
1360 vpx_free(cpi->mb_activity_map);
1361 CHECK_MEM_ERROR(cpi->mb_activity_map,
1362 vpx_calloc(sizeof(unsigned int),
1363 cm->mb_rows * cm->mb_cols));
1364
1365 vpx_free(cpi->mb_norm_activity_map);
1366 CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
1367 vpx_calloc(sizeof(unsigned int),
1368 cm->mb_rows * cm->mb_cols));
1369
1370 vpx_free(cpi->twopass.total_stats);
1371
1372 cpi->twopass.total_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1373
1374 vpx_free(cpi->twopass.total_left_stats);
1375 cpi->twopass.total_left_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1376
1377 vpx_free(cpi->twopass.this_frame_stats);
1378
1379 cpi->twopass.this_frame_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1380
1381 if (!cpi->twopass.total_stats ||
1382 !cpi->twopass.total_left_stats ||
1383 !cpi->twopass.this_frame_stats)
1384 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1385 "Failed to allocate firstpass stats");
1386
1387 vpx_free(cpi->tplist);
1388
1389 CHECK_MEM_ERROR(cpi->tplist,
1390 vpx_malloc(sizeof(TOKENLIST) * (cpi->common.mb_rows)));
1391 }
1392
1393
1394 // TODO perhaps change number of steps expose to outside world when setting
1395 // max and min limits. Also this will likely want refining for the extended Q
1396 // range.
1397 //
1398 // Table that converts 0-63 Q range values passed in outside to the Qindex
1399 // range used internally.
1400 static const int q_trans[] = {
1401 0, 4, 8, 12, 16, 20, 24, 28,
1402 32, 36, 40, 44, 48, 52, 56, 60,
1403 64, 68, 72, 76, 80, 84, 88, 92,
1404 96, 100, 104, 108, 112, 116, 120, 124,
1405 128, 132, 136, 140, 144, 148, 152, 156,
1406 160, 164, 168, 172, 176, 180, 184, 188,
1407 192, 196, 200, 204, 208, 212, 216, 220,
1408 224, 228, 232, 236, 240, 244, 249, 255,
1409 };
1410
1411 int vp9_reverse_trans(int x) {
1412 int i;
1413
1414 for (i = 0; i < 64; i++)
1415 if (q_trans[i] >= x)
1416 return i;
1417
1418 return 63;
1419 };
1420 void vp9_new_frame_rate(VP9_COMP *cpi, double framerate) {
1421 if (framerate < .1)
1422 framerate = 30;
1423
1424 cpi->oxcf.frame_rate = framerate;
1425 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1426 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->ou tput_frame_rate);
1427 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->o utput_frame_rate);
1428 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->o xcf.two_pass_vbrmin_section / 100);
1429
1430 if (cpi->min_frame_bandwidth < FRAME_OVERHEAD_BITS)
1431 cpi->min_frame_bandwidth = FRAME_OVERHEAD_BITS;
1432
1433 // Set Maximum gf/arf interval
1434 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1435
1436 if (cpi->max_gf_interval < 12)
1437 cpi->max_gf_interval = 12;
1438
1439 // Extended interval for genuinely static scenes
1440 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1441
1442 // Special conditions when altr ref frame enabled in lagged compress mode
1443 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) {
1444 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1445 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1446
1447 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1448 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1449 }
1450
1451 if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval)
1452 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1453 }
1454
1455
1456 static int
1457 rescale(int val, int num, int denom) {
1458 int64_t llnum = num;
1459 int64_t llden = denom;
1460 int64_t llval = val;
1461
1462 return (int)(llval * llnum / llden);
1463 }
1464
1465
1466 static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
1467 VP9_COMP *cpi = (VP9_COMP *)(ptr);
1468 VP9_COMMON *cm = &cpi->common;
1469
1470 cpi->oxcf = *oxcf;
1471
1472 cpi->goldfreq = 7;
1473
1474 cm->version = oxcf->Version;
1475 vp9_setup_version(cm);
1476
1477 // change includes all joint functionality
1478 vp9_change_config(ptr, oxcf);
1479
1480 // Initialize active best and worst q and average q values.
1481 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1482 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1483 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1484
1485 // Initialise the starting buffer levels
1486 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1487 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1488
1489 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1490 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1491 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1492 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1493
1494 cpi->total_actual_bits = 0;
1495 cpi->total_target_vs_actual = 0;
1496
1497 cpi->static_mb_pct = 0;
1498
1499 #if VP9_TEMPORAL_ALT_REF
1500 {
1501 int i;
1502
1503 cpi->fixed_divide[0] = 0;
1504
1505 for (i = 1; i < 512; i++)
1506 cpi->fixed_divide[i] = 0x80000 / i;
1507 }
1508 #endif
1509 }
1510
1511
1512 void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
1513 VP9_COMP *cpi = (VP9_COMP *)(ptr);
1514 VP9_COMMON *cm = &cpi->common;
1515
1516 if (!cpi)
1517 return;
1518
1519 if (!oxcf)
1520 return;
1521
1522 if (cm->version != oxcf->Version) {
1523 cm->version = oxcf->Version;
1524 vp9_setup_version(cm);
1525 }
1526
1527 cpi->oxcf = *oxcf;
1528
1529 switch (cpi->oxcf.Mode) {
1530 // Real time and one pass deprecated in test code base
1531 case MODE_FIRSTPASS:
1532 cpi->pass = 1;
1533 cpi->compressor_speed = 1;
1534 break;
1535
1536 case MODE_SECONDPASS:
1537 cpi->pass = 2;
1538 cpi->compressor_speed = 1;
1539
1540 if (cpi->oxcf.cpu_used < -5) {
1541 cpi->oxcf.cpu_used = -5;
1542 }
1543
1544 if (cpi->oxcf.cpu_used > 5)
1545 cpi->oxcf.cpu_used = 5;
1546
1547 break;
1548
1549 case MODE_SECONDPASS_BEST:
1550 cpi->pass = 2;
1551 cpi->compressor_speed = 0;
1552 break;
1553 }
1554
1555 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1556 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1557 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1558
1559 cpi->mb.e_mbd.inv_xform4x4_1_x8 = vp9_short_idct4x4llm_1;
1560 cpi->mb.e_mbd.inv_xform4x4_x8 = vp9_short_idct4x4llm;
1561 cpi->mb.e_mbd.inv_walsh4x4_1 = vp9_short_inv_walsh4x4_1;
1562 cpi->mb.e_mbd.inv_walsh4x4_lossless = vp9_short_inv_walsh4x4;
1563
1564 #if CONFIG_LOSSLESS
1565 cpi->oxcf.lossless = oxcf->lossless;
1566 if (cpi->oxcf.lossless) {
1567 cpi->mb.e_mbd.inv_xform4x4_1_x8 = vp9_short_inv_walsh4x4_1_x8;
1568 cpi->mb.e_mbd.inv_xform4x4_x8 = vp9_short_inv_walsh4x4_x8;
1569 cpi->mb.e_mbd.inv_walsh4x4_1 = vp9_short_inv_walsh4x4_1_lossless;
1570 cpi->mb.e_mbd.inv_walsh4x4_lossless = vp9_short_inv_walsh4x4_lossless;
1571 }
1572 #endif
1573
1574 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1575
1576 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
1577
1578 // cpi->use_golden_frame_only = 0;
1579 // cpi->use_last_frame_only = 0;
1580 cm->refresh_golden_frame = 0;
1581 cm->refresh_last_frame = 1;
1582 cm->refresh_entropy_probs = 1;
1583
1584 setup_features(cpi);
1585 cpi->mb.e_mbd.allow_high_precision_mv = 0; // Default mv precision adaptatio n
1586 set_mvcost(&cpi->mb);
1587
1588 {
1589 int i;
1590
1591 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1592 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1593 }
1594
1595 // At the moment the first order values may not be > MAXQ
1596 if (cpi->oxcf.fixed_q > MAXQ)
1597 cpi->oxcf.fixed_q = MAXQ;
1598
1599 // local file playback mode == really big buffer
1600 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
1601 cpi->oxcf.starting_buffer_level = 60000;
1602 cpi->oxcf.optimal_buffer_level = 60000;
1603 cpi->oxcf.maximum_buffer_size = 240000;
1604 }
1605
1606 // Convert target bandwidth from Kbit/s to Bit/s
1607 cpi->oxcf.target_bandwidth *= 1000;
1608
1609 cpi->oxcf.starting_buffer_level =
1610 rescale(cpi->oxcf.starting_buffer_level,
1611 cpi->oxcf.target_bandwidth, 1000);
1612
1613 // Set or reset optimal and maximum buffer levels.
1614 if (cpi->oxcf.optimal_buffer_level == 0)
1615 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1616 else
1617 cpi->oxcf.optimal_buffer_level =
1618 rescale(cpi->oxcf.optimal_buffer_level,
1619 cpi->oxcf.target_bandwidth, 1000);
1620
1621 if (cpi->oxcf.maximum_buffer_size == 0)
1622 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1623 else
1624 cpi->oxcf.maximum_buffer_size =
1625 rescale(cpi->oxcf.maximum_buffer_size,
1626 cpi->oxcf.target_bandwidth, 1000);
1627
1628 // Set up frame rate and related parameters rate control values.
1629 vp9_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1630
1631 // Set absolute upper and lower quality limits
1632 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1633 cpi->best_quality = cpi->oxcf.best_allowed_q;
1634
1635 // active values should only be modified if out of new range
1636 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q) {
1637 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1638 }
1639 // less likely
1640 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q) {
1641 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1642 }
1643 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q) {
1644 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1645 }
1646 // less likely
1647 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q) {
1648 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1649 }
1650
1651 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1652
1653 cpi->cq_target_quality = cpi->oxcf.cq_level;
1654
1655 if (!cm->use_bilinear_mc_filter)
1656 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER;
1657 else
1658 cm->mcomp_filter_type = BILINEAR;
1659
1660 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1661
1662 cm->Width = cpi->oxcf.Width;
1663 cm->Height = cpi->oxcf.Height;
1664
1665 cm->horiz_scale = cpi->horiz_scale;
1666 cm->vert_scale = cpi->vert_scale;
1667
1668 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1669 if (cpi->oxcf.Sharpness > 7)
1670 cpi->oxcf.Sharpness = 7;
1671
1672 cm->sharpness_level = cpi->oxcf.Sharpness;
1673
1674 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL) {
1675 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1676 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1677
1678 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1679 Scale2Ratio(cm->vert_scale, &vr, &vs);
1680
1681 // always go to the next whole number
1682 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1683 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1684 }
1685
1686 if (((cm->Width + 15) & 0xfffffff0) !=
1687 cm->yv12_fb[cm->lst_fb_idx].y_width ||
1688 ((cm->Height + 15) & 0xfffffff0) !=
1689 cm->yv12_fb[cm->lst_fb_idx].y_height ||
1690 cm->yv12_fb[cm->lst_fb_idx].y_width == 0) {
1691 alloc_raw_frame_buffers(cpi);
1692 vp9_alloc_compressor_data(cpi);
1693 }
1694
1695 if (cpi->oxcf.fixed_q >= 0) {
1696 cpi->last_q[0] = cpi->oxcf.fixed_q;
1697 cpi->last_q[1] = cpi->oxcf.fixed_q;
1698 cpi->last_boosted_qindex = cpi->oxcf.fixed_q;
1699 }
1700
1701 cpi->Speed = cpi->oxcf.cpu_used;
1702
1703 // force to allowlag to 0 if lag_in_frames is 0;
1704 if (cpi->oxcf.lag_in_frames == 0) {
1705 cpi->oxcf.allow_lag = 0;
1706 }
1707 // Limit on lag buffers as these are not currently dynamically allocated
1708 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1709 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1710
1711 // YX Temp
1712 cpi->alt_ref_source = NULL;
1713 cpi->is_src_frame_alt_ref = 0;
1714
1715 #if 0
1716 // Experimental RD Code
1717 cpi->frame_distortion = 0;
1718 cpi->last_frame_distortion = 0;
1719 #endif
1720
1721 }
1722
1723 #define M_LOG2_E 0.693147180559945309417
1724 #define log2f(x) (log (x) / (float) M_LOG2_E)
1725
1726 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1727 mvjointsadcost[0] = 600;
1728 mvjointsadcost[1] = 300;
1729 mvjointsadcost[2] = 300;
1730 mvjointsadcost[0] = 300;
1731 }
1732
1733 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1734 int i = 1;
1735
1736 mvsadcost [0] [0] = 0;
1737 mvsadcost [1] [0] = 0;
1738
1739 do {
1740 double z = 256 * (2 * (log2f(8 * i) + .6));
1741 mvsadcost [0][i] = (int) z;
1742 mvsadcost [1][i] = (int) z;
1743 mvsadcost [0][-i] = (int) z;
1744 mvsadcost [1][-i] = (int) z;
1745 } while (++i <= MV_MAX);
1746 }
1747
1748 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1749 int i = 1;
1750
1751 mvsadcost [0] [0] = 0;
1752 mvsadcost [1] [0] = 0;
1753
1754 do {
1755 double z = 256 * (2 * (log2f(8 * i) + .6));
1756 mvsadcost [0][i] = (int) z;
1757 mvsadcost [1][i] = (int) z;
1758 mvsadcost [0][-i] = (int) z;
1759 mvsadcost [1][-i] = (int) z;
1760 } while (++i <= MV_MAX);
1761 }
1762
1763 VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) {
1764 int i;
1765 volatile union {
1766 VP9_COMP *cpi;
1767 VP9_PTR ptr;
1768 } ctx;
1769
1770 VP9_COMP *cpi;
1771 VP9_COMMON *cm;
1772
1773 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP9_COMP));
1774 // Check that the CPI instance is valid
1775 if (!cpi)
1776 return 0;
1777
1778 cm = &cpi->common;
1779
1780 vpx_memset(cpi, 0, sizeof(VP9_COMP));
1781
1782 if (setjmp(cm->error.jmp)) {
1783 VP9_PTR ptr = ctx.ptr;
1784
1785 ctx.cpi->common.error.setjmp = 0;
1786 vp9_remove_compressor(&ptr);
1787 return 0;
1788 }
1789
1790 cpi->common.error.setjmp = 1;
1791
1792 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEP S * 8) + 1));
1793
1794 vp9_create_common(&cpi->common);
1795
1796 init_config((VP9_PTR)cpi, oxcf);
1797
1798 memcpy(cpi->base_skip_false_prob, base_skip_false_prob, sizeof(base_skip_false _prob));
1799 cpi->common.current_video_frame = 0;
1800 cpi->kf_overspend_bits = 0;
1801 cpi->kf_bitrate_adjustment = 0;
1802 cpi->frames_till_gf_update_due = 0;
1803 cpi->gf_overspend_bits = 0;
1804 cpi->non_gf_bitrate_adjustment = 0;
1805 cm->prob_last_coded = 128;
1806 cm->prob_gf_coded = 128;
1807 cm->prob_intra_coded = 63;
1808 #if CONFIG_SUPERBLOCKS
1809 cm->sb_coded = 200;
1810 #endif
1811 for (i = 0; i < COMP_PRED_CONTEXTS; i++)
1812 cm->prob_comppred[i] = 128;
1813 for (i = 0; i < TX_SIZE_MAX - 1; i++)
1814 cm->prob_tx[i] = 128;
1815
1816 // Prime the recent reference frame useage counters.
1817 // Hereafter they will be maintained as a sort of moving average
1818 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1819 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1820 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1821 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1822
1823 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1824 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1825
1826 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1827
1828 cpi->gold_is_last = 0;
1829 cpi->alt_is_last = 0;
1830 cpi->gold_is_alt = 0;
1831
1832 // allocate memory for storing last frame's MVs for MV prediction.
1833 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows + 2) * (cpi->common .mb_cols + 2), sizeof(int_mv)));
1834 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows + 2) * (cpi->common.mb_cols + 2), sizeof(int)));
1835 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows + 2) * (cpi ->common.mb_cols + 2), sizeof(int)));
1836
1837 // Create the encoder segmentation map and set all entries to 0
1838 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc((cpi->common.mb_rows * cpi-> common.mb_cols), 1));
1839
1840 // And a copy in common for temporal coding
1841 CHECK_MEM_ERROR(cm->last_frame_seg_map,
1842 vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1843
1844 // And a place holder structure is the coding context
1845 // for use if we want to save and restore it
1846 CHECK_MEM_ERROR(cpi->coding_context.last_frame_seg_map_copy,
1847 vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1848
1849 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common. mb_cols, 1));
1850 vpx_memset(cpi->active_map, 1, (cpi->common.mb_rows * cpi->common.mb_cols));
1851 cpi->active_map_enabled = 0;
1852
1853 for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1854 sizeof(cpi->mbgraph_stats[0])); i++) {
1855 CHECK_MEM_ERROR(cpi->mbgraph_stats[i].mb_stats,
1856 vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols *
1857 sizeof(*cpi->mbgraph_stats[i].mb_stats),
1858 1));
1859 }
1860
1861 #ifdef ENTROPY_STATS
1862 if (cpi->pass != 1)
1863 init_context_counters();
1864 #endif
1865 #ifdef MODE_STATS
1866 vp9_zero(y_modes);
1867 vp9_zero(i8x8_modes);
1868 vp9_zero(uv_modes);
1869 vp9_zero(uv_modes_y);
1870 vp9_zero(b_modes);
1871 vp9_zero(inter_y_modes);
1872 vp9_zero(inter_uv_modes);
1873 vp9_zero(inter_b_modes);
1874 #endif
1875 #ifdef NMV_STATS
1876 init_nmvstats();
1877 #endif
1878
1879 /*Initialize the feed-forward activity masking.*/
1880 cpi->activity_avg = 90 << 12;
1881
1882 cpi->frames_since_key = 8; // Give a sensible default for the first fra me.
1883 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1884 cpi->this_key_frame_forced = FALSE;
1885 cpi->next_key_frame_forced = FALSE;
1886
1887 cpi->source_alt_ref_pending = FALSE;
1888 cpi->source_alt_ref_active = FALSE;
1889 cpi->common.refresh_alt_ref_frame = 0;
1890
1891 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1892 #if CONFIG_INTERNAL_STATS
1893 cpi->b_calculate_ssimg = 0;
1894
1895 cpi->count = 0;
1896 cpi->bytes = 0;
1897
1898 if (cpi->b_calculate_psnr) {
1899 cpi->total_sq_error = 0.0;
1900 cpi->total_sq_error2 = 0.0;
1901 cpi->total_y = 0.0;
1902 cpi->total_u = 0.0;
1903 cpi->total_v = 0.0;
1904 cpi->total = 0.0;
1905 cpi->totalp_y = 0.0;
1906 cpi->totalp_u = 0.0;
1907 cpi->totalp_v = 0.0;
1908 cpi->totalp = 0.0;
1909 cpi->tot_recode_hits = 0;
1910 cpi->summed_quality = 0;
1911 cpi->summed_weights = 0;
1912 }
1913
1914 if (cpi->b_calculate_ssimg) {
1915 cpi->total_ssimg_y = 0;
1916 cpi->total_ssimg_u = 0;
1917 cpi->total_ssimg_v = 0;
1918 cpi->total_ssimg_all = 0;
1919 }
1920
1921 #endif
1922
1923 #ifndef LLONG_MAX
1924 #define LLONG_MAX 9223372036854775807LL
1925 #endif
1926 cpi->first_time_stamp_ever = LLONG_MAX;
1927
1928 cpi->frames_till_gf_update_due = 0;
1929 cpi->key_frame_count = 1;
1930
1931 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1932 cpi->ni_tot_qi = 0;
1933 cpi->ni_frames = 0;
1934 cpi->tot_q = 0.0;
1935 cpi->avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q);
1936 cpi->total_byte_count = 0;
1937
1938 cpi->rate_correction_factor = 1.0;
1939 cpi->key_frame_rate_correction_factor = 1.0;
1940 cpi->gf_rate_correction_factor = 1.0;
1941 cpi->twopass.est_max_qcorrection_factor = 1.0;
1942
1943 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
1944 cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX];
1945 cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX];
1946 cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX];
1947 cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX];
1948 cal_nmvsadcosts(cpi->mb.nmvsadcost);
1949
1950 cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX];
1951 cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX];
1952 cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX];
1953 cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX];
1954 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
1955
1956 for (i = 0; i < KEY_FRAME_CONTEXT; i++) {
1957 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
1958 }
1959
1960 #ifdef OUTPUT_YUV_SRC
1961 yuv_file = fopen("bd.yuv", "ab");
1962 #endif
1963 #ifdef OUTPUT_YUV_REC
1964 yuv_rec_file = fopen("rec.yuv", "wb");
1965 #endif
1966
1967 #if 0
1968 framepsnr = fopen("framepsnr.stt", "a");
1969 kf_list = fopen("kf_list.stt", "w");
1970 #endif
1971
1972 cpi->output_pkt_list = oxcf->output_pkt_list;
1973
1974 if (cpi->pass == 1) {
1975 vp9_init_first_pass(cpi);
1976 } else if (cpi->pass == 2) {
1977 size_t packet_sz = sizeof(FIRSTPASS_STATS);
1978 int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1979
1980 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1981 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1982 cpi->twopass.stats_in_end = (void *)((char *)cpi->twopass.stats_in
1983 + (packets - 1) * packet_sz);
1984 vp9_init_second_pass(cpi);
1985 }
1986
1987 vp9_set_speed_features(cpi);
1988
1989 // Set starting values of RD threshold multipliers (128 = *1)
1990 for (i = 0; i < MAX_MODES; i++) {
1991 cpi->rd_thresh_mult[i] = 128;
1992 }
1993
1994 #ifdef ENTROPY_STATS
1995 init_mv_ref_counts();
1996 #endif
1997
1998 #define BFP(BT, SDF, VF, SVF, SVFHH, SVFHV, SVFHHV, SDX3F, SDX8F, SDX4DF) \
1999 cpi->fn_ptr[BT].sdf = SDF; \
2000 cpi->fn_ptr[BT].vf = VF; \
2001 cpi->fn_ptr[BT].svf = SVF; \
2002 cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \
2003 cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \
2004 cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \
2005 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2006 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2007 cpi->fn_ptr[BT].sdx4df = SDX4DF;
2008
2009
2010 #if CONFIG_SUPERBLOCKS
2011 BFP(BLOCK_32X32, vp9_sad32x32, vp9_variance32x32, vp9_sub_pixel_variance32x32,
2012 vp9_variance_halfpixvar32x32_h, vp9_variance_halfpixvar32x32_v,
2013 vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8,
2014 vp9_sad32x32x4d)
2015 #endif
2016
2017 BFP(BLOCK_16X16, vp9_sad16x16, vp9_variance16x16, vp9_sub_pixel_variance16x16,
2018 vp9_variance_halfpixvar16x16_h, vp9_variance_halfpixvar16x16_v,
2019 vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8,
2020 vp9_sad16x16x4d)
2021
2022 BFP(BLOCK_16X8, vp9_sad16x8, vp9_variance16x8, vp9_sub_pixel_variance16x8,
2023 NULL, NULL, NULL, vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
2024
2025 BFP(BLOCK_8X16, vp9_sad8x16, vp9_variance8x16, vp9_sub_pixel_variance8x16,
2026 NULL, NULL, NULL, vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
2027
2028 BFP(BLOCK_8X8, vp9_sad8x8, vp9_variance8x8, vp9_sub_pixel_variance8x8,
2029 NULL, NULL, NULL, vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
2030
2031 BFP(BLOCK_4X4, vp9_sad4x4, vp9_variance4x4, vp9_sub_pixel_variance4x4,
2032 NULL, NULL, NULL, vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
2033
2034 #if ARCH_X86 || ARCH_X86_64
2035 cpi->fn_ptr[BLOCK_16X16].copymem = vp9_copy32xn;
2036 cpi->fn_ptr[BLOCK_16X8].copymem = vp9_copy32xn;
2037 cpi->fn_ptr[BLOCK_8X16].copymem = vp9_copy32xn;
2038 cpi->fn_ptr[BLOCK_8X8].copymem = vp9_copy32xn;
2039 cpi->fn_ptr[BLOCK_4X4].copymem = vp9_copy32xn;
2040 #endif
2041
2042 cpi->full_search_sad = vp9_full_search_sad;
2043 cpi->diamond_search_sad = vp9_diamond_search_sad;
2044 cpi->refining_search_sad = vp9_refining_search_sad;
2045
2046 // make sure frame 1 is okay
2047 cpi->error_bins[0] = cpi->common.MBs;
2048
2049 /* vp9_init_quantizer() is first called here. Add check in
2050 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
2051 * called later when needed. This will avoid unnecessary calls of
2052 * vp9_init_quantizer() for every frame.
2053 */
2054 vp9_init_quantizer(cpi);
2055
2056 vp9_loop_filter_init(cm);
2057
2058 cpi->common.error.setjmp = 0;
2059
2060 vp9_zero(cpi->y_uv_mode_count)
2061
2062 return (VP9_PTR) cpi;
2063 }
2064
2065 void vp9_remove_compressor(VP9_PTR *ptr) {
2066 VP9_COMP *cpi = (VP9_COMP *)(*ptr);
2067 int i;
2068
2069 if (!cpi)
2070 return;
2071
2072 if (cpi && (cpi->common.current_video_frame > 0)) {
2073 if (cpi->pass == 2) {
2074 vp9_end_second_pass(cpi);
2075 }
2076
2077 #ifdef ENTROPY_STATS
2078 if (cpi->pass != 1) {
2079 print_context_counters();
2080 print_tree_update_probs();
2081 print_mode_context();
2082 }
2083 #endif
2084 #ifdef NMV_STATS
2085 if (cpi->pass != 1)
2086 print_nmvstats();
2087 #endif
2088
2089 #if CONFIG_INTERNAL_STATS
2090
2091 vp9_clear_system_state();
2092
2093 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
2094 if (cpi->pass != 1) {
2095 FILE *f = fopen("opsnr.stt", "a");
2096 double time_encoded = (cpi->last_end_time_stamp_seen
2097 - cpi->first_time_stamp_ever) / 10000000.000;
2098 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_da ta) / 1000.000;
2099 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded ;
2100 #if defined(MODE_STATS)
2101 print_mode_contexts(&cpi->common);
2102 #endif
2103 if (cpi->b_calculate_psnr) {
2104 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_i dx];
2105 double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_ height;
2106 double total_psnr = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error);
2107 double total_psnr2 = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error2);
2108 double total_ssim = 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2109
2110 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time (ms)\n");
2111 fprintf(f, "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n",
2112 dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->coun t, total_psnr2, total_ssim,
2113 total_encode_time);
2114 // fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f %1 0ld\n",
2115 // dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->count, total_psnr2, total_ssim,
2116 // total_encode_time, cpi->tot_recode_hits);
2117 }
2118
2119 if (cpi->b_calculate_ssimg) {
2120 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n");
2121 fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2122 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count ,
2123 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->cou nt, total_encode_time);
2124 // fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f %10ld\n" , dr,
2125 // cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
2126 // cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time, cpi->tot_recode_hits);
2127 }
2128
2129 fclose(f);
2130 }
2131
2132 #endif
2133
2134
2135 #ifdef MODE_STATS
2136 {
2137 extern int count_mb_seg[4];
2138 char modes_stats_file[250];
2139 FILE *f;
2140 double dr = (double)cpi->oxcf.frame_rate * (double)cpi->bytes * (double)8 / (double)cpi->count / (double)1000;
2141 sprintf(modes_stats_file, "modes_q%03d.stt", cpi->common.base_qindex);
2142 f = fopen(modes_stats_file, "w");
2143 fprintf(f, "intra_mode in Intra Frames:\n");
2144 {
2145 int i;
2146 fprintf(f, "Y: ");
2147 for (i = 0; i < VP9_YMODES; i++) fprintf(f, " %8d,", y_modes[i]);
2148 fprintf(f, "\n");
2149 }
2150 {
2151 int i;
2152 fprintf(f, "I8: ");
2153 for (i = 0; i < VP9_I8X8_MODES; i++) fprintf(f, " %8d,", i8x8_modes[i]);
2154 fprintf(f, "\n");
2155 }
2156 {
2157 int i;
2158 fprintf(f, "UV: ");
2159 for (i = 0; i < VP9_UV_MODES; i++) fprintf(f, " %8d,", uv_modes[i]);
2160 fprintf(f, "\n");
2161 }
2162 {
2163 int i, j;
2164 fprintf(f, "KeyFrame Y-UV:\n");
2165 for (i = 0; i < VP9_YMODES; i++) {
2166 fprintf(f, "%2d:", i);
2167 for (j = 0; j < VP9_UV_MODES; j++) fprintf(f, "%8d, ", uv_modes_y[i][j ]);
2168 fprintf(f, "\n");
2169 }
2170 }
2171 {
2172 int i, j;
2173 fprintf(f, "Inter Y-UV:\n");
2174 for (i = 0; i < VP9_YMODES; i++) {
2175 fprintf(f, "%2d:", i);
2176 for (j = 0; j < VP9_UV_MODES; j++) fprintf(f, "%8d, ", cpi->y_uv_mode_ count[i][j]);
2177 fprintf(f, "\n");
2178 }
2179 }
2180 {
2181 int i;
2182
2183 fprintf(f, "B: ");
2184 for (i = 0; i < VP9_NKF_BINTRAMODES; i++)
2185 fprintf(f, "%8d, ", b_modes[i]);
2186
2187 fprintf(f, "\n");
2188
2189 }
2190
2191 fprintf(f, "Modes in Inter Frames:\n");
2192 {
2193 int i;
2194 fprintf(f, "Y: ");
2195 for (i = 0; i < MB_MODE_COUNT; i++) fprintf(f, " %8d,", inter_y_modes[i] );
2196 fprintf(f, "\n");
2197 }
2198 {
2199 int i;
2200 fprintf(f, "UV: ");
2201 for (i = 0; i < VP9_UV_MODES; i++) fprintf(f, " %8d,", inter_uv_modes[i] );
2202 fprintf(f, "\n");
2203 }
2204 {
2205 int i;
2206 fprintf(f, "B: ");
2207 for (i = 0; i < B_MODE_COUNT; i++) fprintf(f, "%8d, ", inter_b_modes[i]) ;
2208 fprintf(f, "\n");
2209 }
2210 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], cou nt_mb_seg[2], count_mb_seg[3]);
2211 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_mode s[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2212 fclose(f);
2213 }
2214 #endif
2215
2216 #ifdef ENTROPY_STATS
2217 {
2218 int i, j, k;
2219 FILE *fmode = fopen("vp9_modecontext.c", "w");
2220
2221 fprintf(fmode, "\n#include \"vp9_entropymode.h\"\n\n");
2222 fprintf(fmode, "const unsigned int vp9_kf_default_bmode_counts ");
2223 fprintf(fmode, "[VP9_KF_BINTRAMODES][VP9_KF_BINTRAMODES]"
2224 "[VP9_KF_BINTRAMODES] =\n{\n");
2225
2226 for (i = 0; i < VP8_KF_BINTRAMODES; i++) {
2227
2228 fprintf(fmode, " { // Above Mode : %d\n", i);
2229
2230 for (j = 0; j < VP8_KF_BINTRAMODES; j++) {
2231
2232 fprintf(fmode, " {");
2233
2234 for (k = 0; k < VP9_KF_BINTRAMODES; k++) {
2235 if (!intra_mode_stats[i][j][k])
2236 fprintf(fmode, " %5d, ", 1);
2237 else
2238 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2239 }
2240
2241 fprintf(fmode, "}, // left_mode %d\n", j);
2242
2243 }
2244
2245 fprintf(fmode, " },\n");
2246
2247 }
2248
2249 fprintf(fmode, "};\n");
2250 fclose(fmode);
2251 }
2252 #endif
2253
2254
2255 #if defined(SECTIONBITS_OUTPUT)
2256
2257 if (0) {
2258 int i;
2259 FILE *f = fopen("tokenbits.stt", "a");
2260
2261 for (i = 0; i < 28; i++)
2262 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2263
2264 fprintf(f, "\n");
2265 fclose(f);
2266 }
2267
2268 #endif
2269
2270 #if 0
2271 {
2272 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2273 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2274 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, c pi->time_receive_data / 1000, cpi->time_encode_mb_row / 1000, cpi->time_compress _data / 1000, (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2275 }
2276 #endif
2277
2278 }
2279
2280 dealloc_compressor_data(cpi);
2281 vpx_free(cpi->mb.ss);
2282 vpx_free(cpi->tok);
2283
2284 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]); i+ +) {
2285 vpx_free(cpi->mbgraph_stats[i].mb_stats);
2286 }
2287
2288 vp9_remove_common(&cpi->common);
2289 vpx_free(cpi);
2290 *ptr = 0;
2291
2292 #ifdef OUTPUT_YUV_SRC
2293 fclose(yuv_file);
2294 #endif
2295 #ifdef OUTPUT_YUV_REC
2296 fclose(yuv_rec_file);
2297 #endif
2298
2299 #if 0
2300
2301 if (keyfile)
2302 fclose(keyfile);
2303
2304 if (framepsnr)
2305 fclose(framepsnr);
2306
2307 if (kf_list)
2308 fclose(kf_list);
2309
2310 #endif
2311
2312 }
2313
2314
2315 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2316 unsigned char *recon, int recon_stride,
2317 unsigned int cols, unsigned int rows) {
2318 unsigned int row, col;
2319 uint64_t total_sse = 0;
2320 int diff;
2321
2322 for (row = 0; row + 16 <= rows; row += 16) {
2323 for (col = 0; col + 16 <= cols; col += 16) {
2324 unsigned int sse;
2325
2326 vp9_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse);
2327 total_sse += sse;
2328 }
2329
2330 /* Handle odd-sized width */
2331 if (col < cols) {
2332 unsigned int border_row, border_col;
2333 unsigned char *border_orig = orig;
2334 unsigned char *border_recon = recon;
2335
2336 for (border_row = 0; border_row < 16; border_row++) {
2337 for (border_col = col; border_col < cols; border_col++) {
2338 diff = border_orig[border_col] - border_recon[border_col];
2339 total_sse += diff * diff;
2340 }
2341
2342 border_orig += orig_stride;
2343 border_recon += recon_stride;
2344 }
2345 }
2346
2347 orig += orig_stride * 16;
2348 recon += recon_stride * 16;
2349 }
2350
2351 /* Handle odd-sized height */
2352 for (; row < rows; row++) {
2353 for (col = 0; col < cols; col++) {
2354 diff = orig[col] - recon[col];
2355 total_sse += diff * diff;
2356 }
2357
2358 orig += orig_stride;
2359 recon += recon_stride;
2360 }
2361
2362 return total_sse;
2363 }
2364
2365
2366 static void generate_psnr_packet(VP9_COMP *cpi) {
2367 YV12_BUFFER_CONFIG *orig = cpi->Source;
2368 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2369 struct vpx_codec_cx_pkt pkt;
2370 uint64_t sse;
2371 int i;
2372 unsigned int width = cpi->common.Width;
2373 unsigned int height = cpi->common.Height;
2374
2375 pkt.kind = VPX_CODEC_PSNR_PKT;
2376 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2377 recon->y_buffer, recon->y_stride,
2378 width, height);
2379 pkt.data.psnr.sse[0] = sse;
2380 pkt.data.psnr.sse[1] = sse;
2381 pkt.data.psnr.samples[0] = width * height;
2382 pkt.data.psnr.samples[1] = width * height;
2383
2384 width = (width + 1) / 2;
2385 height = (height + 1) / 2;
2386
2387 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2388 recon->u_buffer, recon->uv_stride,
2389 width, height);
2390 pkt.data.psnr.sse[0] += sse;
2391 pkt.data.psnr.sse[2] = sse;
2392 pkt.data.psnr.samples[0] += width * height;
2393 pkt.data.psnr.samples[2] = width * height;
2394
2395 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2396 recon->v_buffer, recon->uv_stride,
2397 width, height);
2398 pkt.data.psnr.sse[0] += sse;
2399 pkt.data.psnr.sse[3] = sse;
2400 pkt.data.psnr.samples[0] += width * height;
2401 pkt.data.psnr.samples[3] = width * height;
2402
2403 for (i = 0; i < 4; i++)
2404 pkt.data.psnr.psnr[i] = vp9_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2405 (double)pkt.data.psnr.sse[i]);
2406
2407 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2408 }
2409
2410
2411 int vp9_use_as_reference(VP9_PTR ptr, int ref_frame_flags) {
2412 VP9_COMP *cpi = (VP9_COMP *)(ptr);
2413
2414 if (ref_frame_flags > 7)
2415 return -1;
2416
2417 cpi->ref_frame_flags = ref_frame_flags;
2418 return 0;
2419 }
2420 int vp9_update_reference(VP9_PTR ptr, int ref_frame_flags) {
2421 VP9_COMP *cpi = (VP9_COMP *)(ptr);
2422
2423 if (ref_frame_flags > 7)
2424 return -1;
2425
2426 cpi->common.refresh_golden_frame = 0;
2427 cpi->common.refresh_alt_ref_frame = 0;
2428 cpi->common.refresh_last_frame = 0;
2429
2430 if (ref_frame_flags & VP9_LAST_FLAG)
2431 cpi->common.refresh_last_frame = 1;
2432
2433 if (ref_frame_flags & VP9_GOLD_FLAG)
2434 cpi->common.refresh_golden_frame = 1;
2435
2436 if (ref_frame_flags & VP9_ALT_FLAG)
2437 cpi->common.refresh_alt_ref_frame = 1;
2438
2439 return 0;
2440 }
2441
2442 int vp9_get_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
2443 YV12_BUFFER_CONFIG *sd) {
2444 VP9_COMP *cpi = (VP9_COMP *)(ptr);
2445 VP9_COMMON *cm = &cpi->common;
2446 int ref_fb_idx;
2447
2448 if (ref_frame_flag == VP9_LAST_FLAG)
2449 ref_fb_idx = cm->lst_fb_idx;
2450 else if (ref_frame_flag == VP9_GOLD_FLAG)
2451 ref_fb_idx = cm->gld_fb_idx;
2452 else if (ref_frame_flag == VP9_ALT_FLAG)
2453 ref_fb_idx = cm->alt_fb_idx;
2454 else
2455 return -1;
2456
2457 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
2458
2459 return 0;
2460 }
2461
2462 int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
2463 YV12_BUFFER_CONFIG *sd) {
2464 VP9_COMP *cpi = (VP9_COMP *)(ptr);
2465 VP9_COMMON *cm = &cpi->common;
2466
2467 int ref_fb_idx;
2468
2469 if (ref_frame_flag == VP9_LAST_FLAG)
2470 ref_fb_idx = cm->lst_fb_idx;
2471 else if (ref_frame_flag == VP9_GOLD_FLAG)
2472 ref_fb_idx = cm->gld_fb_idx;
2473 else if (ref_frame_flag == VP9_ALT_FLAG)
2474 ref_fb_idx = cm->alt_fb_idx;
2475 else
2476 return -1;
2477
2478 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
2479
2480 return 0;
2481 }
2482 int vp9_update_entropy(VP9_PTR comp, int update) {
2483 VP9_COMP *cpi = (VP9_COMP *) comp;
2484 VP9_COMMON *cm = &cpi->common;
2485 cm->refresh_entropy_probs = update;
2486
2487 return 0;
2488 }
2489
2490
2491 #ifdef OUTPUT_YUV_SRC
2492 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) {
2493 unsigned char *src = s->y_buffer;
2494 int h = s->y_height;
2495
2496 do {
2497 fwrite(src, s->y_width, 1, yuv_file);
2498 src += s->y_stride;
2499 } while (--h);
2500
2501 src = s->u_buffer;
2502 h = s->uv_height;
2503
2504 do {
2505 fwrite(src, s->uv_width, 1, yuv_file);
2506 src += s->uv_stride;
2507 } while (--h);
2508
2509 src = s->v_buffer;
2510 h = s->uv_height;
2511
2512 do {
2513 fwrite(src, s->uv_width, 1, yuv_file);
2514 src += s->uv_stride;
2515 } while (--h);
2516 }
2517 #endif
2518
2519 #ifdef OUTPUT_YUV_REC
2520 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2521 YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2522 unsigned char *src = s->y_buffer;
2523 int h = cm->Height;
2524
2525 do {
2526 fwrite(src, s->y_width, 1, yuv_rec_file);
2527 src += s->y_stride;
2528 } while (--h);
2529
2530 src = s->u_buffer;
2531 h = (cm->Height + 1) / 2;
2532
2533 do {
2534 fwrite(src, s->uv_width, 1, yuv_rec_file);
2535 src += s->uv_stride;
2536 } while (--h);
2537
2538 src = s->v_buffer;
2539 h = (cm->Height + 1) / 2;
2540
2541 do {
2542 fwrite(src, s->uv_width, 1, yuv_rec_file);
2543 src += s->uv_stride;
2544 } while (--h);
2545 }
2546 #endif
2547
2548 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
2549 VP9_COMMON *cm = &cpi->common;
2550
2551 // Update data structure that monitors level of reference to last GF
2552 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2553 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2554
2555 // this frame refreshes means next frames don't unless specified by user
2556 cpi->common.frames_since_golden = 0;
2557
2558 // Clear the alternate reference update pending flag.
2559 cpi->source_alt_ref_pending = FALSE;
2560
2561 // Set the alternate refernce frame active flag
2562 cpi->source_alt_ref_active = TRUE;
2563
2564
2565 }
2566 static void update_golden_frame_stats(VP9_COMP *cpi) {
2567 VP9_COMMON *cm = &cpi->common;
2568
2569 // Update the Golden frame usage counts.
2570 if (cm->refresh_golden_frame) {
2571 // Update data structure that monitors level of reference to last GF
2572 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2573 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2574
2575 // this frame refreshes means next frames don't unless specified by user
2576 cm->refresh_golden_frame = 0;
2577 cpi->common.frames_since_golden = 0;
2578
2579 // if ( cm->frame_type == KEY_FRAME )
2580 // {
2581 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2582 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2583 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2584 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2585 // }
2586 // else
2587 // {
2588 // // Carry a potrtion of count over to begining of next gf sequence
2589 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
2590 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
2591 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
2592 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
2593 // }
2594
2595 // ******** Fixed Q test code only ************
2596 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
2597 if (cpi->oxcf.fixed_q >= 0 &&
2598 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame) {
2599 cpi->source_alt_ref_pending = TRUE;
2600 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2601 }
2602
2603 if (!cpi->source_alt_ref_pending)
2604 cpi->source_alt_ref_active = FALSE;
2605
2606 // Decrement count down till next gf
2607 if (cpi->frames_till_gf_update_due > 0)
2608 cpi->frames_till_gf_update_due--;
2609
2610 } else if (!cpi->common.refresh_alt_ref_frame) {
2611 // Decrement count down till next gf
2612 if (cpi->frames_till_gf_update_due > 0)
2613 cpi->frames_till_gf_update_due--;
2614
2615 if (cpi->common.frames_till_alt_ref_frame)
2616 cpi->common.frames_till_alt_ref_frame--;
2617
2618 cpi->common.frames_since_golden++;
2619
2620 if (cpi->common.frames_since_golden > 1) {
2621 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[ INTRA_FRAME];
2622 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[L AST_FRAME];
2623 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage [GOLDEN_FRAME];
2624 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage [ALTREF_FRAME];
2625 }
2626 }
2627 }
2628
2629 static int find_fp_qindex() {
2630 int i;
2631
2632 for (i = 0; i < QINDEX_RANGE; i++) {
2633 if (vp9_convert_qindex_to_q(i) >= 30.0) {
2634 break;
2635 }
2636 }
2637
2638 if (i == QINDEX_RANGE)
2639 i--;
2640
2641 return i;
2642 }
2643
2644 static void Pass1Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags) {
2645 (void) size;
2646 (void) dest;
2647 (void) frame_flags;
2648
2649
2650 vp9_set_quantizer(cpi, find_fp_qindex());
2651 vp9_first_pass(cpi);
2652 }
2653
2654 #define WRITE_RECON_BUFFER 0
2655 #if WRITE_RECON_BUFFER
2656 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) {
2657
2658 // write the frame
2659 FILE *yframe;
2660 int i;
2661 char filename[255];
2662
2663 sprintf(filename, "cx\\y%04d.raw", this_frame);
2664 yframe = fopen(filename, "wb");
2665
2666 for (i = 0; i < frame->y_height; i++)
2667 fwrite(frame->y_buffer + i * frame->y_stride,
2668 frame->y_width, 1, yframe);
2669
2670 fclose(yframe);
2671 sprintf(filename, "cx\\u%04d.raw", this_frame);
2672 yframe = fopen(filename, "wb");
2673
2674 for (i = 0; i < frame->uv_height; i++)
2675 fwrite(frame->u_buffer + i * frame->uv_stride,
2676 frame->uv_width, 1, yframe);
2677
2678 fclose(yframe);
2679 sprintf(filename, "cx\\v%04d.raw", this_frame);
2680 yframe = fopen(filename, "wb");
2681
2682 for (i = 0; i < frame->uv_height; i++)
2683 fwrite(frame->v_buffer + i * frame->uv_stride,
2684 frame->uv_width, 1, yframe);
2685
2686 fclose(yframe);
2687 }
2688 #endif
2689
2690 static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG *frame) {
2691 #define EDGE_THRESH 128
2692 int i, j;
2693 int num_edge_pels = 0;
2694 int num_pels = (frame->y_height - 2) * (frame->y_width - 2);
2695 unsigned char *prev = frame->y_buffer + 1;
2696 unsigned char *curr = frame->y_buffer + 1 + frame->y_stride;
2697 unsigned char *next = frame->y_buffer + 1 + 2 * frame->y_stride;
2698 for (i = 1; i < frame->y_height - 1; i++) {
2699 for (j = 1; j < frame->y_width - 1; j++) {
2700 /* Sobel hor and ver gradients */
2701 int v = 2 * (curr[1] - curr[-1]) + (prev[1] - prev[-1]) + (next[1] - next[ -1]);
2702 int h = 2 * (prev[0] - next[0]) + (prev[1] - next[1]) + (prev[-1] - next[- 1]);
2703 h = (h < 0 ? -h : h);
2704 v = (v < 0 ? -v : v);
2705 if (h > EDGE_THRESH || v > EDGE_THRESH) num_edge_pels++;
2706 curr++;
2707 prev++;
2708 next++;
2709 }
2710 curr += frame->y_stride - frame->y_width + 2;
2711 prev += frame->y_stride - frame->y_width + 2;
2712 next += frame->y_stride - frame->y_width + 2;
2713 }
2714 return (double)num_edge_pels / (double)num_pels;
2715 }
2716
2717 // Function to test for conditions that indicate we should loop
2718 // back and recode a frame.
2719 static BOOL recode_loop_test(VP9_COMP *cpi,
2720 int high_limit, int low_limit,
2721 int q, int maxq, int minq) {
2722 BOOL force_recode = FALSE;
2723 VP9_COMMON *cm = &cpi->common;
2724
2725 // Is frame recode allowed at all
2726 // Yes if either recode mode 1 is selected or mode two is selcted
2727 // and the frame is a key frame. golden frame or alt_ref_frame
2728 if ((cpi->sf.recode_loop == 1) ||
2729 ((cpi->sf.recode_loop == 2) &&
2730 ((cm->frame_type == KEY_FRAME) ||
2731 cm->refresh_golden_frame ||
2732 cm->refresh_alt_ref_frame))) {
2733 // General over and under shoot tests
2734 if (((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
2735 ((cpi->projected_frame_size < low_limit) && (q > minq))) {
2736 force_recode = TRUE;
2737 }
2738 // Special Constrained quality tests
2739 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2740 // Undershoot and below auto cq level
2741 if ((q > cpi->cq_target_quality) &&
2742 (cpi->projected_frame_size <
2743 ((cpi->this_frame_target * 7) >> 3))) {
2744 force_recode = TRUE;
2745 }
2746 // Severe undershoot and between auto and user cq level
2747 else if ((q > cpi->oxcf.cq_level) &&
2748 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
2749 (cpi->active_best_quality > cpi->oxcf.cq_level)) {
2750 force_recode = TRUE;
2751 cpi->active_best_quality = cpi->oxcf.cq_level;
2752 }
2753 }
2754 }
2755
2756 return force_recode;
2757 }
2758
2759 static void update_reference_frames(VP9_COMMON *cm) {
2760 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
2761
2762 // At this point the new frame has been encoded.
2763 // If any buffer copy / swapping is signaled it should be done here.
2764
2765 if (cm->frame_type == KEY_FRAME) {
2766 yv12_fb[cm->new_fb_idx].flags |= VP9_GOLD_FLAG | VP9_ALT_FLAG;
2767
2768 yv12_fb[cm->gld_fb_idx].flags &= ~VP9_GOLD_FLAG;
2769 yv12_fb[cm->alt_fb_idx].flags &= ~VP9_ALT_FLAG;
2770
2771 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
2772 } else { /* For non key frames */
2773 if (cm->refresh_alt_ref_frame) {
2774 assert(!cm->copy_buffer_to_arf);
2775
2776 cm->yv12_fb[cm->new_fb_idx].flags |= VP9_ALT_FLAG;
2777 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP9_ALT_FLAG;
2778 cm->alt_fb_idx = cm->new_fb_idx;
2779 } else if (cm->copy_buffer_to_arf) {
2780 assert(!(cm->copy_buffer_to_arf & ~0x3));
2781
2782 if (cm->copy_buffer_to_arf == 1) {
2783 if (cm->alt_fb_idx != cm->lst_fb_idx) {
2784 yv12_fb[cm->lst_fb_idx].flags |= VP9_ALT_FLAG;
2785 yv12_fb[cm->alt_fb_idx].flags &= ~VP9_ALT_FLAG;
2786 cm->alt_fb_idx = cm->lst_fb_idx;
2787 }
2788 } else { /* if (cm->copy_buffer_to_arf == 2) */
2789 if (cm->alt_fb_idx != cm->gld_fb_idx) {
2790 yv12_fb[cm->gld_fb_idx].flags |= VP9_ALT_FLAG;
2791 yv12_fb[cm->alt_fb_idx].flags &= ~VP9_ALT_FLAG;
2792 cm->alt_fb_idx = cm->gld_fb_idx;
2793 }
2794 }
2795 }
2796
2797 if (cm->refresh_golden_frame) {
2798 assert(!cm->copy_buffer_to_gf);
2799
2800 cm->yv12_fb[cm->new_fb_idx].flags |= VP9_GOLD_FLAG;
2801 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP9_GOLD_FLAG;
2802 cm->gld_fb_idx = cm->new_fb_idx;
2803 } else if (cm->copy_buffer_to_gf) {
2804 assert(!(cm->copy_buffer_to_arf & ~0x3));
2805
2806 if (cm->copy_buffer_to_gf == 1) {
2807 if (cm->gld_fb_idx != cm->lst_fb_idx) {
2808 yv12_fb[cm->lst_fb_idx].flags |= VP9_GOLD_FLAG;
2809 yv12_fb[cm->gld_fb_idx].flags &= ~VP9_GOLD_FLAG;
2810 cm->gld_fb_idx = cm->lst_fb_idx;
2811 }
2812 } else { /* if (cm->copy_buffer_to_gf == 2) */
2813 if (cm->alt_fb_idx != cm->gld_fb_idx) {
2814 yv12_fb[cm->alt_fb_idx].flags |= VP9_GOLD_FLAG;
2815 yv12_fb[cm->gld_fb_idx].flags &= ~VP9_GOLD_FLAG;
2816 cm->gld_fb_idx = cm->alt_fb_idx;
2817 }
2818 }
2819 }
2820 }
2821
2822 if (cm->refresh_last_frame) {
2823 cm->yv12_fb[cm->new_fb_idx].flags |= VP9_LAST_FLAG;
2824 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP9_LAST_FLAG;
2825 cm->lst_fb_idx = cm->new_fb_idx;
2826 }
2827 }
2828
2829 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2830 if (cm->no_lpf) {
2831 cm->filter_level = 0;
2832 }
2833 #if CONFIG_LOSSLESS
2834 else if (cpi->oxcf.lossless) {
2835 cm->filter_level = 0;
2836 }
2837 #endif
2838 else {
2839 struct vpx_usec_timer timer;
2840
2841 vp9_clear_system_state();
2842
2843 vpx_usec_timer_start(&timer);
2844 if (cpi->sf.auto_filter == 0)
2845 vp9_pick_filter_level_fast(cpi->Source, cpi);
2846 else
2847 vp9_pick_filter_level(cpi->Source, cpi);
2848
2849 vpx_usec_timer_mark(&timer);
2850 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2851 }
2852
2853 if (cm->filter_level > 0) {
2854 vp9_set_alt_lf_level(cpi, cm->filter_level);
2855 vp9_loop_filter_frame(cm, &cpi->mb.e_mbd);
2856 }
2857
2858 vp8_yv12_extend_frame_borders(cm->frame_to_show);
2859
2860 }
2861
2862 #if CONFIG_PRED_FILTER
2863 void select_pred_filter_mode(VP9_COMP *cpi) {
2864 VP9_COMMON *cm = &cpi->common;
2865
2866 int prob_pred_filter_off = cm->prob_pred_filter_off;
2867
2868 // Force filter on/off if probability is extreme
2869 if (prob_pred_filter_off >= 255 * 0.95)
2870 cm->pred_filter_mode = 0; // Off at the frame level
2871 else if (prob_pred_filter_off <= 255 * 0.05)
2872 cm->pred_filter_mode = 1; // On at the frame level
2873 else
2874 cm->pred_filter_mode = 2; // Selectable at the MB level
2875 }
2876
2877 void update_pred_filt_prob(VP9_COMP *cpi) {
2878 VP9_COMMON *cm = &cpi->common;
2879 int prob_pred_filter_off;
2880
2881 // Based on the selection in the previous frame determine what mode
2882 // to use for the current frame and work out the signaling probability
2883 if (cpi->pred_filter_on_count + cpi->pred_filter_off_count) {
2884 prob_pred_filter_off = cpi->pred_filter_off_count * 256 /
2885 (cpi->pred_filter_on_count + cpi->pred_filter_off_cou nt);
2886
2887 if (prob_pred_filter_off < 1)
2888 prob_pred_filter_off = 1;
2889
2890 if (prob_pred_filter_off > 255)
2891 prob_pred_filter_off = 255;
2892
2893 cm->prob_pred_filter_off = prob_pred_filter_off;
2894 } else
2895 cm->prob_pred_filter_off = 128;
2896 /*
2897 {
2898 FILE *fp = fopen("filt_use.txt", "a");
2899 fprintf (fp, "%d %d prob=%d\n", cpi->pred_filter_off_count,
2900 cpi->pred_filter_on_count, cm->prob_pred_filter_off);
2901 fclose(fp);
2902 }
2903 */
2904 }
2905 #endif
2906 #if CONFIG_COMP_INTERINTRA_PRED
2907 static void select_interintra_mode(VP9_COMP *cpi) {
2908 static const double threshold = 0.01;
2909 VP9_COMMON *cm = &cpi->common;
2910 // FIXME(debargha): Make this RD based
2911 int sum = cpi->interintra_select_count[1] + cpi->interintra_select_count[0];
2912 if (sum) {
2913 double fraction = (double) cpi->interintra_select_count[1] / sum;
2914 // printf("fraction: %f\n", fraction);
2915 cm->use_interintra = (fraction > threshold);
2916 }
2917 }
2918 #endif
2919
2920 static void encode_frame_to_data_rate
2921 (
2922 VP9_COMP *cpi,
2923 unsigned long *size,
2924 unsigned char *dest,
2925 unsigned int *frame_flags
2926 ) {
2927 VP9_COMMON *cm = &cpi->common;
2928 MACROBLOCKD *xd = &cpi->mb.e_mbd;
2929
2930 int Q;
2931 int frame_over_shoot_limit;
2932 int frame_under_shoot_limit;
2933
2934 int Loop = FALSE;
2935 int loop_count;
2936 int this_q;
2937 int last_zbin_oq;
2938
2939 int q_low;
2940 int q_high;
2941 int zbin_oq_high;
2942 int zbin_oq_low = 0;
2943
2944 int top_index;
2945 int bottom_index;
2946 int active_worst_qchanged = FALSE;
2947
2948 int overshoot_seen = FALSE;
2949 int undershoot_seen = FALSE;
2950
2951 int loop_size_estimate = 0;
2952
2953 SPEED_FEATURES *sf = &cpi->sf;
2954 #if RESET_FOREACH_FILTER
2955 int q_low0;
2956 int q_high0;
2957 int zbin_oq_high0;
2958 int zbin_oq_low0 = 0;
2959 int Q0;
2960 int last_zbin_oq0;
2961 int active_best_quality0;
2962 int active_worst_quality0;
2963 double rate_correction_factor0;
2964 double gf_rate_correction_factor0;
2965 #endif
2966
2967 /* list of filters to search over */
2968 int mcomp_filters_to_search[] = {
2969 EIGHTTAP, EIGHTTAP_SHARP, SIXTAP, SWITCHABLE
2970 };
2971 int mcomp_filters = sizeof(mcomp_filters_to_search) /
2972 sizeof(*mcomp_filters_to_search);
2973 int mcomp_filter_index = 0;
2974 INT64 mcomp_filter_cost[4];
2975
2976 // Clear down mmx registers to allow floating point in what follows
2977 vp9_clear_system_state();
2978
2979
2980 // For an alt ref frame in 2 pass we skip the call to the second
2981 // pass function that sets the target bandwidth so must set it here
2982 if (cpi->common.refresh_alt_ref_frame) {
2983 cpi->per_frame_bandwidth = cpi->twopass.gf_bits; / / Per frame bit target for the alt ref frame
2984 // per second target bitrate
2985 cpi->target_bandwidth = (int)(cpi->twopass.gf_bits *
2986 cpi->output_frame_rate);
2987 }
2988
2989 // Default turn off buffer to buffer copying
2990 cm->copy_buffer_to_gf = 0;
2991 cm->copy_buffer_to_arf = 0;
2992
2993 // Clear zbin over-quant value and mode boost values.
2994 cpi->zbin_over_quant = 0;
2995 cpi->zbin_mode_boost = 0;
2996
2997 // Enable or disable mode based tweaking of the zbin
2998 // For 2 Pass Only used where GF/ARF prediction quality
2999 // is above a threshold
3000 cpi->zbin_mode_boost = 0;
3001 #if CONFIG_LOSSLESS
3002 cpi->zbin_mode_boost_enabled = FALSE;
3003 #else
3004 cpi->zbin_mode_boost_enabled = TRUE;
3005 #endif
3006 if (cpi->gfu_boost <= 400) {
3007 cpi->zbin_mode_boost_enabled = FALSE;
3008 }
3009
3010 // Current default encoder behaviour for the altref sign bias
3011 if (cpi->source_alt_ref_active)
3012 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3013 else
3014 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3015
3016 // Check to see if a key frame is signalled
3017 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3018 if ((cm->current_video_frame == 0) ||
3019 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3020 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency = = 0))) {
3021 // Key frame from VFW/auto-keyframe/first frame
3022 cm->frame_type = KEY_FRAME;
3023 }
3024
3025 // Set default state for segment based loop filter update flags
3026 xd->mode_ref_lf_delta_update = 0;
3027
3028 #if CONFIG_NEW_MVREF
3029 // Temp defaults probabilities for ecnoding the MV ref id signal
3030 vpx_memset(xd->mb_mv_ref_id_probs, 192,
3031 sizeof(xd->mb_mv_ref_id_probs));
3032 #endif
3033
3034 // Set various flags etc to special state if it is a key frame
3035 if (cm->frame_type == KEY_FRAME) {
3036 int i;
3037
3038 // Reset the loop filter deltas and segmentation map
3039 setup_features(cpi);
3040
3041 // If segmentation is enabled force a map update for key frames
3042 if (xd->segmentation_enabled) {
3043 xd->update_mb_segmentation_map = 1;
3044 xd->update_mb_segmentation_data = 1;
3045 }
3046
3047 // The alternate reference frame cannot be active for a key frame
3048 cpi->source_alt_ref_active = FALSE;
3049
3050 // Reset the RD threshold multipliers to default of * 1 (128)
3051 for (i = 0; i < MAX_MODES; i++) {
3052 cpi->rd_thresh_mult[i] = 128;
3053 }
3054 }
3055
3056 // Test code for new segment features
3057 init_seg_features(cpi);
3058
3059 // Decide how big to make the frame
3060 vp9_pick_frame_size(cpi);
3061
3062 vp9_clear_system_state();
3063
3064 // Set an active best quality and if necessary active worst quality
3065 Q = cpi->active_worst_quality;
3066
3067 if (cm->frame_type == KEY_FRAME) {
3068 int high = 2000;
3069 int low = 400;
3070
3071 if (cpi->kf_boost > high)
3072 cpi->active_best_quality = kf_low_motion_minq[Q];
3073 else if (cpi->kf_boost < low)
3074 cpi->active_best_quality = kf_high_motion_minq[Q];
3075 else {
3076 int gap = high - low;
3077 int offset = high - cpi->kf_boost;
3078 int qdiff = kf_high_motion_minq[Q] - kf_low_motion_minq[Q];
3079 int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
3080
3081 cpi->active_best_quality = kf_low_motion_minq[Q] + adjustment;
3082 }
3083
3084 // Make an adjustment based on the %s static
3085 // The main impact of this is at lower Q to prevent overly large key
3086 // frames unless a lot of the image is static.
3087 if (cpi->kf_zeromotion_pct < 64)
3088 cpi->active_best_quality += 4 - (cpi->kf_zeromotion_pct >> 4);
3089
3090 // Special case for key frames forced because we have reached
3091 // the maximum key frame interval. Here force the Q to a range
3092 // based on the ambient Q to reduce the risk of popping
3093 if (cpi->this_key_frame_forced) {
3094 int delta_qindex;
3095 int qindex = cpi->last_boosted_qindex;
3096
3097 delta_qindex = compute_qdelta(cpi, qindex,
3098 (qindex * 0.75));
3099
3100 cpi->active_best_quality = qindex + delta_qindex;
3101 if (cpi->active_best_quality < cpi->best_quality)
3102 cpi->active_best_quality = cpi->best_quality;
3103 }
3104 }
3105
3106 else if (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame) {
3107 int high = 2000;
3108 int low = 400;
3109
3110 // Use the lower of cpi->active_worst_quality and recent
3111 // average Q as basis for GF/ARF Q limit unless last frame was
3112 // a key frame.
3113 if ((cpi->frames_since_key > 1) &&
3114 (cpi->avg_frame_qindex < cpi->active_worst_quality)) {
3115 Q = cpi->avg_frame_qindex;
3116 }
3117
3118 // For constrained quality dont allow Q less than the cq level
3119 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3120 (Q < cpi->cq_target_quality)) {
3121 Q = cpi->cq_target_quality;
3122 }
3123
3124 if (cpi->gfu_boost > high)
3125 cpi->active_best_quality = gf_low_motion_minq[Q];
3126 else if (cpi->gfu_boost < low)
3127 cpi->active_best_quality = gf_high_motion_minq[Q];
3128 else {
3129 int gap = high - low;
3130 int offset = high - cpi->gfu_boost;
3131 int qdiff = gf_high_motion_minq[Q] - gf_low_motion_minq[Q];
3132 int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
3133
3134 cpi->active_best_quality = gf_low_motion_minq[Q] + adjustment;
3135 }
3136
3137 // Constrained quality use slightly lower active best.
3138 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
3139 cpi->active_best_quality =
3140 cpi->active_best_quality * 15 / 16;
3141 }
3142 } else {
3143 cpi->active_best_quality = inter_minq[Q];
3144
3145 // For the constant/constrained quality mode we dont want
3146 // q to fall below the cq level.
3147 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3148 (cpi->active_best_quality < cpi->cq_target_quality)) {
3149 // If we are strongly undershooting the target rate in the last
3150 // frames then use the user passed in cq value not the auto
3151 // cq value.
3152 if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth)
3153 cpi->active_best_quality = cpi->oxcf.cq_level;
3154 else
3155 cpi->active_best_quality = cpi->cq_target_quality;
3156 }
3157 }
3158
3159 // Clip the active best and worst quality values to limits
3160 if (cpi->active_worst_quality > cpi->worst_quality)
3161 cpi->active_worst_quality = cpi->worst_quality;
3162
3163 if (cpi->active_best_quality < cpi->best_quality)
3164 cpi->active_best_quality = cpi->best_quality;
3165
3166 if (cpi->active_best_quality > cpi->worst_quality)
3167 cpi->active_best_quality = cpi->worst_quality;
3168
3169 if (cpi->active_worst_quality < cpi->active_best_quality)
3170 cpi->active_worst_quality = cpi->active_best_quality;
3171
3172 // Specuial case code to try and match quality with forced key frames
3173 if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
3174 Q = cpi->last_boosted_qindex;
3175 } else {
3176 // Determine initial Q to try
3177 Q = vp9_regulate_q(cpi, cpi->this_frame_target);
3178 }
3179 last_zbin_oq = cpi->zbin_over_quant;
3180
3181 // Set highest allowed value for Zbin over quant
3182 if (cm->frame_type == KEY_FRAME)
3183 zbin_oq_high = 0; // ZBIN_OQ_MAX/16
3184 else if (cm->refresh_alt_ref_frame || (cm->refresh_golden_frame && !cpi->sourc e_alt_ref_active))
3185 zbin_oq_high = 16;
3186 else
3187 zbin_oq_high = ZBIN_OQ_MAX;
3188
3189 vp9_compute_frame_size_bounds(cpi, &frame_under_shoot_limit,
3190 &frame_over_shoot_limit);
3191
3192 // Limit Q range for the adaptive loop.
3193 bottom_index = cpi->active_best_quality;
3194 top_index = cpi->active_worst_quality;
3195 q_low = cpi->active_best_quality;
3196 q_high = cpi->active_worst_quality;
3197
3198 loop_count = 0;
3199
3200 if (cm->frame_type != KEY_FRAME) {
3201 /* TODO: Decide this more intelligently */
3202 if (sf->search_best_filter) {
3203 cm->mcomp_filter_type = mcomp_filters_to_search[0];
3204 mcomp_filter_index = 0;
3205 } else {
3206 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER;
3207 }
3208 /* TODO: Decide this more intelligently */
3209 xd->allow_high_precision_mv = (Q < HIGH_PRECISION_MV_QTHRESH);
3210 set_mvcost(&cpi->mb);
3211 }
3212
3213 #if CONFIG_COMP_INTERINTRA_PRED
3214 if (cm->current_video_frame == 0) {
3215 cm->use_interintra = 1;
3216 }
3217 #endif
3218
3219 #if CONFIG_POSTPROC
3220
3221 if (cpi->oxcf.noise_sensitivity > 0) {
3222 unsigned char *src;
3223 int l = 0;
3224
3225 switch (cpi->oxcf.noise_sensitivity) {
3226 case 1:
3227 l = 20;
3228 break;
3229 case 2:
3230 l = 40;
3231 break;
3232 case 3:
3233 l = 60;
3234 break;
3235 case 4:
3236
3237 case 5:
3238 l = 100;
3239 break;
3240 case 6:
3241 l = 150;
3242 break;
3243 }
3244
3245
3246 if (cm->frame_type == KEY_FRAME) {
3247 vp9_de_noise(cpi->Source, cpi->Source, l, 1, 0);
3248 } else {
3249 vp9_de_noise(cpi->Source, cpi->Source, l, 1, 0);
3250
3251 src = cpi->Source->y_buffer;
3252
3253 if (cpi->Source->y_stride < 0) {
3254 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3255 }
3256 }
3257 }
3258
3259 #endif
3260
3261 #ifdef OUTPUT_YUV_SRC
3262 vp9_write_yuv_frame(cpi->Source);
3263 #endif
3264
3265 #if RESET_FOREACH_FILTER
3266 if (sf->search_best_filter) {
3267 q_low0 = q_low;
3268 q_high0 = q_high;
3269 Q0 = Q;
3270 zbin_oq_low0 = zbin_oq_low;
3271 zbin_oq_high0 = zbin_oq_high;
3272 last_zbin_oq0 = last_zbin_oq;
3273 rate_correction_factor0 = cpi->rate_correction_factor;
3274 gf_rate_correction_factor0 = cpi->gf_rate_correction_factor;
3275 active_best_quality0 = cpi->active_best_quality;
3276 active_worst_quality0 = cpi->active_worst_quality;
3277 }
3278 #endif
3279 do {
3280 vp9_clear_system_state(); // __asm emms;
3281
3282 vp9_set_quantizer(cpi, Q);
3283 this_q = Q;
3284
3285 if (loop_count == 0) {
3286
3287 // setup skip prob for costing in mode/mv decision
3288 if (cpi->common.mb_no_coeff_skip) {
3289 int k;
3290 for (k = 0; k < MBSKIP_CONTEXTS; k++)
3291 cm->mbskip_pred_probs[k] = cpi->base_skip_false_prob[Q][k];
3292
3293 if (cm->frame_type != KEY_FRAME) {
3294 if (cpi->common.refresh_alt_ref_frame) {
3295 for (k = 0; k < MBSKIP_CONTEXTS; k++) {
3296 if (cpi->last_skip_false_probs[2][k] != 0)
3297 cm->mbskip_pred_probs[k] = cpi->last_skip_false_probs[2][k];
3298 }
3299 } else if (cpi->common.refresh_golden_frame) {
3300 for (k = 0; k < MBSKIP_CONTEXTS; k++) {
3301 if (cpi->last_skip_false_probs[1][k] != 0)
3302 cm->mbskip_pred_probs[k] = cpi->last_skip_false_probs[1][k];
3303 }
3304 } else {
3305 int k;
3306 for (k = 0; k < MBSKIP_CONTEXTS; k++) {
3307 if (cpi->last_skip_false_probs[0][k] != 0)
3308 cm->mbskip_pred_probs[k] = cpi->last_skip_false_probs[0][k];
3309 }
3310 }
3311
3312 // as this is for cost estimate, let's make sure it does not
3313 // get extreme either way
3314 {
3315 int k;
3316 for (k = 0; k < MBSKIP_CONTEXTS; ++k) {
3317 if (cm->mbskip_pred_probs[k] < 5)
3318 cm->mbskip_pred_probs[k] = 5;
3319
3320 if (cm->mbskip_pred_probs[k] > 250)
3321 cm->mbskip_pred_probs[k] = 250;
3322
3323 if (cpi->is_src_frame_alt_ref)
3324 cm->mbskip_pred_probs[k] = 1;
3325 }
3326 }
3327 }
3328 }
3329
3330 // Set up entropy depending on frame type.
3331 if (cm->frame_type == KEY_FRAME)
3332 vp9_setup_key_frame(cpi);
3333 else
3334 vp9_setup_inter_frame(cpi);
3335 }
3336
3337 // transform / motion compensation build reconstruction frame
3338
3339 vp9_encode_frame(cpi);
3340
3341 // Update the skip mb flag probabilities based on the distribution
3342 // seen in the last encoder iteration.
3343 update_base_skip_probs(cpi);
3344
3345 vp9_clear_system_state(); // __asm emms;
3346
3347 #if CONFIG_PRED_FILTER
3348 // Update prediction filter on/off probability based on
3349 // selection made for the current frame
3350 if (cm->frame_type != KEY_FRAME)
3351 update_pred_filt_prob(cpi);
3352 #endif
3353
3354 // Dummy pack of the bitstream using up to date stats to get an
3355 // accurate estimate of output frame size to determine if we need
3356 // to recode.
3357 vp9_save_coding_context(cpi);
3358 cpi->dummy_packing = 1;
3359 vp9_pack_bitstream(cpi, dest, size);
3360 cpi->projected_frame_size = (*size) << 3;
3361 vp9_restore_coding_context(cpi);
3362
3363 if (frame_over_shoot_limit == 0)
3364 frame_over_shoot_limit = 1;
3365 active_worst_qchanged = FALSE;
3366
3367 // Special case handling for forced key frames
3368 if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
3369 int last_q = Q;
3370 int kf_err = vp9_calc_ss_err(cpi->Source,
3371 &cm->yv12_fb[cm->new_fb_idx]);
3372
3373 int high_err_target = cpi->ambient_err;
3374 int low_err_target = (cpi->ambient_err >> 1);
3375
3376 // Prevent possible divide by zero error below for perfect KF
3377 kf_err += (!kf_err);
3378
3379 // The key frame is not good enough or we can afford
3380 // to make it better without undue risk of popping.
3381 if (((kf_err > high_err_target) &&
3382 (cpi->projected_frame_size <= frame_over_shoot_limit)) ||
3383 ((kf_err > low_err_target) &&
3384 (cpi->projected_frame_size <= frame_under_shoot_limit))) {
3385 // Lower q_high
3386 q_high = (Q > q_low) ? (Q - 1) : q_low;
3387
3388 // Adjust Q
3389 Q = (Q * high_err_target) / kf_err;
3390 if (Q < ((q_high + q_low) >> 1))
3391 Q = (q_high + q_low) >> 1;
3392 }
3393 // The key frame is much better than the previous frame
3394 else if ((kf_err < low_err_target) &&
3395 (cpi->projected_frame_size >= frame_under_shoot_limit)) {
3396 // Raise q_low
3397 q_low = (Q < q_high) ? (Q + 1) : q_high;
3398
3399 // Adjust Q
3400 Q = (Q * low_err_target) / kf_err;
3401 if (Q > ((q_high + q_low + 1) >> 1))
3402 Q = (q_high + q_low + 1) >> 1;
3403 }
3404
3405 // Clamp Q to upper and lower limits:
3406 if (Q > q_high)
3407 Q = q_high;
3408 else if (Q < q_low)
3409 Q = q_low;
3410
3411 Loop = ((Q != last_q)) ? TRUE : FALSE;
3412 }
3413
3414 // Is the projected frame size out of range and are we allowed to attempt to recode.
3415 else if (recode_loop_test(cpi,
3416 frame_over_shoot_limit, frame_under_shoot_limit,
3417 Q, top_index, bottom_index)) {
3418 int last_q = Q;
3419 int Retries = 0;
3420
3421 // Frame size out of permitted range:
3422 // Update correction factor & compute new Q to try...
3423
3424 // Frame is too large
3425 if (cpi->projected_frame_size > cpi->this_frame_target) {
3426 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least th e current value
3427
3428 if (cpi->zbin_over_quant > 0) // If we are using over quant d o the same for zbin_oq_low
3429 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_ quant + 1) : zbin_oq_high;
3430
3431 if (undershoot_seen || (loop_count > 1)) {
3432 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3433 if (!active_worst_qchanged)
3434 vp9_update_rate_correction_factors(cpi, 1);
3435
3436 Q = (q_high + q_low + 1) / 2;
3437
3438 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
3439 if (Q < MAXQ)
3440 cpi->zbin_over_quant = 0;
3441 else {
3442 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_ove r_quant + 1) : zbin_oq_high;
3443 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
3444 }
3445 } else {
3446 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3447 if (!active_worst_qchanged)
3448 vp9_update_rate_correction_factors(cpi, 0);
3449
3450 Q = vp9_regulate_q(cpi, cpi->this_frame_target);
3451
3452 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retri es < 10)) {
3453 vp9_update_rate_correction_factors(cpi, 0);
3454 Q = vp9_regulate_q(cpi, cpi->this_frame_target);
3455 Retries++;
3456 }
3457 }
3458
3459 overshoot_seen = TRUE;
3460 }
3461 // Frame is too small
3462 else {
3463 if (cpi->zbin_over_quant == 0)
3464 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using o ver quant
3465 else // else lower zbin_oq_high
3466 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_ quant - 1) : zbin_oq_low;
3467
3468 if (overshoot_seen || (loop_count > 1)) {
3469 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3470 if (!active_worst_qchanged)
3471 vp9_update_rate_correction_factors(cpi, 1);
3472
3473 Q = (q_high + q_low) / 2;
3474
3475 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
3476 if (Q < MAXQ)
3477 cpi->zbin_over_quant = 0;
3478 else
3479 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
3480 } else {
3481 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3482 if (!active_worst_qchanged)
3483 vp9_update_rate_correction_factors(cpi, 0);
3484
3485 Q = vp9_regulate_q(cpi, cpi->this_frame_target);
3486
3487 // Special case reset for qlow for constrained quality.
3488 // This should only trigger where there is very substantial
3489 // undershoot on a frame and the auto cq level is above
3490 // the user passsed in value.
3491 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3492 (Q < q_low)) {
3493 q_low = Q;
3494 }
3495
3496 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Ret ries < 10)) {
3497 vp9_update_rate_correction_factors(cpi, 0);
3498 Q = vp9_regulate_q(cpi, cpi->this_frame_target);
3499 Retries++;
3500 }
3501 }
3502
3503 undershoot_seen = TRUE;
3504 }
3505
3506 // Clamp Q to upper and lower limits:
3507 if (Q > q_high)
3508 Q = q_high;
3509 else if (Q < q_low)
3510 Q = q_low;
3511
3512 // Clamp cpi->zbin_over_quant
3513 cpi->zbin_over_quant = (cpi->zbin_over_quant < zbin_oq_low) ?
3514 zbin_oq_low : (cpi->zbin_over_quant > zbin_oq_high) ?
3515 zbin_oq_high : cpi->zbin_over_quant;
3516
3517 // Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
3518 Loop = ((Q != last_q)) ? TRUE : FALSE;
3519 last_zbin_oq = cpi->zbin_over_quant;
3520 } else
3521 Loop = FALSE;
3522
3523 if (cpi->is_src_frame_alt_ref)
3524 Loop = FALSE;
3525
3526 if (cm->frame_type != KEY_FRAME &&
3527 !sf->search_best_filter &&
3528 cm->mcomp_filter_type == SWITCHABLE) {
3529 int interp_factor = Q / 3; /* denominator is 256 */
3530 int count[VP9_SWITCHABLE_FILTERS];
3531 int tot_count = 0, c = 0, thr;
3532 int i, j;
3533 for (i = 0; i < VP9_SWITCHABLE_FILTERS; ++i) {
3534 count[i] = 0;
3535 for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
3536 count[i] += cpi->switchable_interp_count[j][i];
3537 }
3538 tot_count += count[i];
3539 }
3540
3541 thr = ((tot_count * interp_factor + 128) >> 8);
3542 for (i = 0; i < VP9_SWITCHABLE_FILTERS; ++i) {
3543 c += (count[i] >= thr);
3544 }
3545 if (c == 1) {
3546 /* Mostly one filter is used. So set the filter at frame level */
3547 for (i = 0; i < VP9_SWITCHABLE_FILTERS; ++i) {
3548 if (count[i]) {
3549 cm->mcomp_filter_type = vp9_switchable_interp[i];
3550 Loop = TRUE; /* Make sure to loop since the filter changed */
3551 break;
3552 }
3553 }
3554 }
3555 }
3556
3557 if (Loop == FALSE && cm->frame_type != KEY_FRAME && sf->search_best_filter) {
3558 if (mcomp_filter_index < mcomp_filters) {
3559 INT64 err = vp9_calc_ss_err(cpi->Source,
3560 &cm->yv12_fb[cm->new_fb_idx]);
3561 INT64 rate = cpi->projected_frame_size << 8;
3562 mcomp_filter_cost[mcomp_filter_index] =
3563 (RDCOST(cpi->RDMULT, cpi->RDDIV, rate, err));
3564 mcomp_filter_index++;
3565 if (mcomp_filter_index < mcomp_filters) {
3566 cm->mcomp_filter_type = mcomp_filters_to_search[mcomp_filter_index];
3567 loop_count = -1;
3568 Loop = TRUE;
3569 } else {
3570 int f;
3571 INT64 best_cost = mcomp_filter_cost[0];
3572 int mcomp_best_filter = mcomp_filters_to_search[0];
3573 for (f = 1; f < mcomp_filters; f++) {
3574 if (mcomp_filter_cost[f] < best_cost) {
3575 mcomp_best_filter = mcomp_filters_to_search[f];
3576 best_cost = mcomp_filter_cost[f];
3577 }
3578 }
3579 if (mcomp_best_filter != mcomp_filters_to_search[mcomp_filters - 1]) {
3580 loop_count = -1;
3581 Loop = TRUE;
3582 cm->mcomp_filter_type = mcomp_best_filter;
3583 }
3584 /*
3585 printf(" best filter = %d, ( ", mcomp_best_filter);
3586 for (f=0;f<mcomp_filters; f++) printf("%d ", mcomp_filter_cost[f]);
3587 printf(")\n");
3588 */
3589 }
3590 #if RESET_FOREACH_FILTER
3591 if (Loop == TRUE) {
3592 overshoot_seen = FALSE;
3593 undershoot_seen = FALSE;
3594 zbin_oq_low = zbin_oq_low0;
3595 zbin_oq_high = zbin_oq_high0;
3596 q_low = q_low0;
3597 q_high = q_high0;
3598 Q = Q0;
3599 cpi->zbin_over_quant = last_zbin_oq = last_zbin_oq0;
3600 cpi->rate_correction_factor = rate_correction_factor0;
3601 cpi->gf_rate_correction_factor = gf_rate_correction_factor0;
3602 cpi->active_best_quality = active_best_quality0;
3603 cpi->active_worst_quality = active_worst_quality0;
3604 }
3605 #endif
3606 }
3607 }
3608
3609 if (Loop == TRUE) {
3610 loop_count++;
3611 #if CONFIG_INTERNAL_STATS
3612 cpi->tot_recode_hits++;
3613 #endif
3614 }
3615 } while (Loop == TRUE);
3616
3617 // Special case code to reduce pulsing when key frames are forced at a
3618 // fixed interval. Note the reconstruction error if it is the frame before
3619 // the force key frame
3620 if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) {
3621 cpi->ambient_err = vp9_calc_ss_err(cpi->Source,
3622 &cm->yv12_fb[cm->new_fb_idx]);
3623 }
3624
3625 // This frame's MVs are saved and will be used in next frame's MV
3626 // prediction. Last frame has one more line(add to bottom) and one
3627 // more column(add to right) than cm->mip. The edge elements are
3628 // initialized to 0.
3629 if (cm->show_frame) { // do not save for altref frame
3630 int mb_row;
3631 int mb_col;
3632 MODE_INFO *tmp = cm->mip;
3633
3634 if (cm->frame_type != KEY_FRAME) {
3635 for (mb_row = 0; mb_row < cm->mb_rows + 1; mb_row ++) {
3636 for (mb_col = 0; mb_col < cm->mb_cols + 1; mb_col ++) {
3637 if (tmp->mbmi.ref_frame != INTRA_FRAME)
3638 cpi->lfmv[mb_col + mb_row * (cm->mode_info_stride + 1)].as_int = tmp ->mbmi.mv[0].as_int;
3639
3640 cpi->lf_ref_frame_sign_bias[mb_col + mb_row * (cm->mode_info_stride + 1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
3641 cpi->lf_ref_frame[mb_col + mb_row * (cm->mode_info_stride + 1)] = tmp- >mbmi.ref_frame;
3642 tmp++;
3643 }
3644 }
3645 }
3646 }
3647
3648 // Update the GF useage maps.
3649 // This is done after completing the compression of a frame when all modes
3650 // etc. are finalized but before loop filter
3651 vp9_update_gf_useage_maps(cpi, cm, &cpi->mb);
3652
3653 if (cm->frame_type == KEY_FRAME)
3654 cm->refresh_last_frame = 1;
3655
3656 #if 0
3657 {
3658 FILE *f = fopen("gfactive.stt", "a");
3659 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);
3660 fclose(f);
3661 }
3662 #endif
3663
3664 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
3665
3666 #if WRITE_RECON_BUFFER
3667 if (cm->show_frame)
3668 write_cx_frame_to_file(cm->frame_to_show,
3669 cm->current_video_frame);
3670 else
3671 write_cx_frame_to_file(cm->frame_to_show,
3672 cm->current_video_frame + 1000);
3673 #endif
3674
3675 // Pick the loop filter level for the frame.
3676 loopfilter_frame(cpi, cm);
3677
3678 // build the bitstream
3679 cpi->dummy_packing = 0;
3680 vp9_pack_bitstream(cpi, dest, size);
3681
3682 if (cpi->mb.e_mbd.update_mb_segmentation_map) {
3683 update_reference_segmentation_map(cpi);
3684 }
3685
3686 #if CONFIG_PRED_FILTER
3687 // Select the prediction filtering mode to use for the
3688 // next frame based on the current frame selections
3689 if (cm->frame_type != KEY_FRAME)
3690 select_pred_filter_mode(cpi);
3691 #endif
3692
3693 update_reference_frames(cm);
3694 vp9_copy(cpi->common.fc.coef_counts, cpi->coef_counts);
3695 vp9_copy(cpi->common.fc.hybrid_coef_counts, cpi->hybrid_coef_counts);
3696 vp9_copy(cpi->common.fc.coef_counts_8x8, cpi->coef_counts_8x8);
3697 vp9_copy(cpi->common.fc.hybrid_coef_counts_8x8, cpi->hybrid_coef_counts_8x8);
3698 vp9_copy(cpi->common.fc.coef_counts_16x16, cpi->coef_counts_16x16);
3699 vp9_copy(cpi->common.fc.hybrid_coef_counts_16x16,
3700 cpi->hybrid_coef_counts_16x16);
3701 vp9_adapt_coef_probs(&cpi->common);
3702 if (cpi->common.frame_type != KEY_FRAME) {
3703 #if CONFIG_SUPERBLOCKS
3704 vp9_copy(cpi->common.fc.sb_ymode_counts, cpi->sb_ymode_count);
3705 #endif
3706 vp9_copy(cpi->common.fc.ymode_counts, cpi->ymode_count);
3707 vp9_copy(cpi->common.fc.uv_mode_counts, cpi->y_uv_mode_count);
3708 vp9_copy(cpi->common.fc.bmode_counts, cpi->bmode_count);
3709 vp9_copy(cpi->common.fc.i8x8_mode_counts, cpi->i8x8_mode_count);
3710 vp9_copy(cpi->common.fc.sub_mv_ref_counts, cpi->sub_mv_ref_count);
3711 vp9_copy(cpi->common.fc.mbsplit_counts, cpi->mbsplit_count);
3712 #if CONFIG_COMP_INTERINTRA_PRED
3713 vp9_copy(cpi->common.fc.interintra_counts, cpi->interintra_count);
3714 #endif
3715 vp9_adapt_mode_probs(&cpi->common);
3716
3717 cpi->common.fc.NMVcount = cpi->NMVcount;
3718 /*
3719 printf("2: %d %d %d %d\n", cpi->NMVcount.joints[0], cpi->NMVcount.joints[1],
3720 cpi->NMVcount.joints[2], cpi->NMVcount.joints[3]);
3721 */
3722 vp9_adapt_nmv_probs(&cpi->common, cpi->mb.e_mbd.allow_high_precision_mv);
3723 }
3724 #if CONFIG_COMP_INTERINTRA_PRED
3725 if (cm->frame_type != KEY_FRAME)
3726 select_interintra_mode(cpi);
3727 #endif
3728
3729 /* Move storing frame_type out of the above loop since it is also
3730 * needed in motion search besides loopfilter */
3731 cm->last_frame_type = cm->frame_type;
3732
3733 // Keep a copy of the size estimate used in the loop
3734 loop_size_estimate = cpi->projected_frame_size;
3735
3736 // Update rate control heuristics
3737 cpi->total_byte_count += (*size);
3738 cpi->projected_frame_size = (*size) << 3;
3739
3740 if (!active_worst_qchanged)
3741 vp9_update_rate_correction_factors(cpi, 2);
3742
3743 cpi->last_q[cm->frame_type] = cm->base_qindex;
3744
3745 // Keep record of last boosted (KF/KF/ARF) Q value.
3746 // If the current frame is coded at a lower Q then we also update it.
3747 // If all mbs in this group are skipped only update if the Q value is
3748 // better than that already stored.
3749 // This is used to help set quality in forced key frames to reduce popping
3750 if ((cm->base_qindex < cpi->last_boosted_qindex) ||
3751 ((cpi->static_mb_pct < 100) &&
3752 ((cm->frame_type == KEY_FRAME) ||
3753 cm->refresh_alt_ref_frame ||
3754 (cm->refresh_golden_frame && !cpi->is_src_frame_alt_ref)))) {
3755 cpi->last_boosted_qindex = cm->base_qindex;
3756 }
3757
3758 if (cm->frame_type == KEY_FRAME) {
3759 vp9_adjust_key_frame_context(cpi);
3760 }
3761
3762 // Keep a record of ambient average Q.
3763 if (cm->frame_type != KEY_FRAME)
3764 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
3765
3766 // Keep a record from which we can calculate the average Q excluding GF update s and key frames
3767 if ((cm->frame_type != KEY_FRAME) && !cm->refresh_golden_frame && !cm->refresh _alt_ref_frame) {
3768 cpi->ni_frames++;
3769 cpi->tot_q += vp9_convert_qindex_to_q(Q);
3770 cpi->avg_q = cpi->tot_q / (double)cpi->ni_frames;
3771
3772 // Calculate the average Q for normal inter frames (not key or GFU
3773 // frames).
3774 cpi->ni_tot_qi += Q;
3775 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
3776 }
3777
3778 // Update the buffer level variable.
3779 // Non-viewable frames are a special case and are treated as pure overhead.
3780 if (!cm->show_frame)
3781 cpi->bits_off_target -= cpi->projected_frame_size;
3782 else
3783 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_s ize;
3784
3785 // Clip the buffer level at the maximum buffer size
3786 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
3787 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3788
3789 // Rolling monitors of whether we are over or underspending used to help regul ate min and Max Q in two pass.
3790 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_t arget + 2) / 4;
3791 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_fr ame_size + 2) / 4;
3792 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->t his_frame_target + 16) / 32;
3793 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->p rojected_frame_size + 16) / 32;
3794
3795 // Actual bits spent
3796 cpi->total_actual_bits += cpi->projected_frame_size;
3797
3798 // Debug stats
3799 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_ size);
3800
3801 cpi->buffer_level = cpi->bits_off_target;
3802
3803 // Update bits left to the kf and gf groups to account for overshoot or unders hoot on these frames
3804 if (cm->frame_type == KEY_FRAME) {
3805 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_ size;
3806
3807 if (cpi->twopass.kf_group_bits < 0)
3808 cpi->twopass.kf_group_bits = 0;
3809 } else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame) {
3810 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_ size;
3811
3812 if (cpi->twopass.gf_group_bits < 0)
3813 cpi->twopass.gf_group_bits = 0;
3814 }
3815
3816 // Update the skip mb flag probabilities based on the distribution seen
3817 // in this frame.
3818 update_base_skip_probs(cpi);
3819
3820 #if 0 //CONFIG_NEW_MVREF && CONFIG_INTERNAL_STATS
3821 {
3822 FILE *f = fopen("mv_ref_dist.stt", "a");
3823 unsigned int i;
3824 for (i = 0; i < MAX_MV_REFS; ++i) {
3825 fprintf(f, "%10d", cpi->best_ref_index_counts[0][i]);
3826 }
3827 fprintf(f, "\n" );
3828
3829 fclose(f);
3830 }
3831 #endif
3832
3833 #if 0// 1 && CONFIG_INTERNAL_STATS
3834 {
3835 FILE *f = fopen("tmp.stt", "a");
3836 int recon_err;
3837
3838 vp9_clear_system_state(); // __asm emms;
3839
3840 recon_err = vp9_calc_ss_err(cpi->Source,
3841 &cm->yv12_fb[cm->new_fb_idx]);
3842
3843 if (cpi->twopass.total_left_stats->coded_error != 0.0)
3844 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d"
3845 "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f"
3846 "%6d %5d %5d %5d %8d %8.2f %10d %10.3f"
3847 "%10.3f %8d %10d %10d %10d\n",
3848 cpi->common.current_video_frame, cpi->this_frame_target,
3849 cpi->projected_frame_size, loop_size_estimate,
3850 (cpi->projected_frame_size - cpi->this_frame_target),
3851 (int)cpi->total_target_vs_actual,
3852 (cpi->oxcf.starting_buffer_level - cpi->bits_off_target),
3853 (int)cpi->total_actual_bits,
3854 vp9_convert_qindex_to_q(cm->base_qindex),
3855 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
3856 vp9_convert_qindex_to_q(cpi->active_best_quality),
3857 vp9_convert_qindex_to_q(cpi->active_worst_quality),
3858 cpi->avg_q,
3859 vp9_convert_qindex_to_q(cpi->ni_av_qi),
3860 vp9_convert_qindex_to_q(cpi->cq_target_quality),
3861 cpi->zbin_over_quant,
3862 // cpi->avg_frame_qindex, cpi->zbin_over_quant,
3863 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
3864 cm->frame_type, cpi->gfu_boost,
3865 cpi->twopass.est_max_qcorrection_factor,
3866 (int)cpi->twopass.bits_left,
3867 cpi->twopass.total_left_stats->coded_error,
3868 (double)cpi->twopass.bits_left /
3869 cpi->twopass.total_left_stats->coded_error,
3870 cpi->tot_recode_hits, recon_err, cpi->kf_boost,
3871 cpi->kf_zeromotion_pct);
3872 else
3873 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d"
3874 "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f"
3875 "%6d %5d %5d %5d %8d %8.2f %10d %10.3f"
3876 "%8d %10d %10d %10d\n",
3877 cpi->common.current_video_frame,
3878 cpi->this_frame_target, cpi->projected_frame_size,
3879 loop_size_estimate,
3880 (cpi->projected_frame_size - cpi->this_frame_target),
3881 (int)cpi->total_target_vs_actual,
3882 (cpi->oxcf.starting_buffer_level - cpi->bits_off_target),
3883 (int)cpi->total_actual_bits,
3884 vp9_convert_qindex_to_q(cm->base_qindex),
3885 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
3886 vp9_convert_qindex_to_q(cpi->active_best_quality),
3887 vp9_convert_qindex_to_q(cpi->active_worst_quality),
3888 cpi->avg_q,
3889 vp9_convert_qindex_to_q(cpi->ni_av_qi),
3890 vp9_convert_qindex_to_q(cpi->cq_target_quality),
3891 cpi->zbin_over_quant,
3892 // cpi->avg_frame_qindex, cpi->zbin_over_quant,
3893 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
3894 cm->frame_type, cpi->gfu_boost,
3895 cpi->twopass.est_max_qcorrection_factor,
3896 (int)cpi->twopass.bits_left,
3897 cpi->twopass.total_left_stats->coded_error,
3898 cpi->tot_recode_hits, recon_err, cpi->kf_boost,
3899 cpi->kf_zeromotion_pct);
3900
3901 fclose(f);
3902
3903 if (0) {
3904 FILE *fmodes = fopen("Modes.stt", "a");
3905 int i;
3906
3907 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
3908 cpi->common.current_video_frame,
3909 cm->frame_type, cm->refresh_golden_frame,
3910 cm->refresh_alt_ref_frame);
3911
3912 for (i = 0; i < MAX_MODES; i++)
3913 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
3914
3915 fprintf(fmodes, "\n");
3916
3917 fclose(fmodes);
3918 }
3919 }
3920
3921 #endif
3922
3923 #if 0
3924 // Debug stats for segment feature experiments.
3925 print_seg_map(cpi);
3926 #endif
3927
3928 // If this was a kf or Gf note the Q
3929 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_a lt_ref_frame)
3930 cm->last_kf_gf_q = cm->base_qindex;
3931
3932 if (cm->refresh_golden_frame == 1)
3933 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
3934 else
3935 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
3936
3937 if (cm->refresh_alt_ref_frame == 1)
3938 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
3939 else
3940 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
3941
3942
3943 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
3944 cpi->gold_is_last = 1;
3945 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
3946 cpi->gold_is_last = 0;
3947
3948 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
3949 cpi->alt_is_last = 1;
3950 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed bu t not the other
3951 cpi->alt_is_last = 0;
3952
3953 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
3954 cpi->gold_is_alt = 1;
3955 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
3956 cpi->gold_is_alt = 0;
3957
3958 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3959
3960 if (cpi->gold_is_last)
3961 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
3962
3963 if (cpi->alt_is_last)
3964 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
3965
3966 if (cpi->gold_is_alt)
3967 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
3968
3969 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type ! = KEY_FRAME))
3970 // Update the alternate reference frame stats as appropriate.
3971 update_alt_ref_frame_stats(cpi);
3972 else
3973 // Update the Golden frame stats as appropriate.
3974 update_golden_frame_stats(cpi);
3975
3976 if (cm->frame_type == KEY_FRAME) {
3977 // Tell the caller that the frame was coded as a key frame
3978 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
3979
3980 // As this frame is a key frame the next defaults to an inter frame.
3981 cm->frame_type = INTER_FRAME;
3982 } else {
3983 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
3984 }
3985
3986 // Clear the one shot update flags for segmentation map and mode/ref loop filt er deltas.
3987 xd->update_mb_segmentation_map = 0;
3988 xd->update_mb_segmentation_data = 0;
3989 xd->mode_ref_lf_delta_update = 0;
3990
3991
3992 // Dont increment frame counters if this was an altref buffer update not a rea l frame
3993 if (cm->show_frame) {
3994 cm->current_video_frame++;
3995 cpi->frames_since_key++;
3996 }
3997
3998 // reset to normal state now that we are done.
3999
4000
4001
4002 #if 0
4003 {
4004 char filename[512];
4005 FILE *recon_file;
4006 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4007 recon_file = fopen(filename, "wb");
4008 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4009 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4010 fclose(recon_file);
4011 }
4012 #endif
4013 #ifdef OUTPUT_YUV_REC
4014 vp9_write_yuv_rec_frame(cm);
4015 #endif
4016
4017 if (cm->show_frame) {
4018 vpx_memcpy(cm->prev_mip, cm->mip,
4019 (cm->mb_cols + 1) * (cm->mb_rows + 1)* sizeof(MODE_INFO));
4020 } else {
4021 vpx_memset(cm->prev_mip, 0,
4022 (cm->mb_cols + 1) * (cm->mb_rows + 1)* sizeof(MODE_INFO));
4023 }
4024 }
4025
4026 static void Pass2Encode(VP9_COMP *cpi, unsigned long *size,
4027 unsigned char *dest, unsigned int *frame_flags) {
4028
4029 if (!cpi->common.refresh_alt_ref_frame)
4030 vp9_second_pass(cpi);
4031
4032 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4033 cpi->twopass.bits_left -= 8 * *size;
4034
4035 if (!cpi->common.refresh_alt_ref_frame) {
4036 double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.frame_rate;
4037 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4038 * cpi->oxcf.two_pass_vbrmin_section / 10 0);
4039
4040 if (two_pass_min_rate < lower_bounds_min_rate)
4041 two_pass_min_rate = lower_bounds_min_rate;
4042
4043 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->oxcf.frame_rate );
4044 }
4045 }
4046
4047
4048 int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags,
4049 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4050 int64_t end_time) {
4051 VP9_COMP *cpi = (VP9_COMP *) ptr;
4052 VP9_COMMON *cm = &cpi->common;
4053 struct vpx_usec_timer timer;
4054 int res = 0;
4055
4056 vpx_usec_timer_start(&timer);
4057 if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags,
4058 cpi->active_map_enabled ? cpi->active_map : NULL))
4059 res = -1;
4060 cm->clr_type = sd->clrtype;
4061 vpx_usec_timer_mark(&timer);
4062 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4063
4064 return res;
4065 }
4066
4067
4068 static int frame_is_reference(const VP9_COMP *cpi) {
4069 const VP9_COMMON *cm = &cpi->common;
4070 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4071
4072 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame
4073 || cm->refresh_golden_frame || cm->refresh_alt_ref_frame
4074 || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf
4075 || cm->refresh_entropy_probs
4076 || xd->mode_ref_lf_delta_update
4077 || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4078 }
4079
4080
4081 int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags,
4082 unsigned long *size, unsigned char *dest,
4083 int64_t *time_stamp, int64_t *time_end, int flush) {
4084 VP9_COMP *cpi = (VP9_COMP *) ptr;
4085 VP9_COMMON *cm = &cpi->common;
4086 struct vpx_usec_timer cmptimer;
4087 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4088
4089 if (!cpi)
4090 return -1;
4091
4092 vpx_usec_timer_start(&cmptimer);
4093
4094 cpi->source = NULL;
4095
4096 cpi->mb.e_mbd.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV;
4097 set_mvcost(&cpi->mb);
4098
4099 // Should we code an alternate reference frame
4100 if (cpi->oxcf.play_alternate &&
4101 cpi->source_alt_ref_pending) {
4102 if ((cpi->source = vp9_lookahead_peek(cpi->lookahead,
4103 cpi->frames_till_gf_update_due))) {
4104 cpi->alt_ref_source = cpi->source;
4105 if (cpi->oxcf.arnr_max_frames > 0) {
4106 vp9_temporal_filter_prepare(cpi, cpi->frames_till_gf_update_due);
4107 force_src_buffer = &cpi->alt_ref_buffer;
4108 }
4109 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4110 cm->refresh_alt_ref_frame = 1;
4111 cm->refresh_golden_frame = 0;
4112 cm->refresh_last_frame = 0;
4113 cm->show_frame = 0;
4114 cpi->source_alt_ref_pending = FALSE; // Clear Pending altf Ref flag.
4115 cpi->is_src_frame_alt_ref = 0;
4116 }
4117 }
4118
4119 if (!cpi->source) {
4120 if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) {
4121 cm->show_frame = 1;
4122
4123 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
4124 && (cpi->source == cpi->alt_ref_source);
4125
4126 if (cpi->is_src_frame_alt_ref)
4127 cpi->alt_ref_source = NULL;
4128 }
4129 }
4130
4131 if (cpi->source) {
4132 cpi->un_scaled_source =
4133 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4134 *time_stamp = cpi->source->ts_start;
4135 *time_end = cpi->source->ts_end;
4136 *frame_flags = cpi->source->flags;
4137 } else {
4138 *size = 0;
4139 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
4140 vp9_end_first_pass(cpi); /* get last stats packet */
4141 cpi->twopass.first_pass_done = 1;
4142 }
4143
4144 return -1;
4145 }
4146
4147 if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
4148 cpi->first_time_stamp_ever = cpi->source->ts_start;
4149 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
4150 }
4151
4152 // adjust frame rates based on timestamps given
4153 if (!cm->refresh_alt_ref_frame) {
4154 int64_t this_duration;
4155 int step = 0;
4156
4157 if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
4158 this_duration = cpi->source->ts_end - cpi->source->ts_start;
4159 step = 1;
4160 } else {
4161 int64_t last_duration;
4162
4163 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
4164 last_duration = cpi->last_end_time_stamp_seen
4165 - cpi->last_time_stamp_seen;
4166 // do a step update if the duration changes by 10%
4167 if (last_duration)
4168 step = (int)((this_duration - last_duration) * 10 / last_duration);
4169 }
4170
4171 if (this_duration) {
4172 if (step)
4173 vp9_new_frame_rate(cpi, 10000000.0 / this_duration);
4174 else {
4175 double avg_duration, interval;
4176
4177 /* Average this frame's rate into the last second's average
4178 * frame rate. If we haven't seen 1 second yet, then average
4179 * over the whole interval seen.
4180 */
4181 interval = (double)(cpi->source->ts_end
4182 - cpi->first_time_stamp_ever);
4183 if (interval > 10000000.0)
4184 interval = 10000000;
4185
4186 avg_duration = 10000000.0 / cpi->oxcf.frame_rate;
4187 avg_duration *= (interval - avg_duration + this_duration);
4188 avg_duration /= interval;
4189
4190 vp9_new_frame_rate(cpi, 10000000.0 / avg_duration);
4191 }
4192 }
4193
4194 cpi->last_time_stamp_seen = cpi->source->ts_start;
4195 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
4196 }
4197
4198 // start with a 0 size frame
4199 *size = 0;
4200
4201 // Clear down mmx registers
4202 vp9_clear_system_state(); // __asm emms;
4203
4204 cm->frame_type = INTER_FRAME;
4205 cm->frame_flags = *frame_flags;
4206
4207 #if 0
4208
4209 if (cm->refresh_alt_ref_frame) {
4210 // cm->refresh_golden_frame = 1;
4211 cm->refresh_golden_frame = 0;
4212 cm->refresh_last_frame = 0;
4213 } else {
4214 cm->refresh_golden_frame = 0;
4215 cm->refresh_last_frame = 1;
4216 }
4217
4218 #endif
4219 /* find a free buffer for the new frame */
4220 {
4221 int i = 0;
4222 for (; i < NUM_YV12_BUFFERS; i++) {
4223 if (!cm->yv12_fb[i].flags) {
4224 cm->new_fb_idx = i;
4225 break;
4226 }
4227 }
4228
4229 assert(i < NUM_YV12_BUFFERS);
4230 }
4231 if (cpi->pass == 1) {
4232 Pass1Encode(cpi, size, dest, frame_flags);
4233 } else if (cpi->pass == 2) {
4234 Pass2Encode(cpi, size, dest, frame_flags);
4235 } else {
4236 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4237 }
4238
4239 if (cm->refresh_entropy_probs) {
4240 if (cm->refresh_alt_ref_frame)
4241 vpx_memcpy(&cm->lfc_a, &cm->fc, sizeof(cm->fc));
4242 else
4243 vpx_memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc));
4244 }
4245
4246 // if its a dropped frame honor the requests on subsequent frames
4247 if (*size > 0) {
4248 cpi->droppable = !frame_is_reference(cpi);
4249
4250 // return to normal state
4251 cm->refresh_entropy_probs = 1;
4252 cm->refresh_alt_ref_frame = 0;
4253 cm->refresh_golden_frame = 0;
4254 cm->refresh_last_frame = 1;
4255 cm->frame_type = INTER_FRAME;
4256
4257 }
4258
4259 vpx_usec_timer_mark(&cmptimer);
4260 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
4261
4262 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) {
4263 generate_psnr_packet(cpi);
4264 }
4265
4266 #if CONFIG_INTERNAL_STATS
4267
4268 if (cpi->pass != 1) {
4269 cpi->bytes += *size;
4270
4271 if (cm->show_frame) {
4272
4273 cpi->count++;
4274
4275 if (cpi->b_calculate_psnr) {
4276 double ye, ue, ve;
4277 double frame_psnr;
4278 YV12_BUFFER_CONFIG *orig = cpi->Source;
4279 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
4280 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
4281 int y_samples = orig->y_height * orig->y_width;
4282 int uv_samples = orig->uv_height * orig->uv_width;
4283 int t_samples = y_samples + 2 * uv_samples;
4284 double sq_error;
4285
4286 ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride,
4287 recon->y_buffer, recon->y_stride, orig->y_width,
4288 orig->y_height);
4289
4290 ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride,
4291 recon->u_buffer, recon->uv_stride, orig->uv_width,
4292 orig->uv_height);
4293
4294 ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride,
4295 recon->v_buffer, recon->uv_stride, orig->uv_width,
4296 orig->uv_height);
4297
4298 sq_error = ye + ue + ve;
4299
4300 frame_psnr = vp9_mse2psnr(t_samples, 255.0, sq_error);
4301
4302 cpi->total_y += vp9_mse2psnr(y_samples, 255.0, ye);
4303 cpi->total_u += vp9_mse2psnr(uv_samples, 255.0, ue);
4304 cpi->total_v += vp9_mse2psnr(uv_samples, 255.0, ve);
4305 cpi->total_sq_error += sq_error;
4306 cpi->total += frame_psnr;
4307 {
4308 double frame_psnr2, frame_ssim2 = 0;
4309 double weight = 0;
4310 #if CONFIG_POSTPROC
4311 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
4312 cm->filter_level * 10 / 6, 1, 0);
4313 #endif
4314 vp9_clear_system_state();
4315
4316 ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride,
4317 pp->y_buffer, pp->y_stride, orig->y_width,
4318 orig->y_height);
4319
4320 ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride,
4321 pp->u_buffer, pp->uv_stride, orig->uv_width,
4322 orig->uv_height);
4323
4324 ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride,
4325 pp->v_buffer, pp->uv_stride, orig->uv_width,
4326 orig->uv_height);
4327
4328 sq_error = ye + ue + ve;
4329
4330 frame_psnr2 = vp9_mse2psnr(t_samples, 255.0, sq_error);
4331
4332 cpi->totalp_y += vp9_mse2psnr(y_samples, 255.0, ye);
4333 cpi->totalp_u += vp9_mse2psnr(uv_samples, 255.0, ue);
4334 cpi->totalp_v += vp9_mse2psnr(uv_samples, 255.0, ve);
4335 cpi->total_sq_error2 += sq_error;
4336 cpi->totalp += frame_psnr2;
4337
4338 frame_ssim2 = vp9_calc_ssim(cpi->Source,
4339 &cm->post_proc_buffer, 1, &weight);
4340
4341 cpi->summed_quality += frame_ssim2 * weight;
4342 cpi->summed_weights += weight;
4343 #if 0
4344 {
4345 FILE *f = fopen("q_used.stt", "a");
4346 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
4347 cpi->common.current_video_frame, y2, u2, v2,
4348 frame_psnr2, frame_ssim2);
4349 fclose(f);
4350 }
4351 #endif
4352 }
4353 }
4354
4355 if (cpi->b_calculate_ssimg) {
4356 double y, u, v, frame_all;
4357 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show,
4358 &y, &u, &v);
4359 cpi->total_ssimg_y += y;
4360 cpi->total_ssimg_u += u;
4361 cpi->total_ssimg_v += v;
4362 cpi->total_ssimg_all += frame_all;
4363 }
4364
4365 }
4366 }
4367
4368 #endif
4369
4370 return 0;
4371 }
4372
4373 int vp9_get_preview_raw_frame(VP9_PTR comp, YV12_BUFFER_CONFIG *dest,
4374 vp9_ppflags_t *flags) {
4375 VP9_COMP *cpi = (VP9_COMP *) comp;
4376
4377 if (cpi->common.refresh_alt_ref_frame)
4378 return -1;
4379 else {
4380 int ret;
4381 #if CONFIG_POSTPROC
4382 ret = vp9_post_proc_frame(&cpi->common, dest, flags);
4383 #else
4384
4385 if (cpi->common.frame_to_show) {
4386 *dest = *cpi->common.frame_to_show;
4387 dest->y_width = cpi->common.Width;
4388 dest->y_height = cpi->common.Height;
4389 dest->uv_height = cpi->common.Height / 2;
4390 ret = 0;
4391 } else {
4392 ret = -1;
4393 }
4394
4395 #endif // !CONFIG_POSTPROC
4396 vp9_clear_system_state();
4397 return ret;
4398 }
4399 }
4400
4401 int vp9_set_roimap(VP9_PTR comp, unsigned char *map, unsigned int rows,
4402 unsigned int cols, int delta_q[4], int delta_lf[4],
4403 unsigned int threshold[4]) {
4404 VP9_COMP *cpi = (VP9_COMP *) comp;
4405 signed char feature_data[SEG_LVL_MAX][MAX_MB_SEGMENTS];
4406 MACROBLOCKD *xd = &cpi->mb.e_mbd;
4407 int i;
4408
4409 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
4410 return -1;
4411
4412 if (!map) {
4413 vp9_disable_segmentation((VP9_PTR)cpi);
4414 return 0;
4415 }
4416
4417 // Set the segmentation Map
4418 vp9_set_segmentation_map((VP9_PTR)cpi, map);
4419
4420 // Activate segmentation.
4421 vp9_enable_segmentation((VP9_PTR)cpi);
4422
4423 // Set up the quant segment data
4424 feature_data[SEG_LVL_ALT_Q][0] = delta_q[0];
4425 feature_data[SEG_LVL_ALT_Q][1] = delta_q[1];
4426 feature_data[SEG_LVL_ALT_Q][2] = delta_q[2];
4427 feature_data[SEG_LVL_ALT_Q][3] = delta_q[3];
4428
4429 // Set up the loop segment data s
4430 feature_data[SEG_LVL_ALT_LF][0] = delta_lf[0];
4431 feature_data[SEG_LVL_ALT_LF][1] = delta_lf[1];
4432 feature_data[SEG_LVL_ALT_LF][2] = delta_lf[2];
4433 feature_data[SEG_LVL_ALT_LF][3] = delta_lf[3];
4434
4435 cpi->segment_encode_breakout[0] = threshold[0];
4436 cpi->segment_encode_breakout[1] = threshold[1];
4437 cpi->segment_encode_breakout[2] = threshold[2];
4438 cpi->segment_encode_breakout[3] = threshold[3];
4439
4440 // Enable the loop and quant changes in the feature mask
4441 for (i = 0; i < 4; i++) {
4442 if (delta_q[i])
4443 vp9_enable_segfeature(xd, i, SEG_LVL_ALT_Q);
4444 else
4445 vp9_disable_segfeature(xd, i, SEG_LVL_ALT_Q);
4446
4447 if (delta_lf[i])
4448 vp9_enable_segfeature(xd, i, SEG_LVL_ALT_LF);
4449 else
4450 vp9_disable_segfeature(xd, i, SEG_LVL_ALT_LF);
4451 }
4452
4453 // Initialise the feature data structure
4454 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
4455 vp9_set_segment_data((VP9_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
4456
4457 return 0;
4458 }
4459
4460 int vp9_set_active_map(VP9_PTR comp, unsigned char *map,
4461 unsigned int rows, unsigned int cols) {
4462 VP9_COMP *cpi = (VP9_COMP *) comp;
4463
4464 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
4465 if (map) {
4466 vpx_memcpy(cpi->active_map, map, rows * cols);
4467 cpi->active_map_enabled = 1;
4468 } else
4469 cpi->active_map_enabled = 0;
4470
4471 return 0;
4472 } else {
4473 // cpi->active_map_enabled = 0;
4474 return -1;
4475 }
4476 }
4477
4478 int vp9_set_internal_size(VP9_PTR comp,
4479 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
4480 VP9_COMP *cpi = (VP9_COMP *) comp;
4481
4482 if (horiz_mode <= ONETWO)
4483 cpi->common.horiz_scale = horiz_mode;
4484 else
4485 return -1;
4486
4487 if (vert_mode <= ONETWO)
4488 cpi->common.vert_scale = vert_mode;
4489 else
4490 return -1;
4491
4492 return 0;
4493 }
4494
4495
4496
4497 int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) {
4498 int i, j;
4499 int Total = 0;
4500
4501 unsigned char *src = source->y_buffer;
4502 unsigned char *dst = dest->y_buffer;
4503
4504 // Loop through the Y plane raw and reconstruction data summing (square differ ences)
4505 for (i = 0; i < source->y_height; i += 16) {
4506 for (j = 0; j < source->y_width; j += 16) {
4507 unsigned int sse;
4508 Total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
4509 &sse);
4510 }
4511
4512 src += 16 * source->y_stride;
4513 dst += 16 * dest->y_stride;
4514 }
4515
4516 return Total;
4517 }
4518
4519
4520 int vp9_get_quantizer(VP9_PTR c) {
4521 VP9_COMP *cpi = (VP9_COMP *) c;
4522 return cpi->common.base_qindex;
4523 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698