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