1c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen/*
2c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen * Copyright 2016 The Android Open Source Project
3c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen *
4c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen * Licensed under the Apache License, Version 2.0 (the "License");
5c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen * you may not use this file except in compliance with the License.
6c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen * You may obtain a copy of the License at
7c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen *
8c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen *      http://www.apache.org/licenses/LICENSE-2.0
9c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen *
10c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen * Unless required by applicable law or agreed to in writing, software
11c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen * distributed under the License is distributed on an "AS IS" BASIS,
12c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen * See the License for the specific language governing permissions and
14c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen * limitations under the License.
15c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen */
16c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
173841a7f4951fe1498bf5ba88466def3ea18f8867Ari Hausman-Cohen#include "stream_format.h"
18c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
19c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen#include <linux/videodev2.h>
20c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
21ef52310613342dd51a928af92e3287f6783ae8c7Ari Hausman-Cohen#include <system/graphics.h>
22ef52310613342dd51a928af92e3287f6783ae8c7Ari Hausman-Cohen
2328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan#include "arc/image_processor.h"
243841a7f4951fe1498bf5ba88466def3ea18f8867Ari Hausman-Cohen#include "common.h"
25c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
26c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohennamespace v4l2_camera_hal {
27c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
2828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathanusing arc::SupportedFormat;
2928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathanusing arc::SupportedFormats;
3028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan
3128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathanstatic const std::vector<uint32_t> GetSupportedFourCCs() {
3228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  // The preference of supported fourccs in the list is from high to low.
3328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  static const std::vector<uint32_t> kSupportedFourCCs = {V4L2_PIX_FMT_YUYV,
3428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan                                                          V4L2_PIX_FMT_MJPEG};
3528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  return kSupportedFourCCs;
3628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan}
3728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan
38ef52310613342dd51a928af92e3287f6783ae8c7Ari Hausman-CohenStreamFormat::StreamFormat(int format, uint32_t width, uint32_t height)
39c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    // TODO(b/30000211): multiplanar support.
40c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    : type_(V4L2_BUF_TYPE_VIDEO_CAPTURE),
41ef52310613342dd51a928af92e3287f6783ae8c7Ari Hausman-Cohen      v4l2_pixel_format_(StreamFormat::HalToV4L2PixelFormat(format)),
42ef52310613342dd51a928af92e3287f6783ae8c7Ari Hausman-Cohen      width_(width),
43ef52310613342dd51a928af92e3287f6783ae8c7Ari Hausman-Cohen      height_(height),
44c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      bytes_per_line_(0),
45ad6fe2b033997ab0401b0a519b18cea6e957dac0Ari Hausman-Cohen      min_buffer_size_(0) {}
46c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
47c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-CohenStreamFormat::StreamFormat(const v4l2_format& format)
48c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    : type_(format.type),
49c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      // TODO(b/30000211): multiplanar support.
50c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      v4l2_pixel_format_(format.fmt.pix.pixelformat),
51c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      width_(format.fmt.pix.width),
52c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      height_(format.fmt.pix.height),
53c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      bytes_per_line_(format.fmt.pix.bytesperline),
54ad6fe2b033997ab0401b0a519b18cea6e957dac0Ari Hausman-Cohen      min_buffer_size_(format.fmt.pix.sizeimage) {}
55c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
5628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth SwaminathanStreamFormat::StreamFormat(const arc::SupportedFormat& format)
5728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    : type_(V4L2_BUF_TYPE_VIDEO_CAPTURE),
5828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      v4l2_pixel_format_(format.fourcc),
5928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      width_(format.width),
6028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      height_(format.height),
6128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      bytes_per_line_(0),
6228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      min_buffer_size_(0) {}
6328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan
64c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohenvoid StreamFormat::FillFormatRequest(v4l2_format* format) const {
65c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  memset(format, 0, sizeof(*format));
66c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  format->type = type_;
67c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  format->fmt.pix.pixelformat = v4l2_pixel_format_;
68c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  format->fmt.pix.width = width_;
69c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  format->fmt.pix.height = height_;
70c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  // Bytes per line and min buffer size are outputs set by the driver,
71c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  // not part of the request.
72c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen}
73c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
74c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-CohenFormatCategory StreamFormat::Category() const {
75c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  switch (v4l2_pixel_format_) {
76c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    case V4L2_PIX_FMT_JPEG:
77c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      return kFormatCategoryStalling;
780457010119076bef93388fa7dcf5568e9b1f60cfAri Hausman-Cohen    case V4L2_PIX_FMT_YUV420:  // Fall through.
7928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case V4L2_PIX_FMT_BGR32:
80c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      return kFormatCategoryNonStalling;
81c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    default:
82c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      // Note: currently no supported RAW formats.
83c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      return kFormatCategoryUnknown;
84c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  }
85c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen}
86c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
87c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohenbool StreamFormat::operator==(const StreamFormat& other) const {
88c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  // Used to check that a requested format was actually set, so
89c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  // don't compare bytes per line or min buffer size.
90c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  return (type_ == other.type_ &&
91c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen          v4l2_pixel_format_ == other.v4l2_pixel_format_ &&
92c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen          width_ == other.width_ && height_ == other.height_);
93c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen}
94c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
95c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohenbool StreamFormat::operator!=(const StreamFormat& other) const {
96c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  return !(*this == other);
97c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen}
98c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
99c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohenint StreamFormat::V4L2ToHalPixelFormat(uint32_t v4l2_pixel_format) {
100c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  // Translate V4L2 format to HAL format.
101c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  switch (v4l2_pixel_format) {
10228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case V4L2_PIX_FMT_BGR32:
10328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return HAL_PIXEL_FORMAT_RGBA_8888;
104c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    case V4L2_PIX_FMT_JPEG:
10528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return HAL_PIXEL_FORMAT_BLOB;
10628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case V4L2_PIX_FMT_NV21:
10728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return HAL_PIXEL_FORMAT_YCrCb_420_SP;
108c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    case V4L2_PIX_FMT_YUV420:
10928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return HAL_PIXEL_FORMAT_YCbCr_420_888;
11028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case V4L2_PIX_FMT_YUYV:
11128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return HAL_PIXEL_FORMAT_YCbCr_422_I;
11228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case V4L2_PIX_FMT_YVU420:
11328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return HAL_PIXEL_FORMAT_YV12;
114c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    default:
115c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      // Unrecognized format.
1160457010119076bef93388fa7dcf5568e9b1f60cfAri Hausman-Cohen      HAL_LOGV("Unrecognized v4l2 pixel format %u", v4l2_pixel_format);
117c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      break;
118c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  }
11928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  return -1;
120c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen}
121c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
122c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohenuint32_t StreamFormat::HalToV4L2PixelFormat(int hal_pixel_format) {
123c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  switch (hal_pixel_format) {
12428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case HAL_PIXEL_FORMAT_BLOB:
12528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return V4L2_PIX_FMT_JPEG;
12628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:  // Fall-through
1270457010119076bef93388fa7dcf5568e9b1f60cfAri Hausman-Cohen    case HAL_PIXEL_FORMAT_RGBA_8888:
12828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return V4L2_PIX_FMT_BGR32;
129c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    case HAL_PIXEL_FORMAT_YCbCr_420_888:
13028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      // This is a flexible YUV format that depends on platform. Different
13128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      // platform may have different format. It can be YVU420 or NV12. Now we
13228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      // return YVU420 first.
13328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      // TODO(): call drm_drv.get_fourcc() to get correct format.
13428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return V4L2_PIX_FMT_YUV420;
13528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case HAL_PIXEL_FORMAT_YCbCr_422_I:
13628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return V4L2_PIX_FMT_YUYV;
13728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
13828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return V4L2_PIX_FMT_NV21;
13928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    case HAL_PIXEL_FORMAT_YV12:
14028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return V4L2_PIX_FMT_YVU420;
141c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen    default:
14228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      HAL_LOGV("Pixel format 0x%x is unsupported.", hal_pixel_format);
143c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen      break;
144c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen  }
14528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  return -1;
14628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan}
14728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan
14828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan// Copy the qualified format into out_format and return true if there is a
14928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan// proper and fitting format in the given format lists.
15028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathanbool StreamFormat::FindBestFitFormat(const SupportedFormats& supported_formats,
15128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan                                     const SupportedFormats& qualified_formats,
15228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan                                     uint32_t fourcc, uint32_t width,
15328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan                                     uint32_t height,
15428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan                                     SupportedFormat* out_format) {
15528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  // Match exact format and resolution if possible.
15628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  for (const auto& format : supported_formats) {
15728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    if (format.fourcc == fourcc && format.width == width &&
15828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan        format.height == height) {
15928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      if (out_format != NULL) {
16028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan        *out_format = format;
16128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      }
16228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return true;
16328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    }
16428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  }
16528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  // All conversions will be done through CachedFrame for now, which will
16628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  // immediately convert the qualified format into YU12 (YUV420). We check
16728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  // here that the conversion between YU12 and |fourcc| is supported.
16828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  if (!arc::ImageProcessor::SupportsConversion(V4L2_PIX_FMT_YUV420, fourcc)) {
16928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    HAL_LOGE("Conversion between YU12 and 0x%x not supported.", fourcc);
17028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    return false;
17128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  }
17228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan
17328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  // Choose the qualified format with a matching resolution.
17428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  for (const auto& format : qualified_formats) {
17528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    if (format.width == width && format.height == height) {
17628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      if (out_format != NULL) {
17728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan        *out_format = format;
17828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      }
17928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return true;
18028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    }
18128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  }
18228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  return false;
18328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan}
18428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan
18528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan// Copy corresponding format into out_format and return true by matching
18628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan// resolution |width|x|height| in |formats|.
18728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathanbool StreamFormat::FindFormatByResolution(const SupportedFormats& formats,
18828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan                                          uint32_t width, uint32_t height,
18928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan                                          SupportedFormat* out_format) {
19028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  for (const auto& format : formats) {
19128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    if (format.width == width && format.height == height) {
19228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      if (out_format != NULL) {
19328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan        *out_format = format;
19428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      }
19528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      return true;
19628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    }
19728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  }
19828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  return false;
19928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan}
20028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan
20128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth SwaminathanSupportedFormats StreamFormat::GetQualifiedFormats(
20228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    const SupportedFormats& supported_formats) {
20328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  // The preference of supported fourccs in the list is from high to low.
20428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  const std::vector<uint32_t> supported_fourccs = GetSupportedFourCCs();
20528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  SupportedFormats qualified_formats;
20628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  for (const auto& supported_fourcc : supported_fourccs) {
20728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    for (const auto& supported_format : supported_formats) {
20828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      if (supported_format.fourcc != supported_fourcc) {
20928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan        continue;
21028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      }
21128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan
21228e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      // Skip if |qualified_formats| already has the same resolution with a more
21328e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      // preferred fourcc.
21428e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      if (FindFormatByResolution(qualified_formats, supported_format.width,
21528e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan                                 supported_format.height, NULL)) {
21628e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan        continue;
21728e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      }
21828e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan      qualified_formats.push_back(supported_format);
21928e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan    }
22028e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  }
22128e0f76353e06b24b7dae54e636dbb5ddc64d1a5Prashanth Swaminathan  return qualified_formats;
222c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen}
223c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen
224c17fd09be838b093d3de3033c3a65a75b1813e19Ari Hausman-Cohen}  // namespace v4l2_camera_hal
225