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 SOFT_H264_DEC_H_
18 
19 #define SOFT_H264_DEC_H_
20 
21 #include "SoftVideoDecoderOMXComponent.h"
22 #include <sys/time.h>
23 
24 namespace android {
25 
26 /** Number of entries in the time-stamp array */
27 #define MAX_TIME_STAMPS 64
28 
29 /** Maximum number of cores supported by the codec */
30 #define CODEC_MAX_NUM_CORES 4
31 
32 #define CODEC_MAX_WIDTH     1920
33 
34 #define CODEC_MAX_HEIGHT    1088
35 
36 /** Input buffer size */
37 #define INPUT_BUF_SIZE (1024 * 1024)
38 
39 #define MIN(a, b) ((a) < (b)) ? (a) : (b)
40 
41 /** Used to remove warnings about unused parameters */
42 #define UNUSED(x) ((void)(x))
43 
44 /** Get time */
45 #define GETTIME(a, b) gettimeofday(a, b);
46 
47 /** Compute difference between start and end */
48 #define TIME_DIFF(start, end, diff) \
49     diff = ((end.tv_sec - start.tv_sec) * 1000000) + \
50             (end.tv_usec - start.tv_usec);
51 
52 struct SoftAVC : public SoftVideoDecoderOMXComponent {
53     SoftAVC(const char *name, const OMX_CALLBACKTYPE *callbacks,
54             OMX_PTR appData, OMX_COMPONENTTYPE **component);
55 
56 protected:
57     virtual ~SoftAVC();
58 
59     virtual void onQueueFilled(OMX_U32 portIndex);
60     virtual void onPortFlushCompleted(OMX_U32 portIndex);
61     virtual void onReset();
62 private:
63     // Number of input and output buffers
64     enum {
65         kNumBuffers = 8
66     };
67 
68     iv_obj_t *mCodecCtx;         // Codec context
69 
70     size_t mNumCores;            // Number of cores to be uesd by the codec
71 
72     struct timeval mTimeStart;   // Time at the start of decode()
73     struct timeval mTimeEnd;     // Time at the end of decode()
74 
75     // Internal buffer to be used to flush out the buffers from decoder
76     uint8_t *mFlushOutBuffer;
77 
78     // Status of entries in the timestamp array
79     bool mTimeStampsValid[MAX_TIME_STAMPS];
80 
81     // Timestamp array - Since codec does not take 64 bit timestamps,
82     // they are maintained in the plugin
83     OMX_S64 mTimeStamps[MAX_TIME_STAMPS];
84 
85 #ifdef FILE_DUMP_ENABLE
86     char mInFile[200];
87 #endif /* FILE_DUMP_ENABLE */
88 
89     OMX_COLOR_FORMATTYPE mOmxColorFormat;    // OMX Color format
90     IV_COLOR_FORMAT_T mIvColorFormat;        // Ittiam Color format
91 
92     bool mIsInFlush;        // codec is flush mode
93     bool mReceivedEOS;      // EOS is receieved on input port
94 
95     // The input stream has changed to a different resolution, which is still supported by the
96     // codec. So the codec is switching to decode the new resolution.
97     bool mChangingResolution;
98     bool mFlushNeeded;
99     bool mSignalledError;
100     size_t mStride;
101 
102     status_t initDecoder();
103     status_t deInitDecoder();
104     status_t setFlushMode();
105     status_t setParams(size_t stride);
106     void logVersion();
107     status_t setNumCores();
108     status_t resetDecoder();
109     status_t resetPlugin();
110 
111 
112     bool setDecodeArgs(
113             ivd_video_decode_ip_t *ps_dec_ip,
114             ivd_video_decode_op_t *ps_dec_op,
115             OMX_BUFFERHEADERTYPE *inHeader,
116             OMX_BUFFERHEADERTYPE *outHeader,
117             size_t timeStampIx);
118 
119     DISALLOW_EVIL_CONSTRUCTORS(SoftAVC);
120 };
121 
122 #ifdef FILE_DUMP_ENABLE
123 
124 #define INPUT_DUMP_PATH     "/sdcard/media/avcd_input"
125 #define INPUT_DUMP_EXT      "h264"
126 
127 #define GENERATE_FILE_NAMES() {                         \
128     GETTIME(&mTimeStart, NULL);                         \
129     strcpy(mInFile, "");                                \
130     sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH,  \
131             mTimeStart.tv_sec, mTimeStart.tv_usec,      \
132             INPUT_DUMP_EXT);                            \
133 }
134 
135 #define CREATE_DUMP_FILE(m_filename) {                  \
136     FILE *fp = fopen(m_filename, "wb");                 \
137     if (fp != NULL) {                                   \
138         fclose(fp);                                     \
139     } else {                                            \
140         ALOGD("Could not open file %s", m_filename);    \
141     }                                                   \
142 }
143 #define DUMP_TO_FILE(m_filename, m_buf, m_size)         \
144 {                                                       \
145     FILE *fp = fopen(m_filename, "ab");                 \
146     if (fp != NULL && m_buf != NULL) {                  \
147         int i;                                          \
148         i = fwrite(m_buf, 1, m_size, fp);               \
149         ALOGD("fwrite ret %d to write %d", i, m_size);  \
150         if (i != (int) m_size) {                        \
151             ALOGD("Error in fwrite, returned %d", i);   \
152             perror("Error in write to file");           \
153         }                                               \
154         fclose(fp);                                     \
155     } else {                                            \
156         ALOGD("Could not write to file %s", m_filename);\
157     }                                                   \
158 }
159 #else /* FILE_DUMP_ENABLE */
160 #define INPUT_DUMP_PATH
161 #define INPUT_DUMP_EXT
162 #define OUTPUT_DUMP_PATH
163 #define OUTPUT_DUMP_EXT
164 #define GENERATE_FILE_NAMES()
165 #define CREATE_DUMP_FILE(m_filename)
166 #define DUMP_TO_FILE(m_filename, m_buf, m_size)
167 #endif /* FILE_DUMP_ENABLE */
168 
169 } // namespace android
170 
171 #endif  // SOFT_H264_DEC_H_
172