Move EncodedImage class to api/video/

Bug: webrtc:9378
Change-Id: I8fb3b19cad0ad428abc6c8e6b507180d461882ba
Reviewed-on: https://webrtc-review.googlesource.com/c/104002
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Peter Slatala <psla@webrtc.org>
Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25033}
diff --git a/api/DEPS b/api/DEPS
index 847cce2..3bc7287 100644
--- a/api/DEPS
+++ b/api/DEPS
@@ -1,6 +1,5 @@
 include_rules = [
   "+third_party/libyuv",
-  "+common_video",
   "+media",
   "+p2p",
   "+pc",
diff --git a/api/video/BUILD.gn b/api/video/BUILD.gn
index d041260..3f71007 100644
--- a/api/video/BUILD.gn
+++ b/api/video/BUILD.gn
@@ -66,6 +66,20 @@
   ]
 }
 
+rtc_source_set("encoded_image") {
+  sources = [
+    "encoded_image.cc",
+    "encoded_image.h",
+  ]
+  deps = [
+    ":video_frame",
+    "../..:webrtc_common",
+    "../../rtc_base:checks",
+    "../../rtc_base:rtc_base_approved",
+    "//third_party/abseil-cpp/absl/types:optional",
+  ]
+}
+
 rtc_source_set("encoded_frame") {
   visibility = [ "*" ]
   sources = [
diff --git a/api/video/encoded_image.cc b/api/video/encoded_image.cc
new file mode 100644
index 0000000..5658dfc
--- /dev/null
+++ b/api/video/encoded_image.cc
@@ -0,0 +1,47 @@
+/*
+ *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "api/video/encoded_image.h"
+
+#include <string.h>
+
+#include <algorithm>  // swap
+
+#include "rtc_base/bind.h"
+#include "rtc_base/checks.h"
+
+namespace webrtc {
+
+// FFmpeg's decoder, used by H264DecoderImpl, requires up to 8 bytes padding due
+// to optimized bitstream readers. See avcodec_decode_video2.
+const size_t EncodedImage::kBufferPaddingBytesH264 = 8;
+
+size_t EncodedImage::GetBufferPaddingBytes(VideoCodecType codec_type) {
+  switch (codec_type) {
+    case kVideoCodecH264:
+      return kBufferPaddingBytesH264;
+    default:
+      return 0;
+  }
+}
+
+EncodedImage::EncodedImage() : EncodedImage(nullptr, 0, 0) {}
+
+EncodedImage::EncodedImage(const EncodedImage&) = default;
+
+EncodedImage::EncodedImage(uint8_t* buffer, size_t length, size_t size)
+    : _buffer(buffer), _length(length), _size(size) {}
+
+void EncodedImage::SetEncodeTime(int64_t encode_start_ms,
+                                 int64_t encode_finish_ms) {
+  timing_.encode_start_ms = encode_start_ms;
+  timing_.encode_finish_ms = encode_finish_ms;
+}
+}  // namespace webrtc
diff --git a/api/video/encoded_image.h b/api/video/encoded_image.h
new file mode 100644
index 0000000..ed58d96
--- /dev/null
+++ b/api/video/encoded_image.h
@@ -0,0 +1,97 @@
+/*
+ *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef API_VIDEO_ENCODED_IMAGE_H_
+#define API_VIDEO_ENCODED_IMAGE_H_
+
+#include "absl/types/optional.h"
+#include "api/video/video_content_type.h"
+#include "api/video/video_rotation.h"
+#include "api/video/video_timing.h"
+#include "common_types.h"  // NOLINT(build/include)
+
+namespace webrtc {
+
+// TODO(bug.webrtc.org/9378): This is a legacy api class, which is slowly being
+// cleaned up. Direct use of its members is strongly discouraged.
+class EncodedImage {
+ public:
+  static const size_t kBufferPaddingBytesH264;
+
+  // Some decoders require encoded image buffers to be padded with a small
+  // number of additional bytes (due to over-reading byte readers).
+  static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
+
+  EncodedImage();
+  EncodedImage(const EncodedImage&);
+  EncodedImage(uint8_t* buffer, size_t length, size_t size);
+
+  // TODO(nisse): Change style to timestamp(), set_timestamp(), for consistency
+  // with the VideoFrame class.
+  // Set frame timestamp (90kHz).
+  void SetTimestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
+
+  // Get frame timestamp (90kHz).
+  uint32_t Timestamp() const { return timestamp_rtp_; }
+
+  void SetEncodeTime(int64_t encode_start_ms, int64_t encode_finish_ms);
+
+  absl::optional<int> SpatialIndex() const {
+    if (spatial_index_ < 0)
+      return absl::nullopt;
+    return spatial_index_;
+  }
+  void SetSpatialIndex(absl::optional<int> spatial_index) {
+    RTC_DCHECK_GE(spatial_index.value_or(0), 0);
+    RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers);
+    spatial_index_ = spatial_index.value_or(-1);
+  }
+
+  uint32_t _encodedWidth = 0;
+  uint32_t _encodedHeight = 0;
+  // NTP time of the capture time in local timebase in milliseconds.
+  int64_t ntp_time_ms_ = 0;
+  int64_t capture_time_ms_ = 0;
+  FrameType _frameType = kVideoFrameDelta;
+  uint8_t* _buffer;
+  size_t _length;
+  size_t _size;
+  VideoRotation rotation_ = kVideoRotation_0;
+  VideoContentType content_type_ = VideoContentType::UNSPECIFIED;
+  bool _completeFrame = false;
+  int qp_ = -1;  // Quantizer value.
+
+  // When an application indicates non-zero values here, it is taken as an
+  // indication that all future frames will be constrained with those limits
+  // until the application indicates a change again.
+  PlayoutDelay playout_delay_ = {-1, -1};
+
+  struct Timing {
+    uint8_t flags = VideoSendTiming::kInvalid;
+    int64_t encode_start_ms = 0;
+    int64_t encode_finish_ms = 0;
+    int64_t packetization_finish_ms = 0;
+    int64_t pacer_exit_ms = 0;
+    int64_t network_timestamp_ms = 0;
+    int64_t network2_timestamp_ms = 0;
+    int64_t receive_start_ms = 0;
+    int64_t receive_finish_ms = 0;
+  } timing_;
+
+ private:
+  uint32_t timestamp_rtp_ = 0;
+  // -1 means not set. Use a plain int rather than optional, to keep this class
+  // copyable with memcpy.
+  int spatial_index_ = -1;
+};
+
+}  // namespace webrtc
+
+#endif  // API_VIDEO_ENCODED_IMAGE_H_
diff --git a/api/video_codecs/BUILD.gn b/api/video_codecs/BUILD.gn
index 55674d0..f2d7e9e 100644
--- a/api/video_codecs/BUILD.gn
+++ b/api/video_codecs/BUILD.gn
@@ -32,9 +32,9 @@
 
   deps = [
     "../..:webrtc_common",
-    "../../common_video",
     "../../rtc_base:checks",
     "../../rtc_base:rtc_base_approved",
+    "../video:encoded_image",
     "../video:video_bitrate_allocation",
     "../video:video_frame",
     "//third_party/abseil-cpp/absl/types:optional",
diff --git a/api/video_codecs/video_decoder.h b/api/video_codecs/video_decoder.h
index 7995fcc..569e3cf 100644
--- a/api/video_codecs/video_decoder.h
+++ b/api/video_codecs/video_decoder.h
@@ -15,9 +15,9 @@
 #include <string>
 #include <vector>
 
+#include "api/video/encoded_image.h"
 #include "api/video/video_frame.h"
 #include "api/video_codecs/video_codec.h"
-#include "common_video/include/video_frame.h"
 
 namespace webrtc {
 
diff --git a/api/video_codecs/video_encoder.h b/api/video_codecs/video_encoder.h
index 68d9b44..4a4a8b6 100644
--- a/api/video_codecs/video_encoder.h
+++ b/api/video_codecs/video_encoder.h
@@ -16,10 +16,10 @@
 #include <vector>
 
 #include "absl/types/optional.h"
+#include "api/video/encoded_image.h"
 #include "api/video/video_bitrate_allocation.h"
 #include "api/video/video_frame.h"
 #include "api/video_codecs/video_codec.h"
-#include "common_video/include/video_frame.h"
 #include "rtc_base/checks.h"
 
 namespace webrtc {