blob: 9aa5046a3ee742cc7d392aea25084fb229e2792c [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Sergey Silkin2799e632019-05-17 09:51:39 +020016#include <map>
Chen Xingf00bf422019-06-20 10:05:55 +020017#include <utility>
Yves Gerey988cc082018-10-23 12:03:01 +020018
Niels Möller4dc66c52018-10-05 14:17:58 +020019#include "absl/types/optional.h"
Chen Xingf00bf422019-06-20 10:05:55 +020020#include "api/rtp_packet_infos.h"
Niels Möller4d504c72019-06-18 15:56:56 +020021#include "api/scoped_refptr.h"
Johannes Kron4749e4e2018-11-21 10:18:18 +010022#include "api/video/color_space.h"
Niels Möller4a2d57a2019-02-15 09:33:37 +010023#include "api/video/video_codec_constants.h"
Niels Möller22b70ff2018-11-20 11:06:58 +010024#include "api/video/video_codec_type.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020025#include "api/video/video_content_type.h"
Niels Möller8f7ce222019-03-21 15:43:58 +010026#include "api/video/video_frame_type.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020027#include "api/video/video_rotation.h"
28#include "api/video/video_timing.h"
29#include "common_types.h" // NOLINT(build/include)
Yves Gerey988cc082018-10-23 12:03:01 +020030#include "rtc_base/checks.h"
Niels Möller4d504c72019-06-18 15:56:56 +020031#include "rtc_base/ref_count.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020032#include "rtc_base/system/rtc_export.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020033
34namespace webrtc {
35
Niels Möller4d504c72019-06-18 15:56:56 +020036// Abstract interface for buffer storage. Intended to support buffers owned by
37// external encoders with special release requirements, e.g, java encoders with
38// releaseOutputBuffer.
39class EncodedImageBufferInterface : public rtc::RefCountInterface {
40 public:
41 virtual const uint8_t* data() const = 0;
42 // TODO(bugs.webrtc.org/9378): Make interface essentially read-only, delete
43 // this non-const data method.
44 virtual uint8_t* data() = 0;
45 virtual size_t size() const = 0;
46 // TODO(bugs.webrtc.org/9378): Delete from this interface, together with
47 // EncodedImage::Allocate. Implemented properly only by the below concrete
48 // class
49 virtual void Realloc(size_t size) { RTC_NOTREACHED(); }
50};
51
52// Basic implementation of EncodedImageBufferInterface.
53class EncodedImageBuffer : public EncodedImageBufferInterface {
54 public:
55 static rtc::scoped_refptr<EncodedImageBuffer> Create(size_t size);
56 static rtc::scoped_refptr<EncodedImageBuffer> Create(const uint8_t* data,
57 size_t size);
58
59 const uint8_t* data() const override;
60 uint8_t* data() override;
61 size_t size() const override;
62 void Realloc(size_t t) override;
63
64 protected:
65 explicit EncodedImageBuffer(size_t size);
66 EncodedImageBuffer(const uint8_t* data, size_t size);
67 ~EncodedImageBuffer();
68
69 size_t size_;
70 uint8_t* buffer_;
71};
72
Niels Möller4dc66c52018-10-05 14:17:58 +020073// TODO(bug.webrtc.org/9378): This is a legacy api class, which is slowly being
74// cleaned up. Direct use of its members is strongly discouraged.
Mirko Bonadeiac194142018-10-22 17:08:37 +020075class RTC_EXPORT EncodedImage {
Niels Möller4dc66c52018-10-05 14:17:58 +020076 public:
Niels Möller4dc66c52018-10-05 14:17:58 +020077 EncodedImage();
Niels Möller938dd9f2019-02-07 00:02:17 +010078 EncodedImage(EncodedImage&&);
79 // Discouraged: potentially expensive.
Niels Möller4dc66c52018-10-05 14:17:58 +020080 EncodedImage(const EncodedImage&);
Niels Möllera6fd5e42018-12-10 09:07:28 +010081 EncodedImage(uint8_t* buffer, size_t length, size_t capacity);
Niels Möller4dc66c52018-10-05 14:17:58 +020082
Niels Möller938dd9f2019-02-07 00:02:17 +010083 ~EncodedImage();
84
85 EncodedImage& operator=(EncodedImage&&);
86 // Discouraged: potentially expensive.
87 EncodedImage& operator=(const EncodedImage&);
88
Niels Möller4dc66c52018-10-05 14:17:58 +020089 // TODO(nisse): Change style to timestamp(), set_timestamp(), for consistency
90 // with the VideoFrame class.
91 // Set frame timestamp (90kHz).
92 void SetTimestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
93
94 // Get frame timestamp (90kHz).
95 uint32_t Timestamp() const { return timestamp_rtp_; }
96
97 void SetEncodeTime(int64_t encode_start_ms, int64_t encode_finish_ms);
98
Jonas Olssona4d87372019-07-05 19:08:33 +020099 absl::optional<int> SpatialIndex() const { return spatial_index_; }
Niels Möller4dc66c52018-10-05 14:17:58 +0200100 void SetSpatialIndex(absl::optional<int> spatial_index) {
101 RTC_DCHECK_GE(spatial_index.value_or(0), 0);
102 RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers);
Johannes Kron9973fa82018-11-07 14:39:26 +0100103 spatial_index_ = spatial_index;
104 }
105
Sergey Silkin2799e632019-05-17 09:51:39 +0200106 // These methods can be used to set/get size of subframe with spatial index
107 // |spatial_index| on encoded frames that consist of multiple spatial layers.
108 absl::optional<size_t> SpatialLayerFrameSize(int spatial_index) const;
109 void SetSpatialLayerFrameSize(int spatial_index, size_t size_bytes);
110
Johannes Kron4749e4e2018-11-21 10:18:18 +0100111 const webrtc::ColorSpace* ColorSpace() const {
112 return color_space_ ? &*color_space_ : nullptr;
Johannes Kron9973fa82018-11-07 14:39:26 +0100113 }
Danil Chapovalovb7698942019-02-05 11:32:19 +0100114 void SetColorSpace(const absl::optional<webrtc::ColorSpace>& color_space) {
115 color_space_ = color_space;
Niels Möller4dc66c52018-10-05 14:17:58 +0200116 }
117
Chen Xingf00bf422019-06-20 10:05:55 +0200118 const RtpPacketInfos& PacketInfos() const { return packet_infos_; }
119 void SetPacketInfos(RtpPacketInfos packet_infos) {
120 packet_infos_ = std::move(packet_infos);
121 }
122
Elad Alonb64af4b2019-06-05 11:39:37 +0200123 bool RetransmissionAllowed() const { return retransmission_allowed_; }
124 void SetRetransmissionAllowed(bool retransmission_allowed) {
125 retransmission_allowed_ = retransmission_allowed;
126 }
127
Niels Möller77536a22019-01-15 08:50:01 +0100128 size_t size() const { return size_; }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100129 void set_size(size_t new_size) {
Niels Möller4d504c72019-06-18 15:56:56 +0200130 // Allow set_size(0) even if we have no buffer.
131 RTC_DCHECK_LE(new_size, new_size == 0 ? 0 : capacity());
Niels Möller77536a22019-01-15 08:50:01 +0100132 size_ = new_size;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100133 }
Niels Möller4d504c72019-06-18 15:56:56 +0200134 // TODO(nisse): Delete, provide only read-only access to the buffer.
135 size_t capacity() const {
136 return buffer_ ? capacity_ : (encoded_data_ ? encoded_data_->size() : 0);
137 }
Niels Möllerf0eee002018-11-28 16:31:29 +0100138
Niels Möller77894cc2018-12-04 15:38:15 +0100139 void set_buffer(uint8_t* buffer, size_t capacity) {
Niels Möller24871e42019-01-17 11:31:13 +0100140 buffer_ = buffer;
Niels Möllera6fd5e42018-12-10 09:07:28 +0100141 capacity_ = capacity;
Niels Möller77894cc2018-12-04 15:38:15 +0100142 }
143
Niels Möller4d504c72019-06-18 15:56:56 +0200144 // TODO(bugs.webrtc.org/9378): Delete; this method implies realloc, which
145 // should not be generally supported by the EncodedImageBufferInterface.
146 void Allocate(size_t capacity);
Niels Möller938dd9f2019-02-07 00:02:17 +0100147
Niels Möller4d504c72019-06-18 15:56:56 +0200148 void SetEncodedData(
149 rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data) {
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200150 encoded_data_ = encoded_data;
Niels Möller4d504c72019-06-18 15:56:56 +0200151 size_ = encoded_data->size();
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200152 buffer_ = nullptr;
153 }
154
Niels Möller4d504c72019-06-18 15:56:56 +0200155 // TODO(nisse): Delete, provide only read-only access to the buffer.
156 uint8_t* data() {
157 return buffer_ ? buffer_
158 : (encoded_data_ ? encoded_data_->data() : nullptr);
159 }
Niels Möller938dd9f2019-02-07 00:02:17 +0100160 const uint8_t* data() const {
Niels Möller4d504c72019-06-18 15:56:56 +0200161 return buffer_ ? buffer_
162 : (encoded_data_ ? encoded_data_->data() : nullptr);
Niels Möller938dd9f2019-02-07 00:02:17 +0100163 }
164 // TODO(nisse): At some places, code accepts a const ref EncodedImage, but
165 // still writes to it, to clear padding at the end of the encoded data.
166 // Padding is required by ffmpeg; the best way to deal with that is likely to
167 // make this class ensure that buffers always have a few zero padding bytes.
168 uint8_t* mutable_data() const { return const_cast<uint8_t*>(data()); }
169
170 // TODO(bugs.webrtc.org/9378): Delete. Used by code that wants to modify a
171 // buffer corresponding to a const EncodedImage. Requires an un-owned buffer.
172 uint8_t* buffer() const { return buffer_; }
173
174 // Hack to workaround lack of ownership of the encoded data. If we don't
175 // already own the underlying data, make an owned copy.
176 void Retain();
Niels Möllera8f58f02019-01-10 15:12:49 +0100177
Niels Möller4dc66c52018-10-05 14:17:58 +0200178 uint32_t _encodedWidth = 0;
179 uint32_t _encodedHeight = 0;
180 // NTP time of the capture time in local timebase in milliseconds.
181 int64_t ntp_time_ms_ = 0;
182 int64_t capture_time_ms_ = 0;
Niels Möller8f7ce222019-03-21 15:43:58 +0100183 VideoFrameType _frameType = VideoFrameType::kVideoFrameDelta;
Niels Möller4dc66c52018-10-05 14:17:58 +0200184 VideoRotation rotation_ = kVideoRotation_0;
185 VideoContentType content_type_ = VideoContentType::UNSPECIFIED;
186 bool _completeFrame = false;
187 int qp_ = -1; // Quantizer value.
188
189 // When an application indicates non-zero values here, it is taken as an
190 // indication that all future frames will be constrained with those limits
191 // until the application indicates a change again.
192 PlayoutDelay playout_delay_ = {-1, -1};
193
194 struct Timing {
195 uint8_t flags = VideoSendTiming::kInvalid;
196 int64_t encode_start_ms = 0;
197 int64_t encode_finish_ms = 0;
198 int64_t packetization_finish_ms = 0;
199 int64_t pacer_exit_ms = 0;
200 int64_t network_timestamp_ms = 0;
201 int64_t network2_timestamp_ms = 0;
202 int64_t receive_start_ms = 0;
203 int64_t receive_finish_ms = 0;
204 } timing_;
205
206 private:
Niels Möller938dd9f2019-02-07 00:02:17 +0100207 // TODO(bugs.webrtc.org/9378): We're transitioning to always owning the
208 // encoded data.
Niels Möller4d504c72019-06-18 15:56:56 +0200209 rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data_;
Jonas Olssona4d87372019-07-05 19:08:33 +0200210 size_t size_; // Size of encoded frame data.
Niels Möller938dd9f2019-02-07 00:02:17 +0100211 // Non-null when used with an un-owned buffer.
212 uint8_t* buffer_;
213 // Allocated size of _buffer; relevant only if it's non-null.
214 size_t capacity_;
Niels Möller4dc66c52018-10-05 14:17:58 +0200215 uint32_t timestamp_rtp_ = 0;
Johannes Kron9973fa82018-11-07 14:39:26 +0100216 absl::optional<int> spatial_index_;
Sergey Silkin2799e632019-05-17 09:51:39 +0200217 std::map<int, size_t> spatial_layer_frame_size_bytes_;
Johannes Kron4749e4e2018-11-21 10:18:18 +0100218 absl::optional<webrtc::ColorSpace> color_space_;
Chen Xingf00bf422019-06-20 10:05:55 +0200219 // Information about packets used to assemble this video frame. This is needed
220 // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
221 // MediaStreamTrack, in order to implement getContributingSources(). See:
222 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
223 RtpPacketInfos packet_infos_;
Elad Alonb64af4b2019-06-05 11:39:37 +0200224 bool retransmission_allowed_ = true;
Niels Möller4dc66c52018-10-05 14:17:58 +0200225};
226
227} // namespace webrtc
228
229#endif // API_VIDEO_ENCODED_IMAGE_H_