• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 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 #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 
EncodedImageBuffer(size_t size)20 EncodedImageBuffer::EncodedImageBuffer(size_t size) : size_(size) {
21   buffer_ = static_cast<uint8_t*>(malloc(size));
22 }
23 
EncodedImageBuffer(const uint8_t * data,size_t size)24 EncodedImageBuffer::EncodedImageBuffer(const uint8_t* data, size_t size)
25     : EncodedImageBuffer(size) {
26   memcpy(buffer_, data, size);
27 }
28 
~EncodedImageBuffer()29 EncodedImageBuffer::~EncodedImageBuffer() {
30   free(buffer_);
31 }
32 
33 // static
Create(size_t size)34 rtc::scoped_refptr<EncodedImageBuffer> EncodedImageBuffer::Create(size_t size) {
35   return new rtc::RefCountedObject<EncodedImageBuffer>(size);
36 }
37 // static
Create(const uint8_t * data,size_t size)38 rtc::scoped_refptr<EncodedImageBuffer> EncodedImageBuffer::Create(
39     const uint8_t* data,
40     size_t size) {
41   return new rtc::RefCountedObject<EncodedImageBuffer>(data, size);
42 }
43 
data() const44 const uint8_t* EncodedImageBuffer::data() const {
45   return buffer_;
46 }
data()47 uint8_t* EncodedImageBuffer::data() {
48   return buffer_;
49 }
size() const50 size_t EncodedImageBuffer::size() const {
51   return size_;
52 }
53 
Realloc(size_t size)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 
EncodedImage()64 EncodedImage::EncodedImage() : EncodedImage(nullptr, 0, 0) {}
65 
66 EncodedImage::EncodedImage(EncodedImage&&) = default;
67 EncodedImage::EncodedImage(const EncodedImage&) = default;
68 
EncodedImage(uint8_t * buffer,size_t size,size_t capacity)69 EncodedImage::EncodedImage(uint8_t* buffer, size_t size, size_t capacity)
70     : size_(size), buffer_(buffer), capacity_(capacity) {}
71 
72 EncodedImage::~EncodedImage() = default;
73 
74 EncodedImage& EncodedImage::operator=(EncodedImage&&) = default;
75 EncodedImage& EncodedImage::operator=(const EncodedImage&) = default;
76 
Retain()77 void EncodedImage::Retain() {
78   if (buffer_) {
79     encoded_data_ = EncodedImageBuffer::Create(buffer_, size_);
80     buffer_ = nullptr;
81   }
82 }
83 
SetEncodeTime(int64_t encode_start_ms,int64_t encode_finish_ms)84 void EncodedImage::SetEncodeTime(int64_t encode_start_ms,
85                                  int64_t encode_finish_ms) {
86   timing_.encode_start_ms = encode_start_ms;
87   timing_.encode_finish_ms = encode_finish_ms;
88 }
89 
SpatialLayerFrameSize(int spatial_index) const90 absl::optional<size_t> EncodedImage::SpatialLayerFrameSize(
91     int spatial_index) const {
92   RTC_DCHECK_GE(spatial_index, 0);
93   RTC_DCHECK_LE(spatial_index, spatial_index_.value_or(0));
94 
95   auto it = spatial_layer_frame_size_bytes_.find(spatial_index);
96   if (it == spatial_layer_frame_size_bytes_.end()) {
97     return absl::nullopt;
98   }
99 
100   return it->second;
101 }
102 
SetSpatialLayerFrameSize(int spatial_index,size_t size_bytes)103 void EncodedImage::SetSpatialLayerFrameSize(int spatial_index,
104                                             size_t size_bytes) {
105   RTC_DCHECK_GE(spatial_index, 0);
106   RTC_DCHECK_LE(spatial_index, spatial_index_.value_or(0));
107   RTC_DCHECK_GE(size_bytes, 0);
108   spatial_layer_frame_size_bytes_[spatial_index] = size_bytes;
109 }
110 
111 }  // namespace webrtc
112