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 <stddef.h>
12 #include <stdio.h>
13
14 #include "vp9/encoder/vp9_ext_ratectrl.h"
15 #include "vp9/encoder/vp9_encoder.h"
16 #include "vp9/common/vp9_common.h"
17 #include "vpx_dsp/psnr.h"
18 #include "vpx/vpx_codec.h"
19 #include "vpx/vpx_ext_ratectrl.h"
20 #include "vpx/vpx_tpl.h"
21
vp9_extrc_init(EXT_RATECTRL * ext_ratectrl)22 vpx_codec_err_t vp9_extrc_init(EXT_RATECTRL *ext_ratectrl) {
23 if (ext_ratectrl == NULL) {
24 return VPX_CODEC_INVALID_PARAM;
25 }
26 vp9_zero(*ext_ratectrl);
27 return VPX_CODEC_OK;
28 }
29
vp9_extrc_create(vpx_rc_funcs_t funcs,vpx_rc_config_t ratectrl_config,EXT_RATECTRL * ext_ratectrl)30 vpx_codec_err_t vp9_extrc_create(vpx_rc_funcs_t funcs,
31 vpx_rc_config_t ratectrl_config,
32 EXT_RATECTRL *ext_ratectrl) {
33 vpx_rc_status_t rc_status;
34 vpx_rc_firstpass_stats_t *rc_firstpass_stats;
35 if (ext_ratectrl == NULL) {
36 return VPX_CODEC_INVALID_PARAM;
37 }
38 vp9_extrc_delete(ext_ratectrl);
39 ext_ratectrl->funcs = funcs;
40 ext_ratectrl->ratectrl_config = ratectrl_config;
41 rc_status = ext_ratectrl->funcs.create_model(ext_ratectrl->funcs.priv,
42 &ext_ratectrl->ratectrl_config,
43 &ext_ratectrl->model);
44 if (rc_status == VPX_RC_ERROR) {
45 return VPX_CODEC_ERROR;
46 }
47 rc_firstpass_stats = &ext_ratectrl->rc_firstpass_stats;
48 rc_firstpass_stats->num_frames = ratectrl_config.show_frame_count;
49 rc_firstpass_stats->frame_stats =
50 vpx_malloc(sizeof(*rc_firstpass_stats->frame_stats) *
51 rc_firstpass_stats->num_frames);
52 if (rc_firstpass_stats->frame_stats == NULL) {
53 return VPX_CODEC_MEM_ERROR;
54 }
55 if (funcs.rate_ctrl_log_path != NULL) {
56 ext_ratectrl->log_file = fopen(funcs.rate_ctrl_log_path, "w");
57 if (!ext_ratectrl->log_file) {
58 return VPX_CODEC_ERROR;
59 }
60 } else {
61 ext_ratectrl->log_file = NULL;
62 }
63 ext_ratectrl->ready = 1;
64 return VPX_CODEC_OK;
65 }
66
vp9_extrc_delete(EXT_RATECTRL * ext_ratectrl)67 vpx_codec_err_t vp9_extrc_delete(EXT_RATECTRL *ext_ratectrl) {
68 if (ext_ratectrl == NULL) {
69 return VPX_CODEC_INVALID_PARAM;
70 }
71 if (ext_ratectrl->ready) {
72 if (ext_ratectrl->log_file) {
73 fclose(ext_ratectrl->log_file);
74 }
75 vpx_rc_status_t rc_status =
76 ext_ratectrl->funcs.delete_model(ext_ratectrl->model);
77 if (rc_status == VPX_RC_ERROR) {
78 return VPX_CODEC_ERROR;
79 }
80 vpx_free(ext_ratectrl->rc_firstpass_stats.frame_stats);
81 }
82 return vp9_extrc_init(ext_ratectrl);
83 }
84
gen_rc_firstpass_stats(const FIRSTPASS_STATS * stats,vpx_rc_frame_stats_t * rc_frame_stats)85 static void gen_rc_firstpass_stats(const FIRSTPASS_STATS *stats,
86 vpx_rc_frame_stats_t *rc_frame_stats) {
87 rc_frame_stats->frame = stats->frame;
88 rc_frame_stats->weight = stats->weight;
89 rc_frame_stats->intra_error = stats->intra_error;
90 rc_frame_stats->coded_error = stats->coded_error;
91 rc_frame_stats->sr_coded_error = stats->sr_coded_error;
92 rc_frame_stats->frame_noise_energy = stats->frame_noise_energy;
93 rc_frame_stats->pcnt_inter = stats->pcnt_inter;
94 rc_frame_stats->pcnt_motion = stats->pcnt_motion;
95 rc_frame_stats->pcnt_second_ref = stats->pcnt_second_ref;
96 rc_frame_stats->pcnt_neutral = stats->pcnt_neutral;
97 rc_frame_stats->pcnt_intra_low = stats->pcnt_intra_low;
98 rc_frame_stats->pcnt_intra_high = stats->pcnt_intra_high;
99 rc_frame_stats->intra_skip_pct = stats->intra_skip_pct;
100 rc_frame_stats->intra_smooth_pct = stats->intra_smooth_pct;
101 rc_frame_stats->inactive_zone_rows = stats->inactive_zone_rows;
102 rc_frame_stats->inactive_zone_cols = stats->inactive_zone_cols;
103 rc_frame_stats->MVr = stats->MVr;
104 rc_frame_stats->mvr_abs = stats->mvr_abs;
105 rc_frame_stats->MVc = stats->MVc;
106 rc_frame_stats->mvc_abs = stats->mvc_abs;
107 rc_frame_stats->MVrv = stats->MVrv;
108 rc_frame_stats->MVcv = stats->MVcv;
109 rc_frame_stats->mv_in_out_count = stats->mv_in_out_count;
110 rc_frame_stats->duration = stats->duration;
111 rc_frame_stats->count = stats->count;
112 rc_frame_stats->new_mv_count = stats->new_mv_count;
113 }
114
vp9_extrc_send_firstpass_stats(EXT_RATECTRL * ext_ratectrl,const FIRST_PASS_INFO * first_pass_info)115 vpx_codec_err_t vp9_extrc_send_firstpass_stats(
116 EXT_RATECTRL *ext_ratectrl, const FIRST_PASS_INFO *first_pass_info) {
117 if (ext_ratectrl == NULL) {
118 return VPX_CODEC_INVALID_PARAM;
119 }
120 if (ext_ratectrl->ready) {
121 vpx_rc_status_t rc_status;
122 vpx_rc_firstpass_stats_t *rc_firstpass_stats =
123 &ext_ratectrl->rc_firstpass_stats;
124 int i;
125 assert(rc_firstpass_stats->num_frames == first_pass_info->num_frames);
126 for (i = 0; i < rc_firstpass_stats->num_frames; ++i) {
127 gen_rc_firstpass_stats(&first_pass_info->stats[i],
128 &rc_firstpass_stats->frame_stats[i]);
129 }
130 rc_status = ext_ratectrl->funcs.send_firstpass_stats(ext_ratectrl->model,
131 rc_firstpass_stats);
132 if (rc_status == VPX_RC_ERROR) {
133 return VPX_CODEC_ERROR;
134 }
135 }
136 return VPX_CODEC_OK;
137 }
138
vp9_extrc_send_tpl_stats(EXT_RATECTRL * ext_ratectrl,const VpxTplGopStats * tpl_gop_stats)139 vpx_codec_err_t vp9_extrc_send_tpl_stats(EXT_RATECTRL *ext_ratectrl,
140 const VpxTplGopStats *tpl_gop_stats) {
141 if (ext_ratectrl == NULL) {
142 return VPX_CODEC_INVALID_PARAM;
143 }
144 if (ext_ratectrl->ready && ext_ratectrl->funcs.send_tpl_gop_stats != NULL) {
145 vpx_rc_status_t rc_status = ext_ratectrl->funcs.send_tpl_gop_stats(
146 ext_ratectrl->model, tpl_gop_stats);
147 if (rc_status == VPX_RC_ERROR) {
148 return VPX_CODEC_ERROR;
149 }
150 }
151 return VPX_CODEC_OK;
152 }
153
extrc_get_frame_type(FRAME_UPDATE_TYPE update_type)154 static int extrc_get_frame_type(FRAME_UPDATE_TYPE update_type) {
155 // TODO(angiebird): Add unit test to make sure this function behaves like
156 // get_frame_type_from_update_type()
157 // TODO(angiebird): Merge this function with get_frame_type_from_update_type()
158 switch (update_type) {
159 case KF_UPDATE: return 0; // kFrameTypeKey;
160 case ARF_UPDATE: return 2; // kFrameTypeAltRef;
161 case GF_UPDATE: return 4; // kFrameTypeGolden;
162 case OVERLAY_UPDATE: return 3; // kFrameTypeOverlay;
163 case LF_UPDATE: return 1; // kFrameTypeInter;
164 default:
165 fprintf(stderr, "Unsupported update_type %d\n", update_type);
166 abort();
167 }
168 }
169
vp9_extrc_get_encodeframe_decision(EXT_RATECTRL * ext_ratectrl,int gop_index,vpx_rc_encodeframe_decision_t * encode_frame_decision)170 vpx_codec_err_t vp9_extrc_get_encodeframe_decision(
171 EXT_RATECTRL *ext_ratectrl, int gop_index,
172 vpx_rc_encodeframe_decision_t *encode_frame_decision) {
173 assert(ext_ratectrl != NULL);
174 assert(ext_ratectrl->ready && (ext_ratectrl->funcs.rc_type & VPX_RC_QP) != 0);
175
176 vpx_rc_status_t rc_status = ext_ratectrl->funcs.get_encodeframe_decision(
177 ext_ratectrl->model, gop_index, encode_frame_decision);
178 if (rc_status == VPX_RC_ERROR) {
179 return VPX_CODEC_ERROR;
180 }
181 return VPX_CODEC_OK;
182 }
183
vp9_extrc_update_encodeframe_result(EXT_RATECTRL * ext_ratectrl,int64_t bit_count,int actual_encoding_qindex)184 vpx_codec_err_t vp9_extrc_update_encodeframe_result(
185 EXT_RATECTRL *ext_ratectrl, int64_t bit_count, int actual_encoding_qindex) {
186 if (ext_ratectrl == NULL) {
187 return VPX_CODEC_INVALID_PARAM;
188 }
189 if (ext_ratectrl->ready) {
190 vpx_rc_status_t rc_status;
191 vpx_rc_encodeframe_result_t encode_frame_result;
192 encode_frame_result.bit_count = bit_count;
193 encode_frame_result.actual_encoding_qindex = actual_encoding_qindex;
194 rc_status = ext_ratectrl->funcs.update_encodeframe_result(
195 ext_ratectrl->model, &encode_frame_result);
196 if (rc_status == VPX_RC_ERROR) {
197 return VPX_CODEC_ERROR;
198 }
199 }
200 return VPX_CODEC_OK;
201 }
202
vp9_extrc_get_gop_decision(EXT_RATECTRL * ext_ratectrl,vpx_rc_gop_decision_t * gop_decision)203 vpx_codec_err_t vp9_extrc_get_gop_decision(
204 EXT_RATECTRL *ext_ratectrl, vpx_rc_gop_decision_t *gop_decision) {
205 vpx_rc_status_t rc_status;
206 if (ext_ratectrl == NULL || !ext_ratectrl->ready ||
207 (ext_ratectrl->funcs.rc_type & VPX_RC_GOP) == 0) {
208 return VPX_CODEC_INVALID_PARAM;
209 }
210 rc_status =
211 ext_ratectrl->funcs.get_gop_decision(ext_ratectrl->model, gop_decision);
212 if (rc_status == VPX_RC_ERROR) {
213 return VPX_CODEC_ERROR;
214 }
215 return VPX_CODEC_OK;
216 }
217
vp9_extrc_get_key_frame_decision(EXT_RATECTRL * ext_ratectrl,vpx_rc_key_frame_decision_t * key_frame_decision)218 vpx_codec_err_t vp9_extrc_get_key_frame_decision(
219 EXT_RATECTRL *ext_ratectrl,
220 vpx_rc_key_frame_decision_t *key_frame_decision) {
221 if (ext_ratectrl == NULL || !ext_ratectrl->ready ||
222 (ext_ratectrl->funcs.rc_type & VPX_RC_GOP) == 0) {
223 return VPX_CODEC_INVALID_PARAM;
224 }
225 vpx_rc_status_t rc_status = ext_ratectrl->funcs.get_key_frame_decision(
226 ext_ratectrl->model, key_frame_decision);
227 return rc_status == VPX_RC_OK ? VPX_CODEC_OK : VPX_CODEC_ERROR;
228 }
229
vp9_extrc_get_frame_rdmult(EXT_RATECTRL * ext_ratectrl,int show_index,int coding_index,int gop_index,FRAME_UPDATE_TYPE update_type,int gop_size,int use_alt_ref,RefCntBuffer * ref_frame_bufs[MAX_INTER_REF_FRAMES],int ref_frame_flags,int * rdmult)230 vpx_codec_err_t vp9_extrc_get_frame_rdmult(
231 EXT_RATECTRL *ext_ratectrl, int show_index, int coding_index, int gop_index,
232 FRAME_UPDATE_TYPE update_type, int gop_size, int use_alt_ref,
233 RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES], int ref_frame_flags,
234 int *rdmult) {
235 vpx_rc_status_t rc_status;
236 vpx_rc_encodeframe_info_t encode_frame_info;
237 if (ext_ratectrl == NULL || !ext_ratectrl->ready ||
238 (ext_ratectrl->funcs.rc_type & VPX_RC_RDMULT) == 0) {
239 return VPX_CODEC_INVALID_PARAM;
240 }
241 encode_frame_info.show_index = show_index;
242 encode_frame_info.coding_index = coding_index;
243 encode_frame_info.gop_index = gop_index;
244 encode_frame_info.frame_type = extrc_get_frame_type(update_type);
245 encode_frame_info.gop_size = gop_size;
246 encode_frame_info.use_alt_ref = use_alt_ref;
247
248 vp9_get_ref_frame_info(update_type, ref_frame_flags, ref_frame_bufs,
249 encode_frame_info.ref_frame_coding_indexes,
250 encode_frame_info.ref_frame_valid_list);
251 rc_status = ext_ratectrl->funcs.get_frame_rdmult(ext_ratectrl->model,
252 &encode_frame_info, rdmult);
253 if (rc_status == VPX_RC_ERROR) {
254 return VPX_CODEC_ERROR;
255 }
256 return VPX_CODEC_OK;
257 }
258