1 /*
2  * Copyright 2015 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 GOLDFISH_H264_DEC_H_
18 
19 #define GOLDFISH_H264_DEC_H_
20 
21 #include "GoldfishVideoDecoderOMXComponent.h"
22 #include "MediaH264Decoder.h"
23 #include <sys/time.h>
24 
25 #include <vector>
26 #include <map>
27 
28 #include <gralloc_cb_bp.h>
29 #include <utils/RefBase.h>
30 #include <utils/threads.h>
31 #include <utils/Vector.h>
32 #include <utils/List.h>
33 #include <ui/GraphicBuffer.h>
34 
35 
36 namespace android {
37 
38 /** Number of entries in the time-stamp array */
39 #define MAX_TIME_STAMPS 64
40 
41 /** Maximum number of cores supported by the codec */
42 #define CODEC_MAX_NUM_CORES 4
43 
44 #define CODEC_MAX_WIDTH     1920
45 
46 #define CODEC_MAX_HEIGHT    1088
47 
48 /** Input buffer size */
49 #define INPUT_BUF_SIZE (1024 * 1024)
50 
51 #define MIN(a, b) ((a) < (b)) ? (a) : (b)
52 
53 /** Used to remove warnings about unused parameters */
54 #define UNUSED(x) ((void)(x))
55 
56 struct GoldfishAVCDec : public GoldfishVideoDecoderOMXComponent {
57     GoldfishAVCDec(const char *name, const OMX_CALLBACKTYPE *callbacks,
58             OMX_PTR appData, OMX_COMPONENTTYPE **component, RenderMode renderMode);
59 
60 protected:
61     virtual ~GoldfishAVCDec();
62 
63     virtual void onQueueFilled(OMX_U32 portIndex);
64     virtual void onPortFlushCompleted(OMX_U32 portIndex);
65     virtual void onReset();
66     virtual int getColorAspectPreference();
67 
68     virtual OMX_ERRORTYPE internalGetParameter(OMX_INDEXTYPE index, OMX_PTR params);
69 
70     virtual OMX_ERRORTYPE internalSetParameter(OMX_INDEXTYPE index, const OMX_PTR params);
71 
72     virtual OMX_ERRORTYPE getExtensionIndex(const char *name, OMX_INDEXTYPE *index);
73 
74 private:
75     // Number of input and output buffers
76     enum {
77         kNumBuffers = 8
78     };
79 
80     RenderMode  mRenderMode = RenderMode::RENDER_BY_GUEST_CPU;
81     bool mEnableAndroidNativeBuffers = false;
82     std::map<void*, sp<ANativeWindowBuffer>> mNWBuffers;
83 
84     int getHostColorBufferId(void* header);
85 
86     size_t mNumCores;            // Number of cores to be uesd by the codec
87 
88     nsecs_t mTimeStart;   // Time at the start of decode()
89     nsecs_t mTimeEnd;     // Time at the end of decode()
90 
91 #ifdef FILE_DUMP_ENABLE
92     char mInFile[200];
93 #endif /* FILE_DUMP_ENABLE */
94 
95     OMX_COLOR_FORMATTYPE mOmxColorFormat;    // OMX Color format
96 
97     bool mIsInFlush;        // codec is flush mode
98     bool mReceivedEOS;      // EOS is receieved on input port
99 
100     // The input stream has changed to a different resolution, which is still supported by the
101     // codec. So the codec is switching to decode the new resolution.
102     bool mChangingResolution;
103     bool mSignalledError;
104     size_t mInputOffset;
105 
106     status_t initDecoder();
107     status_t deInitDecoder();
108     status_t setFlushMode();
109     status_t setParams(size_t stride);
110     void logVersion();
111     status_t setNumCores();
112     status_t resetDecoder();
113     status_t resetPlugin();
114 
115 
116     void readAndDiscardAllHostBuffers();
117 
118     bool setDecodeArgs(
119             OMX_BUFFERHEADERTYPE *inHeader,
120             OMX_BUFFERHEADERTYPE *outHeader);
121 
122     bool getVUIParams(h264_image_t& img);
123 
124     void copyImageData( OMX_BUFFERHEADERTYPE *outHeader, h264_image_t & img);
125 
126     std::unique_ptr<MediaH264Decoder> mContext;
127     std::vector<uint8_t> mCsd0;
128     std::vector<uint8_t> mCsd1;
129     uint64_t mConsumedBytes = 0;
130     uint8_t* mInPBuffer = nullptr;
131     uint8_t* mOutHeaderBuf = nullptr;
132     DISALLOW_EVIL_CONSTRUCTORS(GoldfishAVCDec);
133 };
134 #ifdef FILE_DUMP_ENABLE
135 
136 #define INPUT_DUMP_PATH     "/sdcard/media/avcd_input"
137 #define INPUT_DUMP_EXT      "h264"
138 
139 #define GENERATE_FILE_NAMES() {                         \
140     strcpy(mInFile, "");                                \
141     sprintf(mInFile, "%s_%lld.%s", INPUT_DUMP_PATH,     \
142             (long long) mTimeStart,                     \
143             INPUT_DUMP_EXT);                            \
144 }
145 
146 #define CREATE_DUMP_FILE(m_filename) {                  \
147     FILE *fp = fopen(m_filename, "wb");                 \
148     if (fp != NULL) {                                   \
149         fclose(fp);                                     \
150     } else {                                            \
151         ALOGD("Could not open file %s", m_filename);    \
152     }                                                   \
153 }
154 #define DUMP_TO_FILE(m_filename, m_buf, m_size, m_offset)\
155 {                                                       \
156     FILE *fp = fopen(m_filename, "ab");                 \
157     if (fp != NULL && m_buf != NULL && m_offset == 0) { \
158         int i;                                          \
159         i = fwrite(m_buf, 1, m_size, fp);               \
160         ALOGD("fwrite ret %d to write %d", i, m_size);  \
161         if (i != (int) m_size) {                        \
162             ALOGD("Error in fwrite, returned %d", i);   \
163             perror("Error in write to file");           \
164         }                                               \
165     } else if (fp == NULL) {                            \
166         ALOGD("Could not write to file %s", m_filename);\
167     }                                                   \
168     if (fp) {                                           \
169         fclose(fp);                                     \
170     }                                                   \
171 }
172 #else /* FILE_DUMP_ENABLE */
173 #define INPUT_DUMP_PATH
174 #define INPUT_DUMP_EXT
175 #define OUTPUT_DUMP_PATH
176 #define OUTPUT_DUMP_EXT
177 #define GENERATE_FILE_NAMES()
178 #define CREATE_DUMP_FILE(m_filename)
179 #define DUMP_TO_FILE(m_filename, m_buf, m_size, m_offset)
180 #endif /* FILE_DUMP_ENABLE */
181 
182 } // namespace android
183 
184 #endif  // GOLDFISH_H264_DEC_H_
185