blob: 30a4a1a642ce60f66fb9062e0982734166312984 [file] [log] [blame]
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +00001/*
2 * Copyright (c) 2012 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 * WEBRTC VP8 wrapper interface
11 */
12
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000013#ifndef WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_VP8_IMPL_H_
14#define WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_VP8_IMPL_H_
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000015
Erik Språng78ce6192016-09-12 16:04:43 +020016#include <memory>
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000017#include <vector>
18
19// NOTE: This include order must remain to avoid compile errors, even though
20// it breaks the style guide.
21#include "vpx/vpx_encoder.h"
22#include "vpx/vpx_decoder.h"
23#include "vpx/vp8cx.h"
24#include "vpx/vp8dx.h"
25
nisseaf916892017-01-10 07:44:26 -080026#include "webrtc/api/video/video_frame.h"
kjellander6f8ce062015-11-16 13:52:24 -080027#include "webrtc/common_video/include/i420_buffer_pool.h"
nisseea3a7982017-05-15 02:42:11 -070028#include "webrtc/common_video/include/video_frame.h"
pbos@webrtc.orga4407322013-07-16 12:32:05 +000029#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
pbosfa18e252017-05-02 02:51:12 -070030#include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h"
31#include "webrtc/modules/video_coding/include/video_codec_interface.h"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010032#include "webrtc/modules/video_coding/utility/quality_scaler.h"
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000033
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000034namespace webrtc {
35
36class TemporalLayers;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000037
38class VP8EncoderImpl : public VP8Encoder {
39 public:
40 VP8EncoderImpl();
41
42 virtual ~VP8EncoderImpl();
43
nisseef8b61e2016-04-29 06:09:15 -070044 int Release() override;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000045
nisseef8b61e2016-04-29 06:09:15 -070046 int InitEncode(const VideoCodec* codec_settings,
47 int number_of_cores,
48 size_t max_payload_size) override;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000049
nisseef8b61e2016-04-29 06:09:15 -070050 int Encode(const VideoFrame& input_image,
51 const CodecSpecificInfo* codec_specific_info,
52 const std::vector<FrameType>* frame_types) override;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000053
nisseef8b61e2016-04-29 06:09:15 -070054 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000055
nisseef8b61e2016-04-29 06:09:15 -070056 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000057
Erik Språng08127a92016-11-16 16:41:30 +010058 int SetRateAllocation(const BitrateAllocation& bitrate,
59 uint32_t new_framerate) override;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000060
kthelgason876222f2016-11-29 01:44:11 -080061 ScalingSettings GetScalingSettings() const override;
jackychen61b4d512015-04-21 15:30:11 -070062
Peter Boströmb7d9a972015-12-18 16:01:11 +010063 const char* ImplementationName() const override;
64
pbos18ad1d42017-05-04 05:04:46 -070065 static vpx_enc_frame_flags_t EncodeFlags(
66 const TemporalLayers::FrameConfig& references);
pbosfa18e252017-05-02 02:51:12 -070067
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000068 private:
philipelcce46fc2015-12-21 03:04:49 -080069 void SetupTemporalLayers(int num_streams,
70 int num_temporal_layers,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000071 const VideoCodec& codec);
72
marpan@webrtc.org6daacbc2015-03-04 21:47:06 +000073 // Set the cpu_speed setting for encoder based on resolution and/or platform.
74 int SetCpuSpeed(int width, int height);
75
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000076 // Determine number of encoder threads to use.
77 int NumberOfThreads(int width, int height, int number_of_cores);
78
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000079 // Call encoder initialize function and set control settings.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000080 int InitAndSetControlSettings();
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000081
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000082 void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
pbos18ad1d42017-05-04 05:04:46 -070083 const TemporalLayers::FrameConfig& tl_config,
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000084 const vpx_codec_cx_pkt& pkt,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000085 int stream_idx,
nisse25d0bdc2017-03-22 07:15:09 -070086 uint32_t timestamp);
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000087
pbos18ad1d42017-05-04 05:04:46 -070088 int GetEncodedPartitions(const TemporalLayers::FrameConfig tl_configs[],
89 const VideoFrame& input_image);
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000090
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000091 // Set the stream state for stream |stream_idx|.
92 void SetStreamState(bool send_stream, int stream_idx);
93
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000094 uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
95
Peter Boström1182afe2017-03-21 12:35:51 -040096 const bool use_gf_boost_;
asapersson142fcc92017-08-17 08:58:54 -070097 const rtc::Optional<int> min_pixels_per_frame_;
Peter Boström1182afe2017-03-21 12:35:51 -040098
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +000099 EncodedImageCallback* encoded_complete_callback_;
100 VideoCodec codec_;
101 bool inited_;
102 int64_t timestamp_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000103 int qp_max_;
marpan@webrtc.org6daacbc2015-03-04 21:47:06 +0000104 int cpu_speed_default_;
asapersson384e7312016-11-01 04:08:22 -0700105 int number_of_cores_;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000106 uint32_t rc_max_intra_target_;
brandtr080830c2017-05-03 03:25:53 -0700107 std::vector<std::unique_ptr<TemporalLayers>> temporal_layers_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000108 std::vector<uint16_t> picture_id_;
brandtr080830c2017-05-03 03:25:53 -0700109 std::vector<uint8_t> tl0_pic_idx_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000110 std::vector<bool> key_frame_request_;
111 std::vector<bool> send_stream_;
112 std::vector<int> cpu_speed_;
113 std::vector<vpx_image_t> raw_images_;
114 std::vector<EncodedImage> encoded_images_;
115 std::vector<vpx_codec_ctx_t> encoders_;
116 std::vector<vpx_codec_enc_cfg_t> configurations_;
117 std::vector<vpx_rational_t> downsampling_factors_;
Peter Boström1182afe2017-03-21 12:35:51 -0400118};
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000119
120class VP8DecoderImpl : public VP8Decoder {
121 public:
122 VP8DecoderImpl();
123
124 virtual ~VP8DecoderImpl();
125
Peter Boströmb302ad42015-05-21 09:42:33 +0200126 int InitDecode(const VideoCodec* inst, int number_of_cores) override;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000127
Peter Boströmb302ad42015-05-21 09:42:33 +0200128 int Decode(const EncodedImage& input_image,
philipelcce46fc2015-12-21 03:04:49 -0800129 bool missing_frames,
130 const RTPFragmentationHeader* fragmentation,
131 const CodecSpecificInfo* codec_specific_info,
132 int64_t /*render_time_ms*/) override;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000133
Peter Boströmb302ad42015-05-21 09:42:33 +0200134 int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override;
135 int Release() override;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000136
Peter Boströmb7d9a972015-12-18 16:01:11 +0100137 const char* ImplementationName() const override;
138
asaperssone5d02f92017-08-09 23:37:05 -0700139 struct DeblockParams {
140 int max_level = 6; // Deblocking strength: [0, 16].
141 int degrade_qp = 1; // If QP value is below, start lowering |max_level|.
142 int min_qp = 0; // If QP value is below, turn off deblocking.
143 };
144
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000145 private:
asaperssone5d02f92017-08-09 23:37:05 -0700146 class QpSmoother;
wu@webrtc.org6c75c982014-04-15 17:46:33 +0000147 int ReturnFrame(const vpx_image_t* img,
148 uint32_t timeStamp,
sakal5fec1282017-02-20 06:43:58 -0800149 int64_t ntp_time_ms,
150 int qp);
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000151
Peter Boström1182afe2017-03-21 12:35:51 -0400152 const bool use_postproc_arm_;
153
magjed@webrtc.org73d763e2015-03-17 11:40:45 +0000154 I420BufferPool buffer_pool_;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000155 DecodedImageCallback* decode_complete_callback_;
156 bool inited_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000157 vpx_codec_ctx_t* decoder_;
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000158 int propagation_cnt_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000159 int last_frame_width_;
160 int last_frame_height_;
mikhal@webrtc.orgb2c28c32013-08-23 21:54:50 +0000161 bool key_frame_required_;
asaperssone5d02f92017-08-09 23:37:05 -0700162 DeblockParams deblock_;
163 const std::unique_ptr<QpSmoother> qp_smoother_;
Peter Boström1182afe2017-03-21 12:35:51 -0400164};
pwestin@webrtc.org8d89b582012-09-20 20:49:12 +0000165} // namespace webrtc
166
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000167#endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_VP8_IMPL_H_