blob: 611da1349a066f49fb9715f987db815fc2e48676 [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>
15
Niels Möller4dc66c52018-10-05 14:17:58 +020016#include "absl/types/optional.h"
Johannes Kron4749e4e2018-11-21 10:18:18 +010017#include "api/video/color_space.h"
Niels Möller4a2d57a2019-02-15 09:33:37 +010018#include "api/video/video_codec_constants.h"
Niels Möller22b70ff2018-11-20 11:06:58 +010019#include "api/video/video_codec_type.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020020#include "api/video/video_content_type.h"
21#include "api/video/video_rotation.h"
22#include "api/video/video_timing.h"
23#include "common_types.h" // NOLINT(build/include)
Yves Gerey988cc082018-10-23 12:03:01 +020024#include "rtc_base/checks.h"
Niels Möllercc26fef2019-02-19 15:42:15 +010025#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020026#include "rtc_base/system/rtc_export.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020027
28namespace 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 Bonadeiac194142018-10-22 17:08:37 +020032class RTC_EXPORT EncodedImage {
Niels Möller4dc66c52018-10-05 14:17:58 +020033 public:
Niels Möller4dc66c52018-10-05 14:17:58 +020034 EncodedImage();
Niels Möller938dd9f2019-02-07 00:02:17 +010035 EncodedImage(EncodedImage&&);
36 // Discouraged: potentially expensive.
Niels Möller4dc66c52018-10-05 14:17:58 +020037 EncodedImage(const EncodedImage&);
Niels Möllera6fd5e42018-12-10 09:07:28 +010038 EncodedImage(uint8_t* buffer, size_t length, size_t capacity);
Niels Möller4dc66c52018-10-05 14:17:58 +020039
Niels Möller938dd9f2019-02-07 00:02:17 +010040 ~EncodedImage();
41
42 EncodedImage& operator=(EncodedImage&&);
43 // Discouraged: potentially expensive.
44 EncodedImage& operator=(const EncodedImage&);
45
Niels Möller4dc66c52018-10-05 14:17:58 +020046 // TODO(nisse): Change style to timestamp(), set_timestamp(), for consistency
47 // with the VideoFrame class.
48 // Set frame timestamp (90kHz).
49 void SetTimestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
50
51 // Get frame timestamp (90kHz).
52 uint32_t Timestamp() const { return timestamp_rtp_; }
53
54 void SetEncodeTime(int64_t encode_start_ms, int64_t encode_finish_ms);
55
56 absl::optional<int> SpatialIndex() const {
Niels Möller4dc66c52018-10-05 14:17:58 +020057 return spatial_index_;
58 }
59 void SetSpatialIndex(absl::optional<int> spatial_index) {
60 RTC_DCHECK_GE(spatial_index.value_or(0), 0);
61 RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers);
Johannes Kron9973fa82018-11-07 14:39:26 +010062 spatial_index_ = spatial_index;
63 }
64
Johannes Kron4749e4e2018-11-21 10:18:18 +010065 const webrtc::ColorSpace* ColorSpace() const {
66 return color_space_ ? &*color_space_ : nullptr;
Johannes Kron9973fa82018-11-07 14:39:26 +010067 }
Danil Chapovalovb7698942019-02-05 11:32:19 +010068 void SetColorSpace(const absl::optional<webrtc::ColorSpace>& color_space) {
69 color_space_ = color_space;
Niels Möller4dc66c52018-10-05 14:17:58 +020070 }
71
Niels Möller77536a22019-01-15 08:50:01 +010072 size_t size() const { return size_; }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010073 void set_size(size_t new_size) {
Niels Möller938dd9f2019-02-07 00:02:17 +010074 RTC_DCHECK_LE(new_size, capacity());
Niels Möller77536a22019-01-15 08:50:01 +010075 size_ = new_size;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010076 }
Niels Möller938dd9f2019-02-07 00:02:17 +010077 size_t capacity() const { return buffer_ ? capacity_ : encoded_data_.size(); }
Niels Möllerf0eee002018-11-28 16:31:29 +010078
Niels Möller77894cc2018-12-04 15:38:15 +010079 void set_buffer(uint8_t* buffer, size_t capacity) {
Niels Möller24871e42019-01-17 11:31:13 +010080 buffer_ = buffer;
Niels Möllera6fd5e42018-12-10 09:07:28 +010081 capacity_ = capacity;
Niels Möller77894cc2018-12-04 15:38:15 +010082 }
83
Niels Möller938dd9f2019-02-07 00:02:17 +010084 void Allocate(size_t capacity) {
Niels Möllercc26fef2019-02-19 15:42:15 +010085 encoded_data_.SetSize(capacity);
Niels Möller938dd9f2019-02-07 00:02:17 +010086 buffer_ = nullptr;
87 }
88
89 uint8_t* data() { return buffer_ ? buffer_ : encoded_data_.data(); }
90 const uint8_t* data() const {
Niels Möllercc26fef2019-02-19 15:42:15 +010091 return buffer_ ? buffer_ : encoded_data_.cdata();
Niels Möller938dd9f2019-02-07 00:02:17 +010092 }
93 // TODO(nisse): At some places, code accepts a const ref EncodedImage, but
94 // still writes to it, to clear padding at the end of the encoded data.
95 // Padding is required by ffmpeg; the best way to deal with that is likely to
96 // make this class ensure that buffers always have a few zero padding bytes.
97 uint8_t* mutable_data() const { return const_cast<uint8_t*>(data()); }
98
99 // TODO(bugs.webrtc.org/9378): Delete. Used by code that wants to modify a
100 // buffer corresponding to a const EncodedImage. Requires an un-owned buffer.
101 uint8_t* buffer() const { return buffer_; }
102
103 // Hack to workaround lack of ownership of the encoded data. If we don't
104 // already own the underlying data, make an owned copy.
105 void Retain();
Niels Möllera8f58f02019-01-10 15:12:49 +0100106
Niels Möller4dc66c52018-10-05 14:17:58 +0200107 uint32_t _encodedWidth = 0;
108 uint32_t _encodedHeight = 0;
109 // NTP time of the capture time in local timebase in milliseconds.
110 int64_t ntp_time_ms_ = 0;
111 int64_t capture_time_ms_ = 0;
Niels Möller87e2d782019-03-07 10:18:23 +0100112 VideoFrameType _frameType = kVideoFrameDelta;
Niels Möller4dc66c52018-10-05 14:17:58 +0200113 VideoRotation rotation_ = kVideoRotation_0;
114 VideoContentType content_type_ = VideoContentType::UNSPECIFIED;
115 bool _completeFrame = false;
116 int qp_ = -1; // Quantizer value.
117
118 // When an application indicates non-zero values here, it is taken as an
119 // indication that all future frames will be constrained with those limits
120 // until the application indicates a change again.
121 PlayoutDelay playout_delay_ = {-1, -1};
122
123 struct Timing {
124 uint8_t flags = VideoSendTiming::kInvalid;
125 int64_t encode_start_ms = 0;
126 int64_t encode_finish_ms = 0;
127 int64_t packetization_finish_ms = 0;
128 int64_t pacer_exit_ms = 0;
129 int64_t network_timestamp_ms = 0;
130 int64_t network2_timestamp_ms = 0;
131 int64_t receive_start_ms = 0;
132 int64_t receive_finish_ms = 0;
133 } timing_;
134
135 private:
Niels Möller938dd9f2019-02-07 00:02:17 +0100136 // TODO(bugs.webrtc.org/9378): We're transitioning to always owning the
137 // encoded data.
Niels Möllercc26fef2019-02-19 15:42:15 +0100138 rtc::CopyOnWriteBuffer encoded_data_;
Niels Möller77536a22019-01-15 08:50:01 +0100139 size_t size_; // Size of encoded frame data.
Niels Möller938dd9f2019-02-07 00:02:17 +0100140 // Non-null when used with an un-owned buffer.
141 uint8_t* buffer_;
142 // Allocated size of _buffer; relevant only if it's non-null.
143 size_t capacity_;
Niels Möller4dc66c52018-10-05 14:17:58 +0200144 uint32_t timestamp_rtp_ = 0;
Johannes Kron9973fa82018-11-07 14:39:26 +0100145 absl::optional<int> spatial_index_;
Johannes Kron4749e4e2018-11-21 10:18:18 +0100146 absl::optional<webrtc::ColorSpace> color_space_;
Niels Möller4dc66c52018-10-05 14:17:58 +0200147};
148
149} // namespace webrtc
150
151#endif // API_VIDEO_ENCODED_IMAGE_H_