File: video_decoder.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 4,265 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_GPU_V4L2_TEST_VIDEO_DECODER_H_
#define MEDIA_GPU_V4L2_TEST_VIDEO_DECODER_H_

#include "media/gpu/v4l2/test/v4l2_ioctl_shim.h"

namespace media {
namespace v4l2_test {

constexpr uint32_t kNumberOfBuffersInOutputQueue = 1;
static_assert(kNumberOfBuffersInOutputQueue == 1,
              "Too many buffers in OUTPUT queue. It is currently designed to "
              "support only 1 request at a time.");

// For stateless API, fourcc |VP9F| is needed instead of |VP90| for VP9 codec.
// Fourcc |AV1F| is needed instead of |AV10| for AV1 codec.
// https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/pixfmt-compressed.html
// Converts fourcc |VP90| or |AV01| from file header to fourcc |VP9F| or |AV1F|,
// which is a format supported on driver.
uint32_t FileFourccToDriverFourcc(uint32_t header_fourcc);

// VideoDecoder decodes encoded video streams using v4l2 ioctl calls.
// To implement a decoder, implement the following:
// 1. A factory function, such as:
//   std::unique_ptr<VideoDecoder> Create(const base::MemoryMappedFile& stream)
// 2. DecodeNextFrame
class VideoDecoder {
 public:
  // Result of decoding the current frame.
  enum Result {
    kOk,
    kError,
    kEOStream,
  };

  enum BitDepth { Depth8, Depth16 };

  VideoDecoder(std::unique_ptr<V4L2IoctlShim> v4l2_ioctl,
               gfx::Size display_resolution);

  virtual ~VideoDecoder();

  VideoDecoder(const VideoDecoder&) = delete;
  VideoDecoder& operator=(const VideoDecoder&) = delete;

  // Initializes setup needed for decoding.
  // https://www.kernel.org/doc/html/v5.10/userspace-api/media/v4l/dev-stateless-decoder.html#initialization
  void CreateOUTPUTQueue(uint32_t compressed_fourcc);
  void CreateCAPTUREQueue(uint32_t num_buffers);

  // Decoders implement this. The function writes the next displayed picture
  // into the output plane buffers |y_plane|, |u_plane|, and |v_plane|. |size|
  // is the visible picture size.
  virtual Result DecodeNextFrame(const int frame_number,
                                 std::vector<uint8_t>& y_plane,
                                 std::vector<uint8_t>& u_plane,
                                 std::vector<uint8_t>& v_plane,
                                 gfx::Size& size,
                                 BitDepth& bit_depth) = 0;

  // Handles dynamic resolution change with new resolution parsed from frame
  // header.
  void HandleDynamicResolutionChange(const gfx::Size& new_resolution);

  // Returns whether the last decoded frame was visible.
  bool LastDecodedFrameVisible() const { return last_decoded_frame_visible_; }

  // Converts raw YUV of decoded frame data to PNG.
  static std::vector<uint8_t> ConvertYUVToPNG(uint8_t* y_plane,
                                              uint8_t* u_plane,
                                              uint8_t* v_plane,
                                              const gfx::Size& size,
                                              BitDepth bit_depth);

 protected:
  void NegotiateCAPTUREFormat();

  // Helper method for converting frames to YUV. Returns the bit depth of
  // the converted frame.
  static BitDepth ConvertToYUV(std::vector<uint8_t>& dest_y,
                               std::vector<uint8_t>& dest_u,
                               std::vector<uint8_t>& dest_v,
                               const gfx::Size& dest_size,
                               const MmappedBuffer::MmappedPlanes& planes,
                               const gfx::Size& src_size,
                               uint32_t fourcc);

  // Wrapper for V4L2 ioctl requests.
  const std::unique_ptr<V4L2IoctlShim> v4l2_ioctl_;

  // OUTPUT_queue needed for compressed (encoded) input.
  std::unique_ptr<V4L2Queue> OUTPUT_queue_;

  // CAPTURE_queue needed for uncompressed (decoded) output.
  std::unique_ptr<V4L2Queue> CAPTURE_queue_;

  // Whether the last decoded frame was visible.
  bool last_decoded_frame_visible_ = false;

  // resolution from the bitstream header
  gfx::Size display_resolution_;
};

}  // namespace v4l2_test
}  // namespace media

#endif  // MEDIA_GPU_V4L2_TEST_VIDEO_DECODER_H_