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> |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame^] | 15 | #include <vector> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Johannes Kron | 4749e4e | 2018-11-21 10:18:18 +0100 | [diff] [blame] | 18 | #include "api/video/color_space.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 19 | #include "api/video/video_bitrate_allocation.h" |
Niels Möller | 22b70ff | 2018-11-20 11:06:58 +0100 | [diff] [blame] | 20 | #include "api/video/video_codec_type.h" |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 21 | #include "api/video/video_content_type.h" |
| 22 | #include "api/video/video_rotation.h" |
| 23 | #include "api/video/video_timing.h" |
| 24 | #include "common_types.h" // NOLINT(build/include) |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 26 | #include "rtc_base/system/rtc_export.h" |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | // TODO(bug.webrtc.org/9378): This is a legacy api class, which is slowly being |
| 31 | // cleaned up. Direct use of its members is strongly discouraged. |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 32 | class RTC_EXPORT EncodedImage { |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 33 | public: |
| 34 | static const size_t kBufferPaddingBytesH264; |
| 35 | |
| 36 | // Some decoders require encoded image buffers to be padded with a small |
| 37 | // number of additional bytes (due to over-reading byte readers). |
| 38 | static size_t GetBufferPaddingBytes(VideoCodecType codec_type); |
| 39 | |
| 40 | EncodedImage(); |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame^] | 41 | EncodedImage(EncodedImage&&); |
| 42 | // Discouraged: potentially expensive. |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 43 | EncodedImage(const EncodedImage&); |
Niels Möller | a6fd5e4 | 2018-12-10 09:07:28 +0100 | [diff] [blame] | 44 | EncodedImage(uint8_t* buffer, size_t length, size_t capacity); |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 45 | |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame^] | 46 | ~EncodedImage(); |
| 47 | |
| 48 | EncodedImage& operator=(EncodedImage&&); |
| 49 | // Discouraged: potentially expensive. |
| 50 | EncodedImage& operator=(const EncodedImage&); |
| 51 | |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 52 | // TODO(nisse): Change style to timestamp(), set_timestamp(), for consistency |
| 53 | // with the VideoFrame class. |
| 54 | // Set frame timestamp (90kHz). |
| 55 | void SetTimestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; } |
| 56 | |
| 57 | // Get frame timestamp (90kHz). |
| 58 | uint32_t Timestamp() const { return timestamp_rtp_; } |
| 59 | |
| 60 | void SetEncodeTime(int64_t encode_start_ms, int64_t encode_finish_ms); |
| 61 | |
| 62 | absl::optional<int> SpatialIndex() const { |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 63 | return spatial_index_; |
| 64 | } |
| 65 | void SetSpatialIndex(absl::optional<int> spatial_index) { |
| 66 | RTC_DCHECK_GE(spatial_index.value_or(0), 0); |
| 67 | RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers); |
Johannes Kron | 9973fa8 | 2018-11-07 14:39:26 +0100 | [diff] [blame] | 68 | spatial_index_ = spatial_index; |
| 69 | } |
| 70 | |
Johannes Kron | 4749e4e | 2018-11-21 10:18:18 +0100 | [diff] [blame] | 71 | const webrtc::ColorSpace* ColorSpace() const { |
| 72 | return color_space_ ? &*color_space_ : nullptr; |
Johannes Kron | 9973fa8 | 2018-11-07 14:39:26 +0100 | [diff] [blame] | 73 | } |
Danil Chapovalov | b769894 | 2019-02-05 11:32:19 +0100 | [diff] [blame] | 74 | void SetColorSpace(const absl::optional<webrtc::ColorSpace>& color_space) { |
| 75 | color_space_ = color_space; |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 78 | size_t size() const { return size_; } |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 79 | void set_size(size_t new_size) { |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame^] | 80 | RTC_DCHECK_LE(new_size, capacity()); |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 81 | size_ = new_size; |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 82 | } |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame^] | 83 | size_t capacity() const { return buffer_ ? capacity_ : encoded_data_.size(); } |
Niels Möller | f0eee00 | 2018-11-28 16:31:29 +0100 | [diff] [blame] | 84 | |
Niels Möller | 77894cc | 2018-12-04 15:38:15 +0100 | [diff] [blame] | 85 | void set_buffer(uint8_t* buffer, size_t capacity) { |
Niels Möller | 24871e4 | 2019-01-17 11:31:13 +0100 | [diff] [blame] | 86 | buffer_ = buffer; |
Niels Möller | a6fd5e4 | 2018-12-10 09:07:28 +0100 | [diff] [blame] | 87 | capacity_ = capacity; |
Niels Möller | 77894cc | 2018-12-04 15:38:15 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame^] | 90 | void Allocate(size_t capacity) { |
| 91 | encoded_data_.resize(capacity); |
| 92 | buffer_ = nullptr; |
| 93 | } |
| 94 | |
| 95 | uint8_t* data() { return buffer_ ? buffer_ : encoded_data_.data(); } |
| 96 | const uint8_t* data() const { |
| 97 | return buffer_ ? buffer_ : encoded_data_.data(); |
| 98 | } |
| 99 | // TODO(nisse): At some places, code accepts a const ref EncodedImage, but |
| 100 | // still writes to it, to clear padding at the end of the encoded data. |
| 101 | // Padding is required by ffmpeg; the best way to deal with that is likely to |
| 102 | // make this class ensure that buffers always have a few zero padding bytes. |
| 103 | uint8_t* mutable_data() const { return const_cast<uint8_t*>(data()); } |
| 104 | |
| 105 | // TODO(bugs.webrtc.org/9378): Delete. Used by code that wants to modify a |
| 106 | // buffer corresponding to a const EncodedImage. Requires an un-owned buffer. |
| 107 | uint8_t* buffer() const { return buffer_; } |
| 108 | |
| 109 | // Hack to workaround lack of ownership of the encoded data. If we don't |
| 110 | // already own the underlying data, make an owned copy. |
| 111 | void Retain(); |
Niels Möller | a8f58f0 | 2019-01-10 15:12:49 +0100 | [diff] [blame] | 112 | |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 113 | uint32_t _encodedWidth = 0; |
| 114 | uint32_t _encodedHeight = 0; |
| 115 | // NTP time of the capture time in local timebase in milliseconds. |
| 116 | int64_t ntp_time_ms_ = 0; |
| 117 | int64_t capture_time_ms_ = 0; |
| 118 | FrameType _frameType = kVideoFrameDelta; |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 119 | VideoRotation rotation_ = kVideoRotation_0; |
| 120 | VideoContentType content_type_ = VideoContentType::UNSPECIFIED; |
| 121 | bool _completeFrame = false; |
| 122 | int qp_ = -1; // Quantizer value. |
| 123 | |
| 124 | // When an application indicates non-zero values here, it is taken as an |
| 125 | // indication that all future frames will be constrained with those limits |
| 126 | // until the application indicates a change again. |
| 127 | PlayoutDelay playout_delay_ = {-1, -1}; |
| 128 | |
| 129 | struct Timing { |
| 130 | uint8_t flags = VideoSendTiming::kInvalid; |
| 131 | int64_t encode_start_ms = 0; |
| 132 | int64_t encode_finish_ms = 0; |
| 133 | int64_t packetization_finish_ms = 0; |
| 134 | int64_t pacer_exit_ms = 0; |
| 135 | int64_t network_timestamp_ms = 0; |
| 136 | int64_t network2_timestamp_ms = 0; |
| 137 | int64_t receive_start_ms = 0; |
| 138 | int64_t receive_finish_ms = 0; |
| 139 | } timing_; |
| 140 | |
| 141 | private: |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame^] | 142 | // TODO(bugs.webrtc.org/9378): We're transitioning to always owning the |
| 143 | // encoded data. |
| 144 | std::vector<uint8_t> encoded_data_; |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 145 | size_t size_; // Size of encoded frame data. |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame^] | 146 | // Non-null when used with an un-owned buffer. |
| 147 | uint8_t* buffer_; |
| 148 | // Allocated size of _buffer; relevant only if it's non-null. |
| 149 | size_t capacity_; |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 150 | uint32_t timestamp_rtp_ = 0; |
Johannes Kron | 9973fa8 | 2018-11-07 14:39:26 +0100 | [diff] [blame] | 151 | absl::optional<int> spatial_index_; |
Johannes Kron | 4749e4e | 2018-11-21 10:18:18 +0100 | [diff] [blame] | 152 | absl::optional<webrtc::ColorSpace> color_space_; |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | } // namespace webrtc |
| 156 | |
| 157 | #endif // API_VIDEO_ENCODED_IMAGE_H_ |