1 /*
2 * Copyright (c) 2018 The WebRTC 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 "modules/video_coding/codecs/interface/libvpx_interface.h"
12
13 #include <memory>
14
15 #include "rtc_base/checks.h"
16
17 namespace webrtc {
18 namespace {
19 class LibvpxFacade : public LibvpxInterface {
20 public:
21 LibvpxFacade() = default;
22 ~LibvpxFacade() override = default;
23
img_alloc(vpx_image_t * img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align) const24 vpx_image_t* img_alloc(vpx_image_t* img,
25 vpx_img_fmt_t fmt,
26 unsigned int d_w,
27 unsigned int d_h,
28 unsigned int align) const override {
29 return ::vpx_img_alloc(img, fmt, d_w, d_h, align);
30 }
31
img_wrap(vpx_image_t * img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int stride_align,unsigned char * img_data) const32 vpx_image_t* img_wrap(vpx_image_t* img,
33 vpx_img_fmt_t fmt,
34 unsigned int d_w,
35 unsigned int d_h,
36 unsigned int stride_align,
37 unsigned char* img_data) const override {
38 return ::vpx_img_wrap(img, fmt, d_w, d_h, stride_align, img_data);
39 }
40
img_free(vpx_image_t * img) const41 void img_free(vpx_image_t* img) const override { ::vpx_img_free(img); }
42
codec_enc_config_set(vpx_codec_ctx_t * ctx,const vpx_codec_enc_cfg_t * cfg) const43 vpx_codec_err_t codec_enc_config_set(
44 vpx_codec_ctx_t* ctx,
45 const vpx_codec_enc_cfg_t* cfg) const override {
46 return ::vpx_codec_enc_config_set(ctx, cfg);
47 }
48
codec_enc_config_default(vpx_codec_iface_t * iface,vpx_codec_enc_cfg_t * cfg,unsigned int usage) const49 vpx_codec_err_t codec_enc_config_default(vpx_codec_iface_t* iface,
50 vpx_codec_enc_cfg_t* cfg,
51 unsigned int usage) const override {
52 return ::vpx_codec_enc_config_default(iface, cfg, usage);
53 }
54
codec_enc_init(vpx_codec_ctx_t * ctx,vpx_codec_iface_t * iface,const vpx_codec_enc_cfg_t * cfg,vpx_codec_flags_t flags) const55 vpx_codec_err_t codec_enc_init(vpx_codec_ctx_t* ctx,
56 vpx_codec_iface_t* iface,
57 const vpx_codec_enc_cfg_t* cfg,
58 vpx_codec_flags_t flags) const override {
59 return ::vpx_codec_enc_init(ctx, iface, cfg, flags);
60 }
61
codec_enc_init_multi(vpx_codec_ctx_t * ctx,vpx_codec_iface_t * iface,vpx_codec_enc_cfg_t * cfg,int num_enc,vpx_codec_flags_t flags,vpx_rational_t * dsf) const62 vpx_codec_err_t codec_enc_init_multi(vpx_codec_ctx_t* ctx,
63 vpx_codec_iface_t* iface,
64 vpx_codec_enc_cfg_t* cfg,
65 int num_enc,
66 vpx_codec_flags_t flags,
67 vpx_rational_t* dsf) const override {
68 return ::vpx_codec_enc_init_multi(ctx, iface, cfg, num_enc, flags, dsf);
69 }
70
codec_destroy(vpx_codec_ctx_t * ctx) const71 vpx_codec_err_t codec_destroy(vpx_codec_ctx_t* ctx) const override {
72 return ::vpx_codec_destroy(ctx);
73 }
74
75 // For types related to these parameters, see section
76 // "VP8 encoder control function parameter type" in vpx/vp8cx.h.
77
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,uint32_t param) const78 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
79 vp8e_enc_control_id ctrl_id,
80 uint32_t param) const override {
81 // We need an explicit call for each type since vpx_codec_control is a
82 // macro that gets expanded into another call based on the parameter name.
83 switch (ctrl_id) {
84 case VP8E_SET_ENABLEAUTOALTREF:
85 return vpx_codec_control(ctx, VP8E_SET_ENABLEAUTOALTREF, param);
86 case VP8E_SET_NOISE_SENSITIVITY:
87 return vpx_codec_control(ctx, VP8E_SET_NOISE_SENSITIVITY, param);
88 case VP8E_SET_SHARPNESS:
89 return vpx_codec_control(ctx, VP8E_SET_SHARPNESS, param);
90 case VP8E_SET_STATIC_THRESHOLD:
91 return vpx_codec_control(ctx, VP8E_SET_STATIC_THRESHOLD, param);
92 case VP8E_SET_ARNR_MAXFRAMES:
93 return vpx_codec_control(ctx, VP8E_SET_ARNR_MAXFRAMES, param);
94 case VP8E_SET_ARNR_STRENGTH:
95 return vpx_codec_control(ctx, VP8E_SET_ARNR_STRENGTH, param);
96 case VP8E_SET_CQ_LEVEL:
97 return vpx_codec_control(ctx, VP8E_SET_CQ_LEVEL, param);
98 case VP8E_SET_MAX_INTRA_BITRATE_PCT:
99 return vpx_codec_control(ctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, param);
100 case VP9E_SET_MAX_INTER_BITRATE_PCT:
101 return vpx_codec_control(ctx, VP9E_SET_MAX_INTER_BITRATE_PCT, param);
102 case VP8E_SET_GF_CBR_BOOST_PCT:
103 return vpx_codec_control(ctx, VP8E_SET_GF_CBR_BOOST_PCT, param);
104 case VP8E_SET_SCREEN_CONTENT_MODE:
105 return vpx_codec_control(ctx, VP8E_SET_SCREEN_CONTENT_MODE, param);
106 case VP9E_SET_GF_CBR_BOOST_PCT:
107 return vpx_codec_control(ctx, VP9E_SET_GF_CBR_BOOST_PCT, param);
108 case VP9E_SET_LOSSLESS:
109 return vpx_codec_control(ctx, VP9E_SET_LOSSLESS, param);
110 case VP9E_SET_FRAME_PARALLEL_DECODING:
111 return vpx_codec_control(ctx, VP9E_SET_FRAME_PARALLEL_DECODING, param);
112 case VP9E_SET_AQ_MODE:
113 return vpx_codec_control(ctx, VP9E_SET_AQ_MODE, param);
114 case VP9E_SET_FRAME_PERIODIC_BOOST:
115 return vpx_codec_control(ctx, VP9E_SET_FRAME_PERIODIC_BOOST, param);
116 case VP9E_SET_NOISE_SENSITIVITY:
117 return vpx_codec_control(ctx, VP9E_SET_NOISE_SENSITIVITY, param);
118 case VP9E_SET_MIN_GF_INTERVAL:
119 return vpx_codec_control(ctx, VP9E_SET_MIN_GF_INTERVAL, param);
120 case VP9E_SET_MAX_GF_INTERVAL:
121 return vpx_codec_control(ctx, VP9E_SET_MAX_GF_INTERVAL, param);
122 case VP9E_SET_TARGET_LEVEL:
123 return vpx_codec_control(ctx, VP9E_SET_TARGET_LEVEL, param);
124 case VP9E_SET_ROW_MT:
125 return vpx_codec_control(ctx, VP9E_SET_ROW_MT, param);
126 case VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST:
127 return vpx_codec_control(ctx, VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST,
128 param);
129 case VP9E_SET_SVC_INTER_LAYER_PRED:
130 return vpx_codec_control(ctx, VP9E_SET_SVC_INTER_LAYER_PRED, param);
131 case VP9E_SET_SVC_GF_TEMPORAL_REF:
132 return vpx_codec_control(ctx, VP9E_SET_SVC_GF_TEMPORAL_REF, param);
133 case VP9E_SET_POSTENCODE_DROP:
134 return vpx_codec_control(ctx, VP9E_SET_POSTENCODE_DROP, param);
135 default:
136 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
137 }
138 return VPX_CODEC_ERROR;
139 }
140
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,int param) const141 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
142 vp8e_enc_control_id ctrl_id,
143 int param) const override {
144 switch (ctrl_id) {
145 case VP8E_SET_FRAME_FLAGS:
146 return vpx_codec_control(ctx, VP8E_SET_FRAME_FLAGS, param);
147 case VP8E_SET_TEMPORAL_LAYER_ID:
148 return vpx_codec_control(ctx, VP8E_SET_TEMPORAL_LAYER_ID, param);
149 case VP9E_SET_SVC:
150 return vpx_codec_control(ctx, VP9E_SET_SVC, param);
151 case VP8E_SET_CPUUSED:
152 return vpx_codec_control(ctx, VP8E_SET_CPUUSED, param);
153 case VP8E_SET_TOKEN_PARTITIONS:
154 return vpx_codec_control(ctx, VP8E_SET_TOKEN_PARTITIONS, param);
155 case VP8E_SET_TUNING:
156 return vpx_codec_control(ctx, VP8E_SET_TUNING, param);
157 case VP9E_SET_TILE_COLUMNS:
158 return vpx_codec_control(ctx, VP9E_SET_TILE_COLUMNS, param);
159 case VP9E_SET_TILE_ROWS:
160 return vpx_codec_control(ctx, VP9E_SET_TILE_ROWS, param);
161 case VP9E_SET_TPL:
162 return vpx_codec_control(ctx, VP9E_SET_TPL, param);
163 case VP9E_SET_ALT_REF_AQ:
164 return vpx_codec_control(ctx, VP9E_SET_ALT_REF_AQ, param);
165 case VP9E_SET_TUNE_CONTENT:
166 return vpx_codec_control(ctx, VP9E_SET_TUNE_CONTENT, param);
167 case VP9E_SET_COLOR_SPACE:
168 return vpx_codec_control(ctx, VP9E_SET_COLOR_SPACE, param);
169 case VP9E_SET_COLOR_RANGE:
170 return vpx_codec_control(ctx, VP9E_SET_COLOR_RANGE, param);
171 case VP9E_SET_DELTA_Q_UV:
172 return vpx_codec_control(ctx, VP9E_SET_DELTA_Q_UV, param);
173 case VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR:
174 return vpx_codec_control(ctx, VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR,
175 param);
176 case VP9E_SET_DISABLE_LOOPFILTER:
177 return vpx_codec_control(ctx, VP9E_SET_DISABLE_LOOPFILTER, param);
178
179 default:
180 if (param >= 0) {
181 // Might be intended for uint32_t but int literal used, try fallback.
182 return codec_control(ctx, ctrl_id, static_cast<uint32_t>(param));
183 }
184 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
185 }
186 return VPX_CODEC_ERROR;
187 }
188
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,int * param) const189 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
190 vp8e_enc_control_id ctrl_id,
191 int* param) const override {
192 switch (ctrl_id) {
193 case VP8E_GET_LAST_QUANTIZER:
194 return vpx_codec_control(ctx, VP8E_GET_LAST_QUANTIZER, param);
195 case VP8E_GET_LAST_QUANTIZER_64:
196 return vpx_codec_control(ctx, VP8E_GET_LAST_QUANTIZER_64, param);
197 case VP9E_SET_RENDER_SIZE:
198 return vpx_codec_control(ctx, VP9E_SET_RENDER_SIZE, param);
199 case VP9E_GET_LEVEL:
200 return vpx_codec_control(ctx, VP9E_GET_LEVEL, param);
201 default:
202 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
203 }
204 return VPX_CODEC_ERROR;
205 }
206
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_roi_map * param) const207 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
208 vp8e_enc_control_id ctrl_id,
209 vpx_roi_map* param) const override {
210 switch (ctrl_id) {
211 case VP8E_SET_ROI_MAP:
212 return vpx_codec_control(ctx, VP8E_SET_ROI_MAP, param);
213 case VP9E_SET_ROI_MAP:
214 return vpx_codec_control(ctx, VP9E_SET_ROI_MAP, param);
215 default:
216 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
217 }
218 return VPX_CODEC_ERROR;
219 }
220
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_active_map * param) const221 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
222 vp8e_enc_control_id ctrl_id,
223 vpx_active_map* param) const override {
224 switch (ctrl_id) {
225 case VP8E_SET_ACTIVEMAP:
226 return vpx_codec_control(ctx, VP8E_SET_ACTIVEMAP, param);
227 case VP9E_GET_ACTIVEMAP:
228 return vpx_codec_control(ctx, VP8E_SET_ACTIVEMAP, param);
229 default:
230 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
231 }
232 return VPX_CODEC_ERROR;
233 }
234
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_scaling_mode * param) const235 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
236 vp8e_enc_control_id ctrl_id,
237 vpx_scaling_mode* param) const override {
238 switch (ctrl_id) {
239 case VP8E_SET_SCALEMODE:
240 return vpx_codec_control(ctx, VP8E_SET_SCALEMODE, param);
241 default:
242 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
243 }
244 return VPX_CODEC_ERROR;
245 }
246
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_svc_extra_cfg_t * param) const247 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
248 vp8e_enc_control_id ctrl_id,
249 vpx_svc_extra_cfg_t* param) const override {
250 switch (ctrl_id) {
251 case VP9E_SET_SVC_PARAMETERS:
252 return vpx_codec_control_(ctx, VP9E_SET_SVC_PARAMETERS, param);
253 default:
254 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
255 }
256 return VPX_CODEC_ERROR;
257 }
258
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_svc_frame_drop_t * param) const259 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
260 vp8e_enc_control_id ctrl_id,
261 vpx_svc_frame_drop_t* param) const override {
262 switch (ctrl_id) {
263 case VP9E_SET_SVC_FRAME_DROP_LAYER:
264 return vpx_codec_control_(ctx, VP9E_SET_SVC_FRAME_DROP_LAYER, param);
265 default:
266 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
267 }
268 return VPX_CODEC_ERROR;
269 }
270
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,void * param) const271 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
272 vp8e_enc_control_id ctrl_id,
273 void* param) const override {
274 switch (ctrl_id) {
275 case VP9E_SET_SVC_PARAMETERS:
276 return vpx_codec_control_(ctx, VP9E_SET_SVC_PARAMETERS, param);
277 case VP9E_REGISTER_CX_CALLBACK:
278 return vpx_codec_control_(ctx, VP9E_REGISTER_CX_CALLBACK, param);
279 default:
280 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
281 }
282 return VPX_CODEC_ERROR;
283 }
284
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_svc_layer_id_t * param) const285 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
286 vp8e_enc_control_id ctrl_id,
287 vpx_svc_layer_id_t* param) const override {
288 switch (ctrl_id) {
289 case VP9E_SET_SVC_LAYER_ID:
290 return vpx_codec_control_(ctx, VP9E_SET_SVC_LAYER_ID, param);
291 case VP9E_GET_SVC_LAYER_ID:
292 return vpx_codec_control_(ctx, VP9E_GET_SVC_LAYER_ID, param);
293 default:
294 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
295 }
296 return VPX_CODEC_ERROR;
297 }
298
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_svc_ref_frame_config_t * param) const299 vpx_codec_err_t codec_control(
300 vpx_codec_ctx_t* ctx,
301 vp8e_enc_control_id ctrl_id,
302 vpx_svc_ref_frame_config_t* param) const override {
303 switch (ctrl_id) {
304 case VP9E_SET_SVC_REF_FRAME_CONFIG:
305 return vpx_codec_control_(ctx, VP9E_SET_SVC_REF_FRAME_CONFIG, param);
306 case VP9E_GET_SVC_REF_FRAME_CONFIG:
307 return vpx_codec_control_(ctx, VP9E_GET_SVC_REF_FRAME_CONFIG, param);
308 default:
309 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
310 }
311 return VPX_CODEC_ERROR;
312 }
313
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_svc_spatial_layer_sync_t * param) const314 vpx_codec_err_t codec_control(
315 vpx_codec_ctx_t* ctx,
316 vp8e_enc_control_id ctrl_id,
317 vpx_svc_spatial_layer_sync_t* param) const override {
318 switch (ctrl_id) {
319 case VP9E_SET_SVC_SPATIAL_LAYER_SYNC:
320 return vpx_codec_control_(ctx, VP9E_SET_SVC_SPATIAL_LAYER_SYNC, param);
321 default:
322 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
323 }
324 return VPX_CODEC_ERROR;
325 }
326
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_rc_funcs_t * param) const327 vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
328 vp8e_enc_control_id ctrl_id,
329 vpx_rc_funcs_t* param) const override {
330 switch (ctrl_id) {
331 case VP9E_SET_EXTERNAL_RATE_CONTROL:
332 return vpx_codec_control_(ctx, VP9E_SET_EXTERNAL_RATE_CONTROL, param);
333 default:
334 RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
335 }
336 return VPX_CODEC_ERROR;
337 }
338
codec_encode(vpx_codec_ctx_t * ctx,const vpx_image_t * img,vpx_codec_pts_t pts,uint64_t duration,vpx_enc_frame_flags_t flags,uint64_t deadline) const339 vpx_codec_err_t codec_encode(vpx_codec_ctx_t* ctx,
340 const vpx_image_t* img,
341 vpx_codec_pts_t pts,
342 uint64_t duration,
343 vpx_enc_frame_flags_t flags,
344 uint64_t deadline) const override {
345 return ::vpx_codec_encode(ctx, img, pts, duration, flags, deadline);
346 }
347
codec_get_cx_data(vpx_codec_ctx_t * ctx,vpx_codec_iter_t * iter) const348 const vpx_codec_cx_pkt_t* codec_get_cx_data(
349 vpx_codec_ctx_t* ctx,
350 vpx_codec_iter_t* iter) const override {
351 return ::vpx_codec_get_cx_data(ctx, iter);
352 }
353
codec_error_detail(vpx_codec_ctx_t * ctx) const354 const char* codec_error_detail(vpx_codec_ctx_t* ctx) const override {
355 return ::vpx_codec_error_detail(ctx);
356 }
357
codec_error(vpx_codec_ctx_t * ctx) const358 const char* codec_error(vpx_codec_ctx_t* ctx) const override {
359 return ::vpx_codec_error(ctx);
360 }
361
codec_err_to_string(vpx_codec_err_t err) const362 const char* codec_err_to_string(vpx_codec_err_t err) const override {
363 return ::vpx_codec_err_to_string(err);
364 }
365 };
366
367 } // namespace
368
Create()369 std::unique_ptr<LibvpxInterface> LibvpxInterface::Create() {
370 return std::make_unique<LibvpxFacade>();
371 }
372
373 } // namespace webrtc
374