Home | History | Annotate | Download | only in inc
      1 /*--------------------------------------------------------------------------
      2 Copyright (c) 2010, Code Aurora Forum. All rights reserved.
      3 
      4 Redistribution and use in source and binary forms, with or without
      5 modification, are permitted provided that the following conditions are met:
      6     * Redistributions of source code must retain the above copyright
      7       notice, this list of conditions and the following disclaimer.
      8     * Redistributions in binary form must reproduce the above copyright
      9       notice, this list of conditions and the following disclaimer in the
     10       documentation and/or other materials provided with the distribution.
     11     * Neither the name of Code Aurora nor
     12       the names of its contributors may be used to endorse or promote
     13       products derived from this software without specific prior written
     14       permission.
     15 
     16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19 NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 --------------------------------------------------------------------------*/
     28 
     29 #ifndef __OMX_VIDEO_BASE_H__
     30 #define __OMX_VIDEO_BASE_H__
     31 /*============================================================================
     32                             O p e n M A X   Component
     33                                 Video Encoder
     34 
     35 *//** @file comx_video_base.h
     36   This module contains the class definition for openMAX decoder component.
     37 
     38 *//*========================================================================*/
     39 
     40 //////////////////////////////////////////////////////////////////////////////
     41 //                             Include Files
     42 //////////////////////////////////////////////////////////////////////////////
     43 
     44 #include<stdlib.h>
     45 #include <stdio.h>
     46 #ifdef _ANDROID_
     47   #include <binder/MemoryHeapBase.h>
     48 #endif // _ANDROID_
     49 #include <pthread.h>
     50 #include <semaphore.h>
     51 #include <linux/msm_vidc_enc.h>
     52 #include "OMX_Core.h"
     53 #include "OMX_QCOMExtns.h"
     54 #include "qc_omx_component.h"
     55 #include "omx_video_common.h"
     56 
     57 
     58 #ifdef _ANDROID_
     59 using namespace android;
     60 // local pmem heap object
     61 class VideoHeap : public MemoryHeapBase
     62 {
     63 public:
     64   VideoHeap(int fd, size_t size, void* base);
     65   virtual ~VideoHeap() {}
     66 };
     67 
     68 #include <utils/Log.h>
     69 //#define LOG_TAG "OMX-VENC-720p"
     70 #ifdef ENABLE_DEBUG_LOW
     71 #define DEBUG_PRINT_LOW ALOGE
     72 #else
     73 #define DEBUG_PRINT_LOW
     74 #endif
     75 #ifdef ENABLE_DEBUG_HIGH
     76 #define DEBUG_PRINT_HIGH ALOGE
     77 #else
     78 #define DEBUG_PRINT_HIGH
     79 #endif
     80 #ifdef ENABLE_DEBUG_ERROR
     81 #define DEBUG_PRINT_ERROR ALOGE
     82 #else
     83 #define DEBUG_PRINT_ERROR
     84 #endif
     85 #endif // _ANDROID_
     86 //////////////////////////////////////////////////////////////////////////////
     87 //                       Module specific globals
     88 //////////////////////////////////////////////////////////////////////////////
     89 
     90 #define OMX_SPEC_VERSION  0x00000101
     91 
     92 
     93 //////////////////////////////////////////////////////////////////////////////
     94 //               Macros
     95 //////////////////////////////////////////////////////////////////////////////
     96 #define PrintFrameHdr(bufHdr) DEBUG_PRINT("bufHdr %x buf %x size %d TS %d\n",\
     97                        (unsigned) bufHdr,\
     98                        (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->pBuffer,\
     99                        (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nFilledLen,\
    100                        (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nTimeStamp)
    101 
    102 // BitMask Management logic
    103 #define BITS_PER_BYTE        32
    104 #define BITMASK_SIZE(mIndex) (((mIndex) + BITS_PER_BYTE - 1)/BITS_PER_BYTE)
    105 #define BITMASK_OFFSET(mIndex) ((mIndex)/BITS_PER_BYTE)
    106 #define BITMASK_FLAG(mIndex) (1 << ((mIndex) % BITS_PER_BYTE))
    107 #define BITMASK_CLEAR(mArray,mIndex) (mArray)[BITMASK_OFFSET(mIndex)] \
    108         &=  ~(BITMASK_FLAG(mIndex))
    109 #define BITMASK_SET(mArray,mIndex)  (mArray)[BITMASK_OFFSET(mIndex)] \
    110         |=  BITMASK_FLAG(mIndex)
    111 #define BITMASK_PRESENT(mArray,mIndex) ((mArray)[BITMASK_OFFSET(mIndex)] \
    112         & BITMASK_FLAG(mIndex))
    113 #define BITMASK_ABSENT(mArray,mIndex) (((mArray)[BITMASK_OFFSET(mIndex)] \
    114         & BITMASK_FLAG(mIndex)) == 0x0)
    115 #define BITMASK_PRESENT(mArray,mIndex) ((mArray)[BITMASK_OFFSET(mIndex)] \
    116         & BITMASK_FLAG(mIndex))
    117 #define BITMASK_ABSENT(mArray,mIndex) (((mArray)[BITMASK_OFFSET(mIndex)] \
    118         & BITMASK_FLAG(mIndex)) == 0x0)
    119 
    120 void* message_thread(void *);
    121 // OMX video class
    122 class omx_video: public qc_omx_component
    123 {
    124 
    125 public:
    126   omx_video();  // constructor
    127   virtual ~omx_video();  // destructor
    128 
    129   // virtual int async_message_process (void *context, void* message);
    130   void process_event_cb(void *ctxt,unsigned char id);
    131 
    132   OMX_ERRORTYPE allocate_buffer(
    133                                OMX_HANDLETYPE hComp,
    134                                OMX_BUFFERHEADERTYPE **bufferHdr,
    135                                OMX_U32 port,
    136                                OMX_PTR appData,
    137                                OMX_U32 bytes
    138                                );
    139 
    140 
    141   virtual OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE hComp)= 0;
    142 
    143   virtual OMX_ERRORTYPE component_init(OMX_STRING role)= 0;
    144 
    145   virtual OMX_U32 dev_stop(void) = 0;
    146   virtual OMX_U32 dev_pause(void) = 0;
    147   virtual OMX_U32 dev_start(void) = 0;
    148   virtual OMX_U32 dev_flush(unsigned) = 0;
    149   virtual OMX_U32 dev_resume(void) = 0;
    150   virtual bool dev_use_buf(void *,unsigned) = 0;
    151   virtual bool dev_free_buf(void *,unsigned) = 0;
    152   virtual bool dev_empty_buf(void *, void *) = 0;
    153   virtual bool dev_fill_buf(void *buffer, void *) = 0;
    154   virtual bool dev_get_buf_req(OMX_U32 *,OMX_U32 *,OMX_U32 *,OMX_U32) = 0;
    155 
    156 
    157   OMX_ERRORTYPE component_role_enum(
    158                                    OMX_HANDLETYPE hComp,
    159                                    OMX_U8 *role,
    160                                    OMX_U32 index
    161                                    );
    162 
    163   OMX_ERRORTYPE component_tunnel_request(
    164                                         OMX_HANDLETYPE hComp,
    165                                         OMX_U32 port,
    166                                         OMX_HANDLETYPE  peerComponent,
    167                                         OMX_U32 peerPort,
    168                                         OMX_TUNNELSETUPTYPE *tunnelSetup
    169                                         );
    170 
    171   OMX_ERRORTYPE empty_this_buffer(
    172                                  OMX_HANDLETYPE hComp,
    173                                  OMX_BUFFERHEADERTYPE *buffer
    174                                  );
    175 
    176 
    177 
    178   OMX_ERRORTYPE fill_this_buffer(
    179                                 OMX_HANDLETYPE hComp,
    180                                 OMX_BUFFERHEADERTYPE *buffer
    181                                 );
    182 
    183 
    184   OMX_ERRORTYPE free_buffer(
    185                            OMX_HANDLETYPE hComp,
    186                            OMX_U32 port,
    187                            OMX_BUFFERHEADERTYPE *buffer
    188                            );
    189 
    190   OMX_ERRORTYPE get_component_version(
    191                                      OMX_HANDLETYPE hComp,
    192                                      OMX_STRING componentName,
    193                                      OMX_VERSIONTYPE *componentVersion,
    194                                      OMX_VERSIONTYPE *specVersion,
    195                                      OMX_UUIDTYPE *componentUUID
    196                                      );
    197 
    198   OMX_ERRORTYPE get_config(
    199                           OMX_HANDLETYPE hComp,
    200                           OMX_INDEXTYPE configIndex,
    201                           OMX_PTR configData
    202                           );
    203 
    204   OMX_ERRORTYPE get_extension_index(
    205                                    OMX_HANDLETYPE hComp,
    206                                    OMX_STRING paramName,
    207                                    OMX_INDEXTYPE *indexType
    208                                    );
    209 
    210   OMX_ERRORTYPE get_parameter(OMX_HANDLETYPE hComp,
    211                               OMX_INDEXTYPE  paramIndex,
    212                               OMX_PTR        paramData);
    213 
    214   OMX_ERRORTYPE get_state(OMX_HANDLETYPE hComp,
    215                           OMX_STATETYPE *state);
    216 
    217 
    218 
    219   OMX_ERRORTYPE send_command(OMX_HANDLETYPE  hComp,
    220                              OMX_COMMANDTYPE cmd,
    221                              OMX_U32         param1,
    222                              OMX_PTR         cmdData);
    223 
    224 
    225   OMX_ERRORTYPE set_callbacks(OMX_HANDLETYPE   hComp,
    226                               OMX_CALLBACKTYPE *callbacks,
    227                               OMX_PTR          appData);
    228 
    229   virtual OMX_ERRORTYPE set_config(OMX_HANDLETYPE hComp,
    230                            OMX_INDEXTYPE  configIndex,
    231                            OMX_PTR        configData) = 0;
    232 
    233   virtual OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE hComp,
    234                                       OMX_INDEXTYPE  paramIndex,
    235                                       OMX_PTR        paramData) =0;
    236 
    237   OMX_ERRORTYPE use_buffer(OMX_HANDLETYPE      hComp,
    238                            OMX_BUFFERHEADERTYPE **bufferHdr,
    239                            OMX_U32              port,
    240                            OMX_PTR              appData,
    241                            OMX_U32              bytes,
    242                            OMX_U8               *buffer);
    243 
    244 
    245   OMX_ERRORTYPE use_EGL_image(OMX_HANDLETYPE     hComp,
    246                               OMX_BUFFERHEADERTYPE **bufferHdr,
    247                               OMX_U32              port,
    248                               OMX_PTR              appData,
    249                               void *               eglImage);
    250 
    251 
    252 
    253   int  m_pipe_in;
    254   int  m_pipe_out;
    255 
    256   pthread_t msg_thread_id;
    257   pthread_t async_thread_id;
    258 
    259   OMX_U8 m_nkind[128];
    260 
    261 
    262   //int *input_pmem_fd;
    263   //int *output_pmem_fd;
    264   struct pmem *m_pInput_pmem;
    265   struct pmem *m_pOutput_pmem;
    266 
    267 
    268 public:
    269   // Bit Positions
    270   enum flags_bit_positions
    271   {
    272     // Defer transition to IDLE
    273     OMX_COMPONENT_IDLE_PENDING            =0x1,
    274     // Defer transition to LOADING
    275     OMX_COMPONENT_LOADING_PENDING         =0x2,
    276     // First  Buffer Pending
    277     OMX_COMPONENT_FIRST_BUFFER_PENDING    =0x3,
    278     // Second Buffer Pending
    279     OMX_COMPONENT_SECOND_BUFFER_PENDING   =0x4,
    280     // Defer transition to Enable
    281     OMX_COMPONENT_INPUT_ENABLE_PENDING    =0x5,
    282     // Defer transition to Enable
    283     OMX_COMPONENT_OUTPUT_ENABLE_PENDING   =0x6,
    284     // Defer transition to Disable
    285     OMX_COMPONENT_INPUT_DISABLE_PENDING   =0x7,
    286     // Defer transition to Disable
    287     OMX_COMPONENT_OUTPUT_DISABLE_PENDING  =0x8,
    288     //defer flush notification
    289     OMX_COMPONENT_OUTPUT_FLUSH_PENDING    =0x9,
    290     OMX_COMPONENT_INPUT_FLUSH_PENDING    =0xA,
    291     OMX_COMPONENT_PAUSE_PENDING          =0xB,
    292     OMX_COMPONENT_EXECUTE_PENDING        =0xC
    293 
    294   };
    295 
    296   // Deferred callback identifiers
    297   enum
    298   {
    299     //Event Callbacks from the venc component thread context
    300     OMX_COMPONENT_GENERATE_EVENT       = 0x1,
    301     //Buffer Done callbacks from the venc component thread context
    302     OMX_COMPONENT_GENERATE_BUFFER_DONE = 0x2,
    303     //Frame Done callbacks from the venc component thread context
    304     OMX_COMPONENT_GENERATE_FRAME_DONE  = 0x3,
    305     //Buffer Done callbacks from the venc component thread context
    306     OMX_COMPONENT_GENERATE_FTB         = 0x4,
    307     //Frame Done callbacks from the venc component thread context
    308     OMX_COMPONENT_GENERATE_ETB         = 0x5,
    309     //Command
    310     OMX_COMPONENT_GENERATE_COMMAND     = 0x6,
    311     //Push-Pending Buffers
    312     OMX_COMPONENT_PUSH_PENDING_BUFS    = 0x7,
    313     // Empty Buffer Done callbacks
    314     OMX_COMPONENT_GENERATE_EBD         = 0x8,
    315     //Flush Event Callbacks from the venc component thread context
    316     OMX_COMPONENT_GENERATE_EVENT_FLUSH       = 0x9,
    317     OMX_COMPONENT_GENERATE_EVENT_INPUT_FLUSH = 0x0A,
    318     OMX_COMPONENT_GENERATE_EVENT_OUTPUT_FLUSH = 0x0B,
    319     OMX_COMPONENT_GENERATE_FBD = 0xc,
    320     OMX_COMPONENT_GENERATE_START_DONE = 0xD,
    321     OMX_COMPONENT_GENERATE_PAUSE_DONE = 0xE,
    322     OMX_COMPONENT_GENERATE_RESUME_DONE = 0xF,
    323     OMX_COMPONENT_GENERATE_STOP_DONE = 0x10,
    324     OMX_COMPONENT_GENERATE_HARDWARE_ERROR = 0x11
    325   };
    326 
    327   struct omx_event
    328   {
    329     unsigned param1;
    330     unsigned param2;
    331     unsigned id;
    332   };
    333 
    334   struct omx_cmd_queue
    335   {
    336     omx_event m_q[OMX_CORE_CONTROL_CMDQ_SIZE];
    337     unsigned m_read;
    338     unsigned m_write;
    339     unsigned m_size;
    340 
    341     omx_cmd_queue();
    342     ~omx_cmd_queue();
    343     bool insert_entry(unsigned p1, unsigned p2, unsigned id);
    344     bool pop_entry(unsigned *p1,unsigned *p2, unsigned *id);
    345     // get msgtype of the first ele from the queue
    346     unsigned get_q_msg_type();
    347 
    348   };
    349 
    350   bool allocate_done(void);
    351   bool allocate_input_done(void);
    352   bool allocate_output_done(void);
    353 
    354   OMX_ERRORTYPE free_input_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
    355   OMX_ERRORTYPE free_output_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
    356 
    357   OMX_ERRORTYPE allocate_input_buffer(OMX_HANDLETYPE       hComp,
    358                                       OMX_BUFFERHEADERTYPE **bufferHdr,
    359                                       OMX_U32              port,
    360                                       OMX_PTR              appData,
    361                                       OMX_U32              bytes);
    362 
    363   OMX_ERRORTYPE allocate_output_buffer(OMX_HANDLETYPE       hComp,
    364                                        OMX_BUFFERHEADERTYPE **bufferHdr,
    365                                        OMX_U32 port,OMX_PTR appData,
    366                                        OMX_U32              bytes);
    367 
    368   OMX_ERRORTYPE use_input_buffer(OMX_HANDLETYPE hComp,
    369                                  OMX_BUFFERHEADERTYPE  **bufferHdr,
    370                                  OMX_U32               port,
    371                                  OMX_PTR               appData,
    372                                  OMX_U32               bytes,
    373                                  OMX_U8                *buffer);
    374 
    375   OMX_ERRORTYPE use_output_buffer(OMX_HANDLETYPE hComp,
    376                                   OMX_BUFFERHEADERTYPE   **bufferHdr,
    377                                   OMX_U32                port,
    378                                   OMX_PTR                appData,
    379                                   OMX_U32                bytes,
    380                                   OMX_U8                 *buffer);
    381 
    382   bool execute_omx_flush(OMX_U32);
    383   bool execute_output_flush(void);
    384   bool execute_input_flush(void);
    385   OMX_ERRORTYPE empty_buffer_done(OMX_HANDLETYPE hComp,
    386                                   OMX_BUFFERHEADERTYPE * buffer);
    387 
    388   OMX_ERRORTYPE fill_buffer_done(OMX_HANDLETYPE hComp,
    389                                  OMX_BUFFERHEADERTYPE * buffer);
    390   OMX_ERRORTYPE empty_this_buffer_proxy(OMX_HANDLETYPE       hComp,
    391                                         OMX_BUFFERHEADERTYPE *buffer);
    392 
    393   OMX_ERRORTYPE fill_this_buffer_proxy(OMX_HANDLETYPE       hComp,
    394                                        OMX_BUFFERHEADERTYPE *buffer);
    395   bool release_done();
    396 
    397   bool release_output_done();
    398   bool release_input_done();
    399 
    400   OMX_ERRORTYPE send_command_proxy(OMX_HANDLETYPE  hComp,
    401                                    OMX_COMMANDTYPE cmd,
    402                                    OMX_U32         param1,
    403                                    OMX_PTR         cmdData);
    404   bool post_event( unsigned int p1,
    405                    unsigned int p2,
    406                    unsigned int id
    407                  );
    408 
    409   inline void omx_report_error ()
    410   {
    411     m_state = OMX_StateInvalid;
    412     if(m_pCallbacks.EventHandler)
    413     {
    414       m_pCallbacks.EventHandler(&m_cmp,m_app_data,
    415                                 OMX_EventError,OMX_ErrorHardware,0,NULL);
    416       m_pCallbacks.EventHandler(&m_cmp,m_app_data,
    417                                 OMX_EventError, OMX_ErrorInvalidState,0, NULL);
    418     }
    419   }
    420 
    421 
    422   //*************************************************************
    423   //*******************MEMBER VARIABLES *************************
    424   //*************************************************************
    425 
    426   pthread_mutex_t       m_lock;
    427   sem_t                 m_cmd_lock;
    428 
    429 
    430   //sem to handle the minimum procesing of commands
    431 
    432 
    433   // compression format
    434   //OMX_VIDEO_CODINGTYPE eCompressionFormat;
    435   // OMX State
    436   OMX_STATETYPE m_state;
    437   // Application data
    438   OMX_PTR m_app_data;
    439   OMX_BOOL m_use_input_pmem;
    440   OMX_BOOL m_use_output_pmem;
    441   // Application callbacks
    442   OMX_CALLBACKTYPE m_pCallbacks;
    443   OMX_PORT_PARAM_TYPE m_sPortParam;
    444   OMX_VIDEO_PARAM_PROFILELEVELTYPE m_sParamProfileLevel;
    445   OMX_VIDEO_PARAM_PORTFORMATTYPE m_sInPortFormat;
    446   OMX_VIDEO_PARAM_PORTFORMATTYPE m_sOutPortFormat;
    447   OMX_PARAM_PORTDEFINITIONTYPE m_sInPortDef;
    448   OMX_PARAM_PORTDEFINITIONTYPE m_sOutPortDef;
    449   OMX_VIDEO_PARAM_MPEG4TYPE m_sParamMPEG4;
    450   OMX_VIDEO_PARAM_H263TYPE m_sParamH263;
    451   OMX_VIDEO_PARAM_AVCTYPE m_sParamAVC;
    452   OMX_PORT_PARAM_TYPE m_sPortParam_img;
    453   OMX_PORT_PARAM_TYPE m_sPortParam_audio;
    454   OMX_VIDEO_CONFIG_BITRATETYPE m_sConfigBitrate;
    455   OMX_CONFIG_FRAMERATETYPE m_sConfigFramerate;
    456   OMX_VIDEO_PARAM_BITRATETYPE m_sParamBitrate;
    457   OMX_PRIORITYMGMTTYPE m_sPriorityMgmt;
    458   OMX_PARAM_BUFFERSUPPLIERTYPE m_sInBufSupplier;
    459   OMX_PARAM_BUFFERSUPPLIERTYPE m_sOutBufSupplier;
    460   OMX_CONFIG_ROTATIONTYPE m_sConfigFrameRotation;
    461   OMX_CONFIG_INTRAREFRESHVOPTYPE m_sConfigIntraRefreshVOP;
    462   OMX_VIDEO_PARAM_QUANTIZATIONTYPE m_sSessionQuantization;
    463   OMX_VIDEO_PARAM_AVCSLICEFMO m_sAVCSliceFMO;
    464 
    465   // fill this buffer queue
    466   omx_cmd_queue         m_ftb_q;
    467   // Command Q for rest of the events
    468   omx_cmd_queue         m_cmd_q;
    469   omx_cmd_queue         m_etb_q;
    470   // Input memory pointer
    471   OMX_BUFFERHEADERTYPE  *m_inp_mem_ptr;
    472   // Output memory pointer
    473   OMX_BUFFERHEADERTYPE  *m_out_mem_ptr;
    474 
    475   bool input_flush_progress;
    476   bool output_flush_progress;
    477   bool input_use_buffer;
    478   bool output_use_buffer;
    479   int pending_input_buffers;
    480   int pending_output_buffers;
    481 
    482   unsigned int m_out_bm_count;
    483   unsigned int m_inp_bm_count;
    484   unsigned int m_flags;
    485   unsigned int m_etb_count;
    486   unsigned int m_fbd_count;
    487 #ifdef _ANDROID_
    488   // Heap pointer to frame buffers
    489   sp<MemoryHeapBase>    m_heap_ptr;
    490 #endif //_ANDROID_
    491   // to know whether Event Port Settings change has been triggered or not.
    492   bool m_event_port_settings_sent;
    493   OMX_U8                m_cRole[OMX_MAX_STRINGNAME_SIZE];
    494 };
    495 
    496 #endif // __OMX_VIDEO_BASE_H__
    497