blob: d7c9250ecafdf4c5ddd0d777d420d2acd7cb1af7 [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"
Sergey Silkind902d582018-05-18 17:31:19 +020020#include "rtc_base/rate_statistics.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000021
johannkoenig8225c402017-01-26 13:23:44 -080022#include "vpx/vp8cx.h"
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000023#include "vpx/vpx_decoder.h"
24#include "vpx/vpx_encoder.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000025
26namespace webrtc {
27
28class VP9EncoderImpl : public VP9Encoder {
29 public:
30 VP9EncoderImpl();
31
32 virtual ~VP9EncoderImpl();
33
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000034 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000035
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000036 int InitEncode(const VideoCodec* codec_settings,
37 int number_of_cores,
38 size_t max_payload_size) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000039
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070040 int Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000041 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -070042 const std::vector<FrameType>* frame_types) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000043
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000044 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000045
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000046 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000047
Erik Språng566124a2018-04-23 12:32:22 +020048 int SetRateAllocation(const VideoBitrateAllocation& bitrate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +010049 uint32_t frame_rate) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000050
Peter Boströmb7d9a972015-12-18 16:01:11 +010051 const char* ImplementationName() const override;
52
marpan@webrtc.org5b883172014-11-01 06:10:48 +000053 private:
marpan@webrtc.org38d11b82015-01-26 15:21:36 +000054 // Determine number of encoder threads to use.
55 int NumberOfThreads(int width, int height, int number_of_cores);
56
marpan@webrtc.org5b883172014-11-01 06:10:48 +000057 // Call encoder initialize function and set control settings.
58 int InitAndSetControlSettings(const VideoCodec* inst);
59
60 void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
61 const vpx_codec_cx_pkt& pkt,
Sergey Silkin07f80cc2018-04-09 13:11:59 +020062 uint32_t timestamp,
63 bool first_frame_in_picture);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000064
sprangce4aef12015-11-02 07:23:20 -080065 bool ExplicitlyConfiguredSpatialLayers() const;
Erik Språng566124a2018-04-23 12:32:22 +020066 bool SetSvcRates(const VideoBitrateAllocation& bitrate_allocation);
asaperssona9455ab2015-07-31 06:10:09 -070067
68 virtual int GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt);
69
70 // Callback function for outputting packets per spatial layer.
71 static void EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
72 void* user_data);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000073
Sergey Silkinbc0f0d32018-04-24 21:29:14 +020074 void DeliverBufferedFrame(bool end_of_picture);
Sergey Silkin2a1f1832018-04-04 11:45:41 +020075
Sergey Silkinbe71a1e2018-05-17 16:46:43 +020076 bool DropFrame(uint32_t rtp_timestamp);
77
marpan@webrtc.org5b883172014-11-01 06:10:48 +000078 // Determine maximum target for Intra frames
79 //
80 // Input:
81 // - optimal_buffer_size : Optimal buffer size
82 // Return Value : Max target size for Intra frames represented as
83 // percentage of the per frame bandwidth
84 uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
85
86 EncodedImage encoded_image_;
Sergey Silkin2a1f1832018-04-04 11:45:41 +020087 CodecSpecificInfo codec_specific_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000088 EncodedImageCallback* encoded_complete_callback_;
89 VideoCodec codec_;
90 bool inited_;
91 int64_t timestamp_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000092 int cpu_speed_;
93 uint32_t rc_max_intra_target_;
94 vpx_codec_ctx_t* encoder_;
95 vpx_codec_enc_cfg_t* config_;
96 vpx_image_t* raw_;
johannkoenig8225c402017-01-26 13:23:44 -080097 vpx_svc_extra_cfg_t svc_params_;
asaperssona9455ab2015-07-31 06:10:09 -070098 const VideoFrame* input_image_;
99 GofInfoVP9 gof_; // Contains each frame's temporal information for
100 // non-flexible mode.
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200101 bool force_key_frame_;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200102 size_t pics_since_key_;
asaperssona9455ab2015-07-31 06:10:09 -0700103 uint8_t num_temporal_layers_;
104 uint8_t num_spatial_layers_;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200105 InterLayerPredMode inter_layer_pred_;
philipelcfc319b2015-11-10 07:17:23 -0800106
Sergey Silkind902d582018-05-18 17:31:19 +0200107 // Framerate controller.
108 rtc::Optional<float> target_framerate_fps_;
109 RateStatistics output_framerate_;
110 uint32_t last_encoded_frame_rtp_timestamp_;
111
philipelcfc319b2015-11-10 07:17:23 -0800112 // Used for flexible mode.
113 bool is_flexible_mode_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000114};
115
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000116class VP9DecoderImpl : public VP9Decoder {
117 public:
118 VP9DecoderImpl();
119
120 virtual ~VP9DecoderImpl();
121
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000122 int InitDecode(const VideoCodec* inst, int number_of_cores) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000123
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000124 int Decode(const EncodedImage& input_image,
125 bool missing_frames,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000126 const CodecSpecificInfo* codec_specific_info,
127 int64_t /*render_time_ms*/) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000128
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000129 int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000130
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000131 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000132
Peter Boströmb7d9a972015-12-18 16:01:11 +0100133 const char* ImplementationName() const override;
134
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000135 private:
asapersson1490f7a2016-09-23 02:09:46 -0700136 int ReturnFrame(const vpx_image_t* img,
137 uint32_t timestamp,
sakal7adadb12017-02-23 02:54:57 -0800138 int64_t ntp_time_ms,
139 int qp);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000140
Henrik Boström9695d852015-05-06 10:42:15 +0200141 // Memory pool used to share buffers between libvpx and webrtc.
142 Vp9FrameBufferPool frame_buffer_pool_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000143 DecodedImageCallback* decode_complete_callback_;
144 bool inited_;
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000145 vpx_codec_ctx_t* decoder_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000146 bool key_frame_required_;
147};
148} // namespace webrtc
149
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200150#endif // MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_