chromium/third_party/tflite_support/src/tensorflow_lite_support/cc/task/vision/utils/libyuv_frame_buffer_utils.cc

/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow_lite_support/cc/task/vision/utils/libyuv_frame_buffer_utils.h"

#include <stdint.h>

#include <memory>
#include <string>

#include "absl/status/status.h"  // from @com_google_absl
#include "absl/strings/str_cat.h"  // from @com_google_absl
#include "absl/strings/str_format.h"  // from @com_google_absl
#include "libyuv.h"  // from @libyuv
#include "libyuv/convert_argb.h"  // from @libyuv
#include "libyuv/scale.h"  // from @libyuv
#include "tensorflow_lite_support/cc/common.h"
#include "tensorflow_lite_support/cc/port/integral_types.h"
#include "tensorflow_lite_support/cc/port/status_macros.h"
#include "tensorflow_lite_support/cc/port/statusor.h"
#include "tensorflow_lite_support/cc/task/vision/core/frame_buffer.h"
#include "tensorflow_lite_support/cc/task/vision/utils/frame_buffer_common_utils.h"

namespace tflite {
namespace task {
namespace vision {

StatusCode;
CreateStatusWithPayload;
TfLiteSupportStatus;

namespace {

// Converts NV12 `buffer` to the `output_buffer` of the target color space.
// Supported output format includes RGB24 and YV21.
absl::Status ConvertFromNv12(const FrameBuffer& buffer,
                             FrameBuffer* output_buffer) {}

// Converts NV21 `buffer` into the `output_buffer` of the target color space.
// Supported output format includes RGB24 and YV21.
absl::Status ConvertFromNv21(const FrameBuffer& buffer,
                             FrameBuffer* output_buffer) {}

// Converts YV12/YV21 `buffer` to the `output_buffer` of the target color space.
// Supported output format includes RGB24, NV12, and NV21.
absl::Status ConvertFromYv(const FrameBuffer& buffer,
                           FrameBuffer* output_buffer) {}

// Resizes YV12/YV21 `buffer` to the target `output_buffer`.
absl::Status ResizeYv(
    const FrameBuffer& buffer, FrameBuffer* output_buffer,
    libyuv::FilterMode interpolation = libyuv::FilterMode::kFilterBilinear) {}

// Resizes NV12/NV21 `buffer` to the target `output_buffer`.
absl::Status ResizeNv(
    const FrameBuffer& buffer, FrameBuffer* output_buffer,
    libyuv::FilterMode interpolation = libyuv::FilterMode::kFilterBilinear) {}

// Converts `buffer` to libyuv ARGB format and stores the conversion result
// in `dest_argb`.
absl::Status ConvertRgbToArgb(const FrameBuffer& buffer, uint8* dest_argb,
                              int dest_stride_argb) {}

// Converts `src_argb` in libyuv ARGB format to FrameBuffer::kRGB format and
// stores the conversion result in `output_buffer`.
absl::Status ConvertArgbToRgb(uint8* src_argb, int src_stride_argb,
                              FrameBuffer* output_buffer) {}

// Converts `buffer` in FrameBuffer::kRGBA format to libyuv ARGB (BGRA in
// memory) format and stores the conversion result in `dest_argb`.
absl::Status ConvertRgbaToArgb(const FrameBuffer& buffer, uint8* dest_argb,
                               int dest_stride_argb) {}

// Converts kRGB `buffer` to the `output_buffer` of the target color space.
absl::Status ConvertFromRgb(const FrameBuffer& buffer,
                            FrameBuffer* output_buffer) {}

// Converts kRGBA `buffer` to the `output_buffer` of the target color space.
absl::Status ConvertFromRgba(const FrameBuffer& buffer,
                             FrameBuffer* output_buffer) {}

// Returns libyuv rotation based on counter-clockwise angle_deg.
libyuv::RotationMode GetLibyuvRotationMode(int angle_deg) {}

absl::Status RotateRgba(const FrameBuffer& buffer, int angle_deg,
                        FrameBuffer* output_buffer) {}

absl::Status RotateRgb(const FrameBuffer& buffer, int angle_deg,
                       FrameBuffer* output_buffer) {}

absl::Status RotateGray(const FrameBuffer& buffer, int angle_deg,
                        FrameBuffer* output_buffer) {}

// Rotates YV12/YV21 frame buffer.
absl::Status RotateYv(const FrameBuffer& buffer, int angle_deg,
                      FrameBuffer* output_buffer) {}

// Rotates NV12/NV21 frame buffer.
// TODO(b/152097364): Refactor NV12/NV21 rotation after libyuv explicitly
// support that.
absl::Status RotateNv(const FrameBuffer& buffer, int angle_deg,
                      FrameBuffer* output_buffer) {}

// This method only supports kGRAY, kRGB, and kRGBA format.
absl::Status FlipPlaneVertically(const FrameBuffer& buffer,
                                 FrameBuffer* output_buffer) {}

// This method only supports kGRAY, kRGBA, and kRGB formats.
absl::Status CropPlane(const FrameBuffer& buffer, int x0, int y0, int x1,
                       int y1, FrameBuffer* output_buffer) {}

// Crops NV12/NV21 FrameBuffer to the subregion defined by the top left pixel
// position (x0, y0) and the bottom right pixel position (x1, y1).
absl::Status CropNv(const FrameBuffer& buffer, int x0, int y0, int x1, int y1,
                    FrameBuffer* output_buffer) {}

// Crops YV12/YV21 FrameBuffer to the subregion defined by the top left pixel
// position (x0, y0) and the bottom right pixel position (x1, y1).
absl::Status CropYv(const FrameBuffer& buffer, int x0, int y0, int x1, int y1,
                    FrameBuffer* output_buffer) {}

absl::Status CropResizeYuv(const FrameBuffer& buffer, int x0, int y0, int x1,
                           int y1, FrameBuffer* output_buffer) {}

absl::Status FlipHorizontallyRgba(const FrameBuffer& buffer,
                                  FrameBuffer* output_buffer) {}

// Flips `buffer` horizontally and store the result in `output_buffer`. This
// method assumes all buffers have pixel stride equals to 1.
absl::Status FlipHorizontallyPlane(const FrameBuffer& buffer,
                                   FrameBuffer* output_buffer) {}

absl::Status ResizeRgb(
    const FrameBuffer& buffer, FrameBuffer* output_buffer,
    libyuv::FilterMode interpolation = libyuv::FilterMode::kFilterBilinear) {}

// Horizontally flip `buffer` and store the result in `output_buffer`.
absl::Status FlipHorizontallyRgb(const FrameBuffer& buffer,
                                 FrameBuffer* output_buffer) {}

absl::Status ResizeRgba(
    const FrameBuffer& buffer, FrameBuffer* output_buffer,
    libyuv::FilterMode interpolation = libyuv::FilterMode::kFilterBilinear) {}

// Flips NV12/NV21 FrameBuffer horizontally.
absl::Status FlipHorizontallyNv(const FrameBuffer& buffer,
                                FrameBuffer* output_buffer) {}

// Flips YV12/YV21 FrameBuffer horizontally.
absl::Status FlipHorizontallyYv(const FrameBuffer& buffer,
                                FrameBuffer* output_buffer) {}

// Flips NV12/NV21 FrameBuffer vertically.
absl::Status FlipVerticallyNv(const FrameBuffer& buffer,
                              FrameBuffer* output_buffer) {}

// Flips NV12/NV21 FrameBuffer vertically.
absl::Status FlipVerticallyYv(const FrameBuffer& buffer,
                              FrameBuffer* output_buffer) {}

// Resize `buffer` to metadata defined in `output_buffer`. This
// method assumes buffer has pixel stride equals to 1 (grayscale equivalent).
absl::Status ResizeGray(
    const FrameBuffer& buffer, FrameBuffer* output_buffer,
    libyuv::FilterMode interpolation = libyuv::FilterMode::kFilterBilinear) {}

// This method only supports kGRAY, kRGBA, and kRGB formats.
absl::Status CropResize(const FrameBuffer& buffer, int x0, int y0, int x1,
                        int y1, FrameBuffer* output_buffer) {}

}  // namespace

absl::Status LibyuvFrameBufferUtils::Crop(const FrameBuffer& buffer, int x0,
                                          int y0, int x1, int y1,
                                          FrameBuffer* output_buffer) {}

absl::Status LibyuvFrameBufferUtils::Resize(const FrameBuffer& buffer,
                                            FrameBuffer* output_buffer) {}

absl::Status LibyuvFrameBufferUtils::ResizeNearestNeighbor(
    const FrameBuffer& buffer, FrameBuffer* output_buffer) {}

absl::Status LibyuvFrameBufferUtils::Rotate(const FrameBuffer& buffer,
                                            int angle_deg,
                                            FrameBuffer* output_buffer) {}

absl::Status LibyuvFrameBufferUtils::FlipHorizontally(
    const FrameBuffer& buffer, FrameBuffer* output_buffer) {}

absl::Status LibyuvFrameBufferUtils::FlipVertically(
    const FrameBuffer& buffer, FrameBuffer* output_buffer) {}

absl::Status LibyuvFrameBufferUtils::Convert(const FrameBuffer& buffer,
                                             FrameBuffer* output_buffer) {}

}  // namespace vision
}  // namespace task
}  // namespace tflite