vp9_impl.h 7.8 KB

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