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_IMPL_H_
14#define WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_IMPL_H_
15
16#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
17#include "webrtc/modules/video_coding/utility/quality_scaler.h"
18
19// VPX forward declaration
20typedef struct vpx_codec_ctx vpx_codec_ctx_t;
21typedef struct vpx_codec_ctx vpx_dec_ctx_t;
22typedef struct vpx_codec_enc_cfg vpx_codec_enc_cfg_t;
23typedef struct vpx_image vpx_image_t;
24typedef struct vpx_ref_frame vpx_ref_frame_t;
25struct vpx_codec_cx_pkt;
26
27namespace webrtc {
28
29class TemporalLayers;
30class ReferencePictureSelection;
31
32class VP8EncoderImpl : public VP8Encoder {
33 public:
34  VP8EncoderImpl();
35
36  virtual ~VP8EncoderImpl();
37
38  // Free encoder memory.
39  //
40  // Return value                : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
41  virtual int Release();
42
43  // Initialize the encoder with the information from the codecSettings
44  //
45  // Input:
46  //          - codec_settings    : Codec settings
47  //          - number_of_cores   : Number of cores available for the encoder
48  //          - max_payload_size  : The maximum size each payload is allowed
49  //                                to have. Usually MTU - overhead.
50  //
51  // Return value                 : Set bit rate if OK
52  //                                <0 - Errors:
53  //                                  WEBRTC_VIDEO_CODEC_ERR_PARAMETER
54  //                                  WEBRTC_VIDEO_CODEC_ERR_SIZE
55  //                                  WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
56  //                                  WEBRTC_VIDEO_CODEC_MEMORY
57  //                                  WEBRTC_VIDEO_CODEC_ERROR
58  virtual int InitEncode(const VideoCodec* codec_settings,
59                         int number_of_cores,
60                         uint32_t max_payload_size);
61
62  // Encode an I420 image (as a part of a video stream). The encoded image
63  // will be returned to the user through the encode complete callback.
64  //
65  // Input:
66  //          - input_image       : Image to be encoded
67  //          - frame_types       : Frame type to be generated by the encoder.
68  //
69  // Return value                 : WEBRTC_VIDEO_CODEC_OK if OK
70  //                                <0 - Errors:
71  //                                  WEBRTC_VIDEO_CODEC_ERR_PARAMETER
72  //                                  WEBRTC_VIDEO_CODEC_MEMORY
73  //                                  WEBRTC_VIDEO_CODEC_ERROR
74  //                                  WEBRTC_VIDEO_CODEC_TIMEOUT
75
76  virtual int Encode(const I420VideoFrame& input_image,
77                     const CodecSpecificInfo* codec_specific_info,
78                     const std::vector<VideoFrameType>* frame_types);
79
80  // Register an encode complete callback object.
81  //
82  // Input:
83  //          - callback         : Callback object which handles encoded images.
84  //
85  // Return value                : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
86  virtual int RegisterEncodeCompleteCallback(EncodedImageCallback* callback);
87
88  // Inform the encoder of the new packet loss rate and the round-trip time of
89  // the network.
90  //
91  //          - packet_loss : Fraction lost
92  //                          (loss rate in percent = 100 * packetLoss / 255)
93  //          - rtt         : Round-trip time in milliseconds
94  // Return value           : WEBRTC_VIDEO_CODEC_OK if OK
95  //                          <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
96  //
97  virtual int SetChannelParameters(uint32_t packet_loss, int rtt);
98
99  // Inform the encoder about the new target bit rate.
100  //
101  //          - new_bitrate_kbit : New target bit rate
102  //          - frame_rate       : The target frame rate
103  //
104  // Return value                : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
105  virtual int SetRates(uint32_t new_bitrate_kbit, uint32_t frame_rate);
106
107 private:
108  // Call encoder initialize function and set control settings.
109  int InitAndSetControlSettings(const VideoCodec* inst);
110
111  // Update frame size for codec.
112  int UpdateCodecFrameSize(const I420VideoFrame& input_image);
113
114  void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
115                             const vpx_codec_cx_pkt& pkt,
116                             uint32_t timestamp);
117
118  int GetEncodedPartitions(const I420VideoFrame& input_image);
119
120  // Determine maximum target for Intra frames
121  //
122  // Input:
123  //    - optimal_buffer_size : Optimal buffer size
124  // Return Value             : Max target size for Intra frames represented as
125  //                            percentage of the per frame bandwidth
126  uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
127
128  EncodedImage encoded_image_;
129  EncodedImageCallback* encoded_complete_callback_;
130  VideoCodec codec_;
131  bool inited_;
132  int64_t timestamp_;
133  uint16_t picture_id_;
134  bool feedback_mode_;
135  int cpu_speed_;
136  uint32_t rc_max_intra_target_;
137  int token_partitions_;
138  ReferencePictureSelection* rps_;
139  TemporalLayers* temporal_layers_;
140  vpx_codec_ctx_t* encoder_;
141  vpx_codec_enc_cfg_t* config_;
142  vpx_image_t* raw_;
143  QualityScaler quality_scaler_;
144};  // end of VP8Encoder class
145
146
147class VP8DecoderImpl : public VP8Decoder {
148 public:
149  VP8DecoderImpl();
150
151  virtual ~VP8DecoderImpl();
152
153  // Initialize the decoder.
154  //
155  // Return value         :  WEBRTC_VIDEO_CODEC_OK.
156  //                        <0 - Errors:
157  //                                  WEBRTC_VIDEO_CODEC_ERROR
158  virtual int InitDecode(const VideoCodec* inst, int number_of_cores);
159
160  // Decode encoded image (as a part of a video stream). The decoded image
161  // will be returned to the user through the decode complete callback.
162  //
163  // Input:
164  //          - input_image         : Encoded image to be decoded
165  //          - missing_frames      : True if one or more frames have been lost
166  //                                  since the previous decode call.
167  //          - fragmentation       : Specifies the start and length of each VP8
168  //                                  partition.
169  //          - codec_specific_info : pointer to specific codec data
170  //          - render_time_ms      : Render time in Ms
171  //
172  // Return value                 : WEBRTC_VIDEO_CODEC_OK if OK
173  //                                <0 - Errors:
174  //                                      WEBRTC_VIDEO_CODEC_ERROR
175  //                                      WEBRTC_VIDEO_CODEC_ERR_PARAMETER
176  virtual int Decode(const EncodedImage& input_image,
177                     bool missing_frames,
178                     const RTPFragmentationHeader* fragmentation,
179                     const CodecSpecificInfo* codec_specific_info,
180                     int64_t /*render_time_ms*/);
181
182  // Register a decode complete callback object.
183  //
184  // Input:
185  //          - callback         : Callback object which handles decoded images.
186  //
187  // Return value                : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
188  virtual int RegisterDecodeCompleteCallback(DecodedImageCallback* callback);
189
190  // Free decoder memory.
191  //
192  // Return value                : WEBRTC_VIDEO_CODEC_OK if OK
193  //                               <0 - Errors:
194  //                                      WEBRTC_VIDEO_CODEC_ERROR
195  virtual int Release();
196
197  // Reset decoder state and prepare for a new call.
198  //
199  // Return value         : WEBRTC_VIDEO_CODEC_OK.
200  //                        <0 - Errors:
201  //                                  WEBRTC_VIDEO_CODEC_UNINITIALIZED
202  //                                  WEBRTC_VIDEO_CODEC_ERROR
203  virtual int Reset();
204
205  // Create a copy of the codec and its internal state.
206  //
207  // Return value                : A copy of the instance if OK, NULL otherwise.
208  virtual VideoDecoder* Copy();
209
210 private:
211  // Copy reference image from this _decoder to the _decoder in copyTo. Set
212  // which frame type to copy in _refFrame->frame_type before the call to
213  // this function.
214  int CopyReference(VP8Decoder* copy);
215
216  int DecodePartitions(const EncodedImage& input_image,
217                       const RTPFragmentationHeader* fragmentation);
218
219  int ReturnFrame(const vpx_image_t* img,
220                  uint32_t timeStamp,
221                  int64_t ntp_time_ms);
222
223  I420VideoFrame decoded_image_;
224  DecodedImageCallback* decode_complete_callback_;
225  bool inited_;
226  bool feedback_mode_;
227  vpx_dec_ctx_t* decoder_;
228  VideoCodec codec_;
229  EncodedImage last_keyframe_;
230  int image_format_;
231  vpx_ref_frame_t* ref_frame_;
232  int propagation_cnt_;
233  bool mfqe_enabled_;
234  bool key_frame_required_;
235};  // end of VP8Decoder class
236}  // namespace webrtc
237
238#endif  // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_IMPL_H_
239