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) 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 NALUType : uint8_t {
45         NON_IDR_SLICE = 0x1,
46         IDR_SLICE = 0x5,
47         SPS = 0x7,
48         PPS = 0x8,
49     };
50 
51     // Slice input stream into fragments. This should be done in constructor.
52     void SliceToFragments(const std::string& data);
53 
54     // For H264, parse csd_flag from |fragment| data and store inside. Return true
55     // if this fragment is in interest; false otherwise (fragment will be
56     // discarded.)
57     bool ParseAUFragmentType(Fragment* fragment);
58 
59     VideoCodecType type_;
60     std::vector<std::unique_ptr<Fragment>> fragments_;
61     std::vector<std::unique_ptr<Fragment>>::iterator next_fragment_iter_;
62 };
63 
64 }  // namespace android
65 
66 #endif  // C2_E2E_TEST_ENCODED_DATA_HELPER_H_
67