1 /* 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 13 #ifndef MODULES_VIDEO_CODING_CODECS_VP8_VP8_IMPL_H_ 14 #define MODULES_VIDEO_CODING_CODECS_VP8_VP8_IMPL_H_ 15 16 #include <memory> 17 #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/vp8cx.h" 22 #include "vpx/vp8dx.h" 23 #include "vpx/vpx_decoder.h" 24 #include "vpx/vpx_encoder.h" 25 26 #include "api/video/video_frame.h" 27 #include "common_video/include/i420_buffer_pool.h" 28 #include "common_video/include/video_frame.h" 29 #include "modules/video_coding/codecs/vp8/include/vp8.h" 30 #include "modules/video_coding/codecs/vp8/temporal_layers.h" 31 #include "modules/video_coding/include/video_codec_interface.h" 32 #include "modules/video_coding/utility/quality_scaler.h" 33 34 namespace webrtc { 35 36 class TemporalLayers; 37 38 class VP8EncoderImpl : public VP8Encoder { 39 public: 40 VP8EncoderImpl(); 41 42 virtual ~VP8EncoderImpl(); 43 44 int Release() override; 45 46 int InitEncode(const VideoCodec* codec_settings, 47 int number_of_cores, 48 size_t max_payload_size) override; 49 50 int Encode(const VideoFrame& input_image, 51 const CodecSpecificInfo* codec_specific_info, 52 const std::vector<FrameType>* frame_types) override; 53 54 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; 55 56 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; 57 58 int SetRateAllocation(const BitrateAllocation& bitrate, 59 uint32_t new_framerate) override; 60 61 ScalingSettings GetScalingSettings() const override; 62 63 const char* ImplementationName() const override; 64 65 static vpx_enc_frame_flags_t EncodeFlags( 66 const TemporalLayers::FrameConfig& references); 67 68 private: 69 void SetupTemporalLayers(int num_streams, 70 int num_temporal_layers, 71 const VideoCodec& codec); 72 73 // Set the cpu_speed setting for encoder based on resolution and/or platform. 74 int SetCpuSpeed(int width, int height); 75 76 // Determine number of encoder threads to use. 77 int NumberOfThreads(int width, int height, int number_of_cores); 78 79 // Call encoder initialize function and set control settings. 80 int InitAndSetControlSettings(); 81 82 void PopulateCodecSpecific(CodecSpecificInfo* codec_specific, 83 const TemporalLayers::FrameConfig& tl_config, 84 const vpx_codec_cx_pkt& pkt, 85 int stream_idx, 86 uint32_t timestamp); 87 88 int GetEncodedPartitions(const TemporalLayers::FrameConfig tl_configs[], 89 const VideoFrame& input_image); 90 91 // Set the stream state for stream |stream_idx|. 92 void SetStreamState(bool send_stream, int stream_idx); 93 94 uint32_t MaxIntraTarget(uint32_t optimal_buffer_size); 95 96 const bool use_gf_boost_; 97 98 EncodedImageCallback* encoded_complete_callback_; 99 VideoCodec codec_; 100 bool inited_; 101 int64_t timestamp_; 102 int qp_max_; 103 int cpu_speed_default_; 104 int number_of_cores_; 105 uint32_t rc_max_intra_target_; 106 std::vector<std::unique_ptr<TemporalLayers>> temporal_layers_; 107 std::vector<std::unique_ptr<TemporalLayersChecker>> temporal_layers_checkers_; 108 std::vector<uint16_t> picture_id_; 109 std::vector<uint8_t> tl0_pic_idx_; 110 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_; 118 }; 119 120 class VP8DecoderImpl : public VP8Decoder { 121 public: 122 VP8DecoderImpl(); 123 124 virtual ~VP8DecoderImpl(); 125 126 int InitDecode(const VideoCodec* inst, int number_of_cores) override; 127 128 int Decode(const EncodedImage& input_image, 129 bool missing_frames, 130 const RTPFragmentationHeader* fragmentation, 131 const CodecSpecificInfo* codec_specific_info, 132 int64_t /*render_time_ms*/) override; 133 134 int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override; 135 int Release() override; 136 137 const char* ImplementationName() const override; 138 139 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 145 private: 146 class QpSmoother; 147 int ReturnFrame(const vpx_image_t* img, 148 uint32_t timeStamp, 149 int64_t ntp_time_ms, 150 int qp); 151 152 const bool use_postproc_arm_; 153 154 I420BufferPool buffer_pool_; 155 DecodedImageCallback* decode_complete_callback_; 156 bool inited_; 157 vpx_codec_ctx_t* decoder_; 158 int propagation_cnt_; 159 int last_frame_width_; 160 int last_frame_height_; 161 bool key_frame_required_; 162 DeblockParams deblock_; 163 const std::unique_ptr<QpSmoother> qp_smoother_; 164 }; 165 } // namespace webrtc 166 167 #endif // MODULES_VIDEO_CODING_CODECS_VP8_VP8_IMPL_H_ 168