Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / cast / video_sender / codecs / vp8 / vp8_encoder.h
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.
4
5 #ifndef MEDIA_CAST_VIDEO_SENDER_CODECS_VP8_VP8_ENCODER_H_
6 #define MEDIA_CAST_VIDEO_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/video_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, uint8 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                       transport::EncodedVideoFrame* 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   void InitEncode(int number_of_cores);
59
60   // Calculate the max target in % for a keyframe.
61   uint32 MaxIntraTarget(uint32 optimal_buffer_size) const;
62
63   // Calculate which next Vp8 buffers to update with the next frame.
64   Vp8Buffers GetNextBufferToUpdate();
65
66   // Calculate which previous frame to reference.
67   uint32 GetLatestFrameIdToReference();
68
69   // Get encoder flags for our referenced encoder buffers.
70   void GetCodecReferenceFlags(vpx_codec_flags_t* flags);
71
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);
75
76   const VideoSenderConfig cast_config_;
77   const bool use_multiple_video_buffers_;
78   const int max_number_of_repeated_buffers_in_a_row_;
79
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_;
84
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_;
93
94   // This is bound to the thread where Initialize() is called.
95   base::ThreadChecker thread_checker_;
96
97   DISALLOW_COPY_AND_ASSIGN(Vp8Encoder);
98 };
99
100 }  // namespace cast
101 }  // namespace media
102
103 #endif  // MEDIA_CAST_VIDEO_SENDER_CODECS_VP8_VP8_ENCODER_H_