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