Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | #include "StreamFormat.h" |
| 18 | |
| 19 | #include <linux/videodev2.h> |
| 20 | |
| 21 | #include "Common.h" |
| 22 | #include "Stream.h" |
| 23 | |
| 24 | namespace v4l2_camera_hal { |
| 25 | |
| 26 | StreamFormat::StreamFormat(const default_camera_hal::Stream& stream) |
| 27 | // TODO(b/30000211): multiplanar support. |
| 28 | : type_(V4L2_BUF_TYPE_VIDEO_CAPTURE), |
| 29 | v4l2_pixel_format_( |
| 30 | StreamFormat::HalToV4L2PixelFormat(stream.getFormat())), |
| 31 | width_(stream.getWidth()), |
| 32 | height_(stream.getHeight()), |
| 33 | bytes_per_line_(0), |
| 34 | min_buffer_size_(0) { |
| 35 | HAL_LOG_ENTER(); |
| 36 | } |
| 37 | |
| 38 | StreamFormat::StreamFormat(const v4l2_format& format) |
| 39 | : type_(format.type), |
| 40 | // TODO(b/30000211): multiplanar support. |
| 41 | v4l2_pixel_format_(format.fmt.pix.pixelformat), |
| 42 | width_(format.fmt.pix.width), |
| 43 | height_(format.fmt.pix.height), |
| 44 | bytes_per_line_(format.fmt.pix.bytesperline), |
| 45 | min_buffer_size_(format.fmt.pix.sizeimage) { |
| 46 | HAL_LOG_ENTER(); |
| 47 | } |
| 48 | |
| 49 | void StreamFormat::FillFormatRequest(v4l2_format* format) const { |
| 50 | HAL_LOG_ENTER(); |
| 51 | |
| 52 | memset(format, 0, sizeof(*format)); |
| 53 | format->type = type_; |
| 54 | format->fmt.pix.pixelformat = v4l2_pixel_format_; |
| 55 | format->fmt.pix.width = width_; |
| 56 | format->fmt.pix.height = height_; |
| 57 | // Bytes per line and min buffer size are outputs set by the driver, |
| 58 | // not part of the request. |
| 59 | } |
| 60 | |
| 61 | FormatCategory StreamFormat::Category() const { |
| 62 | switch (v4l2_pixel_format_) { |
| 63 | case V4L2_PIX_FMT_JPEG: |
| 64 | return kFormatCategoryStalling; |
| 65 | case V4L2_PIX_FMT_YUV420: |
| 66 | return kFormatCategoryNonStalling; |
| 67 | default: |
| 68 | // Note: currently no supported RAW formats. |
| 69 | return kFormatCategoryUnknown; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | bool StreamFormat::operator==(const StreamFormat& other) const { |
| 74 | // Used to check that a requested format was actually set, so |
| 75 | // don't compare bytes per line or min buffer size. |
| 76 | return (type_ == other.type_ && |
| 77 | v4l2_pixel_format_ == other.v4l2_pixel_format_ && |
| 78 | width_ == other.width_ && height_ == other.height_); |
| 79 | } |
| 80 | |
| 81 | bool StreamFormat::operator!=(const StreamFormat& other) const { |
| 82 | return !(*this == other); |
| 83 | } |
| 84 | |
| 85 | int StreamFormat::V4L2ToHalPixelFormat(uint32_t v4l2_pixel_format) { |
| 86 | // Translate V4L2 format to HAL format. |
| 87 | int hal_pixel_format = -1; |
| 88 | switch (v4l2_pixel_format) { |
| 89 | case V4L2_PIX_FMT_JPEG: |
| 90 | hal_pixel_format = HAL_PIXEL_FORMAT_BLOB; |
| 91 | break; |
| 92 | case V4L2_PIX_FMT_YUV420: |
| 93 | hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_420_888; |
| 94 | break; |
| 95 | default: |
| 96 | // Unrecognized format. |
| 97 | break; |
| 98 | } |
| 99 | return hal_pixel_format; |
| 100 | } |
| 101 | |
| 102 | uint32_t StreamFormat::HalToV4L2PixelFormat(int hal_pixel_format) { |
| 103 | // Translate HAL format to V4L2 format. |
| 104 | uint32_t v4l2_pixel_format = 0; |
| 105 | switch (hal_pixel_format) { |
| 106 | case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: // fall-through. |
| 107 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
| 108 | v4l2_pixel_format = V4L2_PIX_FMT_YUV420; |
| 109 | break; |
| 110 | case HAL_PIXEL_FORMAT_BLOB: |
| 111 | v4l2_pixel_format = V4L2_PIX_FMT_JPEG; |
| 112 | break; |
| 113 | default: |
| 114 | // Unrecognized format. |
| 115 | break; |
| 116 | } |
| 117 | return v4l2_pixel_format; |
| 118 | } |
| 119 | |
| 120 | } // namespace v4l2_camera_hal |