blob: 6bd1527e949bdc4a7aaf8abe9bd407d2a6d1393c [file] [log] [blame]
Niels Möller4dc66c52018-10-05 14:17:58 +02001/*
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 Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
Sergey Silkin2799e632019-05-17 09:51:39 +020015#include <map>
Yves Gerey988cc082018-10-23 12:03:01 +020016
Niels Möller4dc66c52018-10-05 14:17:58 +020017#include "absl/types/optional.h"
Johannes Kron4749e4e2018-11-21 10:18:18 +010018#include "api/video/color_space.h"
Niels Möller4a2d57a2019-02-15 09:33:37 +010019#include "api/video/video_codec_constants.h"
Niels Möller22b70ff2018-11-20 11:06:58 +010020#include "api/video/video_codec_type.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020021#include "api/video/video_content_type.h"
Niels Möller8f7ce222019-03-21 15:43:58 +010022#include "api/video/video_frame_type.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020023#include "api/video/video_rotation.h"
24#include "api/video/video_timing.h"
25#include "common_types.h" // NOLINT(build/include)
Yves Gerey988cc082018-10-23 12:03:01 +020026#include "rtc_base/checks.h"
Niels Möllercc26fef2019-02-19 15:42:15 +010027#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020028#include "rtc_base/system/rtc_export.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020029
30namespace webrtc {
31
32// TODO(bug.webrtc.org/9378): This is a legacy api class, which is slowly being
33// cleaned up. Direct use of its members is strongly discouraged.
Mirko Bonadeiac194142018-10-22 17:08:37 +020034class RTC_EXPORT EncodedImage {
Niels Möller4dc66c52018-10-05 14:17:58 +020035 public:
Niels Möller4dc66c52018-10-05 14:17:58 +020036 EncodedImage();
Niels Möller938dd9f2019-02-07 00:02:17 +010037 EncodedImage(EncodedImage&&);
38 // Discouraged: potentially expensive.
Niels Möller4dc66c52018-10-05 14:17:58 +020039 EncodedImage(const EncodedImage&);
Niels Möllera6fd5e42018-12-10 09:07:28 +010040 EncodedImage(uint8_t* buffer, size_t length, size_t capacity);
Niels Möller4dc66c52018-10-05 14:17:58 +020041
Niels Möller938dd9f2019-02-07 00:02:17 +010042 ~EncodedImage();
43
44 EncodedImage& operator=(EncodedImage&&);
45 // Discouraged: potentially expensive.
46 EncodedImage& operator=(const EncodedImage&);
47
Niels Möller4dc66c52018-10-05 14:17:58 +020048 // TODO(nisse): Change style to timestamp(), set_timestamp(), for consistency
49 // with the VideoFrame class.
50 // Set frame timestamp (90kHz).
51 void SetTimestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
52
53 // Get frame timestamp (90kHz).
54 uint32_t Timestamp() const { return timestamp_rtp_; }
55
56 void SetEncodeTime(int64_t encode_start_ms, int64_t encode_finish_ms);
57
58 absl::optional<int> SpatialIndex() const {
Niels Möller4dc66c52018-10-05 14:17:58 +020059 return spatial_index_;
60 }
61 void SetSpatialIndex(absl::optional<int> spatial_index) {
62 RTC_DCHECK_GE(spatial_index.value_or(0), 0);
63 RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers);
Johannes Kron9973fa82018-11-07 14:39:26 +010064 spatial_index_ = spatial_index;
65 }
66
Sergey Silkin2799e632019-05-17 09:51:39 +020067 // These methods can be used to set/get size of subframe with spatial index
68 // |spatial_index| on encoded frames that consist of multiple spatial layers.
69 absl::optional<size_t> SpatialLayerFrameSize(int spatial_index) const;
70 void SetSpatialLayerFrameSize(int spatial_index, size_t size_bytes);
71
Johannes Kron4749e4e2018-11-21 10:18:18 +010072 const webrtc::ColorSpace* ColorSpace() const {
73 return color_space_ ? &*color_space_ : nullptr;
Johannes Kron9973fa82018-11-07 14:39:26 +010074 }
Danil Chapovalovb7698942019-02-05 11:32:19 +010075 void SetColorSpace(const absl::optional<webrtc::ColorSpace>& color_space) {
76 color_space_ = color_space;
Niels Möller4dc66c52018-10-05 14:17:58 +020077 }
78
Elad Alonb64af4b2019-06-05 11:39:37 +020079 bool RetransmissionAllowed() const { return retransmission_allowed_; }
80 void SetRetransmissionAllowed(bool retransmission_allowed) {
81 retransmission_allowed_ = retransmission_allowed;
82 }
83
Niels Möller77536a22019-01-15 08:50:01 +010084 size_t size() const { return size_; }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010085 void set_size(size_t new_size) {
Niels Möller938dd9f2019-02-07 00:02:17 +010086 RTC_DCHECK_LE(new_size, capacity());
Niels Möller77536a22019-01-15 08:50:01 +010087 size_ = new_size;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010088 }
Niels Möller938dd9f2019-02-07 00:02:17 +010089 size_t capacity() const { return buffer_ ? capacity_ : encoded_data_.size(); }
Niels Möllerf0eee002018-11-28 16:31:29 +010090
Niels Möller77894cc2018-12-04 15:38:15 +010091 void set_buffer(uint8_t* buffer, size_t capacity) {
Niels Möller24871e42019-01-17 11:31:13 +010092 buffer_ = buffer;
Niels Möllera6fd5e42018-12-10 09:07:28 +010093 capacity_ = capacity;
Niels Möller77894cc2018-12-04 15:38:15 +010094 }
95
Niels Möller938dd9f2019-02-07 00:02:17 +010096 void Allocate(size_t capacity) {
Niels Möllercc26fef2019-02-19 15:42:15 +010097 encoded_data_.SetSize(capacity);
Niels Möller938dd9f2019-02-07 00:02:17 +010098 buffer_ = nullptr;
99 }
100
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200101 void SetEncodedData(const rtc::CopyOnWriteBuffer& encoded_data) {
102 encoded_data_ = encoded_data;
103 size_ = encoded_data.size();
104 buffer_ = nullptr;
105 }
106
Niels Möller938dd9f2019-02-07 00:02:17 +0100107 uint8_t* data() { return buffer_ ? buffer_ : encoded_data_.data(); }
108 const uint8_t* data() const {
Niels Möllercc26fef2019-02-19 15:42:15 +0100109 return buffer_ ? buffer_ : encoded_data_.cdata();
Niels Möller938dd9f2019-02-07 00:02:17 +0100110 }
111 // TODO(nisse): At some places, code accepts a const ref EncodedImage, but
112 // still writes to it, to clear padding at the end of the encoded data.
113 // Padding is required by ffmpeg; the best way to deal with that is likely to
114 // make this class ensure that buffers always have a few zero padding bytes.
115 uint8_t* mutable_data() const { return const_cast<uint8_t*>(data()); }
116
117 // TODO(bugs.webrtc.org/9378): Delete. Used by code that wants to modify a
118 // buffer corresponding to a const EncodedImage. Requires an un-owned buffer.
119 uint8_t* buffer() const { return buffer_; }
120
121 // Hack to workaround lack of ownership of the encoded data. If we don't
122 // already own the underlying data, make an owned copy.
123 void Retain();
Niels Möllera8f58f02019-01-10 15:12:49 +0100124
Niels Möller4dc66c52018-10-05 14:17:58 +0200125 uint32_t _encodedWidth = 0;
126 uint32_t _encodedHeight = 0;
127 // NTP time of the capture time in local timebase in milliseconds.
128 int64_t ntp_time_ms_ = 0;
129 int64_t capture_time_ms_ = 0;
Niels Möller8f7ce222019-03-21 15:43:58 +0100130 VideoFrameType _frameType = VideoFrameType::kVideoFrameDelta;
Niels Möller4dc66c52018-10-05 14:17:58 +0200131 VideoRotation rotation_ = kVideoRotation_0;
132 VideoContentType content_type_ = VideoContentType::UNSPECIFIED;
133 bool _completeFrame = false;
134 int qp_ = -1; // Quantizer value.
135
136 // When an application indicates non-zero values here, it is taken as an
137 // indication that all future frames will be constrained with those limits
138 // until the application indicates a change again.
139 PlayoutDelay playout_delay_ = {-1, -1};
140
141 struct Timing {
142 uint8_t flags = VideoSendTiming::kInvalid;
143 int64_t encode_start_ms = 0;
144 int64_t encode_finish_ms = 0;
145 int64_t packetization_finish_ms = 0;
146 int64_t pacer_exit_ms = 0;
147 int64_t network_timestamp_ms = 0;
148 int64_t network2_timestamp_ms = 0;
149 int64_t receive_start_ms = 0;
150 int64_t receive_finish_ms = 0;
151 } timing_;
152
153 private:
Niels Möller938dd9f2019-02-07 00:02:17 +0100154 // TODO(bugs.webrtc.org/9378): We're transitioning to always owning the
155 // encoded data.
Niels Möllercc26fef2019-02-19 15:42:15 +0100156 rtc::CopyOnWriteBuffer encoded_data_;
Niels Möller77536a22019-01-15 08:50:01 +0100157 size_t size_; // Size of encoded frame data.
Niels Möller938dd9f2019-02-07 00:02:17 +0100158 // Non-null when used with an un-owned buffer.
159 uint8_t* buffer_;
160 // Allocated size of _buffer; relevant only if it's non-null.
161 size_t capacity_;
Niels Möller4dc66c52018-10-05 14:17:58 +0200162 uint32_t timestamp_rtp_ = 0;
Johannes Kron9973fa82018-11-07 14:39:26 +0100163 absl::optional<int> spatial_index_;
Sergey Silkin2799e632019-05-17 09:51:39 +0200164 std::map<int, size_t> spatial_layer_frame_size_bytes_;
Johannes Kron4749e4e2018-11-21 10:18:18 +0100165 absl::optional<webrtc::ColorSpace> color_space_;
Elad Alonb64af4b2019-06-05 11:39:37 +0200166 bool retransmission_allowed_ = true;
Niels Möller4dc66c52018-10-05 14:17:58 +0200167};
168
169} // namespace webrtc
170
171#endif // API_VIDEO_ENCODED_IMAGE_H_