1 /*
2 * Copyright (c) 2020 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 #include "vp9/encoder/vp9_ext_ratectrl.h"
12 #include "vp9/encoder/vp9_encoder.h"
13 #include "vp9/common/vp9_common.h"
14 #include "vpx_dsp/psnr.h"
15
vp9_extrc_init(EXT_RATECTRL * ext_ratectrl)16 vpx_codec_err_t vp9_extrc_init(EXT_RATECTRL *ext_ratectrl) {
17 if (ext_ratectrl == NULL) {
18 return VPX_CODEC_INVALID_PARAM;
19 }
20 vp9_zero(*ext_ratectrl);
21 return VPX_CODEC_OK;
22 }
23
vp9_extrc_create(vpx_rc_funcs_t funcs,vpx_rc_config_t ratectrl_config,EXT_RATECTRL * ext_ratectrl)24 vpx_codec_err_t vp9_extrc_create(vpx_rc_funcs_t funcs,
25 vpx_rc_config_t ratectrl_config,
26 EXT_RATECTRL *ext_ratectrl) {
27 vpx_rc_status_t rc_status;
28 vpx_rc_firstpass_stats_t *rc_firstpass_stats;
29 if (ext_ratectrl == NULL) {
30 return VPX_CODEC_INVALID_PARAM;
31 }
32 vp9_extrc_delete(ext_ratectrl);
33 ext_ratectrl->funcs = funcs;
34 ext_ratectrl->ratectrl_config = ratectrl_config;
35 rc_status = ext_ratectrl->funcs.create_model(ext_ratectrl->funcs.priv,
36 &ext_ratectrl->ratectrl_config,
37 &ext_ratectrl->model);
38 if (rc_status == VPX_RC_ERROR) {
39 return VPX_CODEC_ERROR;
40 }
41 rc_firstpass_stats = &ext_ratectrl->rc_firstpass_stats;
42 rc_firstpass_stats->num_frames = ratectrl_config.show_frame_count;
43 rc_firstpass_stats->frame_stats =
44 vpx_malloc(sizeof(*rc_firstpass_stats->frame_stats) *
45 rc_firstpass_stats->num_frames);
46 if (rc_firstpass_stats->frame_stats == NULL) {
47 return VPX_CODEC_MEM_ERROR;
48 }
49 ext_ratectrl->ready = 1;
50 return VPX_CODEC_OK;
51 }
52
vp9_extrc_delete(EXT_RATECTRL * ext_ratectrl)53 vpx_codec_err_t vp9_extrc_delete(EXT_RATECTRL *ext_ratectrl) {
54 if (ext_ratectrl == NULL) {
55 return VPX_CODEC_INVALID_PARAM;
56 }
57 if (ext_ratectrl->ready) {
58 vpx_rc_status_t rc_status =
59 ext_ratectrl->funcs.delete_model(ext_ratectrl->model);
60 if (rc_status == VPX_RC_ERROR) {
61 return VPX_CODEC_ERROR;
62 }
63 vpx_free(ext_ratectrl->rc_firstpass_stats.frame_stats);
64 }
65 return vp9_extrc_init(ext_ratectrl);
66 }
67
gen_rc_firstpass_stats(const FIRSTPASS_STATS * stats,vpx_rc_frame_stats_t * rc_frame_stats)68 static void gen_rc_firstpass_stats(const FIRSTPASS_STATS *stats,
69 vpx_rc_frame_stats_t *rc_frame_stats) {
70 rc_frame_stats->frame = stats->frame;
71 rc_frame_stats->weight = stats->weight;
72 rc_frame_stats->intra_error = stats->intra_error;
73 rc_frame_stats->coded_error = stats->coded_error;
74 rc_frame_stats->sr_coded_error = stats->sr_coded_error;
75 rc_frame_stats->frame_noise_energy = stats->frame_noise_energy;
76 rc_frame_stats->pcnt_inter = stats->pcnt_inter;
77 rc_frame_stats->pcnt_motion = stats->pcnt_motion;
78 rc_frame_stats->pcnt_second_ref = stats->pcnt_second_ref;
79 rc_frame_stats->pcnt_neutral = stats->pcnt_neutral;
80 rc_frame_stats->pcnt_intra_low = stats->pcnt_intra_low;
81 rc_frame_stats->pcnt_intra_high = stats->pcnt_intra_high;
82 rc_frame_stats->intra_skip_pct = stats->intra_skip_pct;
83 rc_frame_stats->intra_smooth_pct = stats->intra_smooth_pct;
84 rc_frame_stats->inactive_zone_rows = stats->inactive_zone_rows;
85 rc_frame_stats->inactive_zone_cols = stats->inactive_zone_cols;
86 rc_frame_stats->MVr = stats->MVr;
87 rc_frame_stats->mvr_abs = stats->mvr_abs;
88 rc_frame_stats->MVc = stats->MVc;
89 rc_frame_stats->mvc_abs = stats->mvc_abs;
90 rc_frame_stats->MVrv = stats->MVrv;
91 rc_frame_stats->MVcv = stats->MVcv;
92 rc_frame_stats->mv_in_out_count = stats->mv_in_out_count;
93 rc_frame_stats->duration = stats->duration;
94 rc_frame_stats->count = stats->count;
95 }
96
vp9_extrc_send_firstpass_stats(EXT_RATECTRL * ext_ratectrl,const FIRST_PASS_INFO * first_pass_info)97 vpx_codec_err_t vp9_extrc_send_firstpass_stats(
98 EXT_RATECTRL *ext_ratectrl, const FIRST_PASS_INFO *first_pass_info) {
99 if (ext_ratectrl == NULL) {
100 return VPX_CODEC_INVALID_PARAM;
101 }
102 if (ext_ratectrl->ready) {
103 vpx_rc_status_t rc_status;
104 vpx_rc_firstpass_stats_t *rc_firstpass_stats =
105 &ext_ratectrl->rc_firstpass_stats;
106 int i;
107 assert(rc_firstpass_stats->num_frames == first_pass_info->num_frames);
108 for (i = 0; i < rc_firstpass_stats->num_frames; ++i) {
109 gen_rc_firstpass_stats(&first_pass_info->stats[i],
110 &rc_firstpass_stats->frame_stats[i]);
111 }
112 rc_status = ext_ratectrl->funcs.send_firstpass_stats(ext_ratectrl->model,
113 rc_firstpass_stats);
114 if (rc_status == VPX_RC_ERROR) {
115 return VPX_CODEC_ERROR;
116 }
117 }
118 return VPX_CODEC_OK;
119 }
120
extrc_get_frame_type(FRAME_UPDATE_TYPE update_type)121 static int extrc_get_frame_type(FRAME_UPDATE_TYPE update_type) {
122 // TODO(angiebird): Add unit test to make sure this function behaves like
123 // get_frame_type_from_update_type()
124 // TODO(angiebird): Merge this function with get_frame_type_from_update_type()
125 switch (update_type) {
126 case KF_UPDATE: return 0; // kFrameTypeKey;
127 case ARF_UPDATE: return 2; // kFrameTypeAltRef;
128 case GF_UPDATE: return 4; // kFrameTypeGolden;
129 case OVERLAY_UPDATE: return 3; // kFrameTypeOverlay;
130 case LF_UPDATE: return 1; // kFrameTypeInter;
131 default:
132 fprintf(stderr, "Unsupported update_type %d\n", update_type);
133 abort();
134 return 1;
135 }
136 }
137
vp9_extrc_get_encodeframe_decision(EXT_RATECTRL * ext_ratectrl,int show_index,int coding_index,int gop_index,FRAME_UPDATE_TYPE update_type,RefCntBuffer * ref_frame_bufs[MAX_INTER_REF_FRAMES],int ref_frame_flags,vpx_rc_encodeframe_decision_t * encode_frame_decision)138 vpx_codec_err_t vp9_extrc_get_encodeframe_decision(
139 EXT_RATECTRL *ext_ratectrl, int show_index, int coding_index, int gop_index,
140 FRAME_UPDATE_TYPE update_type,
141 RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES], int ref_frame_flags,
142 vpx_rc_encodeframe_decision_t *encode_frame_decision) {
143 if (ext_ratectrl == NULL) {
144 return VPX_CODEC_INVALID_PARAM;
145 }
146 if (ext_ratectrl->ready) {
147 vpx_rc_status_t rc_status;
148 vpx_rc_encodeframe_info_t encode_frame_info;
149 encode_frame_info.show_index = show_index;
150 encode_frame_info.coding_index = coding_index;
151 encode_frame_info.gop_index = gop_index;
152 encode_frame_info.frame_type = extrc_get_frame_type(update_type);
153
154 vp9_get_ref_frame_info(update_type, ref_frame_flags, ref_frame_bufs,
155 encode_frame_info.ref_frame_coding_indexes,
156 encode_frame_info.ref_frame_valid_list);
157
158 rc_status = ext_ratectrl->funcs.get_encodeframe_decision(
159 ext_ratectrl->model, &encode_frame_info, encode_frame_decision);
160 if (rc_status == VPX_RC_ERROR) {
161 return VPX_CODEC_ERROR;
162 }
163 }
164 return VPX_CODEC_OK;
165 }
166
vp9_extrc_update_encodeframe_result(EXT_RATECTRL * ext_ratectrl,int64_t bit_count,const YV12_BUFFER_CONFIG * source_frame,const YV12_BUFFER_CONFIG * coded_frame,uint32_t bit_depth,uint32_t input_bit_depth,const int actual_encoding_qindex)167 vpx_codec_err_t vp9_extrc_update_encodeframe_result(
168 EXT_RATECTRL *ext_ratectrl, int64_t bit_count,
169 const YV12_BUFFER_CONFIG *source_frame,
170 const YV12_BUFFER_CONFIG *coded_frame, uint32_t bit_depth,
171 uint32_t input_bit_depth, const int actual_encoding_qindex) {
172 if (ext_ratectrl == NULL) {
173 return VPX_CODEC_INVALID_PARAM;
174 }
175 if (ext_ratectrl->ready) {
176 PSNR_STATS psnr;
177 vpx_rc_status_t rc_status;
178 vpx_rc_encodeframe_result_t encode_frame_result;
179 encode_frame_result.bit_count = bit_count;
180 encode_frame_result.pixel_count =
181 source_frame->y_crop_width * source_frame->y_crop_height +
182 2 * source_frame->uv_crop_width * source_frame->uv_crop_height;
183 encode_frame_result.actual_encoding_qindex = actual_encoding_qindex;
184 #if CONFIG_VP9_HIGHBITDEPTH
185 vpx_calc_highbd_psnr(source_frame, coded_frame, &psnr, bit_depth,
186 input_bit_depth);
187 #else
188 (void)bit_depth;
189 (void)input_bit_depth;
190 vpx_calc_psnr(source_frame, coded_frame, &psnr);
191 #endif
192 encode_frame_result.sse = psnr.sse[0];
193 rc_status = ext_ratectrl->funcs.update_encodeframe_result(
194 ext_ratectrl->model, &encode_frame_result);
195 if (rc_status == VPX_RC_ERROR) {
196 return VPX_CODEC_ERROR;
197 }
198 }
199 return VPX_CODEC_OK;
200 }
201