blob: 5efe2c8c6279e1a9cd243590816a3be01d3c1869 [file] [log] [blame]
/* Copyright 2017 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef HAL_USB_IMAGE_PROCESSOR_H_
#define HAL_USB_IMAGE_PROCESSOR_H_
#include <memory>
#include <string>
// FourCC pixel formats (defined as V4L2_PIX_FMT_*).
#include <linux/videodev2.h>
// Declarations of HAL_PIXEL_FORMAT_XXX.
#include <system/graphics.h>
#include <base/memory/ptr_util.h>
#include <camera/camera_metadata.h>
#include "cros-camera/jpeg_decode_accelerator.h"
#include "cros-camera/jpeg_encode_accelerator.h"
#include "hal/usb/frame_buffer.h"
namespace cros {
// V4L2_PIX_FMT_YVU420(YV12) in ImageProcessor has alignment requirement.
// The stride of Y, U, and V planes should a multiple of 16 pixels.
class ImageProcessor {
public:
ImageProcessor();
~ImageProcessor();
// Calculate the output buffer size when converting to the specified pixel
// format according to fourcc, width, height, and stride of |frame|.
// Return 0 on error.
size_t GetConvertedSize(const FrameBuffer& frame);
// Convert format from |in_frame.fourcc| to |out_frame->fourcc|. Caller should
// fill |data|, |buffer_size|, |width|, and |height| of |out_frame|. The
// function will fill |out_frame->data_size|. Return non-zero error code on
// failure; return 0 on success.
int ConvertFormat(const android::CameraMetadata& metadata,
const FrameBuffer& in_frame,
FrameBuffer* out_frame);
// Scale image size according to |in_frame| and |out_frame|. Only support
// V4L2_PIX_FMT_YUV420 format. Caller should fill |data|, |width|, |height|,
// and |buffer_size| of |out_frame|. The function will fill |data_size| and
// |fourcc| of |out_frame|.
int Scale(const FrameBuffer& in_frame, FrameBuffer* out_frame);
// Crop and rotate image size according to |in_frame| and |out_frame|. Only
// support V4L2_PIX_FMT_YUV420 format. Caller should fill |data|, |width|,
// |height|, and |buffer_size| of |out_frame|. The function will fill
// |data_size| and |fourcc| of |out_frame|. |rotate_degree| should be 90 or
// 270.
int CropAndRotate(const FrameBuffer& in_frame,
FrameBuffer* out_frame,
int rotate_degree);
private:
bool ConvertToJpeg(const android::CameraMetadata& metadata,
const FrameBuffer& in_frame,
FrameBuffer* out_frame);
void InsertJpegBlob(FrameBuffer* out_frame, uint32_t jpeg_data_size);
int MJPGToI420(const FrameBuffer& in_frame, FrameBuffer* out_frame);
std::unique_ptr<JpegEncodeAccelerator> jpeg_encoder_;
// Whether |jpeg_encoder_| is started and ready to be used.
bool jpeg_encoder_started_;
// Used for jpeg decode accelerator.
std::unique_ptr<JpegDecodeAccelerator> jda_;
// Indicate if we can use the jpeg decoder accelerator or not.
bool jda_available_;
// If test mode enabled or not.
bool test_enabled_;
};
} // namespace cros
#endif // HAL_USB_IMAGE_PROCESSOR_H_