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 #ifndef H264_UTILS_H
     29 #define H264_UTILS_H
     30 
     31 /*========================================================================
     32 
     33                                  O p e n M M
     34          U t i l i t i e s   a n d   H e l p e r   R o u t i n e s
     35 
     36 *//** @file H264_Utils.h
     37 This module contains H264 video decoder utilities and helper routines.
     38 
     39 *//*====================================================================== */
     40 
     41 /* =======================================================================
     42 
     43                      INCLUDE FILES FOR MODULE
     44 
     45 ========================================================================== */
     46 #include <stdio.h>
     47 #include "Map.h"
     48 #include "qtypes.h"
     49 #include "OMX_Core.h"
     50 
     51 #define STD_MIN(x,y) (((x) < (y)) ? (x) : (y))
     52 
     53 #define OMX_CORE_720P_HEIGHT 720
     54 #define OMX_CORE_720P_WIDTH 1280
     55 
     56 /* =======================================================================
     57 
     58                         DATA DECLARATIONS
     59 
     60 ========================================================================== */
     61 
     62 /* -----------------------------------------------------------------------
     63 ** Constant / Define Declarations
     64 ** ----------------------------------------------------------------------- */
     65 // Common format block header definitions
     66 #define MT_VIDEO_META_STREAM_HEADER             0x00
     67 #define MT_VIDEO_MEDIA_STREAM_HEADER            0x01
     68 #define MT_VIDEO_META_MEDIA_STREAM_HEADER       0x02
     69 
     70 // H.264 format block header definitions
     71 #define MT_VIDEO_H264_ACCESS_UNIT_FORMAT        0x00
     72 #define MT_VIDEO_H264_NAL_FORMT                 0x01
     73 #define MT_VIDEO_H264_BYTE_FORMAT               0x02
     74 #define MT_VIDEO_H264_BYTE_STREAM_FORMAT        0x00
     75 #define MT_VIDEO_H264_NAL_UNIT_STREAM_FORMAT    0x01
     76 #define MT_VIDEO_H264_FORMAT_BLOCK_HEADER_SIZE  18
     77 
     78 // MPEG-4 format block header definitions
     79 #define MT_VIDEO_MPEG4_VOP_FORMAT               0x00
     80 #define MT_VIDEO_MPEG4_SLICE_FORMAT             0x01
     81 #define MT_VIDEO_MPEG4_BYTE_FORMAT              0x02
     82 #define MT_VIDEO_MPEG4_FORMAT_BLOCK_HEADER_SIZE 15
     83 
     84 // H.263 format block header definitions
     85 #define MT_VIDEO_H263_PICTURE_FORMAT            0x00
     86 #define MT_VIDEO_H263_GOB_FORMAT                0x01
     87 #define MT_VIDEO_H263_SLICE_STRUCTURED_FORMAT   0x02
     88 #define MT_VIDEO_H263_BYTE_FORMAT               0x03
     89 #define MT_VIDEO_H263_FORMAT_BLOCK_HEADER_SIZE  16
     90 
     91 /* =======================================================================
     92 **                          Function Declarations
     93 ** ======================================================================= */
     94 
     95 /* -----------------------------------------------------------------------
     96 ** Type Declarations
     97 ** ----------------------------------------------------------------------- */
     98 
     99 // This type is used when parsing an H.264 bitstream to collect H.264 NAL
    100 // units that need to go in the meta data.
    101 struct H264ParamNalu {
    102     uint32 picSetID;
    103     uint32 seqSetID;
    104     uint32 picOrderCntType;
    105     bool frameMbsOnlyFlag;
    106     bool picOrderPresentFlag;
    107     uint32 picWidthInMbsMinus1;
    108     uint32 picHeightInMapUnitsMinus1;
    109     uint32 log2MaxFrameNumMinus4;
    110     uint32 log2MaxPicOrderCntLsbMinus4;
    111     bool deltaPicOrderAlwaysZeroFlag;
    112     //std::vector<uint8> nalu;
    113     uint32 nalu;
    114     uint32 crop_left;
    115     uint32 crop_right;
    116     uint32 crop_top;
    117     uint32 crop_bot;
    118 };
    119 //typedef map<uint32, H264ParamNalu> H264ParamNaluSet;
    120 typedef Map<uint32, H264ParamNalu *> H264ParamNaluSet;
    121 
    122 typedef enum {
    123   NALU_TYPE_UNSPECIFIED = 0,
    124   NALU_TYPE_NON_IDR,
    125   NALU_TYPE_PARTITION_A,
    126   NALU_TYPE_PARTITION_B,
    127   NALU_TYPE_PARTITION_C,
    128   NALU_TYPE_IDR,
    129   NALU_TYPE_SEI,
    130   NALU_TYPE_SPS,
    131   NALU_TYPE_PPS,
    132   NALU_TYPE_ACCESS_DELIM,
    133   NALU_TYPE_EOSEQ,
    134   NALU_TYPE_EOSTREAM,
    135   NALU_TYPE_FILLER_DATA,
    136   NALU_TYPE_RESERVED,
    137 } NALU_TYPE;
    138 
    139 // NAL header information
    140 typedef struct {
    141   uint32 nal_ref_idc;
    142   uint32 nalu_type;
    143   uint32 forbidden_zero_bit;
    144 } NALU;
    145 
    146 // This structure contains persistent information about an H.264 stream as it
    147 // is parsed.
    148 //struct H264StreamInfo {
    149 //    H264ParamNaluSet pic;
    150 //    H264ParamNaluSet seq;
    151 //};
    152 
    153 class RbspParser
    154 /******************************************************************************
    155  ** This class is used to convert an H.264 NALU (network abstraction layer
    156  ** unit) into RBSP (raw byte sequence payload) and extract bits from it.
    157  *****************************************************************************/
    158 {
    159 public:
    160     RbspParser (const uint8 *begin, const uint8 *end);
    161 
    162     virtual ~RbspParser ();
    163 
    164     uint32 next ();
    165     void advance ();
    166     uint32 u (uint32 n);
    167     uint32 ue ();
    168     int32 se ();
    169 
    170 private:
    171     const     uint8 *begin, *end;
    172     int32     pos;
    173     uint32    bit;
    174     uint32    cursor;
    175     bool      advanceNeeded;
    176 };
    177 
    178 class H264_Utils
    179 {
    180 public:
    181     H264_Utils();
    182     ~H264_Utils();
    183     void initialize_frame_checking_environment();
    184     void allocate_rbsp_buffer(uint32 inputBufferSize);
    185     bool isNewFrame(OMX_IN OMX_U8 *bitstream,
    186                     OMX_IN OMX_U32 bitstream_length,
    187                     OMX_IN OMX_U32 size_of_nal_length_field,
    188                     OMX_OUT OMX_BOOL &isNewFrame);
    189 
    190 private:
    191     boolean extract_rbsp(OMX_IN   OMX_U8  *buffer,
    192                          OMX_IN   OMX_U32 buffer_length,
    193                          OMX_IN   OMX_U32 size_of_nal_length_field,
    194                          OMX_OUT  OMX_U8  *rbsp_bistream,
    195                          OMX_OUT  OMX_U32 *rbsp_length,
    196                          OMX_OUT  NALU    *nal_unit);
    197 
    198     unsigned          m_height;
    199     unsigned          m_width;
    200     H264ParamNaluSet  pic;
    201     H264ParamNaluSet  seq;
    202     uint8             *m_rbspBytes;
    203     NALU              m_prv_nalu;
    204     bool              m_forceToStichNextNAL;
    205     bool              m_au_data;
    206 };
    207 
    208 
    209 #endif /* H264_UTILS_H */
    210