1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_SUPPORT_CC_TASK_VISION_UTILS_FRAME_BUFFER_COMMON_UTILS_H_
16 #define TENSORFLOW_LITE_SUPPORT_CC_TASK_VISION_UTILS_FRAME_BUFFER_COMMON_UTILS_H_
17 
18 #include <memory>
19 
20 #include "absl/status/status.h"
21 #include "absl/time/clock.h"
22 #include "absl/time/time.h"
23 #include "tensorflow_lite_support/cc/port/integral_types.h"
24 #include "tensorflow_lite_support/cc/port/statusor.h"
25 #include "tensorflow_lite_support/cc/task/vision/core/frame_buffer.h"
26 
27 namespace tflite {
28 namespace task {
29 namespace vision {
30 
31 constexpr int kRgbaPixelBytes = 4, kRgbPixelBytes = 3, kGrayPixelBytes = 1;
32 
33 // Miscellaneous Methods
34 // -----------------------------------------------------------------
35 
36 // Returns the frame buffer size in bytes based on the input format and
37 // dimensions. GRAY, YV12/YV21 are in the planar formats, NV12/NV21 are in the
38 // semi-planar formats with the interleaved UV planes. RGB/RGBA are in the
39 // interleaved format.
40 int GetFrameBufferByteSize(FrameBuffer::Dimension dimension,
41                            FrameBuffer::Format format);
42 
43 // Returns pixel stride info for kGRAY, kRGB, kRGBA formats.
44 tflite::support::StatusOr<int> GetPixelStrides(FrameBuffer::Format format);
45 
46 // Returns the biplanar UV raw buffer for NV12/NV21 frame buffer.
47 tflite::support::StatusOr<const uint8*> GetUvRawBuffer(
48     const FrameBuffer& buffer);
49 
50 // Returns U or V plane dimension with the given buffer `dimension` and
51 // `format`. Only supports NV12/NV21/YV12/YV21 formats. Returns
52 // InvalidArgumentError if 'dimension' is invalid or 'format' is other than the
53 // supported formats. This method assums the UV plane share the same dimension,
54 // especially for the YV12 / YV21 formats.
55 tflite::support::StatusOr<FrameBuffer::Dimension> GetUvPlaneDimension(
56     FrameBuffer::Dimension dimension, FrameBuffer::Format format);
57 
58 // Returns crop dimension based on crop start and end points.
59 FrameBuffer::Dimension GetCropDimension(int x0, int x1, int y0, int y1);
60 
61 // Validation Methods
62 // -----------------------------------------------------------------
63 
64 // Validates that the given buffer has the correct metadata. Returns error
65 // state when any buffer has missing stride info.
66 absl::Status ValidateBufferPlaneMetadata(const FrameBuffer& buffer);
67 
68 // Validates that the given buffer has the correct format for its configuration.
69 absl::Status ValidateBufferFormat(const FrameBuffer& buffer);
70 
71 // Validates that the given buffers have the correct format for their
72 // configuration.
73 absl::Status ValidateBufferFormats(const FrameBuffer& buffer1,
74                                    const FrameBuffer& buffer2);
75 
76 // Validates the given inputs for resizing `buffer`.
77 absl::Status ValidateResizeBufferInputs(const FrameBuffer& buffer,
78                                         const FrameBuffer& output_buffer);
79 
80 // Validates the given inputs for rotating `buffer`.
81 absl::Status ValidateRotateBufferInputs(const FrameBuffer& buffer,
82                                         const FrameBuffer& output_buffer,
83                                         int angle_deg);
84 
85 // Validates the given inputs for cropping `buffer`.
86 //
87 // (x0, y0) represents the top-left point of the buffer.
88 // (x1, y1) represents the bottom-right point of the buffer.
89 absl::Status ValidateCropBufferInputs(const FrameBuffer& buffer,
90                                       const FrameBuffer& output_buffer, int x0,
91                                       int y0, int x1, int y1);
92 
93 // Validates the given inputs for flipping `buffer` horizontally or vertically.
94 absl::Status ValidateFlipBufferInputs(const FrameBuffer& buffer,
95                                       const FrameBuffer& output_buffer);
96 
97 // Validates that `from_format` can be converted to `to_format`.
98 //
99 // The given formats must not be equal.
100 absl::Status ValidateConvertFormats(FrameBuffer::Format from_format,
101                                     FrameBuffer::Format to_format);
102 
103 // Creation Methods
104 // -----------------------------------------------------------------
105 
106 // Creates a FrameBuffer from raw RGBA buffer and passing arguments.
107 std::unique_ptr<FrameBuffer> CreateFromRgbaRawBuffer(
108     const uint8* input, FrameBuffer::Dimension dimension,
109     FrameBuffer::Orientation orientation = FrameBuffer::Orientation::kTopLeft,
110     absl::Time timestamp = absl::Now());
111 
112 // Creates a FrameBuffer from raw RGB buffer and passing arguments.
113 std::unique_ptr<FrameBuffer> CreateFromRgbRawBuffer(
114     const uint8* input, FrameBuffer::Dimension dimension,
115     FrameBuffer::Orientation orientation = FrameBuffer::Orientation::kTopLeft,
116     absl::Time timestamp = absl::Now());
117 
118 // Creates a FrameBuffer from raw grayscale buffer and passing arguments.
119 std::unique_ptr<FrameBuffer> CreateFromGrayRawBuffer(
120     const uint8* input, FrameBuffer::Dimension dimension,
121     FrameBuffer::Orientation orientation = FrameBuffer::Orientation::kTopLeft,
122     absl::Time timestamp = absl::Now());
123 
124 // Creates a FrameBuffer from raw YUV buffer and passing arguments.
125 tflite::support::StatusOr<std::unique_ptr<FrameBuffer>> CreateFromYuvRawBuffer(
126     const uint8* y_plane, const uint8* u_plane, const uint8* v_plane,
127     FrameBuffer::Format format, FrameBuffer::Dimension dimension,
128     int row_stride_y, int row_stride_uv, int pixel_stride_uv,
129     FrameBuffer::Orientation orientation = FrameBuffer::Orientation::kTopLeft,
130     absl::Time timestamp = absl::Now());
131 
132 // Creates an instance of FrameBuffer from raw buffer and passing arguments.
133 tflite::support::StatusOr<std::unique_ptr<FrameBuffer>> CreateFromRawBuffer(
134     const uint8* buffer, FrameBuffer::Dimension dimension,
135     FrameBuffer::Format target_format,
136     FrameBuffer::Orientation orientation = FrameBuffer::Orientation::kTopLeft,
137     absl::Time timestamp = absl::Now());
138 
139 }  // namespace vision
140 }  // namespace task
141 }  // namespace tflite
142 
143 #endif  // TENSORFLOW_LITE_SUPPORT_CC_TASK_VISION_UTILS_FRAME_BUFFER_COMMON_UTILS_H_
144