blob: a968e4cc03974ea924e64848f18e7ad8d545fb71 [file] [log] [blame]
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001/*
2 * Copyright (c) 2014 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#ifndef MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_
13#define MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_
philipelcce46fc2015-12-21 03:04:49 -080014
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +020015#include <map>
kwiberg3f55dea2016-02-29 05:51:59 -080016#include <memory>
philipelcce46fc2015-12-21 03:04:49 -080017#include <vector>
marpan@webrtc.org5b883172014-11-01 06:10:48 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/codecs/vp9/include/vp9.h"
Emircan Uysaler98badbc2018-06-28 10:59:02 -070020
21#include "media/base/vp9_profile.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/codecs/vp9/vp9_frame_buffer_pool.h"
Sergey Silkinae3144c2018-08-29 01:05:26 +020023#include "modules/video_coding/utility/framerate_controller.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000024
johannkoenig8225c402017-01-26 13:23:44 -080025#include "vpx/vp8cx.h"
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000026#include "vpx/vpx_decoder.h"
27#include "vpx/vpx_encoder.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000028
29namespace webrtc {
30
31class VP9EncoderImpl : public VP9Encoder {
32 public:
Emircan Uysaler98badbc2018-06-28 10:59:02 -070033 explicit VP9EncoderImpl(const cricket::VideoCodec& codec);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000034
35 virtual ~VP9EncoderImpl();
36
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000037 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000038
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000039 int InitEncode(const VideoCodec* codec_settings,
40 int number_of_cores,
41 size_t max_payload_size) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000042
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070043 int Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000044 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -070045 const std::vector<FrameType>* frame_types) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000046
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000047 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000048
Erik Språng566124a2018-04-23 12:32:22 +020049 int SetRateAllocation(const VideoBitrateAllocation& bitrate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +010050 uint32_t frame_rate) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000051
Erik Språng727d1642018-11-07 16:54:15 +010052 EncoderInfo GetEncoderInfo() const override;
Peter Boströmb7d9a972015-12-18 16:01:11 +010053
marpan@webrtc.org5b883172014-11-01 06:10:48 +000054 private:
marpan@webrtc.org38d11b82015-01-26 15:21:36 +000055 // Determine number of encoder threads to use.
56 int NumberOfThreads(int width, int height, int number_of_cores);
57
marpan@webrtc.org5b883172014-11-01 06:10:48 +000058 // Call encoder initialize function and set control settings.
59 int InitAndSetControlSettings(const VideoCodec* inst);
60
61 void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
Niels Möllerd3b8c632018-08-27 15:33:42 +020062 absl::optional<int>* spatial_idx,
marpan@webrtc.org5b883172014-11-01 06:10:48 +000063 const vpx_codec_cx_pkt& pkt,
Sergey Silkin07f80cc2018-04-09 13:11:59 +020064 uint32_t timestamp,
65 bool first_frame_in_picture);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +020066 void FillReferenceIndices(const vpx_codec_cx_pkt& pkt,
67 const size_t pic_num,
68 const bool inter_layer_predicted,
69 CodecSpecificInfoVP9* vp9_info);
70 void UpdateReferenceBuffers(const vpx_codec_cx_pkt& pkt,
71 const size_t pic_num);
Sergey Silkin390f3582018-10-01 17:13:50 +020072 vpx_svc_ref_frame_config_t SetReferences(bool is_key_pic);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000073
sprangce4aef12015-11-02 07:23:20 -080074 bool ExplicitlyConfiguredSpatialLayers() const;
Erik Språng566124a2018-04-23 12:32:22 +020075 bool SetSvcRates(const VideoBitrateAllocation& bitrate_allocation);
asaperssona9455ab2015-07-31 06:10:09 -070076
77 virtual int GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt);
78
79 // Callback function for outputting packets per spatial layer.
80 static void EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
81 void* user_data);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000082
Sergey Silkinbc0f0d32018-04-24 21:29:14 +020083 void DeliverBufferedFrame(bool end_of_picture);
Sergey Silkin2a1f1832018-04-04 11:45:41 +020084
Sergey Silkinae3144c2018-08-29 01:05:26 +020085 bool DropFrame(uint8_t spatial_idx, uint32_t rtp_timestamp);
Sergey Silkinbe71a1e2018-05-17 16:46:43 +020086
marpan@webrtc.org5b883172014-11-01 06:10:48 +000087 // Determine maximum target for Intra frames
88 //
89 // Input:
90 // - optimal_buffer_size : Optimal buffer size
91 // Return Value : Max target size for Intra frames represented as
92 // percentage of the per frame bandwidth
93 uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
94
95 EncodedImage encoded_image_;
Sergey Silkin2a1f1832018-04-04 11:45:41 +020096 CodecSpecificInfo codec_specific_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000097 EncodedImageCallback* encoded_complete_callback_;
98 VideoCodec codec_;
Emircan Uysaler98badbc2018-06-28 10:59:02 -070099 const VP9Profile profile_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000100 bool inited_;
101 int64_t timestamp_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000102 int cpu_speed_;
103 uint32_t rc_max_intra_target_;
104 vpx_codec_ctx_t* encoder_;
105 vpx_codec_enc_cfg_t* config_;
106 vpx_image_t* raw_;
johannkoenig8225c402017-01-26 13:23:44 -0800107 vpx_svc_extra_cfg_t svc_params_;
asaperssona9455ab2015-07-31 06:10:09 -0700108 const VideoFrame* input_image_;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200109 GofInfoVP9 gof_; // Contains each frame's temporal information for
110 // non-flexible mode.
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200111 bool force_key_frame_;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200112 size_t pics_since_key_;
asaperssona9455ab2015-07-31 06:10:09 -0700113 uint8_t num_temporal_layers_;
“Michael23c5a992018-06-21 11:07:21 -0500114 uint8_t num_spatial_layers_; // Number of configured SLs
115 uint8_t num_active_spatial_layers_; // Number of actively encoded SLs
Sergey Silkine7ce8882018-10-03 18:04:57 +0200116 bool layer_deactivation_requires_key_frame_;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200117 bool is_svc_;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200118 InterLayerPredMode inter_layer_pred_;
Sergey Silkin390f3582018-10-01 17:13:50 +0200119 bool external_ref_control_;
Erik Språngd3438aa2018-11-08 16:56:43 +0100120 const bool trusted_rate_controller_;
philipelcfc319b2015-11-10 07:17:23 -0800121
Sergey Silkin96f2c972018-09-05 21:07:17 +0200122 std::vector<FramerateController> framerate_controller_;
Sergey Silkind902d582018-05-18 17:31:19 +0200123
philipelcfc319b2015-11-10 07:17:23 -0800124 // Used for flexible mode.
125 bool is_flexible_mode_;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200126 struct RefFrameBuffer {
127 RefFrameBuffer(size_t pic_num,
128 size_t spatial_layer_id,
129 size_t temporal_layer_id)
130 : pic_num(pic_num),
131 spatial_layer_id(spatial_layer_id),
132 temporal_layer_id(temporal_layer_id) {}
133 RefFrameBuffer() {}
Sergey Silkin82276592018-08-27 14:54:20 +0200134
135 bool operator==(const RefFrameBuffer& o) {
136 return pic_num == o.pic_num && spatial_layer_id == o.spatial_layer_id &&
137 temporal_layer_id == o.temporal_layer_id;
138 }
139
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200140 size_t pic_num = 0;
141 size_t spatial_layer_id = 0;
142 size_t temporal_layer_id = 0;
143 };
144 std::map<size_t, RefFrameBuffer> ref_buf_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000145};
146
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000147class VP9DecoderImpl : public VP9Decoder {
148 public:
149 VP9DecoderImpl();
150
151 virtual ~VP9DecoderImpl();
152
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000153 int InitDecode(const VideoCodec* inst, int number_of_cores) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000154
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000155 int Decode(const EncodedImage& input_image,
156 bool missing_frames,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000157 const CodecSpecificInfo* codec_specific_info,
158 int64_t /*render_time_ms*/) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000159
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000160 int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000161
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000162 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000163
Peter Boströmb7d9a972015-12-18 16:01:11 +0100164 const char* ImplementationName() const override;
165
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000166 private:
asapersson1490f7a2016-09-23 02:09:46 -0700167 int ReturnFrame(const vpx_image_t* img,
168 uint32_t timestamp,
sakal7adadb12017-02-23 02:54:57 -0800169 int64_t ntp_time_ms,
Johannes Kron9973fa82018-11-07 14:39:26 +0100170 int qp,
171 const HdrMetadata* hdr_metadata);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000172
Henrik Boström9695d852015-05-06 10:42:15 +0200173 // Memory pool used to share buffers between libvpx and webrtc.
174 Vp9FrameBufferPool frame_buffer_pool_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000175 DecodedImageCallback* decode_complete_callback_;
176 bool inited_;
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000177 vpx_codec_ctx_t* decoder_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000178 bool key_frame_required_;
179};
180} // namespace webrtc
181
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200182#endif // MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_