summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/video_coding/codecs/interface/libvpx_interface.cc
blob: 4f33bef2baf14f8c3bff4c108c82937f3049d29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/video_coding/codecs/interface/libvpx_interface.h"

#include <memory>

#include "rtc_base/checks.h"

namespace webrtc {
namespace {
class LibvpxFacade : public LibvpxInterface {
 public:
  LibvpxFacade() = default;
  ~LibvpxFacade() override = default;

  vpx_image_t* img_alloc(vpx_image_t* img,
                         vpx_img_fmt_t fmt,
                         unsigned int d_w,
                         unsigned int d_h,
                         unsigned int align) const override {
    return ::vpx_img_alloc(img, fmt, d_w, d_h, align);
  }

  vpx_image_t* 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) const override {
    return ::vpx_img_wrap(img, fmt, d_w, d_h, stride_align, img_data);
  }

  void img_free(vpx_image_t* img) const override { ::vpx_img_free(img); }

  vpx_codec_err_t codec_enc_config_set(
      vpx_codec_ctx_t* ctx,
      const vpx_codec_enc_cfg_t* cfg) const override {
    return ::vpx_codec_enc_config_set(ctx, cfg);
  }

  vpx_codec_err_t codec_enc_config_default(vpx_codec_iface_t* iface,
                                           vpx_codec_enc_cfg_t* cfg,
                                           unsigned int usage) const override {
    return ::vpx_codec_enc_config_default(iface, cfg, usage);
  }

  vpx_codec_err_t 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) const override {
    return ::vpx_codec_enc_init(ctx, iface, cfg, flags);
  }

