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