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