| 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 | #include "api/video/encoded_image.h" |
| 12 |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 |
| 16 | #include "rtc_base/ref_counted_object.h" |
| 17 |
| 18 | namespace webrtc { |
| 19 |
| 20 | EncodedImageBuffer::EncodedImageBuffer(size_t size) : size_(size) { |
| 21 | buffer_ = static_cast<uint8_t*>(malloc(size)); |
| 22 | } |
| 23 |
| 24 | EncodedImageBuffer::EncodedImageBuffer(const uint8_t* data, size_t size) |
| 25 | : EncodedImageBuffer(size) { |
| 26 | memcpy(buffer_, data, size); |
| 27 | } |
| 28 |
| 29 | EncodedImageBuffer::~EncodedImageBuffer() { |
| 30 | free(buffer_); |
| 31 | } |
| 32 |
| 33 | // static |
| 34 | rtc::scoped_refptr<EncodedImageBuffer> EncodedImageBuffer::Create(size_t size) { |
| 35 | return rtc::make_ref_counted<EncodedImageBuffer>(size); |
| 36 | } |
| 37 | // static |
| 38 | rtc::scoped_refptr<EncodedImageBuffer> EncodedImageBuffer::Create( |
| 39 | const uint8_t* data, |
| 40 | size_t size) { |
| 41 | return rtc::make_ref_counted<EncodedImageBuffer>(data, size); |
| 42 | } |
| 43 |
| 44 | const uint8_t* EncodedImageBuffer::data() const { |
| 45 | return buffer_; |
| 46 | } |
| 47 | uint8_t* EncodedImageBuffer::data() { |
| 48 | return buffer_; |
| 49 | } |
| 50 | size_t EncodedImageBuffer::size() const { |
| 51 | return size_; |
| 52 | } |
| 53 |
| 54 | void EncodedImageBuffer::Realloc(size_t size) { |
| 55 | // Calling realloc with size == 0 is equivalent to free, and returns nullptr. |
| 56 | // Which is confusing on systems where malloc(0) doesn't return a nullptr. |
| 57 | // More specifically, it breaks expectations of |
| 58 | // VCMSessionInfo::UpdateDataPointers. |
| 59 | RTC_DCHECK(size > 0); |
| 60 | buffer_ = static_cast<uint8_t*>(realloc(buffer_, size)); |
| 61 | size_ = size; |
| 62 | } |
| 63 |
| 64 | EncodedImage::EncodedImage() = default; |
| 65 |
| 66 | EncodedImage::EncodedImage(EncodedImage&&) = default; |
| 67 | EncodedImage::EncodedImage(const EncodedImage&) = default; |
| 68 |
| 69 | EncodedImage::~EncodedImage() = default; |
| 70 |
| 71 | EncodedImage& EncodedImage::operator=(EncodedImage&&) = default; |
| 72 | EncodedImage& EncodedImage::operator=(const EncodedImage&) = default; |
| 73 |
| 74 | void EncodedImage::SetEncodeTime(int64_t encode_start_ms, |
| 75 | int64_t encode_finish_ms) { |
| 76 | timing_.encode_start_ms = encode_start_ms; |
| 77 | timing_.encode_finish_ms = encode_finish_ms; |
| 78 | } |
| 79 |
| 80 | absl::optional<size_t> EncodedImage::SpatialLayerFrameSize( |
| 81 | int spatial_index) const { |
| 82 | RTC_DCHECK_GE(spatial_index, 0); |
| 83 | RTC_DCHECK_LE(spatial_index, spatial_index_.value_or(0)); |
| 84 |
| 85 | auto it = spatial_layer_frame_size_bytes_.find(spatial_index); |
| 86 | if (it == spatial_layer_frame_size_bytes_.end()) { |
| 87 | return absl::nullopt; |
| 88 | } |
| 89 |
| 90 | return it->second; |
| 91 | } |
| 92 |
| 93 | void EncodedImage::SetSpatialLayerFrameSize(int spatial_index, |
| 94 | size_t size_bytes) { |
| 95 | RTC_DCHECK_GE(spatial_index, 0); |
| 96 | RTC_DCHECK_LE(spatial_index, spatial_index_.value_or(0)); |
| 97 | RTC_DCHECK_GE(size_bytes, 0); |
| 98 | spatial_layer_frame_size_bytes_[spatial_index] = size_bytes; |
| 99 | } |
| 100 |
| 101 | } // namespace webrtc |
| 102 |