You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.3 KiB

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