1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 *
10 * WEBRTC VP8 wrapper interface
11 */
12
13#ifndef WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_VP8_IMPL_H_
14#define WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_VP8_IMPL_H_
15
16#include <vector>
17
18// NOTE: This include order must remain to avoid compile errors, even though
19//       it breaks the style guide.
20#include "vpx/vpx_encoder.h"
21#include "vpx/vpx_decoder.h"
22#include "vpx/vp8cx.h"
23#include "vpx/vp8dx.h"
24
25#include "webrtc/common_video/include/i420_buffer_pool.h"
26#include "webrtc/modules/video_coding/include/video_codec_interface.h"
27#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
28#include "webrtc/modules/video_coding/codecs/vp8/reference_picture_selection.h"
29#include "webrtc/modules/video_coding/utility/frame_dropper.h"
30#include "webrtc/modules/video_coding/utility/quality_scaler.h"
31#include "webrtc/video_frame.h"
32
33namespace webrtc {
34
35class TemporalLayers;
36
37class VP8EncoderImpl : public VP8Encoder {
38 public:
39  VP8EncoderImpl();
40
41  virtual ~VP8EncoderImpl();
42
43  virtual int Release();
44
45  virtual int InitEncode(const VideoCodec* codec_settings,
46                         int number_of_cores,
47                         size_t max_payload_size);
48
49  virtual int Encode(const VideoFrame& input_image,
50                     const CodecSpecificInfo* codec_specific_info,
51                     const std::vector<FrameType>* frame_types);
52
53  virtual int RegisterEncodeCompleteCallback(EncodedImageCallback* callback);
54
55  virtual int SetChannelParameters(uint32_t packet_loss, int64_t rtt);
56
57  virtual int SetRates(uint32_t new_bitrate_kbit, uint32_t frame_rate);
58
59  void OnDroppedFrame() override {}
60
61  const char* ImplementationName() const override;
62
63 private:
64  void SetupTemporalLayers(int num_streams,
65                           int num_temporal_layers,
66                           const VideoCodec& codec);
67
68  // Set the cpu_speed setting for encoder based on resolution and/or platform.
69  int SetCpuSpeed(int width, int height);
70
71  // Determine number of encoder threads to use.
72  int NumberOfThreads(int width, int height, int number_of_cores);
73
74  // Call encoder initialize function and set control settings.
75  int InitAndSetControlSettings();
76
77  // Update frame size for codec.
78  int UpdateCodecFrameSize(const VideoFrame& input_image);
79
80  void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
81                             const vpx_codec_cx_pkt& pkt,
82                             int stream_idx,
83                             uint32_t timestamp,
84                             bool only_predicting_from_key_frame);
85
86  int GetEncodedPartitions(const VideoFrame& input_image,
87                           bool only_predicting_from_key_frame);
88
89  // Set the stream state for stream |stream_idx|.
90  void SetStreamState(bool send_stream, int stream_idx);
91
92  uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
93
94  EncodedImageCallback* encoded_complete_callback_;
95  VideoCodec codec_;
96  bool inited_;
97  int64_t timestamp_;
98  bool feedback_mode_;
99  int qp_max_;
100  int cpu_speed_default_;
101  uint32_t rc_max_intra_target_;
102  int token_partitions_;
103  ReferencePictureSelection rps_;
104  std::vector<TemporalLayers*> temporal_layers_;
105  bool down_scale_requested_;
106  uint32_t down_scale_bitrate_;
107  FrameDropper tl0_frame_dropper_;
108  FrameDropper tl1_frame_dropper_;
109  std::vector<uint16_t> picture_id_;
110  std::vector<int> last_key_frame_picture_id_;
111  std::vector<bool> key_frame_request_;
112  std::vector<bool> send_stream_;
113  std::vector<int> cpu_speed_;
114  std::vector<vpx_image_t> raw_images_;
115  std::vector<EncodedImage> encoded_images_;
116  std::vector<vpx_codec_ctx_t> encoders_;
117  std::vector<vpx_codec_enc_cfg_t> configurations_;
118  std::vector<vpx_rational_t> downsampling_factors_;
119  QualityScaler quality_scaler_;
120  bool quality_scaler_enabled_;
121};  // end of VP8EncoderImpl class
122
123class VP8DecoderImpl : public VP8Decoder {
124 public:
125  VP8DecoderImpl();
126
127  virtual ~VP8DecoderImpl();
128
129  int InitDecode(const VideoCodec* inst, int number_of_cores) override;
130
131  int Decode(const EncodedImage& input_image,
132             bool missing_frames,
133             const RTPFragmentationHeader* fragmentation,
134             const CodecSpecificInfo* codec_specific_info,
135             int64_t /*render_time_ms*/) override;
136
137  int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override;
138  int Release() override;
139  int Reset() override;
140
141  const char* ImplementationName() const override;
142
143 private:
144  // Copy reference image from this _decoder to the _decoder in copyTo. Set
145  // which frame type to copy in _refFrame->frame_type before the call to
146  // this function.
147  int CopyReference(VP8DecoderImpl* copy);
148
149  int DecodePartitions(const EncodedImage& input_image,
150                       const RTPFragmentationHeader* fragmentation);
151
152  int ReturnFrame(const vpx_image_t* img,
153                  uint32_t timeStamp,
154                  int64_t ntp_time_ms);
155
156  I420BufferPool buffer_pool_;
157  DecodedImageCallback* decode_complete_callback_;
158  bool inited_;
159  bool feedback_mode_;
160  vpx_codec_ctx_t* decoder_;
161  VideoCodec codec_;
162  EncodedImage last_keyframe_;
163  int image_format_;
164  vpx_ref_frame_t* ref_frame_;
165  int propagation_cnt_;
166  int last_frame_width_;
167  int last_frame_height_;
168  bool key_frame_required_;
169};  // end of VP8DecoderImpl class
170}  // namespace webrtc
171
172#endif  // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_VP8_IMPL_H_
173