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