1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MEDIA_GPU_TEST_VIDEO_ENCODER_DECODER_BUFFER_VALIDATOR_H_
6 #define MEDIA_GPU_TEST_VIDEO_ENCODER_DECODER_BUFFER_VALIDATOR_H_
7 
8 #include <stdint.h>
9 
10 #include "base/memory/scoped_refptr.h"
11 #include "media/filters/vp9_parser.h"
12 #include "media/gpu/h264_dpb.h"
13 #include "media/gpu/test/bitstream_helpers.h"
14 #include "media/parsers/vp8_parser.h"
15 #include "media/video/h264_parser.h"
16 #include "ui/gfx/geometry/rect.h"
17 
18 namespace media {
19 
20 class DecoderBuffer;
21 
22 namespace test {
23 class DecoderBufferValidator : public BitstreamProcessor {
24  public:
25   explicit DecoderBufferValidator(const gfx::Rect& visible_rect);
26   ~DecoderBufferValidator() override;
27 
28   // BitstreamProcessor implementation.
29   void ProcessBitstream(scoped_refptr<BitstreamRef> bitstream,
30                         size_t frame_index) override;
31   bool WaitUntilDone() override;
32 
33  protected:
34   // Returns true if decoder_buffer is valid and expected, otherwise false.
35   virtual bool Validate(const DecoderBuffer& decoder_buffer,
36                         const BitstreamBufferMetadata& metadata) = 0;
37 
38   // The expected visible rectangle that |decoder_buffer| has.
39   const gfx::Rect visible_rect_;
40 
41  private:
42   // The number of detected errors by Validate().
43   size_t num_errors_ = 0;
44 };
45 
46 // TemporalLayerValidator checks whether the stream is valid on each temporal
47 // layer.
48 class TemporalLayerValidator {
49  public:
50   TemporalLayerValidator(size_t num_temporal_layers);
51   ~TemporalLayerValidator();
52 
53   bool ValidateAndUpdate(bool keyframe,
54                          uint8_t temporal_index,
55                          uint8_t reference_index,
56                          uint8_t refresh_frame_index);
57 
58  private:
59   static constexpr size_t kReferenceFramePoolSize = 8;
60   const size_t num_temporal_layers_ = 3;
61   std::array<uint8_t, kReferenceFramePoolSize> reference_frames_;
62 };
63 
64 class H264Validator : public DecoderBufferValidator {
65  public:
66   H264Validator(VideoCodecProfile profile,
67                 const gfx::Rect& visible_rect,
68                 base::Optional<uint8_t> level = base::nullopt);
69   ~H264Validator() override;
70 
71  private:
72   bool Validate(const DecoderBuffer& decoder_buffer,
73                 const BitstreamBufferMetadata& metadata) override;
74 
75   // Returns whether the |slice_hdr| is the first slice of a new frame.
76   bool IsNewPicture(const H264SliceHeader& slice_hdr);
77   // Initialize |cur_pic_| with |slice_hdr|.
78   bool UpdateCurrentPicture(const H264SliceHeader& slice_hdr);
79 
80   // These represent whether sequence parameter set (SPS), picture parameter set
81   // (PPS) and IDR frame have been input, respectively.
82   bool seen_sps_;
83   bool seen_pps_;
84   bool seen_idr_;
85 
86   // Current h264 picture. It is used to check that H264Picture is created from
87   // a given bitstream.
88   scoped_refptr<H264Picture> cur_pic_;
89   // Current SPS and PPS id.
90   int cur_sps_id_;
91   int cur_pps_id_;
92 
93   H264Parser parser_;
94 
95   // The expected h264 profile of |decoder_buffer|.
96   const int profile_;
97   // The expected h264 level of |decoder_buffer|. Check if it is not
98   // base::nullopt.
99   base::Optional<uint8_t> level_;
100 };
101 
102 class VP8Validator : public DecoderBufferValidator {
103  public:
104   explicit VP8Validator(const gfx::Rect& visible_rect);
105   ~VP8Validator() override;
106 
107  private:
108   bool Validate(const DecoderBuffer& decoder_buffer,
109                 const BitstreamBufferMetadata& metadata) override;
110 
111   Vp8Parser parser_;
112   // Whether key frame has been input.
113   bool seen_keyframe_ = false;
114 };
115 
116 class VP9Validator : public DecoderBufferValidator {
117  public:
118   VP9Validator(VideoCodecProfile profile,
119                const gfx::Rect& visible_rect,
120                size_t num_temporal_layers);
121   ~VP9Validator() override;
122 
123  private:
124   bool Validate(const DecoderBuffer& decoder_buffer,
125                 const BitstreamBufferMetadata& metadata) override;
126 
127   Vp9Parser parser_;
128 
129   // Whether key frame has been input.
130   bool seen_keyframe_ = false;
131 
132   // The expected h264 profile of |decoder_buffer|.
133   const int profile_;
134 
135   const std::unique_ptr<TemporalLayerValidator> temporal_layer_validator_;
136 };
137 }  // namespace test
138 }  // namespace media
139 #endif  // MEDIA_GPU_TEST_VIDEO_ENCODER_DECODER_BUFFER_VALIDATOR_H_
140