blob: 509f04eedff8e9e29a85562ef57c7b0e90c49a16 [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
27class VP9EncoderImpl : public VP9Encoder {
28 public:
29 VP9EncoderImpl();
30
31 virtual ~VP9EncoderImpl();
32
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000033 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000034
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000035 int InitEncode(const VideoCodec* codec_settings,
36 int number_of_cores,
37 size_t max_payload_size) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000038
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070039 int Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000040 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -070041 const std::vector<FrameType>* frame_types) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000042
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000043 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000044
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000045 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000046
Erik Språng566124a2018-04-23 12:32:22 +020047 int SetRateAllocation(const VideoBitrateAllocation& bitrate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +010048 uint32_t frame_rate) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000049
Peter Boströmb7d9a972015-12-18 16:01:11 +010050 const char* ImplementationName() const override;
51
marpan@webrtc.org5b883172014-11-01 06:10:48 +000052 private:
marpan@webrtc.org38d11b82015-01-26 15:21:36 +000053 // Determine number of encoder threads to use.
54 int NumberOfThreads(int width, int height, int number_of_cores);
55
marpan@webrtc.org5b883172014-11-01 06:10:48 +000056 // Call encoder initialize function and set control settings.
57 int InitAndSetControlSettings(const VideoCodec* inst);
58
59 void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
60 const vpx_codec_cx_pkt& pkt,
Sergey Silkin07f80cc2018-04-09 13:11:59 +020061 uint32_t timestamp,
62 bool first_frame_in_picture);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000063
sprangce4aef12015-11-02 07:23:20 -080064 bool ExplicitlyConfiguredSpatialLayers() const;
Erik Språng566124a2018-04-23 12:32:22 +020065 bool SetSvcRates(const VideoBitrateAllocation& bitrate_allocation);
asaperssona9455ab2015-07-31 06:10:09 -070066
67 virtual int GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt);
68
69 // Callback function for outputting packets per spatial layer.
70 static void EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
71 void* user_data);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000072
Sergey Silkinbc0f0d32018-04-24 21:29:14 +020073 void DeliverBufferedFrame(bool end_of_picture);
Sergey Silkin2a1f1832018-04-04 11:45:41 +020074
Sergey Silkinbe71a1e2018-05-17 16:46:43 +020075 bool DropFrame(uint32_t rtp_timestamp);
76
marpan@webrtc.org5b883172014-11-01 06:10:48 +000077 // Determine maximum target for Intra frames
78 //
79 // Input:
80 // - optimal_buffer_size : Optimal buffer size
81 // Return Value : Max target size for Intra frames represented as
82 // percentage of the per frame bandwidth
83 uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
84
85 EncodedImage encoded_image_;
Sergey Silkin2a1f1832018-04-04 11:45:41 +020086 CodecSpecificInfo codec_specific_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000087 EncodedImageCallback* encoded_complete_callback_;
88 VideoCodec codec_;
89 bool inited_;
90 int64_t timestamp_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000091 int cpu_speed_;
92 uint32_t rc_max_intra_target_;
93 vpx_codec_ctx_t* encoder_;
94 vpx_codec_enc_cfg_t* config_;
95 vpx_image_t* raw_;
johannkoenig8225c402017-01-26 13:23:44 -080096 vpx_svc_extra_cfg_t svc_params_;
asaperssona9455ab2015-07-31 06:10:09 -070097 const VideoFrame* input_image_;
98 GofInfoVP9 gof_; // Contains each frame's temporal information for
99 // non-flexible mode.
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200100 bool force_key_frame_;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200101 size_t pics_since_key_;
asaperssona9455ab2015-07-31 06:10:09 -0700102 uint8_t num_temporal_layers_;
103 uint8_t num_spatial_layers_;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200104 InterLayerPredMode inter_layer_pred_;
philipelcfc319b2015-11-10 07:17:23 -0800105
106 // Used for flexible mode.
107 bool is_flexible_mode_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000108};
109
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000110class VP9DecoderImpl : public VP9Decoder {
111 public:
112 VP9DecoderImpl();
113
114 virtual ~VP9DecoderImpl();
115
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000116 int InitDecode(const VideoCodec* inst, int number_of_cores) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000117
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000118 int Decode(const EncodedImage& input_image,
119 bool missing_frames,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000120 const CodecSpecificInfo* codec_specific_info,
121 int64_t /*render_time_ms*/) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000122
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000123 int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000124
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000125 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000126
Peter Boströmb7d9a972015-12-18 16:01:11 +0100127 const char* ImplementationName() const override;
128
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000129 private:
asapersson1490f7a2016-09-23 02:09:46 -0700130 int ReturnFrame(const vpx_image_t* img,
131 uint32_t timestamp,
sakal7adadb12017-02-23 02:54:57 -0800132 int64_t ntp_time_ms,
133 int qp);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000134
Henrik Boström9695d852015-05-06 10:42:15 +0200135 // Memory pool used to share buffers between libvpx and webrtc.
136 Vp9FrameBufferPool frame_buffer_pool_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000137 DecodedImageCallback* decode_complete_callback_;
138 bool inited_;
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000139 vpx_codec_ctx_t* decoder_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000140 bool key_frame_required_;
141};
142} // namespace webrtc
143
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200144#endif // MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_