diff options
author | David Staessens <dstaessens@google.com> | 2021-05-12 05:54:53 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-05-12 05:54:53 +0000 |
commit | 7fea8352212532610e26304974f19c4063baa694 (patch) | |
tree | 05e2675cb35d1d4482163027680e9fcf6c21351a | |
parent | abd65cbb89ef75b66e29e00c1497b5f3102247fa (diff) | |
parent | 021e7a59baf64a44c3966e97c38f9c074abd8742 (diff) | |
download | v4l2_codec2-7fea8352212532610e26304974f19c4063baa694.tar.gz |
v4l2_codec2: Merge VideoFrame into VideoPixelFormat. am: 021e7a59ba
Original change: https://googleplex-android-review.googlesource.com/c/platform/external/v4l2_codec2/+/14511933
Change-Id: Ie84ba383de02ef538388d7e44abc7ad6019e93bf
-rw-r--r-- | accel/Android.bp | 1 | ||||
-rw-r--r-- | accel/video_frame.cc | 223 | ||||
-rw-r--r-- | accel/video_frame.h | 77 | ||||
-rw-r--r-- | accel/video_frame_layout.cc | 44 | ||||
-rw-r--r-- | accel/video_frame_layout.h | 2 | ||||
-rw-r--r-- | accel/video_pixel_format.cc | 255 | ||||
-rw-r--r-- | accel/video_pixel_format.h | 34 | ||||
-rw-r--r-- | common/V4L2Device.cpp | 12 | ||||
-rw-r--r-- | common/include/v4l2_codec2/common/V4L2Device.h | 1 | ||||
-rw-r--r-- | components/V4L2Encoder.cpp | 5 |
10 files changed, 297 insertions, 357 deletions
diff --git a/accel/Android.bp b/accel/Android.bp index c9e6ed3..e104997 100644 --- a/accel/Android.bp +++ b/accel/Android.bp @@ -14,7 +14,6 @@ cc_library { srcs: [ "color_plane_layout.cc", "fourcc.cc", - "video_frame.cc", "video_frame_layout.cc", "video_pixel_format.cc", ], diff --git a/accel/video_frame.cc b/accel/video_frame.cc deleted file mode 100644 index 3009fb4..0000000 --- a/accel/video_frame.cc +++ /dev/null @@ -1,223 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. -// Note: ported from Chromium commit head: 602bc8fa60fa -// Note: only necessary functions are ported. -// Note: some shared memory-related functionality here is no longer present in -// Chromium. - -#include "video_frame.h" - -#include "base/bits.h" -#include "base/logging.h" -#include "base/stl_util.h" - -namespace media { - -// If it is required to allocate aligned to multiple-of-two size overall for the -// frame of pixel |format|. -static bool RequiresEvenSizeAllocation(VideoPixelFormat format) { - switch (format) { - case PIXEL_FORMAT_ARGB: - case PIXEL_FORMAT_XRGB: - case PIXEL_FORMAT_RGB24: - case PIXEL_FORMAT_Y16: - case PIXEL_FORMAT_ABGR: - case PIXEL_FORMAT_XBGR: - case PIXEL_FORMAT_XR30: - case PIXEL_FORMAT_XB30: - case PIXEL_FORMAT_BGRA: - return false; - case PIXEL_FORMAT_NV12: - case PIXEL_FORMAT_NV21: - case PIXEL_FORMAT_I420: - case PIXEL_FORMAT_MJPEG: - case PIXEL_FORMAT_YUY2: - case PIXEL_FORMAT_YV12: - case PIXEL_FORMAT_I422: - case PIXEL_FORMAT_I444: - case PIXEL_FORMAT_YUV420P9: - case PIXEL_FORMAT_YUV422P9: - case PIXEL_FORMAT_YUV444P9: - case PIXEL_FORMAT_YUV420P10: - case PIXEL_FORMAT_YUV422P10: - case PIXEL_FORMAT_YUV444P10: - case PIXEL_FORMAT_YUV420P12: - case PIXEL_FORMAT_YUV422P12: - case PIXEL_FORMAT_YUV444P12: - case PIXEL_FORMAT_I420A: - case PIXEL_FORMAT_P016LE: - return true; - case PIXEL_FORMAT_UNKNOWN: - break; - } - NOTREACHED() << "Unsupported video frame format: " << format; - return false; -} - -// static -size_t VideoFrame::NumPlanes(VideoPixelFormat format) { - return VideoFrameLayout::NumPlanes(format); -} - -// static -size_t VideoFrame::AllocationSize(VideoPixelFormat format, - const android::ui::Size& coded_size) { - size_t total = 0; - for (size_t i = 0; i < NumPlanes(format); ++i) { - android::ui::Size plane_size = PlaneSize(format, i, coded_size); - total += (plane_size.width * plane_size.height); - } - - return total; -} - -// static -android::ui::Size VideoFrame::PlaneSize(VideoPixelFormat format, - size_t plane, - const android::ui::Size& coded_size) { - DCHECK(IsValidPlane(plane, format)); - - int width = coded_size.width; - int height = coded_size.height; - if (RequiresEvenSizeAllocation(format)) { - // Align to multiple-of-two size overall. This ensures that non-subsampled - // planes can be addressed by pixel with the same scaling as the subsampled - // planes. - width = base::bits::Align(width, 2); - height = base::bits::Align(height, 2); - } - - const android::ui::Size subsample = SampleSize(format, plane); - DCHECK(width % subsample.width == 0); - DCHECK(height % subsample.height == 0); - return android::ui::Size(BytesPerElement(format, plane) * width / subsample.width, - height / subsample.height); -} - -// static -int VideoFrame::PlaneHorizontalBitsPerPixel(VideoPixelFormat format, - size_t plane) { - DCHECK(IsValidPlane(plane, format)); - const int bits_per_element = 8 * BytesPerElement(format, plane); - const int horiz_pixels_per_element = SampleSize(format, plane).width; - DCHECK_EQ(bits_per_element % horiz_pixels_per_element, 0); - return bits_per_element / horiz_pixels_per_element; -} - -// static -int VideoFrame::PlaneBitsPerPixel(VideoPixelFormat format, size_t plane) { - DCHECK(IsValidPlane(plane, format)); - return PlaneHorizontalBitsPerPixel(format, plane) / - SampleSize(format, plane).height; -} - -// static -int VideoFrame::BytesPerElement(VideoPixelFormat format, size_t plane) { - DCHECK(IsValidPlane(format, plane)); - switch (format) { - case PIXEL_FORMAT_ARGB: - case PIXEL_FORMAT_BGRA: - case PIXEL_FORMAT_XRGB: - case PIXEL_FORMAT_ABGR: - case PIXEL_FORMAT_XBGR: - case PIXEL_FORMAT_XR30: - case PIXEL_FORMAT_XB30: - return 4; - case PIXEL_FORMAT_RGB24: - return 3; - case PIXEL_FORMAT_Y16: - case PIXEL_FORMAT_YUY2: - case PIXEL_FORMAT_YUV420P9: - case PIXEL_FORMAT_YUV422P9: - case PIXEL_FORMAT_YUV444P9: - case PIXEL_FORMAT_YUV420P10: - case PIXEL_FORMAT_YUV422P10: - case PIXEL_FORMAT_YUV444P10: - case PIXEL_FORMAT_YUV420P12: - case PIXEL_FORMAT_YUV422P12: - case PIXEL_FORMAT_YUV444P12: - case PIXEL_FORMAT_P016LE: - return 2; - case PIXEL_FORMAT_NV12: - case PIXEL_FORMAT_NV21: { - static const int bytes_per_element[] = {1, 2}; - DCHECK_LT(plane, base::size(bytes_per_element)); - return bytes_per_element[plane]; - } - case PIXEL_FORMAT_YV12: - case PIXEL_FORMAT_I420: - case PIXEL_FORMAT_I422: - case PIXEL_FORMAT_I420A: - case PIXEL_FORMAT_I444: - return 1; - case PIXEL_FORMAT_MJPEG: - return 0; - case PIXEL_FORMAT_UNKNOWN: - break; - } - NOTREACHED(); - return 0; -} - -// static -bool VideoFrame::IsValidPlane(VideoPixelFormat format, size_t plane) { - DCHECK_LE(NumPlanes(format), static_cast<size_t>(kMaxPlanes)); - return plane < NumPlanes(format); -} - -// static -android::ui::Size VideoFrame::SampleSize(VideoPixelFormat format, size_t plane) { - DCHECK(IsValidPlane(format, plane)); - - switch (plane) { - case kYPlane: // and kARGBPlane: - case kAPlane: - return android::ui::Size(1, 1); - - case kUPlane: // and kUVPlane: - case kVPlane: - switch (format) { - case PIXEL_FORMAT_I444: - case PIXEL_FORMAT_YUV444P9: - case PIXEL_FORMAT_YUV444P10: - case PIXEL_FORMAT_YUV444P12: - case PIXEL_FORMAT_Y16: - return android::ui::Size(1, 1); - - case PIXEL_FORMAT_I422: - case PIXEL_FORMAT_YUV422P9: - case PIXEL_FORMAT_YUV422P10: - case PIXEL_FORMAT_YUV422P12: - return android::ui::Size(2, 1); - - case PIXEL_FORMAT_YV12: - case PIXEL_FORMAT_I420: - case PIXEL_FORMAT_I420A: - case PIXEL_FORMAT_NV12: - case PIXEL_FORMAT_NV21: - case PIXEL_FORMAT_YUV420P9: - case PIXEL_FORMAT_YUV420P10: - case PIXEL_FORMAT_YUV420P12: - case PIXEL_FORMAT_P016LE: - return android::ui::Size(2, 2); - - case PIXEL_FORMAT_UNKNOWN: - case PIXEL_FORMAT_YUY2: - case PIXEL_FORMAT_ARGB: - case PIXEL_FORMAT_XRGB: - case PIXEL_FORMAT_RGB24: - case PIXEL_FORMAT_MJPEG: - case PIXEL_FORMAT_ABGR: - case PIXEL_FORMAT_XBGR: - case PIXEL_FORMAT_XR30: - case PIXEL_FORMAT_XB30: - case PIXEL_FORMAT_BGRA: - break; - } - } - NOTREACHED(); - return android::ui::Size(); -} - -} // namespace media diff --git a/accel/video_frame.h b/accel/video_frame.h deleted file mode 100644 index 6742281..0000000 --- a/accel/video_frame.h +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. -// Note: ported from Chromium commit head: 602bc8fa60fa -// Note: only necessary functions are ported. -// Note: some OS-specific defines have been removed -// Note: WrapExternalSharedMemory() has been removed in Chromium, but is still -// present here. Porting the code to a newer version of VideoFrame is not -// useful, as this is only a temporary step and all usage of VideoFrame will -// be removed. - -#ifndef VIDEO_FRAME_H_ -#define VIDEO_FRAME_H_ - -#include <stddef.h> -#include <stdint.h> - -#include <memory> -#include <string> -#include <utility> -#include <vector> - -#include "base/memory/ref_counted.h" -#include "ui/Size.h" -#include "video_frame_layout.h" -#include "video_pixel_format.h" - -namespace media { - -class VideoFrame : public base::RefCountedThreadSafe<VideoFrame> { - public: - enum { - kMaxPlanes = 4, - - kYPlane = 0, - kARGBPlane = kYPlane, - kUPlane = 1, - kUVPlane = kUPlane, - kVPlane = 2, - kAPlane = 3, - }; - - static size_t NumPlanes(VideoPixelFormat format); - - // Returns the required allocation size for a (tightly packed) frame of the - // given coded size and format. - static size_t AllocationSize(VideoPixelFormat format, const android::ui::Size& coded_size); - - // Returns the plane Size (in bytes) for a plane of the given coded size - // and format. - static android::ui::Size PlaneSize(VideoPixelFormat format, - size_t plane, - const android::ui::Size& coded_size); - - // Returns horizontal bits per pixel for given |plane| and |format|. - static int PlaneHorizontalBitsPerPixel(VideoPixelFormat format, size_t plane); - - // Returns bits per pixel for given |plane| and |format|. - static int PlaneBitsPerPixel(VideoPixelFormat format, size_t plane); - - // Returns the number of bytes per element for given |plane| and |format|. - static int BytesPerElement(VideoPixelFormat format, size_t plane); - - // Returns true if |plane| is a valid plane index for the given |format|. - static bool IsValidPlane(size_t plane, VideoPixelFormat format); - - // Returns true if |plane| is a valid plane index for the given |format|. - static bool IsValidPlane(VideoPixelFormat format, size_t plane); - - // Returns the pixel size of each subsample for a given |plane| and |format|. - // E.g. 2x2 for the U-plane in PIXEL_FORMAT_I420. - static android::ui::Size SampleSize(VideoPixelFormat format, size_t plane); -}; - -} // namespace media - -#endif // VIDEO_FRAME_H_ diff --git a/accel/video_frame_layout.cc b/accel/video_frame_layout.cc index 2948cc3..5679e3e 100644 --- a/accel/video_frame_layout.cc +++ b/accel/video_frame_layout.cc @@ -42,50 +42,6 @@ std::vector<ColorPlaneLayout> PlanesFromStrides( } // namespace // static -size_t VideoFrameLayout::NumPlanes(VideoPixelFormat format) { - switch (format) { - case PIXEL_FORMAT_YUY2: - case PIXEL_FORMAT_ARGB: - case PIXEL_FORMAT_BGRA: - case PIXEL_FORMAT_XRGB: - case PIXEL_FORMAT_RGB24: - case PIXEL_FORMAT_MJPEG: - case PIXEL_FORMAT_Y16: - case PIXEL_FORMAT_ABGR: - case PIXEL_FORMAT_XBGR: - case PIXEL_FORMAT_XR30: - case PIXEL_FORMAT_XB30: - return 1; - case PIXEL_FORMAT_NV12: - case PIXEL_FORMAT_NV21: - case PIXEL_FORMAT_P016LE: - return 2; - case PIXEL_FORMAT_I420: - case PIXEL_FORMAT_YV12: - case PIXEL_FORMAT_I422: - case PIXEL_FORMAT_I444: - case PIXEL_FORMAT_YUV420P9: - case PIXEL_FORMAT_YUV422P9: - case PIXEL_FORMAT_YUV444P9: - case PIXEL_FORMAT_YUV420P10: - case PIXEL_FORMAT_YUV422P10: - case PIXEL_FORMAT_YUV444P10: - case PIXEL_FORMAT_YUV420P12: - case PIXEL_FORMAT_YUV422P12: - case PIXEL_FORMAT_YUV444P12: - return 3; - case PIXEL_FORMAT_I420A: - return 4; - case PIXEL_FORMAT_UNKNOWN: - // Note: PIXEL_FORMAT_UNKNOWN is used for end-of-stream frame. - // Set its NumPlanes() to zero to avoid NOTREACHED(). - return 0; - } - NOTREACHED() << "Unsupported video frame format: " << format; - return 0; -} - -// static std::optional<VideoFrameLayout> VideoFrameLayout::Create( VideoPixelFormat format, const android::ui::Size& coded_size) { diff --git a/accel/video_frame_layout.h b/accel/video_frame_layout.h index 980d526..e4d8198 100644 --- a/accel/video_frame_layout.h +++ b/accel/video_frame_layout.h @@ -96,8 +96,6 @@ class VideoFrameLayout { VideoFrameLayout& operator=(const VideoFrameLayout&); ~VideoFrameLayout(); - static size_t NumPlanes(VideoPixelFormat format); - VideoPixelFormat format() const { return format_; } const android::ui::Size& coded_size() const { return coded_size_; } diff --git a/accel/video_pixel_format.cc b/accel/video_pixel_format.cc index 20b8537..05627b6 100644 --- a/accel/video_pixel_format.cc +++ b/accel/video_pixel_format.cc @@ -6,11 +6,29 @@ #include "video_pixel_format.h" +#include "base/bits.h" #include "base/logging.h" +#include "base/stl_util.h" #include "base/strings/stringprintf.h" +#include "video_frame_layout.h" + namespace media { +namespace { + +enum { + kMaxPlanes = 4, + kYPlane = 0, + kARGBPlane = kYPlane, + kUPlane = 1, + kUVPlane = kUPlane, + kVPlane = 2, + kAPlane = 3, + }; + +} + std::string VideoPixelFormatToString(VideoPixelFormat format) { switch (format) { case PIXEL_FORMAT_UNKNOWN: @@ -130,5 +148,242 @@ size_t BitDepth(VideoPixelFormat format) { return 0; } +// If it is required to allocate aligned to multiple-of-two size overall for the +// frame of pixel |format|. +static bool RequiresEvenSizeAllocation(VideoPixelFormat format) { + switch (format) { + case PIXEL_FORMAT_ARGB: + case PIXEL_FORMAT_XRGB: + case PIXEL_FORMAT_RGB24: + case PIXEL_FORMAT_Y16: + case PIXEL_FORMAT_ABGR: + case PIXEL_FORMAT_XBGR: + case PIXEL_FORMAT_XR30: + case PIXEL_FORMAT_XB30: + case PIXEL_FORMAT_BGRA: + return false; + case PIXEL_FORMAT_NV12: + case PIXEL_FORMAT_NV21: + case PIXEL_FORMAT_I420: + case PIXEL_FORMAT_MJPEG: + case PIXEL_FORMAT_YUY2: + case PIXEL_FORMAT_YV12: + case PIXEL_FORMAT_I422: + case PIXEL_FORMAT_I444: + case PIXEL_FORMAT_YUV420P9: + case PIXEL_FORMAT_YUV422P9: + case PIXEL_FORMAT_YUV444P9: + case PIXEL_FORMAT_YUV420P10: + case PIXEL_FORMAT_YUV422P10: + case PIXEL_FORMAT_YUV444P10: + case PIXEL_FORMAT_YUV420P12: + case PIXEL_FORMAT_YUV422P12: + case PIXEL_FORMAT_YUV444P12: + case PIXEL_FORMAT_I420A: + case PIXEL_FORMAT_P016LE: + return true; + case PIXEL_FORMAT_UNKNOWN: + break; + } + NOTREACHED() << "Unsupported video frame format: " << format; + return false; +} + +size_t NumPlanes(VideoPixelFormat format) { + switch (format) { + case PIXEL_FORMAT_YUY2: + case PIXEL_FORMAT_ARGB: + case PIXEL_FORMAT_BGRA: + case PIXEL_FORMAT_XRGB: + case PIXEL_FORMAT_RGB24: + case PIXEL_FORMAT_MJPEG: + case PIXEL_FORMAT_Y16: + case PIXEL_FORMAT_ABGR: + case PIXEL_FORMAT_XBGR: + case PIXEL_FORMAT_XR30: + case PIXEL_FORMAT_XB30: + return 1; + case PIXEL_FORMAT_NV12: + case PIXEL_FORMAT_NV21: + case PIXEL_FORMAT_P016LE: + return 2; + case PIXEL_FORMAT_I420: + case PIXEL_FORMAT_YV12: + case PIXEL_FORMAT_I422: + case PIXEL_FORMAT_I444: + case PIXEL_FORMAT_YUV420P9: + case PIXEL_FORMAT_YUV422P9: + case PIXEL_FORMAT_YUV444P9: + case PIXEL_FORMAT_YUV420P10: + case PIXEL_FORMAT_YUV422P10: + case PIXEL_FORMAT_YUV444P10: + case PIXEL_FORMAT_YUV420P12: + case PIXEL_FORMAT_YUV422P12: + case PIXEL_FORMAT_YUV444P12: + return 3; + case PIXEL_FORMAT_I420A: + return 4; + case PIXEL_FORMAT_UNKNOWN: + // Note: PIXEL_FORMAT_UNKNOWN is used for end-of-stream frame. + // Set its NumPlanes() to zero to avoid NOTREACHED(). + return 0; + } + NOTREACHED() << "Unsupported video frame format: " << format; + return 0; +} + +size_t AllocationSize(VideoPixelFormat format, + const android::ui::Size& coded_size) { + size_t total = 0; + for (size_t i = 0; i < NumPlanes(format); ++i) { + android::ui::Size plane_size = PlaneSize(format, i, coded_size); + total += (plane_size.width * plane_size.height); + } + + return total; +} + +android::ui::Size PlaneSize(VideoPixelFormat format, + size_t plane, + const android::ui::Size& coded_size) { + DCHECK(IsValidPlane(plane, format)); + + int width = coded_size.width; + int height = coded_size.height; + if (RequiresEvenSizeAllocation(format)) { + // Align to multiple-of-two size overall. This ensures that non-subsampled + // planes can be addressed by pixel with the same scaling as the subsampled + // planes. + width = base::bits::Align(width, 2); + height = base::bits::Align(height, 2); + } + + const android::ui::Size subsample = SampleSize(format, plane); + DCHECK(width % subsample.width == 0); + DCHECK(height % subsample.height == 0); + return android::ui::Size(BytesPerElement(format, plane) * width / subsample.width, + height / subsample.height); +} + +int PlaneHorizontalBitsPerPixel(VideoPixelFormat format, + size_t plane) { + DCHECK(IsValidPlane(plane, format)); + const int bits_per_element = 8 * BytesPerElement(format, plane); + const int horiz_pixels_per_element = SampleSize(format, plane).width; + DCHECK_EQ(bits_per_element % horiz_pixels_per_element, 0); + return bits_per_element / horiz_pixels_per_element; +} + +int PlaneBitsPerPixel(VideoPixelFormat format, size_t plane) { + DCHECK(IsValidPlane(plane, format)); + return PlaneHorizontalBitsPerPixel(format, plane) / + SampleSize(format, plane).height; +} + +int BytesPerElement(VideoPixelFormat format, size_t plane) { + DCHECK(IsValidPlane(format, plane)); + switch (format) { + case PIXEL_FORMAT_ARGB: + case PIXEL_FORMAT_BGRA: + case PIXEL_FORMAT_XRGB: + case PIXEL_FORMAT_ABGR: + case PIXEL_FORMAT_XBGR: + case PIXEL_FORMAT_XR30: + case PIXEL_FORMAT_XB30: + return 4; + case PIXEL_FORMAT_RGB24: + return 3; + case PIXEL_FORMAT_Y16: + case PIXEL_FORMAT_YUY2: + case PIXEL_FORMAT_YUV420P9: + case PIXEL_FORMAT_YUV422P9: + case PIXEL_FORMAT_YUV444P9: + case PIXEL_FORMAT_YUV420P10: + case PIXEL_FORMAT_YUV422P10: + case PIXEL_FORMAT_YUV444P10: + case PIXEL_FORMAT_YUV420P12: + case PIXEL_FORMAT_YUV422P12: + case PIXEL_FORMAT_YUV444P12: + case PIXEL_FORMAT_P016LE: + return 2; + case PIXEL_FORMAT_NV12: + case PIXEL_FORMAT_NV21: { + static const int bytes_per_element[] = {1, 2}; + DCHECK_LT(plane, base::size(bytes_per_element)); + return bytes_per_element[plane]; + } + case PIXEL_FORMAT_YV12: + case PIXEL_FORMAT_I420: + case PIXEL_FORMAT_I422: + case PIXEL_FORMAT_I420A: + case PIXEL_FORMAT_I444: + return 1; + case PIXEL_FORMAT_MJPEG: + return 0; + case PIXEL_FORMAT_UNKNOWN: + break; + } + NOTREACHED(); + return 0; +} + +bool IsValidPlane(VideoPixelFormat format, size_t plane) { + DCHECK_LE(NumPlanes(format), static_cast<size_t>(kMaxPlanes)); + return plane < NumPlanes(format); +} + +android::ui::Size SampleSize(VideoPixelFormat format, size_t plane) { + DCHECK(IsValidPlane(format, plane)); + + switch (plane) { + case kYPlane: // and kARGBPlane: + case kAPlane: + return android::ui::Size(1, 1); + + case kUPlane: // and kUVPlane: + case kVPlane: + switch (format) { + case PIXEL_FORMAT_I444: + case PIXEL_FORMAT_YUV444P9: + case PIXEL_FORMAT_YUV444P10: + case PIXEL_FORMAT_YUV444P12: + case PIXEL_FORMAT_Y16: + return android::ui::Size(1, 1); + + case PIXEL_FORMAT_I422: + case PIXEL_FORMAT_YUV422P9: + case PIXEL_FORMAT_YUV422P10: + case PIXEL_FORMAT_YUV422P12: + return android::ui::Size(2, 1); + + case PIXEL_FORMAT_YV12: + case PIXEL_FORMAT_I420: + case PIXEL_FORMAT_I420A: + case PIXEL_FORMAT_NV12: + case PIXEL_FORMAT_NV21: + case PIXEL_FORMAT_YUV420P9: + case PIXEL_FORMAT_YUV420P10: + case PIXEL_FORMAT_YUV420P12: + case PIXEL_FORMAT_P016LE: + return android::ui::Size(2, 2); + + case PIXEL_FORMAT_UNKNOWN: + case PIXEL_FORMAT_YUY2: + case PIXEL_FORMAT_ARGB: + case PIXEL_FORMAT_XRGB: + case PIXEL_FORMAT_RGB24: + case PIXEL_FORMAT_MJPEG: + case PIXEL_FORMAT_ABGR: + case PIXEL_FORMAT_XBGR: + case PIXEL_FORMAT_XR30: + case PIXEL_FORMAT_XB30: + case PIXEL_FORMAT_BGRA: + break; + } + } + NOTREACHED(); + return android::ui::Size(); +} + } // namespace media diff --git a/accel/video_pixel_format.h b/accel/video_pixel_format.h index 8d80731..99e604a 100644 --- a/accel/video_pixel_format.h +++ b/accel/video_pixel_format.h @@ -9,6 +9,8 @@ #include <string> +#include "ui/Size.h" + namespace media { // Pixel formats roughly based on FOURCC labels, see: @@ -90,6 +92,38 @@ std::string FourccToString(uint32_t fourcc); // Returns the number of significant bits per channel. size_t BitDepth(VideoPixelFormat format); +// Returns the number of planes for the |format|. +size_t NumPlanes(VideoPixelFormat format); + +// Returns the required allocation size for a (tightly packed) frame of the +// given coded size and format. +size_t AllocationSize(VideoPixelFormat format, const android::ui::Size& coded_size); + +// Returns the plane Size (in bytes) for a plane of the given coded size +// and format. +android::ui::Size PlaneSize(VideoPixelFormat format, + size_t plane, + const android::ui::Size& coded_size); + +// Returns horizontal bits per pixel for given |plane| and |format|. +int PlaneHorizontalBitsPerPixel(VideoPixelFormat format, size_t plane); + +// Returns bits per pixel for given |plane| and |format|. +int PlaneBitsPerPixel(VideoPixelFormat format, size_t plane); + +// Returns the number of bytes per element for given |plane| and |format|. +int BytesPerElement(VideoPixelFormat format, size_t plane); + +// Returns true if |plane| is a valid plane index for the given |format|. +bool IsValidPlane(size_t plane, VideoPixelFormat format); + +// Returns true if |plane| is a valid plane index for the given |format|. +bool IsValidPlane(VideoPixelFormat format, size_t plane); + +// Returns the pixel size of each subsample for a given |plane| and |format|. +// E.g. 2x2 for the U-plane in PIXEL_FORMAT_I420. +android::ui::Size SampleSize(VideoPixelFormat format, size_t plane); + } // namespace media #endif // VIDEO_PIXEL_FORMAT_H_ diff --git a/common/V4L2Device.cpp b/common/V4L2Device.cpp index 90b5c64..8459681 100644 --- a/common/V4L2Device.cpp +++ b/common/V4L2Device.cpp @@ -1487,12 +1487,12 @@ ui::Size V4L2Device::allocatedSizeFromV4L2Format(const struct v4l2_format& forma // elsewhere to calculate coded height. // We need bits per pixel for one component only to calculate the coded width from bytesperline. - int planeHorizBitsPerPixel = media::VideoFrame::PlaneHorizontalBitsPerPixel(frameFormat, 0); + int planeHorizBitsPerPixel = media::PlaneHorizontalBitsPerPixel(frameFormat, 0); // Adding up bpp for each component will give us total bpp for all components. int totalBpp = 0; - for (size_t i = 0; i < media::VideoFrame::NumPlanes(frameFormat); ++i) - totalBpp += media::VideoFrame::PlaneBitsPerPixel(frameFormat, i); + for (size_t i = 0; i < media::NumPlanes(frameFormat); ++i) + totalBpp += media::PlaneBitsPerPixel(frameFormat, i); if (sizeimage == 0 || bytesPerLine == 0 || planeHorizBitsPerPixel == 0 || totalBpp == 0 || (bytesPerLine * 8) % planeHorizBitsPerPixel != 0) { @@ -1512,7 +1512,7 @@ ui::Size V4L2Device::allocatedSizeFromV4L2Format(const struct v4l2_format& forma // Sanity checks. Calculated coded size has to contain given visible size and fulfill buffer // byte size requirements. ALOG_ASSERT(media::Rect(codedSize).Contains(media::Rect(visibleSize))); - ALOG_ASSERT(sizeimage <= media::VideoFrame::AllocationSize(frameFormat, codedSize)); + ALOG_ASSERT(sizeimage <= media::AllocationSize(frameFormat, codedSize)); return codedSize; } @@ -1631,7 +1631,7 @@ std::optional<media::VideoFrameLayout> V4L2Device::v4L2FormatToVideoFrameLayout( } const media::VideoPixelFormat videoFormat = videoFourcc->ToVideoPixelFormat(); const size_t numBuffers = pixMp.num_planes; - const size_t numColorPlanes = media::VideoFrame::NumPlanes(videoFormat); + const size_t numColorPlanes = media::NumPlanes(videoFormat); if (numColorPlanes == 0) { ALOGE("Unsupported video format for NumPlanes(): %s", VideoPixelFormatToString(videoFormat).c_str()); @@ -1705,7 +1705,7 @@ std::optional<media::VideoFrameLayout> V4L2Device::v4L2FormatToVideoFrameLayout( size_t V4L2Device::getNumPlanesOfV4L2PixFmt(uint32_t pixFmt) { std::optional<media::Fourcc> fourcc = media::Fourcc::FromV4L2PixFmt(pixFmt); if (fourcc && fourcc->IsMultiPlanar()) { - return media::VideoFrame::NumPlanes(fourcc->ToVideoPixelFormat()); + return media::NumPlanes(fourcc->ToVideoPixelFormat()); } return 1u; } diff --git a/common/include/v4l2_codec2/common/V4L2Device.h b/common/include/v4l2_codec2/common/V4L2Device.h index 3e23ca3..2c3bdea 100644 --- a/common/include/v4l2_codec2/common/V4L2Device.h +++ b/common/include/v4l2_codec2/common/V4L2Device.h @@ -25,7 +25,6 @@ #include <ui/Size.h> #include <v4l2_codec2/common/V4L2DevicePoller.h> #include <v4l2_codec2/common/VideoTypes.h> -#include <video_frame.h> #include <video_frame_layout.h> #include <video_pixel_format.h> diff --git a/components/V4L2Encoder.cpp b/components/V4L2Encoder.cpp index db3fa63..019dd83 100644 --- a/components/V4L2Encoder.cpp +++ b/components/V4L2Encoder.cpp @@ -727,11 +727,10 @@ bool V4L2Encoder::enqueueInputBuffer(std::unique_ptr<InputFrame> frame) { // buffer should be sum of each color planes' size. size_t bytesUsed = 0; if (planes.size() == 1) { - bytesUsed = media::VideoFrame::AllocationSize(format, mInputLayout->coded_size()); + bytesUsed = media::AllocationSize(format, mInputLayout->coded_size()); } else { bytesUsed = ::base::checked_cast<size_t>( - getArea(media::VideoFrame::PlaneSize(format, i, mInputLayout->coded_size())) - .value()); + getArea(media::PlaneSize(format, i, mInputLayout->coded_size())).value()); } // TODO(crbug.com/901264): The way to pass an offset within a DMA-buf is not defined |