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 WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_IMPL_H_ 14 #define WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_IMPL_H_ 15 16 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" 17 18 // VPX forward declaration 19 typedef struct vpx_codec_ctx vpx_codec_ctx_t; 20 typedef struct vpx_codec_ctx vpx_dec_ctx_t; 21 typedef struct vpx_codec_enc_cfg vpx_codec_enc_cfg_t; 22 typedef struct vpx_image vpx_image_t; 23 typedef struct vpx_ref_frame vpx_ref_frame_t; 24 struct vpx_codec_cx_pkt; 25 26 namespace webrtc { 27 28 class TemporalLayers; 29 class ReferencePictureSelection; 30 31 class VP8EncoderImpl : public VP8Encoder { 32 public: 33 VP8EncoderImpl(); 34 35 virtual ~VP8EncoderImpl(); 36 37 // Free encoder memory. 38 // 39 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. 40 virtual int Release(); 41 42 // Initialize the encoder with the information from the codecSettings 43 // 44 // Input: 45 // - codec_settings : Codec settings 46 // - number_of_cores : Number of cores available for the encoder 47 // - max_payload_size : The maximum size each payload is allowed 48 // to have. Usually MTU - overhead. 49 // 50 // Return value : Set bit rate if OK 51 // <0 - Errors: 52 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER 53 // WEBRTC_VIDEO_CODEC_ERR_SIZE 54 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED 55 // WEBRTC_VIDEO_CODEC_MEMORY 56 // WEBRTC_VIDEO_CODEC_ERROR 57 virtual int InitEncode(const VideoCodec* codec_settings, 58 int number_of_cores, 59 uint32_t max_payload_size); 60 61 // Encode an I420 image (as a part of a video stream). The encoded image 62 // will be returned to the user through the encode complete callback. 63 // 64 // Input: 65 // - input_image : Image to be encoded 66 // - frame_types : Frame type to be generated by the encoder. 67 // 68 // Return value : WEBRTC_VIDEO_CODEC_OK if OK 69 // <0 - Errors: 70 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER 71 // WEBRTC_VIDEO_CODEC_MEMORY 72 // WEBRTC_VIDEO_CODEC_ERROR 73 // WEBRTC_VIDEO_CODEC_TIMEOUT 74 75 virtual int Encode(const I420VideoFrame& input_image, 76 const CodecSpecificInfo* codec_specific_info, 77 const std::vector<VideoFrameType>* frame_types); 78 79 // Register an encode complete callback object. 80 // 81 // Input: 82 // - callback : Callback object which handles encoded images. 83 // 84 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. 85 virtual int RegisterEncodeCompleteCallback(EncodedImageCallback* callback); 86 87 // Inform the encoder of the new packet loss rate and the round-trip time of 88 // the network. 89 // 90 // - packet_loss : Fraction lost 91 // (loss rate in percent = 100 * packetLoss / 255) 92 // - rtt : Round-trip time in milliseconds 93 // Return value : WEBRTC_VIDEO_CODEC_OK if OK 94 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR 95 // 96 virtual int SetChannelParameters(uint32_t packet_loss, int rtt); 97 98 // Inform the encoder about the new target bit rate. 99 // 100 // - new_bitrate_kbit : New target bit rate 101 // - frame_rate : The target frame rate 102 // 103 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. 104 virtual int SetRates(uint32_t new_bitrate_kbit, uint32_t frame_rate); 105 106 private: 107 // Call encoder initialize function and set control settings. 108 int InitAndSetControlSettings(const VideoCodec* inst); 109 110 // Update frame size for codec. 111 int UpdateCodecFrameSize(const I420VideoFrame& input_image); 112 113 void PopulateCodecSpecific(CodecSpecificInfo* codec_specific, 114 const vpx_codec_cx_pkt& pkt, 115 uint32_t timestamp); 116 117 int GetEncodedPartitions(const I420VideoFrame& input_image); 118 119 // Determine maximum target for Intra frames 120 // 121 // Input: 122 // - optimal_buffer_size : Optimal buffer size 123 // Return Value : Max target size for Intra frames represented as 124 // percentage of the per frame bandwidth 125 uint32_t MaxIntraTarget(uint32_t optimal_buffer_size); 126 127 EncodedImage encoded_image_; 128 EncodedImageCallback* encoded_complete_callback_; 129 VideoCodec codec_; 130 bool inited_; 131 int64_t timestamp_; 132 uint16_t picture_id_; 133 bool feedback_mode_; 134 int cpu_speed_; 135 uint32_t rc_max_intra_target_; 136 int token_partitions_; 137 ReferencePictureSelection* rps_; 138 TemporalLayers* temporal_layers_; 139 vpx_codec_ctx_t* encoder_; 140 vpx_codec_enc_cfg_t* config_; 141 vpx_image_t* raw_; 142 }; // end of VP8Encoder class 143 144 145 class VP8DecoderImpl : public VP8Decoder { 146 public: 147 VP8DecoderImpl(); 148 149 virtual ~VP8DecoderImpl(); 150 151 // Initialize the decoder. 152 // 153 // Return value : WEBRTC_VIDEO_CODEC_OK. 154 // <0 - Errors: 155 // WEBRTC_VIDEO_CODEC_ERROR 156 virtual int InitDecode(const VideoCodec* inst, int number_of_cores); 157 158 // Decode encoded image (as a part of a video stream). The decoded image 159 // will be returned to the user through the decode complete callback. 160 // 161 // Input: 162 // - input_image : Encoded image to be decoded 163 // - missing_frames : True if one or more frames have been lost 164 // since the previous decode call. 165 // - fragmentation : Specifies the start and length of each VP8 166 // partition. 167 // - codec_specific_info : pointer to specific codec data 168 // - render_time_ms : Render time in Ms 169 // 170 // Return value : WEBRTC_VIDEO_CODEC_OK if OK 171 // <0 - Errors: 172 // WEBRTC_VIDEO_CODEC_ERROR 173 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER 174 virtual int Decode(const EncodedImage& input_image, 175 bool missing_frames, 176 const RTPFragmentationHeader* fragmentation, 177 const CodecSpecificInfo* codec_specific_info, 178 int64_t /*render_time_ms*/); 179 180 // Register a decode complete callback object. 181 // 182 // Input: 183 // - callback : Callback object which handles decoded images. 184 // 185 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. 186 virtual int RegisterDecodeCompleteCallback(DecodedImageCallback* callback); 187 188 // Free decoder memory. 189 // 190 // Return value : WEBRTC_VIDEO_CODEC_OK if OK 191 // <0 - Errors: 192 // WEBRTC_VIDEO_CODEC_ERROR 193 virtual int Release(); 194 195 // Reset decoder state and prepare for a new call. 196 // 197 // Return value : WEBRTC_VIDEO_CODEC_OK. 198 // <0 - Errors: 199 // WEBRTC_VIDEO_CODEC_UNINITIALIZED 200 // WEBRTC_VIDEO_CODEC_ERROR 201 virtual int Reset(); 202 203 // Create a copy of the codec and its internal state. 204 // 205 // Return value : A copy of the instance if OK, NULL otherwise. 206 virtual VideoDecoder* Copy(); 207 208 private: 209 // Copy reference image from this _decoder to the _decoder in copyTo. Set 210 // which frame type to copy in _refFrame->frame_type before the call to 211 // this function. 212 int CopyReference(VP8Decoder* copy); 213 214 int DecodePartitions(const EncodedImage& input_image, 215 const RTPFragmentationHeader* fragmentation); 216 217 int ReturnFrame(const vpx_image_t* img, 218 uint32_t timeStamp, 219 int64_t ntp_time_ms); 220 221 I420VideoFrame decoded_image_; 222 DecodedImageCallback* decode_complete_callback_; 223 bool inited_; 224 bool feedback_mode_; 225 vpx_dec_ctx_t* decoder_; 226 VideoCodec codec_; 227 EncodedImage last_keyframe_; 228 int image_format_; 229 vpx_ref_frame_t* ref_frame_; 230 int propagation_cnt_; 231 bool mfqe_enabled_; 232 bool key_frame_required_; 233 }; // end of VP8Decoder class 234 } // namespace webrtc 235 236 #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_IMPL_H_ 237