1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __DECODER_H__
18 #define __DECODER_H__
19 
20 #include <chrono>
21 #include <condition_variable>
22 #include <mutex>
23 #include <queue>
24 #include <thread>
25 
26 #include "BenchmarkCommon.h"
27 #include "Extractor.h"
28 #include "Stats.h"
29 
30 class Decoder : public CallBackHandle {
31   public:
Decoder()32     Decoder()
33         : mCodec(nullptr),
34           mFormat(nullptr),
35           mExtractor(nullptr),
36           mNumInputFrame(0),
37           mNumOutputFrame(0),
38           mSawInputEOS(false),
39           mSawOutputEOS(false),
40           mSignalledError(false),
41           mErrorCode(AMEDIA_OK),
42           mInputBuffer(nullptr),
43           mOutFp(nullptr) {
44         mExtractor = new Extractor();
45     }
46 
~Decoder()47     virtual ~Decoder() {
48         if (mExtractor) delete mExtractor;
49     }
50 
getExtractor()51     Extractor *getExtractor() { return mExtractor; }
52 
53     // Decoder related utilities
54     void setupDecoder();
55 
56     void deInitCodec();
57 
58     void resetDecoder();
59 
60     AMediaFormat *getFormat();
61 
62     // Async callback APIs
63     void onInputAvailable(AMediaCodec *codec, int32_t index) override;
64 
65     void onFormatChanged(AMediaCodec *codec, AMediaFormat *format) override;
66 
67     void onError(AMediaCodec *mediaCodec, media_status_t err) override;
68 
69     void onOutputAvailable(AMediaCodec *codec, int32_t index,
70                            AMediaCodecBufferInfo *bufferInfo) override;
71 
72     // Process the frames and give decoded output
73     int32_t decode(uint8_t *inputBuffer, vector<AMediaCodecBufferInfo> &frameInfo,
74                    string &codecName, bool asyncMode, FILE *outFp = nullptr);
75 
76     void dumpStatistics(string inputReference, string componentName = "", string mode = "",
77                         string statsFile = "");
78 
79   private:
80     AMediaCodec *mCodec;
81     AMediaFormat *mFormat;
82 
83     Extractor *mExtractor;
84 
85     int32_t mNumInputFrame;
86     int32_t mNumOutputFrame;
87 
88     bool mSawInputEOS;
89     bool mSawOutputEOS;
90     bool mSignalledError;
91     media_status_t mErrorCode;
92 
93     int32_t mOffset;
94     uint8_t *mInputBuffer;
95     vector<AMediaCodecBufferInfo> mFrameMetaData;
96     FILE *mOutFp;
97 
98     /* Asynchronous locks */
99     mutex mMutex;
100     condition_variable mDecoderDoneCondition;
101 };
102 
103 // Read input samples
104 tuple<ssize_t, uint32_t, int64_t> readSampleData(uint8_t *inputBuffer, int32_t &offset,
105                                                  vector<AMediaCodecBufferInfo> &frameSizes,
106                                                  uint8_t *buf, int32_t frameID, size_t bufSize);
107 
108 #endif  // __DECODER_H__
109