1 // Copyright 2013 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.
5 #ifndef MEDIA_CAST_VIDEO_SENDER_CODECS_VP8_VP8_ENCODER_H_
6 #define MEDIA_CAST_VIDEO_SENDER_CODECS_VP8_VP8_ENCODER_H_
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/video_sender/software_video_encoder.h"
14 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h"
20 // VPX forward declaration.
21 typedef struct vpx_codec_ctx vpx_enc_ctx_t;
26 const int kNumberOfVp8VideoBuffers = 3;
28 class Vp8Encoder : public SoftwareVideoEncoder {
30 Vp8Encoder(const VideoSenderConfig& video_config, uint8 max_unacked_frames);
32 virtual ~Vp8Encoder();
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;
38 // Encode a raw image (as a part of a video stream).
39 virtual bool Encode(const scoped_refptr<media::VideoFrame>& video_frame,
40 transport::EncodedVideoFrame* encoded_image) OVERRIDE;
42 // Update the encoder with a new target bit rate.
43 virtual void UpdateRates(uint32 new_bitrate) OVERRIDE;
45 // Set the next frame to be a key frame.
46 virtual void GenerateKeyFrame() OVERRIDE;
48 virtual void LatestFrameIdToReference(uint32 frame_id) OVERRIDE;
55 kNoBuffer = 3 // Note: must be last.
58 void InitEncode(int number_of_cores);
60 // Calculate the max target in % for a keyframe.
61 uint32 MaxIntraTarget(uint32 optimal_buffer_size) const;
63 // Calculate which next Vp8 buffers to update with the next frame.
64 Vp8Buffers GetNextBufferToUpdate();
66 // Calculate which previous frame to reference.
67 uint32 GetLatestFrameIdToReference();
69 // Get encoder flags for our referenced encoder buffers.
70 void GetCodecReferenceFlags(vpx_codec_flags_t* flags);
72 // Get encoder flags for our encoder buffers to update with next frame.
73 void GetCodecUpdateFlags(Vp8Buffers buffer_to_update,
74 vpx_codec_flags_t* flags);
76 const VideoSenderConfig cast_config_;
77 const bool use_multiple_video_buffers_;
78 const int max_number_of_repeated_buffers_in_a_row_;
80 // VP8 internal objects.
81 scoped_ptr<vpx_codec_enc_cfg_t> config_;
82 scoped_ptr<vpx_enc_ctx_t> encoder_;
83 vpx_image_t* raw_image_;
85 bool key_frame_requested_;
86 bool first_frame_received_;
87 base::TimeDelta first_frame_timestamp_;
88 uint32 last_encoded_frame_id_;
89 uint32 used_buffers_frame_id_[kNumberOfVp8VideoBuffers];
90 bool acked_frame_buffers_[kNumberOfVp8VideoBuffers];
91 Vp8Buffers last_used_vp8_buffer_;
92 int number_of_repeated_buffers_;
94 // This is bound to the thread where Initialize() is called.
95 base::ThreadChecker thread_checker_;
97 DISALLOW_COPY_AND_ASSIGN(Vp8Encoder);
103 #endif // MEDIA_CAST_VIDEO_SENDER_CODECS_VP8_VP8_ENCODER_H_