1 /*
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 
12 #ifndef MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_
13 #define MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_
14 
15 #ifdef RTC_ENABLE_VP9
16 
17 #include <map>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "api/fec_controller_override.h"
23 #include "api/video_codecs/video_encoder.h"
24 #include "media/base/vp9_profile.h"
25 #include "modules/video_coding/codecs/vp9/include/vp9.h"
26 #include "modules/video_coding/codecs/vp9/vp9_frame_buffer_pool.h"
27 #include "modules/video_coding/utility/framerate_controller.h"
28 #include "vpx/vp8cx.h"
29 #include "vpx/vpx_decoder.h"
30 #include "vpx/vpx_encoder.h"
31 
32 namespace webrtc {
33 
34 class VP9EncoderImpl : public VP9Encoder {
35  public:
36   explicit VP9EncoderImpl(const cricket::VideoCodec& codec);
37 
38   ~VP9EncoderImpl() override;
39 
40   void SetFecControllerOverride(
41       FecControllerOverride* fec_controller_override) override;
42 
43   int Release() override;
44 
45   int InitEncode(const VideoCodec* codec_settings,
46                  const Settings& settings) override;
47 
48   int Encode(const VideoFrame& input_image,
49              const std::vector<VideoFrameType>* frame_types) override;
50 
51   int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
52 
53   void SetRates(const RateControlParameters& parameters) override;
54 
55   EncoderInfo GetEncoderInfo() const override;
56 
57  private:
58   // Determine number of encoder threads to use.
59   int NumberOfThreads(int width, int height, int number_of_cores);
60 
61   // Call encoder initialize function and set control settings.
62   int InitAndSetControlSettings(const VideoCodec* inst);
63 
64   // Update frame size for codec.
65   int UpdateCodecFrameSize(const VideoFrame& input_image);
66 
67   void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
68                              absl::optional<int>* spatial_idx,
69                              const vpx_codec_cx_pkt& pkt,
70                              uint32_t timestamp);
71   void FillReferenceIndices(const vpx_codec_cx_pkt& pkt,
72                             const size_t pic_num,
73                             const bool inter_layer_predicted,
74                             CodecSpecificInfoVP9* vp9_info);
75   void UpdateReferenceBuffers(const vpx_codec_cx_pkt& pkt,
76                               const size_t pic_num);
77   vpx_svc_ref_frame_config_t SetReferences(
78       bool is_key_pic,
79       size_t first_active_spatial_layer_id);
80 
81   bool ExplicitlyConfiguredSpatialLayers() const;
82   bool SetSvcRates(const VideoBitrateAllocation& bitrate_allocation);
83 
84   virtual int GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt);
85 
86   // Callback function for outputting packets per spatial layer.
87   static void EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
88                                                void* user_data);
89 
90   void DeliverBufferedFrame(bool end_of_picture);
91 
92   bool DropFrame(uint8_t spatial_idx, uint32_t rtp_timestamp);
93 
94   // Determine maximum target for Intra frames
95   //
96   // Input:
97   //    - optimal_buffer_size : Optimal buffer size
98   // Return Value             : Max target size for Intra frames represented as
99   //                            percentage of the per frame bandwidth
100   uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
101 
102   size_t SteadyStateSize(int sid, int tid);
103 
104   EncodedImage encoded_image_;
105   CodecSpecificInfo codec_specific_;
106   EncodedImageCallback* encoded_complete_callback_;
107   VideoCodec codec_;
108   const VP9Profile profile_;
109   bool inited_;
110   int64_t timestamp_;
111   int cpu_speed_;
112   uint32_t rc_max_intra_target_;
113   vpx_codec_ctx_t* encoder_;
114   vpx_codec_enc_cfg_t* config_;
115   vpx_image_t* raw_;
116   vpx_svc_extra_cfg_t svc_params_;
117   const VideoFrame* input_image_;
118   GofInfoVP9 gof_;  // Contains each frame's temporal information for
119                     // non-flexible mode.
120   bool force_key_frame_;
121   size_t pics_since_key_;
122   uint8_t num_temporal_layers_;
123   uint8_t num_spatial_layers_;         // Number of configured SLs
124   uint8_t num_active_spatial_layers_;  // Number of actively encoded SLs
125   uint8_t first_active_layer_;
126   bool layer_deactivation_requires_key_frame_;
127   bool is_svc_;
128   InterLayerPredMode inter_layer_pred_;
129   bool external_ref_control_;
130   const bool trusted_rate_controller_;
131   const bool dynamic_rate_settings_;
132   bool layer_buffering_;
133   const bool full_superframe_drop_;
134   vpx_svc_frame_drop_t svc_drop_frame_;
135   bool first_frame_in_picture_;
136   VideoBitrateAllocation current_bitrate_allocation_;
137   bool ss_info_needed_;
138   bool force_all_active_layers_;
139   uint8_t num_cores_;
140 
141   std::vector<FramerateController> framerate_controller_;
142 
143   // Used for flexible mode.
144   bool is_flexible_mode_;
145   struct RefFrameBuffer {
RefFrameBufferRefFrameBuffer146     RefFrameBuffer(size_t pic_num,
147                    size_t spatial_layer_id,
148                    size_t temporal_layer_id)
149         : pic_num(pic_num),
150           spatial_layer_id(spatial_layer_id),
151           temporal_layer_id(temporal_layer_id) {}
RefFrameBufferRefFrameBuffer152     RefFrameBuffer() {}
153 
154     bool operator==(const RefFrameBuffer& o) {
155       return pic_num == o.pic_num && spatial_layer_id == o.spatial_layer_id &&
156              temporal_layer_id == o.temporal_layer_id;
157     }
158 
159     size_t pic_num = 0;
160     size_t spatial_layer_id = 0;
161     size_t temporal_layer_id = 0;
162   };
163   std::map<size_t, RefFrameBuffer> ref_buf_;
164 
165   // Variable frame-rate related fields and methods.
166   const struct VariableFramerateExperiment {
167     bool enabled;
168     // Framerate is limited to this value in steady state.
169     float framerate_limit;
170     // This qp or below is considered a steady state.
171     int steady_state_qp;
172     // Frames of at least this percentage below ideal for configured bitrate are
173     // considered in a steady state.
174     int steady_state_undershoot_percentage;
175     // Number of consecutive frames with good QP and size required to detect
176     // the steady state.
177     int frames_before_steady_state;
178   } variable_framerate_experiment_;
179   static VariableFramerateExperiment ParseVariableFramerateConfig(
180       std::string group_name);
181   FramerateController variable_framerate_controller_;
182 
183   const struct QualityScalerExperiment {
184     int low_qp;
185     int high_qp;
186     bool enabled;
187   } quality_scaler_experiment_;
188   static QualityScalerExperiment ParseQualityScalerConfig(
189       std::string group_name);
190 
191   int num_steady_state_frames_;
192   // Only set config when this flag is set.
193   bool config_changed_;
194 };
195 
196 class VP9DecoderImpl : public VP9Decoder {
197  public:
198   VP9DecoderImpl();
199 
200   virtual ~VP9DecoderImpl();
201 
202   int InitDecode(const VideoCodec* inst, int number_of_cores) override;
203 
204   int Decode(const EncodedImage& input_image,
205              bool missing_frames,
206              int64_t /*render_time_ms*/) override;
207 
208   int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override;
209 
210   int Release() override;
211 
212   const char* ImplementationName() const override;
213 
214  private:
215   int ReturnFrame(const vpx_image_t* img,
216                   uint32_t timestamp,
217                   int qp,
218                   const webrtc::ColorSpace* explicit_color_space);
219 
220   // Memory pool used to share buffers between libvpx and webrtc.
221   Vp9FrameBufferPool frame_buffer_pool_;
222   DecodedImageCallback* decode_complete_callback_;
223   bool inited_;
224   vpx_codec_ctx_t* decoder_;
225   bool key_frame_required_;
226   VideoCodec current_codec_;
227   int num_cores_;
228 };
229 }  // namespace webrtc
230 
231 #endif  // RTC_ENABLE_VP9
232 
233 #endif  // MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_
234