xref: /aosp_15_r20/external/v4l2_codec2/tests/c2_e2e_test/jni/encoded_data_helper.h (revision 0ec5a0ec62797f775085659156625e7f1bdb369f)
1 // Copyright 2019 The Chromium OS 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 C2_E2E_TEST_ENCODED_DATA_HELPER_H_
6 #define C2_E2E_TEST_ENCODED_DATA_HELPER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "common.h"
13 
14 namespace android {
15 
16 // Helper class for MediaCodecDecoder to read encoded stream from input file,
17 // and slice it into fragments. MediaCodecDecoder could call GetNextFragment()
18 // to obtain fragment data sequentially.
19 class EncodedDataHelper {
20 public:
21     EncodedDataHelper(const std::string& file_path, VideoCodecType type);
22     ~EncodedDataHelper();
23 
24     // A fragment will contain the bytes of one AU (H264/HEVC) or frame (VP8/9) in
25     // |data|, and |csd_flag| indicator for input buffer flag CODEC_CONFIG.
26     struct Fragment {
27         std::string data;
28         bool csd_flag = false;
29     };
30 
31     // Return the next fragment to be sent to the decoder, and advance the
32     // iterator to after the returned fragment.
33     const Fragment* const GetNextFragment();
34 
Rewind()35     void Rewind() { next_fragment_iter_ = fragments_.begin(); }
IsValid()36     bool IsValid() const { return !fragments_.empty(); }
NumValidFragments()37     size_t NumValidFragments() const { return fragments_.size(); }
38     bool AtHeadOfStream() const;
39     bool ReachEndOfStream() const;
40 
41 private:
42     // NALU type enumeration as defined in H264 Annex-B. Only interested ones are
43     // listed here.
44     enum H264NALUType : uint8_t {
45         NON_IDR_SLICE = 0x1,
46         IDR_SLICE = 0x5,
47         SPS = 0x7,
48         PPS = 0x8,
49     };
50 
51     // NALU type enumeration for ranges for VCL and CSD NALUs for HEVC.
52     enum HEVCNALUType : uint8_t {
53         VCL_NALU_MIN = 0,
54         VCL_NALU_MAX = 31,
55         CSD_NALU_MIN = 32,  // VPS, SPS, PPS
56         CSD_NALU_MAX = 34,
57     };
58 
59     // Slice input stream into fragments. This should be done in constructor.
60     void SliceToFragments(const std::string& data);
61 
62     // For H264/HEVC, parse csd_flag from |fragment| data and store inside. Return true
63     // if this fragment is in interest; false otherwise (fragment will be
64     // discarded.)
65     bool ParseAUFragmentType(Fragment* fragment);
66 
67     VideoCodecType type_;
68     std::vector<std::unique_ptr<Fragment>> fragments_;
69     std::vector<std::unique_ptr<Fragment>>::iterator next_fragment_iter_;
70 };
71 
72 }  // namespace android
73 
74 #endif  // C2_E2E_TEST_ENCODED_DATA_HELPER_H_
75