aboutsummaryrefslogtreecommitdiff
path: root/tests/c2_e2e_test/jni/encoded_data_helper.h
blob: cf107c267d6bd64909d69eafb0ed7ed02716512a (plain)
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
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef C2_E2E_TEST_ENCODED_DATA_HELPER_H_
#define C2_E2E_TEST_ENCODED_DATA_HELPER_H_

#include <memory>
#include <string>
#include <vector>

#include "common.h"

namespace android {

// Helper class for MediaCodecDecoder to read encoded stream from input file,
// and slice it into fragments. MediaCodecDecoder could call GetNextFragment()
// to obtain fragment data sequentially.
class EncodedDataHelper {
public:
    EncodedDataHelper(const std::string& file_path, VideoCodecType type);
    ~EncodedDataHelper();

    // A fragment will contain the bytes of one AU (H264) or frame (VP8/9) in
    // |data|, and |csd_flag| indicator for input buffer flag CODEC_CONFIG.
    struct Fragment {
        std::string data;
        bool csd_flag = false;
    };

    // Return the next fragment to be sent to the decoder, and advance the
    // iterator to after the returned fragment.
    const Fragment* const GetNextFragment();

    void Rewind() { next_fragment_iter_ = fragments_.begin(); }
    bool IsValid() const { return !fragments_.empty(); }
    size_t NumValidFragments() const { return fragments_.size(); }
    bool AtHeadOfStream() const;
    bool ReachEndOfStream() const;

private:
    // NALU type enumeration as defined in H264 Annex-B. Only interested ones are
    // listed here.
    enum NALUType : uint8_t {
        NON_IDR_SLICE = 0x1,
        IDR_SLICE = 0x5,
        SPS = 0x7,
        PPS = 0x8,
    };

    // Slice input stream into fragments. This should be done in constructor.
    void SliceToFragments(const std::string& data);

    // For H264, parse csd_flag from |fragment| data and store inside. Return true
    // if this fragment is in interest; false otherwise (fragment will be
    // discarded.)
    bool ParseAUFragmentType(Fragment* fragment);

    VideoCodecType type_;
    std::vector<std::unique_ptr<Fragment>> fragments_;
    std::vector<std::unique_ptr<Fragment>>::iterator next_fragment_iter_;
};

}  // namespace android

#endif  // C2_E2E_TEST_ENCODED_DATA_HELPER_H_