1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MEDIA_CAST_SENDER_CODECS_VP8_VP8_ENCODER_H_ 6 #define MEDIA_CAST_SENDER_CODECS_VP8_VP8_ENCODER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/threading/thread_checker.h" 11 #include "base/time/time.h" 12 #include "media/cast/cast_config.h" 13 #include "media/cast/sender/software_video_encoder.h" 14 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h" 15 16 namespace media { 17 class VideoFrame; 18 } 19 20 // VPX forward declaration. 21 typedef struct vpx_codec_ctx vpx_enc_ctx_t; 22 23 namespace media { 24 namespace cast { 25 26 const int kNumberOfVp8VideoBuffers = 3; 27 28 class Vp8Encoder : public SoftwareVideoEncoder { 29 public: 30 Vp8Encoder(const VideoSenderConfig& video_config, int max_unacked_frames); 31 32 virtual ~Vp8Encoder(); 33 34 // Initialize the encoder before Encode() can be called. This method 35 // must be called on the thread that Encode() is called. 36 virtual void Initialize() OVERRIDE; 37 38 // Encode a raw image (as a part of a video stream). 39 virtual bool Encode(const scoped_refptr<media::VideoFrame>& video_frame, 40 EncodedFrame* encoded_image) OVERRIDE; 41 42 // Update the encoder with a new target bit rate. 43 virtual void UpdateRates(uint32 new_bitrate) OVERRIDE; 44 45 // Set the next frame to be a key frame. 46 virtual void GenerateKeyFrame() OVERRIDE; 47 48 virtual void LatestFrameIdToReference(uint32 frame_id) OVERRIDE; 49 50 private: 51 enum Vp8Buffers { 52 kAltRefBuffer = 0, 53 kGoldenBuffer = 1, 54 kLastBuffer = 2, 55 kNoBuffer = 3 // Note: must be last. 56 }; 57 58 enum Vp8BufferState { 59 kBufferStartState, 60 kBufferSent, 61 kBufferAcked 62 }; 63 struct BufferState { 64 uint32 frame_id; 65 Vp8BufferState state; 66 }; 67 68 void InitEncode(int number_of_cores); 69 70 // Calculate the max target in % for a keyframe. 71 uint32 MaxIntraTarget(uint32 optimal_buffer_size) const; 72 73 // Calculate which next Vp8 buffers to update with the next frame. 74 Vp8Buffers GetNextBufferToUpdate(); 75 76 // Get encoder flags for our referenced encoder buffers. 77 // Return which previous frame to reference. 78 uint32 GetCodecReferenceFlags(vpx_codec_flags_t* flags); 79 80 // Get encoder flags for our encoder buffers to update with next frame. 81 void GetCodecUpdateFlags(Vp8Buffers buffer_to_update, 82 vpx_codec_flags_t* flags); 83 84 const VideoSenderConfig cast_config_; 85 const bool use_multiple_video_buffers_; 86 87 // VP8 internal objects. 88 scoped_ptr<vpx_codec_enc_cfg_t> config_; 89 scoped_ptr<vpx_enc_ctx_t> encoder_; 90 vpx_image_t* raw_image_; 91 92 bool key_frame_requested_; 93 bool first_frame_received_; 94 base::TimeDelta first_frame_timestamp_; 95 uint32 last_encoded_frame_id_; 96 uint32 last_acked_frame_id_; 97 uint32 frame_id_to_reference_; 98 uint32 undroppable_frames_; 99 BufferState buffer_state_[kNumberOfVp8VideoBuffers]; 100 101 // This is bound to the thread where Initialize() is called. 102 base::ThreadChecker thread_checker_; 103 104 DISALLOW_COPY_AND_ASSIGN(Vp8Encoder); 105 }; 106 107 } // namespace cast 108 } // namespace media 109 110 #endif // MEDIA_CAST_SENDER_CODECS_VP8_VP8_ENCODER_H_ 111