VideoFrame.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/FixedArray.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/Size.h>
  11. #include <LibVideo/Color/CodingIndependentCodePoints.h>
  12. #include <LibVideo/DecoderError.h>
  13. namespace Video {
  14. class VideoFrame {
  15. public:
  16. virtual ~VideoFrame() { }
  17. virtual DecoderErrorOr<void> output_to_bitmap(Gfx::Bitmap& bitmap) = 0;
  18. virtual DecoderErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap()
  19. {
  20. auto bitmap = DECODER_TRY_ALLOC(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { width(), height() }));
  21. TRY(output_to_bitmap(bitmap));
  22. return bitmap;
  23. }
  24. inline Gfx::Size<u32> size() const { return m_size; }
  25. inline u32 width() const { return size().width(); }
  26. inline u32 height() const { return size().height(); }
  27. inline u8 bit_depth() const { return m_bit_depth; }
  28. inline CodingIndependentCodePoints& cicp() { return m_cicp; }
  29. protected:
  30. VideoFrame(Gfx::Size<u32> size,
  31. u8 bit_depth, CodingIndependentCodePoints cicp)
  32. : m_size(size)
  33. , m_bit_depth(bit_depth)
  34. , m_cicp(cicp)
  35. {
  36. }
  37. Gfx::Size<u32> m_size;
  38. u8 m_bit_depth;
  39. CodingIndependentCodePoints m_cicp;
  40. };
  41. class SubsampledYUVFrame : public VideoFrame {
  42. public:
  43. static ErrorOr<NonnullOwnPtr<SubsampledYUVFrame>> try_create(
  44. Gfx::Size<u32> size,
  45. u8 bit_depth, CodingIndependentCodePoints cicp,
  46. bool subsampling_horizontal, bool subsampling_vertical,
  47. Span<u16> plane_y, Span<u16> plane_u, Span<u16> plane_v);
  48. SubsampledYUVFrame(
  49. Gfx::Size<u32> size,
  50. u8 bit_depth, CodingIndependentCodePoints cicp,
  51. bool subsampling_horizontal, bool subsampling_vertical,
  52. FixedArray<u16>& plane_y, FixedArray<u16>& plane_u, FixedArray<u16>& plane_v)
  53. : VideoFrame(size, bit_depth, cicp)
  54. , m_subsampling_horizontal(subsampling_horizontal)
  55. , m_subsampling_vertical(subsampling_vertical)
  56. , m_plane_y(move(plane_y))
  57. , m_plane_u(move(plane_u))
  58. , m_plane_v(move(plane_v))
  59. {
  60. }
  61. DecoderErrorOr<void> output_to_bitmap(Gfx::Bitmap& bitmap) override;
  62. protected:
  63. bool m_subsampling_horizontal;
  64. bool m_subsampling_vertical;
  65. FixedArray<u16> m_plane_y;
  66. FixedArray<u16> m_plane_u;
  67. FixedArray<u16> m_plane_v;
  68. };
  69. }