blob: cdc8a831690a6faae888a9c0522203aa87d226f7 [file] [log] [blame]
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001/*
2 * Copyright (c) 2014 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 */
11
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#ifndef MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_
13#define MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_
philipelcce46fc2015-12-21 03:04:49 -080014
kwiberg3f55dea2016-02-29 05:51:59 -080015#include <memory>
philipelcce46fc2015-12-21 03:04:49 -080016#include <vector>
marpan@webrtc.org5b883172014-11-01 06:10:48 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/codecs/vp9/include/vp9.h"
19#include "modules/video_coding/codecs/vp9/vp9_frame_buffer_pool.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000020
johannkoenig8225c402017-01-26 13:23:44 -080021#include "vpx/vp8cx.h"
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000022#include "vpx/vpx_decoder.h"
23#include "vpx/vpx_encoder.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000024
25namespace webrtc {
26
philipelcfc319b2015-11-10 07:17:23 -080027class ScreenshareLayersVP9;
28
marpan@webrtc.org5b883172014-11-01 06:10:48 +000029class VP9EncoderImpl : public VP9Encoder {
30 public:
31 VP9EncoderImpl();
32
33 virtual ~VP9EncoderImpl();
34
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000035 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000036
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000037 int InitEncode(const VideoCodec* codec_settings,
38 int number_of_cores,
39 size_t max_payload_size) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000040
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070041 int Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000042 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -070043 const std::vector<FrameType>* frame_types) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000044
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000045 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000046
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000047 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000048
Erik Språng08127a92016-11-16 16:41:30 +010049 int SetRateAllocation(const BitrateAllocation& bitrate_allocation,
50 uint32_t frame_rate) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000051
Peter Boströmb7d9a972015-12-18 16:01:11 +010052 const char* ImplementationName() const override;
53
philipelcfc319b2015-11-10 07:17:23 -080054 struct LayerFrameRefSettings {
55 int8_t upd_buf = -1; // -1 - no update, 0..7 - update buffer 0..7
56 int8_t ref_buf1 = -1; // -1 - no reference, 0..7 - reference buffer 0..7
57 int8_t ref_buf2 = -1; // -1 - no reference, 0..7 - reference buffer 0..7
58 int8_t ref_buf3 = -1; // -1 - no reference, 0..7 - reference buffer 0..7
59 };
60
61 struct SuperFrameRefSettings {
62 LayerFrameRefSettings layer[kMaxVp9NumberOfSpatialLayers];
63 uint8_t start_layer = 0; // The first spatial layer to be encoded.
64 uint8_t stop_layer = 0; // The last spatial layer to be encoded.
65 bool is_keyframe = false;
66 };
67
marpan@webrtc.org5b883172014-11-01 06:10:48 +000068 private:
marpan@webrtc.org38d11b82015-01-26 15:21:36 +000069 // Determine number of encoder threads to use.
70 int NumberOfThreads(int width, int height, int number_of_cores);
71
marpan@webrtc.org5b883172014-11-01 06:10:48 +000072 // Call encoder initialize function and set control settings.
73 int InitAndSetControlSettings(const VideoCodec* inst);
74
75 void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
76 const vpx_codec_cx_pkt& pkt,
77 uint32_t timestamp);
78
sprangce4aef12015-11-02 07:23:20 -080079 bool ExplicitlyConfiguredSpatialLayers() const;
Sergey Silkin86684962018-03-28 19:32:37 +020080 bool SetSvcRates(const BitrateAllocation& bitrate_allocation);
asaperssona9455ab2015-07-31 06:10:09 -070081
philipelcfc319b2015-11-10 07:17:23 -080082 // Used for flexible mode to set the flags and buffer references used
83 // by the encoder. Also calculates the references used by the RTP
84 // packetizer.
85 //
86 // Has to be called for every frame (keyframes included) to update the
87 // state used to calculate references.
88 vpx_svc_ref_frame_config GenerateRefsAndFlags(
89 const SuperFrameRefSettings& settings);
90
asaperssona9455ab2015-07-31 06:10:09 -070091 virtual int GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt);
92
93 // Callback function for outputting packets per spatial layer.
94 static void EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
95 void* user_data);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000096
Sergey Silkin2a1f1832018-04-04 11:45:41 +020097 void DeliverBufferedFrame(bool end_of_superframe);
98
marpan@webrtc.org5b883172014-11-01 06:10:48 +000099 // Determine maximum target for Intra frames
100 //
101 // Input:
102 // - optimal_buffer_size : Optimal buffer size
103 // Return Value : Max target size for Intra frames represented as
104 // percentage of the per frame bandwidth
105 uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
106
107 EncodedImage encoded_image_;
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200108 CodecSpecificInfo codec_specific_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000109 EncodedImageCallback* encoded_complete_callback_;
110 VideoCodec codec_;
111 bool inited_;
112 int64_t timestamp_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000113 int cpu_speed_;
114 uint32_t rc_max_intra_target_;
115 vpx_codec_ctx_t* encoder_;
116 vpx_codec_enc_cfg_t* config_;
117 vpx_image_t* raw_;
johannkoenig8225c402017-01-26 13:23:44 -0800118 vpx_svc_extra_cfg_t svc_params_;
asaperssona9455ab2015-07-31 06:10:09 -0700119 const VideoFrame* input_image_;
120 GofInfoVP9 gof_; // Contains each frame's temporal information for
121 // non-flexible mode.
philipelcfc319b2015-11-10 07:17:23 -0800122 size_t frames_since_kf_;
asaperssona9455ab2015-07-31 06:10:09 -0700123 uint8_t num_temporal_layers_;
124 uint8_t num_spatial_layers_;
philipelcfc319b2015-11-10 07:17:23 -0800125
126 // Used for flexible mode.
127 bool is_flexible_mode_;
128 int64_t buffer_updated_at_frame_[kNumVp9Buffers];
129 int64_t frames_encoded_;
130 uint8_t num_ref_pics_[kMaxVp9NumberOfSpatialLayers];
131 uint8_t p_diff_[kMaxVp9NumberOfSpatialLayers][kMaxVp9RefPics];
kwiberg3f55dea2016-02-29 05:51:59 -0800132 std::unique_ptr<ScreenshareLayersVP9> spatial_layer_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000133};
134
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000135class VP9DecoderImpl : public VP9Decoder {
136 public:
137 VP9DecoderImpl();
138
139 virtual ~VP9DecoderImpl();
140
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000141 int InitDecode(const VideoCodec* inst, int number_of_cores) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000142
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000143 int Decode(const EncodedImage& input_image,
144 bool missing_frames,
145 const RTPFragmentationHeader* fragmentation,
146 const CodecSpecificInfo* codec_specific_info,
147 int64_t /*render_time_ms*/) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000148
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000149 int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000150
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000151 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000152
Peter Boströmb7d9a972015-12-18 16:01:11 +0100153 const char* ImplementationName() const override;
154
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000155 private:
asapersson1490f7a2016-09-23 02:09:46 -0700156 int ReturnFrame(const vpx_image_t* img,
157 uint32_t timestamp,
sakal7adadb12017-02-23 02:54:57 -0800158 int64_t ntp_time_ms,
159 int qp);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000160
Henrik Boström9695d852015-05-06 10:42:15 +0200161 // Memory pool used to share buffers between libvpx and webrtc.
162 Vp9FrameBufferPool frame_buffer_pool_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000163 DecodedImageCallback* decode_complete_callback_;
164 bool inited_;
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000165 vpx_codec_ctx_t* decoder_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000166 VideoCodec codec_;
167 bool key_frame_required_;
168};
169} // namespace webrtc
170
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200171#endif // MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_