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