Home | History | Annotate | Download | only in mpeg2dec
      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 
     24 namespace 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 
     55 struct SoftMPEG2 : public SoftVideoDecoderOMXComponent {
     56     SoftMPEG2(
     57             const char *name, const OMX_CALLBACKTYPE *callbacks,
     58             OMX_PTR appData, OMX_COMPONENTTYPE **component);
     59 
     60 protected:
     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);
     67 private:
     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     size_t mStride;
    110 
    111     status_t initDecoder();
    112     status_t deInitDecoder();
    113     status_t setFlushMode();
    114     status_t setParams(size_t stride);
    115     void logVersion();
    116     status_t setNumCores();
    117     status_t resetDecoder();
    118     status_t resetPlugin();
    119     status_t reInitDecoder();
    120 
    121     bool setDecodeArgs(
    122             ivd_video_decode_ip_t *ps_dec_ip,
    123             ivd_video_decode_op_t *ps_dec_op,
    124             OMX_BUFFERHEADERTYPE *inHeader,
    125             OMX_BUFFERHEADERTYPE *outHeader,
    126             size_t timeStampIx);
    127 
    128     DISALLOW_EVIL_CONSTRUCTORS(SoftMPEG2);
    129 };
    130 
    131 #ifdef FILE_DUMP_ENABLE
    132 
    133 #define INPUT_DUMP_PATH     "/sdcard/media/mpeg2d_input"
    134 #define INPUT_DUMP_EXT      "m2v"
    135 
    136 #define GENERATE_FILE_NAMES() {                         \
    137     GETTIME(&mTimeStart, NULL);                         \
    138     strcpy(mInFile, "");                                \
    139     sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH,  \
    140             mTimeStart.tv_sec, mTimeStart.tv_usec,      \
    141             INPUT_DUMP_EXT);                            \
    142 }
    143 
    144 #define CREATE_DUMP_FILE(m_filename) {                  \
    145     FILE *fp = fopen(m_filename, "wb");                 \
    146     if (fp != NULL) {                                   \
    147         fclose(fp);                                     \
    148     } else {                                            \
    149         ALOGD("Could not open file %s", m_filename);    \
    150     }                                                   \
    151 }
    152 #define DUMP_TO_FILE(m_filename, m_buf, m_size)         \
    153 {                                                       \
    154     FILE *fp = fopen(m_filename, "ab");                 \
    155     if (fp != NULL && m_buf != NULL) {                  \
    156         int i;                                          \
    157         i = fwrite(m_buf, 1, m_size, fp);               \
    158         ALOGD("fwrite ret %d to write %d", i, m_size);  \
    159         if (i != (int)m_size) {                         \
    160             ALOGD("Error in fwrite, returned %d", i);   \
    161             perror("Error in write to file");           \
    162         }                                               \
    163         fclose(fp);                                     \
    164     } else {                                            \
    165         ALOGD("Could not write to file %s", m_filename);\
    166     }                                                   \
    167 }
    168 #else /* FILE_DUMP_ENABLE */
    169 #define INPUT_DUMP_PATH
    170 #define INPUT_DUMP_EXT
    171 #define OUTPUT_DUMP_PATH
    172 #define OUTPUT_DUMP_EXT
    173 #define GENERATE_FILE_NAMES()
    174 #define CREATE_DUMP_FILE(m_filename)
    175 #define DUMP_TO_FILE(m_filename, m_buf, m_size)
    176 #endif /* FILE_DUMP_ENABLE */
    177 
    178 } // namespace android
    179 
    180 #endif  // SOFT_MPEG2_H_
    181