1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef V4L2_CAMERA_HAL_STREAM_FORMAT_H_
18 #define V4L2_CAMERA_HAL_STREAM_FORMAT_H_
19 
20 #include <string.h>
21 
22 #include <linux/videodev2.h>
23 
24 #include "arc/common_types.h"
25 #include "common.h"
26 
27 namespace v4l2_camera_hal {
28 
29 enum FormatCategory {
30   kFormatCategoryRaw,
31   kFormatCategoryStalling,
32   kFormatCategoryNonStalling,
33   kFormatCategoryUnknown,
34 };
35 
36 class StreamFormat {
37  public:
38   StreamFormat(int format, uint32_t width, uint32_t height);
39   StreamFormat(const v4l2_format& format);
40   StreamFormat(const arc::SupportedFormat& format);
41   virtual ~StreamFormat() = default;
42   // Only uint32_t members, use default generated copy and assign.
43 
44   void FillFormatRequest(v4l2_format* format) const;
45   FormatCategory Category() const;
46 
47   // Accessors.
type()48   inline uint32_t type() const { return type_; };
width()49   inline uint32_t width() const { return width_; };
height()50   inline uint32_t height() const { return height_; };
v4l2_pixel_format()51   inline uint32_t v4l2_pixel_format() const { return v4l2_pixel_format_; }
bytes_per_line()52   inline uint32_t bytes_per_line() const { return bytes_per_line_; };
53 
54   bool operator==(const StreamFormat& other) const;
55   bool operator!=(const StreamFormat& other) const;
56 
57   // HAL <-> V4L2 conversions
58   // Returns 0 for unrecognized.
59   static uint32_t HalToV4L2PixelFormat(int hal_pixel_format);
60   // Returns -1 for unrecognized.
61   static int V4L2ToHalPixelFormat(uint32_t v4l2_pixel_format);
62 
63   // ARC++ SupportedFormat Helpers
64   static bool FindBestFitFormat(const arc::SupportedFormats& supported_formats,
65                                 const arc::SupportedFormats& qualified_formats,
66                                 uint32_t fourcc, uint32_t width,
67                                 uint32_t height,
68                                 arc::SupportedFormat* out_format);
69   static bool FindFormatByResolution(const arc::SupportedFormats& formats,
70                                      uint32_t width, uint32_t height,
71                                      arc::SupportedFormat* out_format);
72   static arc::SupportedFormats GetQualifiedFormats(
73       const arc::SupportedFormats& supported_formats);
74 
75  private:
76   uint32_t type_;
77   uint32_t v4l2_pixel_format_;
78   uint32_t width_;
79   uint32_t height_;
80   uint32_t bytes_per_line_;
81   uint32_t min_buffer_size_;
82 };
83 
84 }  // namespace v4l2_camera_hal
85 
86 #endif  // V4L2_CAMERA_HAL_STREAM_FORMAT_H_
87