Erik Språng | 5fbc0e0 | 2018-10-04 17:52:36 +0200 | [diff] [blame] | 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" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 12 | |
Erik Språng | 5fbc0e0 | 2018-10-04 17:52:36 +0200 | [diff] [blame] | 13 | #include "absl/memory/memory.h" |
| 14 | #include "rtc_base/checks.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | namespace { |
| 18 | class LibvpxVp8Facade : public LibvpxInterface { |
| 19 | public: |
| 20 | LibvpxVp8Facade() = default; |
| 21 | ~LibvpxVp8Facade() override = default; |
| 22 | |
| 23 | vpx_image_t* img_alloc(vpx_image_t* img, |
| 24 | vpx_img_fmt_t fmt, |
| 25 | unsigned int d_w, |
| 26 | unsigned int d_h, |
| 27 | unsigned int align) const override { |
| 28 | return ::vpx_img_alloc(img, fmt, d_w, d_h, align); |
| 29 | } |
| 30 | |
| 31 | vpx_image_t* img_wrap(vpx_image_t* img, |
| 32 | vpx_img_fmt_t fmt, |
| 33 | unsigned int d_w, |
| 34 | unsigned int d_h, |
| 35 | unsigned int stride_align, |
| 36 | unsigned char* img_data) const override { |
| 37 | return ::vpx_img_wrap(img, fmt, d_w, d_h, stride_align, img_data); |
| 38 | } |
| 39 | |
| 40 | void img_free(vpx_image_t* img) const override { ::vpx_img_free(img); } |
| 41 | |
| 42 | vpx_codec_err_t codec_enc_config_set( |
| 43 | vpx_codec_ctx_t* ctx, |
| 44 | const vpx_codec_enc_cfg_t* cfg) const override { |
| 45 | return ::vpx_codec_enc_config_set(ctx, cfg); |
| 46 | } |
| 47 | |
| 48 | vpx_codec_err_t codec_enc_config_default(vpx_codec_iface_t* iface, |
| 49 | vpx_codec_enc_cfg_t* cfg, |
| 50 | unsigned int usage) const override { |
| 51 | return ::vpx_codec_enc_config_default(iface, cfg, usage); |
| 52 | } |
| 53 | |
| 54 | vpx_codec_err_t codec_enc_init(vpx_codec_ctx_t* ctx, |
| 55 | vpx_codec_iface_t* iface, |
| 56 | const vpx_codec_enc_cfg_t* cfg, |
| 57 | vpx_codec_flags_t flags) const override { |
| 58 | return ::vpx_codec_enc_init(ctx, iface, cfg, flags); |
| 59 | } |
| 60 | |
| 61 | vpx_codec_err_t codec_enc_init_multi(vpx_codec_ctx_t* ctx, |
| 62 | vpx_codec_iface_t* iface, |
| 63 | vpx_codec_enc_cfg_t* cfg, |
| 64 | int num_enc, |
| 65 | vpx_codec_flags_t flags, |
| 66 | vpx_rational_t* dsf) const override { |
| 67 | return ::vpx_codec_enc_init_multi(ctx, iface, cfg, num_enc, flags, dsf); |
| 68 | } |
| 69 | |
| 70 | vpx_codec_err_t codec_destroy(vpx_codec_ctx_t* ctx) const override { |
| 71 | return ::vpx_codec_destroy(ctx); |
| 72 | } |
| 73 | |
| 74 | // For types related to these parameters, see section |
| 75 | // "VP8 encoder control function parameter type" in vpx/vp8cx.h. |
| 76 | |
| 77 | vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx, |
| 78 | vp8e_enc_control_id ctrl_id, |
| 79 | uint32_t param) const override { |
| 80 | // We need an explicit call for each type since vpx_codec_control is a |
| 81 | // macro that gets expanded into another call based on the parameter name. |
| 82 | switch (ctrl_id) { |
| 83 | case VP8E_SET_ENABLEAUTOALTREF: |
| 84 | return vpx_codec_control(ctx, VP8E_SET_ENABLEAUTOALTREF, param); |
| 85 | case VP8E_SET_NOISE_SENSITIVITY: |
| 86 | return vpx_codec_control(ctx, VP8E_SET_NOISE_SENSITIVITY, param); |
| 87 | case VP8E_SET_SHARPNESS: |
| 88 | return vpx_codec_control(ctx, VP8E_SET_SHARPNESS, param); |
| 89 | case VP8E_SET_STATIC_THRESHOLD: |
| 90 | return vpx_codec_control(ctx, VP8E_SET_STATIC_THRESHOLD, param); |
| 91 | case VP8E_SET_ARNR_MAXFRAMES: |
| 92 | return vpx_codec_control(ctx, VP8E_SET_ARNR_MAXFRAMES, param); |
| 93 | case VP8E_SET_ARNR_STRENGTH: |
| 94 | return vpx_codec_control(ctx, VP8E_SET_ARNR_STRENGTH, param); |
| 95 | case VP8E_SET_ARNR_TYPE: |
| 96 | RTC_NOTREACHED() << "VP8E_SET_ARNR_TYPE is deprecated."; |
| 97 | return VPX_CODEC_UNSUP_FEATURE; |
| 98 | case VP8E_SET_CQ_LEVEL: |
| 99 | return vpx_codec_control(ctx, VP8E_SET_CQ_LEVEL, param); |
| 100 | case VP8E_SET_MAX_INTRA_BITRATE_PCT: |
| 101 | return vpx_codec_control(ctx, VP8E_SET_MAX_INTRA_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 | default: |
| 107 | RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id; |
| 108 | } |
| 109 | return VPX_CODEC_ERROR; |
| 110 | } |
| 111 | |
| 112 | vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx, |
| 113 | vp8e_enc_control_id ctrl_id, |
| 114 | int param) const override { |
| 115 | switch (ctrl_id) { |
| 116 | case VP8E_SET_FRAME_FLAGS: |
| 117 | return vpx_codec_control(ctx, VP8E_SET_FRAME_FLAGS, param); |
| 118 | case VP8E_SET_TEMPORAL_LAYER_ID: |
| 119 | return vpx_codec_control(ctx, VP8E_SET_TEMPORAL_LAYER_ID, param); |
| 120 | case VP8E_SET_CPUUSED: |
| 121 | return vpx_codec_control(ctx, VP8E_SET_CPUUSED, param); |
| 122 | case VP8E_SET_TOKEN_PARTITIONS: |
| 123 | return vpx_codec_control(ctx, VP8E_SET_TOKEN_PARTITIONS, param); |
| 124 | case VP8E_SET_TUNING: |
| 125 | return vpx_codec_control(ctx, VP8E_SET_TUNING, param); |
| 126 | |
| 127 | default: |
| 128 | RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id; |
| 129 | } |
| 130 | return VPX_CODEC_ERROR; |
| 131 | } |
| 132 | |
| 133 | vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx, |
| 134 | vp8e_enc_control_id ctrl_id, |
| 135 | int* param) const override { |
| 136 | switch (ctrl_id) { |
| 137 | case VP8E_GET_LAST_QUANTIZER: |
| 138 | return vpx_codec_control(ctx, VP8E_GET_LAST_QUANTIZER, param); |
| 139 | case VP8E_GET_LAST_QUANTIZER_64: |
| 140 | return vpx_codec_control(ctx, VP8E_GET_LAST_QUANTIZER_64, param); |
| 141 | default: |
| 142 | RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id; |
| 143 | } |
| 144 | return VPX_CODEC_ERROR; |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 145 | } |
Erik Språng | 5fbc0e0 | 2018-10-04 17:52:36 +0200 | [diff] [blame] | 146 | |
| 147 | vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx, |
| 148 | vp8e_enc_control_id ctrl_id, |
| 149 | vpx_roi_map* param) const override { |
| 150 | switch (ctrl_id) { |
| 151 | case VP8E_SET_ROI_MAP: |
| 152 | return vpx_codec_control(ctx, VP8E_SET_ROI_MAP, param); |
| 153 | default: |
| 154 | RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id; |
| 155 | } |
| 156 | return VPX_CODEC_ERROR; |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 157 | } |
Erik Språng | 5fbc0e0 | 2018-10-04 17:52:36 +0200 | [diff] [blame] | 158 | |
| 159 | vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx, |
| 160 | vp8e_enc_control_id ctrl_id, |
| 161 | vpx_active_map* param) const override { |
| 162 | switch (ctrl_id) { |
| 163 | case VP8E_SET_ACTIVEMAP: |
| 164 | return vpx_codec_control(ctx, VP8E_SET_ACTIVEMAP, param); |
| 165 | default: |
| 166 | RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id; |
| 167 | } |
| 168 | return VPX_CODEC_ERROR; |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 169 | } |
Erik Språng | 5fbc0e0 | 2018-10-04 17:52:36 +0200 | [diff] [blame] | 170 | |
| 171 | vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx, |
| 172 | vp8e_enc_control_id ctrl_id, |
| 173 | vpx_scaling_mode* param) const override { |
| 174 | switch (ctrl_id) { |
| 175 | case VP8E_SET_SCALEMODE: |
| 176 | return vpx_codec_control(ctx, VP8E_SET_SCALEMODE, param); |
| 177 | default: |
| 178 | RTC_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id; |
| 179 | } |
| 180 | return VPX_CODEC_ERROR; |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 181 | } |
Erik Språng | 5fbc0e0 | 2018-10-04 17:52:36 +0200 | [diff] [blame] | 182 | |
| 183 | vpx_codec_err_t codec_encode(vpx_codec_ctx_t* ctx, |
| 184 | const vpx_image_t* img, |
| 185 | vpx_codec_pts_t pts, |
| 186 | uint64_t duration, |
| 187 | vpx_enc_frame_flags_t flags, |
| 188 | uint64_t deadline) const override { |
| 189 | return ::vpx_codec_encode(ctx, img, pts, duration, flags, deadline); |
| 190 | } |
| 191 | |
| 192 | const vpx_codec_cx_pkt_t* codec_get_cx_data( |
| 193 | vpx_codec_ctx_t* ctx, |
| 194 | vpx_codec_iter_t* iter) const override { |
| 195 | return ::vpx_codec_get_cx_data(ctx, iter); |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | } // namespace |
| 200 | |
| 201 | std::unique_ptr<LibvpxInterface> LibvpxInterface::CreateEncoder() { |
| 202 | return absl::make_unique<LibvpxVp8Facade>(); |
| 203 | } |
| 204 | |
| 205 | } // namespace webrtc |