  vpx_codec_err_t 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) const override {
    return ::vpx_codec_enc_init_multi(ctx, iface, cfg, num_enc, flags, dsf);
  }

  vpx_codec_err_t codec_destroy(vpx_codec_ctx_t* ctx) const override {
    return ::vpx_codec_destroy(ctx);
  }

  // For types related to these parameters, see section
  // "VP8 encoder control function parameter type" in vpx/vp8cx.h.

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                uint32_t param) const override {
    // We need an explicit call for each type since vpx_codec_control is a
    // macro that gets expanded into another call based on the parameter name.
    switch (ctrl_id) {
      case VP8E_SET_ENABLEAUTOALTREF:
        return vpx_codec_control(ctx, VP8E_SET_ENABLEAUTOALTREF, param);
      case VP8E_SET_NOISE_SENSITIVITY:
        return vpx_codec_control(ctx, VP8E_SET_NOISE_SENSITIVITY, param);
      case VP8E_SET_SHARPNESS:
        return vpx_codec_control(ctx, VP8E_SET_SHARPNESS, param);
      case VP8E_SET_STATIC_THRESHOLD:
        return vpx_codec_control(ctx, VP8E_SET_STATIC_THRESHOLD, param);
      case VP8E_SET_ARNR_MAXFRAMES:
        return vpx_codec_control(ctx, VP8E_SET_ARNR_MAXFRAMES, param);
      case VP8E_SET_ARNR_STRENGTH:
        return vpx_codec_control(ctx, VP8E_SET_ARNR_STRENGTH, param);
      case VP8E_SET_CQ_LEVEL:
        return vpx_codec_control(ctx, VP8E_SET_CQ_LEVEL, param);
      case VP8E_SET_MAX_INTRA_BITRATE_PCT:
        return vpx_codec_control(ctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, param);
      case VP9E_SET_MAX_INTER_BITRATE_PCT:
        return vpx_codec_control(ctx, VP9E_SET_MAX_INTER_BITRATE_PCT, param);
      case VP8E_SET_GF_CBR_BOOST_PCT:
        return vpx_codec_control(ctx, VP8E_SET_GF_CBR_BOOST_PCT, param);
      case VP8E_SET_SCREEN_CONTENT_MODE:
        return vpx_codec_control(ctx, VP8E_SET_SCREEN_CONTENT_MODE, param);
      case VP9E_SET_GF_CBR_BOOST_PCT:
        return vpx_codec_control(ctx, VP9E_SET_GF_CBR_BOOST_PCT, param);
      case VP9E_SET_LOSSLESS:
        return vpx_codec_control(ctx, VP9E_SET_LOSSLESS, param);
      case VP9E_SET_FRAME_PARALLEL_DECODING:
        return vpx_codec_control(ctx, VP9E_SET_FRAME_PARALLEL_DECODING, param);
      case VP9E_SET_AQ_MODE:
        return vpx_codec_control(ctx, VP9E_SET_AQ_MODE, param);
      case VP9E_SET_FRAME_PERIODIC_BOOST:
        return vpx_codec_control(ctx, VP9E_SET_FRAME_PERIODIC_BOOST, param);
      case VP9E_SET_NOISE_SENSITIVITY:
        return vpx_codec_control(ctx, VP9E_SET_NOISE_SENSITIVITY, param);
      case VP9E_SET_MIN_GF_INTERVAL:
        return vpx_codec_control(ctx, VP9E_SET_MIN_GF_INTERVAL, param);
      case VP9E_SET_MAX_GF_INTERVAL:
        return vpx_codec_control(ctx, VP9E_SET_MAX_GF_INTERVAL, param);
      case VP9E_SET_TARGET_LEVEL:
        return vpx_codec_control(ctx, VP9E_SET_TARGET_LEVEL, param);
      case VP9E_SET_ROW_MT:
        return vpx_codec_control(ctx, VP9E_SET_ROW_MT, param);
      case VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST:
        return vpx_codec_control(ctx, VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST,
                                 param);
      case VP9E_SET_SVC_INTER_LAYER_PRED:
        return vpx_codec_control(ctx, VP9E_SET_SVC_INTER_LAYER_PRED, param);
      case VP9E_SET_SVC_GF_TEMPORAL_REF:
        return vpx_codec_control(ctx, VP9E_SET_SVC_GF_TEMPORAL_REF, param);
      case VP9E_SET_POSTENCODE_DROP:
        return vpx_codec_control(ctx, VP9E_SET_POSTENCODE_DROP, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                int param) const override {
    switch (ctrl_id) {
      case VP8E_SET_FRAME_FLAGS:
        return vpx_codec_control(ctx, VP8E_SET_FRAME_FLAGS, param);
      case VP8E_SET_TEMPORAL_LAYER_ID:
        return vpx_codec_control(ctx, VP8E_SET_TEMPORAL_LAYER_ID, param);
      case VP9E_SET_SVC:
        return vpx_codec_control(ctx, VP9E_SET_SVC, param);
      case VP8E_SET_CPUUSED:
        return vpx_codec_control(ctx, VP8E_SET_CPUUSED, param);
      case VP8E_SET_TOKEN_PARTITIONS:
        return vpx_codec_control(ctx, VP8E_SET_TOKEN_PARTITIONS, param);
      case VP8E_SET_TUNING:
        return vpx_codec_control(ctx, VP8E_SET_TUNING, param);
      case VP9E_SET_TILE_COLUMNS:
        return vpx_codec_control(ctx, VP9E_SET_TILE_COLUMNS, param);
      case VP9E_SET_TILE_ROWS:
        return vpx_codec_control(ctx, VP9E_SET_TILE_ROWS, param);
      case VP9E_SET_TPL:
        return vpx_codec_control(ctx, VP9E_SET_TPL, param);
      case VP9E_SET_ALT_REF_AQ:
        return vpx_codec_control(ctx, VP9E_SET_ALT_REF_AQ, param);
      case VP9E_SET_TUNE_CONTENT:
        return vpx_codec_control(ctx, VP9E_SET_TUNE_CONTENT, param);
      case VP9E_SET_COLOR_SPACE:
        return vpx_codec_control(ctx, VP9E_SET_COLOR_SPACE, param);
      case VP9E_SET_COLOR_RANGE:
        return vpx_codec_control(ctx, VP9E_SET_COLOR_RANGE, param);
      case VP9E_SET_DELTA_Q_UV:
        return vpx_codec_control(ctx, VP9E_SET_DELTA_Q_UV, param);
      case VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR:
        return vpx_codec_control(ctx, VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR,
                                 param);
      case VP9E_SET_DISABLE_LOOPFILTER:
        return vpx_codec_control(ctx, VP9E_SET_DISABLE_LOOPFILTER, param);

      default:
        if (param >= 0) {
          // Might be intended for uint32_t but int literal used, try fallback.
          return codec_control(ctx, ctrl_id, static_cast<uint32_t>(param));
        }
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                int* param) const override {
    switch (ctrl_id) {
      case VP8E_GET_LAST_QUANTIZER:
        return vpx_codec_control(ctx, VP8E_GET_LAST_QUANTIZER, param);
      case VP8E_GET_LAST_QUANTIZER_64:
        return vpx_codec_control(ctx, VP8E_GET_LAST_QUANTIZER_64, param);
      case VP9E_SET_RENDER_SIZE:
        return vpx_codec_control(ctx, VP9E_SET_RENDER_SIZE, param);
      case VP9E_GET_LEVEL:
        return vpx_codec_control(ctx, VP9E_GET_LEVEL, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                vpx_roi_map* param) const override {
    switch (ctrl_id) {
      case VP8E_SET_ROI_MAP:
        return vpx_codec_control(ctx, VP8E_SET_ROI_MAP, param);
      case VP9E_SET_ROI_MAP:
        return vpx_codec_control(ctx, VP9E_SET_ROI_MAP, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                vpx_active_map* param) const override {
    switch (ctrl_id) {
      case VP8E_SET_ACTIVEMAP:
        return vpx_codec_control(ctx, VP8E_SET_ACTIVEMAP, param);
      case VP9E_GET_ACTIVEMAP:
        return vpx_codec_control(ctx, VP8E_SET_ACTIVEMAP, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                vpx_scaling_mode* param) const override {
    switch (ctrl_id) {
      case VP8E_SET_SCALEMODE:
        return vpx_codec_control(ctx, VP8E_SET_SCALEMODE, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                vpx_svc_extra_cfg_t* param) const override {
    switch (ctrl_id) {
      case VP9E_SET_SVC_PARAMETERS:
        return vpx_codec_control_(ctx, VP9E_SET_SVC_PARAMETERS, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                vpx_svc_frame_drop_t* param) const override {
    switch (ctrl_id) {
      case VP9E_SET_SVC_FRAME_DROP_LAYER:
        return vpx_codec_control_(ctx, VP9E_SET_SVC_FRAME_DROP_LAYER, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                void* param) const override {
    switch (ctrl_id) {
      case VP9E_SET_SVC_PARAMETERS:
        return vpx_codec_control_(ctx, VP9E_SET_SVC_PARAMETERS, param);
      case VP9E_REGISTER_CX_CALLBACK:
        return vpx_codec_control_(ctx, VP9E_REGISTER_CX_CALLBACK, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                vpx_svc_layer_id_t* param) const override {
    switch (ctrl_id) {
      case VP9E_SET_SVC_LAYER_ID:
        return vpx_codec_control_(ctx, VP9E_SET_SVC_LAYER_ID, param);
      case VP9E_GET_SVC_LAYER_ID:
        return vpx_codec_control_(ctx, VP9E_GET_SVC_LAYER_ID, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(
      vpx_codec_ctx_t* ctx,
      vp8e_enc_control_id ctrl_id,
      vpx_svc_ref_frame_config_t* param) const override {
    switch (ctrl_id) {
      case VP9E_SET_SVC_REF_FRAME_CONFIG:
        return vpx_codec_control_(ctx, VP9E_SET_SVC_REF_FRAME_CONFIG, param);
      case VP9E_GET_SVC_REF_FRAME_CONFIG:
        return vpx_codec_control_(ctx, VP9E_GET_SVC_REF_FRAME_CONFIG, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(
      vpx_codec_ctx_t* ctx,
      vp8e_enc_control_id ctrl_id,
      vpx_svc_spatial_layer_sync_t* param) const override {
    switch (ctrl_id) {
      case VP9E_SET_SVC_SPATIAL_LAYER_SYNC:
        return vpx_codec_control_(ctx, VP9E_SET_SVC_SPATIAL_LAYER_SYNC, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t codec_control(vpx_codec_ctx_t* ctx,
                                vp8e_enc_control_id ctrl_id,
                                vpx_rc_funcs_t* param) const override {
    switch (ctrl_id) {
      case VP9E_SET_EXTERNAL_RATE_CONTROL:
        return vpx_codec_control_(ctx, VP9E_SET_EXTERNAL_RATE_CONTROL, param);
      default:
        RTC_DCHECK_NOTREACHED() << "Unsupported libvpx ctrl_id: " << ctrl_id;
    }
    return VPX_CODEC_ERROR;
  }

  vpx_codec_err_t 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) const override {
    return ::vpx_codec_encode(ctx, img, pts, duration, flags, deadline);
  }

  const vpx_codec_cx_pkt_t* codec_get_cx_data(
      vpx_codec_ctx_t* ctx,
      vpx_codec_iter_t* iter) const override {
    return ::vpx_codec_get_cx_data(ctx, iter);
  }

  const char* codec_error_detail(vpx_codec_ctx_t* ctx) const override {
    return ::vpx_codec_error_detail(ctx);
  }

  const char* codec_error(vpx_codec_ctx_t* ctx) const override {
    return ::vpx_codec_error(ctx);
  }

  const char* codec_err_to_string(vpx_codec_err_t err) const override {
    return ::vpx_codec_err_to_string(err);
  }
};

}  // namespace

std::unique_ptr<LibvpxInterface> LibvpxInterface::Create() {
  return std::make_unique<LibvpxFacade>();
}

}  // namespace webrtc