SoftMPEG2.h revision f5a92a768f8d2058d09c9b6d3d370b64723887bd
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_MPEG2_H_
18
19#define SOFT_MPEG2_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 SoftMPEG2 : public SoftVideoDecoderOMXComponent {
56    SoftMPEG2(
57            const char *name, const OMX_CALLBACKTYPE *callbacks,
58            OMX_PTR appData, OMX_COMPONENTTYPE **component);
59
60protected:
61    virtual ~SoftMPEG2();
62
63    virtual void onQueueFilled(OMX_U32 portIndex);
64    virtual void onPortFlushCompleted(OMX_U32 portIndex);
65    virtual void onReset();
66    virtual OMX_ERRORTYPE internalSetParameter(OMX_INDEXTYPE index, const OMX_PTR params);
67private:
68    // Number of input and output buffers
69    enum {
70        kNumBuffers = 8
71    };
72
73    iv_obj_t *mCodecCtx;         // Codec context
74    iv_mem_rec_t *mMemRecords;   // Memory records requested by the codec
75    size_t mNumMemRecords;       // Number of memory records requested by the codec
76
77    size_t mNumCores;            // Number of cores to be uesd by the codec
78
79    struct timeval mTimeStart;   // Time at the start of decode()
80    struct timeval mTimeEnd;     // Time at the end of decode()
81
82    // Internal buffer to be used to flush out the buffers from decoder
83    uint8_t *mFlushOutBuffer;
84
85    // Status of entries in the timestamp array
86    bool mTimeStampsValid[MAX_TIME_STAMPS];
87
88    // Timestamp array - Since codec does not take 64 bit timestamps,
89    // they are maintained in the plugin
90    OMX_S64 mTimeStamps[MAX_TIME_STAMPS];
91
92#ifdef FILE_DUMP_ENABLE
93    char mInFile[200];
94#endif /* FILE_DUMP_ENABLE */
95
96    OMX_COLOR_FORMATTYPE mOmxColorFormat;    // OMX Color format
97    IV_COLOR_FORMAT_T mIvColorFormat;        // Ittiam Color format
98
99    bool mIsInFlush;        // codec is flush mode
100    bool mReceivedEOS;      // EOS is receieved on input port
101    bool mInitNeeded;
102    uint32_t mNewWidth;
103    uint32_t mNewHeight;
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 mWaitForI;
109
110    status_t initDecoder();
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();
119
120    bool 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(SoftMPEG2);
128};
129
130#ifdef FILE_DUMP_ENABLE
131
132#define INPUT_DUMP_PATH     "/sdcard/media/mpeg2d_input"
133#define INPUT_DUMP_EXT      "m2v"
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_MPEG2_H_
180