Home | History | Annotate | Download | only in decoder
      1 /******************************************************************************
      2  *
      3  * Copyright (C) 2015 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 #ifndef __IMPEG2D_STRUCTS_H__
     21 #define __IMPEG2D_STRUCTS_H__
     22 
     23 /* Decoder needs at least 4 reference buffers in order to support format conversion in a thread and
     24 to support B pictures. Because of format conversion in a thread, codec delay is now 2 frames instead of 1.
     25 To reduce this delay, format conversion has to wait for MB status before converting for B pictures.
     26 To avoid this check the delay is increased to 2 and hence number of reference frames minimum is 4.
     27 Because of temporal dependency in deinterlacer one additional buffer is also needed */
     28 #define NUM_INT_FRAME_BUFFERS                     5
     29 
     30 
     31 #define MAX_WIDTH               4096
     32 #define MAX_HEIGHT              2160
     33 
     34 #define MIN_WIDTH               16
     35 #define MIN_HEIGHT              16
     36 
     37 
     38 #define MAX_FRM_SIZE            (MAX_WIDTH * MAX_HEIGHT * 2)  /* Supports only 420P and 422ILE */
     39 
     40 #define DEC_ORDER               0
     41 
     42 #define MAX_BITSTREAM_BUFFER_SIZE       2000 * 1024
     43 #define MIN_BUFFER_BYTES_AT_EOS 8
     44 
     45 /* Flag to signal that buffer is held by deinterlacing */
     46 #define MPEG2_BUF_MGR_DEINT (BUF_MGR_DISP << 1)
     47 
     48 typedef enum
     49 {
     50     CMD_PROCESS,
     51     CMD_FMTCONV,
     52 }e_jobq_cmd_t;
     53 
     54 /**
     55  * Structure to represent a processing job entry
     56  */
     57 typedef struct
     58 {
     59     /**
     60      * Command
     61      * Currently: PROCESS, FMTCONV are the only two jobs
     62      */
     63     WORD32 i4_cmd;
     64 
     65     /**
     66      * MB y of the starting MB
     67      */
     68     WORD16 i2_start_mb_y;
     69 
     70     /**
     71      * MB y of the last MB
     72      */
     73 
     74     WORD16 i2_end_mb_y;
     75 
     76     /**
     77      * Bitstream offset for the current job
     78      */
     79     WORD32 i4_bistream_ofst;
     80 
     81 }job_t;
     82 
     83 typedef struct
     84 {
     85     /* Params of the reference buffer used as input to MC */
     86     UWORD32 u4_src_wd;
     87     UWORD32 u4_src_offset;
     88 
     89     /* Params of the buffer where MC output will be written */
     90     UWORD32 u4_dst_wd_res_buf;
     91     UWORD32 u4_dst_wd_cur_frm;
     92     UWORD32 u4_dst_offset_res_buf;
     93     UWORD32 u4_dst_offset_cur_frm;
     94 
     95     /* Operation Parameters */
     96     UWORD32 u4_rows;
     97     UWORD32 u4_cols;
     98     UWORD32 u4_mode;
     99 }comp_mc_params_t;
    100 
    101 typedef struct
    102 {
    103     yuv_buf_t        s_ref;
    104     comp_mc_params_t s_luma;
    105     comp_mc_params_t s_chroma;
    106 }mb_mc_params_t;
    107 
    108 struct _dec_mb_params_t;
    109 
    110 typedef UWORD8 pf_inv_quant_t (WORD16 *blk,
    111                                 UWORD8 *weighting_matrix,
    112                                 UWORD8 quant_scale,
    113                                 WORD32 intra_flag,
    114                                 WORD32 i4_num_coeffs,
    115                                 WORD16 *pi2_coeffs,
    116                                 UWORD8 *pu1_pos,
    117                                 const UWORD8   *scan,
    118                                 UWORD16 *u2_def_dc_pred,
    119                                 UWORD16 u2_intra_dc_precision);
    120 
    121 typedef IMPEG2D_ERROR_CODES_T  pf_vld_inv_quant_t  (void  *dec,
    122                              WORD16       *out_addr,
    123                              const UWORD8 *scan,
    124                              UWORD16      intra_flag,
    125                              UWORD16      colr_comp,
    126                              UWORD16      d_picture);
    127 
    128 typedef void  pf_mc_t(void *, UWORD8 *, UWORD32 , UWORD8 *, UWORD32 ,
    129                  UWORD32 , UWORD32  );
    130 
    131 typedef struct dec_state_struct_t
    132 {
    133     WORD16          ai2_vld_buf[NUM_PELS_IN_BLOCK];
    134     WORD16          ai2_idct_stg1[NUM_PELS_IN_BLOCK];
    135 
    136 
    137     UWORD8          au1_intra_quant_matrix[NUM_PELS_IN_BLOCK];
    138     UWORD8          au1_inter_quant_matrix[NUM_PELS_IN_BLOCK];
    139 
    140     IMPEG2D_ERROR_CODES_T (*pf_decode_slice)(struct dec_state_struct_t *);
    141 
    142     pf_vld_inv_quant_t *pf_vld_inv_quant;
    143 
    144     pf_idct_recon_t *pf_idct_recon[4];
    145 
    146     pf_mc_t         *pf_mc[4];
    147     pf_interpred_t  *pf_fullx_halfy_8x8;
    148     pf_interpred_t  *pf_halfx_fully_8x8;
    149     pf_interpred_t  *pf_halfx_halfy_8x8;
    150     pf_interpred_t  *pf_fullx_fully_8x8;
    151 
    152 
    153     pf_interpolate_t *pf_interpolate;
    154     pf_copy_mb_t     *pf_copy_mb;
    155 
    156     pf_memset0_one_16bit_buf_t *pf_memset_16bit_8x8_linear_block;
    157     pf_memset_8bit_t    *pf_memset_8bit_8x8_block;
    158     pf_copy_yuv420p_buf_t *pf_copy_yuv420p_buf;
    159     pf_fmt_conv_yuv420p_to_yuv422ile_t *pf_fmt_conv_yuv420p_to_yuv422ile;
    160     pf_fmt_conv_yuv420p_to_yuv420sp_t  *pf_fmt_conv_yuv420p_to_yuv420sp_uv;
    161     pf_fmt_conv_yuv420p_to_yuv420sp_t  *pf_fmt_conv_yuv420p_to_yuv420sp_vu;
    162 
    163     stream_t         s_bit_stream;
    164 /* @ */
    165 
    166     UWORD16         u2_is_mpeg2; /* 0 if stream is MPEG1 1 otherwise */
    167     UWORD16         u2_frame_width;  /* Width of the frame */
    168     UWORD16         u2_frame_height; /* Height of the frame */
    169     UWORD16         u2_picture_width;
    170     UWORD16         u2_horizontal_size;
    171     UWORD16         u2_vertical_size;
    172     UWORD16         u2_create_max_width;
    173     UWORD16         u2_create_max_height;
    174     UWORD16         u2_reinit_max_width;
    175     UWORD16         u2_reinit_max_height;
    176     UWORD16         u2_header_done;
    177     UWORD16         u2_decode_header;
    178 
    179     UWORD16         u2_mb_x;
    180     UWORD16         u2_mb_y;
    181     UWORD16         u2_num_horiz_mb;
    182     UWORD16         u2_num_vert_mb;
    183     UWORD16         u2_num_flds_decoded;
    184     void            *pv_pic_buf_mg;
    185 
    186     UWORD32         u4_frm_buf_stride; /* for display Buffer */
    187 
    188     UWORD16         u2_field_dct;
    189     UWORD16         u2_read_dct_type;
    190 
    191     UWORD16         u2_read_motion_type;
    192     UWORD16         u2_motion_type;
    193 
    194     const UWORD16   *pu2_mb_type;
    195     UWORD16         u2_fld_pic;
    196     UWORD16         u2_frm_pic;
    197 
    198     yuv_buf_t       s_cur_frm_buf;
    199 
    200     UWORD16         u2_fld_parity;
    201     UWORD16         u2_def_dc_pred[MAX_COLR_COMPS];
    202 
    203     /* Variables related to Motion Vector predictors */
    204 
    205     WORD16          ai2_pred_mv[2][2][2];
    206     e_pred_direction_t   e_mb_pred;
    207     UWORD16         au2_fcode_data[2];
    208 
    209     /* Variables related to reference pictures */
    210     yuv_buf_t       as_recent_fld[2][2];
    211 
    212     UWORD8          u1_quant_scale;
    213     UWORD16         u2_num_mbs_left;
    214     UWORD16         u2_first_mb;
    215     UWORD16         u2_num_skipped_mbs;
    216 
    217     UWORD8          *pu1_inv_scan_matrix;
    218 
    219     UWORD16         u2_progressive_sequence;
    220     e_pic_type_t         e_pic_type;
    221 
    222     UWORD16         u2_full_pel_forw_vector;
    223     UWORD16         u2_forw_f_code;
    224     UWORD16         u2_full_pel_back_vector;
    225     UWORD16         u2_back_f_code;
    226 
    227     WORD16          ai2_mv[2][2][2]; /* Motion vectors */
    228 
    229     /* Bitstream code present in Picture coding extension */
    230     UWORD16         au2_f_code[2][2];
    231     UWORD16         u2_intra_dc_precision;
    232     UWORD16         u2_picture_structure;
    233     UWORD16         u2_top_field_first;
    234     UWORD16         u2_frame_pred_frame_dct;
    235     UWORD16         u2_concealment_motion_vectors;
    236     UWORD16         u2_q_scale_type;
    237     UWORD16         u2_intra_vlc_format;
    238     UWORD16         u2_alternate_scan;
    239     UWORD16         u2_repeat_first_field;
    240     UWORD16         u2_progressive_frame;
    241 
    242 
    243     /* Bitstream code related to frame rate of the bitstream */
    244     UWORD16         u2_frame_rate_code;
    245     UWORD16         u2_frame_rate_extension_n;
    246     UWORD16         u2_frame_rate_extension_d;
    247     UWORD16         u2_framePeriod;   /* Frame period in milli seconds */
    248 
    249     /* Members related to display dimensions of bitstream */
    250     /* The size values may not be returned right now. But they are read */
    251     /* and can be returned if there is a requirement.                   */
    252     UWORD8          u1_video_format;
    253     UWORD8          u1_colour_description;
    254     UWORD8          u1_colour_primaries;
    255     UWORD8          u1_transfer_characteristics;
    256     UWORD8          u1_matrix_coefficients;
    257     UWORD16         u2_display_horizontal_size;
    258     UWORD16         u2_display_vertical_size;
    259     UWORD16         u2_aspect_ratio_info;
    260 
    261     /* Members related to motion compensation */
    262     yuv_buf_t       s_mc_fw_buf;
    263     yuv_buf_t       s_mc_bk_buf;
    264     yuv_buf_t       s_mc_buf;
    265     mb_mc_params_t  as_mb_mc_params[2][2];
    266     yuv_buf_t       as_ref_buf[2][2];
    267     e_mb_type_t       s_mb_type;
    268 
    269     yuv_buf_t       s_dest_buf;
    270 
    271     /* Variable to handle intra MB */
    272     UWORD16         u2_prev_intra_mb;
    273     UWORD16         u2_coded_mb;
    274 
    275     /* Bidirect function pointers */
    276     const struct _dec_mb_params_t *ps_func_bi_direct;
    277 
    278     /* Forw or Back function pointers */
    279     const struct _dec_mb_params_t *ps_func_forw_or_back;
    280 
    281 
    282     /* CBP of the current MB        */
    283     UWORD16         u2_cbp;
    284     void            *pv_video_scratch;
    285 
    286 
    287     /* For global error handling */
    288     void            *pv_stack_cntxt;
    289 
    290 /* @ */
    291     WORD32          i4_chromaFormat;
    292     UWORD32         u4_xdmBufID;
    293     UWORD32         u4_num_mem_records;
    294     /* For holding memRecords */
    295     void            *pv_memTab;
    296 
    297     UWORD8          u1_flushfrm;
    298     UWORD8          u1_flushcnt;
    299     iv_yuv_buf_t    as_frame_buf[MAX_FRAME_BUFFER];
    300     iv_yuv_buf_t    ps_yuv_buf;
    301 
    302     ivd_get_display_frame_op_t  s_disp_op;
    303 
    304 
    305     UWORD32         u4_non_zero_cols;
    306     UWORD32         u4_non_zero_rows;
    307 
    308     UWORD32         u4_num_frames_decoded;
    309 
    310     /* Adding error code variable to signal benign errors. */
    311     UWORD32         u4_error_code;
    312 
    313     WORD32          i4_num_cores;
    314 
    315     UWORD8          u1_first_frame_done;
    316 
    317     void            *pv_codec_thread_handle;
    318     void            *ps_dec_state_multi_core;
    319     UWORD32         u4_inp_ts;
    320     pic_buf_t       *ps_cur_pic;
    321     pic_buf_t       *ps_disp_pic;
    322     pic_buf_t       *aps_ref_pics[2];
    323 
    324     WORD32          i4_disp_buf_id;
    325     WORD32          i4_cur_buf_id;
    326     iv_yuv_buf_t    *ps_disp_frm_buf;
    327 
    328     UWORD32         u4_share_disp_buf;
    329     void            *pv_pic_buf_base;
    330 
    331     disp_mgr_t      s_disp_mgr;
    332     UWORD8          *pu1_chroma_ref_buf[BUF_MGR_MAX_CNT];
    333     ivd_out_bufdesc_t as_disp_buffers[BUF_MGR_MAX_CNT];
    334 
    335     /* Count the number of pictures decoded after init/reset */
    336     WORD32          i4_pic_count;
    337 
    338     /* Flag to signal last coeff in a 8x8 block is one
    339     after mismatch contol */
    340     WORD32          i4_last_value_one;
    341 
    342     WORD32          i4_start_mb_y;
    343     WORD32          i4_end_mb_y;
    344 
    345     /* Buffer to store the input stream */
    346     UWORD8 *pu1_input_buffer;
    347 
    348     /**
    349      * Job queue buffer base
    350      */
    351     void            *pv_jobq_buf;
    352 
    353     /**
    354      * Job Queue mem tab size
    355      */
    356     WORD32          i4_jobq_buf_size;
    357 
    358     /**
    359      * Job Queue context
    360      */
    361     void            *pv_jobq;
    362 
    363     /* Pointer to input bitstream */
    364     UWORD8          *pu1_inp_bits_buf;
    365 
    366     /* Number of bytes in the input bitstream */
    367     UWORD32         u4_num_inp_bytes;
    368 
    369     ivd_out_bufdesc_t *ps_out_buf;
    370 
    371     /* Bytes consumed */
    372     WORD32          i4_bytes_consumed;
    373 
    374     IVD_ARCH_T      e_processor_arch;
    375 
    376     IVD_SOC_T       e_processor_soc;
    377 
    378     WORD32          i4_frame_decoded;
    379 
    380     /** Flag to enable deinterlace */
    381     UWORD32          u4_deinterlace;
    382 
    383     /** Deinterlacer context */
    384     void            *pv_deinterlacer_ctxt;
    385 
    386     /** Picture buffer held by deinterlacer */
    387     pic_buf_t       *ps_deint_pic;
    388 
    389     /** Buffer used after deinterlacer for format conversion */
    390     UWORD8          *pu1_deint_fmt_buf;
    391 
    392     /** Flag to indicate if Seq Display Extn is present */
    393     UWORD8          u1_seq_disp_extn_present;
    394 }dec_state_t;
    395 
    396 
    397 
    398 
    399 typedef void (*func_decmb_params)(dec_state_t *);
    400 typedef void  (*mc_funcs)(dec_state_t *);
    401 typedef struct _dec_mb_params_t
    402 {
    403     func_decmb_params    pf_func_mb_params;
    404     e_mb_type_t            s_mb_type;
    405     mc_funcs             pf_mc;
    406 }dec_mb_params_t;
    407 
    408 
    409 
    410 #define MAX_THREADS     4
    411 
    412 
    413 #define MAX_MB_ROWS     (MAX_HEIGHT / 16) // number of rows for 1080p
    414 
    415 typedef struct _dec_state_multi_core
    416 {
    417     // contains the decoder state of decoder for each thread
    418     dec_state_t *ps_dec_state[MAX_THREADS];
    419     UWORD32     au4_thread_launched[MAX_THREADS];
    420     // number of rows: first thread will populate the row offsets and update
    421     // row_offset_cnt. Other threads should pick up offset from this thread
    422     // and start decoding
    423     UWORD32     au4_row_offset[MAX_MB_ROWS];
    424     volatile    UWORD32 u4_row_offset_cnt;
    425 }dec_state_multi_core_t;
    426 
    427 
    428 
    429 #endif /* #ifndef __IMPEG2D_STRUCTS_H__ */
    430