- add sources.
[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 "media/cast/cast_config.h"
11 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h"
12
13 // VPX forward declaration.
14 typedef struct vpx_codec_ctx vpx_enc_ctx_t;
15
16 namespace media {
17 namespace cast {
18
19 const int kNumberOfVp8VideoBuffers = 3;
20
21 class Vp8Encoder {
22  public:
23   Vp8Encoder(const VideoSenderConfig& video_config,
24              uint8 max_unacked_frames);
25
26   ~Vp8Encoder();
27
28   // Encode a raw image (as a part of a video stream).
29   bool Encode(const I420VideoFrame& input_image,
30               EncodedVideoFrame* encoded_image);
31
32   // Update the encoder with a new target bit rate.
33   void UpdateRates(uint32 new_bitrate);
34
35   // Set the next frame to be a key frame.
36   void GenerateKeyFrame();
37
38   void LatestFrameIdToReference(uint8 frame_id);
39
40  private:
41   enum Vp8Buffers {
42     kAltRefBuffer = 0,
43     kGoldenBuffer = 1,
44     kLastBuffer = 2,
45     kNoBuffer = 3  // Note: must be last.
46   };
47
48   void InitEncode(int number_of_cores);
49
50   // Calculate the max target in % for a keyframe.
51   uint32 MaxIntraTarget(uint32 optimal_buffer_size) const;
52
53   // Calculate which next Vp8 buffers to update with the next frame.
54   Vp8Buffers GetNextBufferToUpdate();
55
56   // Calculate which previous frame to reference.
57   uint8 GetLatestFrameIdToReference();
58
59   // Get encoder flags for our referenced encoder buffers.
60   void GetCodecReferenceFlags(vpx_codec_flags_t* flags);
61
62   // Get encoder flags for our encoder buffers to update with next frame.
63   void GetCodecUpdateFlags(Vp8Buffers buffer_to_update,
64                            vpx_codec_flags_t* flags);
65
66   const VideoSenderConfig cast_config_;
67   const bool use_multiple_video_buffers_;
68   const int max_number_of_repeated_buffers_in_a_row_;
69
70   // VP8 internal objects.
71   scoped_ptr<vpx_codec_enc_cfg_t> config_;
72   vpx_enc_ctx_t* encoder_;
73   vpx_image_t* raw_image_;
74
75   bool key_frame_requested_;
76   int64 timestamp_;
77   uint8 last_encoded_frame_id_;
78   uint8 used_buffers_frame_id_[kNumberOfVp8VideoBuffers];
79   bool acked_frame_buffers_[kNumberOfVp8VideoBuffers];
80   Vp8Buffers last_used_vp8_buffer_;
81   int number_of_repeated_buffers_;
82 };
83
84 }  // namespace cast
85 }  // namespace media
86
87 #endif  // MEDIA_CAST_VIDEO_SENDER_CODECS_VP8_VP8_ENCODER_H_