Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 1 | /* |
| 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 | #ifndef API_VIDEO_ENCODED_IMAGE_H_ |
| 12 | #define API_VIDEO_ENCODED_IMAGE_H_ |
| 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Sergey Silkin | 2799e63 | 2019-05-17 09:51:39 +0200 | [diff] [blame] | 16 | #include <map> |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 17 | #include <utility> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 20 | #include "api/rtp_packet_infos.h" |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 21 | #include "api/scoped_refptr.h" |
Johannes Kron | 4749e4e | 2018-11-21 10:18:18 +0100 | [diff] [blame] | 22 | #include "api/video/color_space.h" |
Niels Möller | 4a2d57a | 2019-02-15 09:33:37 +0100 | [diff] [blame] | 23 | #include "api/video/video_codec_constants.h" |
Niels Möller | 22b70ff | 2018-11-20 11:06:58 +0100 | [diff] [blame] | 24 | #include "api/video/video_codec_type.h" |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 25 | #include "api/video/video_content_type.h" |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 26 | #include "api/video/video_frame_type.h" |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 27 | #include "api/video/video_rotation.h" |
| 28 | #include "api/video/video_timing.h" |
| 29 | #include "common_types.h" // NOLINT(build/include) |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 30 | #include "rtc_base/checks.h" |
Niels Möller | b9bfe65 | 2019-10-03 08:43:53 +0200 | [diff] [blame] | 31 | #include "rtc_base/deprecation.h" |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 32 | #include "rtc_base/ref_count.h" |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 33 | #include "rtc_base/system/rtc_export.h" |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 34 | |
| 35 | namespace webrtc { |
| 36 | |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 37 | // Abstract interface for buffer storage. Intended to support buffers owned by |
| 38 | // external encoders with special release requirements, e.g, java encoders with |
| 39 | // releaseOutputBuffer. |
| 40 | class EncodedImageBufferInterface : public rtc::RefCountInterface { |
| 41 | public: |
| 42 | virtual const uint8_t* data() const = 0; |
| 43 | // TODO(bugs.webrtc.org/9378): Make interface essentially read-only, delete |
| 44 | // this non-const data method. |
| 45 | virtual uint8_t* data() = 0; |
| 46 | virtual size_t size() const = 0; |
Mirko Bonadei | 3f0d8e4 | 2019-10-15 07:03:54 +0000 | [diff] [blame] | 47 | // TODO(bugs.webrtc.org/9378): Delete from this interface, together with |
| 48 | // EncodedImage::Allocate. Implemented properly only by the below concrete |
| 49 | // class |
| 50 | virtual void Realloc(size_t size) { RTC_NOTREACHED(); } |
| 51 | // Will be implemented by RefCountedObject, which also implements |
| 52 | // |rtc::RefCountInterface|. |
| 53 | virtual bool HasOneRef() const = 0; |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | // Basic implementation of EncodedImageBufferInterface. |
| 57 | class EncodedImageBuffer : public EncodedImageBufferInterface { |
| 58 | public: |
Niels Möller | b9bfe65 | 2019-10-03 08:43:53 +0200 | [diff] [blame] | 59 | static rtc::scoped_refptr<EncodedImageBuffer> Create() { return Create(0); } |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 60 | static rtc::scoped_refptr<EncodedImageBuffer> Create(size_t size); |
| 61 | static rtc::scoped_refptr<EncodedImageBuffer> Create(const uint8_t* data, |
| 62 | size_t size); |
| 63 | |
| 64 | const uint8_t* data() const override; |
| 65 | uint8_t* data() override; |
| 66 | size_t size() const override; |
Mirko Bonadei | 3f0d8e4 | 2019-10-15 07:03:54 +0000 | [diff] [blame] | 67 | void Realloc(size_t t) override; |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 68 | |
| 69 | protected: |
| 70 | explicit EncodedImageBuffer(size_t size); |
| 71 | EncodedImageBuffer(const uint8_t* data, size_t size); |
| 72 | ~EncodedImageBuffer(); |
| 73 | |
| 74 | size_t size_; |
| 75 | uint8_t* buffer_; |
| 76 | }; |
| 77 | |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 78 | // TODO(bug.webrtc.org/9378): This is a legacy api class, which is slowly being |
| 79 | // cleaned up. Direct use of its members is strongly discouraged. |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 80 | class RTC_EXPORT EncodedImage { |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 81 | public: |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 82 | EncodedImage(); |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 83 | EncodedImage(EncodedImage&&); |
| 84 | // Discouraged: potentially expensive. |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 85 | EncodedImage(const EncodedImage&); |
Niels Möller | a6fd5e4 | 2018-12-10 09:07:28 +0100 | [diff] [blame] | 86 | EncodedImage(uint8_t* buffer, size_t length, size_t capacity); |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 87 | |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 88 | ~EncodedImage(); |
| 89 | |
| 90 | EncodedImage& operator=(EncodedImage&&); |
| 91 | // Discouraged: potentially expensive. |
| 92 | EncodedImage& operator=(const EncodedImage&); |
| 93 | |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 94 | // TODO(nisse): Change style to timestamp(), set_timestamp(), for consistency |
| 95 | // with the VideoFrame class. |
| 96 | // Set frame timestamp (90kHz). |
| 97 | void SetTimestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; } |
| 98 | |
| 99 | // Get frame timestamp (90kHz). |
| 100 | uint32_t Timestamp() const { return timestamp_rtp_; } |
| 101 | |
| 102 | void SetEncodeTime(int64_t encode_start_ms, int64_t encode_finish_ms); |
| 103 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 104 | absl::optional<int> SpatialIndex() const { return spatial_index_; } |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 105 | void SetSpatialIndex(absl::optional<int> spatial_index) { |
| 106 | RTC_DCHECK_GE(spatial_index.value_or(0), 0); |
| 107 | RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers); |
Johannes Kron | 9973fa8 | 2018-11-07 14:39:26 +0100 | [diff] [blame] | 108 | spatial_index_ = spatial_index; |
| 109 | } |
| 110 | |
Sergey Silkin | 2799e63 | 2019-05-17 09:51:39 +0200 | [diff] [blame] | 111 | // These methods can be used to set/get size of subframe with spatial index |
| 112 | // |spatial_index| on encoded frames that consist of multiple spatial layers. |
| 113 | absl::optional<size_t> SpatialLayerFrameSize(int spatial_index) const; |
| 114 | void SetSpatialLayerFrameSize(int spatial_index, size_t size_bytes); |
| 115 | |
Johannes Kron | 4749e4e | 2018-11-21 10:18:18 +0100 | [diff] [blame] | 116 | const webrtc::ColorSpace* ColorSpace() const { |
| 117 | return color_space_ ? &*color_space_ : nullptr; |
Johannes Kron | 9973fa8 | 2018-11-07 14:39:26 +0100 | [diff] [blame] | 118 | } |
Danil Chapovalov | b769894 | 2019-02-05 11:32:19 +0100 | [diff] [blame] | 119 | void SetColorSpace(const absl::optional<webrtc::ColorSpace>& color_space) { |
| 120 | color_space_ = color_space; |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 123 | const RtpPacketInfos& PacketInfos() const { return packet_infos_; } |
| 124 | void SetPacketInfos(RtpPacketInfos packet_infos) { |
| 125 | packet_infos_ = std::move(packet_infos); |
| 126 | } |
| 127 | |
Elad Alon | b64af4b | 2019-06-05 11:39:37 +0200 | [diff] [blame] | 128 | bool RetransmissionAllowed() const { return retransmission_allowed_; } |
| 129 | void SetRetransmissionAllowed(bool retransmission_allowed) { |
| 130 | retransmission_allowed_ = retransmission_allowed; |
| 131 | } |
| 132 | |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 133 | size_t size() const { return size_; } |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 134 | void set_size(size_t new_size) { |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 135 | // Allow set_size(0) even if we have no buffer. |
| 136 | RTC_DCHECK_LE(new_size, new_size == 0 ? 0 : capacity()); |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 137 | size_ = new_size; |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 138 | } |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 139 | // TODO(nisse): Delete, provide only read-only access to the buffer. |
| 140 | size_t capacity() const { |
| 141 | return buffer_ ? capacity_ : (encoded_data_ ? encoded_data_->size() : 0); |
| 142 | } |
Niels Möller | f0eee00 | 2018-11-28 16:31:29 +0100 | [diff] [blame] | 143 | |
Niels Möller | 77894cc | 2018-12-04 15:38:15 +0100 | [diff] [blame] | 144 | void set_buffer(uint8_t* buffer, size_t capacity) { |
Niels Möller | 24871e4 | 2019-01-17 11:31:13 +0100 | [diff] [blame] | 145 | buffer_ = buffer; |
Niels Möller | a6fd5e4 | 2018-12-10 09:07:28 +0100 | [diff] [blame] | 146 | capacity_ = capacity; |
Niels Möller | 77894cc | 2018-12-04 15:38:15 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Mirko Bonadei | 3f0d8e4 | 2019-10-15 07:03:54 +0000 | [diff] [blame] | 149 | // TODO(bugs.webrtc.org/9378): Delete; this method implies realloc, which |
| 150 | // should not be generally supported by the EncodedImageBufferInterface. |
| 151 | RTC_DEPRECATED |
| 152 | void Allocate(size_t capacity); |
| 153 | |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 154 | void SetEncodedData( |
| 155 | rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data) { |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 156 | encoded_data_ = encoded_data; |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 157 | size_ = encoded_data->size(); |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 158 | buffer_ = nullptr; |
| 159 | } |
| 160 | |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 161 | // TODO(nisse): Delete, provide only read-only access to the buffer. |
| 162 | uint8_t* data() { |
| 163 | return buffer_ ? buffer_ |
| 164 | : (encoded_data_ ? encoded_data_->data() : nullptr); |
| 165 | } |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 166 | const uint8_t* data() const { |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 167 | return buffer_ ? buffer_ |
| 168 | : (encoded_data_ ? encoded_data_->data() : nullptr); |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 169 | } |
| 170 | // TODO(nisse): At some places, code accepts a const ref EncodedImage, but |
| 171 | // still writes to it, to clear padding at the end of the encoded data. |
| 172 | // Padding is required by ffmpeg; the best way to deal with that is likely to |
| 173 | // make this class ensure that buffers always have a few zero padding bytes. |
| 174 | uint8_t* mutable_data() const { return const_cast<uint8_t*>(data()); } |
| 175 | |
| 176 | // TODO(bugs.webrtc.org/9378): Delete. Used by code that wants to modify a |
| 177 | // buffer corresponding to a const EncodedImage. Requires an un-owned buffer. |
| 178 | uint8_t* buffer() const { return buffer_; } |
| 179 | |
| 180 | // Hack to workaround lack of ownership of the encoded data. If we don't |
| 181 | // already own the underlying data, make an owned copy. |
| 182 | void Retain(); |
Niels Möller | a8f58f0 | 2019-01-10 15:12:49 +0100 | [diff] [blame] | 183 | |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 184 | uint32_t _encodedWidth = 0; |
| 185 | uint32_t _encodedHeight = 0; |
| 186 | // NTP time of the capture time in local timebase in milliseconds. |
| 187 | int64_t ntp_time_ms_ = 0; |
| 188 | int64_t capture_time_ms_ = 0; |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 189 | VideoFrameType _frameType = VideoFrameType::kVideoFrameDelta; |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 190 | VideoRotation rotation_ = kVideoRotation_0; |
| 191 | VideoContentType content_type_ = VideoContentType::UNSPECIFIED; |
| 192 | bool _completeFrame = false; |
| 193 | int qp_ = -1; // Quantizer value. |
| 194 | |
| 195 | // When an application indicates non-zero values here, it is taken as an |
| 196 | // indication that all future frames will be constrained with those limits |
| 197 | // until the application indicates a change again. |
| 198 | PlayoutDelay playout_delay_ = {-1, -1}; |
| 199 | |
| 200 | struct Timing { |
| 201 | uint8_t flags = VideoSendTiming::kInvalid; |
| 202 | int64_t encode_start_ms = 0; |
| 203 | int64_t encode_finish_ms = 0; |
| 204 | int64_t packetization_finish_ms = 0; |
| 205 | int64_t pacer_exit_ms = 0; |
| 206 | int64_t network_timestamp_ms = 0; |
| 207 | int64_t network2_timestamp_ms = 0; |
| 208 | int64_t receive_start_ms = 0; |
| 209 | int64_t receive_finish_ms = 0; |
| 210 | } timing_; |
| 211 | |
| 212 | private: |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 213 | // TODO(bugs.webrtc.org/9378): We're transitioning to always owning the |
| 214 | // encoded data. |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 215 | rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data_; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 216 | size_t size_; // Size of encoded frame data. |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 217 | // Non-null when used with an un-owned buffer. |
| 218 | uint8_t* buffer_; |
| 219 | // Allocated size of _buffer; relevant only if it's non-null. |
| 220 | size_t capacity_; |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 221 | uint32_t timestamp_rtp_ = 0; |
Johannes Kron | 9973fa8 | 2018-11-07 14:39:26 +0100 | [diff] [blame] | 222 | absl::optional<int> spatial_index_; |
Sergey Silkin | 2799e63 | 2019-05-17 09:51:39 +0200 | [diff] [blame] | 223 | std::map<int, size_t> spatial_layer_frame_size_bytes_; |
Johannes Kron | 4749e4e | 2018-11-21 10:18:18 +0100 | [diff] [blame] | 224 | absl::optional<webrtc::ColorSpace> color_space_; |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 225 | // Information about packets used to assemble this video frame. This is needed |
| 226 | // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's |
| 227 | // MediaStreamTrack, in order to implement getContributingSources(). See: |
| 228 | // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources |
| 229 | RtpPacketInfos packet_infos_; |
Elad Alon | b64af4b | 2019-06-05 11:39:37 +0200 | [diff] [blame] | 230 | bool retransmission_allowed_ = true; |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | } // namespace webrtc |
| 234 | |
| 235 | #endif // API_VIDEO_ENCODED_IMAGE_H_ |