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/vp8/libvpx_interface.h"
12 
13 #include <memory>
14 
15 #include "rtc_base/checks.h"
16 
17 namespace webrtc {
18 namespace {
19 class LibvpxVp8Facade : public LibvpxInterface {
20  public:
21   LibvpxVp8Facade() = default;
22   ~LibvpxVp8Facade() 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_ARNR_TYPE:
97         RTC_NOTREACHED() << "VP8E_SET_ARNR_TYPE is deprecated.";
98         return VPX_CODEC_UNSUP_FEATURE;
99       case VP8E_SET_CQ_LEVEL:
100         return vpx_codec_control(ctx, VP8E_SET_CQ_LEVEL, param);
101       case VP8E_SET_MAX_INTRA_BITRATE_PCT:
102         return vpx_codec_control(ctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, param);
103       case VP8E_SET_GF_CBR_BOOST_PCT:
104         return vpx_codec_control(ctx, VP8E_SET_GF_CBR_BOOST_PCT, param);
105       case VP8E_SET_SCREEN_CONTENT_MODE:
106         return vpx_codec_control(ctx, VP8E_SET_SCREEN_CONTENT_MODE, param);
107       default:
108         RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
109     }
110     return VPX_CODEC_ERROR;
111   }
112 
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,int param) const113   vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
114                                 vp8e_enc_control_id ctrl_id,
115                                 int param) const override {
116     switch (ctrl_id) {
117       case VP8E_SET_FRAME_FLAGS:
118         return vpx_codec_control(ctx, VP8E_SET_FRAME_FLAGS, param);
119       case VP8E_SET_TEMPORAL_LAYER_ID:
120         return vpx_codec_control(ctx, VP8E_SET_TEMPORAL_LAYER_ID, param);
121       case VP8E_SET_CPUUSED:
122         return vpx_codec_control(ctx, VP8E_SET_CPUUSED, param);
123       case VP8E_SET_TOKEN_PARTITIONS:
124         return vpx_codec_control(ctx, VP8E_SET_TOKEN_PARTITIONS, param);
125       case VP8E_SET_TUNING:
126         return vpx_codec_control(ctx, VP8E_SET_TUNING, param);
127 
128       default:
129         RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
130     }
131     return VPX_CODEC_ERROR;
132   }
133 
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,int * param) const134   vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
135                                 vp8e_enc_control_id ctrl_id,
136                                 int* param) const override {
137     switch (ctrl_id) {
138       case VP8E_GET_LAST_QUANTIZER:
139         return vpx_codec_control(ctx, VP8E_GET_LAST_QUANTIZER, param);
140       case VP8E_GET_LAST_QUANTIZER_64:
141         return vpx_codec_control(ctx, VP8E_GET_LAST_QUANTIZER_64, param);
142       default:
143         RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
144     }
145     return VPX_CODEC_ERROR;
146   }
147 
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_roi_map * param) const148   vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
149                                 vp8e_enc_control_id ctrl_id,
150                                 vpx_roi_map* param) const override {
151     switch (ctrl_id) {
152       case VP8E_SET_ROI_MAP:
153         return vpx_codec_control(ctx, VP8E_SET_ROI_MAP, param);
154       default:
155         RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
156     }
157     return VPX_CODEC_ERROR;
158   }
159 
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_active_map * param) const160   vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
161                                 vp8e_enc_control_id ctrl_id,
162                                 vpx_active_map* param) const override {
163     switch (ctrl_id) {
164       case VP8E_SET_ACTIVEMAP:
165         return vpx_codec_control(ctx, VP8E_SET_ACTIVEMAP, param);
166       default:
167         RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
168     }
169     return VPX_CODEC_ERROR;
170   }
171 
codec_control(vpx_codec_ctx_t * ctx,vp8e_enc_control_id ctrl_id,vpx_scaling_mode * param) const172   vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
173                                 vp8e_enc_control_id ctrl_id,
174                                 vpx_scaling_mode* param) const override {
175     switch (ctrl_id) {
176       case VP8E_SET_SCALEMODE:
177         return vpx_codec_control(ctx, VP8E_SET_SCALEMODE, param);
178       default:
179         RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
180     }
181     return VPX_CODEC_ERROR;
182   }
183 
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) const184   vpx_codec_err_t codec_encode(vpx_codec_ctx_t* ctx,
185                                const vpx_image_t* img,
186                                vpx_codec_pts_t pts,
187                                uint64_t duration,
188                                vpx_enc_frame_flags_t flags,
189                                uint64_t deadline) const override {
190     return ::vpx_codec_encode(ctx, img, pts, duration, flags, deadline);
191   }
192 
codec_get_cx_data(vpx_codec_ctx_t * ctx,vpx_codec_iter_t * iter) const193   const vpx_codec_cx_pkt_t* codec_get_cx_data(
194       vpx_codec_ctx_t* ctx,
195       vpx_codec_iter_t* iter) const override {
196     return ::vpx_codec_get_cx_data(ctx, iter);
197   }
198 };
199 
200 }  // namespace
201 
CreateEncoder()202 std::unique_ptr<LibvpxInterface> LibvpxInterface::CreateEncoder() {
203   return std::make_unique<LibvpxVp8Facade>();
204 }
205 
206 }  // namespace webrtc
207