blob: 983b3b54aeb69c7aa04616481cc9c0da0bc4bac2 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AV1_ENCODER_FIRSTPASS_H_
13#define AV1_ENCODER_FIRSTPASS_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include "av1/encoder/lookahead.h"
16#include "av1/encoder/ratectrl.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22#if CONFIG_FP_MB_STATS
23
24#define FPMB_DCINTRA_MASK 0x01
25
26#define FPMB_MOTION_ZERO_MASK 0x02
27#define FPMB_MOTION_LEFT_MASK 0x04
28#define FPMB_MOTION_RIGHT_MASK 0x08
29#define FPMB_MOTION_UP_MASK 0x10
30#define FPMB_MOTION_DOWN_MASK 0x20
31
32#define FPMB_ERROR_SMALL_MASK 0x40
33#define FPMB_ERROR_LARGE_MASK 0x80
34#define FPMB_ERROR_SMALL_TH 2000
35#define FPMB_ERROR_LARGE_TH 48000
36
37typedef struct {
38 uint8_t *mb_stats_start;
39 uint8_t *mb_stats_end;
40} FIRSTPASS_MB_STATS;
41#endif
42
43#if CONFIG_EXT_REFS
44// Length of the bi-predictive frame group (BFG)
45// NOTE: Currently each BFG contains one backward ref (BWF) frame plus a certain
46// number of bi-predictive frames.
47#define BFG_INTERVAL 2
48// The maximum number of extra ALT_REF's
49// NOTE: This number cannot be greater than 2 or the reference frame buffer will
50// overflow.
51#define MAX_EXT_ARFS 2
52#define MIN_EXT_ARF_INTERVAL 4
53#endif // CONFIG_EXT_REFS
54
55#define VLOW_MOTION_THRESHOLD 950
56
57typedef struct {
58 double frame;
59 double weight;
60 double intra_error;
61 double coded_error;
62 double sr_coded_error;
63 double pcnt_inter;
64 double pcnt_motion;
65 double pcnt_second_ref;
66 double pcnt_neutral;
67 double intra_skip_pct;
68 double inactive_zone_rows; // Image mask rows top and bottom.
69 double inactive_zone_cols; // Image mask columns at left and right edges.
70 double MVr;
71 double mvr_abs;
72 double MVc;
73 double mvc_abs;
74 double MVrv;
75 double MVcv;
76 double mv_in_out_count;
77 double new_mv_count;
78 double duration;
79 double count;
80} FIRSTPASS_STATS;
81
82typedef enum {
83 KF_UPDATE = 0,
84 LF_UPDATE = 1,
85 GF_UPDATE = 2,
86 ARF_UPDATE = 3,
87 OVERLAY_UPDATE = 4,
88#if CONFIG_EXT_REFS
89 BRF_UPDATE = 5, // Backward Reference Frame
90 LAST_BIPRED_UPDATE = 6, // Last Bi-predictive Frame
91 BIPRED_UPDATE = 7, // Bi-predictive Frame, but not the last one
92 INTNL_OVERLAY_UPDATE = 8, // Internal Overlay Frame
93 FRAME_UPDATE_TYPES = 9
94#else
95 FRAME_UPDATE_TYPES = 5
96#endif // CONFIG_EXT_REFS
97} FRAME_UPDATE_TYPE;
98
99#define FC_ANIMATION_THRESH 0.15
100typedef enum {
101 FC_NORMAL = 0,
102 FC_GRAPHICS_ANIMATION = 1,
103 FRAME_CONTENT_TYPES = 2
104} FRAME_CONTENT_TYPE;
105
106typedef struct {
107 unsigned char index;
108 RATE_FACTOR_LEVEL rf_level[(MAX_LAG_BUFFERS * 2) + 1];
109 FRAME_UPDATE_TYPE update_type[(MAX_LAG_BUFFERS * 2) + 1];
110 unsigned char arf_src_offset[(MAX_LAG_BUFFERS * 2) + 1];
111 unsigned char arf_update_idx[(MAX_LAG_BUFFERS * 2) + 1];
112 unsigned char arf_ref_idx[(MAX_LAG_BUFFERS * 2) + 1];
113#if CONFIG_EXT_REFS
114 unsigned char brf_src_offset[(MAX_LAG_BUFFERS * 2) + 1];
115 unsigned char bidir_pred_enabled[(MAX_LAG_BUFFERS * 2) + 1];
116#endif // CONFIG_EXT_REFS
117 int bit_allocation[(MAX_LAG_BUFFERS * 2) + 1];
118} GF_GROUP;
119
120typedef struct {
121 unsigned int section_intra_rating;
122 FIRSTPASS_STATS total_stats;
123 FIRSTPASS_STATS this_frame_stats;
124 const FIRSTPASS_STATS *stats_in;
125 const FIRSTPASS_STATS *stats_in_start;
126 const FIRSTPASS_STATS *stats_in_end;
127 FIRSTPASS_STATS total_left_stats;
128 int first_pass_done;
129 int64_t bits_left;
130 double modified_error_min;
131 double modified_error_max;
132 double modified_error_left;
133 double mb_av_energy;
134
135#if CONFIG_FP_MB_STATS
136 uint8_t *frame_mb_stats_buf;
137 uint8_t *this_frame_mb_stats;
138 FIRSTPASS_MB_STATS firstpass_mb_stats;
139#endif
140 // An indication of the content type of the current frame
141 FRAME_CONTENT_TYPE fr_content_type;
142
143 // Projected total bits available for a key frame group of frames
144 int64_t kf_group_bits;
145
146 // Error score of frames still to be coded in kf group
147 int64_t kf_group_error_left;
148
149 // The fraction for a kf groups total bits allocated to the inter frames
150 double kfgroup_inter_fraction;
151
152 int sr_update_lag;
153
154 int kf_zeromotion_pct;
155 int last_kfgroup_zeromotion_pct;
156 int gf_zeromotion_pct;
157 int active_worst_quality;
158 int baseline_active_worst_quality;
159 int extend_minq;
160 int extend_maxq;
161 int extend_minq_fast;
162
163 GF_GROUP gf_group;
164} TWO_PASS;
165
Yaowu Xuf883b422016-08-30 14:01:10 -0700166struct AV1_COMP;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700167
Yaowu Xuf883b422016-08-30 14:01:10 -0700168void av1_init_first_pass(struct AV1_COMP *cpi);
169void av1_rc_get_first_pass_params(struct AV1_COMP *cpi);
170void av1_first_pass(struct AV1_COMP *cpi, const struct lookahead_entry *source);
171void av1_end_first_pass(struct AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172
Yaowu Xuf883b422016-08-30 14:01:10 -0700173void av1_init_second_pass(struct AV1_COMP *cpi);
174void av1_rc_get_second_pass_params(struct AV1_COMP *cpi);
175void av1_twopass_postencode_update(struct AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176
177// Post encode update of the rate control parameters for 2-pass
Yaowu Xuf883b422016-08-30 14:01:10 -0700178void av1_twopass_postencode_update(struct AV1_COMP *cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179
Yaowu Xuc27fc142016-08-22 16:08:15 -0700180#if CONFIG_EXT_REFS
Yaowu Xu958303c2016-09-01 08:45:22 -0700181static INLINE int get_number_of_extra_arfs(int interval, int arf_pending) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 if (arf_pending && MAX_EXT_ARFS > 0)
183 return interval >= MIN_EXT_ARF_INTERVAL * (MAX_EXT_ARFS + 1)
184 ? MAX_EXT_ARFS
185 : interval >= MIN_EXT_ARF_INTERVAL * MAX_EXT_ARFS
186 ? MAX_EXT_ARFS - 1
187 : 0;
188 else
189 return 0;
190}
Zoe Liu6cfaff92016-10-18 17:12:11 -0700191#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192
193#ifdef __cplusplus
194} // extern "C"
195#endif
196
Yaowu Xuf883b422016-08-30 14:01:10 -0700197#endif // AV1_ENCODER_FIRSTPASS_H_